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;
210 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_CRC_STRIP;
214 eth_kni_rx_queue_setup(struct rte_eth_dev *dev,
215 uint16_t rx_queue_id,
216 uint16_t nb_rx_desc __rte_unused,
217 unsigned int socket_id __rte_unused,
218 const struct rte_eth_rxconf *rx_conf __rte_unused,
219 struct rte_mempool *mb_pool)
221 struct pmd_internals *internals = dev->data->dev_private;
224 q = &internals->rx_queues[rx_queue_id];
225 q->internals = internals;
226 q->mb_pool = mb_pool;
228 dev->data->rx_queues[rx_queue_id] = q;
234 eth_kni_tx_queue_setup(struct rte_eth_dev *dev,
235 uint16_t tx_queue_id,
236 uint16_t nb_tx_desc __rte_unused,
237 unsigned int socket_id __rte_unused,
238 const struct rte_eth_txconf *tx_conf __rte_unused)
240 struct pmd_internals *internals = dev->data->dev_private;
243 q = &internals->tx_queues[tx_queue_id];
244 q->internals = internals;
246 dev->data->tx_queues[tx_queue_id] = q;
252 eth_kni_queue_release(void *q __rte_unused)
257 eth_kni_link_update(struct rte_eth_dev *dev __rte_unused,
258 int wait_to_complete __rte_unused)
264 eth_kni_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
266 unsigned long rx_packets_total = 0, rx_bytes_total = 0;
267 unsigned long tx_packets_total = 0, tx_bytes_total = 0;
268 struct rte_eth_dev_data *data = dev->data;
269 unsigned long tx_packets_err_total = 0;
270 unsigned int i, num_stats;
273 num_stats = RTE_MIN((unsigned int)RTE_ETHDEV_QUEUE_STAT_CNTRS,
275 for (i = 0; i < num_stats; i++) {
276 q = data->rx_queues[i];
277 stats->q_ipackets[i] = q->rx.pkts;
278 stats->q_ibytes[i] = q->rx.bytes;
279 rx_packets_total += stats->q_ipackets[i];
280 rx_bytes_total += stats->q_ibytes[i];
283 num_stats = RTE_MIN((unsigned int)RTE_ETHDEV_QUEUE_STAT_CNTRS,
285 for (i = 0; i < num_stats; i++) {
286 q = data->tx_queues[i];
287 stats->q_opackets[i] = q->tx.pkts;
288 stats->q_obytes[i] = q->tx.bytes;
289 stats->q_errors[i] = q->tx.err_pkts;
290 tx_packets_total += stats->q_opackets[i];
291 tx_bytes_total += stats->q_obytes[i];
292 tx_packets_err_total += stats->q_errors[i];
295 stats->ipackets = rx_packets_total;
296 stats->ibytes = rx_bytes_total;
297 stats->opackets = tx_packets_total;
298 stats->obytes = tx_bytes_total;
299 stats->oerrors = tx_packets_err_total;
305 eth_kni_stats_reset(struct rte_eth_dev *dev)
307 struct rte_eth_dev_data *data = dev->data;
311 for (i = 0; i < data->nb_rx_queues; i++) {
312 q = data->rx_queues[i];
316 for (i = 0; i < data->nb_tx_queues; i++) {
317 q = data->tx_queues[i];
324 static const struct eth_dev_ops eth_kni_ops = {
325 .dev_start = eth_kni_dev_start,
326 .dev_stop = eth_kni_dev_stop,
327 .dev_configure = eth_kni_dev_configure,
328 .dev_infos_get = eth_kni_dev_info,
329 .rx_queue_setup = eth_kni_rx_queue_setup,
330 .tx_queue_setup = eth_kni_tx_queue_setup,
331 .rx_queue_release = eth_kni_queue_release,
332 .tx_queue_release = eth_kni_queue_release,
333 .link_update = eth_kni_link_update,
334 .stats_get = eth_kni_stats_get,
335 .stats_reset = eth_kni_stats_reset,
338 static struct rte_eth_dev *
339 eth_kni_create(struct rte_vdev_device *vdev,
340 struct eth_kni_args *args,
341 unsigned int numa_node)
343 struct pmd_internals *internals;
344 struct rte_eth_dev_data *data;
345 struct rte_eth_dev *eth_dev;
347 PMD_LOG(INFO, "Creating kni ethdev on numa socket %u",
350 /* reserve an ethdev entry */
351 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*internals));
355 internals = eth_dev->data->dev_private;
356 data = eth_dev->data;
357 data->nb_rx_queues = 1;
358 data->nb_tx_queues = 1;
359 data->dev_link = pmd_link;
360 data->mac_addrs = &internals->eth_addr;
362 eth_random_addr(internals->eth_addr.addr_bytes);
364 eth_dev->dev_ops = ð_kni_ops;
366 internals->no_request_thread = args->no_request_thread;
374 if (is_kni_initialized == 0)
375 rte_kni_init(MAX_KNI_PORTS);
377 is_kni_initialized++;
383 eth_kni_kvargs_process(struct eth_kni_args *args, const char *params)
385 struct rte_kvargs *kvlist;
387 kvlist = rte_kvargs_parse(params, valid_arguments);
391 memset(args, 0, sizeof(struct eth_kni_args));
393 if (rte_kvargs_count(kvlist, ETH_KNI_NO_REQUEST_THREAD_ARG) == 1)
394 args->no_request_thread = 1;
396 rte_kvargs_free(kvlist);
402 eth_kni_probe(struct rte_vdev_device *vdev)
404 struct rte_eth_dev *eth_dev;
405 struct eth_kni_args args;
410 name = rte_vdev_device_name(vdev);
411 params = rte_vdev_device_args(vdev);
412 PMD_LOG(INFO, "Initializing eth_kni for %s", name);
414 if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
415 strlen(params) == 0) {
416 eth_dev = rte_eth_dev_attach_secondary(name);
418 PMD_LOG(ERR, "Failed to probe %s", name);
421 /* TODO: request info from primary to set up Rx and Tx */
422 eth_dev->dev_ops = ð_kni_ops;
423 rte_eth_dev_probing_finish(eth_dev);
427 ret = eth_kni_kvargs_process(&args, params);
435 eth_dev = eth_kni_create(vdev, &args, rte_socket_id());
439 eth_dev->rx_pkt_burst = eth_kni_rx;
440 eth_dev->tx_pkt_burst = eth_kni_tx;
442 rte_eth_dev_probing_finish(eth_dev);
446 is_kni_initialized--;
447 if (is_kni_initialized == 0)
453 eth_kni_remove(struct rte_vdev_device *vdev)
455 struct rte_eth_dev *eth_dev;
456 struct pmd_internals *internals;
459 name = rte_vdev_device_name(vdev);
460 PMD_LOG(INFO, "Un-Initializing eth_kni for %s", name);
462 /* find the ethdev entry */
463 eth_dev = rte_eth_dev_allocated(name);
467 eth_kni_dev_stop(eth_dev);
469 internals = eth_dev->data->dev_private;
470 rte_kni_release(internals->kni);
474 rte_eth_dev_release_port(eth_dev);
476 is_kni_initialized--;
477 if (is_kni_initialized == 0)
483 static struct rte_vdev_driver eth_kni_drv = {
484 .probe = eth_kni_probe,
485 .remove = eth_kni_remove,
488 RTE_PMD_REGISTER_VDEV(net_kni, eth_kni_drv);
489 RTE_PMD_REGISTER_PARAM_STRING(net_kni, ETH_KNI_NO_REQUEST_THREAD_ARG "=<int>");
491 RTE_INIT(eth_kni_init_log)
493 eth_kni_logtype = rte_log_register("pmd.net.kni");
494 if (eth_kni_logtype >= 0)
495 rte_log_set_level(eth_kni_logtype, RTE_LOG_NOTICE);