4 * Copyright(c) 2010-2014 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>
51 #include <rte_tailq.h>
53 #include <rte_per_lcore.h>
54 #include <rte_launch.h>
55 #include <rte_atomic.h>
56 #include <rte_cycles.h>
57 #include <rte_prefetch.h>
58 #include <rte_lcore.h>
59 #include <rte_per_lcore.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_interrupts.h>
63 #include <rte_random.h>
64 #include <rte_debug.h>
65 #include <rte_ether.h>
66 #include <rte_ethdev.h>
68 #include <rte_mempool.h>
73 #include <rte_string_fns.h>
76 #define DO_RFC_1812_CHECKS
78 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
80 #define MAX_JUMBO_PKT_LEN 9600
82 #define MEMPOOL_CACHE_SIZE 256
84 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
87 * This expression is used to calculate the number of mbufs needed
88 * depending on user input, taking into account memory for rx and tx hardware
89 * rings, cache per lcore and mtable per port per lcore.
90 * RTE_MAX is used to ensure that NB_MBUF never goes below a
91 * minimum value of 8192
94 #define NB_MBUF RTE_MAX(\
95 (nb_ports * nb_rx_queue*RTE_TEST_RX_DESC_DEFAULT + \
96 nb_ports * nb_lcores * MAX_PKT_BURST + \
97 nb_ports * n_tx_queue * RTE_TEST_TX_DESC_DEFAULT + \
98 nb_lcores * MEMPOOL_CACHE_SIZE), \
101 #define MAX_PKT_BURST 32
102 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
106 /* Configure how many packets ahead to prefetch, when reading packets */
107 #define PREFETCH_OFFSET 3
110 * Configurable number of RX/TX ring descriptors
112 #define RTE_TEST_RX_DESC_DEFAULT 128
113 #define RTE_TEST_TX_DESC_DEFAULT 512
114 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
115 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
117 /* ethernet addresses of ports */
118 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
120 /* mask of enabled ports */
121 static uint32_t enabled_port_mask;
122 static int promiscuous_on; /**< Ports set in promiscuous mode off by default. */
123 static int numa_on = 1; /**< NUMA is enabled by default. */
127 struct rte_mbuf *m_table[MAX_PKT_BURST];
130 struct lcore_rx_queue {
133 } __rte_cache_aligned;
135 #define MAX_RX_QUEUE_PER_LCORE 16
136 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
137 #define MAX_RX_QUEUE_PER_PORT 128
139 #define MAX_LCORE_PARAMS 1024
140 struct lcore_params {
144 } __rte_cache_aligned;
146 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
147 static struct lcore_params lcore_params_array_default[] = {
159 static struct lcore_params *lcore_params = lcore_params_array_default;
160 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
161 sizeof(lcore_params_array_default[0]);
163 static struct rte_eth_conf port_conf = {
165 .mq_mode = ETH_MQ_RX_RSS,
166 .max_rx_pkt_len = ETHER_MAX_LEN,
168 .header_split = 0, /**< Header Split disabled */
169 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
170 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
171 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
172 .hw_strip_crc = 0, /**< CRC stripped by hardware */
177 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
178 ETH_RSS_TCP | ETH_RSS_SCTP,
182 .mq_mode = ETH_MQ_TX_NONE,
186 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
188 /***********************start of ACL part******************************/
189 #ifdef DO_RFC_1812_CHECKS
191 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len);
194 send_single_packet(struct rte_mbuf *m, uint8_t port);
196 #define MAX_ACL_RULE_NUM 100000
197 #define DEFAULT_MAX_CATEGORIES 1
198 #define L3FWD_ACL_IPV4_NAME "l3fwd-acl-ipv4"
199 #define L3FWD_ACL_IPV6_NAME "l3fwd-acl-ipv6"
200 #define ACL_LEAD_CHAR ('@')
201 #define ROUTE_LEAD_CHAR ('R')
202 #define COMMENT_LEAD_CHAR ('#')
203 #define OPTION_CONFIG "config"
204 #define OPTION_NONUMA "no-numa"
205 #define OPTION_ENBJMO "enable-jumbo"
206 #define OPTION_RULE_IPV4 "rule_ipv4"
207 #define OPTION_RULE_IPV6 "rule_ipv6"
208 #define OPTION_SCALAR "scalar"
209 #define ACL_DENY_SIGNATURE 0xf0000000
210 #define RTE_LOGTYPE_L3FWDACL RTE_LOGTYPE_USER3
211 #define acl_log(format, ...) RTE_LOG(ERR, L3FWDACL, format, ##__VA_ARGS__)
212 #define uint32_t_to_char(ip, a, b, c, d) do {\
213 *a = (unsigned char)(ip >> 24 & 0xff);\
214 *b = (unsigned char)(ip >> 16 & 0xff);\
215 *c = (unsigned char)(ip >> 8 & 0xff);\
216 *d = (unsigned char)(ip & 0xff);\
218 #define OFF_ETHHEAD (sizeof(struct ether_hdr))
219 #define OFF_IPV42PROTO (offsetof(struct ipv4_hdr, next_proto_id))
220 #define OFF_IPV62PROTO (offsetof(struct ipv6_hdr, proto))
221 #define MBUF_IPV4_2PROTO(m) \
222 (rte_pktmbuf_mtod((m), uint8_t *) + OFF_ETHHEAD + OFF_IPV42PROTO)
223 #define MBUF_IPV6_2PROTO(m) \
224 (rte_pktmbuf_mtod((m), uint8_t *) + OFF_ETHHEAD + OFF_IPV62PROTO)
226 #define GET_CB_FIELD(in, fd, base, lim, dlm) do { \
230 val = strtoul((in), &end, (base)); \
231 if (errno != 0 || end[0] != (dlm) || val > (lim)) \
233 (fd) = (typeof(fd))val; \
238 * ACL rules should have higher priorities than route ones to ensure ACL rule
239 * always be found when input packets have multi-matches in the database.
240 * A exception case is performance measure, which can define route rules with
241 * higher priority and route rules will always be returned in each lookup.
242 * Reserve range from ACL_RULE_PRIORITY_MAX + 1 to
243 * RTE_ACL_MAX_PRIORITY for route entries in performance measure
245 #define ACL_RULE_PRIORITY_MAX 0x10000000
248 * Forward port info save in ACL lib starts from 1
249 * since ACL assume 0 is invalid.
250 * So, need add 1 when saving and minus 1 when forwarding packets.
252 #define FWD_PORT_SHIFT 1
255 * Rule and trace formats definitions.
267 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
269 .type = RTE_ACL_FIELD_TYPE_BITMASK,
270 .size = sizeof(uint8_t),
271 .field_index = PROTO_FIELD_IPV4,
272 .input_index = RTE_ACL_IPV4VLAN_PROTO,
276 .type = RTE_ACL_FIELD_TYPE_MASK,
277 .size = sizeof(uint32_t),
278 .field_index = SRC_FIELD_IPV4,
279 .input_index = RTE_ACL_IPV4VLAN_SRC,
280 .offset = offsetof(struct ipv4_hdr, src_addr) -
281 offsetof(struct ipv4_hdr, next_proto_id),
284 .type = RTE_ACL_FIELD_TYPE_MASK,
285 .size = sizeof(uint32_t),
286 .field_index = DST_FIELD_IPV4,
287 .input_index = RTE_ACL_IPV4VLAN_DST,
288 .offset = offsetof(struct ipv4_hdr, dst_addr) -
289 offsetof(struct ipv4_hdr, next_proto_id),
292 .type = RTE_ACL_FIELD_TYPE_RANGE,
293 .size = sizeof(uint16_t),
294 .field_index = SRCP_FIELD_IPV4,
295 .input_index = RTE_ACL_IPV4VLAN_PORTS,
296 .offset = sizeof(struct ipv4_hdr) -
297 offsetof(struct ipv4_hdr, next_proto_id),
300 .type = RTE_ACL_FIELD_TYPE_RANGE,
301 .size = sizeof(uint16_t),
302 .field_index = DSTP_FIELD_IPV4,
303 .input_index = RTE_ACL_IPV4VLAN_PORTS,
304 .offset = sizeof(struct ipv4_hdr) -
305 offsetof(struct ipv4_hdr, next_proto_id) +
310 #define IPV6_ADDR_LEN 16
311 #define IPV6_ADDR_U16 (IPV6_ADDR_LEN / sizeof(uint16_t))
312 #define IPV6_ADDR_U32 (IPV6_ADDR_LEN / sizeof(uint32_t))
329 struct rte_acl_field_def ipv6_defs[NUM_FIELDS_IPV6] = {
331 .type = RTE_ACL_FIELD_TYPE_BITMASK,
332 .size = sizeof(uint8_t),
333 .field_index = PROTO_FIELD_IPV6,
334 .input_index = PROTO_FIELD_IPV6,
338 .type = RTE_ACL_FIELD_TYPE_MASK,
339 .size = sizeof(uint32_t),
340 .field_index = SRC1_FIELD_IPV6,
341 .input_index = SRC1_FIELD_IPV6,
342 .offset = offsetof(struct ipv6_hdr, src_addr) -
343 offsetof(struct ipv6_hdr, proto),
346 .type = RTE_ACL_FIELD_TYPE_MASK,
347 .size = sizeof(uint32_t),
348 .field_index = SRC2_FIELD_IPV6,
349 .input_index = SRC2_FIELD_IPV6,
350 .offset = offsetof(struct ipv6_hdr, src_addr) -
351 offsetof(struct ipv6_hdr, proto) + sizeof(uint32_t),
354 .type = RTE_ACL_FIELD_TYPE_MASK,
355 .size = sizeof(uint32_t),
356 .field_index = SRC3_FIELD_IPV6,
357 .input_index = SRC3_FIELD_IPV6,
358 .offset = offsetof(struct ipv6_hdr, src_addr) -
359 offsetof(struct ipv6_hdr, proto) + 2 * sizeof(uint32_t),
362 .type = RTE_ACL_FIELD_TYPE_MASK,
363 .size = sizeof(uint32_t),
364 .field_index = SRC4_FIELD_IPV6,
365 .input_index = SRC4_FIELD_IPV6,
366 .offset = offsetof(struct ipv6_hdr, src_addr) -
367 offsetof(struct ipv6_hdr, proto) + 3 * sizeof(uint32_t),
370 .type = RTE_ACL_FIELD_TYPE_MASK,
371 .size = sizeof(uint32_t),
372 .field_index = DST1_FIELD_IPV6,
373 .input_index = DST1_FIELD_IPV6,
374 .offset = offsetof(struct ipv6_hdr, dst_addr)
375 - offsetof(struct ipv6_hdr, proto),
378 .type = RTE_ACL_FIELD_TYPE_MASK,
379 .size = sizeof(uint32_t),
380 .field_index = DST2_FIELD_IPV6,
381 .input_index = DST2_FIELD_IPV6,
382 .offset = offsetof(struct ipv6_hdr, dst_addr) -
383 offsetof(struct ipv6_hdr, proto) + sizeof(uint32_t),
386 .type = RTE_ACL_FIELD_TYPE_MASK,
387 .size = sizeof(uint32_t),
388 .field_index = DST3_FIELD_IPV6,
389 .input_index = DST3_FIELD_IPV6,
390 .offset = offsetof(struct ipv6_hdr, dst_addr) -
391 offsetof(struct ipv6_hdr, proto) + 2 * sizeof(uint32_t),
394 .type = RTE_ACL_FIELD_TYPE_MASK,
395 .size = sizeof(uint32_t),
396 .field_index = DST4_FIELD_IPV6,
397 .input_index = DST4_FIELD_IPV6,
398 .offset = offsetof(struct ipv6_hdr, dst_addr) -
399 offsetof(struct ipv6_hdr, proto) + 3 * sizeof(uint32_t),
402 .type = RTE_ACL_FIELD_TYPE_RANGE,
403 .size = sizeof(uint16_t),
404 .field_index = SRCP_FIELD_IPV6,
405 .input_index = SRCP_FIELD_IPV6,
406 .offset = sizeof(struct ipv6_hdr) -
407 offsetof(struct ipv6_hdr, proto),
410 .type = RTE_ACL_FIELD_TYPE_RANGE,
411 .size = sizeof(uint16_t),
412 .field_index = DSTP_FIELD_IPV6,
413 .input_index = SRCP_FIELD_IPV6,
414 .offset = sizeof(struct ipv6_hdr) -
415 offsetof(struct ipv6_hdr, proto) + sizeof(uint16_t),
424 CB_FLD_SRC_PORT_HIGH,
427 CB_FLD_DST_PORT_HIGH,
433 RTE_ACL_RULE_DEF(acl4_rule, RTE_DIM(ipv4_defs));
434 RTE_ACL_RULE_DEF(acl6_rule, RTE_DIM(ipv6_defs));
436 struct acl_search_t {
437 const uint8_t *data_ipv4[MAX_PKT_BURST];
438 struct rte_mbuf *m_ipv4[MAX_PKT_BURST];
439 uint32_t res_ipv4[MAX_PKT_BURST];
442 const uint8_t *data_ipv6[MAX_PKT_BURST];
443 struct rte_mbuf *m_ipv6[MAX_PKT_BURST];
444 uint32_t res_ipv6[MAX_PKT_BURST];
449 char mapped[NB_SOCKETS];
450 struct rte_acl_ctx *acx_ipv4[NB_SOCKETS];
451 struct rte_acl_ctx *acx_ipv6[NB_SOCKETS];
452 #ifdef L3FWDACL_DEBUG
453 struct acl4_rule *rule_ipv4;
454 struct acl6_rule *rule_ipv6;
459 const char *rule_ipv4_name;
460 const char *rule_ipv6_name;
464 const char cb_port_delim[] = ":";
467 print_one_ipv4_rule(struct acl4_rule *rule, int extra)
469 unsigned char a, b, c, d;
471 uint32_t_to_char(rule->field[SRC_FIELD_IPV4].value.u32,
473 printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
474 rule->field[SRC_FIELD_IPV4].mask_range.u32);
475 uint32_t_to_char(rule->field[DST_FIELD_IPV4].value.u32,
477 printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
478 rule->field[DST_FIELD_IPV4].mask_range.u32);
479 printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
480 rule->field[SRCP_FIELD_IPV4].value.u16,
481 rule->field[SRCP_FIELD_IPV4].mask_range.u16,
482 rule->field[DSTP_FIELD_IPV4].value.u16,
483 rule->field[DSTP_FIELD_IPV4].mask_range.u16,
484 rule->field[PROTO_FIELD_IPV4].value.u8,
485 rule->field[PROTO_FIELD_IPV4].mask_range.u8);
487 printf("0x%x-0x%x-0x%x ",
488 rule->data.category_mask,
490 rule->data.userdata);
494 print_one_ipv6_rule(struct acl6_rule *rule, int extra)
496 unsigned char a, b, c, d;
498 uint32_t_to_char(rule->field[SRC1_FIELD_IPV6].value.u32,
500 printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
501 uint32_t_to_char(rule->field[SRC2_FIELD_IPV6].value.u32,
503 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
504 uint32_t_to_char(rule->field[SRC3_FIELD_IPV6].value.u32,
506 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
507 uint32_t_to_char(rule->field[SRC4_FIELD_IPV6].value.u32,
509 printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
510 rule->field[SRC1_FIELD_IPV6].mask_range.u32
511 + rule->field[SRC2_FIELD_IPV6].mask_range.u32
512 + rule->field[SRC3_FIELD_IPV6].mask_range.u32
513 + rule->field[SRC4_FIELD_IPV6].mask_range.u32);
515 uint32_t_to_char(rule->field[DST1_FIELD_IPV6].value.u32,
517 printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
518 uint32_t_to_char(rule->field[DST2_FIELD_IPV6].value.u32,
520 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
521 uint32_t_to_char(rule->field[DST3_FIELD_IPV6].value.u32,
523 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
524 uint32_t_to_char(rule->field[DST4_FIELD_IPV6].value.u32,
526 printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
527 rule->field[DST1_FIELD_IPV6].mask_range.u32
528 + rule->field[DST2_FIELD_IPV6].mask_range.u32
529 + rule->field[DST3_FIELD_IPV6].mask_range.u32
530 + rule->field[DST4_FIELD_IPV6].mask_range.u32);
532 printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
533 rule->field[SRCP_FIELD_IPV6].value.u16,
534 rule->field[SRCP_FIELD_IPV6].mask_range.u16,
535 rule->field[DSTP_FIELD_IPV6].value.u16,
536 rule->field[DSTP_FIELD_IPV6].mask_range.u16,
537 rule->field[PROTO_FIELD_IPV6].value.u8,
538 rule->field[PROTO_FIELD_IPV6].mask_range.u8);
540 printf("0x%x-0x%x-0x%x ",
541 rule->data.category_mask,
543 rule->data.userdata);
546 /* Bypass comment and empty lines */
548 is_bypass_line(char *buff)
553 if (buff[0] == COMMENT_LEAD_CHAR)
556 while (buff[i] != '\0') {
557 if (!isspace(buff[i]))
564 #ifdef L3FWDACL_DEBUG
566 dump_acl4_rule(struct rte_mbuf *m, uint32_t sig)
568 uint32_t offset = sig & ~ACL_DENY_SIGNATURE;
569 unsigned char a, b, c, d;
570 struct ipv4_hdr *ipv4_hdr = (struct ipv4_hdr *)
571 (rte_pktmbuf_mtod(m, unsigned char *) +
572 sizeof(struct ether_hdr));
574 uint32_t_to_char(rte_bswap32(ipv4_hdr->src_addr), &a, &b, &c, &d);
575 printf("Packet Src:%hhu.%hhu.%hhu.%hhu ", a, b, c, d);
576 uint32_t_to_char(rte_bswap32(ipv4_hdr->dst_addr), &a, &b, &c, &d);
577 printf("Dst:%hhu.%hhu.%hhu.%hhu ", a, b, c, d);
579 printf("Src port:%hu,Dst port:%hu ",
580 rte_bswap16(*(uint16_t *)(ipv4_hdr + 1)),
581 rte_bswap16(*((uint16_t *)(ipv4_hdr + 1) + 1)));
582 printf("hit ACL %d - ", offset);
584 print_one_ipv4_rule(acl_config.rule_ipv4 + offset, 1);
590 dump_acl6_rule(struct rte_mbuf *m, uint32_t sig)
593 uint32_t offset = sig & ~ACL_DENY_SIGNATURE;
594 struct ipv6_hdr *ipv6_hdr = (struct ipv6_hdr *)
595 (rte_pktmbuf_mtod(m, unsigned char *) +
596 sizeof(struct ether_hdr));
598 printf("Packet Src");
599 for (i = 0; i < RTE_DIM(ipv6_hdr->src_addr); i += sizeof(uint16_t))
601 ipv6_hdr->src_addr[i], ipv6_hdr->src_addr[i + 1]);
604 for (i = 0; i < RTE_DIM(ipv6_hdr->dst_addr); i += sizeof(uint16_t))
606 ipv6_hdr->dst_addr[i], ipv6_hdr->dst_addr[i + 1]);
608 printf("\nSrc port:%hu,Dst port:%hu ",
609 rte_bswap16(*(uint16_t *)(ipv6_hdr + 1)),
610 rte_bswap16(*((uint16_t *)(ipv6_hdr + 1) + 1)));
611 printf("hit ACL %d - ", offset);
613 print_one_ipv6_rule(acl_config.rule_ipv6 + offset, 1);
617 #endif /* L3FWDACL_DEBUG */
620 dump_ipv4_rules(struct acl4_rule *rule, int num, int extra)
624 for (i = 0; i < num; i++, rule++) {
625 printf("\t%d:", i + 1);
626 print_one_ipv4_rule(rule, extra);
632 dump_ipv6_rules(struct acl6_rule *rule, int num, int extra)
636 for (i = 0; i < num; i++, rule++) {
637 printf("\t%d:", i + 1);
638 print_one_ipv6_rule(rule, extra);
643 #ifdef DO_RFC_1812_CHECKS
645 prepare_one_packet(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
648 struct ipv4_hdr *ipv4_hdr;
649 struct rte_mbuf *pkt = pkts_in[index];
651 int type = pkt->ol_flags & (PKT_RX_IPV4_HDR | PKT_RX_IPV6_HDR);
653 if (type == PKT_RX_IPV4_HDR) {
655 ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(pkt,
656 unsigned char *) + sizeof(struct ether_hdr));
658 /* Check to make sure the packet is valid (RFC1812) */
659 if (is_valid_ipv4_pkt(ipv4_hdr, pkt->pkt_len) >= 0) {
661 /* Update time to live and header checksum */
662 --(ipv4_hdr->time_to_live);
663 ++(ipv4_hdr->hdr_checksum);
665 /* Fill acl structure */
666 acl->data_ipv4[acl->num_ipv4] = MBUF_IPV4_2PROTO(pkt);
667 acl->m_ipv4[(acl->num_ipv4)++] = pkt;
670 /* Not a valid IPv4 packet */
671 rte_pktmbuf_free(pkt);
674 } else if (type == PKT_RX_IPV6_HDR) {
676 /* Fill acl structure */
677 acl->data_ipv6[acl->num_ipv6] = MBUF_IPV6_2PROTO(pkt);
678 acl->m_ipv6[(acl->num_ipv6)++] = pkt;
681 /* Unknown type, drop the packet */
682 rte_pktmbuf_free(pkt);
688 prepare_one_packet(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
691 struct rte_mbuf *pkt = pkts_in[index];
693 int type = pkt->ol_flags & (PKT_RX_IPV4_HDR | PKT_RX_IPV6_HDR);
695 if (type == PKT_RX_IPV4_HDR) {
697 /* Fill acl structure */
698 acl->data_ipv4[acl->num_ipv4] = MBUF_IPV4_2PROTO(pkt);
699 acl->m_ipv4[(acl->num_ipv4)++] = pkt;
702 } else if (type == PKT_RX_IPV6_HDR) {
704 /* Fill acl structure */
705 acl->data_ipv6[acl->num_ipv6] = MBUF_IPV6_2PROTO(pkt);
706 acl->m_ipv6[(acl->num_ipv6)++] = pkt;
708 /* Unknown type, drop the packet */
709 rte_pktmbuf_free(pkt);
712 #endif /* DO_RFC_1812_CHECKS */
715 prepare_acl_parameter(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
723 /* Prefetch first packets */
724 for (i = 0; i < PREFETCH_OFFSET && i < nb_rx; i++) {
725 rte_prefetch0(rte_pktmbuf_mtod(
726 pkts_in[i], void *));
729 for (i = 0; i < (nb_rx - PREFETCH_OFFSET); i++) {
730 rte_prefetch0(rte_pktmbuf_mtod(pkts_in[
731 i + PREFETCH_OFFSET], void *));
732 prepare_one_packet(pkts_in, acl, i);
735 /* Process left packets */
736 for (; i < nb_rx; i++)
737 prepare_one_packet(pkts_in, acl, i);
741 send_one_packet(struct rte_mbuf *m, uint32_t res)
743 if (likely((res & ACL_DENY_SIGNATURE) == 0 && res != 0)) {
744 /* forward packets */
745 send_single_packet(m,
746 (uint8_t)(res - FWD_PORT_SHIFT));
748 /* in the ACL list, drop it */
749 #ifdef L3FWDACL_DEBUG
750 if ((res & ACL_DENY_SIGNATURE) != 0) {
751 if (m->ol_flags & PKT_RX_IPV4_HDR)
752 dump_acl4_rule(m, res);
754 dump_acl6_rule(m, res);
764 send_packets(struct rte_mbuf **m, uint32_t *res, int num)
768 /* Prefetch first packets */
769 for (i = 0; i < PREFETCH_OFFSET && i < num; i++) {
770 rte_prefetch0(rte_pktmbuf_mtod(
774 for (i = 0; i < (num - PREFETCH_OFFSET); i++) {
775 rte_prefetch0(rte_pktmbuf_mtod(m[
776 i + PREFETCH_OFFSET], void *));
777 send_one_packet(m[i], res[i]);
780 /* Process left packets */
782 send_one_packet(m[i], res[i]);
786 * Parses IPV6 address, exepcts the following format:
787 * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX (where X - is a hexedecimal digit).
790 parse_ipv6_addr(const char *in, const char **end, uint32_t v[IPV6_ADDR_U32],
793 uint32_t addr[IPV6_ADDR_U16];
795 GET_CB_FIELD(in, addr[0], 16, UINT16_MAX, ':');
796 GET_CB_FIELD(in, addr[1], 16, UINT16_MAX, ':');
797 GET_CB_FIELD(in, addr[2], 16, UINT16_MAX, ':');
798 GET_CB_FIELD(in, addr[3], 16, UINT16_MAX, ':');
799 GET_CB_FIELD(in, addr[4], 16, UINT16_MAX, ':');
800 GET_CB_FIELD(in, addr[5], 16, UINT16_MAX, ':');
801 GET_CB_FIELD(in, addr[6], 16, UINT16_MAX, ':');
802 GET_CB_FIELD(in, addr[7], 16, UINT16_MAX, dlm);
806 v[0] = (addr[0] << 16) + addr[1];
807 v[1] = (addr[2] << 16) + addr[3];
808 v[2] = (addr[4] << 16) + addr[5];
809 v[3] = (addr[6] << 16) + addr[7];
815 parse_ipv6_net(const char *in, struct rte_acl_field field[4])
820 const uint32_t nbu32 = sizeof(uint32_t) * CHAR_BIT;
823 rc = parse_ipv6_addr(in, &mp, v, '/');
828 GET_CB_FIELD(mp, m, 0, CHAR_BIT * sizeof(v), 0);
830 /* put all together. */
831 for (i = 0; i != RTE_DIM(v); i++) {
832 if (m >= (i + 1) * nbu32)
833 field[i].mask_range.u32 = nbu32;
835 field[i].mask_range.u32 = m > (i * nbu32) ?
838 field[i].value.u32 = v[i];
845 parse_cb_ipv6_rule(char *str, struct rte_acl_rule *v, int has_userdata)
848 char *s, *sp, *in[CB_FLD_NUM];
849 static const char *dlm = " \t\n";
850 int dim = has_userdata ? CB_FLD_NUM : CB_FLD_USERDATA;
853 for (i = 0; i != dim; i++, s = NULL) {
854 in[i] = strtok_r(s, dlm, &sp);
859 rc = parse_ipv6_net(in[CB_FLD_SRC_ADDR], v->field + SRC1_FIELD_IPV6);
861 acl_log("failed to read source address/mask: %s\n",
862 in[CB_FLD_SRC_ADDR]);
866 rc = parse_ipv6_net(in[CB_FLD_DST_ADDR], v->field + DST1_FIELD_IPV6);
868 acl_log("failed to read destination address/mask: %s\n",
869 in[CB_FLD_DST_ADDR]);
874 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
875 v->field[SRCP_FIELD_IPV6].value.u16,
877 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
878 v->field[SRCP_FIELD_IPV6].mask_range.u16,
881 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
882 sizeof(cb_port_delim)) != 0)
885 /* destination port. */
886 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
887 v->field[DSTP_FIELD_IPV6].value.u16,
889 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
890 v->field[DSTP_FIELD_IPV6].mask_range.u16,
893 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
894 sizeof(cb_port_delim)) != 0)
897 if (v->field[SRCP_FIELD_IPV6].mask_range.u16
898 < v->field[SRCP_FIELD_IPV6].value.u16
899 || v->field[DSTP_FIELD_IPV6].mask_range.u16
900 < v->field[DSTP_FIELD_IPV6].value.u16)
903 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].value.u8,
905 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].mask_range.u8,
909 GET_CB_FIELD(in[CB_FLD_USERDATA], v->data.userdata,
916 * Parse ClassBench rules file.
918 * '@'<src_ipv4_addr>'/'<masklen> <space> \
919 * <dst_ipv4_addr>'/'<masklen> <space> \
920 * <src_port_low> <space> ":" <src_port_high> <space> \
921 * <dst_port_low> <space> ":" <dst_port_high> <space> \
925 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
927 uint8_t a, b, c, d, m;
929 GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
930 GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
931 GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
932 GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
933 GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
935 addr[0] = IPv4(a, b, c, d);
942 parse_cb_ipv4vlan_rule(char *str, struct rte_acl_rule *v, int has_userdata)
945 char *s, *sp, *in[CB_FLD_NUM];
946 static const char *dlm = " \t\n";
947 int dim = has_userdata ? CB_FLD_NUM : CB_FLD_USERDATA;
950 for (i = 0; i != dim; i++, s = NULL) {
951 in[i] = strtok_r(s, dlm, &sp);
956 rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
957 &v->field[SRC_FIELD_IPV4].value.u32,
958 &v->field[SRC_FIELD_IPV4].mask_range.u32);
960 acl_log("failed to read source address/mask: %s\n",
961 in[CB_FLD_SRC_ADDR]);
965 rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
966 &v->field[DST_FIELD_IPV4].value.u32,
967 &v->field[DST_FIELD_IPV4].mask_range.u32);
969 acl_log("failed to read destination address/mask: %s\n",
970 in[CB_FLD_DST_ADDR]);
974 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
975 v->field[SRCP_FIELD_IPV4].value.u16,
977 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
978 v->field[SRCP_FIELD_IPV4].mask_range.u16,
981 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
982 sizeof(cb_port_delim)) != 0)
985 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
986 v->field[DSTP_FIELD_IPV4].value.u16,
988 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
989 v->field[DSTP_FIELD_IPV4].mask_range.u16,
992 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
993 sizeof(cb_port_delim)) != 0)
996 if (v->field[SRCP_FIELD_IPV4].mask_range.u16
997 < v->field[SRCP_FIELD_IPV4].value.u16
998 || v->field[DSTP_FIELD_IPV4].mask_range.u16
999 < v->field[DSTP_FIELD_IPV4].value.u16)
1002 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].value.u8,
1004 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].mask_range.u8,
1008 GET_CB_FIELD(in[CB_FLD_USERDATA], v->data.userdata, 0,
1015 add_rules(const char *rule_path,
1016 struct rte_acl_rule **proute_base,
1017 unsigned int *proute_num,
1018 struct rte_acl_rule **pacl_base,
1019 unsigned int *pacl_num, uint32_t rule_size,
1020 int (*parser)(char *, struct rte_acl_rule*, int))
1022 uint8_t *acl_rules, *route_rules;
1023 struct rte_acl_rule *next;
1024 unsigned int acl_num = 0, route_num = 0, total_num = 0;
1025 unsigned int acl_cnt = 0, route_cnt = 0;
1026 char buff[LINE_MAX];
1027 FILE *fh = fopen(rule_path, "rb");
1031 rte_exit(EXIT_FAILURE, "%s: Open %s failed\n", __func__,
1034 while ((fgets(buff, LINE_MAX, fh) != NULL)) {
1035 if (buff[0] == ROUTE_LEAD_CHAR)
1037 else if (buff[0] == ACL_LEAD_CHAR)
1042 rte_exit(EXIT_FAILURE, "Not find any route entries in %s!\n",
1045 fseek(fh, 0, SEEK_SET);
1047 acl_rules = calloc(acl_num, rule_size);
1049 if (NULL == acl_rules)
1050 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1053 route_rules = calloc(route_num, rule_size);
1055 if (NULL == route_rules)
1056 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1060 while (fgets(buff, LINE_MAX, fh) != NULL) {
1063 if (is_bypass_line(buff))
1069 if (s == ROUTE_LEAD_CHAR)
1070 next = (struct rte_acl_rule *)(route_rules +
1071 route_cnt * rule_size);
1074 else if (s == ACL_LEAD_CHAR)
1075 next = (struct rte_acl_rule *)(acl_rules +
1076 acl_cnt * rule_size);
1080 rte_exit(EXIT_FAILURE,
1081 "%s Line %u: should start with leading "
1083 rule_path, i, ROUTE_LEAD_CHAR, ACL_LEAD_CHAR);
1085 if (parser(buff + 1, next, s == ROUTE_LEAD_CHAR) != 0)
1086 rte_exit(EXIT_FAILURE,
1087 "%s Line %u: parse rules error\n",
1090 if (s == ROUTE_LEAD_CHAR) {
1091 /* Check the forwarding port number */
1092 if ((enabled_port_mask & (1 << next->data.userdata)) ==
1094 rte_exit(EXIT_FAILURE,
1095 "%s Line %u: fwd number illegal:%u\n",
1096 rule_path, i, next->data.userdata);
1097 next->data.userdata += FWD_PORT_SHIFT;
1100 next->data.userdata = ACL_DENY_SIGNATURE + acl_cnt;
1104 next->data.priority = RTE_ACL_MAX_PRIORITY - total_num;
1105 next->data.category_mask = -1;
1111 *pacl_base = (struct rte_acl_rule *)acl_rules;
1112 *pacl_num = acl_num;
1113 *proute_base = (struct rte_acl_rule *)route_rules;
1114 *proute_num = route_cnt;
1120 dump_acl_config(void)
1122 printf("ACL option are:\n");
1123 printf(OPTION_RULE_IPV4": %s\n", parm_config.rule_ipv4_name);
1124 printf(OPTION_RULE_IPV6": %s\n", parm_config.rule_ipv6_name);
1125 printf(OPTION_SCALAR": %d\n", parm_config.scalar);
1129 check_acl_config(void)
1131 if (parm_config.rule_ipv4_name == NULL) {
1132 acl_log("ACL IPv4 rule file not specified\n");
1134 } else if (parm_config.rule_ipv6_name == NULL) {
1135 acl_log("ACL IPv6 rule file not specified\n");
1142 static struct rte_acl_ctx*
1143 setup_acl(struct rte_acl_rule *route_base,
1144 struct rte_acl_rule *acl_base, unsigned int route_num,
1145 unsigned int acl_num, int ipv6, int socketid)
1147 char name[PATH_MAX];
1148 struct rte_acl_param acl_param;
1149 struct rte_acl_config acl_build_param;
1150 struct rte_acl_ctx *context;
1151 int dim = ipv6 ? RTE_DIM(ipv6_defs) : RTE_DIM(ipv4_defs);
1153 /* Create ACL contexts */
1154 snprintf(name, sizeof(name), "%s%d",
1155 ipv6 ? L3FWD_ACL_IPV6_NAME : L3FWD_ACL_IPV4_NAME,
1158 acl_param.name = name;
1159 acl_param.socket_id = socketid;
1160 acl_param.rule_size = RTE_ACL_RULE_SZ(dim);
1161 acl_param.max_rule_num = MAX_ACL_RULE_NUM;
1163 if ((context = rte_acl_create(&acl_param)) == NULL)
1164 rte_exit(EXIT_FAILURE, "Failed to create ACL context\n");
1166 if (parm_config.scalar && rte_acl_set_ctx_classify(context,
1167 RTE_ACL_CLASSIFY_SCALAR) != 0)
1168 rte_exit(EXIT_FAILURE,
1169 "Failed to setup classify method for ACL context\n");
1171 if (rte_acl_add_rules(context, route_base, route_num) < 0)
1172 rte_exit(EXIT_FAILURE, "add rules failed\n");
1174 if (rte_acl_add_rules(context, acl_base, acl_num) < 0)
1175 rte_exit(EXIT_FAILURE, "add rules failed\n");
1177 /* Perform builds */
1178 memset(&acl_build_param, 0, sizeof(acl_build_param));
1180 acl_build_param.num_categories = DEFAULT_MAX_CATEGORIES;
1181 acl_build_param.num_fields = dim;
1182 memcpy(&acl_build_param.defs, ipv6 ? ipv6_defs : ipv4_defs,
1183 ipv6 ? sizeof(ipv6_defs) : sizeof(ipv4_defs));
1185 if (rte_acl_build(context, &acl_build_param) != 0)
1186 rte_exit(EXIT_FAILURE, "Failed to build ACL trie\n");
1188 rte_acl_dump(context);
1199 struct rte_acl_rule *acl_base_ipv4, *route_base_ipv4,
1200 *acl_base_ipv6, *route_base_ipv6;
1201 unsigned int acl_num_ipv4 = 0, route_num_ipv4 = 0,
1202 acl_num_ipv6 = 0, route_num_ipv6 = 0;
1204 if (check_acl_config() != 0)
1205 rte_exit(EXIT_FAILURE, "Failed to get valid ACL options\n");
1209 /* Load rules from the input file */
1210 if (add_rules(parm_config.rule_ipv4_name, &route_base_ipv4,
1211 &route_num_ipv4, &acl_base_ipv4, &acl_num_ipv4,
1212 sizeof(struct acl4_rule), &parse_cb_ipv4vlan_rule) < 0)
1213 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1215 acl_log("IPv4 Route entries %u:\n", route_num_ipv4);
1216 dump_ipv4_rules((struct acl4_rule *)route_base_ipv4, route_num_ipv4, 1);
1218 acl_log("IPv4 ACL entries %u:\n", acl_num_ipv4);
1219 dump_ipv4_rules((struct acl4_rule *)acl_base_ipv4, acl_num_ipv4, 1);
1221 if (add_rules(parm_config.rule_ipv6_name, &route_base_ipv6,
1223 &acl_base_ipv6, &acl_num_ipv6,
1224 sizeof(struct acl6_rule), &parse_cb_ipv6_rule) < 0)
1225 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1227 acl_log("IPv6 Route entries %u:\n", route_num_ipv6);
1228 dump_ipv6_rules((struct acl6_rule *)route_base_ipv6, route_num_ipv6, 1);
1230 acl_log("IPv6 ACL entries %u:\n", acl_num_ipv6);
1231 dump_ipv6_rules((struct acl6_rule *)acl_base_ipv6, acl_num_ipv6, 1);
1233 memset(&acl_config, 0, sizeof(acl_config));
1235 /* Check sockets a context should be created on */
1237 acl_config.mapped[0] = 1;
1239 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1240 if (rte_lcore_is_enabled(lcore_id) == 0)
1243 socketid = rte_lcore_to_socket_id(lcore_id);
1244 if (socketid >= NB_SOCKETS) {
1245 acl_log("Socket %d of lcore %u is out "
1247 socketid, lcore_id, NB_SOCKETS);
1248 free(route_base_ipv4);
1249 free(route_base_ipv6);
1250 free(acl_base_ipv4);
1251 free(acl_base_ipv6);
1255 acl_config.mapped[socketid] = 1;
1259 for (i = 0; i < NB_SOCKETS; i++) {
1260 if (acl_config.mapped[i]) {
1261 acl_config.acx_ipv4[i] = setup_acl(route_base_ipv4,
1262 acl_base_ipv4, route_num_ipv4, acl_num_ipv4,
1265 acl_config.acx_ipv6[i] = setup_acl(route_base_ipv6,
1266 acl_base_ipv6, route_num_ipv6, acl_num_ipv6,
1271 free(route_base_ipv4);
1272 free(route_base_ipv6);
1274 #ifdef L3FWDACL_DEBUG
1275 acl_config.rule_ipv4 = (struct acl4_rule *)acl_base_ipv4;
1276 acl_config.rule_ipv6 = (struct acl6_rule *)acl_base_ipv6;
1278 free(acl_base_ipv4);
1279 free(acl_base_ipv6);
1285 /***********************end of ACL part******************************/
1288 uint16_t n_rx_queue;
1289 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
1290 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
1291 struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
1292 } __rte_cache_aligned;
1294 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
1296 /* Send burst of packets on an output interface */
1298 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
1300 struct rte_mbuf **m_table;
1304 queueid = qconf->tx_queue_id[port];
1305 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
1307 ret = rte_eth_tx_burst(port, queueid, m_table, n);
1308 if (unlikely(ret < n)) {
1310 rte_pktmbuf_free(m_table[ret]);
1311 } while (++ret < n);
1317 /* Enqueue a single packet, and send burst if queue is filled */
1319 send_single_packet(struct rte_mbuf *m, uint8_t port)
1323 struct lcore_conf *qconf;
1325 lcore_id = rte_lcore_id();
1327 qconf = &lcore_conf[lcore_id];
1328 len = qconf->tx_mbufs[port].len;
1329 qconf->tx_mbufs[port].m_table[len] = m;
1332 /* enough pkts to be sent */
1333 if (unlikely(len == MAX_PKT_BURST)) {
1334 send_burst(qconf, MAX_PKT_BURST, port);
1338 qconf->tx_mbufs[port].len = len;
1342 #ifdef DO_RFC_1812_CHECKS
1344 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
1346 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
1348 * 1. The packet length reported by the Link Layer must be large
1349 * enough to hold the minimum length legal IP datagram (20 bytes).
1351 if (link_len < sizeof(struct ipv4_hdr))
1354 /* 2. The IP checksum must be correct. */
1355 /* this is checked in H/W */
1358 * 3. The IP version number must be 4. If the version number is not 4
1359 * then the packet may be another version of IP, such as IPng or
1362 if (((pkt->version_ihl) >> 4) != 4)
1365 * 4. The IP header length field must be large enough to hold the
1366 * minimum length legal IP datagram (20 bytes = 5 words).
1368 if ((pkt->version_ihl & 0xf) < 5)
1372 * 5. The IP total length field must be large enough to hold the IP
1373 * datagram header, whose length is specified in the IP header length
1376 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
1383 /* main processing loop */
1385 main_loop(__attribute__((unused)) void *dummy)
1387 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1389 uint64_t prev_tsc, diff_tsc, cur_tsc;
1391 uint8_t portid, queueid;
1392 struct lcore_conf *qconf;
1394 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
1395 / US_PER_S * BURST_TX_DRAIN_US;
1398 lcore_id = rte_lcore_id();
1399 qconf = &lcore_conf[lcore_id];
1400 socketid = rte_lcore_to_socket_id(lcore_id);
1402 if (qconf->n_rx_queue == 0) {
1403 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
1407 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
1409 for (i = 0; i < qconf->n_rx_queue; i++) {
1411 portid = qconf->rx_queue_list[i].port_id;
1412 queueid = qconf->rx_queue_list[i].queue_id;
1413 RTE_LOG(INFO, L3FWD,
1414 " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
1415 lcore_id, portid, queueid);
1420 cur_tsc = rte_rdtsc();
1423 * TX burst queue drain
1425 diff_tsc = cur_tsc - prev_tsc;
1426 if (unlikely(diff_tsc > drain_tsc)) {
1429 * This could be optimized (use queueid instead of
1430 * portid), but it is not called so often
1432 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
1433 if (qconf->tx_mbufs[portid].len == 0)
1435 send_burst(&lcore_conf[lcore_id],
1436 qconf->tx_mbufs[portid].len,
1438 qconf->tx_mbufs[portid].len = 0;
1445 * Read packet from RX queues
1447 for (i = 0; i < qconf->n_rx_queue; ++i) {
1449 portid = qconf->rx_queue_list[i].port_id;
1450 queueid = qconf->rx_queue_list[i].queue_id;
1451 nb_rx = rte_eth_rx_burst(portid, queueid,
1452 pkts_burst, MAX_PKT_BURST);
1455 struct acl_search_t acl_search;
1457 prepare_acl_parameter(pkts_burst, &acl_search,
1460 if (acl_search.num_ipv4) {
1462 acl_config.acx_ipv4[socketid],
1463 acl_search.data_ipv4,
1464 acl_search.res_ipv4,
1465 acl_search.num_ipv4,
1466 DEFAULT_MAX_CATEGORIES);
1468 send_packets(acl_search.m_ipv4,
1469 acl_search.res_ipv4,
1470 acl_search.num_ipv4);
1473 if (acl_search.num_ipv6) {
1475 acl_config.acx_ipv6[socketid],
1476 acl_search.data_ipv6,
1477 acl_search.res_ipv6,
1478 acl_search.num_ipv6,
1479 DEFAULT_MAX_CATEGORIES);
1481 send_packets(acl_search.m_ipv6,
1482 acl_search.res_ipv6,
1483 acl_search.num_ipv6);
1491 check_lcore_params(void)
1493 uint8_t queue, lcore;
1497 for (i = 0; i < nb_lcore_params; ++i) {
1498 queue = lcore_params[i].queue_id;
1499 if (queue >= MAX_RX_QUEUE_PER_PORT) {
1500 printf("invalid queue number: %hhu\n", queue);
1503 lcore = lcore_params[i].lcore_id;
1504 if (!rte_lcore_is_enabled(lcore)) {
1505 printf("error: lcore %hhu is not enabled in "
1506 "lcore mask\n", lcore);
1509 socketid = rte_lcore_to_socket_id(lcore);
1510 if (socketid != 0 && numa_on == 0) {
1511 printf("warning: lcore %hhu is on socket %d "
1520 check_port_config(const unsigned nb_ports)
1525 for (i = 0; i < nb_lcore_params; ++i) {
1526 portid = lcore_params[i].port_id;
1528 if ((enabled_port_mask & (1 << portid)) == 0) {
1529 printf("port %u is not enabled in port mask\n", portid);
1532 if (portid >= nb_ports) {
1533 printf("port %u is not present on the board\n", portid);
1541 get_port_n_rx_queues(const uint8_t port)
1546 for (i = 0; i < nb_lcore_params; ++i) {
1547 if (lcore_params[i].port_id == port &&
1548 lcore_params[i].queue_id > queue)
1549 queue = lcore_params[i].queue_id;
1551 return (uint8_t)(++queue);
1555 init_lcore_rx_queues(void)
1557 uint16_t i, nb_rx_queue;
1560 for (i = 0; i < nb_lcore_params; ++i) {
1561 lcore = lcore_params[i].lcore_id;
1562 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1563 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1564 printf("error: too many queues (%u) for lcore: %u\n",
1565 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
1568 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1569 lcore_params[i].port_id;
1570 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1571 lcore_params[i].queue_id;
1572 lcore_conf[lcore].n_rx_queue++;
1580 print_usage(const char *prgname)
1582 printf("%s [EAL options] -- -p PORTMASK -P"
1583 "--"OPTION_RULE_IPV4"=FILE"
1584 "--"OPTION_RULE_IPV6"=FILE"
1585 " [--"OPTION_CONFIG" (port,queue,lcore)[,(port,queue,lcore]]"
1586 " [--"OPTION_ENBJMO" [--max-pkt-len PKTLEN]]\n"
1587 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1588 " -P : enable promiscuous mode\n"
1589 " --"OPTION_CONFIG": (port,queue,lcore): "
1590 "rx queues configuration\n"
1591 " --"OPTION_NONUMA": optional, disable numa awareness\n"
1592 " --"OPTION_ENBJMO": enable jumbo frame"
1593 " which max packet len is PKTLEN in decimal (64-9600)\n"
1594 " --"OPTION_RULE_IPV4"=FILE: specify the ipv4 rules entries "
1596 "Each rule occupy one line. "
1597 "2 kinds of rules are supported. "
1598 "One is ACL entry at while line leads with character '%c', "
1599 "another is route entry at while line leads with "
1601 " --"OPTION_RULE_IPV6"=FILE: specify the ipv6 rules "
1603 " --"OPTION_SCALAR": Use scalar function to do lookup\n",
1604 prgname, ACL_LEAD_CHAR, ROUTE_LEAD_CHAR);
1608 parse_max_pkt_len(const char *pktlen)
1613 /* parse decimal string */
1614 len = strtoul(pktlen, &end, 10);
1615 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
1625 parse_portmask(const char *portmask)
1630 /* parse hexadecimal string */
1631 pm = strtoul(portmask, &end, 16);
1632 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1642 parse_config(const char *q_arg)
1645 const char *p, *p0 = q_arg;
1653 unsigned long int_fld[_NUM_FLD];
1654 char *str_fld[_NUM_FLD];
1658 nb_lcore_params = 0;
1660 while ((p = strchr(p0, '(')) != NULL) {
1662 if ((p0 = strchr(p, ')')) == NULL)
1666 if (size >= sizeof(s))
1669 snprintf(s, sizeof(s), "%.*s", size, p);
1670 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1673 for (i = 0; i < _NUM_FLD; i++) {
1675 int_fld[i] = strtoul(str_fld[i], &end, 0);
1676 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1679 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1680 printf("exceeded max number of lcore params: %hu\n",
1684 lcore_params_array[nb_lcore_params].port_id =
1685 (uint8_t)int_fld[FLD_PORT];
1686 lcore_params_array[nb_lcore_params].queue_id =
1687 (uint8_t)int_fld[FLD_QUEUE];
1688 lcore_params_array[nb_lcore_params].lcore_id =
1689 (uint8_t)int_fld[FLD_LCORE];
1692 lcore_params = lcore_params_array;
1696 /* Parse the argument given in the command line of the application */
1698 parse_args(int argc, char **argv)
1703 char *prgname = argv[0];
1704 static struct option lgopts[] = {
1705 {OPTION_CONFIG, 1, 0, 0},
1706 {OPTION_NONUMA, 0, 0, 0},
1707 {OPTION_ENBJMO, 0, 0, 0},
1708 {OPTION_RULE_IPV4, 1, 0, 0},
1709 {OPTION_RULE_IPV6, 1, 0, 0},
1710 {OPTION_SCALAR, 0, 0, 0},
1716 while ((opt = getopt_long(argc, argvopt, "p:P",
1717 lgopts, &option_index)) != EOF) {
1722 enabled_port_mask = parse_portmask(optarg);
1723 if (enabled_port_mask == 0) {
1724 printf("invalid portmask\n");
1725 print_usage(prgname);
1730 printf("Promiscuous mode selected\n");
1736 if (!strncmp(lgopts[option_index].name,
1738 sizeof(OPTION_CONFIG))) {
1739 ret = parse_config(optarg);
1741 printf("invalid config\n");
1742 print_usage(prgname);
1747 if (!strncmp(lgopts[option_index].name,
1749 sizeof(OPTION_NONUMA))) {
1750 printf("numa is disabled\n");
1754 if (!strncmp(lgopts[option_index].name,
1755 OPTION_ENBJMO, sizeof(OPTION_ENBJMO))) {
1756 struct option lenopts = {
1763 printf("jumbo frame is enabled\n");
1764 port_conf.rxmode.jumbo_frame = 1;
1767 * if no max-pkt-len set, then use the
1768 * default value ETHER_MAX_LEN
1770 if (0 == getopt_long(argc, argvopt, "",
1771 &lenopts, &option_index)) {
1772 ret = parse_max_pkt_len(optarg);
1774 (ret > MAX_JUMBO_PKT_LEN)) {
1775 printf("invalid packet "
1777 print_usage(prgname);
1780 port_conf.rxmode.max_rx_pkt_len = ret;
1782 printf("set jumbo frame max packet length "
1785 port_conf.rxmode.max_rx_pkt_len);
1788 if (!strncmp(lgopts[option_index].name,
1790 sizeof(OPTION_RULE_IPV4)))
1791 parm_config.rule_ipv4_name = optarg;
1793 if (!strncmp(lgopts[option_index].name,
1795 sizeof(OPTION_RULE_IPV6))) {
1796 parm_config.rule_ipv6_name = optarg;
1799 if (!strncmp(lgopts[option_index].name,
1800 OPTION_SCALAR, sizeof(OPTION_SCALAR)))
1801 parm_config.scalar = 1;
1807 print_usage(prgname);
1813 argv[optind-1] = prgname;
1816 optind = 0; /* reset getopt lib */
1821 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
1823 char buf[ETHER_ADDR_FMT_SIZE];
1824 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
1825 printf("%s%s", name, buf);
1829 init_mem(unsigned nb_mbuf)
1835 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1836 if (rte_lcore_is_enabled(lcore_id) == 0)
1840 socketid = rte_lcore_to_socket_id(lcore_id);
1844 if (socketid >= NB_SOCKETS) {
1845 rte_exit(EXIT_FAILURE,
1846 "Socket %d of lcore %u is out of range %d\n",
1847 socketid, lcore_id, NB_SOCKETS);
1849 if (pktmbuf_pool[socketid] == NULL) {
1850 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
1851 pktmbuf_pool[socketid] =
1852 rte_mempool_create(s, nb_mbuf, MBUF_SIZE,
1854 sizeof(struct rte_pktmbuf_pool_private),
1855 rte_pktmbuf_pool_init, NULL,
1856 rte_pktmbuf_init, NULL,
1858 if (pktmbuf_pool[socketid] == NULL)
1859 rte_exit(EXIT_FAILURE,
1860 "Cannot init mbuf pool on socket %d\n",
1863 printf("Allocated mbuf pool on socket %d\n",
1870 /* Check the link status of all ports in up to 9s, and print them finally */
1872 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1874 #define CHECK_INTERVAL 100 /* 100ms */
1875 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1876 uint8_t portid, count, all_ports_up, print_flag = 0;
1877 struct rte_eth_link link;
1879 printf("\nChecking link status");
1881 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1883 for (portid = 0; portid < port_num; portid++) {
1884 if ((port_mask & (1 << portid)) == 0)
1886 memset(&link, 0, sizeof(link));
1887 rte_eth_link_get_nowait(portid, &link);
1888 /* print link status if flag set */
1889 if (print_flag == 1) {
1890 if (link.link_status)
1891 printf("Port %d Link Up - speed %u "
1892 "Mbps - %s\n", (uint8_t)portid,
1893 (unsigned)link.link_speed,
1894 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1895 ("full-duplex") : ("half-duplex\n"));
1897 printf("Port %d Link Down\n",
1901 /* clear all_ports_up flag if any link down */
1902 if (link.link_status == 0) {
1907 /* after finally printing all link status, get out */
1908 if (print_flag == 1)
1911 if (all_ports_up == 0) {
1914 rte_delay_ms(CHECK_INTERVAL);
1917 /* set the print_flag if all ports up or timeout */
1918 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1926 main(int argc, char **argv)
1928 struct lcore_conf *qconf;
1929 struct rte_eth_dev_info dev_info;
1930 struct rte_eth_txconf *txconf;
1935 uint32_t n_tx_queue, nb_lcores;
1936 uint8_t portid, nb_rx_queue, queue, socketid;
1939 ret = rte_eal_init(argc, argv);
1941 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1945 /* parse application arguments (after the EAL ones) */
1946 ret = parse_args(argc, argv);
1948 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
1950 if (check_lcore_params() < 0)
1951 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
1953 ret = init_lcore_rx_queues();
1955 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1957 nb_ports = rte_eth_dev_count();
1958 if (nb_ports > RTE_MAX_ETHPORTS)
1959 nb_ports = RTE_MAX_ETHPORTS;
1961 if (check_port_config(nb_ports) < 0)
1962 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
1964 /* Add ACL rules and route entries, build trie */
1965 if (app_acl_init() < 0)
1966 rte_exit(EXIT_FAILURE, "app_acl_init failed\n");
1968 nb_lcores = rte_lcore_count();
1970 /* initialize all ports */
1971 for (portid = 0; portid < nb_ports; portid++) {
1972 /* skip ports that are not enabled */
1973 if ((enabled_port_mask & (1 << portid)) == 0) {
1974 printf("\nSkipping disabled port %d\n", portid);
1979 printf("Initializing port %d ... ", portid);
1982 nb_rx_queue = get_port_n_rx_queues(portid);
1983 n_tx_queue = nb_lcores;
1984 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
1985 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
1986 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
1987 nb_rx_queue, (unsigned)n_tx_queue);
1988 ret = rte_eth_dev_configure(portid, nb_rx_queue,
1989 (uint16_t)n_tx_queue, &port_conf);
1991 rte_exit(EXIT_FAILURE,
1992 "Cannot configure device: err=%d, port=%d\n",
1995 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1996 print_ethaddr(" Address:", &ports_eth_addr[portid]);
2000 ret = init_mem(NB_MBUF);
2002 rte_exit(EXIT_FAILURE, "init_mem failed\n");
2004 /* init one TX queue per couple (lcore,port) */
2006 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2007 if (rte_lcore_is_enabled(lcore_id) == 0)
2011 socketid = (uint8_t)
2012 rte_lcore_to_socket_id(lcore_id);
2016 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
2019 rte_eth_dev_info_get(portid, &dev_info);
2020 txconf = &dev_info.default_txconf;
2021 if (port_conf.rxmode.jumbo_frame)
2022 txconf->txq_flags = 0;
2023 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2026 rte_exit(EXIT_FAILURE,
2027 "rte_eth_tx_queue_setup: err=%d, "
2028 "port=%d\n", ret, portid);
2030 qconf = &lcore_conf[lcore_id];
2031 qconf->tx_queue_id[portid] = queueid;
2037 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2038 if (rte_lcore_is_enabled(lcore_id) == 0)
2040 qconf = &lcore_conf[lcore_id];
2041 printf("\nInitializing rx queues on lcore %u ... ", lcore_id);
2043 /* init RX queues */
2044 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
2045 portid = qconf->rx_queue_list[queue].port_id;
2046 queueid = qconf->rx_queue_list[queue].queue_id;
2049 socketid = (uint8_t)
2050 rte_lcore_to_socket_id(lcore_id);
2054 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2057 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2059 pktmbuf_pool[socketid]);
2061 rte_exit(EXIT_FAILURE,
2062 "rte_eth_rx_queue_setup: err=%d,"
2063 "port=%d\n", ret, portid);
2070 for (portid = 0; portid < nb_ports; portid++) {
2071 if ((enabled_port_mask & (1 << portid)) == 0)
2075 ret = rte_eth_dev_start(portid);
2077 rte_exit(EXIT_FAILURE,
2078 "rte_eth_dev_start: err=%d, port=%d\n",
2082 * If enabled, put device in promiscuous mode.
2083 * This allows IO forwarding mode to forward packets
2084 * to itself through 2 cross-connected ports of the
2088 rte_eth_promiscuous_enable(portid);
2091 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
2093 /* launch per-lcore init on every lcore */
2094 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2095 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2096 if (rte_eal_wait_lcore(lcore_id) < 0)