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.
39 #include <sys/queue.h>
44 #include <rte_common.h>
45 #include <rte_memory.h>
46 #include <rte_memzone.h>
47 #include <rte_tailq.h>
49 #include <rte_atomic.h>
50 #include <rte_branch_prediction.h>
52 #include <rte_per_lcore.h>
53 #include <rte_launch.h>
54 #include <rte_lcore.h>
56 #include <rte_launch.h>
57 #include <rte_lcore.h>
58 #include <rte_debug.h>
59 #include <rte_mempool.h>
61 #include <rte_interrupts.h>
63 #include <rte_ether.h>
64 #include <rte_ethdev.h>
65 #include <rte_string_fns.h>
68 #include "init_drivers.h"
70 /* Number of packets to attempt to read from queue */
71 #define PKT_READ_SIZE ((uint16_t)32)
73 /* our client id number - tells us which rx queue to read, and NIC TX
74 * queue to write to. */
75 static uint8_t client_id = 0;
78 #define MBQ_CAPACITY 32
79 struct rte_mbuf *bufs[MBQ_CAPACITY];
83 /* maps input ports to output ports for packets */
84 static uint8_t output_ports[RTE_MAX_ETHPORTS];
86 /* buffers up a set of packet that are ready to send */
87 static struct mbuf_queue output_bufs[RTE_MAX_ETHPORTS];
89 /* shared data from server. We update statistics here */
90 static volatile struct tx_stats *tx_stats;
94 * print a usage message
97 usage(const char *progname)
99 printf("Usage: %s [EAL args] -- -n <client_id>\n\n", progname);
103 * Convert the client id number from a string to an int.
106 parse_client_num(const char *client)
111 if (client == NULL || *client == '\0')
114 temp = strtoul(client, &end, 10);
115 if (end == NULL || *end != '\0')
118 client_id = (uint8_t)temp;
123 * Parse the application arguments to the client app.
126 parse_app_args(int argc, char *argv[])
128 int option_index, opt;
129 char **argvopt = argv;
130 const char *progname = NULL;
131 static struct option lgopts[] = { /* no long options */
136 while ((opt = getopt_long(argc, argvopt, "n:", lgopts,
137 &option_index)) != EOF){
140 if (parse_client_num(optarg) != 0){
154 * set up output ports so that all traffic on port gets sent out
155 * its paired port. Index using actual port numbers since that is
156 * what comes in the mbuf structure.
158 static void configure_output_ports(const struct port_info *ports)
161 if (ports->num_ports > RTE_MAX_ETHPORTS)
162 rte_exit(EXIT_FAILURE, "Too many ethernet ports. RTE_MAX_ETHPORTS = %u\n",
163 (unsigned)RTE_MAX_ETHPORTS);
164 for (i = 0; i < ports->num_ports - 1; i+=2){
165 uint8_t p1 = ports->id[i];
166 uint8_t p2 = ports->id[i+1];
167 output_ports[p1] = p2;
168 output_ports[p2] = p1;
174 send_packets(uint8_t port)
177 struct mbuf_queue *mbq = &output_bufs[port];
179 if (unlikely(mbq->top == 0))
182 sent = rte_eth_tx_burst(port, client_id, mbq->bufs, mbq->top);
183 if (unlikely(sent < mbq->top)){
184 for (i = sent; i < mbq->top; i++)
185 rte_pktmbuf_free(mbq->bufs[i]);
186 tx_stats->tx_drop[port] += (mbq->top - sent);
188 tx_stats->tx[port] += sent;
193 * Enqueue a packet to be sent on a particular port, but
194 * don't send it yet. Only when the buffer is full.
197 enqueue_packet(struct rte_mbuf *buf, uint8_t port)
199 struct mbuf_queue *mbq = &output_bufs[port];
200 mbq->bufs[mbq->top++] = buf;
202 if (mbq->top == MBQ_CAPACITY)
207 * This function performs routing of packets
208 * Just sends each input packet out an output port based solely on the input
209 * port it arrived on.
212 handle_packet(struct rte_mbuf *buf)
214 const uint8_t in_port = buf->port;
215 const uint8_t out_port = output_ports[in_port];
217 enqueue_packet(buf, out_port);
221 * Application main function - loops through
222 * receiving and processing packets. Never returns
225 main(int argc, char *argv[])
227 const struct rte_memzone *mz;
228 struct rte_ring *rx_ring;
229 struct rte_mempool *mp;
230 struct port_info *ports;
231 int need_flush = 0; /* indicates whether we have unsent packets */
233 void *pkts[PKT_READ_SIZE];
235 if ((retval = rte_eal_init(argc, argv)) < 0)
240 if (parse_app_args(argc, argv) < 0)
241 rte_exit(EXIT_FAILURE, "Invalid command-line arguments\n");
243 if (init_drivers() < 0)
244 rte_exit(EXIT_FAILURE, "Cannot get NIC ports\n");
245 if (rte_eth_dev_count() == 0)
246 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
248 rx_ring = rte_ring_lookup(get_rx_queue_name(client_id));
250 rte_exit(EXIT_FAILURE, "Cannot get RX ring - is server process running?\n");
252 mp = rte_mempool_lookup(PKTMBUF_POOL_NAME);
254 rte_exit(EXIT_FAILURE, "Cannot get mempool for mbufs\n");
256 mz = rte_memzone_lookup(MZ_PORT_INFO);
258 rte_exit(EXIT_FAILURE, "Cannot get port info structure\n");
260 tx_stats = &(ports->tx_stats[client_id]);
262 configure_output_ports(ports);
264 RTE_LOG(INFO, APP, "Finished Process Init.\n");
266 printf("\nClient process %d handling packets\n", client_id);
267 printf("[Press Ctrl-C to quit ...]\n");
270 uint16_t i, rx_pkts = PKT_READ_SIZE;
273 /* try dequeuing max possible packets first, if that fails, get the
274 * most we can. Loop body should only execute once, maximum */
275 while (rx_pkts > 0 &&
276 unlikely(rte_ring_dequeue_bulk(rx_ring, pkts, rx_pkts) != 0))
277 rx_pkts = (uint16_t)RTE_MIN(rte_ring_count(rx_ring), PKT_READ_SIZE);
279 if (unlikely(rx_pkts == 0)){
281 for (port = 0; port < ports->num_ports; port++)
282 send_packets(ports->id[port]);
287 for (i = 0; i < rx_pkts; i++)
288 handle_packet(pkts[i]);