xref: /dpdk/examples/multi_process/client_server_mp/mp_client/client.c (revision af75078fece3615088e561357c1e97603e43a5fe)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2012 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  *  version: DPDK.L.1.2.3-3
34  */
35 
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <inttypes.h>
39 #include <stdarg.h>
40 #include <errno.h>
41 #include <sys/queue.h>
42 #include <stdlib.h>
43 #include <getopt.h>
44 #include <string.h>
45 
46 #include <rte_common.h>
47 #include <rte_memory.h>
48 #include <rte_memzone.h>
49 #include <rte_tailq.h>
50 #include <rte_eal.h>
51 #include <rte_atomic.h>
52 #include <rte_branch_prediction.h>
53 #include <rte_log.h>
54 #include <rte_per_lcore.h>
55 #include <rte_launch.h>
56 #include <rte_lcore.h>
57 #include <rte_ring.h>
58 #include <rte_launch.h>
59 #include <rte_lcore.h>
60 #include <rte_debug.h>
61 #include <rte_mempool.h>
62 #include <rte_mbuf.h>
63 #include <rte_interrupts.h>
64 #include <rte_pci.h>
65 #include <rte_ether.h>
66 #include <rte_ethdev.h>
67 #include <rte_string_fns.h>
68 
69 #include "common.h"
70 #include "init_drivers.h"
71 
72 /* Number of packets to attempt to read from queue */
73 #define PKT_READ_SIZE  ((uint16_t)32)
74 
75 /* our client id number - tells us which rx queue to read, and NIC TX
76  * queue to write to. */
77 static uint8_t client_id = 0;
78 
79 struct mbuf_queue {
80 #define MBQ_CAPACITY 32
81 	struct rte_mbuf *bufs[MBQ_CAPACITY];
82 	uint16_t top;
83 };
84 
85 /* maps input ports to output ports for packets */
86 static uint8_t output_ports[RTE_MAX_ETHPORTS];
87 
88 /* buffers up a set of packet that are ready to send */
89 static struct mbuf_queue output_bufs[RTE_MAX_ETHPORTS];
90 
91 /* shared data from server. We update statistics here */
92 static volatile struct tx_stats *tx_stats;
93 
94 
95 /*
96  * print a usage message
97  */
98 static void
99 usage(const char *progname)
100 {
101 	printf("Usage: %s [EAL args] -- -n <client_id>\n\n", progname);
102 }
103 
104 /*
105  * Convert the client id number from a string to an int.
106  */
107 static int
108 parse_client_num(const char *client)
109 {
110 	char *end = NULL;
111 	unsigned long temp;
112 
113 	if (client == NULL || *client == '\0')
114 		return -1;
115 
116 	temp = strtoul(client, &end, 10);
117 	if (end == NULL || *end != '\0')
118 		return -1;
119 
120 	client_id = (uint8_t)temp;
121 	return 0;
122 }
123 
124 /*
125  * Parse the application arguments to the client app.
126  */
127 static int
128 parse_app_args(int argc, char *argv[])
129 {
130 	int option_index, opt;
131 	char **argvopt = argv;
132 	const char *progname = NULL;
133 	static struct option lgopts[] = { /* no long options */
134 		{NULL, 0, 0, 0 }
135 	};
136 	progname = argv[0];
137 
138 	while ((opt = getopt_long(argc, argvopt, "n:", lgopts,
139 		&option_index)) != EOF){
140 		switch (opt){
141 			case 'n':
142 				if (parse_client_num(optarg) != 0){
143 					usage(progname);
144 					return -1;
145 				}
146 				break;
147 			default:
148 				usage(progname);
149 				return -1;
150 		}
151 	}
152 	return 0;
153 }
154 
155 /*
156  * set up output ports so that all traffic on port gets sent out
157  * its paired port. Index using actual port numbers since that is
158  * what comes in the mbuf structure.
159  */
160 static void configure_output_ports(const struct port_info *ports)
161 {
162 	int i;
163 	if (ports->num_ports > RTE_MAX_ETHPORTS)
164 		rte_exit(EXIT_FAILURE, "Too many ethernet ports. RTE_MAX_ETHPORTS = %u\n",
165 				(unsigned)RTE_MAX_ETHPORTS);
166 	for (i = 0; i < ports->num_ports - 1; i+=2){
167 		uint8_t p1 = ports->id[i];
168 		uint8_t p2 = ports->id[i+1];
169 		output_ports[p1] = p2;
170 		output_ports[p2] = p1;
171 	}
172 }
173 
174 
175 static inline void
176 send_packets(uint8_t port)
177 {
178 	uint16_t i, sent;
179 	struct mbuf_queue *mbq = &output_bufs[port];
180 
181 	if (unlikely(mbq->top == 0))
182 		return;
183 
184 	sent = rte_eth_tx_burst(port, client_id, mbq->bufs, mbq->top);
185 	if (unlikely(sent < mbq->top)){
186 		for (i = sent; i < mbq->top; i++)
187 			rte_pktmbuf_free(mbq->bufs[i]);
188 		tx_stats->tx_drop[port] += (mbq->top - sent);
189 	}
190 	tx_stats->tx[port] += sent;
191 	mbq->top = 0;
192 }
193 
194 /*
195  * Enqueue a packet to be sent on a particular port, but
196  * don't send it yet. Only when the buffer is full.
197  */
198 static inline void
199 enqueue_packet(struct rte_mbuf *buf, uint8_t port)
200 {
201 	struct mbuf_queue *mbq = &output_bufs[port];
202 	mbq->bufs[mbq->top++] = buf;
203 
204 	if (mbq->top == MBQ_CAPACITY)
205 		send_packets(port);
206 }
207 
208 /*
209  * This function performs routing of packets
210  * Just sends each input packet out an output port based solely on the input
211  * port it arrived on.
212  */
213 static void
214 handle_packet(struct rte_mbuf *buf)
215 {
216 	const uint8_t in_port = buf->pkt.in_port;
217 	const uint8_t out_port = output_ports[in_port];
218 
219 	enqueue_packet(buf, out_port);
220 }
221 
222 /*
223  * Application main function - loops through
224  * receiving and processing packets. Never returns
225  */
226 int
227 main(int argc, char *argv[])
228 {
229 	const struct rte_memzone *mz;
230 	struct rte_ring *rx_ring;
231 	struct rte_mempool *mp;
232 	struct port_info *ports;
233 	int need_flush = 0; /* indicates whether we have unsent packets */
234 	int retval;
235 	void *pkts[PKT_READ_SIZE];
236 
237 	if ((retval = rte_eal_init(argc, argv)) < 0)
238 		return -1;
239 	argc -= retval;
240 	argv += retval;
241 
242 	if (parse_app_args(argc, argv) < 0)
243 		rte_exit(EXIT_FAILURE, "Invalid command-line arguments\n");
244 
245 	if (init_drivers() < 0)
246 		rte_exit(EXIT_FAILURE, "Cannot get NIC ports\n");
247 	if (rte_eth_dev_count() == 0)
248 		rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
249 
250 	rx_ring = rte_ring_lookup(get_rx_queue_name(client_id));
251 	if (rx_ring == NULL)
252 		rte_exit(EXIT_FAILURE, "Cannot get RX ring - is server process running?\n");
253 
254 	mp = rte_mempool_lookup(PKTMBUF_POOL_NAME);
255 	if (mp == NULL)
256 		rte_exit(EXIT_FAILURE, "Cannot get mempool for mbufs\n");
257 
258 	mz = rte_memzone_lookup(MZ_PORT_INFO);
259 	if (mz == NULL)
260 		rte_exit(EXIT_FAILURE, "Cannot get port info structure\n");
261 	ports = mz->addr;
262 	tx_stats = &(ports->tx_stats[client_id]);
263 
264 	configure_output_ports(ports);
265 
266 	RTE_LOG(INFO, APP, "Finished Process Init.\n");
267 
268 	printf("\nClient process %d handling packets\n", client_id);
269 	printf("[Press Ctrl-C to quit ...]\n");
270 
271 	for (;;) {
272 		uint16_t i, rx_pkts = PKT_READ_SIZE;
273 		uint8_t port;
274 
275 		/* try dequeuing max possible packets first, if that fails, get the
276 		 * most we can. Loop body should only execute once, maximum */
277 		while (rx_pkts > 0 &&
278 				unlikely(rte_ring_dequeue_bulk(rx_ring, pkts, rx_pkts) != 0))
279 			rx_pkts = (uint16_t)RTE_MIN(rte_ring_count(rx_ring), PKT_READ_SIZE);
280 
281 		if (unlikely(rx_pkts == 0)){
282 			if (need_flush)
283 				for (port = 0; port < ports->num_ports; port++)
284 					send_packets(ports->id[port]);
285 			need_flush = 0;
286 			continue;
287 		}
288 
289 		for (i = 0; i < rx_pkts; i++)
290 			handle_packet(pkts[i]);
291 
292 		need_flush = 1;
293 	}
294 }
295