13998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
23998e2a0SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation
3af75078fSIntel */
4af75078fSIntel
5af75078fSIntel #include <stdio.h>
6af75078fSIntel #include <stdlib.h>
7af75078fSIntel #include <stdint.h>
8af75078fSIntel #include <inttypes.h>
9af75078fSIntel #include <sys/types.h>
10af75078fSIntel #include <string.h>
11af75078fSIntel #include <sys/queue.h>
12af75078fSIntel #include <stdarg.h>
13af75078fSIntel #include <errno.h>
14af75078fSIntel #include <getopt.h>
15af75078fSIntel
16af75078fSIntel #include <rte_common.h>
17af75078fSIntel #include <rte_byteorder.h>
18af75078fSIntel #include <rte_log.h>
19af75078fSIntel #include <rte_memory.h>
20af75078fSIntel #include <rte_memcpy.h>
21af75078fSIntel #include <rte_eal.h>
22af75078fSIntel #include <rte_launch.h>
23af75078fSIntel #include <rte_cycles.h>
24af75078fSIntel #include <rte_prefetch.h>
25af75078fSIntel #include <rte_lcore.h>
26af75078fSIntel #include <rte_per_lcore.h>
27af75078fSIntel #include <rte_branch_prediction.h>
28af75078fSIntel #include <rte_interrupts.h>
29af75078fSIntel #include <rte_random.h>
30af75078fSIntel #include <rte_debug.h>
31af75078fSIntel #include <rte_ether.h>
32af75078fSIntel #include <rte_ethdev.h>
33af75078fSIntel #include <rte_mempool.h>
34af75078fSIntel #include <rte_mbuf.h>
35af75078fSIntel #include <rte_malloc.h>
36af75078fSIntel #include <rte_fbk_hash.h>
37af75078fSIntel #include <rte_ip.h>
38af75078fSIntel
39af75078fSIntel #define RTE_LOGTYPE_IPv4_MULTICAST RTE_LOGTYPE_USER1
40af75078fSIntel
41af75078fSIntel #define MAX_PORTS 16
42af75078fSIntel
43af75078fSIntel #define MCAST_CLONE_PORTS 2
44af75078fSIntel #define MCAST_CLONE_SEGS 2
45af75078fSIntel
46824cb29cSKonstantin Ananyev #define PKT_MBUF_DATA_SIZE RTE_MBUF_DEFAULT_BUF_SIZE
47af75078fSIntel #define NB_PKT_MBUF 8192
48af75078fSIntel
49ea0c20eaSOlivier Matz #define HDR_MBUF_DATA_SIZE (2 * RTE_PKTMBUF_HEADROOM)
50af75078fSIntel #define NB_HDR_MBUF (NB_PKT_MBUF * MAX_PORTS)
51af75078fSIntel
52af75078fSIntel #define NB_CLONE_MBUF (NB_PKT_MBUF * MCAST_CLONE_PORTS * MCAST_CLONE_SEGS * 2)
53af75078fSIntel
54af75078fSIntel /* allow max jumbo frame 9.5 KB */
55af75078fSIntel #define JUMBO_FRAME_MAX_SIZE 0x2600
56af75078fSIntel
57af75078fSIntel #define MAX_PKT_BURST 32
585c95261dSIntel #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
59af75078fSIntel
60af75078fSIntel /* Configure how many packets ahead to prefetch, when reading packets */
61af75078fSIntel #define PREFETCH_OFFSET 3
62af75078fSIntel
63af75078fSIntel /*
64af75078fSIntel * Construct Ethernet multicast address from IPv4 multicast address.
65af75078fSIntel * Citing RFC 1112, section 6.4:
66af75078fSIntel * "An IP host group address is mapped to an Ethernet multicast address
67af75078fSIntel * by placing the low-order 23-bits of the IP address into the low-order
68af75078fSIntel * 23 bits of the Ethernet multicast address 01-00-5E-00-00-00 (hex)."
69af75078fSIntel */
709a212dc0SConor Fogarty
719a212dc0SConor Fogarty /* Construct Ethernet multicast address from IPv4 multicast Address. 8< */
72af75078fSIntel #define ETHER_ADDR_FOR_IPV4_MCAST(x) \
73af75078fSIntel (rte_cpu_to_be_64(0x01005e000000ULL | ((x) & 0x7fffff)) >> 16)
749a212dc0SConor Fogarty /* >8 End of Construction of multicast address from IPv4 multicast address. */
75af75078fSIntel
76af75078fSIntel /*
77af75078fSIntel * Configurable number of RX/TX ring descriptors
78af75078fSIntel */
794ed89049SDavid Marchand #define RX_DESC_DEFAULT 1024
804ed89049SDavid Marchand #define TX_DESC_DEFAULT 1024
814ed89049SDavid Marchand static uint16_t nb_rxd = RX_DESC_DEFAULT;
824ed89049SDavid Marchand static uint16_t nb_txd = TX_DESC_DEFAULT;
83af75078fSIntel
84af75078fSIntel /* ethernet addresses of ports */
856d13ea8eSOlivier Matz static struct rte_ether_addr ports_eth_addr[MAX_PORTS];
86af75078fSIntel
87af75078fSIntel /* mask of enabled ports */
88af75078fSIntel static uint32_t enabled_port_mask = 0;
89af75078fSIntel
90f8244c63SZhiyong Yang static uint16_t nb_ports;
91af75078fSIntel
92af75078fSIntel static int rx_queue_per_lcore = 1;
93af75078fSIntel
94af75078fSIntel struct mbuf_table {
95af75078fSIntel uint16_t len;
96af75078fSIntel struct rte_mbuf *m_table[MAX_PKT_BURST];
97af75078fSIntel };
98af75078fSIntel
99af75078fSIntel #define MAX_RX_QUEUE_PER_LCORE 16
100af75078fSIntel #define MAX_TX_QUEUE_PER_PORT 16
101*7e06c0deSTyler Retzlaff struct __rte_cache_aligned lcore_queue_conf {
102af75078fSIntel uint64_t tx_tsc;
103af75078fSIntel uint16_t n_rx_queue;
104af75078fSIntel uint8_t rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
105af75078fSIntel uint16_t tx_queue_id[MAX_PORTS];
106af75078fSIntel struct mbuf_table tx_mbufs[MAX_PORTS];
107*7e06c0deSTyler Retzlaff };
108af75078fSIntel static struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
109af75078fSIntel
1105e470a66SAndriy Berestovskyy static struct rte_eth_conf port_conf = {
111af75078fSIntel .rxmode = {
1121bb4a528SFerruh Yigit .mtu = JUMBO_FRAME_MAX_SIZE - RTE_ETHER_HDR_LEN -
1131bb4a528SFerruh Yigit RTE_ETHER_CRC_LEN,
114af75078fSIntel },
115af75078fSIntel .txmode = {
116295968d1SFerruh Yigit .mq_mode = RTE_ETH_MQ_TX_NONE,
117295968d1SFerruh Yigit .offloads = RTE_ETH_TX_OFFLOAD_MULTI_SEGS,
118af75078fSIntel },
119af75078fSIntel };
120af75078fSIntel
121af75078fSIntel static struct rte_mempool *packet_pool, *header_pool, *clone_pool;
122af75078fSIntel
123af75078fSIntel
124af75078fSIntel /* Multicast */
125af75078fSIntel static struct rte_fbk_hash_params mcast_hash_params = {
126af75078fSIntel .name = "MCAST_HASH",
127af75078fSIntel .entries = 1024,
128af75078fSIntel .entries_per_bucket = 4,
129e60f71ebSIntel .socket_id = 0,
130af75078fSIntel .hash_func = NULL,
131af75078fSIntel .init_val = 0,
132af75078fSIntel };
133af75078fSIntel
134af75078fSIntel struct rte_fbk_hash_table *mcast_hash = NULL;
135af75078fSIntel
136af75078fSIntel struct mcast_group_params {
137af75078fSIntel uint32_t ip;
138af75078fSIntel uint16_t port_mask;
139af75078fSIntel };
140af75078fSIntel
141af75078fSIntel static struct mcast_group_params mcast_group_table[] = {
1420c9da755SDavid Marchand {RTE_IPV4(224,0,0,101), 0x1},
1430c9da755SDavid Marchand {RTE_IPV4(224,0,0,102), 0x2},
1440c9da755SDavid Marchand {RTE_IPV4(224,0,0,103), 0x3},
1450c9da755SDavid Marchand {RTE_IPV4(224,0,0,104), 0x4},
1460c9da755SDavid Marchand {RTE_IPV4(224,0,0,105), 0x5},
1470c9da755SDavid Marchand {RTE_IPV4(224,0,0,106), 0x6},
1480c9da755SDavid Marchand {RTE_IPV4(224,0,0,107), 0x7},
1490c9da755SDavid Marchand {RTE_IPV4(224,0,0,108), 0x8},
1500c9da755SDavid Marchand {RTE_IPV4(224,0,0,109), 0x9},
1510c9da755SDavid Marchand {RTE_IPV4(224,0,0,110), 0xA},
1520c9da755SDavid Marchand {RTE_IPV4(224,0,0,111), 0xB},
1530c9da755SDavid Marchand {RTE_IPV4(224,0,0,112), 0xC},
1540c9da755SDavid Marchand {RTE_IPV4(224,0,0,113), 0xD},
1550c9da755SDavid Marchand {RTE_IPV4(224,0,0,114), 0xE},
1560c9da755SDavid Marchand {RTE_IPV4(224,0,0,115), 0xF},
157af75078fSIntel };
158af75078fSIntel
159af75078fSIntel /* Send burst of packets on an output interface */
160af75078fSIntel static void
send_burst(struct lcore_queue_conf * qconf,uint16_t port)161f8244c63SZhiyong Yang send_burst(struct lcore_queue_conf *qconf, uint16_t port)
162af75078fSIntel {
163af75078fSIntel struct rte_mbuf **m_table;
164af75078fSIntel uint16_t n, queueid;
165af75078fSIntel int ret;
166af75078fSIntel
167af75078fSIntel queueid = qconf->tx_queue_id[port];
168af75078fSIntel m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
169af75078fSIntel n = qconf->tx_mbufs[port].len;
170af75078fSIntel
171af75078fSIntel ret = rte_eth_tx_burst(port, queueid, m_table, n);
172af75078fSIntel while (unlikely (ret < n)) {
173af75078fSIntel rte_pktmbuf_free(m_table[ret]);
174af75078fSIntel ret++;
175af75078fSIntel }
176af75078fSIntel
177af75078fSIntel qconf->tx_mbufs[port].len = 0;
178af75078fSIntel }
179af75078fSIntel
1809a212dc0SConor Fogarty /* Get number of bits set. 8< */
181af75078fSIntel static inline uint32_t
bitcnt(uint32_t v)182af75078fSIntel bitcnt(uint32_t v)
183af75078fSIntel {
184af75078fSIntel uint32_t n;
185af75078fSIntel
186af75078fSIntel for (n = 0; v != 0; v &= v - 1, n++)
187af75078fSIntel ;
188af75078fSIntel
189693f715dSHuawei Xie return n;
190af75078fSIntel }
1919a212dc0SConor Fogarty /* >8 End of getting number of bits set. */
192af75078fSIntel
193af75078fSIntel /**
194af75078fSIntel * Create the output multicast packet based on the given input packet.
195af75078fSIntel * There are two approaches for creating outgoing packet, though both
196af75078fSIntel * are based on data zero-copy idea, they differ in few details:
197af75078fSIntel * First one creates a clone of the input packet, e.g - walk though all
198af75078fSIntel * segments of the input packet, and for each of them create a new packet
199af75078fSIntel * mbuf and attach that new mbuf to the segment (refer to rte_pktmbuf_clone()
200af75078fSIntel * for more details). Then new mbuf is allocated for the packet header
201af75078fSIntel * and is prepended to the 'clone' mbuf.
202af75078fSIntel * Second approach doesn't make a clone, it just increment refcnt for all
203af75078fSIntel * input packet segments. Then it allocates new mbuf for the packet header
204af75078fSIntel * and prepends it to the input packet.
205af75078fSIntel * Basically first approach reuses only input packet's data, but creates
206af75078fSIntel * it's own copy of packet's metadata. Second approach reuses both input's
207af75078fSIntel * packet data and metadata.
208af75078fSIntel * The advantage of first approach - is that each outgoing packet has it's
209af75078fSIntel * own copy of metadata, so we can safely modify data pointer of the
210af75078fSIntel * input packet. That allows us to skip creation if the output packet for
211af75078fSIntel * the last destination port, but instead modify input packet's header inplace,
212af75078fSIntel * e.g: for N destination ports we need to invoke mcast_out_pkt (N-1) times.
213af75078fSIntel * The advantage of second approach - less work for each outgoing packet,
214af75078fSIntel * e.g: we skip "clone" operation completely. Though it comes with a price -
215af75078fSIntel * input packet's metadata has to be intact. So for N destination ports we
216af75078fSIntel * need to invoke mcast_out_pkt N times.
217af75078fSIntel * So for small number of outgoing ports (and segments in the input packet)
218af75078fSIntel * first approach will be faster.
219af75078fSIntel * As number of outgoing ports (and/or input segments) will grow,
220af75078fSIntel * second way will become more preferable.
221af75078fSIntel *
222af75078fSIntel * @param pkt
223af75078fSIntel * Input packet mbuf.
224af75078fSIntel * @param use_clone
225af75078fSIntel * Control which of the two approaches described above should be used:
226af75078fSIntel * - 0 - use second approach:
227af75078fSIntel * Don't "clone" input packet.
228af75078fSIntel * Prepend new header directly to the input packet
229af75078fSIntel * - 1 - use first approach:
230af75078fSIntel * Make a "clone" of input packet first.
231af75078fSIntel * Prepend new header to the clone of the input packet
232af75078fSIntel * @return
233af75078fSIntel * - The pointer to the new outgoing packet.
234af75078fSIntel * - NULL if operation failed.
235af75078fSIntel */
2369a212dc0SConor Fogarty
2379a212dc0SConor Fogarty /* mcast_out_pkt 8< */
238af75078fSIntel static inline struct rte_mbuf *
mcast_out_pkt(struct rte_mbuf * pkt,int use_clone)239af75078fSIntel mcast_out_pkt(struct rte_mbuf *pkt, int use_clone)
240af75078fSIntel {
241af75078fSIntel struct rte_mbuf *hdr;
242af75078fSIntel
243af75078fSIntel /* Create new mbuf for the header. */
244af75078fSIntel if (unlikely ((hdr = rte_pktmbuf_alloc(header_pool)) == NULL))
245693f715dSHuawei Xie return NULL;
246af75078fSIntel
247af75078fSIntel /* If requested, then make a new clone packet. */
248af75078fSIntel if (use_clone != 0 &&
249af75078fSIntel unlikely ((pkt = rte_pktmbuf_clone(pkt, clone_pool)) == NULL)) {
250af75078fSIntel rte_pktmbuf_free(hdr);
251693f715dSHuawei Xie return NULL;
252af75078fSIntel }
253af75078fSIntel
254af75078fSIntel /* prepend new header */
255ea672a8bSOlivier Matz hdr->next = pkt;
256af75078fSIntel
257af75078fSIntel /* update header's fields */
258ea672a8bSOlivier Matz hdr->pkt_len = (uint16_t)(hdr->data_len + pkt->pkt_len);
2594c20622aSIlya V. Matveychikov hdr->nb_segs = pkt->nb_segs + 1;
260af75078fSIntel
2619aaccf1aSOlivier Matz __rte_mbuf_sanity_check(hdr, 1);
262693f715dSHuawei Xie return hdr;
263af75078fSIntel }
2649a212dc0SConor Fogarty /* >8 End of mcast_out_kt. */
265af75078fSIntel
266af75078fSIntel /*
267af75078fSIntel * Write new Ethernet header to the outgoing packet,
268af75078fSIntel * and put it into the outgoing queue for the given port.
269af75078fSIntel */
2709a212dc0SConor Fogarty
2719a212dc0SConor Fogarty /* Write new Ethernet header to outgoing packets. 8< */
272af75078fSIntel static inline void
mcast_send_pkt(struct rte_mbuf * pkt,struct rte_ether_addr * dest_addr,struct lcore_queue_conf * qconf,uint16_t port)2736d13ea8eSOlivier Matz mcast_send_pkt(struct rte_mbuf *pkt, struct rte_ether_addr *dest_addr,
274f8244c63SZhiyong Yang struct lcore_queue_conf *qconf, uint16_t port)
275af75078fSIntel {
2766d13ea8eSOlivier Matz struct rte_ether_hdr *ethdr;
277af75078fSIntel uint16_t len;
278af75078fSIntel
279af75078fSIntel /* Construct Ethernet header. */
2806d13ea8eSOlivier Matz ethdr = (struct rte_ether_hdr *)
2816d13ea8eSOlivier Matz rte_pktmbuf_prepend(pkt, (uint16_t)sizeof(*ethdr));
28250705e8eSThomas Monjalon RTE_ASSERT(ethdr != NULL);
283af75078fSIntel
28404d43857SDmitry Kozlyuk rte_ether_addr_copy(dest_addr, ðdr->dst_addr);
28504d43857SDmitry Kozlyuk rte_ether_addr_copy(&ports_eth_addr[port], ðdr->src_addr);
2860c9da755SDavid Marchand ethdr->ether_type = rte_be_to_cpu_16(RTE_ETHER_TYPE_IPV4);
287af75078fSIntel
288af75078fSIntel /* Put new packet into the output queue */
289af75078fSIntel len = qconf->tx_mbufs[port].len;
290af75078fSIntel qconf->tx_mbufs[port].m_table[len] = pkt;
291af75078fSIntel qconf->tx_mbufs[port].len = ++len;
292af75078fSIntel
293af75078fSIntel /* Transmit packets */
294af75078fSIntel if (unlikely(MAX_PKT_BURST == len))
295af75078fSIntel send_burst(qconf, port);
296af75078fSIntel }
2979a212dc0SConor Fogarty /* >8 End of writing new Ethernet headers. */
298af75078fSIntel
299af75078fSIntel /* Multicast forward of the input packet */
300af75078fSIntel static inline void
mcast_forward(struct rte_mbuf * m,struct lcore_queue_conf * qconf)301af75078fSIntel mcast_forward(struct rte_mbuf *m, struct lcore_queue_conf *qconf)
302af75078fSIntel {
303af75078fSIntel struct rte_mbuf *mc;
304a7c528e5SOlivier Matz struct rte_ipv4_hdr *iphdr;
305af75078fSIntel uint32_t dest_addr, port_mask, port_num, use_clone;
306af75078fSIntel int32_t hash;
307f8244c63SZhiyong Yang uint16_t port;
308af75078fSIntel union {
309af75078fSIntel uint64_t as_int;
3106d13ea8eSOlivier Matz struct rte_ether_addr as_addr;
311af75078fSIntel } dst_eth_addr;
312af75078fSIntel
3139a212dc0SConor Fogarty /* Remove the Ethernet header from the input packet. 8< */
314a7c528e5SOlivier Matz iphdr = (struct rte_ipv4_hdr *)
3156d13ea8eSOlivier Matz rte_pktmbuf_adj(m, (uint16_t)sizeof(struct rte_ether_hdr));
31650705e8eSThomas Monjalon RTE_ASSERT(iphdr != NULL);
317af75078fSIntel
318af75078fSIntel dest_addr = rte_be_to_cpu_32(iphdr->dst_addr);
3199a212dc0SConor Fogarty /* >8 End of removing the Ethernet header from the input packet. */
320af75078fSIntel
321af75078fSIntel /*
322af75078fSIntel * Check that it is a valid multicast address and
323af75078fSIntel * we have some active ports assigned to it.
324af75078fSIntel */
3259a212dc0SConor Fogarty
3269a212dc0SConor Fogarty /* Check valid multicast address. 8< */
32724ac604eSOlivier Matz if (!RTE_IS_IPV4_MCAST(dest_addr) ||
328af75078fSIntel (hash = rte_fbk_hash_lookup(mcast_hash, dest_addr)) <= 0 ||
329af75078fSIntel (port_mask = hash & enabled_port_mask) == 0) {
330af75078fSIntel rte_pktmbuf_free(m);
331af75078fSIntel return;
332af75078fSIntel }
3339a212dc0SConor Fogarty /* >8 End of valid multicast address check. */
334af75078fSIntel
335af75078fSIntel /* Calculate number of destination ports. */
336af75078fSIntel port_num = bitcnt(port_mask);
337af75078fSIntel
3389a212dc0SConor Fogarty /* Should we use rte_pktmbuf_clone() or not. 8< */
339af75078fSIntel use_clone = (port_num <= MCAST_CLONE_PORTS &&
340ea672a8bSOlivier Matz m->nb_segs <= MCAST_CLONE_SEGS);
3419a212dc0SConor Fogarty /* >8 End of using rte_pktmbuf_clone(). */
342af75078fSIntel
343af75078fSIntel /* Mark all packet's segments as referenced port_num times */
344af75078fSIntel if (use_clone == 0)
345af75078fSIntel rte_pktmbuf_refcnt_update(m, (uint16_t)port_num);
346af75078fSIntel
3479a212dc0SConor Fogarty /* Construct destination ethernet address. 8< */
348af75078fSIntel dst_eth_addr.as_int = ETHER_ADDR_FOR_IPV4_MCAST(dest_addr);
3499a212dc0SConor Fogarty /* >8 End of constructing destination ethernet address. */
350af75078fSIntel
3519a212dc0SConor Fogarty /* Packets dispatched to destination ports. 8< */
352af75078fSIntel for (port = 0; use_clone != port_mask; port_mask >>= 1, port++) {
353af75078fSIntel
354af75078fSIntel /* Prepare output packet and send it out. */
355af75078fSIntel if ((port_mask & 1) != 0) {
356af75078fSIntel if (likely ((mc = mcast_out_pkt(m, use_clone)) != NULL))
357af75078fSIntel mcast_send_pkt(mc, &dst_eth_addr.as_addr,
358af75078fSIntel qconf, port);
359af75078fSIntel else if (use_clone == 0)
360af75078fSIntel rte_pktmbuf_free(m);
361af75078fSIntel }
362af75078fSIntel }
3639a212dc0SConor Fogarty /* >8 End of packets dispatched to destination ports. */
364af75078fSIntel
365af75078fSIntel /*
366af75078fSIntel * If we making clone packets, then, for the last destination port,
367af75078fSIntel * we can overwrite input packet's metadata.
368af75078fSIntel */
369af75078fSIntel if (use_clone != 0)
370af75078fSIntel mcast_send_pkt(m, &dst_eth_addr.as_addr, qconf, port);
371af75078fSIntel else
372af75078fSIntel rte_pktmbuf_free(m);
373af75078fSIntel }
374af75078fSIntel
375af75078fSIntel /* Send burst of outgoing packet, if timeout expires. */
376af75078fSIntel static inline void
send_timeout_burst(struct lcore_queue_conf * qconf)377af75078fSIntel send_timeout_burst(struct lcore_queue_conf *qconf)
378af75078fSIntel {
379af75078fSIntel uint64_t cur_tsc;
380f8244c63SZhiyong Yang uint16_t portid;
3815c95261dSIntel const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
382af75078fSIntel
383af75078fSIntel cur_tsc = rte_rdtsc();
3845c95261dSIntel if (likely (cur_tsc < qconf->tx_tsc + drain_tsc))
385af75078fSIntel return;
386af75078fSIntel
387af75078fSIntel for (portid = 0; portid < MAX_PORTS; portid++) {
388af75078fSIntel if (qconf->tx_mbufs[portid].len != 0)
389af75078fSIntel send_burst(qconf, portid);
390af75078fSIntel }
391af75078fSIntel qconf->tx_tsc = cur_tsc;
392af75078fSIntel }
393af75078fSIntel
394af75078fSIntel /* main processing loop */
395cdfd5dbbSIntel static int
main_loop(__rte_unused void * dummy)396af75078fSIntel main_loop(__rte_unused void *dummy)
397af75078fSIntel {
398af75078fSIntel struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
3996441b9f6SIntel unsigned lcore_id;
400af75078fSIntel int i, j, nb_rx;
401f8244c63SZhiyong Yang uint16_t portid;
402af75078fSIntel struct lcore_queue_conf *qconf;
403af75078fSIntel
404af75078fSIntel lcore_id = rte_lcore_id();
405af75078fSIntel qconf = &lcore_queue_conf[lcore_id];
406af75078fSIntel
407af75078fSIntel
408af75078fSIntel if (qconf->n_rx_queue == 0) {
409af75078fSIntel RTE_LOG(INFO, IPv4_MULTICAST, "lcore %u has nothing to do\n",
410af75078fSIntel lcore_id);
411cdfd5dbbSIntel return 0;
412af75078fSIntel }
413af75078fSIntel
414af75078fSIntel RTE_LOG(INFO, IPv4_MULTICAST, "entering main loop on lcore %u\n",
415af75078fSIntel lcore_id);
416af75078fSIntel
417af75078fSIntel for (i = 0; i < qconf->n_rx_queue; i++) {
418af75078fSIntel
419af75078fSIntel portid = qconf->rx_queue_list[i];
420af75078fSIntel RTE_LOG(INFO, IPv4_MULTICAST, " -- lcoreid=%u portid=%d\n",
421f8244c63SZhiyong Yang lcore_id, portid);
422af75078fSIntel }
423af75078fSIntel
424af75078fSIntel while (1) {
425af75078fSIntel
426af75078fSIntel /*
427af75078fSIntel * Read packet from RX queues
428af75078fSIntel */
429af75078fSIntel for (i = 0; i < qconf->n_rx_queue; i++) {
430af75078fSIntel
431af75078fSIntel portid = qconf->rx_queue_list[i];
432af75078fSIntel nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
433af75078fSIntel MAX_PKT_BURST);
434af75078fSIntel
435af75078fSIntel /* Prefetch first packets */
436af75078fSIntel for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
437af75078fSIntel rte_prefetch0(rte_pktmbuf_mtod(
438af75078fSIntel pkts_burst[j], void *));
439af75078fSIntel }
440af75078fSIntel
441af75078fSIntel /* Prefetch and forward already prefetched packets */
442af75078fSIntel for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
443af75078fSIntel rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
444af75078fSIntel j + PREFETCH_OFFSET], void *));
445af75078fSIntel mcast_forward(pkts_burst[j], qconf);
446af75078fSIntel }
447af75078fSIntel
448af75078fSIntel /* Forward remaining prefetched packets */
449af75078fSIntel for (; j < nb_rx; j++) {
450af75078fSIntel mcast_forward(pkts_burst[j], qconf);
451af75078fSIntel }
452af75078fSIntel }
453af75078fSIntel
454af75078fSIntel /* Send out packets from TX queues */
455af75078fSIntel send_timeout_burst(qconf);
456af75078fSIntel }
457af75078fSIntel }
458af75078fSIntel
459af75078fSIntel /* display usage */
460af75078fSIntel static void
print_usage(const char * prgname)461af75078fSIntel print_usage(const char *prgname)
462af75078fSIntel {
463af75078fSIntel printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
464af75078fSIntel " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
465af75078fSIntel " -q NQ: number of queue (=ports) per lcore (default is 1)\n",
466af75078fSIntel prgname);
467af75078fSIntel }
468af75078fSIntel
469af75078fSIntel static uint32_t
parse_portmask(const char * portmask)470af75078fSIntel parse_portmask(const char *portmask)
471af75078fSIntel {
472af75078fSIntel char *end = NULL;
473af75078fSIntel unsigned long pm;
474af75078fSIntel
475af75078fSIntel /* parse hexadecimal string */
476af75078fSIntel pm = strtoul(portmask, &end, 16);
477af75078fSIntel if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
478af75078fSIntel return 0;
479af75078fSIntel
480693f715dSHuawei Xie return (uint32_t)pm;
481af75078fSIntel }
482af75078fSIntel
483af75078fSIntel static int
parse_nqueue(const char * q_arg)484af75078fSIntel parse_nqueue(const char *q_arg)
485af75078fSIntel {
486af75078fSIntel char *end = NULL;
487af75078fSIntel unsigned long n;
488af75078fSIntel
489af75078fSIntel /* parse numerical string */
490af75078fSIntel errno = 0;
491af75078fSIntel n = strtoul(q_arg, &end, 0);
492af75078fSIntel if (errno != 0 || end == NULL || *end != '\0' ||
493af75078fSIntel n == 0 || n >= MAX_RX_QUEUE_PER_LCORE)
494693f715dSHuawei Xie return -1;
495af75078fSIntel
496693f715dSHuawei Xie return n;
497af75078fSIntel }
498af75078fSIntel
499af75078fSIntel /* Parse the argument given in the command line of the application */
500af75078fSIntel static int
parse_args(int argc,char ** argv)501af75078fSIntel parse_args(int argc, char **argv)
502af75078fSIntel {
503af75078fSIntel int opt, ret;
504af75078fSIntel char **argvopt;
505af75078fSIntel int option_index;
506af75078fSIntel char *prgname = argv[0];
507af75078fSIntel static struct option lgopts[] = {
508af75078fSIntel {NULL, 0, 0, 0}
509af75078fSIntel };
510af75078fSIntel
511af75078fSIntel argvopt = argv;
512af75078fSIntel
513af75078fSIntel while ((opt = getopt_long(argc, argvopt, "p:q:",
514af75078fSIntel lgopts, &option_index)) != EOF) {
515af75078fSIntel
516af75078fSIntel switch (opt) {
517af75078fSIntel /* portmask */
518af75078fSIntel case 'p':
519af75078fSIntel enabled_port_mask = parse_portmask(optarg);
520af75078fSIntel if (enabled_port_mask == 0) {
521af75078fSIntel printf("invalid portmask\n");
522af75078fSIntel print_usage(prgname);
523af75078fSIntel return -1;
524af75078fSIntel }
525af75078fSIntel break;
526af75078fSIntel
527af75078fSIntel /* nqueue */
528af75078fSIntel case 'q':
529af75078fSIntel rx_queue_per_lcore = parse_nqueue(optarg);
530af75078fSIntel if (rx_queue_per_lcore < 0) {
531af75078fSIntel printf("invalid queue number\n");
532af75078fSIntel print_usage(prgname);
533af75078fSIntel return -1;
534af75078fSIntel }
535af75078fSIntel break;
536af75078fSIntel
537af75078fSIntel default:
538af75078fSIntel print_usage(prgname);
539af75078fSIntel return -1;
540af75078fSIntel }
541af75078fSIntel }
542af75078fSIntel
543af75078fSIntel if (optind >= 0)
544af75078fSIntel argv[optind-1] = prgname;
545af75078fSIntel
546af75078fSIntel ret = optind-1;
5479d5ca532SKeith Wiles optind = 1; /* reset getopt lib */
548af75078fSIntel return ret;
549af75078fSIntel }
550af75078fSIntel
551af75078fSIntel static void
print_ethaddr(const char * name,struct rte_ether_addr * eth_addr)5526d13ea8eSOlivier Matz print_ethaddr(const char *name, struct rte_ether_addr *eth_addr)
553af75078fSIntel {
55435b2d13fSOlivier Matz char buf[RTE_ETHER_ADDR_FMT_SIZE];
55535b2d13fSOlivier Matz rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
556ec3d82dbSCunming Liang printf("%s%s", name, buf);
557af75078fSIntel }
558af75078fSIntel
5599a212dc0SConor Fogarty /* Hash object is created and loaded. 8< */
560af75078fSIntel static int
init_mcast_hash(void)561af75078fSIntel init_mcast_hash(void)
562af75078fSIntel {
563af75078fSIntel uint32_t i;
564af75078fSIntel
565e60f71ebSIntel mcast_hash_params.socket_id = rte_socket_id();
566af75078fSIntel mcast_hash = rte_fbk_hash_create(&mcast_hash_params);
567af75078fSIntel if (mcast_hash == NULL){
568af75078fSIntel return -1;
569af75078fSIntel }
570af75078fSIntel
5717efe28bdSPavan Nikhilesh for (i = 0; i < RTE_DIM(mcast_group_table); i++) {
572af75078fSIntel if (rte_fbk_hash_add_key(mcast_hash,
573af75078fSIntel mcast_group_table[i].ip,
574af75078fSIntel mcast_group_table[i].port_mask) < 0) {
575af75078fSIntel return -1;
576af75078fSIntel }
577af75078fSIntel }
578af75078fSIntel
579af75078fSIntel return 0;
580af75078fSIntel }
5819a212dc0SConor Fogarty /* >8 End of hash object is created and loaded. */
582af75078fSIntel
583d3641ae8SIntel /* Check the link status of all ports in up to 9s, and print them finally */
584d3641ae8SIntel static void
check_all_ports_link_status(uint32_t port_mask)5858728ccf3SThomas Monjalon check_all_ports_link_status(uint32_t port_mask)
586d3641ae8SIntel {
587d3641ae8SIntel #define CHECK_INTERVAL 100 /* 100ms */
588d3641ae8SIntel #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
589f8244c63SZhiyong Yang uint16_t portid;
590f8244c63SZhiyong Yang uint8_t count, all_ports_up, print_flag = 0;
591d3641ae8SIntel struct rte_eth_link link;
59222e5c73bSIgor Romanov int ret;
593db4e8135SIvan Dyukov char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
594d3641ae8SIntel
595d3641ae8SIntel printf("\nChecking link status");
596d3641ae8SIntel fflush(stdout);
597d3641ae8SIntel for (count = 0; count <= MAX_CHECK_TIME; count++) {
598d3641ae8SIntel all_ports_up = 1;
5998728ccf3SThomas Monjalon RTE_ETH_FOREACH_DEV(portid) {
600d3641ae8SIntel if ((port_mask & (1 << portid)) == 0)
601d3641ae8SIntel continue;
602d3641ae8SIntel memset(&link, 0, sizeof(link));
60322e5c73bSIgor Romanov ret = rte_eth_link_get_nowait(portid, &link);
60422e5c73bSIgor Romanov if (ret < 0) {
60522e5c73bSIgor Romanov all_ports_up = 0;
60622e5c73bSIgor Romanov if (print_flag == 1)
60722e5c73bSIgor Romanov printf("Port %u link get failed: %s\n",
60822e5c73bSIgor Romanov portid, rte_strerror(-ret));
60922e5c73bSIgor Romanov continue;
61022e5c73bSIgor Romanov }
611d3641ae8SIntel /* print link status if flag set */
612d3641ae8SIntel if (print_flag == 1) {
613db4e8135SIvan Dyukov rte_eth_link_to_str(link_status_text,
614db4e8135SIvan Dyukov sizeof(link_status_text),
615db4e8135SIvan Dyukov &link);
616db4e8135SIvan Dyukov printf("Port %d %s\n", portid,
617db4e8135SIvan Dyukov link_status_text);
618d3641ae8SIntel continue;
619d3641ae8SIntel }
620d3641ae8SIntel /* clear all_ports_up flag if any link down */
621295968d1SFerruh Yigit if (link.link_status == RTE_ETH_LINK_DOWN) {
622d3641ae8SIntel all_ports_up = 0;
623d3641ae8SIntel break;
624d3641ae8SIntel }
625d3641ae8SIntel }
626d3641ae8SIntel /* after finally printing all link status, get out */
627d3641ae8SIntel if (print_flag == 1)
628d3641ae8SIntel break;
629d3641ae8SIntel
630d3641ae8SIntel if (all_ports_up == 0) {
631d3641ae8SIntel printf(".");
632d3641ae8SIntel fflush(stdout);
633d3641ae8SIntel rte_delay_ms(CHECK_INTERVAL);
634d3641ae8SIntel }
635d3641ae8SIntel
636d3641ae8SIntel /* set the print_flag if all ports up or timeout */
637d3641ae8SIntel if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
638d3641ae8SIntel print_flag = 1;
639d3641ae8SIntel printf("done\n");
640d3641ae8SIntel }
641d3641ae8SIntel }
642d3641ae8SIntel }
643d3641ae8SIntel
644af75078fSIntel int
main(int argc,char ** argv)64598a16481SDavid Marchand main(int argc, char **argv)
646af75078fSIntel {
647af75078fSIntel struct lcore_queue_conf *qconf;
64881f7ecd9SPablo de Lara struct rte_eth_dev_info dev_info;
64981f7ecd9SPablo de Lara struct rte_eth_txconf *txconf;
650af75078fSIntel int ret;
651af75078fSIntel uint16_t queueid;
6529787d22fSIntel unsigned lcore_id = 0, rx_lcore_id = 0;
653af75078fSIntel uint32_t n_tx_queue, nb_lcores;
654f8244c63SZhiyong Yang uint16_t portid;
655af75078fSIntel
656af75078fSIntel /* init EAL */
657af75078fSIntel ret = rte_eal_init(argc, argv);
658af75078fSIntel if (ret < 0)
659af75078fSIntel rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
660af75078fSIntel argc -= ret;
661af75078fSIntel argv += ret;
662af75078fSIntel
663af75078fSIntel /* parse application arguments (after the EAL ones) */
664af75078fSIntel ret = parse_args(argc, argv);
665af75078fSIntel if (ret < 0)
666af75078fSIntel rte_exit(EXIT_FAILURE, "Invalid IPV4_MULTICAST parameters\n");
667af75078fSIntel
6689a212dc0SConor Fogarty /* Create the mbuf pools. 8< */
669ea0c20eaSOlivier Matz packet_pool = rte_pktmbuf_pool_create("packet_pool", NB_PKT_MBUF, 32,
670ea0c20eaSOlivier Matz 0, PKT_MBUF_DATA_SIZE, rte_socket_id());
671af75078fSIntel
672af75078fSIntel if (packet_pool == NULL)
673af75078fSIntel rte_exit(EXIT_FAILURE, "Cannot init packet mbuf pool\n");
674af75078fSIntel
675ea0c20eaSOlivier Matz header_pool = rte_pktmbuf_pool_create("header_pool", NB_HDR_MBUF, 32,
676ea0c20eaSOlivier Matz 0, HDR_MBUF_DATA_SIZE, rte_socket_id());
677af75078fSIntel
678af75078fSIntel if (header_pool == NULL)
679af75078fSIntel rte_exit(EXIT_FAILURE, "Cannot init header mbuf pool\n");
680af75078fSIntel
681ea0c20eaSOlivier Matz clone_pool = rte_pktmbuf_pool_create("clone_pool", NB_CLONE_MBUF, 32,
682ea0c20eaSOlivier Matz 0, 0, rte_socket_id());
683af75078fSIntel
684af75078fSIntel if (clone_pool == NULL)
685af75078fSIntel rte_exit(EXIT_FAILURE, "Cannot init clone mbuf pool\n");
6869a212dc0SConor Fogarty /* >8 End of create mbuf pools. */
687af75078fSIntel
688d9a42a69SThomas Monjalon nb_ports = rte_eth_dev_count_avail();
689af75078fSIntel if (nb_ports == 0)
690af75078fSIntel rte_exit(EXIT_FAILURE, "No physical ports!\n");
691af75078fSIntel if (nb_ports > MAX_PORTS)
692af75078fSIntel nb_ports = MAX_PORTS;
693af75078fSIntel
694af75078fSIntel nb_lcores = rte_lcore_count();
695af75078fSIntel
696af75078fSIntel /* initialize all ports */
6978728ccf3SThomas Monjalon RTE_ETH_FOREACH_DEV(portid) {
6986b85f708SShahaf Shuler struct rte_eth_rxconf rxq_conf;
6996b85f708SShahaf Shuler struct rte_eth_conf local_port_conf = port_conf;
7006b85f708SShahaf Shuler
701af75078fSIntel /* skip ports that are not enabled */
702af75078fSIntel if ((enabled_port_mask & (1 << portid)) == 0) {
703af75078fSIntel printf("Skipping disabled port %d\n", portid);
704af75078fSIntel continue;
705af75078fSIntel }
706af75078fSIntel
707af75078fSIntel qconf = &lcore_queue_conf[rx_lcore_id];
708af75078fSIntel
7095e470a66SAndriy Berestovskyy /* limit the frame size to the maximum supported by NIC */
710089e5ed7SIvan Ilchenko ret = rte_eth_dev_info_get(portid, &dev_info);
711089e5ed7SIvan Ilchenko if (ret != 0)
712089e5ed7SIvan Ilchenko rte_exit(EXIT_FAILURE,
713089e5ed7SIvan Ilchenko "Error during getting device (port %u) info: %s\n",
714089e5ed7SIvan Ilchenko portid, strerror(-ret));
715089e5ed7SIvan Ilchenko
7161bb4a528SFerruh Yigit local_port_conf.rxmode.mtu = RTE_MIN(
7171bb4a528SFerruh Yigit dev_info.max_mtu,
7181bb4a528SFerruh Yigit local_port_conf.rxmode.mtu);
7195e470a66SAndriy Berestovskyy
720af75078fSIntel /* get the lcore_id for this port */
721af75078fSIntel while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
722af75078fSIntel qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) {
723af75078fSIntel
724af75078fSIntel rx_lcore_id ++;
725af75078fSIntel qconf = &lcore_queue_conf[rx_lcore_id];
726af75078fSIntel
727af75078fSIntel if (rx_lcore_id >= RTE_MAX_LCORE)
728af75078fSIntel rte_exit(EXIT_FAILURE, "Not enough cores\n");
729af75078fSIntel }
730af75078fSIntel qconf->rx_queue_list[qconf->n_rx_queue] = portid;
731af75078fSIntel qconf->n_rx_queue++;
732af75078fSIntel
733af75078fSIntel /* init port */
734af75078fSIntel printf("Initializing port %d on lcore %u... ", portid,
735af75078fSIntel rx_lcore_id);
736af75078fSIntel fflush(stdout);
737af75078fSIntel
738af75078fSIntel n_tx_queue = nb_lcores;
739af75078fSIntel if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
740af75078fSIntel n_tx_queue = MAX_TX_QUEUE_PER_PORT;
7416b85f708SShahaf Shuler
742af75078fSIntel ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue,
7436b85f708SShahaf Shuler &local_port_conf);
744af75078fSIntel if (ret < 0)
745af75078fSIntel rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
746af75078fSIntel ret, portid);
747af75078fSIntel
74860efb44fSRoman Zhukov ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
74960efb44fSRoman Zhukov &nb_txd);
75060efb44fSRoman Zhukov if (ret < 0)
75160efb44fSRoman Zhukov rte_exit(EXIT_FAILURE,
75260efb44fSRoman Zhukov "Cannot adjust number of descriptors: err=%d, port=%d\n",
75360efb44fSRoman Zhukov ret, portid);
75460efb44fSRoman Zhukov
75570febdcfSIgor Romanov ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
75670febdcfSIgor Romanov if (ret < 0)
75770febdcfSIgor Romanov rte_exit(EXIT_FAILURE,
75870febdcfSIgor Romanov "Cannot get MAC address: err=%d, port=%d\n",
75970febdcfSIgor Romanov ret, portid);
76070febdcfSIgor Romanov
761af75078fSIntel print_ethaddr(" Address:", &ports_eth_addr[portid]);
762af75078fSIntel printf(", ");
763af75078fSIntel
764af75078fSIntel /* init one RX queue */
765af75078fSIntel queueid = 0;
766af75078fSIntel printf("rxq=%hu ", queueid);
767af75078fSIntel fflush(stdout);
7686b85f708SShahaf Shuler rxq_conf = dev_info.default_rxconf;
7696b85f708SShahaf Shuler rxq_conf.offloads = local_port_conf.rxmode.offloads;
770af75078fSIntel ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
77181f7ecd9SPablo de Lara rte_eth_dev_socket_id(portid),
7726b85f708SShahaf Shuler &rxq_conf,
773af75078fSIntel packet_pool);
774af75078fSIntel if (ret < 0)
775af75078fSIntel rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, port=%d\n",
776af75078fSIntel ret, portid);
777af75078fSIntel
778af75078fSIntel /* init one TX queue per couple (lcore,port) */
779af75078fSIntel queueid = 0;
780af75078fSIntel
781af75078fSIntel RTE_LCORE_FOREACH(lcore_id) {
782af75078fSIntel if (rte_lcore_is_enabled(lcore_id) == 0)
783af75078fSIntel continue;
784af75078fSIntel printf("txq=%u,%hu ", lcore_id, queueid);
785af75078fSIntel fflush(stdout);
78681f7ecd9SPablo de Lara
78781f7ecd9SPablo de Lara txconf = &dev_info.default_txconf;
7886b85f708SShahaf Shuler txconf->offloads = local_port_conf.txmode.offloads;
789af75078fSIntel ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
79081f7ecd9SPablo de Lara rte_lcore_to_socket_id(lcore_id), txconf);
791af75078fSIntel if (ret < 0)
792af75078fSIntel rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
793af75078fSIntel "port=%d\n", ret, portid);
794af75078fSIntel
795af75078fSIntel qconf = &lcore_queue_conf[lcore_id];
796af75078fSIntel qconf->tx_queue_id[portid] = queueid;
797af75078fSIntel queueid++;
798af75078fSIntel }
7990ecc27f2SIvan Ilchenko ret = rte_eth_allmulticast_enable(portid);
8000ecc27f2SIvan Ilchenko if (ret < 0)
8010ecc27f2SIvan Ilchenko rte_exit(EXIT_FAILURE,
8020ecc27f2SIvan Ilchenko "rte_eth_allmulticast_enable: err=%d, port=%d\n",
8030ecc27f2SIvan Ilchenko ret, portid);
804af75078fSIntel /* Start device */
805af75078fSIntel ret = rte_eth_dev_start(portid);
806af75078fSIntel if (ret < 0)
807af75078fSIntel rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
808af75078fSIntel ret, portid);
809af75078fSIntel
810d3641ae8SIntel printf("done:\n");
811af75078fSIntel }
812af75078fSIntel
8138728ccf3SThomas Monjalon check_all_ports_link_status(enabled_port_mask);
814af75078fSIntel
815af75078fSIntel /* initialize the multicast hash */
816af75078fSIntel int retval = init_mcast_hash();
817af75078fSIntel if (retval != 0)
818af75078fSIntel rte_exit(EXIT_FAILURE, "Cannot build the multicast hash\n");
819af75078fSIntel
820af75078fSIntel /* launch per-lcore init on every lcore */
821cb056611SStephen Hemminger rte_eal_mp_remote_launch(main_loop, NULL, CALL_MAIN);
822cb056611SStephen Hemminger RTE_LCORE_FOREACH_WORKER(lcore_id) {
823af75078fSIntel if (rte_eal_wait_lcore(lcore_id) < 0)
824af75078fSIntel return -1;
825af75078fSIntel }
826af75078fSIntel
82710aa3757SChengchang Tang /* clean up the EAL */
82810aa3757SChengchang Tang rte_eal_cleanup();
82910aa3757SChengchang Tang
830af75078fSIntel return 0;
831af75078fSIntel }
832