1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
9 #include <rte_ethdev_driver.h>
10 #include <rte_ethdev_vdev.h>
12 #include <rte_kvargs.h>
13 #include <rte_malloc.h>
14 #include <rte_bus_vdev.h>
16 /* Only single queue supported */
17 #define KNI_MAX_QUEUE_PER_PORT 1
19 #define MAX_PACKET_SZ 2048
20 #define MAX_KNI_PORTS 8
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,
29 int no_request_thread;
32 struct pmd_queue_stats {
39 struct pmd_internals *internals;
40 struct rte_mempool *mb_pool;
42 struct pmd_queue_stats rx;
43 struct pmd_queue_stats tx;
46 struct pmd_internals {
52 int no_request_thread;
54 struct ether_addr eth_addr;
56 struct pmd_queue rx_queues[KNI_MAX_QUEUE_PER_PORT];
57 struct pmd_queue tx_queues[KNI_MAX_QUEUE_PER_PORT];
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_FIXED,
66 static int is_kni_initialized;
68 static int eth_kni_logtype;
70 #define PMD_LOG(level, fmt, args...) \
71 rte_log(RTE_LOG_ ## level, eth_kni_logtype, \
72 "%s(): " fmt "\n", __func__, ##args)
74 eth_kni_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
76 struct pmd_queue *kni_q = q;
77 struct rte_kni *kni = kni_q->internals->kni;
80 nb_pkts = rte_kni_rx_burst(kni, bufs, nb_bufs);
82 kni_q->rx.pkts += nb_pkts;
83 kni_q->rx.err_pkts += nb_bufs - nb_pkts;
89 eth_kni_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
91 struct pmd_queue *kni_q = q;
92 struct rte_kni *kni = kni_q->internals->kni;
95 nb_pkts = rte_kni_tx_burst(kni, bufs, nb_bufs);
97 kni_q->tx.pkts += nb_pkts;
98 kni_q->tx.err_pkts += nb_bufs - nb_pkts;
104 kni_handle_request(void *param)
106 struct pmd_internals *internals = param;
109 while (!internals->stop_thread) {
110 rte_kni_handle_request(internals->kni);
118 eth_kni_start(struct rte_eth_dev *dev)
120 struct pmd_internals *internals = dev->data->dev_private;
121 uint16_t port_id = dev->data->port_id;
122 struct rte_mempool *mb_pool;
123 struct rte_kni_conf conf;
124 const char *name = dev->device->name + 4; /* remove net_ */
126 snprintf(conf.name, RTE_KNI_NAMESIZE, "%s", name);
128 conf.group_id = port_id;
129 conf.mbuf_size = MAX_PACKET_SZ;
130 mb_pool = internals->rx_queues[0].mb_pool;
132 internals->kni = rte_kni_alloc(mb_pool, &conf, NULL);
133 if (internals->kni == NULL) {
135 "Fail to create kni interface for port: %d",
144 eth_kni_dev_start(struct rte_eth_dev *dev)
146 struct pmd_internals *internals = dev->data->dev_private;
149 if (internals->is_kni_started == 0) {
150 ret = eth_kni_start(dev);
153 internals->is_kni_started = 1;
156 if (internals->no_request_thread == 0) {
157 ret = rte_ctrl_thread_create(&internals->thread,
158 "kni_handle_req", NULL,
159 kni_handle_request, internals);
162 "Fail to create kni request thread");
167 dev->data->dev_link.link_status = 1;
173 eth_kni_dev_stop(struct rte_eth_dev *dev)
175 struct pmd_internals *internals = dev->data->dev_private;
178 if (internals->no_request_thread == 0) {
179 internals->stop_thread = 1;
181 ret = pthread_cancel(internals->thread);
183 PMD_LOG(ERR, "Can't cancel the thread");
185 ret = pthread_join(internals->thread, NULL);
187 PMD_LOG(ERR, "Can't join the thread");
189 internals->stop_thread = 0;
192 dev->data->dev_link.link_status = 0;
196 eth_kni_dev_configure(struct rte_eth_dev *dev __rte_unused)
202 eth_kni_dev_info(struct rte_eth_dev *dev __rte_unused,
203 struct rte_eth_dev_info *dev_info)
205 dev_info->max_mac_addrs = 1;
206 dev_info->max_rx_pktlen = UINT32_MAX;
207 dev_info->max_rx_queues = KNI_MAX_QUEUE_PER_PORT;
208 dev_info->max_tx_queues = KNI_MAX_QUEUE_PER_PORT;
209 dev_info->min_rx_bufsize = 0;
213 eth_kni_rx_queue_setup(struct rte_eth_dev *dev,
214 uint16_t rx_queue_id,
215 uint16_t nb_rx_desc __rte_unused,
216 unsigned int socket_id __rte_unused,
217 const struct rte_eth_rxconf *rx_conf __rte_unused,
218 struct rte_mempool *mb_pool)
220 struct pmd_internals *internals = dev->data->dev_private;
223 q = &internals->rx_queues[rx_queue_id];
224 q->internals = internals;
225 q->mb_pool = mb_pool;
227 dev->data->rx_queues[rx_queue_id] = q;
233 eth_kni_tx_queue_setup(struct rte_eth_dev *dev,
234 uint16_t tx_queue_id,
235 uint16_t nb_tx_desc __rte_unused,
236 unsigned int socket_id __rte_unused,
237 const struct rte_eth_txconf *tx_conf __rte_unused)
239 struct pmd_internals *internals = dev->data->dev_private;
242 q = &internals->tx_queues[tx_queue_id];
243 q->internals = internals;
245 dev->data->tx_queues[tx_queue_id] = q;
251 eth_kni_queue_release(void *q __rte_unused)
256 eth_kni_link_update(struct rte_eth_dev *dev __rte_unused,
257 int wait_to_complete __rte_unused)
263 eth_kni_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
265 unsigned long rx_packets_total = 0, rx_bytes_total = 0;
266 unsigned long tx_packets_total = 0, tx_bytes_total = 0;
267 struct rte_eth_dev_data *data = dev->data;
268 unsigned long tx_packets_err_total = 0;
269 unsigned int i, num_stats;
272 num_stats = RTE_MIN((unsigned int)RTE_ETHDEV_QUEUE_STAT_CNTRS,
274 for (i = 0; i < num_stats; i++) {
275 q = data->rx_queues[i];
276 stats->q_ipackets[i] = q->rx.pkts;
277 stats->q_ibytes[i] = q->rx.bytes;
278 rx_packets_total += stats->q_ipackets[i];
279 rx_bytes_total += stats->q_ibytes[i];
282 num_stats = RTE_MIN((unsigned int)RTE_ETHDEV_QUEUE_STAT_CNTRS,
284 for (i = 0; i < num_stats; i++) {
285 q = data->tx_queues[i];
286 stats->q_opackets[i] = q->tx.pkts;
287 stats->q_obytes[i] = q->tx.bytes;
288 stats->q_errors[i] = q->tx.err_pkts;
289 tx_packets_total += stats->q_opackets[i];
290 tx_bytes_total += stats->q_obytes[i];
291 tx_packets_err_total += stats->q_errors[i];
294 stats->ipackets = rx_packets_total;
295 stats->ibytes = rx_bytes_total;
296 stats->opackets = tx_packets_total;
297 stats->obytes = tx_bytes_total;
298 stats->oerrors = tx_packets_err_total;
304 eth_kni_stats_reset(struct rte_eth_dev *dev)
306 struct rte_eth_dev_data *data = dev->data;
310 for (i = 0; i < data->nb_rx_queues; i++) {
311 q = data->rx_queues[i];
315 for (i = 0; i < data->nb_tx_queues; i++) {
316 q = data->tx_queues[i];
323 static const struct eth_dev_ops eth_kni_ops = {
324 .dev_start = eth_kni_dev_start,
325 .dev_stop = eth_kni_dev_stop,
326 .dev_configure = eth_kni_dev_configure,
327 .dev_infos_get = eth_kni_dev_info,
328 .rx_queue_setup = eth_kni_rx_queue_setup,
329 .tx_queue_setup = eth_kni_tx_queue_setup,
330 .rx_queue_release = eth_kni_queue_release,
331 .tx_queue_release = eth_kni_queue_release,
332 .link_update = eth_kni_link_update,
333 .stats_get = eth_kni_stats_get,
334 .stats_reset = eth_kni_stats_reset,
337 static struct rte_eth_dev *
338 eth_kni_create(struct rte_vdev_device *vdev,
339 struct eth_kni_args *args,
340 unsigned int numa_node)
342 struct pmd_internals *internals;
343 struct rte_eth_dev_data *data;
344 struct rte_eth_dev *eth_dev;
346 PMD_LOG(INFO, "Creating kni ethdev on numa socket %u",
349 /* reserve an ethdev entry */
350 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*internals));
354 internals = eth_dev->data->dev_private;
355 data = eth_dev->data;
356 data->nb_rx_queues = 1;
357 data->nb_tx_queues = 1;
358 data->dev_link = pmd_link;
359 data->mac_addrs = &internals->eth_addr;
361 eth_random_addr(internals->eth_addr.addr_bytes);
363 eth_dev->dev_ops = ð_kni_ops;
365 internals->no_request_thread = args->no_request_thread;
373 if (is_kni_initialized == 0)
374 rte_kni_init(MAX_KNI_PORTS);
376 is_kni_initialized++;
382 eth_kni_kvargs_process(struct eth_kni_args *args, const char *params)
384 struct rte_kvargs *kvlist;
386 kvlist = rte_kvargs_parse(params, valid_arguments);
390 memset(args, 0, sizeof(struct eth_kni_args));
392 if (rte_kvargs_count(kvlist, ETH_KNI_NO_REQUEST_THREAD_ARG) == 1)
393 args->no_request_thread = 1;
395 rte_kvargs_free(kvlist);
401 eth_kni_probe(struct rte_vdev_device *vdev)
403 struct rte_eth_dev *eth_dev;
404 struct eth_kni_args args;
409 name = rte_vdev_device_name(vdev);
410 params = rte_vdev_device_args(vdev);
411 PMD_LOG(INFO, "Initializing eth_kni for %s", name);
413 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
414 eth_dev = rte_eth_dev_attach_secondary(name);
416 PMD_LOG(ERR, "Failed to probe %s", name);
419 /* TODO: request info from primary to set up Rx and Tx */
420 eth_dev->dev_ops = ð_kni_ops;
421 eth_dev->device = &vdev->device;
422 rte_eth_dev_probing_finish(eth_dev);
426 ret = eth_kni_kvargs_process(&args, params);
434 eth_dev = eth_kni_create(vdev, &args, rte_socket_id());
438 eth_dev->rx_pkt_burst = eth_kni_rx;
439 eth_dev->tx_pkt_burst = eth_kni_tx;
441 rte_eth_dev_probing_finish(eth_dev);
445 is_kni_initialized--;
446 if (is_kni_initialized == 0)
452 eth_kni_remove(struct rte_vdev_device *vdev)
454 struct rte_eth_dev *eth_dev;
455 struct pmd_internals *internals;
458 name = rte_vdev_device_name(vdev);
459 PMD_LOG(INFO, "Un-Initializing eth_kni for %s", name);
461 /* find the ethdev entry */
462 eth_dev = rte_eth_dev_allocated(name);
466 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
467 return rte_eth_dev_release_port_secondary(eth_dev);
469 eth_kni_dev_stop(eth_dev);
471 internals = eth_dev->data->dev_private;
472 rte_kni_release(internals->kni);
476 rte_eth_dev_release_port(eth_dev);
478 is_kni_initialized--;
479 if (is_kni_initialized == 0)
485 static struct rte_vdev_driver eth_kni_drv = {
486 .probe = eth_kni_probe,
487 .remove = eth_kni_remove,
490 RTE_PMD_REGISTER_VDEV(net_kni, eth_kni_drv);
491 RTE_PMD_REGISTER_PARAM_STRING(net_kni, ETH_KNI_NO_REQUEST_THREAD_ARG "=<int>");
493 RTE_INIT(eth_kni_init_log)
495 eth_kni_logtype = rte_log_register("pmd.net.kni");
496 if (eth_kni_logtype >= 0)
497 rte_log_set_level(eth_kni_logtype, RTE_LOG_NOTICE);