+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));
+ if (!t)
+ goto nomem;
+
+ 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;
+
+ 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);
+
+ /* Any other checks. */
+ CHECK(size, EINVAL);
+ CHECK(timeout, EINVAL);
+
+ /* Memory allocation. */
+ l = calloc(1, sizeof(struct learner));
+ if (!l)
+ goto nomem;
+
+ l->fields = calloc(params->n_fields, sizeof(struct field *));
+ if (!l->fields)
+ goto nomem;
+
+ l->actions = calloc(params->n_actions, sizeof(struct action *));
+ if (!l->actions)
+ goto nomem;
+
+ if (action_data_size_max) {
+ l->default_action_data = calloc(1, action_data_size_max);
+ if (!l->default_action_data)
+ goto nomem;
+ }
+
+ l->action_is_for_table_entries = calloc(params->n_actions, sizeof(int));
+ if (!l->action_is_for_table_entries)
+ goto nomem;
+
+ l->action_is_for_default_entry = calloc(params->n_actions, sizeof(int));
+ if (!l->action_is_for_default_entry)
+ goto nomem;
+
+ /* Node initialization. */
+ strcpy(l->name, name);
+
+ for (i = 0; i < params->n_fields; i++) {
+ const char *field_name = params->field_names[i];
+
+ l->fields[i] = header ?
+ header_field_parse(p, field_name, NULL) :
+ metadata_field_parse(p, field_name);
+ }
+
+ l->n_fields = params->n_fields;
+
+ l->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];
+
+ l->actions[i] = action_find(p, params->action_names[i]);
+ l->action_is_for_table_entries[i] = action_is_for_table_entries;
+ l->action_is_for_default_entry[i] = action_is_for_default_entry;
+ }
+
+ l->default_action = default_action;
+
+ if (default_action->st)
+ memcpy(l->default_action_data,
+ params->default_action_data,
+ default_action->st->n_bits / 8);
+
+ l->n_actions = params->n_actions;
+
+ l->default_action_is_const = params->default_action_is_const;
+
+ l->action_data_size_max = action_data_size_max;
+
+ l->size = size;
+
+ l->timeout = timeout;
+
+ l->id = p->n_learners;
+
+ /* Node add to tailq. */
+ TAILQ_INSERT_TAIL(&p->learners, l, node);
+ p->n_learners++;
+
+ return 0;
+
+nomem:
+ if (!l)
+ return -ENOMEM;
+
+ free(l->action_is_for_default_entry);
+ free(l->action_is_for_table_entries);
+ free(l->default_action_data);
+ free(l->actions);
+ free(l->fields);
+ free(l);
+
+ return -ENOMEM;
+}
+
+static void
+learner_params_free(struct rte_swx_table_learner_params *params)
+{
+ if (!params)
+ return;
+
+ free(params->key_mask0);
+
+ free(params);
+}
+
+static struct rte_swx_table_learner_params *
+learner_params_get(struct learner *l)
+{
+ struct rte_swx_table_learner_params *params = NULL;
+ struct field *first, *last;
+ uint32_t i;
+
+ /* Memory allocation. */
+ params = calloc(1, sizeof(struct rte_swx_table_learner_params));
+ if (!params)
+ goto error;
+
+ /* Find first (smallest offset) and last (biggest offset) match fields. */
+ first = l->fields[0];
+ last = l->fields[0];
+
+ for (i = 0; i < l->n_fields; i++) {
+ struct field *f = l->fields[i];
+
+ if (f->offset < first->offset)
+ first = f;
+
+ if (f->offset > last->offset)
+ last = f;
+ }
+
+ /* Key offset and size. */
+ params->key_offset = first->offset / 8;
+ params->key_size = (last->offset + last->n_bits - first->offset) / 8;
+
+ /* Memory allocation. */
+ params->key_mask0 = calloc(1, params->key_size);
+ if (!params->key_mask0)
+ goto error;
+
+ /* Key mask. */
+ for (i = 0; i < l->n_fields; i++) {
+ struct field *f = l->fields[i];
+ uint32_t start = (f->offset - first->offset) / 8;
+ size_t size = f->n_bits / 8;
+
+ memset(¶ms->key_mask0[start], 0xFF, size);
+ }
+
+ /* Action data size. */
+ params->action_data_size = l->action_data_size_max;
+
+ /* Maximum number of keys. */
+ params->n_keys_max = l->size;
+
+ /* Timeout. */
+ params->key_timeout = l->timeout;
+
+ return params;
+
+error:
+ learner_params_free(params);
+ return NULL;
+}
+
+static void
+learner_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->learners)
+ continue;
+
+ for (j = 0; j < p->n_learners; j++) {
+ struct learner_runtime *r = &t->learners[j];
+
+ free(r->mailbox);
+ }
+
+ free(t->learners);
+ t->learners = NULL;
+ }
+
+ if (p->learner_stats) {
+ for (i = 0; i < p->n_learners; i++)
+ free(p->learner_stats[i].n_pkts_action);
+
+ free(p->learner_stats);
+ }
+}
+
+static int
+learner_build(struct rte_swx_pipeline *p)
+{
+ uint32_t i;
+ int status = 0;
+
+ /* Per pipeline: learner statistics. */
+ p->learner_stats = calloc(p->n_learners, sizeof(struct learner_statistics));
+ CHECK(p->learner_stats, ENOMEM);
+
+ for (i = 0; i < p->n_learners; i++) {
+ p->learner_stats[i].n_pkts_action = calloc(p->n_actions, sizeof(uint64_t));
+ CHECK(p->learner_stats[i].n_pkts_action, ENOMEM);
+ }
+
+ /* Per thread: learner run-time. */
+ for (i = 0; i < RTE_SWX_PIPELINE_THREADS_MAX; i++) {
+ struct thread *t = &p->threads[i];
+ struct learner *l;
+
+ t->learners = calloc(p->n_learners, sizeof(struct learner_runtime));
+ if (!t->learners) {
+ status = -ENOMEM;
+ goto error;
+ }
+
+ TAILQ_FOREACH(l, &p->learners, node) {
+ struct learner_runtime *r = &t->learners[l->id];
+ uint64_t size;
+
+ /* r->mailbox. */
+ size = rte_swx_table_learner_mailbox_size_get();
+ if (size) {
+ r->mailbox = calloc(1, size);
+ if (!r->mailbox) {
+ status = -ENOMEM;
+ goto error;
+ }
+ }
+
+ /* r->key. */
+ r->key = l->header ?
+ &t->structs[l->header->struct_id] :
+ &t->structs[p->metadata_struct_id];
+ }
+ }
+
+ return 0;
+
+error:
+ learner_build_free(p);
+ return status;
+}
+
+static void
+learner_free(struct rte_swx_pipeline *p)
+{
+ learner_build_free(p);
+
+ /* Learner tables. */
+ for ( ; ; ) {
+ struct learner *l;
+
+ l = TAILQ_FIRST(&p->learners);
+ if (!l)
+ break;
+
+ TAILQ_REMOVE(&p->learners, l, node);
+ free(l->fields);
+ free(l->actions);
+ free(l->default_action_data);
+ free(l);
+ }
+}
+
+/*
+ * Table state.
+ */
+static int
+table_state_build(struct rte_swx_pipeline *p)
+{
+ struct table *table;
+ struct selector *s;
+ struct learner *l;
+
+ p->table_state = calloc(p->n_tables + p->n_selectors,
+ sizeof(struct rte_swx_table_state));
+ CHECK(p->table_state, ENOMEM);
+
+ TAILQ_FOREACH(table, &p->tables, node) {
+ struct rte_swx_table_state *ts = &p->table_state[table->id];
+
+ if (table->type) {
+ struct rte_swx_table_params *params;
+
+ /* ts->obj. */
+ params = table_params_get(table);
+ CHECK(params, ENOMEM);
+
+ ts->obj = table->type->ops.create(params,
+ NULL,
+ table->args,
+ p->numa_node);
+
+ table_params_free(params);
+ CHECK(ts->obj, ENODEV);
+ }
+
+ /* ts->default_action_data. */
+ if (table->action_data_size_max) {
+ ts->default_action_data =
+ malloc(table->action_data_size_max);
+ CHECK(ts->default_action_data, ENOMEM);
+
+ memcpy(ts->default_action_data,
+ table->default_action_data,
+ table->action_data_size_max);
+ }
+
+ /* ts->default_action_id. */
+ ts->default_action_id = table->default_action->id;
+ }
+
+ TAILQ_FOREACH(s, &p->selectors, node) {
+ struct rte_swx_table_state *ts = &p->table_state[p->n_tables + s->id];
+ struct rte_swx_table_selector_params *params;
+
+ /* ts->obj. */
+ params = selector_table_params_get(s);
+ CHECK(params, ENOMEM);
+
+ ts->obj = rte_swx_table_selector_create(params, NULL, p->numa_node);
+
+ selector_params_free(params);
+ CHECK(ts->obj, ENODEV);
+ }
+
+ TAILQ_FOREACH(l, &p->learners, node) {
+ struct rte_swx_table_state *ts = &p->table_state[p->n_tables +
+ p->n_selectors + l->id];
+ struct rte_swx_table_learner_params *params;
+
+ /* ts->obj. */
+ params = learner_params_get(l);
+ CHECK(params, ENOMEM);
+
+ ts->obj = rte_swx_table_learner_create(params, p->numa_node);
+ learner_params_free(params);
+ CHECK(ts->obj, ENODEV);
+
+ /* ts->default_action_data. */
+ if (l->action_data_size_max) {
+ ts->default_action_data = malloc(l->action_data_size_max);
+ CHECK(ts->default_action_data, ENOMEM);
+
+ memcpy(ts->default_action_data,
+ l->default_action_data,
+ l->action_data_size_max);
+ }
+
+ /* ts->default_action_id. */
+ ts->default_action_id = l->default_action->id;
+ }
+
+ return 0;
+}
+
+static void
+table_state_build_free(struct rte_swx_pipeline *p)
+{
+ uint32_t i;
+
+ if (!p->table_state)
+ return;
+
+ for (i = 0; i < p->n_tables; i++) {
+ struct rte_swx_table_state *ts = &p->table_state[i];
+ struct table *table = table_find_by_id(p, i);
+
+ /* ts->obj. */
+ if (table->type && ts->obj)
+ table->type->ops.free(ts->obj);
+
+ /* ts->default_action_data. */
+ free(ts->default_action_data);
+ }
+
+ for (i = 0; i < p->n_selectors; i++) {
+ struct rte_swx_table_state *ts = &p->table_state[p->n_tables + i];
+
+ /* ts->obj. */
+ if (ts->obj)
+ rte_swx_table_selector_free(ts->obj);
+ }
+
+ for (i = 0; i < p->n_learners; i++) {
+ struct rte_swx_table_state *ts = &p->table_state[p->n_tables + p->n_selectors + i];
+
+ /* ts->obj. */
+ if (ts->obj)
+ rte_swx_table_learner_free(ts->obj);
+
+ /* ts->default_action_data. */
+ free(ts->default_action_data);
+ }
+
+ free(p->table_state);
+ p->table_state = NULL;
+}
+
+static void
+table_state_free(struct rte_swx_pipeline *p)
+{
+ table_state_build_free(p);
+}
+
+/*
+ * Register array.
+ */
+static struct regarray *
+regarray_find(struct rte_swx_pipeline *p, const char *name)
+{
+ struct regarray *elem;
+
+ TAILQ_FOREACH(elem, &p->regarrays, node)
+ if (!strcmp(elem->name, name))
+ return elem;
+
+ return NULL;
+}
+
+static struct regarray *
+regarray_find_by_id(struct rte_swx_pipeline *p, uint32_t id)
+{
+ struct regarray *elem = NULL;
+
+ TAILQ_FOREACH(elem, &p->regarrays, node)
+ if (elem->id == id)
+ return elem;
+
+ return NULL;
+}
+
+int
+rte_swx_pipeline_regarray_config(struct rte_swx_pipeline *p,
+ const char *name,
+ uint32_t size,
+ uint64_t init_val)
+{
+ struct regarray *r;
+
+ CHECK(p, EINVAL);
+
+ CHECK_NAME(name, EINVAL);
+ CHECK(!regarray_find(p, name), EEXIST);
+
+ CHECK(size, EINVAL);
+ size = rte_align32pow2(size);
+
+ /* Memory allocation. */
+ r = calloc(1, sizeof(struct regarray));
+ CHECK(r, ENOMEM);
+
+ /* Node initialization. */
+ strcpy(r->name, name);
+ r->init_val = init_val;
+ r->size = size;
+ r->id = p->n_regarrays;
+
+ /* Node add to tailq. */
+ TAILQ_INSERT_TAIL(&p->regarrays, r, node);
+ p->n_regarrays++;
+
+ return 0;
+}
+
+static int
+regarray_build(struct rte_swx_pipeline *p)
+{
+ struct regarray *regarray;
+
+ if (!p->n_regarrays)
+ return 0;
+
+ p->regarray_runtime = calloc(p->n_regarrays, sizeof(struct regarray_runtime));
+ CHECK(p->regarray_runtime, ENOMEM);
+
+ TAILQ_FOREACH(regarray, &p->regarrays, node) {
+ struct regarray_runtime *r = &p->regarray_runtime[regarray->id];
+ uint32_t i;
+
+ r->regarray = env_malloc(regarray->size * sizeof(uint64_t),
+ RTE_CACHE_LINE_SIZE,
+ p->numa_node);
+ CHECK(r->regarray, ENOMEM);
+
+ if (regarray->init_val)
+ for (i = 0; i < regarray->size; i++)
+ r->regarray[i] = regarray->init_val;
+
+ r->size_mask = regarray->size - 1;
+ }
+
+ return 0;
+}
+
+static void
+regarray_build_free(struct rte_swx_pipeline *p)
+{
+ uint32_t i;
+
+ if (!p->regarray_runtime)
+ return;
+
+ for (i = 0; i < p->n_regarrays; i++) {
+ struct regarray *regarray = regarray_find_by_id(p, i);
+ struct regarray_runtime *r = &p->regarray_runtime[i];
+
+ env_free(r->regarray, regarray->size * sizeof(uint64_t));
+ }
+
+ free(p->regarray_runtime);
+ p->regarray_runtime = NULL;
+}
+
+static void
+regarray_free(struct rte_swx_pipeline *p)
+{
+ regarray_build_free(p);
+
+ for ( ; ; ) {
+ struct regarray *elem;
+
+ elem = TAILQ_FIRST(&p->regarrays);
+ if (!elem)
+ break;
+
+ TAILQ_REMOVE(&p->regarrays, elem, node);
+ free(elem);
+ }
+}
+
+/*
+ * Meter array.
+ */
+static struct meter_profile *
+meter_profile_find(struct rte_swx_pipeline *p, const char *name)
+{
+ struct meter_profile *elem;
+
+ TAILQ_FOREACH(elem, &p->meter_profiles, node)
+ if (!strcmp(elem->name, name))
+ return elem;
+
+ return NULL;
+}
+
+static struct metarray *
+metarray_find(struct rte_swx_pipeline *p, const char *name)
+{
+ struct metarray *elem;
+
+ TAILQ_FOREACH(elem, &p->metarrays, node)
+ if (!strcmp(elem->name, name))
+ return elem;
+
+ return NULL;
+}
+
+static struct metarray *
+metarray_find_by_id(struct rte_swx_pipeline *p, uint32_t id)
+{
+ struct metarray *elem = NULL;
+
+ TAILQ_FOREACH(elem, &p->metarrays, node)
+ if (elem->id == id)
+ return elem;
+
+ return NULL;
+}
+
+int
+rte_swx_pipeline_metarray_config(struct rte_swx_pipeline *p,
+ const char *name,
+ uint32_t size)
+{
+ struct metarray *m;
+
+ CHECK(p, EINVAL);
+
+ CHECK_NAME(name, EINVAL);
+ CHECK(!metarray_find(p, name), EEXIST);
+
+ CHECK(size, EINVAL);
+ size = rte_align32pow2(size);
+
+ /* Memory allocation. */
+ m = calloc(1, sizeof(struct metarray));
+ CHECK(m, ENOMEM);
+
+ /* Node initialization. */
+ strcpy(m->name, name);
+ m->size = size;
+ m->id = p->n_metarrays;
+
+ /* Node add to tailq. */
+ TAILQ_INSERT_TAIL(&p->metarrays, m, node);
+ p->n_metarrays++;
+
+ return 0;
+}
+
+struct meter_profile meter_profile_default = {
+ .node = {0},
+ .name = "",
+ .params = {0},
+
+ .profile = {
+ .cbs = 10000,
+ .pbs = 10000,
+ .cir_period = 1,
+ .cir_bytes_per_period = 1,
+ .pir_period = 1,
+ .pir_bytes_per_period = 1,
+ },
+
+ .n_users = 0,
+};
+
+static void
+meter_init(struct meter *m)
+{
+ memset(m, 0, sizeof(struct meter));
+ rte_meter_trtcm_config(&m->m, &meter_profile_default.profile);
+ m->profile = &meter_profile_default;
+ m->color_mask = RTE_COLOR_GREEN;
+
+ meter_profile_default.n_users++;
+}
+
+static int
+metarray_build(struct rte_swx_pipeline *p)
+{
+ struct metarray *m;
+
+ if (!p->n_metarrays)
+ return 0;
+
+ p->metarray_runtime = calloc(p->n_metarrays, sizeof(struct metarray_runtime));
+ CHECK(p->metarray_runtime, ENOMEM);
+
+ TAILQ_FOREACH(m, &p->metarrays, node) {
+ struct metarray_runtime *r = &p->metarray_runtime[m->id];
+ uint32_t i;
+
+ r->metarray = env_malloc(m->size * sizeof(struct meter),
+ RTE_CACHE_LINE_SIZE,
+ p->numa_node);
+ CHECK(r->metarray, ENOMEM);
+
+ for (i = 0; i < m->size; i++)
+ meter_init(&r->metarray[i]);
+
+ r->size_mask = m->size - 1;
+ }
+
+ return 0;
+}
+
+static void
+metarray_build_free(struct rte_swx_pipeline *p)
+{
+ uint32_t i;
+
+ if (!p->metarray_runtime)
+ return;
+
+ for (i = 0; i < p->n_metarrays; i++) {
+ struct metarray *m = metarray_find_by_id(p, i);
+ struct metarray_runtime *r = &p->metarray_runtime[i];
+
+ env_free(r->metarray, m->size * sizeof(struct meter));
+ }
+
+ free(p->metarray_runtime);
+ p->metarray_runtime = NULL;
+}
+
+static void
+metarray_free(struct rte_swx_pipeline *p)
+{
+ metarray_build_free(p);
+
+ /* Meter arrays. */
+ for ( ; ; ) {
+ struct metarray *elem;
+
+ elem = TAILQ_FIRST(&p->metarrays);
+ if (!elem)
+ break;
+
+ TAILQ_REMOVE(&p->metarrays, elem, node);
+ free(elem);
+ }
+
+ /* Meter profiles. */
+ for ( ; ; ) {
+ struct meter_profile *elem;
+
+ elem = TAILQ_FIRST(&p->meter_profiles);
+ if (!elem)
+ break;
+
+ TAILQ_REMOVE(&p->meter_profiles, elem, node);
+ free(elem);
+ }
+}
+
+/*
+ * Pipeline.
+ */
+int
+rte_swx_pipeline_config(struct rte_swx_pipeline **p, int numa_node)
+{
+ struct rte_swx_pipeline *pipeline;
+
+ /* Check input parameters. */
+ CHECK(p, EINVAL);
+
+ /* Memory allocation. */
+ pipeline = calloc(1, sizeof(struct rte_swx_pipeline));
+ CHECK(pipeline, ENOMEM);
+
+ /* Initialization. */
+ TAILQ_INIT(&pipeline->struct_types);
+ TAILQ_INIT(&pipeline->port_in_types);
+ TAILQ_INIT(&pipeline->ports_in);
+ TAILQ_INIT(&pipeline->port_out_types);
+ TAILQ_INIT(&pipeline->ports_out);
+ TAILQ_INIT(&pipeline->extern_types);
+ TAILQ_INIT(&pipeline->extern_objs);
+ TAILQ_INIT(&pipeline->extern_funcs);
+ TAILQ_INIT(&pipeline->headers);
+ TAILQ_INIT(&pipeline->actions);
+ TAILQ_INIT(&pipeline->table_types);
+ TAILQ_INIT(&pipeline->tables);
+ TAILQ_INIT(&pipeline->selectors);
+ TAILQ_INIT(&pipeline->learners);
+ TAILQ_INIT(&pipeline->regarrays);
+ TAILQ_INIT(&pipeline->meter_profiles);
+ TAILQ_INIT(&pipeline->metarrays);
+
+ pipeline->n_structs = 1; /* Struct 0 is reserved for action_data. */
+ pipeline->numa_node = numa_node;
+
+ *p = pipeline;
+ return 0;
+}
+
+void
+rte_swx_pipeline_free(struct rte_swx_pipeline *p)
+{
+ void *lib;
+
+ if (!p)
+ return;
+
+ lib = p->lib;
+
+ free(p->instruction_data);
+ free(p->instructions);
+
+ metarray_free(p);
+ regarray_free(p);
+ table_state_free(p);
+ learner_free(p);
+ selector_free(p);
+ table_free(p);
+ action_free(p);
+ instruction_table_free(p);
+ metadata_free(p);
+ header_free(p);
+ extern_func_free(p);
+ extern_obj_free(p);
+ port_out_free(p);
+ port_in_free(p);
+ struct_free(p);
+
+ free(p);
+
+ if (lib)
+ dlclose(lib);
+}
+
+int
+rte_swx_pipeline_instructions_config(struct rte_swx_pipeline *p,
+ const char **instructions,
+ uint32_t n_instructions)
+{
+ int err;
+ uint32_t i;
+
+ err = instruction_config(p, NULL, instructions, n_instructions);
+ if (err)
+ return err;
+
+ /* Thread instruction pointer reset. */
+ for (i = 0; i < RTE_SWX_PIPELINE_THREADS_MAX; i++) {
+ struct thread *t = &p->threads[i];
+
+ thread_ip_reset(p, t);
+ }
+
+ return 0;
+}
+
+static int
+pipeline_compile(struct rte_swx_pipeline *p);
+
+int
+rte_swx_pipeline_build(struct rte_swx_pipeline *p)
+{
+ int status;
+
+ CHECK(p, EINVAL);
+ CHECK(p->build_done == 0, EEXIST);
+
+ status = port_in_build(p);
+ if (status)
+ goto error;
+
+ status = port_out_build(p);
+ if (status)
+ goto error;
+
+ status = struct_build(p);
+ if (status)
+ goto error;
+
+ status = extern_obj_build(p);
+ if (status)
+ goto error;
+
+ status = extern_func_build(p);
+ if (status)
+ goto error;
+
+ status = header_build(p);
+ if (status)
+ goto error;
+
+ status = metadata_build(p);
+ if (status)
+ goto error;
+
+ status = instruction_table_build(p);
+ if (status)
+ goto error;
+
+ status = action_build(p);
+ if (status)
+ goto error;
+
+ status = table_build(p);
+ if (status)
+ goto error;
+
+ status = selector_build(p);
+ if (status)
+ goto error;
+
+ status = learner_build(p);
+ if (status)
+ goto error;
+
+ status = table_state_build(p);
+ if (status)
+ goto error;
+
+ status = regarray_build(p);
+ if (status)
+ goto error;
+
+ status = metarray_build(p);
+ if (status)
+ goto error;
+
+ p->build_done = 1;
+
+ pipeline_compile(p);
+
+ return 0;
+
+error:
+ metarray_build_free(p);
+ regarray_build_free(p);
+ table_state_build_free(p);
+ learner_build_free(p);
+ selector_build_free(p);
+ table_build_free(p);
+ action_build_free(p);
+ instruction_table_build_free(p);
+ metadata_build_free(p);
+ header_build_free(p);
+ extern_func_build_free(p);
+ extern_obj_build_free(p);
+ port_out_build_free(p);
+ port_in_build_free(p);
+ struct_build_free(p);
+
+ return status;
+}
+
+void
+rte_swx_pipeline_run(struct rte_swx_pipeline *p, uint32_t n_instructions)
+{
+ uint32_t i;
+
+ for (i = 0; i < n_instructions; i++)
+ instr_exec(p);
+}
+
+void
+rte_swx_pipeline_flush(struct rte_swx_pipeline *p)
+{
+ uint32_t i;
+
+ for (i = 0; i < p->n_ports_out; i++) {
+ struct port_out_runtime *port = &p->out[i];
+
+ if (port->flush)
+ port->flush(port->obj);
+ }
+}
+
+/*
+ * Control.
+ */
+int
+rte_swx_ctl_pipeline_info_get(struct rte_swx_pipeline *p,
+ struct rte_swx_ctl_pipeline_info *pipeline)
+{
+ struct action *action;
+ struct table *table;
+ uint32_t n_actions = 0, n_tables = 0;
+
+ if (!p || !pipeline)
+ return -EINVAL;
+
+ TAILQ_FOREACH(action, &p->actions, node)
+ n_actions++;
+
+ TAILQ_FOREACH(table, &p->tables, node)
+ n_tables++;
+
+ pipeline->n_ports_in = p->n_ports_in;
+ pipeline->n_ports_out = p->n_ports_out;
+ pipeline->n_actions = n_actions;
+ pipeline->n_tables = n_tables;
+ pipeline->n_selectors = p->n_selectors;
+ pipeline->n_learners = p->n_learners;
+ pipeline->n_regarrays = p->n_regarrays;
+ pipeline->n_metarrays = p->n_metarrays;
+
+ return 0;
+}
+
+int
+rte_swx_ctl_pipeline_numa_node_get(struct rte_swx_pipeline *p, int *numa_node)
+{
+ if (!p || !numa_node)
+ return -EINVAL;
+
+ *numa_node = p->numa_node;
+ return 0;
+}
+
+int
+rte_swx_ctl_action_info_get(struct rte_swx_pipeline *p,
+ uint32_t action_id,
+ struct rte_swx_ctl_action_info *action)
+{
+ struct action *a = NULL;
+
+ if (!p || (action_id >= p->n_actions) || !action)
+ return -EINVAL;
+
+ a = action_find_by_id(p, action_id);
+ if (!a)
+ return -EINVAL;
+
+ strcpy(action->name, a->name);
+ action->n_args = a->st ? a->st->n_fields : 0;
+ return 0;
+}
+
+int
+rte_swx_ctl_action_arg_info_get(struct rte_swx_pipeline *p,
+ uint32_t action_id,
+ uint32_t action_arg_id,
+ struct rte_swx_ctl_action_arg_info *action_arg)
+{
+ struct action *a = NULL;
+ struct field *arg = NULL;
+
+ if (!p || (action_id >= p->n_actions) || !action_arg)
+ return -EINVAL;
+
+ a = action_find_by_id(p, action_id);
+ if (!a || !a->st || (action_arg_id >= a->st->n_fields))
+ return -EINVAL;
+
+ arg = &a->st->fields[action_arg_id];
+ strcpy(action_arg->name, arg->name);
+ action_arg->n_bits = arg->n_bits;
+ action_arg->is_network_byte_order = a->args_endianness[action_arg_id];
+
+ return 0;
+}
+
+int
+rte_swx_ctl_table_info_get(struct rte_swx_pipeline *p,
+ uint32_t table_id,
+ struct rte_swx_ctl_table_info *table)
+{
+ struct table *t = NULL;
+
+ if (!p || !table)
+ return -EINVAL;
+
+ t = table_find_by_id(p, table_id);
+ if (!t)
+ return -EINVAL;
+
+ strcpy(table->name, t->name);
+ strcpy(table->args, t->args);
+ table->n_match_fields = t->n_fields;
+ table->n_actions = t->n_actions;
+ table->default_action_is_const = t->default_action_is_const;
+ table->size = t->size;
+ return 0;
+}
+
+int
+rte_swx_ctl_table_match_field_info_get(struct rte_swx_pipeline *p,
+ uint32_t table_id,
+ uint32_t match_field_id,
+ struct rte_swx_ctl_table_match_field_info *match_field)
+{
+ struct table *t;
+ struct match_field *f;
+
+ if (!p || (table_id >= p->n_tables) || !match_field)
+ return -EINVAL;
+
+ t = table_find_by_id(p, table_id);
+ if (!t || (match_field_id >= t->n_fields))
+ return -EINVAL;
+
+ f = &t->fields[match_field_id];
+ match_field->match_type = f->match_type;
+ match_field->is_header = t->header ? 1 : 0;
+ match_field->n_bits = f->field->n_bits;
+ match_field->offset = f->field->offset;
+
+ return 0;
+}
+
+int
+rte_swx_ctl_table_action_info_get(struct rte_swx_pipeline *p,
+ uint32_t table_id,
+ uint32_t table_action_id,
+ struct rte_swx_ctl_table_action_info *table_action)
+{
+ struct table *t;
+
+ if (!p || (table_id >= p->n_tables) || !table_action)
+ return -EINVAL;
+
+ t = table_find_by_id(p, table_id);
+ if (!t || (table_action_id >= t->n_actions))
+ return -EINVAL;
+
+ table_action->action_id = t->actions[table_action_id]->id;
+
+ table_action->action_is_for_table_entries = t->action_is_for_table_entries[table_action_id];
+ table_action->action_is_for_default_entry = t->action_is_for_default_entry[table_action_id];
+
+ return 0;
+}
+
+int
+rte_swx_ctl_table_ops_get(struct rte_swx_pipeline *p,
+ uint32_t table_id,
+ struct rte_swx_table_ops *table_ops,
+ int *is_stub)