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 uint16_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 uint16_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(uint16_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,
178 "Cannot allocate buffer for tx on port %u\n", 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,
186 "Cannot set error callback for tx buffer on port %u\n",
191 * set up output ports so that all traffic on port gets sent out
192 * its paired port. Index using actual port numbers since that is
193 * what comes in the mbuf structure.
196 configure_output_ports(const struct shared_info *info)
200 if (info->num_ports > RTE_MAX_ETHPORTS)
201 rte_exit(EXIT_FAILURE, "Too many ethernet ports. "
202 "RTE_MAX_ETHPORTS = %u\n",
203 (unsigned int)RTE_MAX_ETHPORTS);
204 for (i = 0; i < info->num_ports - 1; i += 2) {
205 uint8_t p1 = info->id[i];
206 uint8_t p2 = info->id[i+1];
208 output_ports[p1] = p2;
209 output_ports[p2] = p1;
211 configure_tx_buffer(p1, MBQ_CAPACITY);
212 configure_tx_buffer(p2, MBQ_CAPACITY);
218 * Create the hash table that will contain the flows that
219 * the node will handle, which will be used to decide if packet
220 * is transmitted or dropped.
222 static struct rte_hash *
223 create_hash_table(const struct shared_info *info)
225 uint32_t num_flows_node = info->num_flows / info->num_nodes;
226 char name[RTE_HASH_NAMESIZE];
230 struct rte_hash_parameters hash_params = {
231 .entries = num_flows_node * 2, /* table load = 50% */
232 .key_len = sizeof(uint32_t), /* Store IPv4 dest IP address */
233 .socket_id = rte_socket_id(),
234 .hash_func_init_val = 0,
237 snprintf(name, sizeof(name), "hash_table_%d", node_id);
238 hash_params.name = name;
239 h = rte_hash_create(&hash_params);
242 rte_exit(EXIT_FAILURE,
243 "Problem creating the hash table for node %d\n",
249 populate_hash_table(const struct rte_hash *h, const struct shared_info *info)
254 uint32_t num_flows_node = 0;
255 uint64_t target_node;
257 /* Add flows in table */
258 for (i = 0; i < info->num_flows; i++) {
259 target_node = i % info->num_nodes;
260 if (target_node != node_id)
263 ip_dst = rte_cpu_to_be_32(i);
265 ret = rte_hash_add_key(h, (void *) &ip_dst);
267 rte_exit(EXIT_FAILURE, "Unable to add entry %u "
268 "in hash table\n", i);
274 printf("Hash table: Adding 0x%x keys\n", num_flows_node);
278 * This function performs routing of packets
279 * Just sends each input packet out an output port based solely on the input
280 * port it arrived on.
283 transmit_packet(struct rte_mbuf *buf)
286 const uint16_t in_port = buf->port;
287 const uint16_t out_port = output_ports[in_port];
288 struct rte_eth_dev_tx_buffer *buffer = tx_buffer[out_port];
290 sent = rte_eth_tx_buffer(out_port, node_id, buffer, buf);
292 tx_stats->tx[out_port] += sent;
297 handle_packets(struct rte_hash *h, struct rte_mbuf **bufs, uint16_t num_packets)
299 struct ipv4_hdr *ipv4_hdr;
300 uint32_t ipv4_dst_ip[PKT_READ_SIZE];
301 const void *key_ptrs[PKT_READ_SIZE];
303 int32_t positions[PKT_READ_SIZE] = {0};
305 for (i = 0; i < num_packets; i++) {
306 /* Handle IPv4 header.*/
307 ipv4_hdr = rte_pktmbuf_mtod_offset(bufs[i], struct ipv4_hdr *,
308 sizeof(struct ether_hdr));
309 ipv4_dst_ip[i] = ipv4_hdr->dst_addr;
310 key_ptrs[i] = &ipv4_dst_ip[i];
312 /* Check if packets belongs to any flows handled by this node */
313 rte_hash_lookup_bulk(h, key_ptrs, num_packets, positions);
315 for (i = 0; i < num_packets; i++) {
316 if (likely(positions[i] >= 0)) {
317 filter_stats->passed++;
318 transmit_packet(bufs[i]);
320 filter_stats->drop++;
321 /* Drop packet, as flow is not handled by this node */
322 rte_pktmbuf_free(bufs[i]);
328 * Application main function - loops through
329 * receiving and processing packets. Never returns
332 main(int argc, char *argv[])
334 const struct rte_memzone *mz;
335 struct rte_ring *rx_ring;
337 struct rte_mempool *mp;
338 struct shared_info *info;
339 int need_flush = 0; /* indicates whether we have unsent packets */
341 void *pkts[PKT_READ_SIZE];
344 retval = rte_eal_init(argc, argv);
350 if (parse_app_args(argc, argv) < 0)
351 rte_exit(EXIT_FAILURE, "Invalid command-line arguments\n");
353 if (rte_eth_dev_count() == 0)
354 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
356 rx_ring = rte_ring_lookup(get_rx_queue_name(node_id));
358 rte_exit(EXIT_FAILURE, "Cannot get RX ring - "
359 "is server process running?\n");
361 mp = rte_mempool_lookup(PKTMBUF_POOL_NAME);
363 rte_exit(EXIT_FAILURE, "Cannot get mempool for mbufs\n");
365 mz = rte_memzone_lookup(MZ_SHARED_INFO);
367 rte_exit(EXIT_FAILURE, "Cannot get port info structure\n");
369 tx_stats = &(info->tx_stats[node_id]);
370 filter_stats = &(info->filter_stats[node_id]);
372 configure_output_ports(info);
374 h = create_hash_table(info);
376 populate_hash_table(h, info);
378 RTE_LOG(INFO, APP, "Finished Process Init.\n");
380 printf("\nNode process %d handling packets\n", node_id);
381 printf("[Press Ctrl-C to quit ...]\n");
384 uint16_t rx_pkts = PKT_READ_SIZE;
388 * Try dequeuing max possible packets first, if that fails,
389 * get the most we can. Loop body should only execute once,
392 while (rx_pkts > 0 &&
393 unlikely(rte_ring_dequeue_bulk(rx_ring, pkts,
394 rx_pkts, NULL) == 0))
395 rx_pkts = (uint16_t)RTE_MIN(rte_ring_count(rx_ring),
398 if (unlikely(rx_pkts == 0)) {
400 for (port = 0; port < info->num_ports; port++) {
401 sent = rte_eth_tx_buffer_flush(
406 tx_stats->tx[port] += sent;
412 handle_packets(h, (struct rte_mbuf **)pkts, rx_pkts);