4 * Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
38 #include <sys/queue.h>
43 #include <rte_common.h>
44 #include <rte_memory.h>
45 #include <rte_memzone.h>
46 #include <rte_tailq.h>
48 #include <rte_byteorder.h>
49 #include <rte_atomic.h>
50 #include <rte_launch.h>
51 #include <rte_per_lcore.h>
52 #include <rte_lcore.h>
53 #include <rte_branch_prediction.h>
54 #include <rte_debug.h>
57 #include <rte_mempool.h>
58 #include <rte_memcpy.h>
60 #include <rte_interrupts.h>
62 #include <rte_ether.h>
63 #include <rte_ethdev.h>
64 #include <rte_malloc.h>
65 #include <rte_fbk_hash.h>
66 #include <rte_string_fns.h>
67 #include <rte_cycles.h>
70 #include "init_drivers.h"
75 #define MBUFS_PER_CLIENT 1536
76 #define MBUFS_PER_PORT 1536
77 #define MBUF_CACHE_SIZE 512
78 #define MBUF_OVERHEAD (sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
79 #define RX_MBUF_DATA_SIZE 2048
80 #define MBUF_SIZE (RX_MBUF_DATA_SIZE + MBUF_OVERHEAD)
82 #define RTE_MP_RX_DESC_DEFAULT 512
83 #define RTE_MP_TX_DESC_DEFAULT 512
84 #define CLIENT_QUEUE_RINGSIZE 128
89 * RX and TX Prefetch, Host, and Write-back threshold values should be
90 * carefully set for optimal performance. Consult the network
91 * controller's datasheet and supporting DPDK documentation for guidance
92 * on how these parameters should be set.
94 /* Default configuration for rx and tx thresholds etc. */
96 * These default values are optimized for use with the Intel(R) 82599 10 GbE
97 * Controller and the DPDK ixgbe PMD. Consider using other values for other
98 * network controllers and/or network drivers.
100 #define MP_DEFAULT_PTHRESH 36
101 #define MP_DEFAULT_RX_HTHRESH 8
102 #define MP_DEFAULT_TX_HTHRESH 0
103 #define MP_DEFAULT_WTHRESH 0
105 static const struct rte_eth_rxconf rx_conf_default = {
107 .pthresh = MP_DEFAULT_PTHRESH,
108 .hthresh = MP_DEFAULT_RX_HTHRESH,
109 .wthresh = MP_DEFAULT_WTHRESH,
113 static const struct rte_eth_txconf tx_conf_default = {
115 .pthresh = MP_DEFAULT_PTHRESH,
116 .hthresh = MP_DEFAULT_TX_HTHRESH,
117 .wthresh = MP_DEFAULT_WTHRESH,
119 .tx_free_thresh = 0, /* Use PMD default values */
120 .tx_rs_thresh = 0, /* Use PMD default values */
123 /* The mbuf pool for packet rx */
124 struct rte_mempool *pktmbuf_pool;
126 /* array of info/queues for clients */
127 struct client *clients = NULL;
129 /* the port details */
130 struct port_info *ports;
133 * Initialise the mbuf pool for packet reception for the NIC, and any other
134 * buffer pools needed by the app - currently none.
137 init_mbuf_pools(void)
139 const unsigned num_mbufs = (num_clients * MBUFS_PER_CLIENT) \
140 + (ports->num_ports * MBUFS_PER_PORT);
142 /* don't pass single-producer/single-consumer flags to mbuf create as it
143 * seems faster to use a cache instead */
144 printf("Creating mbuf pool '%s' [%u mbufs] ...\n",
145 PKTMBUF_POOL_NAME, num_mbufs);
146 pktmbuf_pool = rte_mempool_create(PKTMBUF_POOL_NAME, num_mbufs,
147 MBUF_SIZE, MBUF_CACHE_SIZE,
148 sizeof(struct rte_pktmbuf_pool_private), rte_pktmbuf_pool_init,
149 NULL, rte_pktmbuf_init, NULL, rte_socket_id(), NO_FLAGS );
151 return (pktmbuf_pool == NULL); /* 0 on success */
155 * Initialise an individual port:
156 * - configure number of rx and tx rings
157 * - set up each rx ring, to pull from the main mbuf pool
158 * - set up each tx ring
159 * - start the port and report its status to stdout
162 init_port(uint8_t port_num)
164 /* for port configuration all features are off by default */
165 const struct rte_eth_conf port_conf = {
167 .mq_mode = ETH_MQ_RX_RSS
170 const uint16_t rx_rings = 1, tx_rings = num_clients;
171 const uint16_t rx_ring_size = RTE_MP_RX_DESC_DEFAULT;
172 const uint16_t tx_ring_size = RTE_MP_TX_DESC_DEFAULT;
177 printf("Port %u init ... ", (unsigned)port_num);
180 /* Standard DPDK port initialisation - config port, then set up
182 if ((retval = rte_eth_dev_configure(port_num, rx_rings, tx_rings,
186 for (q = 0; q < rx_rings; q++) {
187 retval = rte_eth_rx_queue_setup(port_num, q, rx_ring_size,
188 rte_eth_dev_socket_id(port_num), &rx_conf_default, pktmbuf_pool);
189 if (retval < 0) return retval;
192 for ( q = 0; q < tx_rings; q ++ ) {
193 retval = rte_eth_tx_queue_setup(port_num, q, tx_ring_size,
194 rte_eth_dev_socket_id(port_num), &tx_conf_default);
195 if (retval < 0) return retval;
198 rte_eth_promiscuous_enable(port_num);
200 retval = rte_eth_dev_start(port_num);
201 if (retval < 0) return retval;
209 * Set up the DPDK rings which will be used to pass packets, via
210 * pointers, between the multi-process server and client processes.
211 * Each client needs one RX queue.
219 const unsigned ringsize = CLIENT_QUEUE_RINGSIZE;
221 clients = rte_malloc("client details",
222 sizeof(*clients) * num_clients, 0);
224 rte_exit(EXIT_FAILURE, "Cannot allocate memory for client program details\n");
226 for (i = 0; i < num_clients; i++) {
227 /* Create an RX queue for each client */
228 socket_id = rte_socket_id();
229 q_name = get_rx_queue_name(i);
230 clients[i].rx_q = rte_ring_create(q_name,
232 RING_F_SP_ENQ | RING_F_SC_DEQ ); /* single prod, single cons */
233 if (clients[i].rx_q == NULL)
234 rte_exit(EXIT_FAILURE, "Cannot create rx ring queue for client %u\n", i);
239 /* Check the link status of all ports in up to 9s, and print them finally */
241 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
243 #define CHECK_INTERVAL 100 /* 100ms */
244 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
245 uint8_t portid, count, all_ports_up, print_flag = 0;
246 struct rte_eth_link link;
248 printf("\nChecking link status");
250 for (count = 0; count <= MAX_CHECK_TIME; count++) {
252 for (portid = 0; portid < port_num; portid++) {
253 if ((port_mask & (1 << ports->id[portid])) == 0)
255 memset(&link, 0, sizeof(link));
256 rte_eth_link_get_nowait(ports->id[portid], &link);
257 /* print link status if flag set */
258 if (print_flag == 1) {
259 if (link.link_status)
260 printf("Port %d Link Up - speed %u "
261 "Mbps - %s\n", ports->id[portid],
262 (unsigned)link.link_speed,
263 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
264 ("full-duplex") : ("half-duplex\n"));
266 printf("Port %d Link Down\n",
267 (uint8_t)ports->id[portid]);
270 /* clear all_ports_up flag if any link down */
271 if (link.link_status == 0) {
276 /* after finally printing all link status, get out */
280 if (all_ports_up == 0) {
283 rte_delay_ms(CHECK_INTERVAL);
286 /* set the print_flag if all ports up or timeout */
287 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
295 * Main init function for the multi-process server app,
296 * calls subfunctions to do each stage of the initialisation.
299 init(int argc, char *argv[])
302 const struct rte_memzone *mz;
303 uint8_t i, total_ports;
305 /* init EAL, parsing EAL args */
306 retval = rte_eal_init(argc, argv);
312 /* initialise the nic drivers */
313 retval = init_drivers();
315 rte_exit(EXIT_FAILURE, "Cannot initialise drivers\n");
317 /* get total number of ports */
318 total_ports = rte_eth_dev_count();
320 /* set up array for port data */
321 mz = rte_memzone_reserve(MZ_PORT_INFO, sizeof(*ports),
322 rte_socket_id(), NO_FLAGS);
324 rte_exit(EXIT_FAILURE, "Cannot reserve memory zone for port information\n");
325 memset(mz->addr, 0, sizeof(*ports));
328 /* parse additional, application arguments */
329 retval = parse_app_args(total_ports, argc, argv);
333 /* initialise mbuf pools */
334 retval = init_mbuf_pools();
336 rte_exit(EXIT_FAILURE, "Cannot create needed mbuf pools\n");
338 /* now initialise the ports we will use */
339 for (i = 0; i < ports->num_ports; i++) {
340 retval = init_port(ports->id[i]);
342 rte_exit(EXIT_FAILURE, "Cannot initialise port %u\n",
346 check_all_ports_link_status(ports->num_ports, (~0x0));
348 /* initialise the client queues/rings for inter-eu comms */