4 * Copyright(c) 2010-2016 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_malloc.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
49 #include <rte_atomic.h>
50 #include <rte_branch_prediction.h>
52 #include <rte_per_lcore.h>
53 #include <rte_lcore.h>
55 #include <rte_launch.h>
56 #include <rte_debug.h>
57 #include <rte_mempool.h>
59 #include <rte_interrupts.h>
61 #include <rte_ether.h>
62 #include <rte_ethdev.h>
63 #include <rte_string_fns.h>
67 /* Number of packets to attempt to read from queue */
68 #define PKT_READ_SIZE ((uint16_t)32)
70 /* our client id number - tells us which rx queue to read, and NIC TX
71 * queue to write to. */
72 static uint8_t client_id = 0;
74 #define MBQ_CAPACITY 32
76 /* maps input ports to output ports for packets */
77 static uint16_t output_ports[RTE_MAX_ETHPORTS];
79 /* buffers up a set of packet that are ready to send */
80 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
82 /* shared data from server. We update statistics here */
83 static volatile struct tx_stats *tx_stats;
87 * print a usage message
90 usage(const char *progname)
92 printf("Usage: %s [EAL args] -- -n <client_id>\n\n", progname);
96 * Convert the client id number from a string to an int.
99 parse_client_num(const char *client)
104 if (client == NULL || *client == '\0')
107 temp = strtoul(client, &end, 10);
108 if (end == NULL || *end != '\0')
111 client_id = (uint8_t)temp;
116 * Parse the application arguments to the client app.
119 parse_app_args(int argc, char *argv[])
121 int option_index, opt;
122 char **argvopt = argv;
123 const char *progname = NULL;
124 static struct option lgopts[] = { /* no long options */
129 while ((opt = getopt_long(argc, argvopt, "n:", lgopts,
130 &option_index)) != EOF){
133 if (parse_client_num(optarg) != 0){
147 * Tx buffer error callback
150 flush_tx_error_callback(struct rte_mbuf **unsent, uint16_t count,
153 uint16_t port_id = (uintptr_t)userdata;
155 tx_stats->tx_drop[port_id] += count;
157 /* free the mbufs which failed from transmit */
158 for (i = 0; i < count; i++)
159 rte_pktmbuf_free(unsent[i]);
164 configure_tx_buffer(uint16_t port_id, uint16_t size)
168 /* Initialize TX buffers */
169 tx_buffer[port_id] = rte_zmalloc_socket("tx_buffer",
170 RTE_ETH_TX_BUFFER_SIZE(size), 0,
171 rte_eth_dev_socket_id(port_id));
172 if (tx_buffer[port_id] == NULL)
173 rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
176 rte_eth_tx_buffer_init(tx_buffer[port_id], size);
178 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[port_id],
179 flush_tx_error_callback, (void *)(intptr_t)port_id);
181 rte_exit(EXIT_FAILURE,
182 "Cannot set error callback for tx buffer on port %u\n",
187 * set up output ports so that all traffic on port gets sent out
188 * its paired port. Index using actual port numbers since that is
189 * what comes in the mbuf structure.
192 configure_output_ports(const struct port_info *ports)
195 if (ports->num_ports > RTE_MAX_ETHPORTS)
196 rte_exit(EXIT_FAILURE, "Too many ethernet ports. RTE_MAX_ETHPORTS = %u\n",
197 (unsigned)RTE_MAX_ETHPORTS);
198 for (i = 0; i < ports->num_ports - 1; i+=2){
199 uint16_t p1 = ports->id[i];
200 uint16_t p2 = ports->id[i+1];
201 output_ports[p1] = p2;
202 output_ports[p2] = p1;
204 configure_tx_buffer(p1, MBQ_CAPACITY);
205 configure_tx_buffer(p2, MBQ_CAPACITY);
211 * This function performs routing of packets
212 * Just sends each input packet out an output port based solely on the input
213 * port it arrived on.
216 handle_packet(struct rte_mbuf *buf)
219 const uint8_t in_port = buf->port;
220 const uint8_t out_port = output_ports[in_port];
221 struct rte_eth_dev_tx_buffer *buffer = tx_buffer[out_port];
223 sent = rte_eth_tx_buffer(out_port, client_id, buffer, buf);
225 tx_stats->tx[out_port] += sent;
230 * Application main function - loops through
231 * receiving and processing packets. Never returns
234 main(int argc, char *argv[])
236 const struct rte_memzone *mz;
237 struct rte_ring *rx_ring;
238 struct rte_mempool *mp;
239 struct port_info *ports;
240 int need_flush = 0; /* indicates whether we have unsent packets */
242 void *pkts[PKT_READ_SIZE];
245 if ((retval = rte_eal_init(argc, argv)) < 0)
250 if (parse_app_args(argc, argv) < 0)
251 rte_exit(EXIT_FAILURE, "Invalid command-line arguments\n");
253 if (rte_eth_dev_count() == 0)
254 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
256 rx_ring = rte_ring_lookup(get_rx_queue_name(client_id));
258 rte_exit(EXIT_FAILURE, "Cannot get RX ring - is server process running?\n");
260 mp = rte_mempool_lookup(PKTMBUF_POOL_NAME);
262 rte_exit(EXIT_FAILURE, "Cannot get mempool for mbufs\n");
264 mz = rte_memzone_lookup(MZ_PORT_INFO);
266 rte_exit(EXIT_FAILURE, "Cannot get port info structure\n");
268 tx_stats = &(ports->tx_stats[client_id]);
270 configure_output_ports(ports);
272 RTE_LOG(INFO, APP, "Finished Process Init.\n");
274 printf("\nClient process %d handling packets\n", client_id);
275 printf("[Press Ctrl-C to quit ...]\n");
281 rx_pkts = rte_ring_dequeue_burst(rx_ring, pkts,
282 PKT_READ_SIZE, NULL);
284 if (unlikely(rx_pkts == 0)){
286 for (port = 0; port < ports->num_ports; port++) {
287 sent = rte_eth_tx_buffer_flush(ports->id[port], client_id,
290 tx_stats->tx[port] += sent;
296 for (i = 0; i < rx_pkts; i++)
297 handle_packet(pkts[i]);