4 * Copyright(c) 2010-2014 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.
37 #include <sys/queue.h>
42 #include <rte_common.h>
43 #include <rte_memory.h>
44 #include <rte_memzone.h>
46 #include <rte_byteorder.h>
47 #include <rte_atomic.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_debug.h>
55 #include <rte_mempool.h>
56 #include <rte_memcpy.h>
58 #include <rte_interrupts.h>
60 #include <rte_ether.h>
61 #include <rte_ethdev.h>
62 #include <rte_malloc.h>
63 #include <rte_string_fns.h>
64 #include <rte_cycles.h>
70 #define MBUFS_PER_CLIENT 1536
71 #define MBUFS_PER_PORT 1536
72 #define MBUF_CACHE_SIZE 512
74 #define RTE_MP_RX_DESC_DEFAULT 512
75 #define RTE_MP_TX_DESC_DEFAULT 512
76 #define CLIENT_QUEUE_RINGSIZE 128
80 /* The mbuf pool for packet rx */
81 struct rte_mempool *pktmbuf_pool;
83 /* array of info/queues for clients */
84 struct client *clients = NULL;
86 /* the port details */
87 struct port_info *ports;
90 * Initialise the mbuf pool for packet reception for the NIC, and any other
91 * buffer pools needed by the app - currently none.
96 const unsigned num_mbufs = (num_clients * MBUFS_PER_CLIENT) \
97 + (ports->num_ports * MBUFS_PER_PORT);
99 /* don't pass single-producer/single-consumer flags to mbuf create as it
100 * seems faster to use a cache instead */
101 printf("Creating mbuf pool '%s' [%u mbufs] ...\n",
102 PKTMBUF_POOL_NAME, num_mbufs);
103 pktmbuf_pool = rte_pktmbuf_pool_create(PKTMBUF_POOL_NAME, num_mbufs,
104 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
106 return pktmbuf_pool == NULL; /* 0 on success */
110 * Initialise an individual port:
111 * - configure number of rx and tx rings
112 * - set up each rx ring, to pull from the main mbuf pool
113 * - set up each tx ring
114 * - start the port and report its status to stdout
117 init_port(uint16_t port_num)
119 /* for port configuration all features are off by default */
120 const struct rte_eth_conf port_conf = {
122 .mq_mode = ETH_MQ_RX_RSS
125 const uint16_t rx_rings = 1, tx_rings = num_clients;
126 uint16_t rx_ring_size = RTE_MP_RX_DESC_DEFAULT;
127 uint16_t tx_ring_size = RTE_MP_TX_DESC_DEFAULT;
132 printf("Port %u init ... ", port_num);
135 /* Standard DPDK port initialisation - config port, then set up
137 if ((retval = rte_eth_dev_configure(port_num, rx_rings, tx_rings,
141 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port_num, &rx_ring_size,
146 for (q = 0; q < rx_rings; q++) {
147 retval = rte_eth_rx_queue_setup(port_num, q, rx_ring_size,
148 rte_eth_dev_socket_id(port_num),
150 if (retval < 0) return retval;
153 for ( q = 0; q < tx_rings; q ++ ) {
154 retval = rte_eth_tx_queue_setup(port_num, q, tx_ring_size,
155 rte_eth_dev_socket_id(port_num),
157 if (retval < 0) return retval;
160 rte_eth_promiscuous_enable(port_num);
162 retval = rte_eth_dev_start(port_num);
163 if (retval < 0) return retval;
171 * Set up the DPDK rings which will be used to pass packets, via
172 * pointers, between the multi-process server and client processes.
173 * Each client needs one RX queue.
181 const unsigned ringsize = CLIENT_QUEUE_RINGSIZE;
183 clients = rte_malloc("client details",
184 sizeof(*clients) * num_clients, 0);
186 rte_exit(EXIT_FAILURE, "Cannot allocate memory for client program details\n");
188 for (i = 0; i < num_clients; i++) {
189 /* Create an RX queue for each client */
190 socket_id = rte_socket_id();
191 q_name = get_rx_queue_name(i);
192 clients[i].rx_q = rte_ring_create(q_name,
194 RING_F_SP_ENQ | RING_F_SC_DEQ ); /* single prod, single cons */
195 if (clients[i].rx_q == NULL)
196 rte_exit(EXIT_FAILURE, "Cannot create rx ring queue for client %u\n", i);
201 /* Check the link status of all ports in up to 9s, and print them finally */
203 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
205 #define CHECK_INTERVAL 100 /* 100ms */
206 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
208 uint8_t count, all_ports_up, print_flag = 0;
209 struct rte_eth_link link;
211 printf("\nChecking link status");
213 for (count = 0; count <= MAX_CHECK_TIME; count++) {
215 for (portid = 0; portid < port_num; portid++) {
216 if ((port_mask & (1 << ports->id[portid])) == 0)
218 memset(&link, 0, sizeof(link));
219 rte_eth_link_get_nowait(ports->id[portid], &link);
220 /* print link status if flag set */
221 if (print_flag == 1) {
222 if (link.link_status)
223 printf("Port %d Link Up - speed %u "
224 "Mbps - %s\n", ports->id[portid],
225 (unsigned)link.link_speed,
226 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
227 ("full-duplex") : ("half-duplex\n"));
229 printf("Port %d Link Down\n",
230 (uint8_t)ports->id[portid]);
233 /* clear all_ports_up flag if any link down */
234 if (link.link_status == ETH_LINK_DOWN) {
239 /* after finally printing all link status, get out */
243 if (all_ports_up == 0) {
246 rte_delay_ms(CHECK_INTERVAL);
249 /* set the print_flag if all ports up or timeout */
250 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
258 * Main init function for the multi-process server app,
259 * calls subfunctions to do each stage of the initialisation.
262 init(int argc, char *argv[])
265 const struct rte_memzone *mz;
266 uint16_t i, total_ports;
268 /* init EAL, parsing EAL args */
269 retval = rte_eal_init(argc, argv);
275 /* get total number of ports */
276 total_ports = rte_eth_dev_count();
278 /* set up array for port data */
279 mz = rte_memzone_reserve(MZ_PORT_INFO, sizeof(*ports),
280 rte_socket_id(), NO_FLAGS);
282 rte_exit(EXIT_FAILURE, "Cannot reserve memory zone for port information\n");
283 memset(mz->addr, 0, sizeof(*ports));
286 /* parse additional, application arguments */
287 retval = parse_app_args(total_ports, argc, argv);
291 /* initialise mbuf pools */
292 retval = init_mbuf_pools();
294 rte_exit(EXIT_FAILURE, "Cannot create needed mbuf pools\n");
296 /* now initialise the ports we will use */
297 for (i = 0; i < ports->num_ports; i++) {
298 retval = init_port(ports->id[i]);
300 rte_exit(EXIT_FAILURE, "Cannot initialise port %u\n",
304 check_all_ports_link_status(ports->num_ports, (~0x0));
306 /* initialise the client queues/rings for inter-eu comms */