ethdev: increase port id range
[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 uint16_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         uint16_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(uint16_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                          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,
182                 "Cannot set error callback for tx buffer on port %u\n",
183                          port_id);
184 }
185
186 /*
187  * set up output ports so that all traffic on port gets sent out
188  * its paired port. Index using actual port numbers since that is
189  * what comes in the mbuf structure.
190  */
191 static void
192 configure_output_ports(const struct port_info *ports)
193 {
194         int i;
195         if (ports->num_ports > RTE_MAX_ETHPORTS)
196                 rte_exit(EXIT_FAILURE, "Too many ethernet ports. RTE_MAX_ETHPORTS = %u\n",
197                                 (unsigned)RTE_MAX_ETHPORTS);
198         for (i = 0; i < ports->num_ports - 1; i+=2){
199                 uint16_t p1 = ports->id[i];
200                 uint16_t p2 = ports->id[i+1];
201                 output_ports[p1] = p2;
202                 output_ports[p2] = p1;
203
204                 configure_tx_buffer(p1, MBQ_CAPACITY);
205                 configure_tx_buffer(p2, MBQ_CAPACITY);
206
207         }
208 }
209
210 /*
211  * This function performs routing of packets
212  * Just sends each input packet out an output port based solely on the input
213  * port it arrived on.
214  */
215 static void
216 handle_packet(struct rte_mbuf *buf)
217 {
218         int sent;
219         const uint8_t in_port = buf->port;
220         const uint8_t out_port = output_ports[in_port];
221         struct rte_eth_dev_tx_buffer *buffer = tx_buffer[out_port];
222
223         sent = rte_eth_tx_buffer(out_port, client_id, buffer, buf);
224         if (sent)
225                 tx_stats->tx[out_port] += sent;
226
227 }
228
229 /*
230  * Application main function - loops through
231  * receiving and processing packets. Never returns
232  */
233 int
234 main(int argc, char *argv[])
235 {
236         const struct rte_memzone *mz;
237         struct rte_ring *rx_ring;
238         struct rte_mempool *mp;
239         struct port_info *ports;
240         int need_flush = 0; /* indicates whether we have unsent packets */
241         int retval;
242         void *pkts[PKT_READ_SIZE];
243         uint16_t sent;
244
245         if ((retval = rte_eal_init(argc, argv)) < 0)
246                 return -1;
247         argc -= retval;
248         argv += retval;
249
250         if (parse_app_args(argc, argv) < 0)
251                 rte_exit(EXIT_FAILURE, "Invalid command-line arguments\n");
252
253         if (rte_eth_dev_count() == 0)
254                 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
255
256         rx_ring = rte_ring_lookup(get_rx_queue_name(client_id));
257         if (rx_ring == NULL)
258                 rte_exit(EXIT_FAILURE, "Cannot get RX ring - is server process running?\n");
259
260         mp = rte_mempool_lookup(PKTMBUF_POOL_NAME);
261         if (mp == NULL)
262                 rte_exit(EXIT_FAILURE, "Cannot get mempool for mbufs\n");
263
264         mz = rte_memzone_lookup(MZ_PORT_INFO);
265         if (mz == NULL)
266                 rte_exit(EXIT_FAILURE, "Cannot get port info structure\n");
267         ports = mz->addr;
268         tx_stats = &(ports->tx_stats[client_id]);
269
270         configure_output_ports(ports);
271
272         RTE_LOG(INFO, APP, "Finished Process Init.\n");
273
274         printf("\nClient process %d handling packets\n", client_id);
275         printf("[Press Ctrl-C to quit ...]\n");
276
277         for (;;) {
278                 uint16_t i, rx_pkts;
279                 uint8_t port;
280
281                 rx_pkts = rte_ring_dequeue_burst(rx_ring, pkts,
282                                 PKT_READ_SIZE, NULL);
283
284                 if (unlikely(rx_pkts == 0)){
285                         if (need_flush)
286                                 for (port = 0; port < ports->num_ports; port++) {
287                                         sent = rte_eth_tx_buffer_flush(ports->id[port], client_id,
288                                                         tx_buffer[port]);
289                                         if (unlikely(sent))
290                                                 tx_stats->tx[port] += sent;
291                                 }
292                         need_flush = 0;
293                         continue;
294                 }
295
296                 for (i = 0; i < rx_pkts; i++)
297                         handle_packet(pkts[i]);
298
299                 need_flush = 1;
300         }
301 }