+ 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;
+
+ 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;
+ }
+
+ 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);
+
+ 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;
+ }
+
+ /* 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++)
+ l->actions[i] = action_find(p, params->action_names[i]);
+
+ 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->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;