4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
39 #include <sys/queue.h>
44 #include <rte_common.h>
45 #include <rte_malloc.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
49 #include <rte_atomic.h>
50 #include <rte_branch_prediction.h>
52 #include <rte_per_lcore.h>
53 #include <rte_launch.h>
54 #include <rte_lcore.h>
56 #include <rte_debug.h>
57 #include <rte_mempool.h>
59 #include <rte_interrupts.h>
61 #include <rte_ether.h>
62 #include <rte_ethdev.h>
63 #include <rte_string_fns.h>
68 /* Number of packets to attempt to read from queue */
69 #define PKT_READ_SIZE ((uint16_t)32)
72 * Our node id number - tells us which rx queue to read, and NIC TX
75 static uint8_t node_id;
77 #define MBQ_CAPACITY 32
79 /* maps input ports to output ports for packets */
80 static uint8_t output_ports[RTE_MAX_ETHPORTS];
82 /* buffers up a set of packet that are ready to send */
83 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
85 /* shared data from server. We update statistics here */
86 static struct tx_stats *tx_stats;
88 static struct filter_stats *filter_stats;
91 * print a usage message
94 usage(const char *progname)
96 printf("Usage: %s [EAL args] -- -n <node_id>\n\n", progname);
100 * Convert the node id number from a string to an int.
103 parse_node_num(const char *node)
108 if (node == NULL || *node == '\0')
111 temp = strtoul(node, &end, 10);
112 if (end == NULL || *end != '\0')
115 node_id = (uint8_t)temp;
120 * Parse the application arguments to the node app.
123 parse_app_args(int argc, char *argv[])
125 int option_index, opt;
126 char **argvopt = argv;
127 const char *progname = NULL;
128 static struct option lgopts[] = { /* no long options */
133 while ((opt = getopt_long(argc, argvopt, "n:", lgopts,
134 &option_index)) != EOF) {
137 if (parse_node_num(optarg) != 0) {
151 * Tx buffer error callback
154 flush_tx_error_callback(struct rte_mbuf **unsent, uint16_t count,
157 uint8_t port_id = (uintptr_t)userdata;
159 tx_stats->tx_drop[port_id] += count;
161 /* free the mbufs which failed from transmit */
162 for (i = 0; i < count; i++)
163 rte_pktmbuf_free(unsent[i]);
168 configure_tx_buffer(uint8_t port_id, uint16_t size)
172 /* Initialize TX buffers */
173 tx_buffer[port_id] = rte_zmalloc_socket("tx_buffer",
174 RTE_ETH_TX_BUFFER_SIZE(size), 0,
175 rte_eth_dev_socket_id(port_id));
176 if (tx_buffer[port_id] == NULL)
177 rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx "
178 "on port %u\n", (unsigned int) port_id);
180 rte_eth_tx_buffer_init(tx_buffer[port_id], size);
182 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[port_id],
183 flush_tx_error_callback, (void *)(intptr_t)port_id);
185 rte_exit(EXIT_FAILURE, "Cannot set error callback for "
186 "tx buffer on port %u\n", (unsigned int) port_id);
190 * set up output ports so that all traffic on port gets sent out
191 * its paired port. Index using actual port numbers since that is
192 * what comes in the mbuf structure.
195 configure_output_ports(const struct shared_info *info)
199 if (info->num_ports > RTE_MAX_ETHPORTS)
200 rte_exit(EXIT_FAILURE, "Too many ethernet ports. "
201 "RTE_MAX_ETHPORTS = %u\n",
202 (unsigned int)RTE_MAX_ETHPORTS);
203 for (i = 0; i < info->num_ports - 1; i += 2) {
204 uint8_t p1 = info->id[i];
205 uint8_t p2 = info->id[i+1];
207 output_ports[p1] = p2;
208 output_ports[p2] = p1;
210 configure_tx_buffer(p1, MBQ_CAPACITY);
211 configure_tx_buffer(p2, MBQ_CAPACITY);
217 * Create the hash table that will contain the flows that
218 * the node will handle, which will be used to decide if packet
219 * is transmitted or dropped.
221 static struct rte_hash *
222 create_hash_table(const struct shared_info *info)
224 uint32_t num_flows_node = info->num_flows / info->num_nodes;
225 char name[RTE_HASH_NAMESIZE];
229 struct rte_hash_parameters hash_params = {
230 .entries = num_flows_node * 2, /* table load = 50% */
231 .key_len = sizeof(uint32_t), /* Store IPv4 dest IP address */
232 .socket_id = rte_socket_id(),
233 .hash_func_init_val = 0,
236 snprintf(name, sizeof(name), "hash_table_%d", node_id);
237 hash_params.name = name;
238 h = rte_hash_create(&hash_params);
241 rte_exit(EXIT_FAILURE,
242 "Problem creating the hash table for node %d\n",
248 populate_hash_table(const struct rte_hash *h, const struct shared_info *info)
253 uint32_t num_flows_node = 0;
254 uint64_t target_node;
256 /* Add flows in table */
257 for (i = 0; i < info->num_flows; i++) {
258 target_node = i % info->num_nodes;
259 if (target_node != node_id)
262 ip_dst = rte_cpu_to_be_32(i);
264 ret = rte_hash_add_key(h, (void *) &ip_dst);
266 rte_exit(EXIT_FAILURE, "Unable to add entry %u "
267 "in hash table\n", i);
273 printf("Hash table: Adding 0x%x keys\n", num_flows_node);
277 * This function performs routing of packets
278 * Just sends each input packet out an output port based solely on the input
279 * port it arrived on.
282 transmit_packet(struct rte_mbuf *buf)
285 const uint8_t in_port = buf->port;
286 const uint8_t out_port = output_ports[in_port];
287 struct rte_eth_dev_tx_buffer *buffer = tx_buffer[out_port];
289 sent = rte_eth_tx_buffer(out_port, node_id, buffer, buf);
291 tx_stats->tx[out_port] += sent;
296 handle_packets(struct rte_hash *h, struct rte_mbuf **bufs, uint16_t num_packets)
298 struct ipv4_hdr *ipv4_hdr;
299 uint32_t ipv4_dst_ip[PKT_READ_SIZE];
300 const void *key_ptrs[PKT_READ_SIZE];
302 int32_t positions[PKT_READ_SIZE] = {0};
304 for (i = 0; i < num_packets; i++) {
305 /* Handle IPv4 header.*/
306 ipv4_hdr = rte_pktmbuf_mtod_offset(bufs[i], struct ipv4_hdr *,
307 sizeof(struct ether_hdr));
308 ipv4_dst_ip[i] = ipv4_hdr->dst_addr;
309 key_ptrs[i] = &ipv4_dst_ip[i];
311 /* Check if packets belongs to any flows handled by this node */
312 rte_hash_lookup_bulk(h, key_ptrs, num_packets, positions);
314 for (i = 0; i < num_packets; i++) {
315 if (likely(positions[i] >= 0)) {
316 filter_stats->passed++;
317 transmit_packet(bufs[i]);
319 filter_stats->drop++;
320 /* Drop packet, as flow is not handled by this node */
321 rte_pktmbuf_free(bufs[i]);
327 * Application main function - loops through
328 * receiving and processing packets. Never returns
331 main(int argc, char *argv[])
333 const struct rte_memzone *mz;
334 struct rte_ring *rx_ring;
336 struct rte_mempool *mp;
337 struct shared_info *info;
338 int need_flush = 0; /* indicates whether we have unsent packets */
340 void *pkts[PKT_READ_SIZE];
343 retval = rte_eal_init(argc, argv);
349 if (parse_app_args(argc, argv) < 0)
350 rte_exit(EXIT_FAILURE, "Invalid command-line arguments\n");
352 if (rte_eth_dev_count() == 0)
353 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
355 rx_ring = rte_ring_lookup(get_rx_queue_name(node_id));
357 rte_exit(EXIT_FAILURE, "Cannot get RX ring - "
358 "is server process running?\n");
360 mp = rte_mempool_lookup(PKTMBUF_POOL_NAME);
362 rte_exit(EXIT_FAILURE, "Cannot get mempool for mbufs\n");
364 mz = rte_memzone_lookup(MZ_SHARED_INFO);
366 rte_exit(EXIT_FAILURE, "Cannot get port info structure\n");
368 tx_stats = &(info->tx_stats[node_id]);
369 filter_stats = &(info->filter_stats[node_id]);
371 configure_output_ports(info);
373 h = create_hash_table(info);
375 populate_hash_table(h, info);
377 RTE_LOG(INFO, APP, "Finished Process Init.\n");
379 printf("\nNode process %d handling packets\n", node_id);
380 printf("[Press Ctrl-C to quit ...]\n");
383 uint16_t rx_pkts = PKT_READ_SIZE;
387 * Try dequeuing max possible packets first, if that fails,
388 * get the most we can. Loop body should only execute once,
391 while (rx_pkts > 0 &&
392 unlikely(rte_ring_dequeue_bulk(rx_ring, pkts,
393 rx_pkts, NULL) == 0))
394 rx_pkts = (uint16_t)RTE_MIN(rte_ring_count(rx_ring),
397 if (unlikely(rx_pkts == 0)) {
399 for (port = 0; port < info->num_ports; port++) {
400 sent = rte_eth_tx_buffer_flush(
405 tx_stats->tx[port] += sent;
411 handle_packets(h, (struct rte_mbuf **)pkts, rx_pkts);