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>
67 #include <rte_mempool.h>
72 #include <rte_string_fns.h>
75 #define DO_RFC_1812_CHECKS
77 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
79 #define MAX_JUMBO_PKT_LEN 9600
81 #define MEMPOOL_CACHE_SIZE 256
84 * This expression is used to calculate the number of mbufs needed
85 * depending on user input, taking into account memory for rx and tx hardware
86 * rings, cache per lcore and mtable per port per lcore.
87 * RTE_MAX is used to ensure that NB_MBUF never goes below a
88 * minimum value of 8192
91 #define NB_MBUF RTE_MAX(\
92 (nb_ports * nb_rx_queue*RTE_TEST_RX_DESC_DEFAULT + \
93 nb_ports * nb_lcores * MAX_PKT_BURST + \
94 nb_ports * n_tx_queue * RTE_TEST_TX_DESC_DEFAULT + \
95 nb_lcores * MEMPOOL_CACHE_SIZE), \
98 #define MAX_PKT_BURST 32
99 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
103 /* Configure how many packets ahead to prefetch, when reading packets */
104 #define PREFETCH_OFFSET 3
107 * Configurable number of RX/TX ring descriptors
109 #define RTE_TEST_RX_DESC_DEFAULT 128
110 #define RTE_TEST_TX_DESC_DEFAULT 512
111 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
112 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
114 /* ethernet addresses of ports */
115 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
117 /* mask of enabled ports */
118 static uint32_t enabled_port_mask;
119 static int promiscuous_on; /**< Ports set in promiscuous mode off by default. */
120 static int numa_on = 1; /**< NUMA is enabled by default. */
122 struct lcore_rx_queue {
125 } __rte_cache_aligned;
127 #define MAX_RX_QUEUE_PER_LCORE 16
128 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
129 #define MAX_RX_QUEUE_PER_PORT 128
131 #define MAX_LCORE_PARAMS 1024
132 struct lcore_params {
136 } __rte_cache_aligned;
138 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
139 static struct lcore_params lcore_params_array_default[] = {
151 static struct lcore_params *lcore_params = lcore_params_array_default;
152 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
153 sizeof(lcore_params_array_default[0]);
155 static struct rte_eth_conf port_conf = {
157 .mq_mode = ETH_MQ_RX_RSS,
158 .max_rx_pkt_len = ETHER_MAX_LEN,
160 .header_split = 0, /**< Header Split disabled */
161 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
162 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
163 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
164 .hw_strip_crc = 0, /**< CRC stripped by hardware */
169 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
170 ETH_RSS_TCP | ETH_RSS_SCTP,
174 .mq_mode = ETH_MQ_TX_NONE,
178 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
180 /***********************start of ACL part******************************/
181 #ifdef DO_RFC_1812_CHECKS
183 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len);
186 send_single_packet(struct rte_mbuf *m, uint8_t port);
188 #define MAX_ACL_RULE_NUM 100000
189 #define DEFAULT_MAX_CATEGORIES 1
190 #define L3FWD_ACL_IPV4_NAME "l3fwd-acl-ipv4"
191 #define L3FWD_ACL_IPV6_NAME "l3fwd-acl-ipv6"
192 #define ACL_LEAD_CHAR ('@')
193 #define ROUTE_LEAD_CHAR ('R')
194 #define COMMENT_LEAD_CHAR ('#')
195 #define OPTION_CONFIG "config"
196 #define OPTION_NONUMA "no-numa"
197 #define OPTION_ENBJMO "enable-jumbo"
198 #define OPTION_RULE_IPV4 "rule_ipv4"
199 #define OPTION_RULE_IPV6 "rule_ipv6"
200 #define OPTION_SCALAR "scalar"
201 #define ACL_DENY_SIGNATURE 0xf0000000
202 #define RTE_LOGTYPE_L3FWDACL RTE_LOGTYPE_USER3
203 #define acl_log(format, ...) RTE_LOG(ERR, L3FWDACL, format, ##__VA_ARGS__)
204 #define uint32_t_to_char(ip, a, b, c, d) do {\
205 *a = (unsigned char)(ip >> 24 & 0xff);\
206 *b = (unsigned char)(ip >> 16 & 0xff);\
207 *c = (unsigned char)(ip >> 8 & 0xff);\
208 *d = (unsigned char)(ip & 0xff);\
210 #define OFF_ETHHEAD (sizeof(struct ether_hdr))
211 #define OFF_IPV42PROTO (offsetof(struct ipv4_hdr, next_proto_id))
212 #define OFF_IPV62PROTO (offsetof(struct ipv6_hdr, proto))
213 #define MBUF_IPV4_2PROTO(m) \
214 rte_pktmbuf_mtod_offset((m), uint8_t *, OFF_ETHHEAD + OFF_IPV42PROTO)
215 #define MBUF_IPV6_2PROTO(m) \
216 rte_pktmbuf_mtod_offset((m), uint8_t *, OFF_ETHHEAD + OFF_IPV62PROTO)
218 #define GET_CB_FIELD(in, fd, base, lim, dlm) do { \
222 val = strtoul((in), &end, (base)); \
223 if (errno != 0 || end[0] != (dlm) || val > (lim)) \
225 (fd) = (typeof(fd))val; \
230 * ACL rules should have higher priorities than route ones to ensure ACL rule
231 * always be found when input packets have multi-matches in the database.
232 * A exception case is performance measure, which can define route rules with
233 * higher priority and route rules will always be returned in each lookup.
234 * Reserve range from ACL_RULE_PRIORITY_MAX + 1 to
235 * RTE_ACL_MAX_PRIORITY for route entries in performance measure
237 #define ACL_RULE_PRIORITY_MAX 0x10000000
240 * Forward port info save in ACL lib starts from 1
241 * since ACL assume 0 is invalid.
242 * So, need add 1 when saving and minus 1 when forwarding packets.
244 #define FWD_PORT_SHIFT 1
247 * Rule and trace formats definitions.
260 * That effectively defines order of IPV4VLAN classifications:
262 * - VLAN (TAG and DOMAIN)
265 * - PORTS (SRC and DST)
268 RTE_ACL_IPV4VLAN_PROTO,
269 RTE_ACL_IPV4VLAN_VLAN,
270 RTE_ACL_IPV4VLAN_SRC,
271 RTE_ACL_IPV4VLAN_DST,
272 RTE_ACL_IPV4VLAN_PORTS,
276 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
278 .type = RTE_ACL_FIELD_TYPE_BITMASK,
279 .size = sizeof(uint8_t),
280 .field_index = PROTO_FIELD_IPV4,
281 .input_index = RTE_ACL_IPV4VLAN_PROTO,
285 .type = RTE_ACL_FIELD_TYPE_MASK,
286 .size = sizeof(uint32_t),
287 .field_index = SRC_FIELD_IPV4,
288 .input_index = RTE_ACL_IPV4VLAN_SRC,
289 .offset = offsetof(struct ipv4_hdr, src_addr) -
290 offsetof(struct ipv4_hdr, next_proto_id),
293 .type = RTE_ACL_FIELD_TYPE_MASK,
294 .size = sizeof(uint32_t),
295 .field_index = DST_FIELD_IPV4,
296 .input_index = RTE_ACL_IPV4VLAN_DST,
297 .offset = offsetof(struct ipv4_hdr, dst_addr) -
298 offsetof(struct ipv4_hdr, next_proto_id),
301 .type = RTE_ACL_FIELD_TYPE_RANGE,
302 .size = sizeof(uint16_t),
303 .field_index = SRCP_FIELD_IPV4,
304 .input_index = RTE_ACL_IPV4VLAN_PORTS,
305 .offset = sizeof(struct ipv4_hdr) -
306 offsetof(struct ipv4_hdr, next_proto_id),
309 .type = RTE_ACL_FIELD_TYPE_RANGE,
310 .size = sizeof(uint16_t),
311 .field_index = DSTP_FIELD_IPV4,
312 .input_index = RTE_ACL_IPV4VLAN_PORTS,
313 .offset = sizeof(struct ipv4_hdr) -
314 offsetof(struct ipv4_hdr, next_proto_id) +
319 #define IPV6_ADDR_LEN 16
320 #define IPV6_ADDR_U16 (IPV6_ADDR_LEN / sizeof(uint16_t))
321 #define IPV6_ADDR_U32 (IPV6_ADDR_LEN / sizeof(uint32_t))
338 struct rte_acl_field_def ipv6_defs[NUM_FIELDS_IPV6] = {
340 .type = RTE_ACL_FIELD_TYPE_BITMASK,
341 .size = sizeof(uint8_t),
342 .field_index = PROTO_FIELD_IPV6,
343 .input_index = PROTO_FIELD_IPV6,
347 .type = RTE_ACL_FIELD_TYPE_MASK,
348 .size = sizeof(uint32_t),
349 .field_index = SRC1_FIELD_IPV6,
350 .input_index = SRC1_FIELD_IPV6,
351 .offset = offsetof(struct ipv6_hdr, src_addr) -
352 offsetof(struct ipv6_hdr, proto),
355 .type = RTE_ACL_FIELD_TYPE_MASK,
356 .size = sizeof(uint32_t),
357 .field_index = SRC2_FIELD_IPV6,
358 .input_index = SRC2_FIELD_IPV6,
359 .offset = offsetof(struct ipv6_hdr, src_addr) -
360 offsetof(struct ipv6_hdr, proto) + sizeof(uint32_t),
363 .type = RTE_ACL_FIELD_TYPE_MASK,
364 .size = sizeof(uint32_t),
365 .field_index = SRC3_FIELD_IPV6,
366 .input_index = SRC3_FIELD_IPV6,
367 .offset = offsetof(struct ipv6_hdr, src_addr) -
368 offsetof(struct ipv6_hdr, proto) + 2 * sizeof(uint32_t),
371 .type = RTE_ACL_FIELD_TYPE_MASK,
372 .size = sizeof(uint32_t),
373 .field_index = SRC4_FIELD_IPV6,
374 .input_index = SRC4_FIELD_IPV6,
375 .offset = offsetof(struct ipv6_hdr, src_addr) -
376 offsetof(struct ipv6_hdr, proto) + 3 * sizeof(uint32_t),
379 .type = RTE_ACL_FIELD_TYPE_MASK,
380 .size = sizeof(uint32_t),
381 .field_index = DST1_FIELD_IPV6,
382 .input_index = DST1_FIELD_IPV6,
383 .offset = offsetof(struct ipv6_hdr, dst_addr)
384 - offsetof(struct ipv6_hdr, proto),
387 .type = RTE_ACL_FIELD_TYPE_MASK,
388 .size = sizeof(uint32_t),
389 .field_index = DST2_FIELD_IPV6,
390 .input_index = DST2_FIELD_IPV6,
391 .offset = offsetof(struct ipv6_hdr, dst_addr) -
392 offsetof(struct ipv6_hdr, proto) + sizeof(uint32_t),
395 .type = RTE_ACL_FIELD_TYPE_MASK,
396 .size = sizeof(uint32_t),
397 .field_index = DST3_FIELD_IPV6,
398 .input_index = DST3_FIELD_IPV6,
399 .offset = offsetof(struct ipv6_hdr, dst_addr) -
400 offsetof(struct ipv6_hdr, proto) + 2 * sizeof(uint32_t),
403 .type = RTE_ACL_FIELD_TYPE_MASK,
404 .size = sizeof(uint32_t),
405 .field_index = DST4_FIELD_IPV6,
406 .input_index = DST4_FIELD_IPV6,
407 .offset = offsetof(struct ipv6_hdr, dst_addr) -
408 offsetof(struct ipv6_hdr, proto) + 3 * sizeof(uint32_t),
411 .type = RTE_ACL_FIELD_TYPE_RANGE,
412 .size = sizeof(uint16_t),
413 .field_index = SRCP_FIELD_IPV6,
414 .input_index = SRCP_FIELD_IPV6,
415 .offset = sizeof(struct ipv6_hdr) -
416 offsetof(struct ipv6_hdr, proto),
419 .type = RTE_ACL_FIELD_TYPE_RANGE,
420 .size = sizeof(uint16_t),
421 .field_index = DSTP_FIELD_IPV6,
422 .input_index = SRCP_FIELD_IPV6,
423 .offset = sizeof(struct ipv6_hdr) -
424 offsetof(struct ipv6_hdr, proto) + sizeof(uint16_t),
433 CB_FLD_SRC_PORT_HIGH,
436 CB_FLD_DST_PORT_HIGH,
442 RTE_ACL_RULE_DEF(acl4_rule, RTE_DIM(ipv4_defs));
443 RTE_ACL_RULE_DEF(acl6_rule, RTE_DIM(ipv6_defs));
445 struct acl_search_t {
446 const uint8_t *data_ipv4[MAX_PKT_BURST];
447 struct rte_mbuf *m_ipv4[MAX_PKT_BURST];
448 uint32_t res_ipv4[MAX_PKT_BURST];
451 const uint8_t *data_ipv6[MAX_PKT_BURST];
452 struct rte_mbuf *m_ipv6[MAX_PKT_BURST];
453 uint32_t res_ipv6[MAX_PKT_BURST];
458 char mapped[NB_SOCKETS];
459 struct rte_acl_ctx *acx_ipv4[NB_SOCKETS];
460 struct rte_acl_ctx *acx_ipv6[NB_SOCKETS];
461 #ifdef L3FWDACL_DEBUG
462 struct acl4_rule *rule_ipv4;
463 struct acl6_rule *rule_ipv6;
468 const char *rule_ipv4_name;
469 const char *rule_ipv6_name;
473 const char cb_port_delim[] = ":";
476 print_one_ipv4_rule(struct acl4_rule *rule, int extra)
478 unsigned char a, b, c, d;
480 uint32_t_to_char(rule->field[SRC_FIELD_IPV4].value.u32,
482 printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
483 rule->field[SRC_FIELD_IPV4].mask_range.u32);
484 uint32_t_to_char(rule->field[DST_FIELD_IPV4].value.u32,
486 printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
487 rule->field[DST_FIELD_IPV4].mask_range.u32);
488 printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
489 rule->field[SRCP_FIELD_IPV4].value.u16,
490 rule->field[SRCP_FIELD_IPV4].mask_range.u16,
491 rule->field[DSTP_FIELD_IPV4].value.u16,
492 rule->field[DSTP_FIELD_IPV4].mask_range.u16,
493 rule->field[PROTO_FIELD_IPV4].value.u8,
494 rule->field[PROTO_FIELD_IPV4].mask_range.u8);
496 printf("0x%x-0x%x-0x%x ",
497 rule->data.category_mask,
499 rule->data.userdata);
503 print_one_ipv6_rule(struct acl6_rule *rule, int extra)
505 unsigned char a, b, c, d;
507 uint32_t_to_char(rule->field[SRC1_FIELD_IPV6].value.u32,
509 printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
510 uint32_t_to_char(rule->field[SRC2_FIELD_IPV6].value.u32,
512 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
513 uint32_t_to_char(rule->field[SRC3_FIELD_IPV6].value.u32,
515 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
516 uint32_t_to_char(rule->field[SRC4_FIELD_IPV6].value.u32,
518 printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
519 rule->field[SRC1_FIELD_IPV6].mask_range.u32
520 + rule->field[SRC2_FIELD_IPV6].mask_range.u32
521 + rule->field[SRC3_FIELD_IPV6].mask_range.u32
522 + rule->field[SRC4_FIELD_IPV6].mask_range.u32);
524 uint32_t_to_char(rule->field[DST1_FIELD_IPV6].value.u32,
526 printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
527 uint32_t_to_char(rule->field[DST2_FIELD_IPV6].value.u32,
529 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
530 uint32_t_to_char(rule->field[DST3_FIELD_IPV6].value.u32,
532 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
533 uint32_t_to_char(rule->field[DST4_FIELD_IPV6].value.u32,
535 printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
536 rule->field[DST1_FIELD_IPV6].mask_range.u32
537 + rule->field[DST2_FIELD_IPV6].mask_range.u32
538 + rule->field[DST3_FIELD_IPV6].mask_range.u32
539 + rule->field[DST4_FIELD_IPV6].mask_range.u32);
541 printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
542 rule->field[SRCP_FIELD_IPV6].value.u16,
543 rule->field[SRCP_FIELD_IPV6].mask_range.u16,
544 rule->field[DSTP_FIELD_IPV6].value.u16,
545 rule->field[DSTP_FIELD_IPV6].mask_range.u16,
546 rule->field[PROTO_FIELD_IPV6].value.u8,
547 rule->field[PROTO_FIELD_IPV6].mask_range.u8);
549 printf("0x%x-0x%x-0x%x ",
550 rule->data.category_mask,
552 rule->data.userdata);
555 /* Bypass comment and empty lines */
557 is_bypass_line(char *buff)
562 if (buff[0] == COMMENT_LEAD_CHAR)
565 while (buff[i] != '\0') {
566 if (!isspace(buff[i]))
573 #ifdef L3FWDACL_DEBUG
575 dump_acl4_rule(struct rte_mbuf *m, uint32_t sig)
577 uint32_t offset = sig & ~ACL_DENY_SIGNATURE;
578 unsigned char a, b, c, d;
579 struct ipv4_hdr *ipv4_hdr = rte_pktmbuf_mtod_offset(m,
581 sizeof(struct ether_hdr));
583 uint32_t_to_char(rte_bswap32(ipv4_hdr->src_addr), &a, &b, &c, &d);
584 printf("Packet Src:%hhu.%hhu.%hhu.%hhu ", a, b, c, d);
585 uint32_t_to_char(rte_bswap32(ipv4_hdr->dst_addr), &a, &b, &c, &d);
586 printf("Dst:%hhu.%hhu.%hhu.%hhu ", a, b, c, d);
588 printf("Src port:%hu,Dst port:%hu ",
589 rte_bswap16(*(uint16_t *)(ipv4_hdr + 1)),
590 rte_bswap16(*((uint16_t *)(ipv4_hdr + 1) + 1)));
591 printf("hit ACL %d - ", offset);
593 print_one_ipv4_rule(acl_config.rule_ipv4 + offset, 1);
599 dump_acl6_rule(struct rte_mbuf *m, uint32_t sig)
602 uint32_t offset = sig & ~ACL_DENY_SIGNATURE;
603 struct ipv6_hdr *ipv6_hdr = rte_pktmbuf_mtod_offset(m,
605 sizeof(struct ether_hdr));
607 printf("Packet Src");
608 for (i = 0; i < RTE_DIM(ipv6_hdr->src_addr); i += sizeof(uint16_t))
610 ipv6_hdr->src_addr[i], ipv6_hdr->src_addr[i + 1]);
613 for (i = 0; i < RTE_DIM(ipv6_hdr->dst_addr); i += sizeof(uint16_t))
615 ipv6_hdr->dst_addr[i], ipv6_hdr->dst_addr[i + 1]);
617 printf("\nSrc port:%hu,Dst port:%hu ",
618 rte_bswap16(*(uint16_t *)(ipv6_hdr + 1)),
619 rte_bswap16(*((uint16_t *)(ipv6_hdr + 1) + 1)));
620 printf("hit ACL %d - ", offset);
622 print_one_ipv6_rule(acl_config.rule_ipv6 + offset, 1);
626 #endif /* L3FWDACL_DEBUG */
629 dump_ipv4_rules(struct acl4_rule *rule, int num, int extra)
633 for (i = 0; i < num; i++, rule++) {
634 printf("\t%d:", i + 1);
635 print_one_ipv4_rule(rule, extra);
641 dump_ipv6_rules(struct acl6_rule *rule, int num, int extra)
645 for (i = 0; i < num; i++, rule++) {
646 printf("\t%d:", i + 1);
647 print_one_ipv6_rule(rule, extra);
652 #ifdef DO_RFC_1812_CHECKS
654 prepare_one_packet(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
657 struct ipv4_hdr *ipv4_hdr;
658 struct rte_mbuf *pkt = pkts_in[index];
660 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
661 ipv4_hdr = rte_pktmbuf_mtod_offset(pkt, struct ipv4_hdr *,
662 sizeof(struct ether_hdr));
664 /* Check to make sure the packet is valid (RFC1812) */
665 if (is_valid_ipv4_pkt(ipv4_hdr, pkt->pkt_len) >= 0) {
667 /* Update time to live and header checksum */
668 --(ipv4_hdr->time_to_live);
669 ++(ipv4_hdr->hdr_checksum);
671 /* Fill acl structure */
672 acl->data_ipv4[acl->num_ipv4] = MBUF_IPV4_2PROTO(pkt);
673 acl->m_ipv4[(acl->num_ipv4)++] = pkt;
676 /* Not a valid IPv4 packet */
677 rte_pktmbuf_free(pkt);
679 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
680 /* Fill acl structure */
681 acl->data_ipv6[acl->num_ipv6] = MBUF_IPV6_2PROTO(pkt);
682 acl->m_ipv6[(acl->num_ipv6)++] = pkt;
685 /* Unknown type, drop the packet */
686 rte_pktmbuf_free(pkt);
692 prepare_one_packet(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
695 struct rte_mbuf *pkt = pkts_in[index];
697 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
698 /* Fill acl structure */
699 acl->data_ipv4[acl->num_ipv4] = MBUF_IPV4_2PROTO(pkt);
700 acl->m_ipv4[(acl->num_ipv4)++] = pkt;
702 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
703 /* Fill acl structure */
704 acl->data_ipv6[acl->num_ipv6] = MBUF_IPV6_2PROTO(pkt);
705 acl->m_ipv6[(acl->num_ipv6)++] = pkt;
707 /* Unknown type, drop the packet */
708 rte_pktmbuf_free(pkt);
711 #endif /* DO_RFC_1812_CHECKS */
714 prepare_acl_parameter(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
722 /* Prefetch first packets */
723 for (i = 0; i < PREFETCH_OFFSET && i < nb_rx; i++) {
724 rte_prefetch0(rte_pktmbuf_mtod(
725 pkts_in[i], void *));
728 for (i = 0; i < (nb_rx - PREFETCH_OFFSET); i++) {
729 rte_prefetch0(rte_pktmbuf_mtod(pkts_in[
730 i + PREFETCH_OFFSET], void *));
731 prepare_one_packet(pkts_in, acl, i);
734 /* Process left packets */
735 for (; i < nb_rx; i++)
736 prepare_one_packet(pkts_in, acl, i);
740 send_one_packet(struct rte_mbuf *m, uint32_t res)
742 if (likely((res & ACL_DENY_SIGNATURE) == 0 && res != 0)) {
743 /* forward packets */
744 send_single_packet(m,
745 (uint8_t)(res - FWD_PORT_SHIFT));
747 /* in the ACL list, drop it */
748 #ifdef L3FWDACL_DEBUG
749 if ((res & ACL_DENY_SIGNATURE) != 0) {
750 if (RTE_ETH_IS_IPV4_HDR(m->packet_type))
751 dump_acl4_rule(m, res);
752 else if (RTE_ETH_IS_IPV6_HDR(m->packet_type))
753 dump_acl6_rule(m, res);
763 send_packets(struct rte_mbuf **m, uint32_t *res, int num)
767 /* Prefetch first packets */
768 for (i = 0; i < PREFETCH_OFFSET && i < num; i++) {
769 rte_prefetch0(rte_pktmbuf_mtod(
773 for (i = 0; i < (num - PREFETCH_OFFSET); i++) {
774 rte_prefetch0(rte_pktmbuf_mtod(m[
775 i + PREFETCH_OFFSET], void *));
776 send_one_packet(m[i], res[i]);
779 /* Process left packets */
781 send_one_packet(m[i], res[i]);
785 * Parses IPV6 address, exepcts the following format:
786 * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX (where X - is a hexedecimal digit).
789 parse_ipv6_addr(const char *in, const char **end, uint32_t v[IPV6_ADDR_U32],
792 uint32_t addr[IPV6_ADDR_U16];
794 GET_CB_FIELD(in, addr[0], 16, UINT16_MAX, ':');
795 GET_CB_FIELD(in, addr[1], 16, UINT16_MAX, ':');
796 GET_CB_FIELD(in, addr[2], 16, UINT16_MAX, ':');
797 GET_CB_FIELD(in, addr[3], 16, UINT16_MAX, ':');
798 GET_CB_FIELD(in, addr[4], 16, UINT16_MAX, ':');
799 GET_CB_FIELD(in, addr[5], 16, UINT16_MAX, ':');
800 GET_CB_FIELD(in, addr[6], 16, UINT16_MAX, ':');
801 GET_CB_FIELD(in, addr[7], 16, UINT16_MAX, dlm);
805 v[0] = (addr[0] << 16) + addr[1];
806 v[1] = (addr[2] << 16) + addr[3];
807 v[2] = (addr[4] << 16) + addr[5];
808 v[3] = (addr[6] << 16) + addr[7];
814 parse_ipv6_net(const char *in, struct rte_acl_field field[4])
819 const uint32_t nbu32 = sizeof(uint32_t) * CHAR_BIT;
822 rc = parse_ipv6_addr(in, &mp, v, '/');
827 GET_CB_FIELD(mp, m, 0, CHAR_BIT * sizeof(v), 0);
829 /* put all together. */
830 for (i = 0; i != RTE_DIM(v); i++) {
831 if (m >= (i + 1) * nbu32)
832 field[i].mask_range.u32 = nbu32;
834 field[i].mask_range.u32 = m > (i * nbu32) ?
837 field[i].value.u32 = v[i];
844 parse_cb_ipv6_rule(char *str, struct rte_acl_rule *v, int has_userdata)
847 char *s, *sp, *in[CB_FLD_NUM];
848 static const char *dlm = " \t\n";
849 int dim = has_userdata ? CB_FLD_NUM : CB_FLD_USERDATA;
852 for (i = 0; i != dim; i++, s = NULL) {
853 in[i] = strtok_r(s, dlm, &sp);
858 rc = parse_ipv6_net(in[CB_FLD_SRC_ADDR], v->field + SRC1_FIELD_IPV6);
860 acl_log("failed to read source address/mask: %s\n",
861 in[CB_FLD_SRC_ADDR]);
865 rc = parse_ipv6_net(in[CB_FLD_DST_ADDR], v->field + DST1_FIELD_IPV6);
867 acl_log("failed to read destination address/mask: %s\n",
868 in[CB_FLD_DST_ADDR]);
873 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
874 v->field[SRCP_FIELD_IPV6].value.u16,
876 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
877 v->field[SRCP_FIELD_IPV6].mask_range.u16,
880 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
881 sizeof(cb_port_delim)) != 0)
884 /* destination port. */
885 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
886 v->field[DSTP_FIELD_IPV6].value.u16,
888 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
889 v->field[DSTP_FIELD_IPV6].mask_range.u16,
892 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
893 sizeof(cb_port_delim)) != 0)
896 if (v->field[SRCP_FIELD_IPV6].mask_range.u16
897 < v->field[SRCP_FIELD_IPV6].value.u16
898 || v->field[DSTP_FIELD_IPV6].mask_range.u16
899 < v->field[DSTP_FIELD_IPV6].value.u16)
902 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].value.u8,
904 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].mask_range.u8,
908 GET_CB_FIELD(in[CB_FLD_USERDATA], v->data.userdata,
915 * Parse ClassBench rules file.
917 * '@'<src_ipv4_addr>'/'<masklen> <space> \
918 * <dst_ipv4_addr>'/'<masklen> <space> \
919 * <src_port_low> <space> ":" <src_port_high> <space> \
920 * <dst_port_low> <space> ":" <dst_port_high> <space> \
924 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
926 uint8_t a, b, c, d, m;
928 GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
929 GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
930 GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
931 GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
932 GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
934 addr[0] = IPv4(a, b, c, d);
941 parse_cb_ipv4vlan_rule(char *str, struct rte_acl_rule *v, int has_userdata)
944 char *s, *sp, *in[CB_FLD_NUM];
945 static const char *dlm = " \t\n";
946 int dim = has_userdata ? CB_FLD_NUM : CB_FLD_USERDATA;
949 for (i = 0; i != dim; i++, s = NULL) {
950 in[i] = strtok_r(s, dlm, &sp);
955 rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
956 &v->field[SRC_FIELD_IPV4].value.u32,
957 &v->field[SRC_FIELD_IPV4].mask_range.u32);
959 acl_log("failed to read source address/mask: %s\n",
960 in[CB_FLD_SRC_ADDR]);
964 rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
965 &v->field[DST_FIELD_IPV4].value.u32,
966 &v->field[DST_FIELD_IPV4].mask_range.u32);
968 acl_log("failed to read destination address/mask: %s\n",
969 in[CB_FLD_DST_ADDR]);
973 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
974 v->field[SRCP_FIELD_IPV4].value.u16,
976 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
977 v->field[SRCP_FIELD_IPV4].mask_range.u16,
980 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
981 sizeof(cb_port_delim)) != 0)
984 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
985 v->field[DSTP_FIELD_IPV4].value.u16,
987 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
988 v->field[DSTP_FIELD_IPV4].mask_range.u16,
991 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
992 sizeof(cb_port_delim)) != 0)
995 if (v->field[SRCP_FIELD_IPV4].mask_range.u16
996 < v->field[SRCP_FIELD_IPV4].value.u16
997 || v->field[DSTP_FIELD_IPV4].mask_range.u16
998 < v->field[DSTP_FIELD_IPV4].value.u16)
1001 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].value.u8,
1003 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].mask_range.u8,
1007 GET_CB_FIELD(in[CB_FLD_USERDATA], v->data.userdata, 0,
1014 add_rules(const char *rule_path,
1015 struct rte_acl_rule **proute_base,
1016 unsigned int *proute_num,
1017 struct rte_acl_rule **pacl_base,
1018 unsigned int *pacl_num, uint32_t rule_size,
1019 int (*parser)(char *, struct rte_acl_rule*, int))
1021 uint8_t *acl_rules, *route_rules;
1022 struct rte_acl_rule *next;
1023 unsigned int acl_num = 0, route_num = 0, total_num = 0;
1024 unsigned int acl_cnt = 0, route_cnt = 0;
1025 char buff[LINE_MAX];
1026 FILE *fh = fopen(rule_path, "rb");
1030 rte_exit(EXIT_FAILURE, "%s: Open %s failed\n", __func__,
1033 while ((fgets(buff, LINE_MAX, fh) != NULL)) {
1034 if (buff[0] == ROUTE_LEAD_CHAR)
1036 else if (buff[0] == ACL_LEAD_CHAR)
1041 rte_exit(EXIT_FAILURE, "Not find any route entries in %s!\n",
1044 fseek(fh, 0, SEEK_SET);
1046 acl_rules = calloc(acl_num, rule_size);
1048 if (NULL == acl_rules)
1049 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1052 route_rules = calloc(route_num, rule_size);
1054 if (NULL == route_rules)
1055 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1059 while (fgets(buff, LINE_MAX, fh) != NULL) {
1062 if (is_bypass_line(buff))
1068 if (s == ROUTE_LEAD_CHAR)
1069 next = (struct rte_acl_rule *)(route_rules +
1070 route_cnt * rule_size);
1073 else if (s == ACL_LEAD_CHAR)
1074 next = (struct rte_acl_rule *)(acl_rules +
1075 acl_cnt * rule_size);
1079 rte_exit(EXIT_FAILURE,
1080 "%s Line %u: should start with leading "
1082 rule_path, i, ROUTE_LEAD_CHAR, ACL_LEAD_CHAR);
1084 if (parser(buff + 1, next, s == ROUTE_LEAD_CHAR) != 0)
1085 rte_exit(EXIT_FAILURE,
1086 "%s Line %u: parse rules error\n",
1089 if (s == ROUTE_LEAD_CHAR) {
1090 /* Check the forwarding port number */
1091 if ((enabled_port_mask & (1 << next->data.userdata)) ==
1093 rte_exit(EXIT_FAILURE,
1094 "%s Line %u: fwd number illegal:%u\n",
1095 rule_path, i, next->data.userdata);
1096 next->data.userdata += FWD_PORT_SHIFT;
1099 next->data.userdata = ACL_DENY_SIGNATURE + acl_cnt;
1103 next->data.priority = RTE_ACL_MAX_PRIORITY - total_num;
1104 next->data.category_mask = -1;
1110 *pacl_base = (struct rte_acl_rule *)acl_rules;
1111 *pacl_num = acl_num;
1112 *proute_base = (struct rte_acl_rule *)route_rules;
1113 *proute_num = route_cnt;
1119 dump_acl_config(void)
1121 printf("ACL option are:\n");
1122 printf(OPTION_RULE_IPV4": %s\n", parm_config.rule_ipv4_name);
1123 printf(OPTION_RULE_IPV6": %s\n", parm_config.rule_ipv6_name);
1124 printf(OPTION_SCALAR": %d\n", parm_config.scalar);
1128 check_acl_config(void)
1130 if (parm_config.rule_ipv4_name == NULL) {
1131 acl_log("ACL IPv4 rule file not specified\n");
1133 } else if (parm_config.rule_ipv6_name == NULL) {
1134 acl_log("ACL IPv6 rule file not specified\n");
1141 static struct rte_acl_ctx*
1142 setup_acl(struct rte_acl_rule *route_base,
1143 struct rte_acl_rule *acl_base, unsigned int route_num,
1144 unsigned int acl_num, int ipv6, int socketid)
1146 char name[PATH_MAX];
1147 struct rte_acl_param acl_param;
1148 struct rte_acl_config acl_build_param;
1149 struct rte_acl_ctx *context;
1150 int dim = ipv6 ? RTE_DIM(ipv6_defs) : RTE_DIM(ipv4_defs);
1152 /* Create ACL contexts */
1153 snprintf(name, sizeof(name), "%s%d",
1154 ipv6 ? L3FWD_ACL_IPV6_NAME : L3FWD_ACL_IPV4_NAME,
1157 acl_param.name = name;
1158 acl_param.socket_id = socketid;
1159 acl_param.rule_size = RTE_ACL_RULE_SZ(dim);
1160 acl_param.max_rule_num = MAX_ACL_RULE_NUM;
1162 if ((context = rte_acl_create(&acl_param)) == NULL)
1163 rte_exit(EXIT_FAILURE, "Failed to create ACL context\n");
1165 if (parm_config.scalar && rte_acl_set_ctx_classify(context,
1166 RTE_ACL_CLASSIFY_SCALAR) != 0)
1167 rte_exit(EXIT_FAILURE,
1168 "Failed to setup classify method for ACL context\n");
1170 if (rte_acl_add_rules(context, route_base, route_num) < 0)
1171 rte_exit(EXIT_FAILURE, "add rules failed\n");
1173 if (rte_acl_add_rules(context, acl_base, acl_num) < 0)
1174 rte_exit(EXIT_FAILURE, "add rules failed\n");
1176 /* Perform builds */
1177 memset(&acl_build_param, 0, sizeof(acl_build_param));
1179 acl_build_param.num_categories = DEFAULT_MAX_CATEGORIES;
1180 acl_build_param.num_fields = dim;
1181 memcpy(&acl_build_param.defs, ipv6 ? ipv6_defs : ipv4_defs,
1182 ipv6 ? sizeof(ipv6_defs) : sizeof(ipv4_defs));
1184 if (rte_acl_build(context, &acl_build_param) != 0)
1185 rte_exit(EXIT_FAILURE, "Failed to build ACL trie\n");
1187 rte_acl_dump(context);
1198 struct rte_acl_rule *acl_base_ipv4, *route_base_ipv4,
1199 *acl_base_ipv6, *route_base_ipv6;
1200 unsigned int acl_num_ipv4 = 0, route_num_ipv4 = 0,
1201 acl_num_ipv6 = 0, route_num_ipv6 = 0;
1203 if (check_acl_config() != 0)
1204 rte_exit(EXIT_FAILURE, "Failed to get valid ACL options\n");
1208 /* Load rules from the input file */
1209 if (add_rules(parm_config.rule_ipv4_name, &route_base_ipv4,
1210 &route_num_ipv4, &acl_base_ipv4, &acl_num_ipv4,
1211 sizeof(struct acl4_rule), &parse_cb_ipv4vlan_rule) < 0)
1212 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1214 acl_log("IPv4 Route entries %u:\n", route_num_ipv4);
1215 dump_ipv4_rules((struct acl4_rule *)route_base_ipv4, route_num_ipv4, 1);
1217 acl_log("IPv4 ACL entries %u:\n", acl_num_ipv4);
1218 dump_ipv4_rules((struct acl4_rule *)acl_base_ipv4, acl_num_ipv4, 1);
1220 if (add_rules(parm_config.rule_ipv6_name, &route_base_ipv6,
1222 &acl_base_ipv6, &acl_num_ipv6,
1223 sizeof(struct acl6_rule), &parse_cb_ipv6_rule) < 0)
1224 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1226 acl_log("IPv6 Route entries %u:\n", route_num_ipv6);
1227 dump_ipv6_rules((struct acl6_rule *)route_base_ipv6, route_num_ipv6, 1);
1229 acl_log("IPv6 ACL entries %u:\n", acl_num_ipv6);
1230 dump_ipv6_rules((struct acl6_rule *)acl_base_ipv6, acl_num_ipv6, 1);
1232 memset(&acl_config, 0, sizeof(acl_config));
1234 /* Check sockets a context should be created on */
1236 acl_config.mapped[0] = 1;
1238 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1239 if (rte_lcore_is_enabled(lcore_id) == 0)
1242 socketid = rte_lcore_to_socket_id(lcore_id);
1243 if (socketid >= NB_SOCKETS) {
1244 acl_log("Socket %d of lcore %u is out "
1246 socketid, lcore_id, NB_SOCKETS);
1247 free(route_base_ipv4);
1248 free(route_base_ipv6);
1249 free(acl_base_ipv4);
1250 free(acl_base_ipv6);
1254 acl_config.mapped[socketid] = 1;
1258 for (i = 0; i < NB_SOCKETS; i++) {
1259 if (acl_config.mapped[i]) {
1260 acl_config.acx_ipv4[i] = setup_acl(route_base_ipv4,
1261 acl_base_ipv4, route_num_ipv4, acl_num_ipv4,
1264 acl_config.acx_ipv6[i] = setup_acl(route_base_ipv6,
1265 acl_base_ipv6, route_num_ipv6, acl_num_ipv6,
1270 free(route_base_ipv4);
1271 free(route_base_ipv6);
1273 #ifdef L3FWDACL_DEBUG
1274 acl_config.rule_ipv4 = (struct acl4_rule *)acl_base_ipv4;
1275 acl_config.rule_ipv6 = (struct acl6_rule *)acl_base_ipv6;
1277 free(acl_base_ipv4);
1278 free(acl_base_ipv6);
1284 /***********************end of ACL part******************************/
1287 uint16_t n_rx_queue;
1288 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
1290 uint16_t tx_port_id[RTE_MAX_ETHPORTS];
1291 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
1292 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
1293 } __rte_cache_aligned;
1295 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
1297 /* Enqueue a single packet, and send burst if queue is filled */
1299 send_single_packet(struct rte_mbuf *m, uint8_t port)
1302 struct lcore_conf *qconf;
1304 lcore_id = rte_lcore_id();
1306 qconf = &lcore_conf[lcore_id];
1307 rte_eth_tx_buffer(port, qconf->tx_queue_id[port],
1308 qconf->tx_buffer[port], m);
1311 #ifdef DO_RFC_1812_CHECKS
1313 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
1315 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
1317 * 1. The packet length reported by the Link Layer must be large
1318 * enough to hold the minimum length legal IP datagram (20 bytes).
1320 if (link_len < sizeof(struct ipv4_hdr))
1323 /* 2. The IP checksum must be correct. */
1324 /* this is checked in H/W */
1327 * 3. The IP version number must be 4. If the version number is not 4
1328 * then the packet may be another version of IP, such as IPng or
1331 if (((pkt->version_ihl) >> 4) != 4)
1334 * 4. The IP header length field must be large enough to hold the
1335 * minimum length legal IP datagram (20 bytes = 5 words).
1337 if ((pkt->version_ihl & 0xf) < 5)
1341 * 5. The IP total length field must be large enough to hold the IP
1342 * datagram header, whose length is specified in the IP header length
1345 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
1352 /* main processing loop */
1354 main_loop(__attribute__((unused)) void *dummy)
1356 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1358 uint64_t prev_tsc, diff_tsc, cur_tsc;
1360 uint8_t portid, queueid;
1361 struct lcore_conf *qconf;
1363 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
1364 / US_PER_S * BURST_TX_DRAIN_US;
1367 lcore_id = rte_lcore_id();
1368 qconf = &lcore_conf[lcore_id];
1369 socketid = rte_lcore_to_socket_id(lcore_id);
1371 if (qconf->n_rx_queue == 0) {
1372 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
1376 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
1378 for (i = 0; i < qconf->n_rx_queue; i++) {
1380 portid = qconf->rx_queue_list[i].port_id;
1381 queueid = qconf->rx_queue_list[i].queue_id;
1382 RTE_LOG(INFO, L3FWD,
1383 " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
1384 lcore_id, portid, queueid);
1389 cur_tsc = rte_rdtsc();
1392 * TX burst queue drain
1394 diff_tsc = cur_tsc - prev_tsc;
1395 if (unlikely(diff_tsc > drain_tsc)) {
1396 for (i = 0; i < qconf->n_tx_port; ++i) {
1397 portid = qconf->tx_port_id[i];
1398 rte_eth_tx_buffer_flush(portid,
1399 qconf->tx_queue_id[portid],
1400 qconf->tx_buffer[portid]);
1406 * Read packet from RX queues
1408 for (i = 0; i < qconf->n_rx_queue; ++i) {
1410 portid = qconf->rx_queue_list[i].port_id;
1411 queueid = qconf->rx_queue_list[i].queue_id;
1412 nb_rx = rte_eth_rx_burst(portid, queueid,
1413 pkts_burst, MAX_PKT_BURST);
1416 struct acl_search_t acl_search;
1418 prepare_acl_parameter(pkts_burst, &acl_search,
1421 if (acl_search.num_ipv4) {
1423 acl_config.acx_ipv4[socketid],
1424 acl_search.data_ipv4,
1425 acl_search.res_ipv4,
1426 acl_search.num_ipv4,
1427 DEFAULT_MAX_CATEGORIES);
1429 send_packets(acl_search.m_ipv4,
1430 acl_search.res_ipv4,
1431 acl_search.num_ipv4);
1434 if (acl_search.num_ipv6) {
1436 acl_config.acx_ipv6[socketid],
1437 acl_search.data_ipv6,
1438 acl_search.res_ipv6,
1439 acl_search.num_ipv6,
1440 DEFAULT_MAX_CATEGORIES);
1442 send_packets(acl_search.m_ipv6,
1443 acl_search.res_ipv6,
1444 acl_search.num_ipv6);
1452 check_lcore_params(void)
1454 uint8_t queue, lcore;
1458 for (i = 0; i < nb_lcore_params; ++i) {
1459 queue = lcore_params[i].queue_id;
1460 if (queue >= MAX_RX_QUEUE_PER_PORT) {
1461 printf("invalid queue number: %hhu\n", queue);
1464 lcore = lcore_params[i].lcore_id;
1465 if (!rte_lcore_is_enabled(lcore)) {
1466 printf("error: lcore %hhu is not enabled in "
1467 "lcore mask\n", lcore);
1470 socketid = rte_lcore_to_socket_id(lcore);
1471 if (socketid != 0 && numa_on == 0) {
1472 printf("warning: lcore %hhu is on socket %d "
1481 check_port_config(const unsigned nb_ports)
1486 for (i = 0; i < nb_lcore_params; ++i) {
1487 portid = lcore_params[i].port_id;
1489 if ((enabled_port_mask & (1 << portid)) == 0) {
1490 printf("port %u is not enabled in port mask\n", portid);
1493 if (portid >= nb_ports) {
1494 printf("port %u is not present on the board\n", portid);
1502 get_port_n_rx_queues(const uint8_t port)
1507 for (i = 0; i < nb_lcore_params; ++i) {
1508 if (lcore_params[i].port_id == port &&
1509 lcore_params[i].queue_id > queue)
1510 queue = lcore_params[i].queue_id;
1512 return (uint8_t)(++queue);
1516 init_lcore_rx_queues(void)
1518 uint16_t i, nb_rx_queue;
1521 for (i = 0; i < nb_lcore_params; ++i) {
1522 lcore = lcore_params[i].lcore_id;
1523 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1524 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1525 printf("error: too many queues (%u) for lcore: %u\n",
1526 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
1529 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1530 lcore_params[i].port_id;
1531 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1532 lcore_params[i].queue_id;
1533 lcore_conf[lcore].n_rx_queue++;
1541 print_usage(const char *prgname)
1543 printf("%s [EAL options] -- -p PORTMASK -P"
1544 "--"OPTION_RULE_IPV4"=FILE"
1545 "--"OPTION_RULE_IPV6"=FILE"
1546 " [--"OPTION_CONFIG" (port,queue,lcore)[,(port,queue,lcore]]"
1547 " [--"OPTION_ENBJMO" [--max-pkt-len PKTLEN]]\n"
1548 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1549 " -P : enable promiscuous mode\n"
1550 " --"OPTION_CONFIG": (port,queue,lcore): "
1551 "rx queues configuration\n"
1552 " --"OPTION_NONUMA": optional, disable numa awareness\n"
1553 " --"OPTION_ENBJMO": enable jumbo frame"
1554 " which max packet len is PKTLEN in decimal (64-9600)\n"
1555 " --"OPTION_RULE_IPV4"=FILE: specify the ipv4 rules entries "
1557 "Each rule occupy one line. "
1558 "2 kinds of rules are supported. "
1559 "One is ACL entry at while line leads with character '%c', "
1560 "another is route entry at while line leads with "
1562 " --"OPTION_RULE_IPV6"=FILE: specify the ipv6 rules "
1564 " --"OPTION_SCALAR": Use scalar function to do lookup\n",
1565 prgname, ACL_LEAD_CHAR, ROUTE_LEAD_CHAR);
1569 parse_max_pkt_len(const char *pktlen)
1574 /* parse decimal string */
1575 len = strtoul(pktlen, &end, 10);
1576 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
1586 parse_portmask(const char *portmask)
1591 /* parse hexadecimal string */
1592 pm = strtoul(portmask, &end, 16);
1593 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1603 parse_config(const char *q_arg)
1606 const char *p, *p0 = q_arg;
1614 unsigned long int_fld[_NUM_FLD];
1615 char *str_fld[_NUM_FLD];
1619 nb_lcore_params = 0;
1621 while ((p = strchr(p0, '(')) != NULL) {
1623 if ((p0 = strchr(p, ')')) == NULL)
1627 if (size >= sizeof(s))
1630 snprintf(s, sizeof(s), "%.*s", size, p);
1631 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1634 for (i = 0; i < _NUM_FLD; i++) {
1636 int_fld[i] = strtoul(str_fld[i], &end, 0);
1637 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1640 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1641 printf("exceeded max number of lcore params: %hu\n",
1645 lcore_params_array[nb_lcore_params].port_id =
1646 (uint8_t)int_fld[FLD_PORT];
1647 lcore_params_array[nb_lcore_params].queue_id =
1648 (uint8_t)int_fld[FLD_QUEUE];
1649 lcore_params_array[nb_lcore_params].lcore_id =
1650 (uint8_t)int_fld[FLD_LCORE];
1653 lcore_params = lcore_params_array;
1657 /* Parse the argument given in the command line of the application */
1659 parse_args(int argc, char **argv)
1664 char *prgname = argv[0];
1665 static struct option lgopts[] = {
1666 {OPTION_CONFIG, 1, 0, 0},
1667 {OPTION_NONUMA, 0, 0, 0},
1668 {OPTION_ENBJMO, 0, 0, 0},
1669 {OPTION_RULE_IPV4, 1, 0, 0},
1670 {OPTION_RULE_IPV6, 1, 0, 0},
1671 {OPTION_SCALAR, 0, 0, 0},
1677 while ((opt = getopt_long(argc, argvopt, "p:P",
1678 lgopts, &option_index)) != EOF) {
1683 enabled_port_mask = parse_portmask(optarg);
1684 if (enabled_port_mask == 0) {
1685 printf("invalid portmask\n");
1686 print_usage(prgname);
1691 printf("Promiscuous mode selected\n");
1697 if (!strncmp(lgopts[option_index].name,
1699 sizeof(OPTION_CONFIG))) {
1700 ret = parse_config(optarg);
1702 printf("invalid config\n");
1703 print_usage(prgname);
1708 if (!strncmp(lgopts[option_index].name,
1710 sizeof(OPTION_NONUMA))) {
1711 printf("numa is disabled\n");
1715 if (!strncmp(lgopts[option_index].name,
1716 OPTION_ENBJMO, sizeof(OPTION_ENBJMO))) {
1717 struct option lenopts = {
1724 printf("jumbo frame is enabled\n");
1725 port_conf.rxmode.jumbo_frame = 1;
1728 * if no max-pkt-len set, then use the
1729 * default value ETHER_MAX_LEN
1731 if (0 == getopt_long(argc, argvopt, "",
1732 &lenopts, &option_index)) {
1733 ret = parse_max_pkt_len(optarg);
1735 (ret > MAX_JUMBO_PKT_LEN)) {
1736 printf("invalid packet "
1738 print_usage(prgname);
1741 port_conf.rxmode.max_rx_pkt_len = ret;
1743 printf("set jumbo frame max packet length "
1746 port_conf.rxmode.max_rx_pkt_len);
1749 if (!strncmp(lgopts[option_index].name,
1751 sizeof(OPTION_RULE_IPV4)))
1752 parm_config.rule_ipv4_name = optarg;
1754 if (!strncmp(lgopts[option_index].name,
1756 sizeof(OPTION_RULE_IPV6))) {
1757 parm_config.rule_ipv6_name = optarg;
1760 if (!strncmp(lgopts[option_index].name,
1761 OPTION_SCALAR, sizeof(OPTION_SCALAR)))
1762 parm_config.scalar = 1;
1768 print_usage(prgname);
1774 argv[optind-1] = prgname;
1777 optind = 0; /* reset getopt lib */
1782 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
1784 char buf[ETHER_ADDR_FMT_SIZE];
1785 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
1786 printf("%s%s", name, buf);
1790 init_mem(unsigned nb_mbuf)
1796 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1797 if (rte_lcore_is_enabled(lcore_id) == 0)
1801 socketid = rte_lcore_to_socket_id(lcore_id);
1805 if (socketid >= NB_SOCKETS) {
1806 rte_exit(EXIT_FAILURE,
1807 "Socket %d of lcore %u is out of range %d\n",
1808 socketid, lcore_id, NB_SOCKETS);
1810 if (pktmbuf_pool[socketid] == NULL) {
1811 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
1812 pktmbuf_pool[socketid] =
1813 rte_pktmbuf_pool_create(s, nb_mbuf,
1814 MEMPOOL_CACHE_SIZE, 0,
1815 RTE_MBUF_DEFAULT_BUF_SIZE,
1817 if (pktmbuf_pool[socketid] == NULL)
1818 rte_exit(EXIT_FAILURE,
1819 "Cannot init mbuf pool on socket %d\n",
1822 printf("Allocated mbuf pool on socket %d\n",
1829 /* Check the link status of all ports in up to 9s, and print them finally */
1831 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1833 #define CHECK_INTERVAL 100 /* 100ms */
1834 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1835 uint8_t portid, count, all_ports_up, print_flag = 0;
1836 struct rte_eth_link link;
1838 printf("\nChecking link status");
1840 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1842 for (portid = 0; portid < port_num; portid++) {
1843 if ((port_mask & (1 << portid)) == 0)
1845 memset(&link, 0, sizeof(link));
1846 rte_eth_link_get_nowait(portid, &link);
1847 /* print link status if flag set */
1848 if (print_flag == 1) {
1849 if (link.link_status)
1850 printf("Port %d Link Up - speed %u "
1851 "Mbps - %s\n", (uint8_t)portid,
1852 (unsigned)link.link_speed,
1853 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1854 ("full-duplex") : ("half-duplex\n"));
1856 printf("Port %d Link Down\n",
1860 /* clear all_ports_up flag if any link down */
1861 if (link.link_status == 0) {
1866 /* after finally printing all link status, get out */
1867 if (print_flag == 1)
1870 if (all_ports_up == 0) {
1873 rte_delay_ms(CHECK_INTERVAL);
1876 /* set the print_flag if all ports up or timeout */
1877 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1885 main(int argc, char **argv)
1887 struct lcore_conf *qconf;
1888 struct rte_eth_dev_info dev_info;
1889 struct rte_eth_txconf *txconf;
1894 uint32_t n_tx_queue, nb_lcores;
1895 uint8_t portid, nb_rx_queue, queue, socketid;
1899 ret = rte_eal_init(argc, argv);
1901 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1905 /* parse application arguments (after the EAL ones) */
1906 ret = parse_args(argc, argv);
1908 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
1910 if (check_lcore_params() < 0)
1911 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
1913 ret = init_lcore_rx_queues();
1915 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1917 nb_ports = rte_eth_dev_count();
1918 if (nb_ports > RTE_MAX_ETHPORTS)
1919 nb_ports = RTE_MAX_ETHPORTS;
1921 if (check_port_config(nb_ports) < 0)
1922 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
1924 /* Add ACL rules and route entries, build trie */
1925 if (app_acl_init() < 0)
1926 rte_exit(EXIT_FAILURE, "app_acl_init failed\n");
1928 nb_lcores = rte_lcore_count();
1931 /* initialize all ports */
1932 for (portid = 0; portid < nb_ports; portid++) {
1933 /* skip ports that are not enabled */
1934 if ((enabled_port_mask & (1 << portid)) == 0) {
1935 printf("\nSkipping disabled port %d\n", portid);
1940 printf("Initializing port %d ... ", portid);
1943 nb_rx_queue = get_port_n_rx_queues(portid);
1944 n_tx_queue = nb_lcores;
1945 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
1946 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
1947 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
1948 nb_rx_queue, (unsigned)n_tx_queue);
1949 ret = rte_eth_dev_configure(portid, nb_rx_queue,
1950 (uint16_t)n_tx_queue, &port_conf);
1952 rte_exit(EXIT_FAILURE,
1953 "Cannot configure device: err=%d, port=%d\n",
1956 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1957 print_ethaddr(" Address:", &ports_eth_addr[portid]);
1961 ret = init_mem(NB_MBUF);
1963 rte_exit(EXIT_FAILURE, "init_mem failed\n");
1965 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1966 if (rte_lcore_is_enabled(lcore_id) == 0)
1969 /* Initialize TX buffers */
1970 qconf = &lcore_conf[lcore_id];
1971 qconf->tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
1972 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
1973 rte_eth_dev_socket_id(portid));
1974 if (qconf->tx_buffer[portid] == NULL)
1975 rte_exit(EXIT_FAILURE, "Can't allocate tx buffer for port %u\n",
1978 rte_eth_tx_buffer_init(qconf->tx_buffer[portid], MAX_PKT_BURST);
1981 /* init one TX queue per couple (lcore,port) */
1983 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1984 if (rte_lcore_is_enabled(lcore_id) == 0)
1988 socketid = (uint8_t)
1989 rte_lcore_to_socket_id(lcore_id);
1993 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
1996 rte_eth_dev_info_get(portid, &dev_info);
1997 txconf = &dev_info.default_txconf;
1998 if (port_conf.rxmode.jumbo_frame)
1999 txconf->txq_flags = 0;
2000 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2003 rte_exit(EXIT_FAILURE,
2004 "rte_eth_tx_queue_setup: err=%d, "
2005 "port=%d\n", ret, portid);
2007 qconf = &lcore_conf[lcore_id];
2008 qconf->tx_queue_id[portid] = queueid;
2011 qconf->n_tx_port = nb_tx_port;
2012 qconf->tx_port_id[qconf->n_tx_port] = portid;
2019 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2020 if (rte_lcore_is_enabled(lcore_id) == 0)
2022 qconf = &lcore_conf[lcore_id];
2023 printf("\nInitializing rx queues on lcore %u ... ", lcore_id);
2025 /* init RX queues */
2026 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
2027 portid = qconf->rx_queue_list[queue].port_id;
2028 queueid = qconf->rx_queue_list[queue].queue_id;
2031 socketid = (uint8_t)
2032 rte_lcore_to_socket_id(lcore_id);
2036 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2039 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2041 pktmbuf_pool[socketid]);
2043 rte_exit(EXIT_FAILURE,
2044 "rte_eth_rx_queue_setup: err=%d,"
2045 "port=%d\n", ret, portid);
2052 for (portid = 0; portid < nb_ports; portid++) {
2053 if ((enabled_port_mask & (1 << portid)) == 0)
2057 ret = rte_eth_dev_start(portid);
2059 rte_exit(EXIT_FAILURE,
2060 "rte_eth_dev_start: err=%d, port=%d\n",
2064 * If enabled, put device in promiscuous mode.
2065 * This allows IO forwarding mode to forward packets
2066 * to itself through 2 cross-connected ports of the
2070 rte_eth_promiscuous_enable(portid);
2073 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
2075 /* launch per-lcore init on every lcore */
2076 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2077 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2078 if (rte_eal_wait_lcore(lcore_id) < 0)