13998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
23998e2a0SBruce Richardson * Copyright(c) 2010-2016 Intel Corporation
3af75078fSIntel */
4af75078fSIntel
5af75078fSIntel #include <stdint.h>
6af75078fSIntel #include <stdio.h>
7af75078fSIntel #include <inttypes.h>
8af75078fSIntel #include <stdarg.h>
9af75078fSIntel #include <errno.h>
10af75078fSIntel #include <sys/queue.h>
11af75078fSIntel #include <stdlib.h>
12af75078fSIntel #include <getopt.h>
13af75078fSIntel #include <string.h>
14af75078fSIntel
15af75078fSIntel #include <rte_common.h>
16e2366e74STomasz Kulasek #include <rte_malloc.h>
17af75078fSIntel #include <rte_memory.h>
18af75078fSIntel #include <rte_memzone.h>
19af75078fSIntel #include <rte_eal.h>
20af75078fSIntel #include <rte_branch_prediction.h>
21af75078fSIntel #include <rte_log.h>
22af75078fSIntel #include <rte_per_lcore.h>
23af75078fSIntel #include <rte_lcore.h>
24af75078fSIntel #include <rte_ring.h>
25af75078fSIntel #include <rte_launch.h>
26af75078fSIntel #include <rte_debug.h>
27af75078fSIntel #include <rte_mempool.h>
28af75078fSIntel #include <rte_mbuf.h>
29af75078fSIntel #include <rte_interrupts.h>
30af75078fSIntel #include <rte_ether.h>
31af75078fSIntel #include <rte_ethdev.h>
32af75078fSIntel #include <rte_string_fns.h>
33af75078fSIntel
34af75078fSIntel #include "common.h"
35af75078fSIntel
36af75078fSIntel /* Number of packets to attempt to read from queue */
37af75078fSIntel #define PKT_READ_SIZE ((uint16_t)32)
38af75078fSIntel
39af75078fSIntel /* our client id number - tells us which rx queue to read, and NIC TX
40af75078fSIntel * queue to write to. */
41af75078fSIntel static uint8_t client_id = 0;
42af75078fSIntel
43af75078fSIntel #define MBQ_CAPACITY 32
44af75078fSIntel
45af75078fSIntel /* maps input ports to output ports for packets */
46f8244c63SZhiyong Yang static uint16_t output_ports[RTE_MAX_ETHPORTS];
47af75078fSIntel
48af75078fSIntel /* buffers up a set of packet that are ready to send */
49e2366e74STomasz Kulasek struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
50af75078fSIntel
51af75078fSIntel /* shared data from server. We update statistics here */
52af75078fSIntel static volatile struct tx_stats *tx_stats;
53af75078fSIntel
54af75078fSIntel
55af75078fSIntel /*
56af75078fSIntel * print a usage message
57af75078fSIntel */
58af75078fSIntel static void
usage(const char * progname)59af75078fSIntel usage(const char *progname)
60af75078fSIntel {
61af75078fSIntel printf("Usage: %s [EAL args] -- -n <client_id>\n\n", progname);
62af75078fSIntel }
63af75078fSIntel
64af75078fSIntel /*
65af75078fSIntel * Convert the client id number from a string to an int.
66af75078fSIntel */
67af75078fSIntel static int
parse_client_num(const char * client)68af75078fSIntel parse_client_num(const char *client)
69af75078fSIntel {
70af75078fSIntel char *end = NULL;
71af75078fSIntel unsigned long temp;
72af75078fSIntel
73af75078fSIntel if (client == NULL || *client == '\0')
74af75078fSIntel return -1;
75af75078fSIntel
76af75078fSIntel temp = strtoul(client, &end, 10);
77af75078fSIntel if (end == NULL || *end != '\0')
78af75078fSIntel return -1;
79af75078fSIntel
80af75078fSIntel client_id = (uint8_t)temp;
81af75078fSIntel return 0;
82af75078fSIntel }
83af75078fSIntel
84af75078fSIntel /*
85af75078fSIntel * Parse the application arguments to the client app.
86af75078fSIntel */
87af75078fSIntel static int
parse_app_args(int argc,char * argv[])88af75078fSIntel parse_app_args(int argc, char *argv[])
89af75078fSIntel {
90af75078fSIntel int option_index, opt;
91af75078fSIntel char **argvopt = argv;
92af75078fSIntel const char *progname = NULL;
93af75078fSIntel static struct option lgopts[] = { /* no long options */
94af75078fSIntel {NULL, 0, 0, 0 }
95af75078fSIntel };
96af75078fSIntel progname = argv[0];
97af75078fSIntel
98af75078fSIntel while ((opt = getopt_long(argc, argvopt, "n:", lgopts,
99af75078fSIntel &option_index)) != EOF){
100af75078fSIntel switch (opt){
101af75078fSIntel case 'n':
102af75078fSIntel if (parse_client_num(optarg) != 0){
103af75078fSIntel usage(progname);
104af75078fSIntel return -1;
105af75078fSIntel }
106af75078fSIntel break;
107af75078fSIntel default:
108af75078fSIntel usage(progname);
109af75078fSIntel return -1;
110af75078fSIntel }
111af75078fSIntel }
112af75078fSIntel return 0;
113af75078fSIntel }
114af75078fSIntel
115af75078fSIntel /*
116e2366e74STomasz Kulasek * Tx buffer error callback
117e2366e74STomasz Kulasek */
118e2366e74STomasz Kulasek static void
flush_tx_error_callback(struct rte_mbuf ** unsent,uint16_t count,void * userdata)119e2366e74STomasz Kulasek flush_tx_error_callback(struct rte_mbuf **unsent, uint16_t count,
120e2366e74STomasz Kulasek void *userdata) {
121e2366e74STomasz Kulasek int i;
122f8244c63SZhiyong Yang uint16_t port_id = (uintptr_t)userdata;
123e2366e74STomasz Kulasek
124e2366e74STomasz Kulasek tx_stats->tx_drop[port_id] += count;
125e2366e74STomasz Kulasek
126e2366e74STomasz Kulasek /* free the mbufs which failed from transmit */
127e2366e74STomasz Kulasek for (i = 0; i < count; i++)
128e2366e74STomasz Kulasek rte_pktmbuf_free(unsent[i]);
129e2366e74STomasz Kulasek
130e2366e74STomasz Kulasek }
131e2366e74STomasz Kulasek
132e2366e74STomasz Kulasek static void
configure_tx_buffer(uint16_t port_id,uint16_t size)133f8244c63SZhiyong Yang configure_tx_buffer(uint16_t port_id, uint16_t size)
134e2366e74STomasz Kulasek {
135e2366e74STomasz Kulasek int ret;
136e2366e74STomasz Kulasek
137e2366e74STomasz Kulasek /* Initialize TX buffers */
138e2366e74STomasz Kulasek tx_buffer[port_id] = rte_zmalloc_socket("tx_buffer",
139e2366e74STomasz Kulasek RTE_ETH_TX_BUFFER_SIZE(size), 0,
140e2366e74STomasz Kulasek rte_eth_dev_socket_id(port_id));
141e2366e74STomasz Kulasek if (tx_buffer[port_id] == NULL)
142e2366e74STomasz Kulasek rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
143f8244c63SZhiyong Yang port_id);
144e2366e74STomasz Kulasek
145e2366e74STomasz Kulasek rte_eth_tx_buffer_init(tx_buffer[port_id], size);
146e2366e74STomasz Kulasek
147e2366e74STomasz Kulasek ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[port_id],
148e2366e74STomasz Kulasek flush_tx_error_callback, (void *)(intptr_t)port_id);
149e2366e74STomasz Kulasek if (ret < 0)
150f8244c63SZhiyong Yang rte_exit(EXIT_FAILURE,
151f8244c63SZhiyong Yang "Cannot set error callback for tx buffer on port %u\n",
152f8244c63SZhiyong Yang port_id);
153e2366e74STomasz Kulasek }
154e2366e74STomasz Kulasek
155e2366e74STomasz Kulasek /*
156af75078fSIntel * set up output ports so that all traffic on port gets sent out
157af75078fSIntel * its paired port. Index using actual port numbers since that is
158af75078fSIntel * what comes in the mbuf structure.
159af75078fSIntel */
160e2366e74STomasz Kulasek static void
configure_output_ports(const struct port_info * ports)161e2366e74STomasz Kulasek configure_output_ports(const struct port_info *ports)
162af75078fSIntel {
163af75078fSIntel int i;
164af75078fSIntel if (ports->num_ports > RTE_MAX_ETHPORTS)
165af75078fSIntel rte_exit(EXIT_FAILURE, "Too many ethernet ports. RTE_MAX_ETHPORTS = %u\n",
166af75078fSIntel (unsigned)RTE_MAX_ETHPORTS);
167af75078fSIntel for (i = 0; i < ports->num_ports - 1; i+=2){
168f8244c63SZhiyong Yang uint16_t p1 = ports->id[i];
169f8244c63SZhiyong Yang uint16_t p2 = ports->id[i+1];
170af75078fSIntel output_ports[p1] = p2;
171af75078fSIntel output_ports[p2] = p1;
172e2366e74STomasz Kulasek
173e2366e74STomasz Kulasek configure_tx_buffer(p1, MBQ_CAPACITY);
174e2366e74STomasz Kulasek configure_tx_buffer(p2, MBQ_CAPACITY);
175e2366e74STomasz Kulasek
176af75078fSIntel }
177af75078fSIntel }
178af75078fSIntel
179af75078fSIntel /*
180af75078fSIntel * This function performs routing of packets
181af75078fSIntel * Just sends each input packet out an output port based solely on the input
182af75078fSIntel * port it arrived on.
183af75078fSIntel */
184af75078fSIntel static void
handle_packet(struct rte_mbuf * buf)185af75078fSIntel handle_packet(struct rte_mbuf *buf)
186af75078fSIntel {
187e2366e74STomasz Kulasek int sent;
18847523597SZhiyong Yang const uint16_t in_port = buf->port;
18947523597SZhiyong Yang const uint16_t out_port = output_ports[in_port];
190e2366e74STomasz Kulasek struct rte_eth_dev_tx_buffer *buffer = tx_buffer[out_port];
191af75078fSIntel
192e2366e74STomasz Kulasek sent = rte_eth_tx_buffer(out_port, client_id, buffer, buf);
193e2366e74STomasz Kulasek if (sent)
194e2366e74STomasz Kulasek tx_stats->tx[out_port] += sent;
195e2366e74STomasz Kulasek
196af75078fSIntel }
197af75078fSIntel
198af75078fSIntel /*
199af75078fSIntel * Application main function - loops through
200af75078fSIntel * receiving and processing packets. Never returns
201af75078fSIntel */
202af75078fSIntel int
main(int argc,char * argv[])203af75078fSIntel main(int argc, char *argv[])
204af75078fSIntel {
205af75078fSIntel const struct rte_memzone *mz;
206af75078fSIntel struct rte_ring *rx_ring;
207af75078fSIntel struct rte_mempool *mp;
208af75078fSIntel struct port_info *ports;
209af75078fSIntel int need_flush = 0; /* indicates whether we have unsent packets */
210af75078fSIntel int retval;
211af75078fSIntel void *pkts[PKT_READ_SIZE];
212e2366e74STomasz Kulasek uint16_t sent;
213af75078fSIntel
214af75078fSIntel if ((retval = rte_eal_init(argc, argv)) < 0)
215af75078fSIntel return -1;
216af75078fSIntel argc -= retval;
217af75078fSIntel argv += retval;
218af75078fSIntel
219af75078fSIntel if (parse_app_args(argc, argv) < 0)
220af75078fSIntel rte_exit(EXIT_FAILURE, "Invalid command-line arguments\n");
221af75078fSIntel
222d9a42a69SThomas Monjalon if (rte_eth_dev_count_avail() == 0)
223af75078fSIntel rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
224af75078fSIntel
225af75078fSIntel rx_ring = rte_ring_lookup(get_rx_queue_name(client_id));
226af75078fSIntel if (rx_ring == NULL)
227af75078fSIntel rte_exit(EXIT_FAILURE, "Cannot get RX ring - is server process running?\n");
228af75078fSIntel
229af75078fSIntel mp = rte_mempool_lookup(PKTMBUF_POOL_NAME);
230af75078fSIntel if (mp == NULL)
231af75078fSIntel rte_exit(EXIT_FAILURE, "Cannot get mempool for mbufs\n");
232af75078fSIntel
233af75078fSIntel mz = rte_memzone_lookup(MZ_PORT_INFO);
234af75078fSIntel if (mz == NULL)
235af75078fSIntel rte_exit(EXIT_FAILURE, "Cannot get port info structure\n");
236af75078fSIntel ports = mz->addr;
237af75078fSIntel tx_stats = &(ports->tx_stats[client_id]);
238af75078fSIntel
239af75078fSIntel configure_output_ports(ports);
240af75078fSIntel
241af75078fSIntel RTE_LOG(INFO, APP, "Finished Process Init.\n");
242af75078fSIntel
243af75078fSIntel printf("\nClient process %d handling packets\n", client_id);
244af75078fSIntel printf("[Press Ctrl-C to quit ...]\n");
245af75078fSIntel
246af75078fSIntel for (;;) {
247cfa7c9e6SBruce Richardson uint16_t i, rx_pkts;
248af75078fSIntel
249ecaed092SBruce Richardson rx_pkts = rte_ring_dequeue_burst(rx_ring, pkts,
250ecaed092SBruce Richardson PKT_READ_SIZE, NULL);
251af75078fSIntel
2526b124806SStephen Hemminger if (rx_pkts == 0 && need_flush) {
2536b124806SStephen Hemminger for (i = 0; i < ports->num_ports; i++) {
2546b124806SStephen Hemminger uint16_t port = ports->id[i];
2556b124806SStephen Hemminger
2566b124806SStephen Hemminger sent = rte_eth_tx_buffer_flush(port,
2576b124806SStephen Hemminger client_id,
258e2366e74STomasz Kulasek tx_buffer[port]);
259e2366e74STomasz Kulasek tx_stats->tx[port] += sent;
260e2366e74STomasz Kulasek }
261af75078fSIntel need_flush = 0;
262af75078fSIntel continue;
263af75078fSIntel }
264af75078fSIntel
265af75078fSIntel for (i = 0; i < rx_pkts; i++)
266af75078fSIntel handle_packet(pkts[i]);
267af75078fSIntel
268af75078fSIntel need_flush = 1;
269af75078fSIntel }
270*10aa3757SChengchang Tang
271*10aa3757SChengchang Tang /* clean up the EAL */
272*10aa3757SChengchang Tang rte_eal_cleanup();
273af75078fSIntel }
274