pipeline: add NAT action
[dpdk.git] / lib / librte_pipeline / rte_table_action.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #ifndef __INCLUDE_RTE_TABLE_ACTION_H__
6 #define __INCLUDE_RTE_TABLE_ACTION_H__
7
8 /**
9  * @file
10  * RTE Pipeline Table Actions
11  *
12  * This API provides a common set of actions for pipeline tables to speed up
13  * application development.
14  *
15  * Each match-action rule added to a pipeline table has associated data that
16  * stores the action context. This data is input to the table action handler
17  * called for every input packet that hits the rule as part of the table lookup
18  * during the pipeline execution. The pipeline library allows the user to define
19  * his own table actions by providing customized table action handlers (table
20  * lookup) and complete freedom of setting the rules and their data (table rule
21  * add/delete). While the user can still follow this process, this API is
22  * intended to provide a quicker development alternative for a set of predefined
23  * actions.
24  *
25  * The typical steps to use this API are:
26  *  - Define a table action profile. This is a configuration template that can
27  *    potentially be shared by multiple tables from the same or different
28  *    pipelines, with different tables from the same pipeline likely to use
29  *    different action profiles. For every table using a given action profile,
30  *    the profile defines the set of actions and the action configuration to be
31  *    implemented for all the table rules. API functions:
32  *    rte_table_action_profile_create(),
33  *    rte_table_action_profile_action_register(),
34  *    rte_table_action_profile_freeze().
35  *
36  *  - Instantiate the table action profile to create table action objects. Each
37  *    pipeline table has its own table action object. API functions:
38  *    rte_table_action_create().
39  *
40  *  - Use the table action object to generate the pipeline table action handlers
41  *    (invoked by the pipeline table lookup operation). API functions:
42  *    rte_table_action_table_params_get().
43  *
44  *  - Use the table action object to generate the rule data (for the pipeline
45  *    table rule add operation) based on given action parameters. API functions:
46  *    rte_table_action_apply().
47  *
48  *  - Use the table action object to read action data (e.g. stats counters) for
49  *    any given rule. API functions: rte_table_action_XYZ_read().
50  *
51  * @warning
52  * @b EXPERIMENTAL: this API may change without prior notice
53  */
54
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58
59 #include <stdint.h>
60
61 #include <rte_compat.h>
62 #include <rte_ether.h>
63 #include <rte_meter.h>
64
65 #include "rte_pipeline.h"
66
67 /** Table actions. */
68 enum rte_table_action_type {
69         /** Forward to next pipeline table, output port or drop. */
70         RTE_TABLE_ACTION_FWD = 0,
71
72         /**  Traffic Metering and Policing. */
73         RTE_TABLE_ACTION_MTR,
74
75         /**  Traffic Management. */
76         RTE_TABLE_ACTION_TM,
77
78         /** Packet encapsulations. */
79         RTE_TABLE_ACTION_ENCAP,
80
81         /** Network Address Translation (NAT). */
82         RTE_TABLE_ACTION_NAT,
83 };
84
85 /** Common action configuration (per table action profile). */
86 struct rte_table_action_common_config {
87         /** Input packet Internet Protocol (IP) version. Non-zero for IPv4, zero
88          * for IPv6.
89          */
90         int ip_version;
91
92         /** IP header offset within the input packet buffer. Offset 0 points to
93          * the first byte of the MBUF structure.
94          */
95         uint32_t ip_offset;
96 };
97
98 /**
99  * RTE_TABLE_ACTION_FWD
100  */
101 /** Forward action parameters (per table rule). */
102 struct rte_table_action_fwd_params {
103         /** Forward action. */
104         enum rte_pipeline_action action;
105
106         /** Pipeline table ID or output port ID. */
107         uint32_t id;
108 };
109
110 /**
111  * RTE_TABLE_ACTION_MTR
112  */
113 /** Max number of traffic classes (TCs). */
114 #define RTE_TABLE_ACTION_TC_MAX                                  4
115
116 /** Max number of queues per traffic class. */
117 #define RTE_TABLE_ACTION_TC_QUEUE_MAX                            4
118
119 /** Differentiated Services Code Point (DSCP) translation table entry. */
120 struct rte_table_action_dscp_table_entry {
121         /** Traffic class. Used by the meter or the traffic management actions.
122          * Has to be strictly smaller than *RTE_TABLE_ACTION_TC_MAX*. Traffic
123          * class 0 is the highest priority.
124          */
125         uint32_t tc_id;
126
127         /** Traffic class queue. Used by the traffic management action. Has to
128          * be strictly smaller than *RTE_TABLE_ACTION_TC_QUEUE_MAX*.
129          */
130         uint32_t tc_queue_id;
131
132         /** Packet color. Used by the meter action as the packet input color
133          * for the color aware mode of the traffic metering algorithm.
134          */
135         enum rte_meter_color color;
136 };
137
138 /** DSCP translation table. */
139 struct rte_table_action_dscp_table {
140         /** Array of DSCP table entries */
141         struct rte_table_action_dscp_table_entry entry[64];
142 };
143
144 /** Supported traffic metering algorithms. */
145 enum rte_table_action_meter_algorithm {
146         /** Single Rate Three Color Marker (srTCM) - IETF RFC 2697. */
147         RTE_TABLE_ACTION_METER_SRTCM,
148
149         /** Two Rate Three Color Marker (trTCM) - IETF RFC 2698. */
150         RTE_TABLE_ACTION_METER_TRTCM,
151 };
152
153 /** Traffic metering profile (configuration template). */
154 struct rte_table_action_meter_profile {
155         /** Traffic metering algorithm. */
156         enum rte_table_action_meter_algorithm alg;
157
158         RTE_STD_C11
159         union {
160                 /** Only valid when *alg* is set to srTCM - IETF RFC 2697. */
161                 struct rte_meter_srtcm_params srtcm;
162
163                 /** Only valid when *alg* is set to trTCM - IETF RFC 2698. */
164                 struct rte_meter_trtcm_params trtcm;
165         };
166 };
167
168 /** Policer actions. */
169 enum rte_table_action_policer {
170         /** Recolor the packet as green. */
171         RTE_TABLE_ACTION_POLICER_COLOR_GREEN = 0,
172
173         /** Recolor the packet as yellow. */
174         RTE_TABLE_ACTION_POLICER_COLOR_YELLOW,
175
176         /** Recolor the packet as red. */
177         RTE_TABLE_ACTION_POLICER_COLOR_RED,
178
179         /** Drop the packet. */
180         RTE_TABLE_ACTION_POLICER_DROP,
181
182         /** Number of policer actions. */
183         RTE_TABLE_ACTION_POLICER_MAX
184 };
185
186 /** Meter action configuration per traffic class. */
187 struct rte_table_action_mtr_tc_params {
188         /** Meter profile ID. */
189         uint32_t meter_profile_id;
190
191         /** Policer actions. */
192         enum rte_table_action_policer policer[e_RTE_METER_COLORS];
193 };
194
195 /** Meter action statistics counters per traffic class. */
196 struct rte_table_action_mtr_counters_tc {
197         /** Number of packets per color at the output of the traffic metering
198          * and before the policer actions are executed. Only valid when
199          * *n_packets_valid* is non-zero.
200          */
201         uint64_t n_packets[e_RTE_METER_COLORS];
202
203         /** Number of packet bytes per color at the output of the traffic
204          * metering and before the policer actions are executed. Only valid when
205          * *n_bytes_valid* is non-zero.
206          */
207         uint64_t n_bytes[e_RTE_METER_COLORS];
208
209         /** When non-zero, the *n_packets* field is valid. */
210         int n_packets_valid;
211
212         /** When non-zero, the *n_bytes* field is valid. */
213         int n_bytes_valid;
214 };
215
216 /** Meter action configuration (per table action profile). */
217 struct rte_table_action_mtr_config {
218         /** Meter algorithm. */
219         enum rte_table_action_meter_algorithm alg;
220
221         /** Number of traffic classes. Each traffic class has its own traffic
222          * meter and policer instances. Needs to be equal to either 1 or to
223          * *RTE_TABLE_ACTION_TC_MAX*.
224          */
225         uint32_t n_tc;
226
227         /** When non-zero, the *n_packets* meter stats counter is enabled,
228          * otherwise it is disabled.
229          *
230          * @see struct rte_table_action_mtr_counters_tc
231          */
232         int n_packets_enabled;
233
234         /** When non-zero, the *n_bytes* meter stats counter is enabled,
235          * otherwise it is disabled.
236          *
237          * @see struct rte_table_action_mtr_counters_tc
238          */
239         int n_bytes_enabled;
240 };
241
242 /** Meter action parameters (per table rule). */
243 struct rte_table_action_mtr_params {
244         /** Traffic meter and policer parameters for each of the *tc_mask*
245          * traffic classes.
246          */
247         struct rte_table_action_mtr_tc_params mtr[RTE_TABLE_ACTION_TC_MAX];
248
249         /** Bit mask defining which traffic class parameters are valid in *mtr*.
250          * If bit N is set in *tc_mask*, then parameters for traffic class N are
251          * valid in *mtr*.
252          */
253         uint32_t tc_mask;
254 };
255
256 /** Meter action statistics counters (per table rule). */
257 struct rte_table_action_mtr_counters {
258         /** Stats counters for each of the *tc_mask* traffic classes. */
259         struct rte_table_action_mtr_counters_tc stats[RTE_TABLE_ACTION_TC_MAX];
260
261         /** Bit mask defining which traffic class parameters are valid in *mtr*.
262          * If bit N is set in *tc_mask*, then parameters for traffic class N are
263          * valid in *mtr*.
264          */
265         uint32_t tc_mask;
266 };
267
268 /**
269  * RTE_TABLE_ACTION_TM
270  */
271 /** Traffic management action configuration (per table action profile). */
272 struct rte_table_action_tm_config {
273         /** Number of subports per port. */
274         uint32_t n_subports_per_port;
275
276         /** Number of pipes per subport. */
277         uint32_t n_pipes_per_subport;
278 };
279
280 /** Traffic management action parameters (per table rule). */
281 struct rte_table_action_tm_params {
282         /** Subport ID. */
283         uint32_t subport_id;
284
285         /** Pipe ID. */
286         uint32_t pipe_id;
287 };
288
289 /**
290  * RTE_TABLE_ACTION_ENCAP
291  */
292 /** Supported packet encapsulation types. */
293 enum rte_table_action_encap_type {
294         /** IP -> { Ether | IP } */
295         RTE_TABLE_ACTION_ENCAP_ETHER = 0,
296
297         /** IP -> { Ether | VLAN | IP } */
298         RTE_TABLE_ACTION_ENCAP_VLAN,
299
300         /** IP -> { Ether | S-VLAN | C-VLAN | IP } */
301         RTE_TABLE_ACTION_ENCAP_QINQ,
302
303         /** IP -> { Ether | MPLS | IP } */
304         RTE_TABLE_ACTION_ENCAP_MPLS,
305
306         /** IP -> { Ether | PPPoE | PPP | IP } */
307         RTE_TABLE_ACTION_ENCAP_PPPOE,
308 };
309
310 /** Pre-computed Ethernet header fields for encapsulation action. */
311 struct rte_table_action_ether_hdr {
312         struct ether_addr da; /**< Destination address. */
313         struct ether_addr sa; /**< Source address. */
314 };
315
316 /** Pre-computed VLAN header fields for encapsulation action. */
317 struct rte_table_action_vlan_hdr {
318         uint8_t pcp; /**< Priority Code Point (PCP). */
319         uint8_t dei; /**< Drop Eligibility Indicator (DEI). */
320         uint16_t vid; /**< VLAN Identifier (VID). */
321 };
322
323 /** Pre-computed MPLS header fields for encapsulation action. */
324 struct rte_table_action_mpls_hdr {
325         uint32_t label; /**< Label. */
326         uint8_t tc; /**< Traffic Class (TC). */
327         uint8_t ttl; /**< Time to Live (TTL). */
328 };
329
330 /** Pre-computed PPPoE header fields for encapsulation action. */
331 struct rte_table_action_pppoe_hdr {
332         uint16_t session_id; /**< Session ID. */
333 };
334
335 /** Ether encap parameters. */
336 struct rte_table_action_encap_ether_params {
337         struct rte_table_action_ether_hdr ether; /**< Ethernet header. */
338 };
339
340 /** VLAN encap parameters. */
341 struct rte_table_action_encap_vlan_params {
342         struct rte_table_action_ether_hdr ether; /**< Ethernet header. */
343         struct rte_table_action_vlan_hdr vlan; /**< VLAN header. */
344 };
345
346 /** QinQ encap parameters. */
347 struct rte_table_action_encap_qinq_params {
348         struct rte_table_action_ether_hdr ether; /**< Ethernet header. */
349         struct rte_table_action_vlan_hdr svlan; /**< Service VLAN header. */
350         struct rte_table_action_vlan_hdr cvlan; /**< Customer VLAN header. */
351 };
352
353 /** Max number of MPLS labels per output packet for MPLS encapsulation. */
354 #ifndef RTE_TABLE_ACTION_MPLS_LABELS_MAX
355 #define RTE_TABLE_ACTION_MPLS_LABELS_MAX                   4
356 #endif
357
358 /** MPLS encap parameters. */
359 struct rte_table_action_encap_mpls_params {
360         /** Ethernet header. */
361         struct rte_table_action_ether_hdr ether;
362
363         /** MPLS header. */
364         struct rte_table_action_mpls_hdr mpls[RTE_TABLE_ACTION_MPLS_LABELS_MAX];
365
366         /** Number of MPLS labels in MPLS header. */
367         uint32_t mpls_count;
368
369         /** Non-zero for MPLS unicast, zero for MPLS multicast. */
370         int unicast;
371 };
372
373 /** PPPoE encap parameters. */
374 struct rte_table_action_encap_pppoe_params {
375         struct rte_table_action_ether_hdr ether; /**< Ethernet header. */
376         struct rte_table_action_pppoe_hdr pppoe; /**< PPPoE/PPP headers. */
377 };
378
379 /** Encap action configuration (per table action profile). */
380 struct rte_table_action_encap_config {
381         /** Bit mask defining the set of packet encapsulations enabled for the
382          * current table action profile. If bit (1 << N) is set in *encap_mask*,
383          * then packet encapsulation N is enabled, otherwise it is disabled.
384          *
385          * @see enum rte_table_action_encap_type
386          */
387         uint64_t encap_mask;
388 };
389
390 /** Encap action parameters (per table rule). */
391 struct rte_table_action_encap_params {
392         /** Encapsulation type. */
393         enum rte_table_action_encap_type type;
394
395         RTE_STD_C11
396         union {
397                 /** Only valid when *type* is set to Ether. */
398                 struct rte_table_action_encap_ether_params ether;
399
400                 /** Only valid when *type* is set to VLAN. */
401                 struct rte_table_action_encap_vlan_params vlan;
402
403                 /** Only valid when *type* is set to QinQ. */
404                 struct rte_table_action_encap_qinq_params qinq;
405
406                 /** Only valid when *type* is set to MPLS. */
407                 struct rte_table_action_encap_mpls_params mpls;
408
409                 /** Only valid when *type* is set to PPPoE. */
410                 struct rte_table_action_encap_pppoe_params pppoe;
411         };
412 };
413
414 /**
415  * RTE_TABLE_ACTION_NAT
416  */
417 /** NAT action configuration (per table action profile). */
418 struct rte_table_action_nat_config {
419         /** When non-zero, the IP source address and L4 protocol source port are
420          * translated. When zero, the IP destination address and L4 protocol
421          * destination port are translated.
422          */
423         int source_nat;
424
425         /** Layer 4 protocol, for example TCP (0x06) or UDP (0x11). The checksum
426          * field is computed differently and placed at different header offset
427          * by each layer 4 protocol.
428          */
429         uint8_t proto;
430 };
431
432 /** NAT action parameters (per table rule). */
433 struct rte_table_action_nat_params {
434         /** IP version for *addr*: non-zero for IPv4, zero for IPv6. */
435         int ip_version;
436
437         /** IP address. */
438         union {
439                 /** IPv4 address; only valid when *ip_version* is non-zero. */
440                 uint32_t ipv4;
441
442                 /** IPv6 address; only valid when *ip_version* is set to 0. */
443                 uint8_t ipv6[16];
444         } addr;
445
446         /** Port. */
447         uint16_t port;
448 };
449
450 /**
451  * Table action profile.
452  */
453 struct rte_table_action_profile;
454
455 /**
456  * Table action profile create.
457  *
458  * @param[in] common
459  *   Common action configuration.
460  * @return
461  *   Table action profile handle on success, NULL otherwise.
462  */
463 struct rte_table_action_profile * __rte_experimental
464 rte_table_action_profile_create(struct rte_table_action_common_config *common);
465
466 /**
467  * Table action profile free.
468  *
469  * @param[in] profile
470  *   Table profile action handle (needs to be valid).
471  * @return
472  *   Zero on success, non-zero error code otherwise.
473  */
474 int __rte_experimental
475 rte_table_action_profile_free(struct rte_table_action_profile *profile);
476
477 /**
478  * Table action profile action register.
479  *
480  * @param[in] profile
481  *   Table profile action handle (needs to be valid and not in frozen state).
482  * @param[in] type
483  *   Specific table action to be registered for *profile*.
484  * @param[in] action_config
485  *   Configuration for the *type* action.
486  *   If struct rte_table_action_*type*_config is defined by the Table Action
487  *   API, it needs to point to a valid instance of this structure, otherwise it
488  *   needs to be set to NULL.
489  * @return
490  *   Zero on success, non-zero error code otherwise.
491  */
492 int __rte_experimental
493 rte_table_action_profile_action_register(struct rte_table_action_profile *profile,
494         enum rte_table_action_type type,
495         void *action_config);
496
497 /**
498  * Table action profile freeze.
499  *
500  * Once this function is called successfully, the given profile enters the
501  * frozen state with the following immediate effects: no more actions can be
502  * registered for this profile, so the profile can be instantiated to create
503  * table action objects.
504  *
505  * @param[in] profile
506  *   Table profile action handle (needs to be valid and not in frozen state).
507  * @return
508  *   Zero on success, non-zero error code otherwise.
509  *
510  * @see rte_table_action_create()
511  */
512 int __rte_experimental
513 rte_table_action_profile_freeze(struct rte_table_action_profile *profile);
514
515 /**
516  * Table action.
517  */
518 struct rte_table_action;
519
520 /**
521  * Table action create.
522  *
523  * Instantiates the given table action profile to create a table action object.
524  *
525  * @param[in] profile
526  *   Table profile action handle (needs to be valid and in frozen state).
527  * @param[in] socket_id
528  *   CPU socket ID where the internal data structures required by the new table
529  *   action object should be allocated.
530  * @return
531  *   Handle to table action object on success, NULL on error.
532  *
533  * @see rte_table_action_create()
534  */
535 struct rte_table_action * __rte_experimental
536 rte_table_action_create(struct rte_table_action_profile *profile,
537         uint32_t socket_id);
538
539 /**
540  * Table action free.
541  *
542  * @param[in] action
543  *   Handle to table action object (needs to be valid).
544  * @return
545  *   Zero on success, non-zero error code otherwise.
546  */
547 int __rte_experimental
548 rte_table_action_free(struct rte_table_action *action);
549
550 /**
551  * Table action table params get.
552  *
553  * @param[in] action
554  *   Handle to table action object (needs to be valid).
555  * @param[inout] params
556  *   Pipeline table parameters (needs to be pre-allocated).
557  * @return
558  *   Zero on success, non-zero error code otherwise.
559  */
560 int __rte_experimental
561 rte_table_action_table_params_get(struct rte_table_action *action,
562         struct rte_pipeline_table_params *params);
563
564 /**
565  * Table action apply.
566  *
567  * @param[in] action
568  *   Handle to table action object (needs to be valid).
569  * @param[in] data
570  *   Data byte array (typically table rule data) to apply action *type* on.
571  * @param[in] type
572  *   Specific table action previously registered for the table action profile of
573  *   the *action* object.
574  * @param[in] action_params
575  *   Parameters for the *type* action.
576  *   If struct rte_table_action_*type*_params is defined by the Table Action
577  *   API, it needs to point to a valid instance of this structure, otherwise it
578  *   needs to be set to NULL.
579  * @return
580  *   Zero on success, non-zero error code otherwise.
581  */
582 int __rte_experimental
583 rte_table_action_apply(struct rte_table_action *action,
584         void *data,
585         enum rte_table_action_type type,
586         void *action_params);
587
588 /**
589  * Table action DSCP table update.
590  *
591  * @param[in] action
592  *   Handle to table action object (needs to be valid).
593  * @param[in] dscp_mask
594  *   64-bit mask defining the DSCP table entries to be updated. If bit N is set
595  *   in this bit mask, then DSCP table entry N is to be updated, otherwise not.
596  * @param[in] table
597  *   DSCP table.
598  * @return
599  *   Zero on success, non-zero error code otherwise.
600  */
601 int __rte_experimental
602 rte_table_action_dscp_table_update(struct rte_table_action *action,
603         uint64_t dscp_mask,
604         struct rte_table_action_dscp_table *table);
605
606 /**
607  * Table action meter profile add.
608  *
609  * @param[in] action
610  *   Handle to table action object (needs to be valid).
611  * @param[in] meter_profile_id
612  *   Meter profile ID to be used for the *profile* once it is successfully added
613  *   to the *action* object (needs to be unused by the set of meter profiles
614  *   currently registered for the *action* object).
615  * @param[in] profile
616  *   Meter profile to be added.
617  * @return
618  *   Zero on success, non-zero error code otherwise.
619  */
620 int __rte_experimental
621 rte_table_action_meter_profile_add(struct rte_table_action *action,
622         uint32_t meter_profile_id,
623         struct rte_table_action_meter_profile *profile);
624
625 /**
626  * Table action meter profile delete.
627  *
628  * @param[in] action
629  *   Handle to table action object (needs to be valid).
630  * @param[in] meter_profile_id
631  *   Meter profile ID of the meter profile to be deleted from the *action*
632  *   object (needs to be valid for the *action* object).
633  * @return
634  *   Zero on success, non-zero error code otherwise.
635  */
636 int __rte_experimental
637 rte_table_action_meter_profile_delete(struct rte_table_action *action,
638         uint32_t meter_profile_id);
639
640 /**
641  * Table action meter read.
642  *
643  * @param[in] action
644  *   Handle to table action object (needs to be valid).
645  * @param[in] data
646  *   Data byte array (typically table rule data) with meter action previously
647  *   applied on it.
648  * @param[in] tc_mask
649  *   Bit mask defining which traffic classes should have the meter stats
650  *   counters read from *data* and stored into *stats*. If bit N is set in this
651  *   bit mask, then traffic class N is part of this operation, otherwise it is
652  *   not. If bit N is set in this bit mask, then traffic class N must be one of
653  *   the traffic classes that are enabled for the meter action in the table
654  *   action profile used by the *action* object.
655  * @param[inout] stats
656  *   When non-NULL, it points to the area where the meter stats counters read
657  *   from *data* are saved. Only the meter stats counters for the *tc_mask*
658  *   traffic classes are read and stored to *stats*.
659  * @param[in] clear
660  *   When non-zero, the meter stats counters are cleared (i.e. set to zero),
661  *   otherwise the counters are not modified. When the read operation is enabled
662  *   (*stats* is non-NULL), the clear operation is performed after the read
663  *   operation is completed.
664  * @return
665  *   Zero on success, non-zero error code otherwise.
666  */
667 int __rte_experimental
668 rte_table_action_meter_read(struct rte_table_action *action,
669         void *data,
670         uint32_t tc_mask,
671         struct rte_table_action_mtr_counters *stats,
672         int clear);
673
674 #ifdef __cplusplus
675 }
676 #endif
677
678 #endif /* __INCLUDE_RTE_TABLE_ACTION_H__ */