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