72a2733ba59c52b4fc9f522871987d5b05790f0d
[deb_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 void
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
325 static void
326 eth_kni_stats_reset(struct rte_eth_dev *dev)
327 {
328         struct rte_eth_dev_data *data = dev->data;
329         struct pmd_queue *q;
330         unsigned int i;
331
332         for (i = 0; i < data->nb_rx_queues; i++) {
333                 q = data->rx_queues[i];
334                 q->rx.pkts = 0;
335                 q->rx.bytes = 0;
336         }
337         for (i = 0; i < data->nb_tx_queues; i++) {
338                 q = data->tx_queues[i];
339                 q->tx.pkts = 0;
340                 q->tx.bytes = 0;
341                 q->tx.err_pkts = 0;
342         }
343 }
344
345 static const struct eth_dev_ops eth_kni_ops = {
346         .dev_start = eth_kni_dev_start,
347         .dev_stop = eth_kni_dev_stop,
348         .dev_configure = eth_kni_dev_configure,
349         .dev_infos_get = eth_kni_dev_info,
350         .rx_queue_setup = eth_kni_rx_queue_setup,
351         .tx_queue_setup = eth_kni_tx_queue_setup,
352         .rx_queue_release = eth_kni_queue_release,
353         .tx_queue_release = eth_kni_queue_release,
354         .link_update = eth_kni_link_update,
355         .stats_get = eth_kni_stats_get,
356         .stats_reset = eth_kni_stats_reset,
357 };
358
359 static struct rte_vdev_driver eth_kni_drv;
360
361 static struct rte_eth_dev *
362 eth_kni_create(struct rte_vdev_device *vdev,
363                 struct eth_kni_args *args,
364                 unsigned int numa_node)
365 {
366         struct pmd_internals *internals;
367         struct rte_eth_dev_data *data;
368         struct rte_eth_dev *eth_dev;
369         const char *name;
370
371         RTE_LOG(INFO, PMD, "Creating kni ethdev on numa socket %u\n",
372                         numa_node);
373
374         name = rte_vdev_device_name(vdev);
375         data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
376         if (data == NULL)
377                 return NULL;
378
379         /* reserve an ethdev entry */
380         eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*internals));
381         if (eth_dev == NULL) {
382                 rte_free(data);
383                 return NULL;
384         }
385
386         internals = eth_dev->data->dev_private;
387         rte_memcpy(data, eth_dev->data, sizeof(*data));
388         data->nb_rx_queues = 1;
389         data->nb_tx_queues = 1;
390         data->dev_link = pmd_link;
391         data->mac_addrs = &internals->eth_addr;
392
393         eth_random_addr(internals->eth_addr.addr_bytes);
394
395         eth_dev->data = data;
396         eth_dev->dev_ops = &eth_kni_ops;
397
398         data->dev_flags = RTE_ETH_DEV_DETACHABLE;
399
400         internals->no_request_thread = args->no_request_thread;
401
402         return eth_dev;
403 }
404
405 static int
406 kni_init(void)
407 {
408         if (is_kni_initialized == 0)
409                 rte_kni_init(MAX_KNI_PORTS);
410
411         is_kni_initialized++;
412
413         return 0;
414 }
415
416 static int
417 eth_kni_kvargs_process(struct eth_kni_args *args, const char *params)
418 {
419         struct rte_kvargs *kvlist;
420
421         kvlist = rte_kvargs_parse(params, valid_arguments);
422         if (kvlist == NULL)
423                 return -1;
424
425         memset(args, 0, sizeof(struct eth_kni_args));
426
427         if (rte_kvargs_count(kvlist, ETH_KNI_NO_REQUEST_THREAD_ARG) == 1)
428                 args->no_request_thread = 1;
429
430         rte_kvargs_free(kvlist);
431
432         return 0;
433 }
434
435 static int
436 eth_kni_probe(struct rte_vdev_device *vdev)
437 {
438         struct rte_eth_dev *eth_dev;
439         struct eth_kni_args args;
440         const char *name;
441         const char *params;
442         int ret;
443
444         name = rte_vdev_device_name(vdev);
445         params = rte_vdev_device_args(vdev);
446         RTE_LOG(INFO, PMD, "Initializing eth_kni for %s\n", name);
447
448         ret = eth_kni_kvargs_process(&args, params);
449         if (ret < 0)
450                 return ret;
451
452         ret = kni_init();
453         if (ret < 0)
454                 return ret;
455
456         eth_dev = eth_kni_create(vdev, &args, rte_socket_id());
457         if (eth_dev == NULL)
458                 goto kni_uninit;
459
460         eth_dev->rx_pkt_burst = eth_kni_rx;
461         eth_dev->tx_pkt_burst = eth_kni_tx;
462
463         return 0;
464
465 kni_uninit:
466         is_kni_initialized--;
467         if (is_kni_initialized == 0)
468                 rte_kni_close();
469         return -1;
470 }
471
472 static int
473 eth_kni_remove(struct rte_vdev_device *vdev)
474 {
475         struct rte_eth_dev *eth_dev;
476         struct pmd_internals *internals;
477         const char *name;
478
479         name = rte_vdev_device_name(vdev);
480         RTE_LOG(INFO, PMD, "Un-Initializing eth_kni for %s\n", name);
481
482         /* find the ethdev entry */
483         eth_dev = rte_eth_dev_allocated(name);
484         if (eth_dev == NULL)
485                 return -1;
486
487         eth_kni_dev_stop(eth_dev);
488
489         internals = eth_dev->data->dev_private;
490         rte_kni_release(internals->kni);
491
492         rte_free(internals);
493         rte_free(eth_dev->data);
494
495         rte_eth_dev_release_port(eth_dev);
496
497         is_kni_initialized--;
498         if (is_kni_initialized == 0)
499                 rte_kni_close();
500
501         return 0;
502 }
503
504 static struct rte_vdev_driver eth_kni_drv = {
505         .probe = eth_kni_probe,
506         .remove = eth_kni_remove,
507 };
508
509 RTE_PMD_REGISTER_VDEV(net_kni, eth_kni_drv);
510 RTE_PMD_REGISTER_PARAM_STRING(net_kni, ETH_KNI_NO_REQUEST_THREAD_ARG "=<int>");