1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
10 #include <rte_ethdev.h>
11 #include <rte_cycles.h>
12 #include <rte_lcore.h>
15 #include <rte_flow_classify.h>
16 #include <rte_table_acl.h>
18 #define RX_RING_SIZE 1024
19 #define TX_RING_SIZE 1024
21 #define NUM_MBUFS 8191
22 #define MBUF_CACHE_SIZE 250
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
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__)
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);\
58 const char *rule_ipv4_name;
60 const char cb_port_delim[] = ":";
62 /* Ethernet ports configured with default settings using struct. 8< */
63 static const struct rte_eth_conf port_conf_default = {
65 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
68 /* >8 End of configuration of Ethernet ports. */
70 /* Creation of flow classifier object. 8< */
71 struct flow_classifier {
72 struct rte_flow_classifier *cls;
75 struct flow_classifier_acl {
76 struct flow_classifier cls;
77 } __rte_cache_aligned;
78 /* >8 End of creation of flow classifier object. */
80 /* Creation of ACL table during initialization of application. 8< */
82 /* ACL field definitions for IPv4 5 tuple rule */
99 static struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
100 /* first input field - always one byte long. */
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),
109 /* next input field (IPv4 source address) - 4 consecutive bytes. */
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),
119 /* next input field (IPv4 destination address) - 4 consecutive bytes. */
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),
130 * Next 2 fields (src & dst ports) form 4 consecutive bytes.
131 * They share the same input index.
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),
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),
154 /* >8 End of creation of ACL table. */
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
163 /* >8 End of flow classify data. */
165 /* parameters for rte_flow_classify_validate and
166 * rte_flow_classify_table_entry_add functions
169 static struct rte_flow_item eth_item = { RTE_FLOW_ITEM_TYPE_ETH,
171 static struct rte_flow_item end_item = { RTE_FLOW_ITEM_TYPE_END,
175 * "actions count / end"
177 struct rte_flow_query_count count = {
184 static struct rte_flow_action count_action = { RTE_FLOW_ACTION_TYPE_COUNT,
186 static struct rte_flow_action end_action = { RTE_FLOW_ACTION_TYPE_END, 0};
187 static struct rte_flow_action actions[2];
189 /* sample attributes */
190 static struct rte_flow_attr attr;
192 /* flow_classify.c: * Based on DPDK skeleton forwarding example. */
195 * Initializes a given port using global settings and with the RX buffers
196 * coming from the mbuf_pool passed as a parameter.
199 /* Initializing port using global settings. 8< */
201 port_init(uint8_t port, struct rte_mempool *mbuf_pool)
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;
208 struct rte_eth_dev_info dev_info;
209 struct rte_eth_txconf txconf;
211 if (!rte_eth_dev_is_valid_port(port))
214 retval = rte_eth_dev_info_get(port, &dev_info);
216 printf("Error during getting device (port %u) info: %s\n",
217 port, strerror(-retval));
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;
225 /* Configure the Ethernet device. */
226 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
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);
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);
248 /* Start the Ethernet port. 8< */
249 retval = rte_eth_dev_start(port);
250 /* >8 End of starting the Ethernet port. */
254 /* Display the port MAC address. */
255 retval = rte_eth_macaddr_get(port, &addr);
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));
263 /* Enable RX in promiscuous mode for the Ethernet device. */
264 retval = rte_eth_promiscuous_enable(port);
270 /* >8 End of initializing a given port. */
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.
277 /* Classifying the packets. 8< */
278 static __rte_noreturn void
279 lcore_main(struct flow_classifier *cls_app)
285 ret = rte_flow_classify_table_entry_delete(cls_app->cls,
288 printf("table_entry_delete failed [7] %d\n\n", ret);
290 printf("table_entry_delete succeeded [7]\n\n");
293 * Check that the port is on the same NUMA node as the polling thread
294 * for best performance.
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()) {
300 printf("WARNING: port %u is on remote NUMA node\n",
302 printf("to polling thread.\n");
303 printf("Performance will not be optimal.\n");
305 printf("\nCore %u forwarding packets. ", rte_lcore_id());
306 printf("[Ctrl+C to quit]\n");
308 /* Run until the application is quit or killed. 8< */
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.
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,
321 if (unlikely(nb_rx == 0))
324 for (i = 0; i < MAX_NUM_CLASSIFY; i++) {
326 ret = rte_flow_classifier_query(
328 bufs, nb_rx, rules[i],
332 "rule [%d] query failed ret [%d]\n\n",
336 "rule[%d] count=%"PRIu64"\n",
337 i, ntuple_stats.counter1);
339 printf("proto = %d\n",
340 ntuple_stats.ipv4_5tuple.proto);
345 /* Send burst of TX packets, to second port of pair. */
346 const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
349 /* Free any unsent packets. */
350 if (unlikely(nb_tx < nb_rx)) {
353 for (buf = nb_tx; buf < nb_rx; buf++)
354 rte_pktmbuf_free(bufs[buf]);
358 /* >8 End of main loop. */
360 /* >8 End of lcore main. */
363 * Parse IPv4 5 tuple rules file, ipv4_rules_file.txt.
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> \
374 get_cb_field(char **in, uint32_t *fd, int base, unsigned long lim,
381 val = strtoul(*in, &end, base);
382 if (errno != 0 || end[0] != dlm || val > lim)
390 parse_ipv4_net(char *in, uint32_t *addr, uint32_t *mask_len)
392 uint32_t a, b, c, d, m;
394 if (get_cb_field(&in, &a, 0, UINT8_MAX, '.'))
396 if (get_cb_field(&in, &b, 0, UINT8_MAX, '.'))
398 if (get_cb_field(&in, &c, 0, UINT8_MAX, '.'))
400 if (get_cb_field(&in, &d, 0, UINT8_MAX, '/'))
402 if (get_cb_field(&in, &m, 0, sizeof(uint32_t) * CHAR_BIT, 0))
405 addr[0] = RTE_IPV4(a, b, c, d);
411 parse_ipv4_5tuple_rule(char *str, struct rte_eth_ntuple_filter *ntuple_filter)
414 char *s, *sp, *in[CB_FLD_NUM];
415 static const char *dlm = " \t\n";
416 int dim = CB_FLD_NUM;
420 for (i = 0; i != dim; i++, s = NULL) {
421 in[i] = strtok_r(s, dlm, &sp);
426 ret = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
427 &ntuple_filter->src_ip,
428 &ntuple_filter->src_ip_mask);
430 flow_classify_log("failed to read source address/mask: %s\n",
431 in[CB_FLD_SRC_ADDR]);
435 ret = parse_ipv4_net(in[CB_FLD_DST_ADDR],
436 &ntuple_filter->dst_ip,
437 &ntuple_filter->dst_ip_mask);
439 flow_classify_log("failed to read source address/mask: %s\n",
440 in[CB_FLD_DST_ADDR]);
444 if (get_cb_field(&in[CB_FLD_SRC_PORT], &temp, 0, UINT16_MAX, 0))
446 ntuple_filter->src_port = (uint16_t)temp;
448 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
449 sizeof(cb_port_delim)) != 0)
452 if (get_cb_field(&in[CB_FLD_SRC_PORT_MASK], &temp, 0, UINT16_MAX, 0))
454 ntuple_filter->src_port_mask = (uint16_t)temp;
456 if (get_cb_field(&in[CB_FLD_DST_PORT], &temp, 0, UINT16_MAX, 0))
458 ntuple_filter->dst_port = (uint16_t)temp;
460 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
461 sizeof(cb_port_delim)) != 0)
464 if (get_cb_field(&in[CB_FLD_DST_PORT_MASK], &temp, 0, UINT16_MAX, 0))
466 ntuple_filter->dst_port_mask = (uint16_t)temp;
468 if (get_cb_field(&in[CB_FLD_PROTO], &temp, 0, UINT8_MAX, '/'))
470 ntuple_filter->proto = (uint8_t)temp;
472 if (get_cb_field(&in[CB_FLD_PROTO], &temp, 0, UINT8_MAX, 0))
474 ntuple_filter->proto_mask = (uint8_t)temp;
476 if (get_cb_field(&in[CB_FLD_PRIORITY], &temp, 0, UINT16_MAX, 0))
478 ntuple_filter->priority = (uint16_t)temp;
479 if (ntuple_filter->priority > FLOW_CLASSIFY_MAX_PRIORITY)
485 /* Bypass comment and empty lines */
487 is_bypass_line(char *buff)
492 if (buff[0] == COMMENT_LEAD_CHAR)
495 while (buff[i] != '\0') {
496 if (!isspace(buff[i]))
504 convert_depth_to_bitmask(uint32_t depth_val)
506 uint32_t bitmask = 0;
509 for (i = depth_val, j = 0; i > 0; i--, j++)
510 bitmask |= (1 << (31 - j));
515 add_classify_rule(struct rte_eth_ntuple_filter *ntuple_filter,
516 struct flow_classifier *cls_app)
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;
539 if (num_classify_rules >= MAX_NUM_CLASSIFY) {
541 "\nINFO: classify rule capacity %d reached\n",
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;
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);
562 switch (ipv4_proto) {
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;
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;
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;
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;
584 attr.priority = ntuple_filter->priority;
585 pattern_ipv4_5tuple[1] = ipv4_udp_item;
586 pattern_ipv4_5tuple[2] = udp_item;
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;
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;
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;
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;
607 attr.priority = ntuple_filter->priority;
608 pattern_ipv4_5tuple[1] = ipv4_tcp_item;
609 pattern_ipv4_5tuple[2] = tcp_item;
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;
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;
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;
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;
632 attr.priority = ntuple_filter->priority;
633 pattern_ipv4_5tuple[1] = ipv4_sctp_item;
634 pattern_ipv4_5tuple[2] = sctp_item;
641 pattern_ipv4_5tuple[0] = eth_item;
642 pattern_ipv4_5tuple[3] = end_item;
643 actions[0] = count_action;
644 actions[1] = end_action;
646 /* Validate and add rule */
647 ret = rte_flow_classify_validate(cls_app->cls, &attr,
648 pattern_ipv4_5tuple, actions, &error);
650 printf("table entry validate failed ipv4_proto = %u\n",
655 rule = rte_flow_classify_table_entry_add(
656 cls_app->cls, &attr, pattern_ipv4_5tuple,
657 actions, &key_found, &error);
659 printf("table entry add failed ipv4_proto = %u\n",
665 rules[num_classify_rules] = rule;
666 num_classify_rules++;
670 /* Reads file and calls the add_classify_rule function. 8< */
672 add_rules(const char *rule_path, struct flow_classifier *cls_app)
677 unsigned int total_num = 0;
678 struct rte_eth_ntuple_filter ntuple_filter;
681 fh = fopen(rule_path, "rb");
683 rte_exit(EXIT_FAILURE, "%s: fopen %s failed\n", __func__,
686 ret = fseek(fh, 0, SEEK_SET);
688 rte_exit(EXIT_FAILURE, "%s: fseek %d failed\n", __func__,
692 while (fgets(buff, LINE_MAX, fh) != NULL) {
695 if (is_bypass_line(buff))
698 if (total_num >= FLOW_CLASSIFY_MAX_RULE_NUM - 1) {
699 printf("\nINFO: classify rule capacity %d reached\n",
704 if (parse_ipv4_5tuple_rule(buff, &ntuple_filter) != 0)
705 rte_exit(EXIT_FAILURE,
706 "%s Line %u: parse rules error\n",
709 if (add_classify_rule(&ntuple_filter, cls_app) != 0)
710 rte_exit(EXIT_FAILURE, "add rule error\n");
718 /* >8 End of add_rules. */
722 print_usage(const char *prgname)
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");
730 /* Parse the argument given in the command line of the application */
732 parse_args(int argc, char **argv)
737 char *prgname = argv[0];
738 static struct option lgopts[] = {
739 {OPTION_RULE_IPV4, 1, 0, 0},
745 while ((opt = getopt_long(argc, argvopt, "",
746 lgopts, &option_index)) != EOF) {
751 if (!strncmp(lgopts[option_index].name,
753 sizeof(OPTION_RULE_IPV4)))
754 parm_config.rule_ipv4_name = optarg;
757 print_usage(prgname);
763 argv[optind-1] = prgname;
766 optind = 1; /* reset getopt lib */
771 * The main function, which does initialization and calls the lcore_main
775 main(int argc, char *argv[])
777 struct rte_mempool *mbuf_pool;
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;
788 /* Initialize the Environment Abstraction Layer (EAL). 8< */
789 ret = rte_eal_init(argc, argv);
791 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
792 /* >8 End of initialization of EAL. */
797 /* Parse application arguments (after the EAL ones). 8< */
798 ret = parse_args(argc, argv);
800 rte_exit(EXIT_FAILURE, "Invalid flow_classify parameters\n");
801 /* >8 End of parse application arguments. */
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");
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. */
813 if (mbuf_pool == NULL)
814 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
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",
821 /* >8 End of initialization of all ports. */
823 if (rte_lcore_count() > 1)
824 printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
826 socket_id = rte_eth_dev_socket_id(0);
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);
832 rte_exit(EXIT_FAILURE, "Cannot allocate classifier memory\n");
834 cls_params.name = "flow_classifier";
835 cls_params.socket_id = socket_id;
837 cls_app->cls = rte_flow_classifier_create(&cls_params);
838 if (cls_app->cls == NULL) {
840 rte_exit(EXIT_FAILURE, "Cannot create classifier\n");
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));
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;
854 ret = rte_flow_classify_table_create(cls_app->cls, &cls_table_params);
856 rte_flow_classifier_free(cls_app->cls);
858 rte_exit(EXIT_FAILURE, "Failed to create classifier table\n");
860 /* >8 End of initialization of table create params. */
862 /* read file of IPv4 5 tuple rules and initialize parameters
863 * for rte_flow_classify_validate and rte_flow_classify_table_entry_add
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);
871 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
873 /* >8 End of reading file of IPv4 5 tuple rules. */
875 /* Call lcore_main on the main core only. */
878 /* clean up the EAL */