ethdev: add return value to stats get dev op
[dpdk.git] / drivers / net / kni / rte_eth_kni.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 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 #include <fcntl.h>
35 #include <pthread.h>
36 #include <unistd.h>
37
38 #include <rte_ethdev.h>
39 #include <rte_ethdev_vdev.h>
40 #include <rte_kni.h>
41 #include <rte_kvargs.h>
42 #include <rte_malloc.h>
43 #include <rte_vdev.h>
44
45 /* Only single queue supported */
46 #define KNI_MAX_QUEUE_PER_PORT 1
47
48 #define MAX_PACKET_SZ 2048
49 #define MAX_KNI_PORTS 8
50
51 #define ETH_KNI_NO_REQUEST_THREAD_ARG   "no_request_thread"
52 static const char * const valid_arguments[] = {
53         ETH_KNI_NO_REQUEST_THREAD_ARG,
54         NULL
55 };
56
57 struct eth_kni_args {
58         int no_request_thread;
59 };
60
61 struct pmd_queue_stats {
62         uint64_t pkts;
63         uint64_t bytes;
64         uint64_t err_pkts;
65 };
66
67 struct pmd_queue {
68         struct pmd_internals *internals;
69         struct rte_mempool *mb_pool;
70
71         struct pmd_queue_stats rx;
72         struct pmd_queue_stats tx;
73 };
74
75 struct pmd_internals {
76         struct rte_kni *kni;
77         int is_kni_started;
78
79         pthread_t thread;
80         int stop_thread;
81         int no_request_thread;
82
83         struct ether_addr eth_addr;
84
85         struct pmd_queue rx_queues[KNI_MAX_QUEUE_PER_PORT];
86         struct pmd_queue tx_queues[KNI_MAX_QUEUE_PER_PORT];
87 };
88
89 static const struct rte_eth_link pmd_link = {
90                 .link_speed = ETH_SPEED_NUM_10G,
91                 .link_duplex = ETH_LINK_FULL_DUPLEX,
92                 .link_status = ETH_LINK_DOWN,
93                 .link_autoneg = ETH_LINK_SPEED_AUTONEG,
94 };
95 static int is_kni_initialized;
96
97 static uint16_t
98 eth_kni_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
99 {
100         struct pmd_queue *kni_q = q;
101         struct rte_kni *kni = kni_q->internals->kni;
102         uint16_t nb_pkts;
103
104         nb_pkts = rte_kni_rx_burst(kni, bufs, nb_bufs);
105
106         kni_q->rx.pkts += nb_pkts;
107         kni_q->rx.err_pkts += nb_bufs - nb_pkts;
108
109         return nb_pkts;
110 }
111
112 static uint16_t
113 eth_kni_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
114 {
115         struct pmd_queue *kni_q = q;
116         struct rte_kni *kni = kni_q->internals->kni;
117         uint16_t nb_pkts;
118
119         nb_pkts =  rte_kni_tx_burst(kni, bufs, nb_bufs);
120
121         kni_q->tx.pkts += nb_pkts;
122         kni_q->tx.err_pkts += nb_bufs - nb_pkts;
123
124         return nb_pkts;
125 }
126
127 static void *
128 kni_handle_request(void *param)
129 {
130         struct pmd_internals *internals = param;
131 #define MS 1000
132
133         while (!internals->stop_thread) {
134                 rte_kni_handle_request(internals->kni);
135                 usleep(500 * MS);
136         }
137
138         return param;
139 }
140
141 static int
142 eth_kni_start(struct rte_eth_dev *dev)
143 {
144         struct pmd_internals *internals = dev->data->dev_private;
145         uint16_t port_id = dev->data->port_id;
146         struct rte_mempool *mb_pool;
147         struct rte_kni_conf conf;
148         const char *name = dev->device->name + 4; /* remove net_ */
149
150         snprintf(conf.name, RTE_KNI_NAMESIZE, "%s", name);
151         conf.force_bind = 0;
152         conf.group_id = port_id;
153         conf.mbuf_size = MAX_PACKET_SZ;
154         mb_pool = internals->rx_queues[0].mb_pool;
155
156         internals->kni = rte_kni_alloc(mb_pool, &conf, NULL);
157         if (internals->kni == NULL) {
158                 RTE_LOG(ERR, PMD,
159                         "Fail to create kni interface for port: %d\n",
160                         port_id);
161                 return -1;
162         }
163
164         return 0;
165 }
166
167 static int
168 eth_kni_dev_start(struct rte_eth_dev *dev)
169 {
170         struct pmd_internals *internals = dev->data->dev_private;
171         int ret;
172
173         if (internals->is_kni_started == 0) {
174                 ret = eth_kni_start(dev);
175                 if (ret)
176                         return -1;
177                 internals->is_kni_started = 1;
178         }
179
180         if (internals->no_request_thread == 0) {
181                 ret = pthread_create(&internals->thread, NULL,
182                         kni_handle_request, internals);
183                 if (ret) {
184                         RTE_LOG(ERR, PMD,
185                                 "Fail to create kni request thread\n");
186                         return -1;
187                 }
188         }
189
190         dev->data->dev_link.link_status = 1;
191
192         return 0;
193 }
194
195 static void
196 eth_kni_dev_stop(struct rte_eth_dev *dev)
197 {
198         struct pmd_internals *internals = dev->data->dev_private;
199         int ret;
200
201         if (internals->no_request_thread == 0) {
202                 internals->stop_thread = 1;
203
204                 ret = pthread_cancel(internals->thread);
205                 if (ret)
206                         RTE_LOG(ERR, PMD, "Can't cancel the thread\n");
207
208                 ret = pthread_join(internals->thread, NULL);
209                 if (ret)
210                         RTE_LOG(ERR, PMD, "Can't join the thread\n");
211
212                 internals->stop_thread = 0;
213         }
214
215         dev->data->dev_link.link_status = 0;
216 }
217
218 static int
219 eth_kni_dev_configure(struct rte_eth_dev *dev __rte_unused)
220 {
221         return 0;
222 }
223
224 static void
225 eth_kni_dev_info(struct rte_eth_dev *dev __rte_unused,
226                 struct rte_eth_dev_info *dev_info)
227 {
228         dev_info->max_mac_addrs = 1;
229         dev_info->max_rx_pktlen = UINT32_MAX;
230         dev_info->max_rx_queues = KNI_MAX_QUEUE_PER_PORT;
231         dev_info->max_tx_queues = KNI_MAX_QUEUE_PER_PORT;
232         dev_info->min_rx_bufsize = 0;
233         dev_info->pci_dev = NULL;
234 }
235
236 static int
237 eth_kni_rx_queue_setup(struct rte_eth_dev *dev,
238                 uint16_t rx_queue_id,
239                 uint16_t nb_rx_desc __rte_unused,
240                 unsigned int socket_id __rte_unused,
241                 const struct rte_eth_rxconf *rx_conf __rte_unused,
242                 struct rte_mempool *mb_pool)
243 {
244         struct pmd_internals *internals = dev->data->dev_private;
245         struct pmd_queue *q;
246
247         q = &internals->rx_queues[rx_queue_id];
248         q->internals = internals;
249         q->mb_pool = mb_pool;
250
251         dev->data->rx_queues[rx_queue_id] = q;
252
253         return 0;
254 }
255
256 static int
257 eth_kni_tx_queue_setup(struct rte_eth_dev *dev,
258                 uint16_t tx_queue_id,
259                 uint16_t nb_tx_desc __rte_unused,
260                 unsigned int socket_id __rte_unused,
261                 const struct rte_eth_txconf *tx_conf __rte_unused)
262 {
263         struct pmd_internals *internals = dev->data->dev_private;
264         struct pmd_queue *q;
265
266         q = &internals->tx_queues[tx_queue_id];
267         q->internals = internals;
268
269         dev->data->tx_queues[tx_queue_id] = q;
270
271         return 0;
272 }
273
274 static void
275 eth_kni_queue_release(void *q __rte_unused)
276 {
277 }
278
279 static int
280 eth_kni_link_update(struct rte_eth_dev *dev __rte_unused,
281                 int wait_to_complete __rte_unused)
282 {
283         return 0;
284 }
285
286 static int
287 eth_kni_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
288 {
289         unsigned long rx_packets_total = 0, rx_bytes_total = 0;
290         unsigned long tx_packets_total = 0, tx_bytes_total = 0;
291         struct rte_eth_dev_data *data = dev->data;
292         unsigned long tx_packets_err_total = 0;
293         unsigned int i, num_stats;
294         struct pmd_queue *q;
295
296         num_stats = RTE_MIN((unsigned int)RTE_ETHDEV_QUEUE_STAT_CNTRS,
297                         data->nb_rx_queues);
298         for (i = 0; i < num_stats; i++) {
299                 q = data->rx_queues[i];
300                 stats->q_ipackets[i] = q->rx.pkts;
301                 stats->q_ibytes[i] = q->rx.bytes;
302                 rx_packets_total += stats->q_ipackets[i];
303                 rx_bytes_total += stats->q_ibytes[i];
304         }
305
306         num_stats = RTE_MIN((unsigned int)RTE_ETHDEV_QUEUE_STAT_CNTRS,
307                         data->nb_tx_queues);
308         for (i = 0; i < num_stats; i++) {
309                 q = data->tx_queues[i];
310                 stats->q_opackets[i] = q->tx.pkts;
311                 stats->q_obytes[i] = q->tx.bytes;
312                 stats->q_errors[i] = q->tx.err_pkts;
313                 tx_packets_total += stats->q_opackets[i];
314                 tx_bytes_total += stats->q_obytes[i];
315                 tx_packets_err_total += stats->q_errors[i];
316         }
317
318         stats->ipackets = rx_packets_total;
319         stats->ibytes = rx_bytes_total;
320         stats->opackets = tx_packets_total;
321         stats->obytes = tx_bytes_total;
322         stats->oerrors = tx_packets_err_total;
323
324         return 0;
325 }
326
327 static void
328 eth_kni_stats_reset(struct rte_eth_dev *dev)
329 {
330         struct rte_eth_dev_data *data = dev->data;
331         struct pmd_queue *q;
332         unsigned int i;
333
334         for (i = 0; i < data->nb_rx_queues; i++) {
335                 q = data->rx_queues[i];
336                 q->rx.pkts = 0;
337                 q->rx.bytes = 0;
338         }
339         for (i = 0; i < data->nb_tx_queues; i++) {
340                 q = data->tx_queues[i];
341                 q->tx.pkts = 0;
342                 q->tx.bytes = 0;
343                 q->tx.err_pkts = 0;
344         }
345 }
346
347 static const struct eth_dev_ops eth_kni_ops = {
348         .dev_start = eth_kni_dev_start,
349         .dev_stop = eth_kni_dev_stop,
350         .dev_configure = eth_kni_dev_configure,
351         .dev_infos_get = eth_kni_dev_info,
352         .rx_queue_setup = eth_kni_rx_queue_setup,
353         .tx_queue_setup = eth_kni_tx_queue_setup,
354         .rx_queue_release = eth_kni_queue_release,
355         .tx_queue_release = eth_kni_queue_release,
356         .link_update = eth_kni_link_update,
357         .stats_get = eth_kni_stats_get,
358         .stats_reset = eth_kni_stats_reset,
359 };
360
361 static struct rte_vdev_driver eth_kni_drv;
362
363 static struct rte_eth_dev *
364 eth_kni_create(struct rte_vdev_device *vdev,
365                 struct eth_kni_args *args,
366                 unsigned int numa_node)
367 {
368         struct pmd_internals *internals;
369         struct rte_eth_dev_data *data;
370         struct rte_eth_dev *eth_dev;
371         const char *name;
372
373         RTE_LOG(INFO, PMD, "Creating kni ethdev on numa socket %u\n",
374                         numa_node);
375
376         name = rte_vdev_device_name(vdev);
377         data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
378         if (data == NULL)
379                 return NULL;
380
381         /* reserve an ethdev entry */
382         eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*internals));
383         if (eth_dev == NULL) {
384                 rte_free(data);
385                 return NULL;
386         }
387
388         internals = eth_dev->data->dev_private;
389         rte_memcpy(data, eth_dev->data, sizeof(*data));
390         data->nb_rx_queues = 1;
391         data->nb_tx_queues = 1;
392         data->dev_link = pmd_link;
393         data->mac_addrs = &internals->eth_addr;
394
395         eth_random_addr(internals->eth_addr.addr_bytes);
396
397         eth_dev->data = data;
398         eth_dev->dev_ops = &eth_kni_ops;
399
400         data->dev_flags = RTE_ETH_DEV_DETACHABLE;
401
402         internals->no_request_thread = args->no_request_thread;
403
404         return eth_dev;
405 }
406
407 static int
408 kni_init(void)
409 {
410         if (is_kni_initialized == 0)
411                 rte_kni_init(MAX_KNI_PORTS);
412
413         is_kni_initialized++;
414
415         return 0;
416 }
417
418 static int
419 eth_kni_kvargs_process(struct eth_kni_args *args, const char *params)
420 {
421         struct rte_kvargs *kvlist;
422
423         kvlist = rte_kvargs_parse(params, valid_arguments);
424         if (kvlist == NULL)
425                 return -1;
426
427         memset(args, 0, sizeof(struct eth_kni_args));
428
429         if (rte_kvargs_count(kvlist, ETH_KNI_NO_REQUEST_THREAD_ARG) == 1)
430                 args->no_request_thread = 1;
431
432         rte_kvargs_free(kvlist);
433
434         return 0;
435 }
436
437 static int
438 eth_kni_probe(struct rte_vdev_device *vdev)
439 {
440         struct rte_eth_dev *eth_dev;
441         struct eth_kni_args args;
442         const char *name;
443         const char *params;
444         int ret;
445
446         name = rte_vdev_device_name(vdev);
447         params = rte_vdev_device_args(vdev);
448         RTE_LOG(INFO, PMD, "Initializing eth_kni for %s\n", name);
449
450         ret = eth_kni_kvargs_process(&args, params);
451         if (ret < 0)
452                 return ret;
453
454         ret = kni_init();
455         if (ret < 0)
456                 return ret;
457
458         eth_dev = eth_kni_create(vdev, &args, rte_socket_id());
459         if (eth_dev == NULL)
460                 goto kni_uninit;
461
462         eth_dev->rx_pkt_burst = eth_kni_rx;
463         eth_dev->tx_pkt_burst = eth_kni_tx;
464
465         return 0;
466
467 kni_uninit:
468         is_kni_initialized--;
469         if (is_kni_initialized == 0)
470                 rte_kni_close();
471         return -1;
472 }
473
474 static int
475 eth_kni_remove(struct rte_vdev_device *vdev)
476 {
477         struct rte_eth_dev *eth_dev;
478         struct pmd_internals *internals;
479         const char *name;
480
481         name = rte_vdev_device_name(vdev);
482         RTE_LOG(INFO, PMD, "Un-Initializing eth_kni for %s\n", name);
483
484         /* find the ethdev entry */
485         eth_dev = rte_eth_dev_allocated(name);
486         if (eth_dev == NULL)
487                 return -1;
488
489         eth_kni_dev_stop(eth_dev);
490
491         internals = eth_dev->data->dev_private;
492         rte_kni_release(internals->kni);
493
494         rte_free(internals);
495         rte_free(eth_dev->data);
496
497         rte_eth_dev_release_port(eth_dev);
498
499         is_kni_initialized--;
500         if (is_kni_initialized == 0)
501                 rte_kni_close();
502
503         return 0;
504 }
505
506 static struct rte_vdev_driver eth_kni_drv = {
507         .probe = eth_kni_probe,
508         .remove = eth_kni_remove,
509 };
510
511 RTE_PMD_REGISTER_VDEV(net_kni, eth_kni_drv);
512 RTE_PMD_REGISTER_PARAM_STRING(net_kni, ETH_KNI_NO_REQUEST_THREAD_ARG "=<int>");