net/softnic: update meter profile
[dpdk.git] / drivers / net / softnic / rte_eth_softnic_thread.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 #include <stdlib.h>
6
7 #include <rte_common.h>
8 #include <rte_cycles.h>
9 #include <rte_lcore.h>
10 #include <rte_ring.h>
11
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>
17 #include "rte_eth_softnic_internals.h"
18
19 /**
20  * Master thread: data plane thread init
21  */
22 void
23 softnic_thread_free(struct pmd_internals *softnic)
24 {
25         uint32_t i;
26
27         RTE_LCORE_FOREACH_SLAVE(i) {
28                 struct softnic_thread *t = &softnic->thread[i];
29
30                 /* MSGQs */
31                 if (t->msgq_req)
32                         rte_ring_free(t->msgq_req);
33
34                 if (t->msgq_rsp)
35                         rte_ring_free(t->msgq_rsp);
36         }
37 }
38
39 int
40 softnic_thread_init(struct pmd_internals *softnic)
41 {
42         uint32_t i;
43
44         RTE_LCORE_FOREACH_SLAVE(i) {
45                 char ring_name[NAME_MAX];
46                 struct rte_ring *msgq_req, *msgq_rsp;
47                 struct softnic_thread *t = &softnic->thread[i];
48                 struct softnic_thread_data *t_data = &softnic->thread_data[i];
49                 uint32_t cpu_id = rte_lcore_to_socket_id(i);
50
51                 /* MSGQs */
52                 snprintf(ring_name, sizeof(ring_name), "%s-TH%u-REQ",
53                         softnic->params.name,
54                         i);
55
56                 msgq_req = rte_ring_create(ring_name,
57                         THREAD_MSGQ_SIZE,
58                         cpu_id,
59                         RING_F_SP_ENQ | RING_F_SC_DEQ);
60
61                 if (msgq_req == NULL) {
62                         softnic_thread_free(softnic);
63                         return -1;
64                 }
65
66                 snprintf(ring_name, sizeof(ring_name), "%s-TH%u-RSP",
67                         softnic->params.name,
68                         i);
69
70                 msgq_rsp = rte_ring_create(ring_name,
71                         THREAD_MSGQ_SIZE,
72                         cpu_id,
73                         RING_F_SP_ENQ | RING_F_SC_DEQ);
74
75                 if (msgq_rsp == NULL) {
76                         softnic_thread_free(softnic);
77                         return -1;
78                 }
79
80                 /* Master thread records */
81                 t->msgq_req = msgq_req;
82                 t->msgq_rsp = msgq_rsp;
83                 t->enabled = 1;
84
85                 /* Data plane thread records */
86                 t_data->n_pipelines = 0;
87                 t_data->msgq_req = msgq_req;
88                 t_data->msgq_rsp = msgq_rsp;
89                 t_data->timer_period =
90                         (rte_get_tsc_hz() * THREAD_TIMER_PERIOD_MS) / 1000;
91                 t_data->time_next = rte_get_tsc_cycles() + t_data->timer_period;
92                 t_data->time_next_min = t_data->time_next;
93         }
94
95         return 0;
96 }
97
98 static inline int
99 thread_is_running(uint32_t thread_id)
100 {
101         enum rte_lcore_state_t thread_state;
102
103         thread_state = rte_eal_get_lcore_state(thread_id);
104         return (thread_state == RUNNING)? 1 : 0;
105 }
106
107 /**
108  * Pipeline is running when:
109  *    (A) Pipeline is mapped to a data plane thread AND
110  *    (B) Its data plane thread is in RUNNING state.
111  */
112 static inline int
113 pipeline_is_running(struct pipeline *p)
114 {
115         if (p->enabled == 0)
116                 return 0;
117
118         return thread_is_running(p->thread_id);
119 }
120
121 /**
122  * Master thread & data plane threads: message passing
123  */
124 enum thread_req_type {
125         THREAD_REQ_PIPELINE_ENABLE = 0,
126         THREAD_REQ_PIPELINE_DISABLE,
127         THREAD_REQ_MAX
128 };
129
130 struct thread_msg_req {
131         enum thread_req_type type;
132
133         union {
134                 struct {
135                         struct rte_pipeline *p;
136                         struct {
137                                 struct rte_table_action *a;
138                         } table[RTE_PIPELINE_TABLE_MAX];
139                         struct rte_ring *msgq_req;
140                         struct rte_ring *msgq_rsp;
141                         uint32_t timer_period_ms;
142                         uint32_t n_tables;
143                 } pipeline_enable;
144
145                 struct {
146                         struct rte_pipeline *p;
147                 } pipeline_disable;
148         };
149 };
150
151 struct thread_msg_rsp {
152         int status;
153 };
154
155 /**
156  * Master thread
157  */
158 static struct thread_msg_req *
159 thread_msg_alloc(void)
160 {
161         size_t size = RTE_MAX(sizeof(struct thread_msg_req),
162                 sizeof(struct thread_msg_rsp));
163
164         return calloc(1, size);
165 }
166
167 static void
168 thread_msg_free(struct thread_msg_rsp *rsp)
169 {
170         free(rsp);
171 }
172
173 static struct thread_msg_rsp *
174 thread_msg_send_recv(struct pmd_internals *softnic,
175         uint32_t thread_id,
176         struct thread_msg_req *req)
177 {
178         struct softnic_thread *t = &softnic->thread[thread_id];
179         struct rte_ring *msgq_req = t->msgq_req;
180         struct rte_ring *msgq_rsp = t->msgq_rsp;
181         struct thread_msg_rsp *rsp;
182         int status;
183
184         /* send */
185         do {
186                 status = rte_ring_sp_enqueue(msgq_req, req);
187         } while (status == -ENOBUFS);
188
189         /* recv */
190         do {
191                 status = rte_ring_sc_dequeue(msgq_rsp, (void **)&rsp);
192         } while (status != 0);
193
194         return rsp;
195 }
196
197 int
198 softnic_thread_pipeline_enable(struct pmd_internals *softnic,
199         uint32_t thread_id,
200         const char *pipeline_name)
201 {
202         struct pipeline *p = softnic_pipeline_find(softnic, pipeline_name);
203         struct softnic_thread *t;
204         struct thread_msg_req *req;
205         struct thread_msg_rsp *rsp;
206         uint32_t i;
207         int status;
208
209         /* Check input params */
210         if ((thread_id >= RTE_MAX_LCORE) ||
211                 (p == NULL) ||
212                 (p->n_ports_in == 0) ||
213                 (p->n_ports_out == 0) ||
214                 (p->n_tables == 0))
215                 return -1;
216
217         t = &softnic->thread[thread_id];
218         if ((t->enabled == 0) ||
219                 p->enabled)
220                 return -1;
221
222         if (!thread_is_running(thread_id)) {
223                 struct softnic_thread_data *td = &softnic->thread_data[thread_id];
224                 struct pipeline_data *tdp = &td->pipeline_data[td->n_pipelines];
225
226                 if (td->n_pipelines >= THREAD_PIPELINES_MAX)
227                         return -1;
228
229                 /* Data plane thread */
230                 td->p[td->n_pipelines] = p->p;
231
232                 tdp->p = p->p;
233                 for (i = 0; i < p->n_tables; i++)
234                         tdp->table_data[i].a =
235                                 p->table[i].a;
236                 tdp->n_tables = p->n_tables;
237
238                 tdp->msgq_req = p->msgq_req;
239                 tdp->msgq_rsp = p->msgq_rsp;
240                 tdp->timer_period = (rte_get_tsc_hz() * p->timer_period_ms) / 1000;
241                 tdp->time_next = rte_get_tsc_cycles() + tdp->timer_period;
242
243                 td->n_pipelines++;
244
245                 /* Pipeline */
246                 p->thread_id = thread_id;
247                 p->enabled = 1;
248
249                 return 0;
250         }
251
252         /* Allocate request */
253         req = thread_msg_alloc();
254         if (req == NULL)
255                 return -1;
256
257         /* Write request */
258         req->type = THREAD_REQ_PIPELINE_ENABLE;
259         req->pipeline_enable.p = p->p;
260         for (i = 0; i < p->n_tables; i++)
261                 req->pipeline_enable.table[i].a =
262                         p->table[i].a;
263         req->pipeline_enable.msgq_req = p->msgq_req;
264         req->pipeline_enable.msgq_rsp = p->msgq_rsp;
265         req->pipeline_enable.timer_period_ms = p->timer_period_ms;
266         req->pipeline_enable.n_tables = p->n_tables;
267
268         /* Send request and wait for response */
269         rsp = thread_msg_send_recv(softnic, thread_id, req);
270         if (rsp == NULL)
271                 return -1;
272
273         /* Read response */
274         status = rsp->status;
275
276         /* Free response */
277         thread_msg_free(rsp);
278
279         /* Request completion */
280         if (status)
281                 return status;
282
283         p->thread_id = thread_id;
284         p->enabled = 1;
285
286         return 0;
287 }
288
289 int
290 softnic_thread_pipeline_disable(struct pmd_internals *softnic,
291         uint32_t thread_id,
292         const char *pipeline_name)
293 {
294         struct pipeline *p = softnic_pipeline_find(softnic, pipeline_name);
295         struct softnic_thread *t;
296         struct thread_msg_req *req;
297         struct thread_msg_rsp *rsp;
298         int status;
299
300         /* Check input params */
301         if ((thread_id >= RTE_MAX_LCORE) ||
302                 (p == NULL))
303                 return -1;
304
305         t = &softnic->thread[thread_id];
306         if (t->enabled == 0)
307                 return -1;
308
309         if (p->enabled == 0)
310                 return 0;
311
312         if (p->thread_id != thread_id)
313                 return -1;
314
315         if (!thread_is_running(thread_id)) {
316                 struct softnic_thread_data *td = &softnic->thread_data[thread_id];
317                 uint32_t i;
318
319                 for (i = 0; i < td->n_pipelines; i++) {
320                         struct pipeline_data *tdp = &td->pipeline_data[i];
321
322                         if (tdp->p != p->p)
323                                 continue;
324
325                         /* Data plane thread */
326                         if (i < td->n_pipelines - 1) {
327                                 struct rte_pipeline *pipeline_last =
328                                         td->p[td->n_pipelines - 1];
329                                 struct pipeline_data *tdp_last =
330                                         &td->pipeline_data[td->n_pipelines - 1];
331
332                                 td->p[i] = pipeline_last;
333                                 memcpy(tdp, tdp_last, sizeof(*tdp));
334                         }
335
336                         td->n_pipelines--;
337
338                         /* Pipeline */
339                         p->enabled = 0;
340
341                         break;
342                 }
343
344                 return 0;
345         }
346
347         /* Allocate request */
348         req = thread_msg_alloc();
349         if (req == NULL)
350                 return -1;
351
352         /* Write request */
353         req->type = THREAD_REQ_PIPELINE_DISABLE;
354         req->pipeline_disable.p = p->p;
355
356         /* Send request and wait for response */
357         rsp = thread_msg_send_recv(softnic, thread_id, req);
358         if (rsp == NULL)
359                 return -1;
360
361         /* Read response */
362         status = rsp->status;
363
364         /* Free response */
365         thread_msg_free(rsp);
366
367         /* Request completion */
368         if (status)
369                 return status;
370
371         p->enabled = 0;
372
373         return 0;
374 }
375
376 /**
377  * Data plane threads: message handling
378  */
379 static inline struct thread_msg_req *
380 thread_msg_recv(struct rte_ring *msgq_req)
381 {
382         struct thread_msg_req *req;
383
384         int status = rte_ring_sc_dequeue(msgq_req, (void **)&req);
385
386         if (status != 0)
387                 return NULL;
388
389         return req;
390 }
391
392 static inline void
393 thread_msg_send(struct rte_ring *msgq_rsp,
394         struct thread_msg_rsp *rsp)
395 {
396         int status;
397
398         do {
399                 status = rte_ring_sp_enqueue(msgq_rsp, rsp);
400         } while (status == -ENOBUFS);
401 }
402
403 static struct thread_msg_rsp *
404 thread_msg_handle_pipeline_enable(struct softnic_thread_data *t,
405         struct thread_msg_req *req)
406 {
407         struct thread_msg_rsp *rsp = (struct thread_msg_rsp *)req;
408         struct pipeline_data *p = &t->pipeline_data[t->n_pipelines];
409         uint32_t i;
410
411         /* Request */
412         if (t->n_pipelines >= THREAD_PIPELINES_MAX) {
413                 rsp->status = -1;
414                 return rsp;
415         }
416
417         t->p[t->n_pipelines] = req->pipeline_enable.p;
418
419         p->p = req->pipeline_enable.p;
420         for (i = 0; i < req->pipeline_enable.n_tables; i++)
421                 p->table_data[i].a =
422                         req->pipeline_enable.table[i].a;
423
424         p->n_tables = req->pipeline_enable.n_tables;
425
426         p->msgq_req = req->pipeline_enable.msgq_req;
427         p->msgq_rsp = req->pipeline_enable.msgq_rsp;
428         p->timer_period =
429                 (rte_get_tsc_hz() * req->pipeline_enable.timer_period_ms) / 1000;
430         p->time_next = rte_get_tsc_cycles() + p->timer_period;
431
432         t->n_pipelines++;
433
434         /* Response */
435         rsp->status = 0;
436         return rsp;
437 }
438
439 static struct thread_msg_rsp *
440 thread_msg_handle_pipeline_disable(struct softnic_thread_data *t,
441         struct thread_msg_req *req)
442 {
443         struct thread_msg_rsp *rsp = (struct thread_msg_rsp *)req;
444         uint32_t n_pipelines = t->n_pipelines;
445         struct rte_pipeline *pipeline = req->pipeline_disable.p;
446         uint32_t i;
447
448         /* find pipeline */
449         for (i = 0; i < n_pipelines; i++) {
450                 struct pipeline_data *p = &t->pipeline_data[i];
451
452                 if (p->p != pipeline)
453                         continue;
454
455                 if (i < n_pipelines - 1) {
456                         struct rte_pipeline *pipeline_last =
457                                 t->p[n_pipelines - 1];
458                         struct pipeline_data *p_last =
459                                 &t->pipeline_data[n_pipelines - 1];
460
461                         t->p[i] = pipeline_last;
462                         memcpy(p, p_last, sizeof(*p));
463                 }
464
465                 t->n_pipelines--;
466
467                 rsp->status = 0;
468                 return rsp;
469         }
470
471         /* should not get here */
472         rsp->status = 0;
473         return rsp;
474 }
475
476 static void
477 thread_msg_handle(struct softnic_thread_data *t)
478 {
479         for ( ; ; ) {
480                 struct thread_msg_req *req;
481                 struct thread_msg_rsp *rsp;
482
483                 req = thread_msg_recv(t->msgq_req);
484                 if (req == NULL)
485                         break;
486
487                 switch (req->type) {
488                 case THREAD_REQ_PIPELINE_ENABLE:
489                         rsp = thread_msg_handle_pipeline_enable(t, req);
490                         break;
491
492                 case THREAD_REQ_PIPELINE_DISABLE:
493                         rsp = thread_msg_handle_pipeline_disable(t, req);
494                         break;
495
496                 default:
497                         rsp = (struct thread_msg_rsp *)req;
498                         rsp->status = -1;
499                 }
500
501                 thread_msg_send(t->msgq_rsp, rsp);
502         }
503 }
504
505 /**
506  * Master thread & data plane threads: message passing
507  */
508 enum pipeline_req_type {
509         /* Port IN */
510         PIPELINE_REQ_PORT_IN_STATS_READ,
511         PIPELINE_REQ_PORT_IN_ENABLE,
512         PIPELINE_REQ_PORT_IN_DISABLE,
513
514         /* Port OUT */
515         PIPELINE_REQ_PORT_OUT_STATS_READ,
516
517         /* Table */
518         PIPELINE_REQ_TABLE_STATS_READ,
519         PIPELINE_REQ_TABLE_RULE_ADD,
520         PIPELINE_REQ_TABLE_RULE_ADD_DEFAULT,
521         PIPELINE_REQ_TABLE_RULE_ADD_BULK,
522         PIPELINE_REQ_TABLE_RULE_DELETE,
523         PIPELINE_REQ_TABLE_RULE_DELETE_DEFAULT,
524         PIPELINE_REQ_TABLE_RULE_STATS_READ,
525         PIPELINE_REQ_TABLE_MTR_PROFILE_ADD,
526         PIPELINE_REQ_TABLE_MTR_PROFILE_DELETE,
527         PIPELINE_REQ_TABLE_RULE_MTR_READ,
528         PIPELINE_REQ_TABLE_DSCP_TABLE_UPDATE,
529         PIPELINE_REQ_TABLE_RULE_TTL_READ,
530         PIPELINE_REQ_MAX
531 };
532
533 struct pipeline_msg_req_port_in_stats_read {
534         int clear;
535 };
536
537 struct pipeline_msg_req_port_out_stats_read {
538         int clear;
539 };
540
541 struct pipeline_msg_req_table_stats_read {
542         int clear;
543 };
544
545 struct pipeline_msg_req_table_rule_add {
546         struct softnic_table_rule_match match;
547         struct softnic_table_rule_action action;
548 };
549
550 struct pipeline_msg_req_table_rule_add_default {
551         struct softnic_table_rule_action action;
552 };
553
554 struct pipeline_msg_req_table_rule_add_bulk {
555         struct softnic_table_rule_match *match;
556         struct softnic_table_rule_action *action;
557         void **data;
558         uint32_t n_rules;
559         int bulk;
560 };
561
562 struct pipeline_msg_req_table_rule_delete {
563         struct softnic_table_rule_match match;
564 };
565
566 struct pipeline_msg_req_table_rule_stats_read {
567         void *data;
568         int clear;
569 };
570
571 struct pipeline_msg_req_table_mtr_profile_add {
572         uint32_t meter_profile_id;
573         struct rte_table_action_meter_profile profile;
574 };
575
576 struct pipeline_msg_req_table_mtr_profile_delete {
577         uint32_t meter_profile_id;
578 };
579
580 struct pipeline_msg_req_table_rule_mtr_read {
581         void *data;
582         uint32_t tc_mask;
583         int clear;
584 };
585
586 struct pipeline_msg_req_table_dscp_table_update {
587         uint64_t dscp_mask;
588         struct rte_table_action_dscp_table dscp_table;
589 };
590
591 struct pipeline_msg_req_table_rule_ttl_read {
592         void *data;
593         int clear;
594 };
595
596 struct pipeline_msg_req {
597         enum pipeline_req_type type;
598         uint32_t id; /* Port IN, port OUT or table ID */
599
600         RTE_STD_C11
601         union {
602                 struct pipeline_msg_req_port_in_stats_read port_in_stats_read;
603                 struct pipeline_msg_req_port_out_stats_read port_out_stats_read;
604                 struct pipeline_msg_req_table_stats_read table_stats_read;
605                 struct pipeline_msg_req_table_rule_add table_rule_add;
606                 struct pipeline_msg_req_table_rule_add_default table_rule_add_default;
607                 struct pipeline_msg_req_table_rule_add_bulk table_rule_add_bulk;
608                 struct pipeline_msg_req_table_rule_delete table_rule_delete;
609                 struct pipeline_msg_req_table_rule_stats_read table_rule_stats_read;
610                 struct pipeline_msg_req_table_mtr_profile_add table_mtr_profile_add;
611                 struct pipeline_msg_req_table_mtr_profile_delete table_mtr_profile_delete;
612                 struct pipeline_msg_req_table_rule_mtr_read table_rule_mtr_read;
613                 struct pipeline_msg_req_table_dscp_table_update table_dscp_table_update;
614                 struct pipeline_msg_req_table_rule_ttl_read table_rule_ttl_read;
615         };
616 };
617
618 struct pipeline_msg_rsp_port_in_stats_read {
619         struct rte_pipeline_port_in_stats stats;
620 };
621
622 struct pipeline_msg_rsp_port_out_stats_read {
623         struct rte_pipeline_port_out_stats stats;
624 };
625
626 struct pipeline_msg_rsp_table_stats_read {
627         struct rte_pipeline_table_stats stats;
628 };
629
630 struct pipeline_msg_rsp_table_rule_add {
631         void *data;
632 };
633
634 struct pipeline_msg_rsp_table_rule_add_default {
635         void *data;
636 };
637
638 struct pipeline_msg_rsp_table_rule_add_bulk {
639         uint32_t n_rules;
640 };
641
642 struct pipeline_msg_rsp_table_rule_stats_read {
643         struct rte_table_action_stats_counters stats;
644 };
645
646 struct pipeline_msg_rsp_table_rule_mtr_read {
647         struct rte_table_action_mtr_counters stats;
648 };
649
650 struct pipeline_msg_rsp_table_rule_ttl_read {
651         struct rte_table_action_ttl_counters stats;
652 };
653
654 struct pipeline_msg_rsp {
655         int status;
656
657         RTE_STD_C11
658         union {
659                 struct pipeline_msg_rsp_port_in_stats_read port_in_stats_read;
660                 struct pipeline_msg_rsp_port_out_stats_read port_out_stats_read;
661                 struct pipeline_msg_rsp_table_stats_read table_stats_read;
662                 struct pipeline_msg_rsp_table_rule_add table_rule_add;
663                 struct pipeline_msg_rsp_table_rule_add_default table_rule_add_default;
664                 struct pipeline_msg_rsp_table_rule_add_bulk table_rule_add_bulk;
665                 struct pipeline_msg_rsp_table_rule_stats_read table_rule_stats_read;
666                 struct pipeline_msg_rsp_table_rule_mtr_read table_rule_mtr_read;
667                 struct pipeline_msg_rsp_table_rule_ttl_read table_rule_ttl_read;
668         };
669 };
670
671 /**
672  * Master thread
673  */
674 static struct pipeline_msg_req *
675 pipeline_msg_alloc(void)
676 {
677         size_t size = RTE_MAX(sizeof(struct pipeline_msg_req),
678                 sizeof(struct pipeline_msg_rsp));
679
680         return calloc(1, size);
681 }
682
683 static void
684 pipeline_msg_free(struct pipeline_msg_rsp *rsp)
685 {
686         free(rsp);
687 }
688
689 static struct pipeline_msg_rsp *
690 pipeline_msg_send_recv(struct pipeline *p,
691         struct pipeline_msg_req *req)
692 {
693         struct rte_ring *msgq_req = p->msgq_req;
694         struct rte_ring *msgq_rsp = p->msgq_rsp;
695         struct pipeline_msg_rsp *rsp;
696         int status;
697
698         /* send */
699         do {
700                 status = rte_ring_sp_enqueue(msgq_req, req);
701         } while (status == -ENOBUFS);
702
703         /* recv */
704         do {
705                 status = rte_ring_sc_dequeue(msgq_rsp, (void **)&rsp);
706         } while (status != 0);
707
708         return rsp;
709 }
710
711 int
712 softnic_pipeline_port_in_stats_read(struct pmd_internals *softnic,
713         const char *pipeline_name,
714         uint32_t port_id,
715         struct rte_pipeline_port_in_stats *stats,
716         int clear)
717 {
718         struct pipeline *p;
719         struct pipeline_msg_req *req;
720         struct pipeline_msg_rsp *rsp;
721         int status;
722
723         /* Check input params */
724         if (pipeline_name == NULL ||
725                 stats == NULL)
726                 return -1;
727
728         p = softnic_pipeline_find(softnic, pipeline_name);
729         if (p == NULL ||
730                 port_id >= p->n_ports_in)
731                 return -1;
732
733         if (!pipeline_is_running(p)) {
734                 status = rte_pipeline_port_in_stats_read(p->p,
735                         port_id,
736                         stats,
737                         clear);
738
739                 return status;
740         }
741
742         /* Allocate request */
743         req = pipeline_msg_alloc();
744         if (req == NULL)
745                 return -1;
746
747         /* Write request */
748         req->type = PIPELINE_REQ_PORT_IN_STATS_READ;
749         req->id = port_id;
750         req->port_in_stats_read.clear = clear;
751
752         /* Send request and wait for response */
753         rsp = pipeline_msg_send_recv(p, req);
754         if (rsp == NULL)
755                 return -1;
756
757         /* Read response */
758         status = rsp->status;
759         if (status)
760                 memcpy(stats, &rsp->port_in_stats_read.stats, sizeof(*stats));
761
762         /* Free response */
763         pipeline_msg_free(rsp);
764
765         return status;
766 }
767
768 int
769 softnic_pipeline_port_in_enable(struct pmd_internals *softnic,
770         const char *pipeline_name,
771         uint32_t port_id)
772 {
773         struct pipeline *p;
774         struct pipeline_msg_req *req;
775         struct pipeline_msg_rsp *rsp;
776         int status;
777
778         /* Check input params */
779         if (pipeline_name == NULL)
780                 return -1;
781
782         p = softnic_pipeline_find(softnic, pipeline_name);
783         if (p == NULL ||
784                 port_id >= p->n_ports_in)
785                 return -1;
786
787         if (!pipeline_is_running(p)) {
788                 status = rte_pipeline_port_in_enable(p->p, port_id);
789                 return status;
790         }
791
792         /* Allocate request */
793         req = pipeline_msg_alloc();
794         if (req == NULL)
795                 return -1;
796
797         /* Write request */
798         req->type = PIPELINE_REQ_PORT_IN_ENABLE;
799         req->id = port_id;
800
801         /* Send request and wait for response */
802         rsp = pipeline_msg_send_recv(p, req);
803         if (rsp == NULL)
804                 return -1;
805
806         /* Read response */
807         status = rsp->status;
808
809         /* Free response */
810         pipeline_msg_free(rsp);
811
812         return status;
813 }
814
815 int
816 softnic_pipeline_port_in_disable(struct pmd_internals *softnic,
817         const char *pipeline_name,
818         uint32_t port_id)
819 {
820         struct pipeline *p;
821         struct pipeline_msg_req *req;
822         struct pipeline_msg_rsp *rsp;
823         int status;
824
825         /* Check input params */
826         if (pipeline_name == NULL)
827                 return -1;
828
829         p = softnic_pipeline_find(softnic, pipeline_name);
830         if (p == NULL ||
831                 port_id >= p->n_ports_in)
832                 return -1;
833
834         if (!pipeline_is_running(p)) {
835                 status = rte_pipeline_port_in_disable(p->p, port_id);
836                 return status;
837         }
838
839         /* Allocate request */
840         req = pipeline_msg_alloc();
841         if (req == NULL)
842                 return -1;
843
844         /* Write request */
845         req->type = PIPELINE_REQ_PORT_IN_DISABLE;
846         req->id = port_id;
847
848         /* Send request and wait for response */
849         rsp = pipeline_msg_send_recv(p, req);
850         if (rsp == NULL)
851                 return -1;
852
853         /* Read response */
854         status = rsp->status;
855
856         /* Free response */
857         pipeline_msg_free(rsp);
858
859         return status;
860 }
861
862 int
863 softnic_pipeline_port_out_stats_read(struct pmd_internals *softnic,
864         const char *pipeline_name,
865         uint32_t port_id,
866         struct rte_pipeline_port_out_stats *stats,
867         int clear)
868 {
869         struct pipeline *p;
870         struct pipeline_msg_req *req;
871         struct pipeline_msg_rsp *rsp;
872         int status;
873
874         /* Check input params */
875         if (pipeline_name == NULL ||
876                 stats == NULL)
877                 return -1;
878
879         p = softnic_pipeline_find(softnic, pipeline_name);
880         if (p == NULL ||
881                 port_id >= p->n_ports_out)
882                 return -1;
883
884         if (!pipeline_is_running(p)) {
885                 status = rte_pipeline_port_out_stats_read(p->p,
886                         port_id,
887                         stats,
888                         clear);
889
890                 return status;
891         }
892
893         /* Allocate request */
894         req = pipeline_msg_alloc();
895         if (req == NULL)
896                 return -1;
897
898         /* Write request */
899         req->type = PIPELINE_REQ_PORT_OUT_STATS_READ;
900         req->id = port_id;
901         req->port_out_stats_read.clear = clear;
902
903         /* Send request and wait for response */
904         rsp = pipeline_msg_send_recv(p, req);
905         if (rsp == NULL)
906                 return -1;
907
908         /* Read response */
909         status = rsp->status;
910         if (status)
911                 memcpy(stats, &rsp->port_out_stats_read.stats, sizeof(*stats));
912
913         /* Free response */
914         pipeline_msg_free(rsp);
915
916         return status;
917 }
918
919 int
920 softnic_pipeline_table_stats_read(struct pmd_internals *softnic,
921         const char *pipeline_name,
922         uint32_t table_id,
923         struct rte_pipeline_table_stats *stats,
924         int clear)
925 {
926         struct pipeline *p;
927         struct pipeline_msg_req *req;
928         struct pipeline_msg_rsp *rsp;
929         int status;
930
931         /* Check input params */
932         if (pipeline_name == NULL ||
933                 stats == NULL)
934                 return -1;
935
936         p = softnic_pipeline_find(softnic, pipeline_name);
937         if (p == NULL ||
938                 table_id >= p->n_tables)
939                 return -1;
940
941         if (!pipeline_is_running(p)) {
942                 status = rte_pipeline_table_stats_read(p->p,
943                         table_id,
944                         stats,
945                         clear);
946
947                 return status;
948         }
949
950         /* Allocate request */
951         req = pipeline_msg_alloc();
952         if (req == NULL)
953                 return -1;
954
955         /* Write request */
956         req->type = PIPELINE_REQ_TABLE_STATS_READ;
957         req->id = table_id;
958         req->table_stats_read.clear = clear;
959
960         /* Send request and wait for response */
961         rsp = pipeline_msg_send_recv(p, req);
962         if (rsp == NULL)
963                 return -1;
964
965         /* Read response */
966         status = rsp->status;
967         if (status)
968                 memcpy(stats, &rsp->table_stats_read.stats, sizeof(*stats));
969
970         /* Free response */
971         pipeline_msg_free(rsp);
972
973         return status;
974 }
975
976 static int
977 match_check(struct softnic_table_rule_match *match,
978         struct pipeline *p,
979         uint32_t table_id)
980 {
981         struct softnic_table *table;
982
983         if (match == NULL ||
984                 p == NULL ||
985                 table_id >= p->n_tables)
986                 return -1;
987
988         table = &p->table[table_id];
989         if (match->match_type != table->params.match_type)
990                 return -1;
991
992         switch (match->match_type) {
993         case TABLE_ACL:
994         {
995                 struct softnic_table_acl_params *t = &table->params.match.acl;
996                 struct softnic_table_rule_match_acl *r = &match->match.acl;
997
998                 if ((r->ip_version && (t->ip_version == 0)) ||
999                         ((r->ip_version == 0) && t->ip_version))
1000                         return -1;
1001
1002                 if (r->ip_version) {
1003                         if (r->sa_depth > 32 ||
1004                                 r->da_depth > 32)
1005                                 return -1;
1006                 } else {
1007                         if (r->sa_depth > 128 ||
1008                                 r->da_depth > 128)
1009                                 return -1;
1010                 }
1011                 return 0;
1012         }
1013
1014         case TABLE_ARRAY:
1015                 return 0;
1016
1017         case TABLE_HASH:
1018                 return 0;
1019
1020         case TABLE_LPM:
1021         {
1022                 struct softnic_table_lpm_params *t = &table->params.match.lpm;
1023                 struct softnic_table_rule_match_lpm *r = &match->match.lpm;
1024
1025                 if ((r->ip_version && (t->key_size != 4)) ||
1026                         ((r->ip_version == 0) && (t->key_size != 16)))
1027                         return -1;
1028
1029                 if (r->ip_version) {
1030                         if (r->depth > 32)
1031                                 return -1;
1032                 } else {
1033                         if (r->depth > 128)
1034                                 return -1;
1035                 }
1036                 return 0;
1037         }
1038
1039         case TABLE_STUB:
1040                 return -1;
1041
1042         default:
1043                 return -1;
1044         }
1045 }
1046
1047 static int
1048 action_check(struct softnic_table_rule_action *action,
1049         struct pipeline *p,
1050         uint32_t table_id)
1051 {
1052         struct softnic_table_action_profile *ap;
1053
1054         if (action == NULL ||
1055                 p == NULL ||
1056                 table_id >= p->n_tables)
1057                 return -1;
1058
1059         ap = p->table[table_id].ap;
1060         if (action->action_mask != ap->params.action_mask)
1061                 return -1;
1062
1063         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_FWD)) {
1064                 if (action->fwd.action == RTE_PIPELINE_ACTION_PORT &&
1065                         action->fwd.id >= p->n_ports_out)
1066                         return -1;
1067
1068                 if (action->fwd.action == RTE_PIPELINE_ACTION_TABLE &&
1069                         action->fwd.id >= p->n_tables)
1070                         return -1;
1071         }
1072
1073         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) {
1074                 uint32_t tc_mask0 = (1 << ap->params.mtr.n_tc) - 1;
1075                 uint32_t tc_mask1 = action->mtr.tc_mask;
1076
1077                 if (tc_mask1 != tc_mask0)
1078                         return -1;
1079         }
1080
1081         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_TM)) {
1082                 uint32_t n_subports_per_port =
1083                         ap->params.tm.n_subports_per_port;
1084                 uint32_t n_pipes_per_subport =
1085                         ap->params.tm.n_pipes_per_subport;
1086                 uint32_t subport_id = action->tm.subport_id;
1087                 uint32_t pipe_id = action->tm.pipe_id;
1088
1089                 if (subport_id >= n_subports_per_port ||
1090                         pipe_id >= n_pipes_per_subport)
1091                         return -1;
1092         }
1093
1094         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_ENCAP)) {
1095                 uint64_t encap_mask = ap->params.encap.encap_mask;
1096                 enum rte_table_action_encap_type type = action->encap.type;
1097
1098                 if ((encap_mask & (1LLU << type)) == 0)
1099                         return -1;
1100         }
1101
1102         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_NAT)) {
1103                 int ip_version0 = ap->params.common.ip_version;
1104                 int ip_version1 = action->nat.ip_version;
1105
1106                 if ((ip_version1 && (ip_version0 == 0)) ||
1107                         ((ip_version1 == 0) && ip_version0))
1108                         return -1;
1109         }
1110
1111         return 0;
1112 }
1113
1114 static int
1115 action_default_check(struct softnic_table_rule_action *action,
1116         struct pipeline *p,
1117         uint32_t table_id)
1118 {
1119         if (action == NULL ||
1120                 action->action_mask != (1LLU << RTE_TABLE_ACTION_FWD) ||
1121                 p == NULL ||
1122                 table_id >= p->n_tables)
1123                 return -1;
1124
1125         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_FWD)) {
1126                 if (action->fwd.action == RTE_PIPELINE_ACTION_PORT &&
1127                         action->fwd.id >= p->n_ports_out)
1128                         return -1;
1129
1130                 if (action->fwd.action == RTE_PIPELINE_ACTION_TABLE &&
1131                         action->fwd.id >= p->n_tables)
1132                         return -1;
1133         }
1134
1135         return 0;
1136 }
1137
1138 union table_rule_match_low_level {
1139         struct rte_table_acl_rule_add_params acl_add;
1140         struct rte_table_acl_rule_delete_params acl_delete;
1141         struct rte_table_array_key array;
1142         uint8_t hash[TABLE_RULE_MATCH_SIZE_MAX];
1143         struct rte_table_lpm_key lpm_ipv4;
1144         struct rte_table_lpm_ipv6_key lpm_ipv6;
1145 };
1146
1147 static int
1148 match_convert(struct softnic_table_rule_match *mh,
1149         union table_rule_match_low_level *ml,
1150         int add);
1151
1152 static int
1153 action_convert(struct rte_table_action *a,
1154         struct softnic_table_rule_action *action,
1155         struct rte_pipeline_table_entry *data);
1156
1157 int
1158 softnic_pipeline_table_rule_add(struct pmd_internals *softnic,
1159         const char *pipeline_name,
1160         uint32_t table_id,
1161         struct softnic_table_rule_match *match,
1162         struct softnic_table_rule_action *action,
1163         void **data)
1164 {
1165         struct pipeline *p;
1166         struct pipeline_msg_req *req;
1167         struct pipeline_msg_rsp *rsp;
1168         int status;
1169
1170         /* Check input params */
1171         if (pipeline_name == NULL ||
1172                 match == NULL ||
1173                 action == NULL ||
1174                 data == NULL)
1175                 return -1;
1176
1177         p = softnic_pipeline_find(softnic, pipeline_name);
1178         if (p == NULL ||
1179                 table_id >= p->n_tables ||
1180                 match_check(match, p, table_id) ||
1181                 action_check(action, p, table_id))
1182                 return -1;
1183
1184         if (!pipeline_is_running(p)) {
1185                 struct rte_table_action *a = p->table[table_id].a;
1186                 union table_rule_match_low_level match_ll;
1187                 struct rte_pipeline_table_entry *data_in, *data_out;
1188                 int key_found;
1189                 uint8_t *buffer;
1190
1191                 buffer = calloc(TABLE_RULE_ACTION_SIZE_MAX, sizeof(uint8_t));
1192                 if (buffer == NULL)
1193                         return -1;
1194
1195                 /* Table match-action rule conversion */
1196                 data_in = (struct rte_pipeline_table_entry *)buffer;
1197
1198                 status = match_convert(match, &match_ll, 1);
1199                 if (status) {
1200                         free(buffer);
1201                         return -1;
1202                 }
1203
1204                 status = action_convert(a, action, data_in);
1205                 if (status) {
1206                         free(buffer);
1207                         return -1;
1208                 }
1209
1210                 /* Add rule (match, action) to table */
1211                 status = rte_pipeline_table_entry_add(p->p,
1212                                 table_id,
1213                                 &match_ll,
1214                                 data_in,
1215                                 &key_found,
1216                                 &data_out);
1217                 if (status) {
1218                         free(buffer);
1219                         return -1;
1220                 }
1221
1222                 /* Write Response */
1223                 *data = data_out;
1224
1225                 free(buffer);
1226                 return 0;
1227         }
1228
1229         /* Allocate request */
1230         req = pipeline_msg_alloc();
1231         if (req == NULL)
1232                 return -1;
1233
1234         /* Write request */
1235         req->type = PIPELINE_REQ_TABLE_RULE_ADD;
1236         req->id = table_id;
1237         memcpy(&req->table_rule_add.match, match, sizeof(*match));
1238         memcpy(&req->table_rule_add.action, action, sizeof(*action));
1239
1240         /* Send request and wait for response */
1241         rsp = pipeline_msg_send_recv(p, req);
1242         if (rsp == NULL)
1243                 return -1;
1244
1245         /* Read response */
1246         status = rsp->status;
1247         if (status == 0)
1248                 *data = rsp->table_rule_add.data;
1249
1250         /* Free response */
1251         pipeline_msg_free(rsp);
1252
1253         return status;
1254 }
1255
1256 int
1257 softnic_pipeline_table_rule_add_default(struct pmd_internals *softnic,
1258         const char *pipeline_name,
1259         uint32_t table_id,
1260         struct softnic_table_rule_action *action,
1261         void **data)
1262 {
1263         struct pipeline *p;
1264         struct pipeline_msg_req *req;
1265         struct pipeline_msg_rsp *rsp;
1266         int status;
1267
1268         /* Check input params */
1269         if (pipeline_name == NULL ||
1270                 action == NULL ||
1271                 data == NULL)
1272                 return -1;
1273
1274         p = softnic_pipeline_find(softnic, pipeline_name);
1275         if (p == NULL ||
1276                 table_id >= p->n_tables ||
1277                 action_default_check(action, p, table_id))
1278                 return -1;
1279
1280         if (!pipeline_is_running(p)) {
1281                 struct rte_pipeline_table_entry *data_in, *data_out;
1282                 uint8_t *buffer;
1283
1284                 buffer = calloc(TABLE_RULE_ACTION_SIZE_MAX, sizeof(uint8_t));
1285                 if (buffer == NULL)
1286                         return -1;
1287
1288                 /* Apply actions */
1289                 data_in = (struct rte_pipeline_table_entry *)buffer;
1290
1291                 data_in->action = action->fwd.action;
1292                 if (action->fwd.action == RTE_PIPELINE_ACTION_PORT)
1293                         data_in->port_id = action->fwd.id;
1294                 if (action->fwd.action == RTE_PIPELINE_ACTION_TABLE)
1295                         data_in->table_id = action->fwd.id;
1296
1297                 /* Add default rule to table */
1298                 status = rte_pipeline_table_default_entry_add(p->p,
1299                                 table_id,
1300                                 data_in,
1301                                 &data_out);
1302                 if (status) {
1303                         free(buffer);
1304                         return -1;
1305                 }
1306
1307                 /* Write Response */
1308                 *data = data_out;
1309
1310                 free(buffer);
1311                 return 0;
1312         }
1313
1314         /* Allocate request */
1315         req = pipeline_msg_alloc();
1316         if (req == NULL)
1317                 return -1;
1318
1319         /* Write request */
1320         req->type = PIPELINE_REQ_TABLE_RULE_ADD_DEFAULT;
1321         req->id = table_id;
1322         memcpy(&req->table_rule_add_default.action, action, sizeof(*action));
1323
1324         /* Send request and wait for response */
1325         rsp = pipeline_msg_send_recv(p, req);
1326         if (rsp == NULL)
1327                 return -1;
1328
1329         /* Read response */
1330         status = rsp->status;
1331         if (status == 0)
1332                 *data = rsp->table_rule_add_default.data;
1333
1334         /* Free response */
1335         pipeline_msg_free(rsp);
1336
1337         return status;
1338 }
1339
1340 int
1341 softnic_pipeline_table_rule_add_bulk(struct pmd_internals *softnic,
1342         const char *pipeline_name,
1343         uint32_t table_id,
1344         struct softnic_table_rule_match *match,
1345         struct softnic_table_rule_action *action,
1346         void **data,
1347         uint32_t *n_rules)
1348 {
1349         struct pipeline *p;
1350         struct pipeline_msg_req *req;
1351         struct pipeline_msg_rsp *rsp;
1352         uint32_t i;
1353         int status;
1354
1355         /* Check input params */
1356         if (pipeline_name == NULL ||
1357                 match == NULL ||
1358                 action == NULL ||
1359                 data == NULL ||
1360                 n_rules == NULL ||
1361                 (*n_rules == 0))
1362                 return -1;
1363
1364         p = softnic_pipeline_find(softnic, pipeline_name);
1365         if (p == NULL ||
1366                 table_id >= p->n_tables)
1367                 return -1;
1368
1369         for (i = 0; i < *n_rules; i++)
1370                 if (match_check(match, p, table_id) ||
1371                         action_check(action, p, table_id))
1372                         return -1;
1373
1374         if (!pipeline_is_running(p)) {
1375                 struct rte_table_action *a = p->table[table_id].a;
1376                 union table_rule_match_low_level *match_ll;
1377                 uint8_t *action_ll;
1378                 void **match_ll_ptr;
1379                 struct rte_pipeline_table_entry **action_ll_ptr;
1380                 struct rte_pipeline_table_entry **entries_ptr =
1381                         (struct rte_pipeline_table_entry **)data;
1382                 uint32_t bulk =
1383                         (p->table[table_id].params.match_type == TABLE_ACL) ? 1 : 0;
1384                 int *found;
1385
1386                 /* Memory allocation */
1387                 match_ll = calloc(*n_rules, sizeof(union table_rule_match_low_level));
1388                 action_ll = calloc(*n_rules, TABLE_RULE_ACTION_SIZE_MAX);
1389                 match_ll_ptr = calloc(*n_rules, sizeof(void *));
1390                 action_ll_ptr =
1391                         calloc(*n_rules, sizeof(struct rte_pipeline_table_entry *));
1392                 found = calloc(*n_rules, sizeof(int));
1393
1394                 if (match_ll == NULL ||
1395                         action_ll == NULL ||
1396                         match_ll_ptr == NULL ||
1397                         action_ll_ptr == NULL ||
1398                         found == NULL)
1399                         goto fail;
1400
1401                 for (i = 0; i < *n_rules; i++) {
1402                         match_ll_ptr[i] = (void *)&match_ll[i];
1403                         action_ll_ptr[i] =
1404                                 (struct rte_pipeline_table_entry *)&action_ll[i * TABLE_RULE_ACTION_SIZE_MAX];
1405                 }
1406
1407                 /* Rule match conversion */
1408                 for (i = 0; i < *n_rules; i++) {
1409                         status = match_convert(&match[i], match_ll_ptr[i], 1);
1410                         if (status)
1411                                 goto fail;
1412                 }
1413
1414                 /* Rule action conversion */
1415                 for (i = 0; i < *n_rules; i++) {
1416                         status = action_convert(a, &action[i], action_ll_ptr[i]);
1417                         if (status)
1418                                 goto fail;
1419                 }
1420
1421                 /* Add rule (match, action) to table */
1422                 if (bulk) {
1423                         status = rte_pipeline_table_entry_add_bulk(p->p,
1424                                 table_id,
1425                                 match_ll_ptr,
1426                                 action_ll_ptr,
1427                                 *n_rules,
1428                                 found,
1429                                 entries_ptr);
1430                         if (status)
1431                                 *n_rules = 0;
1432                 } else {
1433                         for (i = 0; i < *n_rules; i++) {
1434                                 status = rte_pipeline_table_entry_add(p->p,
1435                                         table_id,
1436                                         match_ll_ptr[i],
1437                                         action_ll_ptr[i],
1438                                         &found[i],
1439                                         &entries_ptr[i]);
1440                                 if (status) {
1441                                         *n_rules = i;
1442                                         break;
1443                                 }
1444                         }
1445                 }
1446
1447                 /* Free */
1448                 free(found);
1449                 free(action_ll_ptr);
1450                 free(match_ll_ptr);
1451                 free(action_ll);
1452                 free(match_ll);
1453
1454                 return status;
1455
1456 fail:
1457                 free(found);
1458                 free(action_ll_ptr);
1459                 free(match_ll_ptr);
1460                 free(action_ll);
1461                 free(match_ll);
1462
1463                 *n_rules = 0;
1464                 return -1;
1465         }
1466
1467         /* Allocate request */
1468         req = pipeline_msg_alloc();
1469         if (req == NULL)
1470                 return -1;
1471
1472         /* Write request */
1473         req->type = PIPELINE_REQ_TABLE_RULE_ADD_BULK;
1474         req->id = table_id;
1475         req->table_rule_add_bulk.match = match;
1476         req->table_rule_add_bulk.action = action;
1477         req->table_rule_add_bulk.data = data;
1478         req->table_rule_add_bulk.n_rules = *n_rules;
1479         req->table_rule_add_bulk.bulk =
1480                 (p->table[table_id].params.match_type == TABLE_ACL) ? 1 : 0;
1481
1482         /* Send request and wait for response */
1483         rsp = pipeline_msg_send_recv(p, req);
1484         if (rsp == NULL)
1485                 return -1;
1486
1487         /* Read response */
1488         status = rsp->status;
1489         if (status == 0)
1490                 *n_rules = rsp->table_rule_add_bulk.n_rules;
1491
1492         /* Free response */
1493         pipeline_msg_free(rsp);
1494
1495         return status;
1496 }
1497
1498 int
1499 softnic_pipeline_table_rule_delete(struct pmd_internals *softnic,
1500         const char *pipeline_name,
1501         uint32_t table_id,
1502         struct softnic_table_rule_match *match)
1503 {
1504         struct pipeline *p;
1505         struct pipeline_msg_req *req;
1506         struct pipeline_msg_rsp *rsp;
1507         int status;
1508
1509         /* Check input params */
1510         if (pipeline_name == NULL ||
1511                 match == NULL)
1512                 return -1;
1513
1514         p = softnic_pipeline_find(softnic, pipeline_name);
1515         if (p == NULL ||
1516                 table_id >= p->n_tables ||
1517                 match_check(match, p, table_id))
1518                 return -1;
1519
1520         if (!pipeline_is_running(p)) {
1521                 union table_rule_match_low_level match_ll;
1522                 int key_found;
1523
1524                 status = match_convert(match, &match_ll, 0);
1525                 if (status)
1526                         return -1;
1527
1528                 status = rte_pipeline_table_entry_delete(p->p,
1529                                 table_id,
1530                                 &match_ll,
1531                                 &key_found,
1532                                 NULL);
1533
1534                 return status;
1535         }
1536
1537         /* Allocate request */
1538         req = pipeline_msg_alloc();
1539         if (req == NULL)
1540                 return -1;
1541
1542         /* Write request */
1543         req->type = PIPELINE_REQ_TABLE_RULE_DELETE;
1544         req->id = table_id;
1545         memcpy(&req->table_rule_delete.match, match, sizeof(*match));
1546
1547         /* Send request and wait for response */
1548         rsp = pipeline_msg_send_recv(p, req);
1549         if (rsp == NULL)
1550                 return -1;
1551
1552         /* Read response */
1553         status = rsp->status;
1554
1555         /* Free response */
1556         pipeline_msg_free(rsp);
1557
1558         return status;
1559 }
1560
1561 int
1562 softnic_pipeline_table_rule_delete_default(struct pmd_internals *softnic,
1563         const char *pipeline_name,
1564         uint32_t table_id)
1565 {
1566         struct pipeline *p;
1567         struct pipeline_msg_req *req;
1568         struct pipeline_msg_rsp *rsp;
1569         int status;
1570
1571         /* Check input params */
1572         if (pipeline_name == NULL)
1573                 return -1;
1574
1575         p = softnic_pipeline_find(softnic, pipeline_name);
1576         if (p == NULL ||
1577                 table_id >= p->n_tables)
1578                 return -1;
1579
1580         if (!pipeline_is_running(p)) {
1581                 status = rte_pipeline_table_default_entry_delete(p->p,
1582                         table_id,
1583                         NULL);
1584
1585                 return status;
1586         }
1587
1588         /* Allocate request */
1589         req = pipeline_msg_alloc();
1590         if (req == NULL)
1591                 return -1;
1592
1593         /* Write request */
1594         req->type = PIPELINE_REQ_TABLE_RULE_DELETE_DEFAULT;
1595         req->id = table_id;
1596
1597         /* Send request and wait for response */
1598         rsp = pipeline_msg_send_recv(p, req);
1599         if (rsp == NULL)
1600                 return -1;
1601
1602         /* Read response */
1603         status = rsp->status;
1604
1605         /* Free response */
1606         pipeline_msg_free(rsp);
1607
1608         return status;
1609 }
1610
1611 int
1612 softnic_pipeline_table_rule_stats_read(struct pmd_internals *softnic,
1613         const char *pipeline_name,
1614         uint32_t table_id,
1615         void *data,
1616         struct rte_table_action_stats_counters *stats,
1617         int clear)
1618 {
1619         struct pipeline *p;
1620         struct pipeline_msg_req *req;
1621         struct pipeline_msg_rsp *rsp;
1622         int status;
1623
1624         /* Check input params */
1625         if (pipeline_name == NULL ||
1626                 data == NULL ||
1627                 stats == NULL)
1628                 return -1;
1629
1630         p = softnic_pipeline_find(softnic, pipeline_name);
1631         if (p == NULL ||
1632                 table_id >= p->n_tables)
1633                 return -1;
1634
1635         if (!pipeline_is_running(p)) {
1636                 struct rte_table_action *a = p->table[table_id].a;
1637
1638                 status = rte_table_action_stats_read(a,
1639                         data,
1640                         stats,
1641                         clear);
1642
1643                 return status;
1644         }
1645
1646         /* Allocate request */
1647         req = pipeline_msg_alloc();
1648         if (req == NULL)
1649                 return -1;
1650
1651         /* Write request */
1652         req->type = PIPELINE_REQ_TABLE_RULE_STATS_READ;
1653         req->id = table_id;
1654         req->table_rule_stats_read.data = data;
1655         req->table_rule_stats_read.clear = clear;
1656
1657         /* Send request and wait for response */
1658         rsp = pipeline_msg_send_recv(p, req);
1659         if (rsp == NULL)
1660                 return -1;
1661
1662         /* Read response */
1663         status = rsp->status;
1664         if (status)
1665                 memcpy(stats, &rsp->table_rule_stats_read.stats, sizeof(*stats));
1666
1667         /* Free response */
1668         pipeline_msg_free(rsp);
1669
1670         return status;
1671 }
1672
1673 int
1674 softnic_pipeline_table_mtr_profile_add(struct pmd_internals *softnic,
1675         const char *pipeline_name,
1676         uint32_t table_id,
1677         uint32_t meter_profile_id,
1678         struct rte_table_action_meter_profile *profile)
1679 {
1680         struct pipeline *p;
1681         struct pipeline_msg_req *req;
1682         struct pipeline_msg_rsp *rsp;
1683         struct softnic_table *table;
1684         struct softnic_table_meter_profile *mp;
1685         int status;
1686
1687         /* Check input params */
1688         if (pipeline_name == NULL ||
1689                 profile == NULL)
1690                 return -1;
1691
1692         p = softnic_pipeline_find(softnic, pipeline_name);
1693         if (p == NULL ||
1694                 table_id >= p->n_tables)
1695                 return -1;
1696
1697         table = &p->table[table_id];
1698         mp = softnic_pipeline_table_meter_profile_find(table, meter_profile_id);
1699         if (mp)
1700                 return -1;
1701
1702         /* Resource Allocation */
1703         mp = calloc(1, sizeof(struct softnic_table_meter_profile));
1704         if (mp == NULL)
1705                 return -1;
1706
1707         mp->meter_profile_id = meter_profile_id;
1708         memcpy(&mp->profile, profile, sizeof(mp->profile));
1709
1710         if (!pipeline_is_running(p)) {
1711                 status = rte_table_action_meter_profile_add(table->a,
1712                         meter_profile_id,
1713                         profile);
1714                 if (status) {
1715                         free(mp);
1716                         return status;
1717                 }
1718
1719                 /* Add profile to the table. */
1720                 TAILQ_INSERT_TAIL(&table->meter_profiles, mp, node);
1721
1722                 return status;
1723         }
1724
1725         /* Allocate request */
1726         req = pipeline_msg_alloc();
1727         if (req == NULL) {
1728                 free(mp);
1729                 return -1;
1730         }
1731
1732         /* Write request */
1733         req->type = PIPELINE_REQ_TABLE_MTR_PROFILE_ADD;
1734         req->id = table_id;
1735         req->table_mtr_profile_add.meter_profile_id = meter_profile_id;
1736         memcpy(&req->table_mtr_profile_add.profile, profile, sizeof(*profile));
1737
1738         /* Send request and wait for response */
1739         rsp = pipeline_msg_send_recv(p, req);
1740         if (rsp == NULL) {
1741                 free(mp);
1742                 return -1;
1743         }
1744
1745         /* Read response */
1746         status = rsp->status;
1747         if (status == 0)
1748                 TAILQ_INSERT_TAIL(&table->meter_profiles, mp, node);
1749         else
1750                 free(mp);
1751
1752         /* Free response */
1753         pipeline_msg_free(rsp);
1754
1755         return status;
1756 }
1757
1758 int
1759 softnic_pipeline_table_mtr_profile_delete(struct pmd_internals *softnic,
1760         const char *pipeline_name,
1761         uint32_t table_id,
1762         uint32_t meter_profile_id)
1763 {
1764         struct pipeline *p;
1765         struct pipeline_msg_req *req;
1766         struct pipeline_msg_rsp *rsp;
1767         int status;
1768
1769         /* Check input params */
1770         if (pipeline_name == NULL)
1771                 return -1;
1772
1773         p = softnic_pipeline_find(softnic, pipeline_name);
1774         if (p == NULL ||
1775                 table_id >= p->n_tables)
1776                 return -1;
1777
1778         if (!pipeline_is_running(p)) {
1779                 struct rte_table_action *a = p->table[table_id].a;
1780
1781                 status = rte_table_action_meter_profile_delete(a,
1782                                 meter_profile_id);
1783
1784                 return status;
1785         }
1786
1787         /* Allocate request */
1788         req = pipeline_msg_alloc();
1789         if (req == NULL)
1790                 return -1;
1791
1792         /* Write request */
1793         req->type = PIPELINE_REQ_TABLE_MTR_PROFILE_DELETE;
1794         req->id = table_id;
1795         req->table_mtr_profile_delete.meter_profile_id = meter_profile_id;
1796
1797         /* Send request and wait for response */
1798         rsp = pipeline_msg_send_recv(p, req);
1799         if (rsp == NULL)
1800                 return -1;
1801
1802         /* Read response */
1803         status = rsp->status;
1804
1805         /* Free response */
1806         pipeline_msg_free(rsp);
1807
1808         return status;
1809 }
1810
1811 int
1812 softnic_pipeline_table_rule_mtr_read(struct pmd_internals *softnic,
1813         const char *pipeline_name,
1814         uint32_t table_id,
1815         void *data,
1816         uint32_t tc_mask,
1817         struct rte_table_action_mtr_counters *stats,
1818         int clear)
1819 {
1820         struct pipeline *p;
1821         struct pipeline_msg_req *req;
1822         struct pipeline_msg_rsp *rsp;
1823         int status;
1824
1825         /* Check input params */
1826         if (pipeline_name == NULL ||
1827                 data == NULL ||
1828                 stats == NULL)
1829                 return -1;
1830
1831         p = softnic_pipeline_find(softnic, pipeline_name);
1832         if (p == NULL ||
1833                 table_id >= p->n_tables)
1834                 return -1;
1835
1836         if (!pipeline_is_running(p)) {
1837                 struct rte_table_action *a = p->table[table_id].a;
1838
1839                 status = rte_table_action_meter_read(a,
1840                                 data,
1841                                 tc_mask,
1842                                 stats,
1843                                 clear);
1844
1845                 return status;
1846         }
1847
1848         /* Allocate request */
1849         req = pipeline_msg_alloc();
1850         if (req == NULL)
1851                 return -1;
1852
1853         /* Write request */
1854         req->type = PIPELINE_REQ_TABLE_RULE_MTR_READ;
1855         req->id = table_id;
1856         req->table_rule_mtr_read.data = data;
1857         req->table_rule_mtr_read.tc_mask = tc_mask;
1858         req->table_rule_mtr_read.clear = clear;
1859
1860         /* Send request and wait for response */
1861         rsp = pipeline_msg_send_recv(p, req);
1862         if (rsp == NULL)
1863                 return -1;
1864
1865         /* Read response */
1866         status = rsp->status;
1867         if (status)
1868                 memcpy(stats, &rsp->table_rule_mtr_read.stats, sizeof(*stats));
1869
1870         /* Free response */
1871         pipeline_msg_free(rsp);
1872
1873         return status;
1874 }
1875
1876 int
1877 softnic_pipeline_table_dscp_table_update(struct pmd_internals *softnic,
1878         const char *pipeline_name,
1879         uint32_t table_id,
1880         uint64_t dscp_mask,
1881         struct rte_table_action_dscp_table *dscp_table)
1882 {
1883         struct pipeline *p;
1884         struct pipeline_msg_req *req;
1885         struct pipeline_msg_rsp *rsp;
1886         int status;
1887
1888         /* Check input params */
1889         if (pipeline_name == NULL ||
1890                 dscp_table == NULL)
1891                 return -1;
1892
1893         p = softnic_pipeline_find(softnic, pipeline_name);
1894         if (p == NULL ||
1895                 table_id >= p->n_tables)
1896                 return -1;
1897
1898         if (!pipeline_is_running(p)) {
1899                 struct rte_table_action *a = p->table[table_id].a;
1900
1901                 status = rte_table_action_dscp_table_update(a,
1902                                 dscp_mask,
1903                                 dscp_table);
1904
1905                 return status;
1906         }
1907
1908         /* Allocate request */
1909         req = pipeline_msg_alloc();
1910         if (req == NULL)
1911                 return -1;
1912
1913         /* Write request */
1914         req->type = PIPELINE_REQ_TABLE_DSCP_TABLE_UPDATE;
1915         req->id = table_id;
1916         req->table_dscp_table_update.dscp_mask = dscp_mask;
1917         memcpy(&req->table_dscp_table_update.dscp_table,
1918                 dscp_table, sizeof(*dscp_table));
1919
1920         /* Send request and wait for response */
1921         rsp = pipeline_msg_send_recv(p, req);
1922         if (rsp == NULL)
1923                 return -1;
1924
1925         /* Read response */
1926         status = rsp->status;
1927
1928         /* Free response */
1929         pipeline_msg_free(rsp);
1930
1931         return status;
1932 }
1933
1934 int
1935 softnic_pipeline_table_rule_ttl_read(struct pmd_internals *softnic,
1936         const char *pipeline_name,
1937         uint32_t table_id,
1938         void *data,
1939         struct rte_table_action_ttl_counters *stats,
1940         int clear)
1941 {
1942         struct pipeline *p;
1943         struct pipeline_msg_req *req;
1944         struct pipeline_msg_rsp *rsp;
1945         int status;
1946
1947         /* Check input params */
1948         if (pipeline_name == NULL ||
1949                 data == NULL ||
1950                 stats == NULL)
1951                 return -1;
1952
1953         p = softnic_pipeline_find(softnic, pipeline_name);
1954         if (p == NULL ||
1955                 table_id >= p->n_tables)
1956                 return -1;
1957
1958         if (!pipeline_is_running(p)) {
1959                 struct rte_table_action *a = p->table[table_id].a;
1960
1961                 status = rte_table_action_ttl_read(a,
1962                                 data,
1963                                 stats,
1964                                 clear);
1965
1966                 return status;
1967         }
1968
1969         /* Allocate request */
1970         req = pipeline_msg_alloc();
1971         if (req == NULL)
1972                 return -1;
1973
1974         /* Write request */
1975         req->type = PIPELINE_REQ_TABLE_RULE_TTL_READ;
1976         req->id = table_id;
1977         req->table_rule_ttl_read.data = data;
1978         req->table_rule_ttl_read.clear = clear;
1979
1980         /* Send request and wait for response */
1981         rsp = pipeline_msg_send_recv(p, req);
1982         if (rsp == NULL)
1983                 return -1;
1984
1985         /* Read response */
1986         status = rsp->status;
1987         if (status)
1988                 memcpy(stats, &rsp->table_rule_ttl_read.stats, sizeof(*stats));
1989
1990         /* Free response */
1991         pipeline_msg_free(rsp);
1992
1993         return status;
1994 }
1995
1996 /**
1997  * Data plane threads: message handling
1998  */
1999 static inline struct pipeline_msg_req *
2000 pipeline_msg_recv(struct rte_ring *msgq_req)
2001 {
2002         struct pipeline_msg_req *req;
2003
2004         int status = rte_ring_sc_dequeue(msgq_req, (void **)&req);
2005
2006         if (status != 0)
2007                 return NULL;
2008
2009         return req;
2010 }
2011
2012 static inline void
2013 pipeline_msg_send(struct rte_ring *msgq_rsp,
2014         struct pipeline_msg_rsp *rsp)
2015 {
2016         int status;
2017
2018         do {
2019                 status = rte_ring_sp_enqueue(msgq_rsp, rsp);
2020         } while (status == -ENOBUFS);
2021 }
2022
2023 static struct pipeline_msg_rsp *
2024 pipeline_msg_handle_port_in_stats_read(struct pipeline_data *p,
2025         struct pipeline_msg_req *req)
2026 {
2027         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2028         uint32_t port_id = req->id;
2029         int clear = req->port_in_stats_read.clear;
2030
2031         rsp->status = rte_pipeline_port_in_stats_read(p->p,
2032                 port_id,
2033                 &rsp->port_in_stats_read.stats,
2034                 clear);
2035
2036         return rsp;
2037 }
2038
2039 static struct pipeline_msg_rsp *
2040 pipeline_msg_handle_port_in_enable(struct pipeline_data *p,
2041         struct pipeline_msg_req *req)
2042 {
2043         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2044         uint32_t port_id = req->id;
2045
2046         rsp->status = rte_pipeline_port_in_enable(p->p,
2047                 port_id);
2048
2049         return rsp;
2050 }
2051
2052 static struct pipeline_msg_rsp *
2053 pipeline_msg_handle_port_in_disable(struct pipeline_data *p,
2054         struct pipeline_msg_req *req)
2055 {
2056         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2057         uint32_t port_id = req->id;
2058
2059         rsp->status = rte_pipeline_port_in_disable(p->p,
2060                 port_id);
2061
2062         return rsp;
2063 }
2064
2065 static struct pipeline_msg_rsp *
2066 pipeline_msg_handle_port_out_stats_read(struct pipeline_data *p,
2067         struct pipeline_msg_req *req)
2068 {
2069         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2070         uint32_t port_id = req->id;
2071         int clear = req->port_out_stats_read.clear;
2072
2073         rsp->status = rte_pipeline_port_out_stats_read(p->p,
2074                 port_id,
2075                 &rsp->port_out_stats_read.stats,
2076                 clear);
2077
2078         return rsp;
2079 }
2080
2081 static struct pipeline_msg_rsp *
2082 pipeline_msg_handle_table_stats_read(struct pipeline_data *p,
2083         struct pipeline_msg_req *req)
2084 {
2085         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2086         uint32_t port_id = req->id;
2087         int clear = req->table_stats_read.clear;
2088
2089         rsp->status = rte_pipeline_table_stats_read(p->p,
2090                 port_id,
2091                 &rsp->table_stats_read.stats,
2092                 clear);
2093
2094         return rsp;
2095 }
2096
2097 static int
2098 match_convert_ipv6_depth(uint32_t depth, uint32_t *depth32)
2099 {
2100         if (depth > 128)
2101                 return -1;
2102
2103         switch (depth / 32) {
2104         case 0:
2105                 depth32[0] = depth;
2106                 depth32[1] = 0;
2107                 depth32[2] = 0;
2108                 depth32[3] = 0;
2109                 return 0;
2110
2111         case 1:
2112                 depth32[0] = 32;
2113                 depth32[1] = depth - 32;
2114                 depth32[2] = 0;
2115                 depth32[3] = 0;
2116                 return 0;
2117
2118         case 2:
2119                 depth32[0] = 32;
2120                 depth32[1] = 32;
2121                 depth32[2] = depth - 64;
2122                 depth32[3] = 0;
2123                 return 0;
2124
2125         case 3:
2126                 depth32[0] = 32;
2127                 depth32[1] = 32;
2128                 depth32[2] = 32;
2129                 depth32[3] = depth - 96;
2130                 return 0;
2131
2132         case 4:
2133                 depth32[0] = 32;
2134                 depth32[1] = 32;
2135                 depth32[2] = 32;
2136                 depth32[3] = 32;
2137                 return 0;
2138
2139         default:
2140                 return -1;
2141         }
2142 }
2143
2144 static int
2145 match_convert(struct softnic_table_rule_match *mh,
2146         union table_rule_match_low_level *ml,
2147         int add)
2148 {
2149         memset(ml, 0, sizeof(*ml));
2150
2151         switch (mh->match_type) {
2152         case TABLE_ACL:
2153                 if (mh->match.acl.ip_version)
2154                         if (add) {
2155                                 ml->acl_add.field_value[0].value.u8 =
2156                                         mh->match.acl.proto;
2157                                 ml->acl_add.field_value[0].mask_range.u8 =
2158                                         mh->match.acl.proto_mask;
2159
2160                                 ml->acl_add.field_value[1].value.u32 =
2161                                         mh->match.acl.ipv4.sa;
2162                                 ml->acl_add.field_value[1].mask_range.u32 =
2163                                         mh->match.acl.sa_depth;
2164
2165                                 ml->acl_add.field_value[2].value.u32 =
2166                                         mh->match.acl.ipv4.da;
2167                                 ml->acl_add.field_value[2].mask_range.u32 =
2168                                         mh->match.acl.da_depth;
2169
2170                                 ml->acl_add.field_value[3].value.u16 =
2171                                         mh->match.acl.sp0;
2172                                 ml->acl_add.field_value[3].mask_range.u16 =
2173                                         mh->match.acl.sp1;
2174
2175                                 ml->acl_add.field_value[4].value.u16 =
2176                                         mh->match.acl.dp0;
2177                                 ml->acl_add.field_value[4].mask_range.u16 =
2178                                         mh->match.acl.dp1;
2179
2180                                 ml->acl_add.priority =
2181                                         (int32_t)mh->match.acl.priority;
2182                         } else {
2183                                 ml->acl_delete.field_value[0].value.u8 =
2184                                         mh->match.acl.proto;
2185                                 ml->acl_delete.field_value[0].mask_range.u8 =
2186                                         mh->match.acl.proto_mask;
2187
2188                                 ml->acl_delete.field_value[1].value.u32 =
2189                                         mh->match.acl.ipv4.sa;
2190                                 ml->acl_delete.field_value[1].mask_range.u32 =
2191                                         mh->match.acl.sa_depth;
2192
2193                                 ml->acl_delete.field_value[2].value.u32 =
2194                                         mh->match.acl.ipv4.da;
2195                                 ml->acl_delete.field_value[2].mask_range.u32 =
2196                                         mh->match.acl.da_depth;
2197
2198                                 ml->acl_delete.field_value[3].value.u16 =
2199                                         mh->match.acl.sp0;
2200                                 ml->acl_delete.field_value[3].mask_range.u16 =
2201                                         mh->match.acl.sp1;
2202
2203                                 ml->acl_delete.field_value[4].value.u16 =
2204                                         mh->match.acl.dp0;
2205                                 ml->acl_delete.field_value[4].mask_range.u16 =
2206                                         mh->match.acl.dp1;
2207                         }
2208                 else
2209                         if (add) {
2210                                 uint32_t *sa32 =
2211                                         (uint32_t *)mh->match.acl.ipv6.sa;
2212                                 uint32_t *da32 =
2213                                         (uint32_t *)mh->match.acl.ipv6.da;
2214                                 uint32_t sa32_depth[4], da32_depth[4];
2215                                 int status;
2216
2217                                 status = match_convert_ipv6_depth(mh->match.acl.sa_depth,
2218                                         sa32_depth);
2219                                 if (status)
2220                                         return status;
2221
2222                                 status = match_convert_ipv6_depth(
2223                                         mh->match.acl.da_depth,
2224                                         da32_depth);
2225                                 if (status)
2226                                         return status;
2227
2228                                 ml->acl_add.field_value[0].value.u8 =
2229                                         mh->match.acl.proto;
2230                                 ml->acl_add.field_value[0].mask_range.u8 =
2231                                         mh->match.acl.proto_mask;
2232
2233                                 ml->acl_add.field_value[1].value.u32 = sa32[0];
2234                                 ml->acl_add.field_value[1].mask_range.u32 =
2235                                         sa32_depth[0];
2236                                 ml->acl_add.field_value[2].value.u32 = sa32[1];
2237                                 ml->acl_add.field_value[2].mask_range.u32 =
2238                                         sa32_depth[1];
2239                                 ml->acl_add.field_value[3].value.u32 = sa32[2];
2240                                 ml->acl_add.field_value[3].mask_range.u32 =
2241                                         sa32_depth[2];
2242                                 ml->acl_add.field_value[4].value.u32 = sa32[3];
2243                                 ml->acl_add.field_value[4].mask_range.u32 =
2244                                         sa32_depth[3];
2245
2246                                 ml->acl_add.field_value[5].value.u32 = da32[0];
2247                                 ml->acl_add.field_value[5].mask_range.u32 =
2248                                         da32_depth[0];
2249                                 ml->acl_add.field_value[6].value.u32 = da32[1];
2250                                 ml->acl_add.field_value[6].mask_range.u32 =
2251                                         da32_depth[1];
2252                                 ml->acl_add.field_value[7].value.u32 = da32[2];
2253                                 ml->acl_add.field_value[7].mask_range.u32 =
2254                                         da32_depth[2];
2255                                 ml->acl_add.field_value[8].value.u32 = da32[3];
2256                                 ml->acl_add.field_value[8].mask_range.u32 =
2257                                         da32_depth[3];
2258
2259                                 ml->acl_add.field_value[9].value.u16 =
2260                                         mh->match.acl.sp0;
2261                                 ml->acl_add.field_value[9].mask_range.u16 =
2262                                         mh->match.acl.sp1;
2263
2264                                 ml->acl_add.field_value[10].value.u16 =
2265                                         mh->match.acl.dp0;
2266                                 ml->acl_add.field_value[10].mask_range.u16 =
2267                                         mh->match.acl.dp1;
2268
2269                                 ml->acl_add.priority =
2270                                         (int32_t)mh->match.acl.priority;
2271                         } else {
2272                                 uint32_t *sa32 =
2273                                         (uint32_t *)mh->match.acl.ipv6.sa;
2274                                 uint32_t *da32 =
2275                                         (uint32_t *)mh->match.acl.ipv6.da;
2276                                 uint32_t sa32_depth[4], da32_depth[4];
2277                                 int status;
2278
2279                                 status = match_convert_ipv6_depth(mh->match.acl.sa_depth,
2280                                         sa32_depth);
2281                                 if (status)
2282                                         return status;
2283
2284                                 status = match_convert_ipv6_depth(mh->match.acl.da_depth,
2285                                         da32_depth);
2286                                 if (status)
2287                                         return status;
2288
2289                                 ml->acl_delete.field_value[0].value.u8 =
2290                                         mh->match.acl.proto;
2291                                 ml->acl_delete.field_value[0].mask_range.u8 =
2292                                         mh->match.acl.proto_mask;
2293
2294                                 ml->acl_delete.field_value[1].value.u32 =
2295                                         sa32[0];
2296                                 ml->acl_delete.field_value[1].mask_range.u32 =
2297                                         sa32_depth[0];
2298                                 ml->acl_delete.field_value[2].value.u32 =
2299                                         sa32[1];
2300                                 ml->acl_delete.field_value[2].mask_range.u32 =
2301                                         sa32_depth[1];
2302                                 ml->acl_delete.field_value[3].value.u32 =
2303                                         sa32[2];
2304                                 ml->acl_delete.field_value[3].mask_range.u32 =
2305                                         sa32_depth[2];
2306                                 ml->acl_delete.field_value[4].value.u32 =
2307                                         sa32[3];
2308                                 ml->acl_delete.field_value[4].mask_range.u32 =
2309                                         sa32_depth[3];
2310
2311                                 ml->acl_delete.field_value[5].value.u32 =
2312                                         da32[0];
2313                                 ml->acl_delete.field_value[5].mask_range.u32 =
2314                                         da32_depth[0];
2315                                 ml->acl_delete.field_value[6].value.u32 =
2316                                         da32[1];
2317                                 ml->acl_delete.field_value[6].mask_range.u32 =
2318                                         da32_depth[1];
2319                                 ml->acl_delete.field_value[7].value.u32 =
2320                                         da32[2];
2321                                 ml->acl_delete.field_value[7].mask_range.u32 =
2322                                         da32_depth[2];
2323                                 ml->acl_delete.field_value[8].value.u32 =
2324                                         da32[3];
2325                                 ml->acl_delete.field_value[8].mask_range.u32 =
2326                                         da32_depth[3];
2327
2328                                 ml->acl_delete.field_value[9].value.u16 =
2329                                         mh->match.acl.sp0;
2330                                 ml->acl_delete.field_value[9].mask_range.u16 =
2331                                         mh->match.acl.sp1;
2332
2333                                 ml->acl_delete.field_value[10].value.u16 =
2334                                         mh->match.acl.dp0;
2335                                 ml->acl_delete.field_value[10].mask_range.u16 =
2336                                         mh->match.acl.dp1;
2337                         }
2338                 return 0;
2339
2340         case TABLE_ARRAY:
2341                 ml->array.pos = mh->match.array.pos;
2342                 return 0;
2343
2344         case TABLE_HASH:
2345                 memcpy(ml->hash, mh->match.hash.key, sizeof(ml->hash));
2346                 return 0;
2347
2348         case TABLE_LPM:
2349                 if (mh->match.lpm.ip_version) {
2350                         ml->lpm_ipv4.ip = mh->match.lpm.ipv4;
2351                         ml->lpm_ipv4.depth = mh->match.lpm.depth;
2352                 } else {
2353                         memcpy(ml->lpm_ipv6.ip,
2354                                 mh->match.lpm.ipv6, sizeof(ml->lpm_ipv6.ip));
2355                         ml->lpm_ipv6.depth = mh->match.lpm.depth;
2356                 }
2357
2358                 return 0;
2359
2360         default:
2361                 return -1;
2362         }
2363 }
2364
2365 static int
2366 action_convert(struct rte_table_action *a,
2367         struct softnic_table_rule_action *action,
2368         struct rte_pipeline_table_entry *data)
2369 {
2370         int status;
2371
2372         /* Apply actions */
2373         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_FWD)) {
2374                 status = rte_table_action_apply(a,
2375                         data,
2376                         RTE_TABLE_ACTION_FWD,
2377                         &action->fwd);
2378
2379                 if (status)
2380                         return status;
2381         }
2382
2383         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_LB)) {
2384                 status = rte_table_action_apply(a,
2385                         data,
2386                         RTE_TABLE_ACTION_LB,
2387                         &action->lb);
2388
2389                 if (status)
2390                         return status;
2391         }
2392
2393         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) {
2394                 status = rte_table_action_apply(a,
2395                         data,
2396                         RTE_TABLE_ACTION_MTR,
2397                         &action->mtr);
2398
2399                 if (status)
2400                         return status;
2401         }
2402
2403         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_TM)) {
2404                 status = rte_table_action_apply(a,
2405                         data,
2406                         RTE_TABLE_ACTION_TM,
2407                         &action->tm);
2408
2409                 if (status)
2410                         return status;
2411         }
2412
2413         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_ENCAP)) {
2414                 status = rte_table_action_apply(a,
2415                         data,
2416                         RTE_TABLE_ACTION_ENCAP,
2417                         &action->encap);
2418
2419                 if (status)
2420                         return status;
2421         }
2422
2423         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_NAT)) {
2424                 status = rte_table_action_apply(a,
2425                         data,
2426                         RTE_TABLE_ACTION_NAT,
2427                         &action->nat);
2428
2429                 if (status)
2430                         return status;
2431         }
2432
2433         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_TTL)) {
2434                 status = rte_table_action_apply(a,
2435                         data,
2436                         RTE_TABLE_ACTION_TTL,
2437                         &action->ttl);
2438
2439                 if (status)
2440                         return status;
2441         }
2442
2443         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_STATS)) {
2444                 status = rte_table_action_apply(a,
2445                         data,
2446                         RTE_TABLE_ACTION_STATS,
2447                         &action->stats);
2448
2449                 if (status)
2450                         return status;
2451         }
2452
2453         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_TIME)) {
2454                 status = rte_table_action_apply(a,
2455                         data,
2456                         RTE_TABLE_ACTION_TIME,
2457                         &action->time);
2458
2459                 if (status)
2460                         return status;
2461         }
2462
2463         return 0;
2464 }
2465
2466 static struct pipeline_msg_rsp *
2467 pipeline_msg_handle_table_rule_add(struct pipeline_data *p,
2468         struct pipeline_msg_req *req)
2469 {
2470         union table_rule_match_low_level match_ll;
2471         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2472         struct softnic_table_rule_match *match = &req->table_rule_add.match;
2473         struct softnic_table_rule_action *action = &req->table_rule_add.action;
2474         struct rte_pipeline_table_entry *data_in, *data_out;
2475         uint32_t table_id = req->id;
2476         int key_found, status;
2477         struct rte_table_action *a = p->table_data[table_id].a;
2478
2479         /* Apply actions */
2480         memset(p->buffer, 0, sizeof(p->buffer));
2481         data_in = (struct rte_pipeline_table_entry *)p->buffer;
2482
2483         status = match_convert(match, &match_ll, 1);
2484         if (status) {
2485                 rsp->status = -1;
2486                 return rsp;
2487         }
2488
2489         status = action_convert(a, action, data_in);
2490         if (status) {
2491                 rsp->status = -1;
2492                 return rsp;
2493         }
2494
2495         status = rte_pipeline_table_entry_add(p->p,
2496                 table_id,
2497                 &match_ll,
2498                 data_in,
2499                 &key_found,
2500                 &data_out);
2501         if (status) {
2502                 rsp->status = -1;
2503                 return rsp;
2504         }
2505
2506         /* Write response */
2507         rsp->status = 0;
2508         rsp->table_rule_add.data = data_out;
2509
2510         return rsp;
2511 }
2512
2513 static struct pipeline_msg_rsp *
2514 pipeline_msg_handle_table_rule_add_default(struct pipeline_data *p,
2515         struct pipeline_msg_req *req)
2516 {
2517         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2518         struct softnic_table_rule_action *action = &req->table_rule_add_default.action;
2519         struct rte_pipeline_table_entry *data_in, *data_out;
2520         uint32_t table_id = req->id;
2521         int status;
2522
2523         /* Apply actions */
2524         memset(p->buffer, 0, sizeof(p->buffer));
2525         data_in = (struct rte_pipeline_table_entry *)p->buffer;
2526
2527         data_in->action = action->fwd.action;
2528         if (action->fwd.action == RTE_PIPELINE_ACTION_PORT)
2529                 data_in->port_id = action->fwd.id;
2530         if (action->fwd.action == RTE_PIPELINE_ACTION_TABLE)
2531                 data_in->table_id = action->fwd.id;
2532
2533         /* Add default rule to table */
2534         status = rte_pipeline_table_default_entry_add(p->p,
2535                 table_id,
2536                 data_in,
2537                 &data_out);
2538         if (status) {
2539                 rsp->status = -1;
2540                 return rsp;
2541         }
2542
2543         /* Write response */
2544         rsp->status = 0;
2545         rsp->table_rule_add_default.data = data_out;
2546
2547         return rsp;
2548 }
2549
2550 static struct pipeline_msg_rsp *
2551 pipeline_msg_handle_table_rule_add_bulk(struct pipeline_data *p,
2552         struct pipeline_msg_req *req)
2553 {
2554         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2555
2556         uint32_t table_id = req->id;
2557         struct softnic_table_rule_match *match = req->table_rule_add_bulk.match;
2558         struct softnic_table_rule_action *action = req->table_rule_add_bulk.action;
2559         struct rte_pipeline_table_entry **data =
2560                 (struct rte_pipeline_table_entry **)req->table_rule_add_bulk.data;
2561         uint32_t n_rules = req->table_rule_add_bulk.n_rules;
2562         uint32_t bulk = req->table_rule_add_bulk.bulk;
2563
2564         struct rte_table_action *a = p->table_data[table_id].a;
2565         union table_rule_match_low_level *match_ll;
2566         uint8_t *action_ll;
2567         void **match_ll_ptr;
2568         struct rte_pipeline_table_entry **action_ll_ptr;
2569         int *found, status;
2570         uint32_t i;
2571
2572         /* Memory allocation */
2573         match_ll = calloc(n_rules, sizeof(union table_rule_match_low_level));
2574         action_ll = calloc(n_rules, TABLE_RULE_ACTION_SIZE_MAX);
2575         match_ll_ptr = calloc(n_rules, sizeof(void *));
2576         action_ll_ptr =
2577                 calloc(n_rules, sizeof(struct rte_pipeline_table_entry *));
2578         found = calloc(n_rules, sizeof(int));
2579
2580         if (match_ll == NULL ||
2581                 action_ll == NULL ||
2582                 match_ll_ptr == NULL ||
2583                 action_ll_ptr == NULL ||
2584                 found == NULL)
2585                 goto fail;
2586
2587         for (i = 0; i < n_rules; i++) {
2588                 match_ll_ptr[i] = (void *)&match_ll[i];
2589                 action_ll_ptr[i] =
2590                         (struct rte_pipeline_table_entry *)&action_ll[i * TABLE_RULE_ACTION_SIZE_MAX];
2591         }
2592
2593         /* Rule match conversion */
2594         for (i = 0; i < n_rules; i++) {
2595                 status = match_convert(&match[i], match_ll_ptr[i], 1);
2596                 if (status)
2597                         goto fail;
2598         }
2599
2600         /* Rule action conversion */
2601         for (i = 0; i < n_rules; i++) {
2602                 status = action_convert(a, &action[i], action_ll_ptr[i]);
2603                 if (status)
2604                         goto fail;
2605         }
2606
2607         /* Add rule (match, action) to table */
2608         if (bulk) {
2609                 status = rte_pipeline_table_entry_add_bulk(p->p,
2610                         table_id,
2611                         match_ll_ptr,
2612                         action_ll_ptr,
2613                         n_rules,
2614                         found,
2615                         data);
2616                 if (status)
2617                         n_rules = 0;
2618         } else {
2619                 for (i = 0; i < n_rules; i++) {
2620                         status = rte_pipeline_table_entry_add(p->p,
2621                                 table_id,
2622                                 match_ll_ptr[i],
2623                                 action_ll_ptr[i],
2624                                 &found[i],
2625                                 &data[i]);
2626                         if (status) {
2627                                 n_rules = i;
2628                                 break;
2629                         }
2630                 }
2631         }
2632
2633         /* Write response */
2634         rsp->status = 0;
2635         rsp->table_rule_add_bulk.n_rules = n_rules;
2636
2637         /* Free */
2638         free(found);
2639         free(action_ll_ptr);
2640         free(match_ll_ptr);
2641         free(action_ll);
2642         free(match_ll);
2643
2644         return rsp;
2645
2646 fail:
2647         free(found);
2648         free(action_ll_ptr);
2649         free(match_ll_ptr);
2650         free(action_ll);
2651         free(match_ll);
2652
2653         rsp->status = -1;
2654         rsp->table_rule_add_bulk.n_rules = 0;
2655         return rsp;
2656 }
2657
2658 static struct pipeline_msg_rsp *
2659 pipeline_msg_handle_table_rule_delete(struct pipeline_data *p,
2660         struct pipeline_msg_req *req)
2661 {
2662         union table_rule_match_low_level match_ll;
2663         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2664         struct softnic_table_rule_match *match = &req->table_rule_delete.match;
2665         uint32_t table_id = req->id;
2666         int key_found, status;
2667
2668         status = match_convert(match, &match_ll, 0);
2669         if (status) {
2670                 rsp->status = -1;
2671                 return rsp;
2672         }
2673
2674         rsp->status = rte_pipeline_table_entry_delete(p->p,
2675                 table_id,
2676                 &match_ll,
2677                 &key_found,
2678                 NULL);
2679
2680         return rsp;
2681 }
2682
2683 static struct pipeline_msg_rsp *
2684 pipeline_msg_handle_table_rule_delete_default(struct pipeline_data *p,
2685         struct pipeline_msg_req *req)
2686 {
2687         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2688         uint32_t table_id = req->id;
2689
2690         rsp->status = rte_pipeline_table_default_entry_delete(p->p,
2691                 table_id,
2692                 NULL);
2693
2694         return rsp;
2695 }
2696
2697 static struct pipeline_msg_rsp *
2698 pipeline_msg_handle_table_rule_stats_read(struct pipeline_data *p,
2699         struct pipeline_msg_req *req)
2700 {
2701         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2702         uint32_t table_id = req->id;
2703         void *data = req->table_rule_stats_read.data;
2704         int clear = req->table_rule_stats_read.clear;
2705         struct rte_table_action *a = p->table_data[table_id].a;
2706
2707         rsp->status = rte_table_action_stats_read(a,
2708                 data,
2709                 &rsp->table_rule_stats_read.stats,
2710                 clear);
2711
2712         return rsp;
2713 }
2714
2715 static struct pipeline_msg_rsp *
2716 pipeline_msg_handle_table_mtr_profile_add(struct pipeline_data *p,
2717         struct pipeline_msg_req *req)
2718 {
2719         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2720         uint32_t table_id = req->id;
2721         uint32_t meter_profile_id = req->table_mtr_profile_add.meter_profile_id;
2722         struct rte_table_action_meter_profile *profile =
2723                 &req->table_mtr_profile_add.profile;
2724         struct rte_table_action *a = p->table_data[table_id].a;
2725
2726         rsp->status = rte_table_action_meter_profile_add(a,
2727                 meter_profile_id,
2728                 profile);
2729
2730         return rsp;
2731 }
2732
2733 static struct pipeline_msg_rsp *
2734 pipeline_msg_handle_table_mtr_profile_delete(struct pipeline_data *p,
2735         struct pipeline_msg_req *req)
2736 {
2737         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2738         uint32_t table_id = req->id;
2739         uint32_t meter_profile_id =
2740                 req->table_mtr_profile_delete.meter_profile_id;
2741         struct rte_table_action *a = p->table_data[table_id].a;
2742
2743         rsp->status = rte_table_action_meter_profile_delete(a,
2744                 meter_profile_id);
2745
2746         return rsp;
2747 }
2748
2749 static struct pipeline_msg_rsp *
2750 pipeline_msg_handle_table_rule_mtr_read(struct pipeline_data *p,
2751         struct pipeline_msg_req *req)
2752 {
2753         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2754         uint32_t table_id = req->id;
2755         void *data = req->table_rule_mtr_read.data;
2756         uint32_t tc_mask = req->table_rule_mtr_read.tc_mask;
2757         int clear = req->table_rule_mtr_read.clear;
2758         struct rte_table_action *a = p->table_data[table_id].a;
2759
2760         rsp->status = rte_table_action_meter_read(a,
2761                 data,
2762                 tc_mask,
2763                 &rsp->table_rule_mtr_read.stats,
2764                 clear);
2765
2766         return rsp;
2767 }
2768
2769 static struct pipeline_msg_rsp *
2770 pipeline_msg_handle_table_dscp_table_update(struct pipeline_data *p,
2771         struct pipeline_msg_req *req)
2772 {
2773         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2774         uint32_t table_id = req->id;
2775         uint64_t dscp_mask = req->table_dscp_table_update.dscp_mask;
2776         struct rte_table_action_dscp_table *dscp_table =
2777                 &req->table_dscp_table_update.dscp_table;
2778         struct rte_table_action *a = p->table_data[table_id].a;
2779
2780         rsp->status = rte_table_action_dscp_table_update(a,
2781                 dscp_mask,
2782                 dscp_table);
2783
2784         return rsp;
2785 }
2786
2787 static struct pipeline_msg_rsp *
2788 pipeline_msg_handle_table_rule_ttl_read(struct pipeline_data *p,
2789         struct pipeline_msg_req *req)
2790 {
2791         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2792         uint32_t table_id = req->id;
2793         void *data = req->table_rule_ttl_read.data;
2794         int clear = req->table_rule_ttl_read.clear;
2795         struct rte_table_action *a = p->table_data[table_id].a;
2796
2797         rsp->status = rte_table_action_ttl_read(a,
2798                 data,
2799                 &rsp->table_rule_ttl_read.stats,
2800                 clear);
2801
2802         return rsp;
2803 }
2804
2805 static void
2806 pipeline_msg_handle(struct pipeline_data *p)
2807 {
2808         for ( ; ; ) {
2809                 struct pipeline_msg_req *req;
2810                 struct pipeline_msg_rsp *rsp;
2811
2812                 req = pipeline_msg_recv(p->msgq_req);
2813                 if (req == NULL)
2814                         break;
2815
2816                 switch (req->type) {
2817                 case PIPELINE_REQ_PORT_IN_STATS_READ:
2818                         rsp = pipeline_msg_handle_port_in_stats_read(p, req);
2819                         break;
2820
2821                 case PIPELINE_REQ_PORT_IN_ENABLE:
2822                         rsp = pipeline_msg_handle_port_in_enable(p, req);
2823                         break;
2824
2825                 case PIPELINE_REQ_PORT_IN_DISABLE:
2826                         rsp = pipeline_msg_handle_port_in_disable(p, req);
2827                         break;
2828
2829                 case PIPELINE_REQ_PORT_OUT_STATS_READ:
2830                         rsp = pipeline_msg_handle_port_out_stats_read(p, req);
2831                         break;
2832
2833                 case PIPELINE_REQ_TABLE_STATS_READ:
2834                         rsp = pipeline_msg_handle_table_stats_read(p, req);
2835                         break;
2836
2837                 case PIPELINE_REQ_TABLE_RULE_ADD:
2838                         rsp = pipeline_msg_handle_table_rule_add(p, req);
2839                         break;
2840
2841                 case PIPELINE_REQ_TABLE_RULE_ADD_DEFAULT:
2842                         rsp = pipeline_msg_handle_table_rule_add_default(p,     req);
2843                         break;
2844
2845                 case PIPELINE_REQ_TABLE_RULE_ADD_BULK:
2846                         rsp = pipeline_msg_handle_table_rule_add_bulk(p, req);
2847                         break;
2848
2849                 case PIPELINE_REQ_TABLE_RULE_DELETE:
2850                         rsp = pipeline_msg_handle_table_rule_delete(p, req);
2851                         break;
2852
2853                 case PIPELINE_REQ_TABLE_RULE_DELETE_DEFAULT:
2854                         rsp = pipeline_msg_handle_table_rule_delete_default(p, req);
2855                         break;
2856
2857                 case PIPELINE_REQ_TABLE_RULE_STATS_READ:
2858                         rsp = pipeline_msg_handle_table_rule_stats_read(p, req);
2859                         break;
2860
2861                 case PIPELINE_REQ_TABLE_MTR_PROFILE_ADD:
2862                         rsp = pipeline_msg_handle_table_mtr_profile_add(p, req);
2863                         break;
2864
2865                 case PIPELINE_REQ_TABLE_MTR_PROFILE_DELETE:
2866                         rsp = pipeline_msg_handle_table_mtr_profile_delete(p, req);
2867                         break;
2868
2869                 case PIPELINE_REQ_TABLE_RULE_MTR_READ:
2870                         rsp = pipeline_msg_handle_table_rule_mtr_read(p, req);
2871                         break;
2872
2873                 case PIPELINE_REQ_TABLE_DSCP_TABLE_UPDATE:
2874                         rsp = pipeline_msg_handle_table_dscp_table_update(p, req);
2875                         break;
2876
2877                 case PIPELINE_REQ_TABLE_RULE_TTL_READ:
2878                         rsp = pipeline_msg_handle_table_rule_ttl_read(p, req);
2879                         break;
2880
2881                 default:
2882                         rsp = (struct pipeline_msg_rsp *)req;
2883                         rsp->status = -1;
2884                 }
2885
2886                 pipeline_msg_send(p->msgq_rsp, rsp);
2887         }
2888 }
2889
2890 /**
2891  * Data plane threads: main
2892  */
2893 int
2894 rte_pmd_softnic_run(uint16_t port_id)
2895 {
2896         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2897         struct pmd_internals *softnic;
2898         struct softnic_thread_data *t;
2899         uint32_t thread_id, j;
2900
2901 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
2902         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, 0);
2903 #endif
2904
2905         softnic = dev->data->dev_private;
2906         thread_id = rte_lcore_id();
2907         t = &softnic->thread_data[thread_id];
2908         t->iter++;
2909
2910         /* Data Plane */
2911         for (j = 0; j < t->n_pipelines; j++)
2912                 rte_pipeline_run(t->p[j]);
2913
2914         /* Control Plane */
2915         if ((t->iter & 0xFLLU) == 0) {
2916                 uint64_t time = rte_get_tsc_cycles();
2917                 uint64_t time_next_min = UINT64_MAX;
2918
2919                 if (time < t->time_next_min)
2920                         return 0;
2921
2922                 /* Pipeline message queues */
2923                 for (j = 0; j < t->n_pipelines; j++) {
2924                         struct pipeline_data *p =
2925                                 &t->pipeline_data[j];
2926                         uint64_t time_next = p->time_next;
2927
2928                         if (time_next <= time) {
2929                                 pipeline_msg_handle(p);
2930                                 rte_pipeline_flush(p->p);
2931                                 time_next = time + p->timer_period;
2932                                 p->time_next = time_next;
2933                         }
2934
2935                         if (time_next < time_next_min)
2936                                 time_next_min = time_next;
2937                 }
2938
2939                 /* Thread message queues */
2940                 {
2941                         uint64_t time_next = t->time_next;
2942
2943                         if (time_next <= time) {
2944                                 thread_msg_handle(t);
2945                                 time_next = time + t->timer_period;
2946                                 t->time_next = time_next;
2947                         }
2948
2949                         if (time_next < time_next_min)
2950                                 time_next_min = time_next;
2951                 }
2952
2953                 t->time_next_min = time_next_min;
2954         }
2955
2956         return 0;
2957 }