+ 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;
+
+ 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;
+}