f8453e57ad83ced934451819ac2495a221a33ac3
[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_pci.h>
61 #include <rte_ether.h>
62 #include <rte_ethdev.h>
63 #include <rte_string_fns.h>
64
65 #include "common.h"
66
67 /* Number of packets to attempt to read from queue */
68 #define PKT_READ_SIZE  ((uint16_t)32)
69
70 /* our client id number - tells us which rx queue to read, and NIC TX
71  * queue to write to. */
72 static uint8_t client_id = 0;
73
74 #define MBQ_CAPACITY 32
75
76 /* maps input ports to output ports for packets */
77 static uint8_t output_ports[RTE_MAX_ETHPORTS];
78
79 /* buffers up a set of packet that are ready to send */
80 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
81
82 /* shared data from server. We update statistics here */
83 static volatile struct tx_stats *tx_stats;
84
85
86 /*
87  * print a usage message
88  */
89 static void
90 usage(const char *progname)
91 {
92         printf("Usage: %s [EAL args] -- -n <client_id>\n\n", progname);
93 }
94
95 /*
96  * Convert the client id number from a string to an int.
97  */
98 static int
99 parse_client_num(const char *client)
100 {
101         char *end = NULL;
102         unsigned long temp;
103
104         if (client == NULL || *client == '\0')
105                 return -1;
106
107         temp = strtoul(client, &end, 10);
108         if (end == NULL || *end != '\0')
109                 return -1;
110
111         client_id = (uint8_t)temp;
112         return 0;
113 }
114
115 /*
116  * Parse the application arguments to the client app.
117  */
118 static int
119 parse_app_args(int argc, char *argv[])
120 {
121         int option_index, opt;
122         char **argvopt = argv;
123         const char *progname = NULL;
124         static struct option lgopts[] = { /* no long options */
125                 {NULL, 0, 0, 0 }
126         };
127         progname = argv[0];
128
129         while ((opt = getopt_long(argc, argvopt, "n:", lgopts,
130                 &option_index)) != EOF){
131                 switch (opt){
132                         case 'n':
133                                 if (parse_client_num(optarg) != 0){
134                                         usage(progname);
135                                         return -1;
136                                 }
137                                 break;
138                         default:
139                                 usage(progname);
140                                 return -1;
141                 }
142         }
143         return 0;
144 }
145
146 /*
147  * Tx buffer error callback
148  */
149 static void
150 flush_tx_error_callback(struct rte_mbuf **unsent, uint16_t count,
151                 void *userdata) {
152         int i;
153         uint8_t port_id = (uintptr_t)userdata;
154
155         tx_stats->tx_drop[port_id] += count;
156
157         /* free the mbufs which failed from transmit */
158         for (i = 0; i < count; i++)
159                 rte_pktmbuf_free(unsent[i]);
160
161 }
162
163 static void
164 configure_tx_buffer(uint8_t port_id, uint16_t size)
165 {
166         int ret;
167
168         /* Initialize TX buffers */
169         tx_buffer[port_id] = rte_zmalloc_socket("tx_buffer",
170                         RTE_ETH_TX_BUFFER_SIZE(size), 0,
171                         rte_eth_dev_socket_id(port_id));
172         if (tx_buffer[port_id] == NULL)
173                 rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
174                                 (unsigned) port_id);
175
176         rte_eth_tx_buffer_init(tx_buffer[port_id], size);
177
178         ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[port_id],
179                         flush_tx_error_callback, (void *)(intptr_t)port_id);
180         if (ret < 0)
181                         rte_exit(EXIT_FAILURE, "Cannot set error callback for "
182                                         "tx buffer on port %u\n", (unsigned) 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                 uint8_t p1 = ports->id[i];
199                 uint8_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 uint8_t in_port = buf->port;
219         const uint8_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                 uint8_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 }