New upstream version 17.11-rc3
[deb_dpdk.git] / examples / ip_pipeline / pipeline / pipeline_flow_classification.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <sys/queue.h>
37 #include <netinet/in.h>
38 #include <unistd.h>
39
40 #include <rte_common.h>
41 #include <rte_hexdump.h>
42 #include <rte_malloc.h>
43 #include <cmdline_rdline.h>
44 #include <cmdline_parse.h>
45 #include <cmdline_parse_num.h>
46 #include <cmdline_parse_string.h>
47
48 #include "app.h"
49 #include "pipeline_common_fe.h"
50 #include "pipeline_flow_classification.h"
51 #include "hash_func.h"
52 #include "parser.h"
53
54 /*
55  * Key conversion
56  */
57
58 struct pkt_key_qinq {
59         uint16_t ethertype_svlan;
60         uint16_t svlan;
61         uint16_t ethertype_cvlan;
62         uint16_t cvlan;
63 } __attribute__((__packed__));
64
65 struct pkt_key_ipv4_5tuple {
66         uint8_t ttl;
67         uint8_t proto;
68         uint16_t checksum;
69         uint32_t ip_src;
70         uint32_t ip_dst;
71         uint16_t port_src;
72         uint16_t port_dst;
73 } __attribute__((__packed__));
74
75 struct pkt_key_ipv6_5tuple {
76         uint16_t payload_length;
77         uint8_t proto;
78         uint8_t hop_limit;
79         uint8_t ip_src[16];
80         uint8_t ip_dst[16];
81         uint16_t port_src;
82         uint16_t port_dst;
83 } __attribute__((__packed__));
84
85 static int
86 app_pipeline_fc_key_convert(struct pipeline_fc_key *key_in,
87         uint8_t *key_out,
88         uint32_t *signature)
89 {
90         uint8_t buffer[PIPELINE_FC_FLOW_KEY_MAX_SIZE];
91         uint8_t m[PIPELINE_FC_FLOW_KEY_MAX_SIZE]; /* key mask */
92         void *key_buffer = (key_out) ? key_out : buffer;
93
94         memset(m, 0xFF, sizeof(m));
95         switch (key_in->type) {
96         case FLOW_KEY_QINQ:
97         {
98                 struct pkt_key_qinq *qinq = key_buffer;
99
100                 qinq->ethertype_svlan = 0;
101                 qinq->svlan = rte_cpu_to_be_16(key_in->key.qinq.svlan);
102                 qinq->ethertype_cvlan = 0;
103                 qinq->cvlan = rte_cpu_to_be_16(key_in->key.qinq.cvlan);
104
105                 if (signature)
106                         *signature = (uint32_t) hash_default_key8(qinq, m, 8, 0);
107                 return 0;
108         }
109
110         case FLOW_KEY_IPV4_5TUPLE:
111         {
112                 struct pkt_key_ipv4_5tuple *ipv4 = key_buffer;
113
114                 ipv4->ttl = 0;
115                 ipv4->proto = key_in->key.ipv4_5tuple.proto;
116                 ipv4->checksum = 0;
117                 ipv4->ip_src = rte_cpu_to_be_32(key_in->key.ipv4_5tuple.ip_src);
118                 ipv4->ip_dst = rte_cpu_to_be_32(key_in->key.ipv4_5tuple.ip_dst);
119                 ipv4->port_src = rte_cpu_to_be_16(key_in->key.ipv4_5tuple.port_src);
120                 ipv4->port_dst = rte_cpu_to_be_16(key_in->key.ipv4_5tuple.port_dst);
121
122                 if (signature)
123                         *signature = (uint32_t) hash_default_key16(ipv4, m, 16, 0);
124                 return 0;
125         }
126
127         case FLOW_KEY_IPV6_5TUPLE:
128         {
129                 struct pkt_key_ipv6_5tuple *ipv6 = key_buffer;
130
131                 memset(ipv6, 0, 64);
132                 ipv6->payload_length = 0;
133                 ipv6->proto = key_in->key.ipv6_5tuple.proto;
134                 ipv6->hop_limit = 0;
135                 memcpy(&ipv6->ip_src, &key_in->key.ipv6_5tuple.ip_src, 16);
136                 memcpy(&ipv6->ip_dst, &key_in->key.ipv6_5tuple.ip_dst, 16);
137                 ipv6->port_src = rte_cpu_to_be_16(key_in->key.ipv6_5tuple.port_src);
138                 ipv6->port_dst = rte_cpu_to_be_16(key_in->key.ipv6_5tuple.port_dst);
139
140                 if (signature)
141                         *signature = (uint32_t) hash_default_key64(ipv6, m, 64, 0);
142                 return 0;
143         }
144
145         default:
146                 return -1;
147         }
148 }
149
150 /*
151  * Flow classification pipeline
152  */
153
154 struct app_pipeline_fc_flow {
155         struct pipeline_fc_key key;
156         uint32_t port_id;
157         uint32_t flow_id;
158         uint32_t signature;
159         void *entry_ptr;
160
161         TAILQ_ENTRY(app_pipeline_fc_flow) node;
162 };
163
164 #define N_BUCKETS                                65536
165
166 struct app_pipeline_fc {
167         /* Parameters */
168         uint32_t n_ports_in;
169         uint32_t n_ports_out;
170
171         /* Flows */
172         TAILQ_HEAD(, app_pipeline_fc_flow) flows[N_BUCKETS];
173         uint32_t n_flows;
174
175         /* Default flow */
176         uint32_t default_flow_present;
177         uint32_t default_flow_port_id;
178         void *default_flow_entry_ptr;
179 };
180
181 static struct app_pipeline_fc_flow *
182 app_pipeline_fc_flow_find(struct app_pipeline_fc *p,
183         struct pipeline_fc_key *key)
184 {
185         struct app_pipeline_fc_flow *f;
186         uint32_t signature, bucket_id;
187
188         app_pipeline_fc_key_convert(key, NULL, &signature);
189         bucket_id = signature & (N_BUCKETS - 1);
190
191         TAILQ_FOREACH(f, &p->flows[bucket_id], node)
192                 if ((signature == f->signature) &&
193                         (memcmp(key,
194                                 &f->key,
195                                 sizeof(struct pipeline_fc_key)) == 0))
196                         return f;
197
198         return NULL;
199 }
200
201 static void*
202 app_pipeline_fc_init(struct pipeline_params *params,
203         __rte_unused void *arg)
204 {
205         struct app_pipeline_fc *p;
206         uint32_t size, i;
207
208         /* Check input arguments */
209         if ((params == NULL) ||
210                 (params->n_ports_in == 0) ||
211                 (params->n_ports_out == 0))
212                 return NULL;
213
214         /* Memory allocation */
215         size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct app_pipeline_fc));
216         p = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
217         if (p == NULL)
218                 return NULL;
219
220         /* Initialization */
221         p->n_ports_in = params->n_ports_in;
222         p->n_ports_out = params->n_ports_out;
223
224         for (i = 0; i < N_BUCKETS; i++)
225                 TAILQ_INIT(&p->flows[i]);
226         p->n_flows = 0;
227
228         return (void *) p;
229 }
230
231 static int
232 app_pipeline_fc_free(void *pipeline)
233 {
234         struct app_pipeline_fc *p = pipeline;
235         uint32_t i;
236
237         /* Check input arguments */
238         if (p == NULL)
239                 return -1;
240
241         /* Free resources */
242         for (i = 0; i < N_BUCKETS; i++)
243                 while (!TAILQ_EMPTY(&p->flows[i])) {
244                         struct app_pipeline_fc_flow *flow;
245
246                         flow = TAILQ_FIRST(&p->flows[i]);
247                         TAILQ_REMOVE(&p->flows[i], flow, node);
248                         rte_free(flow);
249                 }
250
251         rte_free(p);
252         return 0;
253 }
254
255 static int
256 app_pipeline_fc_key_check(struct pipeline_fc_key *key)
257 {
258         switch (key->type) {
259         case FLOW_KEY_QINQ:
260         {
261                 uint16_t svlan = key->key.qinq.svlan;
262                 uint16_t cvlan = key->key.qinq.cvlan;
263
264                 if ((svlan & 0xF000) ||
265                         (cvlan & 0xF000))
266                         return -1;
267
268                 return 0;
269         }
270
271         case FLOW_KEY_IPV4_5TUPLE:
272                 return 0;
273
274         case FLOW_KEY_IPV6_5TUPLE:
275                 return 0;
276
277         default:
278                 return -1;
279         }
280 }
281
282 int
283 app_pipeline_fc_load_file_qinq(char *filename,
284         struct pipeline_fc_key *keys,
285         uint32_t *port_ids,
286         uint32_t *flow_ids,
287         uint32_t *n_keys,
288         uint32_t *line)
289 {
290         FILE *f = NULL;
291         char file_buf[1024];
292         uint32_t i, l;
293
294         /* Check input arguments */
295         if ((filename == NULL) ||
296                 (keys == NULL) ||
297                 (port_ids == NULL) ||
298                 (flow_ids == NULL) ||
299                 (n_keys == NULL) ||
300                 (*n_keys == 0) ||
301                 (line == NULL)) {
302                 if (line)
303                         *line = 0;
304                 return -1;
305                 }
306
307         /* Open input file */
308         f = fopen(filename, "r");
309         if (f == NULL) {
310                 *line = 0;
311                 return -1;
312         }
313
314         /* Read file */
315         for (i = 0, l = 1; i < *n_keys; l++) {
316                 char *tokens[32];
317                 uint32_t n_tokens = RTE_DIM(tokens);
318
319                 uint16_t svlan, cvlan;
320                 uint32_t portid, flowid;
321                 int status;
322
323                 if (fgets(file_buf, sizeof(file_buf), f) == NULL)
324                         break;
325
326                 status = parse_tokenize_string(file_buf, tokens, &n_tokens);
327                 if (status)
328                         goto error1;
329
330                 if ((n_tokens == 0) || (tokens[0][0] == '#'))
331                         continue;
332
333                 if ((n_tokens != 7) ||
334                         strcmp(tokens[0], "qinq") ||
335                         parser_read_uint16(&svlan, tokens[1]) ||
336                         parser_read_uint16(&cvlan, tokens[2]) ||
337                         strcmp(tokens[3], "port") ||
338                         parser_read_uint32(&portid, tokens[4]) ||
339                         strcmp(tokens[5], "id") ||
340                         parser_read_uint32(&flowid, tokens[6]))
341                         goto error1;
342
343                 keys[i].type = FLOW_KEY_QINQ;
344                 keys[i].key.qinq.svlan = svlan;
345                 keys[i].key.qinq.cvlan = cvlan;
346
347                 port_ids[i] = portid;
348                 flow_ids[i] = flowid;
349
350                 if (app_pipeline_fc_key_check(&keys[i]))
351                         goto error1;
352
353                 i++;
354         }
355
356         /* Close file */
357         *n_keys = i;
358         fclose(f);
359         return 0;
360
361 error1:
362         *line = l;
363         fclose(f);
364         return -1;
365 }
366
367 int
368 app_pipeline_fc_load_file_ipv4(char *filename,
369         struct pipeline_fc_key *keys,
370         uint32_t *port_ids,
371         uint32_t *flow_ids,
372         uint32_t *n_keys,
373         uint32_t *line)
374 {
375         FILE *f = NULL;
376         char file_buf[1024];
377         uint32_t i, l;
378
379         /* Check input arguments */
380         if ((filename == NULL) ||
381                 (keys == NULL) ||
382                 (port_ids == NULL) ||
383                 (flow_ids == NULL) ||
384                 (n_keys == NULL) ||
385                 (*n_keys == 0) ||
386                 (line == NULL)) {
387                 if (line)
388                         *line = 0;
389                 return -1;
390                 }
391
392         /* Open input file */
393         f = fopen(filename, "r");
394         if (f == NULL) {
395                 *line = 0;
396                 return -1;
397         }
398
399         /* Read file */
400         for (i = 0, l = 1; i < *n_keys; l++) {
401                 char *tokens[32];
402                 uint32_t n_tokens = RTE_DIM(tokens);
403
404                 struct in_addr sipaddr, dipaddr;
405                 uint16_t sport, dport;
406                 uint8_t proto;
407                 uint32_t portid, flowid;
408                 int status;
409
410                 if (fgets(file_buf, sizeof(file_buf), f) == NULL)
411                         break;
412
413                 status = parse_tokenize_string(file_buf, tokens, &n_tokens);
414                 if (status)
415                         goto error2;
416
417                 if ((n_tokens == 0) || (tokens[0][0] == '#'))
418                         continue;
419
420                 if ((n_tokens != 10) ||
421                         strcmp(tokens[0], "ipv4") ||
422                         parse_ipv4_addr(tokens[1], &sipaddr) ||
423                         parse_ipv4_addr(tokens[2], &dipaddr) ||
424                         parser_read_uint16(&sport, tokens[3]) ||
425                         parser_read_uint16(&dport, tokens[4]) ||
426                         parser_read_uint8(&proto, tokens[5]) ||
427                         strcmp(tokens[6], "port") ||
428                         parser_read_uint32(&portid, tokens[7]) ||
429                         strcmp(tokens[8], "id") ||
430                         parser_read_uint32(&flowid, tokens[9]))
431                         goto error2;
432
433                 keys[i].type = FLOW_KEY_IPV4_5TUPLE;
434                 keys[i].key.ipv4_5tuple.ip_src = rte_be_to_cpu_32(sipaddr.s_addr);
435                 keys[i].key.ipv4_5tuple.ip_dst = rte_be_to_cpu_32(dipaddr.s_addr);
436                 keys[i].key.ipv4_5tuple.port_src = sport;
437                 keys[i].key.ipv4_5tuple.port_dst = dport;
438                 keys[i].key.ipv4_5tuple.proto = proto;
439
440                 port_ids[i] = portid;
441                 flow_ids[i] = flowid;
442
443                 if (app_pipeline_fc_key_check(&keys[i]))
444                         goto error2;
445
446                 i++;
447         }
448
449         /* Close file */
450         *n_keys = i;
451         fclose(f);
452         return 0;
453
454 error2:
455         *line = l;
456         fclose(f);
457         return -1;
458 }
459
460 int
461 app_pipeline_fc_load_file_ipv6(char *filename,
462         struct pipeline_fc_key *keys,
463         uint32_t *port_ids,
464         uint32_t *flow_ids,
465         uint32_t *n_keys,
466         uint32_t *line)
467 {
468         FILE *f = NULL;
469         char file_buf[1024];
470         uint32_t i, l;
471
472         /* Check input arguments */
473         if ((filename == NULL) ||
474                 (keys == NULL) ||
475                 (port_ids == NULL) ||
476                 (flow_ids == NULL) ||
477                 (n_keys == NULL) ||
478                 (*n_keys == 0) ||
479                 (line == NULL)) {
480                 if (line)
481                         *line = 0;
482                 return -1;
483                 }
484
485         /* Open input file */
486         f = fopen(filename, "r");
487         if (f == NULL) {
488                 *line = 0;
489                 return -1;
490         }
491
492         /* Read file */
493         for (i = 0, l = 1; i < *n_keys; l++) {
494                 char *tokens[32];
495                 uint32_t n_tokens = RTE_DIM(tokens);
496
497                 struct in6_addr sipaddr, dipaddr;
498                 uint16_t sport, dport;
499                 uint8_t proto;
500                 uint32_t portid, flowid;
501                 int status;
502
503                 if (fgets(file_buf, sizeof(file_buf), f) == NULL)
504                         break;
505
506                 status = parse_tokenize_string(file_buf, tokens, &n_tokens);
507                 if (status)
508                         goto error3;
509
510                 if ((n_tokens == 0) || (tokens[0][0] == '#'))
511                         continue;
512
513                 if ((n_tokens != 10) ||
514                         strcmp(tokens[0], "ipv6") ||
515                         parse_ipv6_addr(tokens[1], &sipaddr) ||
516                         parse_ipv6_addr(tokens[2], &dipaddr) ||
517                         parser_read_uint16(&sport, tokens[3]) ||
518                         parser_read_uint16(&dport, tokens[4]) ||
519                         parser_read_uint8(&proto, tokens[5]) ||
520                         strcmp(tokens[6], "port") ||
521                         parser_read_uint32(&portid, tokens[7]) ||
522                         strcmp(tokens[8], "id") ||
523                         parser_read_uint32(&flowid, tokens[9]))
524                         goto error3;
525
526                 keys[i].type = FLOW_KEY_IPV6_5TUPLE;
527                 memcpy(keys[i].key.ipv6_5tuple.ip_src,
528                         sipaddr.s6_addr,
529                         sizeof(sipaddr.s6_addr));
530                 memcpy(keys[i].key.ipv6_5tuple.ip_dst,
531                         dipaddr.s6_addr,
532                         sizeof(dipaddr.s6_addr));
533                 keys[i].key.ipv6_5tuple.port_src = sport;
534                 keys[i].key.ipv6_5tuple.port_dst = dport;
535                 keys[i].key.ipv6_5tuple.proto = proto;
536
537                 port_ids[i] = portid;
538                 flow_ids[i] = flowid;
539
540                 if (app_pipeline_fc_key_check(&keys[i]))
541                         goto error3;
542
543                 i++;
544         }
545
546         /* Close file */
547         *n_keys = i;
548         fclose(f);
549         return 0;
550
551 error3:
552         *line = l;
553         fclose(f);
554         return -1;
555 }
556
557
558
559 int
560 app_pipeline_fc_add(struct app_params *app,
561         uint32_t pipeline_id,
562         struct pipeline_fc_key *key,
563         uint32_t port_id,
564         uint32_t flow_id)
565 {
566         struct app_pipeline_fc *p;
567         struct app_pipeline_fc_flow *flow;
568
569         struct pipeline_fc_add_msg_req *req;
570         struct pipeline_fc_add_msg_rsp *rsp;
571
572         uint32_t signature;
573         int new_flow;
574
575         /* Check input arguments */
576         if ((app == NULL) ||
577                 (key == NULL))
578                 return -1;
579
580         p = app_pipeline_data_fe(app, pipeline_id, &pipeline_flow_classification);
581         if (p == NULL)
582                 return -1;
583
584         if (port_id >= p->n_ports_out)
585                 return -1;
586
587         if (app_pipeline_fc_key_check(key) != 0)
588                 return -1;
589
590         /* Find existing flow or allocate new flow */
591         flow = app_pipeline_fc_flow_find(p, key);
592         new_flow = (flow == NULL);
593         if (flow == NULL) {
594                 flow = rte_malloc(NULL, sizeof(*flow), RTE_CACHE_LINE_SIZE);
595
596                 if (flow == NULL)
597                         return -1;
598         }
599
600         /* Allocate and write request */
601         req = app_msg_alloc(app);
602         if (req == NULL)
603                 return -1;
604
605         req->type = PIPELINE_MSG_REQ_CUSTOM;
606         req->subtype = PIPELINE_FC_MSG_REQ_FLOW_ADD;
607         app_pipeline_fc_key_convert(key, req->key, &signature);
608         req->port_id = port_id;
609         req->flow_id = flow_id;
610
611         /* Send request and wait for response */
612         rsp = app_msg_send_recv(app, pipeline_id, req, MSG_TIMEOUT_DEFAULT);
613         if (rsp == NULL) {
614                 if (new_flow)
615                         rte_free(flow);
616                 return -1;
617         }
618
619         /* Read response and write flow */
620         if (rsp->status ||
621                 (rsp->entry_ptr == NULL) ||
622                 ((new_flow == 0) && (rsp->key_found == 0)) ||
623                 ((new_flow == 1) && (rsp->key_found == 1))) {
624                 app_msg_free(app, rsp);
625                 if (new_flow)
626                         rte_free(flow);
627                 return -1;
628         }
629
630         memset(&flow->key, 0, sizeof(flow->key));
631         memcpy(&flow->key, key, sizeof(flow->key));
632         flow->port_id = port_id;
633         flow->flow_id = flow_id;
634         flow->signature = signature;
635         flow->entry_ptr = rsp->entry_ptr;
636
637         /* Commit rule */
638         if (new_flow) {
639                 uint32_t bucket_id = signature & (N_BUCKETS - 1);
640
641                 TAILQ_INSERT_TAIL(&p->flows[bucket_id], flow, node);
642                 p->n_flows++;
643         }
644
645         /* Free response */
646         app_msg_free(app, rsp);
647
648         return 0;
649 }
650
651 int
652 app_pipeline_fc_add_bulk(struct app_params *app,
653         uint32_t pipeline_id,
654         struct pipeline_fc_key *key,
655         uint32_t *port_id,
656         uint32_t *flow_id,
657         uint32_t n_keys)
658 {
659         struct app_pipeline_fc *p;
660         struct pipeline_fc_add_bulk_msg_req *req;
661         struct pipeline_fc_add_bulk_msg_rsp *rsp;
662
663         struct app_pipeline_fc_flow **flow;
664         uint32_t *signature;
665         int *new_flow;
666         struct pipeline_fc_add_bulk_flow_req *flow_req;
667         struct pipeline_fc_add_bulk_flow_rsp *flow_rsp;
668
669         uint32_t i;
670         int status;
671
672         /* Check input arguments */
673         if ((app == NULL) ||
674                 (key == NULL) ||
675                 (port_id == NULL) ||
676                 (flow_id == NULL) ||
677                 (n_keys == 0))
678                 return -1;
679
680         p = app_pipeline_data_fe(app, pipeline_id, &pipeline_flow_classification);
681         if (p == NULL)
682                 return -1;
683
684         for (i = 0; i < n_keys; i++)
685                 if (port_id[i] >= p->n_ports_out)
686                         return -1;
687
688         for (i = 0; i < n_keys; i++)
689                 if (app_pipeline_fc_key_check(&key[i]) != 0)
690                         return -1;
691
692         /* Memory allocation */
693         flow = rte_malloc(NULL,
694                 n_keys * sizeof(struct app_pipeline_fc_flow *),
695                 RTE_CACHE_LINE_SIZE);
696         if (flow == NULL)
697                 return -1;
698
699         signature = rte_malloc(NULL,
700                 n_keys * sizeof(uint32_t),
701                 RTE_CACHE_LINE_SIZE);
702         if (signature == NULL) {
703                 rte_free(flow);
704                 return -1;
705         }
706
707         new_flow = rte_malloc(
708                 NULL,
709                 n_keys * sizeof(int),
710                 RTE_CACHE_LINE_SIZE);
711         if (new_flow == NULL) {
712                 rte_free(signature);
713                 rte_free(flow);
714                 return -1;
715         }
716
717         flow_req = rte_malloc(NULL,
718                 n_keys * sizeof(struct pipeline_fc_add_bulk_flow_req),
719                 RTE_CACHE_LINE_SIZE);
720         if (flow_req == NULL) {
721                 rte_free(new_flow);
722                 rte_free(signature);
723                 rte_free(flow);
724                 return -1;
725         }
726
727         flow_rsp = rte_malloc(NULL,
728                 n_keys * sizeof(struct pipeline_fc_add_bulk_flow_rsp),
729                 RTE_CACHE_LINE_SIZE);
730         if (flow_rsp == NULL) {
731                 rte_free(flow_req);
732                 rte_free(new_flow);
733                 rte_free(signature);
734                 rte_free(flow);
735                 return -1;
736         }
737
738         /* Find existing flow or allocate new flow */
739         for (i = 0; i < n_keys; i++) {
740                 flow[i] = app_pipeline_fc_flow_find(p, &key[i]);
741                 new_flow[i] = (flow[i] == NULL);
742                 if (flow[i] == NULL) {
743                         flow[i] = rte_zmalloc(NULL,
744                                 sizeof(struct app_pipeline_fc_flow),
745                                 RTE_CACHE_LINE_SIZE);
746
747                         if (flow[i] == NULL) {
748                                 uint32_t j;
749
750                                 for (j = 0; j < i; j++)
751                                         if (new_flow[j])
752                                                 rte_free(flow[j]);
753
754                                 rte_free(flow_rsp);
755                                 rte_free(flow_req);
756                                 rte_free(new_flow);
757                                 rte_free(signature);
758                                 rte_free(flow);
759                                 return -1;
760                         }
761                 }
762         }
763
764         /* Allocate and write request */
765         req = app_msg_alloc(app);
766         if (req == NULL) {
767                 for (i = 0; i < n_keys; i++)
768                         if (new_flow[i])
769                                 rte_free(flow[i]);
770
771                 rte_free(flow_rsp);
772                 rte_free(flow_req);
773                 rte_free(new_flow);
774                 rte_free(signature);
775                 rte_free(flow);
776                 return -1;
777         }
778
779         for (i = 0; i < n_keys; i++) {
780                 app_pipeline_fc_key_convert(&key[i],
781                         flow_req[i].key,
782                         &signature[i]);
783                 flow_req[i].port_id = port_id[i];
784                 flow_req[i].flow_id = flow_id[i];
785         }
786
787         req->type = PIPELINE_MSG_REQ_CUSTOM;
788         req->subtype = PIPELINE_FC_MSG_REQ_FLOW_ADD_BULK;
789         req->req = flow_req;
790         req->rsp = flow_rsp;
791         req->n_keys = n_keys;
792
793         /* Send request and wait for response */
794         rsp = app_msg_send_recv(app, pipeline_id, req, 10000);
795         if (rsp == NULL) {
796                 for (i = 0; i < n_keys; i++)
797                         if (new_flow[i])
798                                 rte_free(flow[i]);
799
800                 rte_free(flow_rsp);
801                 rte_free(flow_req);
802                 rte_free(new_flow);
803                 rte_free(signature);
804                 rte_free(flow);
805                 return -1;
806         }
807
808         /* Read response */
809         status = 0;
810
811         for (i = 0; i < rsp->n_keys; i++)
812                 if ((flow_rsp[i].entry_ptr == NULL) ||
813                         ((new_flow[i] == 0) && (flow_rsp[i].key_found == 0)) ||
814                         ((new_flow[i] == 1) && (flow_rsp[i].key_found == 1)))
815                         status = -1;
816
817         if (rsp->n_keys < n_keys)
818                 status = -1;
819
820         /* Commit flows */
821         for (i = 0; i < rsp->n_keys; i++) {
822                 memcpy(&flow[i]->key, &key[i], sizeof(flow[i]->key));
823                 flow[i]->port_id = port_id[i];
824                 flow[i]->flow_id = flow_id[i];
825                 flow[i]->signature = signature[i];
826                 flow[i]->entry_ptr = flow_rsp[i].entry_ptr;
827
828                 if (new_flow[i]) {
829                         uint32_t bucket_id = signature[i] & (N_BUCKETS - 1);
830
831                         TAILQ_INSERT_TAIL(&p->flows[bucket_id], flow[i], node);
832                         p->n_flows++;
833                 }
834         }
835
836         /* Free resources */
837
838         for (i = rsp->n_keys; i < n_keys; i++)
839                 if (new_flow[i])
840                         rte_free(flow[i]);
841
842         app_msg_free(app, rsp);
843         rte_free(flow_rsp);
844         rte_free(flow_req);
845         rte_free(new_flow);
846         rte_free(signature);
847         rte_free(flow);
848
849         return status;
850 }
851
852 int
853 app_pipeline_fc_del(struct app_params *app,
854         uint32_t pipeline_id,
855         struct pipeline_fc_key *key)
856 {
857         struct app_pipeline_fc *p;
858         struct app_pipeline_fc_flow *flow;
859
860         struct pipeline_fc_del_msg_req *req;
861         struct pipeline_fc_del_msg_rsp *rsp;
862
863         uint32_t signature, bucket_id;
864
865         /* Check input arguments */
866         if ((app == NULL) ||
867                 (key == NULL))
868                 return -1;
869
870         p = app_pipeline_data_fe(app, pipeline_id, &pipeline_flow_classification);
871         if (p == NULL)
872                 return -1;
873
874         if (app_pipeline_fc_key_check(key) != 0)
875                 return -1;
876
877         /* Find rule */
878         flow = app_pipeline_fc_flow_find(p, key);
879         if (flow == NULL)
880                 return 0;
881
882         /* Allocate and write request */
883         req = app_msg_alloc(app);
884         if (req == NULL)
885                 return -1;
886
887         req->type = PIPELINE_MSG_REQ_CUSTOM;
888         req->subtype = PIPELINE_FC_MSG_REQ_FLOW_DEL;
889         app_pipeline_fc_key_convert(key, req->key, &signature);
890
891         /* Send request and wait for response */
892         rsp = app_msg_send_recv(app, pipeline_id, req, MSG_TIMEOUT_DEFAULT);
893         if (rsp == NULL)
894                 return -1;
895
896         /* Read response */
897         if (rsp->status || !rsp->key_found) {
898                 app_msg_free(app, rsp);
899                 return -1;
900         }
901
902         /* Remove rule */
903         bucket_id = signature & (N_BUCKETS - 1);
904         TAILQ_REMOVE(&p->flows[bucket_id], flow, node);
905         p->n_flows--;
906         rte_free(flow);
907
908         /* Free response */
909         app_msg_free(app, rsp);
910
911         return 0;
912 }
913
914 int
915 app_pipeline_fc_add_default(struct app_params *app,
916         uint32_t pipeline_id,
917         uint32_t port_id)
918 {
919         struct app_pipeline_fc *p;
920
921         struct pipeline_fc_add_default_msg_req *req;
922         struct pipeline_fc_add_default_msg_rsp *rsp;
923
924         /* Check input arguments */
925         if (app == NULL)
926                 return -1;
927
928         p = app_pipeline_data_fe(app, pipeline_id, &pipeline_flow_classification);
929         if (p == NULL)
930                 return -1;
931
932         if (port_id >= p->n_ports_out)
933                 return -1;
934
935         /* Allocate and write request */
936         req = app_msg_alloc(app);
937         if (req == NULL)
938                 return -1;
939
940         req->type = PIPELINE_MSG_REQ_CUSTOM;
941         req->subtype = PIPELINE_FC_MSG_REQ_FLOW_ADD_DEFAULT;
942         req->port_id = port_id;
943
944         /* Send request and wait for response */
945         rsp = app_msg_send_recv(app, pipeline_id, req, MSG_TIMEOUT_DEFAULT);
946         if (rsp == NULL)
947                 return -1;
948
949         /* Read response and write flow */
950         if (rsp->status || (rsp->entry_ptr == NULL)) {
951                 app_msg_free(app, rsp);
952                 return -1;
953         }
954
955         p->default_flow_port_id = port_id;
956         p->default_flow_entry_ptr = rsp->entry_ptr;
957
958         /* Commit route */
959         p->default_flow_present = 1;
960
961         /* Free response */
962         app_msg_free(app, rsp);
963
964         return 0;
965 }
966
967 int
968 app_pipeline_fc_del_default(struct app_params *app,
969         uint32_t pipeline_id)
970 {
971         struct app_pipeline_fc *p;
972
973         struct pipeline_fc_del_default_msg_req *req;
974         struct pipeline_fc_del_default_msg_rsp *rsp;
975
976         /* Check input arguments */
977         if (app == NULL)
978                 return -1;
979
980         p = app_pipeline_data_fe(app, pipeline_id, &pipeline_flow_classification);
981         if (p == NULL)
982                 return -EINVAL;
983
984         /* Allocate and write request */
985         req = app_msg_alloc(app);
986         if (req == NULL)
987                 return -1;
988
989         req->type = PIPELINE_MSG_REQ_CUSTOM;
990         req->subtype = PIPELINE_FC_MSG_REQ_FLOW_DEL_DEFAULT;
991
992         /* Send request and wait for response */
993         rsp = app_msg_send_recv(app, pipeline_id, req, MSG_TIMEOUT_DEFAULT);
994         if (rsp == NULL)
995                 return -1;
996
997         /* Read response */
998         if (rsp->status) {
999                 app_msg_free(app, rsp);
1000                 return -1;
1001         }
1002
1003         /* Commit route */
1004         p->default_flow_present = 0;
1005
1006         /* Free response */
1007         app_msg_free(app, rsp);
1008
1009         return 0;
1010 }
1011
1012 /*
1013  * Flow ls
1014  */
1015
1016 static void
1017 print_fc_qinq_flow(struct app_pipeline_fc_flow *flow)
1018 {
1019         printf("(SVLAN = %" PRIu32 ", "
1020                 "CVLAN = %" PRIu32 ") => "
1021                 "Port = %" PRIu32 ", "
1022                 "Flow ID = %" PRIu32 ", "
1023                 "(signature = 0x%08" PRIx32 ", "
1024                 "entry_ptr = %p)\n",
1025
1026                 flow->key.key.qinq.svlan,
1027                 flow->key.key.qinq.cvlan,
1028                 flow->port_id,
1029                 flow->flow_id,
1030                 flow->signature,
1031                 flow->entry_ptr);
1032 }
1033
1034 static void
1035 print_fc_ipv4_5tuple_flow(struct app_pipeline_fc_flow *flow)
1036 {
1037         printf("(SA = %" PRIu32 ".%" PRIu32 ".%" PRIu32 ".%" PRIu32 ", "
1038                    "DA = %" PRIu32 ".%" PRIu32 ".%" PRIu32 ".%" PRIu32 ", "
1039                    "SP = %" PRIu32 ", "
1040                    "DP = %" PRIu32 ", "
1041                    "Proto = %" PRIu32 ") => "
1042                    "Port = %" PRIu32 ", "
1043                    "Flow ID = %" PRIu32 " "
1044                    "(signature = 0x%08" PRIx32 ", "
1045                    "entry_ptr = %p)\n",
1046
1047                    (flow->key.key.ipv4_5tuple.ip_src >> 24) & 0xFF,
1048                    (flow->key.key.ipv4_5tuple.ip_src >> 16) & 0xFF,
1049                    (flow->key.key.ipv4_5tuple.ip_src >> 8) & 0xFF,
1050                    flow->key.key.ipv4_5tuple.ip_src & 0xFF,
1051
1052                    (flow->key.key.ipv4_5tuple.ip_dst >> 24) & 0xFF,
1053                    (flow->key.key.ipv4_5tuple.ip_dst >> 16) & 0xFF,
1054                    (flow->key.key.ipv4_5tuple.ip_dst >> 8) & 0xFF,
1055                    flow->key.key.ipv4_5tuple.ip_dst & 0xFF,
1056
1057                    flow->key.key.ipv4_5tuple.port_src,
1058                    flow->key.key.ipv4_5tuple.port_dst,
1059
1060                    flow->key.key.ipv4_5tuple.proto,
1061
1062                    flow->port_id,
1063                    flow->flow_id,
1064                    flow->signature,
1065                    flow->entry_ptr);
1066 }
1067
1068 static void
1069 print_fc_ipv6_5tuple_flow(struct app_pipeline_fc_flow *flow) {
1070         printf("(SA = %02" PRIx32 "%02" PRIx32 ":%02" PRIx32 "%02" PRIx32
1071                 ":%02" PRIx32 "%02" PRIx32 ":%02" PRIx32 "%02" PRIx32
1072                 ":%02" PRIx32 "%02" PRIx32 ":%02" PRIx32 "%02" PRIx32
1073                 ":%02" PRIx32 "%02" PRIx32 ":%02" PRIx32 "%02" PRIx32 ", "
1074                 "DA = %02" PRIx32 "%02" PRIx32 ":%02" PRIx32 "%02" PRIx32
1075                 ":%02" PRIx32 "%02" PRIx32 ":%02" PRIx32 "%02" PRIx32
1076                 ":%02" PRIx32 "%02" PRIx32 ":%02" PRIx32 "%02" PRIx32
1077                 ":%02" PRIx32 "%02" PRIx32 ":%02" PRIx32 "%02" PRIx32 ", "
1078                 "SP = %" PRIu32 ", "
1079                 "DP = %" PRIu32 " "
1080                 "Proto = %" PRIu32 " "
1081                 "=> Port = %" PRIu32 ", "
1082                 "Flow ID = %" PRIu32 " "
1083                 "(signature = 0x%08" PRIx32 ", "
1084                 "entry_ptr = %p)\n",
1085
1086                 flow->key.key.ipv6_5tuple.ip_src[0],
1087                 flow->key.key.ipv6_5tuple.ip_src[1],
1088                 flow->key.key.ipv6_5tuple.ip_src[2],
1089                 flow->key.key.ipv6_5tuple.ip_src[3],
1090                 flow->key.key.ipv6_5tuple.ip_src[4],
1091                 flow->key.key.ipv6_5tuple.ip_src[5],
1092                 flow->key.key.ipv6_5tuple.ip_src[6],
1093                 flow->key.key.ipv6_5tuple.ip_src[7],
1094                 flow->key.key.ipv6_5tuple.ip_src[8],
1095                 flow->key.key.ipv6_5tuple.ip_src[9],
1096                 flow->key.key.ipv6_5tuple.ip_src[10],
1097                 flow->key.key.ipv6_5tuple.ip_src[11],
1098                 flow->key.key.ipv6_5tuple.ip_src[12],
1099                 flow->key.key.ipv6_5tuple.ip_src[13],
1100                 flow->key.key.ipv6_5tuple.ip_src[14],
1101                 flow->key.key.ipv6_5tuple.ip_src[15],
1102
1103                 flow->key.key.ipv6_5tuple.ip_dst[0],
1104                 flow->key.key.ipv6_5tuple.ip_dst[1],
1105                 flow->key.key.ipv6_5tuple.ip_dst[2],
1106                 flow->key.key.ipv6_5tuple.ip_dst[3],
1107                 flow->key.key.ipv6_5tuple.ip_dst[4],
1108                 flow->key.key.ipv6_5tuple.ip_dst[5],
1109                 flow->key.key.ipv6_5tuple.ip_dst[6],
1110                 flow->key.key.ipv6_5tuple.ip_dst[7],
1111                 flow->key.key.ipv6_5tuple.ip_dst[8],
1112                 flow->key.key.ipv6_5tuple.ip_dst[9],
1113                 flow->key.key.ipv6_5tuple.ip_dst[10],
1114                 flow->key.key.ipv6_5tuple.ip_dst[11],
1115                 flow->key.key.ipv6_5tuple.ip_dst[12],
1116                 flow->key.key.ipv6_5tuple.ip_dst[13],
1117                 flow->key.key.ipv6_5tuple.ip_dst[14],
1118                 flow->key.key.ipv6_5tuple.ip_dst[15],
1119
1120                 flow->key.key.ipv6_5tuple.port_src,
1121                 flow->key.key.ipv6_5tuple.port_dst,
1122
1123                 flow->key.key.ipv6_5tuple.proto,
1124
1125                 flow->port_id,
1126                 flow->flow_id,
1127                 flow->signature,
1128                 flow->entry_ptr);
1129 }
1130
1131 static void
1132 print_fc_flow(struct app_pipeline_fc_flow *flow)
1133 {
1134         switch (flow->key.type) {
1135         case FLOW_KEY_QINQ:
1136                 print_fc_qinq_flow(flow);
1137                 break;
1138
1139         case FLOW_KEY_IPV4_5TUPLE:
1140                 print_fc_ipv4_5tuple_flow(flow);
1141                 break;
1142
1143         case FLOW_KEY_IPV6_5TUPLE:
1144                 print_fc_ipv6_5tuple_flow(flow);
1145                 break;
1146         }
1147 }
1148
1149 static int
1150 app_pipeline_fc_ls(struct app_params *app,
1151                 uint32_t pipeline_id)
1152 {
1153         struct app_pipeline_fc *p;
1154         struct app_pipeline_fc_flow *flow;
1155         uint32_t i;
1156
1157         /* Check input arguments */
1158         if (app == NULL)
1159                 return -1;
1160
1161         p = app_pipeline_data_fe(app, pipeline_id, &pipeline_flow_classification);
1162         if (p == NULL)
1163                 return -1;
1164
1165         for (i = 0; i < N_BUCKETS; i++)
1166                 TAILQ_FOREACH(flow, &p->flows[i], node)
1167                         print_fc_flow(flow);
1168
1169         if (p->default_flow_present)
1170                 printf("Default flow: port %" PRIu32 " (entry ptr = %p)\n",
1171                         p->default_flow_port_id,
1172                         p->default_flow_entry_ptr);
1173         else
1174                 printf("Default: DROP\n");
1175
1176         return 0;
1177 }
1178 /*
1179  * flow
1180  *
1181  * flow add:
1182  *    p <pipelineid> flow add qinq <svlan> <cvlan> port <portid> id <flowid>
1183  *    p <pipelineid> flow add qinq bulk <file>
1184  *    p <pipelineid> flow add ipv4 <sipaddr> <dipaddr> <sport> <dport> <proto> port <port ID> id <flowid>
1185  *    p <pipelineid> flow add ipv4 bulk <file>
1186  *    p <pipelineid> flow add ipv6 <sipaddr> <dipaddr> <sport> <dport> <proto> port <port ID> id <flowid>
1187  *    p <pipelineid> flow add ipv6 bulk <file>
1188  *
1189  * flow add default:
1190  *    p <pipelineid> flow add default <portid>
1191  *
1192  * flow del:
1193  *    p <pipelineid> flow del qinq <svlan> <cvlan>
1194  *    p <pipelineid> flow del ipv4 <sipaddr> <dipaddr> <sport> <dport> <proto>
1195  *    p <pipelineid> flow del ipv6 <sipaddr> <dipaddr> <sport> <dport> <proto>
1196  *
1197  * flow del default:
1198  *    p <pipelineid> flow del default
1199  *
1200  * flow ls:
1201  *    p <pipelineid> flow ls
1202  */
1203
1204 struct cmd_flow_result {
1205         cmdline_fixed_string_t p_string;
1206         uint32_t pipeline_id;
1207         cmdline_fixed_string_t flow_string;
1208         cmdline_multi_string_t multi_string;
1209 };
1210
1211 static void
1212 cmd_flow_parsed(void *parsed_result,
1213         __attribute__((unused)) struct cmdline *cl,
1214         void *data)
1215 {
1216         struct cmd_flow_result *results = parsed_result;
1217         struct app_params *app = data;
1218
1219         char *tokens[16];
1220         uint32_t n_tokens = RTE_DIM(tokens);
1221         int status;
1222
1223         status = parse_tokenize_string(results->multi_string, tokens, &n_tokens);
1224         if (status) {
1225                 printf(CMD_MSG_TOO_MANY_ARGS, "flow");
1226                 return;
1227         }
1228
1229         /* flow add qinq */
1230         if ((n_tokens >= 3) &&
1231                 (strcmp(tokens[0], "add") == 0) &&
1232                 (strcmp(tokens[1], "qinq") == 0) &&
1233                 strcmp(tokens[2], "bulk")) {
1234                 struct pipeline_fc_key key;
1235                 uint32_t svlan;
1236                 uint32_t cvlan;
1237                 uint32_t port_id;
1238                 uint32_t flow_id;
1239
1240                 memset(&key, 0, sizeof(key));
1241
1242                 if (n_tokens != 8) {
1243                         printf(CMD_MSG_MISMATCH_ARGS, "flow add qinq");
1244                         return;
1245                 }
1246
1247                 if (parser_read_uint32(&svlan, tokens[2]) != 0) {
1248                         printf(CMD_MSG_INVALID_ARG, "svlan");
1249                         return;
1250                 }
1251
1252                 if (parser_read_uint32(&cvlan, tokens[3]) != 0) {
1253                         printf(CMD_MSG_INVALID_ARG, "cvlan");
1254                         return;
1255                 }
1256
1257                 if (strcmp(tokens[4], "port") != 0) {
1258                         printf(CMD_MSG_ARG_NOT_FOUND, "port");
1259                         return;
1260                 }
1261
1262                 if (parser_read_uint32(&port_id, tokens[5]) != 0) {
1263                         printf(CMD_MSG_INVALID_ARG, "portid");
1264                         return;
1265                 }
1266
1267                 if (strcmp(tokens[6], "id") != 0) {
1268                         printf(CMD_MSG_ARG_NOT_FOUND, "id");
1269                         return;
1270                 }
1271
1272                 if (parser_read_uint32(&flow_id, tokens[7]) != 0) {
1273                         printf(CMD_MSG_INVALID_ARG, "flowid");
1274                         return;
1275                 }
1276
1277                 key.type = FLOW_KEY_QINQ;
1278                 key.key.qinq.svlan = svlan;
1279                 key.key.qinq.cvlan = cvlan;
1280
1281                 status = app_pipeline_fc_add(app,
1282                         results->pipeline_id,
1283                         &key,
1284                         port_id,
1285                         flow_id);
1286                 if (status)
1287                         printf(CMD_MSG_FAIL, "flow add qinq");
1288
1289                 return;
1290         } /* flow add qinq */
1291
1292         /* flow add ipv4 */
1293         if ((n_tokens >= 3) &&
1294                 (strcmp(tokens[0], "add") == 0) &&
1295                 (strcmp(tokens[1], "ipv4") == 0) &&
1296                 strcmp(tokens[2], "bulk")) {
1297                 struct pipeline_fc_key key;
1298                 struct in_addr sipaddr;
1299                 struct in_addr dipaddr;
1300                 uint32_t sport;
1301                 uint32_t dport;
1302                 uint32_t proto;
1303                 uint32_t port_id;
1304                 uint32_t flow_id;
1305
1306                 memset(&key, 0, sizeof(key));
1307
1308                 if (n_tokens != 11) {
1309                         printf(CMD_MSG_MISMATCH_ARGS, "flow add ipv4");
1310                         return;
1311                 }
1312
1313                 if (parse_ipv4_addr(tokens[2], &sipaddr) != 0) {
1314                         printf(CMD_MSG_INVALID_ARG, "sipv4addr");
1315                         return;
1316                 }
1317                 if (parse_ipv4_addr(tokens[3], &dipaddr) != 0) {
1318                         printf(CMD_MSG_INVALID_ARG, "dipv4addr");
1319                         return;
1320                 }
1321
1322                 if (parser_read_uint32(&sport, tokens[4]) != 0) {
1323                         printf(CMD_MSG_INVALID_ARG, "sport");
1324                         return;
1325                 }
1326
1327                 if (parser_read_uint32(&dport, tokens[5]) != 0) {
1328                         printf(CMD_MSG_INVALID_ARG, "dport");
1329                         return;
1330                 }
1331
1332                 if (parser_read_uint32(&proto, tokens[6]) != 0) {
1333                         printf(CMD_MSG_INVALID_ARG, "proto");
1334                         return;
1335                 }
1336
1337                 if (strcmp(tokens[7], "port") != 0) {
1338                         printf(CMD_MSG_ARG_NOT_FOUND, "port");
1339                         return;
1340                 }
1341
1342                 if (parser_read_uint32(&port_id, tokens[8]) != 0) {
1343                         printf(CMD_MSG_INVALID_ARG, "portid");
1344                         return;
1345                 }
1346
1347                 if (strcmp(tokens[9], "id") != 0) {
1348                         printf(CMD_MSG_ARG_NOT_FOUND, "id");
1349                         return;
1350                 }
1351
1352                 if (parser_read_uint32(&flow_id, tokens[10]) != 0) {
1353                         printf(CMD_MSG_INVALID_ARG, "flowid");
1354                         return;
1355                 }
1356
1357                 key.type = FLOW_KEY_IPV4_5TUPLE;
1358                 key.key.ipv4_5tuple.ip_src = rte_be_to_cpu_32(sipaddr.s_addr);
1359                 key.key.ipv4_5tuple.ip_dst = rte_be_to_cpu_32(dipaddr.s_addr);
1360                 key.key.ipv4_5tuple.port_src = sport;
1361                 key.key.ipv4_5tuple.port_dst = dport;
1362                 key.key.ipv4_5tuple.proto = proto;
1363
1364                 status = app_pipeline_fc_add(app,
1365                         results->pipeline_id,
1366                         &key,
1367                         port_id,
1368                         flow_id);
1369                 if (status)
1370                         printf(CMD_MSG_FAIL, "flow add ipv4");
1371
1372                 return;
1373         } /* flow add ipv4 */
1374
1375         /* flow add ipv6 */
1376         if ((n_tokens >= 3) &&
1377                 (strcmp(tokens[0], "add") == 0) &&
1378                 (strcmp(tokens[1], "ipv6") == 0) &&
1379                 strcmp(tokens[2], "bulk")) {
1380                 struct pipeline_fc_key key;
1381                 struct in6_addr sipaddr;
1382                 struct in6_addr dipaddr;
1383                 uint32_t sport;
1384                 uint32_t dport;
1385                 uint32_t proto;
1386                 uint32_t port_id;
1387                 uint32_t flow_id;
1388
1389                 memset(&key, 0, sizeof(key));
1390
1391                 if (n_tokens != 11) {
1392                         printf(CMD_MSG_MISMATCH_ARGS, "flow add ipv6");
1393                         return;
1394                 }
1395
1396                 if (parse_ipv6_addr(tokens[2], &sipaddr) != 0) {
1397                         printf(CMD_MSG_INVALID_ARG, "sipv6addr");
1398                         return;
1399                 }
1400                 if (parse_ipv6_addr(tokens[3], &dipaddr) != 0) {
1401                         printf(CMD_MSG_INVALID_ARG, "dipv6addr");
1402                         return;
1403                 }
1404
1405                 if (parser_read_uint32(&sport, tokens[4]) != 0) {
1406                         printf(CMD_MSG_INVALID_ARG, "sport");
1407                         return;
1408                 }
1409
1410                 if (parser_read_uint32(&dport, tokens[5]) != 0) {
1411                         printf(CMD_MSG_INVALID_ARG, "dport");
1412                         return;
1413                 }
1414
1415                 if (parser_read_uint32(&proto, tokens[6]) != 0) {
1416                         printf(CMD_MSG_INVALID_ARG, "proto");
1417                         return;
1418                 }
1419
1420                 if (strcmp(tokens[7], "port") != 0) {
1421                         printf(CMD_MSG_ARG_NOT_FOUND, "port");
1422                         return;
1423                 }
1424
1425                 if (parser_read_uint32(&port_id, tokens[8]) != 0) {
1426                         printf(CMD_MSG_INVALID_ARG, "portid");
1427                         return;
1428                 }
1429
1430                 if (strcmp(tokens[9], "id") != 0) {
1431                         printf(CMD_MSG_ARG_NOT_FOUND, "id");
1432                         return;
1433                 }
1434
1435                 if (parser_read_uint32(&flow_id, tokens[10]) != 0) {
1436                         printf(CMD_MSG_INVALID_ARG, "flowid");
1437                         return;
1438                 }
1439
1440                 key.type = FLOW_KEY_IPV6_5TUPLE;
1441                 memcpy(key.key.ipv6_5tuple.ip_src, (void *)&sipaddr, 16);
1442                 memcpy(key.key.ipv6_5tuple.ip_dst, (void *)&dipaddr, 16);
1443                 key.key.ipv6_5tuple.port_src = sport;
1444                 key.key.ipv6_5tuple.port_dst = dport;
1445                 key.key.ipv6_5tuple.proto = proto;
1446
1447                 status = app_pipeline_fc_add(app,
1448                         results->pipeline_id,
1449                         &key,
1450                         port_id,
1451                         flow_id);
1452                 if (status)
1453                         printf(CMD_MSG_FAIL, "flow add ipv6");
1454
1455                 return;
1456         } /* flow add ipv6 */
1457
1458         /* flow add qinq bulk */
1459         if ((n_tokens >= 3) &&
1460                 (strcmp(tokens[0], "add") == 0) &&
1461                 (strcmp(tokens[1], "qinq") == 0) &&
1462                 (strcmp(tokens[2], "bulk") == 0)) {
1463                 struct pipeline_fc_key *keys;
1464                 uint32_t *port_ids, *flow_ids, n_keys, line;
1465                 char *filename;
1466
1467                 if (n_tokens != 4) {
1468                         printf(CMD_MSG_MISMATCH_ARGS, "flow add qinq bulk");
1469                         return;
1470                 }
1471
1472                 filename = tokens[3];
1473
1474                 n_keys = APP_PIPELINE_FC_MAX_FLOWS_IN_FILE;
1475                 keys = malloc(n_keys * sizeof(struct pipeline_fc_key));
1476                 if (keys == NULL)
1477                         return;
1478                 memset(keys, 0, n_keys * sizeof(struct pipeline_fc_key));
1479
1480                 port_ids = malloc(n_keys * sizeof(uint32_t));
1481                 if (port_ids == NULL) {
1482                         free(keys);
1483                         return;
1484                 }
1485
1486                 flow_ids = malloc(n_keys * sizeof(uint32_t));
1487                 if (flow_ids == NULL) {
1488                         free(port_ids);
1489                         free(keys);
1490                         return;
1491                 }
1492
1493                 status = app_pipeline_fc_load_file_qinq(filename,
1494                         keys,
1495                         port_ids,
1496                         flow_ids,
1497                         &n_keys,
1498                         &line);
1499                 if (status != 0) {
1500                         printf(CMD_MSG_FILE_ERR, filename, line);
1501                         free(flow_ids);
1502                         free(port_ids);
1503                         free(keys);
1504                         return;
1505                 }
1506
1507                 status = app_pipeline_fc_add_bulk(app,
1508                         results->pipeline_id,
1509                         keys,
1510                         port_ids,
1511                         flow_ids,
1512                         n_keys);
1513                 if (status)
1514                         printf(CMD_MSG_FAIL, "flow add qinq bulk");
1515
1516                 free(flow_ids);
1517                 free(port_ids);
1518                 free(keys);
1519                 return;
1520         } /* flow add qinq bulk */
1521
1522         /* flow add ipv4 bulk */
1523         if ((n_tokens >= 3) &&
1524                 (strcmp(tokens[0], "add") == 0) &&
1525                 (strcmp(tokens[1], "ipv4") == 0) &&
1526                 (strcmp(tokens[2], "bulk") == 0)) {
1527                 struct pipeline_fc_key *keys;
1528                 uint32_t *port_ids, *flow_ids, n_keys, line;
1529                 char *filename;
1530
1531                 if (n_tokens != 4) {
1532                         printf(CMD_MSG_MISMATCH_ARGS, "flow add ipv4 bulk");
1533                         return;
1534                 }
1535
1536                 filename = tokens[3];
1537
1538                 n_keys = APP_PIPELINE_FC_MAX_FLOWS_IN_FILE;
1539                 keys = malloc(n_keys * sizeof(struct pipeline_fc_key));
1540                 if (keys == NULL)
1541                         return;
1542                 memset(keys, 0, n_keys * sizeof(struct pipeline_fc_key));
1543
1544                 port_ids = malloc(n_keys * sizeof(uint32_t));
1545                 if (port_ids == NULL) {
1546                         free(keys);
1547                         return;
1548                 }
1549
1550                 flow_ids = malloc(n_keys * sizeof(uint32_t));
1551                 if (flow_ids == NULL) {
1552                         free(port_ids);
1553                         free(keys);
1554                         return;
1555                 }
1556
1557                 status = app_pipeline_fc_load_file_ipv4(filename,
1558                         keys,
1559                         port_ids,
1560                         flow_ids,
1561                         &n_keys,
1562                         &line);
1563                 if (status != 0) {
1564                         printf(CMD_MSG_FILE_ERR, filename, line);
1565                         free(flow_ids);
1566                         free(port_ids);
1567                         free(keys);
1568                         return;
1569                 }
1570
1571                 status = app_pipeline_fc_add_bulk(app,
1572                         results->pipeline_id,
1573                         keys,
1574                         port_ids,
1575                         flow_ids,
1576                         n_keys);
1577                 if (status)
1578                         printf(CMD_MSG_FAIL, "flow add ipv4 bulk");
1579
1580                 free(flow_ids);
1581                 free(port_ids);
1582                 free(keys);
1583                 return;
1584         } /* flow add ipv4 bulk */
1585
1586         /* flow add ipv6 bulk */
1587         if ((n_tokens >= 3) &&
1588                 (strcmp(tokens[0], "add") == 0) &&
1589                 (strcmp(tokens[1], "ipv6") == 0) &&
1590                 (strcmp(tokens[2], "bulk") == 0)) {
1591                 struct pipeline_fc_key *keys;
1592                 uint32_t *port_ids, *flow_ids, n_keys, line;
1593                 char *filename;
1594
1595                 if (n_tokens != 4) {
1596                         printf(CMD_MSG_MISMATCH_ARGS, "flow add ipv6 bulk");
1597                         return;
1598                 }
1599
1600                 filename = tokens[3];
1601
1602                 n_keys = APP_PIPELINE_FC_MAX_FLOWS_IN_FILE;
1603                 keys = malloc(n_keys * sizeof(struct pipeline_fc_key));
1604                 if (keys == NULL)
1605                         return;
1606                 memset(keys, 0, n_keys * sizeof(struct pipeline_fc_key));
1607
1608                 port_ids = malloc(n_keys * sizeof(uint32_t));
1609                 if (port_ids == NULL) {
1610                         free(keys);
1611                         return;
1612                 }
1613
1614                 flow_ids = malloc(n_keys * sizeof(uint32_t));
1615                 if (flow_ids == NULL) {
1616                         free(port_ids);
1617                         free(keys);
1618                         return;
1619                 }
1620
1621                 status = app_pipeline_fc_load_file_ipv6(filename,
1622                         keys,
1623                         port_ids,
1624                         flow_ids,
1625                         &n_keys,
1626                         &line);
1627                 if (status != 0) {
1628                         printf(CMD_MSG_FILE_ERR, filename, line);
1629                         free(flow_ids);
1630                         free(port_ids);
1631                         free(keys);
1632                         return;
1633                 }
1634
1635                 status = app_pipeline_fc_add_bulk(app,
1636                         results->pipeline_id,
1637                         keys,
1638                         port_ids,
1639                         flow_ids,
1640                         n_keys);
1641                 if (status)
1642                         printf(CMD_MSG_FAIL, "flow add ipv6 bulk");
1643
1644                 free(flow_ids);
1645                 free(port_ids);
1646                 free(keys);
1647                 return;
1648         } /* flow add ipv6 bulk */
1649
1650         /* flow add default*/
1651         if ((n_tokens >= 2) &&
1652                 (strcmp(tokens[0], "add") == 0) &&
1653                 (strcmp(tokens[1], "default") == 0)) {
1654                 uint32_t port_id;
1655
1656                 if (n_tokens != 3) {
1657                         printf(CMD_MSG_MISMATCH_ARGS, "flow add default");
1658                         return;
1659                 }
1660
1661                 if (parser_read_uint32(&port_id, tokens[2]) != 0) {
1662                         printf(CMD_MSG_INVALID_ARG, "portid");
1663                         return;
1664                 }
1665
1666                 status = app_pipeline_fc_add_default(app,
1667                         results->pipeline_id,
1668                         port_id);
1669                 if (status)
1670                         printf(CMD_MSG_FAIL, "flow add default");
1671
1672                 return;
1673         } /* flow add default */
1674
1675         /* flow del qinq */
1676         if ((n_tokens >= 2) &&
1677                 (strcmp(tokens[0], "del") == 0) &&
1678                 (strcmp(tokens[1], "qinq") == 0)) {
1679                 struct pipeline_fc_key key;
1680                 uint32_t svlan;
1681                 uint32_t cvlan;
1682
1683                 memset(&key, 0, sizeof(key));
1684
1685                 if (n_tokens != 4) {
1686                         printf(CMD_MSG_MISMATCH_ARGS, "flow del qinq");
1687                         return;
1688                 }
1689
1690                 if (parser_read_uint32(&svlan, tokens[2]) != 0) {
1691                         printf(CMD_MSG_INVALID_ARG, "svlan");
1692                         return;
1693                 }
1694
1695                 if (parser_read_uint32(&cvlan, tokens[3]) != 0) {
1696                         printf(CMD_MSG_INVALID_ARG, "cvlan");
1697                         return;
1698                 }
1699
1700                 key.type = FLOW_KEY_QINQ;
1701                 key.key.qinq.svlan = svlan;
1702                 key.key.qinq.cvlan = cvlan;
1703
1704                 status = app_pipeline_fc_del(app,
1705                         results->pipeline_id,
1706                         &key);
1707                 if (status)
1708                         printf(CMD_MSG_FAIL, "flow del qinq");
1709
1710                 return;
1711         } /* flow del qinq */
1712
1713         /* flow del ipv4 */
1714         if ((n_tokens >= 2) &&
1715                 (strcmp(tokens[0], "del") == 0) &&
1716                 (strcmp(tokens[1], "ipv4") == 0)) {
1717                 struct pipeline_fc_key key;
1718                 struct in_addr sipaddr;
1719                 struct in_addr dipaddr;
1720                 uint32_t sport;
1721                 uint32_t dport;
1722                 uint32_t proto;
1723
1724                 memset(&key, 0, sizeof(key));
1725
1726                 if (n_tokens != 7) {
1727                         printf(CMD_MSG_MISMATCH_ARGS, "flow del ipv4");
1728                         return;
1729                 }
1730
1731                 if (parse_ipv4_addr(tokens[2], &sipaddr) != 0) {
1732                         printf(CMD_MSG_INVALID_ARG, "sipv4addr");
1733                         return;
1734                 }
1735                 if (parse_ipv4_addr(tokens[3], &dipaddr) != 0) {
1736                         printf(CMD_MSG_INVALID_ARG, "dipv4addr");
1737                         return;
1738                 }
1739
1740                 if (parser_read_uint32(&sport, tokens[4]) != 0) {
1741                         printf(CMD_MSG_INVALID_ARG, "sport");
1742                         return;
1743                 }
1744
1745                 if (parser_read_uint32(&dport, tokens[5]) != 0) {
1746                         printf(CMD_MSG_INVALID_ARG, "dport");
1747                         return;
1748                 }
1749
1750                 if (parser_read_uint32(&proto, tokens[6]) != 0) {
1751                         printf(CMD_MSG_INVALID_ARG, "proto");
1752                         return;
1753                 }
1754
1755                 key.type = FLOW_KEY_IPV4_5TUPLE;
1756                 key.key.ipv4_5tuple.ip_src = rte_be_to_cpu_32(sipaddr.s_addr);
1757                 key.key.ipv4_5tuple.ip_dst = rte_be_to_cpu_32(dipaddr.s_addr);
1758                 key.key.ipv4_5tuple.port_src = sport;
1759                 key.key.ipv4_5tuple.port_dst = dport;
1760                 key.key.ipv4_5tuple.proto = proto;
1761
1762                 status = app_pipeline_fc_del(app,
1763                         results->pipeline_id,
1764                         &key);
1765                 if (status)
1766                         printf(CMD_MSG_FAIL, "flow del ipv4");
1767
1768                 return;
1769         } /* flow del ipv4 */
1770
1771         /* flow del ipv6 */
1772         if ((n_tokens >= 2) &&
1773                 (strcmp(tokens[0], "del") == 0) &&
1774                 (strcmp(tokens[1], "ipv6") == 0)) {
1775                 struct pipeline_fc_key key;
1776                 struct in6_addr sipaddr;
1777                 struct in6_addr dipaddr;
1778                 uint32_t sport;
1779                 uint32_t dport;
1780                 uint32_t proto;
1781
1782                 memset(&key, 0, sizeof(key));
1783
1784                 if (n_tokens != 7) {
1785                         printf(CMD_MSG_MISMATCH_ARGS, "flow del ipv6");
1786                         return;
1787                 }
1788
1789                 if (parse_ipv6_addr(tokens[2], &sipaddr) != 0) {
1790                         printf(CMD_MSG_INVALID_ARG, "sipv6addr");
1791                         return;
1792                 }
1793
1794                 if (parse_ipv6_addr(tokens[3], &dipaddr) != 0) {
1795                         printf(CMD_MSG_INVALID_ARG, "dipv6addr");
1796                         return;
1797                 }
1798
1799                 if (parser_read_uint32(&sport, tokens[4]) != 0) {
1800                         printf(CMD_MSG_INVALID_ARG, "sport");
1801                         return;
1802                 }
1803
1804                 if (parser_read_uint32(&dport, tokens[5]) != 0) {
1805                         printf(CMD_MSG_INVALID_ARG, "dport");
1806                         return;
1807                 }
1808
1809                 if (parser_read_uint32(&proto, tokens[6]) != 0) {
1810                         printf(CMD_MSG_INVALID_ARG, "proto");
1811                         return;
1812                 }
1813
1814                 key.type = FLOW_KEY_IPV6_5TUPLE;
1815                 memcpy(key.key.ipv6_5tuple.ip_src, &sipaddr, 16);
1816                 memcpy(key.key.ipv6_5tuple.ip_dst, &dipaddr, 16);
1817                 key.key.ipv6_5tuple.port_src = sport;
1818                 key.key.ipv6_5tuple.port_dst = dport;
1819                 key.key.ipv6_5tuple.proto = proto;
1820
1821                 status = app_pipeline_fc_del(app,
1822                         results->pipeline_id,
1823                         &key);
1824                 if (status)
1825                         printf(CMD_MSG_FAIL, "flow del ipv6");
1826
1827                 return;
1828         } /* flow del ipv6 */
1829
1830         /* flow del default*/
1831         if ((n_tokens >= 2) &&
1832                 (strcmp(tokens[0], "del") == 0) &&
1833                 (strcmp(tokens[1], "default") == 0)) {
1834                 if (n_tokens != 2) {
1835                         printf(CMD_MSG_MISMATCH_ARGS, "flow del default");
1836                         return;
1837                 }
1838
1839                 status = app_pipeline_fc_del_default(app,
1840                         results->pipeline_id);
1841                 if (status)
1842                         printf(CMD_MSG_FAIL, "flow del default");
1843
1844                 return;
1845         } /* flow del default */
1846
1847         /* flow ls */
1848         if ((n_tokens >= 1) && (strcmp(tokens[0], "ls") == 0)) {
1849                 if (n_tokens != 1) {
1850                         printf(CMD_MSG_MISMATCH_ARGS, "flow ls");
1851                         return;
1852                 }
1853
1854                 status = app_pipeline_fc_ls(app, results->pipeline_id);
1855                 if (status)
1856                         printf(CMD_MSG_FAIL, "flow ls");
1857
1858                 return;
1859         } /* flow ls */
1860
1861         printf(CMD_MSG_MISMATCH_ARGS, "flow");
1862 }
1863
1864 static cmdline_parse_token_string_t cmd_flow_p_string =
1865         TOKEN_STRING_INITIALIZER(struct cmd_flow_result, p_string, "p");
1866
1867 static cmdline_parse_token_num_t cmd_flow_pipeline_id =
1868         TOKEN_NUM_INITIALIZER(struct cmd_flow_result, pipeline_id, UINT32);
1869
1870 static cmdline_parse_token_string_t cmd_flow_flow_string =
1871         TOKEN_STRING_INITIALIZER(struct cmd_flow_result, flow_string, "flow");
1872
1873 static cmdline_parse_token_string_t cmd_flow_multi_string =
1874         TOKEN_STRING_INITIALIZER(struct cmd_flow_result, multi_string,
1875                 TOKEN_STRING_MULTI);
1876
1877 static cmdline_parse_inst_t cmd_flow = {
1878         .f = cmd_flow_parsed,
1879         .data = NULL,
1880         .help_str = "flow add / add bulk / add default / del / del default / ls",
1881         .tokens = {
1882                 (void *) &cmd_flow_p_string,
1883                 (void *) &cmd_flow_pipeline_id,
1884                 (void *) &cmd_flow_flow_string,
1885                 (void *) &cmd_flow_multi_string,
1886                 NULL,
1887         },
1888 };
1889
1890 static cmdline_parse_ctx_t pipeline_cmds[] = {
1891         (cmdline_parse_inst_t *) &cmd_flow,
1892         NULL,
1893 };
1894
1895 static struct pipeline_fe_ops pipeline_flow_classification_fe_ops = {
1896         .f_init = app_pipeline_fc_init,
1897         .f_post_init = NULL,
1898         .f_free = app_pipeline_fc_free,
1899         .f_track = app_pipeline_track_default,
1900         .cmds = pipeline_cmds,
1901 };
1902
1903 struct pipeline_type pipeline_flow_classification = {
1904         .name = "FLOW_CLASSIFICATION",
1905         .be_ops = &pipeline_flow_classification_be_ops,
1906         .fe_ops = &pipeline_flow_classification_fe_ops,
1907 };