xref: /dpdk/examples/server_node_efd/node/node.c (revision ecaed092)
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 <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_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 #include <rte_ip.h>
67 
68 #include "common.h"
69 
70 /* Number of packets to attempt to read from queue */
71 #define PKT_READ_SIZE  ((uint16_t)32)
72 
73 /*
74  * Our node id number - tells us which rx queue to read, and NIC TX
75  * queue to write to.
76  */
77 static uint8_t node_id;
78 
79 #define MBQ_CAPACITY 32
80 
81 /* maps input ports to output ports for packets */
82 static uint8_t output_ports[RTE_MAX_ETHPORTS];
83 
84 /* buffers up a set of packet that are ready to send */
85 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
86 
87 /* shared data from server. We update statistics here */
88 static struct tx_stats *tx_stats;
89 
90 static struct filter_stats *filter_stats;
91 
92 /*
93  * print a usage message
94  */
95 static void
96 usage(const char *progname)
97 {
98 	printf("Usage: %s [EAL args] -- -n <node_id>\n\n", progname);
99 }
100 
101 /*
102  * Convert the node id number from a string to an int.
103  */
104 static int
105 parse_node_num(const char *node)
106 {
107 	char *end = NULL;
108 	unsigned long temp;
109 
110 	if (node == NULL || *node == '\0')
111 		return -1;
112 
113 	temp = strtoul(node, &end, 10);
114 	if (end == NULL || *end != '\0')
115 		return -1;
116 
117 	node_id = (uint8_t)temp;
118 	return 0;
119 }
120 
121 /*
122  * Parse the application arguments to the node 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_node_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  * Tx buffer error callback
154  */
155 static void
156 flush_tx_error_callback(struct rte_mbuf **unsent, uint16_t count,
157 		void *userdata) {
158 	int i;
159 	uint8_t port_id = (uintptr_t)userdata;
160 
161 	tx_stats->tx_drop[port_id] += count;
162 
163 	/* free the mbufs which failed from transmit */
164 	for (i = 0; i < count; i++)
165 		rte_pktmbuf_free(unsent[i]);
166 
167 }
168 
169 static void
170 configure_tx_buffer(uint8_t port_id, uint16_t size)
171 {
172 	int ret;
173 
174 	/* Initialize TX buffers */
175 	tx_buffer[port_id] = rte_zmalloc_socket("tx_buffer",
176 			RTE_ETH_TX_BUFFER_SIZE(size), 0,
177 			rte_eth_dev_socket_id(port_id));
178 	if (tx_buffer[port_id] == NULL)
179 		rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx "
180 				"on port %u\n", (unsigned int) port_id);
181 
182 	rte_eth_tx_buffer_init(tx_buffer[port_id], size);
183 
184 	ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[port_id],
185 			flush_tx_error_callback, (void *)(intptr_t)port_id);
186 	if (ret < 0)
187 		rte_exit(EXIT_FAILURE, "Cannot set error callback for "
188 			"tx buffer on port %u\n", (unsigned int) port_id);
189 }
190 
191 /*
192  * set up output ports so that all traffic on port gets sent out
193  * its paired port. Index using actual port numbers since that is
194  * what comes in the mbuf structure.
195  */
196 static void
197 configure_output_ports(const struct shared_info *info)
198 {
199 	int i;
200 
201 	if (info->num_ports > RTE_MAX_ETHPORTS)
202 		rte_exit(EXIT_FAILURE, "Too many ethernet ports. "
203 				"RTE_MAX_ETHPORTS = %u\n",
204 				(unsigned int)RTE_MAX_ETHPORTS);
205 	for (i = 0; i < info->num_ports - 1; i += 2) {
206 		uint8_t p1 = info->id[i];
207 		uint8_t p2 = info->id[i+1];
208 
209 		output_ports[p1] = p2;
210 		output_ports[p2] = p1;
211 
212 		configure_tx_buffer(p1, MBQ_CAPACITY);
213 		configure_tx_buffer(p2, MBQ_CAPACITY);
214 
215 	}
216 }
217 
218 /*
219  * Create the hash table that will contain the flows that
220  * the node will handle, which will be used to decide if packet
221  * is transmitted or dropped.
222  */
223 static struct rte_hash *
224 create_hash_table(const struct shared_info *info)
225 {
226 	uint32_t num_flows_node = info->num_flows / info->num_nodes;
227 	char name[RTE_HASH_NAMESIZE];
228 	struct rte_hash *h;
229 
230 	/* create table */
231 	struct rte_hash_parameters hash_params = {
232 		.entries = num_flows_node * 2, /* table load = 50% */
233 		.key_len = sizeof(uint32_t), /* Store IPv4 dest IP address */
234 		.socket_id = rte_socket_id(),
235 		.hash_func_init_val = 0,
236 	};
237 
238 	snprintf(name, sizeof(name), "hash_table_%d", node_id);
239 	hash_params.name = name;
240 	h = rte_hash_create(&hash_params);
241 
242 	if (h == NULL)
243 		rte_exit(EXIT_FAILURE,
244 				"Problem creating the hash table for node %d\n",
245 				node_id);
246 	return h;
247 }
248 
249 static void
250 populate_hash_table(const struct rte_hash *h, const struct shared_info *info)
251 {
252 	unsigned int i;
253 	int32_t ret;
254 	uint32_t ip_dst;
255 	uint32_t num_flows_node = 0;
256 	uint64_t target_node;
257 
258 	/* Add flows in table */
259 	for (i = 0; i < info->num_flows; i++) {
260 		target_node = i % info->num_nodes;
261 		if (target_node != node_id)
262 			continue;
263 
264 		ip_dst = rte_cpu_to_be_32(i);
265 
266 		ret = rte_hash_add_key(h, (void *) &ip_dst);
267 		if (ret < 0)
268 			rte_exit(EXIT_FAILURE, "Unable to add entry %u "
269 					"in hash table\n", i);
270 		else
271 			num_flows_node++;
272 
273 	}
274 
275 	printf("Hash table: Adding 0x%x keys\n", num_flows_node);
276 }
277 
278 /*
279  * This function performs routing of packets
280  * Just sends each input packet out an output port based solely on the input
281  * port it arrived on.
282  */
283 static inline void
284 transmit_packet(struct rte_mbuf *buf)
285 {
286 	int sent;
287 	const uint8_t in_port = buf->port;
288 	const uint8_t out_port = output_ports[in_port];
289 	struct rte_eth_dev_tx_buffer *buffer = tx_buffer[out_port];
290 
291 	sent = rte_eth_tx_buffer(out_port, node_id, buffer, buf);
292 	if (sent)
293 		tx_stats->tx[out_port] += sent;
294 
295 }
296 
297 static inline void
298 handle_packets(struct rte_hash *h, struct rte_mbuf **bufs, uint16_t num_packets)
299 {
300 	struct ipv4_hdr *ipv4_hdr;
301 	uint32_t ipv4_dst_ip[PKT_READ_SIZE];
302 	const void *key_ptrs[PKT_READ_SIZE];
303 	unsigned int i;
304 	int32_t positions[PKT_READ_SIZE] = {0};
305 
306 	for (i = 0; i < num_packets; i++) {
307 		/* Handle IPv4 header.*/
308 		ipv4_hdr = rte_pktmbuf_mtod_offset(bufs[i], struct ipv4_hdr *,
309 				sizeof(struct ether_hdr));
310 		ipv4_dst_ip[i] = ipv4_hdr->dst_addr;
311 		key_ptrs[i] = &ipv4_dst_ip[i];
312 	}
313 	/* Check if packets belongs to any flows handled by this node */
314 	rte_hash_lookup_bulk(h, key_ptrs, num_packets, positions);
315 
316 	for (i = 0; i < num_packets; i++) {
317 		if (likely(positions[i] >= 0)) {
318 			filter_stats->passed++;
319 			transmit_packet(bufs[i]);
320 		} else {
321 			filter_stats->drop++;
322 			/* Drop packet, as flow is not handled by this node */
323 			rte_pktmbuf_free(bufs[i]);
324 		}
325 	}
326 }
327 
328 /*
329  * Application main function - loops through
330  * receiving and processing packets. Never returns
331  */
332 int
333 main(int argc, char *argv[])
334 {
335 	const struct rte_memzone *mz;
336 	struct rte_ring *rx_ring;
337 	struct rte_hash *h;
338 	struct rte_mempool *mp;
339 	struct shared_info *info;
340 	int need_flush = 0; /* indicates whether we have unsent packets */
341 	int retval;
342 	void *pkts[PKT_READ_SIZE];
343 	uint16_t sent;
344 
345 	retval = rte_eal_init(argc, argv);
346 	if (retval  < 0)
347 		return -1;
348 	argc -= retval;
349 	argv += retval;
350 
351 	if (parse_app_args(argc, argv) < 0)
352 		rte_exit(EXIT_FAILURE, "Invalid command-line arguments\n");
353 
354 	if (rte_eth_dev_count() == 0)
355 		rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
356 
357 	rx_ring = rte_ring_lookup(get_rx_queue_name(node_id));
358 	if (rx_ring == NULL)
359 		rte_exit(EXIT_FAILURE, "Cannot get RX ring - "
360 				"is server process running?\n");
361 
362 	mp = rte_mempool_lookup(PKTMBUF_POOL_NAME);
363 	if (mp == NULL)
364 		rte_exit(EXIT_FAILURE, "Cannot get mempool for mbufs\n");
365 
366 	mz = rte_memzone_lookup(MZ_SHARED_INFO);
367 	if (mz == NULL)
368 		rte_exit(EXIT_FAILURE, "Cannot get port info structure\n");
369 	info = mz->addr;
370 	tx_stats = &(info->tx_stats[node_id]);
371 	filter_stats = &(info->filter_stats[node_id]);
372 
373 	configure_output_ports(info);
374 
375 	h = create_hash_table(info);
376 
377 	populate_hash_table(h, info);
378 
379 	RTE_LOG(INFO, APP, "Finished Process Init.\n");
380 
381 	printf("\nNode process %d handling packets\n", node_id);
382 	printf("[Press Ctrl-C to quit ...]\n");
383 
384 	for (;;) {
385 		uint16_t  rx_pkts = PKT_READ_SIZE;
386 		uint8_t port;
387 
388 		/*
389 		 * Try dequeuing max possible packets first, if that fails,
390 		 * get the most we can. Loop body should only execute once,
391 		 * maximum
392 		 */
393 		while (rx_pkts > 0 &&
394 				unlikely(rte_ring_dequeue_bulk(rx_ring, pkts,
395 					rx_pkts, NULL) == 0))
396 			rx_pkts = (uint16_t)RTE_MIN(rte_ring_count(rx_ring),
397 					PKT_READ_SIZE);
398 
399 		if (unlikely(rx_pkts == 0)) {
400 			if (need_flush)
401 				for (port = 0; port < info->num_ports; port++) {
402 					sent = rte_eth_tx_buffer_flush(
403 							info->id[port],
404 							node_id,
405 							tx_buffer[port]);
406 					if (unlikely(sent))
407 						tx_stats->tx[port] += sent;
408 				}
409 			need_flush = 0;
410 			continue;
411 		}
412 
413 		handle_packets(h, (struct rte_mbuf **)pkts, rx_pkts);
414 
415 		need_flush = 1;
416 	}
417 }
418