1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
11 #include <sys/queue.h>
16 #include <rte_common.h>
17 #include <rte_byteorder.h>
19 #include <rte_memory.h>
20 #include <rte_memcpy.h>
22 #include <rte_launch.h>
23 #include <rte_cycles.h>
24 #include <rte_prefetch.h>
25 #include <rte_lcore.h>
26 #include <rte_per_lcore.h>
27 #include <rte_branch_prediction.h>
28 #include <rte_interrupts.h>
29 #include <rte_random.h>
30 #include <rte_debug.h>
31 #include <rte_ether.h>
32 #include <rte_ethdev.h>
33 #include <rte_mempool.h>
38 #include <rte_string_fns.h>
41 #include <cmdline_parse.h>
42 #include <cmdline_parse_etheraddr.h>
44 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
45 #define L3FWDACL_DEBUG
47 #define DO_RFC_1812_CHECKS
49 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
51 #define MAX_JUMBO_PKT_LEN 9600
53 #define MEMPOOL_CACHE_SIZE 256
56 * This expression is used to calculate the number of mbufs needed
57 * depending on user input, taking into account memory for rx and tx hardware
58 * rings, cache per lcore and mtable per port per lcore.
59 * RTE_MAX is used to ensure that NB_MBUF never goes below a
60 * minimum value of 8192
63 #define NB_MBUF RTE_MAX(\
64 (nb_ports * nb_rx_queue * nb_rxd + \
65 nb_ports * nb_lcores * MAX_PKT_BURST + \
66 nb_ports * n_tx_queue * nb_txd + \
67 nb_lcores * MEMPOOL_CACHE_SIZE), \
70 #define MAX_PKT_BURST 32
71 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
75 /* Configure how many packets ahead to prefetch, when reading packets */
76 #define PREFETCH_OFFSET 3
79 * Configurable number of RX/TX ring descriptors
81 #define RTE_TEST_RX_DESC_DEFAULT 1024
82 #define RTE_TEST_TX_DESC_DEFAULT 1024
83 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
84 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
86 /* mask of enabled ports */
87 static uint32_t enabled_port_mask;
88 static int promiscuous_on; /**< Ports set in promiscuous mode off by default. */
89 static int numa_on = 1; /**< NUMA is enabled by default. */
91 struct lcore_rx_queue {
94 } __rte_cache_aligned;
96 #define MAX_RX_QUEUE_PER_LCORE 16
97 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
98 #define MAX_RX_QUEUE_PER_PORT 128
100 #define MAX_LCORE_PARAMS 1024
101 struct lcore_params {
105 } __rte_cache_aligned;
107 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
108 static struct lcore_params lcore_params_array_default[] = {
120 static struct lcore_params *lcore_params = lcore_params_array_default;
121 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
122 sizeof(lcore_params_array_default[0]);
124 static struct rte_eth_conf port_conf = {
126 .mq_mode = ETH_MQ_RX_RSS,
127 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
129 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
134 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
135 ETH_RSS_TCP | ETH_RSS_SCTP,
139 .mq_mode = ETH_MQ_TX_NONE,
143 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
145 /* ethernet addresses of ports */
146 static struct rte_ether_hdr port_l2hdr[RTE_MAX_ETHPORTS];
148 static const struct {
150 enum rte_acl_classify_alg alg;
154 .alg = RTE_ACL_CLASSIFY_SCALAR,
158 .alg = RTE_ACL_CLASSIFY_SSE,
162 .alg = RTE_ACL_CLASSIFY_AVX2,
166 .alg = RTE_ACL_CLASSIFY_NEON,
170 .alg = RTE_ACL_CLASSIFY_ALTIVEC,
174 .alg = RTE_ACL_CLASSIFY_AVX512X16,
178 .alg = RTE_ACL_CLASSIFY_AVX512X32,
182 /***********************start of ACL part******************************/
183 #ifdef DO_RFC_1812_CHECKS
185 is_valid_ipv4_pkt(struct rte_ipv4_hdr *pkt, uint32_t link_len);
188 send_single_packet(struct rte_mbuf *m, uint16_t port);
190 #define MAX_ACL_RULE_NUM 100000
191 #define DEFAULT_MAX_CATEGORIES 1
192 #define L3FWD_ACL_IPV4_NAME "l3fwd-acl-ipv4"
193 #define L3FWD_ACL_IPV6_NAME "l3fwd-acl-ipv6"
194 #define ACL_LEAD_CHAR ('@')
195 #define ROUTE_LEAD_CHAR ('R')
196 #define COMMENT_LEAD_CHAR ('#')
199 #define OPT_CONFIG "config"
200 OPT_CONFIG_NUM = 256,
201 #define OPT_NONUMA "no-numa"
203 #define OPT_ENBJMO "enable-jumbo"
205 #define OPT_RULE_IPV4 "rule_ipv4"
207 #define OPT_RULE_IPV6 "rule_ipv6"
209 #define OPT_ALG "alg"
211 #define OPT_ETH_DEST "eth-dest"
215 #define ACL_DENY_SIGNATURE 0xf0000000
216 #define RTE_LOGTYPE_L3FWDACL RTE_LOGTYPE_USER3
217 #define acl_log(format, ...) RTE_LOG(ERR, L3FWDACL, format, ##__VA_ARGS__)
218 #define uint32_t_to_char(ip, a, b, c, d) do {\
219 *a = (unsigned char)(ip >> 24 & 0xff);\
220 *b = (unsigned char)(ip >> 16 & 0xff);\
221 *c = (unsigned char)(ip >> 8 & 0xff);\
222 *d = (unsigned char)(ip & 0xff);\
224 #define OFF_ETHHEAD (sizeof(struct rte_ether_hdr))
225 #define OFF_IPV42PROTO (offsetof(struct rte_ipv4_hdr, next_proto_id))
226 #define OFF_IPV62PROTO (offsetof(struct rte_ipv6_hdr, proto))
227 #define MBUF_IPV4_2PROTO(m) \
228 rte_pktmbuf_mtod_offset((m), uint8_t *, OFF_ETHHEAD + OFF_IPV42PROTO)
229 #define MBUF_IPV6_2PROTO(m) \
230 rte_pktmbuf_mtod_offset((m), uint8_t *, OFF_ETHHEAD + OFF_IPV62PROTO)
232 #define GET_CB_FIELD(in, fd, base, lim, dlm) do { \
236 val = strtoul((in), &end, (base)); \
237 if (errno != 0 || end[0] != (dlm) || val > (lim)) \
239 (fd) = (typeof(fd))val; \
244 * ACL rules should have higher priorities than route ones to ensure ACL rule
245 * always be found when input packets have multi-matches in the database.
246 * A exception case is performance measure, which can define route rules with
247 * higher priority and route rules will always be returned in each lookup.
248 * Reserve range from ACL_RULE_PRIORITY_MAX + 1 to
249 * RTE_ACL_MAX_PRIORITY for route entries in performance measure
251 #define ACL_RULE_PRIORITY_MAX 0x10000000
254 * Forward port info save in ACL lib starts from 1
255 * since ACL assume 0 is invalid.
256 * So, need add 1 when saving and minus 1 when forwarding packets.
258 #define FWD_PORT_SHIFT 1
261 * Rule and trace formats definitions.
274 * That effectively defines order of IPV4VLAN classifications:
276 * - VLAN (TAG and DOMAIN)
279 * - PORTS (SRC and DST)
282 RTE_ACL_IPV4VLAN_PROTO,
283 RTE_ACL_IPV4VLAN_VLAN,
284 RTE_ACL_IPV4VLAN_SRC,
285 RTE_ACL_IPV4VLAN_DST,
286 RTE_ACL_IPV4VLAN_PORTS,
290 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
292 .type = RTE_ACL_FIELD_TYPE_BITMASK,
293 .size = sizeof(uint8_t),
294 .field_index = PROTO_FIELD_IPV4,
295 .input_index = RTE_ACL_IPV4VLAN_PROTO,
299 .type = RTE_ACL_FIELD_TYPE_MASK,
300 .size = sizeof(uint32_t),
301 .field_index = SRC_FIELD_IPV4,
302 .input_index = RTE_ACL_IPV4VLAN_SRC,
303 .offset = offsetof(struct rte_ipv4_hdr, src_addr) -
304 offsetof(struct rte_ipv4_hdr, next_proto_id),
307 .type = RTE_ACL_FIELD_TYPE_MASK,
308 .size = sizeof(uint32_t),
309 .field_index = DST_FIELD_IPV4,
310 .input_index = RTE_ACL_IPV4VLAN_DST,
311 .offset = offsetof(struct rte_ipv4_hdr, dst_addr) -
312 offsetof(struct rte_ipv4_hdr, next_proto_id),
315 .type = RTE_ACL_FIELD_TYPE_RANGE,
316 .size = sizeof(uint16_t),
317 .field_index = SRCP_FIELD_IPV4,
318 .input_index = RTE_ACL_IPV4VLAN_PORTS,
319 .offset = sizeof(struct rte_ipv4_hdr) -
320 offsetof(struct rte_ipv4_hdr, next_proto_id),
323 .type = RTE_ACL_FIELD_TYPE_RANGE,
324 .size = sizeof(uint16_t),
325 .field_index = DSTP_FIELD_IPV4,
326 .input_index = RTE_ACL_IPV4VLAN_PORTS,
327 .offset = sizeof(struct rte_ipv4_hdr) -
328 offsetof(struct rte_ipv4_hdr, next_proto_id) +
333 #define IPV6_ADDR_LEN 16
334 #define IPV6_ADDR_U16 (IPV6_ADDR_LEN / sizeof(uint16_t))
335 #define IPV6_ADDR_U32 (IPV6_ADDR_LEN / sizeof(uint32_t))
352 struct rte_acl_field_def ipv6_defs[NUM_FIELDS_IPV6] = {
354 .type = RTE_ACL_FIELD_TYPE_BITMASK,
355 .size = sizeof(uint8_t),
356 .field_index = PROTO_FIELD_IPV6,
357 .input_index = PROTO_FIELD_IPV6,
361 .type = RTE_ACL_FIELD_TYPE_MASK,
362 .size = sizeof(uint32_t),
363 .field_index = SRC1_FIELD_IPV6,
364 .input_index = SRC1_FIELD_IPV6,
365 .offset = offsetof(struct rte_ipv6_hdr, src_addr) -
366 offsetof(struct rte_ipv6_hdr, proto),
369 .type = RTE_ACL_FIELD_TYPE_MASK,
370 .size = sizeof(uint32_t),
371 .field_index = SRC2_FIELD_IPV6,
372 .input_index = SRC2_FIELD_IPV6,
373 .offset = offsetof(struct rte_ipv6_hdr, src_addr) -
374 offsetof(struct rte_ipv6_hdr, proto) + sizeof(uint32_t),
377 .type = RTE_ACL_FIELD_TYPE_MASK,
378 .size = sizeof(uint32_t),
379 .field_index = SRC3_FIELD_IPV6,
380 .input_index = SRC3_FIELD_IPV6,
381 .offset = offsetof(struct rte_ipv6_hdr, src_addr) -
382 offsetof(struct rte_ipv6_hdr, proto) +
383 2 * sizeof(uint32_t),
386 .type = RTE_ACL_FIELD_TYPE_MASK,
387 .size = sizeof(uint32_t),
388 .field_index = SRC4_FIELD_IPV6,
389 .input_index = SRC4_FIELD_IPV6,
390 .offset = offsetof(struct rte_ipv6_hdr, src_addr) -
391 offsetof(struct rte_ipv6_hdr, proto) +
392 3 * sizeof(uint32_t),
395 .type = RTE_ACL_FIELD_TYPE_MASK,
396 .size = sizeof(uint32_t),
397 .field_index = DST1_FIELD_IPV6,
398 .input_index = DST1_FIELD_IPV6,
399 .offset = offsetof(struct rte_ipv6_hdr, dst_addr)
400 - offsetof(struct rte_ipv6_hdr, proto),
403 .type = RTE_ACL_FIELD_TYPE_MASK,
404 .size = sizeof(uint32_t),
405 .field_index = DST2_FIELD_IPV6,
406 .input_index = DST2_FIELD_IPV6,
407 .offset = offsetof(struct rte_ipv6_hdr, dst_addr) -
408 offsetof(struct rte_ipv6_hdr, proto) + sizeof(uint32_t),
411 .type = RTE_ACL_FIELD_TYPE_MASK,
412 .size = sizeof(uint32_t),
413 .field_index = DST3_FIELD_IPV6,
414 .input_index = DST3_FIELD_IPV6,
415 .offset = offsetof(struct rte_ipv6_hdr, dst_addr) -
416 offsetof(struct rte_ipv6_hdr, proto) +
417 2 * sizeof(uint32_t),
420 .type = RTE_ACL_FIELD_TYPE_MASK,
421 .size = sizeof(uint32_t),
422 .field_index = DST4_FIELD_IPV6,
423 .input_index = DST4_FIELD_IPV6,
424 .offset = offsetof(struct rte_ipv6_hdr, dst_addr) -
425 offsetof(struct rte_ipv6_hdr, proto) +
426 3 * sizeof(uint32_t),
429 .type = RTE_ACL_FIELD_TYPE_RANGE,
430 .size = sizeof(uint16_t),
431 .field_index = SRCP_FIELD_IPV6,
432 .input_index = SRCP_FIELD_IPV6,
433 .offset = sizeof(struct rte_ipv6_hdr) -
434 offsetof(struct rte_ipv6_hdr, proto),
437 .type = RTE_ACL_FIELD_TYPE_RANGE,
438 .size = sizeof(uint16_t),
439 .field_index = DSTP_FIELD_IPV6,
440 .input_index = SRCP_FIELD_IPV6,
441 .offset = sizeof(struct rte_ipv6_hdr) -
442 offsetof(struct rte_ipv6_hdr, proto) + sizeof(uint16_t),
451 CB_FLD_SRC_PORT_HIGH,
454 CB_FLD_DST_PORT_HIGH,
460 RTE_ACL_RULE_DEF(acl4_rule, RTE_DIM(ipv4_defs));
461 RTE_ACL_RULE_DEF(acl6_rule, RTE_DIM(ipv6_defs));
463 struct acl_search_t {
464 const uint8_t *data_ipv4[MAX_PKT_BURST];
465 struct rte_mbuf *m_ipv4[MAX_PKT_BURST];
466 uint32_t res_ipv4[MAX_PKT_BURST];
469 const uint8_t *data_ipv6[MAX_PKT_BURST];
470 struct rte_mbuf *m_ipv6[MAX_PKT_BURST];
471 uint32_t res_ipv6[MAX_PKT_BURST];
476 char mapped[NB_SOCKETS];
477 struct rte_acl_ctx *acx_ipv4[NB_SOCKETS];
478 struct rte_acl_ctx *acx_ipv6[NB_SOCKETS];
479 #ifdef L3FWDACL_DEBUG
480 struct acl4_rule *rule_ipv4;
481 struct acl6_rule *rule_ipv6;
486 const char *rule_ipv4_name;
487 const char *rule_ipv6_name;
488 enum rte_acl_classify_alg alg;
491 const char cb_port_delim[] = ":";
494 print_one_ipv4_rule(struct acl4_rule *rule, int extra)
496 unsigned char a, b, c, d;
498 uint32_t_to_char(rule->field[SRC_FIELD_IPV4].value.u32,
500 printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
501 rule->field[SRC_FIELD_IPV4].mask_range.u32);
502 uint32_t_to_char(rule->field[DST_FIELD_IPV4].value.u32,
504 printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
505 rule->field[DST_FIELD_IPV4].mask_range.u32);
506 printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
507 rule->field[SRCP_FIELD_IPV4].value.u16,
508 rule->field[SRCP_FIELD_IPV4].mask_range.u16,
509 rule->field[DSTP_FIELD_IPV4].value.u16,
510 rule->field[DSTP_FIELD_IPV4].mask_range.u16,
511 rule->field[PROTO_FIELD_IPV4].value.u8,
512 rule->field[PROTO_FIELD_IPV4].mask_range.u8);
514 printf("0x%x-0x%x-0x%x ",
515 rule->data.category_mask,
517 rule->data.userdata);
521 print_one_ipv6_rule(struct acl6_rule *rule, int extra)
523 unsigned char a, b, c, d;
525 uint32_t_to_char(rule->field[SRC1_FIELD_IPV6].value.u32,
527 printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
528 uint32_t_to_char(rule->field[SRC2_FIELD_IPV6].value.u32,
530 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
531 uint32_t_to_char(rule->field[SRC3_FIELD_IPV6].value.u32,
533 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
534 uint32_t_to_char(rule->field[SRC4_FIELD_IPV6].value.u32,
536 printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
537 rule->field[SRC1_FIELD_IPV6].mask_range.u32
538 + rule->field[SRC2_FIELD_IPV6].mask_range.u32
539 + rule->field[SRC3_FIELD_IPV6].mask_range.u32
540 + rule->field[SRC4_FIELD_IPV6].mask_range.u32);
542 uint32_t_to_char(rule->field[DST1_FIELD_IPV6].value.u32,
544 printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
545 uint32_t_to_char(rule->field[DST2_FIELD_IPV6].value.u32,
547 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
548 uint32_t_to_char(rule->field[DST3_FIELD_IPV6].value.u32,
550 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
551 uint32_t_to_char(rule->field[DST4_FIELD_IPV6].value.u32,
553 printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
554 rule->field[DST1_FIELD_IPV6].mask_range.u32
555 + rule->field[DST2_FIELD_IPV6].mask_range.u32
556 + rule->field[DST3_FIELD_IPV6].mask_range.u32
557 + rule->field[DST4_FIELD_IPV6].mask_range.u32);
559 printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
560 rule->field[SRCP_FIELD_IPV6].value.u16,
561 rule->field[SRCP_FIELD_IPV6].mask_range.u16,
562 rule->field[DSTP_FIELD_IPV6].value.u16,
563 rule->field[DSTP_FIELD_IPV6].mask_range.u16,
564 rule->field[PROTO_FIELD_IPV6].value.u8,
565 rule->field[PROTO_FIELD_IPV6].mask_range.u8);
567 printf("0x%x-0x%x-0x%x ",
568 rule->data.category_mask,
570 rule->data.userdata);
573 /* Bypass comment and empty lines */
575 is_bypass_line(char *buff)
580 if (buff[0] == COMMENT_LEAD_CHAR)
583 while (buff[i] != '\0') {
584 if (!isspace(buff[i]))
591 #ifdef L3FWDACL_DEBUG
593 dump_acl4_rule(struct rte_mbuf *m, uint32_t sig)
595 uint32_t offset = sig & ~ACL_DENY_SIGNATURE;
596 unsigned char a, b, c, d;
597 struct rte_ipv4_hdr *ipv4_hdr =
598 rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
599 sizeof(struct rte_ether_hdr));
601 uint32_t_to_char(rte_bswap32(ipv4_hdr->src_addr), &a, &b, &c, &d);
602 printf("Packet Src:%hhu.%hhu.%hhu.%hhu ", a, b, c, d);
603 uint32_t_to_char(rte_bswap32(ipv4_hdr->dst_addr), &a, &b, &c, &d);
604 printf("Dst:%hhu.%hhu.%hhu.%hhu ", a, b, c, d);
606 printf("Src port:%hu,Dst port:%hu ",
607 rte_bswap16(*(uint16_t *)(ipv4_hdr + 1)),
608 rte_bswap16(*((uint16_t *)(ipv4_hdr + 1) + 1)));
609 printf("hit ACL %d - ", offset);
611 print_one_ipv4_rule(acl_config.rule_ipv4 + offset, 1);
617 dump_acl6_rule(struct rte_mbuf *m, uint32_t sig)
620 uint32_t offset = sig & ~ACL_DENY_SIGNATURE;
621 struct rte_ipv6_hdr *ipv6_hdr =
622 rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
623 sizeof(struct rte_ether_hdr));
625 printf("Packet Src");
626 for (i = 0; i < RTE_DIM(ipv6_hdr->src_addr); i += sizeof(uint16_t))
628 ipv6_hdr->src_addr[i], ipv6_hdr->src_addr[i + 1]);
631 for (i = 0; i < RTE_DIM(ipv6_hdr->dst_addr); i += sizeof(uint16_t))
633 ipv6_hdr->dst_addr[i], ipv6_hdr->dst_addr[i + 1]);
635 printf("\nSrc port:%hu,Dst port:%hu ",
636 rte_bswap16(*(uint16_t *)(ipv6_hdr + 1)),
637 rte_bswap16(*((uint16_t *)(ipv6_hdr + 1) + 1)));
638 printf("hit ACL %d - ", offset);
640 print_one_ipv6_rule(acl_config.rule_ipv6 + offset, 1);
644 #endif /* L3FWDACL_DEBUG */
647 dump_ipv4_rules(struct acl4_rule *rule, int num, int extra)
651 for (i = 0; i < num; i++, rule++) {
652 printf("\t%d:", i + 1);
653 print_one_ipv4_rule(rule, extra);
659 dump_ipv6_rules(struct acl6_rule *rule, int num, int extra)
663 for (i = 0; i < num; i++, rule++) {
664 printf("\t%d:", i + 1);
665 print_one_ipv6_rule(rule, extra);
670 #ifdef DO_RFC_1812_CHECKS
672 prepare_one_packet(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
675 struct rte_ipv4_hdr *ipv4_hdr;
676 struct rte_mbuf *pkt = pkts_in[index];
678 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
679 ipv4_hdr = rte_pktmbuf_mtod_offset(pkt, struct rte_ipv4_hdr *,
680 sizeof(struct rte_ether_hdr));
682 /* Check to make sure the packet is valid (RFC1812) */
683 if (is_valid_ipv4_pkt(ipv4_hdr, pkt->pkt_len) >= 0) {
685 /* Update time to live and header checksum */
686 --(ipv4_hdr->time_to_live);
687 ++(ipv4_hdr->hdr_checksum);
689 /* Fill acl structure */
690 acl->data_ipv4[acl->num_ipv4] = MBUF_IPV4_2PROTO(pkt);
691 acl->m_ipv4[(acl->num_ipv4)++] = pkt;
694 /* Not a valid IPv4 packet */
695 rte_pktmbuf_free(pkt);
697 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
698 /* Fill acl structure */
699 acl->data_ipv6[acl->num_ipv6] = MBUF_IPV6_2PROTO(pkt);
700 acl->m_ipv6[(acl->num_ipv6)++] = pkt;
703 /* Unknown type, drop the packet */
704 rte_pktmbuf_free(pkt);
710 prepare_one_packet(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
713 struct rte_mbuf *pkt = pkts_in[index];
715 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
716 /* Fill acl structure */
717 acl->data_ipv4[acl->num_ipv4] = MBUF_IPV4_2PROTO(pkt);
718 acl->m_ipv4[(acl->num_ipv4)++] = pkt;
720 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
721 /* Fill acl structure */
722 acl->data_ipv6[acl->num_ipv6] = MBUF_IPV6_2PROTO(pkt);
723 acl->m_ipv6[(acl->num_ipv6)++] = pkt;
725 /* Unknown type, drop the packet */
726 rte_pktmbuf_free(pkt);
729 #endif /* DO_RFC_1812_CHECKS */
732 prepare_acl_parameter(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
740 /* Prefetch first packets */
741 for (i = 0; i < PREFETCH_OFFSET && i < nb_rx; i++) {
742 rte_prefetch0(rte_pktmbuf_mtod(
743 pkts_in[i], void *));
746 for (i = 0; i < (nb_rx - PREFETCH_OFFSET); i++) {
747 rte_prefetch0(rte_pktmbuf_mtod(pkts_in[
748 i + PREFETCH_OFFSET], void *));
749 prepare_one_packet(pkts_in, acl, i);
752 /* Process left packets */
753 for (; i < nb_rx; i++)
754 prepare_one_packet(pkts_in, acl, i);
758 send_one_packet(struct rte_mbuf *m, uint32_t res)
760 if (likely((res & ACL_DENY_SIGNATURE) == 0 && res != 0)) {
761 /* forward packets */
762 send_single_packet(m,
763 (uint8_t)(res - FWD_PORT_SHIFT));
765 /* in the ACL list, drop it */
766 #ifdef L3FWDACL_DEBUG
767 if ((res & ACL_DENY_SIGNATURE) != 0) {
768 if (RTE_ETH_IS_IPV4_HDR(m->packet_type))
769 dump_acl4_rule(m, res);
770 else if (RTE_ETH_IS_IPV6_HDR(m->packet_type))
771 dump_acl6_rule(m, res);
781 send_packets(struct rte_mbuf **m, uint32_t *res, int num)
785 /* Prefetch first packets */
786 for (i = 0; i < PREFETCH_OFFSET && i < num; i++) {
787 rte_prefetch0(rte_pktmbuf_mtod(
791 for (i = 0; i < (num - PREFETCH_OFFSET); i++) {
792 rte_prefetch0(rte_pktmbuf_mtod(m[
793 i + PREFETCH_OFFSET], void *));
794 send_one_packet(m[i], res[i]);
797 /* Process left packets */
799 send_one_packet(m[i], res[i]);
803 * Parses IPV6 address, exepcts the following format:
804 * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX (where X - is a hexedecimal digit).
807 parse_ipv6_addr(const char *in, const char **end, uint32_t v[IPV6_ADDR_U32],
810 uint32_t addr[IPV6_ADDR_U16];
812 GET_CB_FIELD(in, addr[0], 16, UINT16_MAX, ':');
813 GET_CB_FIELD(in, addr[1], 16, UINT16_MAX, ':');
814 GET_CB_FIELD(in, addr[2], 16, UINT16_MAX, ':');
815 GET_CB_FIELD(in, addr[3], 16, UINT16_MAX, ':');
816 GET_CB_FIELD(in, addr[4], 16, UINT16_MAX, ':');
817 GET_CB_FIELD(in, addr[5], 16, UINT16_MAX, ':');
818 GET_CB_FIELD(in, addr[6], 16, UINT16_MAX, ':');
819 GET_CB_FIELD(in, addr[7], 16, UINT16_MAX, dlm);
823 v[0] = (addr[0] << 16) + addr[1];
824 v[1] = (addr[2] << 16) + addr[3];
825 v[2] = (addr[4] << 16) + addr[5];
826 v[3] = (addr[6] << 16) + addr[7];
832 parse_ipv6_net(const char *in, struct rte_acl_field field[4])
837 const uint32_t nbu32 = sizeof(uint32_t) * CHAR_BIT;
840 rc = parse_ipv6_addr(in, &mp, v, '/');
845 GET_CB_FIELD(mp, m, 0, CHAR_BIT * sizeof(v), 0);
847 /* put all together. */
848 for (i = 0; i != RTE_DIM(v); i++) {
849 if (m >= (i + 1) * nbu32)
850 field[i].mask_range.u32 = nbu32;
852 field[i].mask_range.u32 = m > (i * nbu32) ?
855 field[i].value.u32 = v[i];
862 parse_cb_ipv6_rule(char *str, struct rte_acl_rule *v, int has_userdata)
865 char *s, *sp, *in[CB_FLD_NUM];
866 static const char *dlm = " \t\n";
867 int dim = has_userdata ? CB_FLD_NUM : CB_FLD_USERDATA;
870 for (i = 0; i != dim; i++, s = NULL) {
871 in[i] = strtok_r(s, dlm, &sp);
876 rc = parse_ipv6_net(in[CB_FLD_SRC_ADDR], v->field + SRC1_FIELD_IPV6);
878 acl_log("failed to read source address/mask: %s\n",
879 in[CB_FLD_SRC_ADDR]);
883 rc = parse_ipv6_net(in[CB_FLD_DST_ADDR], v->field + DST1_FIELD_IPV6);
885 acl_log("failed to read destination address/mask: %s\n",
886 in[CB_FLD_DST_ADDR]);
891 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
892 v->field[SRCP_FIELD_IPV6].value.u16,
894 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
895 v->field[SRCP_FIELD_IPV6].mask_range.u16,
898 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
899 sizeof(cb_port_delim)) != 0)
902 /* destination port. */
903 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
904 v->field[DSTP_FIELD_IPV6].value.u16,
906 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
907 v->field[DSTP_FIELD_IPV6].mask_range.u16,
910 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
911 sizeof(cb_port_delim)) != 0)
914 if (v->field[SRCP_FIELD_IPV6].mask_range.u16
915 < v->field[SRCP_FIELD_IPV6].value.u16
916 || v->field[DSTP_FIELD_IPV6].mask_range.u16
917 < v->field[DSTP_FIELD_IPV6].value.u16)
920 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].value.u8,
922 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].mask_range.u8,
926 GET_CB_FIELD(in[CB_FLD_USERDATA], v->data.userdata,
933 * Parse ClassBench rules file.
935 * '@'<src_ipv4_addr>'/'<masklen> <space> \
936 * <dst_ipv4_addr>'/'<masklen> <space> \
937 * <src_port_low> <space> ":" <src_port_high> <space> \
938 * <dst_port_low> <space> ":" <dst_port_high> <space> \
942 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
944 uint8_t a, b, c, d, m;
946 GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
947 GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
948 GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
949 GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
950 GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
952 addr[0] = RTE_IPV4(a, b, c, d);
959 parse_cb_ipv4vlan_rule(char *str, struct rte_acl_rule *v, int has_userdata)
962 char *s, *sp, *in[CB_FLD_NUM];
963 static const char *dlm = " \t\n";
964 int dim = has_userdata ? CB_FLD_NUM : CB_FLD_USERDATA;
967 for (i = 0; i != dim; i++, s = NULL) {
968 in[i] = strtok_r(s, dlm, &sp);
973 rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
974 &v->field[SRC_FIELD_IPV4].value.u32,
975 &v->field[SRC_FIELD_IPV4].mask_range.u32);
977 acl_log("failed to read source address/mask: %s\n",
978 in[CB_FLD_SRC_ADDR]);
982 rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
983 &v->field[DST_FIELD_IPV4].value.u32,
984 &v->field[DST_FIELD_IPV4].mask_range.u32);
986 acl_log("failed to read destination address/mask: %s\n",
987 in[CB_FLD_DST_ADDR]);
991 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
992 v->field[SRCP_FIELD_IPV4].value.u16,
994 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
995 v->field[SRCP_FIELD_IPV4].mask_range.u16,
998 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
999 sizeof(cb_port_delim)) != 0)
1002 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
1003 v->field[DSTP_FIELD_IPV4].value.u16,
1005 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
1006 v->field[DSTP_FIELD_IPV4].mask_range.u16,
1009 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
1010 sizeof(cb_port_delim)) != 0)
1013 if (v->field[SRCP_FIELD_IPV4].mask_range.u16
1014 < v->field[SRCP_FIELD_IPV4].value.u16
1015 || v->field[DSTP_FIELD_IPV4].mask_range.u16
1016 < v->field[DSTP_FIELD_IPV4].value.u16)
1019 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].value.u8,
1021 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].mask_range.u8,
1025 GET_CB_FIELD(in[CB_FLD_USERDATA], v->data.userdata, 0,
1032 add_rules(const char *rule_path,
1033 struct rte_acl_rule **proute_base,
1034 unsigned int *proute_num,
1035 struct rte_acl_rule **pacl_base,
1036 unsigned int *pacl_num, uint32_t rule_size,
1037 int (*parser)(char *, struct rte_acl_rule*, int))
1039 uint8_t *acl_rules, *route_rules;
1040 struct rte_acl_rule *next;
1041 unsigned int acl_num = 0, route_num = 0, total_num = 0;
1042 unsigned int acl_cnt = 0, route_cnt = 0;
1043 char buff[LINE_MAX];
1044 FILE *fh = fopen(rule_path, "rb");
1049 rte_exit(EXIT_FAILURE, "%s: Open %s failed\n", __func__,
1052 while ((fgets(buff, LINE_MAX, fh) != NULL)) {
1053 if (buff[0] == ROUTE_LEAD_CHAR)
1055 else if (buff[0] == ACL_LEAD_CHAR)
1060 rte_exit(EXIT_FAILURE, "Not find any route entries in %s!\n",
1063 val = fseek(fh, 0, SEEK_SET);
1065 rte_exit(EXIT_FAILURE, "%s: File seek operation failed\n",
1069 acl_rules = calloc(acl_num, rule_size);
1071 if (NULL == acl_rules)
1072 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1075 route_rules = calloc(route_num, rule_size);
1077 if (NULL == route_rules)
1078 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1082 while (fgets(buff, LINE_MAX, fh) != NULL) {
1085 if (is_bypass_line(buff))
1091 if (s == ROUTE_LEAD_CHAR)
1092 next = (struct rte_acl_rule *)(route_rules +
1093 route_cnt * rule_size);
1096 else if (s == ACL_LEAD_CHAR)
1097 next = (struct rte_acl_rule *)(acl_rules +
1098 acl_cnt * rule_size);
1102 rte_exit(EXIT_FAILURE,
1103 "%s Line %u: should start with leading "
1105 rule_path, i, ROUTE_LEAD_CHAR, ACL_LEAD_CHAR);
1107 if (parser(buff + 1, next, s == ROUTE_LEAD_CHAR) != 0)
1108 rte_exit(EXIT_FAILURE,
1109 "%s Line %u: parse rules error\n",
1112 if (s == ROUTE_LEAD_CHAR) {
1113 /* Check the forwarding port number */
1114 if ((enabled_port_mask & (1 << next->data.userdata)) ==
1116 rte_exit(EXIT_FAILURE,
1117 "%s Line %u: fwd number illegal:%u\n",
1118 rule_path, i, next->data.userdata);
1119 next->data.userdata += FWD_PORT_SHIFT;
1122 next->data.userdata = ACL_DENY_SIGNATURE + acl_cnt;
1126 next->data.priority = RTE_ACL_MAX_PRIORITY - total_num;
1127 next->data.category_mask = -1;
1133 *pacl_base = (struct rte_acl_rule *)acl_rules;
1134 *pacl_num = acl_num;
1135 *proute_base = (struct rte_acl_rule *)route_rules;
1136 *proute_num = route_cnt;
1142 usage_acl_alg(char *buf, size_t sz)
1144 uint32_t i, n, rc, tn;
1148 for (i = 0; i < RTE_DIM(acl_alg); i++) {
1149 rc = snprintf(buf + n, sz - n,
1150 i == RTE_DIM(acl_alg) - 1 ? "%s" : "%s|",
1161 str_acl_alg(enum rte_acl_classify_alg alg)
1165 for (i = 0; i != RTE_DIM(acl_alg); i++) {
1166 if (alg == acl_alg[i].alg)
1167 return acl_alg[i].name;
1173 static enum rte_acl_classify_alg
1174 parse_acl_alg(const char *alg)
1178 for (i = 0; i != RTE_DIM(acl_alg); i++) {
1179 if (strcmp(alg, acl_alg[i].name) == 0)
1180 return acl_alg[i].alg;
1183 return RTE_ACL_CLASSIFY_DEFAULT;
1187 dump_acl_config(void)
1189 printf("ACL option are:\n");
1190 printf(OPT_RULE_IPV4": %s\n", parm_config.rule_ipv4_name);
1191 printf(OPT_RULE_IPV6": %s\n", parm_config.rule_ipv6_name);
1192 printf(OPT_ALG": %s\n", str_acl_alg(parm_config.alg));
1196 check_acl_config(void)
1198 if (parm_config.rule_ipv4_name == NULL) {
1199 acl_log("ACL IPv4 rule file not specified\n");
1201 } else if (parm_config.rule_ipv6_name == NULL) {
1202 acl_log("ACL IPv6 rule file not specified\n");
1209 static struct rte_acl_ctx*
1210 setup_acl(struct rte_acl_rule *route_base,
1211 struct rte_acl_rule *acl_base, unsigned int route_num,
1212 unsigned int acl_num, int ipv6, int socketid)
1214 char name[PATH_MAX];
1215 struct rte_acl_param acl_param;
1216 struct rte_acl_config acl_build_param;
1217 struct rte_acl_ctx *context;
1218 int dim = ipv6 ? RTE_DIM(ipv6_defs) : RTE_DIM(ipv4_defs);
1220 /* Create ACL contexts */
1221 snprintf(name, sizeof(name), "%s%d",
1222 ipv6 ? L3FWD_ACL_IPV6_NAME : L3FWD_ACL_IPV4_NAME,
1225 acl_param.name = name;
1226 acl_param.socket_id = socketid;
1227 acl_param.rule_size = RTE_ACL_RULE_SZ(dim);
1228 acl_param.max_rule_num = MAX_ACL_RULE_NUM;
1230 if ((context = rte_acl_create(&acl_param)) == NULL)
1231 rte_exit(EXIT_FAILURE, "Failed to create ACL context\n");
1233 if (parm_config.alg != RTE_ACL_CLASSIFY_DEFAULT &&
1234 rte_acl_set_ctx_classify(context, parm_config.alg) != 0)
1235 rte_exit(EXIT_FAILURE,
1236 "Failed to setup classify method for ACL context\n");
1238 if (rte_acl_add_rules(context, route_base, route_num) < 0)
1239 rte_exit(EXIT_FAILURE, "add rules failed\n");
1241 if (rte_acl_add_rules(context, acl_base, acl_num) < 0)
1242 rte_exit(EXIT_FAILURE, "add rules failed\n");
1244 /* Perform builds */
1245 memset(&acl_build_param, 0, sizeof(acl_build_param));
1247 acl_build_param.num_categories = DEFAULT_MAX_CATEGORIES;
1248 acl_build_param.num_fields = dim;
1249 memcpy(&acl_build_param.defs, ipv6 ? ipv6_defs : ipv4_defs,
1250 ipv6 ? sizeof(ipv6_defs) : sizeof(ipv4_defs));
1252 if (rte_acl_build(context, &acl_build_param) != 0)
1253 rte_exit(EXIT_FAILURE, "Failed to build ACL trie\n");
1255 rte_acl_dump(context);
1266 struct rte_acl_rule *acl_base_ipv4, *route_base_ipv4,
1267 *acl_base_ipv6, *route_base_ipv6;
1268 unsigned int acl_num_ipv4 = 0, route_num_ipv4 = 0,
1269 acl_num_ipv6 = 0, route_num_ipv6 = 0;
1271 if (check_acl_config() != 0)
1272 rte_exit(EXIT_FAILURE, "Failed to get valid ACL options\n");
1276 /* Load rules from the input file */
1277 if (add_rules(parm_config.rule_ipv4_name, &route_base_ipv4,
1278 &route_num_ipv4, &acl_base_ipv4, &acl_num_ipv4,
1279 sizeof(struct acl4_rule), &parse_cb_ipv4vlan_rule) < 0)
1280 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1282 acl_log("IPv4 Route entries %u:\n", route_num_ipv4);
1283 dump_ipv4_rules((struct acl4_rule *)route_base_ipv4, route_num_ipv4, 1);
1285 acl_log("IPv4 ACL entries %u:\n", acl_num_ipv4);
1286 dump_ipv4_rules((struct acl4_rule *)acl_base_ipv4, acl_num_ipv4, 1);
1288 if (add_rules(parm_config.rule_ipv6_name, &route_base_ipv6,
1290 &acl_base_ipv6, &acl_num_ipv6,
1291 sizeof(struct acl6_rule), &parse_cb_ipv6_rule) < 0)
1292 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1294 acl_log("IPv6 Route entries %u:\n", route_num_ipv6);
1295 dump_ipv6_rules((struct acl6_rule *)route_base_ipv6, route_num_ipv6, 1);
1297 acl_log("IPv6 ACL entries %u:\n", acl_num_ipv6);
1298 dump_ipv6_rules((struct acl6_rule *)acl_base_ipv6, acl_num_ipv6, 1);
1300 memset(&acl_config, 0, sizeof(acl_config));
1302 /* Check sockets a context should be created on */
1304 acl_config.mapped[0] = 1;
1306 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1307 if (rte_lcore_is_enabled(lcore_id) == 0)
1310 socketid = rte_lcore_to_socket_id(lcore_id);
1311 if (socketid >= NB_SOCKETS) {
1312 acl_log("Socket %d of lcore %u is out "
1314 socketid, lcore_id, NB_SOCKETS);
1315 free(route_base_ipv4);
1316 free(route_base_ipv6);
1317 free(acl_base_ipv4);
1318 free(acl_base_ipv6);
1322 acl_config.mapped[socketid] = 1;
1326 for (i = 0; i < NB_SOCKETS; i++) {
1327 if (acl_config.mapped[i]) {
1328 acl_config.acx_ipv4[i] = setup_acl(route_base_ipv4,
1329 acl_base_ipv4, route_num_ipv4, acl_num_ipv4,
1332 acl_config.acx_ipv6[i] = setup_acl(route_base_ipv6,
1333 acl_base_ipv6, route_num_ipv6, acl_num_ipv6,
1338 free(route_base_ipv4);
1339 free(route_base_ipv6);
1341 #ifdef L3FWDACL_DEBUG
1342 acl_config.rule_ipv4 = (struct acl4_rule *)acl_base_ipv4;
1343 acl_config.rule_ipv6 = (struct acl6_rule *)acl_base_ipv6;
1345 free(acl_base_ipv4);
1346 free(acl_base_ipv6);
1352 /***********************end of ACL part******************************/
1355 uint16_t n_rx_queue;
1356 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
1358 uint16_t tx_port_id[RTE_MAX_ETHPORTS];
1359 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
1360 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
1361 } __rte_cache_aligned;
1363 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
1365 /* Enqueue a single packet, and send burst if queue is filled */
1367 send_single_packet(struct rte_mbuf *m, uint16_t port)
1370 struct lcore_conf *qconf;
1371 struct rte_ether_hdr *eh;
1373 lcore_id = rte_lcore_id();
1375 /* update src and dst mac*/
1376 eh = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1377 memcpy(eh, &port_l2hdr[port],
1378 sizeof(eh->dst_addr) + sizeof(eh->src_addr));
1380 qconf = &lcore_conf[lcore_id];
1381 rte_eth_tx_buffer(port, qconf->tx_queue_id[port],
1382 qconf->tx_buffer[port], m);
1385 #ifdef DO_RFC_1812_CHECKS
1387 is_valid_ipv4_pkt(struct rte_ipv4_hdr *pkt, uint32_t link_len)
1389 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
1391 * 1. The packet length reported by the Link Layer must be large
1392 * enough to hold the minimum length legal IP datagram (20 bytes).
1394 if (link_len < sizeof(struct rte_ipv4_hdr))
1397 /* 2. The IP checksum must be correct. */
1398 /* this is checked in H/W */
1401 * 3. The IP version number must be 4. If the version number is not 4
1402 * then the packet may be another version of IP, such as IPng or
1405 if (((pkt->version_ihl) >> 4) != 4)
1408 * 4. The IP header length field must be large enough to hold the
1409 * minimum length legal IP datagram (20 bytes = 5 words).
1411 if ((pkt->version_ihl & 0xf) < 5)
1415 * 5. The IP total length field must be large enough to hold the IP
1416 * datagram header, whose length is specified in the IP header length
1419 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct rte_ipv4_hdr))
1426 /* main processing loop */
1428 main_loop(__rte_unused void *dummy)
1430 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1432 uint64_t prev_tsc, diff_tsc, cur_tsc;
1436 struct lcore_conf *qconf;
1438 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
1439 / US_PER_S * BURST_TX_DRAIN_US;
1442 lcore_id = rte_lcore_id();
1443 qconf = &lcore_conf[lcore_id];
1444 socketid = rte_lcore_to_socket_id(lcore_id);
1446 if (qconf->n_rx_queue == 0) {
1447 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
1451 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
1453 for (i = 0; i < qconf->n_rx_queue; i++) {
1455 portid = qconf->rx_queue_list[i].port_id;
1456 queueid = qconf->rx_queue_list[i].queue_id;
1457 RTE_LOG(INFO, L3FWD,
1458 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
1459 lcore_id, portid, queueid);
1464 cur_tsc = rte_rdtsc();
1467 * TX burst queue drain
1469 diff_tsc = cur_tsc - prev_tsc;
1470 if (unlikely(diff_tsc > drain_tsc)) {
1471 for (i = 0; i < qconf->n_tx_port; ++i) {
1472 portid = qconf->tx_port_id[i];
1473 rte_eth_tx_buffer_flush(portid,
1474 qconf->tx_queue_id[portid],
1475 qconf->tx_buffer[portid]);
1481 * Read packet from RX queues
1483 for (i = 0; i < qconf->n_rx_queue; ++i) {
1485 portid = qconf->rx_queue_list[i].port_id;
1486 queueid = qconf->rx_queue_list[i].queue_id;
1487 nb_rx = rte_eth_rx_burst(portid, queueid,
1488 pkts_burst, MAX_PKT_BURST);
1491 struct acl_search_t acl_search;
1493 prepare_acl_parameter(pkts_burst, &acl_search,
1496 if (acl_search.num_ipv4) {
1498 acl_config.acx_ipv4[socketid],
1499 acl_search.data_ipv4,
1500 acl_search.res_ipv4,
1501 acl_search.num_ipv4,
1502 DEFAULT_MAX_CATEGORIES);
1504 send_packets(acl_search.m_ipv4,
1505 acl_search.res_ipv4,
1506 acl_search.num_ipv4);
1509 if (acl_search.num_ipv6) {
1511 acl_config.acx_ipv6[socketid],
1512 acl_search.data_ipv6,
1513 acl_search.res_ipv6,
1514 acl_search.num_ipv6,
1515 DEFAULT_MAX_CATEGORIES);
1517 send_packets(acl_search.m_ipv6,
1518 acl_search.res_ipv6,
1519 acl_search.num_ipv6);
1527 check_lcore_params(void)
1529 uint8_t queue, lcore;
1533 for (i = 0; i < nb_lcore_params; ++i) {
1534 queue = lcore_params[i].queue_id;
1535 if (queue >= MAX_RX_QUEUE_PER_PORT) {
1536 printf("invalid queue number: %hhu\n", queue);
1539 lcore = lcore_params[i].lcore_id;
1540 if (!rte_lcore_is_enabled(lcore)) {
1541 printf("error: lcore %hhu is not enabled in "
1542 "lcore mask\n", lcore);
1545 socketid = rte_lcore_to_socket_id(lcore);
1546 if (socketid != 0 && numa_on == 0) {
1547 printf("warning: lcore %hhu is on socket %d "
1556 check_port_config(void)
1561 for (i = 0; i < nb_lcore_params; ++i) {
1562 portid = lcore_params[i].port_id;
1564 if ((enabled_port_mask & (1 << portid)) == 0) {
1565 printf("port %u is not enabled in port mask\n", portid);
1568 if (!rte_eth_dev_is_valid_port(portid)) {
1569 printf("port %u is not present on the board\n", portid);
1577 get_port_n_rx_queues(const uint16_t port)
1582 for (i = 0; i < nb_lcore_params; ++i) {
1583 if (lcore_params[i].port_id == port &&
1584 lcore_params[i].queue_id > queue)
1585 queue = lcore_params[i].queue_id;
1587 return (uint8_t)(++queue);
1591 init_lcore_rx_queues(void)
1593 uint16_t i, nb_rx_queue;
1596 for (i = 0; i < nb_lcore_params; ++i) {
1597 lcore = lcore_params[i].lcore_id;
1598 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1599 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1600 printf("error: too many queues (%u) for lcore: %u\n",
1601 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
1604 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1605 lcore_params[i].port_id;
1606 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1607 lcore_params[i].queue_id;
1608 lcore_conf[lcore].n_rx_queue++;
1616 print_usage(const char *prgname)
1620 usage_acl_alg(alg, sizeof(alg));
1621 printf("%s [EAL options] -- -p PORTMASK -P"
1622 "--"OPT_RULE_IPV4"=FILE"
1623 "--"OPT_RULE_IPV6"=FILE"
1624 " [--"OPT_CONFIG" (port,queue,lcore)[,(port,queue,lcore]]"
1625 " [--"OPT_ENBJMO" [--max-pkt-len PKTLEN]]\n"
1626 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1627 " -P : enable promiscuous mode\n"
1628 " --"OPT_CONFIG": (port,queue,lcore): "
1629 "rx queues configuration\n"
1630 " --"OPT_NONUMA": optional, disable numa awareness\n"
1631 " --"OPT_ENBJMO": enable jumbo frame"
1632 " which max packet len is PKTLEN in decimal (64-9600)\n"
1633 " --"OPT_RULE_IPV4"=FILE: specify the ipv4 rules entries "
1635 "Each rule occupy one line. "
1636 "2 kinds of rules are supported. "
1637 "One is ACL entry at while line leads with character '%c', "
1638 "another is route entry at while line leads with "
1640 " --"OPT_RULE_IPV6"=FILE: specify the ipv6 rules "
1642 " --"OPT_ALG": ACL classify method to use, one of: %s\n",
1643 prgname, ACL_LEAD_CHAR, ROUTE_LEAD_CHAR, alg);
1647 parse_max_pkt_len(const char *pktlen)
1652 /* parse decimal string */
1653 len = strtoul(pktlen, &end, 10);
1654 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
1664 parse_portmask(const char *portmask)
1669 /* parse hexadecimal string */
1670 pm = strtoul(portmask, &end, 16);
1671 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1678 parse_config(const char *q_arg)
1681 const char *p, *p0 = q_arg;
1689 unsigned long int_fld[_NUM_FLD];
1690 char *str_fld[_NUM_FLD];
1694 nb_lcore_params = 0;
1696 while ((p = strchr(p0, '(')) != NULL) {
1698 if ((p0 = strchr(p, ')')) == NULL)
1702 if (size >= sizeof(s))
1705 snprintf(s, sizeof(s), "%.*s", size, p);
1706 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1709 for (i = 0; i < _NUM_FLD; i++) {
1711 int_fld[i] = strtoul(str_fld[i], &end, 0);
1712 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1715 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1716 printf("exceeded max number of lcore params: %hu\n",
1720 lcore_params_array[nb_lcore_params].port_id =
1721 (uint8_t)int_fld[FLD_PORT];
1722 lcore_params_array[nb_lcore_params].queue_id =
1723 (uint8_t)int_fld[FLD_QUEUE];
1724 lcore_params_array[nb_lcore_params].lcore_id =
1725 (uint8_t)int_fld[FLD_LCORE];
1728 lcore_params = lcore_params_array;
1733 parse_eth_dest(const char *optarg)
1735 unsigned long portid;
1739 portid = strtoul(optarg, &port_end, 0);
1740 if (errno != 0 || port_end == optarg || *port_end++ != ',')
1741 return "Invalid format";
1742 else if (portid >= RTE_MAX_ETHPORTS)
1743 return "port value exceeds RTE_MAX_ETHPORTS("
1744 RTE_STR(RTE_MAX_ETHPORTS) ")";
1746 if (cmdline_parse_etheraddr(NULL, port_end,
1747 &port_l2hdr[portid].dst_addr,
1748 sizeof(port_l2hdr[portid].dst_addr)) < 0)
1749 return "Invalid ethernet address";
1753 /* Parse the argument given in the command line of the application */
1755 parse_args(int argc, char **argv)
1760 char *prgname = argv[0];
1761 static struct option lgopts[] = {
1762 {OPT_CONFIG, 1, NULL, OPT_CONFIG_NUM },
1763 {OPT_NONUMA, 0, NULL, OPT_NONUMA_NUM },
1764 {OPT_ENBJMO, 0, NULL, OPT_ENBJMO_NUM },
1765 {OPT_RULE_IPV4, 1, NULL, OPT_RULE_IPV4_NUM },
1766 {OPT_RULE_IPV6, 1, NULL, OPT_RULE_IPV6_NUM },
1767 {OPT_ALG, 1, NULL, OPT_ALG_NUM },
1768 {OPT_ETH_DEST, 1, NULL, OPT_ETH_DEST_NUM },
1774 while ((opt = getopt_long(argc, argvopt, "p:P",
1775 lgopts, &option_index)) != EOF) {
1780 enabled_port_mask = parse_portmask(optarg);
1781 if (enabled_port_mask == 0) {
1782 printf("invalid portmask\n");
1783 print_usage(prgname);
1789 printf("Promiscuous mode selected\n");
1794 case OPT_CONFIG_NUM:
1795 ret = parse_config(optarg);
1797 printf("invalid config\n");
1798 print_usage(prgname);
1803 case OPT_NONUMA_NUM:
1804 printf("numa is disabled\n");
1808 case OPT_ENBJMO_NUM:
1810 struct option lenopts = {
1817 printf("jumbo frame is enabled\n");
1818 port_conf.rxmode.offloads |=
1819 DEV_RX_OFFLOAD_JUMBO_FRAME;
1820 port_conf.txmode.offloads |=
1821 DEV_TX_OFFLOAD_MULTI_SEGS;
1824 * if no max-pkt-len set, then use the
1825 * default value RTE_ETHER_MAX_LEN
1827 if (getopt_long(argc, argvopt, "",
1828 &lenopts, &option_index) == 0) {
1829 ret = parse_max_pkt_len(optarg);
1831 (ret > MAX_JUMBO_PKT_LEN)) {
1832 printf("invalid packet "
1834 print_usage(prgname);
1837 port_conf.rxmode.max_rx_pkt_len = ret;
1839 printf("set jumbo frame max packet length "
1842 port_conf.rxmode.max_rx_pkt_len);
1845 case OPT_RULE_IPV4_NUM:
1846 parm_config.rule_ipv4_name = optarg;
1849 case OPT_RULE_IPV6_NUM:
1850 parm_config.rule_ipv6_name = optarg;
1854 parm_config.alg = parse_acl_alg(optarg);
1855 if (parm_config.alg ==
1856 RTE_ACL_CLASSIFY_DEFAULT) {
1857 printf("unknown %s value:\"%s\"\n",
1859 print_usage(prgname);
1864 case OPT_ETH_DEST_NUM:
1866 const char *serr = parse_eth_dest(optarg);
1868 printf("invalid %s value:\"%s\": %s\n",
1869 OPT_ETH_DEST, optarg, serr);
1870 print_usage(prgname);
1876 print_usage(prgname);
1882 argv[optind-1] = prgname;
1885 optind = 1; /* reset getopt lib */
1890 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
1892 char buf[RTE_ETHER_ADDR_FMT_SIZE];
1893 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
1894 printf("%s%s", name, buf);
1898 init_mem(unsigned nb_mbuf)
1904 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1905 if (rte_lcore_is_enabled(lcore_id) == 0)
1909 socketid = rte_lcore_to_socket_id(lcore_id);
1913 if (socketid >= NB_SOCKETS) {
1914 rte_exit(EXIT_FAILURE,
1915 "Socket %d of lcore %u is out of range %d\n",
1916 socketid, lcore_id, NB_SOCKETS);
1918 if (pktmbuf_pool[socketid] == NULL) {
1919 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
1920 pktmbuf_pool[socketid] =
1921 rte_pktmbuf_pool_create(s, nb_mbuf,
1922 MEMPOOL_CACHE_SIZE, 0,
1923 RTE_MBUF_DEFAULT_BUF_SIZE,
1925 if (pktmbuf_pool[socketid] == NULL)
1926 rte_exit(EXIT_FAILURE,
1927 "Cannot init mbuf pool on socket %d\n",
1930 printf("Allocated mbuf pool on socket %d\n",
1937 /* Check the link status of all ports in up to 9s, and print them finally */
1939 check_all_ports_link_status(uint32_t port_mask)
1941 #define CHECK_INTERVAL 100 /* 100ms */
1942 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1944 uint8_t count, all_ports_up, print_flag = 0;
1945 struct rte_eth_link link;
1947 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
1949 printf("\nChecking link status");
1951 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1953 RTE_ETH_FOREACH_DEV(portid) {
1954 if ((port_mask & (1 << portid)) == 0)
1956 memset(&link, 0, sizeof(link));
1957 ret = rte_eth_link_get_nowait(portid, &link);
1960 if (print_flag == 1)
1961 printf("Port %u link get failed: %s\n",
1962 portid, rte_strerror(-ret));
1965 /* print link status if flag set */
1966 if (print_flag == 1) {
1967 rte_eth_link_to_str(link_status_text,
1968 sizeof(link_status_text), &link);
1969 printf("Port %d %s\n", portid,
1973 /* clear all_ports_up flag if any link down */
1974 if (link.link_status == ETH_LINK_DOWN) {
1979 /* after finally printing all link status, get out */
1980 if (print_flag == 1)
1983 if (all_ports_up == 0) {
1986 rte_delay_ms(CHECK_INTERVAL);
1989 /* set the print_flag if all ports up or timeout */
1990 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1998 * build-up default vaues for dest MACs.
2001 set_default_dest_mac(void)
2005 for (i = 0; i != RTE_DIM(port_l2hdr); i++) {
2006 port_l2hdr[i].dst_addr.addr_bytes[0] =
2007 RTE_ETHER_LOCAL_ADMIN_ADDR;
2008 port_l2hdr[i].dst_addr.addr_bytes[5] = i;
2013 main(int argc, char **argv)
2015 struct lcore_conf *qconf;
2016 struct rte_eth_dev_info dev_info;
2017 struct rte_eth_txconf *txconf;
2022 uint32_t n_tx_queue, nb_lcores;
2024 uint8_t nb_rx_queue, queue, socketid;
2027 ret = rte_eal_init(argc, argv);
2029 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2033 set_default_dest_mac();
2035 /* parse application arguments (after the EAL ones) */
2036 ret = parse_args(argc, argv);
2038 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
2040 if (check_lcore_params() < 0)
2041 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
2043 ret = init_lcore_rx_queues();
2045 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2047 nb_ports = rte_eth_dev_count_avail();
2049 if (check_port_config() < 0)
2050 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
2052 /* Add ACL rules and route entries, build trie */
2053 if (app_acl_init() < 0)
2054 rte_exit(EXIT_FAILURE, "app_acl_init failed\n");
2056 nb_lcores = rte_lcore_count();
2058 /* initialize all ports */
2059 RTE_ETH_FOREACH_DEV(portid) {
2060 struct rte_eth_conf local_port_conf = port_conf;
2062 /* skip ports that are not enabled */
2063 if ((enabled_port_mask & (1 << portid)) == 0) {
2064 printf("\nSkipping disabled port %d\n", portid);
2069 printf("Initializing port %d ... ", portid);
2072 nb_rx_queue = get_port_n_rx_queues(portid);
2073 n_tx_queue = nb_lcores;
2074 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
2075 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
2076 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
2077 nb_rx_queue, (unsigned)n_tx_queue);
2079 ret = rte_eth_dev_info_get(portid, &dev_info);
2081 rte_exit(EXIT_FAILURE,
2082 "Error during getting device (port %u) info: %s\n",
2083 portid, strerror(-ret));
2085 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2086 local_port_conf.txmode.offloads |=
2087 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2089 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
2090 dev_info.flow_type_rss_offloads;
2091 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
2092 port_conf.rx_adv_conf.rss_conf.rss_hf) {
2093 printf("Port %u modified RSS hash function based on hardware support,"
2094 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
2096 port_conf.rx_adv_conf.rss_conf.rss_hf,
2097 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
2100 ret = rte_eth_dev_configure(portid, nb_rx_queue,
2101 (uint16_t)n_tx_queue, &local_port_conf);
2103 rte_exit(EXIT_FAILURE,
2104 "Cannot configure device: err=%d, port=%d\n",
2107 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2110 rte_exit(EXIT_FAILURE,
2111 "rte_eth_dev_adjust_nb_rx_tx_desc: err=%d, port=%d\n",
2114 ret = rte_eth_macaddr_get(portid, &port_l2hdr[portid].src_addr);
2116 rte_exit(EXIT_FAILURE,
2117 "rte_eth_macaddr_get: err=%d, port=%d\n",
2120 print_ethaddr("Dst MAC:", &port_l2hdr[portid].dst_addr);
2121 print_ethaddr(", Src MAC:", &port_l2hdr[portid].src_addr);
2125 ret = init_mem(NB_MBUF);
2127 rte_exit(EXIT_FAILURE, "init_mem failed\n");
2129 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2130 if (rte_lcore_is_enabled(lcore_id) == 0)
2133 /* Initialize TX buffers */
2134 qconf = &lcore_conf[lcore_id];
2135 qconf->tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
2136 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
2137 rte_eth_dev_socket_id(portid));
2138 if (qconf->tx_buffer[portid] == NULL)
2139 rte_exit(EXIT_FAILURE, "Can't allocate tx buffer for port %u\n",
2142 rte_eth_tx_buffer_init(qconf->tx_buffer[portid], MAX_PKT_BURST);
2145 /* init one TX queue per couple (lcore,port) */
2147 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2148 if (rte_lcore_is_enabled(lcore_id) == 0)
2152 socketid = (uint8_t)
2153 rte_lcore_to_socket_id(lcore_id);
2157 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
2160 ret = rte_eth_dev_info_get(portid, &dev_info);
2162 rte_exit(EXIT_FAILURE,
2163 "Error during getting device (port %u) info: %s\n",
2164 portid, strerror(-ret));
2166 txconf = &dev_info.default_txconf;
2167 txconf->offloads = local_port_conf.txmode.offloads;
2168 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2171 rte_exit(EXIT_FAILURE,
2172 "rte_eth_tx_queue_setup: err=%d, "
2173 "port=%d\n", ret, portid);
2175 qconf = &lcore_conf[lcore_id];
2176 qconf->tx_queue_id[portid] = queueid;
2179 qconf->tx_port_id[qconf->n_tx_port] = portid;
2185 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2186 if (rte_lcore_is_enabled(lcore_id) == 0)
2188 qconf = &lcore_conf[lcore_id];
2189 printf("\nInitializing rx queues on lcore %u ... ", lcore_id);
2191 /* init RX queues */
2192 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
2193 struct rte_eth_rxconf rxq_conf;
2195 portid = qconf->rx_queue_list[queue].port_id;
2196 queueid = qconf->rx_queue_list[queue].queue_id;
2199 socketid = (uint8_t)
2200 rte_lcore_to_socket_id(lcore_id);
2204 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2207 ret = rte_eth_dev_info_get(portid, &dev_info);
2209 rte_exit(EXIT_FAILURE,
2210 "Error during getting device (port %u) info: %s\n",
2211 portid, strerror(-ret));
2213 rxq_conf = dev_info.default_rxconf;
2214 rxq_conf.offloads = port_conf.rxmode.offloads;
2215 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2216 socketid, &rxq_conf,
2217 pktmbuf_pool[socketid]);
2219 rte_exit(EXIT_FAILURE,
2220 "rte_eth_rx_queue_setup: err=%d,"
2221 "port=%d\n", ret, portid);
2228 RTE_ETH_FOREACH_DEV(portid) {
2229 if ((enabled_port_mask & (1 << portid)) == 0)
2233 ret = rte_eth_dev_start(portid);
2235 rte_exit(EXIT_FAILURE,
2236 "rte_eth_dev_start: err=%d, port=%d\n",
2240 * If enabled, put device in promiscuous mode.
2241 * This allows IO forwarding mode to forward packets
2242 * to itself through 2 cross-connected ports of the
2245 if (promiscuous_on) {
2246 ret = rte_eth_promiscuous_enable(portid);
2248 rte_exit(EXIT_FAILURE,
2249 "rte_eth_promiscuous_enable: err=%s, port=%u\n",
2250 rte_strerror(-ret), portid);
2254 check_all_ports_link_status(enabled_port_mask);
2256 /* launch per-lcore init on every lcore */
2257 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MAIN);
2258 RTE_LCORE_FOREACH_WORKER(lcore_id) {
2259 if (rte_eal_wait_lcore(lcore_id) < 0)
2263 /* clean up the EAL */