4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <rte_common.h>
37 #include <rte_debug.h>
38 #include <rte_errno.h>
39 #include <rte_ethdev.h>
40 #include <rte_launch.h>
41 #include <rte_lcore.h>
46 #include <rte_byteorder.h>
51 #include "../include/conf.h"
55 #define SEND_PAUSE_FRAME(port_id, duration) send_pause_frame(port_id, duration)
57 #define SEND_PAUSE_FRAME(port_id, duration) do { } while(0)
60 #define ETHER_TYPE_FLOW_CONTROL 0x8808
62 struct ether_fc_frame {
65 } __attribute__((__packed__));
69 unsigned int *low_watermark;
70 unsigned int *high_watermark;
72 uint8_t port_pairs[RTE_MAX_ETHPORTS];
74 struct rte_ring *rings[RTE_MAX_LCORE][RTE_MAX_ETHPORTS];
75 struct rte_mempool *mbuf_pool;
78 static void send_pause_frame(uint8_t port_id, uint16_t duration)
80 struct rte_mbuf *mbuf;
81 struct ether_fc_frame *pause_frame;
82 struct ether_hdr *hdr;
83 struct ether_addr mac_addr;
85 RTE_LOG_DP(DEBUG, USER1,
86 "Sending PAUSE frame (duration=%d) on port %d\n",
89 /* Get a mbuf from the pool */
90 mbuf = rte_pktmbuf_alloc(mbuf_pool);
91 if (unlikely(mbuf == NULL))
94 /* Prepare a PAUSE frame */
95 hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
96 pause_frame = (struct ether_fc_frame *) &hdr[1];
98 rte_eth_macaddr_get(port_id, &mac_addr);
99 ether_addr_copy(&mac_addr, &hdr->s_addr);
101 void *tmp = &hdr->d_addr.addr_bytes[0];
102 *((uint64_t *)tmp) = 0x010000C28001ULL;
104 hdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_FLOW_CONTROL);
106 pause_frame->opcode = rte_cpu_to_be_16(0x0001);
107 pause_frame->param = rte_cpu_to_be_16(duration);
112 rte_eth_tx_burst(port_id, 0, &mbuf, 1);
116 * Get the previous enabled lcore ID
119 * The current lcore ID.
121 * The previous enabled lcore_id or -1 if not found.
124 get_previous_lcore_id(unsigned int lcore_id)
128 for (i = lcore_id - 1; i >= 0; i--)
129 if (rte_lcore_is_enabled(i))
136 * Get the last enabled lcore ID
139 * The last enabled lcore_id.
142 get_last_lcore_id(void)
146 for (i = RTE_MAX_LCORE; i >= 0; i--)
147 if (rte_lcore_is_enabled(i))
154 receive_stage(__attribute__((unused)) void *args)
161 unsigned int lcore_id;
164 struct rte_mbuf *pkts[MAX_PKT_QUOTA];
165 struct rte_ring *ring;
166 enum ring_state ring_state[RTE_MAX_ETHPORTS] = { RING_READY };
168 lcore_id = rte_lcore_id();
171 "%s() started on core %u\n", __func__, lcore_id);
175 /* Process each port round robin style */
176 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
178 if (!is_bit_set(port_id, portmask))
181 ring = rings[lcore_id][port_id];
183 if (ring_state[port_id] != RING_READY) {
184 if (rte_ring_count(ring) > *low_watermark)
187 ring_state[port_id] = RING_READY;
190 /* Enqueue received packets on the RX ring */
191 nb_rx_pkts = rte_eth_rx_burst(port_id, 0, pkts,
193 ret = rte_ring_enqueue_bulk(ring, (void *) pkts,
195 if (RING_SIZE - free > *high_watermark) {
196 ring_state[port_id] = RING_OVERLOADED;
197 send_pause_frame(port_id, 1337);
203 * Return mbufs to the pool,
204 * effectively dropping packets
206 for (i = 0; i < nb_rx_pkts; i++)
207 rte_pktmbuf_free(pkts[i]);
214 pipeline_stage(__attribute__((unused)) void *args)
221 unsigned int lcore_id, previous_lcore_id;
224 void *pkts[MAX_PKT_QUOTA];
225 struct rte_ring *rx, *tx;
226 enum ring_state ring_state[RTE_MAX_ETHPORTS] = { RING_READY };
228 lcore_id = rte_lcore_id();
229 previous_lcore_id = get_previous_lcore_id(lcore_id);
232 "%s() started on core %u - processing packets from core %u\n",
233 __func__, lcore_id, previous_lcore_id);
237 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
239 if (!is_bit_set(port_id, portmask))
242 tx = rings[lcore_id][port_id];
243 rx = rings[previous_lcore_id][port_id];
245 if (ring_state[port_id] != RING_READY) {
246 if (rte_ring_count(tx) > *low_watermark)
249 ring_state[port_id] = RING_READY;
252 /* Dequeue up to quota mbuf from rx */
253 nb_dq_pkts = rte_ring_dequeue_burst(rx, pkts,
255 if (unlikely(nb_dq_pkts < 0))
258 /* Enqueue them on tx */
259 ret = rte_ring_enqueue_bulk(tx, pkts,
261 if (RING_SIZE - free > *high_watermark)
262 ring_state[port_id] = RING_OVERLOADED;
267 * Return mbufs to the pool,
268 * effectively dropping packets
270 for (i = 0; i < nb_dq_pkts; i++)
271 rte_pktmbuf_free(pkts[i]);
278 send_stage(__attribute__((unused)) void *args)
283 uint8_t dest_port_id;
285 unsigned int lcore_id, previous_lcore_id;
288 struct rte_mbuf *tx_pkts[MAX_PKT_QUOTA];
290 lcore_id = rte_lcore_id();
291 previous_lcore_id = get_previous_lcore_id(lcore_id);
294 "%s() started on core %u - processing packets from core %u\n",
295 __func__, lcore_id, previous_lcore_id);
299 /* Process each ring round robin style */
300 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
302 if (!is_bit_set(port_id, portmask))
305 dest_port_id = port_pairs[port_id];
306 tx = rings[previous_lcore_id][port_id];
308 if (rte_ring_empty(tx))
311 /* Dequeue packets from tx and send them */
312 nb_dq_pkts = (uint16_t) rte_ring_dequeue_burst(tx,
313 (void *) tx_pkts, *quota, NULL);
314 rte_eth_tx_burst(dest_port_id, 0, tx_pkts, nb_dq_pkts);
316 /* TODO: Check if nb_dq_pkts == nb_tx_pkts? */
322 main(int argc, char **argv)
325 unsigned int lcore_id, master_lcore_id, last_lcore_id;
329 rte_log_set_global_level(RTE_LOG_INFO);
331 ret = rte_eal_init(argc, argv);
333 rte_exit(EXIT_FAILURE, "Cannot initialize EAL\n");
339 setup_shared_variables();
342 *low_watermark = 60 * RING_SIZE / 100;
344 last_lcore_id = get_last_lcore_id();
345 master_lcore_id = rte_get_master_lcore();
347 /* Parse the application's arguments */
348 ret = parse_qw_args(argc, argv);
350 rte_exit(EXIT_FAILURE, "Invalid quota/watermark argument(s)\n");
352 /* Create a pool of mbuf to store packets */
353 mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL, 32, 0,
354 MBUF_DATA_SIZE, rte_socket_id());
355 if (mbuf_pool == NULL)
356 rte_panic("%s\n", rte_strerror(rte_errno));
358 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++)
359 if (is_bit_set(port_id, portmask)) {
360 configure_eth_port(port_id);
361 init_ring(master_lcore_id, port_id);
367 * Start pipeline_connect() on all the available slave lcores
370 for (lcore_id = 0 ; lcore_id < last_lcore_id; lcore_id++) {
371 if (rte_lcore_is_enabled(lcore_id) &&
372 lcore_id != master_lcore_id) {
374 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++)
375 if (is_bit_set(port_id, portmask))
376 init_ring(lcore_id, port_id);
378 /* typecast is a workaround for GCC 4.3 bug */
379 rte_eal_remote_launch((int (*)(void *))pipeline_stage,
384 /* Start send_stage() on the last slave core */
385 /* typecast is a workaround for GCC 4.3 bug */
386 rte_eal_remote_launch((int (*)(void *))send_stage, NULL, last_lcore_id);
388 /* Start receive_stage() on the master core */