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",
262 addr.addr_bytes[0], addr.addr_bytes[1],
263 addr.addr_bytes[2], addr.addr_bytes[3],
264 addr.addr_bytes[4], addr.addr_bytes[5]);
266 /* Enable RX in promiscuous mode for the Ethernet device. */
267 retval = rte_eth_promiscuous_enable(port);
273 /* >8 End of initializing a given port. */
276 * The lcore main. This is the main thread that does the work, reading from
277 * an input port classifying the packets and writing to an output port.
280 /* Classifying the packets. 8< */
281 static __rte_noreturn void
282 lcore_main(struct flow_classifier *cls_app)
288 ret = rte_flow_classify_table_entry_delete(cls_app->cls,
291 printf("table_entry_delete failed [7] %d\n\n", ret);
293 printf("table_entry_delete succeeded [7]\n\n");
296 * Check that the port is on the same NUMA node as the polling thread
297 * for best performance.
299 RTE_ETH_FOREACH_DEV(port)
300 if (rte_eth_dev_socket_id(port) >= 0 &&
301 rte_eth_dev_socket_id(port) != (int)rte_socket_id()) {
303 printf("WARNING: port %u is on remote NUMA node\n",
305 printf("to polling thread.\n");
306 printf("Performance will not be optimal.\n");
308 printf("\nCore %u forwarding packets. ", rte_lcore_id());
309 printf("[Ctrl+C to quit]\n");
311 /* Run until the application is quit or killed. 8< */
314 * Receive packets on a port, classify them and forward them
315 * on the paired port.
316 * The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc.
318 RTE_ETH_FOREACH_DEV(port) {
319 /* Get burst of RX packets, from first port of pair. */
320 struct rte_mbuf *bufs[BURST_SIZE];
321 const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
324 if (unlikely(nb_rx == 0))
327 for (i = 0; i < MAX_NUM_CLASSIFY; i++) {
329 ret = rte_flow_classifier_query(
331 bufs, nb_rx, rules[i],
335 "rule [%d] query failed ret [%d]\n\n",
339 "rule[%d] count=%"PRIu64"\n",
340 i, ntuple_stats.counter1);
342 printf("proto = %d\n",
343 ntuple_stats.ipv4_5tuple.proto);
348 /* Send burst of TX packets, to second port of pair. */
349 const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
352 /* Free any unsent packets. */
353 if (unlikely(nb_tx < nb_rx)) {
356 for (buf = nb_tx; buf < nb_rx; buf++)
357 rte_pktmbuf_free(bufs[buf]);
361 /* >8 End of main loop. */
363 /* >8 End of lcore main. */
366 * Parse IPv4 5 tuple rules file, ipv4_rules_file.txt.
368 * <src_ipv4_addr>'/'<masklen> <space> \
369 * <dst_ipv4_addr>'/'<masklen> <space> \
370 * <src_port> <space> ":" <src_port_mask> <space> \
371 * <dst_port> <space> ":" <dst_port_mask> <space> \
372 * <proto>'/'<proto_mask> <space> \
377 get_cb_field(char **in, uint32_t *fd, int base, unsigned long lim,
384 val = strtoul(*in, &end, base);
385 if (errno != 0 || end[0] != dlm || val > lim)
393 parse_ipv4_net(char *in, uint32_t *addr, uint32_t *mask_len)
395 uint32_t a, b, c, d, m;
397 if (get_cb_field(&in, &a, 0, UINT8_MAX, '.'))
399 if (get_cb_field(&in, &b, 0, UINT8_MAX, '.'))
401 if (get_cb_field(&in, &c, 0, UINT8_MAX, '.'))
403 if (get_cb_field(&in, &d, 0, UINT8_MAX, '/'))
405 if (get_cb_field(&in, &m, 0, sizeof(uint32_t) * CHAR_BIT, 0))
408 addr[0] = RTE_IPV4(a, b, c, d);
414 parse_ipv4_5tuple_rule(char *str, struct rte_eth_ntuple_filter *ntuple_filter)
417 char *s, *sp, *in[CB_FLD_NUM];
418 static const char *dlm = " \t\n";
419 int dim = CB_FLD_NUM;
423 for (i = 0; i != dim; i++, s = NULL) {
424 in[i] = strtok_r(s, dlm, &sp);
429 ret = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
430 &ntuple_filter->src_ip,
431 &ntuple_filter->src_ip_mask);
433 flow_classify_log("failed to read source address/mask: %s\n",
434 in[CB_FLD_SRC_ADDR]);
438 ret = parse_ipv4_net(in[CB_FLD_DST_ADDR],
439 &ntuple_filter->dst_ip,
440 &ntuple_filter->dst_ip_mask);
442 flow_classify_log("failed to read source address/mask: %s\n",
443 in[CB_FLD_DST_ADDR]);
447 if (get_cb_field(&in[CB_FLD_SRC_PORT], &temp, 0, UINT16_MAX, 0))
449 ntuple_filter->src_port = (uint16_t)temp;
451 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
452 sizeof(cb_port_delim)) != 0)
455 if (get_cb_field(&in[CB_FLD_SRC_PORT_MASK], &temp, 0, UINT16_MAX, 0))
457 ntuple_filter->src_port_mask = (uint16_t)temp;
459 if (get_cb_field(&in[CB_FLD_DST_PORT], &temp, 0, UINT16_MAX, 0))
461 ntuple_filter->dst_port = (uint16_t)temp;
463 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
464 sizeof(cb_port_delim)) != 0)
467 if (get_cb_field(&in[CB_FLD_DST_PORT_MASK], &temp, 0, UINT16_MAX, 0))
469 ntuple_filter->dst_port_mask = (uint16_t)temp;
471 if (get_cb_field(&in[CB_FLD_PROTO], &temp, 0, UINT8_MAX, '/'))
473 ntuple_filter->proto = (uint8_t)temp;
475 if (get_cb_field(&in[CB_FLD_PROTO], &temp, 0, UINT8_MAX, 0))
477 ntuple_filter->proto_mask = (uint8_t)temp;
479 if (get_cb_field(&in[CB_FLD_PRIORITY], &temp, 0, UINT16_MAX, 0))
481 ntuple_filter->priority = (uint16_t)temp;
482 if (ntuple_filter->priority > FLOW_CLASSIFY_MAX_PRIORITY)
488 /* Bypass comment and empty lines */
490 is_bypass_line(char *buff)
495 if (buff[0] == COMMENT_LEAD_CHAR)
498 while (buff[i] != '\0') {
499 if (!isspace(buff[i]))
507 convert_depth_to_bitmask(uint32_t depth_val)
509 uint32_t bitmask = 0;
512 for (i = depth_val, j = 0; i > 0; i--, j++)
513 bitmask |= (1 << (31 - j));
518 add_classify_rule(struct rte_eth_ntuple_filter *ntuple_filter,
519 struct flow_classifier *cls_app)
523 struct rte_flow_error error;
524 struct rte_flow_item_ipv4 ipv4_spec;
525 struct rte_flow_item_ipv4 ipv4_mask;
526 struct rte_flow_item ipv4_udp_item;
527 struct rte_flow_item ipv4_tcp_item;
528 struct rte_flow_item ipv4_sctp_item;
529 struct rte_flow_item_udp udp_spec;
530 struct rte_flow_item_udp udp_mask;
531 struct rte_flow_item udp_item;
532 struct rte_flow_item_tcp tcp_spec;
533 struct rte_flow_item_tcp tcp_mask;
534 struct rte_flow_item tcp_item;
535 struct rte_flow_item_sctp sctp_spec;
536 struct rte_flow_item_sctp sctp_mask;
537 struct rte_flow_item sctp_item;
538 struct rte_flow_item pattern_ipv4_5tuple[4];
539 struct rte_flow_classify_rule *rule;
542 if (num_classify_rules >= MAX_NUM_CLASSIFY) {
544 "\nINFO: classify rule capacity %d reached\n",
549 /* set up parameters for validate and add */
550 memset(&ipv4_spec, 0, sizeof(ipv4_spec));
551 ipv4_spec.hdr.next_proto_id = ntuple_filter->proto;
552 ipv4_spec.hdr.src_addr = ntuple_filter->src_ip;
553 ipv4_spec.hdr.dst_addr = ntuple_filter->dst_ip;
554 ipv4_proto = ipv4_spec.hdr.next_proto_id;
556 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
557 ipv4_mask.hdr.next_proto_id = ntuple_filter->proto_mask;
558 ipv4_mask.hdr.src_addr = ntuple_filter->src_ip_mask;
559 ipv4_mask.hdr.src_addr =
560 convert_depth_to_bitmask(ipv4_mask.hdr.src_addr);
561 ipv4_mask.hdr.dst_addr = ntuple_filter->dst_ip_mask;
562 ipv4_mask.hdr.dst_addr =
563 convert_depth_to_bitmask(ipv4_mask.hdr.dst_addr);
565 switch (ipv4_proto) {
567 ipv4_udp_item.type = RTE_FLOW_ITEM_TYPE_IPV4;
568 ipv4_udp_item.spec = &ipv4_spec;
569 ipv4_udp_item.mask = &ipv4_mask;
570 ipv4_udp_item.last = NULL;
572 udp_spec.hdr.src_port = ntuple_filter->src_port;
573 udp_spec.hdr.dst_port = ntuple_filter->dst_port;
574 udp_spec.hdr.dgram_len = 0;
575 udp_spec.hdr.dgram_cksum = 0;
577 udp_mask.hdr.src_port = ntuple_filter->src_port_mask;
578 udp_mask.hdr.dst_port = ntuple_filter->dst_port_mask;
579 udp_mask.hdr.dgram_len = 0;
580 udp_mask.hdr.dgram_cksum = 0;
582 udp_item.type = RTE_FLOW_ITEM_TYPE_UDP;
583 udp_item.spec = &udp_spec;
584 udp_item.mask = &udp_mask;
585 udp_item.last = NULL;
587 attr.priority = ntuple_filter->priority;
588 pattern_ipv4_5tuple[1] = ipv4_udp_item;
589 pattern_ipv4_5tuple[2] = udp_item;
592 ipv4_tcp_item.type = RTE_FLOW_ITEM_TYPE_IPV4;
593 ipv4_tcp_item.spec = &ipv4_spec;
594 ipv4_tcp_item.mask = &ipv4_mask;
595 ipv4_tcp_item.last = NULL;
597 memset(&tcp_spec, 0, sizeof(tcp_spec));
598 tcp_spec.hdr.src_port = ntuple_filter->src_port;
599 tcp_spec.hdr.dst_port = ntuple_filter->dst_port;
601 memset(&tcp_mask, 0, sizeof(tcp_mask));
602 tcp_mask.hdr.src_port = ntuple_filter->src_port_mask;
603 tcp_mask.hdr.dst_port = ntuple_filter->dst_port_mask;
605 tcp_item.type = RTE_FLOW_ITEM_TYPE_TCP;
606 tcp_item.spec = &tcp_spec;
607 tcp_item.mask = &tcp_mask;
608 tcp_item.last = NULL;
610 attr.priority = ntuple_filter->priority;
611 pattern_ipv4_5tuple[1] = ipv4_tcp_item;
612 pattern_ipv4_5tuple[2] = tcp_item;
615 ipv4_sctp_item.type = RTE_FLOW_ITEM_TYPE_IPV4;
616 ipv4_sctp_item.spec = &ipv4_spec;
617 ipv4_sctp_item.mask = &ipv4_mask;
618 ipv4_sctp_item.last = NULL;
620 sctp_spec.hdr.src_port = ntuple_filter->src_port;
621 sctp_spec.hdr.dst_port = ntuple_filter->dst_port;
622 sctp_spec.hdr.cksum = 0;
623 sctp_spec.hdr.tag = 0;
625 sctp_mask.hdr.src_port = ntuple_filter->src_port_mask;
626 sctp_mask.hdr.dst_port = ntuple_filter->dst_port_mask;
627 sctp_mask.hdr.cksum = 0;
628 sctp_mask.hdr.tag = 0;
630 sctp_item.type = RTE_FLOW_ITEM_TYPE_SCTP;
631 sctp_item.spec = &sctp_spec;
632 sctp_item.mask = &sctp_mask;
633 sctp_item.last = NULL;
635 attr.priority = ntuple_filter->priority;
636 pattern_ipv4_5tuple[1] = ipv4_sctp_item;
637 pattern_ipv4_5tuple[2] = sctp_item;
644 pattern_ipv4_5tuple[0] = eth_item;
645 pattern_ipv4_5tuple[3] = end_item;
646 actions[0] = count_action;
647 actions[1] = end_action;
649 /* Validate and add rule */
650 ret = rte_flow_classify_validate(cls_app->cls, &attr,
651 pattern_ipv4_5tuple, actions, &error);
653 printf("table entry validate failed ipv4_proto = %u\n",
658 rule = rte_flow_classify_table_entry_add(
659 cls_app->cls, &attr, pattern_ipv4_5tuple,
660 actions, &key_found, &error);
662 printf("table entry add failed ipv4_proto = %u\n",
668 rules[num_classify_rules] = rule;
669 num_classify_rules++;
673 /* Reads file and calls the add_classify_rule function. 8< */
675 add_rules(const char *rule_path, struct flow_classifier *cls_app)
680 unsigned int total_num = 0;
681 struct rte_eth_ntuple_filter ntuple_filter;
684 fh = fopen(rule_path, "rb");
686 rte_exit(EXIT_FAILURE, "%s: fopen %s failed\n", __func__,
689 ret = fseek(fh, 0, SEEK_SET);
691 rte_exit(EXIT_FAILURE, "%s: fseek %d failed\n", __func__,
695 while (fgets(buff, LINE_MAX, fh) != NULL) {
698 if (is_bypass_line(buff))
701 if (total_num >= FLOW_CLASSIFY_MAX_RULE_NUM - 1) {
702 printf("\nINFO: classify rule capacity %d reached\n",
707 if (parse_ipv4_5tuple_rule(buff, &ntuple_filter) != 0)
708 rte_exit(EXIT_FAILURE,
709 "%s Line %u: parse rules error\n",
712 if (add_classify_rule(&ntuple_filter, cls_app) != 0)
713 rte_exit(EXIT_FAILURE, "add rule error\n");
721 /* >8 End of add_rules. */
725 print_usage(const char *prgname)
727 printf("%s usage:\n", prgname);
728 printf("[EAL options] -- --"OPTION_RULE_IPV4"=FILE: ");
729 printf("specify the ipv4 rules file.\n");
730 printf("Each rule occupies one line in the file.\n");
733 /* Parse the argument given in the command line of the application */
735 parse_args(int argc, char **argv)
740 char *prgname = argv[0];
741 static struct option lgopts[] = {
742 {OPTION_RULE_IPV4, 1, 0, 0},
748 while ((opt = getopt_long(argc, argvopt, "",
749 lgopts, &option_index)) != EOF) {
754 if (!strncmp(lgopts[option_index].name,
756 sizeof(OPTION_RULE_IPV4)))
757 parm_config.rule_ipv4_name = optarg;
760 print_usage(prgname);
766 argv[optind-1] = prgname;
769 optind = 1; /* reset getopt lib */
774 * The main function, which does initialization and calls the lcore_main
778 main(int argc, char *argv[])
780 struct rte_mempool *mbuf_pool;
785 struct rte_table_acl_params table_acl_params;
786 struct rte_flow_classify_table_params cls_table_params;
787 struct flow_classifier *cls_app;
788 struct rte_flow_classifier_params cls_params;
791 /* Initialize the Environment Abstraction Layer (EAL). 8< */
792 ret = rte_eal_init(argc, argv);
794 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
795 /* >8 End of initialization of EAL. */
800 /* Parse application arguments (after the EAL ones). 8< */
801 ret = parse_args(argc, argv);
803 rte_exit(EXIT_FAILURE, "Invalid flow_classify parameters\n");
804 /* >8 End of parse application arguments. */
806 /* Check that there is an even number of ports to send/receive on. */
807 nb_ports = rte_eth_dev_count_avail();
808 if (nb_ports < 2 || (nb_ports & 1))
809 rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
811 /* Creates a new mempool in memory to hold the mbufs. 8< */
812 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
813 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
814 /* >8 End of creation of new mempool in memory. */
816 if (mbuf_pool == NULL)
817 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
819 /* Initialize all ports. 8< */
820 RTE_ETH_FOREACH_DEV(portid)
821 if (port_init(portid, mbuf_pool) != 0)
822 rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8 "\n",
824 /* >8 End of initialization of all ports. */
826 if (rte_lcore_count() > 1)
827 printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
829 socket_id = rte_eth_dev_socket_id(0);
831 /* Memory allocation. 8< */
832 size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct flow_classifier_acl));
833 cls_app = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
835 rte_exit(EXIT_FAILURE, "Cannot allocate classifier memory\n");
837 cls_params.name = "flow_classifier";
838 cls_params.socket_id = socket_id;
840 cls_app->cls = rte_flow_classifier_create(&cls_params);
841 if (cls_app->cls == NULL) {
843 rte_exit(EXIT_FAILURE, "Cannot create classifier\n");
846 /* initialise ACL table params */
847 table_acl_params.name = "table_acl_ipv4_5tuple";
848 table_acl_params.n_rules = FLOW_CLASSIFY_MAX_RULE_NUM;
849 table_acl_params.n_rule_fields = RTE_DIM(ipv4_defs);
850 memcpy(table_acl_params.field_format, ipv4_defs, sizeof(ipv4_defs));
852 /* initialise table create params */
853 cls_table_params.ops = &rte_table_acl_ops;
854 cls_table_params.arg_create = &table_acl_params;
855 cls_table_params.type = RTE_FLOW_CLASSIFY_TABLE_ACL_IP4_5TUPLE;
857 ret = rte_flow_classify_table_create(cls_app->cls, &cls_table_params);
859 rte_flow_classifier_free(cls_app->cls);
861 rte_exit(EXIT_FAILURE, "Failed to create classifier table\n");
863 /* >8 End of initialization of table create params. */
865 /* read file of IPv4 5 tuple rules and initialize parameters
866 * for rte_flow_classify_validate and rte_flow_classify_table_entry_add
870 /* Read file of IPv4 tuple rules. 8< */
871 if (add_rules(parm_config.rule_ipv4_name, cls_app)) {
872 rte_flow_classifier_free(cls_app->cls);
874 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
876 /* >8 End of reading file of IPv4 5 tuple rules. */
878 /* Call lcore_main on the main core only. */
881 /* clean up the EAL */