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