+static int
+action_does_learning(struct action *a)
+{
+ uint32_t i;
+
+ for (i = 0; i < a->n_instructions; i++)
+ switch (a->instructions[i].type) {
+ case INSTR_LEARNER_LEARN:
+ return 1; /* TRUE */
+
+ case INSTR_LEARNER_FORGET:
+ return 1; /* TRUE */
+
+ default:
+ continue;
+ }
+
+ return 0; /* FALSE */
+}
+
+int
+rte_swx_pipeline_action_config(struct rte_swx_pipeline *p,
+ const char *name,
+ const char *args_struct_type_name,
+ const char **instructions,
+ uint32_t n_instructions)
+{
+ struct struct_type *args_struct_type = NULL;
+ struct action *a;
+ int err;
+
+ CHECK(p, EINVAL);
+
+ CHECK_NAME(name, EINVAL);
+ CHECK(!action_find(p, name), EEXIST);
+
+ if (args_struct_type_name) {
+ CHECK_NAME(args_struct_type_name, EINVAL);
+ args_struct_type = struct_type_find(p, args_struct_type_name);
+ CHECK(args_struct_type, EINVAL);
+ CHECK(!args_struct_type->var_size, EINVAL);
+ }
+
+ /* Node allocation. */
+ a = calloc(1, sizeof(struct action));
+ CHECK(a, ENOMEM);
+ if (args_struct_type) {
+ a->args_endianness = calloc(args_struct_type->n_fields, sizeof(int));
+ if (!a->args_endianness) {
+ free(a);
+ CHECK(0, ENOMEM);
+ }
+ }
+
+ /* Node initialization. */
+ strcpy(a->name, name);
+ a->st = args_struct_type;
+ a->id = p->n_actions;
+
+ /* Instruction translation. */
+ err = instruction_config(p, a, instructions, n_instructions);
+ if (err) {
+ free(a->args_endianness);
+ free(a);
+ return err;
+ }
+
+ /* Node add to tailq. */
+ TAILQ_INSERT_TAIL(&p->actions, a, node);
+ p->n_actions++;
+
+ return 0;
+}
+
+static int
+action_build(struct rte_swx_pipeline *p)
+{
+ struct action *action;
+
+ /* p->action_instructions. */
+ p->action_instructions = calloc(p->n_actions, sizeof(struct instruction *));
+ CHECK(p->action_instructions, ENOMEM);
+
+ TAILQ_FOREACH(action, &p->actions, node)
+ p->action_instructions[action->id] = action->instructions;
+
+ /* p->action_funcs. */
+ p->action_funcs = calloc(p->n_actions, sizeof(action_func_t));
+ CHECK(p->action_funcs, ENOMEM);
+
+ return 0;
+}
+
+static void
+action_build_free(struct rte_swx_pipeline *p)
+{
+ free(p->action_funcs);
+ p->action_funcs = NULL;
+
+ free(p->action_instructions);
+ p->action_instructions = NULL;
+}
+
+static void
+action_free(struct rte_swx_pipeline *p)
+{
+ action_build_free(p);
+
+ for ( ; ; ) {
+ struct action *action;
+
+ action = TAILQ_FIRST(&p->actions);
+ if (!action)
+ break;
+
+ TAILQ_REMOVE(&p->actions, action, node);
+ free(action->instruction_data);
+ free(action->instructions);
+ free(action);
+ }
+}
+
+static uint32_t
+action_arg_src_mov_count(struct action *a,
+ uint32_t arg_id,
+ struct instruction *instructions,
+ struct instruction_data *instruction_data,
+ uint32_t n_instructions)
+{
+ uint32_t offset, n_users = 0, i;
+
+ if (!a->st ||
+ (arg_id >= a->st->n_fields) ||
+ !instructions ||
+ !instruction_data ||
+ !n_instructions)
+ return 0;
+
+ offset = a->st->fields[arg_id].offset / 8;
+
+ for (i = 0; i < n_instructions; i++) {
+ struct instruction *instr = &instructions[i];
+ struct instruction_data *data = &instruction_data[i];
+
+ if (data->invalid ||
+ ((instr->type != INSTR_MOV) && (instr->type != INSTR_MOV_HM)) ||
+ instr->mov.src.struct_id ||
+ (instr->mov.src.offset != offset))
+ continue;
+
+ n_users++;
+ }
+
+ return n_users;
+}
+
+/*
+ * Table.
+ */
+static struct table_type *
+table_type_find(struct rte_swx_pipeline *p, const char *name)
+{
+ struct table_type *elem;
+
+ TAILQ_FOREACH(elem, &p->table_types, node)
+ if (strcmp(elem->name, name) == 0)
+ return elem;
+
+ return NULL;
+}
+
+static struct table_type *
+table_type_resolve(struct rte_swx_pipeline *p,
+ const char *recommended_type_name,
+ enum rte_swx_table_match_type match_type)
+{
+ struct table_type *elem;
+
+ /* Only consider the recommended type if the match type is correct. */
+ if (recommended_type_name)
+ TAILQ_FOREACH(elem, &p->table_types, node)
+ if (!strcmp(elem->name, recommended_type_name) &&
+ (elem->match_type == match_type))
+ return elem;
+
+ /* Ignore the recommended type and get the first element with this match
+ * type.
+ */
+ TAILQ_FOREACH(elem, &p->table_types, node)
+ if (elem->match_type == match_type)
+ return elem;
+
+ return NULL;
+}
+
+static struct table *
+table_find(struct rte_swx_pipeline *p, const char *name)
+{
+ struct table *elem;
+
+ TAILQ_FOREACH(elem, &p->tables, node)
+ if (strcmp(elem->name, name) == 0)
+ return elem;
+
+ return NULL;
+}
+
+static struct table *
+table_find_by_id(struct rte_swx_pipeline *p, uint32_t id)
+{
+ struct table *table = NULL;
+
+ TAILQ_FOREACH(table, &p->tables, node)
+ if (table->id == id)
+ return table;
+
+ return NULL;
+}
+
+int
+rte_swx_pipeline_table_type_register(struct rte_swx_pipeline *p,
+ const char *name,
+ enum rte_swx_table_match_type match_type,
+ struct rte_swx_table_ops *ops)
+{
+ struct table_type *elem;
+
+ CHECK(p, EINVAL);
+
+ CHECK_NAME(name, EINVAL);
+ CHECK(!table_type_find(p, name), EEXIST);
+
+ CHECK(ops, EINVAL);
+ CHECK(ops->create, EINVAL);
+ CHECK(ops->lkp, EINVAL);
+ CHECK(ops->free, EINVAL);
+
+ /* Node allocation. */
+ elem = calloc(1, sizeof(struct table_type));
+ CHECK(elem, ENOMEM);
+
+ /* Node initialization. */
+ strcpy(elem->name, name);
+ elem->match_type = match_type;
+ memcpy(&elem->ops, ops, sizeof(*ops));
+
+ /* Node add to tailq. */
+ TAILQ_INSERT_TAIL(&p->table_types, elem, node);
+
+ return 0;
+}
+
+static int
+table_match_type_resolve(struct rte_swx_match_field_params *fields,
+ uint32_t n_fields,
+ enum rte_swx_table_match_type *match_type)
+{
+ uint32_t n_fields_em = 0, n_fields_lpm = 0, i;
+
+ for (i = 0; i < n_fields; i++) {
+ struct rte_swx_match_field_params *f = &fields[i];
+
+ if (f->match_type == RTE_SWX_TABLE_MATCH_EXACT)
+ n_fields_em++;
+
+ if (f->match_type == RTE_SWX_TABLE_MATCH_LPM)
+ n_fields_lpm++;
+ }
+
+ if ((n_fields_lpm > 1) ||
+ (n_fields_lpm && (n_fields_em != n_fields - 1)))
+ return -EINVAL;
+
+ *match_type = (n_fields_em == n_fields) ?
+ RTE_SWX_TABLE_MATCH_EXACT :
+ RTE_SWX_TABLE_MATCH_WILDCARD;
+
+ return 0;
+}
+
+static int
+table_match_fields_check(struct rte_swx_pipeline *p,
+ struct rte_swx_pipeline_table_params *params,
+ struct header **header)
+{
+ struct header *h0 = NULL;
+ struct field *hf, *mf;
+ uint32_t *offset = NULL, i;
+ int status = 0;
+
+ /* Return if no match fields. */
+ if (!params->n_fields) {
+ if (params->fields) {
+ status = -EINVAL;
+ goto end;
+ }
+
+ if (header)
+ *header = NULL;
+
+ return 0;
+ }
+
+ /* Memory allocation. */
+ offset = calloc(params->n_fields, sizeof(uint32_t));
+ if (!offset) {
+ status = -ENOMEM;
+ goto end;
+ }
+
+ /* Check that all the match fields belong to either the same header or
+ * to the meta-data.
+ */
+ hf = header_field_parse(p, params->fields[0].name, &h0);
+ mf = metadata_field_parse(p, params->fields[0].name);
+ if ((!hf && !mf) || (hf && hf->var_size)) {
+ status = -EINVAL;
+ goto end;
+ }
+
+ offset[0] = h0 ? hf->offset : mf->offset;
+
+ for (i = 1; i < params->n_fields; i++)
+ if (h0) {
+ struct header *h;
+
+ hf = header_field_parse(p, params->fields[i].name, &h);
+ if (!hf || (h->id != h0->id) || hf->var_size) {
+ status = -EINVAL;
+ goto end;
+ }
+
+ offset[i] = hf->offset;
+ } else {
+ mf = metadata_field_parse(p, params->fields[i].name);
+ if (!mf) {
+ status = -EINVAL;
+ goto end;
+ }
+
+ offset[i] = mf->offset;
+ }
+
+ /* Check that there are no duplicated match fields. */
+ for (i = 0; i < params->n_fields; i++) {
+ uint32_t j;
+
+ for (j = 0; j < i; j++)
+ if (offset[j] == offset[i]) {
+ status = -EINVAL;
+ goto end;
+ }
+ }
+
+ /* Return. */
+ if (header)
+ *header = h0;
+
+end:
+ free(offset);
+ return status;
+}
+
+int
+rte_swx_pipeline_table_config(struct rte_swx_pipeline *p,
+ const char *name,
+ struct rte_swx_pipeline_table_params *params,
+ const char *recommended_table_type_name,
+ const char *args,
+ uint32_t size)
+{
+ struct table_type *type;
+ struct table *t = NULL;
+ struct action *default_action;
+ struct header *header = NULL;
+ uint32_t action_data_size_max = 0, i;
+ int status = 0;
+
+ CHECK(p, EINVAL);
+
+ CHECK_NAME(name, EINVAL);
+ CHECK(!table_find(p, name), EEXIST);
+ CHECK(!selector_find(p, name), EEXIST);
+ CHECK(!learner_find(p, name), EEXIST);
+
+ CHECK(params, EINVAL);
+
+ /* Match checks. */
+ status = table_match_fields_check(p, params, &header);
+ if (status)
+ return status;
+
+ /* Action checks. */
+ CHECK(params->n_actions, EINVAL);
+ CHECK(params->action_names, EINVAL);
+ for (i = 0; i < params->n_actions; i++) {
+ const char *action_name = params->action_names[i];
+ struct action *a;
+ uint32_t action_data_size;
+ int action_is_for_table_entries = 1, action_is_for_default_entry = 1;
+
+ CHECK_NAME(action_name, EINVAL);
+
+ a = action_find(p, action_name);
+ CHECK(a, EINVAL);
+ CHECK(!action_does_learning(a), EINVAL);
+
+ action_data_size = a->st ? a->st->n_bits / 8 : 0;
+ if (action_data_size > action_data_size_max)
+ action_data_size_max = action_data_size;
+
+ if (params->action_is_for_table_entries)
+ action_is_for_table_entries = params->action_is_for_table_entries[i];
+ if (params->action_is_for_default_entry)
+ action_is_for_default_entry = params->action_is_for_default_entry[i];
+ CHECK(action_is_for_table_entries || action_is_for_default_entry, EINVAL);
+ }
+
+ CHECK_NAME(params->default_action_name, EINVAL);
+ for (i = 0; i < p->n_actions; i++)
+ if (!strcmp(params->action_names[i],
+ params->default_action_name))
+ break;
+ CHECK(i < params->n_actions, EINVAL);
+ CHECK(!params->action_is_for_default_entry || params->action_is_for_default_entry[i],
+ EINVAL);
+
+ default_action = action_find(p, params->default_action_name);
+ CHECK((default_action->st && params->default_action_data) ||
+ !params->default_action_data, EINVAL);
+
+ /* Table type checks. */
+ if (recommended_table_type_name)
+ CHECK_NAME(recommended_table_type_name, EINVAL);
+
+ if (params->n_fields) {
+ enum rte_swx_table_match_type match_type;
+
+ status = table_match_type_resolve(params->fields, params->n_fields, &match_type);
+ if (status)
+ return status;
+
+ type = table_type_resolve(p, recommended_table_type_name, match_type);
+ CHECK(type, EINVAL);
+ } else {
+ type = NULL;
+ }
+
+ /* Memory allocation. */
+ t = calloc(1, sizeof(struct table));
+ CHECK(t, ENOMEM);
+
+ t->fields = calloc(params->n_fields, sizeof(struct match_field));
+ if (!t->fields)
+ goto nomem;
+
+ t->actions = calloc(params->n_actions, sizeof(struct action *));
+ if (!t->actions)
+ goto nomem;
+
+ if (action_data_size_max) {
+ t->default_action_data = calloc(1, action_data_size_max);
+ if (!t->default_action_data)
+ goto nomem;
+ }
+
+ t->action_is_for_table_entries = calloc(params->n_actions, sizeof(int));
+ if (!t->action_is_for_table_entries)
+ goto nomem;
+
+ t->action_is_for_default_entry = calloc(params->n_actions, sizeof(int));
+ if (!t->action_is_for_default_entry)
+ goto nomem;
+
+ /* Node initialization. */
+ strcpy(t->name, name);
+ if (args && args[0])
+ strcpy(t->args, args);
+ t->type = type;
+
+ for (i = 0; i < params->n_fields; i++) {
+ struct rte_swx_match_field_params *field = ¶ms->fields[i];
+ struct match_field *f = &t->fields[i];
+
+ f->match_type = field->match_type;
+ f->field = header ?
+ header_field_parse(p, field->name, NULL) :
+ metadata_field_parse(p, field->name);
+ }
+ t->n_fields = params->n_fields;
+ t->header = header;
+
+ for (i = 0; i < params->n_actions; i++) {
+ int action_is_for_table_entries = 1, action_is_for_default_entry = 1;
+
+ if (params->action_is_for_table_entries)
+ action_is_for_table_entries = params->action_is_for_table_entries[i];
+ if (params->action_is_for_default_entry)
+ action_is_for_default_entry = params->action_is_for_default_entry[i];
+
+ t->actions[i] = action_find(p, params->action_names[i]);
+ t->action_is_for_table_entries[i] = action_is_for_table_entries;
+ t->action_is_for_default_entry[i] = action_is_for_default_entry;
+ }
+ t->default_action = default_action;
+ if (default_action->st)
+ memcpy(t->default_action_data,
+ params->default_action_data,
+ default_action->st->n_bits / 8);
+ t->n_actions = params->n_actions;
+ t->default_action_is_const = params->default_action_is_const;
+ t->action_data_size_max = action_data_size_max;
+
+ t->size = size;
+ t->id = p->n_tables;
+
+ /* Node add to tailq. */
+ TAILQ_INSERT_TAIL(&p->tables, t, node);
+ p->n_tables++;
+
+ return 0;
+
+nomem:
+ if (!t)
+ return -ENOMEM;
+
+ free(t->action_is_for_default_entry);
+ free(t->action_is_for_table_entries);
+ free(t->default_action_data);
+ free(t->actions);
+ free(t->fields);
+ free(t);
+
+ return -ENOMEM;
+}
+
+static struct rte_swx_table_params *
+table_params_get(struct table *table)
+{
+ struct rte_swx_table_params *params;
+ struct field *first, *last;
+ uint8_t *key_mask;
+ uint32_t key_size, key_offset, action_data_size, i;
+
+ /* Memory allocation. */
+ params = calloc(1, sizeof(struct rte_swx_table_params));
+ if (!params)
+ return NULL;
+
+ /* Find first (smallest offset) and last (biggest offset) match fields. */
+ first = table->fields[0].field;
+ last = table->fields[0].field;
+
+ for (i = 0; i < table->n_fields; i++) {
+ struct field *f = table->fields[i].field;
+
+ if (f->offset < first->offset)
+ first = f;
+
+ if (f->offset > last->offset)
+ last = f;
+ }
+
+ /* Key offset and size. */
+ key_offset = first->offset / 8;
+ key_size = (last->offset + last->n_bits - first->offset) / 8;
+
+ /* Memory allocation. */
+ key_mask = calloc(1, key_size);
+ if (!key_mask) {
+ free(params);
+ return NULL;
+ }
+
+ /* Key mask. */
+ for (i = 0; i < table->n_fields; i++) {
+ struct field *f = table->fields[i].field;
+ uint32_t start = (f->offset - first->offset) / 8;
+ size_t size = f->n_bits / 8;
+
+ memset(&key_mask[start], 0xFF, size);
+ }
+
+ /* Action data size. */
+ action_data_size = 0;
+ for (i = 0; i < table->n_actions; i++) {
+ struct action *action = table->actions[i];
+ uint32_t ads = action->st ? action->st->n_bits / 8 : 0;
+
+ if (ads > action_data_size)
+ action_data_size = ads;
+ }
+
+ /* Fill in. */
+ params->match_type = table->type->match_type;
+ params->key_size = key_size;
+ params->key_offset = key_offset;
+ params->key_mask0 = key_mask;
+ params->action_data_size = action_data_size;
+ params->n_keys_max = table->size;
+
+ return params;
+}
+
+static void
+table_params_free(struct rte_swx_table_params *params)
+{
+ if (!params)
+ return;
+
+ free(params->key_mask0);
+ free(params);
+}
+
+static int
+table_stub_lkp(void *table __rte_unused,
+ void *mailbox __rte_unused,
+ uint8_t **key __rte_unused,
+ uint64_t *action_id __rte_unused,
+ uint8_t **action_data __rte_unused,
+ int *hit)
+{
+ *hit = 0;
+ return 1; /* DONE. */
+}
+
+static int
+table_build(struct rte_swx_pipeline *p)
+{
+ uint32_t i;
+
+ /* Per pipeline: table statistics. */
+ p->table_stats = calloc(p->n_tables, sizeof(struct table_statistics));
+ CHECK(p->table_stats, ENOMEM);
+
+ for (i = 0; i < p->n_tables; i++) {
+ p->table_stats[i].n_pkts_action = calloc(p->n_actions, sizeof(uint64_t));
+ CHECK(p->table_stats[i].n_pkts_action, ENOMEM);
+ }
+
+ /* Per thread: table runt-time. */
+ for (i = 0; i < RTE_SWX_PIPELINE_THREADS_MAX; i++) {
+ struct thread *t = &p->threads[i];
+ struct table *table;
+
+ t->tables = calloc(p->n_tables, sizeof(struct table_runtime));
+ CHECK(t->tables, ENOMEM);
+
+ TAILQ_FOREACH(table, &p->tables, node) {
+ struct table_runtime *r = &t->tables[table->id];
+
+ if (table->type) {
+ uint64_t size;
+
+ size = table->type->ops.mailbox_size_get();
+
+ /* r->func. */
+ r->func = table->type->ops.lkp;
+
+ /* r->mailbox. */
+ if (size) {
+ r->mailbox = calloc(1, size);
+ CHECK(r->mailbox, ENOMEM);
+ }
+
+ /* r->key. */
+ r->key = table->header ?
+ &t->structs[table->header->struct_id] :
+ &t->structs[p->metadata_struct_id];
+ } else {
+ r->func = table_stub_lkp;
+ }
+ }
+ }
+
+ return 0;
+}
+
+static void
+table_build_free(struct rte_swx_pipeline *p)
+{
+ uint32_t i;
+
+ for (i = 0; i < RTE_SWX_PIPELINE_THREADS_MAX; i++) {
+ struct thread *t = &p->threads[i];
+ uint32_t j;
+
+ if (!t->tables)
+ continue;
+
+ for (j = 0; j < p->n_tables; j++) {
+ struct table_runtime *r = &t->tables[j];
+
+ free(r->mailbox);
+ }
+
+ free(t->tables);
+ t->tables = NULL;
+ }
+
+ if (p->table_stats) {
+ for (i = 0; i < p->n_tables; i++)
+ free(p->table_stats[i].n_pkts_action);
+
+ free(p->table_stats);
+ }
+}
+
+static void
+table_free(struct rte_swx_pipeline *p)
+{
+ table_build_free(p);
+
+ /* Tables. */
+ for ( ; ; ) {
+ struct table *elem;
+
+ elem = TAILQ_FIRST(&p->tables);
+ if (!elem)
+ break;
+
+ TAILQ_REMOVE(&p->tables, elem, node);
+ free(elem->fields);
+ free(elem->actions);
+ free(elem->default_action_data);
+ free(elem);
+ }
+
+ /* Table types. */
+ for ( ; ; ) {
+ struct table_type *elem;
+
+ elem = TAILQ_FIRST(&p->table_types);
+ if (!elem)
+ break;
+
+ TAILQ_REMOVE(&p->table_types, elem, node);
+ free(elem);
+ }
+}
+
+/*
+ * Selector.
+ */
+static struct selector *
+selector_find(struct rte_swx_pipeline *p, const char *name)
+{
+ struct selector *s;
+
+ TAILQ_FOREACH(s, &p->selectors, node)
+ if (strcmp(s->name, name) == 0)
+ return s;
+
+ return NULL;
+}
+
+static struct selector *
+selector_find_by_id(struct rte_swx_pipeline *p, uint32_t id)
+{
+ struct selector *s = NULL;
+
+ TAILQ_FOREACH(s, &p->selectors, node)
+ if (s->id == id)
+ return s;
+
+ return NULL;
+}
+
+static int
+selector_fields_check(struct rte_swx_pipeline *p,
+ struct rte_swx_pipeline_selector_params *params,
+ struct header **header)
+{
+ struct header *h0 = NULL;
+ struct field *hf, *mf;
+ uint32_t i;
+
+ /* Return if no selector fields. */
+ if (!params->n_selector_fields || !params->selector_field_names)
+ return -EINVAL;
+
+ /* Check that all the selector fields either belong to the same header
+ * or are all meta-data fields.
+ */
+ hf = header_field_parse(p, params->selector_field_names[0], &h0);
+ mf = metadata_field_parse(p, params->selector_field_names[0]);
+ if (!hf && !mf)
+ return -EINVAL;
+
+ for (i = 1; i < params->n_selector_fields; i++)
+ if (h0) {
+ struct header *h;
+
+ hf = header_field_parse(p, params->selector_field_names[i], &h);
+ if (!hf || (h->id != h0->id))
+ return -EINVAL;
+ } else {
+ mf = metadata_field_parse(p, params->selector_field_names[i]);
+ if (!mf)
+ return -EINVAL;
+ }
+
+ /* Check that there are no duplicated match fields. */
+ for (i = 0; i < params->n_selector_fields; i++) {
+ const char *field_name = params->selector_field_names[i];
+ uint32_t j;
+
+ for (j = i + 1; j < params->n_selector_fields; j++)
+ if (!strcmp(params->selector_field_names[j], field_name))
+ return -EINVAL;
+ }
+
+ /* Return. */
+ if (header)
+ *header = h0;
+
+ return 0;
+}
+
+int
+rte_swx_pipeline_selector_config(struct rte_swx_pipeline *p,
+ const char *name,
+ struct rte_swx_pipeline_selector_params *params)
+{
+ struct selector *s;
+ struct header *selector_header = NULL;
+ struct field *group_id_field, *member_id_field;
+ uint32_t i;
+ int status = 0;
+
+ CHECK(p, EINVAL);
+
+ CHECK_NAME(name, EINVAL);
+ CHECK(!table_find(p, name), EEXIST);
+ CHECK(!selector_find(p, name), EEXIST);
+ CHECK(!learner_find(p, name), EEXIST);
+
+ CHECK(params, EINVAL);
+
+ CHECK_NAME(params->group_id_field_name, EINVAL);
+ group_id_field = metadata_field_parse(p, params->group_id_field_name);
+ CHECK(group_id_field, EINVAL);
+
+ for (i = 0; i < params->n_selector_fields; i++) {
+ const char *field_name = params->selector_field_names[i];
+
+ CHECK_NAME(field_name, EINVAL);
+ }
+ status = selector_fields_check(p, params, &selector_header);
+ if (status)
+ return status;
+
+ CHECK_NAME(params->member_id_field_name, EINVAL);
+ member_id_field = metadata_field_parse(p, params->member_id_field_name);
+ CHECK(member_id_field, EINVAL);
+
+ CHECK(params->n_groups_max, EINVAL);
+
+ CHECK(params->n_members_per_group_max, EINVAL);
+
+ /* Memory allocation. */
+ s = calloc(1, sizeof(struct selector));
+ if (!s) {
+ status = -ENOMEM;
+ goto error;
+ }
+
+ s->selector_fields = calloc(params->n_selector_fields, sizeof(struct field *));
+ if (!s->selector_fields) {
+ status = -ENOMEM;
+ goto error;
+ }
+
+ /* Node initialization. */
+ strcpy(s->name, name);
+
+ s->group_id_field = group_id_field;
+
+ for (i = 0; i < params->n_selector_fields; i++) {
+ const char *field_name = params->selector_field_names[i];
+
+ s->selector_fields[i] = selector_header ?
+ header_field_parse(p, field_name, NULL) :
+ metadata_field_parse(p, field_name);
+ }
+
+ s->n_selector_fields = params->n_selector_fields;
+
+ s->selector_header = selector_header;
+
+ s->member_id_field = member_id_field;
+
+ s->n_groups_max = params->n_groups_max;
+
+ s->n_members_per_group_max = params->n_members_per_group_max;
+
+ s->id = p->n_selectors;
+
+ /* Node add to tailq. */
+ TAILQ_INSERT_TAIL(&p->selectors, s, node);
+ p->n_selectors++;
+
+ return 0;
+
+error:
+ if (!s)
+ return status;
+
+ free(s->selector_fields);
+
+ free(s);
+
+ return status;
+}
+
+static void
+selector_params_free(struct rte_swx_table_selector_params *params)
+{
+ if (!params)
+ return;
+
+ free(params->selector_mask);
+
+ free(params);
+}
+
+static struct rte_swx_table_selector_params *
+selector_table_params_get(struct selector *s)
+{
+ struct rte_swx_table_selector_params *params = NULL;
+ struct field *first, *last;
+ uint32_t i;
+
+ /* Memory allocation. */
+ params = calloc(1, sizeof(struct rte_swx_table_selector_params));
+ if (!params)
+ goto error;
+
+ /* Group ID. */
+ params->group_id_offset = s->group_id_field->offset / 8;
+
+ /* Find first (smallest offset) and last (biggest offset) selector fields. */
+ first = s->selector_fields[0];
+ last = s->selector_fields[0];
+
+ for (i = 0; i < s->n_selector_fields; i++) {
+ struct field *f = s->selector_fields[i];
+
+ if (f->offset < first->offset)
+ first = f;
+
+ if (f->offset > last->offset)
+ last = f;
+ }
+
+ /* Selector offset and size. */
+ params->selector_offset = first->offset / 8;
+ params->selector_size = (last->offset + last->n_bits - first->offset) / 8;
+
+ /* Memory allocation. */
+ params->selector_mask = calloc(1, params->selector_size);
+ if (!params->selector_mask)
+ goto error;
+
+ /* Selector mask. */
+ for (i = 0; i < s->n_selector_fields; i++) {
+ struct field *f = s->selector_fields[i];
+ uint32_t start = (f->offset - first->offset) / 8;
+ size_t size = f->n_bits / 8;
+
+ memset(¶ms->selector_mask[start], 0xFF, size);
+ }
+
+ /* Member ID. */
+ params->member_id_offset = s->member_id_field->offset / 8;
+
+ /* Maximum number of groups. */
+ params->n_groups_max = s->n_groups_max;
+
+ /* Maximum number of members per group. */
+ params->n_members_per_group_max = s->n_members_per_group_max;
+
+ return params;
+
+error:
+ selector_params_free(params);
+ return NULL;
+}
+
+static void
+selector_build_free(struct rte_swx_pipeline *p)
+{
+ uint32_t i;
+
+ for (i = 0; i < RTE_SWX_PIPELINE_THREADS_MAX; i++) {
+ struct thread *t = &p->threads[i];
+ uint32_t j;
+
+ if (!t->selectors)
+ continue;
+
+ for (j = 0; j < p->n_selectors; j++) {
+ struct selector_runtime *r = &t->selectors[j];
+
+ free(r->mailbox);
+ }
+
+ free(t->selectors);
+ t->selectors = NULL;
+ }
+
+ free(p->selector_stats);
+ p->selector_stats = NULL;
+}
+
+static int
+selector_build(struct rte_swx_pipeline *p)
+{
+ uint32_t i;
+ int status = 0;
+
+ /* Per pipeline: selector statistics. */
+ p->selector_stats = calloc(p->n_selectors, sizeof(struct selector_statistics));
+ if (!p->selector_stats) {
+ status = -ENOMEM;
+ goto error;
+ }
+
+ /* Per thread: selector run-time. */
+ for (i = 0; i < RTE_SWX_PIPELINE_THREADS_MAX; i++) {
+ struct thread *t = &p->threads[i];
+ struct selector *s;
+
+ t->selectors = calloc(p->n_selectors, sizeof(struct selector_runtime));
+ if (!t->selectors) {
+ status = -ENOMEM;
+ goto error;
+ }
+
+ TAILQ_FOREACH(s, &p->selectors, node) {
+ struct selector_runtime *r = &t->selectors[s->id];
+ uint64_t size;
+
+ /* r->mailbox. */
+ size = rte_swx_table_selector_mailbox_size_get();
+ if (size) {
+ r->mailbox = calloc(1, size);
+ if (!r->mailbox) {
+ status = -ENOMEM;
+ goto error;
+ }
+ }
+
+ /* r->group_id_buffer. */
+ r->group_id_buffer = &t->structs[p->metadata_struct_id];
+
+ /* r->selector_buffer. */
+ r->selector_buffer = s->selector_header ?
+ &t->structs[s->selector_header->struct_id] :
+ &t->structs[p->metadata_struct_id];
+
+ /* r->member_id_buffer. */
+ r->member_id_buffer = &t->structs[p->metadata_struct_id];
+ }
+ }
+
+ return 0;
+
+error:
+ selector_build_free(p);
+ return status;
+}
+
+static void
+selector_free(struct rte_swx_pipeline *p)
+{
+ selector_build_free(p);
+
+ /* Selector tables. */
+ for ( ; ; ) {
+ struct selector *elem;
+
+ elem = TAILQ_FIRST(&p->selectors);
+ if (!elem)
+ break;
+
+ TAILQ_REMOVE(&p->selectors, elem, node);
+ free(elem->selector_fields);
+ free(elem);
+ }
+}
+
+/*
+ * Learner table.
+ */
+static struct learner *
+learner_find(struct rte_swx_pipeline *p, const char *name)
+{
+ struct learner *l;
+
+ TAILQ_FOREACH(l, &p->learners, node)
+ if (!strcmp(l->name, name))
+ return l;
+
+ return NULL;
+}
+
+static struct learner *
+learner_find_by_id(struct rte_swx_pipeline *p, uint32_t id)
+{
+ struct learner *l = NULL;
+
+ TAILQ_FOREACH(l, &p->learners, node)
+ if (l->id == id)
+ return l;
+
+ return NULL;
+}
+
+static int
+learner_match_fields_check(struct rte_swx_pipeline *p,
+ struct rte_swx_pipeline_learner_params *params,
+ struct header **header)
+{
+ struct header *h0 = NULL;
+ struct field *hf, *mf;
+ uint32_t i;
+
+ /* Return if no match fields. */
+ if (!params->n_fields || !params->field_names)
+ return -EINVAL;
+
+ /* Check that all the match fields either belong to the same header
+ * or are all meta-data fields.
+ */
+ hf = header_field_parse(p, params->field_names[0], &h0);
+ mf = metadata_field_parse(p, params->field_names[0]);
+ if (!hf && !mf)
+ return -EINVAL;
+
+ for (i = 1; i < params->n_fields; i++)
+ if (h0) {
+ struct header *h;
+
+ hf = header_field_parse(p, params->field_names[i], &h);
+ if (!hf || (h->id != h0->id))
+ return -EINVAL;
+ } else {
+ mf = metadata_field_parse(p, params->field_names[i]);
+ if (!mf)
+ return -EINVAL;
+ }
+
+ /* Check that there are no duplicated match fields. */
+ for (i = 0; i < params->n_fields; i++) {
+ const char *field_name = params->field_names[i];
+ uint32_t j;
+
+ for (j = i + 1; j < params->n_fields; j++)
+ if (!strcmp(params->field_names[j], field_name))
+ return -EINVAL;
+ }
+
+ /* Return. */
+ if (header)
+ *header = h0;
+
+ return 0;
+}
+
+static int
+learner_action_args_check(struct rte_swx_pipeline *p, struct action *a, const char *mf_name)
+{
+ struct struct_type *mst = p->metadata_st, *ast = a->st;
+ struct field *mf, *af;
+ uint32_t mf_pos, i;
+
+ if (!ast) {
+ if (mf_name)
+ return -EINVAL;
+
+ return 0;
+ }
+
+ /* Check that mf_name is the name of a valid meta-data field. */
+ CHECK_NAME(mf_name, EINVAL);
+ mf = metadata_field_parse(p, mf_name);
+ CHECK(mf, EINVAL);
+
+ /* Check that there are enough meta-data fields, starting with the mf_name field, to cover
+ * all the action arguments.
+ */
+ mf_pos = mf - mst->fields;
+ CHECK(mst->n_fields - mf_pos >= ast->n_fields, EINVAL);
+
+ /* Check that the size of each of the identified meta-data fields matches exactly the size
+ * of the corresponding action argument.
+ */
+ for (i = 0; i < ast->n_fields; i++) {
+ mf = &mst->fields[mf_pos + i];
+ af = &ast->fields[i];
+
+ CHECK(mf->n_bits == af->n_bits, EINVAL);
+ }
+
+ return 0;
+}
+
+static int
+learner_action_learning_check(struct rte_swx_pipeline *p,
+ struct action *action,
+ const char **action_names,
+ uint32_t n_actions)
+{
+ uint32_t i;
+
+ /* For each "learn" instruction of the current action, check that the learned action (i.e.
+ * the action passed as argument to the "learn" instruction) is also enabled for the
+ * current learner table.
+ */
+ for (i = 0; i < action->n_instructions; i++) {
+ struct instruction *instr = &action->instructions[i];
+ uint32_t found = 0, j;
+
+ if (instr->type != INSTR_LEARNER_LEARN)
+ continue;
+
+ for (j = 0; j < n_actions; j++) {
+ struct action *a;
+
+ a = action_find(p, action_names[j]);
+ if (!a)
+ return -EINVAL;
+
+ if (a->id == instr->learn.action_id)
+ found = 1;
+ }
+
+ if (!found)
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+int
+rte_swx_pipeline_learner_config(struct rte_swx_pipeline *p,
+ const char *name,
+ struct rte_swx_pipeline_learner_params *params,
+ uint32_t size,
+ uint32_t timeout)
+{
+ struct learner *l = NULL;
+ struct action *default_action;
+ struct header *header = NULL;
+ uint32_t action_data_size_max = 0, i;
+ int status = 0;
+
+ CHECK(p, EINVAL);
+
+ CHECK_NAME(name, EINVAL);
+ CHECK(!table_find(p, name), EEXIST);
+ CHECK(!selector_find(p, name), EEXIST);
+ CHECK(!learner_find(p, name), EEXIST);
+
+ CHECK(params, EINVAL);
+
+ /* Match checks. */
+ status = learner_match_fields_check(p, params, &header);
+ if (status)
+ return status;
+
+ /* Action checks. */
+ CHECK(params->n_actions, EINVAL);
+ CHECK(params->action_names, EINVAL);
+ for (i = 0; i < params->n_actions; i++) {
+ const char *action_name = params->action_names[i];
+ struct action *a;
+ uint32_t action_data_size;
+ int action_is_for_table_entries = 1, action_is_for_default_entry = 1;
+
+ CHECK_NAME(action_name, EINVAL);
+
+ a = action_find(p, action_name);
+ CHECK(a, EINVAL);
+
+ status = learner_action_learning_check(p,
+ a,
+ params->action_names,
+ params->n_actions);
+ if (status)
+ return status;