eal: deprecate rte_snprintf
[dpdk.git] / lib / librte_pipeline / rte_pipeline.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef __INCLUDE_RTE_PIPELINE_H__
35 #define __INCLUDE_RTE_PIPELINE_H__
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 /**
42  * @file
43  * RTE Pipeline
44  *
45  * This tool is part of the Intel DPDK Packet Framework tool suite and provides
46  * a standard methodology (logically similar to OpenFlow) for rapid development
47  * of complex packet processing pipelines out of ports, tables and actions.
48  *
49  * <B>Basic operation.</B> A pipeline is constructed by connecting its input
50  * ports to its output ports through a chain of lookup tables. As result of
51  * lookup operation into the current table, one of the table entries (or the
52  * default table entry, in case of lookup miss) is identified to provide the
53  * actions to be executed on the current packet and the associated action
54  * meta-data. The behavior of user actions is defined through the configurable
55  * table action handler, while the reserved actions define the next hop for the
56  * current packet (either another table, an output port or packet drop) and are
57  * handled transparently by the framework.
58  *
59  * <B>Initialization and run-time flows.</B> Once all the pipeline elements
60  * (input ports, tables, output ports) have been created, input ports connected
61  * to tables, table action handlers configured, tables populated with the
62  * initial set of entries (actions and action meta-data) and input ports
63  * enabled, the pipeline runs automatically, pushing packets from input ports
64  * to tables and output ports. At each table, the identified user actions are
65  * being executed, resulting in action meta-data (stored in the table entry)
66  * and packet meta-data (stored with the packet descriptor) being updated. The
67  * pipeline tables can have further updates and input ports can be disabled or
68  * enabled later on as required.
69  *
70  * <B>Multi-core scaling.</B> Typically, each CPU core will run its own
71  * pipeline instance. Complex application-level pipelines can be implemented by
72  * interconnecting multiple CPU core-level pipelines in tree-like topologies,
73  * as the same port devices (e.g. SW rings) can serve as output ports for the
74  * pipeline running on CPU core A, as well as input ports for the pipeline
75  * running on CPU core B. This approach enables the application development
76  * using the pipeline (CPU cores connected serially), cluster/run-to-completion
77  * (CPU cores connected in parallel) or mixed (pipeline of CPU core clusters)
78  * programming models.
79  *
80  * <B>Thread safety.</B> It is possible to have multiple pipelines running on
81  * the same CPU core, but it is not allowed (for thread safety reasons) to have
82  * multiple CPU cores running the same pipeline instance.
83  *
84  ***/
85
86 #include <stdint.h>
87
88 #include <rte_mbuf.h>
89 #include <rte_port.h>
90 #include <rte_table.h>
91
92 /*
93  * Pipeline
94  *
95  */
96 /** Opaque data type for pipeline */
97 struct rte_pipeline;
98
99 /** Parameters for pipeline creation  */
100 struct rte_pipeline_params {
101         /** Pipeline name */
102         const char *name;
103
104         /** CPU socket ID where memory for the pipeline and its elements (ports
105         and tables) should be allocated */
106         int socket_id;
107
108         /** Offset within packet meta-data to port_id to be used by action
109         "Send packet to output port read from packet meta-data". Has to be
110         4-byte aligned. */
111         uint32_t offset_port_id;
112 };
113
114 /**
115  * Pipeline create
116  *
117  * @param params
118  *   Parameters for pipeline creation
119  * @return
120  *   Handle to pipeline instance on success or NULL otherwise
121  */
122 struct rte_pipeline *rte_pipeline_create(struct rte_pipeline_params *params);
123
124 /**
125  * Pipeline free
126  *
127  * @param p
128  *   Handle to pipeline instance
129  * @return
130  *   0 on success, error code otherwise
131  */
132 int rte_pipeline_free(struct rte_pipeline *p);
133
134 /**
135  * Pipeline consistency check
136  *
137  * @param p
138  *   Handle to pipeline instance
139  * @return
140  *   0 on success, error code otherwise
141  */
142 int rte_pipeline_check(struct rte_pipeline *p);
143
144 /**
145  * Pipeline run
146  *
147  * @param p
148  *   Handle to pipeline instance
149  * @return
150  *   0 on success, error code otherwise
151  */
152 int rte_pipeline_run(struct rte_pipeline *p);
153
154 /**
155  * Pipeline flush
156  *
157  * @param p
158  *   Handle to pipeline instance
159  * @return
160  *   0 on success, error code otherwise
161  */
162 int rte_pipeline_flush(struct rte_pipeline *p);
163
164 /*
165  * Actions
166  *
167  */
168 /** Reserved actions */
169 enum rte_pipeline_action {
170         /** Drop the packet */
171         RTE_PIPELINE_ACTION_DROP = 0,
172
173         /** Send packet to output port */
174         RTE_PIPELINE_ACTION_PORT,
175
176         /** Send packet to output port read from packet meta-data */
177         RTE_PIPELINE_ACTION_PORT_META,
178
179         /** Send packet to table */
180         RTE_PIPELINE_ACTION_TABLE,
181
182         /** Number of reserved actions */
183         RTE_PIPELINE_ACTIONS
184 };
185
186 /*
187  * Table
188  *
189  */
190 /** Maximum number of tables allowed for any given pipeline instance. The
191         value of this parameter cannot be changed. */
192 #define RTE_PIPELINE_TABLE_MAX                                     64
193
194 /**
195  * Head format for the table entry of any pipeline table. For any given
196  * pipeline table, all table entries should have the same size and format. For
197  * any given pipeline table, the table entry has to start with a head of this
198  * structure, which contains the reserved actions and their associated
199  * meta-data, and then optionally continues with user actions and their
200  * associated meta-data. As all the currently defined reserved actions are
201  * mutually exclusive, only one reserved action can be set per table entry.
202  */
203 struct rte_pipeline_table_entry {
204         /** Reserved action */
205         enum rte_pipeline_action action;
206
207         union {
208                 /** Output port ID (meta-data for "Send packet to output port"
209                 action) */
210                 uint32_t port_id;
211                 /** Table ID (meta-data for "Send packet to table" action) */
212                 uint32_t table_id;
213         };
214         /** Start of table entry area for user defined actions and meta-data */
215         uint8_t action_data[0];
216 };
217
218 /**
219  * Pipeline table action handler on lookup hit
220  *
221  * The action handler can decide to drop packets by resetting the associated
222  * packet bit in the pkts_mask parameter. In this case, the action handler is
223  * required not to free the packet buffer, which will be freed eventually by
224  * the pipeline.
225  *
226  * @param pkts
227  *   Burst of input packets specified as array of up to 64 pointers to struct
228  *   rte_mbuf
229  * @param pkts_mask
230  *   64-bit bitmask specifying which packets in the input burst are valid. When
231  *   pkts_mask bit n is set, then element n of pkts array is pointing to a
232  *   valid packet and element n of entries array is pointing to a valid table
233  *   entry associated with the packet, with the association typically done by
234  *   the table lookup operation. Otherwise, element n of pkts array and element
235  *   n of entries array will not be accessed.
236  * @param entries
237  *   Set of table entries specified as array of up to 64 pointers to struct
238  *   rte_pipeline_table_entry
239  * @param arg
240  *   Opaque parameter registered by the user at the pipeline table creation
241  *   time
242  * @return
243  *   0 on success, error code otherwise
244  */
245 typedef int (*rte_pipeline_table_action_handler_hit)(
246         struct rte_mbuf **pkts,
247         uint64_t *pkts_mask,
248         struct rte_pipeline_table_entry **entries,
249         void *arg);
250
251 /**
252  * Pipeline table action handler on lookup miss
253  *
254  * The action handler can decide to drop packets by resetting the associated
255  * packet bit in the pkts_mask parameter. In this case, the action handler is
256  * required not to free the packet buffer, which will be freed eventually by
257  * the pipeline.
258  *
259  * @param pkts
260  *   Burst of input packets specified as array of up to 64 pointers to struct
261  *   rte_mbuf
262  * @param pkts_mask
263  *   64-bit bitmask specifying which packets in the input burst are valid. When
264  *   pkts_mask bit n is set, then element n of pkts array is pointing to a
265  *   valid packet. Otherwise, element n of pkts array will not be accessed.
266  * @param entry
267  *   Single table entry associated with all the valid packets from the input
268  *   burst, specified as pointer to struct rte_pipeline_table_entry.
269  *   This entry is the pipeline table default entry that is associated by the
270  *   table lookup operation with the input packets that have resulted in lookup
271  *   miss.
272  * @param arg
273  *   Opaque parameter registered by the user at the pipeline table creation
274  *   time
275  * @return
276  *   0 on success, error code otherwise
277  */
278 typedef int (*rte_pipeline_table_action_handler_miss)(
279         struct rte_mbuf **pkts,
280         uint64_t *pkts_mask,
281         struct rte_pipeline_table_entry *entry,
282         void *arg);
283
284 /** Parameters for pipeline table creation. Action handlers have to be either
285     both enabled or both disabled (they can be disabled by setting them to
286     NULL). */
287 struct rte_pipeline_table_params {
288         /** Table operations (specific to each table type) */
289         struct rte_table_ops *ops;
290         /** Opaque param to be passed to the table create operation when
291         invoked */
292         void *arg_create;
293         /** Callback function to execute the user actions on input packets in
294         case of lookup hit */
295         rte_pipeline_table_action_handler_hit f_action_hit;
296         /** Callback function to execute the user actions on input packets in
297         case of lookup miss */
298         rte_pipeline_table_action_handler_miss f_action_miss;
299
300         /** Opaque parameter to be passed to lookup hit and/or lookup miss
301         action handlers when invoked */
302         void *arg_ah;
303         /** Memory size to be reserved per table entry for storing the user
304         actions and their meta-data */
305         uint32_t action_data_size;
306 };
307
308 /**
309  * Pipeline table create
310  *
311  * @param p
312  *   Handle to pipeline instance
313  * @param params
314  *   Parameters for pipeline table creation
315  * @param table_id
316  *   Table ID. Valid only within the scope of table IDs of the current
317  *   pipeline. Only returned after a successful invocation.
318  * @return
319  *   0 on success, error code otherwise
320  */
321 int rte_pipeline_table_create(struct rte_pipeline *p,
322         struct rte_pipeline_table_params *params,
323         uint32_t *table_id);
324
325 /**
326  * Pipeline table default entry add
327  *
328  * The contents of the table default entry is updated with the provided actions
329  * and meta-data. When the default entry is not configured (by using this
330  * function), the built-in default entry has the action "Drop" and meta-data
331  * set to all-zeros.
332  *
333  * @param p
334  *   Handle to pipeline instance
335  * @param table_id
336  *   Table ID (returned by previous invocation of pipeline table create)
337  * @param default_entry
338  *   New contents for the table default entry
339  * @param default_entry_ptr
340  *   On successful invocation, pointer to the default table entry which can be
341  *   used for further read-write accesses to this table entry. This pointer
342  *   is valid until the default entry is deleted or re-added.
343  * @return
344  *   0 on success, error code otherwise
345  */
346 int rte_pipeline_table_default_entry_add(struct rte_pipeline *p,
347         uint32_t table_id,
348         struct rte_pipeline_table_entry *default_entry,
349         struct rte_pipeline_table_entry **default_entry_ptr);
350
351 /**
352  * Pipeline table default entry delete
353  *
354  * The new contents of the table default entry is set to reserved action "Drop
355  * the packet" with meta-data cleared (i.e. set to all-zeros).
356  *
357  * @param p
358  *   Handle to pipeline instance
359  * @param table_id
360  *   Table ID (returned by previous invocation of pipeline table create)
361  * @param entry
362  *   On successful invocation, when entry points to a valid buffer, the
363  *   previous contents of the table default entry (as it was just before the
364  *   delete operation) is copied to this buffer
365  * @return
366  *   0 on success, error code otherwise
367  */
368 int rte_pipeline_table_default_entry_delete(struct rte_pipeline *p,
369         uint32_t table_id,
370         struct rte_pipeline_table_entry *entry);
371
372 /**
373  * Pipeline table entry add
374  *
375  * @param p
376  *   Handle to pipeline instance
377  * @param table_id
378  *   Table ID (returned by previous invocation of pipeline table create)
379  * @param key
380  *   Table entry key
381  * @param entry
382  *   New contents for the table entry identified by key
383  * @param key_found
384  *   On successful invocation, set to TRUE (value different than 0) if key was
385  *   already present in the table before the add operation and to FALSE (value
386  *   0) if not
387  * @param entry_ptr
388  *   On successful invocation, pointer to the table entry associated with key.
389  *   This can be used for further read-write accesses to this table entry and
390  *   is valid until the key is deleted from the table or re-added (usually for
391  *   associating different actions and/or action meta-data to the current key)
392  * @return
393  *   0 on success, error code otherwise
394  */
395 int rte_pipeline_table_entry_add(struct rte_pipeline *p,
396         uint32_t table_id,
397         void *key,
398         struct rte_pipeline_table_entry *entry,
399         int *key_found,
400         struct rte_pipeline_table_entry **entry_ptr);
401
402 /**
403  * Pipeline table entry delete
404  *
405  * @param p
406  *   Handle to pipeline instance
407  * @param table_id
408  *   Table ID (returned by previous invocation of pipeline table create)
409  * @param key
410  *   Table entry key
411  * @param key_found
412  *   On successful invocation, set to TRUE (value different than 0) if key was
413  *   found in the table before the delete operation and to FALSE (value 0) if
414  *   not
415  * @param entry
416  *   On successful invocation, when key is found in the table and entry points
417  *   to a valid buffer, the table entry contents (as it was before the delete
418  *   was performed) is copied to this buffer
419  * @return
420  *   0 on success, error code otherwise
421  */
422 int rte_pipeline_table_entry_delete(struct rte_pipeline *p,
423         uint32_t table_id,
424         void *key,
425         int *key_found,
426         struct rte_pipeline_table_entry *entry);
427
428 /*
429  * Port IN
430  *
431  */
432 /** Maximum number of input ports allowed for any given pipeline instance. The
433         value of this parameter cannot be changed. */
434 #define RTE_PIPELINE_PORT_IN_MAX                                    64
435
436 /**
437  * Pipeline input port action handler
438  *
439  * The action handler can decide to drop packets by resetting the associated
440  * packet bit in the pkts_mask parameter. In this case, the action handler is
441  * required not to free the packet buffer, which will be freed eventually by
442  * the pipeline.
443  *
444  * @param pkts
445  *   Burst of input packets specified as array of up to 64 pointers to struct
446  *   rte_mbuf
447  * @param n
448  *   Number of packets in the input burst. This parameter specifies that
449  *   elements 0 to (n-1) of pkts array are valid.
450  * @param pkts_mask
451  *   64-bit bitmask specifying which packets in the input burst are still valid
452  *   after the action handler is executed. When pkts_mask bit n is set, then
453  *   element n of pkts array is pointing to a valid packet.
454  * @param arg
455  *   Opaque parameter registered by the user at the pipeline table creation
456  *   time
457  * @return
458  *   0 on success, error code otherwise
459  */
460 typedef int (*rte_pipeline_port_in_action_handler)(
461         struct rte_mbuf **pkts,
462         uint32_t n,
463         uint64_t *pkts_mask,
464         void *arg);
465
466 /** Parameters for pipeline input port creation */
467 struct rte_pipeline_port_in_params {
468         /** Input port operations (specific to each table type) */
469         struct rte_port_in_ops *ops;
470         /** Opaque parameter to be passed to create operation when invoked */
471         void *arg_create;
472
473         /** Callback function to execute the user actions on input packets.
474                 Disabled if set to NULL. */
475         rte_pipeline_port_in_action_handler f_action;
476         /** Opaque parameter to be passed to the action handler when invoked */
477         void *arg_ah;
478
479         /** Recommended burst size for the RX operation(in number of pkts) */
480         uint32_t burst_size;
481 };
482
483 /**
484  * Pipeline input port create
485  *
486  * @param p
487  *   Handle to pipeline instance
488  * @param params
489  *   Parameters for pipeline input port creation
490  * @param port_id
491  *   Input port ID. Valid only within the scope of input port IDs of the
492  *   current pipeline. Only returned after a successful invocation.
493  * @return
494  *   0 on success, error code otherwise
495  */
496 int rte_pipeline_port_in_create(struct rte_pipeline *p,
497         struct rte_pipeline_port_in_params *params,
498         uint32_t *port_id);
499
500 /**
501  * Pipeline input port connect to table
502  *
503  * @param p
504  *   Handle to pipeline instance
505  * @param port_id
506  *   Port ID (returned by previous invocation of pipeline input port create)
507  * @param table_id
508  *   Table ID (returned by previous invocation of pipeline table create)
509  * @return
510  *   0 on success, error code otherwise
511  */
512 int rte_pipeline_port_in_connect_to_table(struct rte_pipeline *p,
513         uint32_t port_id,
514         uint32_t table_id);
515
516 /**
517  * Pipeline input port enable
518  *
519  * @param p
520  *   Handle to pipeline instance
521  * @param port_id
522  *   Port ID (returned by previous invocation of pipeline input port create)
523  * @return
524  *   0 on success, error code otherwise
525  */
526 int rte_pipeline_port_in_enable(struct rte_pipeline *p,
527         uint32_t port_id);
528
529 /**
530  * Pipeline input port disable
531  *
532  * @param p
533  *   Handle to pipeline instance
534  * @param port_id
535  *   Port ID (returned by previous invocation of pipeline input port create)
536  * @return
537  *   0 on success, error code otherwise
538  */
539 int rte_pipeline_port_in_disable(struct rte_pipeline *p,
540         uint32_t port_id);
541
542 /*
543  * Port OUT
544  *
545  */
546 /** Maximum number of output ports allowed for any given pipeline instance. The
547         value of this parameter cannot be changed. */
548 #define RTE_PIPELINE_PORT_OUT_MAX                                   64
549
550 /**
551  * Pipeline output port action handler for single packet
552  *
553  * The action handler can decide to drop packets by resetting the pkt_mask
554  * argument. In this case, the action handler is required not to free the
555  * packet buffer, which will be freed eventually by the pipeline.
556  *
557  * @param pkt
558  *   Input packet
559  * @param pkt_mask
560  *   Output argument set to 0 when the action handler decides to drop the input
561  *   packet and to 1LLU otherwise
562  * @param arg
563  *   Opaque parameter registered by the user at the pipeline table creation
564  *   time
565  * @return
566  *   0 on success, error code otherwise
567  */
568 typedef int (*rte_pipeline_port_out_action_handler)(
569         struct rte_mbuf *pkt,
570         uint64_t *pkt_mask,
571         void *arg);
572
573 /**
574  * Pipeline output port action handler bulk
575  *
576  * The action handler can decide to drop packets by resetting the associated
577  * packet bit in the pkts_mask parameter. In this case, the action handler is
578  * required not to free the packet buffer, which will be freed eventually by
579  * the pipeline.
580  *
581  * @param pkts
582  *   Burst of input packets specified as array of up to 64 pointers to struct
583  *   rte_mbuf
584  * @param pkts_mask
585  *   64-bit bitmask specifying which packets in the input burst are valid. When
586  *   pkts_mask bit n is set, then element n of pkts array is pointing to a
587  *   valid packet. Otherwise, element n of pkts array will not be accessed.
588  * @param arg
589  *   Opaque parameter registered by the user at the pipeline table creation
590  *   time
591  * @return
592  *   0 on success, error code otherwise
593  */
594 typedef int (*rte_pipeline_port_out_action_handler_bulk)(
595         struct rte_mbuf **pkts,
596         uint64_t *pkts_mask,
597         void *arg);
598
599 /** Parameters for pipeline output port creation. The action handlers have to
600 be either both enabled or both disabled (by setting them to NULL). When
601 enabled, the pipeline selects between them at different moments, based on the
602 number of packets that have to be sent to the same output port. */
603 struct rte_pipeline_port_out_params {
604         /** Output port operations (specific to each table type) */
605         struct rte_port_out_ops *ops;
606         /** Opaque parameter to be passed to create operation when invoked */
607         void *arg_create;
608
609         /** Callback function executing the user actions on single input
610         packet */
611         rte_pipeline_port_out_action_handler f_action;
612         /** Callback function executing the user actions on bust of input
613         packets */
614         rte_pipeline_port_out_action_handler_bulk f_action_bulk;
615         /** Opaque parameter to be passed to the action handler when invoked */
616         void *arg_ah;
617 };
618
619 /**
620  * Pipeline output port create
621  *
622  * @param p
623  *   Handle to pipeline instance
624  * @param params
625  *   Parameters for pipeline output port creation
626  * @param port_id
627  *   Output port ID. Valid only within the scope of output port IDs of the
628  *   current pipeline. Only returned after a successful invocation.
629  * @return
630  *   0 on success, error code otherwise
631  */
632 int rte_pipeline_port_out_create(struct rte_pipeline *p,
633         struct rte_pipeline_port_out_params *params,
634         uint32_t *port_id);
635
636 /**
637  * Pipeline output port packet insert
638  *
639  * This function is called by the table action handler whenever it generates a
640  * new packet to be sent out though one of the pipeline output ports. This
641  * packet is not part of the burst of input packets read from any of the
642  * pipeline input ports, so it is not an element of the pkts array input
643  * parameter of the table action handler. This packet can be dropped by the
644  * output port action handler.
645  *
646  * @param p
647  *   Handle to pipeline instance
648  * @param port_id
649  *   Output port ID (returned by previous invocation of pipeline output port
650  *   create) to send the packet specified by pkt
651  * @param pkt
652  *   New packet generated by the table action handler
653  * @return
654  *   0 on success, error code otherwise
655  */
656 int rte_pipeline_port_out_packet_insert(struct rte_pipeline *p,
657         uint32_t port_id,
658         struct rte_mbuf *pkt);
659
660 #ifdef __cplusplus
661 }
662 #endif
663
664 #endif