flow_classify: remove table id parameter from API
[dpdk.git] / examples / flow_classify / flow_classify.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <inttypes.h>
7 #include <getopt.h>
8
9 #include <rte_eal.h>
10 #include <rte_ethdev.h>
11 #include <rte_cycles.h>
12 #include <rte_lcore.h>
13 #include <rte_mbuf.h>
14 #include <rte_flow.h>
15 #include <rte_flow_classify.h>
16 #include <rte_table_acl.h>
17
18 #define RX_RING_SIZE 128
19 #define TX_RING_SIZE 512
20
21 #define NUM_MBUFS 8191
22 #define MBUF_CACHE_SIZE 250
23 #define BURST_SIZE 32
24
25 #define MAX_NUM_CLASSIFY 30
26 #define FLOW_CLASSIFY_MAX_RULE_NUM 91
27 #define FLOW_CLASSIFY_MAX_PRIORITY 8
28 #define FLOW_CLASSIFIER_NAME_SIZE 64
29
30 #define COMMENT_LEAD_CHAR       ('#')
31 #define OPTION_RULE_IPV4        "rule_ipv4"
32 #define RTE_LOGTYPE_FLOW_CLASSIFY       RTE_LOGTYPE_USER3
33 #define flow_classify_log(format, ...) \
34                 RTE_LOG(ERR, FLOW_CLASSIFY, format, ##__VA_ARGS__)
35
36 #define uint32_t_to_char(ip, a, b, c, d) do {\
37                 *a = (unsigned char)(ip >> 24 & 0xff);\
38                 *b = (unsigned char)(ip >> 16 & 0xff);\
39                 *c = (unsigned char)(ip >> 8 & 0xff);\
40                 *d = (unsigned char)(ip & 0xff);\
41         } while (0)
42
43 enum {
44         CB_FLD_SRC_ADDR,
45         CB_FLD_DST_ADDR,
46         CB_FLD_SRC_PORT,
47         CB_FLD_SRC_PORT_DLM,
48         CB_FLD_SRC_PORT_MASK,
49         CB_FLD_DST_PORT,
50         CB_FLD_DST_PORT_DLM,
51         CB_FLD_DST_PORT_MASK,
52         CB_FLD_PROTO,
53         CB_FLD_PRIORITY,
54         CB_FLD_NUM,
55 };
56
57 static struct{
58         const char *rule_ipv4_name;
59 } parm_config;
60 const char cb_port_delim[] = ":";
61
62 static const struct rte_eth_conf port_conf_default = {
63         .rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN }
64 };
65
66 struct flow_classifier {
67         struct rte_flow_classifier *cls;
68 };
69
70 struct flow_classifier_acl {
71         struct flow_classifier cls;
72 } __rte_cache_aligned;
73
74 /* ACL field definitions for IPv4 5 tuple rule */
75
76 enum {
77         PROTO_FIELD_IPV4,
78         SRC_FIELD_IPV4,
79         DST_FIELD_IPV4,
80         SRCP_FIELD_IPV4,
81         DSTP_FIELD_IPV4,
82         NUM_FIELDS_IPV4
83 };
84
85 enum {
86         PROTO_INPUT_IPV4,
87         SRC_INPUT_IPV4,
88         DST_INPUT_IPV4,
89         SRCP_DESTP_INPUT_IPV4
90 };
91
92 static struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
93         /* first input field - always one byte long. */
94         {
95                 .type = RTE_ACL_FIELD_TYPE_BITMASK,
96                 .size = sizeof(uint8_t),
97                 .field_index = PROTO_FIELD_IPV4,
98                 .input_index = PROTO_INPUT_IPV4,
99                 .offset = sizeof(struct ether_hdr) +
100                         offsetof(struct ipv4_hdr, next_proto_id),
101         },
102         /* next input field (IPv4 source address) - 4 consecutive bytes. */
103         {
104                 /* rte_flow uses a bit mask for IPv4 addresses */
105                 .type = RTE_ACL_FIELD_TYPE_BITMASK,
106                 .size = sizeof(uint32_t),
107                 .field_index = SRC_FIELD_IPV4,
108                 .input_index = SRC_INPUT_IPV4,
109                 .offset = sizeof(struct ether_hdr) +
110                         offsetof(struct ipv4_hdr, src_addr),
111         },
112         /* next input field (IPv4 destination address) - 4 consecutive bytes. */
113         {
114                 /* rte_flow uses a bit mask for IPv4 addresses */
115                 .type = RTE_ACL_FIELD_TYPE_BITMASK,
116                 .size = sizeof(uint32_t),
117                 .field_index = DST_FIELD_IPV4,
118                 .input_index = DST_INPUT_IPV4,
119                 .offset = sizeof(struct ether_hdr) +
120                         offsetof(struct ipv4_hdr, dst_addr),
121         },
122         /*
123          * Next 2 fields (src & dst ports) form 4 consecutive bytes.
124          * They share the same input index.
125          */
126         {
127                 /* rte_flow uses a bit mask for protocol ports */
128                 .type = RTE_ACL_FIELD_TYPE_BITMASK,
129                 .size = sizeof(uint16_t),
130                 .field_index = SRCP_FIELD_IPV4,
131                 .input_index = SRCP_DESTP_INPUT_IPV4,
132                 .offset = sizeof(struct ether_hdr) +
133                         sizeof(struct ipv4_hdr) +
134                         offsetof(struct tcp_hdr, src_port),
135         },
136         {
137                 /* rte_flow uses a bit mask for protocol ports */
138                 .type = RTE_ACL_FIELD_TYPE_BITMASK,
139                 .size = sizeof(uint16_t),
140                 .field_index = DSTP_FIELD_IPV4,
141                 .input_index = SRCP_DESTP_INPUT_IPV4,
142                 .offset = sizeof(struct ether_hdr) +
143                         sizeof(struct ipv4_hdr) +
144                         offsetof(struct tcp_hdr, dst_port),
145         },
146 };
147
148 /* flow classify data */
149 static int num_classify_rules;
150 static struct rte_flow_classify_rule *rules[MAX_NUM_CLASSIFY];
151 static struct rte_flow_classify_ipv4_5tuple_stats ntuple_stats;
152 static struct rte_flow_classify_stats classify_stats = {
153                 .stats = (void **)&ntuple_stats
154 };
155
156 /* parameters for rte_flow_classify_validate and
157  * rte_flow_classify_table_entry_add functions
158  */
159
160 static struct rte_flow_item  eth_item = { RTE_FLOW_ITEM_TYPE_ETH,
161         0, 0, 0 };
162 static struct rte_flow_item  end_item = { RTE_FLOW_ITEM_TYPE_END,
163         0, 0, 0 };
164
165 /* sample actions:
166  * "actions count / end"
167  */
168 struct rte_flow_query_count count = {
169         .reset = 1,
170         .hits_set = 1,
171         .bytes_set = 1,
172         .hits = 0,
173         .bytes = 0,
174 };
175 static struct rte_flow_action count_action = { RTE_FLOW_ACTION_TYPE_COUNT,
176         &count};
177 static struct rte_flow_action end_action = { RTE_FLOW_ACTION_TYPE_END, 0};
178 static struct rte_flow_action actions[2];
179
180 /* sample attributes */
181 static struct rte_flow_attr attr;
182
183 /* flow_classify.c: * Based on DPDK skeleton forwarding example. */
184
185 /*
186  * Initializes a given port using global settings and with the RX buffers
187  * coming from the mbuf_pool passed as a parameter.
188  */
189 static inline int
190 port_init(uint8_t port, struct rte_mempool *mbuf_pool)
191 {
192         struct rte_eth_conf port_conf = port_conf_default;
193         struct ether_addr addr;
194         const uint16_t rx_rings = 1, tx_rings = 1;
195         int retval;
196         uint16_t q;
197
198         if (port >= rte_eth_dev_count())
199                 return -1;
200
201         /* Configure the Ethernet device. */
202         retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
203         if (retval != 0)
204                 return retval;
205
206         /* Allocate and set up 1 RX queue per Ethernet port. */
207         for (q = 0; q < rx_rings; q++) {
208                 retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
209                                 rte_eth_dev_socket_id(port), NULL, mbuf_pool);
210                 if (retval < 0)
211                         return retval;
212         }
213
214         /* Allocate and set up 1 TX queue per Ethernet port. */
215         for (q = 0; q < tx_rings; q++) {
216                 retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE,
217                                 rte_eth_dev_socket_id(port), NULL);
218                 if (retval < 0)
219                         return retval;
220         }
221
222         /* Start the Ethernet port. */
223         retval = rte_eth_dev_start(port);
224         if (retval < 0)
225                 return retval;
226
227         /* Display the port MAC address. */
228         rte_eth_macaddr_get(port, &addr);
229         printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
230                            " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
231                         port,
232                         addr.addr_bytes[0], addr.addr_bytes[1],
233                         addr.addr_bytes[2], addr.addr_bytes[3],
234                         addr.addr_bytes[4], addr.addr_bytes[5]);
235
236         /* Enable RX in promiscuous mode for the Ethernet device. */
237         rte_eth_promiscuous_enable(port);
238
239         return 0;
240 }
241
242 /*
243  * The lcore main. This is the main thread that does the work, reading from
244  * an input port classifying the packets and writing to an output port.
245  */
246 static __attribute__((noreturn)) void
247 lcore_main(struct flow_classifier *cls_app)
248 {
249         const uint8_t nb_ports = rte_eth_dev_count();
250         uint8_t port;
251         int ret;
252         int i = 0;
253
254         ret = rte_flow_classify_table_entry_delete(cls_app->cls,
255                         rules[7]);
256         if (ret)
257                 printf("table_entry_delete failed [7] %d\n\n", ret);
258         else
259                 printf("table_entry_delete succeeded [7]\n\n");
260
261         /*
262          * Check that the port is on the same NUMA node as the polling thread
263          * for best performance.
264          */
265         for (port = 0; port < nb_ports; port++)
266                 if (rte_eth_dev_socket_id(port) > 0 &&
267                         rte_eth_dev_socket_id(port) != (int)rte_socket_id()) {
268                         printf("\n\n");
269                         printf("WARNING: port %u is on remote NUMA node\n",
270                                port);
271                         printf("to polling thread.\n");
272                         printf("Performance will not be optimal.\n");
273                 }
274         printf("\nCore %u forwarding packets. ", rte_lcore_id());
275         printf("[Ctrl+C to quit]\n");
276
277         /* Run until the application is quit or killed. */
278         for (;;) {
279                 /*
280                  * Receive packets on a port, classify them and forward them
281                  * on the paired port.
282                  * The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc.
283                  */
284                 for (port = 0; port < nb_ports; port++) {
285                         /* Get burst of RX packets, from first port of pair. */
286                         struct rte_mbuf *bufs[BURST_SIZE];
287                         const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
288                                         bufs, BURST_SIZE);
289
290                         if (unlikely(nb_rx == 0))
291                                 continue;
292
293                         for (i = 0; i < MAX_NUM_CLASSIFY; i++) {
294                                 if (rules[i]) {
295                                         ret = rte_flow_classifier_query(
296                                                 cls_app->cls,
297                                                 bufs, nb_rx, rules[i],
298                                                 &classify_stats);
299                                         if (ret)
300                                                 printf(
301                                                         "rule [%d] query failed ret [%d]\n\n",
302                                                         i, ret);
303                                         else {
304                                                 printf(
305                                                 "rule[%d] count=%"PRIu64"\n",
306                                                 i, ntuple_stats.counter1);
307
308                                                 printf("proto = %d\n",
309                                                 ntuple_stats.ipv4_5tuple.proto);
310                                         }
311                                 }
312                         }
313
314                         /* Send burst of TX packets, to second port of pair. */
315                         const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
316                                         bufs, nb_rx);
317
318                         /* Free any unsent packets. */
319                         if (unlikely(nb_tx < nb_rx)) {
320                                 uint16_t buf;
321
322                                 for (buf = nb_tx; buf < nb_rx; buf++)
323                                         rte_pktmbuf_free(bufs[buf]);
324                         }
325                 }
326         }
327 }
328
329 /*
330  * Parse IPv4 5 tuple rules file, ipv4_rules_file.txt.
331  * Expected format:
332  * <src_ipv4_addr>'/'<masklen> <space> \
333  * <dst_ipv4_addr>'/'<masklen> <space> \
334  * <src_port> <space> ":" <src_port_mask> <space> \
335  * <dst_port> <space> ":" <dst_port_mask> <space> \
336  * <proto>'/'<proto_mask> <space> \
337  * <priority>
338  */
339
340 static int
341 get_cb_field(char **in, uint32_t *fd, int base, unsigned long lim,
342                 char dlm)
343 {
344         unsigned long val;
345         char *end;
346
347         errno = 0;
348         val = strtoul(*in, &end, base);
349         if (errno != 0 || end[0] != dlm || val > lim)
350                 return -EINVAL;
351         *fd = (uint32_t)val;
352         *in = end + 1;
353         return 0;
354 }
355
356 static int
357 parse_ipv4_net(char *in, uint32_t *addr, uint32_t *mask_len)
358 {
359         uint32_t a, b, c, d, m;
360
361         if (get_cb_field(&in, &a, 0, UINT8_MAX, '.'))
362                 return -EINVAL;
363         if (get_cb_field(&in, &b, 0, UINT8_MAX, '.'))
364                 return -EINVAL;
365         if (get_cb_field(&in, &c, 0, UINT8_MAX, '.'))
366                 return -EINVAL;
367         if (get_cb_field(&in, &d, 0, UINT8_MAX, '/'))
368                 return -EINVAL;
369         if (get_cb_field(&in, &m, 0, sizeof(uint32_t) * CHAR_BIT, 0))
370                 return -EINVAL;
371
372         addr[0] = IPv4(a, b, c, d);
373         mask_len[0] = m;
374         return 0;
375 }
376
377 static int
378 parse_ipv4_5tuple_rule(char *str, struct rte_eth_ntuple_filter *ntuple_filter)
379 {
380         int i, ret;
381         char *s, *sp, *in[CB_FLD_NUM];
382         static const char *dlm = " \t\n";
383         int dim = CB_FLD_NUM;
384         uint32_t temp;
385
386         s = str;
387         for (i = 0; i != dim; i++, s = NULL) {
388                 in[i] = strtok_r(s, dlm, &sp);
389                 if (in[i] == NULL)
390                         return -EINVAL;
391         }
392
393         ret = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
394                         &ntuple_filter->src_ip,
395                         &ntuple_filter->src_ip_mask);
396         if (ret != 0) {
397                 flow_classify_log("failed to read source address/mask: %s\n",
398                         in[CB_FLD_SRC_ADDR]);
399                 return ret;
400         }
401
402         ret = parse_ipv4_net(in[CB_FLD_DST_ADDR],
403                         &ntuple_filter->dst_ip,
404                         &ntuple_filter->dst_ip_mask);
405         if (ret != 0) {
406                 flow_classify_log("failed to read source address/mask: %s\n",
407                         in[CB_FLD_DST_ADDR]);
408                 return ret;
409         }
410
411         if (get_cb_field(&in[CB_FLD_SRC_PORT], &temp, 0, UINT16_MAX, 0))
412                 return -EINVAL;
413         ntuple_filter->src_port = (uint16_t)temp;
414
415         if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
416                         sizeof(cb_port_delim)) != 0)
417                 return -EINVAL;
418
419         if (get_cb_field(&in[CB_FLD_SRC_PORT_MASK], &temp, 0, UINT16_MAX, 0))
420                 return -EINVAL;
421         ntuple_filter->src_port_mask = (uint16_t)temp;
422
423         if (get_cb_field(&in[CB_FLD_DST_PORT], &temp, 0, UINT16_MAX, 0))
424                 return -EINVAL;
425         ntuple_filter->dst_port = (uint16_t)temp;
426
427         if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
428                         sizeof(cb_port_delim)) != 0)
429                 return -EINVAL;
430
431         if (get_cb_field(&in[CB_FLD_DST_PORT_MASK], &temp, 0, UINT16_MAX, 0))
432                 return -EINVAL;
433         ntuple_filter->dst_port_mask = (uint16_t)temp;
434
435         if (get_cb_field(&in[CB_FLD_PROTO], &temp, 0, UINT8_MAX, '/'))
436                 return -EINVAL;
437         ntuple_filter->proto = (uint8_t)temp;
438
439         if (get_cb_field(&in[CB_FLD_PROTO], &temp, 0, UINT8_MAX, 0))
440                 return -EINVAL;
441         ntuple_filter->proto_mask = (uint8_t)temp;
442
443         if (get_cb_field(&in[CB_FLD_PRIORITY], &temp, 0, UINT16_MAX, 0))
444                 return -EINVAL;
445         ntuple_filter->priority = (uint16_t)temp;
446         if (ntuple_filter->priority > FLOW_CLASSIFY_MAX_PRIORITY)
447                 ret = -EINVAL;
448
449         return ret;
450 }
451
452 /* Bypass comment and empty lines */
453 static inline int
454 is_bypass_line(char *buff)
455 {
456         int i = 0;
457
458         /* comment line */
459         if (buff[0] == COMMENT_LEAD_CHAR)
460                 return 1;
461         /* empty line */
462         while (buff[i] != '\0') {
463                 if (!isspace(buff[i]))
464                         return 0;
465                 i++;
466         }
467         return 1;
468 }
469
470 static uint32_t
471 convert_depth_to_bitmask(uint32_t depth_val)
472 {
473         uint32_t bitmask = 0;
474         int i, j;
475
476         for (i = depth_val, j = 0; i > 0; i--, j++)
477                 bitmask |= (1 << (31 - j));
478         return bitmask;
479 }
480
481 static int
482 add_classify_rule(struct rte_eth_ntuple_filter *ntuple_filter,
483                 struct flow_classifier *cls_app)
484 {
485         int ret = -1;
486         int key_found;
487         struct rte_flow_error error;
488         struct rte_flow_item_ipv4 ipv4_spec;
489         struct rte_flow_item_ipv4 ipv4_mask;
490         struct rte_flow_item ipv4_udp_item;
491         struct rte_flow_item ipv4_tcp_item;
492         struct rte_flow_item ipv4_sctp_item;
493         struct rte_flow_item_udp udp_spec;
494         struct rte_flow_item_udp udp_mask;
495         struct rte_flow_item udp_item;
496         struct rte_flow_item_tcp tcp_spec;
497         struct rte_flow_item_tcp tcp_mask;
498         struct rte_flow_item tcp_item;
499         struct rte_flow_item_sctp sctp_spec;
500         struct rte_flow_item_sctp sctp_mask;
501         struct rte_flow_item sctp_item;
502         struct rte_flow_item pattern_ipv4_5tuple[4];
503         struct rte_flow_classify_rule *rule;
504         uint8_t ipv4_proto;
505
506         if (num_classify_rules >= MAX_NUM_CLASSIFY) {
507                 printf(
508                         "\nINFO:  classify rule capacity %d reached\n",
509                         num_classify_rules);
510                 return ret;
511         }
512
513         /* set up parameters for validate and add */
514         memset(&ipv4_spec, 0, sizeof(ipv4_spec));
515         ipv4_spec.hdr.next_proto_id = ntuple_filter->proto;
516         ipv4_spec.hdr.src_addr = ntuple_filter->src_ip;
517         ipv4_spec.hdr.dst_addr = ntuple_filter->dst_ip;
518         ipv4_proto = ipv4_spec.hdr.next_proto_id;
519
520         memset(&ipv4_mask, 0, sizeof(ipv4_mask));
521         ipv4_mask.hdr.next_proto_id = ntuple_filter->proto_mask;
522         ipv4_mask.hdr.src_addr = ntuple_filter->src_ip_mask;
523         ipv4_mask.hdr.src_addr =
524                 convert_depth_to_bitmask(ipv4_mask.hdr.src_addr);
525         ipv4_mask.hdr.dst_addr = ntuple_filter->dst_ip_mask;
526         ipv4_mask.hdr.dst_addr =
527                 convert_depth_to_bitmask(ipv4_mask.hdr.dst_addr);
528
529         switch (ipv4_proto) {
530         case IPPROTO_UDP:
531                 ipv4_udp_item.type = RTE_FLOW_ITEM_TYPE_IPV4;
532                 ipv4_udp_item.spec = &ipv4_spec;
533                 ipv4_udp_item.mask = &ipv4_mask;
534                 ipv4_udp_item.last = NULL;
535
536                 udp_spec.hdr.src_port = ntuple_filter->src_port;
537                 udp_spec.hdr.dst_port = ntuple_filter->dst_port;
538                 udp_spec.hdr.dgram_len = 0;
539                 udp_spec.hdr.dgram_cksum = 0;
540
541                 udp_mask.hdr.src_port = ntuple_filter->src_port_mask;
542                 udp_mask.hdr.dst_port = ntuple_filter->dst_port_mask;
543                 udp_mask.hdr.dgram_len = 0;
544                 udp_mask.hdr.dgram_cksum = 0;
545
546                 udp_item.type = RTE_FLOW_ITEM_TYPE_UDP;
547                 udp_item.spec = &udp_spec;
548                 udp_item.mask = &udp_mask;
549                 udp_item.last = NULL;
550
551                 attr.priority = ntuple_filter->priority;
552                 pattern_ipv4_5tuple[1] = ipv4_udp_item;
553                 pattern_ipv4_5tuple[2] = udp_item;
554                 break;
555         case IPPROTO_TCP:
556                 ipv4_tcp_item.type = RTE_FLOW_ITEM_TYPE_IPV4;
557                 ipv4_tcp_item.spec = &ipv4_spec;
558                 ipv4_tcp_item.mask = &ipv4_mask;
559                 ipv4_tcp_item.last = NULL;
560
561                 memset(&tcp_spec, 0, sizeof(tcp_spec));
562                 tcp_spec.hdr.src_port = ntuple_filter->src_port;
563                 tcp_spec.hdr.dst_port = ntuple_filter->dst_port;
564
565                 memset(&tcp_mask, 0, sizeof(tcp_mask));
566                 tcp_mask.hdr.src_port = ntuple_filter->src_port_mask;
567                 tcp_mask.hdr.dst_port = ntuple_filter->dst_port_mask;
568
569                 tcp_item.type = RTE_FLOW_ITEM_TYPE_TCP;
570                 tcp_item.spec = &tcp_spec;
571                 tcp_item.mask = &tcp_mask;
572                 tcp_item.last = NULL;
573
574                 attr.priority = ntuple_filter->priority;
575                 pattern_ipv4_5tuple[1] = ipv4_tcp_item;
576                 pattern_ipv4_5tuple[2] = tcp_item;
577                 break;
578         case IPPROTO_SCTP:
579                 ipv4_sctp_item.type = RTE_FLOW_ITEM_TYPE_IPV4;
580                 ipv4_sctp_item.spec = &ipv4_spec;
581                 ipv4_sctp_item.mask = &ipv4_mask;
582                 ipv4_sctp_item.last = NULL;
583
584                 sctp_spec.hdr.src_port = ntuple_filter->src_port;
585                 sctp_spec.hdr.dst_port = ntuple_filter->dst_port;
586                 sctp_spec.hdr.cksum = 0;
587                 sctp_spec.hdr.tag = 0;
588
589                 sctp_mask.hdr.src_port = ntuple_filter->src_port_mask;
590                 sctp_mask.hdr.dst_port = ntuple_filter->dst_port_mask;
591                 sctp_mask.hdr.cksum = 0;
592                 sctp_mask.hdr.tag = 0;
593
594                 sctp_item.type = RTE_FLOW_ITEM_TYPE_SCTP;
595                 sctp_item.spec = &sctp_spec;
596                 sctp_item.mask = &sctp_mask;
597                 sctp_item.last = NULL;
598
599                 attr.priority = ntuple_filter->priority;
600                 pattern_ipv4_5tuple[1] = ipv4_sctp_item;
601                 pattern_ipv4_5tuple[2] = sctp_item;
602                 break;
603         default:
604                 return ret;
605         }
606
607         attr.ingress = 1;
608         pattern_ipv4_5tuple[0] = eth_item;
609         pattern_ipv4_5tuple[3] = end_item;
610         actions[0] = count_action;
611         actions[1] = end_action;
612
613         /* Validate and add rule */
614         ret = rte_flow_classify_validate(cls_app->cls, &attr,
615                         pattern_ipv4_5tuple, actions, &error);
616         if (ret) {
617                 printf("table entry validate failed ipv4_proto = %u\n",
618                         ipv4_proto);
619                 return ret;
620         }
621
622         rule = rte_flow_classify_table_entry_add(
623                         cls_app->cls, &attr, pattern_ipv4_5tuple,
624                         actions, &key_found, &error);
625         if (rule == NULL) {
626                 printf("table entry add failed ipv4_proto = %u\n",
627                         ipv4_proto);
628                 ret = -1;
629                 return ret;
630         }
631
632         rules[num_classify_rules] = rule;
633         num_classify_rules++;
634         return 0;
635 }
636
637 static int
638 add_rules(const char *rule_path, struct flow_classifier *cls_app)
639 {
640         FILE *fh;
641         char buff[LINE_MAX];
642         unsigned int i = 0;
643         unsigned int total_num = 0;
644         struct rte_eth_ntuple_filter ntuple_filter;
645         int ret;
646
647         fh = fopen(rule_path, "rb");
648         if (fh == NULL)
649                 rte_exit(EXIT_FAILURE, "%s: fopen %s failed\n", __func__,
650                         rule_path);
651
652         ret = fseek(fh, 0, SEEK_SET);
653         if (ret)
654                 rte_exit(EXIT_FAILURE, "%s: fseek %d failed\n", __func__,
655                         ret);
656
657         i = 0;
658         while (fgets(buff, LINE_MAX, fh) != NULL) {
659                 i++;
660
661                 if (is_bypass_line(buff))
662                         continue;
663
664                 if (total_num >= FLOW_CLASSIFY_MAX_RULE_NUM - 1) {
665                         printf("\nINFO: classify rule capacity %d reached\n",
666                                 total_num);
667                         break;
668                 }
669
670                 if (parse_ipv4_5tuple_rule(buff, &ntuple_filter) != 0)
671                         rte_exit(EXIT_FAILURE,
672                                 "%s Line %u: parse rules error\n",
673                                 rule_path, i);
674
675                 if (add_classify_rule(&ntuple_filter, cls_app) != 0)
676                         rte_exit(EXIT_FAILURE, "add rule error\n");
677
678                 total_num++;
679         }
680
681         fclose(fh);
682         return 0;
683 }
684
685 /* display usage */
686 static void
687 print_usage(const char *prgname)
688 {
689         printf("%s usage:\n", prgname);
690         printf("[EAL options] --  --"OPTION_RULE_IPV4"=FILE: ");
691         printf("specify the ipv4 rules file.\n");
692         printf("Each rule occupies one line in the file.\n");
693 }
694
695 /* Parse the argument given in the command line of the application */
696 static int
697 parse_args(int argc, char **argv)
698 {
699         int opt, ret;
700         char **argvopt;
701         int option_index;
702         char *prgname = argv[0];
703         static struct option lgopts[] = {
704                 {OPTION_RULE_IPV4, 1, 0, 0},
705                 {NULL, 0, 0, 0}
706         };
707
708         argvopt = argv;
709
710         while ((opt = getopt_long(argc, argvopt, "",
711                                 lgopts, &option_index)) != EOF) {
712
713                 switch (opt) {
714                 /* long options */
715                 case 0:
716                         if (!strncmp(lgopts[option_index].name,
717                                         OPTION_RULE_IPV4,
718                                         sizeof(OPTION_RULE_IPV4)))
719                                 parm_config.rule_ipv4_name = optarg;
720                         break;
721                 default:
722                         print_usage(prgname);
723                         return -1;
724                 }
725         }
726
727         if (optind >= 0)
728                 argv[optind-1] = prgname;
729
730         ret = optind-1;
731         optind = 1; /* reset getopt lib */
732         return ret;
733 }
734
735 /*
736  * The main function, which does initialization and calls the lcore_main
737  * function.
738  */
739 int
740 main(int argc, char *argv[])
741 {
742         struct rte_mempool *mbuf_pool;
743         uint8_t nb_ports;
744         uint8_t portid;
745         int ret;
746         int socket_id;
747         struct rte_table_acl_params table_acl_params;
748         struct rte_flow_classify_table_params cls_table_params;
749         struct flow_classifier *cls_app;
750         struct rte_flow_classifier_params cls_params;
751         uint32_t size;
752
753         /* Initialize the Environment Abstraction Layer (EAL). */
754         ret = rte_eal_init(argc, argv);
755         if (ret < 0)
756                 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
757
758         argc -= ret;
759         argv += ret;
760
761         /* parse application arguments (after the EAL ones) */
762         ret = parse_args(argc, argv);
763         if (ret < 0)
764                 rte_exit(EXIT_FAILURE, "Invalid flow_classify parameters\n");
765
766         /* Check that there is an even number of ports to send/receive on. */
767         nb_ports = rte_eth_dev_count();
768         if (nb_ports < 2 || (nb_ports & 1))
769                 rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
770
771         /* Creates a new mempool in memory to hold the mbufs. */
772         mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
773                 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
774
775         if (mbuf_pool == NULL)
776                 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
777
778         /* Initialize all ports. */
779         for (portid = 0; portid < nb_ports; portid++)
780                 if (port_init(portid, mbuf_pool) != 0)
781                         rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8 "\n",
782                                         portid);
783
784         if (rte_lcore_count() > 1)
785                 printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
786
787         socket_id = rte_eth_dev_socket_id(0);
788
789         /* Memory allocation */
790         size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct flow_classifier_acl));
791         cls_app = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
792         if (cls_app == NULL)
793                 rte_exit(EXIT_FAILURE, "Cannot allocate classifier memory\n");
794
795         cls_params.name = "flow_classifier";
796         cls_params.socket_id = socket_id;
797
798         cls_app->cls = rte_flow_classifier_create(&cls_params);
799         if (cls_app->cls == NULL) {
800                 rte_free(cls_app);
801                 rte_exit(EXIT_FAILURE, "Cannot create classifier\n");
802         }
803
804         /* initialise ACL table params */
805         table_acl_params.name = "table_acl_ipv4_5tuple";
806         table_acl_params.n_rules = FLOW_CLASSIFY_MAX_RULE_NUM;
807         table_acl_params.n_rule_fields = RTE_DIM(ipv4_defs);
808         memcpy(table_acl_params.field_format, ipv4_defs, sizeof(ipv4_defs));
809
810         /* initialise table create params */
811         cls_table_params.ops = &rte_table_acl_ops;
812         cls_table_params.arg_create = &table_acl_params;
813         cls_table_params.type = RTE_FLOW_CLASSIFY_TABLE_ACL_IP4_5TUPLE;
814
815         ret = rte_flow_classify_table_create(cls_app->cls, &cls_table_params);
816         if (ret) {
817                 rte_flow_classifier_free(cls_app->cls);
818                 rte_free(cls_app);
819                 rte_exit(EXIT_FAILURE, "Failed to create classifier table\n");
820         }
821
822         /* read file of IPv4 5 tuple rules and initialize parameters
823          * for rte_flow_classify_validate and rte_flow_classify_table_entry_add
824          * API's.
825          */
826         if (add_rules(parm_config.rule_ipv4_name, cls_app)) {
827                 rte_flow_classifier_free(cls_app->cls);
828                 rte_free(cls_app);
829                 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
830         }
831
832         /* Call lcore_main on the master core only. */
833         lcore_main(cls_app);
834
835         return 0;
836 }