1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
10 #include <sys/queue.h>
15 #include <rte_common.h>
16 #include <rte_malloc.h>
17 #include <rte_memory.h>
18 #include <rte_memzone.h>
20 #include <rte_branch_prediction.h>
22 #include <rte_per_lcore.h>
23 #include <rte_launch.h>
24 #include <rte_lcore.h>
26 #include <rte_debug.h>
27 #include <rte_mempool.h>
29 #include <rte_interrupts.h>
30 #include <rte_ether.h>
31 #include <rte_ethdev.h>
32 #include <rte_string_fns.h>
37 /* Number of packets to attempt to read from queue */
38 #define PKT_READ_SIZE ((uint16_t)32)
41 * Our node id number - tells us which rx queue to read, and NIC TX
44 static uint8_t node_id;
46 #define MBQ_CAPACITY 32
48 /* maps input ports to output ports for packets */
49 static uint16_t output_ports[RTE_MAX_ETHPORTS];
51 /* buffers up a set of packet that are ready to send */
52 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
54 /* shared data from server. We update statistics here */
55 static struct tx_stats *tx_stats;
57 static struct filter_stats *filter_stats;
60 * print a usage message
63 usage(const char *progname)
65 printf("Usage: %s [EAL args] -- -n <node_id>\n\n", progname);
69 * Convert the node id number from a string to an int.
72 parse_node_num(const char *node)
77 if (node == NULL || *node == '\0')
80 temp = strtoul(node, &end, 10);
81 if (end == NULL || *end != '\0')
84 node_id = (uint8_t)temp;
89 * Parse the application arguments to the node app.
92 parse_app_args(int argc, char *argv[])
94 int option_index, opt;
95 char **argvopt = argv;
96 const char *progname = NULL;
97 static struct option lgopts[] = { /* no long options */
102 while ((opt = getopt_long(argc, argvopt, "n:", lgopts,
103 &option_index)) != EOF) {
106 if (parse_node_num(optarg) != 0) {
120 * Tx buffer error callback
123 flush_tx_error_callback(struct rte_mbuf **unsent, uint16_t count,
126 uint16_t port_id = (uintptr_t)userdata;
128 tx_stats->tx_drop[port_id] += count;
130 /* free the mbufs which failed from transmit */
131 for (i = 0; i < count; i++)
132 rte_pktmbuf_free(unsent[i]);
137 configure_tx_buffer(uint16_t port_id, uint16_t size)
141 /* Initialize TX buffers */
142 tx_buffer[port_id] = rte_zmalloc_socket("tx_buffer",
143 RTE_ETH_TX_BUFFER_SIZE(size), 0,
144 rte_eth_dev_socket_id(port_id));
145 if (tx_buffer[port_id] == NULL)
146 rte_exit(EXIT_FAILURE,
147 "Cannot allocate buffer for tx on port %u\n", port_id);
149 rte_eth_tx_buffer_init(tx_buffer[port_id], size);
151 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[port_id],
152 flush_tx_error_callback, (void *)(intptr_t)port_id);
154 rte_exit(EXIT_FAILURE,
155 "Cannot set error callback for tx buffer on port %u\n",
160 * set up output ports so that all traffic on port gets sent out
161 * its paired port. Index using actual port numbers since that is
162 * what comes in the mbuf structure.
165 configure_output_ports(const struct shared_info *info)
169 if (info->num_ports > RTE_MAX_ETHPORTS)
170 rte_exit(EXIT_FAILURE, "Too many ethernet ports. "
171 "RTE_MAX_ETHPORTS = %u\n",
172 (unsigned int)RTE_MAX_ETHPORTS);
173 for (i = 0; i < info->num_ports - 1; i += 2) {
174 uint8_t p1 = info->id[i];
175 uint8_t p2 = info->id[i+1];
177 output_ports[p1] = p2;
178 output_ports[p2] = p1;
180 configure_tx_buffer(p1, MBQ_CAPACITY);
181 configure_tx_buffer(p2, MBQ_CAPACITY);
187 * Create the hash table that will contain the flows that
188 * the node will handle, which will be used to decide if packet
189 * is transmitted or dropped.
192 /* Creation of hash table. 8< */
193 static struct rte_hash *
194 create_hash_table(const struct shared_info *info)
196 uint32_t num_flows_node = info->num_flows / info->num_nodes;
197 char name[RTE_HASH_NAMESIZE];
201 struct rte_hash_parameters hash_params = {
202 .entries = num_flows_node * 2, /* table load = 50% */
203 .key_len = sizeof(uint32_t), /* Store IPv4 dest IP address */
204 .socket_id = rte_socket_id(),
205 .hash_func_init_val = 0,
208 snprintf(name, sizeof(name), "hash_table_%d", node_id);
209 hash_params.name = name;
210 h = rte_hash_create(&hash_params);
213 rte_exit(EXIT_FAILURE,
214 "Problem creating the hash table for node %d\n",
220 populate_hash_table(const struct rte_hash *h, const struct shared_info *info)
225 uint32_t num_flows_node = 0;
226 uint64_t target_node;
228 /* Add flows in table */
229 for (i = 0; i < info->num_flows; i++) {
230 target_node = i % info->num_nodes;
231 if (target_node != node_id)
234 ip_dst = rte_cpu_to_be_32(i);
236 ret = rte_hash_add_key(h, (void *) &ip_dst);
238 rte_exit(EXIT_FAILURE, "Unable to add entry %u "
239 "in hash table\n", i);
245 printf("Hash table: Adding 0x%x keys\n", num_flows_node);
247 /* >8 End of creation of hash table. */
250 * This function performs routing of packets
251 * Just sends each input packet out an output port based solely on the input
252 * port it arrived on.
255 transmit_packet(struct rte_mbuf *buf)
258 const uint16_t in_port = buf->port;
259 const uint16_t out_port = output_ports[in_port];
260 struct rte_eth_dev_tx_buffer *buffer = tx_buffer[out_port];
262 sent = rte_eth_tx_buffer(out_port, node_id, buffer, buf);
264 tx_stats->tx[out_port] += sent;
268 /* Packets dequeued from the shared ring. 8< */
270 handle_packets(struct rte_hash *h, struct rte_mbuf **bufs, uint16_t num_packets)
272 struct rte_ipv4_hdr *ipv4_hdr;
273 uint32_t ipv4_dst_ip[PKT_READ_SIZE];
274 const void *key_ptrs[PKT_READ_SIZE];
276 int32_t positions[PKT_READ_SIZE] = {0};
278 for (i = 0; i < num_packets; i++) {
279 /* Handle IPv4 header.*/
280 ipv4_hdr = rte_pktmbuf_mtod_offset(bufs[i],
281 struct rte_ipv4_hdr *, sizeof(struct rte_ether_hdr));
282 ipv4_dst_ip[i] = ipv4_hdr->dst_addr;
283 key_ptrs[i] = &ipv4_dst_ip[i];
285 /* Check if packets belongs to any flows handled by this node */
286 rte_hash_lookup_bulk(h, key_ptrs, num_packets, positions);
288 for (i = 0; i < num_packets; i++) {
289 if (likely(positions[i] >= 0)) {
290 filter_stats->passed++;
291 transmit_packet(bufs[i]);
293 filter_stats->drop++;
294 /* Drop packet, as flow is not handled by this node */
295 rte_pktmbuf_free(bufs[i]);
299 /* >8 End of packets dequeueing. */
302 * Application main function - loops through
303 * receiving and processing packets. Never returns
306 main(int argc, char *argv[])
308 const struct rte_memzone *mz;
309 struct rte_ring *rx_ring;
311 struct rte_mempool *mp;
312 struct shared_info *info;
313 int need_flush = 0; /* indicates whether we have unsent packets */
315 void *pkts[PKT_READ_SIZE];
318 retval = rte_eal_init(argc, argv);
324 if (parse_app_args(argc, argv) < 0)
325 rte_exit(EXIT_FAILURE, "Invalid command-line arguments\n");
327 if (rte_eth_dev_count_avail() == 0)
328 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
330 /* Attaching to the server process memory. 8< */
331 rx_ring = rte_ring_lookup(get_rx_queue_name(node_id));
333 rte_exit(EXIT_FAILURE, "Cannot get RX ring - "
334 "is server process running?\n");
336 mp = rte_mempool_lookup(PKTMBUF_POOL_NAME);
338 rte_exit(EXIT_FAILURE, "Cannot get mempool for mbufs\n");
340 mz = rte_memzone_lookup(MZ_SHARED_INFO);
342 rte_exit(EXIT_FAILURE, "Cannot get port info structure\n");
344 tx_stats = &(info->tx_stats[node_id]);
345 filter_stats = &(info->filter_stats[node_id]);
346 /* >8 End of attaching to the server process memory. */
348 configure_output_ports(info);
350 h = create_hash_table(info);
352 populate_hash_table(h, info);
354 RTE_LOG(INFO, APP, "Finished Process Init.\n");
356 printf("\nNode process %d handling packets\n", node_id);
357 printf("[Press Ctrl-C to quit ...]\n");
360 uint16_t rx_pkts = PKT_READ_SIZE;
364 * Try dequeuing max possible packets first, if that fails,
365 * get the most we can. Loop body should only execute once,
368 while (rx_pkts > 0 &&
369 unlikely(rte_ring_dequeue_bulk(rx_ring, pkts,
370 rx_pkts, NULL) == 0))
371 rx_pkts = (uint16_t)RTE_MIN(rte_ring_count(rx_ring),
374 if (unlikely(rx_pkts == 0)) {
376 for (port = 0; port < info->num_ports; port++) {
377 sent = rte_eth_tx_buffer_flush(
382 tx_stats->tx[port] += sent;
388 handle_packets(h, (struct rte_mbuf **)pkts, rx_pkts);
393 /* clean up the EAL */