]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | /* SPDX-License-Identifier: BSD-3-Clause |
2 | * Copyright(c) 2010-2014 Intel Corporation | |
7c673cae FG |
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 <string.h> | |
11 | #include <sys/queue.h> | |
12 | #include <stdarg.h> | |
13 | #include <errno.h> | |
14 | #include <getopt.h> | |
15 | ||
16 | #include <rte_common.h> | |
17 | #include <rte_byteorder.h> | |
18 | #include <rte_log.h> | |
19 | #include <rte_memory.h> | |
20 | #include <rte_memcpy.h> | |
7c673cae | 21 | #include <rte_eal.h> |
7c673cae FG |
22 | #include <rte_launch.h> |
23 | #include <rte_atomic.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> | |
7c673cae FG |
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_malloc.h> | |
37 | #include <rte_fbk_hash.h> | |
38 | #include <rte_ip.h> | |
39 | ||
40 | #define RTE_LOGTYPE_IPv4_MULTICAST RTE_LOGTYPE_USER1 | |
41 | ||
42 | #define MAX_PORTS 16 | |
43 | ||
44 | #define MCAST_CLONE_PORTS 2 | |
45 | #define MCAST_CLONE_SEGS 2 | |
46 | ||
47 | #define PKT_MBUF_DATA_SIZE RTE_MBUF_DEFAULT_BUF_SIZE | |
48 | #define NB_PKT_MBUF 8192 | |
49 | ||
50 | #define HDR_MBUF_DATA_SIZE (2 * RTE_PKTMBUF_HEADROOM) | |
51 | #define NB_HDR_MBUF (NB_PKT_MBUF * MAX_PORTS) | |
52 | ||
53 | #define NB_CLONE_MBUF (NB_PKT_MBUF * MCAST_CLONE_PORTS * MCAST_CLONE_SEGS * 2) | |
54 | ||
55 | /* allow max jumbo frame 9.5 KB */ | |
56 | #define JUMBO_FRAME_MAX_SIZE 0x2600 | |
57 | ||
58 | #define MAX_PKT_BURST 32 | |
59 | #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */ | |
60 | ||
61 | /* Configure how many packets ahead to prefetch, when reading packets */ | |
62 | #define PREFETCH_OFFSET 3 | |
63 | ||
64 | /* | |
65 | * Construct Ethernet multicast address from IPv4 multicast address. | |
66 | * Citing RFC 1112, section 6.4: | |
67 | * "An IP host group address is mapped to an Ethernet multicast address | |
68 | * by placing the low-order 23-bits of the IP address into the low-order | |
69 | * 23 bits of the Ethernet multicast address 01-00-5E-00-00-00 (hex)." | |
70 | */ | |
71 | #define ETHER_ADDR_FOR_IPV4_MCAST(x) \ | |
72 | (rte_cpu_to_be_64(0x01005e000000ULL | ((x) & 0x7fffff)) >> 16) | |
73 | ||
74 | /* | |
75 | * Configurable number of RX/TX ring descriptors | |
76 | */ | |
9f95a23c TL |
77 | #define RTE_TEST_RX_DESC_DEFAULT 1024 |
78 | #define RTE_TEST_TX_DESC_DEFAULT 1024 | |
7c673cae FG |
79 | static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT; |
80 | static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT; | |
81 | ||
82 | /* ethernet addresses of ports */ | |
83 | static struct ether_addr ports_eth_addr[MAX_PORTS]; | |
84 | ||
85 | /* mask of enabled ports */ | |
86 | static uint32_t enabled_port_mask = 0; | |
87 | ||
9f95a23c | 88 | static uint16_t nb_ports; |
7c673cae FG |
89 | |
90 | static int rx_queue_per_lcore = 1; | |
91 | ||
92 | struct mbuf_table { | |
93 | uint16_t len; | |
94 | struct rte_mbuf *m_table[MAX_PKT_BURST]; | |
95 | }; | |
96 | ||
97 | #define MAX_RX_QUEUE_PER_LCORE 16 | |
98 | #define MAX_TX_QUEUE_PER_PORT 16 | |
99 | struct lcore_queue_conf { | |
100 | uint64_t tx_tsc; | |
101 | uint16_t n_rx_queue; | |
102 | uint8_t rx_queue_list[MAX_RX_QUEUE_PER_LCORE]; | |
103 | uint16_t tx_queue_id[MAX_PORTS]; | |
104 | struct mbuf_table tx_mbufs[MAX_PORTS]; | |
105 | } __rte_cache_aligned; | |
106 | static struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE]; | |
107 | ||
11fdf7f2 | 108 | static struct rte_eth_conf port_conf = { |
7c673cae FG |
109 | .rxmode = { |
110 | .max_rx_pkt_len = JUMBO_FRAME_MAX_SIZE, | |
111 | .split_hdr_size = 0, | |
9f95a23c | 112 | .offloads = DEV_RX_OFFLOAD_JUMBO_FRAME, |
7c673cae FG |
113 | }, |
114 | .txmode = { | |
115 | .mq_mode = ETH_MQ_TX_NONE, | |
9f95a23c | 116 | .offloads = DEV_TX_OFFLOAD_MULTI_SEGS, |
7c673cae FG |
117 | }, |
118 | }; | |
119 | ||
120 | static struct rte_mempool *packet_pool, *header_pool, *clone_pool; | |
121 | ||
122 | ||
123 | /* Multicast */ | |
124 | static struct rte_fbk_hash_params mcast_hash_params = { | |
125 | .name = "MCAST_HASH", | |
126 | .entries = 1024, | |
127 | .entries_per_bucket = 4, | |
128 | .socket_id = 0, | |
129 | .hash_func = NULL, | |
130 | .init_val = 0, | |
131 | }; | |
132 | ||
133 | struct rte_fbk_hash_table *mcast_hash = NULL; | |
134 | ||
135 | struct mcast_group_params { | |
136 | uint32_t ip; | |
137 | uint16_t port_mask; | |
138 | }; | |
139 | ||
140 | static struct mcast_group_params mcast_group_table[] = { | |
141 | {IPv4(224,0,0,101), 0x1}, | |
142 | {IPv4(224,0,0,102), 0x2}, | |
143 | {IPv4(224,0,0,103), 0x3}, | |
144 | {IPv4(224,0,0,104), 0x4}, | |
145 | {IPv4(224,0,0,105), 0x5}, | |
146 | {IPv4(224,0,0,106), 0x6}, | |
147 | {IPv4(224,0,0,107), 0x7}, | |
148 | {IPv4(224,0,0,108), 0x8}, | |
149 | {IPv4(224,0,0,109), 0x9}, | |
150 | {IPv4(224,0,0,110), 0xA}, | |
151 | {IPv4(224,0,0,111), 0xB}, | |
152 | {IPv4(224,0,0,112), 0xC}, | |
153 | {IPv4(224,0,0,113), 0xD}, | |
154 | {IPv4(224,0,0,114), 0xE}, | |
155 | {IPv4(224,0,0,115), 0xF}, | |
156 | }; | |
157 | ||
158 | #define N_MCAST_GROUPS \ | |
159 | (sizeof (mcast_group_table) / sizeof (mcast_group_table[0])) | |
160 | ||
161 | ||
162 | /* Send burst of packets on an output interface */ | |
163 | static void | |
9f95a23c | 164 | send_burst(struct lcore_queue_conf *qconf, uint16_t port) |
7c673cae FG |
165 | { |
166 | struct rte_mbuf **m_table; | |
167 | uint16_t n, queueid; | |
168 | int ret; | |
169 | ||
170 | queueid = qconf->tx_queue_id[port]; | |
171 | m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table; | |
172 | n = qconf->tx_mbufs[port].len; | |
173 | ||
174 | ret = rte_eth_tx_burst(port, queueid, m_table, n); | |
175 | while (unlikely (ret < n)) { | |
176 | rte_pktmbuf_free(m_table[ret]); | |
177 | ret++; | |
178 | } | |
179 | ||
180 | qconf->tx_mbufs[port].len = 0; | |
181 | } | |
182 | ||
183 | /* Get number of bits set. */ | |
184 | static inline uint32_t | |
185 | bitcnt(uint32_t v) | |
186 | { | |
187 | uint32_t n; | |
188 | ||
189 | for (n = 0; v != 0; v &= v - 1, n++) | |
190 | ; | |
191 | ||
192 | return n; | |
193 | } | |
194 | ||
195 | /** | |
196 | * Create the output multicast packet based on the given input packet. | |
197 | * There are two approaches for creating outgoing packet, though both | |
198 | * are based on data zero-copy idea, they differ in few details: | |
199 | * First one creates a clone of the input packet, e.g - walk though all | |
200 | * segments of the input packet, and for each of them create a new packet | |
201 | * mbuf and attach that new mbuf to the segment (refer to rte_pktmbuf_clone() | |
202 | * for more details). Then new mbuf is allocated for the packet header | |
203 | * and is prepended to the 'clone' mbuf. | |
204 | * Second approach doesn't make a clone, it just increment refcnt for all | |
205 | * input packet segments. Then it allocates new mbuf for the packet header | |
206 | * and prepends it to the input packet. | |
207 | * Basically first approach reuses only input packet's data, but creates | |
208 | * it's own copy of packet's metadata. Second approach reuses both input's | |
209 | * packet data and metadata. | |
210 | * The advantage of first approach - is that each outgoing packet has it's | |
211 | * own copy of metadata, so we can safely modify data pointer of the | |
212 | * input packet. That allows us to skip creation if the output packet for | |
213 | * the last destination port, but instead modify input packet's header inplace, | |
214 | * e.g: for N destination ports we need to invoke mcast_out_pkt (N-1) times. | |
215 | * The advantage of second approach - less work for each outgoing packet, | |
216 | * e.g: we skip "clone" operation completely. Though it comes with a price - | |
217 | * input packet's metadata has to be intact. So for N destination ports we | |
218 | * need to invoke mcast_out_pkt N times. | |
219 | * So for small number of outgoing ports (and segments in the input packet) | |
220 | * first approach will be faster. | |
221 | * As number of outgoing ports (and/or input segments) will grow, | |
222 | * second way will become more preferable. | |
223 | * | |
224 | * @param pkt | |
225 | * Input packet mbuf. | |
226 | * @param use_clone | |
227 | * Control which of the two approaches described above should be used: | |
228 | * - 0 - use second approach: | |
229 | * Don't "clone" input packet. | |
230 | * Prepend new header directly to the input packet | |
231 | * - 1 - use first approach: | |
232 | * Make a "clone" of input packet first. | |
233 | * Prepend new header to the clone of the input packet | |
234 | * @return | |
235 | * - The pointer to the new outgoing packet. | |
236 | * - NULL if operation failed. | |
237 | */ | |
238 | static inline struct rte_mbuf * | |
239 | mcast_out_pkt(struct rte_mbuf *pkt, int use_clone) | |
240 | { | |
241 | struct rte_mbuf *hdr; | |
242 | ||
243 | /* Create new mbuf for the header. */ | |
244 | if (unlikely ((hdr = rte_pktmbuf_alloc(header_pool)) == NULL)) | |
245 | return NULL; | |
246 | ||
247 | /* If requested, then make a new clone packet. */ | |
248 | if (use_clone != 0 && | |
249 | unlikely ((pkt = rte_pktmbuf_clone(pkt, clone_pool)) == NULL)) { | |
250 | rte_pktmbuf_free(hdr); | |
251 | return NULL; | |
252 | } | |
253 | ||
254 | /* prepend new header */ | |
255 | hdr->next = pkt; | |
256 | ||
7c673cae FG |
257 | /* update header's fields */ |
258 | hdr->pkt_len = (uint16_t)(hdr->data_len + pkt->pkt_len); | |
9f95a23c | 259 | hdr->nb_segs = pkt->nb_segs + 1; |
7c673cae FG |
260 | |
261 | __rte_mbuf_sanity_check(hdr, 1); | |
262 | return hdr; | |
263 | } | |
264 | ||
265 | /* | |
266 | * Write new Ethernet header to the outgoing packet, | |
267 | * and put it into the outgoing queue for the given port. | |
268 | */ | |
269 | static inline void | |
270 | mcast_send_pkt(struct rte_mbuf *pkt, struct ether_addr *dest_addr, | |
9f95a23c | 271 | struct lcore_queue_conf *qconf, uint16_t port) |
7c673cae FG |
272 | { |
273 | struct ether_hdr *ethdr; | |
274 | uint16_t len; | |
275 | ||
276 | /* Construct Ethernet header. */ | |
277 | ethdr = (struct ether_hdr *)rte_pktmbuf_prepend(pkt, (uint16_t)sizeof(*ethdr)); | |
278 | RTE_ASSERT(ethdr != NULL); | |
279 | ||
280 | ether_addr_copy(dest_addr, ðdr->d_addr); | |
281 | ether_addr_copy(&ports_eth_addr[port], ðdr->s_addr); | |
282 | ethdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv4); | |
283 | ||
284 | /* Put new packet into the output queue */ | |
285 | len = qconf->tx_mbufs[port].len; | |
286 | qconf->tx_mbufs[port].m_table[len] = pkt; | |
287 | qconf->tx_mbufs[port].len = ++len; | |
288 | ||
289 | /* Transmit packets */ | |
290 | if (unlikely(MAX_PKT_BURST == len)) | |
291 | send_burst(qconf, port); | |
292 | } | |
293 | ||
294 | /* Multicast forward of the input packet */ | |
295 | static inline void | |
296 | mcast_forward(struct rte_mbuf *m, struct lcore_queue_conf *qconf) | |
297 | { | |
298 | struct rte_mbuf *mc; | |
299 | struct ipv4_hdr *iphdr; | |
300 | uint32_t dest_addr, port_mask, port_num, use_clone; | |
301 | int32_t hash; | |
9f95a23c | 302 | uint16_t port; |
7c673cae FG |
303 | union { |
304 | uint64_t as_int; | |
305 | struct ether_addr as_addr; | |
306 | } dst_eth_addr; | |
307 | ||
308 | /* Remove the Ethernet header from the input packet */ | |
309 | iphdr = (struct ipv4_hdr *)rte_pktmbuf_adj(m, (uint16_t)sizeof(struct ether_hdr)); | |
310 | RTE_ASSERT(iphdr != NULL); | |
311 | ||
312 | dest_addr = rte_be_to_cpu_32(iphdr->dst_addr); | |
313 | ||
314 | /* | |
315 | * Check that it is a valid multicast address and | |
316 | * we have some active ports assigned to it. | |
317 | */ | |
318 | if(!IS_IPV4_MCAST(dest_addr) || | |
319 | (hash = rte_fbk_hash_lookup(mcast_hash, dest_addr)) <= 0 || | |
320 | (port_mask = hash & enabled_port_mask) == 0) { | |
321 | rte_pktmbuf_free(m); | |
322 | return; | |
323 | } | |
324 | ||
325 | /* Calculate number of destination ports. */ | |
326 | port_num = bitcnt(port_mask); | |
327 | ||
328 | /* Should we use rte_pktmbuf_clone() or not. */ | |
329 | use_clone = (port_num <= MCAST_CLONE_PORTS && | |
330 | m->nb_segs <= MCAST_CLONE_SEGS); | |
331 | ||
332 | /* Mark all packet's segments as referenced port_num times */ | |
333 | if (use_clone == 0) | |
334 | rte_pktmbuf_refcnt_update(m, (uint16_t)port_num); | |
335 | ||
336 | /* construct destination ethernet address */ | |
337 | dst_eth_addr.as_int = ETHER_ADDR_FOR_IPV4_MCAST(dest_addr); | |
338 | ||
339 | for (port = 0; use_clone != port_mask; port_mask >>= 1, port++) { | |
340 | ||
341 | /* Prepare output packet and send it out. */ | |
342 | if ((port_mask & 1) != 0) { | |
343 | if (likely ((mc = mcast_out_pkt(m, use_clone)) != NULL)) | |
344 | mcast_send_pkt(mc, &dst_eth_addr.as_addr, | |
345 | qconf, port); | |
346 | else if (use_clone == 0) | |
347 | rte_pktmbuf_free(m); | |
348 | } | |
349 | } | |
350 | ||
351 | /* | |
352 | * If we making clone packets, then, for the last destination port, | |
353 | * we can overwrite input packet's metadata. | |
354 | */ | |
355 | if (use_clone != 0) | |
356 | mcast_send_pkt(m, &dst_eth_addr.as_addr, qconf, port); | |
357 | else | |
358 | rte_pktmbuf_free(m); | |
359 | } | |
360 | ||
361 | /* Send burst of outgoing packet, if timeout expires. */ | |
362 | static inline void | |
363 | send_timeout_burst(struct lcore_queue_conf *qconf) | |
364 | { | |
365 | uint64_t cur_tsc; | |
9f95a23c | 366 | uint16_t portid; |
7c673cae FG |
367 | const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US; |
368 | ||
369 | cur_tsc = rte_rdtsc(); | |
370 | if (likely (cur_tsc < qconf->tx_tsc + drain_tsc)) | |
371 | return; | |
372 | ||
373 | for (portid = 0; portid < MAX_PORTS; portid++) { | |
374 | if (qconf->tx_mbufs[portid].len != 0) | |
375 | send_burst(qconf, portid); | |
376 | } | |
377 | qconf->tx_tsc = cur_tsc; | |
378 | } | |
379 | ||
380 | /* main processing loop */ | |
381 | static int | |
382 | main_loop(__rte_unused void *dummy) | |
383 | { | |
384 | struct rte_mbuf *pkts_burst[MAX_PKT_BURST]; | |
385 | unsigned lcore_id; | |
386 | int i, j, nb_rx; | |
9f95a23c | 387 | uint16_t portid; |
7c673cae FG |
388 | struct lcore_queue_conf *qconf; |
389 | ||
390 | lcore_id = rte_lcore_id(); | |
391 | qconf = &lcore_queue_conf[lcore_id]; | |
392 | ||
393 | ||
394 | if (qconf->n_rx_queue == 0) { | |
395 | RTE_LOG(INFO, IPv4_MULTICAST, "lcore %u has nothing to do\n", | |
396 | lcore_id); | |
397 | return 0; | |
398 | } | |
399 | ||
400 | RTE_LOG(INFO, IPv4_MULTICAST, "entering main loop on lcore %u\n", | |
401 | lcore_id); | |
402 | ||
403 | for (i = 0; i < qconf->n_rx_queue; i++) { | |
404 | ||
405 | portid = qconf->rx_queue_list[i]; | |
406 | RTE_LOG(INFO, IPv4_MULTICAST, " -- lcoreid=%u portid=%d\n", | |
9f95a23c | 407 | lcore_id, portid); |
7c673cae FG |
408 | } |
409 | ||
410 | while (1) { | |
411 | ||
412 | /* | |
413 | * Read packet from RX queues | |
414 | */ | |
415 | for (i = 0; i < qconf->n_rx_queue; i++) { | |
416 | ||
417 | portid = qconf->rx_queue_list[i]; | |
418 | nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst, | |
419 | MAX_PKT_BURST); | |
420 | ||
421 | /* Prefetch first packets */ | |
422 | for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) { | |
423 | rte_prefetch0(rte_pktmbuf_mtod( | |
424 | pkts_burst[j], void *)); | |
425 | } | |
426 | ||
427 | /* Prefetch and forward already prefetched packets */ | |
428 | for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) { | |
429 | rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[ | |
430 | j + PREFETCH_OFFSET], void *)); | |
431 | mcast_forward(pkts_burst[j], qconf); | |
432 | } | |
433 | ||
434 | /* Forward remaining prefetched packets */ | |
435 | for (; j < nb_rx; j++) { | |
436 | mcast_forward(pkts_burst[j], qconf); | |
437 | } | |
438 | } | |
439 | ||
440 | /* Send out packets from TX queues */ | |
441 | send_timeout_burst(qconf); | |
442 | } | |
443 | } | |
444 | ||
445 | /* display usage */ | |
446 | static void | |
447 | print_usage(const char *prgname) | |
448 | { | |
449 | printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n" | |
450 | " -p PORTMASK: hexadecimal bitmask of ports to configure\n" | |
451 | " -q NQ: number of queue (=ports) per lcore (default is 1)\n", | |
452 | prgname); | |
453 | } | |
454 | ||
455 | static uint32_t | |
456 | parse_portmask(const char *portmask) | |
457 | { | |
458 | char *end = NULL; | |
459 | unsigned long pm; | |
460 | ||
461 | /* parse hexadecimal string */ | |
462 | pm = strtoul(portmask, &end, 16); | |
463 | if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0')) | |
464 | return 0; | |
465 | ||
466 | return (uint32_t)pm; | |
467 | } | |
468 | ||
469 | static int | |
470 | parse_nqueue(const char *q_arg) | |
471 | { | |
472 | char *end = NULL; | |
473 | unsigned long n; | |
474 | ||
475 | /* parse numerical string */ | |
476 | errno = 0; | |
477 | n = strtoul(q_arg, &end, 0); | |
478 | if (errno != 0 || end == NULL || *end != '\0' || | |
479 | n == 0 || n >= MAX_RX_QUEUE_PER_LCORE) | |
480 | return -1; | |
481 | ||
482 | return n; | |
483 | } | |
484 | ||
485 | /* Parse the argument given in the command line of the application */ | |
486 | static int | |
487 | parse_args(int argc, char **argv) | |
488 | { | |
489 | int opt, ret; | |
490 | char **argvopt; | |
491 | int option_index; | |
492 | char *prgname = argv[0]; | |
493 | static struct option lgopts[] = { | |
494 | {NULL, 0, 0, 0} | |
495 | }; | |
496 | ||
497 | argvopt = argv; | |
498 | ||
499 | while ((opt = getopt_long(argc, argvopt, "p:q:", | |
500 | lgopts, &option_index)) != EOF) { | |
501 | ||
502 | switch (opt) { | |
503 | /* portmask */ | |
504 | case 'p': | |
505 | enabled_port_mask = parse_portmask(optarg); | |
506 | if (enabled_port_mask == 0) { | |
507 | printf("invalid portmask\n"); | |
508 | print_usage(prgname); | |
509 | return -1; | |
510 | } | |
511 | break; | |
512 | ||
513 | /* nqueue */ | |
514 | case 'q': | |
515 | rx_queue_per_lcore = parse_nqueue(optarg); | |
516 | if (rx_queue_per_lcore < 0) { | |
517 | printf("invalid queue number\n"); | |
518 | print_usage(prgname); | |
519 | return -1; | |
520 | } | |
521 | break; | |
522 | ||
523 | default: | |
524 | print_usage(prgname); | |
525 | return -1; | |
526 | } | |
527 | } | |
528 | ||
529 | if (optind >= 0) | |
530 | argv[optind-1] = prgname; | |
531 | ||
532 | ret = optind-1; | |
11fdf7f2 | 533 | optind = 1; /* reset getopt lib */ |
7c673cae FG |
534 | return ret; |
535 | } | |
536 | ||
537 | static void | |
538 | print_ethaddr(const char *name, struct ether_addr *eth_addr) | |
539 | { | |
540 | char buf[ETHER_ADDR_FMT_SIZE]; | |
541 | ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr); | |
542 | printf("%s%s", name, buf); | |
543 | } | |
544 | ||
545 | static int | |
546 | init_mcast_hash(void) | |
547 | { | |
548 | uint32_t i; | |
549 | ||
550 | mcast_hash_params.socket_id = rte_socket_id(); | |
551 | mcast_hash = rte_fbk_hash_create(&mcast_hash_params); | |
552 | if (mcast_hash == NULL){ | |
553 | return -1; | |
554 | } | |
555 | ||
556 | for (i = 0; i < N_MCAST_GROUPS; i ++){ | |
557 | if (rte_fbk_hash_add_key(mcast_hash, | |
558 | mcast_group_table[i].ip, | |
559 | mcast_group_table[i].port_mask) < 0) { | |
560 | return -1; | |
561 | } | |
562 | } | |
563 | ||
564 | return 0; | |
565 | } | |
566 | ||
567 | /* Check the link status of all ports in up to 9s, and print them finally */ | |
568 | static void | |
9f95a23c | 569 | check_all_ports_link_status(uint32_t port_mask) |
7c673cae FG |
570 | { |
571 | #define CHECK_INTERVAL 100 /* 100ms */ | |
572 | #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */ | |
9f95a23c TL |
573 | uint16_t portid; |
574 | uint8_t count, all_ports_up, print_flag = 0; | |
7c673cae FG |
575 | struct rte_eth_link link; |
576 | ||
577 | printf("\nChecking link status"); | |
578 | fflush(stdout); | |
579 | for (count = 0; count <= MAX_CHECK_TIME; count++) { | |
580 | all_ports_up = 1; | |
9f95a23c | 581 | RTE_ETH_FOREACH_DEV(portid) { |
7c673cae FG |
582 | if ((port_mask & (1 << portid)) == 0) |
583 | continue; | |
584 | memset(&link, 0, sizeof(link)); | |
585 | rte_eth_link_get_nowait(portid, &link); | |
586 | /* print link status if flag set */ | |
587 | if (print_flag == 1) { | |
588 | if (link.link_status) | |
9f95a23c TL |
589 | printf( |
590 | "Port%d Link Up. Speed %u Mbps - %s\n", | |
591 | portid, link.link_speed, | |
7c673cae FG |
592 | (link.link_duplex == ETH_LINK_FULL_DUPLEX) ? |
593 | ("full-duplex") : ("half-duplex\n")); | |
594 | else | |
9f95a23c | 595 | printf("Port %d Link Down\n", portid); |
7c673cae FG |
596 | continue; |
597 | } | |
598 | /* clear all_ports_up flag if any link down */ | |
599 | if (link.link_status == ETH_LINK_DOWN) { | |
600 | all_ports_up = 0; | |
601 | break; | |
602 | } | |
603 | } | |
604 | /* after finally printing all link status, get out */ | |
605 | if (print_flag == 1) | |
606 | break; | |
607 | ||
608 | if (all_ports_up == 0) { | |
609 | printf("."); | |
610 | fflush(stdout); | |
611 | rte_delay_ms(CHECK_INTERVAL); | |
612 | } | |
613 | ||
614 | /* set the print_flag if all ports up or timeout */ | |
615 | if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) { | |
616 | print_flag = 1; | |
617 | printf("done\n"); | |
618 | } | |
619 | } | |
620 | } | |
621 | ||
622 | int | |
623 | main(int argc, char **argv) | |
624 | { | |
625 | struct lcore_queue_conf *qconf; | |
626 | struct rte_eth_dev_info dev_info; | |
627 | struct rte_eth_txconf *txconf; | |
628 | int ret; | |
629 | uint16_t queueid; | |
630 | unsigned lcore_id = 0, rx_lcore_id = 0; | |
631 | uint32_t n_tx_queue, nb_lcores; | |
9f95a23c | 632 | uint16_t portid; |
7c673cae FG |
633 | |
634 | /* init EAL */ | |
635 | ret = rte_eal_init(argc, argv); | |
636 | if (ret < 0) | |
637 | rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n"); | |
638 | argc -= ret; | |
639 | argv += ret; | |
640 | ||
641 | /* parse application arguments (after the EAL ones) */ | |
642 | ret = parse_args(argc, argv); | |
643 | if (ret < 0) | |
644 | rte_exit(EXIT_FAILURE, "Invalid IPV4_MULTICAST parameters\n"); | |
645 | ||
646 | /* create the mbuf pools */ | |
647 | packet_pool = rte_pktmbuf_pool_create("packet_pool", NB_PKT_MBUF, 32, | |
648 | 0, PKT_MBUF_DATA_SIZE, rte_socket_id()); | |
649 | ||
650 | if (packet_pool == NULL) | |
651 | rte_exit(EXIT_FAILURE, "Cannot init packet mbuf pool\n"); | |
652 | ||
653 | header_pool = rte_pktmbuf_pool_create("header_pool", NB_HDR_MBUF, 32, | |
654 | 0, HDR_MBUF_DATA_SIZE, rte_socket_id()); | |
655 | ||
656 | if (header_pool == NULL) | |
657 | rte_exit(EXIT_FAILURE, "Cannot init header mbuf pool\n"); | |
658 | ||
659 | clone_pool = rte_pktmbuf_pool_create("clone_pool", NB_CLONE_MBUF, 32, | |
660 | 0, 0, rte_socket_id()); | |
661 | ||
662 | if (clone_pool == NULL) | |
663 | rte_exit(EXIT_FAILURE, "Cannot init clone mbuf pool\n"); | |
664 | ||
9f95a23c | 665 | nb_ports = rte_eth_dev_count_avail(); |
7c673cae FG |
666 | if (nb_ports == 0) |
667 | rte_exit(EXIT_FAILURE, "No physical ports!\n"); | |
668 | if (nb_ports > MAX_PORTS) | |
669 | nb_ports = MAX_PORTS; | |
670 | ||
671 | nb_lcores = rte_lcore_count(); | |
672 | ||
673 | /* initialize all ports */ | |
9f95a23c TL |
674 | RTE_ETH_FOREACH_DEV(portid) { |
675 | struct rte_eth_rxconf rxq_conf; | |
676 | struct rte_eth_conf local_port_conf = port_conf; | |
677 | ||
7c673cae FG |
678 | /* skip ports that are not enabled */ |
679 | if ((enabled_port_mask & (1 << portid)) == 0) { | |
680 | printf("Skipping disabled port %d\n", portid); | |
681 | continue; | |
682 | } | |
683 | ||
684 | qconf = &lcore_queue_conf[rx_lcore_id]; | |
685 | ||
11fdf7f2 TL |
686 | /* limit the frame size to the maximum supported by NIC */ |
687 | rte_eth_dev_info_get(portid, &dev_info); | |
9f95a23c TL |
688 | local_port_conf.rxmode.max_rx_pkt_len = RTE_MIN( |
689 | dev_info.max_rx_pktlen, | |
690 | local_port_conf.rxmode.max_rx_pkt_len); | |
11fdf7f2 | 691 | |
7c673cae FG |
692 | /* get the lcore_id for this port */ |
693 | while (rte_lcore_is_enabled(rx_lcore_id) == 0 || | |
694 | qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) { | |
695 | ||
696 | rx_lcore_id ++; | |
697 | qconf = &lcore_queue_conf[rx_lcore_id]; | |
698 | ||
699 | if (rx_lcore_id >= RTE_MAX_LCORE) | |
700 | rte_exit(EXIT_FAILURE, "Not enough cores\n"); | |
701 | } | |
702 | qconf->rx_queue_list[qconf->n_rx_queue] = portid; | |
703 | qconf->n_rx_queue++; | |
704 | ||
705 | /* init port */ | |
706 | printf("Initializing port %d on lcore %u... ", portid, | |
707 | rx_lcore_id); | |
708 | fflush(stdout); | |
709 | ||
710 | n_tx_queue = nb_lcores; | |
711 | if (n_tx_queue > MAX_TX_QUEUE_PER_PORT) | |
712 | n_tx_queue = MAX_TX_QUEUE_PER_PORT; | |
9f95a23c | 713 | |
7c673cae | 714 | ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue, |
9f95a23c | 715 | &local_port_conf); |
7c673cae FG |
716 | if (ret < 0) |
717 | rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n", | |
718 | ret, portid); | |
719 | ||
9f95a23c TL |
720 | ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, |
721 | &nb_txd); | |
722 | if (ret < 0) | |
723 | rte_exit(EXIT_FAILURE, | |
724 | "Cannot adjust number of descriptors: err=%d, port=%d\n", | |
725 | ret, portid); | |
726 | ||
7c673cae FG |
727 | rte_eth_macaddr_get(portid, &ports_eth_addr[portid]); |
728 | print_ethaddr(" Address:", &ports_eth_addr[portid]); | |
729 | printf(", "); | |
730 | ||
731 | /* init one RX queue */ | |
732 | queueid = 0; | |
733 | printf("rxq=%hu ", queueid); | |
734 | fflush(stdout); | |
9f95a23c TL |
735 | rxq_conf = dev_info.default_rxconf; |
736 | rxq_conf.offloads = local_port_conf.rxmode.offloads; | |
7c673cae FG |
737 | ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd, |
738 | rte_eth_dev_socket_id(portid), | |
9f95a23c | 739 | &rxq_conf, |
7c673cae FG |
740 | packet_pool); |
741 | if (ret < 0) | |
742 | rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, port=%d\n", | |
743 | ret, portid); | |
744 | ||
745 | /* init one TX queue per couple (lcore,port) */ | |
746 | queueid = 0; | |
747 | ||
748 | RTE_LCORE_FOREACH(lcore_id) { | |
749 | if (rte_lcore_is_enabled(lcore_id) == 0) | |
750 | continue; | |
751 | printf("txq=%u,%hu ", lcore_id, queueid); | |
752 | fflush(stdout); | |
753 | ||
7c673cae | 754 | txconf = &dev_info.default_txconf; |
9f95a23c | 755 | txconf->offloads = local_port_conf.txmode.offloads; |
7c673cae FG |
756 | ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd, |
757 | rte_lcore_to_socket_id(lcore_id), txconf); | |
758 | if (ret < 0) | |
759 | rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, " | |
760 | "port=%d\n", ret, portid); | |
761 | ||
762 | qconf = &lcore_queue_conf[lcore_id]; | |
763 | qconf->tx_queue_id[portid] = queueid; | |
764 | queueid++; | |
765 | } | |
9f95a23c | 766 | rte_eth_allmulticast_enable(portid); |
7c673cae FG |
767 | /* Start device */ |
768 | ret = rte_eth_dev_start(portid); | |
769 | if (ret < 0) | |
770 | rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n", | |
771 | ret, portid); | |
772 | ||
773 | printf("done:\n"); | |
774 | } | |
775 | ||
9f95a23c | 776 | check_all_ports_link_status(enabled_port_mask); |
7c673cae FG |
777 | |
778 | /* initialize the multicast hash */ | |
779 | int retval = init_mcast_hash(); | |
780 | if (retval != 0) | |
781 | rte_exit(EXIT_FAILURE, "Cannot build the multicast hash\n"); | |
782 | ||
783 | /* launch per-lcore init on every lcore */ | |
784 | rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER); | |
785 | RTE_LCORE_FOREACH_SLAVE(lcore_id) { | |
786 | if (rte_eal_wait_lcore(lcore_id) < 0) | |
787 | return -1; | |
788 | } | |
789 | ||
790 | return 0; | |
791 | } |