7055b543a7984a3bb22d262fab1370d08d512b8d
[deb_dpdk.git] / examples / multi_process / client_server_mp / mp_server / main.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 <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <stdint.h>
39 #include <stdarg.h>
40 #include <inttypes.h>
41 #include <sys/queue.h>
42 #include <errno.h>
43 #include <netinet/ip.h>
44
45 #include <rte_common.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_eal.h>
49 #include <rte_launch.h>
50 #include <rte_per_lcore.h>
51 #include <rte_lcore.h>
52 #include <rte_branch_prediction.h>
53 #include <rte_atomic.h>
54 #include <rte_ring.h>
55 #include <rte_log.h>
56 #include <rte_debug.h>
57 #include <rte_mempool.h>
58 #include <rte_memcpy.h>
59 #include <rte_mbuf.h>
60 #include <rte_ether.h>
61 #include <rte_interrupts.h>
62 #include <rte_pci.h>
63 #include <rte_ethdev.h>
64 #include <rte_byteorder.h>
65 #include <rte_malloc.h>
66 #include <rte_string_fns.h>
67
68 #include "common.h"
69 #include "args.h"
70 #include "init.h"
71
72 /*
73  * When doing reads from the NIC or the client queues,
74  * use this batch size
75  */
76 #define PACKET_READ_SIZE 32
77
78 /*
79  * Local buffers to put packets in, used to send packets in bursts to the
80  * clients
81  */
82 struct client_rx_buf {
83         struct rte_mbuf *buffer[PACKET_READ_SIZE];
84         uint16_t count;
85 };
86
87 /* One buffer per client rx queue - dynamically allocate array */
88 static struct client_rx_buf *cl_rx_buf;
89
90 static const char *
91 get_printable_mac_addr(uint8_t port)
92 {
93         static const char err_address[] = "00:00:00:00:00:00";
94         static char addresses[RTE_MAX_ETHPORTS][sizeof(err_address)];
95
96         if (unlikely(port >= RTE_MAX_ETHPORTS))
97                 return err_address;
98         if (unlikely(addresses[port][0]=='\0')){
99                 struct ether_addr mac;
100                 rte_eth_macaddr_get(port, &mac);
101                 snprintf(addresses[port], sizeof(addresses[port]),
102                                 "%02x:%02x:%02x:%02x:%02x:%02x\n",
103                                 mac.addr_bytes[0], mac.addr_bytes[1], mac.addr_bytes[2],
104                                 mac.addr_bytes[3], mac.addr_bytes[4], mac.addr_bytes[5]);
105         }
106         return addresses[port];
107 }
108
109 /*
110  * This function displays the recorded statistics for each port
111  * and for each client. It uses ANSI terminal codes to clear
112  * screen when called. It is called from a single non-master
113  * thread in the server process, when the process is run with more
114  * than one lcore enabled.
115  */
116 static void
117 do_stats_display(void)
118 {
119         unsigned i, j;
120         const char clr[] = { 27, '[', '2', 'J', '\0' };
121         const char topLeft[] = { 27, '[', '1', ';', '1', 'H','\0' };
122         uint64_t port_tx[RTE_MAX_ETHPORTS], port_tx_drop[RTE_MAX_ETHPORTS];
123         uint64_t client_tx[MAX_CLIENTS], client_tx_drop[MAX_CLIENTS];
124
125         /* to get TX stats, we need to do some summing calculations */
126         memset(port_tx, 0, sizeof(port_tx));
127         memset(port_tx_drop, 0, sizeof(port_tx_drop));
128         memset(client_tx, 0, sizeof(client_tx));
129         memset(client_tx_drop, 0, sizeof(client_tx_drop));
130
131         for (i = 0; i < num_clients; i++){
132                 const volatile struct tx_stats *tx = &ports->tx_stats[i];
133                 for (j = 0; j < ports->num_ports; j++){
134                         /* assign to local variables here, save re-reading volatile vars */
135                         const uint64_t tx_val = tx->tx[ports->id[j]];
136                         const uint64_t drop_val = tx->tx_drop[ports->id[j]];
137                         port_tx[j] += tx_val;
138                         port_tx_drop[j] += drop_val;
139                         client_tx[i] += tx_val;
140                         client_tx_drop[i] += drop_val;
141                 }
142         }
143
144         /* Clear screen and move to top left */
145         printf("%s%s", clr, topLeft);
146
147         printf("PORTS\n");
148         printf("-----\n");
149         for (i = 0; i < ports->num_ports; i++)
150                 printf("Port %u: '%s'\t", (unsigned)ports->id[i],
151                                 get_printable_mac_addr(ports->id[i]));
152         printf("\n\n");
153         for (i = 0; i < ports->num_ports; i++){
154                 printf("Port %u - rx: %9"PRIu64"\t"
155                                 "tx: %9"PRIu64"\n",
156                                 (unsigned)ports->id[i], ports->rx_stats.rx[i],
157                                 port_tx[i]);
158         }
159
160         printf("\nCLIENTS\n");
161         printf("-------\n");
162         for (i = 0; i < num_clients; i++){
163                 const unsigned long long rx = clients[i].stats.rx;
164                 const unsigned long long rx_drop = clients[i].stats.rx_drop;
165                 printf("Client %2u - rx: %9llu, rx_drop: %9llu\n"
166                                 "            tx: %9"PRIu64", tx_drop: %9"PRIu64"\n",
167                                 i, rx, rx_drop, client_tx[i], client_tx_drop[i]);
168         }
169
170         printf("\n");
171 }
172
173 /*
174  * The function called from each non-master lcore used by the process.
175  * The test_and_set function is used to randomly pick a single lcore on which
176  * the code to display the statistics will run. Otherwise, the code just
177  * repeatedly sleeps.
178  */
179 static int
180 sleep_lcore(__attribute__((unused)) void *dummy)
181 {
182         /* Used to pick a display thread - static, so zero-initialised */
183         static rte_atomic32_t display_stats;
184
185         /* Only one core should display stats */
186         if (rte_atomic32_test_and_set(&display_stats)) {
187                 const unsigned sleeptime = 1;
188                 printf("Core %u displaying statistics\n", rte_lcore_id());
189
190                 /* Longer initial pause so above printf is seen */
191                 sleep(sleeptime * 3);
192
193                 /* Loop forever: sleep always returns 0 or <= param */
194                 while (sleep(sleeptime) <= sleeptime)
195                         do_stats_display();
196         }
197         return 0;
198 }
199
200 /*
201  * Function to set all the client statistic values to zero.
202  * Called at program startup.
203  */
204 static void
205 clear_stats(void)
206 {
207         unsigned i;
208
209         for (i = 0; i < num_clients; i++)
210                 clients[i].stats.rx = clients[i].stats.rx_drop = 0;
211 }
212
213 /*
214  * send a burst of traffic to a client, assuming there are packets
215  * available to be sent to this client
216  */
217 static void
218 flush_rx_queue(uint16_t client)
219 {
220         uint16_t j;
221         struct client *cl;
222
223         if (cl_rx_buf[client].count == 0)
224                 return;
225
226         cl = &clients[client];
227         if (rte_ring_enqueue_bulk(cl->rx_q, (void **)cl_rx_buf[client].buffer,
228                         cl_rx_buf[client].count, NULL) == 0){
229                 for (j = 0; j < cl_rx_buf[client].count; j++)
230                         rte_pktmbuf_free(cl_rx_buf[client].buffer[j]);
231                 cl->stats.rx_drop += cl_rx_buf[client].count;
232         }
233         else
234                 cl->stats.rx += cl_rx_buf[client].count;
235
236         cl_rx_buf[client].count = 0;
237 }
238
239 /*
240  * marks a packet down to be sent to a particular client process
241  */
242 static inline void
243 enqueue_rx_packet(uint8_t client, struct rte_mbuf *buf)
244 {
245         cl_rx_buf[client].buffer[cl_rx_buf[client].count++] = buf;
246 }
247
248 /*
249  * This function takes a group of packets and routes them
250  * individually to the client process. Very simply round-robins the packets
251  * without checking any of the packet contents.
252  */
253 static void
254 process_packets(uint32_t port_num __rte_unused,
255                 struct rte_mbuf *pkts[], uint16_t rx_count)
256 {
257         uint16_t i;
258         uint8_t client = 0;
259
260         for (i = 0; i < rx_count; i++) {
261                 enqueue_rx_packet(client, pkts[i]);
262
263                 if (++client == num_clients)
264                         client = 0;
265         }
266
267         for (i = 0; i < num_clients; i++)
268                 flush_rx_queue(i);
269 }
270
271 /*
272  * Function called by the master lcore of the DPDK process.
273  */
274 static void
275 do_packet_forwarding(void)
276 {
277         unsigned port_num = 0; /* indexes the port[] array */
278
279         for (;;) {
280                 struct rte_mbuf *buf[PACKET_READ_SIZE];
281                 uint16_t rx_count;
282
283                 /* read a port */
284                 rx_count = rte_eth_rx_burst(ports->id[port_num], 0, \
285                                 buf, PACKET_READ_SIZE);
286                 ports->rx_stats.rx[port_num] += rx_count;
287
288                 /* Now process the NIC packets read */
289                 if (likely(rx_count > 0))
290                         process_packets(port_num, buf, rx_count);
291
292                 /* move to next port */
293                 if (++port_num == ports->num_ports)
294                         port_num = 0;
295         }
296 }
297
298 int
299 main(int argc, char *argv[])
300 {
301         /* initialise the system */
302         if (init(argc, argv) < 0 )
303                 return -1;
304         RTE_LOG(INFO, APP, "Finished Process Init.\n");
305
306         cl_rx_buf = calloc(num_clients, sizeof(cl_rx_buf[0]));
307
308         /* clear statistics */
309         clear_stats();
310
311         /* put all other cores to sleep bar master */
312         rte_eal_mp_remote_launch(sleep_lcore, NULL, SKIP_MASTER);
313
314         do_packet_forwarding();
315         return 0;
316 }