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