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_atomic.h>
24 #include <rte_cycles.h>
25 #include <rte_prefetch.h>
26 #include <rte_lcore.h>
27 #include <rte_per_lcore.h>
28 #include <rte_branch_prediction.h>
29 #include <rte_interrupts.h>
30 #include <rte_random.h>
31 #include <rte_debug.h>
32 #include <rte_ether.h>
33 #include <rte_ethdev.h>
34 #include <rte_mempool.h>
39 #include <rte_string_fns.h>
42 #include <cmdline_parse.h>
43 #include <cmdline_parse_etheraddr.h>
45 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
46 #define L3FWDACL_DEBUG
48 #define DO_RFC_1812_CHECKS
50 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
52 #define MAX_JUMBO_PKT_LEN 9600
54 #define MEMPOOL_CACHE_SIZE 256
57 * This expression is used to calculate the number of mbufs needed
58 * depending on user input, taking into account memory for rx and tx hardware
59 * rings, cache per lcore and mtable per port per lcore.
60 * RTE_MAX is used to ensure that NB_MBUF never goes below a
61 * minimum value of 8192
64 #define NB_MBUF RTE_MAX(\
65 (nb_ports * nb_rx_queue * nb_rxd + \
66 nb_ports * nb_lcores * MAX_PKT_BURST + \
67 nb_ports * n_tx_queue * nb_txd + \
68 nb_lcores * MEMPOOL_CACHE_SIZE), \
71 #define MAX_PKT_BURST 32
72 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
76 /* Configure how many packets ahead to prefetch, when reading packets */
77 #define PREFETCH_OFFSET 3
80 * Configurable number of RX/TX ring descriptors
82 #define RTE_TEST_RX_DESC_DEFAULT 1024
83 #define RTE_TEST_TX_DESC_DEFAULT 1024
84 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
85 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
87 /* mask of enabled ports */
88 static uint32_t enabled_port_mask;
89 static int promiscuous_on; /**< Ports set in promiscuous mode off by default. */
90 static int numa_on = 1; /**< NUMA is enabled by default. */
92 struct lcore_rx_queue {
95 } __rte_cache_aligned;
97 #define MAX_RX_QUEUE_PER_LCORE 16
98 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
99 #define MAX_RX_QUEUE_PER_PORT 128
101 #define MAX_LCORE_PARAMS 1024
102 struct lcore_params {
106 } __rte_cache_aligned;
108 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
109 static struct lcore_params lcore_params_array_default[] = {
121 static struct lcore_params *lcore_params = lcore_params_array_default;
122 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
123 sizeof(lcore_params_array_default[0]);
125 static struct rte_eth_conf port_conf = {
127 .mq_mode = ETH_MQ_RX_RSS,
128 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
130 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
135 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
136 ETH_RSS_TCP | ETH_RSS_SCTP,
140 .mq_mode = ETH_MQ_TX_NONE,
144 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
146 /* ethernet addresses of ports */
147 static struct rte_ether_hdr port_l2hdr[RTE_MAX_ETHPORTS];
149 static const struct {
151 enum rte_acl_classify_alg alg;
155 .alg = RTE_ACL_CLASSIFY_SCALAR,
159 .alg = RTE_ACL_CLASSIFY_SSE,
163 .alg = RTE_ACL_CLASSIFY_AVX2,
167 .alg = RTE_ACL_CLASSIFY_NEON,
171 .alg = RTE_ACL_CLASSIFY_ALTIVEC,
175 .alg = RTE_ACL_CLASSIFY_AVX512X16,
179 .alg = RTE_ACL_CLASSIFY_AVX512X32,
183 /***********************start of ACL part******************************/
184 #ifdef DO_RFC_1812_CHECKS
186 is_valid_ipv4_pkt(struct rte_ipv4_hdr *pkt, uint32_t link_len);
189 send_single_packet(struct rte_mbuf *m, uint16_t port);
191 #define MAX_ACL_RULE_NUM 100000
192 #define DEFAULT_MAX_CATEGORIES 1
193 #define L3FWD_ACL_IPV4_NAME "l3fwd-acl-ipv4"
194 #define L3FWD_ACL_IPV6_NAME "l3fwd-acl-ipv6"
195 #define ACL_LEAD_CHAR ('@')
196 #define ROUTE_LEAD_CHAR ('R')
197 #define COMMENT_LEAD_CHAR ('#')
200 #define OPT_CONFIG "config"
201 OPT_CONFIG_NUM = 256,
202 #define OPT_NONUMA "no-numa"
204 #define OPT_ENBJMO "enable-jumbo"
206 #define OPT_RULE_IPV4 "rule_ipv4"
208 #define OPT_RULE_IPV6 "rule_ipv6"
210 #define OPT_ALG "alg"
212 #define OPT_ETH_DEST "eth-dest"
216 #define ACL_DENY_SIGNATURE 0xf0000000
217 #define RTE_LOGTYPE_L3FWDACL RTE_LOGTYPE_USER3
218 #define acl_log(format, ...) RTE_LOG(ERR, L3FWDACL, format, ##__VA_ARGS__)
219 #define uint32_t_to_char(ip, a, b, c, d) do {\
220 *a = (unsigned char)(ip >> 24 & 0xff);\
221 *b = (unsigned char)(ip >> 16 & 0xff);\
222 *c = (unsigned char)(ip >> 8 & 0xff);\
223 *d = (unsigned char)(ip & 0xff);\
225 #define OFF_ETHHEAD (sizeof(struct rte_ether_hdr))
226 #define OFF_IPV42PROTO (offsetof(struct rte_ipv4_hdr, next_proto_id))
227 #define OFF_IPV62PROTO (offsetof(struct rte_ipv6_hdr, proto))
228 #define MBUF_IPV4_2PROTO(m) \
229 rte_pktmbuf_mtod_offset((m), uint8_t *, OFF_ETHHEAD + OFF_IPV42PROTO)
230 #define MBUF_IPV6_2PROTO(m) \
231 rte_pktmbuf_mtod_offset((m), uint8_t *, OFF_ETHHEAD + OFF_IPV62PROTO)
233 #define GET_CB_FIELD(in, fd, base, lim, dlm) do { \
237 val = strtoul((in), &end, (base)); \
238 if (errno != 0 || end[0] != (dlm) || val > (lim)) \
240 (fd) = (typeof(fd))val; \
245 * ACL rules should have higher priorities than route ones to ensure ACL rule
246 * always be found when input packets have multi-matches in the database.
247 * A exception case is performance measure, which can define route rules with
248 * higher priority and route rules will always be returned in each lookup.
249 * Reserve range from ACL_RULE_PRIORITY_MAX + 1 to
250 * RTE_ACL_MAX_PRIORITY for route entries in performance measure
252 #define ACL_RULE_PRIORITY_MAX 0x10000000
255 * Forward port info save in ACL lib starts from 1
256 * since ACL assume 0 is invalid.
257 * So, need add 1 when saving and minus 1 when forwarding packets.
259 #define FWD_PORT_SHIFT 1
262 * Rule and trace formats definitions.
275 * That effectively defines order of IPV4VLAN classifications:
277 * - VLAN (TAG and DOMAIN)
280 * - PORTS (SRC and DST)
283 RTE_ACL_IPV4VLAN_PROTO,
284 RTE_ACL_IPV4VLAN_VLAN,
285 RTE_ACL_IPV4VLAN_SRC,
286 RTE_ACL_IPV4VLAN_DST,
287 RTE_ACL_IPV4VLAN_PORTS,
291 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
293 .type = RTE_ACL_FIELD_TYPE_BITMASK,
294 .size = sizeof(uint8_t),
295 .field_index = PROTO_FIELD_IPV4,
296 .input_index = RTE_ACL_IPV4VLAN_PROTO,
300 .type = RTE_ACL_FIELD_TYPE_MASK,
301 .size = sizeof(uint32_t),
302 .field_index = SRC_FIELD_IPV4,
303 .input_index = RTE_ACL_IPV4VLAN_SRC,
304 .offset = offsetof(struct rte_ipv4_hdr, src_addr) -
305 offsetof(struct rte_ipv4_hdr, next_proto_id),
308 .type = RTE_ACL_FIELD_TYPE_MASK,
309 .size = sizeof(uint32_t),
310 .field_index = DST_FIELD_IPV4,
311 .input_index = RTE_ACL_IPV4VLAN_DST,
312 .offset = offsetof(struct rte_ipv4_hdr, dst_addr) -
313 offsetof(struct rte_ipv4_hdr, next_proto_id),
316 .type = RTE_ACL_FIELD_TYPE_RANGE,
317 .size = sizeof(uint16_t),
318 .field_index = SRCP_FIELD_IPV4,
319 .input_index = RTE_ACL_IPV4VLAN_PORTS,
320 .offset = sizeof(struct rte_ipv4_hdr) -
321 offsetof(struct rte_ipv4_hdr, next_proto_id),
324 .type = RTE_ACL_FIELD_TYPE_RANGE,
325 .size = sizeof(uint16_t),
326 .field_index = DSTP_FIELD_IPV4,
327 .input_index = RTE_ACL_IPV4VLAN_PORTS,
328 .offset = sizeof(struct rte_ipv4_hdr) -
329 offsetof(struct rte_ipv4_hdr, next_proto_id) +
334 #define IPV6_ADDR_LEN 16
335 #define IPV6_ADDR_U16 (IPV6_ADDR_LEN / sizeof(uint16_t))
336 #define IPV6_ADDR_U32 (IPV6_ADDR_LEN / sizeof(uint32_t))
353 struct rte_acl_field_def ipv6_defs[NUM_FIELDS_IPV6] = {
355 .type = RTE_ACL_FIELD_TYPE_BITMASK,
356 .size = sizeof(uint8_t),
357 .field_index = PROTO_FIELD_IPV6,
358 .input_index = PROTO_FIELD_IPV6,
362 .type = RTE_ACL_FIELD_TYPE_MASK,
363 .size = sizeof(uint32_t),
364 .field_index = SRC1_FIELD_IPV6,
365 .input_index = SRC1_FIELD_IPV6,
366 .offset = offsetof(struct rte_ipv6_hdr, src_addr) -
367 offsetof(struct rte_ipv6_hdr, proto),
370 .type = RTE_ACL_FIELD_TYPE_MASK,
371 .size = sizeof(uint32_t),
372 .field_index = SRC2_FIELD_IPV6,
373 .input_index = SRC2_FIELD_IPV6,
374 .offset = offsetof(struct rte_ipv6_hdr, src_addr) -
375 offsetof(struct rte_ipv6_hdr, proto) + sizeof(uint32_t),
378 .type = RTE_ACL_FIELD_TYPE_MASK,
379 .size = sizeof(uint32_t),
380 .field_index = SRC3_FIELD_IPV6,
381 .input_index = SRC3_FIELD_IPV6,
382 .offset = offsetof(struct rte_ipv6_hdr, src_addr) -
383 offsetof(struct rte_ipv6_hdr, proto) +
384 2 * sizeof(uint32_t),
387 .type = RTE_ACL_FIELD_TYPE_MASK,
388 .size = sizeof(uint32_t),
389 .field_index = SRC4_FIELD_IPV6,
390 .input_index = SRC4_FIELD_IPV6,
391 .offset = offsetof(struct rte_ipv6_hdr, src_addr) -
392 offsetof(struct rte_ipv6_hdr, proto) +
393 3 * sizeof(uint32_t),
396 .type = RTE_ACL_FIELD_TYPE_MASK,
397 .size = sizeof(uint32_t),
398 .field_index = DST1_FIELD_IPV6,
399 .input_index = DST1_FIELD_IPV6,
400 .offset = offsetof(struct rte_ipv6_hdr, dst_addr)
401 - offsetof(struct rte_ipv6_hdr, proto),
404 .type = RTE_ACL_FIELD_TYPE_MASK,
405 .size = sizeof(uint32_t),
406 .field_index = DST2_FIELD_IPV6,
407 .input_index = DST2_FIELD_IPV6,
408 .offset = offsetof(struct rte_ipv6_hdr, dst_addr) -
409 offsetof(struct rte_ipv6_hdr, proto) + sizeof(uint32_t),
412 .type = RTE_ACL_FIELD_TYPE_MASK,
413 .size = sizeof(uint32_t),
414 .field_index = DST3_FIELD_IPV6,
415 .input_index = DST3_FIELD_IPV6,
416 .offset = offsetof(struct rte_ipv6_hdr, dst_addr) -
417 offsetof(struct rte_ipv6_hdr, proto) +
418 2 * sizeof(uint32_t),
421 .type = RTE_ACL_FIELD_TYPE_MASK,
422 .size = sizeof(uint32_t),
423 .field_index = DST4_FIELD_IPV6,
424 .input_index = DST4_FIELD_IPV6,
425 .offset = offsetof(struct rte_ipv6_hdr, dst_addr) -
426 offsetof(struct rte_ipv6_hdr, proto) +
427 3 * sizeof(uint32_t),
430 .type = RTE_ACL_FIELD_TYPE_RANGE,
431 .size = sizeof(uint16_t),
432 .field_index = SRCP_FIELD_IPV6,
433 .input_index = SRCP_FIELD_IPV6,
434 .offset = sizeof(struct rte_ipv6_hdr) -
435 offsetof(struct rte_ipv6_hdr, proto),
438 .type = RTE_ACL_FIELD_TYPE_RANGE,
439 .size = sizeof(uint16_t),
440 .field_index = DSTP_FIELD_IPV6,
441 .input_index = SRCP_FIELD_IPV6,
442 .offset = sizeof(struct rte_ipv6_hdr) -
443 offsetof(struct rte_ipv6_hdr, proto) + sizeof(uint16_t),
452 CB_FLD_SRC_PORT_HIGH,
455 CB_FLD_DST_PORT_HIGH,
461 RTE_ACL_RULE_DEF(acl4_rule, RTE_DIM(ipv4_defs));
462 RTE_ACL_RULE_DEF(acl6_rule, RTE_DIM(ipv6_defs));
464 struct acl_search_t {
465 const uint8_t *data_ipv4[MAX_PKT_BURST];
466 struct rte_mbuf *m_ipv4[MAX_PKT_BURST];
467 uint32_t res_ipv4[MAX_PKT_BURST];
470 const uint8_t *data_ipv6[MAX_PKT_BURST];
471 struct rte_mbuf *m_ipv6[MAX_PKT_BURST];
472 uint32_t res_ipv6[MAX_PKT_BURST];
477 char mapped[NB_SOCKETS];
478 struct rte_acl_ctx *acx_ipv4[NB_SOCKETS];
479 struct rte_acl_ctx *acx_ipv6[NB_SOCKETS];
480 #ifdef L3FWDACL_DEBUG
481 struct acl4_rule *rule_ipv4;
482 struct acl6_rule *rule_ipv6;
487 const char *rule_ipv4_name;
488 const char *rule_ipv6_name;
489 enum rte_acl_classify_alg alg;
492 const char cb_port_delim[] = ":";
495 print_one_ipv4_rule(struct acl4_rule *rule, int extra)
497 unsigned char a, b, c, d;
499 uint32_t_to_char(rule->field[SRC_FIELD_IPV4].value.u32,
501 printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
502 rule->field[SRC_FIELD_IPV4].mask_range.u32);
503 uint32_t_to_char(rule->field[DST_FIELD_IPV4].value.u32,
505 printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
506 rule->field[DST_FIELD_IPV4].mask_range.u32);
507 printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
508 rule->field[SRCP_FIELD_IPV4].value.u16,
509 rule->field[SRCP_FIELD_IPV4].mask_range.u16,
510 rule->field[DSTP_FIELD_IPV4].value.u16,
511 rule->field[DSTP_FIELD_IPV4].mask_range.u16,
512 rule->field[PROTO_FIELD_IPV4].value.u8,
513 rule->field[PROTO_FIELD_IPV4].mask_range.u8);
515 printf("0x%x-0x%x-0x%x ",
516 rule->data.category_mask,
518 rule->data.userdata);
522 print_one_ipv6_rule(struct acl6_rule *rule, int extra)
524 unsigned char a, b, c, d;
526 uint32_t_to_char(rule->field[SRC1_FIELD_IPV6].value.u32,
528 printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
529 uint32_t_to_char(rule->field[SRC2_FIELD_IPV6].value.u32,
531 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
532 uint32_t_to_char(rule->field[SRC3_FIELD_IPV6].value.u32,
534 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
535 uint32_t_to_char(rule->field[SRC4_FIELD_IPV6].value.u32,
537 printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
538 rule->field[SRC1_FIELD_IPV6].mask_range.u32
539 + rule->field[SRC2_FIELD_IPV6].mask_range.u32
540 + rule->field[SRC3_FIELD_IPV6].mask_range.u32
541 + rule->field[SRC4_FIELD_IPV6].mask_range.u32);
543 uint32_t_to_char(rule->field[DST1_FIELD_IPV6].value.u32,
545 printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
546 uint32_t_to_char(rule->field[DST2_FIELD_IPV6].value.u32,
548 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
549 uint32_t_to_char(rule->field[DST3_FIELD_IPV6].value.u32,
551 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
552 uint32_t_to_char(rule->field[DST4_FIELD_IPV6].value.u32,
554 printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
555 rule->field[DST1_FIELD_IPV6].mask_range.u32
556 + rule->field[DST2_FIELD_IPV6].mask_range.u32
557 + rule->field[DST3_FIELD_IPV6].mask_range.u32
558 + rule->field[DST4_FIELD_IPV6].mask_range.u32);
560 printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
561 rule->field[SRCP_FIELD_IPV6].value.u16,
562 rule->field[SRCP_FIELD_IPV6].mask_range.u16,
563 rule->field[DSTP_FIELD_IPV6].value.u16,
564 rule->field[DSTP_FIELD_IPV6].mask_range.u16,
565 rule->field[PROTO_FIELD_IPV6].value.u8,
566 rule->field[PROTO_FIELD_IPV6].mask_range.u8);
568 printf("0x%x-0x%x-0x%x ",
569 rule->data.category_mask,
571 rule->data.userdata);
574 /* Bypass comment and empty lines */
576 is_bypass_line(char *buff)
581 if (buff[0] == COMMENT_LEAD_CHAR)
584 while (buff[i] != '\0') {
585 if (!isspace(buff[i]))
592 #ifdef L3FWDACL_DEBUG
594 dump_acl4_rule(struct rte_mbuf *m, uint32_t sig)
596 uint32_t offset = sig & ~ACL_DENY_SIGNATURE;
597 unsigned char a, b, c, d;
598 struct rte_ipv4_hdr *ipv4_hdr =
599 rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
600 sizeof(struct rte_ether_hdr));
602 uint32_t_to_char(rte_bswap32(ipv4_hdr->src_addr), &a, &b, &c, &d);
603 printf("Packet Src:%hhu.%hhu.%hhu.%hhu ", a, b, c, d);
604 uint32_t_to_char(rte_bswap32(ipv4_hdr->dst_addr), &a, &b, &c, &d);
605 printf("Dst:%hhu.%hhu.%hhu.%hhu ", a, b, c, d);
607 printf("Src port:%hu,Dst port:%hu ",
608 rte_bswap16(*(uint16_t *)(ipv4_hdr + 1)),
609 rte_bswap16(*((uint16_t *)(ipv4_hdr + 1) + 1)));
610 printf("hit ACL %d - ", offset);
612 print_one_ipv4_rule(acl_config.rule_ipv4 + offset, 1);
618 dump_acl6_rule(struct rte_mbuf *m, uint32_t sig)
621 uint32_t offset = sig & ~ACL_DENY_SIGNATURE;
622 struct rte_ipv6_hdr *ipv6_hdr =
623 rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
624 sizeof(struct rte_ether_hdr));
626 printf("Packet Src");
627 for (i = 0; i < RTE_DIM(ipv6_hdr->src_addr); i += sizeof(uint16_t))
629 ipv6_hdr->src_addr[i], ipv6_hdr->src_addr[i + 1]);
632 for (i = 0; i < RTE_DIM(ipv6_hdr->dst_addr); i += sizeof(uint16_t))
634 ipv6_hdr->dst_addr[i], ipv6_hdr->dst_addr[i + 1]);
636 printf("\nSrc port:%hu,Dst port:%hu ",
637 rte_bswap16(*(uint16_t *)(ipv6_hdr + 1)),
638 rte_bswap16(*((uint16_t *)(ipv6_hdr + 1) + 1)));
639 printf("hit ACL %d - ", offset);
641 print_one_ipv6_rule(acl_config.rule_ipv6 + offset, 1);
645 #endif /* L3FWDACL_DEBUG */
648 dump_ipv4_rules(struct acl4_rule *rule, int num, int extra)
652 for (i = 0; i < num; i++, rule++) {
653 printf("\t%d:", i + 1);
654 print_one_ipv4_rule(rule, extra);
660 dump_ipv6_rules(struct acl6_rule *rule, int num, int extra)
664 for (i = 0; i < num; i++, rule++) {
665 printf("\t%d:", i + 1);
666 print_one_ipv6_rule(rule, extra);
671 #ifdef DO_RFC_1812_CHECKS
673 prepare_one_packet(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
676 struct rte_ipv4_hdr *ipv4_hdr;
677 struct rte_mbuf *pkt = pkts_in[index];
679 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
680 ipv4_hdr = rte_pktmbuf_mtod_offset(pkt, struct rte_ipv4_hdr *,
681 sizeof(struct rte_ether_hdr));
683 /* Check to make sure the packet is valid (RFC1812) */
684 if (is_valid_ipv4_pkt(ipv4_hdr, pkt->pkt_len) >= 0) {
686 /* Update time to live and header checksum */
687 --(ipv4_hdr->time_to_live);
688 ++(ipv4_hdr->hdr_checksum);
690 /* Fill acl structure */
691 acl->data_ipv4[acl->num_ipv4] = MBUF_IPV4_2PROTO(pkt);
692 acl->m_ipv4[(acl->num_ipv4)++] = pkt;
695 /* Not a valid IPv4 packet */
696 rte_pktmbuf_free(pkt);
698 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
699 /* Fill acl structure */
700 acl->data_ipv6[acl->num_ipv6] = MBUF_IPV6_2PROTO(pkt);
701 acl->m_ipv6[(acl->num_ipv6)++] = pkt;
704 /* Unknown type, drop the packet */
705 rte_pktmbuf_free(pkt);
711 prepare_one_packet(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
714 struct rte_mbuf *pkt = pkts_in[index];
716 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
717 /* Fill acl structure */
718 acl->data_ipv4[acl->num_ipv4] = MBUF_IPV4_2PROTO(pkt);
719 acl->m_ipv4[(acl->num_ipv4)++] = pkt;
721 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
722 /* Fill acl structure */
723 acl->data_ipv6[acl->num_ipv6] = MBUF_IPV6_2PROTO(pkt);
724 acl->m_ipv6[(acl->num_ipv6)++] = pkt;
726 /* Unknown type, drop the packet */
727 rte_pktmbuf_free(pkt);
730 #endif /* DO_RFC_1812_CHECKS */
733 prepare_acl_parameter(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
741 /* Prefetch first packets */
742 for (i = 0; i < PREFETCH_OFFSET && i < nb_rx; i++) {
743 rte_prefetch0(rte_pktmbuf_mtod(
744 pkts_in[i], void *));
747 for (i = 0; i < (nb_rx - PREFETCH_OFFSET); i++) {
748 rte_prefetch0(rte_pktmbuf_mtod(pkts_in[
749 i + PREFETCH_OFFSET], void *));
750 prepare_one_packet(pkts_in, acl, i);
753 /* Process left packets */
754 for (; i < nb_rx; i++)
755 prepare_one_packet(pkts_in, acl, i);
759 send_one_packet(struct rte_mbuf *m, uint32_t res)
761 if (likely((res & ACL_DENY_SIGNATURE) == 0 && res != 0)) {
762 /* forward packets */
763 send_single_packet(m,
764 (uint8_t)(res - FWD_PORT_SHIFT));
766 /* in the ACL list, drop it */
767 #ifdef L3FWDACL_DEBUG
768 if ((res & ACL_DENY_SIGNATURE) != 0) {
769 if (RTE_ETH_IS_IPV4_HDR(m->packet_type))
770 dump_acl4_rule(m, res);
771 else if (RTE_ETH_IS_IPV6_HDR(m->packet_type))
772 dump_acl6_rule(m, res);
782 send_packets(struct rte_mbuf **m, uint32_t *res, int num)
786 /* Prefetch first packets */
787 for (i = 0; i < PREFETCH_OFFSET && i < num; i++) {
788 rte_prefetch0(rte_pktmbuf_mtod(
792 for (i = 0; i < (num - PREFETCH_OFFSET); i++) {
793 rte_prefetch0(rte_pktmbuf_mtod(m[
794 i + PREFETCH_OFFSET], void *));
795 send_one_packet(m[i], res[i]);
798 /* Process left packets */
800 send_one_packet(m[i], res[i]);
804 * Parses IPV6 address, exepcts the following format:
805 * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX (where X - is a hexedecimal digit).
808 parse_ipv6_addr(const char *in, const char **end, uint32_t v[IPV6_ADDR_U32],
811 uint32_t addr[IPV6_ADDR_U16];
813 GET_CB_FIELD(in, addr[0], 16, UINT16_MAX, ':');
814 GET_CB_FIELD(in, addr[1], 16, UINT16_MAX, ':');
815 GET_CB_FIELD(in, addr[2], 16, UINT16_MAX, ':');
816 GET_CB_FIELD(in, addr[3], 16, UINT16_MAX, ':');
817 GET_CB_FIELD(in, addr[4], 16, UINT16_MAX, ':');
818 GET_CB_FIELD(in, addr[5], 16, UINT16_MAX, ':');
819 GET_CB_FIELD(in, addr[6], 16, UINT16_MAX, ':');
820 GET_CB_FIELD(in, addr[7], 16, UINT16_MAX, dlm);
824 v[0] = (addr[0] << 16) + addr[1];
825 v[1] = (addr[2] << 16) + addr[3];
826 v[2] = (addr[4] << 16) + addr[5];
827 v[3] = (addr[6] << 16) + addr[7];
833 parse_ipv6_net(const char *in, struct rte_acl_field field[4])
838 const uint32_t nbu32 = sizeof(uint32_t) * CHAR_BIT;
841 rc = parse_ipv6_addr(in, &mp, v, '/');
846 GET_CB_FIELD(mp, m, 0, CHAR_BIT * sizeof(v), 0);
848 /* put all together. */
849 for (i = 0; i != RTE_DIM(v); i++) {
850 if (m >= (i + 1) * nbu32)
851 field[i].mask_range.u32 = nbu32;
853 field[i].mask_range.u32 = m > (i * nbu32) ?
856 field[i].value.u32 = v[i];
863 parse_cb_ipv6_rule(char *str, struct rte_acl_rule *v, int has_userdata)
866 char *s, *sp, *in[CB_FLD_NUM];
867 static const char *dlm = " \t\n";
868 int dim = has_userdata ? CB_FLD_NUM : CB_FLD_USERDATA;
871 for (i = 0; i != dim; i++, s = NULL) {
872 in[i] = strtok_r(s, dlm, &sp);
877 rc = parse_ipv6_net(in[CB_FLD_SRC_ADDR], v->field + SRC1_FIELD_IPV6);
879 acl_log("failed to read source address/mask: %s\n",
880 in[CB_FLD_SRC_ADDR]);
884 rc = parse_ipv6_net(in[CB_FLD_DST_ADDR], v->field + DST1_FIELD_IPV6);
886 acl_log("failed to read destination address/mask: %s\n",
887 in[CB_FLD_DST_ADDR]);
892 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
893 v->field[SRCP_FIELD_IPV6].value.u16,
895 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
896 v->field[SRCP_FIELD_IPV6].mask_range.u16,
899 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
900 sizeof(cb_port_delim)) != 0)
903 /* destination port. */
904 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
905 v->field[DSTP_FIELD_IPV6].value.u16,
907 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
908 v->field[DSTP_FIELD_IPV6].mask_range.u16,
911 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
912 sizeof(cb_port_delim)) != 0)
915 if (v->field[SRCP_FIELD_IPV6].mask_range.u16
916 < v->field[SRCP_FIELD_IPV6].value.u16
917 || v->field[DSTP_FIELD_IPV6].mask_range.u16
918 < v->field[DSTP_FIELD_IPV6].value.u16)
921 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].value.u8,
923 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].mask_range.u8,
927 GET_CB_FIELD(in[CB_FLD_USERDATA], v->data.userdata,
934 * Parse ClassBench rules file.
936 * '@'<src_ipv4_addr>'/'<masklen> <space> \
937 * <dst_ipv4_addr>'/'<masklen> <space> \
938 * <src_port_low> <space> ":" <src_port_high> <space> \
939 * <dst_port_low> <space> ":" <dst_port_high> <space> \
943 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
945 uint8_t a, b, c, d, m;
947 GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
948 GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
949 GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
950 GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
951 GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
953 addr[0] = RTE_IPV4(a, b, c, d);
960 parse_cb_ipv4vlan_rule(char *str, struct rte_acl_rule *v, int has_userdata)
963 char *s, *sp, *in[CB_FLD_NUM];
964 static const char *dlm = " \t\n";
965 int dim = has_userdata ? CB_FLD_NUM : CB_FLD_USERDATA;
968 for (i = 0; i != dim; i++, s = NULL) {
969 in[i] = strtok_r(s, dlm, &sp);
974 rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
975 &v->field[SRC_FIELD_IPV4].value.u32,
976 &v->field[SRC_FIELD_IPV4].mask_range.u32);
978 acl_log("failed to read source address/mask: %s\n",
979 in[CB_FLD_SRC_ADDR]);
983 rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
984 &v->field[DST_FIELD_IPV4].value.u32,
985 &v->field[DST_FIELD_IPV4].mask_range.u32);
987 acl_log("failed to read destination address/mask: %s\n",
988 in[CB_FLD_DST_ADDR]);
992 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
993 v->field[SRCP_FIELD_IPV4].value.u16,
995 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
996 v->field[SRCP_FIELD_IPV4].mask_range.u16,
999 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
1000 sizeof(cb_port_delim)) != 0)
1003 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
1004 v->field[DSTP_FIELD_IPV4].value.u16,
1006 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
1007 v->field[DSTP_FIELD_IPV4].mask_range.u16,
1010 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
1011 sizeof(cb_port_delim)) != 0)
1014 if (v->field[SRCP_FIELD_IPV4].mask_range.u16
1015 < v->field[SRCP_FIELD_IPV4].value.u16
1016 || v->field[DSTP_FIELD_IPV4].mask_range.u16
1017 < v->field[DSTP_FIELD_IPV4].value.u16)
1020 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].value.u8,
1022 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].mask_range.u8,
1026 GET_CB_FIELD(in[CB_FLD_USERDATA], v->data.userdata, 0,
1033 add_rules(const char *rule_path,
1034 struct rte_acl_rule **proute_base,
1035 unsigned int *proute_num,
1036 struct rte_acl_rule **pacl_base,
1037 unsigned int *pacl_num, uint32_t rule_size,
1038 int (*parser)(char *, struct rte_acl_rule*, int))
1040 uint8_t *acl_rules, *route_rules;
1041 struct rte_acl_rule *next;
1042 unsigned int acl_num = 0, route_num = 0, total_num = 0;
1043 unsigned int acl_cnt = 0, route_cnt = 0;
1044 char buff[LINE_MAX];
1045 FILE *fh = fopen(rule_path, "rb");
1050 rte_exit(EXIT_FAILURE, "%s: Open %s failed\n", __func__,
1053 while ((fgets(buff, LINE_MAX, fh) != NULL)) {
1054 if (buff[0] == ROUTE_LEAD_CHAR)
1056 else if (buff[0] == ACL_LEAD_CHAR)
1061 rte_exit(EXIT_FAILURE, "Not find any route entries in %s!\n",
1064 val = fseek(fh, 0, SEEK_SET);
1066 rte_exit(EXIT_FAILURE, "%s: File seek operation failed\n",
1070 acl_rules = calloc(acl_num, rule_size);
1072 if (NULL == acl_rules)
1073 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1076 route_rules = calloc(route_num, rule_size);
1078 if (NULL == route_rules)
1079 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1083 while (fgets(buff, LINE_MAX, fh) != NULL) {
1086 if (is_bypass_line(buff))
1092 if (s == ROUTE_LEAD_CHAR)
1093 next = (struct rte_acl_rule *)(route_rules +
1094 route_cnt * rule_size);
1097 else if (s == ACL_LEAD_CHAR)
1098 next = (struct rte_acl_rule *)(acl_rules +
1099 acl_cnt * rule_size);
1103 rte_exit(EXIT_FAILURE,
1104 "%s Line %u: should start with leading "
1106 rule_path, i, ROUTE_LEAD_CHAR, ACL_LEAD_CHAR);
1108 if (parser(buff + 1, next, s == ROUTE_LEAD_CHAR) != 0)
1109 rte_exit(EXIT_FAILURE,
1110 "%s Line %u: parse rules error\n",
1113 if (s == ROUTE_LEAD_CHAR) {
1114 /* Check the forwarding port number */
1115 if ((enabled_port_mask & (1 << next->data.userdata)) ==
1117 rte_exit(EXIT_FAILURE,
1118 "%s Line %u: fwd number illegal:%u\n",
1119 rule_path, i, next->data.userdata);
1120 next->data.userdata += FWD_PORT_SHIFT;
1123 next->data.userdata = ACL_DENY_SIGNATURE + acl_cnt;
1127 next->data.priority = RTE_ACL_MAX_PRIORITY - total_num;
1128 next->data.category_mask = -1;
1134 *pacl_base = (struct rte_acl_rule *)acl_rules;
1135 *pacl_num = acl_num;
1136 *proute_base = (struct rte_acl_rule *)route_rules;
1137 *proute_num = route_cnt;
1143 usage_acl_alg(char *buf, size_t sz)
1145 uint32_t i, n, rc, tn;
1149 for (i = 0; i < RTE_DIM(acl_alg); i++) {
1150 rc = snprintf(buf + n, sz - n,
1151 i == RTE_DIM(acl_alg) - 1 ? "%s" : "%s|",
1162 str_acl_alg(enum rte_acl_classify_alg alg)
1166 for (i = 0; i != RTE_DIM(acl_alg); i++) {
1167 if (alg == acl_alg[i].alg)
1168 return acl_alg[i].name;
1174 static enum rte_acl_classify_alg
1175 parse_acl_alg(const char *alg)
1179 for (i = 0; i != RTE_DIM(acl_alg); i++) {
1180 if (strcmp(alg, acl_alg[i].name) == 0)
1181 return acl_alg[i].alg;
1184 return RTE_ACL_CLASSIFY_DEFAULT;
1188 dump_acl_config(void)
1190 printf("ACL option are:\n");
1191 printf(OPT_RULE_IPV4": %s\n", parm_config.rule_ipv4_name);
1192 printf(OPT_RULE_IPV6": %s\n", parm_config.rule_ipv6_name);
1193 printf(OPT_ALG": %s\n", str_acl_alg(parm_config.alg));
1197 check_acl_config(void)
1199 if (parm_config.rule_ipv4_name == NULL) {
1200 acl_log("ACL IPv4 rule file not specified\n");
1202 } else if (parm_config.rule_ipv6_name == NULL) {
1203 acl_log("ACL IPv6 rule file not specified\n");
1210 static struct rte_acl_ctx*
1211 setup_acl(struct rte_acl_rule *route_base,
1212 struct rte_acl_rule *acl_base, unsigned int route_num,
1213 unsigned int acl_num, int ipv6, int socketid)
1215 char name[PATH_MAX];
1216 struct rte_acl_param acl_param;
1217 struct rte_acl_config acl_build_param;
1218 struct rte_acl_ctx *context;
1219 int dim = ipv6 ? RTE_DIM(ipv6_defs) : RTE_DIM(ipv4_defs);
1221 /* Create ACL contexts */
1222 snprintf(name, sizeof(name), "%s%d",
1223 ipv6 ? L3FWD_ACL_IPV6_NAME : L3FWD_ACL_IPV4_NAME,
1226 acl_param.name = name;
1227 acl_param.socket_id = socketid;
1228 acl_param.rule_size = RTE_ACL_RULE_SZ(dim);
1229 acl_param.max_rule_num = MAX_ACL_RULE_NUM;
1231 if ((context = rte_acl_create(&acl_param)) == NULL)
1232 rte_exit(EXIT_FAILURE, "Failed to create ACL context\n");
1234 if (parm_config.alg != RTE_ACL_CLASSIFY_DEFAULT &&
1235 rte_acl_set_ctx_classify(context, parm_config.alg) != 0)
1236 rte_exit(EXIT_FAILURE,
1237 "Failed to setup classify method for ACL context\n");
1239 if (rte_acl_add_rules(context, route_base, route_num) < 0)
1240 rte_exit(EXIT_FAILURE, "add rules failed\n");
1242 if (rte_acl_add_rules(context, acl_base, acl_num) < 0)
1243 rte_exit(EXIT_FAILURE, "add rules failed\n");
1245 /* Perform builds */
1246 memset(&acl_build_param, 0, sizeof(acl_build_param));
1248 acl_build_param.num_categories = DEFAULT_MAX_CATEGORIES;
1249 acl_build_param.num_fields = dim;
1250 memcpy(&acl_build_param.defs, ipv6 ? ipv6_defs : ipv4_defs,
1251 ipv6 ? sizeof(ipv6_defs) : sizeof(ipv4_defs));
1253 if (rte_acl_build(context, &acl_build_param) != 0)
1254 rte_exit(EXIT_FAILURE, "Failed to build ACL trie\n");
1256 rte_acl_dump(context);
1267 struct rte_acl_rule *acl_base_ipv4, *route_base_ipv4,
1268 *acl_base_ipv6, *route_base_ipv6;
1269 unsigned int acl_num_ipv4 = 0, route_num_ipv4 = 0,
1270 acl_num_ipv6 = 0, route_num_ipv6 = 0;
1272 if (check_acl_config() != 0)
1273 rte_exit(EXIT_FAILURE, "Failed to get valid ACL options\n");
1277 /* Load rules from the input file */
1278 if (add_rules(parm_config.rule_ipv4_name, &route_base_ipv4,
1279 &route_num_ipv4, &acl_base_ipv4, &acl_num_ipv4,
1280 sizeof(struct acl4_rule), &parse_cb_ipv4vlan_rule) < 0)
1281 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1283 acl_log("IPv4 Route entries %u:\n", route_num_ipv4);
1284 dump_ipv4_rules((struct acl4_rule *)route_base_ipv4, route_num_ipv4, 1);
1286 acl_log("IPv4 ACL entries %u:\n", acl_num_ipv4);
1287 dump_ipv4_rules((struct acl4_rule *)acl_base_ipv4, acl_num_ipv4, 1);
1289 if (add_rules(parm_config.rule_ipv6_name, &route_base_ipv6,
1291 &acl_base_ipv6, &acl_num_ipv6,
1292 sizeof(struct acl6_rule), &parse_cb_ipv6_rule) < 0)
1293 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1295 acl_log("IPv6 Route entries %u:\n", route_num_ipv6);
1296 dump_ipv6_rules((struct acl6_rule *)route_base_ipv6, route_num_ipv6, 1);
1298 acl_log("IPv6 ACL entries %u:\n", acl_num_ipv6);
1299 dump_ipv6_rules((struct acl6_rule *)acl_base_ipv6, acl_num_ipv6, 1);
1301 memset(&acl_config, 0, sizeof(acl_config));
1303 /* Check sockets a context should be created on */
1305 acl_config.mapped[0] = 1;
1307 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1308 if (rte_lcore_is_enabled(lcore_id) == 0)
1311 socketid = rte_lcore_to_socket_id(lcore_id);
1312 if (socketid >= NB_SOCKETS) {
1313 acl_log("Socket %d of lcore %u is out "
1315 socketid, lcore_id, NB_SOCKETS);
1316 free(route_base_ipv4);
1317 free(route_base_ipv6);
1318 free(acl_base_ipv4);
1319 free(acl_base_ipv6);
1323 acl_config.mapped[socketid] = 1;
1327 for (i = 0; i < NB_SOCKETS; i++) {
1328 if (acl_config.mapped[i]) {
1329 acl_config.acx_ipv4[i] = setup_acl(route_base_ipv4,
1330 acl_base_ipv4, route_num_ipv4, acl_num_ipv4,
1333 acl_config.acx_ipv6[i] = setup_acl(route_base_ipv6,
1334 acl_base_ipv6, route_num_ipv6, acl_num_ipv6,
1339 free(route_base_ipv4);
1340 free(route_base_ipv6);
1342 #ifdef L3FWDACL_DEBUG
1343 acl_config.rule_ipv4 = (struct acl4_rule *)acl_base_ipv4;
1344 acl_config.rule_ipv6 = (struct acl6_rule *)acl_base_ipv6;
1346 free(acl_base_ipv4);
1347 free(acl_base_ipv6);
1353 /***********************end of ACL part******************************/
1356 uint16_t n_rx_queue;
1357 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
1359 uint16_t tx_port_id[RTE_MAX_ETHPORTS];
1360 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
1361 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
1362 } __rte_cache_aligned;
1364 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
1366 /* Enqueue a single packet, and send burst if queue is filled */
1368 send_single_packet(struct rte_mbuf *m, uint16_t port)
1371 struct lcore_conf *qconf;
1372 struct rte_ether_hdr *eh;
1374 lcore_id = rte_lcore_id();
1376 /* update src and dst mac*/
1377 eh = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1378 memcpy(eh, &port_l2hdr[port], sizeof(eh->d_addr) + sizeof(eh->s_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, &port_l2hdr[portid].d_addr,
1747 sizeof(port_l2hdr[portid].d_addr)) < 0)
1748 return "Invalid ethernet address";
1752 /* Parse the argument given in the command line of the application */
1754 parse_args(int argc, char **argv)
1759 char *prgname = argv[0];
1760 static struct option lgopts[] = {
1761 {OPT_CONFIG, 1, NULL, OPT_CONFIG_NUM },
1762 {OPT_NONUMA, 0, NULL, OPT_NONUMA_NUM },
1763 {OPT_ENBJMO, 0, NULL, OPT_ENBJMO_NUM },
1764 {OPT_RULE_IPV4, 1, NULL, OPT_RULE_IPV4_NUM },
1765 {OPT_RULE_IPV6, 1, NULL, OPT_RULE_IPV6_NUM },
1766 {OPT_ALG, 1, NULL, OPT_ALG_NUM },
1767 {OPT_ETH_DEST, 1, NULL, OPT_ETH_DEST_NUM },
1773 while ((opt = getopt_long(argc, argvopt, "p:P",
1774 lgopts, &option_index)) != EOF) {
1779 enabled_port_mask = parse_portmask(optarg);
1780 if (enabled_port_mask == 0) {
1781 printf("invalid portmask\n");
1782 print_usage(prgname);
1788 printf("Promiscuous mode selected\n");
1793 case OPT_CONFIG_NUM:
1794 ret = parse_config(optarg);
1796 printf("invalid config\n");
1797 print_usage(prgname);
1802 case OPT_NONUMA_NUM:
1803 printf("numa is disabled\n");
1807 case OPT_ENBJMO_NUM:
1809 struct option lenopts = {
1816 printf("jumbo frame is enabled\n");
1817 port_conf.rxmode.offloads |=
1818 DEV_RX_OFFLOAD_JUMBO_FRAME;
1819 port_conf.txmode.offloads |=
1820 DEV_TX_OFFLOAD_MULTI_SEGS;
1823 * if no max-pkt-len set, then use the
1824 * default value RTE_ETHER_MAX_LEN
1826 if (getopt_long(argc, argvopt, "",
1827 &lenopts, &option_index) == 0) {
1828 ret = parse_max_pkt_len(optarg);
1830 (ret > MAX_JUMBO_PKT_LEN)) {
1831 printf("invalid packet "
1833 print_usage(prgname);
1836 port_conf.rxmode.max_rx_pkt_len = ret;
1838 printf("set jumbo frame max packet length "
1841 port_conf.rxmode.max_rx_pkt_len);
1844 case OPT_RULE_IPV4_NUM:
1845 parm_config.rule_ipv4_name = optarg;
1848 case OPT_RULE_IPV6_NUM:
1849 parm_config.rule_ipv6_name = optarg;
1853 parm_config.alg = parse_acl_alg(optarg);
1854 if (parm_config.alg ==
1855 RTE_ACL_CLASSIFY_DEFAULT) {
1856 printf("unknown %s value:\"%s\"\n",
1858 print_usage(prgname);
1863 case OPT_ETH_DEST_NUM:
1865 const char *serr = parse_eth_dest(optarg);
1867 printf("invalid %s value:\"%s\": %s\n",
1868 OPT_ETH_DEST, optarg, serr);
1869 print_usage(prgname);
1875 print_usage(prgname);
1881 argv[optind-1] = prgname;
1884 optind = 1; /* reset getopt lib */
1889 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
1891 char buf[RTE_ETHER_ADDR_FMT_SIZE];
1892 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
1893 printf("%s%s", name, buf);
1897 init_mem(unsigned nb_mbuf)
1903 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1904 if (rte_lcore_is_enabled(lcore_id) == 0)
1908 socketid = rte_lcore_to_socket_id(lcore_id);
1912 if (socketid >= NB_SOCKETS) {
1913 rte_exit(EXIT_FAILURE,
1914 "Socket %d of lcore %u is out of range %d\n",
1915 socketid, lcore_id, NB_SOCKETS);
1917 if (pktmbuf_pool[socketid] == NULL) {
1918 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
1919 pktmbuf_pool[socketid] =
1920 rte_pktmbuf_pool_create(s, nb_mbuf,
1921 MEMPOOL_CACHE_SIZE, 0,
1922 RTE_MBUF_DEFAULT_BUF_SIZE,
1924 if (pktmbuf_pool[socketid] == NULL)
1925 rte_exit(EXIT_FAILURE,
1926 "Cannot init mbuf pool on socket %d\n",
1929 printf("Allocated mbuf pool on socket %d\n",
1936 /* Check the link status of all ports in up to 9s, and print them finally */
1938 check_all_ports_link_status(uint32_t port_mask)
1940 #define CHECK_INTERVAL 100 /* 100ms */
1941 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1943 uint8_t count, all_ports_up, print_flag = 0;
1944 struct rte_eth_link link;
1946 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
1948 printf("\nChecking link status");
1950 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1952 RTE_ETH_FOREACH_DEV(portid) {
1953 if ((port_mask & (1 << portid)) == 0)
1955 memset(&link, 0, sizeof(link));
1956 ret = rte_eth_link_get_nowait(portid, &link);
1959 if (print_flag == 1)
1960 printf("Port %u link get failed: %s\n",
1961 portid, rte_strerror(-ret));
1964 /* print link status if flag set */
1965 if (print_flag == 1) {
1966 rte_eth_link_to_str(link_status_text,
1967 sizeof(link_status_text), &link);
1968 printf("Port %d %s\n", portid,
1972 /* clear all_ports_up flag if any link down */
1973 if (link.link_status == ETH_LINK_DOWN) {
1978 /* after finally printing all link status, get out */
1979 if (print_flag == 1)
1982 if (all_ports_up == 0) {
1985 rte_delay_ms(CHECK_INTERVAL);
1988 /* set the print_flag if all ports up or timeout */
1989 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1997 * build-up default vaues for dest MACs.
2000 set_default_dest_mac(void)
2004 for (i = 0; i != RTE_DIM(port_l2hdr); i++) {
2005 port_l2hdr[i].d_addr.addr_bytes[0] = RTE_ETHER_LOCAL_ADMIN_ADDR;
2006 port_l2hdr[i].d_addr.addr_bytes[5] = i;
2011 main(int argc, char **argv)
2013 struct lcore_conf *qconf;
2014 struct rte_eth_dev_info dev_info;
2015 struct rte_eth_txconf *txconf;
2020 uint32_t n_tx_queue, nb_lcores;
2022 uint8_t nb_rx_queue, queue, socketid;
2025 ret = rte_eal_init(argc, argv);
2027 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2031 set_default_dest_mac();
2033 /* parse application arguments (after the EAL ones) */
2034 ret = parse_args(argc, argv);
2036 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
2038 if (check_lcore_params() < 0)
2039 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
2041 ret = init_lcore_rx_queues();
2043 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2045 nb_ports = rte_eth_dev_count_avail();
2047 if (check_port_config() < 0)
2048 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
2050 /* Add ACL rules and route entries, build trie */
2051 if (app_acl_init() < 0)
2052 rte_exit(EXIT_FAILURE, "app_acl_init failed\n");
2054 nb_lcores = rte_lcore_count();
2056 /* initialize all ports */
2057 RTE_ETH_FOREACH_DEV(portid) {
2058 struct rte_eth_conf local_port_conf = port_conf;
2060 /* skip ports that are not enabled */
2061 if ((enabled_port_mask & (1 << portid)) == 0) {
2062 printf("\nSkipping disabled port %d\n", portid);
2067 printf("Initializing port %d ... ", portid);
2070 nb_rx_queue = get_port_n_rx_queues(portid);
2071 n_tx_queue = nb_lcores;
2072 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
2073 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
2074 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
2075 nb_rx_queue, (unsigned)n_tx_queue);
2077 ret = rte_eth_dev_info_get(portid, &dev_info);
2079 rte_exit(EXIT_FAILURE,
2080 "Error during getting device (port %u) info: %s\n",
2081 portid, strerror(-ret));
2083 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2084 local_port_conf.txmode.offloads |=
2085 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2087 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
2088 dev_info.flow_type_rss_offloads;
2089 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
2090 port_conf.rx_adv_conf.rss_conf.rss_hf) {
2091 printf("Port %u modified RSS hash function based on hardware support,"
2092 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
2094 port_conf.rx_adv_conf.rss_conf.rss_hf,
2095 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
2098 ret = rte_eth_dev_configure(portid, nb_rx_queue,
2099 (uint16_t)n_tx_queue, &local_port_conf);
2101 rte_exit(EXIT_FAILURE,
2102 "Cannot configure device: err=%d, port=%d\n",
2105 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2108 rte_exit(EXIT_FAILURE,
2109 "rte_eth_dev_adjust_nb_rx_tx_desc: err=%d, port=%d\n",
2112 ret = rte_eth_macaddr_get(portid, &port_l2hdr[portid].s_addr);
2114 rte_exit(EXIT_FAILURE,
2115 "rte_eth_macaddr_get: err=%d, port=%d\n",
2118 print_ethaddr("Dst MAC:", &port_l2hdr[portid].d_addr);
2119 print_ethaddr(", Src MAC:", &port_l2hdr[portid].s_addr);
2123 ret = init_mem(NB_MBUF);
2125 rte_exit(EXIT_FAILURE, "init_mem failed\n");
2127 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2128 if (rte_lcore_is_enabled(lcore_id) == 0)
2131 /* Initialize TX buffers */
2132 qconf = &lcore_conf[lcore_id];
2133 qconf->tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
2134 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
2135 rte_eth_dev_socket_id(portid));
2136 if (qconf->tx_buffer[portid] == NULL)
2137 rte_exit(EXIT_FAILURE, "Can't allocate tx buffer for port %u\n",
2140 rte_eth_tx_buffer_init(qconf->tx_buffer[portid], MAX_PKT_BURST);
2143 /* init one TX queue per couple (lcore,port) */
2145 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2146 if (rte_lcore_is_enabled(lcore_id) == 0)
2150 socketid = (uint8_t)
2151 rte_lcore_to_socket_id(lcore_id);
2155 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
2158 ret = rte_eth_dev_info_get(portid, &dev_info);
2160 rte_exit(EXIT_FAILURE,
2161 "Error during getting device (port %u) info: %s\n",
2162 portid, strerror(-ret));
2164 txconf = &dev_info.default_txconf;
2165 txconf->offloads = local_port_conf.txmode.offloads;
2166 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2169 rte_exit(EXIT_FAILURE,
2170 "rte_eth_tx_queue_setup: err=%d, "
2171 "port=%d\n", ret, portid);
2173 qconf = &lcore_conf[lcore_id];
2174 qconf->tx_queue_id[portid] = queueid;
2177 qconf->tx_port_id[qconf->n_tx_port] = portid;
2183 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2184 if (rte_lcore_is_enabled(lcore_id) == 0)
2186 qconf = &lcore_conf[lcore_id];
2187 printf("\nInitializing rx queues on lcore %u ... ", lcore_id);
2189 /* init RX queues */
2190 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
2191 struct rte_eth_rxconf rxq_conf;
2193 portid = qconf->rx_queue_list[queue].port_id;
2194 queueid = qconf->rx_queue_list[queue].queue_id;
2197 socketid = (uint8_t)
2198 rte_lcore_to_socket_id(lcore_id);
2202 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2205 ret = rte_eth_dev_info_get(portid, &dev_info);
2207 rte_exit(EXIT_FAILURE,
2208 "Error during getting device (port %u) info: %s\n",
2209 portid, strerror(-ret));
2211 rxq_conf = dev_info.default_rxconf;
2212 rxq_conf.offloads = port_conf.rxmode.offloads;
2213 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2214 socketid, &rxq_conf,
2215 pktmbuf_pool[socketid]);
2217 rte_exit(EXIT_FAILURE,
2218 "rte_eth_rx_queue_setup: err=%d,"
2219 "port=%d\n", ret, portid);
2226 RTE_ETH_FOREACH_DEV(portid) {
2227 if ((enabled_port_mask & (1 << portid)) == 0)
2231 ret = rte_eth_dev_start(portid);
2233 rte_exit(EXIT_FAILURE,
2234 "rte_eth_dev_start: err=%d, port=%d\n",
2238 * If enabled, put device in promiscuous mode.
2239 * This allows IO forwarding mode to forward packets
2240 * to itself through 2 cross-connected ports of the
2243 if (promiscuous_on) {
2244 ret = rte_eth_promiscuous_enable(portid);
2246 rte_exit(EXIT_FAILURE,
2247 "rte_eth_promiscuous_enable: err=%s, port=%u\n",
2248 rte_strerror(-ret), portid);
2252 check_all_ports_link_status(enabled_port_mask);
2254 /* launch per-lcore init on every lcore */
2255 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MAIN);
2256 RTE_LCORE_FOREACH_WORKER(lcore_id) {
2257 if (rte_eal_wait_lcore(lcore_id) < 0)
2261 /* clean up the EAL */