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