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 "%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]); 91 } 92 return addresses[port]; 93 } 94 95 /* 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 worker 99 * thread in the server process, when the process is run with more 100 * than one lcore enabled. 101 */ 102 103 /* Display recorded statistics. 8< */ 104 static void 105 do_stats_display(void) 106 { 107 unsigned int i, j; 108 const char clr[] = {27, '[', '2', 'J', '\0'}; 109 const char topLeft[] = {27, '[', '1', ';', '1', 'H', '\0'}; 110 uint64_t port_tx[RTE_MAX_ETHPORTS], port_tx_drop[RTE_MAX_ETHPORTS]; 111 uint64_t node_tx[MAX_NODES], node_tx_drop[MAX_NODES]; 112 113 /* to get TX stats, we need to do some summing calculations */ 114 memset(port_tx, 0, sizeof(port_tx)); 115 memset(port_tx_drop, 0, sizeof(port_tx_drop)); 116 memset(node_tx, 0, sizeof(node_tx)); 117 memset(node_tx_drop, 0, sizeof(node_tx_drop)); 118 119 for (i = 0; i < num_nodes; i++) { 120 const struct tx_stats *tx = &info->tx_stats[i]; 121 122 for (j = 0; j < info->num_ports; j++) { 123 const uint64_t tx_val = tx->tx[info->id[j]]; 124 const uint64_t drop_val = tx->tx_drop[info->id[j]]; 125 126 port_tx[j] += tx_val; 127 port_tx_drop[j] += drop_val; 128 node_tx[i] += tx_val; 129 node_tx_drop[i] += drop_val; 130 } 131 } 132 133 /* Clear screen and move to top left */ 134 printf("%s%s", clr, topLeft); 135 136 printf("PORTS\n"); 137 printf("-----\n"); 138 for (i = 0; i < info->num_ports; i++) 139 printf("Port %u: '%s'\t", (unsigned int)info->id[i], 140 get_printable_mac_addr(info->id[i])); 141 printf("\n\n"); 142 for (i = 0; i < info->num_ports; i++) { 143 printf("Port %u - rx: %9"PRIu64"\t" 144 "tx: %9"PRIu64"\n", 145 (unsigned int)info->id[i], info->rx_stats.rx[i], 146 port_tx[i]); 147 } 148 149 printf("\nSERVER\n"); 150 printf("-----\n"); 151 printf("distributed: %9"PRIu64", drop: %9"PRIu64"\n", 152 flow_dist_stats.distributed, flow_dist_stats.drop); 153 154 printf("\nNODES\n"); 155 printf("-------\n"); 156 for (i = 0; i < num_nodes; i++) { 157 const unsigned long long rx = nodes[i].stats.rx; 158 const unsigned long long rx_drop = nodes[i].stats.rx_drop; 159 const struct filter_stats *filter = &info->filter_stats[i]; 160 161 printf("Node %2u - rx: %9llu, rx_drop: %9llu\n" 162 " tx: %9"PRIu64", tx_drop: %9"PRIu64"\n" 163 " filter_passed: %9"PRIu64", " 164 "filter_drop: %9"PRIu64"\n", 165 i, rx, rx_drop, node_tx[i], node_tx_drop[i], 166 filter->passed, filter->drop); 167 } 168 169 printf("\n"); 170 } 171 /* >8 End of displaying the recorded statistics. */ 172 173 /* 174 * The function called from each non-main lcore used by the process. 175 * The test_and_set function is used to randomly pick a single lcore on which 176 * the code to display the statistics will run. Otherwise, the code just 177 * repeatedly sleeps. 178 */ 179 static int 180 sleep_lcore(__rte_unused void *dummy) 181 { 182 /* Used to pick a display thread - static, so zero-initialised */ 183 static rte_atomic32_t display_stats; 184 185 /* Only one core should display stats */ 186 if (rte_atomic32_test_and_set(&display_stats)) { 187 const unsigned int sleeptime = 1; 188 189 printf("Core %u displaying statistics\n", rte_lcore_id()); 190 191 /* Longer initial pause so above printf is seen */ 192 sleep(sleeptime * 3); 193 194 /* Loop forever: sleep always returns 0 or <= param */ 195 while (sleep(sleeptime) <= sleeptime) 196 do_stats_display(); 197 } 198 return 0; 199 } 200 201 /* 202 * Function to set all the node statistic values to zero. 203 * Called at program startup. 204 */ 205 static void 206 clear_stats(void) 207 { 208 unsigned int i; 209 210 for (i = 0; i < num_nodes; i++) 211 nodes[i].stats.rx = nodes[i].stats.rx_drop = 0; 212 } 213 214 /* 215 * send a burst of traffic to a node, assuming there are packets 216 * available to be sent to this node 217 */ 218 219 /* Flush rx queue. 8< */ 220 static void 221 flush_rx_queue(uint16_t node) 222 { 223 uint16_t j; 224 struct node *cl; 225 226 if (cl_rx_buf[node].count == 0) 227 return; 228 229 cl = &nodes[node]; 230 if (rte_ring_enqueue_bulk(cl->rx_q, (void **)cl_rx_buf[node].buffer, 231 cl_rx_buf[node].count, NULL) != cl_rx_buf[node].count){ 232 for (j = 0; j < cl_rx_buf[node].count; j++) 233 rte_pktmbuf_free(cl_rx_buf[node].buffer[j]); 234 cl->stats.rx_drop += cl_rx_buf[node].count; 235 } else 236 cl->stats.rx += cl_rx_buf[node].count; 237 238 cl_rx_buf[node].count = 0; 239 } 240 /* >8 End of sending a burst of traffic to a node. */ 241 242 /* 243 * marks a packet down to be sent to a particular node process 244 */ 245 static inline void 246 enqueue_rx_packet(uint8_t node, struct rte_mbuf *buf) 247 { 248 cl_rx_buf[node].buffer[cl_rx_buf[node].count++] = buf; 249 } 250 251 /* 252 * This function takes a group of packets and routes them 253 * individually to the node process. Very simply round-robins the packets 254 * without checking any of the packet contents. 8< 255 */ 256 257 /* Processing packets. 8< */ 258 static void 259 process_packets(uint32_t port_num __rte_unused, struct rte_mbuf *pkts[], 260 uint16_t rx_count, unsigned int socket_id) 261 { 262 uint16_t i; 263 uint8_t node; 264 efd_value_t data[RTE_EFD_BURST_MAX]; 265 const void *key_ptrs[RTE_EFD_BURST_MAX]; 266 267 struct rte_ipv4_hdr *ipv4_hdr; 268 uint32_t ipv4_dst_ip[RTE_EFD_BURST_MAX]; 269 270 for (i = 0; i < rx_count; i++) { 271 /* Handle IPv4 header.*/ 272 ipv4_hdr = rte_pktmbuf_mtod_offset(pkts[i], 273 struct rte_ipv4_hdr *, sizeof(struct rte_ether_hdr)); 274 ipv4_dst_ip[i] = ipv4_hdr->dst_addr; 275 key_ptrs[i] = (void *)&ipv4_dst_ip[i]; 276 } 277 278 rte_efd_lookup_bulk(efd_table, socket_id, rx_count, 279 (const void **) key_ptrs, data); 280 for (i = 0; i < rx_count; i++) { 281 node = (uint8_t) ((uintptr_t)data[i]); 282 283 if (node >= num_nodes) { 284 /* 285 * Node is out of range, which means that 286 * flow has not been inserted 287 */ 288 flow_dist_stats.drop++; 289 rte_pktmbuf_free(pkts[i]); 290 } else { 291 flow_dist_stats.distributed++; 292 enqueue_rx_packet(node, pkts[i]); 293 } 294 } 295 296 for (i = 0; i < num_nodes; i++) 297 flush_rx_queue(i); 298 } 299 /* >8 End of process_packets. */ 300 301 /* 302 * Function called by the main lcore of the DPDK process. 303 */ 304 static void 305 do_packet_forwarding(void) 306 { 307 unsigned int port_num = 0; /* indexes the port[] array */ 308 unsigned int socket_id = rte_socket_id(); 309 310 for (;;) { 311 struct rte_mbuf *buf[PACKET_READ_SIZE]; 312 uint16_t rx_count; 313 314 /* read a port */ 315 rx_count = rte_eth_rx_burst(info->id[port_num], 0, 316 buf, PACKET_READ_SIZE); 317 info->rx_stats.rx[port_num] += rx_count; 318 319 /* Now process the NIC packets read */ 320 if (likely(rx_count > 0)) 321 process_packets(port_num, buf, rx_count, socket_id); 322 323 /* move to next port */ 324 if (++port_num == info->num_ports) 325 port_num = 0; 326 } 327 } 328 329 int 330 main(int argc, char *argv[]) 331 { 332 /* initialise the system */ 333 if (init(argc, argv) < 0) 334 return -1; 335 RTE_LOG(INFO, APP, "Finished Process Init.\n"); 336 337 cl_rx_buf = calloc(num_nodes, sizeof(cl_rx_buf[0])); 338 339 /* clear statistics */ 340 clear_stats(); 341 342 /* put all other cores to sleep except main */ 343 rte_eal_mp_remote_launch(sleep_lcore, NULL, SKIP_MAIN); 344 345 do_packet_forwarding(); 346 347 /* clean up the EAL */ 348 rte_eal_cleanup(); 349 350 return 0; 351 } 352