ethdev: replace bus specific struct with generic dev
[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_AUTONEG,
65 };
66 static int is_kni_initialized;
67
68 static uint16_t
69 eth_kni_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
70 {
71         struct pmd_queue *kni_q = q;
72         struct rte_kni *kni = kni_q->internals->kni;
73         uint16_t nb_pkts;
74
75         nb_pkts = rte_kni_rx_burst(kni, bufs, nb_bufs);
76
77         kni_q->rx.pkts += nb_pkts;
78         kni_q->rx.err_pkts += nb_bufs - nb_pkts;
79
80         return nb_pkts;
81 }
82
83 static uint16_t
84 eth_kni_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
85 {
86         struct pmd_queue *kni_q = q;
87         struct rte_kni *kni = kni_q->internals->kni;
88         uint16_t nb_pkts;
89
90         nb_pkts =  rte_kni_tx_burst(kni, bufs, nb_bufs);
91
92         kni_q->tx.pkts += nb_pkts;
93         kni_q->tx.err_pkts += nb_bufs - nb_pkts;
94
95         return nb_pkts;
96 }
97
98 static void *
99 kni_handle_request(void *param)
100 {
101         struct pmd_internals *internals = param;
102 #define MS 1000
103
104         while (!internals->stop_thread) {
105                 rte_kni_handle_request(internals->kni);
106                 usleep(500 * MS);
107         }
108
109         return param;
110 }
111
112 static int
113 eth_kni_start(struct rte_eth_dev *dev)
114 {
115         struct pmd_internals *internals = dev->data->dev_private;
116         uint16_t port_id = dev->data->port_id;
117         struct rte_mempool *mb_pool;
118         struct rte_kni_conf conf;
119         const char *name = dev->device->name + 4; /* remove net_ */
120
121         snprintf(conf.name, RTE_KNI_NAMESIZE, "%s", name);
122         conf.force_bind = 0;
123         conf.group_id = port_id;
124         conf.mbuf_size = MAX_PACKET_SZ;
125         mb_pool = internals->rx_queues[0].mb_pool;
126
127         internals->kni = rte_kni_alloc(mb_pool, &conf, NULL);
128         if (internals->kni == NULL) {
129                 RTE_LOG(ERR, PMD,
130                         "Fail to create kni interface for port: %d\n",
131                         port_id);
132                 return -1;
133         }
134
135         return 0;
136 }
137
138 static int
139 eth_kni_dev_start(struct rte_eth_dev *dev)
140 {
141         struct pmd_internals *internals = dev->data->dev_private;
142         int ret;
143
144         if (internals->is_kni_started == 0) {
145                 ret = eth_kni_start(dev);
146                 if (ret)
147                         return -1;
148                 internals->is_kni_started = 1;
149         }
150
151         if (internals->no_request_thread == 0) {
152                 ret = pthread_create(&internals->thread, NULL,
153                         kni_handle_request, internals);
154                 if (ret) {
155                         RTE_LOG(ERR, PMD,
156                                 "Fail to create kni request thread\n");
157                         return -1;
158                 }
159         }
160
161         dev->data->dev_link.link_status = 1;
162
163         return 0;
164 }
165
166 static void
167 eth_kni_dev_stop(struct rte_eth_dev *dev)
168 {
169         struct pmd_internals *internals = dev->data->dev_private;
170         int ret;
171
172         if (internals->no_request_thread == 0) {
173                 internals->stop_thread = 1;
174
175                 ret = pthread_cancel(internals->thread);
176                 if (ret)
177                         RTE_LOG(ERR, PMD, "Can't cancel the thread\n");
178
179                 ret = pthread_join(internals->thread, NULL);
180                 if (ret)
181                         RTE_LOG(ERR, PMD, "Can't join the thread\n");
182
183                 internals->stop_thread = 0;
184         }
185
186         dev->data->dev_link.link_status = 0;
187 }
188
189 static int
190 eth_kni_dev_configure(struct rte_eth_dev *dev __rte_unused)
191 {
192         return 0;
193 }
194
195 static void
196 eth_kni_dev_info(struct rte_eth_dev *dev __rte_unused,
197                 struct rte_eth_dev_info *dev_info)
198 {
199         dev_info->max_mac_addrs = 1;
200         dev_info->max_rx_pktlen = UINT32_MAX;
201         dev_info->max_rx_queues = KNI_MAX_QUEUE_PER_PORT;
202         dev_info->max_tx_queues = KNI_MAX_QUEUE_PER_PORT;
203         dev_info->min_rx_bufsize = 0;
204 }
205
206 static int
207 eth_kni_rx_queue_setup(struct rte_eth_dev *dev,
208                 uint16_t rx_queue_id,
209                 uint16_t nb_rx_desc __rte_unused,
210                 unsigned int socket_id __rte_unused,
211                 const struct rte_eth_rxconf *rx_conf __rte_unused,
212                 struct rte_mempool *mb_pool)
213 {
214         struct pmd_internals *internals = dev->data->dev_private;
215         struct pmd_queue *q;
216
217         q = &internals->rx_queues[rx_queue_id];
218         q->internals = internals;
219         q->mb_pool = mb_pool;
220
221         dev->data->rx_queues[rx_queue_id] = q;
222
223         return 0;
224 }
225
226 static int
227 eth_kni_tx_queue_setup(struct rte_eth_dev *dev,
228                 uint16_t tx_queue_id,
229                 uint16_t nb_tx_desc __rte_unused,
230                 unsigned int socket_id __rte_unused,
231                 const struct rte_eth_txconf *tx_conf __rte_unused)
232 {
233         struct pmd_internals *internals = dev->data->dev_private;
234         struct pmd_queue *q;
235
236         q = &internals->tx_queues[tx_queue_id];
237         q->internals = internals;
238
239         dev->data->tx_queues[tx_queue_id] = q;
240
241         return 0;
242 }
243
244 static void
245 eth_kni_queue_release(void *q __rte_unused)
246 {
247 }
248
249 static int
250 eth_kni_link_update(struct rte_eth_dev *dev __rte_unused,
251                 int wait_to_complete __rte_unused)
252 {
253         return 0;
254 }
255
256 static int
257 eth_kni_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
258 {
259         unsigned long rx_packets_total = 0, rx_bytes_total = 0;
260         unsigned long tx_packets_total = 0, tx_bytes_total = 0;
261         struct rte_eth_dev_data *data = dev->data;
262         unsigned long tx_packets_err_total = 0;
263         unsigned int i, num_stats;
264         struct pmd_queue *q;
265
266         num_stats = RTE_MIN((unsigned int)RTE_ETHDEV_QUEUE_STAT_CNTRS,
267                         data->nb_rx_queues);
268         for (i = 0; i < num_stats; i++) {
269                 q = data->rx_queues[i];
270                 stats->q_ipackets[i] = q->rx.pkts;
271                 stats->q_ibytes[i] = q->rx.bytes;
272                 rx_packets_total += stats->q_ipackets[i];
273                 rx_bytes_total += stats->q_ibytes[i];
274         }
275
276         num_stats = RTE_MIN((unsigned int)RTE_ETHDEV_QUEUE_STAT_CNTRS,
277                         data->nb_tx_queues);
278         for (i = 0; i < num_stats; i++) {
279                 q = data->tx_queues[i];
280                 stats->q_opackets[i] = q->tx.pkts;
281                 stats->q_obytes[i] = q->tx.bytes;
282                 stats->q_errors[i] = q->tx.err_pkts;
283                 tx_packets_total += stats->q_opackets[i];
284                 tx_bytes_total += stats->q_obytes[i];
285                 tx_packets_err_total += stats->q_errors[i];
286         }
287
288         stats->ipackets = rx_packets_total;
289         stats->ibytes = rx_bytes_total;
290         stats->opackets = tx_packets_total;
291         stats->obytes = tx_bytes_total;
292         stats->oerrors = tx_packets_err_total;
293
294         return 0;
295 }
296
297 static void
298 eth_kni_stats_reset(struct rte_eth_dev *dev)
299 {
300         struct rte_eth_dev_data *data = dev->data;
301         struct pmd_queue *q;
302         unsigned int i;
303
304         for (i = 0; i < data->nb_rx_queues; i++) {
305                 q = data->rx_queues[i];
306                 q->rx.pkts = 0;
307                 q->rx.bytes = 0;
308         }
309         for (i = 0; i < data->nb_tx_queues; i++) {
310                 q = data->tx_queues[i];
311                 q->tx.pkts = 0;
312                 q->tx.bytes = 0;
313                 q->tx.err_pkts = 0;
314         }
315 }
316
317 static const struct eth_dev_ops eth_kni_ops = {
318         .dev_start = eth_kni_dev_start,
319         .dev_stop = eth_kni_dev_stop,
320         .dev_configure = eth_kni_dev_configure,
321         .dev_infos_get = eth_kni_dev_info,
322         .rx_queue_setup = eth_kni_rx_queue_setup,
323         .tx_queue_setup = eth_kni_tx_queue_setup,
324         .rx_queue_release = eth_kni_queue_release,
325         .tx_queue_release = eth_kni_queue_release,
326         .link_update = eth_kni_link_update,
327         .stats_get = eth_kni_stats_get,
328         .stats_reset = eth_kni_stats_reset,
329 };
330
331 static struct rte_eth_dev *
332 eth_kni_create(struct rte_vdev_device *vdev,
333                 struct eth_kni_args *args,
334                 unsigned int numa_node)
335 {
336         struct pmd_internals *internals;
337         struct rte_eth_dev_data *data;
338         struct rte_eth_dev *eth_dev;
339         const char *name;
340
341         RTE_LOG(INFO, PMD, "Creating kni ethdev on numa socket %u\n",
342                         numa_node);
343
344         name = rte_vdev_device_name(vdev);
345         data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
346         if (data == NULL)
347                 return NULL;
348
349         /* reserve an ethdev entry */
350         eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*internals));
351         if (eth_dev == NULL) {
352                 rte_free(data);
353                 return NULL;
354         }
355
356         internals = eth_dev->data->dev_private;
357         rte_memcpy(data, eth_dev->data, sizeof(*data));
358         data->nb_rx_queues = 1;
359         data->nb_tx_queues = 1;
360         data->dev_link = pmd_link;
361         data->mac_addrs = &internals->eth_addr;
362
363         eth_random_addr(internals->eth_addr.addr_bytes);
364
365         eth_dev->data = data;
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         RTE_LOG(INFO, PMD, "Initializing eth_kni for %s\n", name);
415
416         ret = eth_kni_kvargs_process(&args, params);
417         if (ret < 0)
418                 return ret;
419
420         ret = kni_init();
421         if (ret < 0)
422                 return ret;
423
424         eth_dev = eth_kni_create(vdev, &args, rte_socket_id());
425         if (eth_dev == NULL)
426                 goto kni_uninit;
427
428         eth_dev->rx_pkt_burst = eth_kni_rx;
429         eth_dev->tx_pkt_burst = eth_kni_tx;
430
431         return 0;
432
433 kni_uninit:
434         is_kni_initialized--;
435         if (is_kni_initialized == 0)
436                 rte_kni_close();
437         return -1;
438 }
439
440 static int
441 eth_kni_remove(struct rte_vdev_device *vdev)
442 {
443         struct rte_eth_dev *eth_dev;
444         struct pmd_internals *internals;
445         const char *name;
446
447         name = rte_vdev_device_name(vdev);
448         RTE_LOG(INFO, PMD, "Un-Initializing eth_kni for %s\n", name);
449
450         /* find the ethdev entry */
451         eth_dev = rte_eth_dev_allocated(name);
452         if (eth_dev == NULL)
453                 return -1;
454
455         eth_kni_dev_stop(eth_dev);
456
457         internals = eth_dev->data->dev_private;
458         rte_kni_release(internals->kni);
459
460         rte_free(internals);
461         rte_free(eth_dev->data);
462
463         rte_eth_dev_release_port(eth_dev);
464
465         is_kni_initialized--;
466         if (is_kni_initialized == 0)
467                 rte_kni_close();
468
469         return 0;
470 }
471
472 static struct rte_vdev_driver eth_kni_drv = {
473         .probe = eth_kni_probe,
474         .remove = eth_kni_remove,
475 };
476
477 RTE_PMD_REGISTER_VDEV(net_kni, eth_kni_drv);
478 RTE_PMD_REGISTER_PARAM_STRING(net_kni, ETH_KNI_NO_REQUEST_THREAD_ARG "=<int>");