New upstream version 18.08
[deb_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         int status;
1684
1685         /* Check input params */
1686         if (pipeline_name == NULL ||
1687                 profile == NULL)
1688                 return -1;
1689
1690         p = softnic_pipeline_find(softnic, pipeline_name);
1691         if (p == NULL ||
1692                 table_id >= p->n_tables)
1693                 return -1;
1694
1695         if (!pipeline_is_running(p)) {
1696                 struct rte_table_action *a = p->table[table_id].a;
1697
1698                 status = rte_table_action_meter_profile_add(a,
1699                         meter_profile_id,
1700                         profile);
1701
1702                 return status;
1703         }
1704
1705         /* Allocate request */
1706         req = pipeline_msg_alloc();
1707         if (req == NULL)
1708                 return -1;
1709
1710         /* Write request */
1711         req->type = PIPELINE_REQ_TABLE_MTR_PROFILE_ADD;
1712         req->id = table_id;
1713         req->table_mtr_profile_add.meter_profile_id = meter_profile_id;
1714         memcpy(&req->table_mtr_profile_add.profile, profile, sizeof(*profile));
1715
1716         /* Send request and wait for response */
1717         rsp = pipeline_msg_send_recv(p, req);
1718         if (rsp == NULL)
1719                 return -1;
1720
1721         /* Read response */
1722         status = rsp->status;
1723
1724         /* Free response */
1725         pipeline_msg_free(rsp);
1726
1727         return status;
1728 }
1729
1730 int
1731 softnic_pipeline_table_mtr_profile_delete(struct pmd_internals *softnic,
1732         const char *pipeline_name,
1733         uint32_t table_id,
1734         uint32_t meter_profile_id)
1735 {
1736         struct pipeline *p;
1737         struct pipeline_msg_req *req;
1738         struct pipeline_msg_rsp *rsp;
1739         int status;
1740
1741         /* Check input params */
1742         if (pipeline_name == NULL)
1743                 return -1;
1744
1745         p = softnic_pipeline_find(softnic, pipeline_name);
1746         if (p == NULL ||
1747                 table_id >= p->n_tables)
1748                 return -1;
1749
1750         if (!pipeline_is_running(p)) {
1751                 struct rte_table_action *a = p->table[table_id].a;
1752
1753                 status = rte_table_action_meter_profile_delete(a,
1754                                 meter_profile_id);
1755
1756                 return status;
1757         }
1758
1759         /* Allocate request */
1760         req = pipeline_msg_alloc();
1761         if (req == NULL)
1762                 return -1;
1763
1764         /* Write request */
1765         req->type = PIPELINE_REQ_TABLE_MTR_PROFILE_DELETE;
1766         req->id = table_id;
1767         req->table_mtr_profile_delete.meter_profile_id = meter_profile_id;
1768
1769         /* Send request and wait for response */
1770         rsp = pipeline_msg_send_recv(p, req);
1771         if (rsp == NULL)
1772                 return -1;
1773
1774         /* Read response */
1775         status = rsp->status;
1776
1777         /* Free response */
1778         pipeline_msg_free(rsp);
1779
1780         return status;
1781 }
1782
1783 int
1784 softnic_pipeline_table_rule_mtr_read(struct pmd_internals *softnic,
1785         const char *pipeline_name,
1786         uint32_t table_id,
1787         void *data,
1788         uint32_t tc_mask,
1789         struct rte_table_action_mtr_counters *stats,
1790         int clear)
1791 {
1792         struct pipeline *p;
1793         struct pipeline_msg_req *req;
1794         struct pipeline_msg_rsp *rsp;
1795         int status;
1796
1797         /* Check input params */
1798         if (pipeline_name == NULL ||
1799                 data == NULL ||
1800                 stats == NULL)
1801                 return -1;
1802
1803         p = softnic_pipeline_find(softnic, pipeline_name);
1804         if (p == NULL ||
1805                 table_id >= p->n_tables)
1806                 return -1;
1807
1808         if (!pipeline_is_running(p)) {
1809                 struct rte_table_action *a = p->table[table_id].a;
1810
1811                 status = rte_table_action_meter_read(a,
1812                                 data,
1813                                 tc_mask,
1814                                 stats,
1815                                 clear);
1816
1817                 return status;
1818         }
1819
1820         /* Allocate request */
1821         req = pipeline_msg_alloc();
1822         if (req == NULL)
1823                 return -1;
1824
1825         /* Write request */
1826         req->type = PIPELINE_REQ_TABLE_RULE_MTR_READ;
1827         req->id = table_id;
1828         req->table_rule_mtr_read.data = data;
1829         req->table_rule_mtr_read.tc_mask = tc_mask;
1830         req->table_rule_mtr_read.clear = clear;
1831
1832         /* Send request and wait for response */
1833         rsp = pipeline_msg_send_recv(p, req);
1834         if (rsp == NULL)
1835                 return -1;
1836
1837         /* Read response */
1838         status = rsp->status;
1839         if (status)
1840                 memcpy(stats, &rsp->table_rule_mtr_read.stats, sizeof(*stats));
1841
1842         /* Free response */
1843         pipeline_msg_free(rsp);
1844
1845         return status;
1846 }
1847
1848 int
1849 softnic_pipeline_table_dscp_table_update(struct pmd_internals *softnic,
1850         const char *pipeline_name,
1851         uint32_t table_id,
1852         uint64_t dscp_mask,
1853         struct rte_table_action_dscp_table *dscp_table)
1854 {
1855         struct pipeline *p;
1856         struct pipeline_msg_req *req;
1857         struct pipeline_msg_rsp *rsp;
1858         int status;
1859
1860         /* Check input params */
1861         if (pipeline_name == NULL ||
1862                 dscp_table == NULL)
1863                 return -1;
1864
1865         p = softnic_pipeline_find(softnic, pipeline_name);
1866         if (p == NULL ||
1867                 table_id >= p->n_tables)
1868                 return -1;
1869
1870         if (!pipeline_is_running(p)) {
1871                 struct rte_table_action *a = p->table[table_id].a;
1872
1873                 status = rte_table_action_dscp_table_update(a,
1874                                 dscp_mask,
1875                                 dscp_table);
1876
1877                 return status;
1878         }
1879
1880         /* Allocate request */
1881         req = pipeline_msg_alloc();
1882         if (req == NULL)
1883                 return -1;
1884
1885         /* Write request */
1886         req->type = PIPELINE_REQ_TABLE_DSCP_TABLE_UPDATE;
1887         req->id = table_id;
1888         req->table_dscp_table_update.dscp_mask = dscp_mask;
1889         memcpy(&req->table_dscp_table_update.dscp_table,
1890                 dscp_table, sizeof(*dscp_table));
1891
1892         /* Send request and wait for response */
1893         rsp = pipeline_msg_send_recv(p, req);
1894         if (rsp == NULL)
1895                 return -1;
1896
1897         /* Read response */
1898         status = rsp->status;
1899
1900         /* Free response */
1901         pipeline_msg_free(rsp);
1902
1903         return status;
1904 }
1905
1906 int
1907 softnic_pipeline_table_rule_ttl_read(struct pmd_internals *softnic,
1908         const char *pipeline_name,
1909         uint32_t table_id,
1910         void *data,
1911         struct rte_table_action_ttl_counters *stats,
1912         int clear)
1913 {
1914         struct pipeline *p;
1915         struct pipeline_msg_req *req;
1916         struct pipeline_msg_rsp *rsp;
1917         int status;
1918
1919         /* Check input params */
1920         if (pipeline_name == NULL ||
1921                 data == NULL ||
1922                 stats == NULL)
1923                 return -1;
1924
1925         p = softnic_pipeline_find(softnic, pipeline_name);
1926         if (p == NULL ||
1927                 table_id >= p->n_tables)
1928                 return -1;
1929
1930         if (!pipeline_is_running(p)) {
1931                 struct rte_table_action *a = p->table[table_id].a;
1932
1933                 status = rte_table_action_ttl_read(a,
1934                                 data,
1935                                 stats,
1936                                 clear);
1937
1938                 return status;
1939         }
1940
1941         /* Allocate request */
1942         req = pipeline_msg_alloc();
1943         if (req == NULL)
1944                 return -1;
1945
1946         /* Write request */
1947         req->type = PIPELINE_REQ_TABLE_RULE_TTL_READ;
1948         req->id = table_id;
1949         req->table_rule_ttl_read.data = data;
1950         req->table_rule_ttl_read.clear = clear;
1951
1952         /* Send request and wait for response */
1953         rsp = pipeline_msg_send_recv(p, req);
1954         if (rsp == NULL)
1955                 return -1;
1956
1957         /* Read response */
1958         status = rsp->status;
1959         if (status)
1960                 memcpy(stats, &rsp->table_rule_ttl_read.stats, sizeof(*stats));
1961
1962         /* Free response */
1963         pipeline_msg_free(rsp);
1964
1965         return status;
1966 }
1967
1968 /**
1969  * Data plane threads: message handling
1970  */
1971 static inline struct pipeline_msg_req *
1972 pipeline_msg_recv(struct rte_ring *msgq_req)
1973 {
1974         struct pipeline_msg_req *req;
1975
1976         int status = rte_ring_sc_dequeue(msgq_req, (void **)&req);
1977
1978         if (status != 0)
1979                 return NULL;
1980
1981         return req;
1982 }
1983
1984 static inline void
1985 pipeline_msg_send(struct rte_ring *msgq_rsp,
1986         struct pipeline_msg_rsp *rsp)
1987 {
1988         int status;
1989
1990         do {
1991                 status = rte_ring_sp_enqueue(msgq_rsp, rsp);
1992         } while (status == -ENOBUFS);
1993 }
1994
1995 static struct pipeline_msg_rsp *
1996 pipeline_msg_handle_port_in_stats_read(struct pipeline_data *p,
1997         struct pipeline_msg_req *req)
1998 {
1999         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2000         uint32_t port_id = req->id;
2001         int clear = req->port_in_stats_read.clear;
2002
2003         rsp->status = rte_pipeline_port_in_stats_read(p->p,
2004                 port_id,
2005                 &rsp->port_in_stats_read.stats,
2006                 clear);
2007
2008         return rsp;
2009 }
2010
2011 static struct pipeline_msg_rsp *
2012 pipeline_msg_handle_port_in_enable(struct pipeline_data *p,
2013         struct pipeline_msg_req *req)
2014 {
2015         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2016         uint32_t port_id = req->id;
2017
2018         rsp->status = rte_pipeline_port_in_enable(p->p,
2019                 port_id);
2020
2021         return rsp;
2022 }
2023
2024 static struct pipeline_msg_rsp *
2025 pipeline_msg_handle_port_in_disable(struct pipeline_data *p,
2026         struct pipeline_msg_req *req)
2027 {
2028         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2029         uint32_t port_id = req->id;
2030
2031         rsp->status = rte_pipeline_port_in_disable(p->p,
2032                 port_id);
2033
2034         return rsp;
2035 }
2036
2037 static struct pipeline_msg_rsp *
2038 pipeline_msg_handle_port_out_stats_read(struct pipeline_data *p,
2039         struct pipeline_msg_req *req)
2040 {
2041         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2042         uint32_t port_id = req->id;
2043         int clear = req->port_out_stats_read.clear;
2044
2045         rsp->status = rte_pipeline_port_out_stats_read(p->p,
2046                 port_id,
2047                 &rsp->port_out_stats_read.stats,
2048                 clear);
2049
2050         return rsp;
2051 }
2052
2053 static struct pipeline_msg_rsp *
2054 pipeline_msg_handle_table_stats_read(struct pipeline_data *p,
2055         struct pipeline_msg_req *req)
2056 {
2057         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2058         uint32_t port_id = req->id;
2059         int clear = req->table_stats_read.clear;
2060
2061         rsp->status = rte_pipeline_table_stats_read(p->p,
2062                 port_id,
2063                 &rsp->table_stats_read.stats,
2064                 clear);
2065
2066         return rsp;
2067 }
2068
2069 static int
2070 match_convert_ipv6_depth(uint32_t depth, uint32_t *depth32)
2071 {
2072         if (depth > 128)
2073                 return -1;
2074
2075         switch (depth / 32) {
2076         case 0:
2077                 depth32[0] = depth;
2078                 depth32[1] = 0;
2079                 depth32[2] = 0;
2080                 depth32[3] = 0;
2081                 return 0;
2082
2083         case 1:
2084                 depth32[0] = 32;
2085                 depth32[1] = depth - 32;
2086                 depth32[2] = 0;
2087                 depth32[3] = 0;
2088                 return 0;
2089
2090         case 2:
2091                 depth32[0] = 32;
2092                 depth32[1] = 32;
2093                 depth32[2] = depth - 64;
2094                 depth32[3] = 0;
2095                 return 0;
2096
2097         case 3:
2098                 depth32[0] = 32;
2099                 depth32[1] = 32;
2100                 depth32[2] = 32;
2101                 depth32[3] = depth - 96;
2102                 return 0;
2103
2104         case 4:
2105                 depth32[0] = 32;
2106                 depth32[1] = 32;
2107                 depth32[2] = 32;
2108                 depth32[3] = 32;
2109                 return 0;
2110
2111         default:
2112                 return -1;
2113         }
2114 }
2115
2116 static int
2117 match_convert(struct softnic_table_rule_match *mh,
2118         union table_rule_match_low_level *ml,
2119         int add)
2120 {
2121         memset(ml, 0, sizeof(*ml));
2122
2123         switch (mh->match_type) {
2124         case TABLE_ACL:
2125                 if (mh->match.acl.ip_version)
2126                         if (add) {
2127                                 ml->acl_add.field_value[0].value.u8 =
2128                                         mh->match.acl.proto;
2129                                 ml->acl_add.field_value[0].mask_range.u8 =
2130                                         mh->match.acl.proto_mask;
2131
2132                                 ml->acl_add.field_value[1].value.u32 =
2133                                         mh->match.acl.ipv4.sa;
2134                                 ml->acl_add.field_value[1].mask_range.u32 =
2135                                         mh->match.acl.sa_depth;
2136
2137                                 ml->acl_add.field_value[2].value.u32 =
2138                                         mh->match.acl.ipv4.da;
2139                                 ml->acl_add.field_value[2].mask_range.u32 =
2140                                         mh->match.acl.da_depth;
2141
2142                                 ml->acl_add.field_value[3].value.u16 =
2143                                         mh->match.acl.sp0;
2144                                 ml->acl_add.field_value[3].mask_range.u16 =
2145                                         mh->match.acl.sp1;
2146
2147                                 ml->acl_add.field_value[4].value.u16 =
2148                                         mh->match.acl.dp0;
2149                                 ml->acl_add.field_value[4].mask_range.u16 =
2150                                         mh->match.acl.dp1;
2151
2152                                 ml->acl_add.priority =
2153                                         (int32_t)mh->match.acl.priority;
2154                         } else {
2155                                 ml->acl_delete.field_value[0].value.u8 =
2156                                         mh->match.acl.proto;
2157                                 ml->acl_delete.field_value[0].mask_range.u8 =
2158                                         mh->match.acl.proto_mask;
2159
2160                                 ml->acl_delete.field_value[1].value.u32 =
2161                                         mh->match.acl.ipv4.sa;
2162                                 ml->acl_delete.field_value[1].mask_range.u32 =
2163                                         mh->match.acl.sa_depth;
2164
2165                                 ml->acl_delete.field_value[2].value.u32 =
2166                                         mh->match.acl.ipv4.da;
2167                                 ml->acl_delete.field_value[2].mask_range.u32 =
2168                                         mh->match.acl.da_depth;
2169
2170                                 ml->acl_delete.field_value[3].value.u16 =
2171                                         mh->match.acl.sp0;
2172                                 ml->acl_delete.field_value[3].mask_range.u16 =
2173                                         mh->match.acl.sp1;
2174
2175                                 ml->acl_delete.field_value[4].value.u16 =
2176                                         mh->match.acl.dp0;
2177                                 ml->acl_delete.field_value[4].mask_range.u16 =
2178                                         mh->match.acl.dp1;
2179                         }
2180                 else
2181                         if (add) {
2182                                 uint32_t *sa32 =
2183                                         (uint32_t *)mh->match.acl.ipv6.sa;
2184                                 uint32_t *da32 =
2185                                         (uint32_t *)mh->match.acl.ipv6.da;
2186                                 uint32_t sa32_depth[4], da32_depth[4];
2187                                 int status;
2188
2189                                 status = match_convert_ipv6_depth(mh->match.acl.sa_depth,
2190                                         sa32_depth);
2191                                 if (status)
2192                                         return status;
2193
2194                                 status = match_convert_ipv6_depth(
2195                                         mh->match.acl.da_depth,
2196                                         da32_depth);
2197                                 if (status)
2198                                         return status;
2199
2200                                 ml->acl_add.field_value[0].value.u8 =
2201                                         mh->match.acl.proto;
2202                                 ml->acl_add.field_value[0].mask_range.u8 =
2203                                         mh->match.acl.proto_mask;
2204
2205                                 ml->acl_add.field_value[1].value.u32 = sa32[0];
2206                                 ml->acl_add.field_value[1].mask_range.u32 =
2207                                         sa32_depth[0];
2208                                 ml->acl_add.field_value[2].value.u32 = sa32[1];
2209                                 ml->acl_add.field_value[2].mask_range.u32 =
2210                                         sa32_depth[1];
2211                                 ml->acl_add.field_value[3].value.u32 = sa32[2];
2212                                 ml->acl_add.field_value[3].mask_range.u32 =
2213                                         sa32_depth[2];
2214                                 ml->acl_add.field_value[4].value.u32 = sa32[3];
2215                                 ml->acl_add.field_value[4].mask_range.u32 =
2216                                         sa32_depth[3];
2217
2218                                 ml->acl_add.field_value[5].value.u32 = da32[0];
2219                                 ml->acl_add.field_value[5].mask_range.u32 =
2220                                         da32_depth[0];
2221                                 ml->acl_add.field_value[6].value.u32 = da32[1];
2222                                 ml->acl_add.field_value[6].mask_range.u32 =
2223                                         da32_depth[1];
2224                                 ml->acl_add.field_value[7].value.u32 = da32[2];
2225                                 ml->acl_add.field_value[7].mask_range.u32 =
2226                                         da32_depth[2];
2227                                 ml->acl_add.field_value[8].value.u32 = da32[3];
2228                                 ml->acl_add.field_value[8].mask_range.u32 =
2229                                         da32_depth[3];
2230
2231                                 ml->acl_add.field_value[9].value.u16 =
2232                                         mh->match.acl.sp0;
2233                                 ml->acl_add.field_value[9].mask_range.u16 =
2234                                         mh->match.acl.sp1;
2235
2236                                 ml->acl_add.field_value[10].value.u16 =
2237                                         mh->match.acl.dp0;
2238                                 ml->acl_add.field_value[10].mask_range.u16 =
2239                                         mh->match.acl.dp1;
2240
2241                                 ml->acl_add.priority =
2242                                         (int32_t)mh->match.acl.priority;
2243                         } else {
2244                                 uint32_t *sa32 =
2245                                         (uint32_t *)mh->match.acl.ipv6.sa;
2246                                 uint32_t *da32 =
2247                                         (uint32_t *)mh->match.acl.ipv6.da;
2248                                 uint32_t sa32_depth[4], da32_depth[4];
2249                                 int status;
2250
2251                                 status = match_convert_ipv6_depth(mh->match.acl.sa_depth,
2252                                         sa32_depth);
2253                                 if (status)
2254                                         return status;
2255
2256                                 status = match_convert_ipv6_depth(mh->match.acl.da_depth,
2257                                         da32_depth);
2258                                 if (status)
2259                                         return status;
2260
2261                                 ml->acl_delete.field_value[0].value.u8 =
2262                                         mh->match.acl.proto;
2263                                 ml->acl_delete.field_value[0].mask_range.u8 =
2264                                         mh->match.acl.proto_mask;
2265
2266                                 ml->acl_delete.field_value[1].value.u32 =
2267                                         sa32[0];
2268                                 ml->acl_delete.field_value[1].mask_range.u32 =
2269                                         sa32_depth[0];
2270                                 ml->acl_delete.field_value[2].value.u32 =
2271                                         sa32[1];
2272                                 ml->acl_delete.field_value[2].mask_range.u32 =
2273                                         sa32_depth[1];
2274                                 ml->acl_delete.field_value[3].value.u32 =
2275                                         sa32[2];
2276                                 ml->acl_delete.field_value[3].mask_range.u32 =
2277                                         sa32_depth[2];
2278                                 ml->acl_delete.field_value[4].value.u32 =
2279                                         sa32[3];
2280                                 ml->acl_delete.field_value[4].mask_range.u32 =
2281                                         sa32_depth[3];
2282
2283                                 ml->acl_delete.field_value[5].value.u32 =
2284                                         da32[0];
2285                                 ml->acl_delete.field_value[5].mask_range.u32 =
2286                                         da32_depth[0];
2287                                 ml->acl_delete.field_value[6].value.u32 =
2288                                         da32[1];
2289                                 ml->acl_delete.field_value[6].mask_range.u32 =
2290                                         da32_depth[1];
2291                                 ml->acl_delete.field_value[7].value.u32 =
2292                                         da32[2];
2293                                 ml->acl_delete.field_value[7].mask_range.u32 =
2294                                         da32_depth[2];
2295                                 ml->acl_delete.field_value[8].value.u32 =
2296                                         da32[3];
2297                                 ml->acl_delete.field_value[8].mask_range.u32 =
2298                                         da32_depth[3];
2299
2300                                 ml->acl_delete.field_value[9].value.u16 =
2301                                         mh->match.acl.sp0;
2302                                 ml->acl_delete.field_value[9].mask_range.u16 =
2303                                         mh->match.acl.sp1;
2304
2305                                 ml->acl_delete.field_value[10].value.u16 =
2306                                         mh->match.acl.dp0;
2307                                 ml->acl_delete.field_value[10].mask_range.u16 =
2308                                         mh->match.acl.dp1;
2309                         }
2310                 return 0;
2311
2312         case TABLE_ARRAY:
2313                 ml->array.pos = mh->match.array.pos;
2314                 return 0;
2315
2316         case TABLE_HASH:
2317                 memcpy(ml->hash, mh->match.hash.key, sizeof(ml->hash));
2318                 return 0;
2319
2320         case TABLE_LPM:
2321                 if (mh->match.lpm.ip_version) {
2322                         ml->lpm_ipv4.ip = mh->match.lpm.ipv4;
2323                         ml->lpm_ipv4.depth = mh->match.lpm.depth;
2324                 } else {
2325                         memcpy(ml->lpm_ipv6.ip,
2326                                 mh->match.lpm.ipv6, sizeof(ml->lpm_ipv6.ip));
2327                         ml->lpm_ipv6.depth = mh->match.lpm.depth;
2328                 }
2329
2330                 return 0;
2331
2332         default:
2333                 return -1;
2334         }
2335 }
2336
2337 static int
2338 action_convert(struct rte_table_action *a,
2339         struct softnic_table_rule_action *action,
2340         struct rte_pipeline_table_entry *data)
2341 {
2342         int status;
2343
2344         /* Apply actions */
2345         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_FWD)) {
2346                 status = rte_table_action_apply(a,
2347                         data,
2348                         RTE_TABLE_ACTION_FWD,
2349                         &action->fwd);
2350
2351                 if (status)
2352                         return status;
2353         }
2354
2355         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_LB)) {
2356                 status = rte_table_action_apply(a,
2357                         data,
2358                         RTE_TABLE_ACTION_LB,
2359                         &action->lb);
2360
2361                 if (status)
2362                         return status;
2363         }
2364
2365         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) {
2366                 status = rte_table_action_apply(a,
2367                         data,
2368                         RTE_TABLE_ACTION_MTR,
2369                         &action->mtr);
2370
2371                 if (status)
2372                         return status;
2373         }
2374
2375         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_TM)) {
2376                 status = rte_table_action_apply(a,
2377                         data,
2378                         RTE_TABLE_ACTION_TM,
2379                         &action->tm);
2380
2381                 if (status)
2382                         return status;
2383         }
2384
2385         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_ENCAP)) {
2386                 status = rte_table_action_apply(a,
2387                         data,
2388                         RTE_TABLE_ACTION_ENCAP,
2389                         &action->encap);
2390
2391                 if (status)
2392                         return status;
2393         }
2394
2395         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_NAT)) {
2396                 status = rte_table_action_apply(a,
2397                         data,
2398                         RTE_TABLE_ACTION_NAT,
2399                         &action->nat);
2400
2401                 if (status)
2402                         return status;
2403         }
2404
2405         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_TTL)) {
2406                 status = rte_table_action_apply(a,
2407                         data,
2408                         RTE_TABLE_ACTION_TTL,
2409                         &action->ttl);
2410
2411                 if (status)
2412                         return status;
2413         }
2414
2415         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_STATS)) {
2416                 status = rte_table_action_apply(a,
2417                         data,
2418                         RTE_TABLE_ACTION_STATS,
2419                         &action->stats);
2420
2421                 if (status)
2422                         return status;
2423         }
2424
2425         if (action->action_mask & (1LLU << RTE_TABLE_ACTION_TIME)) {
2426                 status = rte_table_action_apply(a,
2427                         data,
2428                         RTE_TABLE_ACTION_TIME,
2429                         &action->time);
2430
2431                 if (status)
2432                         return status;
2433         }
2434
2435         return 0;
2436 }
2437
2438 static struct pipeline_msg_rsp *
2439 pipeline_msg_handle_table_rule_add(struct pipeline_data *p,
2440         struct pipeline_msg_req *req)
2441 {
2442         union table_rule_match_low_level match_ll;
2443         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2444         struct softnic_table_rule_match *match = &req->table_rule_add.match;
2445         struct softnic_table_rule_action *action = &req->table_rule_add.action;
2446         struct rte_pipeline_table_entry *data_in, *data_out;
2447         uint32_t table_id = req->id;
2448         int key_found, status;
2449         struct rte_table_action *a = p->table_data[table_id].a;
2450
2451         /* Apply actions */
2452         memset(p->buffer, 0, sizeof(p->buffer));
2453         data_in = (struct rte_pipeline_table_entry *)p->buffer;
2454
2455         status = match_convert(match, &match_ll, 1);
2456         if (status) {
2457                 rsp->status = -1;
2458                 return rsp;
2459         }
2460
2461         status = action_convert(a, action, data_in);
2462         if (status) {
2463                 rsp->status = -1;
2464                 return rsp;
2465         }
2466
2467         status = rte_pipeline_table_entry_add(p->p,
2468                 table_id,
2469                 &match_ll,
2470                 data_in,
2471                 &key_found,
2472                 &data_out);
2473         if (status) {
2474                 rsp->status = -1;
2475                 return rsp;
2476         }
2477
2478         /* Write response */
2479         rsp->status = 0;
2480         rsp->table_rule_add.data = data_out;
2481
2482         return rsp;
2483 }
2484
2485 static struct pipeline_msg_rsp *
2486 pipeline_msg_handle_table_rule_add_default(struct pipeline_data *p,
2487         struct pipeline_msg_req *req)
2488 {
2489         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2490         struct softnic_table_rule_action *action = &req->table_rule_add_default.action;
2491         struct rte_pipeline_table_entry *data_in, *data_out;
2492         uint32_t table_id = req->id;
2493         int status;
2494
2495         /* Apply actions */
2496         memset(p->buffer, 0, sizeof(p->buffer));
2497         data_in = (struct rte_pipeline_table_entry *)p->buffer;
2498
2499         data_in->action = action->fwd.action;
2500         if (action->fwd.action == RTE_PIPELINE_ACTION_PORT)
2501                 data_in->port_id = action->fwd.id;
2502         if (action->fwd.action == RTE_PIPELINE_ACTION_TABLE)
2503                 data_in->table_id = action->fwd.id;
2504
2505         /* Add default rule to table */
2506         status = rte_pipeline_table_default_entry_add(p->p,
2507                 table_id,
2508                 data_in,
2509                 &data_out);
2510         if (status) {
2511                 rsp->status = -1;
2512                 return rsp;
2513         }
2514
2515         /* Write response */
2516         rsp->status = 0;
2517         rsp->table_rule_add_default.data = data_out;
2518
2519         return rsp;
2520 }
2521
2522 static struct pipeline_msg_rsp *
2523 pipeline_msg_handle_table_rule_add_bulk(struct pipeline_data *p,
2524         struct pipeline_msg_req *req)
2525 {
2526         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2527
2528         uint32_t table_id = req->id;
2529         struct softnic_table_rule_match *match = req->table_rule_add_bulk.match;
2530         struct softnic_table_rule_action *action = req->table_rule_add_bulk.action;
2531         struct rte_pipeline_table_entry **data =
2532                 (struct rte_pipeline_table_entry **)req->table_rule_add_bulk.data;
2533         uint32_t n_rules = req->table_rule_add_bulk.n_rules;
2534         uint32_t bulk = req->table_rule_add_bulk.bulk;
2535
2536         struct rte_table_action *a = p->table_data[table_id].a;
2537         union table_rule_match_low_level *match_ll;
2538         uint8_t *action_ll;
2539         void **match_ll_ptr;
2540         struct rte_pipeline_table_entry **action_ll_ptr;
2541         int *found, status;
2542         uint32_t i;
2543
2544         /* Memory allocation */
2545         match_ll = calloc(n_rules, sizeof(union table_rule_match_low_level));
2546         action_ll = calloc(n_rules, TABLE_RULE_ACTION_SIZE_MAX);
2547         match_ll_ptr = calloc(n_rules, sizeof(void *));
2548         action_ll_ptr =
2549                 calloc(n_rules, sizeof(struct rte_pipeline_table_entry *));
2550         found = calloc(n_rules, sizeof(int));
2551
2552         if (match_ll == NULL ||
2553                 action_ll == NULL ||
2554                 match_ll_ptr == NULL ||
2555                 action_ll_ptr == NULL ||
2556                 found == NULL)
2557                 goto fail;
2558
2559         for (i = 0; i < n_rules; i++) {
2560                 match_ll_ptr[i] = (void *)&match_ll[i];
2561                 action_ll_ptr[i] =
2562                         (struct rte_pipeline_table_entry *)&action_ll[i * TABLE_RULE_ACTION_SIZE_MAX];
2563         }
2564
2565         /* Rule match conversion */
2566         for (i = 0; i < n_rules; i++) {
2567                 status = match_convert(&match[i], match_ll_ptr[i], 1);
2568                 if (status)
2569                         goto fail;
2570         }
2571
2572         /* Rule action conversion */
2573         for (i = 0; i < n_rules; i++) {
2574                 status = action_convert(a, &action[i], action_ll_ptr[i]);
2575                 if (status)
2576                         goto fail;
2577         }
2578
2579         /* Add rule (match, action) to table */
2580         if (bulk) {
2581                 status = rte_pipeline_table_entry_add_bulk(p->p,
2582                         table_id,
2583                         match_ll_ptr,
2584                         action_ll_ptr,
2585                         n_rules,
2586                         found,
2587                         data);
2588                 if (status)
2589                         n_rules = 0;
2590         } else {
2591                 for (i = 0; i < n_rules; i++) {
2592                         status = rte_pipeline_table_entry_add(p->p,
2593                                 table_id,
2594                                 match_ll_ptr[i],
2595                                 action_ll_ptr[i],
2596                                 &found[i],
2597                                 &data[i]);
2598                         if (status) {
2599                                 n_rules = i;
2600                                 break;
2601                         }
2602                 }
2603         }
2604
2605         /* Write response */
2606         rsp->status = 0;
2607         rsp->table_rule_add_bulk.n_rules = n_rules;
2608
2609         /* Free */
2610         free(found);
2611         free(action_ll_ptr);
2612         free(match_ll_ptr);
2613         free(action_ll);
2614         free(match_ll);
2615
2616         return rsp;
2617
2618 fail:
2619         free(found);
2620         free(action_ll_ptr);
2621         free(match_ll_ptr);
2622         free(action_ll);
2623         free(match_ll);
2624
2625         rsp->status = -1;
2626         rsp->table_rule_add_bulk.n_rules = 0;
2627         return rsp;
2628 }
2629
2630 static struct pipeline_msg_rsp *
2631 pipeline_msg_handle_table_rule_delete(struct pipeline_data *p,
2632         struct pipeline_msg_req *req)
2633 {
2634         union table_rule_match_low_level match_ll;
2635         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2636         struct softnic_table_rule_match *match = &req->table_rule_delete.match;
2637         uint32_t table_id = req->id;
2638         int key_found, status;
2639
2640         status = match_convert(match, &match_ll, 0);
2641         if (status) {
2642                 rsp->status = -1;
2643                 return rsp;
2644         }
2645
2646         rsp->status = rte_pipeline_table_entry_delete(p->p,
2647                 table_id,
2648                 &match_ll,
2649                 &key_found,
2650                 NULL);
2651
2652         return rsp;
2653 }
2654
2655 static struct pipeline_msg_rsp *
2656 pipeline_msg_handle_table_rule_delete_default(struct pipeline_data *p,
2657         struct pipeline_msg_req *req)
2658 {
2659         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2660         uint32_t table_id = req->id;
2661
2662         rsp->status = rte_pipeline_table_default_entry_delete(p->p,
2663                 table_id,
2664                 NULL);
2665
2666         return rsp;
2667 }
2668
2669 static struct pipeline_msg_rsp *
2670 pipeline_msg_handle_table_rule_stats_read(struct pipeline_data *p,
2671         struct pipeline_msg_req *req)
2672 {
2673         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2674         uint32_t table_id = req->id;
2675         void *data = req->table_rule_stats_read.data;
2676         int clear = req->table_rule_stats_read.clear;
2677         struct rte_table_action *a = p->table_data[table_id].a;
2678
2679         rsp->status = rte_table_action_stats_read(a,
2680                 data,
2681                 &rsp->table_rule_stats_read.stats,
2682                 clear);
2683
2684         return rsp;
2685 }
2686
2687 static struct pipeline_msg_rsp *
2688 pipeline_msg_handle_table_mtr_profile_add(struct pipeline_data *p,
2689         struct pipeline_msg_req *req)
2690 {
2691         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2692         uint32_t table_id = req->id;
2693         uint32_t meter_profile_id = req->table_mtr_profile_add.meter_profile_id;
2694         struct rte_table_action_meter_profile *profile =
2695                 &req->table_mtr_profile_add.profile;
2696         struct rte_table_action *a = p->table_data[table_id].a;
2697
2698         rsp->status = rte_table_action_meter_profile_add(a,
2699                 meter_profile_id,
2700                 profile);
2701
2702         return rsp;
2703 }
2704
2705 static struct pipeline_msg_rsp *
2706 pipeline_msg_handle_table_mtr_profile_delete(struct pipeline_data *p,
2707         struct pipeline_msg_req *req)
2708 {
2709         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2710         uint32_t table_id = req->id;
2711         uint32_t meter_profile_id =
2712                 req->table_mtr_profile_delete.meter_profile_id;
2713         struct rte_table_action *a = p->table_data[table_id].a;
2714
2715         rsp->status = rte_table_action_meter_profile_delete(a,
2716                 meter_profile_id);
2717
2718         return rsp;
2719 }
2720
2721 static struct pipeline_msg_rsp *
2722 pipeline_msg_handle_table_rule_mtr_read(struct pipeline_data *p,
2723         struct pipeline_msg_req *req)
2724 {
2725         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2726         uint32_t table_id = req->id;
2727         void *data = req->table_rule_mtr_read.data;
2728         uint32_t tc_mask = req->table_rule_mtr_read.tc_mask;
2729         int clear = req->table_rule_mtr_read.clear;
2730         struct rte_table_action *a = p->table_data[table_id].a;
2731
2732         rsp->status = rte_table_action_meter_read(a,
2733                 data,
2734                 tc_mask,
2735                 &rsp->table_rule_mtr_read.stats,
2736                 clear);
2737
2738         return rsp;
2739 }
2740
2741 static struct pipeline_msg_rsp *
2742 pipeline_msg_handle_table_dscp_table_update(struct pipeline_data *p,
2743         struct pipeline_msg_req *req)
2744 {
2745         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2746         uint32_t table_id = req->id;
2747         uint64_t dscp_mask = req->table_dscp_table_update.dscp_mask;
2748         struct rte_table_action_dscp_table *dscp_table =
2749                 &req->table_dscp_table_update.dscp_table;
2750         struct rte_table_action *a = p->table_data[table_id].a;
2751
2752         rsp->status = rte_table_action_dscp_table_update(a,
2753                 dscp_mask,
2754                 dscp_table);
2755
2756         return rsp;
2757 }
2758
2759 static struct pipeline_msg_rsp *
2760 pipeline_msg_handle_table_rule_ttl_read(struct pipeline_data *p,
2761         struct pipeline_msg_req *req)
2762 {
2763         struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *)req;
2764         uint32_t table_id = req->id;
2765         void *data = req->table_rule_ttl_read.data;
2766         int clear = req->table_rule_ttl_read.clear;
2767         struct rte_table_action *a = p->table_data[table_id].a;
2768
2769         rsp->status = rte_table_action_ttl_read(a,
2770                 data,
2771                 &rsp->table_rule_ttl_read.stats,
2772                 clear);
2773
2774         return rsp;
2775 }
2776
2777 static void
2778 pipeline_msg_handle(struct pipeline_data *p)
2779 {
2780         for ( ; ; ) {
2781                 struct pipeline_msg_req *req;
2782                 struct pipeline_msg_rsp *rsp;
2783
2784                 req = pipeline_msg_recv(p->msgq_req);
2785                 if (req == NULL)
2786                         break;
2787
2788                 switch (req->type) {
2789                 case PIPELINE_REQ_PORT_IN_STATS_READ:
2790                         rsp = pipeline_msg_handle_port_in_stats_read(p, req);
2791                         break;
2792
2793                 case PIPELINE_REQ_PORT_IN_ENABLE:
2794                         rsp = pipeline_msg_handle_port_in_enable(p, req);
2795                         break;
2796
2797                 case PIPELINE_REQ_PORT_IN_DISABLE:
2798                         rsp = pipeline_msg_handle_port_in_disable(p, req);
2799                         break;
2800
2801                 case PIPELINE_REQ_PORT_OUT_STATS_READ:
2802                         rsp = pipeline_msg_handle_port_out_stats_read(p, req);
2803                         break;
2804
2805                 case PIPELINE_REQ_TABLE_STATS_READ:
2806                         rsp = pipeline_msg_handle_table_stats_read(p, req);
2807                         break;
2808
2809                 case PIPELINE_REQ_TABLE_RULE_ADD:
2810                         rsp = pipeline_msg_handle_table_rule_add(p, req);
2811                         break;
2812
2813                 case PIPELINE_REQ_TABLE_RULE_ADD_DEFAULT:
2814                         rsp = pipeline_msg_handle_table_rule_add_default(p,     req);
2815                         break;
2816
2817                 case PIPELINE_REQ_TABLE_RULE_ADD_BULK:
2818                         rsp = pipeline_msg_handle_table_rule_add_bulk(p, req);
2819                         break;
2820
2821                 case PIPELINE_REQ_TABLE_RULE_DELETE:
2822                         rsp = pipeline_msg_handle_table_rule_delete(p, req);
2823                         break;
2824
2825                 case PIPELINE_REQ_TABLE_RULE_DELETE_DEFAULT:
2826                         rsp = pipeline_msg_handle_table_rule_delete_default(p, req);
2827                         break;
2828
2829                 case PIPELINE_REQ_TABLE_RULE_STATS_READ:
2830                         rsp = pipeline_msg_handle_table_rule_stats_read(p, req);
2831                         break;
2832
2833                 case PIPELINE_REQ_TABLE_MTR_PROFILE_ADD:
2834                         rsp = pipeline_msg_handle_table_mtr_profile_add(p, req);
2835                         break;
2836
2837                 case PIPELINE_REQ_TABLE_MTR_PROFILE_DELETE:
2838                         rsp = pipeline_msg_handle_table_mtr_profile_delete(p, req);
2839                         break;
2840
2841                 case PIPELINE_REQ_TABLE_RULE_MTR_READ:
2842                         rsp = pipeline_msg_handle_table_rule_mtr_read(p, req);
2843                         break;
2844
2845                 case PIPELINE_REQ_TABLE_DSCP_TABLE_UPDATE:
2846                         rsp = pipeline_msg_handle_table_dscp_table_update(p, req);
2847                         break;
2848
2849                 case PIPELINE_REQ_TABLE_RULE_TTL_READ:
2850                         rsp = pipeline_msg_handle_table_rule_ttl_read(p, req);
2851                         break;
2852
2853                 default:
2854                         rsp = (struct pipeline_msg_rsp *)req;
2855                         rsp->status = -1;
2856                 }
2857
2858                 pipeline_msg_send(p->msgq_rsp, rsp);
2859         }
2860 }
2861
2862 /**
2863  * Data plane threads: main
2864  */
2865 int
2866 rte_pmd_softnic_run(uint16_t port_id)
2867 {
2868         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2869         struct pmd_internals *softnic;
2870         struct softnic_thread_data *t;
2871         uint32_t thread_id, j;
2872
2873 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
2874         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, 0);
2875 #endif
2876
2877         softnic = dev->data->dev_private;
2878         thread_id = rte_lcore_id();
2879         t = &softnic->thread_data[thread_id];
2880         t->iter++;
2881
2882         /* Data Plane */
2883         for (j = 0; j < t->n_pipelines; j++)
2884                 rte_pipeline_run(t->p[j]);
2885
2886         /* Control Plane */
2887         if ((t->iter & 0xFLLU) == 0) {
2888                 uint64_t time = rte_get_tsc_cycles();
2889                 uint64_t time_next_min = UINT64_MAX;
2890
2891                 if (time < t->time_next_min)
2892                         return 0;
2893
2894                 /* Pipeline message queues */
2895                 for (j = 0; j < t->n_pipelines; j++) {
2896                         struct pipeline_data *p =
2897                                 &t->pipeline_data[j];
2898                         uint64_t time_next = p->time_next;
2899
2900                         if (time_next <= time) {
2901                                 pipeline_msg_handle(p);
2902                                 rte_pipeline_flush(p->p);
2903                                 time_next = time + p->timer_period;
2904                                 p->time_next = time_next;
2905                         }
2906
2907                         if (time_next < time_next_min)
2908                                 time_next_min = time_next;
2909                 }
2910
2911                 /* Thread message queues */
2912                 {
2913                         uint64_t time_next = t->time_next;
2914
2915                         if (time_next <= time) {
2916                                 thread_msg_handle(t);
2917                                 time_next = time + t->timer_period;
2918                                 t->time_next = time_next;
2919                         }
2920
2921                         if (time_next < time_next_min)
2922                                 time_next_min = time_next;
2923                 }
2924
2925                 t->time_next_min = time_next_min;
2926         }
2927
2928         return 0;
2929 }