New upstream version 17.11.1
[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_bus_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_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_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         internals->no_request_thread = args->no_request_thread;
399
400         return eth_dev;
401 }
402
403 static int
404 kni_init(void)
405 {
406         if (is_kni_initialized == 0)
407                 rte_kni_init(MAX_KNI_PORTS);
408
409         is_kni_initialized++;
410
411         return 0;
412 }
413
414 static int
415 eth_kni_kvargs_process(struct eth_kni_args *args, const char *params)
416 {
417         struct rte_kvargs *kvlist;
418
419         kvlist = rte_kvargs_parse(params, valid_arguments);
420         if (kvlist == NULL)
421                 return -1;
422
423         memset(args, 0, sizeof(struct eth_kni_args));
424
425         if (rte_kvargs_count(kvlist, ETH_KNI_NO_REQUEST_THREAD_ARG) == 1)
426                 args->no_request_thread = 1;
427
428         rte_kvargs_free(kvlist);
429
430         return 0;
431 }
432
433 static int
434 eth_kni_probe(struct rte_vdev_device *vdev)
435 {
436         struct rte_eth_dev *eth_dev;
437         struct eth_kni_args args;
438         const char *name;
439         const char *params;
440         int ret;
441
442         name = rte_vdev_device_name(vdev);
443         params = rte_vdev_device_args(vdev);
444         RTE_LOG(INFO, PMD, "Initializing eth_kni for %s\n", name);
445
446         ret = eth_kni_kvargs_process(&args, params);
447         if (ret < 0)
448                 return ret;
449
450         ret = kni_init();
451         if (ret < 0)
452                 return ret;
453
454         eth_dev = eth_kni_create(vdev, &args, rte_socket_id());
455         if (eth_dev == NULL)
456                 goto kni_uninit;
457
458         eth_dev->rx_pkt_burst = eth_kni_rx;
459         eth_dev->tx_pkt_burst = eth_kni_tx;
460
461         return 0;
462
463 kni_uninit:
464         is_kni_initialized--;
465         if (is_kni_initialized == 0)
466                 rte_kni_close();
467         return -1;
468 }
469
470 static int
471 eth_kni_remove(struct rte_vdev_device *vdev)
472 {
473         struct rte_eth_dev *eth_dev;
474         struct pmd_internals *internals;
475         const char *name;
476
477         name = rte_vdev_device_name(vdev);
478         RTE_LOG(INFO, PMD, "Un-Initializing eth_kni for %s\n", name);
479
480         /* find the ethdev entry */
481         eth_dev = rte_eth_dev_allocated(name);
482         if (eth_dev == NULL)
483                 return -1;
484
485         eth_kni_dev_stop(eth_dev);
486
487         internals = eth_dev->data->dev_private;
488         rte_kni_release(internals->kni);
489
490         rte_free(internals);
491         rte_free(eth_dev->data);
492
493         rte_eth_dev_release_port(eth_dev);
494
495         is_kni_initialized--;
496         if (is_kni_initialized == 0)
497                 rte_kni_close();
498
499         return 0;
500 }
501
502 static struct rte_vdev_driver eth_kni_drv = {
503         .probe = eth_kni_probe,
504         .remove = eth_kni_remove,
505 };
506
507 RTE_PMD_REGISTER_VDEV(net_kni, eth_kni_drv);
508 RTE_PMD_REGISTER_PARAM_STRING(net_kni, ETH_KNI_NO_REQUEST_THREAD_ARG "=<int>");