xref: /dpdk/examples/multi_process/client_server_mp/mp_client/client.c (revision ceb1ccd5d50c1a89ba8bdd97cc199e7f07422b98)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33 
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <inttypes.h>
37 #include <stdarg.h>
38 #include <errno.h>
39 #include <sys/queue.h>
40 #include <stdlib.h>
41 #include <getopt.h>
42 #include <string.h>
43 
44 #include <rte_common.h>
45 #include <rte_malloc.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_eal.h>
49 #include <rte_atomic.h>
50 #include <rte_branch_prediction.h>
51 #include <rte_log.h>
52 #include <rte_per_lcore.h>
53 #include <rte_launch.h>
54 #include <rte_lcore.h>
55 #include <rte_ring.h>
56 #include <rte_launch.h>
57 #include <rte_lcore.h>
58 #include <rte_debug.h>
59 #include <rte_mempool.h>
60 #include <rte_mbuf.h>
61 #include <rte_interrupts.h>
62 #include <rte_pci.h>
63 #include <rte_ether.h>
64 #include <rte_ethdev.h>
65 #include <rte_string_fns.h>
66 
67 #include "common.h"
68 
69 /* Number of packets to attempt to read from queue */
70 #define PKT_READ_SIZE  ((uint16_t)32)
71 
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;
75 
76 #define MBQ_CAPACITY 32
77 
78 /* maps input ports to output ports for packets */
79 static uint8_t output_ports[RTE_MAX_ETHPORTS];
80 
81 /* buffers up a set of packet that are ready to send */
82 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
83 
84 /* shared data from server. We update statistics here */
85 static volatile struct tx_stats *tx_stats;
86 
87 
88 /*
89  * print a usage message
90  */
91 static void
92 usage(const char *progname)
93 {
94 	printf("Usage: %s [EAL args] -- -n <client_id>\n\n", progname);
95 }
96 
97 /*
98  * Convert the client id number from a string to an int.
99  */
100 static int
101 parse_client_num(const char *client)
102 {
103 	char *end = NULL;
104 	unsigned long temp;
105 
106 	if (client == NULL || *client == '\0')
107 		return -1;
108 
109 	temp = strtoul(client, &end, 10);
110 	if (end == NULL || *end != '\0')
111 		return -1;
112 
113 	client_id = (uint8_t)temp;
114 	return 0;
115 }
116 
117 /*
118  * Parse the application arguments to the client app.
119  */
120 static int
121 parse_app_args(int argc, char *argv[])
122 {
123 	int option_index, opt;
124 	char **argvopt = argv;
125 	const char *progname = NULL;
126 	static struct option lgopts[] = { /* no long options */
127 		{NULL, 0, 0, 0 }
128 	};
129 	progname = argv[0];
130 
131 	while ((opt = getopt_long(argc, argvopt, "n:", lgopts,
132 		&option_index)) != EOF){
133 		switch (opt){
134 			case 'n':
135 				if (parse_client_num(optarg) != 0){
136 					usage(progname);
137 					return -1;
138 				}
139 				break;
140 			default:
141 				usage(progname);
142 				return -1;
143 		}
144 	}
145 	return 0;
146 }
147 
148 /*
149  * Tx buffer error callback
150  */
151 static void
152 flush_tx_error_callback(struct rte_mbuf **unsent, uint16_t count,
153 		void *userdata) {
154 	int i;
155 	uint8_t port_id = (uintptr_t)userdata;
156 
157 	tx_stats->tx_drop[port_id] += count;
158 
159 	/* free the mbufs which failed from transmit */
160 	for (i = 0; i < count; i++)
161 		rte_pktmbuf_free(unsent[i]);
162 
163 }
164 
165 static void
166 configure_tx_buffer(uint8_t port_id, uint16_t size)
167 {
168 	int ret;
169 
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",
176 				(unsigned) port_id);
177 
178 	rte_eth_tx_buffer_init(tx_buffer[port_id], size);
179 
180 	ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[port_id],
181 			flush_tx_error_callback, (void *)(intptr_t)port_id);
182 	if (ret < 0)
183 			rte_exit(EXIT_FAILURE, "Cannot set error callback for "
184 					"tx buffer on port %u\n", (unsigned) port_id);
185 }
186 
187 /*
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.
191  */
192 static void
193 configure_output_ports(const struct port_info *ports)
194 {
195 	int i;
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;
204 
205 		configure_tx_buffer(p1, MBQ_CAPACITY);
206 		configure_tx_buffer(p2, MBQ_CAPACITY);
207 
208 	}
209 }
210 
211 /*
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.
215  */
216 static void
217 handle_packet(struct rte_mbuf *buf)
218 {
219 	int sent;
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];
223 
224 	sent = rte_eth_tx_buffer(out_port, client_id, buffer, buf);
225 	if (sent)
226 		tx_stats->tx[out_port] += sent;
227 
228 }
229 
230 /*
231  * Application main function - loops through
232  * receiving and processing packets. Never returns
233  */
234 int
235 main(int argc, char *argv[])
236 {
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 */
242 	int retval;
243 	void *pkts[PKT_READ_SIZE];
244 	uint16_t sent;
245 
246 	if ((retval = rte_eal_init(argc, argv)) < 0)
247 		return -1;
248 	argc -= retval;
249 	argv += retval;
250 
251 	if (parse_app_args(argc, argv) < 0)
252 		rte_exit(EXIT_FAILURE, "Invalid command-line arguments\n");
253 
254 	if (rte_eth_dev_count() == 0)
255 		rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
256 
257 	rx_ring = rte_ring_lookup(get_rx_queue_name(client_id));
258 	if (rx_ring == NULL)
259 		rte_exit(EXIT_FAILURE, "Cannot get RX ring - is server process running?\n");
260 
261 	mp = rte_mempool_lookup(PKTMBUF_POOL_NAME);
262 	if (mp == NULL)
263 		rte_exit(EXIT_FAILURE, "Cannot get mempool for mbufs\n");
264 
265 	mz = rte_memzone_lookup(MZ_PORT_INFO);
266 	if (mz == NULL)
267 		rte_exit(EXIT_FAILURE, "Cannot get port info structure\n");
268 	ports = mz->addr;
269 	tx_stats = &(ports->tx_stats[client_id]);
270 
271 	configure_output_ports(ports);
272 
273 	RTE_LOG(INFO, APP, "Finished Process Init.\n");
274 
275 	printf("\nClient process %d handling packets\n", client_id);
276 	printf("[Press Ctrl-C to quit ...]\n");
277 
278 	for (;;) {
279 		uint16_t i, rx_pkts = PKT_READ_SIZE;
280 		uint8_t port;
281 
282 		/* try dequeuing max possible packets first, if that fails, get the
283 		 * most we can. Loop body should only execute once, maximum */
284 		while (rx_pkts > 0 &&
285 				unlikely(rte_ring_dequeue_bulk(rx_ring, pkts, rx_pkts) != 0))
286 			rx_pkts = (uint16_t)RTE_MIN(rte_ring_count(rx_ring), PKT_READ_SIZE);
287 
288 		if (unlikely(rx_pkts == 0)){
289 			if (need_flush)
290 				for (port = 0; port < ports->num_ports; port++) {
291 					sent = rte_eth_tx_buffer_flush(ports->id[port], client_id,
292 							tx_buffer[port]);
293 					if (unlikely(sent))
294 						tx_stats->tx[port] += sent;
295 				}
296 			need_flush = 0;
297 			continue;
298 		}
299 
300 		for (i = 0; i < rx_pkts; i++)
301 			handle_packet(pkts[i]);
302 
303 		need_flush = 1;
304 	}
305 }
306