doc: update release notes for 17.08
[dpdk.git] / examples / ethtool / ethtool-app / main.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015 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 <stdio.h>
36 #include <stdlib.h>
37
38 #include <rte_common.h>
39 #include <rte_spinlock.h>
40 #include <rte_eal.h>
41 #include <rte_ethdev.h>
42 #include <rte_ether.h>
43 #include <rte_ip.h>
44 #include <rte_memory.h>
45 #include <rte_mempool.h>
46 #include <rte_mbuf.h>
47
48 #include "ethapp.h"
49
50 #define MAX_PORTS RTE_MAX_ETHPORTS
51 #define MAX_BURST_LENGTH 32
52 #define PORT_RX_QUEUE_SIZE 128
53 #define PORT_TX_QUEUE_SIZE 256
54 #define PKTPOOL_EXTRA_SIZE 512
55 #define PKTPOOL_CACHE 32
56
57
58 struct txq_port {
59         uint16_t cnt_unsent;
60         struct rte_mbuf *buf_frames[MAX_BURST_LENGTH];
61 };
62
63 struct app_port {
64         struct ether_addr mac_addr;
65         struct txq_port txq;
66         rte_spinlock_t lock;
67         int port_active;
68         int port_dirty;
69         int idx_port;
70         struct rte_mempool *pkt_pool;
71 };
72
73 struct app_config {
74         struct app_port ports[MAX_PORTS];
75         int cnt_ports;
76         int exit_now;
77 };
78
79
80 struct app_config app_cfg;
81
82
83 void lock_port(int idx_port)
84 {
85         struct app_port *ptr_port = &app_cfg.ports[idx_port];
86
87         rte_spinlock_lock(&ptr_port->lock);
88 }
89
90 void unlock_port(int idx_port)
91 {
92         struct app_port *ptr_port = &app_cfg.ports[idx_port];
93
94         rte_spinlock_unlock(&ptr_port->lock);
95 }
96
97 void mark_port_active(int idx_port)
98 {
99         struct app_port *ptr_port = &app_cfg.ports[idx_port];
100
101         ptr_port->port_active = 1;
102 }
103
104 void mark_port_inactive(int idx_port)
105 {
106         struct app_port *ptr_port = &app_cfg.ports[idx_port];
107
108         ptr_port->port_active = 0;
109 }
110
111 void mark_port_newmac(int idx_port)
112 {
113         struct app_port *ptr_port = &app_cfg.ports[idx_port];
114
115         ptr_port->port_dirty = 1;
116 }
117
118 static void setup_ports(struct app_config *app_cfg, int cnt_ports)
119 {
120         int idx_port;
121         int size_pktpool;
122         struct rte_eth_conf cfg_port;
123         struct rte_eth_dev_info dev_info;
124         char str_name[16];
125         uint16_t nb_rxd = PORT_RX_QUEUE_SIZE;
126         uint16_t nb_txd = PORT_TX_QUEUE_SIZE;
127
128         memset(&cfg_port, 0, sizeof(cfg_port));
129         cfg_port.txmode.mq_mode = ETH_MQ_TX_NONE;
130
131         for (idx_port = 0; idx_port < cnt_ports; idx_port++) {
132                 struct app_port *ptr_port = &app_cfg->ports[idx_port];
133
134                 rte_eth_dev_info_get(idx_port, &dev_info);
135                 size_pktpool = dev_info.rx_desc_lim.nb_max +
136                         dev_info.tx_desc_lim.nb_max + PKTPOOL_EXTRA_SIZE;
137
138                 snprintf(str_name, 16, "pkt_pool%i", idx_port);
139                 ptr_port->pkt_pool = rte_pktmbuf_pool_create(
140                         str_name,
141                         size_pktpool, PKTPOOL_CACHE,
142                         0,
143                         RTE_MBUF_DEFAULT_BUF_SIZE,
144                         rte_socket_id()
145                         );
146                 if (ptr_port->pkt_pool == NULL)
147                         rte_exit(EXIT_FAILURE,
148                                 "rte_pktmbuf_pool_create failed"
149                                 );
150
151                 printf("Init port %i..\n", idx_port);
152                 ptr_port->port_active = 1;
153                 ptr_port->port_dirty = 0;
154                 ptr_port->idx_port = idx_port;
155
156                 if (rte_eth_dev_configure(idx_port, 1, 1, &cfg_port) < 0)
157                         rte_exit(EXIT_FAILURE,
158                                  "rte_eth_dev_configure failed");
159                 if (rte_eth_dev_adjust_nb_rx_tx_desc(idx_port, &nb_rxd,
160                                                      &nb_txd) < 0)
161                         rte_exit(EXIT_FAILURE,
162                                  "rte_eth_dev_adjust_nb_rx_tx_desc failed");
163                 if (rte_eth_rx_queue_setup(
164                             idx_port, 0, nb_rxd,
165                             rte_eth_dev_socket_id(idx_port), NULL,
166                             ptr_port->pkt_pool) < 0)
167                         rte_exit(EXIT_FAILURE,
168                                  "rte_eth_rx_queue_setup failed"
169                                 );
170                 if (rte_eth_tx_queue_setup(
171                             idx_port, 0, nb_txd,
172                             rte_eth_dev_socket_id(idx_port), NULL) < 0)
173                         rte_exit(EXIT_FAILURE,
174                                  "rte_eth_tx_queue_setup failed"
175                                 );
176                 if (rte_eth_dev_start(idx_port) < 0)
177                         rte_exit(EXIT_FAILURE,
178                                  "%s:%i: rte_eth_dev_start failed",
179                                  __FILE__, __LINE__
180                                 );
181                 rte_eth_macaddr_get(idx_port, &ptr_port->mac_addr);
182                 rte_spinlock_init(&ptr_port->lock);
183         }
184 }
185
186 static void process_frame(struct app_port *ptr_port,
187         struct rte_mbuf *ptr_frame)
188 {
189         struct ether_hdr *ptr_mac_hdr;
190
191         ptr_mac_hdr = rte_pktmbuf_mtod(ptr_frame, struct ether_hdr *);
192         ether_addr_copy(&ptr_mac_hdr->s_addr, &ptr_mac_hdr->d_addr);
193         ether_addr_copy(&ptr_port->mac_addr, &ptr_mac_hdr->s_addr);
194 }
195
196 static int slave_main(__attribute__((unused)) void *ptr_data)
197 {
198         struct app_port *ptr_port;
199         struct rte_mbuf *ptr_frame;
200         struct txq_port *txq;
201
202         uint16_t cnt_recv_frames;
203         uint16_t idx_frame;
204         uint16_t cnt_sent;
205         uint16_t idx_port;
206         uint16_t lock_result;
207
208         while (app_cfg.exit_now == 0) {
209                 for (idx_port = 0; idx_port < app_cfg.cnt_ports; idx_port++) {
210                         /* Check that port is active and unlocked */
211                         ptr_port = &app_cfg.ports[idx_port];
212                         lock_result = rte_spinlock_trylock(&ptr_port->lock);
213                         if (lock_result == 0)
214                                 continue;
215                         if (ptr_port->port_active == 0) {
216                                 rte_spinlock_unlock(&ptr_port->lock);
217                                 continue;
218                         }
219                         txq = &ptr_port->txq;
220
221                         /* MAC address was updated */
222                         if (ptr_port->port_dirty == 1) {
223                                 rte_eth_macaddr_get(ptr_port->idx_port,
224                                         &ptr_port->mac_addr);
225                                 ptr_port->port_dirty = 0;
226                         }
227
228                         /* Incoming frames */
229                         cnt_recv_frames = rte_eth_rx_burst(
230                                 ptr_port->idx_port, 0,
231                                 &txq->buf_frames[txq->cnt_unsent],
232                                 RTE_DIM(txq->buf_frames) - txq->cnt_unsent
233                                 );
234                         if (cnt_recv_frames > 0) {
235                                 for (idx_frame = 0;
236                                         idx_frame < cnt_recv_frames;
237                                         idx_frame++) {
238                                         ptr_frame = txq->buf_frames[
239                                                 idx_frame + txq->cnt_unsent];
240                                         process_frame(ptr_port, ptr_frame);
241                                 }
242                                 txq->cnt_unsent += cnt_recv_frames;
243                         }
244
245                         /* Outgoing frames */
246                         if (txq->cnt_unsent > 0) {
247                                 cnt_sent = rte_eth_tx_burst(
248                                         ptr_port->idx_port, 0,
249                                         txq->buf_frames,
250                                         txq->cnt_unsent
251                                         );
252                                 /* Shuffle up unsent frame pointers */
253                                 for (idx_frame = cnt_sent;
254                                         idx_frame < txq->cnt_unsent;
255                                         idx_frame++)
256                                         txq->buf_frames[idx_frame - cnt_sent] =
257                                                 txq->buf_frames[idx_frame];
258                                 txq->cnt_unsent -= cnt_sent;
259                         }
260                         rte_spinlock_unlock(&ptr_port->lock);
261                 } /* end for( idx_port ) */
262         } /* end for(;;) */
263
264         return 0;
265 }
266
267 int main(int argc, char **argv)
268 {
269         int cnt_args_parsed;
270         uint32_t id_core;
271         uint32_t cnt_ports;
272
273         /* Init runtime environment */
274         cnt_args_parsed = rte_eal_init(argc, argv);
275         if (cnt_args_parsed < 0)
276                 rte_exit(EXIT_FAILURE, "rte_eal_init(): Failed");
277
278         cnt_ports = rte_eth_dev_count();
279         printf("Number of NICs: %i\n", cnt_ports);
280         if (cnt_ports == 0)
281                 rte_exit(EXIT_FAILURE, "No available NIC ports!\n");
282         if (cnt_ports > MAX_PORTS) {
283                 printf("Info: Using only %i of %i ports\n",
284                         cnt_ports, MAX_PORTS
285                         );
286                 cnt_ports = MAX_PORTS;
287         }
288
289         setup_ports(&app_cfg, cnt_ports);
290
291         app_cfg.exit_now = 0;
292         app_cfg.cnt_ports = cnt_ports;
293
294         if (rte_lcore_count() < 2)
295                 rte_exit(EXIT_FAILURE, "No available slave core!\n");
296         /* Assume there is an available slave.. */
297         id_core = rte_lcore_id();
298         id_core = rte_get_next_lcore(id_core, 1, 1);
299         rte_eal_remote_launch(slave_main, NULL, id_core);
300
301         ethapp_main();
302
303         app_cfg.exit_now = 1;
304         RTE_LCORE_FOREACH_SLAVE(id_core) {
305                 if (rte_eal_wait_lcore(id_core) < 0)
306                         return -1;
307         }
308
309         return 0;
310 }