1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
12 #include <sys/queue.h>
14 #include <sys/types.h>
15 #include <netinet/in.h>
16 #include <netinet/ip.h>
18 #include <rte_common.h>
19 #include <rte_memory.h>
21 #include <rte_launch.h>
22 #include <rte_per_lcore.h>
23 #include <rte_lcore.h>
24 #include <rte_branch_prediction.h>
25 #include <rte_atomic.h>
28 #include <rte_debug.h>
29 #include <rte_mempool.h>
30 #include <rte_memcpy.h>
32 #include <rte_ether.h>
33 #include <rte_interrupts.h>
34 #include <rte_ethdev.h>
35 #include <rte_byteorder.h>
36 #include <rte_malloc.h>
37 #include <rte_string_fns.h>
46 * When doing reads from the NIC or the node queues,
49 #define PACKET_READ_SIZE 32
52 * Local buffers to put packets in, used to send packets in bursts to the
56 struct rte_mbuf *buffer[PACKET_READ_SIZE];
65 /* One buffer per node rx queue - dynamically allocate array */
66 static struct node_rx_buf *cl_rx_buf;
69 get_printable_mac_addr(uint16_t port)
71 static const char err_address[] = "00:00:00:00:00:00";
72 static char addresses[RTE_MAX_ETHPORTS][sizeof(err_address)];
73 struct rte_ether_addr mac;
76 if (unlikely(port >= RTE_MAX_ETHPORTS))
78 if (unlikely(addresses[port][0] == '\0')) {
79 ret = rte_eth_macaddr_get(port, &mac);
81 printf("Failed to get MAC address (port %u): %s\n",
82 port, rte_strerror(-ret));
86 snprintf(addresses[port], sizeof(addresses[port]),
87 "%02x:%02x:%02x:%02x:%02x:%02x\n",
88 mac.addr_bytes[0], mac.addr_bytes[1],
89 mac.addr_bytes[2], mac.addr_bytes[3],
90 mac.addr_bytes[4], mac.addr_bytes[5]);
92 return addresses[port];
96 * This function displays the recorded statistics for each port
97 * and for each node. It uses ANSI terminal codes to clear
98 * screen when called. It is called from a single non-master
99 * thread in the server process, when the process is run with more
100 * than one lcore enabled.
103 do_stats_display(void)
106 const char clr[] = {27, '[', '2', 'J', '\0'};
107 const char topLeft[] = {27, '[', '1', ';', '1', 'H', '\0'};
108 uint64_t port_tx[RTE_MAX_ETHPORTS], port_tx_drop[RTE_MAX_ETHPORTS];
109 uint64_t node_tx[MAX_NODES], node_tx_drop[MAX_NODES];
111 /* to get TX stats, we need to do some summing calculations */
112 memset(port_tx, 0, sizeof(port_tx));
113 memset(port_tx_drop, 0, sizeof(port_tx_drop));
114 memset(node_tx, 0, sizeof(node_tx));
115 memset(node_tx_drop, 0, sizeof(node_tx_drop));
117 for (i = 0; i < num_nodes; i++) {
118 const struct tx_stats *tx = &info->tx_stats[i];
120 for (j = 0; j < info->num_ports; j++) {
121 const uint64_t tx_val = tx->tx[info->id[j]];
122 const uint64_t drop_val = tx->tx_drop[info->id[j]];
124 port_tx[j] += tx_val;
125 port_tx_drop[j] += drop_val;
126 node_tx[i] += tx_val;
127 node_tx_drop[i] += drop_val;
131 /* Clear screen and move to top left */
132 printf("%s%s", clr, topLeft);
136 for (i = 0; i < info->num_ports; i++)
137 printf("Port %u: '%s'\t", (unsigned int)info->id[i],
138 get_printable_mac_addr(info->id[i]));
140 for (i = 0; i < info->num_ports; i++) {
141 printf("Port %u - rx: %9"PRIu64"\t"
143 (unsigned int)info->id[i], info->rx_stats.rx[i],
147 printf("\nSERVER\n");
149 printf("distributed: %9"PRIu64", drop: %9"PRIu64"\n",
150 flow_dist_stats.distributed, flow_dist_stats.drop);
154 for (i = 0; i < num_nodes; i++) {
155 const unsigned long long rx = nodes[i].stats.rx;
156 const unsigned long long rx_drop = nodes[i].stats.rx_drop;
157 const struct filter_stats *filter = &info->filter_stats[i];
159 printf("Node %2u - rx: %9llu, rx_drop: %9llu\n"
160 " tx: %9"PRIu64", tx_drop: %9"PRIu64"\n"
161 " filter_passed: %9"PRIu64", "
162 "filter_drop: %9"PRIu64"\n",
163 i, rx, rx_drop, node_tx[i], node_tx_drop[i],
164 filter->passed, filter->drop);
171 * The function called from each non-master lcore used by the process.
172 * The test_and_set function is used to randomly pick a single lcore on which
173 * the code to display the statistics will run. Otherwise, the code just
177 sleep_lcore(__attribute__((unused)) void *dummy)
179 /* Used to pick a display thread - static, so zero-initialised */
180 static rte_atomic32_t display_stats;
182 /* Only one core should display stats */
183 if (rte_atomic32_test_and_set(&display_stats)) {
184 const unsigned int sleeptime = 1;
186 printf("Core %u displaying statistics\n", rte_lcore_id());
188 /* Longer initial pause so above printf is seen */
189 sleep(sleeptime * 3);
191 /* Loop forever: sleep always returns 0 or <= param */
192 while (sleep(sleeptime) <= sleeptime)
199 * Function to set all the node statistic values to zero.
200 * Called at program startup.
207 for (i = 0; i < num_nodes; i++)
208 nodes[i].stats.rx = nodes[i].stats.rx_drop = 0;
212 * send a burst of traffic to a node, assuming there are packets
213 * available to be sent to this node
216 flush_rx_queue(uint16_t node)
221 if (cl_rx_buf[node].count == 0)
225 if (rte_ring_enqueue_bulk(cl->rx_q, (void **)cl_rx_buf[node].buffer,
226 cl_rx_buf[node].count, NULL) != cl_rx_buf[node].count){
227 for (j = 0; j < cl_rx_buf[node].count; j++)
228 rte_pktmbuf_free(cl_rx_buf[node].buffer[j]);
229 cl->stats.rx_drop += cl_rx_buf[node].count;
231 cl->stats.rx += cl_rx_buf[node].count;
233 cl_rx_buf[node].count = 0;
237 * marks a packet down to be sent to a particular node process
240 enqueue_rx_packet(uint8_t node, struct rte_mbuf *buf)
242 cl_rx_buf[node].buffer[cl_rx_buf[node].count++] = buf;
246 * This function takes a group of packets and routes them
247 * individually to the node process. Very simply round-robins the packets
248 * without checking any of the packet contents.
251 process_packets(uint32_t port_num __rte_unused, struct rte_mbuf *pkts[],
252 uint16_t rx_count, unsigned int socket_id)
256 efd_value_t data[RTE_EFD_BURST_MAX];
257 const void *key_ptrs[RTE_EFD_BURST_MAX];
259 struct rte_ipv4_hdr *ipv4_hdr;
260 uint32_t ipv4_dst_ip[RTE_EFD_BURST_MAX];
262 for (i = 0; i < rx_count; i++) {
263 /* Handle IPv4 header.*/
264 ipv4_hdr = rte_pktmbuf_mtod_offset(pkts[i],
265 struct rte_ipv4_hdr *, sizeof(struct rte_ether_hdr));
266 ipv4_dst_ip[i] = ipv4_hdr->dst_addr;
267 key_ptrs[i] = (void *)&ipv4_dst_ip[i];
270 rte_efd_lookup_bulk(efd_table, socket_id, rx_count,
271 (const void **) key_ptrs, data);
272 for (i = 0; i < rx_count; i++) {
273 node = (uint8_t) ((uintptr_t)data[i]);
275 if (node >= num_nodes) {
277 * Node is out of range, which means that
278 * flow has not been inserted
280 flow_dist_stats.drop++;
281 rte_pktmbuf_free(pkts[i]);
283 flow_dist_stats.distributed++;
284 enqueue_rx_packet(node, pkts[i]);
288 for (i = 0; i < num_nodes; i++)
293 * Function called by the master lcore of the DPDK process.
296 do_packet_forwarding(void)
298 unsigned int port_num = 0; /* indexes the port[] array */
299 unsigned int socket_id = rte_socket_id();
302 struct rte_mbuf *buf[PACKET_READ_SIZE];
306 rx_count = rte_eth_rx_burst(info->id[port_num], 0,
307 buf, PACKET_READ_SIZE);
308 info->rx_stats.rx[port_num] += rx_count;
310 /* Now process the NIC packets read */
311 if (likely(rx_count > 0))
312 process_packets(port_num, buf, rx_count, socket_id);
314 /* move to next port */
315 if (++port_num == info->num_ports)
321 main(int argc, char *argv[])
323 /* initialise the system */
324 if (init(argc, argv) < 0)
326 RTE_LOG(INFO, APP, "Finished Process Init.\n");
328 cl_rx_buf = calloc(num_nodes, sizeof(cl_rx_buf[0]));
330 /* clear statistics */
333 /* put all other cores to sleep bar master */
334 rte_eal_mp_remote_launch(sleep_lcore, NULL, SKIP_MASTER);
336 do_packet_forwarding();