pipeline: add meter array to SWX
[dpdk.git] / lib / librte_pipeline / rte_swx_ctl.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2020 Intel Corporation
3  */
4 #ifndef __INCLUDE_RTE_SWX_CTL_H__
5 #define __INCLUDE_RTE_SWX_CTL_H__
6
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10
11 /**
12  * @file
13  * RTE SWX Pipeline Control
14  */
15
16 #include <stddef.h>
17 #include <stdint.h>
18 #include <stdio.h>
19
20 #include <rte_compat.h>
21 #include <rte_meter.h>
22
23 #include "rte_swx_port.h"
24 #include "rte_swx_table.h"
25
26 struct rte_swx_pipeline;
27
28 /** Name size. */
29 #ifndef RTE_SWX_CTL_NAME_SIZE
30 #define RTE_SWX_CTL_NAME_SIZE 64
31 #endif
32
33 /*
34  * Pipeline Query API.
35  */
36
37 /** Pipeline info. */
38 struct rte_swx_ctl_pipeline_info {
39         /** Number of input ports. */
40         uint32_t n_ports_in;
41
42         /** Number of input ports. */
43         uint32_t n_ports_out;
44
45         /** Number of actions. */
46         uint32_t n_actions;
47
48         /** Number of tables. */
49         uint32_t n_tables;
50
51         /** Number of register arrays. */
52         uint32_t n_regarrays;
53
54         /** Number of meter arrays. */
55         uint32_t n_metarrays;
56 };
57
58 /**
59  * Pipeline info get
60  *
61  * @param[in] p
62  *   Pipeline handle.
63  * @param[out] pipeline
64  *   Pipeline info.
65  * @return
66  *   0 on success or the following error codes otherwise:
67  *   -EINVAL: Invalid argument.
68  */
69 __rte_experimental
70 int
71 rte_swx_ctl_pipeline_info_get(struct rte_swx_pipeline *p,
72                               struct rte_swx_ctl_pipeline_info *pipeline);
73
74 /**
75  * Pipeline NUMA node get
76  *
77  * @param[in] p
78  *   Pipeline handle.
79  * @param[out] numa_node
80  *   Pipeline NUMA node.
81  * @return
82  *   0 on success or the following error codes otherwise:
83  *   -EINVAL: Invalid argument.
84  */
85 __rte_experimental
86 int
87 rte_swx_ctl_pipeline_numa_node_get(struct rte_swx_pipeline *p,
88                                    int *numa_node);
89
90 /*
91  * Ports Query API.
92  */
93
94 /**
95  * Input port statistics counters read
96  *
97  * @param[in] p
98  *   Pipeline handle.
99  * @param[in] port_id
100  *   Port ID (0 .. *n_ports_in* - 1).
101  * @param[out] stats
102  *   Input port stats.
103  * @return
104  *   0 on success or the following error codes otherwise:
105  *   -EINVAL: Invalid argument.
106  */
107 __rte_experimental
108 int
109 rte_swx_ctl_pipeline_port_in_stats_read(struct rte_swx_pipeline *p,
110                                         uint32_t port_id,
111                                         struct rte_swx_port_in_stats *stats);
112
113 /**
114  * Output port statistics counters read
115  *
116  * @param[in] p
117  *   Pipeline handle.
118  * @param[in] port_id
119  *   Port ID (0 .. *n_ports_out* - 1).
120  * @param[out] stats
121  *   Output port stats.
122  * @return
123  *   0 on success or the following error codes otherwise:
124  *   -EINVAL: Invalid argument.
125  */
126 __rte_experimental
127 int
128 rte_swx_ctl_pipeline_port_out_stats_read(struct rte_swx_pipeline *p,
129                                          uint32_t port_id,
130                                          struct rte_swx_port_out_stats *stats);
131
132 /*
133  * Action Query API.
134  */
135
136 /** Action info. */
137 struct rte_swx_ctl_action_info {
138         /** Action name. */
139         char name[RTE_SWX_CTL_NAME_SIZE];
140
141         /** Number of action arguments. */
142         uint32_t n_args;
143 };
144
145 /**
146  * Action info get
147  *
148  * @param[in] p
149  *   Pipeline handle.
150  * @param[in] action_id
151  *   Action ID (0 .. *n_actions* - 1).
152  * @param[out] action
153  *   Action info.
154  * @return
155  *   0 on success or the following error codes otherwise:
156  *   -EINVAL: Invalid argument.
157  */
158 __rte_experimental
159 int
160 rte_swx_ctl_action_info_get(struct rte_swx_pipeline *p,
161                             uint32_t action_id,
162                             struct rte_swx_ctl_action_info *action);
163
164 /** Action argument info. */
165 struct rte_swx_ctl_action_arg_info {
166         /** Action argument name. */
167         char name[RTE_SWX_CTL_NAME_SIZE];
168
169         /** Action argument size (in bits). */
170         uint32_t n_bits;
171 };
172
173 /**
174  * Action argument info get
175  *
176  * @param[in] p
177  *   Pipeline handle.
178  * @param[in] action_id
179  *   Action ID (0 .. *n_actions* - 1).
180  * @param[in] action_arg_id
181  *   Action ID (0 .. *n_args* - 1).
182  * @param[out] action_arg
183  *   Action argument info.
184  * @return
185  *   0 on success or the following error codes otherwise:
186  *   -EINVAL: Invalid argument.
187  */
188 __rte_experimental
189 int
190 rte_swx_ctl_action_arg_info_get(struct rte_swx_pipeline *p,
191                                 uint32_t action_id,
192                                 uint32_t action_arg_id,
193                                 struct rte_swx_ctl_action_arg_info *action_arg);
194
195 /*
196  * Table Query API.
197  */
198
199 /** Table info. */
200 struct rte_swx_ctl_table_info {
201         /** Table name. */
202         char name[RTE_SWX_CTL_NAME_SIZE];
203
204         /** Table creation arguments. */
205         char args[RTE_SWX_CTL_NAME_SIZE];
206
207         /** Number of match fields. */
208         uint32_t n_match_fields;
209
210         /** Number of actions. */
211         uint32_t n_actions;
212
213         /** Non-zero (true) when the default action is constant, therefore it
214          * cannot be changed; zero (false) when the default action not constant,
215          * therefore it can be changed.
216          */
217         int default_action_is_const;
218
219         /** Table size parameter. */
220         uint32_t size;
221 };
222
223 /**
224  * Table info get
225  *
226  * @param[in] p
227  *   Pipeline handle.
228  * @param[in] table_id
229  *   Table ID (0 .. *n_tables* - 1).
230  * @param[out] table
231  *   Table info.
232  * @return
233  *   0 on success or the following error codes otherwise:
234  *   -EINVAL: Invalid argument.
235  */
236 __rte_experimental
237 int
238 rte_swx_ctl_table_info_get(struct rte_swx_pipeline *p,
239                            uint32_t table_id,
240                            struct rte_swx_ctl_table_info *table);
241
242 /** Table match field info.
243  *
244  * If (n_bits, offset) are known for all the match fields of the table, then the
245  * table (key_offset, key_size, key_mask0) can be computed.
246  */
247 struct rte_swx_ctl_table_match_field_info {
248         /** Match type of the current match field. */
249         enum rte_swx_table_match_type match_type;
250
251         /** Non-zero (true) when the current match field is part of a registered
252          * header, zero (false) when it is part of the registered meta-data.
253          */
254         int is_header;
255
256         /** Match field size (in bits). */
257         uint32_t n_bits;
258
259         /** Match field offset within its parent struct (one of the headers or
260          * the meta-data).
261          */
262         uint32_t offset;
263 };
264
265 /**
266  * Table match field info get
267  *
268  * @param[in] p
269  *   Pipeline handle.
270  * @param[in] table_id
271  *   Table ID (0 .. *n_tables*).
272  * @param[in] match_field_id
273  *   Match field ID (0 .. *n_match_fields* - 1).
274  * @param[out] match_field
275  *   Table match field info.
276  * @return
277  *   0 on success or the following error codes otherwise:
278  *   -EINVAL: Invalid argument.
279  */
280 __rte_experimental
281 int
282 rte_swx_ctl_table_match_field_info_get(struct rte_swx_pipeline *p,
283         uint32_t table_id,
284         uint32_t match_field_id,
285         struct rte_swx_ctl_table_match_field_info *match_field);
286
287 /** Table action info. */
288 struct rte_swx_ctl_table_action_info {
289         /** Action ID. */
290         uint32_t action_id;
291 };
292
293 /**
294  * Table action info get
295  *
296  * @param[in] p
297  *   Pipeline handle.
298  * @param[in] table_id
299  *   Table ID (0 .. *n_tables*).
300  * @param[in] table_action_id
301  *   Action index within the set of table actions (0 .. table n_actions - 1).
302  *   Not to be confused with the action ID, which works at the pipeline level
303  *   (0 .. pipeline n_actions - 1), which is precisely what this function
304  *   returns as part of *table_action*.
305  * @param[out] table_action
306  *   Table action info.
307  * @return
308  *   0 on success or the following error codes otherwise:
309  *   -EINVAL: Invalid argument.
310  */
311 __rte_experimental
312 int
313 rte_swx_ctl_table_action_info_get(struct rte_swx_pipeline *p,
314         uint32_t table_id,
315         uint32_t table_action_id,
316         struct rte_swx_ctl_table_action_info *table_action);
317
318 /**
319  * Table operations get
320  *
321  * @param[in] p
322  *   Pipeline handle.
323  * @param[in] table_id
324  *   Table ID (0 .. *n_tables*).
325  * @param[out] table_ops
326  *   Table operations. Only valid when function returns success and *is_stub* is
327  *   zero (false).
328  * @param[out] is_stub
329  *   A stub table is a table with no match fields. No "regular" table entries
330  *   (i.e. entries other than the default entry) can be added to such a table,
331  *   therefore the lookup operation always results in lookup miss. Non-zero
332  *   (true) when the current table is a stub table, zero (false) otherwise.
333  * @return
334  *   0 on success or the following error codes otherwise:
335  *   -EINVAL: Invalid argument.
336  */
337 __rte_experimental
338 int
339 rte_swx_ctl_table_ops_get(struct rte_swx_pipeline *p,
340                           uint32_t table_id,
341                           struct rte_swx_table_ops *table_ops,
342                           int *is_stub);
343
344 /*
345  * Table Update API.
346  */
347
348 /** Table state. */
349 struct rte_swx_table_state {
350         /** Table object. */
351         void *obj;
352
353         /** Action ID of the table default action. */
354         uint64_t default_action_id;
355
356         /** Action data of the table default action. Ignored when the action
357          * data size is zero; otherwise, action data size bytes are meaningful.
358          */
359         uint8_t *default_action_data;
360 };
361
362 /**
363  * Pipeline table state get
364  *
365  * @param[in] p
366  *   Pipeline handle.
367  * @param[out] table_state
368  *   After successful execution, the *table_state* contains the pointer to the
369  *   current pipeline table state, which is an array of *n_tables* elements,
370  *   with array element i containing the state of the i-th pipeline table. The
371  *   pipeline continues to own all the data structures directly or indirectly
372  *   referenced by the *table_state* until the subsequent successful invocation
373  *   of function *rte_swx_pipeline_table_state_set*.
374  * @return
375  *   0 on success or the following error codes otherwise:
376  *   -EINVAL: Invalid argument.
377  */
378 __rte_experimental
379 int
380 rte_swx_pipeline_table_state_get(struct rte_swx_pipeline *p,
381                                  struct rte_swx_table_state **table_state);
382
383 /**
384  * Pipeline table state set
385  *
386  * @param[in] p
387  *   Pipeline handle.
388  * @param[out] table_state
389  *   After successful execution, the pipeline table state is updated to this
390  *   *table_state*. The ownership of all the data structures directly or
391  *   indirectly referenced by this *table_state* is passed from the caller to
392  *   the pipeline.
393  * @return
394  *   0 on success or the following error codes otherwise:
395  *   -EINVAL: Invalid argument.
396  */
397 __rte_experimental
398 int
399 rte_swx_pipeline_table_state_set(struct rte_swx_pipeline *p,
400                                  struct rte_swx_table_state *table_state);
401
402 /*
403  * High Level Reference Table Update API.
404  */
405
406 /** Pipeline control opaque data structure. */
407 struct rte_swx_ctl_pipeline;
408
409 /**
410  * Pipeline control create
411  *
412  * @param[in] p
413  *   Pipeline handle.
414  * @return
415  *   Pipeline control handle, on success, or NULL, on error.
416  */
417 __rte_experimental
418 struct rte_swx_ctl_pipeline *
419 rte_swx_ctl_pipeline_create(struct rte_swx_pipeline *p);
420
421 /**
422  * Pipeline table entry add
423  *
424  * Schedule entry for addition to table or update as part of the next commit
425  * operation.
426  *
427  * @param[in] ctl
428  *   Pipeline control handle.
429  * @param[in] table_name
430  *   Table name.
431  * @param[in] entry
432  *   Entry to be added to the table.
433  * @return
434  *   0 on success or the following error codes otherwise:
435  *   -EINVAL: Invalid argument.
436  */
437 __rte_experimental
438 int
439 rte_swx_ctl_pipeline_table_entry_add(struct rte_swx_ctl_pipeline *ctl,
440                                      const char *table_name,
441                                      struct rte_swx_table_entry *entry);
442
443 /**
444  * Pipeline table default entry add
445  *
446  * Schedule table default entry update as part of the next commit operation.
447  *
448  * @param[in] ctl
449  *   Pipeline control handle.
450  * @param[in] table_name
451  *   Table name.
452  * @param[in] entry
453  *   The new table default entry. The *key* and *key_mask* entry fields are
454  *   ignored.
455  * @return
456  *   0 on success or the following error codes otherwise:
457  *   -EINVAL: Invalid argument.
458  */
459 __rte_experimental
460 int
461 rte_swx_ctl_pipeline_table_default_entry_add(struct rte_swx_ctl_pipeline *ctl,
462                                              const char *table_name,
463                                              struct rte_swx_table_entry *entry);
464
465 /**
466  * Pipeline table entry delete
467  *
468  * Schedule entry for deletion from table as part of the next commit operation.
469  * Request is silently discarded if no such entry exists.
470  *
471  * @param[in] ctl
472  *   Pipeline control handle.
473  * @param[in] table_name
474  *   Table name.
475  * @param[in] entry
476  *   Entry to be deleted from the table. The *action_id* and *action_data* entry
477  *   fields are ignored.
478  * @return
479  *   0 on success or the following error codes otherwise:
480  *   -EINVAL: Invalid argument.
481  */
482 __rte_experimental
483 int
484 rte_swx_ctl_pipeline_table_entry_delete(struct rte_swx_ctl_pipeline *ctl,
485                                         const char *table_name,
486                                         struct rte_swx_table_entry *entry);
487
488 /**
489  * Pipeline commit
490  *
491  * Perform all the scheduled table work.
492  *
493  * @param[in] ctl
494  *   Pipeline control handle.
495  * @param[in] abort_on_fail
496  *   When non-zero (false), all the scheduled work is discarded after a failed
497  *   commit. Otherwise, the scheduled work is still kept pending for the next
498  *   commit.
499  * @return
500  *   0 on success or the following error codes otherwise:
501  *   -EINVAL: Invalid argument.
502  */
503 __rte_experimental
504 int
505 rte_swx_ctl_pipeline_commit(struct rte_swx_ctl_pipeline *ctl,
506                             int abort_on_fail);
507
508 /**
509  * Pipeline abort
510  *
511  * Discard all the scheduled table work.
512  *
513  * @param[in] ctl
514  *   Pipeline control handle.
515  */
516 __rte_experimental
517 void
518 rte_swx_ctl_pipeline_abort(struct rte_swx_ctl_pipeline *ctl);
519
520 /**
521  * Pipeline table entry read
522  *
523  * Read table entry from string.
524  *
525  * @param[in] ctl
526  *   Pipeline control handle.
527  * @param[in] table_name
528  *   Table name.
529  * @param[in] string
530  *   String containing the table entry.
531  * @param[out] is_blank_or_comment
532  *   On error, this argument provides an indication of whether *string* contains
533  *   an invalid table entry (set to zero) or a blank or comment line that should
534  *   typically be ignored (set to a non-zero value).
535  * @return
536  *   0 on success or the following error codes otherwise:
537  *   -EINVAL: Invalid argument.
538  */
539 __rte_experimental
540 struct rte_swx_table_entry *
541 rte_swx_ctl_pipeline_table_entry_read(struct rte_swx_ctl_pipeline *ctl,
542                                       const char *table_name,
543                                       const char *string,
544                                       int *is_blank_or_comment);
545
546 /**
547  * Pipeline table print to file
548  *
549  * Print all the table entries to file.
550  *
551  * @param[in] f
552  *   Output file.
553  * @param[in] ctl
554  *   Pipeline control handle.
555  * @param[in] table_name
556  *   Table name.
557  * @return
558  *   0 on success or the following error codes otherwise:
559  *   -EINVAL: Invalid argument.
560  */
561 __rte_experimental
562 int
563 rte_swx_ctl_pipeline_table_fprintf(FILE *f,
564                                    struct rte_swx_ctl_pipeline *ctl,
565                                    const char *table_name);
566
567 /*
568  * Register Array Query API.
569  */
570
571 /** Register array info. */
572 struct rte_swx_ctl_regarray_info {
573         /** Register array name. */
574         char name[RTE_SWX_CTL_NAME_SIZE];
575
576         /** Register array size. */
577         uint32_t size;
578 };
579
580 /**
581  * Register array info get
582  *
583  * @param[in] p
584  *   Pipeline handle.
585  * @param[in] regarray_id
586  *   Register array ID (0 .. *n_regarrays* - 1).
587  * @param[out] regarray
588  *   Register array info.
589  * @return
590  *   0 on success or the following error codes otherwise:
591  *   -EINVAL: Invalid argument.
592  */
593 __rte_experimental
594 int
595 rte_swx_ctl_regarray_info_get(struct rte_swx_pipeline *p,
596                               uint32_t regarray_id,
597                               struct rte_swx_ctl_regarray_info *regarray);
598
599 /**
600  * Register read
601  *
602  * @param[in] p
603  *   Pipeline handle.
604  * @param[in] regarray_name
605  *   Register array name.
606  * @param[in] regarray_index
607  *   Register index within the array (0 .. *size* - 1).
608  * @param[out] value
609  *   Current register value.
610  * @return
611  *   0 on success or the following error codes otherwise:
612  *   -EINVAL: Invalid argument.
613  */
614 __rte_experimental
615 int
616 rte_swx_ctl_pipeline_regarray_read(struct rte_swx_pipeline *p,
617                                    const char *regarray_name,
618                                    uint32_t regarray_index,
619                                    uint64_t *value);
620
621 /**
622  * Register write
623  *
624  * @param[in] p
625  *   Pipeline handle.
626  * @param[in] regarray_name
627  *   Register array name.
628  * @param[in] regarray_index
629  *   Register index within the array (0 .. *size* - 1).
630  * @param[in] value
631  *   Value to be written to the register.
632  * @return
633  *   0 on success or the following error codes otherwise:
634  *   -EINVAL: Invalid argument.
635  */
636 __rte_experimental
637 int
638 rte_swx_ctl_pipeline_regarray_write(struct rte_swx_pipeline *p,
639                                    const char *regarray_name,
640                                    uint32_t regarray_index,
641                                    uint64_t value);
642
643 /*
644  * Meter Array Query and Configuration API.
645  */
646
647 /** Meter array info. */
648 struct rte_swx_ctl_metarray_info {
649         /** Meter array name. */
650         char name[RTE_SWX_CTL_NAME_SIZE];
651
652         /** Meter array size. */
653         uint32_t size;
654 };
655
656 /**
657  * Meter array info get
658  *
659  * @param[in] p
660  *   Pipeline handle.
661  * @param[in] metarray_id
662  *   Meter array ID (0 .. *n_metarrays* - 1).
663  * @param[out] metarray
664  *   Meter array info.
665  * @return
666  *   0 on success or the following error codes otherwise:
667  *   -EINVAL: Invalid argument.
668  */
669 __rte_experimental
670 int
671 rte_swx_ctl_metarray_info_get(struct rte_swx_pipeline *p,
672                               uint32_t metarray_id,
673                               struct rte_swx_ctl_metarray_info *metarray);
674
675 /**
676  * Meter profile add
677  *
678  * @param[in] p
679  *   Pipeline handle.
680  * @param[in] name
681  *   Meter profile name.
682  * @param[in] params
683  *   Meter profile parameters.
684  * @return
685  *   0 on success or the following error codes otherwise:
686  *   -EINVAL: Invalid argument;
687  *   -ENOMEM: Not enough space/cannot allocate memory;
688  *   -EEXIST: Meter profile with this name already exists.
689  */
690 __rte_experimental
691 int
692 rte_swx_ctl_meter_profile_add(struct rte_swx_pipeline *p,
693                               const char *name,
694                               struct rte_meter_trtcm_params *params);
695
696 /**
697  * Meter profile delete
698  *
699  * @param[in] p
700  *   Pipeline handle.
701  * @param[in] name
702  *   Meter profile name.
703  * @return
704  *   0 on success or the following error codes otherwise:
705  *   -EINVAL: Invalid argument;
706  *   -EBUSY: Meter profile is currently in use.
707  */
708 __rte_experimental
709 int
710 rte_swx_ctl_meter_profile_delete(struct rte_swx_pipeline *p,
711                                  const char *name);
712
713 /**
714  * Meter reset
715  *
716  * Reset a meter within a given meter array to use the default profile that
717  * causes all the input packets to be colored as green. It is the responsibility
718  * of the control plane to make sure this meter is not used by the data plane
719  * pipeline before calling this function.
720  *
721  * @param[in] p
722  *   Pipeline handle.
723  * @param[in] metarray_name
724  *   Meter array name.
725  * @param[in] metarray_index
726  *   Meter index within the meter array.
727  * @return
728  *   0 on success or the following error codes otherwise:
729  *   -EINVAL: Invalid argument.
730  */
731 __rte_experimental
732 int
733 rte_swx_ctl_meter_reset(struct rte_swx_pipeline *p,
734                         const char *metarray_name,
735                         uint32_t metarray_index);
736
737 /**
738  * Meter set
739  *
740  * Set a meter within a given meter array to use a specific profile. It is the
741  * responsibility of the control plane to make sure this meter is not used by
742  * the data plane pipeline before calling this function.
743  *
744  * @param[in] p
745  *   Pipeline handle.
746  * @param[in] metarray_name
747  *   Meter array name.
748  * @param[in] metarray_index
749  *   Meter index within the meter array.
750  * @param[in] profile_name
751  *   Existing meter profile name.
752  * @return
753  *   0 on success or the following error codes otherwise:
754  *   -EINVAL: Invalid argument.
755  */
756 __rte_experimental
757 int
758 rte_swx_ctl_meter_set(struct rte_swx_pipeline *p,
759                       const char *metarray_name,
760                       uint32_t metarray_index,
761                       const char *profile_name);
762
763 /** Meter statistics counters. */
764 struct rte_swx_ctl_meter_stats {
765         /** Number of packets tagged by the meter for each color. */
766         uint64_t n_pkts[RTE_COLORS];
767
768         /** Number of bytes tagged by the meter for each color. */
769         uint64_t n_bytes[RTE_COLORS];
770 };
771
772 /**
773  * Meter statistics counters read
774  *
775  * @param[in] p
776  *   Pipeline handle.
777  * @param[in] metarray_name
778  *   Meter array name.
779  * @param[in] metarray_index
780  *   Meter index within the meter array.
781  * @param[out] stats
782  *   Meter statistics counters.
783  * @return
784  *   0 on success or the following error codes otherwise:
785  *   -EINVAL: Invalid argument.
786  */
787 __rte_experimental
788 int
789 rte_swx_ctl_meter_stats_read(struct rte_swx_pipeline *p,
790                              const char *metarray_name,
791                              uint32_t metarray_index,
792                              struct rte_swx_ctl_meter_stats *stats);
793
794 /**
795  * Pipeline control free
796  *
797  * @param[in] ctl
798  *   Pipeline control handle.
799  */
800 __rte_experimental
801 void
802 rte_swx_ctl_pipeline_free(struct rte_swx_ctl_pipeline *ctl);
803
804 #ifdef __cplusplus
805 }
806 #endif
807
808 #endif