1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <sys/queue.h>
17 #include <rte_common.h>
18 #include <rte_byteorder.h>
20 #include <rte_memory.h>
21 #include <rte_memcpy.h>
23 #include <rte_launch.h>
24 #include <rte_atomic.h>
25 #include <rte_spinlock.h>
26 #include <rte_cycles.h>
27 #include <rte_prefetch.h>
28 #include <rte_lcore.h>
29 #include <rte_per_lcore.h>
30 #include <rte_branch_prediction.h>
31 #include <rte_interrupts.h>
32 #include <rte_random.h>
33 #include <rte_debug.h>
34 #include <rte_ether.h>
35 #include <rte_ethdev.h>
36 #include <rte_mempool.h>
41 #include <rte_string_fns.h>
43 #define APP_LOOKUP_EXACT_MATCH 0
44 #define APP_LOOKUP_LPM 1
45 #define DO_RFC_1812_CHECKS
47 //#define APP_LOOKUP_METHOD APP_LOOKUP_EXACT_MATCH
48 #ifndef APP_LOOKUP_METHOD
49 #define APP_LOOKUP_METHOD APP_LOOKUP_LPM
52 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
54 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
57 #error "APP_LOOKUP_METHOD set to incorrect value"
60 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
62 #define MEMPOOL_CACHE_SIZE 256
65 * This expression is used to calculate the number of mbufs needed depending on user input, taking
66 * into account memory for rx and tx hardware rings, cache per lcore and mtable per port per lcore.
67 * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum value of 8192
70 #define NB_MBUF RTE_MAX ( \
71 (nb_ports*nb_rx_queue*nb_rxd + \
72 nb_ports*nb_lcores*MAX_PKT_BURST + \
73 nb_ports*n_tx_queue*nb_txd + \
74 nb_lcores*MEMPOOL_CACHE_SIZE), \
78 * RX and TX Prefetch, Host, and Write-back threshold values should be
79 * carefully set for optimal performance. Consult the network
80 * controller's datasheet and supporting DPDK documentation for guidance
81 * on how these parameters should be set.
83 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
84 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
85 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
88 * These default values are optimized for use with the Intel(R) 82599 10 GbE
89 * Controller and the DPDK ixgbe PMD. Consider using other values for other
90 * network controllers and/or network drivers.
92 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
93 #define TX_HTHRESH 0 /**< Default values of TX host threshold reg. */
94 #define TX_WTHRESH 0 /**< Default values of TX write-back threshold reg. */
96 #define MAX_PKT_BURST 32
97 #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 = 0;
119 static int numa_on = 1; /**< NUMA is enabled by default. */
123 struct rte_mbuf *m_table[MAX_PKT_BURST];
126 struct lcore_rx_queue {
129 } __rte_cache_aligned;
131 #define MAX_RX_QUEUE_PER_LCORE 16
132 #define MAX_TX_QUEUE_PER_PORT 1
133 #define MAX_RX_QUEUE_PER_PORT 1
135 #define MAX_LCORE_PARAMS 1024
136 struct lcore_params {
140 } __rte_cache_aligned;
142 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
143 static struct lcore_params lcore_params_array_default[] = {
155 static struct lcore_params * lcore_params = lcore_params_array_default;
156 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
157 sizeof(lcore_params_array_default[0]);
159 static struct rte_eth_conf port_conf = {
161 .mq_mode = ETH_MQ_RX_RSS,
162 .max_rx_pkt_len = ETHER_MAX_LEN,
164 .header_split = 0, /**< Header Split disabled */
165 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
166 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
167 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
168 .hw_strip_crc = 1, /**< CRC stripped by hardware */
173 .rss_hf = ETH_RSS_IP,
177 .mq_mode = ETH_MQ_TX_NONE,
181 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
184 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
187 #include <rte_hash_crc.h>
188 #define DEFAULT_HASH_FUNC rte_hash_crc
190 #include <rte_jhash.h>
191 #define DEFAULT_HASH_FUNC rte_jhash
200 } __attribute__((__packed__));
203 struct ipv4_5tuple key;
207 static struct l3fwd_route l3fwd_route_array[] = {
208 {{IPv4(100,10,0,1), IPv4(200,10,0,1), 101, 11, IPPROTO_TCP}, 0},
209 {{IPv4(100,20,0,2), IPv4(200,20,0,2), 102, 12, IPPROTO_TCP}, 1},
210 {{IPv4(100,30,0,3), IPv4(200,30,0,3), 103, 13, IPPROTO_TCP}, 2},
211 {{IPv4(100,40,0,4), IPv4(200,40,0,4), 104, 14, IPPROTO_TCP}, 3},
214 typedef struct rte_hash lookup_struct_t;
215 static lookup_struct_t *l3fwd_lookup_struct[NB_SOCKETS];
217 #define L3FWD_HASH_ENTRIES 1024
218 struct rte_hash_parameters l3fwd_hash_params = {
219 .name = "l3fwd_hash_0",
220 .entries = L3FWD_HASH_ENTRIES,
221 .key_len = sizeof(struct ipv4_5tuple),
222 .hash_func = DEFAULT_HASH_FUNC,
223 .hash_func_init_val = 0,
224 .socket_id = SOCKET0,
227 #define L3FWD_NUM_ROUTES \
228 (sizeof(l3fwd_route_array) / sizeof(l3fwd_route_array[0]))
230 static uint8_t l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
233 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
240 static struct l3fwd_route l3fwd_route_array[] = {
241 {IPv4(1,1,1,0), 24, 0},
242 {IPv4(2,1,1,0), 24, 1},
243 {IPv4(3,1,1,0), 24, 2},
244 {IPv4(4,1,1,0), 24, 3},
245 {IPv4(5,1,1,0), 24, 4},
246 {IPv4(6,1,1,0), 24, 5},
247 {IPv4(7,1,1,0), 24, 6},
248 {IPv4(8,1,1,0), 24, 7},
251 #define L3FWD_NUM_ROUTES \
252 (sizeof(l3fwd_route_array) / sizeof(l3fwd_route_array[0]))
254 #define L3FWD_LPM_MAX_RULES 1024
256 typedef struct rte_lpm lookup_struct_t;
257 static lookup_struct_t *l3fwd_lookup_struct[NB_SOCKETS];
262 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
263 uint16_t tx_queue_id;
264 struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
265 lookup_struct_t * lookup_struct;
266 } __rte_cache_aligned;
268 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
269 static rte_spinlock_t spinlock_conf[RTE_MAX_ETHPORTS] = {RTE_SPINLOCK_INITIALIZER};
270 /* Send burst of packets on an output interface */
272 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
274 struct rte_mbuf **m_table;
278 queueid = qconf->tx_queue_id;
279 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
281 rte_spinlock_lock(&spinlock_conf[port]);
282 ret = rte_eth_tx_burst(port, queueid, m_table, n);
283 rte_spinlock_unlock(&spinlock_conf[port]);
285 if (unlikely(ret < n)) {
287 rte_pktmbuf_free(m_table[ret]);
294 /* Enqueue a single packet, and send burst if queue is filled */
296 send_single_packet(struct rte_mbuf *m, uint16_t port)
300 struct lcore_conf *qconf;
302 lcore_id = rte_lcore_id();
304 qconf = &lcore_conf[lcore_id];
305 len = qconf->tx_mbufs[port].len;
306 qconf->tx_mbufs[port].m_table[len] = m;
309 /* enough pkts to be sent */
310 if (unlikely(len == MAX_PKT_BURST)) {
311 send_burst(qconf, MAX_PKT_BURST, port);
315 qconf->tx_mbufs[port].len = len;
319 #ifdef DO_RFC_1812_CHECKS
321 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
323 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
325 * 1. The packet length reported by the Link Layer must be large
326 * enough to hold the minimum length legal IP datagram (20 bytes).
328 if (link_len < sizeof(struct ipv4_hdr))
331 /* 2. The IP checksum must be correct. */
332 /* this is checked in H/W */
335 * 3. The IP version number must be 4. If the version number is not 4
336 * then the packet may be another version of IP, such as IPng or
339 if (((pkt->version_ihl) >> 4) != 4)
342 * 4. The IP header length field must be large enough to hold the
343 * minimum length legal IP datagram (20 bytes = 5 words).
345 if ((pkt->version_ihl & 0xf) < 5)
349 * 5. The IP total length field must be large enough to hold the IP
350 * datagram header, whose length is specified in the IP header length
353 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
360 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
362 print_key(struct ipv4_5tuple key)
364 printf("IP dst = %08x, IP src = %08x, port dst = %d, port src = %d, proto = %d\n",
365 (unsigned)key.ip_dst, (unsigned)key.ip_src, key.port_dst, key.port_src, key.proto);
368 static inline uint16_t
369 get_dst_port(struct ipv4_hdr *ipv4_hdr, uint16_t portid,
370 lookup_struct_t *l3fwd_lookup_struct)
372 struct ipv4_5tuple key;
377 key.ip_dst = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
378 key.ip_src = rte_be_to_cpu_32(ipv4_hdr->src_addr);
379 key.proto = ipv4_hdr->next_proto_id;
381 switch (ipv4_hdr->next_proto_id) {
383 tcp = (struct tcp_hdr *)((unsigned char *) ipv4_hdr +
384 sizeof(struct ipv4_hdr));
385 key.port_dst = rte_be_to_cpu_16(tcp->dst_port);
386 key.port_src = rte_be_to_cpu_16(tcp->src_port);
390 udp = (struct udp_hdr *)((unsigned char *) ipv4_hdr +
391 sizeof(struct ipv4_hdr));
392 key.port_dst = rte_be_to_cpu_16(udp->dst_port);
393 key.port_src = rte_be_to_cpu_16(udp->src_port);
401 /* Find destination port */
402 ret = rte_hash_lookup(l3fwd_lookup_struct, (const void *)&key);
403 return ((ret < 0) ? portid : l3fwd_out_if[ret]);
407 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
408 static inline uint32_t
409 get_dst_port(struct ipv4_hdr *ipv4_hdr, uint16_t portid,
410 lookup_struct_t *l3fwd_lookup_struct)
414 return ((rte_lpm_lookup(l3fwd_lookup_struct,
415 rte_be_to_cpu_32(ipv4_hdr->dst_addr), &next_hop) == 0) ?
421 l3fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
422 lookup_struct_t *l3fwd_lookup_struct)
424 struct ether_hdr *eth_hdr;
425 struct ipv4_hdr *ipv4_hdr;
429 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
431 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
432 sizeof(struct ether_hdr));
434 #ifdef DO_RFC_1812_CHECKS
435 /* Check to make sure the packet is valid (RFC1812) */
436 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
442 dst_port = get_dst_port(ipv4_hdr, portid, l3fwd_lookup_struct);
443 if (dst_port >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port) == 0)
446 /* 02:00:00:00:00:xx */
447 tmp = ð_hdr->d_addr.addr_bytes[0];
448 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
450 #ifdef DO_RFC_1812_CHECKS
451 /* Update time to live and header checksum */
452 --(ipv4_hdr->time_to_live);
453 ++(ipv4_hdr->hdr_checksum);
457 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
459 send_single_packet(m, dst_port);
463 /* main processing loop */
465 main_loop(__attribute__((unused)) void *dummy)
467 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
469 uint64_t prev_tsc, diff_tsc, cur_tsc;
473 struct lcore_conf *qconf;
474 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
478 lcore_id = rte_lcore_id();
479 qconf = &lcore_conf[lcore_id];
481 if (qconf->n_rx_queue == 0) {
482 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
486 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
488 for (i = 0; i < qconf->n_rx_queue; i++) {
490 portid = qconf->rx_queue_list[i].port_id;
491 queueid = qconf->rx_queue_list[i].queue_id;
492 RTE_LOG(INFO, L3FWD, " --lcoreid=%u portid=%u rxqueueid=%hhu\n",
493 lcore_id, portid, queueid);
498 cur_tsc = rte_rdtsc();
501 * TX burst queue drain
503 diff_tsc = cur_tsc - prev_tsc;
504 if (unlikely(diff_tsc > drain_tsc)) {
507 * This could be optimized (use queueid instead of
508 * portid), but it is not called so often
510 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
511 if (qconf->tx_mbufs[portid].len == 0)
513 send_burst(&lcore_conf[lcore_id],
514 qconf->tx_mbufs[portid].len,
516 qconf->tx_mbufs[portid].len = 0;
523 * Read packet from RX queues
525 for (i = 0; i < qconf->n_rx_queue; ++i) {
527 portid = qconf->rx_queue_list[i].port_id;
528 queueid = qconf->rx_queue_list[i].queue_id;
529 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst, MAX_PKT_BURST);
531 /* Prefetch first packets */
532 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
533 rte_prefetch0(rte_pktmbuf_mtod(
534 pkts_burst[j], void *));
537 /* Prefetch and forward already prefetched packets */
538 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
539 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
540 j + PREFETCH_OFFSET], void *));
541 l3fwd_simple_forward(pkts_burst[j], portid, qconf->lookup_struct);
544 /* Forward remaining prefetched packets */
545 for (; j < nb_rx; j++) {
546 l3fwd_simple_forward(pkts_burst[j], portid, qconf->lookup_struct);
553 check_lcore_params(void)
555 uint8_t queue, lcore;
559 for (i = 0; i < nb_lcore_params; ++i) {
560 queue = lcore_params[i].queue_id;
561 if (queue >= MAX_RX_QUEUE_PER_PORT) {
562 printf("invalid queue number: %hhu\n", queue);
565 lcore = lcore_params[i].lcore_id;
566 if (!rte_lcore_is_enabled(lcore)) {
567 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
570 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
572 printf("warning: lcore %hhu is on socket %d with numa off \n",
580 check_port_config(const unsigned nb_ports)
585 for (i = 0; i < nb_lcore_params; ++i) {
586 portid = lcore_params[i].port_id;
587 if ((enabled_port_mask & (1 << portid)) == 0) {
588 printf("port %u is not enabled in port mask\n", portid);
591 if (portid >= nb_ports) {
592 printf("port %u is not present on the board\n", portid);
600 get_port_n_rx_queues(const uint16_t port)
605 for (i = 0; i < nb_lcore_params; ++i) {
606 if (lcore_params[i].port_id == port && lcore_params[i].queue_id > queue)
607 queue = lcore_params[i].queue_id;
609 return (uint8_t)(++queue);
613 init_lcore_rx_queues(void)
615 uint16_t i, nb_rx_queue;
618 for (i = 0; i < nb_lcore_params; ++i) {
619 lcore = lcore_params[i].lcore_id;
620 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
621 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
622 printf("error: too many queues (%u) for lcore: %u\n",
623 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
626 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
627 lcore_params[i].port_id;
628 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
629 lcore_params[i].queue_id;
630 lcore_conf[lcore].n_rx_queue++;
638 print_usage(const char *prgname)
640 printf ("%s [EAL options] -- -p PORTMASK"
641 " [--config (port,queue,lcore)[,(port,queue,lcore]]\n"
642 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
643 " --config (port,queue,lcore): rx queues configuration\n"
644 " --no-numa: optional, disable numa awareness\n",
648 /* Custom handling of signals to handle process terminal */
650 signal_handler(int signum)
653 uint16_t nb_ports = rte_eth_dev_count();
655 /* When we receive a SIGINT signal */
656 if (signum == SIGINT) {
657 for (portid = 0; portid < nb_ports; portid++) {
658 /* skip ports that are not enabled */
659 if ((enabled_port_mask & (1 << portid)) == 0)
661 rte_eth_dev_close(portid);
664 rte_exit(EXIT_SUCCESS, "\n User forced exit\n");
667 parse_portmask(const char *portmask)
672 /* parse hexadecimal string */
673 pm = strtoul(portmask, &end, 16);
674 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
684 parse_config(const char *q_arg)
687 const char *p, *p0 = q_arg;
695 unsigned long int_fld[_NUM_FLD];
696 char *str_fld[_NUM_FLD];
702 while ((p = strchr(p0,'(')) != NULL) {
704 if((p0 = strchr(p,')')) == NULL)
708 if(size >= sizeof(s))
711 snprintf(s, sizeof(s), "%.*s", size, p);
712 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
714 for (i = 0; i < _NUM_FLD; i++){
716 int_fld[i] = strtoul(str_fld[i], &end, 0);
717 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
720 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
721 printf("exceeded max number of lcore params: %hu\n",
725 lcore_params_array[nb_lcore_params].port_id = int_fld[FLD_PORT];
726 lcore_params_array[nb_lcore_params].queue_id = (uint8_t)int_fld[FLD_QUEUE];
727 lcore_params_array[nb_lcore_params].lcore_id = (uint8_t)int_fld[FLD_LCORE];
730 lcore_params = lcore_params_array;
734 /* Parse the argument given in the command line of the application */
736 parse_args(int argc, char **argv)
741 char *prgname = argv[0];
742 static struct option lgopts[] = {
744 {"no-numa", 0, 0, 0},
750 while ((opt = getopt_long(argc, argvopt, "p:",
751 lgopts, &option_index)) != EOF) {
756 enabled_port_mask = parse_portmask(optarg);
757 if (enabled_port_mask == 0) {
758 printf("invalid portmask\n");
759 print_usage(prgname);
766 if (!strcmp(lgopts[option_index].name, "config")) {
767 ret = parse_config(optarg);
769 printf("invalid config\n");
770 print_usage(prgname);
775 if (!strcmp(lgopts[option_index].name, "no-numa")) {
776 printf("numa is disabled \n");
782 print_usage(prgname);
788 argv[optind-1] = prgname;
791 optind = 1; /* reset getopt lib */
796 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
798 char buf[ETHER_ADDR_FMT_SIZE];
799 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
800 printf("%s%s", name, buf);
803 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
805 setup_hash(int socketid)
812 snprintf(s, sizeof(s), "l3fwd_hash_%d", socketid);
813 l3fwd_hash_params.name = s;
814 l3fwd_hash_params.socket_id = socketid;
815 l3fwd_lookup_struct[socketid] = rte_hash_create(&l3fwd_hash_params);
816 if (l3fwd_lookup_struct[socketid] == NULL)
817 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
818 "socket %d\n", socketid);
820 /* populate the hash */
821 for (i = 0; i < L3FWD_NUM_ROUTES; i++) {
822 ret = rte_hash_add_key (l3fwd_lookup_struct[socketid],
823 (void *) &l3fwd_route_array[i].key);
825 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
826 "l3fwd hash on socket %d\n", i, socketid);
828 l3fwd_out_if[ret] = l3fwd_route_array[i].if_out;
829 printf("Hash: Adding key\n");
830 print_key(l3fwd_route_array[i].key);
835 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
837 setup_lpm(int socketid)
843 struct rte_lpm_config lpm_ipv4_config;
845 lpm_ipv4_config.max_rules = L3FWD_LPM_MAX_RULES;
846 lpm_ipv4_config.number_tbl8s = 256;
847 lpm_ipv4_config.flags = 0;
849 /* create the LPM table */
850 snprintf(s, sizeof(s), "L3FWD_LPM_%d", socketid);
851 l3fwd_lookup_struct[socketid] =
852 rte_lpm_create(s, socketid, &lpm_ipv4_config);
853 if (l3fwd_lookup_struct[socketid] == NULL)
854 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
855 " on socket %d\n", socketid);
857 /* populate the LPM table */
858 for (i = 0; i < L3FWD_NUM_ROUTES; i++) {
859 ret = rte_lpm_add(l3fwd_lookup_struct[socketid],
860 l3fwd_route_array[i].ip,
861 l3fwd_route_array[i].depth,
862 l3fwd_route_array[i].if_out);
865 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
866 "l3fwd LPM table on socket %d\n",
870 printf("LPM: Adding route 0x%08x / %d (%d)\n",
871 (unsigned)l3fwd_route_array[i].ip,
872 l3fwd_route_array[i].depth,
873 l3fwd_route_array[i].if_out);
879 init_mem(unsigned nb_mbuf)
881 struct lcore_conf *qconf;
886 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
887 if (rte_lcore_is_enabled(lcore_id) == 0)
891 socketid = rte_lcore_to_socket_id(lcore_id);
895 if (socketid >= NB_SOCKETS) {
896 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
897 socketid, lcore_id, NB_SOCKETS);
899 if (pktmbuf_pool[socketid] == NULL) {
900 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
901 pktmbuf_pool[socketid] = rte_pktmbuf_pool_create(s,
902 nb_mbuf, MEMPOOL_CACHE_SIZE, 0,
903 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
904 if (pktmbuf_pool[socketid] == NULL)
905 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n", socketid);
907 printf("Allocated mbuf pool on socket %d\n", socketid);
909 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
912 setup_hash(socketid);
915 qconf = &lcore_conf[lcore_id];
916 qconf->lookup_struct = l3fwd_lookup_struct[socketid];
922 main(int argc, char **argv)
924 struct lcore_conf *qconf;
925 struct rte_eth_dev_info dev_info;
926 struct rte_eth_txconf *txconf;
929 uint16_t queueid, portid;
933 uint8_t nb_rx_queue, queue, socketid;
935 signal(SIGINT, signal_handler);
937 ret = rte_eal_init(argc, argv);
939 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
943 /* parse application arguments (after the EAL ones) */
944 ret = parse_args(argc, argv);
946 rte_exit(EXIT_FAILURE, "Invalid L3FWD-VF parameters\n");
948 if (check_lcore_params() < 0)
949 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
951 ret = init_lcore_rx_queues();
953 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
955 nb_ports = rte_eth_dev_count();
957 if (check_port_config(nb_ports) < 0)
958 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
960 nb_lcores = rte_lcore_count();
962 /* initialize all ports */
963 for (portid = 0; portid < nb_ports; portid++) {
964 /* skip ports that are not enabled */
965 if ((enabled_port_mask & (1 << portid)) == 0) {
966 printf("\nSkipping disabled port %d\n", portid);
971 printf("Initializing port %d ... ", portid );
974 /* must always equal(=1) */
975 nb_rx_queue = get_port_n_rx_queues(portid);
976 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
978 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
979 nb_rx_queue, (unsigned)1 );
980 ret = rte_eth_dev_configure(portid, nb_rx_queue, n_tx_queue, &port_conf);
982 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
985 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
988 rte_exit(EXIT_FAILURE,
989 "Cannot adjust number of descriptors: err=%d, port=%d\n",
992 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
993 print_ethaddr(" Address:", &ports_eth_addr[portid]);
996 ret = init_mem(NB_MBUF);
998 rte_exit(EXIT_FAILURE, "init_mem failed\n");
1000 /* init one TX queue */
1001 socketid = (uint8_t)rte_lcore_to_socket_id(rte_get_master_lcore());
1003 printf("txq=%d,%d,%d ", portid, 0, socketid);
1006 rte_eth_dev_info_get(portid, &dev_info);
1007 txconf = &dev_info.default_txconf;
1008 if (port_conf.rxmode.jumbo_frame)
1009 txconf->txq_flags = 0;
1010 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1013 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
1014 "port=%d\n", ret, portid);
1019 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1020 if (rte_lcore_is_enabled(lcore_id) == 0)
1022 qconf = &lcore_conf[lcore_id];
1023 qconf->tx_queue_id = 0;
1025 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
1027 /* init RX queues */
1028 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
1029 portid = qconf->rx_queue_list[queue].port_id;
1030 queueid = qconf->rx_queue_list[queue].queue_id;
1033 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1037 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
1040 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
1042 pktmbuf_pool[socketid]);
1044 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
1045 "port=%d\n", ret, portid);
1051 for (portid = 0; portid < nb_ports; portid++) {
1052 if ((enabled_port_mask & (1 << portid)) == 0) {
1056 ret = rte_eth_dev_start(portid);
1058 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
1061 printf("done: Port %d\n", portid);
1065 /* launch per-lcore init on every lcore */
1066 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1067 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1068 if (rte_eal_wait_lcore(lcore_id) < 0)