1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <inttypes.h>
9 #include <sys/types.h>
10 #include <sys/param.h>
11 #include <string.h>
12 #include <sys/queue.h>
13 #include <stdarg.h>
14 #include <errno.h>
15 #include <getopt.h>
16
17 #include <rte_common.h>
18 #include <rte_byteorder.h>
19 #include <rte_log.h>
20 #include <rte_memory.h>
21 #include <rte_memcpy.h>
22 #include <rte_eal.h>
23 #include <rte_launch.h>
24 #include <rte_cycles.h>
25 #include <rte_prefetch.h>
26 #include <rte_lcore.h>
27 #include <rte_per_lcore.h>
28 #include <rte_branch_prediction.h>
29 #include <rte_interrupts.h>
30 #include <rte_random.h>
31 #include <rte_debug.h>
32 #include <rte_ether.h>
33 #include <rte_ethdev.h>
34 #include <rte_mempool.h>
35 #include <rte_mbuf.h>
36 #include <rte_lpm.h>
37 #include <rte_lpm6.h>
38 #include <rte_ip.h>
39 #include <rte_string_fns.h>
40
41 #include <rte_ip_frag.h>
42
43 #define RTE_LOGTYPE_IP_FRAG RTE_LOGTYPE_USER1
44
45 /* allow max jumbo frame 9.5 KB */
46 #define JUMBO_FRAME_MAX_SIZE 0x2600
47
48 #define ROUNDUP_DIV(a, b) (((a) + (b) - 1) / (b))
49
50 /*
51 * Default byte size for the IPv6 Maximum Transfer Unit (MTU).
52 * This value includes the size of IPv6 header.
53 */
54 #define IPV4_MTU_DEFAULT RTE_ETHER_MTU
55 #define IPV6_MTU_DEFAULT RTE_ETHER_MTU
56
57 /*
58 * The overhead from max frame size to MTU.
59 * We have to consider the max possible overhead.
60 */
61 #define MTU_OVERHEAD \
62 (RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN + \
63 2 * sizeof(struct rte_vlan_hdr))
64
65 /*
66 * Default payload in bytes for the IPv6 packet.
67 */
68 #define IPV4_DEFAULT_PAYLOAD (IPV4_MTU_DEFAULT - sizeof(struct rte_ipv4_hdr))
69 #define IPV6_DEFAULT_PAYLOAD (IPV6_MTU_DEFAULT - sizeof(struct rte_ipv6_hdr))
70
71 /*
72 * Max number of fragments per packet expected - defined by config file.
73 */
74 #define MAX_PACKET_FRAG RTE_LIBRTE_IP_FRAG_MAX_FRAG
75
76 #define NB_MBUF 8192
77
78 #define MAX_PKT_BURST 32
79 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
80
81 /* Configure how many packets ahead to prefetch, when reading packets */
82 #define PREFETCH_OFFSET 3
83
84 /*
85 * Configurable number of RX/TX ring descriptors
86 */
87 #define RTE_TEST_RX_DESC_DEFAULT 1024
88 #define RTE_TEST_TX_DESC_DEFAULT 1024
89 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
90 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
91
92 /* ethernet addresses of ports */
93 static struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
94
95 #ifndef IPv4_BYTES
96 #define IPv4_BYTES_FMT "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8
97 #define IPv4_BYTES(addr) \
98 (uint8_t) (((addr) >> 24) & 0xFF),\
99 (uint8_t) (((addr) >> 16) & 0xFF),\
100 (uint8_t) (((addr) >> 8) & 0xFF),\
101 (uint8_t) ((addr) & 0xFF)
102 #endif
103
104 #ifndef IPv6_BYTES
105 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
106 "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
107 #define IPv6_BYTES(addr) \
108 addr[0], addr[1], addr[2], addr[3], \
109 addr[4], addr[5], addr[6], addr[7], \
110 addr[8], addr[9], addr[10], addr[11],\
111 addr[12], addr[13],addr[14], addr[15]
112 #endif
113
114 #define IPV6_ADDR_LEN 16
115
116 /* mask of enabled ports */
117 static int enabled_port_mask = 0;
118
119 static int rx_queue_per_lcore = 1;
120
121 #define MBUF_TABLE_SIZE (2 * MAX(MAX_PKT_BURST, MAX_PACKET_FRAG))
122
123 struct mbuf_table {
124 uint16_t len;
125 struct rte_mbuf *m_table[MBUF_TABLE_SIZE];
126 };
127
128 struct rx_queue {
129 struct rte_mempool *direct_pool;
130 struct rte_mempool *indirect_pool;
131 struct rte_lpm *lpm;
132 struct rte_lpm6 *lpm6;
133 uint16_t portid;
134 };
135
136 #define MAX_RX_QUEUE_PER_LCORE 16
137 #define MAX_TX_QUEUE_PER_PORT 16
138 struct lcore_queue_conf {
139 uint16_t n_rx_queue;
140 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
141 struct rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
142 struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
143 } __rte_cache_aligned;
144 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
145
146 static struct rte_eth_conf port_conf = {
147 .rxmode = {
148 .mtu = JUMBO_FRAME_MAX_SIZE - RTE_ETHER_HDR_LEN -
149 RTE_ETHER_CRC_LEN,
150 .split_hdr_size = 0,
151 .offloads = (RTE_ETH_RX_OFFLOAD_CHECKSUM |
152 RTE_ETH_RX_OFFLOAD_SCATTER),
153 },
154 .txmode = {
155 .mq_mode = RTE_ETH_MQ_TX_NONE,
156 .offloads = (RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
157 RTE_ETH_TX_OFFLOAD_MULTI_SEGS),
158 },
159 };
160
161 /*
162 * IPv4 forwarding table
163 */
164 struct l3fwd_ipv4_route {
165 uint32_t ip;
166 uint8_t depth;
167 uint8_t if_out;
168 };
169
170 /* Default l3fwd_ipv4_route_array table. 8< */
171 struct l3fwd_ipv4_route l3fwd_ipv4_route_array[] = {
172 {RTE_IPV4(100,10,0,0), 16, 0},
173 {RTE_IPV4(100,20,0,0), 16, 1},
174 {RTE_IPV4(100,30,0,0), 16, 2},
175 {RTE_IPV4(100,40,0,0), 16, 3},
176 {RTE_IPV4(100,50,0,0), 16, 4},
177 {RTE_IPV4(100,60,0,0), 16, 5},
178 {RTE_IPV4(100,70,0,0), 16, 6},
179 {RTE_IPV4(100,80,0,0), 16, 7},
180 };
181 /* >8 End of default l3fwd_ipv4_route_array table */
182
183 /*
184 * IPv6 forwarding table
185 */
186
187 struct l3fwd_ipv6_route {
188 uint8_t ip[IPV6_ADDR_LEN];
189 uint8_t depth;
190 uint8_t if_out;
191 };
192
193 /* Default l3fwd_ipv6_route_array table. 8< */
194 static struct l3fwd_ipv6_route l3fwd_ipv6_route_array[] = {
195 {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0},
196 {{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1},
197 {{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2},
198 {{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3},
199 {{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4},
200 {{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5},
201 {{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6},
202 {{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7},
203 };
204 /* >8 End of default l3fwd_ipv6_route_array table. */
205
206 #define LPM_MAX_RULES 1024
207 #define LPM6_MAX_RULES 1024
208 #define LPM6_NUMBER_TBL8S (1 << 16)
209
210 struct rte_lpm6_config lpm6_config = {
211 .max_rules = LPM6_MAX_RULES,
212 .number_tbl8s = LPM6_NUMBER_TBL8S,
213 .flags = 0
214 };
215
216 static struct rte_mempool *socket_direct_pool[RTE_MAX_NUMA_NODES];
217 static struct rte_mempool *socket_indirect_pool[RTE_MAX_NUMA_NODES];
218 static struct rte_lpm *socket_lpm[RTE_MAX_NUMA_NODES];
219 static struct rte_lpm6 *socket_lpm6[RTE_MAX_NUMA_NODES];
220
221 /* Send burst of packets on an output interface */
222 static inline int
send_burst(struct lcore_queue_conf * qconf,uint16_t n,uint16_t port)223 send_burst(struct lcore_queue_conf *qconf, uint16_t n, uint16_t port)
224 {
225 struct rte_mbuf **m_table;
226 int ret;
227 uint16_t queueid;
228
229 queueid = qconf->tx_queue_id[port];
230 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
231
232 ret = rte_eth_tx_burst(port, queueid, m_table, n);
233 if (unlikely(ret < n)) {
234 do {
235 rte_pktmbuf_free(m_table[ret]);
236 } while (++ret < n);
237 }
238
239 return 0;
240 }
241
242 static inline void
l3fwd_simple_forward(struct rte_mbuf * m,struct lcore_queue_conf * qconf,uint8_t queueid,uint16_t port_in)243 l3fwd_simple_forward(struct rte_mbuf *m, struct lcore_queue_conf *qconf,
244 uint8_t queueid, uint16_t port_in)
245 {
246 struct rx_queue *rxq;
247 uint32_t i, len, next_hop;
248 uint16_t port_out, ether_type;
249 int32_t len2;
250 uint64_t ol_flags;
251 const struct rte_ether_hdr *eth;
252
253 ol_flags = 0;
254 rxq = &qconf->rx_queue_list[queueid];
255
256 /* by default, send everything back to the source port */
257 port_out = port_in;
258
259 /* save ether type of the incoming packet */
260 eth = rte_pktmbuf_mtod(m, const struct rte_ether_hdr *);
261 ether_type = eth->ether_type;
262
263 /* Remove the Ethernet header and trailer from the input packet */
264 rte_pktmbuf_adj(m, (uint16_t)sizeof(struct rte_ether_hdr));
265
266 /* Build transmission burst */
267 len = qconf->tx_mbufs[port_out].len;
268
269 /* if this is an IPv4 packet */
270 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
271 struct rte_ipv4_hdr *ip_hdr;
272 uint32_t ip_dst;
273 /* Read the lookup key (i.e. ip_dst) from the input packet */
274 ip_hdr = rte_pktmbuf_mtod(m, struct rte_ipv4_hdr *);
275 ip_dst = rte_be_to_cpu_32(ip_hdr->dst_addr);
276
277 /* Find destination port */
278 if (rte_lpm_lookup(rxq->lpm, ip_dst, &next_hop) == 0 &&
279 (enabled_port_mask & 1 << next_hop) != 0) {
280 port_out = next_hop;
281
282 /* Build transmission burst for new port */
283 len = qconf->tx_mbufs[port_out].len;
284 }
285
286 /* if we don't need to do any fragmentation */
287 if (likely (IPV4_MTU_DEFAULT >= m->pkt_len)) {
288 qconf->tx_mbufs[port_out].m_table[len] = m;
289 len2 = 1;
290 } else {
291 len2 = rte_ipv4_fragment_packet(m,
292 &qconf->tx_mbufs[port_out].m_table[len],
293 (uint16_t)(MBUF_TABLE_SIZE - len),
294 IPV4_MTU_DEFAULT,
295 rxq->direct_pool, rxq->indirect_pool);
296
297 /* Free input packet */
298 rte_pktmbuf_free(m);
299
300 /* request HW to regenerate IPv4 cksum */
301 ol_flags |= (RTE_MBUF_F_TX_IPV4 | RTE_MBUF_F_TX_IP_CKSUM);
302
303 /* If we fail to fragment the packet */
304 if (unlikely (len2 < 0))
305 return;
306 }
307 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
308 /* if this is an IPv6 packet */
309 struct rte_ipv6_hdr *ip_hdr;
310
311 /* Read the lookup key (i.e. ip_dst) from the input packet */
312 ip_hdr = rte_pktmbuf_mtod(m, struct rte_ipv6_hdr *);
313
314 /* Find destination port */
315 if (rte_lpm6_lookup(rxq->lpm6, ip_hdr->dst_addr,
316 &next_hop) == 0 &&
317 (enabled_port_mask & 1 << next_hop) != 0) {
318 port_out = next_hop;
319
320 /* Build transmission burst for new port */
321 len = qconf->tx_mbufs[port_out].len;
322 }
323
324 /* if we don't need to do any fragmentation */
325 if (likely (IPV6_MTU_DEFAULT >= m->pkt_len)) {
326 qconf->tx_mbufs[port_out].m_table[len] = m;
327 len2 = 1;
328 } else {
329 len2 = rte_ipv6_fragment_packet(m,
330 &qconf->tx_mbufs[port_out].m_table[len],
331 (uint16_t)(MBUF_TABLE_SIZE - len),
332 IPV6_MTU_DEFAULT,
333 rxq->direct_pool, rxq->indirect_pool);
334
335 /* Free input packet */
336 rte_pktmbuf_free(m);
337
338 /* If we fail to fragment the packet */
339 if (unlikely (len2 < 0))
340 return;
341 }
342 }
343 /* else, just forward the packet */
344 else {
345 qconf->tx_mbufs[port_out].m_table[len] = m;
346 len2 = 1;
347 }
348
349 for (i = len; i < len + len2; i ++) {
350 void *d_addr_bytes;
351
352 m = qconf->tx_mbufs[port_out].m_table[i];
353 struct rte_ether_hdr *eth_hdr = (struct rte_ether_hdr *)
354 rte_pktmbuf_prepend(m,
355 (uint16_t)sizeof(struct rte_ether_hdr));
356 if (eth_hdr == NULL) {
357 rte_panic("No headroom in mbuf.\n");
358 }
359
360 m->ol_flags |= ol_flags;
361 m->l2_len = sizeof(struct rte_ether_hdr);
362
363 /* 02:00:00:00:00:xx */
364 d_addr_bytes = ð_hdr->dst_addr.addr_bytes[0];
365 *((uint64_t *)d_addr_bytes) = 0x000000000002 +
366 ((uint64_t)port_out << 40);
367
368 /* src addr */
369 rte_ether_addr_copy(&ports_eth_addr[port_out],
370 ð_hdr->src_addr);
371 eth_hdr->ether_type = ether_type;
372 }
373
374 len += len2;
375
376 if (likely(len < MAX_PKT_BURST)) {
377 qconf->tx_mbufs[port_out].len = (uint16_t)len;
378 return;
379 }
380
381 /* Transmit packets */
382 send_burst(qconf, (uint16_t)len, port_out);
383 qconf->tx_mbufs[port_out].len = 0;
384 }
385
386 /* main processing loop */
387 static int
main_loop(__rte_unused void * dummy)388 main_loop(__rte_unused void *dummy)
389 {
390 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
391 unsigned lcore_id;
392 uint64_t prev_tsc, diff_tsc, cur_tsc;
393 int i, j, nb_rx;
394 uint16_t portid;
395 struct lcore_queue_conf *qconf;
396 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
397
398 prev_tsc = 0;
399
400 lcore_id = rte_lcore_id();
401 qconf = &lcore_queue_conf[lcore_id];
402
403 if (qconf->n_rx_queue == 0) {
404 RTE_LOG(INFO, IP_FRAG, "lcore %u has nothing to do\n", lcore_id);
405 return 0;
406 }
407
408 RTE_LOG(INFO, IP_FRAG, "entering main loop on lcore %u\n", lcore_id);
409
410 for (i = 0; i < qconf->n_rx_queue; i++) {
411
412 portid = qconf->rx_queue_list[i].portid;
413 RTE_LOG(INFO, IP_FRAG, " -- lcoreid=%u portid=%d\n", lcore_id,
414 portid);
415 }
416
417 while (1) {
418
419 cur_tsc = rte_rdtsc();
420
421 /*
422 * TX burst queue drain
423 */
424 diff_tsc = cur_tsc - prev_tsc;
425 if (unlikely(diff_tsc > drain_tsc)) {
426
427 /*
428 * This could be optimized (use queueid instead of
429 * portid), but it is not called so often
430 */
431 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
432 if (qconf->tx_mbufs[portid].len == 0)
433 continue;
434 send_burst(&lcore_queue_conf[lcore_id],
435 qconf->tx_mbufs[portid].len,
436 portid);
437 qconf->tx_mbufs[portid].len = 0;
438 }
439
440 prev_tsc = cur_tsc;
441 }
442
443 /*
444 * Read packet from RX queues
445 */
446 for (i = 0; i < qconf->n_rx_queue; i++) {
447
448 portid = qconf->rx_queue_list[i].portid;
449 nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
450 MAX_PKT_BURST);
451
452 /* Prefetch first packets */
453 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
454 rte_prefetch0(rte_pktmbuf_mtod(
455 pkts_burst[j], void *));
456 }
457
458 /* Prefetch and forward already prefetched packets */
459 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
460 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
461 j + PREFETCH_OFFSET], void *));
462 l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
463 }
464
465 /* Forward remaining prefetched packets */
466 for (; j < nb_rx; j++) {
467 l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
468 }
469 }
470 }
471 }
472
473 /* display usage */
474 static void
print_usage(const char * prgname)475 print_usage(const char *prgname)
476 {
477 printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
478 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
479 " -q NQ: number of queue (=ports) per lcore (default is 1)\n",
480 prgname);
481 }
482
483 static int
parse_portmask(const char * portmask)484 parse_portmask(const char *portmask)
485 {
486 char *end = NULL;
487 unsigned long pm;
488
489 /* parse hexadecimal string */
490 pm = strtoul(portmask, &end, 16);
491 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
492 return -1;
493
494 if (pm == 0)
495 return -1;
496
497 return pm;
498 }
499
500 static int
parse_nqueue(const char * q_arg)501 parse_nqueue(const char *q_arg)
502 {
503 char *end = NULL;
504 unsigned long n;
505
506 /* parse hexadecimal string */
507 n = strtoul(q_arg, &end, 10);
508 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
509 return -1;
510 if (n == 0)
511 return -1;
512 if (n >= MAX_RX_QUEUE_PER_LCORE)
513 return -1;
514
515 return n;
516 }
517
518 /* Parse the argument given in the command line of the application */
519 static int
parse_args(int argc,char ** argv)520 parse_args(int argc, char **argv)
521 {
522 int opt, ret;
523 char **argvopt;
524 int option_index;
525 char *prgname = argv[0];
526 static struct option lgopts[] = {
527 {NULL, 0, 0, 0}
528 };
529
530 argvopt = argv;
531
532 while ((opt = getopt_long(argc, argvopt, "p:q:",
533 lgopts, &option_index)) != EOF) {
534
535 switch (opt) {
536 /* portmask */
537 case 'p':
538 enabled_port_mask = parse_portmask(optarg);
539 if (enabled_port_mask < 0) {
540 printf("invalid portmask\n");
541 print_usage(prgname);
542 return -1;
543 }
544 break;
545
546 /* nqueue */
547 case 'q':
548 rx_queue_per_lcore = parse_nqueue(optarg);
549 if (rx_queue_per_lcore < 0) {
550 printf("invalid queue number\n");
551 print_usage(prgname);
552 return -1;
553 }
554 break;
555
556 /* long options */
557 case 0:
558 print_usage(prgname);
559 return -1;
560
561 default:
562 print_usage(prgname);
563 return -1;
564 }
565 }
566
567 if (enabled_port_mask == 0) {
568 printf("portmask not specified\n");
569 print_usage(prgname);
570 return -1;
571 }
572
573 if (optind >= 0)
574 argv[optind-1] = prgname;
575
576 ret = optind-1;
577 optind = 1; /* reset getopt lib */
578 return ret;
579 }
580
581 static void
print_ethaddr(const char * name,struct rte_ether_addr * eth_addr)582 print_ethaddr(const char *name, struct rte_ether_addr *eth_addr)
583 {
584 char buf[RTE_ETHER_ADDR_FMT_SIZE];
585 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
586 printf("%s%s", name, buf);
587 }
588
589 /* Check the link status of all ports in up to 9s, and print them finally */
590 static void
check_all_ports_link_status(uint32_t port_mask)591 check_all_ports_link_status(uint32_t port_mask)
592 {
593 #define CHECK_INTERVAL 100 /* 100ms */
594 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
595 uint16_t portid;
596 uint8_t count, all_ports_up, print_flag = 0;
597 struct rte_eth_link link;
598 int ret;
599 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
600
601 printf("\nChecking link status");
602 fflush(stdout);
603 for (count = 0; count <= MAX_CHECK_TIME; count++) {
604 all_ports_up = 1;
605 RTE_ETH_FOREACH_DEV(portid) {
606 if ((port_mask & (1 << portid)) == 0)
607 continue;
608 memset(&link, 0, sizeof(link));
609 ret = rte_eth_link_get_nowait(portid, &link);
610 if (ret < 0) {
611 all_ports_up = 0;
612 if (print_flag == 1)
613 printf("Port %u link get failed: %s\n",
614 portid, rte_strerror(-ret));
615 continue;
616 }
617 /* print link status if flag set */
618 if (print_flag == 1) {
619 rte_eth_link_to_str(link_status_text,
620 sizeof(link_status_text), &link);
621 printf("Port %d %s\n", portid,
622 link_status_text);
623 continue;
624 }
625 /* clear all_ports_up flag if any link down */
626 if (link.link_status == RTE_ETH_LINK_DOWN) {
627 all_ports_up = 0;
628 break;
629 }
630 }
631 /* after finally printing all link status, get out */
632 if (print_flag == 1)
633 break;
634
635 if (all_ports_up == 0) {
636 printf(".");
637 fflush(stdout);
638 rte_delay_ms(CHECK_INTERVAL);
639 }
640
641 /* set the print_flag if all ports up or timeout */
642 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
643 print_flag = 1;
644 printf("\ndone\n");
645 }
646 }
647 }
648
649 /* Check L3 packet type detection capability of the NIC port */
650 static int
check_ptype(int portid)651 check_ptype(int portid)
652 {
653 int i, ret;
654 int ptype_l3_ipv4 = 0, ptype_l3_ipv6 = 0;
655 uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
656
657 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
658 if (ret <= 0)
659 return 0;
660
661 uint32_t ptypes[ret];
662
663 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
664 for (i = 0; i < ret; ++i) {
665 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
666 ptype_l3_ipv4 = 1;
667 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
668 ptype_l3_ipv6 = 1;
669 }
670
671 if (ptype_l3_ipv4 == 0)
672 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
673
674 if (ptype_l3_ipv6 == 0)
675 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
676
677 if (ptype_l3_ipv4 && ptype_l3_ipv6)
678 return 1;
679
680 return 0;
681
682 }
683
684 /* Parse packet type of a packet by SW */
685 static inline void
parse_ptype(struct rte_mbuf * m)686 parse_ptype(struct rte_mbuf *m)
687 {
688 struct rte_ether_hdr *eth_hdr;
689 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
690 uint16_t ether_type;
691
692 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
693 ether_type = eth_hdr->ether_type;
694 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
695 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
696 else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
697 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
698
699 m->packet_type = packet_type;
700 }
701
702 /* callback function to detect packet type for a queue of a port */
703 static uint16_t
cb_parse_ptype(uint16_t port __rte_unused,uint16_t queue __rte_unused,struct rte_mbuf * pkts[],uint16_t nb_pkts,uint16_t max_pkts __rte_unused,void * user_param __rte_unused)704 cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
705 struct rte_mbuf *pkts[], uint16_t nb_pkts,
706 uint16_t max_pkts __rte_unused,
707 void *user_param __rte_unused)
708 {
709 uint16_t i;
710
711 for (i = 0; i < nb_pkts; ++i)
712 parse_ptype(pkts[i]);
713
714 return nb_pkts;
715 }
716
717 static int
init_routing_table(void)718 init_routing_table(void)
719 {
720 struct rte_lpm *lpm;
721 struct rte_lpm6 *lpm6;
722 int socket, ret;
723 unsigned i;
724
725 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
726 if (socket_lpm[socket]) {
727 lpm = socket_lpm[socket];
728 /* populate the LPM table */
729 for (i = 0; i < RTE_DIM(l3fwd_ipv4_route_array); i++) {
730 ret = rte_lpm_add(lpm,
731 l3fwd_ipv4_route_array[i].ip,
732 l3fwd_ipv4_route_array[i].depth,
733 l3fwd_ipv4_route_array[i].if_out);
734
735 if (ret < 0) {
736 RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
737 "LPM table\n", i);
738 return -1;
739 }
740
741 RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv4_BYTES_FMT
742 "/%d (port %d)\n",
743 socket,
744 IPv4_BYTES(l3fwd_ipv4_route_array[i].ip),
745 l3fwd_ipv4_route_array[i].depth,
746 l3fwd_ipv4_route_array[i].if_out);
747 }
748 }
749
750 if (socket_lpm6[socket]) {
751 lpm6 = socket_lpm6[socket];
752 /* populate the LPM6 table */
753 for (i = 0; i < RTE_DIM(l3fwd_ipv6_route_array); i++) {
754 ret = rte_lpm6_add(lpm6,
755 l3fwd_ipv6_route_array[i].ip,
756 l3fwd_ipv6_route_array[i].depth,
757 l3fwd_ipv6_route_array[i].if_out);
758
759 if (ret < 0) {
760 RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
761 "LPM6 table\n", i);
762 return -1;
763 }
764
765 RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv6_BYTES_FMT
766 "/%d (port %d)\n",
767 socket,
768 IPv6_BYTES(l3fwd_ipv6_route_array[i].ip),
769 l3fwd_ipv6_route_array[i].depth,
770 l3fwd_ipv6_route_array[i].if_out);
771 }
772 }
773 }
774 return 0;
775 }
776
777 static int
init_mem(void)778 init_mem(void)
779 {
780 char buf[PATH_MAX];
781 struct rte_mempool *mp;
782 struct rte_lpm *lpm;
783 struct rte_lpm6 *lpm6;
784 struct rte_lpm_config lpm_config;
785 int socket;
786 unsigned lcore_id;
787
788 /* traverse through lcores and initialize structures on each socket */
789
790 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
791
792 if (rte_lcore_is_enabled(lcore_id) == 0)
793 continue;
794
795 socket = rte_lcore_to_socket_id(lcore_id);
796
797 if (socket == SOCKET_ID_ANY)
798 socket = 0;
799
800 if (socket_direct_pool[socket] == NULL) {
801 RTE_LOG(INFO, IP_FRAG, "Creating direct mempool on socket %i\n",
802 socket);
803 snprintf(buf, sizeof(buf), "pool_direct_%i", socket);
804
805 mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32,
806 0, RTE_MBUF_DEFAULT_BUF_SIZE, socket);
807 if (mp == NULL) {
808 RTE_LOG(ERR, IP_FRAG, "Cannot create direct mempool\n");
809 return -1;
810 }
811 socket_direct_pool[socket] = mp;
812 }
813
814 if (socket_indirect_pool[socket] == NULL) {
815 RTE_LOG(INFO, IP_FRAG, "Creating indirect mempool on socket %i\n",
816 socket);
817 snprintf(buf, sizeof(buf), "pool_indirect_%i", socket);
818
819 mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32, 0, 0,
820 socket);
821 if (mp == NULL) {
822 RTE_LOG(ERR, IP_FRAG, "Cannot create indirect mempool\n");
823 return -1;
824 }
825 socket_indirect_pool[socket] = mp;
826 }
827
828 if (socket_lpm[socket] == NULL) {
829 RTE_LOG(INFO, IP_FRAG, "Creating LPM table on socket %i\n", socket);
830 snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
831
832 lpm_config.max_rules = LPM_MAX_RULES;
833 lpm_config.number_tbl8s = 256;
834 lpm_config.flags = 0;
835
836 lpm = rte_lpm_create(buf, socket, &lpm_config);
837 if (lpm == NULL) {
838 RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
839 return -1;
840 }
841 socket_lpm[socket] = lpm;
842 }
843
844 if (socket_lpm6[socket] == NULL) {
845 RTE_LOG(INFO, IP_FRAG, "Creating LPM6 table on socket %i\n", socket);
846 snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
847
848 lpm6 = rte_lpm6_create(buf, socket, &lpm6_config);
849 if (lpm6 == NULL) {
850 RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
851 return -1;
852 }
853 socket_lpm6[socket] = lpm6;
854 }
855 }
856
857 return 0;
858 }
859
860 int
main(int argc,char ** argv)861 main(int argc, char **argv)
862 {
863 struct lcore_queue_conf *qconf;
864 struct rte_eth_dev_info dev_info;
865 struct rte_eth_txconf *txconf;
866 struct rx_queue *rxq;
867 int socket, ret;
868 uint16_t nb_ports;
869 uint16_t queueid = 0;
870 unsigned lcore_id = 0, rx_lcore_id = 0;
871 uint32_t n_tx_queue, nb_lcores;
872 uint16_t portid;
873
874 /* init EAL */
875 ret = rte_eal_init(argc, argv);
876 if (ret < 0)
877 rte_exit(EXIT_FAILURE, "rte_eal_init failed");
878 argc -= ret;
879 argv += ret;
880
881 /* parse application arguments (after the EAL ones) */
882 ret = parse_args(argc, argv);
883 if (ret < 0)
884 rte_exit(EXIT_FAILURE, "Invalid arguments");
885
886 nb_ports = rte_eth_dev_count_avail();
887 if (nb_ports == 0)
888 rte_exit(EXIT_FAILURE, "No ports found!\n");
889
890 nb_lcores = rte_lcore_count();
891
892 /* initialize structures (mempools, lpm etc.) */
893 if (init_mem() < 0)
894 rte_panic("Cannot initialize memory structures!\n");
895
896 /* check if portmask has non-existent ports */
897 if (enabled_port_mask & ~(RTE_LEN2MASK(nb_ports, unsigned)))
898 rte_exit(EXIT_FAILURE, "Non-existent ports in portmask!\n");
899
900 /* initialize all ports */
901 RTE_ETH_FOREACH_DEV(portid) {
902 struct rte_eth_conf local_port_conf = port_conf;
903 struct rte_eth_rxconf rxq_conf;
904
905 /* skip ports that are not enabled */
906 if ((enabled_port_mask & (1 << portid)) == 0) {
907 printf("Skipping disabled port %d\n", portid);
908 continue;
909 }
910
911 qconf = &lcore_queue_conf[rx_lcore_id];
912
913 /* limit the frame size to the maximum supported by NIC */
914 ret = rte_eth_dev_info_get(portid, &dev_info);
915 if (ret != 0)
916 rte_exit(EXIT_FAILURE,
917 "Error during getting device (port %u) info: %s\n",
918 portid, strerror(-ret));
919
920 local_port_conf.rxmode.mtu = RTE_MIN(
921 dev_info.max_mtu,
922 local_port_conf.rxmode.mtu);
923
924 /* get the lcore_id for this port */
925 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
926 qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) {
927
928 rx_lcore_id ++;
929 if (rx_lcore_id >= RTE_MAX_LCORE)
930 rte_exit(EXIT_FAILURE, "Not enough cores\n");
931
932 qconf = &lcore_queue_conf[rx_lcore_id];
933 }
934
935 socket = (int) rte_lcore_to_socket_id(rx_lcore_id);
936 if (socket == SOCKET_ID_ANY)
937 socket = 0;
938
939 rxq = &qconf->rx_queue_list[qconf->n_rx_queue];
940 rxq->portid = portid;
941 rxq->direct_pool = socket_direct_pool[socket];
942 rxq->indirect_pool = socket_indirect_pool[socket];
943 rxq->lpm = socket_lpm[socket];
944 rxq->lpm6 = socket_lpm6[socket];
945 qconf->n_rx_queue++;
946
947 /* init port */
948 printf("Initializing port %d on lcore %u...", portid,
949 rx_lcore_id);
950 fflush(stdout);
951
952 n_tx_queue = nb_lcores;
953 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
954 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
955 ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue,
956 &local_port_conf);
957 if (ret < 0) {
958 printf("\n");
959 rte_exit(EXIT_FAILURE, "Cannot configure device: "
960 "err=%d, port=%d\n",
961 ret, portid);
962 }
963
964 /* set the mtu to the maximum received packet size */
965 ret = rte_eth_dev_set_mtu(portid, local_port_conf.rxmode.mtu);
966 if (ret < 0) {
967 printf("\n");
968 rte_exit(EXIT_FAILURE, "Set MTU failed: "
969 "err=%d, port=%d\n",
970 ret, portid);
971 }
972
973 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
974 &nb_txd);
975 if (ret < 0) {
976 printf("\n");
977 rte_exit(EXIT_FAILURE, "Cannot adjust number of "
978 "descriptors: err=%d, port=%d\n", ret, portid);
979 }
980
981 /* init one RX queue */
982 rxq_conf = dev_info.default_rxconf;
983 rxq_conf.offloads = local_port_conf.rxmode.offloads;
984 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
985 socket, &rxq_conf,
986 socket_direct_pool[socket]);
987 if (ret < 0) {
988 printf("\n");
989 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: "
990 "err=%d, port=%d\n",
991 ret, portid);
992 }
993
994 ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
995 if (ret < 0) {
996 printf("\n");
997 rte_exit(EXIT_FAILURE,
998 "rte_eth_macaddr_get: err=%d, port=%d\n",
999 ret, portid);
1000 }
1001
1002 print_ethaddr(" Address:", &ports_eth_addr[portid]);
1003 printf("\n");
1004
1005 /* init one TX queue per couple (lcore,port) */
1006 ret = rte_eth_dev_info_get(portid, &dev_info);
1007 if (ret != 0)
1008 rte_exit(EXIT_FAILURE,
1009 "Error during getting device (port %u) info: %s\n",
1010 portid, strerror(-ret));
1011
1012 queueid = 0;
1013 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1014 if (rte_lcore_is_enabled(lcore_id) == 0)
1015 continue;
1016
1017 if (queueid >= dev_info.nb_tx_queues)
1018 break;
1019
1020 socket = (int) rte_lcore_to_socket_id(lcore_id);
1021 printf("txq=%u,%d ", lcore_id, queueid);
1022 fflush(stdout);
1023
1024 txconf = &dev_info.default_txconf;
1025 txconf->offloads = local_port_conf.txmode.offloads;
1026 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
1027 socket, txconf);
1028 if (ret < 0) {
1029 printf("\n");
1030 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
1031 "err=%d, port=%d\n", ret, portid);
1032 }
1033
1034 qconf = &lcore_queue_conf[lcore_id];
1035 qconf->tx_queue_id[portid] = queueid;
1036 queueid++;
1037 }
1038
1039 printf("\n");
1040 }
1041
1042 printf("\n");
1043
1044 /* start ports */
1045 RTE_ETH_FOREACH_DEV(portid) {
1046 if ((enabled_port_mask & (1 << portid)) == 0) {
1047 continue;
1048 }
1049 /* Start device */
1050 ret = rte_eth_dev_start(portid);
1051 if (ret < 0)
1052 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
1053 ret, portid);
1054
1055 ret = rte_eth_promiscuous_enable(portid);
1056 if (ret != 0)
1057 rte_exit(EXIT_FAILURE,
1058 "rte_eth_promiscuous_enable: err=%s, port=%d\n",
1059 rte_strerror(-ret), portid);
1060
1061 if (check_ptype(portid) == 0) {
1062 rte_eth_add_rx_callback(portid, 0, cb_parse_ptype, NULL);
1063 printf("Add Rx callback function to detect L3 packet type by SW :"
1064 " port = %d\n", portid);
1065 }
1066 }
1067
1068 if (init_routing_table() < 0)
1069 rte_exit(EXIT_FAILURE, "Cannot init routing table\n");
1070
1071 check_all_ports_link_status(enabled_port_mask);
1072
1073 /* launch per-lcore init on every lcore */
1074 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MAIN);
1075 RTE_LCORE_FOREACH_WORKER(lcore_id) {
1076 if (rte_eal_wait_lcore(lcore_id) < 0)
1077 return -1;
1078 }
1079
1080 /* clean up the EAL */
1081 rte_eal_cleanup();
1082
1083 return 0;
1084 }
1085