1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2016-2017 Intel Corporation 3 */ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include <unistd.h> 9 #include <stdint.h> 10 #include <stdarg.h> 11 #include <inttypes.h> 12 #include <sys/queue.h> 13 #include <errno.h> 14 #include <sys/types.h> 15 #include <netinet/in.h> 16 #include <netinet/ip.h> 17 18 #include <rte_common.h> 19 #include <rte_memory.h> 20 #include <rte_eal.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> 26 #include <rte_ring.h> 27 #include <rte_log.h> 28 #include <rte_debug.h> 29 #include <rte_mempool.h> 30 #include <rte_memcpy.h> 31 #include <rte_mbuf.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> 38 #include <rte_efd.h> 39 #include <rte_ip.h> 40 41 #include "common.h" 42 #include "args.h" 43 #include "init.h" 44 45 /* 46 * When doing reads from the NIC or the node queues, 47 * use this batch size 48 */ 49 #define PACKET_READ_SIZE 32 50 51 /* 52 * Local buffers to put packets in, used to send packets in bursts to the 53 * nodes 54 */ 55 struct node_rx_buf { 56 struct rte_mbuf *buffer[PACKET_READ_SIZE]; 57 uint16_t count; 58 }; 59 60 struct efd_stats { 61 uint64_t distributed; 62 uint64_t drop; 63 } flow_dist_stats; 64 65 /* One buffer per node rx queue - dynamically allocate array */ 66 static struct node_rx_buf *cl_rx_buf; 67 68 static const char * 69 get_printable_mac_addr(uint16_t port) 70 { 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; 74 int ret; 75 76 if (unlikely(port >= RTE_MAX_ETHPORTS)) 77 return err_address; 78 if (unlikely(addresses[port][0] == '\0')) { 79 ret = rte_eth_macaddr_get(port, &mac); 80 if (ret != 0) { 81 printf("Failed to get MAC address (port %u): %s\n", 82 port, rte_strerror(-ret)); 83 return err_address; 84 } 85 86 snprintf(addresses[port], sizeof(addresses[port]), 87 RTE_ETHER_ADDR_PRT_FMT "\n", 88 RTE_ETHER_ADDR_BYTES(&mac)); 89 } 90 return addresses[port]; 91 } 92 93 /* 94 * This function displays the recorded statistics for each port 95 * and for each node. It uses ANSI terminal codes to clear 96 * screen when called. It is called from a single worker 97 * thread in the server process, when the process is run with more 98 * than one lcore enabled. 99 */ 100 101 /* Display recorded statistics. 8< */ 102 static void 103 do_stats_display(void) 104 { 105 unsigned int i, j; 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]; 110 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)); 116 117 for (i = 0; i < num_nodes; i++) { 118 const struct tx_stats *tx = &info->tx_stats[i]; 119 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]]; 123 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; 128 } 129 } 130 131 /* Clear screen and move to top left */ 132 printf("%s%s", clr, topLeft); 133 134 printf("PORTS\n"); 135 printf("-----\n"); 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])); 139 printf("\n\n"); 140 for (i = 0; i < info->num_ports; i++) { 141 printf("Port %u - rx: %9"PRIu64"\t" 142 "tx: %9"PRIu64"\n", 143 (unsigned int)info->id[i], info->rx_stats.rx[i], 144 port_tx[i]); 145 } 146 147 printf("\nSERVER\n"); 148 printf("-----\n"); 149 printf("distributed: %9"PRIu64", drop: %9"PRIu64"\n", 150 flow_dist_stats.distributed, flow_dist_stats.drop); 151 152 printf("\nNODES\n"); 153 printf("-------\n"); 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]; 158 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); 165 } 166 167 printf("\n"); 168 } 169 /* >8 End of displaying the recorded statistics. */ 170 171 /* 172 * The function called from each non-main lcore used by the process. 173 * The test_and_set function is used to randomly pick a single lcore on which 174 * the code to display the statistics will run. Otherwise, the code just 175 * repeatedly sleeps. 176 */ 177 static int 178 sleep_lcore(__rte_unused void *dummy) 179 { 180 /* Used to pick a display thread - static, so zero-initialised */ 181 static rte_atomic32_t display_stats; 182 183 /* Only one core should display stats */ 184 if (rte_atomic32_test_and_set(&display_stats)) { 185 const unsigned int sleeptime = 1; 186 187 printf("Core %u displaying statistics\n", rte_lcore_id()); 188 189 /* Longer initial pause so above printf is seen */ 190 sleep(sleeptime * 3); 191 192 /* Loop forever: sleep always returns 0 or <= param */ 193 while (sleep(sleeptime) <= sleeptime) 194 do_stats_display(); 195 } 196 return 0; 197 } 198 199 /* 200 * Function to set all the node statistic values to zero. 201 * Called at program startup. 202 */ 203 static void 204 clear_stats(void) 205 { 206 unsigned int i; 207 208 for (i = 0; i < num_nodes; i++) 209 nodes[i].stats.rx = nodes[i].stats.rx_drop = 0; 210 } 211 212 /* 213 * send a burst of traffic to a node, assuming there are packets 214 * available to be sent to this node 215 */ 216 217 /* Flush rx queue. 8< */ 218 static void 219 flush_rx_queue(uint16_t node) 220 { 221 uint16_t j; 222 struct node *cl; 223 224 if (cl_rx_buf[node].count == 0) 225 return; 226 227 cl = &nodes[node]; 228 if (rte_ring_enqueue_bulk(cl->rx_q, (void **)cl_rx_buf[node].buffer, 229 cl_rx_buf[node].count, NULL) != cl_rx_buf[node].count){ 230 for (j = 0; j < cl_rx_buf[node].count; j++) 231 rte_pktmbuf_free(cl_rx_buf[node].buffer[j]); 232 cl->stats.rx_drop += cl_rx_buf[node].count; 233 } else 234 cl->stats.rx += cl_rx_buf[node].count; 235 236 cl_rx_buf[node].count = 0; 237 } 238 /* >8 End of sending a burst of traffic to a node. */ 239 240 /* 241 * marks a packet down to be sent to a particular node process 242 */ 243 static inline void 244 enqueue_rx_packet(uint8_t node, struct rte_mbuf *buf) 245 { 246 cl_rx_buf[node].buffer[cl_rx_buf[node].count++] = buf; 247 } 248 249 /* 250 * This function takes a group of packets and routes them 251 * individually to the node process. Very simply round-robins the packets 252 * without checking any of the packet contents. 8< 253 */ 254 255 /* Processing packets. 8< */ 256 static void 257 process_packets(uint32_t port_num __rte_unused, struct rte_mbuf *pkts[], 258 uint16_t rx_count, unsigned int socket_id) 259 { 260 uint16_t i; 261 uint8_t node; 262 efd_value_t data[RTE_EFD_BURST_MAX]; 263 const void *key_ptrs[RTE_EFD_BURST_MAX]; 264 265 struct rte_ipv4_hdr *ipv4_hdr; 266 uint32_t ipv4_dst_ip[RTE_EFD_BURST_MAX]; 267 268 for (i = 0; i < rx_count; i++) { 269 /* Handle IPv4 header.*/ 270 ipv4_hdr = rte_pktmbuf_mtod_offset(pkts[i], 271 struct rte_ipv4_hdr *, sizeof(struct rte_ether_hdr)); 272 ipv4_dst_ip[i] = ipv4_hdr->dst_addr; 273 key_ptrs[i] = (void *)&ipv4_dst_ip[i]; 274 } 275 276 rte_efd_lookup_bulk(efd_table, socket_id, rx_count, 277 (const void **) key_ptrs, data); 278 for (i = 0; i < rx_count; i++) { 279 node = (uint8_t) ((uintptr_t)data[i]); 280 281 if (node >= num_nodes) { 282 /* 283 * Node is out of range, which means that 284 * flow has not been inserted 285 */ 286 flow_dist_stats.drop++; 287 rte_pktmbuf_free(pkts[i]); 288 } else { 289 flow_dist_stats.distributed++; 290 enqueue_rx_packet(node, pkts[i]); 291 } 292 } 293 294 for (i = 0; i < num_nodes; i++) 295 flush_rx_queue(i); 296 } 297 /* >8 End of process_packets. */ 298 299 /* 300 * Function called by the main lcore of the DPDK process. 301 */ 302 static void 303 do_packet_forwarding(void) 304 { 305 unsigned int port_num = 0; /* indexes the port[] array */ 306 unsigned int socket_id = rte_socket_id(); 307 308 for (;;) { 309 struct rte_mbuf *buf[PACKET_READ_SIZE]; 310 uint16_t rx_count; 311 312 /* read a port */ 313 rx_count = rte_eth_rx_burst(info->id[port_num], 0, 314 buf, PACKET_READ_SIZE); 315 info->rx_stats.rx[port_num] += rx_count; 316 317 /* Now process the NIC packets read */ 318 if (likely(rx_count > 0)) 319 process_packets(port_num, buf, rx_count, socket_id); 320 321 /* move to next port */ 322 if (++port_num == info->num_ports) 323 port_num = 0; 324 } 325 } 326 327 int 328 main(int argc, char *argv[]) 329 { 330 /* initialise the system */ 331 if (init(argc, argv) < 0) 332 return -1; 333 RTE_LOG(INFO, APP, "Finished Process Init.\n"); 334 335 cl_rx_buf = calloc(num_nodes, sizeof(cl_rx_buf[0])); 336 337 /* clear statistics */ 338 clear_stats(); 339 340 /* put all other cores to sleep except main */ 341 rte_eal_mp_remote_launch(sleep_lcore, NULL, SKIP_MAIN); 342 343 do_packet_forwarding(); 344 345 /* clean up the EAL */ 346 rte_eal_cleanup(); 347 348 return 0; 349 } 350