1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
7 #include <rte_common.h>
8 #include <rte_cycles.h>
12 #include <rte_table_acl.h>
13 #include <rte_table_array.h>
14 #include <rte_table_hash.h>
15 #include <rte_table_lpm.h>
16 #include <rte_table_lpm_ipv6.h>
22 #ifndef THREAD_PIPELINES_MAX
23 #define THREAD_PIPELINES_MAX 256
26 #ifndef THREAD_MSGQ_SIZE
27 #define THREAD_MSGQ_SIZE 64
30 #ifndef THREAD_TIMER_PERIOD_MS
31 #define THREAD_TIMER_PERIOD_MS 100
35 * Master thead: data plane thread context
38 struct rte_ring *msgq_req;
39 struct rte_ring *msgq_rsp;
44 static struct thread thread[RTE_MAX_LCORE];
47 * Data plane threads: context
50 struct rte_table_action *a;
53 struct pipeline_data {
54 struct rte_pipeline *p;
55 struct table_data table_data[RTE_PIPELINE_TABLE_MAX];
58 struct rte_ring *msgq_req;
59 struct rte_ring *msgq_rsp;
60 uint64_t timer_period; /* Measured in CPU cycles. */
63 uint8_t buffer[TABLE_RULE_ACTION_SIZE_MAX];
67 struct rte_pipeline *p[THREAD_PIPELINES_MAX];
70 struct pipeline_data pipeline_data[THREAD_PIPELINES_MAX];
71 struct rte_ring *msgq_req;
72 struct rte_ring *msgq_rsp;
73 uint64_t timer_period; /* Measured in CPU cycles. */
75 uint64_t time_next_min;
76 } __rte_cache_aligned;
78 static struct thread_data thread_data[RTE_MAX_LCORE];
81 * Master thread: data plane thread init
88 for (i = 0; i < RTE_MAX_LCORE; i++) {
89 struct thread *t = &thread[i];
91 if (!rte_lcore_is_enabled(i))
96 rte_ring_free(t->msgq_req);
99 rte_ring_free(t->msgq_rsp);
108 RTE_LCORE_FOREACH_SLAVE(i) {
110 struct rte_ring *msgq_req, *msgq_rsp;
111 struct thread *t = &thread[i];
112 struct thread_data *t_data = &thread_data[i];
113 uint32_t cpu_id = rte_lcore_to_socket_id(i);
116 snprintf(name, sizeof(name), "THREAD-%04x-MSGQ-REQ", i);
118 msgq_req = rte_ring_create(name,
121 RING_F_SP_ENQ | RING_F_SC_DEQ);
123 if (msgq_req == NULL) {
128 snprintf(name, sizeof(name), "THREAD-%04x-MSGQ-RSP", i);
130 msgq_rsp = rte_ring_create(name,
133 RING_F_SP_ENQ | RING_F_SC_DEQ);
135 if (msgq_rsp == NULL) {
140 /* Master thread records */
141 t->msgq_req = msgq_req;
142 t->msgq_rsp = msgq_rsp;
145 /* Data plane thread records */
146 t_data->n_pipelines = 0;
147 t_data->msgq_req = msgq_req;
148 t_data->msgq_rsp = msgq_rsp;
149 t_data->timer_period =
150 (rte_get_tsc_hz() * THREAD_TIMER_PERIOD_MS) / 1000;
151 t_data->time_next = rte_get_tsc_cycles() + t_data->timer_period;
152 t_data->time_next_min = t_data->time_next;
159 * Master thread & data plane threads: message passing
161 enum thread_req_type {
162 THREAD_REQ_PIPELINE_ENABLE = 0,
163 THREAD_REQ_PIPELINE_DISABLE,
167 struct thread_msg_req {
168 enum thread_req_type type;
172 struct rte_pipeline *p;
174 struct rte_table_action *a;
175 } table[RTE_PIPELINE_TABLE_MAX];
176 struct rte_ring *msgq_req;
177 struct rte_ring *msgq_rsp;
178 uint32_t timer_period_ms;
183 struct rte_pipeline *p;
188 struct thread_msg_rsp {
195 static struct thread_msg_req *
196 thread_msg_alloc(void)
198 size_t size = RTE_MAX(sizeof(struct thread_msg_req),
199 sizeof(struct thread_msg_rsp));
201 return calloc(1, size);
205 thread_msg_free(struct thread_msg_rsp *rsp)
210 static struct thread_msg_rsp *
211 thread_msg_send_recv(uint32_t thread_id,
212 struct thread_msg_req *req)
214 struct thread *t = &thread[thread_id];
215 struct rte_ring *msgq_req = t->msgq_req;
216 struct rte_ring *msgq_rsp = t->msgq_rsp;
217 struct thread_msg_rsp *rsp;
222 status = rte_ring_sp_enqueue(msgq_req, req);
223 } while (status == -ENOBUFS);
227 status = rte_ring_sc_dequeue(msgq_rsp, (void **) &rsp);
228 } while (status != 0);
234 thread_pipeline_enable(uint32_t thread_id,
235 const char *pipeline_name)
237 struct pipeline *p = pipeline_find(pipeline_name);
239 struct thread_msg_req *req;
240 struct thread_msg_rsp *rsp;
244 /* Check input params */
245 if ((thread_id >= RTE_MAX_LCORE) ||
247 (p->n_ports_in == 0) ||
248 (p->n_ports_out == 0) ||
252 t = &thread[thread_id];
253 if ((t->enabled == 0) ||
257 /* Allocate request */
258 req = thread_msg_alloc();
263 req->type = THREAD_REQ_PIPELINE_ENABLE;
264 req->pipeline_enable.p = p->p;
265 for (i = 0; i < p->n_tables; i++)
266 req->pipeline_enable.table[i].a =
268 req->pipeline_enable.msgq_req = p->msgq_req;
269 req->pipeline_enable.msgq_rsp = p->msgq_rsp;
270 req->pipeline_enable.timer_period_ms = p->timer_period_ms;
271 req->pipeline_enable.n_tables = p->n_tables;
273 /* Send request and wait for response */
274 rsp = thread_msg_send_recv(thread_id, req);
279 status = rsp->status;
282 thread_msg_free(rsp);
284 /* Request completion */
288 p->thread_id = thread_id;
295 thread_pipeline_disable(uint32_t thread_id,
296 const char *pipeline_name)
298 struct pipeline *p = pipeline_find(pipeline_name);
300 struct thread_msg_req *req;
301 struct thread_msg_rsp *rsp;
304 /* Check input params */
305 if ((thread_id >= RTE_MAX_LCORE) ||
309 t = &thread[thread_id];
316 if (p->thread_id != thread_id)
319 /* Allocate request */
320 req = thread_msg_alloc();
325 req->type = THREAD_REQ_PIPELINE_DISABLE;
326 req->pipeline_disable.p = p->p;
328 /* Send request and wait for response */
329 rsp = thread_msg_send_recv(thread_id, req);
334 status = rsp->status;
337 thread_msg_free(rsp);
339 /* Request completion */
349 * Data plane threads: message handling
351 static inline struct thread_msg_req *
352 thread_msg_recv(struct rte_ring *msgq_req)
354 struct thread_msg_req *req;
356 int status = rte_ring_sc_dequeue(msgq_req, (void **) &req);
365 thread_msg_send(struct rte_ring *msgq_rsp,
366 struct thread_msg_rsp *rsp)
371 status = rte_ring_sp_enqueue(msgq_rsp, rsp);
372 } while (status == -ENOBUFS);
375 static struct thread_msg_rsp *
376 thread_msg_handle_pipeline_enable(struct thread_data *t,
377 struct thread_msg_req *req)
379 struct thread_msg_rsp *rsp = (struct thread_msg_rsp *) req;
380 struct pipeline_data *p = &t->pipeline_data[t->n_pipelines];
384 if (t->n_pipelines >= THREAD_PIPELINES_MAX) {
389 t->p[t->n_pipelines] = req->pipeline_enable.p;
391 p->p = req->pipeline_enable.p;
392 for (i = 0; i < req->pipeline_enable.n_tables; i++)
394 req->pipeline_enable.table[i].a;
396 p->n_tables = req->pipeline_enable.n_tables;
398 p->msgq_req = req->pipeline_enable.msgq_req;
399 p->msgq_rsp = req->pipeline_enable.msgq_rsp;
401 (rte_get_tsc_hz() * req->pipeline_enable.timer_period_ms) / 1000;
402 p->time_next = rte_get_tsc_cycles() + p->timer_period;
411 static struct thread_msg_rsp *
412 thread_msg_handle_pipeline_disable(struct thread_data *t,
413 struct thread_msg_req *req)
415 struct thread_msg_rsp *rsp = (struct thread_msg_rsp *) req;
416 uint32_t n_pipelines = t->n_pipelines;
417 struct rte_pipeline *pipeline = req->pipeline_disable.p;
421 for (i = 0; i < n_pipelines; i++) {
422 struct pipeline_data *p = &t->pipeline_data[i];
424 if (p->p != pipeline)
427 if (i < n_pipelines - 1) {
428 struct rte_pipeline *pipeline_last =
429 t->p[n_pipelines - 1];
430 struct pipeline_data *p_last =
431 &t->pipeline_data[n_pipelines - 1];
433 t->p[i] = pipeline_last;
434 memcpy(p, p_last, sizeof(*p));
443 /* should not get here */
449 thread_msg_handle(struct thread_data *t)
452 struct thread_msg_req *req;
453 struct thread_msg_rsp *rsp;
455 req = thread_msg_recv(t->msgq_req);
460 case THREAD_REQ_PIPELINE_ENABLE:
461 rsp = thread_msg_handle_pipeline_enable(t, req);
464 case THREAD_REQ_PIPELINE_DISABLE:
465 rsp = thread_msg_handle_pipeline_disable(t, req);
469 rsp = (struct thread_msg_rsp *) req;
473 thread_msg_send(t->msgq_rsp, rsp);
478 * Master thread & data plane threads: message passing
480 enum pipeline_req_type {
482 PIPELINE_REQ_PORT_IN_STATS_READ,
483 PIPELINE_REQ_PORT_IN_ENABLE,
484 PIPELINE_REQ_PORT_IN_DISABLE,
487 PIPELINE_REQ_PORT_OUT_STATS_READ,
490 PIPELINE_REQ_TABLE_STATS_READ,
491 PIPELINE_REQ_TABLE_RULE_ADD,
492 PIPELINE_REQ_TABLE_RULE_ADD_DEFAULT,
493 PIPELINE_REQ_TABLE_RULE_ADD_BULK,
494 PIPELINE_REQ_TABLE_RULE_DELETE,
495 PIPELINE_REQ_TABLE_RULE_DELETE_DEFAULT,
499 struct pipeline_msg_req_port_in_stats_read {
503 struct pipeline_msg_req_port_out_stats_read {
507 struct pipeline_msg_req_table_stats_read {
511 struct pipeline_msg_req_table_rule_add {
512 struct table_rule_match match;
513 struct table_rule_action action;
516 struct pipeline_msg_req_table_rule_add_default {
517 struct table_rule_action action;
520 struct pipeline_msg_req_table_rule_add_bulk {
521 struct table_rule_match *match;
522 struct table_rule_action *action;
528 struct pipeline_msg_req_table_rule_delete {
529 struct table_rule_match match;
532 struct pipeline_msg_req {
533 enum pipeline_req_type type;
534 uint32_t id; /* Port IN, port OUT or table ID */
538 struct pipeline_msg_req_port_in_stats_read port_in_stats_read;
539 struct pipeline_msg_req_port_out_stats_read port_out_stats_read;
540 struct pipeline_msg_req_table_stats_read table_stats_read;
541 struct pipeline_msg_req_table_rule_add table_rule_add;
542 struct pipeline_msg_req_table_rule_add_default table_rule_add_default;
543 struct pipeline_msg_req_table_rule_add_bulk table_rule_add_bulk;
544 struct pipeline_msg_req_table_rule_delete table_rule_delete;
548 struct pipeline_msg_rsp_port_in_stats_read {
549 struct rte_pipeline_port_in_stats stats;
552 struct pipeline_msg_rsp_port_out_stats_read {
553 struct rte_pipeline_port_out_stats stats;
556 struct pipeline_msg_rsp_table_stats_read {
557 struct rte_pipeline_table_stats stats;
560 struct pipeline_msg_rsp_table_rule_add {
564 struct pipeline_msg_rsp_table_rule_add_default {
568 struct pipeline_msg_rsp_table_rule_add_bulk {
572 struct pipeline_msg_rsp {
577 struct pipeline_msg_rsp_port_in_stats_read port_in_stats_read;
578 struct pipeline_msg_rsp_port_out_stats_read port_out_stats_read;
579 struct pipeline_msg_rsp_table_stats_read table_stats_read;
580 struct pipeline_msg_rsp_table_rule_add table_rule_add;
581 struct pipeline_msg_rsp_table_rule_add_default table_rule_add_default;
582 struct pipeline_msg_rsp_table_rule_add_bulk table_rule_add_bulk;
589 static struct pipeline_msg_req *
590 pipeline_msg_alloc(void)
592 size_t size = RTE_MAX(sizeof(struct pipeline_msg_req),
593 sizeof(struct pipeline_msg_rsp));
595 return calloc(1, size);
599 pipeline_msg_free(struct pipeline_msg_rsp *rsp)
604 static struct pipeline_msg_rsp *
605 pipeline_msg_send_recv(struct pipeline *p,
606 struct pipeline_msg_req *req)
608 struct rte_ring *msgq_req = p->msgq_req;
609 struct rte_ring *msgq_rsp = p->msgq_rsp;
610 struct pipeline_msg_rsp *rsp;
615 status = rte_ring_sp_enqueue(msgq_req, req);
616 } while (status == -ENOBUFS);
620 status = rte_ring_sc_dequeue(msgq_rsp, (void **) &rsp);
621 } while (status != 0);
627 pipeline_port_in_stats_read(const char *pipeline_name,
629 struct rte_pipeline_port_in_stats *stats,
633 struct pipeline_msg_req *req;
634 struct pipeline_msg_rsp *rsp;
637 /* Check input params */
638 if ((pipeline_name == NULL) ||
642 p = pipeline_find(pipeline_name);
645 (port_id >= p->n_ports_in))
648 /* Allocate request */
649 req = pipeline_msg_alloc();
654 req->type = PIPELINE_REQ_PORT_IN_STATS_READ;
656 req->port_in_stats_read.clear = clear;
658 /* Send request and wait for response */
659 rsp = pipeline_msg_send_recv(p, req);
664 status = rsp->status;
666 memcpy(stats, &rsp->port_in_stats_read.stats, sizeof(*stats));
669 pipeline_msg_free(rsp);
675 pipeline_port_in_enable(const char *pipeline_name,
679 struct pipeline_msg_req *req;
680 struct pipeline_msg_rsp *rsp;
683 /* Check input params */
684 if (pipeline_name == NULL)
687 p = pipeline_find(pipeline_name);
690 (port_id >= p->n_ports_in))
693 /* Allocate request */
694 req = pipeline_msg_alloc();
699 req->type = PIPELINE_REQ_PORT_IN_ENABLE;
702 /* Send request and wait for response */
703 rsp = pipeline_msg_send_recv(p, req);
708 status = rsp->status;
711 pipeline_msg_free(rsp);
717 pipeline_port_in_disable(const char *pipeline_name,
721 struct pipeline_msg_req *req;
722 struct pipeline_msg_rsp *rsp;
725 /* Check input params */
726 if (pipeline_name == NULL)
729 p = pipeline_find(pipeline_name);
732 (port_id >= p->n_ports_in))
735 /* Allocate request */
736 req = pipeline_msg_alloc();
741 req->type = PIPELINE_REQ_PORT_IN_DISABLE;
744 /* Send request and wait for response */
745 rsp = pipeline_msg_send_recv(p, req);
750 status = rsp->status;
753 pipeline_msg_free(rsp);
759 pipeline_port_out_stats_read(const char *pipeline_name,
761 struct rte_pipeline_port_out_stats *stats,
765 struct pipeline_msg_req *req;
766 struct pipeline_msg_rsp *rsp;
769 /* Check input params */
770 if ((pipeline_name == NULL) ||
774 p = pipeline_find(pipeline_name);
777 (port_id >= p->n_ports_out))
780 /* Allocate request */
781 req = pipeline_msg_alloc();
786 req->type = PIPELINE_REQ_PORT_OUT_STATS_READ;
788 req->port_out_stats_read.clear = clear;
790 /* Send request and wait for response */
791 rsp = pipeline_msg_send_recv(p, req);
796 status = rsp->status;
798 memcpy(stats, &rsp->port_out_stats_read.stats, sizeof(*stats));
801 pipeline_msg_free(rsp);
807 pipeline_table_stats_read(const char *pipeline_name,
809 struct rte_pipeline_table_stats *stats,
813 struct pipeline_msg_req *req;
814 struct pipeline_msg_rsp *rsp;
817 /* Check input params */
818 if ((pipeline_name == NULL) ||
822 p = pipeline_find(pipeline_name);
825 (table_id >= p->n_tables))
828 /* Allocate request */
829 req = pipeline_msg_alloc();
834 req->type = PIPELINE_REQ_TABLE_STATS_READ;
836 req->table_stats_read.clear = clear;
838 /* Send request and wait for response */
839 rsp = pipeline_msg_send_recv(p, req);
844 status = rsp->status;
846 memcpy(stats, &rsp->table_stats_read.stats, sizeof(*stats));
849 pipeline_msg_free(rsp);
855 match_check(struct table_rule_match *match,
861 if ((match == NULL) ||
863 (table_id >= p->n_tables))
866 table = &p->table[table_id];
867 if (match->match_type != table->params.match_type)
870 switch (match->match_type) {
873 struct table_acl_params *t = &table->params.match.acl;
874 struct table_rule_match_acl *r = &match->match.acl;
876 if ((r->ip_version && (t->ip_version == 0)) ||
877 ((r->ip_version == 0) && t->ip_version))
881 if ((r->sa_depth > 32) ||
885 if ((r->sa_depth > 128) ||
900 struct table_lpm_params *t = &table->params.match.lpm;
901 struct table_rule_match_lpm *r = &match->match.lpm;
903 if ((r->ip_version && (t->key_size != 4)) ||
904 ((r->ip_version == 0) && (t->key_size != 16)))
926 action_check(struct table_rule_action *action,
930 struct table_action_profile *ap;
932 if ((action == NULL) ||
934 (table_id >= p->n_tables))
937 ap = p->table[table_id].ap;
938 if (action->action_mask != ap->params.action_mask)
941 if (action->action_mask & (1LLU << RTE_TABLE_ACTION_FWD)) {
942 if ((action->fwd.action == RTE_PIPELINE_ACTION_PORT) &&
943 (action->fwd.id >= p->n_ports_out))
946 if ((action->fwd.action == RTE_PIPELINE_ACTION_TABLE) &&
947 (action->fwd.id >= p->n_tables))
951 if (action->action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) {
952 uint32_t tc_mask0 = (1 << ap->params.mtr.n_tc) - 1;
953 uint32_t tc_mask1 = action->mtr.tc_mask;
955 if (tc_mask1 != tc_mask0)
959 if (action->action_mask & (1LLU << RTE_TABLE_ACTION_TM)) {
960 uint32_t n_subports_per_port =
961 ap->params.tm.n_subports_per_port;
962 uint32_t n_pipes_per_subport =
963 ap->params.tm.n_pipes_per_subport;
964 uint32_t subport_id = action->tm.subport_id;
965 uint32_t pipe_id = action->tm.pipe_id;
967 if ((subport_id >= n_subports_per_port) ||
968 (pipe_id >= n_pipes_per_subport))
972 if (action->action_mask & (1LLU << RTE_TABLE_ACTION_ENCAP)) {
973 uint64_t encap_mask = ap->params.encap.encap_mask;
974 enum rte_table_action_encap_type type = action->encap.type;
976 if ((encap_mask & (1LLU << type)) == 0)
980 if (action->action_mask & (1LLU << RTE_TABLE_ACTION_NAT)) {
981 int ip_version0 = ap->params.common.ip_version;
982 int ip_version1 = action->nat.ip_version;
984 if ((ip_version1 && (ip_version0 == 0)) ||
985 ((ip_version1 == 0) && ip_version0))
993 action_default_check(struct table_rule_action *action,
997 if ((action == NULL) ||
998 (action->action_mask != (1LLU << RTE_TABLE_ACTION_FWD)) ||
1000 (table_id >= p->n_tables))
1003 if (action->action_mask & (1LLU << RTE_TABLE_ACTION_FWD)) {
1004 if ((action->fwd.action == RTE_PIPELINE_ACTION_PORT) &&
1005 (action->fwd.id >= p->n_ports_out))
1008 if ((action->fwd.action == RTE_PIPELINE_ACTION_TABLE) &&
1009 (action->fwd.id >= p->n_tables))
1017 pipeline_table_rule_add(const char *pipeline_name,
1019 struct table_rule_match *match,
1020 struct table_rule_action *action,
1024 struct pipeline_msg_req *req;
1025 struct pipeline_msg_rsp *rsp;
1028 /* Check input params */
1029 if ((pipeline_name == NULL) ||
1035 p = pipeline_find(pipeline_name);
1037 (p->enabled == 0) ||
1038 (table_id >= p->n_tables) ||
1039 match_check(match, p, table_id) ||
1040 action_check(action, p, table_id))
1043 /* Allocate request */
1044 req = pipeline_msg_alloc();
1049 req->type = PIPELINE_REQ_TABLE_RULE_ADD;
1051 memcpy(&req->table_rule_add.match, match, sizeof(*match));
1052 memcpy(&req->table_rule_add.action, action, sizeof(*action));
1054 /* Send request and wait for response */
1055 rsp = pipeline_msg_send_recv(p, req);
1060 status = rsp->status;
1062 *data = rsp->table_rule_add.data;
1065 pipeline_msg_free(rsp);
1071 pipeline_table_rule_add_default(const char *pipeline_name,
1073 struct table_rule_action *action,
1077 struct pipeline_msg_req *req;
1078 struct pipeline_msg_rsp *rsp;
1081 /* Check input params */
1082 if ((pipeline_name == NULL) ||
1087 p = pipeline_find(pipeline_name);
1089 (p->enabled == 0) ||
1090 (table_id >= p->n_tables) ||
1091 action_default_check(action, p, table_id))
1094 /* Allocate request */
1095 req = pipeline_msg_alloc();
1100 req->type = PIPELINE_REQ_TABLE_RULE_ADD_DEFAULT;
1102 memcpy(&req->table_rule_add_default.action, action, sizeof(*action));
1104 /* Send request and wait for response */
1105 rsp = pipeline_msg_send_recv(p, req);
1110 status = rsp->status;
1112 *data = rsp->table_rule_add_default.data;
1115 pipeline_msg_free(rsp);
1121 pipeline_table_rule_add_bulk(const char *pipeline_name,
1123 struct table_rule_match *match,
1124 struct table_rule_action *action,
1129 struct pipeline_msg_req *req;
1130 struct pipeline_msg_rsp *rsp;
1134 /* Check input params */
1135 if ((pipeline_name == NULL) ||
1139 (n_rules == NULL) ||
1143 p = pipeline_find(pipeline_name);
1145 (p->enabled == 0) ||
1146 (table_id >= p->n_tables))
1149 for (i = 0; i < *n_rules; i++)
1150 if (match_check(match, p, table_id) ||
1151 action_check(action, p, table_id))
1154 /* Allocate request */
1155 req = pipeline_msg_alloc();
1160 req->type = PIPELINE_REQ_TABLE_RULE_ADD_BULK;
1162 req->table_rule_add_bulk.match = match;
1163 req->table_rule_add_bulk.action = action;
1164 req->table_rule_add_bulk.data = data;
1165 req->table_rule_add_bulk.n_rules = *n_rules;
1166 req->table_rule_add_bulk.bulk =
1167 (p->table[table_id].params.match_type == TABLE_ACL) ? 1 : 0;
1169 /* Send request and wait for response */
1170 rsp = pipeline_msg_send_recv(p, req);
1175 status = rsp->status;
1177 *n_rules = rsp->table_rule_add_bulk.n_rules;
1180 pipeline_msg_free(rsp);
1186 pipeline_table_rule_delete(const char *pipeline_name,
1188 struct table_rule_match *match)
1191 struct pipeline_msg_req *req;
1192 struct pipeline_msg_rsp *rsp;
1195 /* Check input params */
1196 if ((pipeline_name == NULL) ||
1200 p = pipeline_find(pipeline_name);
1202 (p->enabled == 0) ||
1203 (table_id >= p->n_tables) ||
1204 match_check(match, p, table_id))
1207 /* Allocate request */
1208 req = pipeline_msg_alloc();
1213 req->type = PIPELINE_REQ_TABLE_RULE_DELETE;
1215 memcpy(&req->table_rule_delete.match, match, sizeof(*match));
1217 /* Send request and wait for response */
1218 rsp = pipeline_msg_send_recv(p, req);
1223 status = rsp->status;
1226 pipeline_msg_free(rsp);
1232 pipeline_table_rule_delete_default(const char *pipeline_name,
1236 struct pipeline_msg_req *req;
1237 struct pipeline_msg_rsp *rsp;
1240 /* Check input params */
1241 if (pipeline_name == NULL)
1244 p = pipeline_find(pipeline_name);
1246 (p->enabled == 0) ||
1247 (table_id >= p->n_tables))
1250 /* Allocate request */
1251 req = pipeline_msg_alloc();
1256 req->type = PIPELINE_REQ_TABLE_RULE_DELETE_DEFAULT;
1259 /* Send request and wait for response */
1260 rsp = pipeline_msg_send_recv(p, req);
1265 status = rsp->status;
1268 pipeline_msg_free(rsp);
1274 * Data plane threads: message handling
1276 static inline struct pipeline_msg_req *
1277 pipeline_msg_recv(struct rte_ring *msgq_req)
1279 struct pipeline_msg_req *req;
1281 int status = rte_ring_sc_dequeue(msgq_req, (void **) &req);
1290 pipeline_msg_send(struct rte_ring *msgq_rsp,
1291 struct pipeline_msg_rsp *rsp)
1296 status = rte_ring_sp_enqueue(msgq_rsp, rsp);
1297 } while (status == -ENOBUFS);
1300 static struct pipeline_msg_rsp *
1301 pipeline_msg_handle_port_in_stats_read(struct pipeline_data *p,
1302 struct pipeline_msg_req *req)
1304 struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *) req;
1305 uint32_t port_id = req->id;
1306 int clear = req->port_in_stats_read.clear;
1308 rsp->status = rte_pipeline_port_in_stats_read(p->p,
1310 &rsp->port_in_stats_read.stats,
1316 static struct pipeline_msg_rsp *
1317 pipeline_msg_handle_port_in_enable(struct pipeline_data *p,
1318 struct pipeline_msg_req *req)
1320 struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *) req;
1321 uint32_t port_id = req->id;
1323 rsp->status = rte_pipeline_port_in_enable(p->p,
1329 static struct pipeline_msg_rsp *
1330 pipeline_msg_handle_port_in_disable(struct pipeline_data *p,
1331 struct pipeline_msg_req *req)
1333 struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *) req;
1334 uint32_t port_id = req->id;
1336 rsp->status = rte_pipeline_port_in_disable(p->p,
1342 static struct pipeline_msg_rsp *
1343 pipeline_msg_handle_port_out_stats_read(struct pipeline_data *p,
1344 struct pipeline_msg_req *req)
1346 struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *) req;
1347 uint32_t port_id = req->id;
1348 int clear = req->port_out_stats_read.clear;
1350 rsp->status = rte_pipeline_port_out_stats_read(p->p,
1352 &rsp->port_out_stats_read.stats,
1358 static struct pipeline_msg_rsp *
1359 pipeline_msg_handle_table_stats_read(struct pipeline_data *p,
1360 struct pipeline_msg_req *req)
1362 struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *) req;
1363 uint32_t port_id = req->id;
1364 int clear = req->table_stats_read.clear;
1366 rsp->status = rte_pipeline_table_stats_read(p->p,
1368 &rsp->table_stats_read.stats,
1374 union table_rule_match_low_level {
1375 struct rte_table_acl_rule_add_params acl_add;
1376 struct rte_table_acl_rule_delete_params acl_delete;
1377 struct rte_table_array_key array;
1378 uint8_t hash[TABLE_RULE_MATCH_SIZE_MAX];
1379 struct rte_table_lpm_key lpm_ipv4;
1380 struct rte_table_lpm_ipv6_key lpm_ipv6;
1384 match_convert_ipv6_depth(uint32_t depth, uint32_t *depth32)
1389 switch (depth / 32) {
1399 depth32[1] = depth - 32;
1407 depth32[2] = depth - 64;
1415 depth32[3] = depth - 96;
1431 match_convert(struct table_rule_match *mh,
1432 union table_rule_match_low_level *ml,
1435 memset(ml, 0, sizeof(*ml));
1437 switch (mh->match_type) {
1439 if (mh->match.acl.ip_version)
1441 ml->acl_add.field_value[0].value.u8 =
1442 mh->match.acl.proto;
1443 ml->acl_add.field_value[0].mask_range.u8 =
1444 mh->match.acl.proto_mask;
1446 ml->acl_add.field_value[1].value.u32 =
1447 mh->match.acl.ipv4.sa;
1448 ml->acl_add.field_value[1].mask_range.u32 =
1449 mh->match.acl.sa_depth;
1451 ml->acl_add.field_value[2].value.u32 =
1452 mh->match.acl.ipv4.da;
1453 ml->acl_add.field_value[2].mask_range.u32 =
1454 mh->match.acl.da_depth;
1456 ml->acl_add.field_value[3].value.u16 =
1458 ml->acl_add.field_value[3].mask_range.u16 =
1461 ml->acl_add.field_value[4].value.u16 =
1463 ml->acl_add.field_value[4].mask_range.u16 =
1466 ml->acl_add.priority =
1467 (int32_t) mh->match.acl.priority;
1469 ml->acl_delete.field_value[0].value.u8 =
1470 mh->match.acl.proto;
1471 ml->acl_delete.field_value[0].mask_range.u8 =
1472 mh->match.acl.proto_mask;
1474 ml->acl_delete.field_value[1].value.u32 =
1475 mh->match.acl.ipv4.sa;
1476 ml->acl_delete.field_value[1].mask_range.u32 =
1477 mh->match.acl.sa_depth;
1479 ml->acl_delete.field_value[2].value.u32 =
1480 mh->match.acl.ipv4.da;
1481 ml->acl_delete.field_value[2].mask_range.u32 =
1482 mh->match.acl.da_depth;
1484 ml->acl_delete.field_value[3].value.u16 =
1486 ml->acl_delete.field_value[3].mask_range.u16 =
1489 ml->acl_delete.field_value[4].value.u16 =
1491 ml->acl_delete.field_value[4].mask_range.u16 =
1497 (uint32_t *) mh->match.acl.ipv6.sa;
1499 (uint32_t *) mh->match.acl.ipv6.da;
1500 uint32_t sa32_depth[4], da32_depth[4];
1503 status = match_convert_ipv6_depth(
1504 mh->match.acl.sa_depth,
1509 status = match_convert_ipv6_depth(
1510 mh->match.acl.da_depth,
1515 ml->acl_add.field_value[0].value.u8 =
1516 mh->match.acl.proto;
1517 ml->acl_add.field_value[0].mask_range.u8 =
1518 mh->match.acl.proto_mask;
1520 ml->acl_add.field_value[1].value.u32 = sa32[0];
1521 ml->acl_add.field_value[1].mask_range.u32 =
1523 ml->acl_add.field_value[2].value.u32 = sa32[1];
1524 ml->acl_add.field_value[2].mask_range.u32 =
1526 ml->acl_add.field_value[3].value.u32 = sa32[2];
1527 ml->acl_add.field_value[3].mask_range.u32 =
1529 ml->acl_add.field_value[4].value.u32 = sa32[3];
1530 ml->acl_add.field_value[4].mask_range.u32 =
1533 ml->acl_add.field_value[5].value.u32 = da32[0];
1534 ml->acl_add.field_value[5].mask_range.u32 =
1536 ml->acl_add.field_value[6].value.u32 = da32[1];
1537 ml->acl_add.field_value[6].mask_range.u32 =
1539 ml->acl_add.field_value[7].value.u32 = da32[2];
1540 ml->acl_add.field_value[7].mask_range.u32 =
1542 ml->acl_add.field_value[8].value.u32 = da32[3];
1543 ml->acl_add.field_value[8].mask_range.u32 =
1546 ml->acl_add.field_value[9].value.u16 =
1548 ml->acl_add.field_value[9].mask_range.u16 =
1551 ml->acl_add.field_value[10].value.u16 =
1553 ml->acl_add.field_value[10].mask_range.u16 =
1556 ml->acl_add.priority =
1557 (int32_t) mh->match.acl.priority;
1560 (uint32_t *) mh->match.acl.ipv6.sa;
1562 (uint32_t *) mh->match.acl.ipv6.da;
1563 uint32_t sa32_depth[4], da32_depth[4];
1566 status = match_convert_ipv6_depth(
1567 mh->match.acl.sa_depth,
1572 status = match_convert_ipv6_depth(
1573 mh->match.acl.da_depth,
1578 ml->acl_delete.field_value[0].value.u8 =
1579 mh->match.acl.proto;
1580 ml->acl_delete.field_value[0].mask_range.u8 =
1581 mh->match.acl.proto_mask;
1583 ml->acl_delete.field_value[1].value.u32 =
1585 ml->acl_delete.field_value[1].mask_range.u32 =
1587 ml->acl_delete.field_value[2].value.u32 =
1589 ml->acl_delete.field_value[2].mask_range.u32 =
1591 ml->acl_delete.field_value[3].value.u32 =
1593 ml->acl_delete.field_value[3].mask_range.u32 =
1595 ml->acl_delete.field_value[4].value.u32 =
1597 ml->acl_delete.field_value[4].mask_range.u32 =
1600 ml->acl_delete.field_value[5].value.u32 =
1602 ml->acl_delete.field_value[5].mask_range.u32 =
1604 ml->acl_delete.field_value[6].value.u32 =
1606 ml->acl_delete.field_value[6].mask_range.u32 =
1608 ml->acl_delete.field_value[7].value.u32 =
1610 ml->acl_delete.field_value[7].mask_range.u32 =
1612 ml->acl_delete.field_value[8].value.u32 =
1614 ml->acl_delete.field_value[8].mask_range.u32 =
1617 ml->acl_delete.field_value[9].value.u16 =
1619 ml->acl_delete.field_value[9].mask_range.u16 =
1622 ml->acl_delete.field_value[10].value.u16 =
1624 ml->acl_delete.field_value[10].mask_range.u16 =
1630 ml->array.pos = mh->match.array.pos;
1634 memcpy(ml->hash, mh->match.hash.key, sizeof(ml->hash));
1638 if (mh->match.lpm.ip_version) {
1639 ml->lpm_ipv4.ip = mh->match.lpm.ipv4;
1640 ml->lpm_ipv4.depth = mh->match.lpm.depth;
1642 memcpy(ml->lpm_ipv6.ip,
1643 mh->match.lpm.ipv6, sizeof(ml->lpm_ipv6.ip));
1644 ml->lpm_ipv6.depth = mh->match.lpm.depth;
1654 static struct pipeline_msg_rsp *
1655 pipeline_msg_handle_table_rule_add(struct pipeline_data *p,
1656 struct pipeline_msg_req *req)
1658 union table_rule_match_low_level match_ll;
1659 struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *) req;
1660 struct table_rule_match *match = &req->table_rule_add.match;
1661 struct table_rule_action *action = &req->table_rule_add.action;
1662 struct rte_pipeline_table_entry *data_in, *data_out;
1663 uint32_t table_id = req->id;
1664 int key_found, status;
1665 struct rte_table_action *a = p->table_data[table_id].a;
1668 memset(p->buffer, 0, sizeof(p->buffer));
1669 data_in = (struct rte_pipeline_table_entry *) p->buffer;
1671 if (action->action_mask & (1LLU << RTE_TABLE_ACTION_FWD)) {
1672 status = rte_table_action_apply(a,
1674 RTE_TABLE_ACTION_FWD,
1683 if (action->action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) {
1684 status = rte_table_action_apply(a,
1686 RTE_TABLE_ACTION_MTR,
1695 if (action->action_mask & (1LLU << RTE_TABLE_ACTION_TM)) {
1696 status = rte_table_action_apply(a,
1698 RTE_TABLE_ACTION_TM,
1707 if (action->action_mask & (1LLU << RTE_TABLE_ACTION_ENCAP)) {
1708 status = rte_table_action_apply(a,
1710 RTE_TABLE_ACTION_ENCAP,
1719 if (action->action_mask & (1LLU << RTE_TABLE_ACTION_NAT)) {
1720 status = rte_table_action_apply(a,
1722 RTE_TABLE_ACTION_NAT,
1731 if (action->action_mask & (1LLU << RTE_TABLE_ACTION_TTL)) {
1732 status = rte_table_action_apply(a,
1734 RTE_TABLE_ACTION_TTL,
1743 if (action->action_mask & (1LLU << RTE_TABLE_ACTION_STATS)) {
1744 status = rte_table_action_apply(a,
1746 RTE_TABLE_ACTION_STATS,
1755 if (action->action_mask & (1LLU << RTE_TABLE_ACTION_TIME)) {
1756 status = rte_table_action_apply(a,
1758 RTE_TABLE_ACTION_TIME,
1767 /* Add rule (match, action) to table */
1768 status = match_convert(match, &match_ll, 1);
1774 status = rte_pipeline_table_entry_add(p->p,
1785 /* Write response */
1787 rsp->table_rule_add.data = data_out;
1792 static struct pipeline_msg_rsp *
1793 pipeline_msg_handle_table_rule_add_default(struct pipeline_data *p,
1794 struct pipeline_msg_req *req)
1796 struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *) req;
1797 struct table_rule_action *action = &req->table_rule_add_default.action;
1798 struct rte_pipeline_table_entry *data_in, *data_out;
1799 uint32_t table_id = req->id;
1803 memset(p->buffer, 0, sizeof(p->buffer));
1804 data_in = (struct rte_pipeline_table_entry *) p->buffer;
1806 data_in->action = action->fwd.action;
1807 if (action->fwd.action == RTE_PIPELINE_ACTION_PORT)
1808 data_in->port_id = action->fwd.id;
1809 if (action->fwd.action == RTE_PIPELINE_ACTION_TABLE)
1810 data_in->table_id = action->fwd.id;
1812 /* Add default rule to table */
1813 status = rte_pipeline_table_default_entry_add(p->p,
1822 /* Write response */
1824 rsp->table_rule_add_default.data = data_out;
1829 static struct pipeline_msg_rsp *
1830 pipeline_msg_handle_table_rule_add_bulk(struct pipeline_data *p,
1831 struct pipeline_msg_req *req)
1834 struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *) req;
1836 uint32_t table_id = req->id;
1837 struct table_rule_match *match = req->table_rule_add_bulk.match;
1838 struct table_rule_action *action = req->table_rule_add_bulk.action;
1839 struct rte_pipeline_table_entry **data =
1840 (struct rte_pipeline_table_entry **)req->table_rule_add_bulk.data;
1841 uint32_t n_rules = req->table_rule_add_bulk.n_rules;
1842 uint32_t bulk = req->table_rule_add_bulk.bulk;
1844 struct rte_table_action *a = p->table_data[table_id].a;
1845 union table_rule_match_low_level *match_ll;
1847 void **match_ll_ptr;
1848 struct rte_pipeline_table_entry **action_ll_ptr;
1852 /* Memory allocation */
1853 match_ll = calloc(n_rules, sizeof(union table_rule_match_low_level));
1854 action_ll = calloc(n_rules, TABLE_RULE_ACTION_SIZE_MAX);
1855 match_ll_ptr = calloc(n_rules, sizeof(void *));
1857 calloc(n_rules, sizeof(struct rte_pipeline_table_entry *));
1858 found = calloc(n_rules, sizeof(int));
1860 if ((match_ll == NULL) ||
1861 (action_ll == NULL) ||
1862 (match_ll_ptr == NULL) ||
1863 (action_ll_ptr == NULL) ||
1867 for (i = 0; i < n_rules; i++) {
1868 match_ll_ptr[i] = (void *)&match_ll[i];
1870 (struct rte_pipeline_table_entry *)&action_ll[i * TABLE_RULE_ACTION_SIZE_MAX];
1873 /* Rule match conversion */
1874 for (i = 0; i < n_rules; i++) {
1875 status = match_convert(&match[i], match_ll_ptr[i], 1);
1880 /* Rule action conversion */
1881 for (i = 0; i < n_rules; i++) {
1882 void *data_in = action_ll_ptr[i];
1883 struct table_rule_action *act = &action[i];
1885 if (act->action_mask & (1LLU << RTE_TABLE_ACTION_FWD)) {
1886 status = rte_table_action_apply(a,
1888 RTE_TABLE_ACTION_FWD,
1895 if (act->action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) {
1896 status = rte_table_action_apply(a,
1898 RTE_TABLE_ACTION_MTR,
1905 if (act->action_mask & (1LLU << RTE_TABLE_ACTION_TM)) {
1906 status = rte_table_action_apply(a,
1908 RTE_TABLE_ACTION_TM,
1915 if (act->action_mask & (1LLU << RTE_TABLE_ACTION_ENCAP)) {
1916 status = rte_table_action_apply(a,
1918 RTE_TABLE_ACTION_ENCAP,
1925 if (act->action_mask & (1LLU << RTE_TABLE_ACTION_NAT)) {
1926 status = rte_table_action_apply(a,
1928 RTE_TABLE_ACTION_NAT,
1935 if (act->action_mask & (1LLU << RTE_TABLE_ACTION_TTL)) {
1936 status = rte_table_action_apply(a,
1938 RTE_TABLE_ACTION_TTL,
1945 if (act->action_mask & (1LLU << RTE_TABLE_ACTION_STATS)) {
1946 status = rte_table_action_apply(a,
1948 RTE_TABLE_ACTION_STATS,
1955 if (act->action_mask & (1LLU << RTE_TABLE_ACTION_TIME)) {
1956 status = rte_table_action_apply(a,
1958 RTE_TABLE_ACTION_TIME,
1966 /* Add rule (match, action) to table */
1968 status = rte_pipeline_table_entry_add_bulk(p->p,
1978 for (i = 0; i < n_rules; i++) {
1979 status = rte_pipeline_table_entry_add(p->p,
1991 /* Write response */
1993 rsp->table_rule_add_bulk.n_rules = n_rules;
1997 free(action_ll_ptr);
2006 free(action_ll_ptr);
2012 rsp->table_rule_add_bulk.n_rules = 0;
2016 static struct pipeline_msg_rsp *
2017 pipeline_msg_handle_table_rule_delete(struct pipeline_data *p,
2018 struct pipeline_msg_req *req)
2020 union table_rule_match_low_level match_ll;
2021 struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *) req;
2022 struct table_rule_match *match = &req->table_rule_delete.match;
2023 uint32_t table_id = req->id;
2024 int key_found, status;
2026 status = match_convert(match, &match_ll, 0);
2032 rsp->status = rte_pipeline_table_entry_delete(p->p,
2041 static struct pipeline_msg_rsp *
2042 pipeline_msg_handle_table_rule_delete_default(struct pipeline_data *p,
2043 struct pipeline_msg_req *req)
2045 struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *) req;
2046 uint32_t table_id = req->id;
2048 rsp->status = rte_pipeline_table_default_entry_delete(p->p,
2056 pipeline_msg_handle(struct pipeline_data *p)
2059 struct pipeline_msg_req *req;
2060 struct pipeline_msg_rsp *rsp;
2062 req = pipeline_msg_recv(p->msgq_req);
2066 switch (req->type) {
2067 case PIPELINE_REQ_PORT_IN_STATS_READ:
2068 rsp = pipeline_msg_handle_port_in_stats_read(p, req);
2071 case PIPELINE_REQ_PORT_IN_ENABLE:
2072 rsp = pipeline_msg_handle_port_in_enable(p, req);
2075 case PIPELINE_REQ_PORT_IN_DISABLE:
2076 rsp = pipeline_msg_handle_port_in_disable(p, req);
2079 case PIPELINE_REQ_PORT_OUT_STATS_READ:
2080 rsp = pipeline_msg_handle_port_out_stats_read(p, req);
2083 case PIPELINE_REQ_TABLE_STATS_READ:
2084 rsp = pipeline_msg_handle_table_stats_read(p, req);
2087 case PIPELINE_REQ_TABLE_RULE_ADD:
2088 rsp = pipeline_msg_handle_table_rule_add(p, req);
2091 case PIPELINE_REQ_TABLE_RULE_ADD_DEFAULT:
2092 rsp = pipeline_msg_handle_table_rule_add_default(p, req);
2095 case PIPELINE_REQ_TABLE_RULE_ADD_BULK:
2096 rsp = pipeline_msg_handle_table_rule_add_bulk(p, req);
2099 case PIPELINE_REQ_TABLE_RULE_DELETE:
2100 rsp = pipeline_msg_handle_table_rule_delete(p, req);
2103 case PIPELINE_REQ_TABLE_RULE_DELETE_DEFAULT:
2104 rsp = pipeline_msg_handle_table_rule_delete_default(p, req);
2108 rsp = (struct pipeline_msg_rsp *) req;
2112 pipeline_msg_send(p->msgq_rsp, rsp);
2117 * Data plane threads: main
2120 thread_main(void *arg __rte_unused)
2122 struct thread_data *t;
2123 uint32_t thread_id, i;
2125 thread_id = rte_lcore_id();
2126 t = &thread_data[thread_id];
2129 for (i = 0; ; i++) {
2133 for (j = 0; j < t->n_pipelines; j++)
2134 rte_pipeline_run(t->p[j]);
2137 if ((i & 0xF) == 0) {
2138 uint64_t time = rte_get_tsc_cycles();
2139 uint64_t time_next_min = UINT64_MAX;
2141 if (time < t->time_next_min)
2144 /* Pipeline message queues */
2145 for (j = 0; j < t->n_pipelines; j++) {
2146 struct pipeline_data *p =
2147 &t->pipeline_data[j];
2148 uint64_t time_next = p->time_next;
2150 if (time_next <= time) {
2151 pipeline_msg_handle(p);
2152 rte_pipeline_flush(p->p);
2153 time_next = time + p->timer_period;
2154 p->time_next = time_next;
2157 if (time_next < time_next_min)
2158 time_next_min = time_next;
2161 /* Thread message queues */
2163 uint64_t time_next = t->time_next;
2165 if (time_next <= time) {
2166 thread_msg_handle(t);
2167 time_next = time + t->timer_period;
2168 t->time_next = time_next;
2171 if (time_next < time_next_min)
2172 time_next_min = time_next;
2175 t->time_next_min = time_next_min;