1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2010-2013 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 35 #include <stdint.h> 36 #include <stdio.h> 37 #include <inttypes.h> 38 #include <stdarg.h> 39 #include <errno.h> 40 #include <sys/queue.h> 41 #include <stdlib.h> 42 #include <getopt.h> 43 #include <string.h> 44 45 #include <rte_common.h> 46 #include <rte_memory.h> 47 #include <rte_memzone.h> 48 #include <rte_tailq.h> 49 #include <rte_eal.h> 50 #include <rte_atomic.h> 51 #include <rte_branch_prediction.h> 52 #include <rte_log.h> 53 #include <rte_per_lcore.h> 54 #include <rte_launch.h> 55 #include <rte_lcore.h> 56 #include <rte_ring.h> 57 #include <rte_launch.h> 58 #include <rte_lcore.h> 59 #include <rte_debug.h> 60 #include <rte_mempool.h> 61 #include <rte_mbuf.h> 62 #include <rte_interrupts.h> 63 #include <rte_pci.h> 64 #include <rte_ether.h> 65 #include <rte_ethdev.h> 66 #include <rte_string_fns.h> 67 68 #include "common.h" 69 #include "init_drivers.h" 70 71 /* Number of packets to attempt to read from queue */ 72 #define PKT_READ_SIZE ((uint16_t)32) 73 74 /* our client id number - tells us which rx queue to read, and NIC TX 75 * queue to write to. */ 76 static uint8_t client_id = 0; 77 78 struct mbuf_queue { 79 #define MBQ_CAPACITY 32 80 struct rte_mbuf *bufs[MBQ_CAPACITY]; 81 uint16_t top; 82 }; 83 84 /* maps input ports to output ports for packets */ 85 static uint8_t output_ports[RTE_MAX_ETHPORTS]; 86 87 /* buffers up a set of packet that are ready to send */ 88 static struct mbuf_queue output_bufs[RTE_MAX_ETHPORTS]; 89 90 /* shared data from server. We update statistics here */ 91 static volatile struct tx_stats *tx_stats; 92 93 94 /* 95 * print a usage message 96 */ 97 static void 98 usage(const char *progname) 99 { 100 printf("Usage: %s [EAL args] -- -n <client_id>\n\n", progname); 101 } 102 103 /* 104 * Convert the client id number from a string to an int. 105 */ 106 static int 107 parse_client_num(const char *client) 108 { 109 char *end = NULL; 110 unsigned long temp; 111 112 if (client == NULL || *client == '\0') 113 return -1; 114 115 temp = strtoul(client, &end, 10); 116 if (end == NULL || *end != '\0') 117 return -1; 118 119 client_id = (uint8_t)temp; 120 return 0; 121 } 122 123 /* 124 * Parse the application arguments to the client app. 125 */ 126 static int 127 parse_app_args(int argc, char *argv[]) 128 { 129 int option_index, opt; 130 char **argvopt = argv; 131 const char *progname = NULL; 132 static struct option lgopts[] = { /* no long options */ 133 {NULL, 0, 0, 0 } 134 }; 135 progname = argv[0]; 136 137 while ((opt = getopt_long(argc, argvopt, "n:", lgopts, 138 &option_index)) != EOF){ 139 switch (opt){ 140 case 'n': 141 if (parse_client_num(optarg) != 0){ 142 usage(progname); 143 return -1; 144 } 145 break; 146 default: 147 usage(progname); 148 return -1; 149 } 150 } 151 return 0; 152 } 153 154 /* 155 * set up output ports so that all traffic on port gets sent out 156 * its paired port. Index using actual port numbers since that is 157 * what comes in the mbuf structure. 158 */ 159 static void configure_output_ports(const struct port_info *ports) 160 { 161 int i; 162 if (ports->num_ports > RTE_MAX_ETHPORTS) 163 rte_exit(EXIT_FAILURE, "Too many ethernet ports. RTE_MAX_ETHPORTS = %u\n", 164 (unsigned)RTE_MAX_ETHPORTS); 165 for (i = 0; i < ports->num_ports - 1; i+=2){ 166 uint8_t p1 = ports->id[i]; 167 uint8_t p2 = ports->id[i+1]; 168 output_ports[p1] = p2; 169 output_ports[p2] = p1; 170 } 171 } 172 173 174 static inline void 175 send_packets(uint8_t port) 176 { 177 uint16_t i, sent; 178 struct mbuf_queue *mbq = &output_bufs[port]; 179 180 if (unlikely(mbq->top == 0)) 181 return; 182 183 sent = rte_eth_tx_burst(port, client_id, mbq->bufs, mbq->top); 184 if (unlikely(sent < mbq->top)){ 185 for (i = sent; i < mbq->top; i++) 186 rte_pktmbuf_free(mbq->bufs[i]); 187 tx_stats->tx_drop[port] += (mbq->top - sent); 188 } 189 tx_stats->tx[port] += sent; 190 mbq->top = 0; 191 } 192 193 /* 194 * Enqueue a packet to be sent on a particular port, but 195 * don't send it yet. Only when the buffer is full. 196 */ 197 static inline void 198 enqueue_packet(struct rte_mbuf *buf, uint8_t port) 199 { 200 struct mbuf_queue *mbq = &output_bufs[port]; 201 mbq->bufs[mbq->top++] = buf; 202 203 if (mbq->top == MBQ_CAPACITY) 204 send_packets(port); 205 } 206 207 /* 208 * This function performs routing of packets 209 * Just sends each input packet out an output port based solely on the input 210 * port it arrived on. 211 */ 212 static void 213 handle_packet(struct rte_mbuf *buf) 214 { 215 const uint8_t in_port = buf->pkt.in_port; 216 const uint8_t out_port = output_ports[in_port]; 217 218 enqueue_packet(buf, out_port); 219 } 220 221 /* 222 * Application main function - loops through 223 * receiving and processing packets. Never returns 224 */ 225 int 226 main(int argc, char *argv[]) 227 { 228 const struct rte_memzone *mz; 229 struct rte_ring *rx_ring; 230 struct rte_mempool *mp; 231 struct port_info *ports; 232 int need_flush = 0; /* indicates whether we have unsent packets */ 233 int retval; 234 void *pkts[PKT_READ_SIZE]; 235 236 if ((retval = rte_eal_init(argc, argv)) < 0) 237 return -1; 238 argc -= retval; 239 argv += retval; 240 241 if (parse_app_args(argc, argv) < 0) 242 rte_exit(EXIT_FAILURE, "Invalid command-line arguments\n"); 243 244 if (init_drivers() < 0) 245 rte_exit(EXIT_FAILURE, "Cannot get NIC ports\n"); 246 if (rte_eth_dev_count() == 0) 247 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n"); 248 249 rx_ring = rte_ring_lookup(get_rx_queue_name(client_id)); 250 if (rx_ring == NULL) 251 rte_exit(EXIT_FAILURE, "Cannot get RX ring - is server process running?\n"); 252 253 mp = rte_mempool_lookup(PKTMBUF_POOL_NAME); 254 if (mp == NULL) 255 rte_exit(EXIT_FAILURE, "Cannot get mempool for mbufs\n"); 256 257 mz = rte_memzone_lookup(MZ_PORT_INFO); 258 if (mz == NULL) 259 rte_exit(EXIT_FAILURE, "Cannot get port info structure\n"); 260 ports = mz->addr; 261 tx_stats = &(ports->tx_stats[client_id]); 262 263 configure_output_ports(ports); 264 265 RTE_LOG(INFO, APP, "Finished Process Init.\n"); 266 267 printf("\nClient process %d handling packets\n", client_id); 268 printf("[Press Ctrl-C to quit ...]\n"); 269 270 for (;;) { 271 uint16_t i, rx_pkts = PKT_READ_SIZE; 272 uint8_t port; 273 274 /* try dequeuing max possible packets first, if that fails, get the 275 * most we can. Loop body should only execute once, maximum */ 276 while (rx_pkts > 0 && 277 unlikely(rte_ring_dequeue_bulk(rx_ring, pkts, rx_pkts) != 0)) 278 rx_pkts = (uint16_t)RTE_MIN(rte_ring_count(rx_ring), PKT_READ_SIZE); 279 280 if (unlikely(rx_pkts == 0)){ 281 if (need_flush) 282 for (port = 0; port < ports->num_ports; port++) 283 send_packets(ports->id[port]); 284 need_flush = 0; 285 continue; 286 } 287 288 for (i = 0; i < rx_pkts; i++) 289 handle_packet(pkts[i]); 290 291 need_flush = 1; 292 } 293 } 294