4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/types.h>
40 #include <sys/queue.h>
45 #include <rte_common.h>
46 #include <rte_byteorder.h>
48 #include <rte_memory.h>
49 #include <rte_memcpy.h>
50 #include <rte_memzone.h>
52 #include <rte_per_lcore.h>
53 #include <rte_launch.h>
54 #include <rte_atomic.h>
55 #include <rte_cycles.h>
56 #include <rte_prefetch.h>
57 #include <rte_lcore.h>
58 #include <rte_per_lcore.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_interrupts.h>
62 #include <rte_random.h>
63 #include <rte_debug.h>
64 #include <rte_ether.h>
65 #include <rte_ethdev.h>
66 #include <rte_mempool.h>
71 #include <rte_string_fns.h>
74 #if RTE_LOG_LEVEL >= RTE_LOG_DEBUG
75 #define L3FWDACL_DEBUG
77 #define DO_RFC_1812_CHECKS
79 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
81 #define MAX_JUMBO_PKT_LEN 9600
83 #define MEMPOOL_CACHE_SIZE 256
86 * This expression is used to calculate the number of mbufs needed
87 * depending on user input, taking into account memory for rx and tx hardware
88 * rings, cache per lcore and mtable per port per lcore.
89 * RTE_MAX is used to ensure that NB_MBUF never goes below a
90 * minimum value of 8192
93 #define NB_MBUF RTE_MAX(\
94 (nb_ports * nb_rx_queue*RTE_TEST_RX_DESC_DEFAULT + \
95 nb_ports * nb_lcores * MAX_PKT_BURST + \
96 nb_ports * n_tx_queue * RTE_TEST_TX_DESC_DEFAULT + \
97 nb_lcores * MEMPOOL_CACHE_SIZE), \
100 #define MAX_PKT_BURST 32
101 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
105 /* Configure how many packets ahead to prefetch, when reading packets */
106 #define PREFETCH_OFFSET 3
109 * Configurable number of RX/TX ring descriptors
111 #define RTE_TEST_RX_DESC_DEFAULT 128
112 #define RTE_TEST_TX_DESC_DEFAULT 512
113 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
114 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
116 /* ethernet addresses of ports */
117 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
119 /* mask of enabled ports */
120 static uint32_t enabled_port_mask;
121 static int promiscuous_on; /**< Ports set in promiscuous mode off by default. */
122 static int numa_on = 1; /**< NUMA is enabled by default. */
124 struct lcore_rx_queue {
127 } __rte_cache_aligned;
129 #define MAX_RX_QUEUE_PER_LCORE 16
130 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
131 #define MAX_RX_QUEUE_PER_PORT 128
133 #define MAX_LCORE_PARAMS 1024
134 struct lcore_params {
138 } __rte_cache_aligned;
140 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
141 static struct lcore_params lcore_params_array_default[] = {
153 static struct lcore_params *lcore_params = lcore_params_array_default;
154 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
155 sizeof(lcore_params_array_default[0]);
157 static struct rte_eth_conf port_conf = {
159 .mq_mode = ETH_MQ_RX_RSS,
160 .max_rx_pkt_len = ETHER_MAX_LEN,
162 .header_split = 0, /**< Header Split disabled */
163 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
164 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
165 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
166 .hw_strip_crc = 0, /**< CRC stripped by hardware */
171 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
172 ETH_RSS_TCP | ETH_RSS_SCTP,
176 .mq_mode = ETH_MQ_TX_NONE,
180 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
182 /***********************start of ACL part******************************/
183 #ifdef DO_RFC_1812_CHECKS
185 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len);
188 send_single_packet(struct rte_mbuf *m, uint8_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 ('#')
197 #define OPTION_CONFIG "config"
198 #define OPTION_NONUMA "no-numa"
199 #define OPTION_ENBJMO "enable-jumbo"
200 #define OPTION_RULE_IPV4 "rule_ipv4"
201 #define OPTION_RULE_IPV6 "rule_ipv6"
202 #define OPTION_SCALAR "scalar"
203 #define ACL_DENY_SIGNATURE 0xf0000000
204 #define RTE_LOGTYPE_L3FWDACL RTE_LOGTYPE_USER3
205 #define acl_log(format, ...) RTE_LOG(ERR, L3FWDACL, format, ##__VA_ARGS__)
206 #define uint32_t_to_char(ip, a, b, c, d) do {\
207 *a = (unsigned char)(ip >> 24 & 0xff);\
208 *b = (unsigned char)(ip >> 16 & 0xff);\
209 *c = (unsigned char)(ip >> 8 & 0xff);\
210 *d = (unsigned char)(ip & 0xff);\
212 #define OFF_ETHHEAD (sizeof(struct ether_hdr))
213 #define OFF_IPV42PROTO (offsetof(struct ipv4_hdr, next_proto_id))
214 #define OFF_IPV62PROTO (offsetof(struct ipv6_hdr, proto))
215 #define MBUF_IPV4_2PROTO(m) \
216 rte_pktmbuf_mtod_offset((m), uint8_t *, OFF_ETHHEAD + OFF_IPV42PROTO)
217 #define MBUF_IPV6_2PROTO(m) \
218 rte_pktmbuf_mtod_offset((m), uint8_t *, OFF_ETHHEAD + OFF_IPV62PROTO)
220 #define GET_CB_FIELD(in, fd, base, lim, dlm) do { \
224 val = strtoul((in), &end, (base)); \
225 if (errno != 0 || end[0] != (dlm) || val > (lim)) \
227 (fd) = (typeof(fd))val; \
232 * ACL rules should have higher priorities than route ones to ensure ACL rule
233 * always be found when input packets have multi-matches in the database.
234 * A exception case is performance measure, which can define route rules with
235 * higher priority and route rules will always be returned in each lookup.
236 * Reserve range from ACL_RULE_PRIORITY_MAX + 1 to
237 * RTE_ACL_MAX_PRIORITY for route entries in performance measure
239 #define ACL_RULE_PRIORITY_MAX 0x10000000
242 * Forward port info save in ACL lib starts from 1
243 * since ACL assume 0 is invalid.
244 * So, need add 1 when saving and minus 1 when forwarding packets.
246 #define FWD_PORT_SHIFT 1
249 * Rule and trace formats definitions.
262 * That effectively defines order of IPV4VLAN classifications:
264 * - VLAN (TAG and DOMAIN)
267 * - PORTS (SRC and DST)
270 RTE_ACL_IPV4VLAN_PROTO,
271 RTE_ACL_IPV4VLAN_VLAN,
272 RTE_ACL_IPV4VLAN_SRC,
273 RTE_ACL_IPV4VLAN_DST,
274 RTE_ACL_IPV4VLAN_PORTS,
278 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
280 .type = RTE_ACL_FIELD_TYPE_BITMASK,
281 .size = sizeof(uint8_t),
282 .field_index = PROTO_FIELD_IPV4,
283 .input_index = RTE_ACL_IPV4VLAN_PROTO,
287 .type = RTE_ACL_FIELD_TYPE_MASK,
288 .size = sizeof(uint32_t),
289 .field_index = SRC_FIELD_IPV4,
290 .input_index = RTE_ACL_IPV4VLAN_SRC,
291 .offset = offsetof(struct ipv4_hdr, src_addr) -
292 offsetof(struct ipv4_hdr, next_proto_id),
295 .type = RTE_ACL_FIELD_TYPE_MASK,
296 .size = sizeof(uint32_t),
297 .field_index = DST_FIELD_IPV4,
298 .input_index = RTE_ACL_IPV4VLAN_DST,
299 .offset = offsetof(struct ipv4_hdr, dst_addr) -
300 offsetof(struct ipv4_hdr, next_proto_id),
303 .type = RTE_ACL_FIELD_TYPE_RANGE,
304 .size = sizeof(uint16_t),
305 .field_index = SRCP_FIELD_IPV4,
306 .input_index = RTE_ACL_IPV4VLAN_PORTS,
307 .offset = sizeof(struct ipv4_hdr) -
308 offsetof(struct ipv4_hdr, next_proto_id),
311 .type = RTE_ACL_FIELD_TYPE_RANGE,
312 .size = sizeof(uint16_t),
313 .field_index = DSTP_FIELD_IPV4,
314 .input_index = RTE_ACL_IPV4VLAN_PORTS,
315 .offset = sizeof(struct ipv4_hdr) -
316 offsetof(struct ipv4_hdr, next_proto_id) +
321 #define IPV6_ADDR_LEN 16
322 #define IPV6_ADDR_U16 (IPV6_ADDR_LEN / sizeof(uint16_t))
323 #define IPV6_ADDR_U32 (IPV6_ADDR_LEN / sizeof(uint32_t))
340 struct rte_acl_field_def ipv6_defs[NUM_FIELDS_IPV6] = {
342 .type = RTE_ACL_FIELD_TYPE_BITMASK,
343 .size = sizeof(uint8_t),
344 .field_index = PROTO_FIELD_IPV6,
345 .input_index = PROTO_FIELD_IPV6,
349 .type = RTE_ACL_FIELD_TYPE_MASK,
350 .size = sizeof(uint32_t),
351 .field_index = SRC1_FIELD_IPV6,
352 .input_index = SRC1_FIELD_IPV6,
353 .offset = offsetof(struct ipv6_hdr, src_addr) -
354 offsetof(struct ipv6_hdr, proto),
357 .type = RTE_ACL_FIELD_TYPE_MASK,
358 .size = sizeof(uint32_t),
359 .field_index = SRC2_FIELD_IPV6,
360 .input_index = SRC2_FIELD_IPV6,
361 .offset = offsetof(struct ipv6_hdr, src_addr) -
362 offsetof(struct ipv6_hdr, proto) + sizeof(uint32_t),
365 .type = RTE_ACL_FIELD_TYPE_MASK,
366 .size = sizeof(uint32_t),
367 .field_index = SRC3_FIELD_IPV6,
368 .input_index = SRC3_FIELD_IPV6,
369 .offset = offsetof(struct ipv6_hdr, src_addr) -
370 offsetof(struct ipv6_hdr, proto) + 2 * sizeof(uint32_t),
373 .type = RTE_ACL_FIELD_TYPE_MASK,
374 .size = sizeof(uint32_t),
375 .field_index = SRC4_FIELD_IPV6,
376 .input_index = SRC4_FIELD_IPV6,
377 .offset = offsetof(struct ipv6_hdr, src_addr) -
378 offsetof(struct ipv6_hdr, proto) + 3 * sizeof(uint32_t),
381 .type = RTE_ACL_FIELD_TYPE_MASK,
382 .size = sizeof(uint32_t),
383 .field_index = DST1_FIELD_IPV6,
384 .input_index = DST1_FIELD_IPV6,
385 .offset = offsetof(struct ipv6_hdr, dst_addr)
386 - offsetof(struct ipv6_hdr, proto),
389 .type = RTE_ACL_FIELD_TYPE_MASK,
390 .size = sizeof(uint32_t),
391 .field_index = DST2_FIELD_IPV6,
392 .input_index = DST2_FIELD_IPV6,
393 .offset = offsetof(struct ipv6_hdr, dst_addr) -
394 offsetof(struct ipv6_hdr, proto) + sizeof(uint32_t),
397 .type = RTE_ACL_FIELD_TYPE_MASK,
398 .size = sizeof(uint32_t),
399 .field_index = DST3_FIELD_IPV6,
400 .input_index = DST3_FIELD_IPV6,
401 .offset = offsetof(struct ipv6_hdr, dst_addr) -
402 offsetof(struct ipv6_hdr, proto) + 2 * sizeof(uint32_t),
405 .type = RTE_ACL_FIELD_TYPE_MASK,
406 .size = sizeof(uint32_t),
407 .field_index = DST4_FIELD_IPV6,
408 .input_index = DST4_FIELD_IPV6,
409 .offset = offsetof(struct ipv6_hdr, dst_addr) -
410 offsetof(struct ipv6_hdr, proto) + 3 * sizeof(uint32_t),
413 .type = RTE_ACL_FIELD_TYPE_RANGE,
414 .size = sizeof(uint16_t),
415 .field_index = SRCP_FIELD_IPV6,
416 .input_index = SRCP_FIELD_IPV6,
417 .offset = sizeof(struct ipv6_hdr) -
418 offsetof(struct ipv6_hdr, proto),
421 .type = RTE_ACL_FIELD_TYPE_RANGE,
422 .size = sizeof(uint16_t),
423 .field_index = DSTP_FIELD_IPV6,
424 .input_index = SRCP_FIELD_IPV6,
425 .offset = sizeof(struct ipv6_hdr) -
426 offsetof(struct ipv6_hdr, proto) + sizeof(uint16_t),
435 CB_FLD_SRC_PORT_HIGH,
438 CB_FLD_DST_PORT_HIGH,
444 RTE_ACL_RULE_DEF(acl4_rule, RTE_DIM(ipv4_defs));
445 RTE_ACL_RULE_DEF(acl6_rule, RTE_DIM(ipv6_defs));
447 struct acl_search_t {
448 const uint8_t *data_ipv4[MAX_PKT_BURST];
449 struct rte_mbuf *m_ipv4[MAX_PKT_BURST];
450 uint32_t res_ipv4[MAX_PKT_BURST];
453 const uint8_t *data_ipv6[MAX_PKT_BURST];
454 struct rte_mbuf *m_ipv6[MAX_PKT_BURST];
455 uint32_t res_ipv6[MAX_PKT_BURST];
460 char mapped[NB_SOCKETS];
461 struct rte_acl_ctx *acx_ipv4[NB_SOCKETS];
462 struct rte_acl_ctx *acx_ipv6[NB_SOCKETS];
463 #ifdef L3FWDACL_DEBUG
464 struct acl4_rule *rule_ipv4;
465 struct acl6_rule *rule_ipv6;
470 const char *rule_ipv4_name;
471 const char *rule_ipv6_name;
475 const char cb_port_delim[] = ":";
478 print_one_ipv4_rule(struct acl4_rule *rule, int extra)
480 unsigned char a, b, c, d;
482 uint32_t_to_char(rule->field[SRC_FIELD_IPV4].value.u32,
484 printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
485 rule->field[SRC_FIELD_IPV4].mask_range.u32);
486 uint32_t_to_char(rule->field[DST_FIELD_IPV4].value.u32,
488 printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
489 rule->field[DST_FIELD_IPV4].mask_range.u32);
490 printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
491 rule->field[SRCP_FIELD_IPV4].value.u16,
492 rule->field[SRCP_FIELD_IPV4].mask_range.u16,
493 rule->field[DSTP_FIELD_IPV4].value.u16,
494 rule->field[DSTP_FIELD_IPV4].mask_range.u16,
495 rule->field[PROTO_FIELD_IPV4].value.u8,
496 rule->field[PROTO_FIELD_IPV4].mask_range.u8);
498 printf("0x%x-0x%x-0x%x ",
499 rule->data.category_mask,
501 rule->data.userdata);
505 print_one_ipv6_rule(struct acl6_rule *rule, int extra)
507 unsigned char a, b, c, d;
509 uint32_t_to_char(rule->field[SRC1_FIELD_IPV6].value.u32,
511 printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
512 uint32_t_to_char(rule->field[SRC2_FIELD_IPV6].value.u32,
514 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
515 uint32_t_to_char(rule->field[SRC3_FIELD_IPV6].value.u32,
517 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
518 uint32_t_to_char(rule->field[SRC4_FIELD_IPV6].value.u32,
520 printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
521 rule->field[SRC1_FIELD_IPV6].mask_range.u32
522 + rule->field[SRC2_FIELD_IPV6].mask_range.u32
523 + rule->field[SRC3_FIELD_IPV6].mask_range.u32
524 + rule->field[SRC4_FIELD_IPV6].mask_range.u32);
526 uint32_t_to_char(rule->field[DST1_FIELD_IPV6].value.u32,
528 printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
529 uint32_t_to_char(rule->field[DST2_FIELD_IPV6].value.u32,
531 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
532 uint32_t_to_char(rule->field[DST3_FIELD_IPV6].value.u32,
534 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
535 uint32_t_to_char(rule->field[DST4_FIELD_IPV6].value.u32,
537 printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
538 rule->field[DST1_FIELD_IPV6].mask_range.u32
539 + rule->field[DST2_FIELD_IPV6].mask_range.u32
540 + rule->field[DST3_FIELD_IPV6].mask_range.u32
541 + rule->field[DST4_FIELD_IPV6].mask_range.u32);
543 printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
544 rule->field[SRCP_FIELD_IPV6].value.u16,
545 rule->field[SRCP_FIELD_IPV6].mask_range.u16,
546 rule->field[DSTP_FIELD_IPV6].value.u16,
547 rule->field[DSTP_FIELD_IPV6].mask_range.u16,
548 rule->field[PROTO_FIELD_IPV6].value.u8,
549 rule->field[PROTO_FIELD_IPV6].mask_range.u8);
551 printf("0x%x-0x%x-0x%x ",
552 rule->data.category_mask,
554 rule->data.userdata);
557 /* Bypass comment and empty lines */
559 is_bypass_line(char *buff)
564 if (buff[0] == COMMENT_LEAD_CHAR)
567 while (buff[i] != '\0') {
568 if (!isspace(buff[i]))
575 #ifdef L3FWDACL_DEBUG
577 dump_acl4_rule(struct rte_mbuf *m, uint32_t sig)
579 uint32_t offset = sig & ~ACL_DENY_SIGNATURE;
580 unsigned char a, b, c, d;
581 struct ipv4_hdr *ipv4_hdr = rte_pktmbuf_mtod_offset(m,
583 sizeof(struct ether_hdr));
585 uint32_t_to_char(rte_bswap32(ipv4_hdr->src_addr), &a, &b, &c, &d);
586 printf("Packet Src:%hhu.%hhu.%hhu.%hhu ", a, b, c, d);
587 uint32_t_to_char(rte_bswap32(ipv4_hdr->dst_addr), &a, &b, &c, &d);
588 printf("Dst:%hhu.%hhu.%hhu.%hhu ", a, b, c, d);
590 printf("Src port:%hu,Dst port:%hu ",
591 rte_bswap16(*(uint16_t *)(ipv4_hdr + 1)),
592 rte_bswap16(*((uint16_t *)(ipv4_hdr + 1) + 1)));
593 printf("hit ACL %d - ", offset);
595 print_one_ipv4_rule(acl_config.rule_ipv4 + offset, 1);
601 dump_acl6_rule(struct rte_mbuf *m, uint32_t sig)
604 uint32_t offset = sig & ~ACL_DENY_SIGNATURE;
605 struct ipv6_hdr *ipv6_hdr = rte_pktmbuf_mtod_offset(m,
607 sizeof(struct ether_hdr));
609 printf("Packet Src");
610 for (i = 0; i < RTE_DIM(ipv6_hdr->src_addr); i += sizeof(uint16_t))
612 ipv6_hdr->src_addr[i], ipv6_hdr->src_addr[i + 1]);
615 for (i = 0; i < RTE_DIM(ipv6_hdr->dst_addr); i += sizeof(uint16_t))
617 ipv6_hdr->dst_addr[i], ipv6_hdr->dst_addr[i + 1]);
619 printf("\nSrc port:%hu,Dst port:%hu ",
620 rte_bswap16(*(uint16_t *)(ipv6_hdr + 1)),
621 rte_bswap16(*((uint16_t *)(ipv6_hdr + 1) + 1)));
622 printf("hit ACL %d - ", offset);
624 print_one_ipv6_rule(acl_config.rule_ipv6 + offset, 1);
628 #endif /* L3FWDACL_DEBUG */
631 dump_ipv4_rules(struct acl4_rule *rule, int num, int extra)
635 for (i = 0; i < num; i++, rule++) {
636 printf("\t%d:", i + 1);
637 print_one_ipv4_rule(rule, extra);
643 dump_ipv6_rules(struct acl6_rule *rule, int num, int extra)
647 for (i = 0; i < num; i++, rule++) {
648 printf("\t%d:", i + 1);
649 print_one_ipv6_rule(rule, extra);
654 #ifdef DO_RFC_1812_CHECKS
656 prepare_one_packet(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
659 struct ipv4_hdr *ipv4_hdr;
660 struct rte_mbuf *pkt = pkts_in[index];
662 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
663 ipv4_hdr = rte_pktmbuf_mtod_offset(pkt, struct ipv4_hdr *,
664 sizeof(struct ether_hdr));
666 /* Check to make sure the packet is valid (RFC1812) */
667 if (is_valid_ipv4_pkt(ipv4_hdr, pkt->pkt_len) >= 0) {
669 /* Update time to live and header checksum */
670 --(ipv4_hdr->time_to_live);
671 ++(ipv4_hdr->hdr_checksum);
673 /* Fill acl structure */
674 acl->data_ipv4[acl->num_ipv4] = MBUF_IPV4_2PROTO(pkt);
675 acl->m_ipv4[(acl->num_ipv4)++] = pkt;
678 /* Not a valid IPv4 packet */
679 rte_pktmbuf_free(pkt);
681 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
682 /* Fill acl structure */
683 acl->data_ipv6[acl->num_ipv6] = MBUF_IPV6_2PROTO(pkt);
684 acl->m_ipv6[(acl->num_ipv6)++] = pkt;
687 /* Unknown type, drop the packet */
688 rte_pktmbuf_free(pkt);
694 prepare_one_packet(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
697 struct rte_mbuf *pkt = pkts_in[index];
699 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
700 /* Fill acl structure */
701 acl->data_ipv4[acl->num_ipv4] = MBUF_IPV4_2PROTO(pkt);
702 acl->m_ipv4[(acl->num_ipv4)++] = pkt;
704 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
705 /* Fill acl structure */
706 acl->data_ipv6[acl->num_ipv6] = MBUF_IPV6_2PROTO(pkt);
707 acl->m_ipv6[(acl->num_ipv6)++] = pkt;
709 /* Unknown type, drop the packet */
710 rte_pktmbuf_free(pkt);
713 #endif /* DO_RFC_1812_CHECKS */
716 prepare_acl_parameter(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
724 /* Prefetch first packets */
725 for (i = 0; i < PREFETCH_OFFSET && i < nb_rx; i++) {
726 rte_prefetch0(rte_pktmbuf_mtod(
727 pkts_in[i], void *));
730 for (i = 0; i < (nb_rx - PREFETCH_OFFSET); i++) {
731 rte_prefetch0(rte_pktmbuf_mtod(pkts_in[
732 i + PREFETCH_OFFSET], void *));
733 prepare_one_packet(pkts_in, acl, i);
736 /* Process left packets */
737 for (; i < nb_rx; i++)
738 prepare_one_packet(pkts_in, acl, i);
742 send_one_packet(struct rte_mbuf *m, uint32_t res)
744 if (likely((res & ACL_DENY_SIGNATURE) == 0 && res != 0)) {
745 /* forward packets */
746 send_single_packet(m,
747 (uint8_t)(res - FWD_PORT_SHIFT));
749 /* in the ACL list, drop it */
750 #ifdef L3FWDACL_DEBUG
751 if ((res & ACL_DENY_SIGNATURE) != 0) {
752 if (RTE_ETH_IS_IPV4_HDR(m->packet_type))
753 dump_acl4_rule(m, res);
754 else if (RTE_ETH_IS_IPV6_HDR(m->packet_type))
755 dump_acl6_rule(m, res);
765 send_packets(struct rte_mbuf **m, uint32_t *res, int num)
769 /* Prefetch first packets */
770 for (i = 0; i < PREFETCH_OFFSET && i < num; i++) {
771 rte_prefetch0(rte_pktmbuf_mtod(
775 for (i = 0; i < (num - PREFETCH_OFFSET); i++) {
776 rte_prefetch0(rte_pktmbuf_mtod(m[
777 i + PREFETCH_OFFSET], void *));
778 send_one_packet(m[i], res[i]);
781 /* Process left packets */
783 send_one_packet(m[i], res[i]);
787 * Parses IPV6 address, exepcts the following format:
788 * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX (where X - is a hexedecimal digit).
791 parse_ipv6_addr(const char *in, const char **end, uint32_t v[IPV6_ADDR_U32],
794 uint32_t addr[IPV6_ADDR_U16];
796 GET_CB_FIELD(in, addr[0], 16, UINT16_MAX, ':');
797 GET_CB_FIELD(in, addr[1], 16, UINT16_MAX, ':');
798 GET_CB_FIELD(in, addr[2], 16, UINT16_MAX, ':');
799 GET_CB_FIELD(in, addr[3], 16, UINT16_MAX, ':');
800 GET_CB_FIELD(in, addr[4], 16, UINT16_MAX, ':');
801 GET_CB_FIELD(in, addr[5], 16, UINT16_MAX, ':');
802 GET_CB_FIELD(in, addr[6], 16, UINT16_MAX, ':');
803 GET_CB_FIELD(in, addr[7], 16, UINT16_MAX, dlm);
807 v[0] = (addr[0] << 16) + addr[1];
808 v[1] = (addr[2] << 16) + addr[3];
809 v[2] = (addr[4] << 16) + addr[5];
810 v[3] = (addr[6] << 16) + addr[7];
816 parse_ipv6_net(const char *in, struct rte_acl_field field[4])
821 const uint32_t nbu32 = sizeof(uint32_t) * CHAR_BIT;
824 rc = parse_ipv6_addr(in, &mp, v, '/');
829 GET_CB_FIELD(mp, m, 0, CHAR_BIT * sizeof(v), 0);
831 /* put all together. */
832 for (i = 0; i != RTE_DIM(v); i++) {
833 if (m >= (i + 1) * nbu32)
834 field[i].mask_range.u32 = nbu32;
836 field[i].mask_range.u32 = m > (i * nbu32) ?
839 field[i].value.u32 = v[i];
846 parse_cb_ipv6_rule(char *str, struct rte_acl_rule *v, int has_userdata)
849 char *s, *sp, *in[CB_FLD_NUM];
850 static const char *dlm = " \t\n";
851 int dim = has_userdata ? CB_FLD_NUM : CB_FLD_USERDATA;
854 for (i = 0; i != dim; i++, s = NULL) {
855 in[i] = strtok_r(s, dlm, &sp);
860 rc = parse_ipv6_net(in[CB_FLD_SRC_ADDR], v->field + SRC1_FIELD_IPV6);
862 acl_log("failed to read source address/mask: %s\n",
863 in[CB_FLD_SRC_ADDR]);
867 rc = parse_ipv6_net(in[CB_FLD_DST_ADDR], v->field + DST1_FIELD_IPV6);
869 acl_log("failed to read destination address/mask: %s\n",
870 in[CB_FLD_DST_ADDR]);
875 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
876 v->field[SRCP_FIELD_IPV6].value.u16,
878 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
879 v->field[SRCP_FIELD_IPV6].mask_range.u16,
882 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
883 sizeof(cb_port_delim)) != 0)
886 /* destination port. */
887 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
888 v->field[DSTP_FIELD_IPV6].value.u16,
890 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
891 v->field[DSTP_FIELD_IPV6].mask_range.u16,
894 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
895 sizeof(cb_port_delim)) != 0)
898 if (v->field[SRCP_FIELD_IPV6].mask_range.u16
899 < v->field[SRCP_FIELD_IPV6].value.u16
900 || v->field[DSTP_FIELD_IPV6].mask_range.u16
901 < v->field[DSTP_FIELD_IPV6].value.u16)
904 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].value.u8,
906 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].mask_range.u8,
910 GET_CB_FIELD(in[CB_FLD_USERDATA], v->data.userdata,
917 * Parse ClassBench rules file.
919 * '@'<src_ipv4_addr>'/'<masklen> <space> \
920 * <dst_ipv4_addr>'/'<masklen> <space> \
921 * <src_port_low> <space> ":" <src_port_high> <space> \
922 * <dst_port_low> <space> ":" <dst_port_high> <space> \
926 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
928 uint8_t a, b, c, d, m;
930 GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
931 GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
932 GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
933 GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
934 GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
936 addr[0] = IPv4(a, b, c, d);
943 parse_cb_ipv4vlan_rule(char *str, struct rte_acl_rule *v, int has_userdata)
946 char *s, *sp, *in[CB_FLD_NUM];
947 static const char *dlm = " \t\n";
948 int dim = has_userdata ? CB_FLD_NUM : CB_FLD_USERDATA;
951 for (i = 0; i != dim; i++, s = NULL) {
952 in[i] = strtok_r(s, dlm, &sp);
957 rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
958 &v->field[SRC_FIELD_IPV4].value.u32,
959 &v->field[SRC_FIELD_IPV4].mask_range.u32);
961 acl_log("failed to read source address/mask: %s\n",
962 in[CB_FLD_SRC_ADDR]);
966 rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
967 &v->field[DST_FIELD_IPV4].value.u32,
968 &v->field[DST_FIELD_IPV4].mask_range.u32);
970 acl_log("failed to read destination address/mask: %s\n",
971 in[CB_FLD_DST_ADDR]);
975 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
976 v->field[SRCP_FIELD_IPV4].value.u16,
978 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
979 v->field[SRCP_FIELD_IPV4].mask_range.u16,
982 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
983 sizeof(cb_port_delim)) != 0)
986 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
987 v->field[DSTP_FIELD_IPV4].value.u16,
989 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
990 v->field[DSTP_FIELD_IPV4].mask_range.u16,
993 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
994 sizeof(cb_port_delim)) != 0)
997 if (v->field[SRCP_FIELD_IPV4].mask_range.u16
998 < v->field[SRCP_FIELD_IPV4].value.u16
999 || v->field[DSTP_FIELD_IPV4].mask_range.u16
1000 < v->field[DSTP_FIELD_IPV4].value.u16)
1003 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].value.u8,
1005 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].mask_range.u8,
1009 GET_CB_FIELD(in[CB_FLD_USERDATA], v->data.userdata, 0,
1016 add_rules(const char *rule_path,
1017 struct rte_acl_rule **proute_base,
1018 unsigned int *proute_num,
1019 struct rte_acl_rule **pacl_base,
1020 unsigned int *pacl_num, uint32_t rule_size,
1021 int (*parser)(char *, struct rte_acl_rule*, int))
1023 uint8_t *acl_rules, *route_rules;
1024 struct rte_acl_rule *next;
1025 unsigned int acl_num = 0, route_num = 0, total_num = 0;
1026 unsigned int acl_cnt = 0, route_cnt = 0;
1027 char buff[LINE_MAX];
1028 FILE *fh = fopen(rule_path, "rb");
1032 rte_exit(EXIT_FAILURE, "%s: Open %s failed\n", __func__,
1035 while ((fgets(buff, LINE_MAX, fh) != NULL)) {
1036 if (buff[0] == ROUTE_LEAD_CHAR)
1038 else if (buff[0] == ACL_LEAD_CHAR)
1043 rte_exit(EXIT_FAILURE, "Not find any route entries in %s!\n",
1046 fseek(fh, 0, SEEK_SET);
1048 acl_rules = calloc(acl_num, rule_size);
1050 if (NULL == acl_rules)
1051 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1054 route_rules = calloc(route_num, rule_size);
1056 if (NULL == route_rules)
1057 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1061 while (fgets(buff, LINE_MAX, fh) != NULL) {
1064 if (is_bypass_line(buff))
1070 if (s == ROUTE_LEAD_CHAR)
1071 next = (struct rte_acl_rule *)(route_rules +
1072 route_cnt * rule_size);
1075 else if (s == ACL_LEAD_CHAR)
1076 next = (struct rte_acl_rule *)(acl_rules +
1077 acl_cnt * rule_size);
1081 rte_exit(EXIT_FAILURE,
1082 "%s Line %u: should start with leading "
1084 rule_path, i, ROUTE_LEAD_CHAR, ACL_LEAD_CHAR);
1086 if (parser(buff + 1, next, s == ROUTE_LEAD_CHAR) != 0)
1087 rte_exit(EXIT_FAILURE,
1088 "%s Line %u: parse rules error\n",
1091 if (s == ROUTE_LEAD_CHAR) {
1092 /* Check the forwarding port number */
1093 if ((enabled_port_mask & (1 << next->data.userdata)) ==
1095 rte_exit(EXIT_FAILURE,
1096 "%s Line %u: fwd number illegal:%u\n",
1097 rule_path, i, next->data.userdata);
1098 next->data.userdata += FWD_PORT_SHIFT;
1101 next->data.userdata = ACL_DENY_SIGNATURE + acl_cnt;
1105 next->data.priority = RTE_ACL_MAX_PRIORITY - total_num;
1106 next->data.category_mask = -1;
1112 *pacl_base = (struct rte_acl_rule *)acl_rules;
1113 *pacl_num = acl_num;
1114 *proute_base = (struct rte_acl_rule *)route_rules;
1115 *proute_num = route_cnt;
1121 dump_acl_config(void)
1123 printf("ACL option are:\n");
1124 printf(OPTION_RULE_IPV4": %s\n", parm_config.rule_ipv4_name);
1125 printf(OPTION_RULE_IPV6": %s\n", parm_config.rule_ipv6_name);
1126 printf(OPTION_SCALAR": %d\n", parm_config.scalar);
1130 check_acl_config(void)
1132 if (parm_config.rule_ipv4_name == NULL) {
1133 acl_log("ACL IPv4 rule file not specified\n");
1135 } else if (parm_config.rule_ipv6_name == NULL) {
1136 acl_log("ACL IPv6 rule file not specified\n");
1143 static struct rte_acl_ctx*
1144 setup_acl(struct rte_acl_rule *route_base,
1145 struct rte_acl_rule *acl_base, unsigned int route_num,
1146 unsigned int acl_num, int ipv6, int socketid)
1148 char name[PATH_MAX];
1149 struct rte_acl_param acl_param;
1150 struct rte_acl_config acl_build_param;
1151 struct rte_acl_ctx *context;
1152 int dim = ipv6 ? RTE_DIM(ipv6_defs) : RTE_DIM(ipv4_defs);
1154 /* Create ACL contexts */
1155 snprintf(name, sizeof(name), "%s%d",
1156 ipv6 ? L3FWD_ACL_IPV6_NAME : L3FWD_ACL_IPV4_NAME,
1159 acl_param.name = name;
1160 acl_param.socket_id = socketid;
1161 acl_param.rule_size = RTE_ACL_RULE_SZ(dim);
1162 acl_param.max_rule_num = MAX_ACL_RULE_NUM;
1164 if ((context = rte_acl_create(&acl_param)) == NULL)
1165 rte_exit(EXIT_FAILURE, "Failed to create ACL context\n");
1167 if (parm_config.scalar && rte_acl_set_ctx_classify(context,
1168 RTE_ACL_CLASSIFY_SCALAR) != 0)
1169 rte_exit(EXIT_FAILURE,
1170 "Failed to setup classify method for ACL context\n");
1172 if (rte_acl_add_rules(context, route_base, route_num) < 0)
1173 rte_exit(EXIT_FAILURE, "add rules failed\n");
1175 if (rte_acl_add_rules(context, acl_base, acl_num) < 0)
1176 rte_exit(EXIT_FAILURE, "add rules failed\n");
1178 /* Perform builds */
1179 memset(&acl_build_param, 0, sizeof(acl_build_param));
1181 acl_build_param.num_categories = DEFAULT_MAX_CATEGORIES;
1182 acl_build_param.num_fields = dim;
1183 memcpy(&acl_build_param.defs, ipv6 ? ipv6_defs : ipv4_defs,
1184 ipv6 ? sizeof(ipv6_defs) : sizeof(ipv4_defs));
1186 if (rte_acl_build(context, &acl_build_param) != 0)
1187 rte_exit(EXIT_FAILURE, "Failed to build ACL trie\n");
1189 rte_acl_dump(context);
1200 struct rte_acl_rule *acl_base_ipv4, *route_base_ipv4,
1201 *acl_base_ipv6, *route_base_ipv6;
1202 unsigned int acl_num_ipv4 = 0, route_num_ipv4 = 0,
1203 acl_num_ipv6 = 0, route_num_ipv6 = 0;
1205 if (check_acl_config() != 0)
1206 rte_exit(EXIT_FAILURE, "Failed to get valid ACL options\n");
1210 /* Load rules from the input file */
1211 if (add_rules(parm_config.rule_ipv4_name, &route_base_ipv4,
1212 &route_num_ipv4, &acl_base_ipv4, &acl_num_ipv4,
1213 sizeof(struct acl4_rule), &parse_cb_ipv4vlan_rule) < 0)
1214 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1216 acl_log("IPv4 Route entries %u:\n", route_num_ipv4);
1217 dump_ipv4_rules((struct acl4_rule *)route_base_ipv4, route_num_ipv4, 1);
1219 acl_log("IPv4 ACL entries %u:\n", acl_num_ipv4);
1220 dump_ipv4_rules((struct acl4_rule *)acl_base_ipv4, acl_num_ipv4, 1);
1222 if (add_rules(parm_config.rule_ipv6_name, &route_base_ipv6,
1224 &acl_base_ipv6, &acl_num_ipv6,
1225 sizeof(struct acl6_rule), &parse_cb_ipv6_rule) < 0)
1226 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1228 acl_log("IPv6 Route entries %u:\n", route_num_ipv6);
1229 dump_ipv6_rules((struct acl6_rule *)route_base_ipv6, route_num_ipv6, 1);
1231 acl_log("IPv6 ACL entries %u:\n", acl_num_ipv6);
1232 dump_ipv6_rules((struct acl6_rule *)acl_base_ipv6, acl_num_ipv6, 1);
1234 memset(&acl_config, 0, sizeof(acl_config));
1236 /* Check sockets a context should be created on */
1238 acl_config.mapped[0] = 1;
1240 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1241 if (rte_lcore_is_enabled(lcore_id) == 0)
1244 socketid = rte_lcore_to_socket_id(lcore_id);
1245 if (socketid >= NB_SOCKETS) {
1246 acl_log("Socket %d of lcore %u is out "
1248 socketid, lcore_id, NB_SOCKETS);
1249 free(route_base_ipv4);
1250 free(route_base_ipv6);
1251 free(acl_base_ipv4);
1252 free(acl_base_ipv6);
1256 acl_config.mapped[socketid] = 1;
1260 for (i = 0; i < NB_SOCKETS; i++) {
1261 if (acl_config.mapped[i]) {
1262 acl_config.acx_ipv4[i] = setup_acl(route_base_ipv4,
1263 acl_base_ipv4, route_num_ipv4, acl_num_ipv4,
1266 acl_config.acx_ipv6[i] = setup_acl(route_base_ipv6,
1267 acl_base_ipv6, route_num_ipv6, acl_num_ipv6,
1272 free(route_base_ipv4);
1273 free(route_base_ipv6);
1275 #ifdef L3FWDACL_DEBUG
1276 acl_config.rule_ipv4 = (struct acl4_rule *)acl_base_ipv4;
1277 acl_config.rule_ipv6 = (struct acl6_rule *)acl_base_ipv6;
1279 free(acl_base_ipv4);
1280 free(acl_base_ipv6);
1286 /***********************end of ACL part******************************/
1289 uint16_t n_rx_queue;
1290 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
1292 uint16_t tx_port_id[RTE_MAX_ETHPORTS];
1293 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
1294 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
1295 } __rte_cache_aligned;
1297 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
1299 /* Enqueue a single packet, and send burst if queue is filled */
1301 send_single_packet(struct rte_mbuf *m, uint8_t port)
1304 struct lcore_conf *qconf;
1306 lcore_id = rte_lcore_id();
1308 qconf = &lcore_conf[lcore_id];
1309 rte_eth_tx_buffer(port, qconf->tx_queue_id[port],
1310 qconf->tx_buffer[port], m);
1313 #ifdef DO_RFC_1812_CHECKS
1315 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
1317 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
1319 * 1. The packet length reported by the Link Layer must be large
1320 * enough to hold the minimum length legal IP datagram (20 bytes).
1322 if (link_len < sizeof(struct ipv4_hdr))
1325 /* 2. The IP checksum must be correct. */
1326 /* this is checked in H/W */
1329 * 3. The IP version number must be 4. If the version number is not 4
1330 * then the packet may be another version of IP, such as IPng or
1333 if (((pkt->version_ihl) >> 4) != 4)
1336 * 4. The IP header length field must be large enough to hold the
1337 * minimum length legal IP datagram (20 bytes = 5 words).
1339 if ((pkt->version_ihl & 0xf) < 5)
1343 * 5. The IP total length field must be large enough to hold the IP
1344 * datagram header, whose length is specified in the IP header length
1347 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
1354 /* main processing loop */
1356 main_loop(__attribute__((unused)) void *dummy)
1358 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1360 uint64_t prev_tsc, diff_tsc, cur_tsc;
1362 uint8_t portid, queueid;
1363 struct lcore_conf *qconf;
1365 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
1366 / US_PER_S * BURST_TX_DRAIN_US;
1369 lcore_id = rte_lcore_id();
1370 qconf = &lcore_conf[lcore_id];
1371 socketid = rte_lcore_to_socket_id(lcore_id);
1373 if (qconf->n_rx_queue == 0) {
1374 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
1378 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
1380 for (i = 0; i < qconf->n_rx_queue; i++) {
1382 portid = qconf->rx_queue_list[i].port_id;
1383 queueid = qconf->rx_queue_list[i].queue_id;
1384 RTE_LOG(INFO, L3FWD,
1385 " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
1386 lcore_id, portid, queueid);
1391 cur_tsc = rte_rdtsc();
1394 * TX burst queue drain
1396 diff_tsc = cur_tsc - prev_tsc;
1397 if (unlikely(diff_tsc > drain_tsc)) {
1398 for (i = 0; i < qconf->n_tx_port; ++i) {
1399 portid = qconf->tx_port_id[i];
1400 rte_eth_tx_buffer_flush(portid,
1401 qconf->tx_queue_id[portid],
1402 qconf->tx_buffer[portid]);
1408 * Read packet from RX queues
1410 for (i = 0; i < qconf->n_rx_queue; ++i) {
1412 portid = qconf->rx_queue_list[i].port_id;
1413 queueid = qconf->rx_queue_list[i].queue_id;
1414 nb_rx = rte_eth_rx_burst(portid, queueid,
1415 pkts_burst, MAX_PKT_BURST);
1418 struct acl_search_t acl_search;
1420 prepare_acl_parameter(pkts_burst, &acl_search,
1423 if (acl_search.num_ipv4) {
1425 acl_config.acx_ipv4[socketid],
1426 acl_search.data_ipv4,
1427 acl_search.res_ipv4,
1428 acl_search.num_ipv4,
1429 DEFAULT_MAX_CATEGORIES);
1431 send_packets(acl_search.m_ipv4,
1432 acl_search.res_ipv4,
1433 acl_search.num_ipv4);
1436 if (acl_search.num_ipv6) {
1438 acl_config.acx_ipv6[socketid],
1439 acl_search.data_ipv6,
1440 acl_search.res_ipv6,
1441 acl_search.num_ipv6,
1442 DEFAULT_MAX_CATEGORIES);
1444 send_packets(acl_search.m_ipv6,
1445 acl_search.res_ipv6,
1446 acl_search.num_ipv6);
1454 check_lcore_params(void)
1456 uint8_t queue, lcore;
1460 for (i = 0; i < nb_lcore_params; ++i) {
1461 queue = lcore_params[i].queue_id;
1462 if (queue >= MAX_RX_QUEUE_PER_PORT) {
1463 printf("invalid queue number: %hhu\n", queue);
1466 lcore = lcore_params[i].lcore_id;
1467 if (!rte_lcore_is_enabled(lcore)) {
1468 printf("error: lcore %hhu is not enabled in "
1469 "lcore mask\n", lcore);
1472 socketid = rte_lcore_to_socket_id(lcore);
1473 if (socketid != 0 && numa_on == 0) {
1474 printf("warning: lcore %hhu is on socket %d "
1483 check_port_config(const unsigned nb_ports)
1488 for (i = 0; i < nb_lcore_params; ++i) {
1489 portid = lcore_params[i].port_id;
1491 if ((enabled_port_mask & (1 << portid)) == 0) {
1492 printf("port %u is not enabled in port mask\n", portid);
1495 if (portid >= nb_ports) {
1496 printf("port %u is not present on the board\n", portid);
1504 get_port_n_rx_queues(const uint8_t port)
1509 for (i = 0; i < nb_lcore_params; ++i) {
1510 if (lcore_params[i].port_id == port &&
1511 lcore_params[i].queue_id > queue)
1512 queue = lcore_params[i].queue_id;
1514 return (uint8_t)(++queue);
1518 init_lcore_rx_queues(void)
1520 uint16_t i, nb_rx_queue;
1523 for (i = 0; i < nb_lcore_params; ++i) {
1524 lcore = lcore_params[i].lcore_id;
1525 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1526 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1527 printf("error: too many queues (%u) for lcore: %u\n",
1528 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
1531 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1532 lcore_params[i].port_id;
1533 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1534 lcore_params[i].queue_id;
1535 lcore_conf[lcore].n_rx_queue++;
1543 print_usage(const char *prgname)
1545 printf("%s [EAL options] -- -p PORTMASK -P"
1546 "--"OPTION_RULE_IPV4"=FILE"
1547 "--"OPTION_RULE_IPV6"=FILE"
1548 " [--"OPTION_CONFIG" (port,queue,lcore)[,(port,queue,lcore]]"
1549 " [--"OPTION_ENBJMO" [--max-pkt-len PKTLEN]]\n"
1550 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1551 " -P : enable promiscuous mode\n"
1552 " --"OPTION_CONFIG": (port,queue,lcore): "
1553 "rx queues configuration\n"
1554 " --"OPTION_NONUMA": optional, disable numa awareness\n"
1555 " --"OPTION_ENBJMO": enable jumbo frame"
1556 " which max packet len is PKTLEN in decimal (64-9600)\n"
1557 " --"OPTION_RULE_IPV4"=FILE: specify the ipv4 rules entries "
1559 "Each rule occupy one line. "
1560 "2 kinds of rules are supported. "
1561 "One is ACL entry at while line leads with character '%c', "
1562 "another is route entry at while line leads with "
1564 " --"OPTION_RULE_IPV6"=FILE: specify the ipv6 rules "
1566 " --"OPTION_SCALAR": Use scalar function to do lookup\n",
1567 prgname, ACL_LEAD_CHAR, ROUTE_LEAD_CHAR);
1571 parse_max_pkt_len(const char *pktlen)
1576 /* parse decimal string */
1577 len = strtoul(pktlen, &end, 10);
1578 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
1588 parse_portmask(const char *portmask)
1593 /* parse hexadecimal string */
1594 pm = strtoul(portmask, &end, 16);
1595 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1605 parse_config(const char *q_arg)
1608 const char *p, *p0 = q_arg;
1616 unsigned long int_fld[_NUM_FLD];
1617 char *str_fld[_NUM_FLD];
1621 nb_lcore_params = 0;
1623 while ((p = strchr(p0, '(')) != NULL) {
1625 if ((p0 = strchr(p, ')')) == NULL)
1629 if (size >= sizeof(s))
1632 snprintf(s, sizeof(s), "%.*s", size, p);
1633 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1636 for (i = 0; i < _NUM_FLD; i++) {
1638 int_fld[i] = strtoul(str_fld[i], &end, 0);
1639 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1642 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1643 printf("exceeded max number of lcore params: %hu\n",
1647 lcore_params_array[nb_lcore_params].port_id =
1648 (uint8_t)int_fld[FLD_PORT];
1649 lcore_params_array[nb_lcore_params].queue_id =
1650 (uint8_t)int_fld[FLD_QUEUE];
1651 lcore_params_array[nb_lcore_params].lcore_id =
1652 (uint8_t)int_fld[FLD_LCORE];
1655 lcore_params = lcore_params_array;
1659 /* Parse the argument given in the command line of the application */
1661 parse_args(int argc, char **argv)
1666 char *prgname = argv[0];
1667 static struct option lgopts[] = {
1668 {OPTION_CONFIG, 1, 0, 0},
1669 {OPTION_NONUMA, 0, 0, 0},
1670 {OPTION_ENBJMO, 0, 0, 0},
1671 {OPTION_RULE_IPV4, 1, 0, 0},
1672 {OPTION_RULE_IPV6, 1, 0, 0},
1673 {OPTION_SCALAR, 0, 0, 0},
1679 while ((opt = getopt_long(argc, argvopt, "p:P",
1680 lgopts, &option_index)) != EOF) {
1685 enabled_port_mask = parse_portmask(optarg);
1686 if (enabled_port_mask == 0) {
1687 printf("invalid portmask\n");
1688 print_usage(prgname);
1693 printf("Promiscuous mode selected\n");
1699 if (!strncmp(lgopts[option_index].name,
1701 sizeof(OPTION_CONFIG))) {
1702 ret = parse_config(optarg);
1704 printf("invalid config\n");
1705 print_usage(prgname);
1710 if (!strncmp(lgopts[option_index].name,
1712 sizeof(OPTION_NONUMA))) {
1713 printf("numa is disabled\n");
1717 if (!strncmp(lgopts[option_index].name,
1718 OPTION_ENBJMO, sizeof(OPTION_ENBJMO))) {
1719 struct option lenopts = {
1726 printf("jumbo frame is enabled\n");
1727 port_conf.rxmode.jumbo_frame = 1;
1730 * if no max-pkt-len set, then use the
1731 * default value ETHER_MAX_LEN
1733 if (0 == getopt_long(argc, argvopt, "",
1734 &lenopts, &option_index)) {
1735 ret = parse_max_pkt_len(optarg);
1737 (ret > MAX_JUMBO_PKT_LEN)) {
1738 printf("invalid packet "
1740 print_usage(prgname);
1743 port_conf.rxmode.max_rx_pkt_len = ret;
1745 printf("set jumbo frame max packet length "
1748 port_conf.rxmode.max_rx_pkt_len);
1751 if (!strncmp(lgopts[option_index].name,
1753 sizeof(OPTION_RULE_IPV4)))
1754 parm_config.rule_ipv4_name = optarg;
1756 if (!strncmp(lgopts[option_index].name,
1758 sizeof(OPTION_RULE_IPV6))) {
1759 parm_config.rule_ipv6_name = optarg;
1762 if (!strncmp(lgopts[option_index].name,
1763 OPTION_SCALAR, sizeof(OPTION_SCALAR)))
1764 parm_config.scalar = 1;
1770 print_usage(prgname);
1776 argv[optind-1] = prgname;
1779 optind = 1; /* reset getopt lib */
1784 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
1786 char buf[ETHER_ADDR_FMT_SIZE];
1787 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
1788 printf("%s%s", name, buf);
1792 init_mem(unsigned nb_mbuf)
1798 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1799 if (rte_lcore_is_enabled(lcore_id) == 0)
1803 socketid = rte_lcore_to_socket_id(lcore_id);
1807 if (socketid >= NB_SOCKETS) {
1808 rte_exit(EXIT_FAILURE,
1809 "Socket %d of lcore %u is out of range %d\n",
1810 socketid, lcore_id, NB_SOCKETS);
1812 if (pktmbuf_pool[socketid] == NULL) {
1813 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
1814 pktmbuf_pool[socketid] =
1815 rte_pktmbuf_pool_create(s, nb_mbuf,
1816 MEMPOOL_CACHE_SIZE, 0,
1817 RTE_MBUF_DEFAULT_BUF_SIZE,
1819 if (pktmbuf_pool[socketid] == NULL)
1820 rte_exit(EXIT_FAILURE,
1821 "Cannot init mbuf pool on socket %d\n",
1824 printf("Allocated mbuf pool on socket %d\n",
1831 /* Check the link status of all ports in up to 9s, and print them finally */
1833 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1835 #define CHECK_INTERVAL 100 /* 100ms */
1836 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1837 uint8_t portid, count, all_ports_up, print_flag = 0;
1838 struct rte_eth_link link;
1840 printf("\nChecking link status");
1842 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1844 for (portid = 0; portid < port_num; portid++) {
1845 if ((port_mask & (1 << portid)) == 0)
1847 memset(&link, 0, sizeof(link));
1848 rte_eth_link_get_nowait(portid, &link);
1849 /* print link status if flag set */
1850 if (print_flag == 1) {
1851 if (link.link_status)
1852 printf("Port %d Link Up - speed %u "
1853 "Mbps - %s\n", (uint8_t)portid,
1854 (unsigned)link.link_speed,
1855 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1856 ("full-duplex") : ("half-duplex\n"));
1858 printf("Port %d Link Down\n",
1862 /* clear all_ports_up flag if any link down */
1863 if (link.link_status == ETH_LINK_DOWN) {
1868 /* after finally printing all link status, get out */
1869 if (print_flag == 1)
1872 if (all_ports_up == 0) {
1875 rte_delay_ms(CHECK_INTERVAL);
1878 /* set the print_flag if all ports up or timeout */
1879 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1887 main(int argc, char **argv)
1889 struct lcore_conf *qconf;
1890 struct rte_eth_dev_info dev_info;
1891 struct rte_eth_txconf *txconf;
1896 uint32_t n_tx_queue, nb_lcores;
1897 uint8_t portid, nb_rx_queue, queue, socketid;
1900 ret = rte_eal_init(argc, argv);
1902 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1906 /* parse application arguments (after the EAL ones) */
1907 ret = parse_args(argc, argv);
1909 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
1911 if (check_lcore_params() < 0)
1912 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
1914 ret = init_lcore_rx_queues();
1916 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1918 nb_ports = rte_eth_dev_count();
1920 if (check_port_config(nb_ports) < 0)
1921 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
1923 /* Add ACL rules and route entries, build trie */
1924 if (app_acl_init() < 0)
1925 rte_exit(EXIT_FAILURE, "app_acl_init failed\n");
1927 nb_lcores = rte_lcore_count();
1929 /* initialize all ports */
1930 for (portid = 0; portid < nb_ports; portid++) {
1931 /* skip ports that are not enabled */
1932 if ((enabled_port_mask & (1 << portid)) == 0) {
1933 printf("\nSkipping disabled port %d\n", portid);
1938 printf("Initializing port %d ... ", portid);
1941 nb_rx_queue = get_port_n_rx_queues(portid);
1942 n_tx_queue = nb_lcores;
1943 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
1944 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
1945 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
1946 nb_rx_queue, (unsigned)n_tx_queue);
1947 ret = rte_eth_dev_configure(portid, nb_rx_queue,
1948 (uint16_t)n_tx_queue, &port_conf);
1950 rte_exit(EXIT_FAILURE,
1951 "Cannot configure device: err=%d, port=%d\n",
1954 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1955 print_ethaddr(" Address:", &ports_eth_addr[portid]);
1959 ret = init_mem(NB_MBUF);
1961 rte_exit(EXIT_FAILURE, "init_mem failed\n");
1963 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1964 if (rte_lcore_is_enabled(lcore_id) == 0)
1967 /* Initialize TX buffers */
1968 qconf = &lcore_conf[lcore_id];
1969 qconf->tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
1970 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
1971 rte_eth_dev_socket_id(portid));
1972 if (qconf->tx_buffer[portid] == NULL)
1973 rte_exit(EXIT_FAILURE, "Can't allocate tx buffer for port %u\n",
1976 rte_eth_tx_buffer_init(qconf->tx_buffer[portid], MAX_PKT_BURST);
1979 /* init one TX queue per couple (lcore,port) */
1981 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1982 if (rte_lcore_is_enabled(lcore_id) == 0)
1986 socketid = (uint8_t)
1987 rte_lcore_to_socket_id(lcore_id);
1991 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
1994 rte_eth_dev_info_get(portid, &dev_info);
1995 txconf = &dev_info.default_txconf;
1996 if (port_conf.rxmode.jumbo_frame)
1997 txconf->txq_flags = 0;
1998 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2001 rte_exit(EXIT_FAILURE,
2002 "rte_eth_tx_queue_setup: err=%d, "
2003 "port=%d\n", ret, portid);
2005 qconf = &lcore_conf[lcore_id];
2006 qconf->tx_queue_id[portid] = queueid;
2009 qconf->tx_port_id[qconf->n_tx_port] = portid;
2015 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2016 if (rte_lcore_is_enabled(lcore_id) == 0)
2018 qconf = &lcore_conf[lcore_id];
2019 printf("\nInitializing rx queues on lcore %u ... ", lcore_id);
2021 /* init RX queues */
2022 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
2023 portid = qconf->rx_queue_list[queue].port_id;
2024 queueid = qconf->rx_queue_list[queue].queue_id;
2027 socketid = (uint8_t)
2028 rte_lcore_to_socket_id(lcore_id);
2032 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2035 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2037 pktmbuf_pool[socketid]);
2039 rte_exit(EXIT_FAILURE,
2040 "rte_eth_rx_queue_setup: err=%d,"
2041 "port=%d\n", ret, portid);
2048 for (portid = 0; portid < nb_ports; portid++) {
2049 if ((enabled_port_mask & (1 << portid)) == 0)
2053 ret = rte_eth_dev_start(portid);
2055 rte_exit(EXIT_FAILURE,
2056 "rte_eth_dev_start: err=%d, port=%d\n",
2060 * If enabled, put device in promiscuous mode.
2061 * This allows IO forwarding mode to forward packets
2062 * to itself through 2 cross-connected ports of the
2066 rte_eth_promiscuous_enable(portid);
2069 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
2071 /* launch per-lcore init on every lcore */
2072 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2073 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2074 if (rte_eal_wait_lcore(lcore_id) < 0)