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