New upstream version 17.11-rc3
[deb_dpdk.git] / examples / server_node_efd / server / main.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 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_eal.h>
48 #include <rte_launch.h>
49 #include <rte_per_lcore.h>
50 #include <rte_lcore.h>
51 #include <rte_branch_prediction.h>
52 #include <rte_atomic.h>
53 #include <rte_ring.h>
54 #include <rte_log.h>
55 #include <rte_debug.h>
56 #include <rte_mempool.h>
57 #include <rte_memcpy.h>
58 #include <rte_mbuf.h>
59 #include <rte_ether.h>
60 #include <rte_interrupts.h>
61 #include <rte_ethdev.h>
62 #include <rte_byteorder.h>
63 #include <rte_malloc.h>
64 #include <rte_string_fns.h>
65 #include <rte_efd.h>
66 #include <rte_ip.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 node 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  * nodes
81  */
82 struct node_rx_buf {
83         struct rte_mbuf *buffer[PACKET_READ_SIZE];
84         uint16_t count;
85 };
86
87 struct efd_stats {
88         uint64_t distributed;
89         uint64_t drop;
90 } flow_dist_stats;
91
92 /* One buffer per node rx queue - dynamically allocate array */
93 static struct node_rx_buf *cl_rx_buf;
94
95 static const char *
96 get_printable_mac_addr(uint16_t port)
97 {
98         static const char err_address[] = "00:00:00:00:00:00";
99         static char addresses[RTE_MAX_ETHPORTS][sizeof(err_address)];
100         struct ether_addr mac;
101
102         if (unlikely(port >= RTE_MAX_ETHPORTS))
103                 return err_address;
104         if (unlikely(addresses[port][0] == '\0')) {
105                 rte_eth_macaddr_get(port, &mac);
106                 snprintf(addresses[port], sizeof(addresses[port]),
107                                 "%02x:%02x:%02x:%02x:%02x:%02x\n",
108                                 mac.addr_bytes[0], mac.addr_bytes[1],
109                                 mac.addr_bytes[2], mac.addr_bytes[3],
110                                 mac.addr_bytes[4], mac.addr_bytes[5]);
111         }
112         return addresses[port];
113 }
114
115 /*
116  * This function displays the recorded statistics for each port
117  * and for each node. It uses ANSI terminal codes to clear
118  * screen when called. It is called from a single non-master
119  * thread in the server process, when the process is run with more
120  * than one lcore enabled.
121  */
122 static void
123 do_stats_display(void)
124 {
125         unsigned int i, j;
126         const char clr[] = {27, '[', '2', 'J', '\0'};
127         const char topLeft[] = {27, '[', '1', ';', '1', 'H', '\0'};
128         uint64_t port_tx[RTE_MAX_ETHPORTS], port_tx_drop[RTE_MAX_ETHPORTS];
129         uint64_t node_tx[MAX_NODES], node_tx_drop[MAX_NODES];
130
131         /* to get TX stats, we need to do some summing calculations */
132         memset(port_tx, 0, sizeof(port_tx));
133         memset(port_tx_drop, 0, sizeof(port_tx_drop));
134         memset(node_tx, 0, sizeof(node_tx));
135         memset(node_tx_drop, 0, sizeof(node_tx_drop));
136
137         for (i = 0; i < num_nodes; i++) {
138                 const struct tx_stats *tx = &info->tx_stats[i];
139
140                 for (j = 0; j < info->num_ports; j++) {
141                         const uint64_t tx_val = tx->tx[info->id[j]];
142                         const uint64_t drop_val = tx->tx_drop[info->id[j]];
143
144                         port_tx[j] += tx_val;
145                         port_tx_drop[j] += drop_val;
146                         node_tx[i] += tx_val;
147                         node_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 < info->num_ports; i++)
157                 printf("Port %u: '%s'\t", (unsigned int)info->id[i],
158                                 get_printable_mac_addr(info->id[i]));
159         printf("\n\n");
160         for (i = 0; i < info->num_ports; i++) {
161                 printf("Port %u - rx: %9"PRIu64"\t"
162                                 "tx: %9"PRIu64"\n",
163                                 (unsigned int)info->id[i], info->rx_stats.rx[i],
164                                 port_tx[i]);
165         }
166
167         printf("\nSERVER\n");
168         printf("-----\n");
169         printf("distributed: %9"PRIu64", drop: %9"PRIu64"\n",
170                         flow_dist_stats.distributed, flow_dist_stats.drop);
171
172         printf("\nNODES\n");
173         printf("-------\n");
174         for (i = 0; i < num_nodes; i++) {
175                 const unsigned long long rx = nodes[i].stats.rx;
176                 const unsigned long long rx_drop = nodes[i].stats.rx_drop;
177                 const struct filter_stats *filter = &info->filter_stats[i];
178
179                 printf("Node %2u - rx: %9llu, rx_drop: %9llu\n"
180                                 "            tx: %9"PRIu64", tx_drop: %9"PRIu64"\n"
181                                 "            filter_passed: %9"PRIu64", "
182                                 "filter_drop: %9"PRIu64"\n",
183                                 i, rx, rx_drop, node_tx[i], node_tx_drop[i],
184                                 filter->passed, filter->drop);
185         }
186
187         printf("\n");
188 }
189
190 /*
191  * The function called from each non-master lcore used by the process.
192  * The test_and_set function is used to randomly pick a single lcore on which
193  * the code to display the statistics will run. Otherwise, the code just
194  * repeatedly sleeps.
195  */
196 static int
197 sleep_lcore(__attribute__((unused)) void *dummy)
198 {
199         /* Used to pick a display thread - static, so zero-initialised */
200         static rte_atomic32_t display_stats;
201
202         /* Only one core should display stats */
203         if (rte_atomic32_test_and_set(&display_stats)) {
204                 const unsigned int sleeptime = 1;
205
206                 printf("Core %u displaying statistics\n", rte_lcore_id());
207
208                 /* Longer initial pause so above printf is seen */
209                 sleep(sleeptime * 3);
210
211                 /* Loop forever: sleep always returns 0 or <= param */
212                 while (sleep(sleeptime) <= sleeptime)
213                         do_stats_display();
214         }
215         return 0;
216 }
217
218 /*
219  * Function to set all the node statistic values to zero.
220  * Called at program startup.
221  */
222 static void
223 clear_stats(void)
224 {
225         unsigned int i;
226
227         for (i = 0; i < num_nodes; i++)
228                 nodes[i].stats.rx = nodes[i].stats.rx_drop = 0;
229 }
230
231 /*
232  * send a burst of traffic to a node, assuming there are packets
233  * available to be sent to this node
234  */
235 static void
236 flush_rx_queue(uint16_t node)
237 {
238         uint16_t j;
239         struct node *cl;
240
241         if (cl_rx_buf[node].count == 0)
242                 return;
243
244         cl = &nodes[node];
245         if (rte_ring_enqueue_bulk(cl->rx_q, (void **)cl_rx_buf[node].buffer,
246                         cl_rx_buf[node].count, NULL) != cl_rx_buf[node].count){
247                 for (j = 0; j < cl_rx_buf[node].count; j++)
248                         rte_pktmbuf_free(cl_rx_buf[node].buffer[j]);
249                 cl->stats.rx_drop += cl_rx_buf[node].count;
250         } else
251                 cl->stats.rx += cl_rx_buf[node].count;
252
253         cl_rx_buf[node].count = 0;
254 }
255
256 /*
257  * marks a packet down to be sent to a particular node process
258  */
259 static inline void
260 enqueue_rx_packet(uint8_t node, struct rte_mbuf *buf)
261 {
262         cl_rx_buf[node].buffer[cl_rx_buf[node].count++] = buf;
263 }
264
265 /*
266  * This function takes a group of packets and routes them
267  * individually to the node process. Very simply round-robins the packets
268  * without checking any of the packet contents.
269  */
270 static void
271 process_packets(uint32_t port_num __rte_unused, struct rte_mbuf *pkts[],
272                 uint16_t rx_count, unsigned int socket_id)
273 {
274         uint16_t i;
275         uint8_t node;
276         efd_value_t data[RTE_EFD_BURST_MAX];
277         const void *key_ptrs[RTE_EFD_BURST_MAX];
278
279         struct ipv4_hdr *ipv4_hdr;
280         uint32_t ipv4_dst_ip[RTE_EFD_BURST_MAX];
281
282         for (i = 0; i < rx_count; i++) {
283                 /* Handle IPv4 header.*/
284                 ipv4_hdr = rte_pktmbuf_mtod_offset(pkts[i], struct ipv4_hdr *,
285                                 sizeof(struct ether_hdr));
286                 ipv4_dst_ip[i] = ipv4_hdr->dst_addr;
287                 key_ptrs[i] = (void *)&ipv4_dst_ip[i];
288         }
289
290         rte_efd_lookup_bulk(efd_table, socket_id, rx_count,
291                                 (const void **) key_ptrs, data);
292         for (i = 0; i < rx_count; i++) {
293                 node = (uint8_t) ((uintptr_t)data[i]);
294
295                 if (node >= num_nodes) {
296                         /*
297                          * Node is out of range, which means that
298                          * flow has not been inserted
299                          */
300                         flow_dist_stats.drop++;
301                         rte_pktmbuf_free(pkts[i]);
302                 } else {
303                         flow_dist_stats.distributed++;
304                         enqueue_rx_packet(node, pkts[i]);
305                 }
306         }
307
308         for (i = 0; i < num_nodes; i++)
309                 flush_rx_queue(i);
310 }
311
312 /*
313  * Function called by the master lcore of the DPDK process.
314  */
315 static void
316 do_packet_forwarding(void)
317 {
318         unsigned int port_num = 0; /* indexes the port[] array */
319         unsigned int socket_id = rte_socket_id();
320
321         for (;;) {
322                 struct rte_mbuf *buf[PACKET_READ_SIZE];
323                 uint16_t rx_count;
324
325                 /* read a port */
326                 rx_count = rte_eth_rx_burst(info->id[port_num], 0,
327                                 buf, PACKET_READ_SIZE);
328                 info->rx_stats.rx[port_num] += rx_count;
329
330                 /* Now process the NIC packets read */
331                 if (likely(rx_count > 0))
332                         process_packets(port_num, buf, rx_count, socket_id);
333
334                 /* move to next port */
335                 if (++port_num == info->num_ports)
336                         port_num = 0;
337         }
338 }
339
340 int
341 main(int argc, char *argv[])
342 {
343         /* initialise the system */
344         if (init(argc, argv) < 0)
345                 return -1;
346         RTE_LOG(INFO, APP, "Finished Process Init.\n");
347
348         cl_rx_buf = calloc(num_nodes, sizeof(cl_rx_buf[0]));
349
350         /* clear statistics */
351         clear_stats();
352
353         /* put all other cores to sleep bar master */
354         rte_eal_mp_remote_launch(sleep_lcore, NULL, SKIP_MASTER);
355
356         do_packet_forwarding();
357         return 0;
358 }