1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
10 #include <rte_common.h>
11 #include <rte_byteorder.h>
13 #include <rte_swx_table_selector.h>
15 #include "rte_swx_ctl.h"
17 #define CHECK(condition, err_code) \
23 #define ntoh64(x) rte_be_to_cpu_64(x)
24 #define hton64(x) rte_cpu_to_be_64(x)
26 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
27 #define field_ntoh(val, n_bits) (ntoh64((val) << (64 - n_bits)))
28 #define field_hton(val, n_bits) (hton64((val) << (64 - n_bits)))
30 #define field_ntoh(val, n_bits) (val)
31 #define field_hton(val, n_bits) (val)
35 struct rte_swx_ctl_action_info info;
36 struct rte_swx_ctl_action_arg_info *args;
41 struct rte_swx_ctl_table_info info;
42 struct rte_swx_ctl_table_match_field_info *mf;
44 /* Match field with the smallest offset. */
45 struct rte_swx_ctl_table_match_field_info *mf_first;
47 /* Match field with the biggest offset. */
48 struct rte_swx_ctl_table_match_field_info *mf_last;
50 struct rte_swx_ctl_table_action_info *actions;
51 struct rte_swx_table_ops ops;
52 struct rte_swx_table_params params;
54 /* Set of "stable" keys: these keys are currently part of the table;
55 * these keys will be preserved with no action data changes after the
58 struct rte_swx_table_entry_list entries;
60 /* Set of new keys: these keys are currently NOT part of the table;
61 * these keys will be added to the table on the next commit, if
62 * the commit operation is successful.
64 struct rte_swx_table_entry_list pending_add;
66 /* Set of keys to be modified: these keys are currently part of the
67 * table; these keys are still going to be part of the table after the
68 * next commit, but their action data will be modified if the commit
69 * operation is successful. The modify0 list contains the keys with the
70 * current action data, the modify1 list contains the keys with the
71 * modified action data.
73 struct rte_swx_table_entry_list pending_modify0;
74 struct rte_swx_table_entry_list pending_modify1;
76 /* Set of keys to be deleted: these keys are currently part of the
77 * table; these keys are to be deleted from the table on the next
78 * commit, if the commit operation is successful.
80 struct rte_swx_table_entry_list pending_delete;
82 /* The pending default action: this is NOT the current default action;
83 * this will be the new default action after the next commit, if the
84 * next commit operation is successful.
86 struct rte_swx_table_entry *pending_default;
95 /* Selector table info. */
96 struct rte_swx_ctl_selector_info info;
99 struct rte_swx_ctl_table_match_field_info group_id_field;
101 /* selector fields. */
102 struct rte_swx_ctl_table_match_field_info *selector_fields;
104 /* member_id field. */
105 struct rte_swx_ctl_table_match_field_info member_id_field;
107 /* Current selector table. Array of info.n_groups_max elements.*/
108 struct rte_swx_table_selector_group **groups;
110 /* Pending selector table subject to the next commit. Array of info.n_groups_max elements.
112 struct rte_swx_table_selector_group **pending_groups;
114 /* Valid flag per group. Array of n_groups_max elements. */
117 /* Pending delete flag per group. Group deletion is subject to the next commit. Array of
118 * info.n_groups_max elements.
120 int *groups_pending_delete;
123 struct rte_swx_table_selector_params params;
127 struct rte_swx_ctl_learner_info info;
128 struct rte_swx_ctl_table_match_field_info *mf;
129 struct rte_swx_ctl_table_action_info *actions;
130 uint32_t action_data_size;
132 /* The pending default action: this is NOT the current default action;
133 * this will be the new default action after the next commit, if the
134 * next commit operation is successful.
136 struct rte_swx_table_entry *pending_default;
139 struct rte_swx_ctl_pipeline {
140 struct rte_swx_ctl_pipeline_info info;
141 struct rte_swx_pipeline *p;
142 struct action *actions;
143 struct table *tables;
144 struct selector *selectors;
145 struct learner *learners;
146 struct rte_swx_table_state *ts;
147 struct rte_swx_table_state *ts_next;
151 static struct action *
152 action_find(struct rte_swx_ctl_pipeline *ctl, const char *action_name)
156 for (i = 0; i < ctl->info.n_actions; i++) {
157 struct action *a = &ctl->actions[i];
159 if (!strcmp(action_name, a->info.name))
167 action_free(struct rte_swx_ctl_pipeline *ctl)
174 for (i = 0; i < ctl->info.n_actions; i++) {
175 struct action *action = &ctl->actions[i];
184 static struct table *
185 table_find(struct rte_swx_ctl_pipeline *ctl, const char *table_name)
189 for (i = 0; i < ctl->info.n_tables; i++) {
190 struct table *table = &ctl->tables[i];
192 if (!strcmp(table_name, table->info.name))
200 table_params_get(struct rte_swx_ctl_pipeline *ctl, uint32_t table_id)
202 struct table *table = &ctl->tables[table_id];
203 struct rte_swx_ctl_table_match_field_info *first = NULL, *last = NULL;
204 uint8_t *key_mask = NULL;
205 enum rte_swx_table_match_type match_type = RTE_SWX_TABLE_MATCH_WILDCARD;
206 uint32_t key_size = 0, key_offset = 0, action_data_size = 0, i;
208 if (table->info.n_match_fields) {
209 uint32_t n_match_fields_em = 0, i;
211 /* Find first (smallest offset) and last (biggest offset) match fields. */
212 first = &table->mf[0];
213 last = &table->mf[0];
215 for (i = 1; i < table->info.n_match_fields; i++) {
216 struct rte_swx_ctl_table_match_field_info *f = &table->mf[i];
218 if (f->offset < first->offset)
221 if (f->offset > last->offset)
226 for (i = 0; i < table->info.n_match_fields; i++) {
227 struct rte_swx_ctl_table_match_field_info *f = &table->mf[i];
229 if (f->match_type == RTE_SWX_TABLE_MATCH_EXACT)
233 if (n_match_fields_em == table->info.n_match_fields)
234 match_type = RTE_SWX_TABLE_MATCH_EXACT;
237 key_offset = first->offset / 8;
240 key_size = (last->offset + last->n_bits - first->offset) / 8;
243 key_mask = calloc(1, key_size);
244 CHECK(key_mask, ENOMEM);
246 for (i = 0; i < table->info.n_match_fields; i++) {
247 struct rte_swx_ctl_table_match_field_info *f = &table->mf[i];
251 start = (f->offset - first->offset) / 8;
252 size = f->n_bits / 8;
254 memset(&key_mask[start], 0xFF, size);
258 /* action_data_size. */
259 for (i = 0; i < table->info.n_actions; i++) {
260 uint32_t action_id = table->actions[i].action_id;
261 struct action *a = &ctl->actions[action_id];
263 if (a->data_size > action_data_size)
264 action_data_size = a->data_size;
268 table->params.match_type = match_type;
269 table->params.key_size = key_size;
270 table->params.key_offset = key_offset;
271 table->params.key_mask0 = key_mask;
272 table->params.action_data_size = action_data_size;
273 table->params.n_keys_max = table->info.size;
275 table->mf_first = first;
276 table->mf_last = last;
282 table_entry_free(struct rte_swx_table_entry *entry)
288 free(entry->key_mask);
289 free(entry->action_data);
293 static struct rte_swx_table_entry *
294 table_entry_alloc(struct table *table)
296 struct rte_swx_table_entry *entry;
298 entry = calloc(1, sizeof(struct rte_swx_table_entry));
303 if (!table->is_stub) {
304 entry->key = calloc(1, table->params.key_size);
308 if (table->params.match_type != RTE_SWX_TABLE_MATCH_EXACT) {
309 entry->key_mask = calloc(1, table->params.key_size);
310 if (!entry->key_mask)
316 if (table->params.action_data_size) {
317 entry->action_data = calloc(1, table->params.action_data_size);
318 if (!entry->action_data)
325 table_entry_free(entry);
330 table_entry_key_check_em(struct table *table, struct rte_swx_table_entry *entry)
332 uint8_t *key_mask0 = table->params.key_mask0;
333 uint32_t key_size = table->params.key_size, i;
335 if (!entry->key_mask)
338 for (i = 0; i < key_size; i++) {
339 uint8_t km0 = key_mask0[i];
340 uint8_t km = entry->key_mask[i];
342 if ((km & km0) != km0)
350 table_entry_check(struct rte_swx_ctl_pipeline *ctl,
352 struct rte_swx_table_entry *entry,
356 struct table *table = &ctl->tables[table_id];
359 CHECK(entry, EINVAL);
361 if (key_check && !table->is_stub) {
363 CHECK(entry->key, EINVAL);
366 if (table->params.match_type == RTE_SWX_TABLE_MATCH_EXACT) {
367 status = table_entry_key_check_em(table, entry);
378 for (i = 0; i < table->info.n_actions; i++)
379 if (entry->action_id == table->actions[i].action_id)
382 CHECK(i < table->info.n_actions, EINVAL);
385 a = &ctl->actions[entry->action_id];
386 CHECK(!(a->data_size && !entry->action_data), EINVAL);
392 static struct rte_swx_table_entry *
393 table_entry_duplicate(struct rte_swx_ctl_pipeline *ctl,
395 struct rte_swx_table_entry *entry,
399 struct table *table = &ctl->tables[table_id];
400 struct rte_swx_table_entry *new_entry = NULL;
405 new_entry = calloc(1, sizeof(struct rte_swx_table_entry));
409 if (key_duplicate && !table->is_stub) {
414 new_entry->key = malloc(table->params.key_size);
418 memcpy(new_entry->key, entry->key, table->params.key_size);
421 new_entry->key_signature = entry->key_signature;
424 if (entry->key_mask) {
425 new_entry->key_mask = malloc(table->params.key_size);
426 if (!new_entry->key_mask)
429 memcpy(new_entry->key_mask,
431 table->params.key_size);
435 new_entry->key_priority = entry->key_priority;
438 if (data_duplicate) {
443 for (i = 0; i < table->info.n_actions; i++)
444 if (entry->action_id == table->actions[i].action_id)
447 if (i >= table->info.n_actions)
450 new_entry->action_id = entry->action_id;
453 a = &ctl->actions[entry->action_id];
454 if (a->data_size && !entry->action_data)
457 /* The table layer provisions a constant action data size per
458 * entry, which should be the largest data size for all the
459 * actions enabled for the current table, and attempts to copy
460 * this many bytes each time a table entry is added, even if the
461 * specific action requires less data or even no data at all,
462 * hence we always have to allocate the max.
464 new_entry->action_data = calloc(1, table->params.action_data_size);
465 if (!new_entry->action_data)
469 memcpy(new_entry->action_data,
477 table_entry_free(new_entry);
482 table_entry_keycmp(struct table *table,
483 struct rte_swx_table_entry *e0,
484 struct rte_swx_table_entry *e1)
486 uint32_t key_size = table->params.key_size;
489 for (i = 0; i < key_size; i++) {
490 uint8_t *key_mask0 = table->params.key_mask0;
491 uint8_t km0, km[2], k[2];
493 km0 = key_mask0 ? key_mask0[i] : 0xFF;
495 km[0] = e0->key_mask ? e0->key_mask[i] : 0xFF;
496 km[1] = e1->key_mask ? e1->key_mask[i] : 0xFF;
501 /* Mask comparison. */
502 if ((km[0] & km0) != (km[1] & km0))
503 return 1; /* Not equal. */
505 /* Value comparison. */
506 if ((k[0] & km[0] & km0) != (k[1] & km[1] & km0))
507 return 1; /* Not equal. */
510 return 0; /* Equal. */
513 static struct rte_swx_table_entry *
514 table_entries_find(struct table *table, struct rte_swx_table_entry *entry)
516 struct rte_swx_table_entry *e;
518 TAILQ_FOREACH(e, &table->entries, node)
519 if (!table_entry_keycmp(table, entry, e))
520 return e; /* Found. */
522 return NULL; /* Not found. */
526 table_entries_free(struct table *table)
529 struct rte_swx_table_entry *entry;
531 entry = TAILQ_FIRST(&table->entries);
535 TAILQ_REMOVE(&table->entries, entry, node);
536 table_entry_free(entry);
540 static struct rte_swx_table_entry *
541 table_pending_add_find(struct table *table, struct rte_swx_table_entry *entry)
543 struct rte_swx_table_entry *e;
545 TAILQ_FOREACH(e, &table->pending_add, node)
546 if (!table_entry_keycmp(table, entry, e))
547 return e; /* Found. */
549 return NULL; /* Not found. */
553 table_pending_add_admit(struct table *table)
555 TAILQ_CONCAT(&table->entries, &table->pending_add, node);
559 table_pending_add_free(struct table *table)
562 struct rte_swx_table_entry *entry;
564 entry = TAILQ_FIRST(&table->pending_add);
568 TAILQ_REMOVE(&table->pending_add, entry, node);
569 table_entry_free(entry);
573 static struct rte_swx_table_entry *
574 table_pending_modify0_find(struct table *table,
575 struct rte_swx_table_entry *entry)
577 struct rte_swx_table_entry *e;
579 TAILQ_FOREACH(e, &table->pending_modify0, node)
580 if (!table_entry_keycmp(table, entry, e))
581 return e; /* Found. */
583 return NULL; /* Not found. */
587 table_pending_modify0_admit(struct table *table)
589 TAILQ_CONCAT(&table->entries, &table->pending_modify0, node);
593 table_pending_modify0_free(struct table *table)
596 struct rte_swx_table_entry *entry;
598 entry = TAILQ_FIRST(&table->pending_modify0);
602 TAILQ_REMOVE(&table->pending_modify0, entry, node);
603 table_entry_free(entry);
607 static struct rte_swx_table_entry *
608 table_pending_modify1_find(struct table *table,
609 struct rte_swx_table_entry *entry)
611 struct rte_swx_table_entry *e;
613 TAILQ_FOREACH(e, &table->pending_modify1, node)
614 if (!table_entry_keycmp(table, entry, e))
615 return e; /* Found. */
617 return NULL; /* Not found. */
621 table_pending_modify1_admit(struct table *table)
623 TAILQ_CONCAT(&table->entries, &table->pending_modify1, node);
627 table_pending_modify1_free(struct table *table)
630 struct rte_swx_table_entry *entry;
632 entry = TAILQ_FIRST(&table->pending_modify1);
636 TAILQ_REMOVE(&table->pending_modify1, entry, node);
637 table_entry_free(entry);
641 static struct rte_swx_table_entry *
642 table_pending_delete_find(struct table *table,
643 struct rte_swx_table_entry *entry)
645 struct rte_swx_table_entry *e;
647 TAILQ_FOREACH(e, &table->pending_delete, node)
648 if (!table_entry_keycmp(table, entry, e))
649 return e; /* Found. */
651 return NULL; /* Not found. */
655 table_pending_delete_admit(struct table *table)
657 TAILQ_CONCAT(&table->entries, &table->pending_delete, node);
661 table_pending_delete_free(struct table *table)
664 struct rte_swx_table_entry *entry;
666 entry = TAILQ_FIRST(&table->pending_delete);
670 TAILQ_REMOVE(&table->pending_delete, entry, node);
671 table_entry_free(entry);
676 table_pending_default_free(struct table *table)
678 if (!table->pending_default)
681 free(table->pending_default->action_data);
682 free(table->pending_default);
683 table->pending_default = NULL;
687 table_is_update_pending(struct table *table, int consider_pending_default)
689 struct rte_swx_table_entry *e;
693 TAILQ_FOREACH(e, &table->pending_add, node)
696 /* Pending modify. */
697 TAILQ_FOREACH(e, &table->pending_modify1, node)
700 /* Pending delete. */
701 TAILQ_FOREACH(e, &table->pending_delete, node)
704 /* Pending default. */
705 if (consider_pending_default && table->pending_default)
712 table_free(struct rte_swx_ctl_pipeline *ctl)
719 for (i = 0; i < ctl->info.n_tables; i++) {
720 struct table *table = &ctl->tables[i];
723 free(table->actions);
724 free(table->params.key_mask0);
726 table_entries_free(table);
727 table_pending_add_free(table);
728 table_pending_modify0_free(table);
729 table_pending_modify1_free(table);
730 table_pending_delete_free(table);
731 table_pending_default_free(table);
739 selector_group_members_free(struct selector *s, uint32_t group_id)
741 struct rte_swx_table_selector_group *group = s->groups[group_id];
747 struct rte_swx_table_selector_member *m;
749 m = TAILQ_FIRST(&group->members);
753 TAILQ_REMOVE(&group->members, m, node);
758 s->groups[group_id] = NULL;
762 selector_pending_group_members_free(struct selector *s, uint32_t group_id)
764 struct rte_swx_table_selector_group *group = s->pending_groups[group_id];
770 struct rte_swx_table_selector_member *m;
772 m = TAILQ_FIRST(&group->members);
776 TAILQ_REMOVE(&group->members, m, node);
781 s->pending_groups[group_id] = NULL;
785 selector_group_duplicate_to_pending(struct selector *s, uint32_t group_id)
787 struct rte_swx_table_selector_group *g, *gp;
788 struct rte_swx_table_selector_member *m;
790 selector_pending_group_members_free(s, group_id);
792 g = s->groups[group_id];
793 gp = s->pending_groups[group_id];
796 gp = calloc(1, sizeof(struct rte_swx_table_selector_group));
800 TAILQ_INIT(&gp->members);
802 s->pending_groups[group_id] = gp;
808 TAILQ_FOREACH(m, &g->members, node) {
809 struct rte_swx_table_selector_member *mp;
811 mp = calloc(1, sizeof(struct rte_swx_table_selector_member));
815 memcpy(mp, m, sizeof(struct rte_swx_table_selector_member));
817 TAILQ_INSERT_TAIL(&gp->members, mp, node);
823 selector_pending_group_members_free(s, group_id);
828 selector_free(struct rte_swx_ctl_pipeline *ctl)
835 for (i = 0; i < ctl->info.n_selectors; i++) {
836 struct selector *s = &ctl->selectors[i];
839 /* selector_fields. */
840 free(s->selector_fields);
844 for (i = 0; i < s->info.n_groups_max; i++)
845 selector_group_members_free(s, i);
849 /* pending_groups. */
850 if (s->pending_groups)
851 for (i = 0; i < s->info.n_groups_max; i++)
852 selector_pending_group_members_free(s, i);
854 free(s->pending_groups);
857 free(s->groups_added);
859 /* groups_pending_delete. */
860 free(s->groups_pending_delete);
863 free(s->params.selector_mask);
866 free(ctl->selectors);
867 ctl->selectors = NULL;
870 static struct selector *
871 selector_find(struct rte_swx_ctl_pipeline *ctl, const char *selector_name)
875 for (i = 0; i < ctl->info.n_selectors; i++) {
876 struct selector *s = &ctl->selectors[i];
878 if (!strcmp(selector_name, s->info.name))
886 selector_params_get(struct rte_swx_ctl_pipeline *ctl, uint32_t selector_id)
888 struct selector *s = &ctl->selectors[selector_id];
889 struct rte_swx_ctl_table_match_field_info *first = NULL, *last = NULL;
890 uint8_t *selector_mask = NULL;
891 uint32_t selector_size = 0, selector_offset = 0, i;
893 /* Find first (smallest offset) and last (biggest offset) match fields. */
894 first = &s->selector_fields[0];
895 last = &s->selector_fields[0];
897 for (i = 1; i < s->info.n_selector_fields; i++) {
898 struct rte_swx_ctl_table_match_field_info *f = &s->selector_fields[i];
900 if (f->offset < first->offset)
903 if (f->offset > last->offset)
907 /* selector_offset. */
908 selector_offset = first->offset / 8;
911 selector_size = (last->offset + last->n_bits - first->offset) / 8;
914 selector_mask = calloc(1, selector_size);
918 for (i = 0; i < s->info.n_selector_fields; i++) {
919 struct rte_swx_ctl_table_match_field_info *f = &s->selector_fields[i];
923 start = (f->offset - first->offset) / 8;
924 size = f->n_bits / 8;
926 memset(&selector_mask[start], 0xFF, size);
930 s->params.group_id_offset = s->group_id_field.offset / 8;
931 s->params.selector_size = selector_size;
932 s->params.selector_offset = selector_offset;
933 s->params.selector_mask = selector_mask;
934 s->params.member_id_offset = s->member_id_field.offset / 8;
935 s->params.n_groups_max = s->info.n_groups_max;
936 s->params.n_members_per_group_max = s->info.n_members_per_group_max;
942 learner_pending_default_free(struct learner *l)
944 if (!l->pending_default)
947 free(l->pending_default->action_data);
948 free(l->pending_default);
949 l->pending_default = NULL;
954 learner_free(struct rte_swx_ctl_pipeline *ctl)
961 for (i = 0; i < ctl->info.n_learners; i++) {
962 struct learner *l = &ctl->learners[i];
967 learner_pending_default_free(l);
971 ctl->learners = NULL;
974 static struct learner *
975 learner_find(struct rte_swx_ctl_pipeline *ctl, const char *learner_name)
979 for (i = 0; i < ctl->info.n_learners; i++) {
980 struct learner *l = &ctl->learners[i];
982 if (!strcmp(learner_name, l->info.name))
990 learner_action_data_size_get(struct rte_swx_ctl_pipeline *ctl, struct learner *l)
992 uint32_t action_data_size = 0, i;
994 for (i = 0; i < l->info.n_actions; i++) {
995 uint32_t action_id = l->actions[i].action_id;
996 struct action *a = &ctl->actions[action_id];
998 if (a->data_size > action_data_size)
999 action_data_size = a->data_size;
1002 return action_data_size;
1006 table_state_free(struct rte_swx_ctl_pipeline *ctl)
1013 /* For each table, free its table state. */
1014 for (i = 0; i < ctl->info.n_tables; i++) {
1015 struct table *table = &ctl->tables[i];
1016 struct rte_swx_table_state *ts = &ctl->ts_next[i];
1018 /* Default action data. */
1019 free(ts->default_action_data);
1022 if (!table->is_stub && table->ops.free && ts->obj)
1023 table->ops.free(ts->obj);
1026 /* For each selector table, free its table state. */
1027 for (i = 0; i < ctl->info.n_selectors; i++) {
1028 struct rte_swx_table_state *ts = &ctl->ts_next[i];
1032 rte_swx_table_selector_free(ts->obj);
1035 /* For each learner table, free its table state. */
1036 for (i = 0; i < ctl->info.n_learners; i++) {
1037 struct rte_swx_table_state *ts = &ctl->ts_next[i];
1039 /* Default action data. */
1040 free(ts->default_action_data);
1044 ctl->ts_next = NULL;
1048 table_state_create(struct rte_swx_ctl_pipeline *ctl)
1053 ctl->ts_next = calloc(ctl->info.n_tables + ctl->info.n_selectors,
1054 sizeof(struct rte_swx_table_state));
1055 if (!ctl->ts_next) {
1061 for (i = 0; i < ctl->info.n_tables; i++) {
1062 struct table *table = &ctl->tables[i];
1063 struct rte_swx_table_state *ts = &ctl->ts[i];
1064 struct rte_swx_table_state *ts_next = &ctl->ts_next[i];
1067 if (!table->is_stub && table->ops.add) {
1068 ts_next->obj = table->ops.create(&table->params,
1072 if (!ts_next->obj) {
1078 if (!table->is_stub && !table->ops.add)
1079 ts_next->obj = ts->obj;
1081 /* Default action data: duplicate from current table state. */
1082 ts_next->default_action_data =
1083 malloc(table->params.action_data_size);
1084 if (!ts_next->default_action_data) {
1089 memcpy(ts_next->default_action_data,
1090 ts->default_action_data,
1091 table->params.action_data_size);
1093 ts_next->default_action_id = ts->default_action_id;
1096 /* Selector tables. */
1097 for (i = 0; i < ctl->info.n_selectors; i++) {
1098 struct selector *s = &ctl->selectors[i];
1099 struct rte_swx_table_state *ts_next = &ctl->ts_next[ctl->info.n_tables + i];
1102 ts_next->obj = rte_swx_table_selector_create(&s->params, NULL, ctl->numa_node);
1103 if (!ts_next->obj) {
1109 /* Learner tables. */
1110 for (i = 0; i < ctl->info.n_learners; i++) {
1111 struct learner *l = &ctl->learners[i];
1112 struct rte_swx_table_state *ts = &ctl->ts[i];
1113 struct rte_swx_table_state *ts_next = &ctl->ts_next[i];
1115 /* Table object: duplicate from the current table state. */
1116 ts_next->obj = ts->obj;
1118 /* Default action data: duplicate from the current table state. */
1119 ts_next->default_action_data = malloc(l->action_data_size);
1120 if (!ts_next->default_action_data) {
1125 memcpy(ts_next->default_action_data,
1126 ts->default_action_data,
1127 l->action_data_size);
1129 ts_next->default_action_id = ts->default_action_id;
1135 table_state_free(ctl);
1140 rte_swx_ctl_pipeline_free(struct rte_swx_ctl_pipeline *ctl)
1147 table_state_free(ctl);
1158 struct rte_swx_ctl_pipeline *
1159 rte_swx_ctl_pipeline_create(struct rte_swx_pipeline *p)
1161 struct rte_swx_ctl_pipeline *ctl = NULL;
1168 ctl = calloc(1, sizeof(struct rte_swx_ctl_pipeline));
1173 status = rte_swx_ctl_pipeline_info_get(p, &ctl->info);
1178 status = rte_swx_ctl_pipeline_numa_node_get(p, &ctl->numa_node);
1186 ctl->actions = calloc(ctl->info.n_actions, sizeof(struct action));
1190 for (i = 0; i < ctl->info.n_actions; i++) {
1191 struct action *a = &ctl->actions[i];
1195 status = rte_swx_ctl_action_info_get(p, i, &a->info);
1200 a->args = calloc(a->info.n_args,
1201 sizeof(struct rte_swx_ctl_action_arg_info));
1205 for (j = 0; j < a->info.n_args; j++) {
1206 status = rte_swx_ctl_action_arg_info_get(p,
1215 for (j = 0; j < a->info.n_args; j++) {
1216 struct rte_swx_ctl_action_arg_info *info = &a->args[j];
1218 a->data_size += info->n_bits;
1221 a->data_size = (a->data_size + 7) / 8;
1225 ctl->tables = calloc(ctl->info.n_tables, sizeof(struct table));
1229 for (i = 0; i < ctl->info.n_tables; i++) {
1230 struct table *t = &ctl->tables[i];
1232 TAILQ_INIT(&t->entries);
1233 TAILQ_INIT(&t->pending_add);
1234 TAILQ_INIT(&t->pending_modify0);
1235 TAILQ_INIT(&t->pending_modify1);
1236 TAILQ_INIT(&t->pending_delete);
1239 for (i = 0; i < ctl->info.n_tables; i++) {
1240 struct table *t = &ctl->tables[i];
1244 status = rte_swx_ctl_table_info_get(p, i, &t->info);
1249 t->mf = calloc(t->info.n_match_fields,
1250 sizeof(struct rte_swx_ctl_table_match_field_info));
1254 for (j = 0; j < t->info.n_match_fields; j++) {
1255 status = rte_swx_ctl_table_match_field_info_get(p,
1264 t->actions = calloc(t->info.n_actions,
1265 sizeof(struct rte_swx_ctl_table_action_info));
1269 for (j = 0; j < t->info.n_actions; j++) {
1270 status = rte_swx_ctl_table_action_info_get(p,
1275 t->actions[j].action_id >= ctl->info.n_actions)
1280 status = rte_swx_ctl_table_ops_get(p, i, &t->ops, &t->is_stub);
1284 if ((t->is_stub && t->info.n_match_fields) ||
1285 (!t->is_stub && !t->info.n_match_fields))
1289 status = table_params_get(ctl, i);
1294 /* selector tables. */
1295 ctl->selectors = calloc(ctl->info.n_selectors, sizeof(struct selector));
1296 if (!ctl->selectors)
1299 for (i = 0; i < ctl->info.n_selectors; i++) {
1300 struct selector *s = &ctl->selectors[i];
1304 status = rte_swx_ctl_selector_info_get(p, i, &s->info);
1308 /* group_id field. */
1309 status = rte_swx_ctl_selector_group_id_field_info_get(p,
1311 &s->group_id_field);
1315 /* selector fields. */
1316 s->selector_fields = calloc(s->info.n_selector_fields,
1317 sizeof(struct rte_swx_ctl_table_match_field_info));
1318 if (!s->selector_fields)
1321 for (j = 0; j < s->info.n_selector_fields; j++) {
1322 status = rte_swx_ctl_selector_field_info_get(p,
1325 &s->selector_fields[j]);
1330 /* member_id field. */
1331 status = rte_swx_ctl_selector_member_id_field_info_get(p,
1333 &s->member_id_field);
1338 s->groups = calloc(s->info.n_groups_max,
1339 sizeof(struct rte_swx_table_selector_group *));
1343 /* pending_groups. */
1344 s->pending_groups = calloc(s->info.n_groups_max,
1345 sizeof(struct rte_swx_table_selector_group *));
1346 if (!s->pending_groups)
1350 s->groups_added = calloc(s->info.n_groups_max, sizeof(int));
1351 if (!s->groups_added)
1354 /* groups_pending_delete. */
1355 s->groups_pending_delete = calloc(s->info.n_groups_max, sizeof(int));
1356 if (!s->groups_pending_delete)
1360 status = selector_params_get(ctl, i);
1365 /* learner tables. */
1366 ctl->learners = calloc(ctl->info.n_learners, sizeof(struct learner));
1370 for (i = 0; i < ctl->info.n_learners; i++) {
1371 struct learner *l = &ctl->learners[i];
1375 status = rte_swx_ctl_learner_info_get(p, i, &l->info);
1380 l->mf = calloc(l->info.n_match_fields,
1381 sizeof(struct rte_swx_ctl_table_match_field_info));
1385 for (j = 0; j < l->info.n_match_fields; j++) {
1386 status = rte_swx_ctl_learner_match_field_info_get(p,
1395 l->actions = calloc(l->info.n_actions,
1396 sizeof(struct rte_swx_ctl_table_action_info));
1400 for (j = 0; j < l->info.n_actions; j++) {
1401 status = rte_swx_ctl_learner_action_info_get(p,
1405 if (status || l->actions[j].action_id >= ctl->info.n_actions)
1409 /* action_data_size. */
1410 l->action_data_size = learner_action_data_size_get(ctl, l);
1414 status = rte_swx_pipeline_table_state_get(p, &ctl->ts);
1419 status = table_state_create(ctl);
1426 rte_swx_ctl_pipeline_free(ctl);
1431 rte_swx_ctl_pipeline_table_entry_add(struct rte_swx_ctl_pipeline *ctl,
1432 const char *table_name,
1433 struct rte_swx_table_entry *entry)
1435 struct table *table;
1436 struct rte_swx_table_entry *new_entry, *existing_entry;
1440 CHECK(table_name && table_name[0], EINVAL);
1442 table = table_find(ctl, table_name);
1443 CHECK(table, EINVAL);
1444 table_id = table - ctl->tables;
1446 CHECK(entry, EINVAL);
1447 CHECK(!table_entry_check(ctl, table_id, entry, 1, 1), EINVAL);
1449 new_entry = table_entry_duplicate(ctl, table_id, entry, 1, 1);
1450 CHECK(new_entry, ENOMEM);
1452 /* The new entry is found in the table->entries list:
1453 * - Add the new entry to the table->pending_modify1 list;
1454 * - Move the existing entry from the table->entries list to the
1455 * table->pending_modify0 list.
1457 existing_entry = table_entries_find(table, entry);
1458 if (existing_entry) {
1459 TAILQ_INSERT_TAIL(&table->pending_modify1,
1463 TAILQ_REMOVE(&table->entries,
1467 TAILQ_INSERT_TAIL(&table->pending_modify0,
1474 /* The new entry is found in the table->pending_add list:
1475 * - Replace the entry in the table->pending_add list with the new entry
1476 * (and free the replaced entry).
1478 existing_entry = table_pending_add_find(table, entry);
1479 if (existing_entry) {
1480 TAILQ_INSERT_AFTER(&table->pending_add,
1485 TAILQ_REMOVE(&table->pending_add,
1489 table_entry_free(existing_entry);
1494 /* The new entry is found in the table->pending_modify1 list:
1495 * - Replace the entry in the table->pending_modify1 list with the new
1496 * entry (and free the replaced entry).
1498 existing_entry = table_pending_modify1_find(table, entry);
1499 if (existing_entry) {
1500 TAILQ_INSERT_AFTER(&table->pending_modify1,
1505 TAILQ_REMOVE(&table->pending_modify1,
1509 table_entry_free(existing_entry);
1514 /* The new entry is found in the table->pending_delete list:
1515 * - Add the new entry to the table->pending_modify1 list;
1516 * - Move the existing entry from the table->pending_delete list to the
1517 * table->pending_modify0 list.
1519 existing_entry = table_pending_delete_find(table, entry);
1520 if (existing_entry) {
1521 TAILQ_INSERT_TAIL(&table->pending_modify1,
1525 TAILQ_REMOVE(&table->pending_delete,
1529 TAILQ_INSERT_TAIL(&table->pending_modify0,
1536 /* The new entry is not found in any of the above lists:
1537 * - Add the new entry to the table->pending_add list.
1539 TAILQ_INSERT_TAIL(&table->pending_add, new_entry, node);
1545 rte_swx_ctl_pipeline_table_entry_delete(struct rte_swx_ctl_pipeline *ctl,
1546 const char *table_name,
1547 struct rte_swx_table_entry *entry)
1549 struct table *table;
1550 struct rte_swx_table_entry *existing_entry;
1555 CHECK(table_name && table_name[0], EINVAL);
1556 table = table_find(ctl, table_name);
1557 CHECK(table, EINVAL);
1558 table_id = table - ctl->tables;
1560 CHECK(entry, EINVAL);
1561 CHECK(!table_entry_check(ctl, table_id, entry, 1, 0), EINVAL);
1563 /* The entry is found in the table->entries list:
1564 * - Move the existing entry from the table->entries list to to the
1565 * table->pending_delete list.
1567 existing_entry = table_entries_find(table, entry);
1568 if (existing_entry) {
1569 TAILQ_REMOVE(&table->entries,
1573 TAILQ_INSERT_TAIL(&table->pending_delete,
1580 /* The entry is found in the table->pending_add list:
1581 * - Remove the entry from the table->pending_add list and free it.
1583 existing_entry = table_pending_add_find(table, entry);
1584 if (existing_entry) {
1585 TAILQ_REMOVE(&table->pending_add,
1589 table_entry_free(existing_entry);
1592 /* The entry is found in the table->pending_modify1 list:
1593 * - Free the entry in the table->pending_modify1 list;
1594 * - Move the existing entry from the table->pending_modify0 list to the
1595 * table->pending_delete list.
1597 existing_entry = table_pending_modify1_find(table, entry);
1598 if (existing_entry) {
1599 struct rte_swx_table_entry *real_existing_entry;
1601 TAILQ_REMOVE(&table->pending_modify1,
1605 table_entry_free(existing_entry);
1607 real_existing_entry = table_pending_modify0_find(table, entry);
1608 CHECK(real_existing_entry, EINVAL); /* Coverity. */
1610 TAILQ_REMOVE(&table->pending_modify0,
1611 real_existing_entry,
1614 TAILQ_INSERT_TAIL(&table->pending_delete,
1615 real_existing_entry,
1621 /* The entry is found in the table->pending_delete list:
1622 * - Do nothing: the existing entry is already in the
1623 * table->pending_delete list, i.e. already marked for delete, so
1624 * simply keep it there as it is.
1627 /* The entry is not found in any of the above lists:
1628 * - Do nothing: no existing entry to delete.
1635 rte_swx_ctl_pipeline_table_default_entry_add(struct rte_swx_ctl_pipeline *ctl,
1636 const char *table_name,
1637 struct rte_swx_table_entry *entry)
1639 struct table *table;
1640 struct rte_swx_table_entry *new_entry;
1645 CHECK(table_name && table_name[0], EINVAL);
1646 table = table_find(ctl, table_name);
1647 CHECK(table, EINVAL);
1648 table_id = table - ctl->tables;
1649 CHECK(!table->info.default_action_is_const, EINVAL);
1651 CHECK(entry, EINVAL);
1652 CHECK(!table_entry_check(ctl, table_id, entry, 0, 1), EINVAL);
1654 new_entry = table_entry_duplicate(ctl, table_id, entry, 0, 1);
1655 CHECK(new_entry, ENOMEM);
1657 table_pending_default_free(table);
1659 table->pending_default = new_entry;
1665 table_entry_list_free(struct rte_swx_table_entry_list *list)
1668 struct rte_swx_table_entry *entry;
1670 entry = TAILQ_FIRST(list);
1674 TAILQ_REMOVE(list, entry, node);
1675 table_entry_free(entry);
1680 table_entry_list_duplicate(struct rte_swx_ctl_pipeline *ctl,
1682 struct rte_swx_table_entry_list *dst,
1683 struct rte_swx_table_entry_list *src)
1685 struct rte_swx_table_entry *src_entry;
1687 TAILQ_FOREACH(src_entry, src, node) {
1688 struct rte_swx_table_entry *dst_entry;
1690 dst_entry = table_entry_duplicate(ctl, table_id, src_entry, 1, 1);
1694 TAILQ_INSERT_TAIL(dst, dst_entry, node);
1700 table_entry_list_free(dst);
1704 /* This commit stage contains all the operations that can fail; in case ANY of
1705 * them fails for ANY table, ALL of them are rolled back for ALL the tables.
1708 table_rollfwd0(struct rte_swx_ctl_pipeline *ctl,
1710 uint32_t after_swap)
1712 struct table *table = &ctl->tables[table_id];
1713 struct rte_swx_table_state *ts = &ctl->ts[table_id];
1714 struct rte_swx_table_state *ts_next = &ctl->ts_next[table_id];
1716 if (table->is_stub || !table_is_update_pending(table, 0))
1720 * Current table supports incremental update.
1722 if (table->ops.add) {
1723 /* Reset counters. */
1725 table->n_modify = 0;
1726 table->n_delete = 0;
1728 /* Add pending rules. */
1729 struct rte_swx_table_entry *entry;
1731 TAILQ_FOREACH(entry, &table->pending_add, node) {
1734 status = table->ops.add(ts_next->obj, entry);
1741 /* Modify pending rules. */
1742 TAILQ_FOREACH(entry, &table->pending_modify1, node) {
1745 status = table->ops.add(ts_next->obj, entry);
1752 /* Delete pending rules. */
1753 TAILQ_FOREACH(entry, &table->pending_delete, node) {
1756 status = table->ops.del(ts_next->obj, entry);
1767 * Current table does NOT support incremental update.
1770 struct rte_swx_table_entry_list list;
1773 /* Create updated list of entries included. */
1776 status = table_entry_list_duplicate(ctl,
1783 status = table_entry_list_duplicate(ctl,
1786 &table->pending_add);
1790 status = table_entry_list_duplicate(ctl,
1793 &table->pending_modify1);
1797 /* Create new table object with the updates included. */
1798 ts_next->obj = table->ops.create(&table->params,
1802 if (!ts_next->obj) {
1807 table_entry_list_free(&list);
1812 table_entry_list_free(&list);
1816 /* Free the old table object. */
1817 if (ts_next->obj && table->ops.free)
1818 table->ops.free(ts_next->obj);
1820 /* Copy over the new table object. */
1821 ts_next->obj = ts->obj;
1826 /* This commit stage contains all the operations that cannot fail. They are
1827 * executed only if the previous stage was successful for ALL the tables. Hence,
1828 * none of these operations has to be rolled back for ANY table.
1831 table_rollfwd1(struct rte_swx_ctl_pipeline *ctl, uint32_t table_id)
1833 struct table *table = &ctl->tables[table_id];
1834 struct rte_swx_table_state *ts_next = &ctl->ts_next[table_id];
1836 uint8_t *action_data;
1839 /* Copy the pending default entry. */
1840 if (!table->pending_default)
1843 action_id = table->pending_default->action_id;
1844 action_data = table->pending_default->action_data;
1845 a = &ctl->actions[action_id];
1848 memcpy(ts_next->default_action_data, action_data, a->data_size);
1850 ts_next->default_action_id = action_id;
1853 /* This last commit stage is simply finalizing a successful commit operation.
1854 * This stage is only executed if all the previous stages were successful. This
1855 * stage cannot fail.
1858 table_rollfwd2(struct rte_swx_ctl_pipeline *ctl, uint32_t table_id)
1860 struct table *table = &ctl->tables[table_id];
1862 /* Move all the pending add entries to the table, as they are now part
1865 table_pending_add_admit(table);
1867 /* Move all the pending modify1 entries to table, are they are now part
1868 * of the table. Free up all the pending modify0 entries, as they are no
1869 * longer part of the table.
1871 table_pending_modify1_admit(table);
1872 table_pending_modify0_free(table);
1874 /* Free up all the pending delete entries, as they are no longer part of
1877 table_pending_delete_free(table);
1879 /* Free up the pending default entry, as it is now part of the table. */
1880 table_pending_default_free(table);
1883 /* The rollback stage is only executed when the commit failed, i.e. ANY of the
1884 * commit operations that can fail did fail for ANY table. It reverts ALL the
1885 * tables to their state before the commit started, as if the commit never
1889 table_rollback(struct rte_swx_ctl_pipeline *ctl, uint32_t table_id)
1891 struct table *table = &ctl->tables[table_id];
1892 struct rte_swx_table_state *ts_next = &ctl->ts_next[table_id];
1894 if (table->is_stub || !table_is_update_pending(table, 0))
1897 if (table->ops.add) {
1898 struct rte_swx_table_entry *entry;
1900 /* Add back all the entries that were just deleted. */
1901 TAILQ_FOREACH(entry, &table->pending_delete, node) {
1902 if (!table->n_delete)
1905 table->ops.add(ts_next->obj, entry);
1909 /* Add back the old copy for all the entries that were just
1912 TAILQ_FOREACH(entry, &table->pending_modify0, node) {
1913 if (!table->n_modify)
1916 table->ops.add(ts_next->obj, entry);
1920 /* Delete all the entries that were just added. */
1921 TAILQ_FOREACH(entry, &table->pending_add, node) {
1925 table->ops.del(ts_next->obj, entry);
1929 struct rte_swx_table_state *ts = &ctl->ts[table_id];
1931 /* Free the new table object, as update was cancelled. */
1932 if (ts_next->obj && table->ops.free)
1933 table->ops.free(ts_next->obj);
1935 /* Reinstate the old table object. */
1936 ts_next->obj = ts->obj;
1940 /* This stage is conditionally executed (as instructed by the user) after a
1941 * failed commit operation to remove ALL the pending work for ALL the tables.
1944 table_abort(struct rte_swx_ctl_pipeline *ctl, uint32_t table_id)
1946 struct table *table = &ctl->tables[table_id];
1948 /* Free up all the pending add entries, as none of them is part of the
1951 table_pending_add_free(table);
1953 /* Free up all the pending modify1 entries, as none of them made it to
1954 * the table. Add back all the pending modify0 entries, as none of them
1955 * was deleted from the table.
1957 table_pending_modify1_free(table);
1958 table_pending_modify0_admit(table);
1960 /* Add back all the pending delete entries, as none of them was deleted
1963 table_pending_delete_admit(table);
1965 /* Free up the pending default entry, as it is no longer going to be
1966 * added to the table.
1968 table_pending_default_free(table);
1972 rte_swx_ctl_pipeline_selector_group_add(struct rte_swx_ctl_pipeline *ctl,
1973 const char *selector_name,
1979 /* Check input arguments. */
1980 if (!ctl || !selector_name || !selector_name[0] || !group_id)
1983 s = selector_find(ctl, selector_name);
1987 /* Find an unused group. */
1988 for (i = 0; i < s->info.n_groups_max; i++)
1989 if (!s->groups_added[i]) {
1991 s->groups_added[i] = 1;
1999 rte_swx_ctl_pipeline_selector_group_delete(struct rte_swx_ctl_pipeline *ctl,
2000 const char *selector_name,
2004 struct rte_swx_table_selector_group *group;
2006 /* Check input arguments. */
2007 if (!ctl || !selector_name || !selector_name[0])
2010 s = selector_find(ctl, selector_name);
2012 (group_id >= s->info.n_groups_max) ||
2013 !s->groups_added[group_id])
2016 /* Check if this group is already scheduled for deletion. */
2017 if (s->groups_pending_delete[group_id])
2020 /* Initialize the pending group, if needed. */
2021 if (!s->pending_groups[group_id]) {
2024 status = selector_group_duplicate_to_pending(s, group_id);
2029 group = s->pending_groups[group_id];
2031 /* Schedule removal of all the members from the current group. */
2033 struct rte_swx_table_selector_member *m;
2035 m = TAILQ_FIRST(&group->members);
2039 TAILQ_REMOVE(&group->members, m, node);
2043 /* Schedule the group for deletion. */
2044 s->groups_pending_delete[group_id] = 1;
2050 rte_swx_ctl_pipeline_selector_group_member_add(struct rte_swx_ctl_pipeline *ctl,
2051 const char *selector_name,
2054 uint32_t member_weight)
2057 struct rte_swx_table_selector_group *group;
2058 struct rte_swx_table_selector_member *m;
2061 return rte_swx_ctl_pipeline_selector_group_member_delete(ctl,
2066 /* Check input arguments. */
2067 if (!ctl || !selector_name || !selector_name[0])
2070 s = selector_find(ctl, selector_name);
2072 (group_id >= s->info.n_groups_max) ||
2073 !s->groups_added[group_id] ||
2074 s->groups_pending_delete[group_id])
2077 /* Initialize the pending group, if needed. */
2078 if (!s->pending_groups[group_id]) {
2081 status = selector_group_duplicate_to_pending(s, group_id);
2086 group = s->pending_groups[group_id];
2088 /* If this member is already in this group, then simply update its weight and return. */
2089 TAILQ_FOREACH(m, &group->members, node)
2090 if (m->member_id == member_id) {
2091 m->member_weight = member_weight;
2095 /* Add new member to this group. */
2096 m = calloc(1, sizeof(struct rte_swx_table_selector_member));
2100 m->member_id = member_id;
2101 m->member_weight = member_weight;
2103 TAILQ_INSERT_TAIL(&group->members, m, node);
2109 rte_swx_ctl_pipeline_selector_group_member_delete(struct rte_swx_ctl_pipeline *ctl,
2110 const char *selector_name,
2111 uint32_t group_id __rte_unused,
2112 uint32_t member_id __rte_unused)
2115 struct rte_swx_table_selector_group *group;
2116 struct rte_swx_table_selector_member *m;
2118 /* Check input arguments. */
2119 if (!ctl || !selector_name || !selector_name[0])
2122 s = selector_find(ctl, selector_name);
2124 (group_id >= s->info.n_groups_max) ||
2125 !s->groups_added[group_id] ||
2126 s->groups_pending_delete[group_id])
2129 /* Initialize the pending group, if needed. */
2130 if (!s->pending_groups[group_id]) {
2133 status = selector_group_duplicate_to_pending(s, group_id);
2138 group = s->pending_groups[group_id];
2140 /* Look for this member in the group and remove it, if found. */
2141 TAILQ_FOREACH(m, &group->members, node)
2142 if (m->member_id == member_id) {
2143 TAILQ_REMOVE(&group->members, m, node);
2152 selector_rollfwd(struct rte_swx_ctl_pipeline *ctl, uint32_t selector_id)
2154 struct selector *s = &ctl->selectors[selector_id];
2155 struct rte_swx_table_state *ts_next = &ctl->ts_next[ctl->info.n_tables + selector_id];
2158 /* Push pending group member changes (s->pending_groups[group_id]) to the selector table
2159 * mirror copy (ts_next->obj).
2161 for (group_id = 0; group_id < s->info.n_groups_max; group_id++) {
2162 struct rte_swx_table_selector_group *group = s->pending_groups[group_id];
2165 /* Skip this group if no change needed. */
2169 /* Apply the pending changes for the current group. */
2170 status = rte_swx_table_selector_group_set(ts_next->obj, group_id, group);
2179 selector_rollfwd_finalize(struct rte_swx_ctl_pipeline *ctl, uint32_t selector_id)
2181 struct selector *s = &ctl->selectors[selector_id];
2184 /* Commit pending group member changes (s->pending_groups[group_id]) to the stable group
2185 * records (s->groups[group_id).
2187 for (group_id = 0; group_id < s->info.n_groups_max; group_id++) {
2188 struct rte_swx_table_selector_group *g = s->groups[group_id];
2189 struct rte_swx_table_selector_group *gp = s->pending_groups[group_id];
2191 /* Skip this group if no change needed. */
2195 /* Transition the pending changes to stable. */
2196 s->groups[group_id] = gp;
2197 s->pending_groups[group_id] = NULL;
2199 /* Free the old group member list. */
2204 struct rte_swx_table_selector_member *m;
2206 m = TAILQ_FIRST(&g->members);
2210 TAILQ_REMOVE(&g->members, m, node);
2217 /* Commit pending group validity changes (from s->groups_pending_delete[group_id] to
2218 * s->groups_added[group_id].
2220 for (group_id = 0; group_id < s->info.n_groups_max; group_id++)
2221 if (s->groups_pending_delete[group_id]) {
2222 s->groups_added[group_id] = 0;
2223 s->groups_pending_delete[group_id] = 0;
2228 selector_rollback(struct rte_swx_ctl_pipeline *ctl, uint32_t selector_id)
2230 struct selector *s = &ctl->selectors[selector_id];
2231 struct rte_swx_table_state *ts = &ctl->ts[ctl->info.n_tables + selector_id];
2232 struct rte_swx_table_state *ts_next = &ctl->ts_next[ctl->info.n_tables + selector_id];
2235 /* Discard any previous changes to the selector table mirror copy (ts_next->obj). */
2236 for (group_id = 0; group_id < s->info.n_groups_max; group_id++) {
2237 struct rte_swx_table_selector_group *gp = s->pending_groups[group_id];
2240 ts_next->obj = ts->obj;
2247 selector_abort(struct rte_swx_ctl_pipeline *ctl, uint32_t selector_id)
2249 struct selector *s = &ctl->selectors[selector_id];
2252 /* Discard any pending group member changes (s->pending_groups[group_id]). */
2253 for (group_id = 0; group_id < s->info.n_groups_max; group_id++)
2254 selector_pending_group_members_free(s, group_id);
2256 /* Discard any pending group deletions. */
2257 memset(s->groups_pending_delete, 0, s->info.n_groups_max * sizeof(int));
2260 static struct rte_swx_table_entry *
2261 learner_default_entry_alloc(struct learner *l)
2263 struct rte_swx_table_entry *entry;
2265 entry = calloc(1, sizeof(struct rte_swx_table_entry));
2270 if (l->action_data_size) {
2271 entry->action_data = calloc(1, l->action_data_size);
2272 if (!entry->action_data)
2279 table_entry_free(entry);
2284 learner_default_entry_check(struct rte_swx_ctl_pipeline *ctl,
2285 uint32_t learner_id,
2286 struct rte_swx_table_entry *entry)
2288 struct learner *l = &ctl->learners[learner_id];
2292 CHECK(entry, EINVAL);
2295 for (i = 0; i < l->info.n_actions; i++)
2296 if (entry->action_id == l->actions[i].action_id)
2299 CHECK(i < l->info.n_actions, EINVAL);
2302 a = &ctl->actions[entry->action_id];
2303 CHECK(!(a->data_size && !entry->action_data), EINVAL);
2308 static struct rte_swx_table_entry *
2309 learner_default_entry_duplicate(struct rte_swx_ctl_pipeline *ctl,
2310 uint32_t learner_id,
2311 struct rte_swx_table_entry *entry)
2313 struct learner *l = &ctl->learners[learner_id];
2314 struct rte_swx_table_entry *new_entry = NULL;
2321 new_entry = calloc(1, sizeof(struct rte_swx_table_entry));
2326 for (i = 0; i < l->info.n_actions; i++)
2327 if (entry->action_id == l->actions[i].action_id)
2330 if (i >= l->info.n_actions)
2333 new_entry->action_id = entry->action_id;
2336 a = &ctl->actions[entry->action_id];
2337 if (a->data_size && !entry->action_data)
2340 /* The table layer provisions a constant action data size per
2341 * entry, which should be the largest data size for all the
2342 * actions enabled for the current table, and attempts to copy
2343 * this many bytes each time a table entry is added, even if the
2344 * specific action requires less data or even no data at all,
2345 * hence we always have to allocate the max.
2347 new_entry->action_data = calloc(1, l->action_data_size);
2348 if (!new_entry->action_data)
2352 memcpy(new_entry->action_data, entry->action_data, a->data_size);
2357 table_entry_free(new_entry);
2362 rte_swx_ctl_pipeline_learner_default_entry_add(struct rte_swx_ctl_pipeline *ctl,
2363 const char *learner_name,
2364 struct rte_swx_table_entry *entry)
2367 struct rte_swx_table_entry *new_entry;
2368 uint32_t learner_id;
2372 CHECK(learner_name && learner_name[0], EINVAL);
2373 l = learner_find(ctl, learner_name);
2375 learner_id = l - ctl->learners;
2376 CHECK(!l->info.default_action_is_const, EINVAL);
2378 CHECK(entry, EINVAL);
2379 CHECK(!learner_default_entry_check(ctl, learner_id, entry), EINVAL);
2381 new_entry = learner_default_entry_duplicate(ctl, learner_id, entry);
2382 CHECK(new_entry, ENOMEM);
2384 learner_pending_default_free(l);
2386 l->pending_default = new_entry;
2391 learner_rollfwd(struct rte_swx_ctl_pipeline *ctl, uint32_t learner_id)
2393 struct learner *l = &ctl->learners[learner_id];
2394 struct rte_swx_table_state *ts_next = &ctl->ts_next[ctl->info.n_tables +
2395 ctl->info.n_selectors + learner_id];
2397 uint8_t *action_data;
2400 /* Copy the pending default entry. */
2401 if (!l->pending_default)
2404 action_id = l->pending_default->action_id;
2405 action_data = l->pending_default->action_data;
2406 a = &ctl->actions[action_id];
2409 memcpy(ts_next->default_action_data, action_data, a->data_size);
2411 ts_next->default_action_id = action_id;
2415 learner_rollfwd_finalize(struct rte_swx_ctl_pipeline *ctl, uint32_t learner_id)
2417 struct learner *l = &ctl->learners[learner_id];
2419 /* Free up the pending default entry, as it is now part of the table. */
2420 learner_pending_default_free(l);
2424 learner_abort(struct rte_swx_ctl_pipeline *ctl, uint32_t learner_id)
2426 struct learner *l = &ctl->learners[learner_id];
2428 /* Free up the pending default entry, as it is no longer going to be added to the table. */
2429 learner_pending_default_free(l);
2433 rte_swx_ctl_pipeline_commit(struct rte_swx_ctl_pipeline *ctl, int abort_on_fail)
2435 struct rte_swx_table_state *ts;
2441 /* Operate the changes on the current ts_next before it becomes the new ts. First, operate
2442 * all the changes that can fail; if no failure, then operate the changes that cannot fail.
2443 * We must be able to fully revert all the changes that can fail as if they never happened.
2445 for (i = 0; i < ctl->info.n_tables; i++) {
2446 status = table_rollfwd0(ctl, i, 0);
2451 for (i = 0; i < ctl->info.n_selectors; i++) {
2452 status = selector_rollfwd(ctl, i);
2457 /* Second, operate all the changes that cannot fail. Since nothing can fail from this point
2458 * onwards, the transaction is guaranteed to be successful.
2460 for (i = 0; i < ctl->info.n_tables; i++)
2461 table_rollfwd1(ctl, i);
2463 for (i = 0; i < ctl->info.n_learners; i++)
2464 learner_rollfwd(ctl, i);
2466 /* Swap the table state for the data plane. The current ts and ts_next
2467 * become the new ts_next and ts, respectively.
2469 rte_swx_pipeline_table_state_set(ctl->p, ctl->ts_next);
2472 ctl->ts = ctl->ts_next;
2475 /* Operate the changes on the current ts_next, which is the previous ts, in order to get
2476 * the current ts_next in sync with the current ts. Since the changes that can fail did
2477 * not fail on the previous ts_next, it is guaranteed that they will not fail on the
2478 * current ts_next, hence no error checking is needed.
2480 for (i = 0; i < ctl->info.n_tables; i++) {
2481 table_rollfwd0(ctl, i, 1);
2482 table_rollfwd1(ctl, i);
2483 table_rollfwd2(ctl, i);
2486 for (i = 0; i < ctl->info.n_selectors; i++) {
2487 selector_rollfwd(ctl, i);
2488 selector_rollfwd_finalize(ctl, i);
2491 for (i = 0; i < ctl->info.n_learners; i++) {
2492 learner_rollfwd(ctl, i);
2493 learner_rollfwd_finalize(ctl, i);
2499 for (i = 0; i < ctl->info.n_tables; i++) {
2500 table_rollback(ctl, i);
2502 table_abort(ctl, i);
2505 for (i = 0; i < ctl->info.n_selectors; i++) {
2506 selector_rollback(ctl, i);
2508 selector_abort(ctl, i);
2512 for (i = 0; i < ctl->info.n_learners; i++)
2513 learner_abort(ctl, i);
2519 rte_swx_ctl_pipeline_abort(struct rte_swx_ctl_pipeline *ctl)
2526 for (i = 0; i < ctl->info.n_tables; i++)
2527 table_abort(ctl, i);
2529 for (i = 0; i < ctl->info.n_selectors; i++)
2530 selector_abort(ctl, i);
2532 for (i = 0; i < ctl->info.n_learners; i++)
2533 learner_abort(ctl, i);
2537 mask_to_prefix(uint64_t mask, uint32_t mask_length, uint32_t *prefix_length)
2539 uint32_t n_trailing_zeros = 0, n_ones = 0, i;
2546 /* Count trailing zero bits. */
2547 for (i = 0; i < 64; i++) {
2548 if (mask & (1LLU << i))
2554 /* Count the one bits that follow. */
2555 for ( ; i < 64; i++) {
2556 if (!(mask & (1LLU << i)))
2562 /* Check that no more one bits are present */
2563 for ( ; i < 64; i++)
2564 if (mask & (1LLU << i))
2567 /* Check that the input mask is a prefix or the right length. */
2568 if (n_ones + n_trailing_zeros != mask_length)
2571 *prefix_length = n_ones;
2576 token_is_comment(const char *token)
2578 if ((token[0] == '#') ||
2579 (token[0] == ';') ||
2580 ((token[0] == '/') && (token[1] == '/')))
2581 return 1; /* TRUE. */
2583 return 0; /* FALSE. */
2586 #define RTE_SWX_CTL_ENTRY_TOKENS_MAX 256
2588 struct rte_swx_table_entry *
2589 rte_swx_ctl_pipeline_table_entry_read(struct rte_swx_ctl_pipeline *ctl,
2590 const char *table_name,
2592 int *is_blank_or_comment)
2594 char *token_array[RTE_SWX_CTL_ENTRY_TOKENS_MAX], **tokens;
2595 struct table *table;
2596 struct action *action;
2597 struct rte_swx_table_entry *entry = NULL;
2598 char *s0 = NULL, *s;
2599 uint32_t n_tokens = 0, arg_offset = 0, lpm_prefix_length_max = 0, lpm_prefix_length = 0, i;
2600 int lpm = 0, blank_or_comment = 0;
2602 /* Check input arguments. */
2606 if (!table_name || !table_name[0])
2609 table = table_find(ctl, table_name);
2613 if (!string || !string[0])
2616 /* Memory allocation. */
2617 s0 = strdup(string);
2621 entry = table_entry_alloc(table);
2625 /* Parse the string into tokens. */
2629 token = strtok_r(s, " \f\n\r\t\v", &s);
2630 if (!token || token_is_comment(token))
2633 if (n_tokens >= RTE_SWX_CTL_ENTRY_TOKENS_MAX)
2636 token_array[n_tokens] = token;
2641 blank_or_comment = 1;
2645 tokens = token_array;
2650 if (!(n_tokens && !strcmp(tokens[0], "match")))
2653 if (n_tokens < 1 + table->info.n_match_fields)
2656 for (i = 0; i < table->info.n_match_fields; i++) {
2657 struct rte_swx_ctl_table_match_field_info *mf = &table->mf[i];
2658 char *mf_val = tokens[1 + i], *mf_mask = NULL;
2659 uint64_t val, mask = UINT64_MAX;
2660 uint32_t offset = (mf->offset - table->mf_first->offset) / 8;
2665 mf_mask = strchr(mf_val, '/');
2671 mask = strtoull(mf_mask, &mf_mask, 0);
2676 if (mf->match_type == RTE_SWX_TABLE_MATCH_LPM) {
2681 lpm_prefix_length_max = mf->n_bits;
2683 status = mask_to_prefix(mask, mf->n_bits, &lpm_prefix_length);
2688 /* Endianness conversion. */
2690 mask = field_hton(mask, mf->n_bits);
2693 /* Copy to entry. */
2694 if (entry->key_mask)
2695 memcpy(&entry->key_mask[offset],
2703 val = strtoull(mf_val, &mf_val, 0);
2707 /* Endianness conversion. */
2709 val = field_hton(val, mf->n_bits);
2711 /* Copy to entry. */
2712 memcpy(&entry->key[offset],
2717 tokens += 1 + table->info.n_match_fields;
2718 n_tokens -= 1 + table->info.n_match_fields;
2723 if (n_tokens && !strcmp(tokens[0], "priority")) {
2724 char *priority = tokens[1];
2731 val = strtoul(priority, &priority, 0);
2735 /* Copy to entry. */
2736 entry->key_priority = val;
2744 entry->key_priority = lpm_prefix_length_max - lpm_prefix_length;
2750 if (!(n_tokens && !strcmp(tokens[0], "action")))
2756 action = action_find(ctl, tokens[1]);
2760 if (n_tokens < 2 + action->info.n_args * 2)
2764 entry->action_id = action - ctl->actions;
2767 for (i = 0; i < action->info.n_args; i++) {
2768 struct rte_swx_ctl_action_arg_info *arg = &action->args[i];
2769 char *arg_name, *arg_val;
2772 arg_name = tokens[2 + i * 2];
2773 arg_val = tokens[2 + i * 2 + 1];
2775 if (strcmp(arg_name, arg->name))
2778 val = strtoull(arg_val, &arg_val, 0);
2782 /* Endianness conversion. */
2783 if (arg->is_network_byte_order)
2784 val = field_hton(val, arg->n_bits);
2786 /* Copy to entry. */
2787 memcpy(&entry->action_data[arg_offset],
2791 arg_offset += arg->n_bits / 8;
2794 tokens += 2 + action->info.n_args * 2;
2795 n_tokens -= 2 + action->info.n_args * 2;
2805 table_entry_free(entry);
2807 if (is_blank_or_comment)
2808 *is_blank_or_comment = blank_or_comment;
2812 struct rte_swx_table_entry *
2813 rte_swx_ctl_pipeline_learner_default_entry_read(struct rte_swx_ctl_pipeline *ctl,
2814 const char *learner_name,
2816 int *is_blank_or_comment)
2818 char *token_array[RTE_SWX_CTL_ENTRY_TOKENS_MAX], **tokens;
2820 struct action *action;
2821 struct rte_swx_table_entry *entry = NULL;
2822 char *s0 = NULL, *s;
2823 uint32_t n_tokens = 0, arg_offset = 0, i;
2824 int blank_or_comment = 0;
2826 /* Check input arguments. */
2830 if (!learner_name || !learner_name[0])
2833 l = learner_find(ctl, learner_name);
2837 if (!string || !string[0])
2840 /* Memory allocation. */
2841 s0 = strdup(string);
2845 entry = learner_default_entry_alloc(l);
2849 /* Parse the string into tokens. */
2853 token = strtok_r(s, " \f\n\r\t\v", &s);
2854 if (!token || token_is_comment(token))
2857 if (n_tokens >= RTE_SWX_CTL_ENTRY_TOKENS_MAX)
2860 token_array[n_tokens] = token;
2865 blank_or_comment = 1;
2869 tokens = token_array;
2874 if (!(n_tokens && !strcmp(tokens[0], "action")))
2880 action = action_find(ctl, tokens[1]);
2884 if (n_tokens < 2 + action->info.n_args * 2)
2888 entry->action_id = action - ctl->actions;
2891 for (i = 0; i < action->info.n_args; i++) {
2892 struct rte_swx_ctl_action_arg_info *arg = &action->args[i];
2893 char *arg_name, *arg_val;
2896 arg_name = tokens[2 + i * 2];
2897 arg_val = tokens[2 + i * 2 + 1];
2899 if (strcmp(arg_name, arg->name))
2902 val = strtoull(arg_val, &arg_val, 0);
2906 /* Endianness conversion. */
2907 if (arg->is_network_byte_order)
2908 val = field_hton(val, arg->n_bits);
2910 /* Copy to entry. */
2911 memcpy(&entry->action_data[arg_offset],
2915 arg_offset += arg->n_bits / 8;
2918 tokens += 2 + action->info.n_args * 2;
2919 n_tokens -= 2 + action->info.n_args * 2;
2929 table_entry_free(entry);
2931 if (is_blank_or_comment)
2932 *is_blank_or_comment = blank_or_comment;
2937 table_entry_printf(FILE *f,
2938 struct rte_swx_ctl_pipeline *ctl,
2939 struct table *table,
2940 struct rte_swx_table_entry *entry)
2942 struct action *action = &ctl->actions[entry->action_id];
2945 fprintf(f, "match ");
2946 for (i = 0; i < table->params.key_size; i++)
2947 fprintf(f, "%02x", entry->key[i]);
2949 if (entry->key_mask) {
2951 for (i = 0; i < table->params.key_size; i++)
2952 fprintf(f, "%02x", entry->key_mask[i]);
2955 fprintf(f, " priority %u", entry->key_priority);
2957 fprintf(f, " action %s ", action->info.name);
2958 for (i = 0; i < action->data_size; i++)
2959 fprintf(f, "%02x", entry->action_data[i]);
2965 rte_swx_ctl_pipeline_table_fprintf(FILE *f,
2966 struct rte_swx_ctl_pipeline *ctl,
2967 const char *table_name)
2969 struct table *table;
2970 struct rte_swx_table_entry *entry;
2971 uint32_t n_entries = 0, i;
2973 if (!f || !ctl || !table_name || !table_name[0])
2976 table = table_find(ctl, table_name);
2981 fprintf(f, "# Table %s: key size %u bytes, key offset %u, key mask [",
2983 table->params.key_size,
2984 table->params.key_offset);
2986 for (i = 0; i < table->params.key_size; i++)
2987 fprintf(f, "%02x", table->params.key_mask0[i]);
2989 fprintf(f, "], action data size %u bytes\n",
2990 table->params.action_data_size);
2992 /* Table entries. */
2993 TAILQ_FOREACH(entry, &table->entries, node) {
2994 table_entry_printf(f, ctl, table, entry);
2998 TAILQ_FOREACH(entry, &table->pending_modify0, node) {
2999 table_entry_printf(f, ctl, table, entry);
3003 TAILQ_FOREACH(entry, &table->pending_delete, node) {
3004 table_entry_printf(f, ctl, table, entry);
3008 fprintf(f, "# Table %s currently has %u entries.\n",
3015 rte_swx_ctl_pipeline_selector_fprintf(FILE *f,
3016 struct rte_swx_ctl_pipeline *ctl,
3017 const char *selector_name)
3022 if (!f || !ctl || !selector_name || !selector_name[0])
3025 s = selector_find(ctl, selector_name);
3030 fprintf(f, "# Selector %s: max groups %u, max members per group %u\n",
3032 s->info.n_groups_max,
3033 s->info.n_members_per_group_max);
3036 for (group_id = 0; group_id < s->info.n_groups_max; group_id++) {
3037 struct rte_swx_table_selector_group *group = s->groups[group_id];
3038 struct rte_swx_table_selector_member *m;
3039 uint32_t n_members = 0;
3041 fprintf(f, "Group %u = [", group_id);
3043 /* Non-empty group. */
3045 TAILQ_FOREACH(m, &group->members, node) {
3046 fprintf(f, "%u:%u ", m->member_id, m->member_weight);