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>
60 #include <rte_ether.h>
61 #include <rte_ethdev.h>
62 #include <rte_string_fns.h>
66 /* Number of packets to attempt to read from queue */
67 #define PKT_READ_SIZE ((uint16_t)32)
69 /* our client id number - tells us which rx queue to read, and NIC TX
70 * queue to write to. */
71 static uint8_t client_id = 0;
73 #define MBQ_CAPACITY 32
75 /* maps input ports to output ports for packets */
76 static uint16_t output_ports[RTE_MAX_ETHPORTS];
78 /* buffers up a set of packet that are ready to send */
79 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
81 /* shared data from server. We update statistics here */
82 static volatile struct tx_stats *tx_stats;
86 * print a usage message
89 usage(const char *progname)
91 printf("Usage: %s [EAL args] -- -n <client_id>\n\n", progname);
95 * Convert the client id number from a string to an int.
98 parse_client_num(const char *client)
103 if (client == NULL || *client == '\0')
106 temp = strtoul(client, &end, 10);
107 if (end == NULL || *end != '\0')
110 client_id = (uint8_t)temp;
115 * Parse the application arguments to the client app.
118 parse_app_args(int argc, char *argv[])
120 int option_index, opt;
121 char **argvopt = argv;
122 const char *progname = NULL;
123 static struct option lgopts[] = { /* no long options */
128 while ((opt = getopt_long(argc, argvopt, "n:", lgopts,
129 &option_index)) != EOF){
132 if (parse_client_num(optarg) != 0){
146 * Tx buffer error callback
149 flush_tx_error_callback(struct rte_mbuf **unsent, uint16_t count,
152 uint16_t port_id = (uintptr_t)userdata;
154 tx_stats->tx_drop[port_id] += count;
156 /* free the mbufs which failed from transmit */
157 for (i = 0; i < count; i++)
158 rte_pktmbuf_free(unsent[i]);
163 configure_tx_buffer(uint16_t port_id, uint16_t size)
167 /* Initialize TX buffers */
168 tx_buffer[port_id] = rte_zmalloc_socket("tx_buffer",
169 RTE_ETH_TX_BUFFER_SIZE(size), 0,
170 rte_eth_dev_socket_id(port_id));
171 if (tx_buffer[port_id] == NULL)
172 rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
175 rte_eth_tx_buffer_init(tx_buffer[port_id], size);
177 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[port_id],
178 flush_tx_error_callback, (void *)(intptr_t)port_id);
180 rte_exit(EXIT_FAILURE,
181 "Cannot set error callback for tx buffer on port %u\n",
186 * set up output ports so that all traffic on port gets sent out
187 * its paired port. Index using actual port numbers since that is
188 * what comes in the mbuf structure.
191 configure_output_ports(const struct port_info *ports)
194 if (ports->num_ports > RTE_MAX_ETHPORTS)
195 rte_exit(EXIT_FAILURE, "Too many ethernet ports. RTE_MAX_ETHPORTS = %u\n",
196 (unsigned)RTE_MAX_ETHPORTS);
197 for (i = 0; i < ports->num_ports - 1; i+=2){
198 uint16_t p1 = ports->id[i];
199 uint16_t p2 = ports->id[i+1];
200 output_ports[p1] = p2;
201 output_ports[p2] = p1;
203 configure_tx_buffer(p1, MBQ_CAPACITY);
204 configure_tx_buffer(p2, MBQ_CAPACITY);
210 * This function performs routing of packets
211 * Just sends each input packet out an output port based solely on the input
212 * port it arrived on.
215 handle_packet(struct rte_mbuf *buf)
218 const uint16_t in_port = buf->port;
219 const uint16_t out_port = output_ports[in_port];
220 struct rte_eth_dev_tx_buffer *buffer = tx_buffer[out_port];
222 sent = rte_eth_tx_buffer(out_port, client_id, buffer, buf);
224 tx_stats->tx[out_port] += sent;
229 * Application main function - loops through
230 * receiving and processing packets. Never returns
233 main(int argc, char *argv[])
235 const struct rte_memzone *mz;
236 struct rte_ring *rx_ring;
237 struct rte_mempool *mp;
238 struct port_info *ports;
239 int need_flush = 0; /* indicates whether we have unsent packets */
241 void *pkts[PKT_READ_SIZE];
244 if ((retval = rte_eal_init(argc, argv)) < 0)
249 if (parse_app_args(argc, argv) < 0)
250 rte_exit(EXIT_FAILURE, "Invalid command-line arguments\n");
252 if (rte_eth_dev_count() == 0)
253 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
255 rx_ring = rte_ring_lookup(get_rx_queue_name(client_id));
257 rte_exit(EXIT_FAILURE, "Cannot get RX ring - is server process running?\n");
259 mp = rte_mempool_lookup(PKTMBUF_POOL_NAME);
261 rte_exit(EXIT_FAILURE, "Cannot get mempool for mbufs\n");
263 mz = rte_memzone_lookup(MZ_PORT_INFO);
265 rte_exit(EXIT_FAILURE, "Cannot get port info structure\n");
267 tx_stats = &(ports->tx_stats[client_id]);
269 configure_output_ports(ports);
271 RTE_LOG(INFO, APP, "Finished Process Init.\n");
273 printf("\nClient process %d handling packets\n", client_id);
274 printf("[Press Ctrl-C to quit ...]\n");
280 rx_pkts = rte_ring_dequeue_burst(rx_ring, pkts,
281 PKT_READ_SIZE, NULL);
283 if (unlikely(rx_pkts == 0)){
285 for (port = 0; port < ports->num_ports; port++) {
286 sent = rte_eth_tx_buffer_flush(ports->id[port], client_id,
289 tx_stats->tx[port] += sent;
295 for (i = 0; i < rx_pkts; i++)
296 handle_packet(pkts[i]);