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