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