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