2219cdef543f9278e511e2be769445ab4e9f70b4
[senf.git] / PPI / Mainpage.dox
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 /** \mainpage libPPI : The Packet Processing Infrastructure
22
23     The PPI provides an infrastructure to create packet oriented network processing applications. A
24     PPI application is built by combining processing modules in a very flexible manner.
25
26     \image html scenario.png Target Scenario
27     
28     The PPI concept is built around some key concepts
29
30     \li The PPI is based on processing \ref packets. It does not handle stream oriented channels.
31     \li The PPI is built around reusable \ref modules. Each module is completely independent.
32     \li Each module has an arbitrary number of \ref connectors, inputs and outputs.
33     \li The modules are connected to each other using flexible \ref connections.
34     \li Data flow throughout the network is governed via flexible automatic or manual \ref
35         throttling.
36     \li Modules may register additional external \ref events (file descriptor events or timers).
37     
38     The PPI thereby builds on the facilities provided by the other components of the SENF
39     framework. The target scenario above depicts a diffserv capable UDLR/ULE router including
40     performance optimizations for TCP traffic (PEP). This router is built by combining several
41     modules.
42
43     \see \ref overview \n
44         <a href="../../../Examples/RateStuffer/doc/html/index.html">PPI Example Application:
45         RateStuffer</a> \n
46         \ref senf::ppi::module "Modules" \n
47         \ref senf::ppi::connector "Connectors" \n
48         \ref event_group
49  */
50
51 /** \page overview PPI Overview and Concepts
52
53     \section design Design considerations
54
55     The PPI interface is designed to be as simple as possible. It provides sane defaults for all
56     configurable parameters to simplify getting started. It also automates all resource
57     management. Especially to simplify resource management, the PPI will take many configuration
58     objects by value. Even though this is not as efficient, it frees the user from most resource
59     management chores. This decision does not affect the runtime performance since it only affects
60     the configuration step.
61
62     \section packets Packets
63
64     The PPI processes packets and uses the <a href="@TOPDIR@/Packets/doc/html/index.html">Packet
65     library</a> to handle them. All packets are passed around as generic Packet::ptr's, the PPI
66     does not enforce any packet type restrictions.
67
68     \section modules Modules
69
70     A module is represented by a class type. Each module has several components:
71
72     \li It may have any number of connectors (inputs and outputs)
73     \li Each module declares flow information which details the route packets take within the
74         module. This information does not define how the information is processed, it only tells,
75         where data arriving on some input will be directed at.
76     \li The module might take additional parameters.
77     \li The module might also register additional events.
78
79     Modules are divided roughly in to two categories: I/O modules provide packet sources and sinks
80     (network connection, writing packets to disk, generating new packets) whereas processing modules
81     process packets internally.  In the target scenario, <em>TAP</em>, <em>ASI Out</em>, <em>Raw
82     Socket</em> and in a limited way <em>Generator</em> are I/O modules whereas <em>PEP</em>,
83     <em>DiffServ</em>, <em>DVB Enc</em>, <em>GRE/UDLR</em>, <em>TCP Filter</em> and <em>Stuffer</em>
84     are processing modules. <em>ASI/MPEG</em> and <em>Net</em> are external I/O ports which are
85     integrated via the <em>TAP</em>, <em>ASI Out</em> and <em>Raw Sock</em> modules using external
86     events.
87
88     The following example module declares three I/O connectors (see below): <tt>payload</tt>,
89     <tt>stuffing</tt> and <tt>output</tt>. These connectors are defined as <em>public</em> data
90     members so they can be accessed from the outside. This is important as we will see below.
91
92     \code
93       class RateStuffer
94           : public senf::ppi::module::Module
95       {
96           senf::ppi::IntervalTimer timer_;
97
98       public:
99           senf::ppi::connector::ActiveInput payload;
100           senf::ppi::connector::ActiveInput stuffing;
101           senf::ppi::connector::ActiveOutput output;
102
103           RateStuffer(unsigned packetsPerSecond)
104               : timer_(1000u, packetsPerSecond)
105           {
106               route(payload, output);
107               route(stuffing, output);
108
109               registerEvent(&RateStuffer::tick, timer_);
110           }
111
112       private:
113           void tick()
114           {
115               if (payload)
116                   output(payload());
117               else
118                   output(stuffing());
119           }
120       };
121     \endcode
122
123     On module instantiation, it will declare it's flow information with <tt>route</tt> (which is
124     inherited from <tt>senf::ppi::module::Module</tt>). Then the module registers an interval timer
125     which will fire <tt>packetsPerSecond</tt> times every <tt>1000</tt> milliseconds.
126
127     The processing of the module is very simple: Whenever a timer tick arrives a packet is sent. If
128     the <tt>payload</tt> input is ready (see throttling below), a payload packet is sent, otherwise
129     a stuffing packet is sent. The module will therefore provide a constant stream of packets at a
130     fixed rate on <tt>output</tt>
131     
132     An example module to generate the stuffing packets could be
133
134     \code
135       class CopyPacketGenerator
136           : public senf::ppi::module::Module
137       {
138       public:
139           senf::ppi::connector::PassiveOutput output;
140
141           CopyPacketGenerator(Packet::ptr template)
142               : template_ (template)
143           {
144               noroute(output);
145               output.onRequest(&CopyPacketGenerator::makePacket);
146           }
147
148       private:
149           Packet::ptr template_;
150
151           void makePacket()
152           {
153               output(template_.clone());
154           }
155       };
156     \endcode
157
158     This module just produces a copy of a given packet whenever output is requested.
159
160     \section connectors Connectors
161     
162     Inputs and Outputs can be active and passive. An \e active I/O is <em>activated by the
163     module</em> to send data or to poll for available packets. A \e passive I/O is <em>signaled by
164     the framework</em> to fetch data from the module or to pass data into the module.
165
166     To send or receive a packet (either actively or after packet reception has been signaled), the
167     module just calls the connector. This allows to generate or process multiple packets in one
168     iteration. However, reading will only succeed, as long as packets are available from the
169     connection.
170
171     Since a module is free to generate more than a single packet on incoming packet requests, all
172     input connectors incorporate a packet queue. This queue is exposed to the module and allows the
173     module to process packets in batches.
174
175     \section connections Connections
176
177     \image html ratestuffer.png Simple RateStuffer
178
179     To make use of the modules, they have to be instantiated and connections have to be created
180     between the I/O connectors. It is possible to connect any pair of input/output connectors as
181     long as one of them is active and the other is passive.
182     
183     It is possible to connect two active connectors with each other using a special adaptor
184     module. This Module has a passive input and a passive output. It will queue any incoming packets
185     and automatically handle throttling events (see below). This adaptor is automatically added by
186     the connect method if needed.
187
188     To complete our simplified example: Lets say we have an <tt>ActiveSocketInput</tt> and a
189     <tt>PassiveUdpOutput</tt> module. We can then use our <tt>RateStuffer</tt> module to build an
190     application which will create a fixed-rate UDP stream:
191
192     \code
193       RateStuffer rateStuffer (10);
194
195       senf::Packet::ptr stuffingPacket = senf::Packet::create<...>(...); 
196       CopyPacketGenerator generator (stuffingPacket);
197
198       senf::UDPv4ClientSocketHandle inputSocket (1111);
199       senf::ppi::module::ActiveSocketReader udpInput (inputSocket);
200
201       senf::UDPv4ClientSocketHandle outputSocket ("2.3.4.5:2222");
202       senf::ppi::module::PassiveSocketWriter udpOutput (outputSocket);
203
204       senf::ppi::module::PassiveQueue adaptor;
205
206       senf::ppi::connect(udpInput.output, adaptor.input);
207       senf::ppi::connect(adaptor.output, rateStuffer.payload);
208       adaptor.qdisc(ThresholdQueueing(10,5));
209       senf::ppi::connect(generator.output, rateStuffer.stuffing);
210       senf::ppi::connect(rateStuffer.output, udpOutput.input);
211
212       senf::ppi::run();
213     \endcode
214
215     First all necessary modules are created. Then the connections between these modules are set
216     up. The buffering on the udpInput <-> rateStuffer adaptor is changed so the queue will begin to
217     throttle only if more than 10 packets are in the queue. The connection will be unthrottled as
218     soon as there are no more than 5 packets left in the queue. This application will read
219     udp-packets coming in on port 1111 and will forward them to port 2222 on host 2.3.4.5 with a
220     fixed rate of 10 packets / second.
221
222     \section throttling Throttling
223
224     If a passive connector cannot handle incoming requests, this connector may be \e
225     throttled. Throttling a request will forward a throttle notification to the module connected
226     to that connector. The module then must handle this throttle notification. If automatic
227     throttling is enabled for the module (which is the default), the notification will automatically
228     be forwarded to all dependent connectors as taken from the flow information. For there it will
229     be forwarded to further modules and so on.
230
231     A throttle notification reaching an I/O module will normally disable the input/output by
232     disabling any external I/O events registered by the module. When the passive connector which
233     originated the notification becomes active again, it creates an unthrottle notification which
234     will be forwarded in the same way. This notification will re-enable any registered I/O events.
235
236     The above discussion shows, that throttle events are always generated on passive connectors and
237     received on active connectors. To differentiate further, the throttling originating from a
238     passive input is called <em>backward throttling</em> since it is forwarded in the direction \e
239     opposite to the data flow. Backward throttling notifications are sent towards the input
240     modules. On the other hand, the throttling originating from a passive output is called
241     <em>forward throttling</em> since it is forwarded along the \e same direction the data
242     is. Forward throttling notifications are therefore sent towards the output modules.
243
244     Since throttling a passive input may not disable all further packet delivery immediately, all
245     inputs contains an input queue. In it's default configuration, the queue will send out throttle
246     notifications when it becomes non-empty and unthrottle notifications when it becomes empty
247     again. This automatic behavior may however be disabled. This allows a module to collect incoming
248     packets in it's input queue before processing a bunch of them in one go.
249
250     \section events Events
251
252     Modules may register additional events. These external events are very important since they
253     drive the PPI framework. Possible event sources are
254     \li time based events
255     \li file descriptors.
256     \li internal events (e.g. IdleEvent)
257
258     Here some example code implementing the ActiveSocketInput Module:
259
260     \code
261       class ActiveSocketReader
262           : public senf::ppi::module::Module
263       {
264           typedef senf::ClientSocketHandle<
265               senf::MakeSocketPolicy< senf::ReadablePolicy,
266                                       senf::DatagramFramingPolicy > > SocketHandle;
267           SocketHandle socket_;
268           DataParser const & parser_;
269           senf::ppi:IOSignaler event_;
270
271           static PacketParser<senf::DataPacket> defaultParser_;
272
273       public:
274           senf::ppi::connector::ActiveOutput output;
275
276           // I hestitate taking parser by const & since a const & can be bound to
277           // a temporary even though a const & is all we need. The real implementation
278           // will probably make this a template arg. This simplifies the memory management
279           // from the users pov.
280           ActiveSocketReader(SocketHandle socket, 
281                              DataParser & parser = ActiveSocketReader::defaultParser_)
282               : socket_ (socket), 
283                 parser_ (parser)
284                 event_ (socket, senf::ppi::IOSignaler::Read)
285           {
286               registerEvent( &ActiveSocketReader::data, event_ );
287               route(event_, output);
288           }
289       
290       private:
291     
292           void data()
293           {
294               std::string data;
295               socket_.read(data);
296               output(parser_(data));
297           }
298       };
299     \endcode
300
301     First we declare our own socket handle type which allows us to read packets. The constructor
302     then takes two arguments: A compatible socket and a parser object. This parser object gets
303     passed the packet data as read from the socket (an \c std::string) and returns a
304     senf::Packet::ptr. The \c PacketParser is a simple parser which interprets the data as specified
305     by the template argument.
306
307     We register an IOSignaler event. This event will be signaled whenever the socket is
308     readable. This event is routed to the output. This routing automates throttling for the socket:
309     Whenever the output receives a throttle notifications, the event will be temporarily disabled.
310
311     Processing arriving packets happens in the \c data() member: This member simple reads a packet
312     from the socket. It passes this packet to the \c parser_ and sends the generated packet out.
313
314     \section flows Information Flow
315
316     The above description conceptually introduces three different flow levels:
317      
318     \li The <em>data flow</em> is, where the packets are flowing. This flow always goes from output
319         to input connector.
320     \li The <em>execution flow</em> describes the flow of execution from one module to another. This
321         flow always proceeds from active to passive connector.
322     \li The <em>control flow</em> is the flow of throttling notifications. This flow always proceeds
323         \e opposite to the execution flow, from passive to active connector.
324
325     This is the outside view, from without any module. These flows are set up using
326     senf::ppi::connect() statements.
327
328     Within a module, the different flow levels are defined differently depending on the type of
329     flow:
330     
331     \li The <em>data flow</em> is defined by how data is processed. The different event and
332         connector callbacks will pass packets around and thereby define the data flow
333     \li Likewise, the <em>execution flow</em> is defined parallel to the data flow (however possible
334         in opposite direction) by how the handler of one connector calls other connectors.
335     \li The <em>control flow</em> is set up using senf::ppi::Module::route statements (as long as
336         automatic throttling is used. Manual throttling defines the control flow within the
337         respective callbacks).
338
339     In nearly all cases, these flows will be parallel. Therefore it makes sense to define the \c
340     route statement as defining the 'conceptual data flow' since this is also how control messages
341     should flow (sans the direction, which is defined by the connectors active/passive property).
342
343     \see \ref ppi_implementation
344  */
345
346 /** \page ppi_implementation Implementation Notes
347     
348     \section processing Data Processing
349
350     The processing in the PPI is driven by events. Without events <em>nothing will happen</em>. When
351     an event is generated, the called module will probably call one of it's active connectors.
352
353     Calling an active connector will directly call the handler registered at the connected passive
354     connector. This way the call and data are handed across the connections until an I/O module will
355     finally handle the request (by not calling any other connectors).
356
357     Throttling is handled in the same way: Throttling a passive connector will call a corresponding
358     (internal) method of the connected active connector. This method will call registered handlers
359     and will analyze the routing information of the module for other (passive) connectors to call
360     and throttle. This will again create a call chain which terminates at the I/O modules. An event
361     which is called to be throttled will disable the event temporarily. Unthrottling works in the
362     same way.
363
364     This simple structure is complicated by the existence of the input queues. This affects both
365     data forwarding and throttling:
366     \li A data request will only be forwarded, if no data is available in the queue
367     \li The connection will only be throttled when the queue is empty
368     \li Handlers of passive input connectors must be called repeatedly until either the queue is
369         empty or the handler does not take any packets from the queue
370
371
372     \section logistics Managing the Data Structures
373
374     The PPI itself is a singleton. This simplifies many of the interfaces (We do not need to pass
375     the PPI instance). Should it be necessary to have several PPI systems working in parallel
376     (either by registering all events with the same event handler or by utilizing multiple threads),
377     we can still extend the API by adding an optional PPI instance argument.
378
379     Every module manages a collection of all it's connectors and every connector has a reference to
380     it's containing module. In addition, every connector maintains a collection of all it's routing
381     targets. 
382
383     All this data is initialized via the routing statements. This is, why \e every connector must
384     appear in at least one routing statement: These statements will as a side effect initialize the
385     connector with it's containing module.
386
387     Since all access to the PPI via the module is via it's base class, unbound member function
388     pointers can be provided as handler arguments: They will automatically be bound to the current
389     instance. This simplifies the PPI usage considerably. The same is true for the connectors: Since
390     they know the containing module, they can explicitly bind unbound member function pointers to
391     the instance.
392     
393
394     \section random_notes Random implementation notes
395     
396     Generation of throttle notifications: Backward throttling notifications are automatically
397     generated (if this is not disabled) whenever the input queue is non-empty \e after the event
398     handler has finished processing. Forward throttling notifications are not generated
399     automatically within the connector. However, the Passive-Passive adaptor will generate
400     Forward-throttling notifications whenever the input queue is empty.
401
402     \section class_diagram Class Diagram
403
404     \image html classes.png
405  */
406
407 \f
408 // Local Variables:
409 // mode: c++
410 // fill-column: 100
411 // c-file-style: "senf"
412 // indent-tabs-mode: nil
413 // ispell-local-dictionary: "american"
414 // mode: flyspell
415 // mode: auto-fill
416 // End:
417