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