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