1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
7 #include <rte_common.h>
10 #include <rte_ethdev.h>
11 #include <rte_launch.h>
12 #include <rte_lcore.h>
17 #include <rte_byteorder.h>
22 #include "../include/conf.h"
26 #define SEND_PAUSE_FRAME(port_id, duration) send_pause_frame(port_id, duration)
28 #define SEND_PAUSE_FRAME(port_id, duration) do { } while(0)
31 #define ETHER_TYPE_FLOW_CONTROL 0x8808
33 struct ether_fc_frame {
36 } __attribute__((__packed__));
40 unsigned int *low_watermark;
41 unsigned int *high_watermark;
43 uint16_t port_pairs[RTE_MAX_ETHPORTS];
45 struct rte_ring *rings[RTE_MAX_LCORE][RTE_MAX_ETHPORTS];
46 struct rte_mempool *mbuf_pool;
49 static void send_pause_frame(uint16_t port_id, uint16_t duration)
51 struct rte_mbuf *mbuf;
52 struct ether_fc_frame *pause_frame;
53 struct rte_ether_hdr *hdr;
54 struct rte_ether_addr mac_addr;
56 RTE_LOG_DP(DEBUG, USER1,
57 "Sending PAUSE frame (duration=%d) on port %d\n",
60 /* Get a mbuf from the pool */
61 mbuf = rte_pktmbuf_alloc(mbuf_pool);
62 if (unlikely(mbuf == NULL))
65 /* Prepare a PAUSE frame */
66 hdr = rte_pktmbuf_mtod(mbuf, struct rte_ether_hdr *);
67 pause_frame = (struct ether_fc_frame *) &hdr[1];
69 rte_eth_macaddr_get(port_id, &mac_addr);
70 ether_addr_copy(&mac_addr, &hdr->s_addr);
72 void *tmp = &hdr->d_addr.addr_bytes[0];
73 *((uint64_t *)tmp) = 0x010000C28001ULL;
75 hdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_FLOW_CONTROL);
77 pause_frame->opcode = rte_cpu_to_be_16(0x0001);
78 pause_frame->param = rte_cpu_to_be_16(duration);
83 rte_eth_tx_burst(port_id, 0, &mbuf, 1);
87 * Get the previous enabled lcore ID
90 * The current lcore ID.
92 * The previous enabled lcore_id or -1 if not found.
95 get_previous_lcore_id(unsigned int lcore_id)
99 for (i = lcore_id - 1; i >= 0; i--)
100 if (rte_lcore_is_enabled(i))
107 * Get the last enabled lcore ID
110 * The last enabled lcore_id.
113 get_last_lcore_id(void)
117 for (i = RTE_MAX_LCORE; i >= 0; i--)
118 if (rte_lcore_is_enabled(i))
125 receive_stage(__attribute__((unused)) void *args)
132 unsigned int lcore_id;
135 struct rte_mbuf *pkts[MAX_PKT_QUOTA];
136 struct rte_ring *ring;
137 enum ring_state ring_state[RTE_MAX_ETHPORTS] = { RING_READY };
139 lcore_id = rte_lcore_id();
142 "%s() started on core %u\n", __func__, lcore_id);
146 /* Process each port round robin style */
147 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
149 if (!is_bit_set(port_id, portmask))
152 ring = rings[lcore_id][port_id];
154 if (ring_state[port_id] != RING_READY) {
155 if (rte_ring_count(ring) > *low_watermark)
158 ring_state[port_id] = RING_READY;
161 /* Enqueue received packets on the RX ring */
162 nb_rx_pkts = rte_eth_rx_burst(port_id, 0, pkts,
164 ret = rte_ring_enqueue_bulk(ring, (void *) pkts,
166 if (RING_SIZE - free > *high_watermark) {
167 ring_state[port_id] = RING_OVERLOADED;
168 send_pause_frame(port_id, 1337);
174 * Return mbufs to the pool,
175 * effectively dropping packets
177 for (i = 0; i < nb_rx_pkts; i++)
178 rte_pktmbuf_free(pkts[i]);
185 pipeline_stage(__attribute__((unused)) void *args)
192 unsigned int lcore_id, previous_lcore_id;
195 void *pkts[MAX_PKT_QUOTA];
196 struct rte_ring *rx, *tx;
197 enum ring_state ring_state[RTE_MAX_ETHPORTS] = { RING_READY };
199 lcore_id = rte_lcore_id();
200 previous_lcore_id = get_previous_lcore_id(lcore_id);
203 "%s() started on core %u - processing packets from core %u\n",
204 __func__, lcore_id, previous_lcore_id);
208 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
210 if (!is_bit_set(port_id, portmask))
213 tx = rings[lcore_id][port_id];
214 rx = rings[previous_lcore_id][port_id];
216 if (ring_state[port_id] != RING_READY) {
217 if (rte_ring_count(tx) > *low_watermark)
220 ring_state[port_id] = RING_READY;
223 /* Dequeue up to quota mbuf from rx */
224 nb_dq_pkts = rte_ring_dequeue_burst(rx, pkts,
226 if (unlikely(nb_dq_pkts < 0))
229 /* Enqueue them on tx */
230 ret = rte_ring_enqueue_bulk(tx, pkts,
232 if (RING_SIZE - free > *high_watermark)
233 ring_state[port_id] = RING_OVERLOADED;
238 * Return mbufs to the pool,
239 * effectively dropping packets
241 for (i = 0; i < nb_dq_pkts; i++)
242 rte_pktmbuf_free(pkts[i]);
251 send_stage(__attribute__((unused)) void *args)
256 uint16_t dest_port_id;
258 unsigned int lcore_id, previous_lcore_id;
261 struct rte_mbuf *tx_pkts[MAX_PKT_QUOTA];
263 lcore_id = rte_lcore_id();
264 previous_lcore_id = get_previous_lcore_id(lcore_id);
267 "%s() started on core %u - processing packets from core %u\n",
268 __func__, lcore_id, previous_lcore_id);
272 /* Process each ring round robin style */
273 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
275 if (!is_bit_set(port_id, portmask))
278 dest_port_id = port_pairs[port_id];
279 tx = rings[previous_lcore_id][port_id];
281 if (rte_ring_empty(tx))
284 /* Dequeue packets from tx and send them */
285 nb_dq_pkts = (uint16_t) rte_ring_dequeue_burst(tx,
286 (void *) tx_pkts, *quota, NULL);
287 rte_eth_tx_burst(dest_port_id, 0, tx_pkts, nb_dq_pkts);
289 /* TODO: Check if nb_dq_pkts == nb_tx_pkts? */
297 main(int argc, char **argv)
300 unsigned int lcore_id, master_lcore_id, last_lcore_id;
304 rte_log_set_global_level(RTE_LOG_INFO);
306 ret = rte_eal_init(argc, argv);
308 rte_exit(EXIT_FAILURE, "Cannot initialize EAL\n");
314 setup_shared_variables();
317 *low_watermark = 60 * RING_SIZE / 100;
319 last_lcore_id = get_last_lcore_id();
320 master_lcore_id = rte_get_master_lcore();
322 /* Parse the application's arguments */
323 ret = parse_qw_args(argc, argv);
325 rte_exit(EXIT_FAILURE, "Invalid quota/watermark argument(s)\n");
327 /* Create a pool of mbuf to store packets */
328 mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL, 32, 0,
329 MBUF_DATA_SIZE, rte_socket_id());
330 if (mbuf_pool == NULL)
331 rte_panic("%s\n", rte_strerror(rte_errno));
333 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++)
334 if (is_bit_set(port_id, portmask)) {
335 configure_eth_port(port_id);
336 init_ring(master_lcore_id, port_id);
342 * Start pipeline_connect() on all the available slave lcores
345 for (lcore_id = 0 ; lcore_id < last_lcore_id; lcore_id++) {
346 if (rte_lcore_is_enabled(lcore_id) &&
347 lcore_id != master_lcore_id) {
349 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++)
350 if (is_bit_set(port_id, portmask))
351 init_ring(lcore_id, port_id);
353 rte_eal_remote_launch(pipeline_stage,
358 /* Start send_stage() on the last slave core */
359 rte_eal_remote_launch(send_stage, NULL, last_lcore_id);
361 /* Start receive_stage() on the master core */