1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
9 #include <rte_string_fns.h>
10 #include <ethdev_driver.h>
11 #include <ethdev_vdev.h>
13 #include <rte_kvargs.h>
14 #include <rte_malloc.h>
15 #include <rte_bus_vdev.h>
17 /* Only single queue supported */
18 #define KNI_MAX_QUEUE_PER_PORT 1
20 #define MAX_KNI_PORTS 8
22 #define KNI_ETHER_MTU(mbuf_size) \
23 ((mbuf_size) - RTE_ETHER_HDR_LEN) /**< Ethernet MTU. */
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,
32 int no_request_thread;
35 struct pmd_queue_stats {
41 struct pmd_internals *internals;
42 struct rte_mempool *mb_pool;
44 struct pmd_queue_stats rx;
45 struct pmd_queue_stats tx;
48 struct pmd_internals {
55 int no_request_thread;
57 struct rte_ether_addr eth_addr;
59 struct pmd_queue rx_queues[KNI_MAX_QUEUE_PER_PORT];
60 struct pmd_queue tx_queues[KNI_MAX_QUEUE_PER_PORT];
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,
69 static int is_kni_initialized;
71 RTE_LOG_REGISTER(eth_kni_logtype, pmd.net.kni, NOTICE);
73 #define PMD_LOG(level, fmt, args...) \
74 rte_log(RTE_LOG_ ## level, eth_kni_logtype, \
75 "%s(): " fmt "\n", __func__, ##args)
77 eth_kni_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
79 struct pmd_queue *kni_q = q;
80 struct rte_kni *kni = kni_q->internals->kni;
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;
88 kni_q->rx.pkts += nb_pkts;
94 eth_kni_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
96 struct pmd_queue *kni_q = q;
97 struct rte_kni *kni = kni_q->internals->kni;
100 nb_pkts = rte_kni_tx_burst(kni, bufs, nb_bufs);
102 kni_q->tx.pkts += nb_pkts;
108 kni_handle_request(void *param)
110 struct pmd_internals *internals = param;
113 while (!internals->stop_thread) {
114 rte_kni_handle_request(internals->kni);
122 eth_kni_start(struct rte_eth_dev *dev)
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_ */
130 mb_pool = internals->rx_queues[0].mb_pool;
131 strlcpy(conf.name, name, RTE_KNI_NAMESIZE);
133 conf.group_id = port_id;
135 rte_pktmbuf_data_room_size(mb_pool) - RTE_PKTMBUF_HEADROOM;
136 conf.mtu = KNI_ETHER_MTU(conf.mbuf_size);
138 internals->kni = rte_kni_alloc(mb_pool, &conf, NULL);
139 if (internals->kni == NULL) {
141 "Fail to create kni interface for port: %d",
150 eth_kni_dev_start(struct rte_eth_dev *dev)
152 struct pmd_internals *internals = dev->data->dev_private;
155 if (internals->is_kni_started == 0) {
156 ret = eth_kni_start(dev);
159 internals->is_kni_started = 1;
162 if (internals->no_request_thread == 0) {
163 internals->stop_thread = 0;
165 ret = rte_ctrl_thread_create(&internals->thread,
166 "kni_handle_req", NULL,
167 kni_handle_request, internals);
170 "Fail to create kni request thread");
175 dev->data->dev_link.link_status = 1;
181 eth_kni_dev_stop(struct rte_eth_dev *dev)
183 struct pmd_internals *internals = dev->data->dev_private;
186 if (internals->no_request_thread == 0 && internals->stop_thread == 0) {
187 internals->stop_thread = 1;
189 ret = pthread_cancel(internals->thread);
191 PMD_LOG(ERR, "Can't cancel the thread");
193 ret = pthread_join(internals->thread, NULL);
195 PMD_LOG(ERR, "Can't join the thread");
198 dev->data->dev_link.link_status = 0;
199 dev->data->dev_started = 0;
205 eth_kni_close(struct rte_eth_dev *eth_dev)
207 struct pmd_internals *internals;
210 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
213 ret = eth_kni_dev_stop(eth_dev);
215 PMD_LOG(WARNING, "Not able to stop kni for %s",
216 eth_dev->data->name);
218 /* mac_addrs must not be freed alone because part of dev_private */
219 eth_dev->data->mac_addrs = NULL;
221 internals = eth_dev->data->dev_private;
222 ret = rte_kni_release(internals->kni);
224 PMD_LOG(WARNING, "Not able to release kni for %s",
225 eth_dev->data->name);
231 eth_kni_dev_configure(struct rte_eth_dev *dev __rte_unused)
237 eth_kni_dev_info(struct rte_eth_dev *dev __rte_unused,
238 struct rte_eth_dev_info *dev_info)
240 dev_info->max_mac_addrs = 1;
241 dev_info->max_rx_pktlen = UINT32_MAX;
242 dev_info->max_rx_queues = KNI_MAX_QUEUE_PER_PORT;
243 dev_info->max_tx_queues = KNI_MAX_QUEUE_PER_PORT;
244 dev_info->min_rx_bufsize = 0;
250 eth_kni_rx_queue_setup(struct rte_eth_dev *dev,
251 uint16_t rx_queue_id,
252 uint16_t nb_rx_desc __rte_unused,
253 unsigned int socket_id __rte_unused,
254 const struct rte_eth_rxconf *rx_conf __rte_unused,
255 struct rte_mempool *mb_pool)
257 struct pmd_internals *internals = dev->data->dev_private;
260 q = &internals->rx_queues[rx_queue_id];
261 q->internals = internals;
262 q->mb_pool = mb_pool;
264 dev->data->rx_queues[rx_queue_id] = q;
270 eth_kni_tx_queue_setup(struct rte_eth_dev *dev,
271 uint16_t tx_queue_id,
272 uint16_t nb_tx_desc __rte_unused,
273 unsigned int socket_id __rte_unused,
274 const struct rte_eth_txconf *tx_conf __rte_unused)
276 struct pmd_internals *internals = dev->data->dev_private;
279 q = &internals->tx_queues[tx_queue_id];
280 q->internals = internals;
282 dev->data->tx_queues[tx_queue_id] = q;
288 eth_kni_queue_release(void *q __rte_unused)
293 eth_kni_link_update(struct rte_eth_dev *dev __rte_unused,
294 int wait_to_complete __rte_unused)
300 eth_kni_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
302 unsigned long rx_packets_total = 0, rx_bytes_total = 0;
303 unsigned long tx_packets_total = 0, tx_bytes_total = 0;
304 struct rte_eth_dev_data *data = dev->data;
305 unsigned int i, num_stats;
308 num_stats = RTE_MIN((unsigned int)RTE_ETHDEV_QUEUE_STAT_CNTRS,
310 for (i = 0; i < num_stats; i++) {
311 q = data->rx_queues[i];
312 stats->q_ipackets[i] = q->rx.pkts;
313 stats->q_ibytes[i] = q->rx.bytes;
314 rx_packets_total += stats->q_ipackets[i];
315 rx_bytes_total += stats->q_ibytes[i];
318 num_stats = RTE_MIN((unsigned int)RTE_ETHDEV_QUEUE_STAT_CNTRS,
320 for (i = 0; i < num_stats; i++) {
321 q = data->tx_queues[i];
322 stats->q_opackets[i] = q->tx.pkts;
323 stats->q_obytes[i] = q->tx.bytes;
324 tx_packets_total += stats->q_opackets[i];
325 tx_bytes_total += stats->q_obytes[i];
328 stats->ipackets = rx_packets_total;
329 stats->ibytes = rx_bytes_total;
330 stats->opackets = tx_packets_total;
331 stats->obytes = tx_bytes_total;
337 eth_kni_stats_reset(struct rte_eth_dev *dev)
339 struct rte_eth_dev_data *data = dev->data;
343 for (i = 0; i < data->nb_rx_queues; i++) {
344 q = data->rx_queues[i];
348 for (i = 0; i < data->nb_tx_queues; i++) {
349 q = data->tx_queues[i];
357 static const struct eth_dev_ops eth_kni_ops = {
358 .dev_start = eth_kni_dev_start,
359 .dev_stop = eth_kni_dev_stop,
360 .dev_close = eth_kni_close,
361 .dev_configure = eth_kni_dev_configure,
362 .dev_infos_get = eth_kni_dev_info,
363 .rx_queue_setup = eth_kni_rx_queue_setup,
364 .tx_queue_setup = eth_kni_tx_queue_setup,
365 .rx_queue_release = eth_kni_queue_release,
366 .tx_queue_release = eth_kni_queue_release,
367 .link_update = eth_kni_link_update,
368 .stats_get = eth_kni_stats_get,
369 .stats_reset = eth_kni_stats_reset,
372 static struct rte_eth_dev *
373 eth_kni_create(struct rte_vdev_device *vdev,
374 struct eth_kni_args *args,
375 unsigned int numa_node)
377 struct pmd_internals *internals;
378 struct rte_eth_dev_data *data;
379 struct rte_eth_dev *eth_dev;
381 PMD_LOG(INFO, "Creating kni ethdev on numa socket %u",
384 /* reserve an ethdev entry */
385 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*internals));
389 internals = eth_dev->data->dev_private;
390 internals->port_id = eth_dev->data->port_id;
391 data = eth_dev->data;
392 data->nb_rx_queues = 1;
393 data->nb_tx_queues = 1;
394 data->dev_link = pmd_link;
395 data->mac_addrs = &internals->eth_addr;
396 data->promiscuous = 1;
397 data->all_multicast = 1;
398 data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
400 rte_eth_random_addr(internals->eth_addr.addr_bytes);
402 eth_dev->dev_ops = ð_kni_ops;
404 internals->no_request_thread = args->no_request_thread;
414 if (is_kni_initialized == 0) {
415 ret = rte_kni_init(MAX_KNI_PORTS);
420 is_kni_initialized++;
426 eth_kni_kvargs_process(struct eth_kni_args *args, const char *params)
428 struct rte_kvargs *kvlist;
430 kvlist = rte_kvargs_parse(params, valid_arguments);
434 memset(args, 0, sizeof(struct eth_kni_args));
436 if (rte_kvargs_count(kvlist, ETH_KNI_NO_REQUEST_THREAD_ARG) == 1)
437 args->no_request_thread = 1;
439 rte_kvargs_free(kvlist);
445 eth_kni_probe(struct rte_vdev_device *vdev)
447 struct rte_eth_dev *eth_dev;
448 struct eth_kni_args args;
453 name = rte_vdev_device_name(vdev);
454 params = rte_vdev_device_args(vdev);
455 PMD_LOG(INFO, "Initializing eth_kni for %s", name);
457 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
458 eth_dev = rte_eth_dev_attach_secondary(name);
460 PMD_LOG(ERR, "Failed to probe %s", name);
463 /* TODO: request info from primary to set up Rx and Tx */
464 eth_dev->dev_ops = ð_kni_ops;
465 eth_dev->device = &vdev->device;
466 rte_eth_dev_probing_finish(eth_dev);
470 ret = eth_kni_kvargs_process(&args, params);
478 eth_dev = eth_kni_create(vdev, &args, rte_socket_id());
482 eth_dev->rx_pkt_burst = eth_kni_rx;
483 eth_dev->tx_pkt_burst = eth_kni_tx;
485 rte_eth_dev_probing_finish(eth_dev);
489 is_kni_initialized--;
490 if (is_kni_initialized == 0)
496 eth_kni_remove(struct rte_vdev_device *vdev)
498 struct rte_eth_dev *eth_dev;
502 name = rte_vdev_device_name(vdev);
503 PMD_LOG(INFO, "Un-Initializing eth_kni for %s", name);
505 /* find the ethdev entry */
506 eth_dev = rte_eth_dev_allocated(name);
507 if (eth_dev != NULL) {
508 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
509 ret = eth_kni_dev_stop(eth_dev);
512 return rte_eth_dev_release_port(eth_dev);
514 eth_kni_close(eth_dev);
515 rte_eth_dev_release_port(eth_dev);
518 is_kni_initialized--;
519 if (is_kni_initialized == 0)
525 static struct rte_vdev_driver eth_kni_drv = {
526 .probe = eth_kni_probe,
527 .remove = eth_kni_remove,
530 RTE_PMD_REGISTER_VDEV(net_kni, eth_kni_drv);
531 RTE_PMD_REGISTER_PARAM_STRING(net_kni, ETH_KNI_NO_REQUEST_THREAD_ARG "=<int>");