New upstream version 17.11-rc3
[deb_dpdk.git] / examples / multi_process / client_server_mp / mp_client / client.c
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_lcore.h>
54 #include <rte_ring.h>
55 #include <rte_launch.h>
56 #include <rte_debug.h>
57 #include <rte_mempool.h>
58 #include <rte_mbuf.h>
59 #include <rte_interrupts.h>
60 #include <rte_ether.h>
61 #include <rte_ethdev.h>
62 #include <rte_string_fns.h>
63
64 #include "common.h"
65
66 /* Number of packets to attempt to read from queue */
67 #define PKT_READ_SIZE  ((uint16_t)32)
68
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;
72
73 #define MBQ_CAPACITY 32
74
75 /* maps input ports to output ports for packets */
76 static uint16_t output_ports[RTE_MAX_ETHPORTS];
77
78 /* buffers up a set of packet that are ready to send */
79 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
80
81 /* shared data from server. We update statistics here */
82 static volatile struct tx_stats *tx_stats;
83
84
85 /*
86  * print a usage message
87  */
88 static void
89 usage(const char *progname)
90 {
91         printf("Usage: %s [EAL args] -- -n <client_id>\n\n", progname);
92 }
93
94 /*
95  * Convert the client id number from a string to an int.
96  */
97 static int
98 parse_client_num(const char *client)
99 {
100         char *end = NULL;
101         unsigned long temp;
102
103         if (client == NULL || *client == '\0')
104                 return -1;
105
106         temp = strtoul(client, &end, 10);
107         if (end == NULL || *end != '\0')
108                 return -1;
109
110         client_id = (uint8_t)temp;
111         return 0;
112 }
113
114 /*
115  * Parse the application arguments to the client app.
116  */
117 static int
118 parse_app_args(int argc, char *argv[])
119 {
120         int option_index, opt;
121         char **argvopt = argv;
122         const char *progname = NULL;
123         static struct option lgopts[] = { /* no long options */
124                 {NULL, 0, 0, 0 }
125         };
126         progname = argv[0];
127
128         while ((opt = getopt_long(argc, argvopt, "n:", lgopts,
129                 &option_index)) != EOF){
130                 switch (opt){
131                         case 'n':
132                                 if (parse_client_num(optarg) != 0){
133                                         usage(progname);
134                                         return -1;
135                                 }
136                                 break;
137                         default:
138                                 usage(progname);
139                                 return -1;
140                 }
141         }
142         return 0;
143 }
144
145 /*
146  * Tx buffer error callback
147  */
148 static void
149 flush_tx_error_callback(struct rte_mbuf **unsent, uint16_t count,
150                 void *userdata) {
151         int i;
152         uint16_t port_id = (uintptr_t)userdata;
153
154         tx_stats->tx_drop[port_id] += count;
155
156         /* free the mbufs which failed from transmit */
157         for (i = 0; i < count; i++)
158                 rte_pktmbuf_free(unsent[i]);
159
160 }
161
162 static void
163 configure_tx_buffer(uint16_t port_id, uint16_t size)
164 {
165         int ret;
166
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",
173                          port_id);
174
175         rte_eth_tx_buffer_init(tx_buffer[port_id], size);
176
177         ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[port_id],
178                         flush_tx_error_callback, (void *)(intptr_t)port_id);
179         if (ret < 0)
180                 rte_exit(EXIT_FAILURE,
181                 "Cannot set error callback for tx buffer on port %u\n",
182                          port_id);
183 }
184
185 /*
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.
189  */
190 static void
191 configure_output_ports(const struct port_info *ports)
192 {
193         int i;
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;
202
203                 configure_tx_buffer(p1, MBQ_CAPACITY);
204                 configure_tx_buffer(p2, MBQ_CAPACITY);
205
206         }
207 }
208
209 /*
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.
213  */
214 static void
215 handle_packet(struct rte_mbuf *buf)
216 {
217         int sent;
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];
221
222         sent = rte_eth_tx_buffer(out_port, client_id, buffer, buf);
223         if (sent)
224                 tx_stats->tx[out_port] += sent;
225
226 }
227
228 /*
229  * Application main function - loops through
230  * receiving and processing packets. Never returns
231  */
232 int
233 main(int argc, char *argv[])
234 {
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 */
240         int retval;
241         void *pkts[PKT_READ_SIZE];
242         uint16_t sent;
243
244         if ((retval = rte_eal_init(argc, argv)) < 0)
245                 return -1;
246         argc -= retval;
247         argv += retval;
248
249         if (parse_app_args(argc, argv) < 0)
250                 rte_exit(EXIT_FAILURE, "Invalid command-line arguments\n");
251
252         if (rte_eth_dev_count() == 0)
253                 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
254
255         rx_ring = rte_ring_lookup(get_rx_queue_name(client_id));
256         if (rx_ring == NULL)
257                 rte_exit(EXIT_FAILURE, "Cannot get RX ring - is server process running?\n");
258
259         mp = rte_mempool_lookup(PKTMBUF_POOL_NAME);
260         if (mp == NULL)
261                 rte_exit(EXIT_FAILURE, "Cannot get mempool for mbufs\n");
262
263         mz = rte_memzone_lookup(MZ_PORT_INFO);
264         if (mz == NULL)
265                 rte_exit(EXIT_FAILURE, "Cannot get port info structure\n");
266         ports = mz->addr;
267         tx_stats = &(ports->tx_stats[client_id]);
268
269         configure_output_ports(ports);
270
271         RTE_LOG(INFO, APP, "Finished Process Init.\n");
272
273         printf("\nClient process %d handling packets\n", client_id);
274         printf("[Press Ctrl-C to quit ...]\n");
275
276         for (;;) {
277                 uint16_t i, rx_pkts;
278                 uint16_t port;
279
280                 rx_pkts = rte_ring_dequeue_burst(rx_ring, pkts,
281                                 PKT_READ_SIZE, NULL);
282
283                 if (unlikely(rx_pkts == 0)){
284                         if (need_flush)
285                                 for (port = 0; port < ports->num_ports; port++) {
286                                         sent = rte_eth_tx_buffer_flush(ports->id[port], client_id,
287                                                         tx_buffer[port]);
288                                         if (unlikely(sent))
289                                                 tx_stats->tx[port] += sent;
290                                 }
291                         need_flush = 0;
292                         continue;
293                 }
294
295                 for (i = 0; i < rx_pkts; i++)
296                         handle_packet(pkts[i]);
297
298                 need_flush = 1;
299         }
300 }