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_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>
69 /* Number of packets to attempt to read from queue */
70 #define PKT_READ_SIZE ((uint16_t)32)
72 /* our client id number - tells us which rx queue to read, and NIC TX
73 * queue to write to. */
74 static uint8_t client_id = 0;
76 #define MBQ_CAPACITY 32
78 /* maps input ports to output ports for packets */
79 static uint8_t output_ports[RTE_MAX_ETHPORTS];
81 /* buffers up a set of packet that are ready to send */
82 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
84 /* shared data from server. We update statistics here */
85 static volatile struct tx_stats *tx_stats;
89 * print a usage message
92 usage(const char *progname)
94 printf("Usage: %s [EAL args] -- -n <client_id>\n\n", progname);
98 * Convert the client id number from a string to an int.
101 parse_client_num(const char *client)
106 if (client == NULL || *client == '\0')
109 temp = strtoul(client, &end, 10);
110 if (end == NULL || *end != '\0')
113 client_id = (uint8_t)temp;
118 * Parse the application arguments to the client app.
121 parse_app_args(int argc, char *argv[])
123 int option_index, opt;
124 char **argvopt = argv;
125 const char *progname = NULL;
126 static struct option lgopts[] = { /* no long options */
131 while ((opt = getopt_long(argc, argvopt, "n:", lgopts,
132 &option_index)) != EOF){
135 if (parse_client_num(optarg) != 0){
149 * Tx buffer error callback
152 flush_tx_error_callback(struct rte_mbuf **unsent, uint16_t count,
155 uint8_t port_id = (uintptr_t)userdata;
157 tx_stats->tx_drop[port_id] += count;
159 /* free the mbufs which failed from transmit */
160 for (i = 0; i < count; i++)
161 rte_pktmbuf_free(unsent[i]);
166 configure_tx_buffer(uint8_t port_id, uint16_t size)
170 /* Initialize TX buffers */
171 tx_buffer[port_id] = rte_zmalloc_socket("tx_buffer",
172 RTE_ETH_TX_BUFFER_SIZE(size), 0,
173 rte_eth_dev_socket_id(port_id));
174 if (tx_buffer[port_id] == NULL)
175 rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
178 rte_eth_tx_buffer_init(tx_buffer[port_id], size);
180 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[port_id],
181 flush_tx_error_callback, (void *)(intptr_t)port_id);
183 rte_exit(EXIT_FAILURE, "Cannot set error callback for "
184 "tx buffer on port %u\n", (unsigned) port_id);
188 * set up output ports so that all traffic on port gets sent out
189 * its paired port. Index using actual port numbers since that is
190 * what comes in the mbuf structure.
193 configure_output_ports(const struct port_info *ports)
196 if (ports->num_ports > RTE_MAX_ETHPORTS)
197 rte_exit(EXIT_FAILURE, "Too many ethernet ports. RTE_MAX_ETHPORTS = %u\n",
198 (unsigned)RTE_MAX_ETHPORTS);
199 for (i = 0; i < ports->num_ports - 1; i+=2){
200 uint8_t p1 = ports->id[i];
201 uint8_t p2 = ports->id[i+1];
202 output_ports[p1] = p2;
203 output_ports[p2] = p1;
205 configure_tx_buffer(p1, MBQ_CAPACITY);
206 configure_tx_buffer(p2, MBQ_CAPACITY);
212 * This function performs routing of packets
213 * Just sends each input packet out an output port based solely on the input
214 * port it arrived on.
217 handle_packet(struct rte_mbuf *buf)
220 const uint8_t in_port = buf->port;
221 const uint8_t out_port = output_ports[in_port];
222 struct rte_eth_dev_tx_buffer *buffer = tx_buffer[out_port];
224 sent = rte_eth_tx_buffer(out_port, client_id, buffer, buf);
226 tx_stats->tx[out_port] += sent;
231 * Application main function - loops through
232 * receiving and processing packets. Never returns
235 main(int argc, char *argv[])
237 const struct rte_memzone *mz;
238 struct rte_ring *rx_ring;
239 struct rte_mempool *mp;
240 struct port_info *ports;
241 int need_flush = 0; /* indicates whether we have unsent packets */
243 void *pkts[PKT_READ_SIZE];
246 if ((retval = rte_eal_init(argc, argv)) < 0)
251 if (parse_app_args(argc, argv) < 0)
252 rte_exit(EXIT_FAILURE, "Invalid command-line arguments\n");
254 if (rte_eth_dev_count() == 0)
255 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
257 rx_ring = rte_ring_lookup(get_rx_queue_name(client_id));
259 rte_exit(EXIT_FAILURE, "Cannot get RX ring - is server process running?\n");
261 mp = rte_mempool_lookup(PKTMBUF_POOL_NAME);
263 rte_exit(EXIT_FAILURE, "Cannot get mempool for mbufs\n");
265 mz = rte_memzone_lookup(MZ_PORT_INFO);
267 rte_exit(EXIT_FAILURE, "Cannot get port info structure\n");
269 tx_stats = &(ports->tx_stats[client_id]);
271 configure_output_ports(ports);
273 RTE_LOG(INFO, APP, "Finished Process Init.\n");
275 printf("\nClient process %d handling packets\n", client_id);
276 printf("[Press Ctrl-C to quit ...]\n");
282 rx_pkts = rte_ring_dequeue_burst(rx_ring, pkts,
283 PKT_READ_SIZE, NULL);
285 if (unlikely(rx_pkts == 0)){
287 for (port = 0; port < ports->num_ports; port++) {
288 sent = rte_eth_tx_buffer_flush(ports->id[port], client_id,
291 tx_stats->tx[port] += sent;
297 for (i = 0; i < rx_pkts; i++)
298 handle_packet(pkts[i]);