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>
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. */
124 struct rte_mbuf *m_table[MAX_PKT_BURST];
127 struct lcore_rx_queue {
130 } __rte_cache_aligned;
132 #define MAX_RX_QUEUE_PER_LCORE 16
133 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
134 #define MAX_RX_QUEUE_PER_PORT 128
136 #define MAX_LCORE_PARAMS 1024
137 struct lcore_params {
141 } __rte_cache_aligned;
143 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
144 static struct lcore_params lcore_params_array_default[] = {
156 static struct lcore_params *lcore_params = lcore_params_array_default;
157 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
158 sizeof(lcore_params_array_default[0]);
160 static struct rte_eth_conf port_conf = {
162 .mq_mode = ETH_MQ_RX_RSS,
163 .max_rx_pkt_len = ETHER_MAX_LEN,
165 .header_split = 0, /**< Header Split disabled */
166 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
167 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
168 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
169 .hw_strip_crc = 0, /**< CRC stripped by hardware */
174 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
175 ETH_RSS_TCP | ETH_RSS_SCTP,
179 .mq_mode = ETH_MQ_TX_NONE,
183 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
185 /***********************start of ACL part******************************/
186 #ifdef DO_RFC_1812_CHECKS
188 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len);
191 send_single_packet(struct rte_mbuf *m, uint8_t port);
193 #define MAX_ACL_RULE_NUM 100000
194 #define DEFAULT_MAX_CATEGORIES 1
195 #define L3FWD_ACL_IPV4_NAME "l3fwd-acl-ipv4"
196 #define L3FWD_ACL_IPV6_NAME "l3fwd-acl-ipv6"
197 #define ACL_LEAD_CHAR ('@')
198 #define ROUTE_LEAD_CHAR ('R')
199 #define COMMENT_LEAD_CHAR ('#')
200 #define OPTION_CONFIG "config"
201 #define OPTION_NONUMA "no-numa"
202 #define OPTION_ENBJMO "enable-jumbo"
203 #define OPTION_RULE_IPV4 "rule_ipv4"
204 #define OPTION_RULE_IPV6 "rule_ipv6"
205 #define OPTION_SCALAR "scalar"
206 #define ACL_DENY_SIGNATURE 0xf0000000
207 #define RTE_LOGTYPE_L3FWDACL RTE_LOGTYPE_USER3
208 #define acl_log(format, ...) RTE_LOG(ERR, L3FWDACL, format, ##__VA_ARGS__)
209 #define uint32_t_to_char(ip, a, b, c, d) do {\
210 *a = (unsigned char)(ip >> 24 & 0xff);\
211 *b = (unsigned char)(ip >> 16 & 0xff);\
212 *c = (unsigned char)(ip >> 8 & 0xff);\
213 *d = (unsigned char)(ip & 0xff);\
215 #define OFF_ETHHEAD (sizeof(struct ether_hdr))
216 #define OFF_IPV42PROTO (offsetof(struct ipv4_hdr, next_proto_id))
217 #define OFF_IPV62PROTO (offsetof(struct ipv6_hdr, proto))
218 #define MBUF_IPV4_2PROTO(m) \
219 rte_pktmbuf_mtod_offset((m), uint8_t *, OFF_ETHHEAD + OFF_IPV42PROTO)
220 #define MBUF_IPV6_2PROTO(m) \
221 rte_pktmbuf_mtod_offset((m), uint8_t *, OFF_ETHHEAD + OFF_IPV62PROTO)
223 #define GET_CB_FIELD(in, fd, base, lim, dlm) do { \
227 val = strtoul((in), &end, (base)); \
228 if (errno != 0 || end[0] != (dlm) || val > (lim)) \
230 (fd) = (typeof(fd))val; \
235 * ACL rules should have higher priorities than route ones to ensure ACL rule
236 * always be found when input packets have multi-matches in the database.
237 * A exception case is performance measure, which can define route rules with
238 * higher priority and route rules will always be returned in each lookup.
239 * Reserve range from ACL_RULE_PRIORITY_MAX + 1 to
240 * RTE_ACL_MAX_PRIORITY for route entries in performance measure
242 #define ACL_RULE_PRIORITY_MAX 0x10000000
245 * Forward port info save in ACL lib starts from 1
246 * since ACL assume 0 is invalid.
247 * So, need add 1 when saving and minus 1 when forwarding packets.
249 #define FWD_PORT_SHIFT 1
252 * Rule and trace formats definitions.
264 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
266 .type = RTE_ACL_FIELD_TYPE_BITMASK,
267 .size = sizeof(uint8_t),
268 .field_index = PROTO_FIELD_IPV4,
269 .input_index = RTE_ACL_IPV4VLAN_PROTO,
273 .type = RTE_ACL_FIELD_TYPE_MASK,
274 .size = sizeof(uint32_t),
275 .field_index = SRC_FIELD_IPV4,
276 .input_index = RTE_ACL_IPV4VLAN_SRC,
277 .offset = offsetof(struct ipv4_hdr, src_addr) -
278 offsetof(struct ipv4_hdr, next_proto_id),
281 .type = RTE_ACL_FIELD_TYPE_MASK,
282 .size = sizeof(uint32_t),
283 .field_index = DST_FIELD_IPV4,
284 .input_index = RTE_ACL_IPV4VLAN_DST,
285 .offset = offsetof(struct ipv4_hdr, dst_addr) -
286 offsetof(struct ipv4_hdr, next_proto_id),
289 .type = RTE_ACL_FIELD_TYPE_RANGE,
290 .size = sizeof(uint16_t),
291 .field_index = SRCP_FIELD_IPV4,
292 .input_index = RTE_ACL_IPV4VLAN_PORTS,
293 .offset = sizeof(struct ipv4_hdr) -
294 offsetof(struct ipv4_hdr, next_proto_id),
297 .type = RTE_ACL_FIELD_TYPE_RANGE,
298 .size = sizeof(uint16_t),
299 .field_index = DSTP_FIELD_IPV4,
300 .input_index = RTE_ACL_IPV4VLAN_PORTS,
301 .offset = sizeof(struct ipv4_hdr) -
302 offsetof(struct ipv4_hdr, next_proto_id) +
307 #define IPV6_ADDR_LEN 16
308 #define IPV6_ADDR_U16 (IPV6_ADDR_LEN / sizeof(uint16_t))
309 #define IPV6_ADDR_U32 (IPV6_ADDR_LEN / sizeof(uint32_t))
326 struct rte_acl_field_def ipv6_defs[NUM_FIELDS_IPV6] = {
328 .type = RTE_ACL_FIELD_TYPE_BITMASK,
329 .size = sizeof(uint8_t),
330 .field_index = PROTO_FIELD_IPV6,
331 .input_index = PROTO_FIELD_IPV6,
335 .type = RTE_ACL_FIELD_TYPE_MASK,
336 .size = sizeof(uint32_t),
337 .field_index = SRC1_FIELD_IPV6,
338 .input_index = SRC1_FIELD_IPV6,
339 .offset = offsetof(struct ipv6_hdr, src_addr) -
340 offsetof(struct ipv6_hdr, proto),
343 .type = RTE_ACL_FIELD_TYPE_MASK,
344 .size = sizeof(uint32_t),
345 .field_index = SRC2_FIELD_IPV6,
346 .input_index = SRC2_FIELD_IPV6,
347 .offset = offsetof(struct ipv6_hdr, src_addr) -
348 offsetof(struct ipv6_hdr, proto) + sizeof(uint32_t),
351 .type = RTE_ACL_FIELD_TYPE_MASK,
352 .size = sizeof(uint32_t),
353 .field_index = SRC3_FIELD_IPV6,
354 .input_index = SRC3_FIELD_IPV6,
355 .offset = offsetof(struct ipv6_hdr, src_addr) -
356 offsetof(struct ipv6_hdr, proto) + 2 * sizeof(uint32_t),
359 .type = RTE_ACL_FIELD_TYPE_MASK,
360 .size = sizeof(uint32_t),
361 .field_index = SRC4_FIELD_IPV6,
362 .input_index = SRC4_FIELD_IPV6,
363 .offset = offsetof(struct ipv6_hdr, src_addr) -
364 offsetof(struct ipv6_hdr, proto) + 3 * sizeof(uint32_t),
367 .type = RTE_ACL_FIELD_TYPE_MASK,
368 .size = sizeof(uint32_t),
369 .field_index = DST1_FIELD_IPV6,
370 .input_index = DST1_FIELD_IPV6,
371 .offset = offsetof(struct ipv6_hdr, dst_addr)
372 - offsetof(struct ipv6_hdr, proto),
375 .type = RTE_ACL_FIELD_TYPE_MASK,
376 .size = sizeof(uint32_t),
377 .field_index = DST2_FIELD_IPV6,
378 .input_index = DST2_FIELD_IPV6,
379 .offset = offsetof(struct ipv6_hdr, dst_addr) -
380 offsetof(struct ipv6_hdr, proto) + sizeof(uint32_t),
383 .type = RTE_ACL_FIELD_TYPE_MASK,
384 .size = sizeof(uint32_t),
385 .field_index = DST3_FIELD_IPV6,
386 .input_index = DST3_FIELD_IPV6,
387 .offset = offsetof(struct ipv6_hdr, dst_addr) -
388 offsetof(struct ipv6_hdr, proto) + 2 * sizeof(uint32_t),
391 .type = RTE_ACL_FIELD_TYPE_MASK,
392 .size = sizeof(uint32_t),
393 .field_index = DST4_FIELD_IPV6,
394 .input_index = DST4_FIELD_IPV6,
395 .offset = offsetof(struct ipv6_hdr, dst_addr) -
396 offsetof(struct ipv6_hdr, proto) + 3 * sizeof(uint32_t),
399 .type = RTE_ACL_FIELD_TYPE_RANGE,
400 .size = sizeof(uint16_t),
401 .field_index = SRCP_FIELD_IPV6,
402 .input_index = SRCP_FIELD_IPV6,
403 .offset = sizeof(struct ipv6_hdr) -
404 offsetof(struct ipv6_hdr, proto),
407 .type = RTE_ACL_FIELD_TYPE_RANGE,
408 .size = sizeof(uint16_t),
409 .field_index = DSTP_FIELD_IPV6,
410 .input_index = SRCP_FIELD_IPV6,
411 .offset = sizeof(struct ipv6_hdr) -
412 offsetof(struct ipv6_hdr, proto) + sizeof(uint16_t),
421 CB_FLD_SRC_PORT_HIGH,
424 CB_FLD_DST_PORT_HIGH,
430 RTE_ACL_RULE_DEF(acl4_rule, RTE_DIM(ipv4_defs));
431 RTE_ACL_RULE_DEF(acl6_rule, RTE_DIM(ipv6_defs));
433 struct acl_search_t {
434 const uint8_t *data_ipv4[MAX_PKT_BURST];
435 struct rte_mbuf *m_ipv4[MAX_PKT_BURST];
436 uint32_t res_ipv4[MAX_PKT_BURST];
439 const uint8_t *data_ipv6[MAX_PKT_BURST];
440 struct rte_mbuf *m_ipv6[MAX_PKT_BURST];
441 uint32_t res_ipv6[MAX_PKT_BURST];
446 char mapped[NB_SOCKETS];
447 struct rte_acl_ctx *acx_ipv4[NB_SOCKETS];
448 struct rte_acl_ctx *acx_ipv6[NB_SOCKETS];
449 #ifdef L3FWDACL_DEBUG
450 struct acl4_rule *rule_ipv4;
451 struct acl6_rule *rule_ipv6;
456 const char *rule_ipv4_name;
457 const char *rule_ipv6_name;
461 const char cb_port_delim[] = ":";
464 print_one_ipv4_rule(struct acl4_rule *rule, int extra)
466 unsigned char a, b, c, d;
468 uint32_t_to_char(rule->field[SRC_FIELD_IPV4].value.u32,
470 printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
471 rule->field[SRC_FIELD_IPV4].mask_range.u32);
472 uint32_t_to_char(rule->field[DST_FIELD_IPV4].value.u32,
474 printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
475 rule->field[DST_FIELD_IPV4].mask_range.u32);
476 printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
477 rule->field[SRCP_FIELD_IPV4].value.u16,
478 rule->field[SRCP_FIELD_IPV4].mask_range.u16,
479 rule->field[DSTP_FIELD_IPV4].value.u16,
480 rule->field[DSTP_FIELD_IPV4].mask_range.u16,
481 rule->field[PROTO_FIELD_IPV4].value.u8,
482 rule->field[PROTO_FIELD_IPV4].mask_range.u8);
484 printf("0x%x-0x%x-0x%x ",
485 rule->data.category_mask,
487 rule->data.userdata);
491 print_one_ipv6_rule(struct acl6_rule *rule, int extra)
493 unsigned char a, b, c, d;
495 uint32_t_to_char(rule->field[SRC1_FIELD_IPV6].value.u32,
497 printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
498 uint32_t_to_char(rule->field[SRC2_FIELD_IPV6].value.u32,
500 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
501 uint32_t_to_char(rule->field[SRC3_FIELD_IPV6].value.u32,
503 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
504 uint32_t_to_char(rule->field[SRC4_FIELD_IPV6].value.u32,
506 printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
507 rule->field[SRC1_FIELD_IPV6].mask_range.u32
508 + rule->field[SRC2_FIELD_IPV6].mask_range.u32
509 + rule->field[SRC3_FIELD_IPV6].mask_range.u32
510 + rule->field[SRC4_FIELD_IPV6].mask_range.u32);
512 uint32_t_to_char(rule->field[DST1_FIELD_IPV6].value.u32,
514 printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
515 uint32_t_to_char(rule->field[DST2_FIELD_IPV6].value.u32,
517 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
518 uint32_t_to_char(rule->field[DST3_FIELD_IPV6].value.u32,
520 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
521 uint32_t_to_char(rule->field[DST4_FIELD_IPV6].value.u32,
523 printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
524 rule->field[DST1_FIELD_IPV6].mask_range.u32
525 + rule->field[DST2_FIELD_IPV6].mask_range.u32
526 + rule->field[DST3_FIELD_IPV6].mask_range.u32
527 + rule->field[DST4_FIELD_IPV6].mask_range.u32);
529 printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
530 rule->field[SRCP_FIELD_IPV6].value.u16,
531 rule->field[SRCP_FIELD_IPV6].mask_range.u16,
532 rule->field[DSTP_FIELD_IPV6].value.u16,
533 rule->field[DSTP_FIELD_IPV6].mask_range.u16,
534 rule->field[PROTO_FIELD_IPV6].value.u8,
535 rule->field[PROTO_FIELD_IPV6].mask_range.u8);
537 printf("0x%x-0x%x-0x%x ",
538 rule->data.category_mask,
540 rule->data.userdata);
543 /* Bypass comment and empty lines */
545 is_bypass_line(char *buff)
550 if (buff[0] == COMMENT_LEAD_CHAR)
553 while (buff[i] != '\0') {
554 if (!isspace(buff[i]))
561 #ifdef L3FWDACL_DEBUG
563 dump_acl4_rule(struct rte_mbuf *m, uint32_t sig)
565 uint32_t offset = sig & ~ACL_DENY_SIGNATURE;
566 unsigned char a, b, c, d;
567 struct ipv4_hdr *ipv4_hdr = rte_pktmbuf_mtod_offset(m,
569 sizeof(struct ether_hdr));
571 uint32_t_to_char(rte_bswap32(ipv4_hdr->src_addr), &a, &b, &c, &d);
572 printf("Packet Src:%hhu.%hhu.%hhu.%hhu ", a, b, c, d);
573 uint32_t_to_char(rte_bswap32(ipv4_hdr->dst_addr), &a, &b, &c, &d);
574 printf("Dst:%hhu.%hhu.%hhu.%hhu ", a, b, c, d);
576 printf("Src port:%hu,Dst port:%hu ",
577 rte_bswap16(*(uint16_t *)(ipv4_hdr + 1)),
578 rte_bswap16(*((uint16_t *)(ipv4_hdr + 1) + 1)));
579 printf("hit ACL %d - ", offset);
581 print_one_ipv4_rule(acl_config.rule_ipv4 + offset, 1);
587 dump_acl6_rule(struct rte_mbuf *m, uint32_t sig)
590 uint32_t offset = sig & ~ACL_DENY_SIGNATURE;
591 struct ipv6_hdr *ipv6_hdr = rte_pktmbuf_mtod_offset(m,
593 sizeof(struct ether_hdr));
595 printf("Packet Src");
596 for (i = 0; i < RTE_DIM(ipv6_hdr->src_addr); i += sizeof(uint16_t))
598 ipv6_hdr->src_addr[i], ipv6_hdr->src_addr[i + 1]);
601 for (i = 0; i < RTE_DIM(ipv6_hdr->dst_addr); i += sizeof(uint16_t))
603 ipv6_hdr->dst_addr[i], ipv6_hdr->dst_addr[i + 1]);
605 printf("\nSrc port:%hu,Dst port:%hu ",
606 rte_bswap16(*(uint16_t *)(ipv6_hdr + 1)),
607 rte_bswap16(*((uint16_t *)(ipv6_hdr + 1) + 1)));
608 printf("hit ACL %d - ", offset);
610 print_one_ipv6_rule(acl_config.rule_ipv6 + offset, 1);
614 #endif /* L3FWDACL_DEBUG */
617 dump_ipv4_rules(struct acl4_rule *rule, int num, int extra)
621 for (i = 0; i < num; i++, rule++) {
622 printf("\t%d:", i + 1);
623 print_one_ipv4_rule(rule, extra);
629 dump_ipv6_rules(struct acl6_rule *rule, int num, int extra)
633 for (i = 0; i < num; i++, rule++) {
634 printf("\t%d:", i + 1);
635 print_one_ipv6_rule(rule, extra);
640 #ifdef DO_RFC_1812_CHECKS
642 prepare_one_packet(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
645 struct ipv4_hdr *ipv4_hdr;
646 struct rte_mbuf *pkt = pkts_in[index];
649 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
651 int type = pkt->ol_flags & (PKT_RX_IPV4_HDR | PKT_RX_IPV6_HDR);
653 if (type == PKT_RX_IPV4_HDR) {
655 ipv4_hdr = rte_pktmbuf_mtod_offset(pkt, struct ipv4_hdr *,
656 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 (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
676 } else if (type == PKT_RX_IPV6_HDR) {
678 /* Fill acl structure */
679 acl->data_ipv6[acl->num_ipv6] = MBUF_IPV6_2PROTO(pkt);
680 acl->m_ipv6[(acl->num_ipv6)++] = pkt;
683 /* Unknown type, drop the packet */
684 rte_pktmbuf_free(pkt);
690 prepare_one_packet(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
693 struct rte_mbuf *pkt = pkts_in[index];
696 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
698 int type = pkt->ol_flags & (PKT_RX_IPV4_HDR | PKT_RX_IPV6_HDR);
700 if (type == PKT_RX_IPV4_HDR) {
702 /* Fill acl structure */
703 acl->data_ipv4[acl->num_ipv4] = MBUF_IPV4_2PROTO(pkt);
704 acl->m_ipv4[(acl->num_ipv4)++] = pkt;
707 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
709 } else if (type == PKT_RX_IPV6_HDR) {
711 /* Fill acl structure */
712 acl->data_ipv6[acl->num_ipv6] = MBUF_IPV6_2PROTO(pkt);
713 acl->m_ipv6[(acl->num_ipv6)++] = pkt;
715 /* Unknown type, drop the packet */
716 rte_pktmbuf_free(pkt);
719 #endif /* DO_RFC_1812_CHECKS */
722 prepare_acl_parameter(struct rte_mbuf **pkts_in, struct acl_search_t *acl,
730 /* Prefetch first packets */
731 for (i = 0; i < PREFETCH_OFFSET && i < nb_rx; i++) {
732 rte_prefetch0(rte_pktmbuf_mtod(
733 pkts_in[i], void *));
736 for (i = 0; i < (nb_rx - PREFETCH_OFFSET); i++) {
737 rte_prefetch0(rte_pktmbuf_mtod(pkts_in[
738 i + PREFETCH_OFFSET], void *));
739 prepare_one_packet(pkts_in, acl, i);
742 /* Process left packets */
743 for (; i < nb_rx; i++)
744 prepare_one_packet(pkts_in, acl, i);
748 send_one_packet(struct rte_mbuf *m, uint32_t res)
750 if (likely((res & ACL_DENY_SIGNATURE) == 0 && res != 0)) {
751 /* forward packets */
752 send_single_packet(m,
753 (uint8_t)(res - FWD_PORT_SHIFT));
755 /* in the ACL list, drop it */
756 #ifdef L3FWDACL_DEBUG
757 if ((res & ACL_DENY_SIGNATURE) != 0) {
759 if (RTE_ETH_IS_IPV4_HDR(m->packet_type))
760 dump_acl4_rule(m, res);
761 else if (RTE_ETH_IS_IPV6_HDR(m->packet_type))
762 dump_acl6_rule(m, res);
764 if (m->ol_flags & PKT_RX_IPV4_HDR)
765 dump_acl4_rule(m, res);
767 dump_acl6_rule(m, res);
768 #endif /* RTE_NEXT_ABI */
778 send_packets(struct rte_mbuf **m, uint32_t *res, int num)
782 /* Prefetch first packets */
783 for (i = 0; i < PREFETCH_OFFSET && i < num; i++) {
784 rte_prefetch0(rte_pktmbuf_mtod(
788 for (i = 0; i < (num - PREFETCH_OFFSET); i++) {
789 rte_prefetch0(rte_pktmbuf_mtod(m[
790 i + PREFETCH_OFFSET], void *));
791 send_one_packet(m[i], res[i]);
794 /* Process left packets */
796 send_one_packet(m[i], res[i]);
800 * Parses IPV6 address, exepcts the following format:
801 * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX (where X - is a hexedecimal digit).
804 parse_ipv6_addr(const char *in, const char **end, uint32_t v[IPV6_ADDR_U32],
807 uint32_t addr[IPV6_ADDR_U16];
809 GET_CB_FIELD(in, addr[0], 16, UINT16_MAX, ':');
810 GET_CB_FIELD(in, addr[1], 16, UINT16_MAX, ':');
811 GET_CB_FIELD(in, addr[2], 16, UINT16_MAX, ':');
812 GET_CB_FIELD(in, addr[3], 16, UINT16_MAX, ':');
813 GET_CB_FIELD(in, addr[4], 16, UINT16_MAX, ':');
814 GET_CB_FIELD(in, addr[5], 16, UINT16_MAX, ':');
815 GET_CB_FIELD(in, addr[6], 16, UINT16_MAX, ':');
816 GET_CB_FIELD(in, addr[7], 16, UINT16_MAX, dlm);
820 v[0] = (addr[0] << 16) + addr[1];
821 v[1] = (addr[2] << 16) + addr[3];
822 v[2] = (addr[4] << 16) + addr[5];
823 v[3] = (addr[6] << 16) + addr[7];
829 parse_ipv6_net(const char *in, struct rte_acl_field field[4])
834 const uint32_t nbu32 = sizeof(uint32_t) * CHAR_BIT;
837 rc = parse_ipv6_addr(in, &mp, v, '/');
842 GET_CB_FIELD(mp, m, 0, CHAR_BIT * sizeof(v), 0);
844 /* put all together. */
845 for (i = 0; i != RTE_DIM(v); i++) {
846 if (m >= (i + 1) * nbu32)
847 field[i].mask_range.u32 = nbu32;
849 field[i].mask_range.u32 = m > (i * nbu32) ?
852 field[i].value.u32 = v[i];
859 parse_cb_ipv6_rule(char *str, struct rte_acl_rule *v, int has_userdata)
862 char *s, *sp, *in[CB_FLD_NUM];
863 static const char *dlm = " \t\n";
864 int dim = has_userdata ? CB_FLD_NUM : CB_FLD_USERDATA;
867 for (i = 0; i != dim; i++, s = NULL) {
868 in[i] = strtok_r(s, dlm, &sp);
873 rc = parse_ipv6_net(in[CB_FLD_SRC_ADDR], v->field + SRC1_FIELD_IPV6);
875 acl_log("failed to read source address/mask: %s\n",
876 in[CB_FLD_SRC_ADDR]);
880 rc = parse_ipv6_net(in[CB_FLD_DST_ADDR], v->field + DST1_FIELD_IPV6);
882 acl_log("failed to read destination address/mask: %s\n",
883 in[CB_FLD_DST_ADDR]);
888 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
889 v->field[SRCP_FIELD_IPV6].value.u16,
891 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
892 v->field[SRCP_FIELD_IPV6].mask_range.u16,
895 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
896 sizeof(cb_port_delim)) != 0)
899 /* destination port. */
900 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
901 v->field[DSTP_FIELD_IPV6].value.u16,
903 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
904 v->field[DSTP_FIELD_IPV6].mask_range.u16,
907 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
908 sizeof(cb_port_delim)) != 0)
911 if (v->field[SRCP_FIELD_IPV6].mask_range.u16
912 < v->field[SRCP_FIELD_IPV6].value.u16
913 || v->field[DSTP_FIELD_IPV6].mask_range.u16
914 < v->field[DSTP_FIELD_IPV6].value.u16)
917 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].value.u8,
919 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV6].mask_range.u8,
923 GET_CB_FIELD(in[CB_FLD_USERDATA], v->data.userdata,
930 * Parse ClassBench rules file.
932 * '@'<src_ipv4_addr>'/'<masklen> <space> \
933 * <dst_ipv4_addr>'/'<masklen> <space> \
934 * <src_port_low> <space> ":" <src_port_high> <space> \
935 * <dst_port_low> <space> ":" <dst_port_high> <space> \
939 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
941 uint8_t a, b, c, d, m;
943 GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
944 GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
945 GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
946 GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
947 GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
949 addr[0] = IPv4(a, b, c, d);
956 parse_cb_ipv4vlan_rule(char *str, struct rte_acl_rule *v, int has_userdata)
959 char *s, *sp, *in[CB_FLD_NUM];
960 static const char *dlm = " \t\n";
961 int dim = has_userdata ? CB_FLD_NUM : CB_FLD_USERDATA;
964 for (i = 0; i != dim; i++, s = NULL) {
965 in[i] = strtok_r(s, dlm, &sp);
970 rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
971 &v->field[SRC_FIELD_IPV4].value.u32,
972 &v->field[SRC_FIELD_IPV4].mask_range.u32);
974 acl_log("failed to read source address/mask: %s\n",
975 in[CB_FLD_SRC_ADDR]);
979 rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
980 &v->field[DST_FIELD_IPV4].value.u32,
981 &v->field[DST_FIELD_IPV4].mask_range.u32);
983 acl_log("failed to read destination address/mask: %s\n",
984 in[CB_FLD_DST_ADDR]);
988 GET_CB_FIELD(in[CB_FLD_SRC_PORT_LOW],
989 v->field[SRCP_FIELD_IPV4].value.u16,
991 GET_CB_FIELD(in[CB_FLD_SRC_PORT_HIGH],
992 v->field[SRCP_FIELD_IPV4].mask_range.u16,
995 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
996 sizeof(cb_port_delim)) != 0)
999 GET_CB_FIELD(in[CB_FLD_DST_PORT_LOW],
1000 v->field[DSTP_FIELD_IPV4].value.u16,
1002 GET_CB_FIELD(in[CB_FLD_DST_PORT_HIGH],
1003 v->field[DSTP_FIELD_IPV4].mask_range.u16,
1006 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
1007 sizeof(cb_port_delim)) != 0)
1010 if (v->field[SRCP_FIELD_IPV4].mask_range.u16
1011 < v->field[SRCP_FIELD_IPV4].value.u16
1012 || v->field[DSTP_FIELD_IPV4].mask_range.u16
1013 < v->field[DSTP_FIELD_IPV4].value.u16)
1016 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].value.u8,
1018 GET_CB_FIELD(in[CB_FLD_PROTO], v->field[PROTO_FIELD_IPV4].mask_range.u8,
1022 GET_CB_FIELD(in[CB_FLD_USERDATA], v->data.userdata, 0,
1029 add_rules(const char *rule_path,
1030 struct rte_acl_rule **proute_base,
1031 unsigned int *proute_num,
1032 struct rte_acl_rule **pacl_base,
1033 unsigned int *pacl_num, uint32_t rule_size,
1034 int (*parser)(char *, struct rte_acl_rule*, int))
1036 uint8_t *acl_rules, *route_rules;
1037 struct rte_acl_rule *next;
1038 unsigned int acl_num = 0, route_num = 0, total_num = 0;
1039 unsigned int acl_cnt = 0, route_cnt = 0;
1040 char buff[LINE_MAX];
1041 FILE *fh = fopen(rule_path, "rb");
1045 rte_exit(EXIT_FAILURE, "%s: Open %s failed\n", __func__,
1048 while ((fgets(buff, LINE_MAX, fh) != NULL)) {
1049 if (buff[0] == ROUTE_LEAD_CHAR)
1051 else if (buff[0] == ACL_LEAD_CHAR)
1056 rte_exit(EXIT_FAILURE, "Not find any route entries in %s!\n",
1059 fseek(fh, 0, SEEK_SET);
1061 acl_rules = calloc(acl_num, rule_size);
1063 if (NULL == acl_rules)
1064 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1067 route_rules = calloc(route_num, rule_size);
1069 if (NULL == route_rules)
1070 rte_exit(EXIT_FAILURE, "%s: failed to malloc memory\n",
1074 while (fgets(buff, LINE_MAX, fh) != NULL) {
1077 if (is_bypass_line(buff))
1083 if (s == ROUTE_LEAD_CHAR)
1084 next = (struct rte_acl_rule *)(route_rules +
1085 route_cnt * rule_size);
1088 else if (s == ACL_LEAD_CHAR)
1089 next = (struct rte_acl_rule *)(acl_rules +
1090 acl_cnt * rule_size);
1094 rte_exit(EXIT_FAILURE,
1095 "%s Line %u: should start with leading "
1097 rule_path, i, ROUTE_LEAD_CHAR, ACL_LEAD_CHAR);
1099 if (parser(buff + 1, next, s == ROUTE_LEAD_CHAR) != 0)
1100 rte_exit(EXIT_FAILURE,
1101 "%s Line %u: parse rules error\n",
1104 if (s == ROUTE_LEAD_CHAR) {
1105 /* Check the forwarding port number */
1106 if ((enabled_port_mask & (1 << next->data.userdata)) ==
1108 rte_exit(EXIT_FAILURE,
1109 "%s Line %u: fwd number illegal:%u\n",
1110 rule_path, i, next->data.userdata);
1111 next->data.userdata += FWD_PORT_SHIFT;
1114 next->data.userdata = ACL_DENY_SIGNATURE + acl_cnt;
1118 next->data.priority = RTE_ACL_MAX_PRIORITY - total_num;
1119 next->data.category_mask = -1;
1125 *pacl_base = (struct rte_acl_rule *)acl_rules;
1126 *pacl_num = acl_num;
1127 *proute_base = (struct rte_acl_rule *)route_rules;
1128 *proute_num = route_cnt;
1134 dump_acl_config(void)
1136 printf("ACL option are:\n");
1137 printf(OPTION_RULE_IPV4": %s\n", parm_config.rule_ipv4_name);
1138 printf(OPTION_RULE_IPV6": %s\n", parm_config.rule_ipv6_name);
1139 printf(OPTION_SCALAR": %d\n", parm_config.scalar);
1143 check_acl_config(void)
1145 if (parm_config.rule_ipv4_name == NULL) {
1146 acl_log("ACL IPv4 rule file not specified\n");
1148 } else if (parm_config.rule_ipv6_name == NULL) {
1149 acl_log("ACL IPv6 rule file not specified\n");
1156 static struct rte_acl_ctx*
1157 setup_acl(struct rte_acl_rule *route_base,
1158 struct rte_acl_rule *acl_base, unsigned int route_num,
1159 unsigned int acl_num, int ipv6, int socketid)
1161 char name[PATH_MAX];
1162 struct rte_acl_param acl_param;
1163 struct rte_acl_config acl_build_param;
1164 struct rte_acl_ctx *context;
1165 int dim = ipv6 ? RTE_DIM(ipv6_defs) : RTE_DIM(ipv4_defs);
1167 /* Create ACL contexts */
1168 snprintf(name, sizeof(name), "%s%d",
1169 ipv6 ? L3FWD_ACL_IPV6_NAME : L3FWD_ACL_IPV4_NAME,
1172 acl_param.name = name;
1173 acl_param.socket_id = socketid;
1174 acl_param.rule_size = RTE_ACL_RULE_SZ(dim);
1175 acl_param.max_rule_num = MAX_ACL_RULE_NUM;
1177 if ((context = rte_acl_create(&acl_param)) == NULL)
1178 rte_exit(EXIT_FAILURE, "Failed to create ACL context\n");
1180 if (parm_config.scalar && rte_acl_set_ctx_classify(context,
1181 RTE_ACL_CLASSIFY_SCALAR) != 0)
1182 rte_exit(EXIT_FAILURE,
1183 "Failed to setup classify method for ACL context\n");
1185 if (rte_acl_add_rules(context, route_base, route_num) < 0)
1186 rte_exit(EXIT_FAILURE, "add rules failed\n");
1188 if (rte_acl_add_rules(context, acl_base, acl_num) < 0)
1189 rte_exit(EXIT_FAILURE, "add rules failed\n");
1191 /* Perform builds */
1192 memset(&acl_build_param, 0, sizeof(acl_build_param));
1194 acl_build_param.num_categories = DEFAULT_MAX_CATEGORIES;
1195 acl_build_param.num_fields = dim;
1196 memcpy(&acl_build_param.defs, ipv6 ? ipv6_defs : ipv4_defs,
1197 ipv6 ? sizeof(ipv6_defs) : sizeof(ipv4_defs));
1199 if (rte_acl_build(context, &acl_build_param) != 0)
1200 rte_exit(EXIT_FAILURE, "Failed to build ACL trie\n");
1202 rte_acl_dump(context);
1213 struct rte_acl_rule *acl_base_ipv4, *route_base_ipv4,
1214 *acl_base_ipv6, *route_base_ipv6;
1215 unsigned int acl_num_ipv4 = 0, route_num_ipv4 = 0,
1216 acl_num_ipv6 = 0, route_num_ipv6 = 0;
1218 if (check_acl_config() != 0)
1219 rte_exit(EXIT_FAILURE, "Failed to get valid ACL options\n");
1223 /* Load rules from the input file */
1224 if (add_rules(parm_config.rule_ipv4_name, &route_base_ipv4,
1225 &route_num_ipv4, &acl_base_ipv4, &acl_num_ipv4,
1226 sizeof(struct acl4_rule), &parse_cb_ipv4vlan_rule) < 0)
1227 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1229 acl_log("IPv4 Route entries %u:\n", route_num_ipv4);
1230 dump_ipv4_rules((struct acl4_rule *)route_base_ipv4, route_num_ipv4, 1);
1232 acl_log("IPv4 ACL entries %u:\n", acl_num_ipv4);
1233 dump_ipv4_rules((struct acl4_rule *)acl_base_ipv4, acl_num_ipv4, 1);
1235 if (add_rules(parm_config.rule_ipv6_name, &route_base_ipv6,
1237 &acl_base_ipv6, &acl_num_ipv6,
1238 sizeof(struct acl6_rule), &parse_cb_ipv6_rule) < 0)
1239 rte_exit(EXIT_FAILURE, "Failed to add rules\n");
1241 acl_log("IPv6 Route entries %u:\n", route_num_ipv6);
1242 dump_ipv6_rules((struct acl6_rule *)route_base_ipv6, route_num_ipv6, 1);
1244 acl_log("IPv6 ACL entries %u:\n", acl_num_ipv6);
1245 dump_ipv6_rules((struct acl6_rule *)acl_base_ipv6, acl_num_ipv6, 1);
1247 memset(&acl_config, 0, sizeof(acl_config));
1249 /* Check sockets a context should be created on */
1251 acl_config.mapped[0] = 1;
1253 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1254 if (rte_lcore_is_enabled(lcore_id) == 0)
1257 socketid = rte_lcore_to_socket_id(lcore_id);
1258 if (socketid >= NB_SOCKETS) {
1259 acl_log("Socket %d of lcore %u is out "
1261 socketid, lcore_id, NB_SOCKETS);
1262 free(route_base_ipv4);
1263 free(route_base_ipv6);
1264 free(acl_base_ipv4);
1265 free(acl_base_ipv6);
1269 acl_config.mapped[socketid] = 1;
1273 for (i = 0; i < NB_SOCKETS; i++) {
1274 if (acl_config.mapped[i]) {
1275 acl_config.acx_ipv4[i] = setup_acl(route_base_ipv4,
1276 acl_base_ipv4, route_num_ipv4, acl_num_ipv4,
1279 acl_config.acx_ipv6[i] = setup_acl(route_base_ipv6,
1280 acl_base_ipv6, route_num_ipv6, acl_num_ipv6,
1285 free(route_base_ipv4);
1286 free(route_base_ipv6);
1288 #ifdef L3FWDACL_DEBUG
1289 acl_config.rule_ipv4 = (struct acl4_rule *)acl_base_ipv4;
1290 acl_config.rule_ipv6 = (struct acl6_rule *)acl_base_ipv6;
1292 free(acl_base_ipv4);
1293 free(acl_base_ipv6);
1299 /***********************end of ACL part******************************/
1302 uint16_t n_rx_queue;
1303 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
1304 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
1305 struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
1306 } __rte_cache_aligned;
1308 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
1310 /* Send burst of packets on an output interface */
1312 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
1314 struct rte_mbuf **m_table;
1318 queueid = qconf->tx_queue_id[port];
1319 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
1321 ret = rte_eth_tx_burst(port, queueid, m_table, n);
1322 if (unlikely(ret < n)) {
1324 rte_pktmbuf_free(m_table[ret]);
1325 } while (++ret < n);
1331 /* Enqueue a single packet, and send burst if queue is filled */
1333 send_single_packet(struct rte_mbuf *m, uint8_t port)
1337 struct lcore_conf *qconf;
1339 lcore_id = rte_lcore_id();
1341 qconf = &lcore_conf[lcore_id];
1342 len = qconf->tx_mbufs[port].len;
1343 qconf->tx_mbufs[port].m_table[len] = m;
1346 /* enough pkts to be sent */
1347 if (unlikely(len == MAX_PKT_BURST)) {
1348 send_burst(qconf, MAX_PKT_BURST, port);
1352 qconf->tx_mbufs[port].len = len;
1356 #ifdef DO_RFC_1812_CHECKS
1358 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
1360 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
1362 * 1. The packet length reported by the Link Layer must be large
1363 * enough to hold the minimum length legal IP datagram (20 bytes).
1365 if (link_len < sizeof(struct ipv4_hdr))
1368 /* 2. The IP checksum must be correct. */
1369 /* this is checked in H/W */
1372 * 3. The IP version number must be 4. If the version number is not 4
1373 * then the packet may be another version of IP, such as IPng or
1376 if (((pkt->version_ihl) >> 4) != 4)
1379 * 4. The IP header length field must be large enough to hold the
1380 * minimum length legal IP datagram (20 bytes = 5 words).
1382 if ((pkt->version_ihl & 0xf) < 5)
1386 * 5. The IP total length field must be large enough to hold the IP
1387 * datagram header, whose length is specified in the IP header length
1390 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
1397 /* main processing loop */
1399 main_loop(__attribute__((unused)) void *dummy)
1401 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1403 uint64_t prev_tsc, diff_tsc, cur_tsc;
1405 uint8_t portid, queueid;
1406 struct lcore_conf *qconf;
1408 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
1409 / US_PER_S * BURST_TX_DRAIN_US;
1412 lcore_id = rte_lcore_id();
1413 qconf = &lcore_conf[lcore_id];
1414 socketid = rte_lcore_to_socket_id(lcore_id);
1416 if (qconf->n_rx_queue == 0) {
1417 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
1421 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
1423 for (i = 0; i < qconf->n_rx_queue; i++) {
1425 portid = qconf->rx_queue_list[i].port_id;
1426 queueid = qconf->rx_queue_list[i].queue_id;
1427 RTE_LOG(INFO, L3FWD,
1428 " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
1429 lcore_id, portid, queueid);
1434 cur_tsc = rte_rdtsc();
1437 * TX burst queue drain
1439 diff_tsc = cur_tsc - prev_tsc;
1440 if (unlikely(diff_tsc > drain_tsc)) {
1443 * This could be optimized (use queueid instead of
1444 * portid), but it is not called so often
1446 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
1447 if (qconf->tx_mbufs[portid].len == 0)
1449 send_burst(&lcore_conf[lcore_id],
1450 qconf->tx_mbufs[portid].len,
1452 qconf->tx_mbufs[portid].len = 0;
1459 * Read packet from RX queues
1461 for (i = 0; i < qconf->n_rx_queue; ++i) {
1463 portid = qconf->rx_queue_list[i].port_id;
1464 queueid = qconf->rx_queue_list[i].queue_id;
1465 nb_rx = rte_eth_rx_burst(portid, queueid,
1466 pkts_burst, MAX_PKT_BURST);
1469 struct acl_search_t acl_search;
1471 prepare_acl_parameter(pkts_burst, &acl_search,
1474 if (acl_search.num_ipv4) {
1476 acl_config.acx_ipv4[socketid],
1477 acl_search.data_ipv4,
1478 acl_search.res_ipv4,
1479 acl_search.num_ipv4,
1480 DEFAULT_MAX_CATEGORIES);
1482 send_packets(acl_search.m_ipv4,
1483 acl_search.res_ipv4,
1484 acl_search.num_ipv4);
1487 if (acl_search.num_ipv6) {
1489 acl_config.acx_ipv6[socketid],
1490 acl_search.data_ipv6,
1491 acl_search.res_ipv6,
1492 acl_search.num_ipv6,
1493 DEFAULT_MAX_CATEGORIES);
1495 send_packets(acl_search.m_ipv6,
1496 acl_search.res_ipv6,
1497 acl_search.num_ipv6);
1505 check_lcore_params(void)
1507 uint8_t queue, lcore;
1511 for (i = 0; i < nb_lcore_params; ++i) {
1512 queue = lcore_params[i].queue_id;
1513 if (queue >= MAX_RX_QUEUE_PER_PORT) {
1514 printf("invalid queue number: %hhu\n", queue);
1517 lcore = lcore_params[i].lcore_id;
1518 if (!rte_lcore_is_enabled(lcore)) {
1519 printf("error: lcore %hhu is not enabled in "
1520 "lcore mask\n", lcore);
1523 socketid = rte_lcore_to_socket_id(lcore);
1524 if (socketid != 0 && numa_on == 0) {
1525 printf("warning: lcore %hhu is on socket %d "
1534 check_port_config(const unsigned nb_ports)
1539 for (i = 0; i < nb_lcore_params; ++i) {
1540 portid = lcore_params[i].port_id;
1542 if ((enabled_port_mask & (1 << portid)) == 0) {
1543 printf("port %u is not enabled in port mask\n", portid);
1546 if (portid >= nb_ports) {
1547 printf("port %u is not present on the board\n", portid);
1555 get_port_n_rx_queues(const uint8_t port)
1560 for (i = 0; i < nb_lcore_params; ++i) {
1561 if (lcore_params[i].port_id == port &&
1562 lcore_params[i].queue_id > queue)
1563 queue = lcore_params[i].queue_id;
1565 return (uint8_t)(++queue);
1569 init_lcore_rx_queues(void)
1571 uint16_t i, nb_rx_queue;
1574 for (i = 0; i < nb_lcore_params; ++i) {
1575 lcore = lcore_params[i].lcore_id;
1576 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1577 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1578 printf("error: too many queues (%u) for lcore: %u\n",
1579 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
1582 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1583 lcore_params[i].port_id;
1584 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1585 lcore_params[i].queue_id;
1586 lcore_conf[lcore].n_rx_queue++;
1594 print_usage(const char *prgname)
1596 printf("%s [EAL options] -- -p PORTMASK -P"
1597 "--"OPTION_RULE_IPV4"=FILE"
1598 "--"OPTION_RULE_IPV6"=FILE"
1599 " [--"OPTION_CONFIG" (port,queue,lcore)[,(port,queue,lcore]]"
1600 " [--"OPTION_ENBJMO" [--max-pkt-len PKTLEN]]\n"
1601 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1602 " -P : enable promiscuous mode\n"
1603 " --"OPTION_CONFIG": (port,queue,lcore): "
1604 "rx queues configuration\n"
1605 " --"OPTION_NONUMA": optional, disable numa awareness\n"
1606 " --"OPTION_ENBJMO": enable jumbo frame"
1607 " which max packet len is PKTLEN in decimal (64-9600)\n"
1608 " --"OPTION_RULE_IPV4"=FILE: specify the ipv4 rules entries "
1610 "Each rule occupy one line. "
1611 "2 kinds of rules are supported. "
1612 "One is ACL entry at while line leads with character '%c', "
1613 "another is route entry at while line leads with "
1615 " --"OPTION_RULE_IPV6"=FILE: specify the ipv6 rules "
1617 " --"OPTION_SCALAR": Use scalar function to do lookup\n",
1618 prgname, ACL_LEAD_CHAR, ROUTE_LEAD_CHAR);
1622 parse_max_pkt_len(const char *pktlen)
1627 /* parse decimal string */
1628 len = strtoul(pktlen, &end, 10);
1629 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
1639 parse_portmask(const char *portmask)
1644 /* parse hexadecimal string */
1645 pm = strtoul(portmask, &end, 16);
1646 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1656 parse_config(const char *q_arg)
1659 const char *p, *p0 = q_arg;
1667 unsigned long int_fld[_NUM_FLD];
1668 char *str_fld[_NUM_FLD];
1672 nb_lcore_params = 0;
1674 while ((p = strchr(p0, '(')) != NULL) {
1676 if ((p0 = strchr(p, ')')) == NULL)
1680 if (size >= sizeof(s))
1683 snprintf(s, sizeof(s), "%.*s", size, p);
1684 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1687 for (i = 0; i < _NUM_FLD; i++) {
1689 int_fld[i] = strtoul(str_fld[i], &end, 0);
1690 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1693 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1694 printf("exceeded max number of lcore params: %hu\n",
1698 lcore_params_array[nb_lcore_params].port_id =
1699 (uint8_t)int_fld[FLD_PORT];
1700 lcore_params_array[nb_lcore_params].queue_id =
1701 (uint8_t)int_fld[FLD_QUEUE];
1702 lcore_params_array[nb_lcore_params].lcore_id =
1703 (uint8_t)int_fld[FLD_LCORE];
1706 lcore_params = lcore_params_array;
1710 /* Parse the argument given in the command line of the application */
1712 parse_args(int argc, char **argv)
1717 char *prgname = argv[0];
1718 static struct option lgopts[] = {
1719 {OPTION_CONFIG, 1, 0, 0},
1720 {OPTION_NONUMA, 0, 0, 0},
1721 {OPTION_ENBJMO, 0, 0, 0},
1722 {OPTION_RULE_IPV4, 1, 0, 0},
1723 {OPTION_RULE_IPV6, 1, 0, 0},
1724 {OPTION_SCALAR, 0, 0, 0},
1730 while ((opt = getopt_long(argc, argvopt, "p:P",
1731 lgopts, &option_index)) != EOF) {
1736 enabled_port_mask = parse_portmask(optarg);
1737 if (enabled_port_mask == 0) {
1738 printf("invalid portmask\n");
1739 print_usage(prgname);
1744 printf("Promiscuous mode selected\n");
1750 if (!strncmp(lgopts[option_index].name,
1752 sizeof(OPTION_CONFIG))) {
1753 ret = parse_config(optarg);
1755 printf("invalid config\n");
1756 print_usage(prgname);
1761 if (!strncmp(lgopts[option_index].name,
1763 sizeof(OPTION_NONUMA))) {
1764 printf("numa is disabled\n");
1768 if (!strncmp(lgopts[option_index].name,
1769 OPTION_ENBJMO, sizeof(OPTION_ENBJMO))) {
1770 struct option lenopts = {
1777 printf("jumbo frame is enabled\n");
1778 port_conf.rxmode.jumbo_frame = 1;
1781 * if no max-pkt-len set, then use the
1782 * default value ETHER_MAX_LEN
1784 if (0 == getopt_long(argc, argvopt, "",
1785 &lenopts, &option_index)) {
1786 ret = parse_max_pkt_len(optarg);
1788 (ret > MAX_JUMBO_PKT_LEN)) {
1789 printf("invalid packet "
1791 print_usage(prgname);
1794 port_conf.rxmode.max_rx_pkt_len = ret;
1796 printf("set jumbo frame max packet length "
1799 port_conf.rxmode.max_rx_pkt_len);
1802 if (!strncmp(lgopts[option_index].name,
1804 sizeof(OPTION_RULE_IPV4)))
1805 parm_config.rule_ipv4_name = optarg;
1807 if (!strncmp(lgopts[option_index].name,
1809 sizeof(OPTION_RULE_IPV6))) {
1810 parm_config.rule_ipv6_name = optarg;
1813 if (!strncmp(lgopts[option_index].name,
1814 OPTION_SCALAR, sizeof(OPTION_SCALAR)))
1815 parm_config.scalar = 1;
1821 print_usage(prgname);
1827 argv[optind-1] = prgname;
1830 optind = 0; /* reset getopt lib */
1835 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
1837 char buf[ETHER_ADDR_FMT_SIZE];
1838 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
1839 printf("%s%s", name, buf);
1843 init_mem(unsigned nb_mbuf)
1849 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1850 if (rte_lcore_is_enabled(lcore_id) == 0)
1854 socketid = rte_lcore_to_socket_id(lcore_id);
1858 if (socketid >= NB_SOCKETS) {
1859 rte_exit(EXIT_FAILURE,
1860 "Socket %d of lcore %u is out of range %d\n",
1861 socketid, lcore_id, NB_SOCKETS);
1863 if (pktmbuf_pool[socketid] == NULL) {
1864 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
1865 pktmbuf_pool[socketid] =
1866 rte_pktmbuf_pool_create(s, nb_mbuf,
1867 MEMPOOL_CACHE_SIZE, 0,
1868 RTE_MBUF_DEFAULT_BUF_SIZE,
1870 if (pktmbuf_pool[socketid] == NULL)
1871 rte_exit(EXIT_FAILURE,
1872 "Cannot init mbuf pool on socket %d\n",
1875 printf("Allocated mbuf pool on socket %d\n",
1882 /* Check the link status of all ports in up to 9s, and print them finally */
1884 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1886 #define CHECK_INTERVAL 100 /* 100ms */
1887 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1888 uint8_t portid, count, all_ports_up, print_flag = 0;
1889 struct rte_eth_link link;
1891 printf("\nChecking link status");
1893 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1895 for (portid = 0; portid < port_num; portid++) {
1896 if ((port_mask & (1 << portid)) == 0)
1898 memset(&link, 0, sizeof(link));
1899 rte_eth_link_get_nowait(portid, &link);
1900 /* print link status if flag set */
1901 if (print_flag == 1) {
1902 if (link.link_status)
1903 printf("Port %d Link Up - speed %u "
1904 "Mbps - %s\n", (uint8_t)portid,
1905 (unsigned)link.link_speed,
1906 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1907 ("full-duplex") : ("half-duplex\n"));
1909 printf("Port %d Link Down\n",
1913 /* clear all_ports_up flag if any link down */
1914 if (link.link_status == 0) {
1919 /* after finally printing all link status, get out */
1920 if (print_flag == 1)
1923 if (all_ports_up == 0) {
1926 rte_delay_ms(CHECK_INTERVAL);
1929 /* set the print_flag if all ports up or timeout */
1930 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1938 main(int argc, char **argv)
1940 struct lcore_conf *qconf;
1941 struct rte_eth_dev_info dev_info;
1942 struct rte_eth_txconf *txconf;
1947 uint32_t n_tx_queue, nb_lcores;
1948 uint8_t portid, nb_rx_queue, queue, socketid;
1951 ret = rte_eal_init(argc, argv);
1953 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1957 /* parse application arguments (after the EAL ones) */
1958 ret = parse_args(argc, argv);
1960 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
1962 if (check_lcore_params() < 0)
1963 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
1965 ret = init_lcore_rx_queues();
1967 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1969 nb_ports = rte_eth_dev_count();
1970 if (nb_ports > RTE_MAX_ETHPORTS)
1971 nb_ports = RTE_MAX_ETHPORTS;
1973 if (check_port_config(nb_ports) < 0)
1974 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
1976 /* Add ACL rules and route entries, build trie */
1977 if (app_acl_init() < 0)
1978 rte_exit(EXIT_FAILURE, "app_acl_init failed\n");
1980 nb_lcores = rte_lcore_count();
1982 /* initialize all ports */
1983 for (portid = 0; portid < nb_ports; portid++) {
1984 /* skip ports that are not enabled */
1985 if ((enabled_port_mask & (1 << portid)) == 0) {
1986 printf("\nSkipping disabled port %d\n", portid);
1991 printf("Initializing port %d ... ", portid);
1994 nb_rx_queue = get_port_n_rx_queues(portid);
1995 n_tx_queue = nb_lcores;
1996 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
1997 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
1998 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
1999 nb_rx_queue, (unsigned)n_tx_queue);
2000 ret = rte_eth_dev_configure(portid, nb_rx_queue,
2001 (uint16_t)n_tx_queue, &port_conf);
2003 rte_exit(EXIT_FAILURE,
2004 "Cannot configure device: err=%d, port=%d\n",
2007 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
2008 print_ethaddr(" Address:", &ports_eth_addr[portid]);
2012 ret = init_mem(NB_MBUF);
2014 rte_exit(EXIT_FAILURE, "init_mem failed\n");
2016 /* init one TX queue per couple (lcore,port) */
2018 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2019 if (rte_lcore_is_enabled(lcore_id) == 0)
2023 socketid = (uint8_t)
2024 rte_lcore_to_socket_id(lcore_id);
2028 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
2031 rte_eth_dev_info_get(portid, &dev_info);
2032 txconf = &dev_info.default_txconf;
2033 if (port_conf.rxmode.jumbo_frame)
2034 txconf->txq_flags = 0;
2035 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2038 rte_exit(EXIT_FAILURE,
2039 "rte_eth_tx_queue_setup: err=%d, "
2040 "port=%d\n", ret, portid);
2042 qconf = &lcore_conf[lcore_id];
2043 qconf->tx_queue_id[portid] = queueid;
2049 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2050 if (rte_lcore_is_enabled(lcore_id) == 0)
2052 qconf = &lcore_conf[lcore_id];
2053 printf("\nInitializing rx queues on lcore %u ... ", lcore_id);
2055 /* init RX queues */
2056 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
2057 portid = qconf->rx_queue_list[queue].port_id;
2058 queueid = qconf->rx_queue_list[queue].queue_id;
2061 socketid = (uint8_t)
2062 rte_lcore_to_socket_id(lcore_id);
2066 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2069 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2071 pktmbuf_pool[socketid]);
2073 rte_exit(EXIT_FAILURE,
2074 "rte_eth_rx_queue_setup: err=%d,"
2075 "port=%d\n", ret, portid);
2082 for (portid = 0; portid < nb_ports; portid++) {
2083 if ((enabled_port_mask & (1 << portid)) == 0)
2087 ret = rte_eth_dev_start(portid);
2089 rte_exit(EXIT_FAILURE,
2090 "rte_eth_dev_start: err=%d, port=%d\n",
2094 * If enabled, put device in promiscuous mode.
2095 * This allows IO forwarding mode to forward packets
2096 * to itself through 2 cross-connected ports of the
2100 rte_eth_promiscuous_enable(portid);
2103 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
2105 /* launch per-lcore init on every lcore */
2106 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2107 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2108 if (rte_eal_wait_lcore(lcore_id) < 0)