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.
42 #include <sys/types.h>
44 #include <sys/queue.h>
49 #include <sys/ioctl.h>
52 #include <rte_common.h>
53 #include <rte_eal_memconfig.h>
55 #include <rte_memory.h>
56 #include <rte_memcpy.h>
57 #include <rte_memzone.h>
58 #include <rte_tailq.h>
60 #include <rte_per_lcore.h>
61 #include <rte_launch.h>
62 #include <rte_atomic.h>
63 #include <rte_cycles.h>
64 #include <rte_prefetch.h>
65 #include <rte_lcore.h>
66 #include <rte_per_lcore.h>
67 #include <rte_branch_prediction.h>
68 #include <rte_interrupts.h>
70 #include <rte_random.h>
71 #include <rte_debug.h>
72 #include <rte_ether.h>
73 #include <rte_ethdev.h>
75 #include <rte_mempool.h>
77 #include <rte_ivshmem.h>
79 #include "../include/common.h"
81 #define MAX_RX_QUEUE_PER_LCORE 16
82 #define MAX_TX_QUEUE_PER_PORT 16
83 struct lcore_queue_conf {
85 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
86 struct mbuf_table rx_mbufs[RTE_MAX_ETHPORTS];
87 struct vm_port_param * port_param[MAX_RX_QUEUE_PER_LCORE];
88 } __rte_cache_aligned;
89 static struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
91 /* Print out statistics on packets dropped */
95 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
98 total_packets_dropped = 0;
100 total_packets_rx = 0;
102 const char clr[] = { 27, '[', '2', 'J', '\0' };
103 const char topLeft[] = { 27, '[', '1', ';', '1', 'H','\0' };
105 /* Clear screen and move to top left */
106 printf("%s%s", clr, topLeft);
108 printf("\nPort statistics ====================================");
110 for (portid = 0; portid < ctrl->nb_ports; portid++) {
111 /* skip ports that are not enabled */
112 printf("\nStatistics for port %u ------------------------------"
113 "\nPackets sent: %24"PRIu64
114 "\nPackets received: %20"PRIu64
115 "\nPackets dropped: %21"PRIu64,
117 ctrl->vm_ports[portid].stats.tx,
118 ctrl->vm_ports[portid].stats.rx,
119 ctrl->vm_ports[portid].stats.dropped);
121 total_packets_dropped += ctrl->vm_ports[portid].stats.dropped;
122 total_packets_tx += ctrl->vm_ports[portid].stats.tx;
123 total_packets_rx += ctrl->vm_ports[portid].stats.rx;
125 printf("\nAggregate statistics ==============================="
126 "\nTotal packets sent: %18"PRIu64
127 "\nTotal packets received: %14"PRIu64
128 "\nTotal packets dropped: %15"PRIu64,
131 total_packets_dropped);
132 printf("\n====================================================\n");
137 l2fwd_ivshmem_usage(const char *prgname)
139 printf("%s [EAL options] -- [-q NQ -T PERIOD]\n"
140 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
141 " -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n",
146 l2fwd_ivshmem_parse_nqueue(const char *q_arg)
151 /* parse hexadecimal string */
152 n = strtoul(q_arg, &end, 10);
153 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
157 if (n >= MAX_RX_QUEUE_PER_LCORE)
164 l2fwd_ivshmem_parse_timer_period(const char *q_arg)
169 /* parse number string */
170 n = strtol(q_arg, &end, 10);
171 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
173 if (n >= MAX_TIMER_PERIOD)
179 /* Parse the argument given in the command line of the application */
181 l2fwd_ivshmem_parse_args(int argc, char **argv)
186 char *prgname = argv[0];
187 static struct option lgopts[] = {
193 while ((opt = getopt_long(argc, argvopt, "q:p:T:",
194 lgopts, &option_index)) != EOF) {
200 l2fwd_ivshmem_rx_queue_per_lcore = l2fwd_ivshmem_parse_nqueue(optarg);
201 if (l2fwd_ivshmem_rx_queue_per_lcore == 0) {
202 printf("invalid queue number\n");
203 l2fwd_ivshmem_usage(prgname);
210 timer_period = l2fwd_ivshmem_parse_timer_period(optarg) * 1000 * TIMER_MILLISECOND;
211 if (timer_period < 0) {
212 printf("invalid timer period\n");
213 l2fwd_ivshmem_usage(prgname);
220 l2fwd_ivshmem_usage(prgname);
224 l2fwd_ivshmem_usage(prgname);
230 argv[optind-1] = prgname;
233 optind = 0; /* reset getopt lib */
238 * this loop is getting packets from RX rings of each port, and puts them
239 * into TX rings of destination ports.
245 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
246 struct rte_mbuf **m_table;
248 struct rte_ring *rx, *tx;
249 unsigned lcore_id, len;
250 uint64_t prev_tsc, diff_tsc, cur_tsc, timer_tsc;
251 unsigned i, j, portid, nb_rx;
252 struct lcore_queue_conf *qconf;
253 struct ether_hdr *eth;
259 lcore_id = rte_lcore_id();
260 qconf = &lcore_queue_conf[lcore_id];
262 if (qconf->n_rx_port == 0) {
263 RTE_LOG(INFO, L2FWD_IVSHMEM, "lcore %u has nothing to do\n", lcore_id);
267 RTE_LOG(INFO, L2FWD_IVSHMEM, "entering main loop on lcore %u\n", lcore_id);
269 for (i = 0; i < qconf->n_rx_port; i++) {
270 portid = qconf->rx_port_list[i];
271 RTE_LOG(INFO, L2FWD_IVSHMEM, " -- lcoreid=%u portid=%u\n", lcore_id,
275 while (ctrl->state == STATE_FWD) {
276 cur_tsc = rte_rdtsc();
278 diff_tsc = cur_tsc - prev_tsc;
281 * Read packet from RX queues and send it to TX queues
283 for (i = 0; i < qconf->n_rx_port; i++) {
285 portid = qconf->rx_port_list[i];
287 len = qconf->rx_mbufs[portid].len;
289 rx = ctrl->vm_ports[portid].rx_ring;
290 tx = ctrl->vm_ports[portid].dst->tx_ring;
292 m_table = qconf->rx_mbufs[portid].m_table;
294 /* if we have something in the queue, try and transmit it down */
297 /* if we succeed in sending the packets down, mark queue as free */
298 if (rte_ring_enqueue_bulk(tx, (void**) m_table, len) == 0) {
299 ctrl->vm_ports[portid].stats.tx += len;
300 qconf->rx_mbufs[portid].len = 0;
305 nb_rx = rte_ring_count(rx);
307 nb_rx = RTE_MIN(nb_rx, (unsigned) MAX_PKT_BURST);
312 /* if we can get packets into the m_table */
313 if (nb_rx < (RTE_DIM(qconf->rx_mbufs[portid].m_table) - len)) {
315 /* this situation cannot exist, so if we fail to dequeue, that
316 * means something went horribly wrong, hence the failure. */
317 if (rte_ring_dequeue_bulk(rx, (void**) pkts_burst, nb_rx) < 0) {
318 ctrl->state = STATE_FAIL;
322 ctrl->vm_ports[portid].stats.rx += nb_rx;
324 /* put packets into the queue */
325 for (j = 0; j < nb_rx; j++) {
328 rte_prefetch0(rte_pktmbuf_mtod(m, void *));
330 m_table[len + j] = m;
332 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
334 /* 02:00:00:00:00:xx */
335 tmp = ð->d_addr.addr_bytes[0];
336 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)portid << 40);
339 ether_addr_copy(&ctrl->vm_ports[portid].dst->ethaddr,
342 qconf->rx_mbufs[portid].len += nb_rx;
348 /* if timer is enabled */
349 if (timer_period > 0) {
351 /* advance the timer */
352 timer_tsc += diff_tsc;
354 /* if timer has reached its timeout */
355 if (unlikely(timer_tsc >= (uint64_t) timer_period)) {
357 /* do this only on master core */
358 if (lcore_id == rte_get_master_lcore()) {
360 /* reset the timer */
371 l2fwd_ivshmem_launch_one_lcore(__attribute__((unused)) void *dummy)
378 main(int argc, char **argv)
380 struct lcore_queue_conf *qconf;
381 const struct rte_memzone * mz;
384 unsigned rx_lcore_id, lcore_id;
387 ret = rte_eal_init(argc, argv);
389 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
393 /* parse application arguments (after the EAL ones) */
394 ret = l2fwd_ivshmem_parse_args(argc, argv);
396 rte_exit(EXIT_FAILURE, "Invalid l2fwd-ivshmem arguments\n");
398 /* find control structure */
399 mz = rte_memzone_lookup(CTRL_MZ_NAME);
401 rte_exit(EXIT_FAILURE, "Cannot find control memzone\n");
403 ctrl = (struct ivshmem_ctrl*) mz->addr;
405 /* lock the ctrl so that we don't have conflicts with anything else */
406 rte_spinlock_lock(&ctrl->lock);
408 if (ctrl->state == STATE_FWD)
409 rte_exit(EXIT_FAILURE, "Forwarding already started!\n");
414 /* Initialize the port/queue configuration of each logical core */
415 for (portid = 0; portid < ctrl->nb_ports; portid++) {
417 /* get the lcore_id for this port */
418 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
419 lcore_queue_conf[rx_lcore_id].n_rx_port ==
420 l2fwd_ivshmem_rx_queue_per_lcore) {
422 if (rx_lcore_id >= RTE_MAX_LCORE)
423 rte_exit(EXIT_FAILURE, "Not enough cores\n");
426 if (qconf != &lcore_queue_conf[rx_lcore_id])
427 /* Assigned a new logical core in the loop above. */
428 qconf = &lcore_queue_conf[rx_lcore_id];
430 qconf->rx_port_list[qconf->n_rx_port] = portid;
431 qconf->port_param[qconf->n_rx_port] = &ctrl->vm_ports[portid];
434 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned) portid);
439 /* indicate that we are ready to forward */
440 ctrl->state = STATE_FWD;
443 rte_spinlock_unlock(&ctrl->lock);
445 /* launch per-lcore init on every lcore */
446 rte_eal_mp_remote_launch(l2fwd_ivshmem_launch_one_lcore, NULL, CALL_MASTER);
447 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
448 if (rte_eal_wait_lcore(lcore_id) < 0)