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