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