4 * Copyright(c) 2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <rte_ethdev.h>
39 #include <rte_ethdev_vdev.h>
41 #include <rte_kvargs.h>
42 #include <rte_malloc.h>
45 /* Only single queue supported */
46 #define KNI_MAX_QUEUE_PER_PORT 1
48 #define MAX_PACKET_SZ 2048
49 #define MAX_KNI_PORTS 8
51 #define ETH_KNI_NO_REQUEST_THREAD_ARG "no_request_thread"
52 static const char * const valid_arguments[] = {
53 ETH_KNI_NO_REQUEST_THREAD_ARG,
58 int no_request_thread;
61 struct pmd_queue_stats {
68 struct pmd_internals *internals;
69 struct rte_mempool *mb_pool;
71 struct pmd_queue_stats rx;
72 struct pmd_queue_stats tx;
75 struct pmd_internals {
81 int no_request_thread;
83 struct ether_addr eth_addr;
85 struct pmd_queue rx_queues[KNI_MAX_QUEUE_PER_PORT];
86 struct pmd_queue tx_queues[KNI_MAX_QUEUE_PER_PORT];
89 static const struct rte_eth_link pmd_link = {
90 .link_speed = ETH_SPEED_NUM_10G,
91 .link_duplex = ETH_LINK_FULL_DUPLEX,
92 .link_status = ETH_LINK_DOWN,
93 .link_autoneg = ETH_LINK_SPEED_AUTONEG,
95 static int is_kni_initialized;
98 eth_kni_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
100 struct pmd_queue *kni_q = q;
101 struct rte_kni *kni = kni_q->internals->kni;
104 nb_pkts = rte_kni_rx_burst(kni, bufs, nb_bufs);
106 kni_q->rx.pkts += nb_pkts;
107 kni_q->rx.err_pkts += nb_bufs - nb_pkts;
113 eth_kni_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
115 struct pmd_queue *kni_q = q;
116 struct rte_kni *kni = kni_q->internals->kni;
119 nb_pkts = rte_kni_tx_burst(kni, bufs, nb_bufs);
121 kni_q->tx.pkts += nb_pkts;
122 kni_q->tx.err_pkts += nb_bufs - nb_pkts;
128 kni_handle_request(void *param)
130 struct pmd_internals *internals = param;
133 while (!internals->stop_thread) {
134 rte_kni_handle_request(internals->kni);
142 eth_kni_start(struct rte_eth_dev *dev)
144 struct pmd_internals *internals = dev->data->dev_private;
145 uint16_t port_id = dev->data->port_id;
146 struct rte_mempool *mb_pool;
147 struct rte_kni_conf conf;
148 const char *name = dev->device->name + 4; /* remove net_ */
150 snprintf(conf.name, RTE_KNI_NAMESIZE, "%s", name);
152 conf.group_id = port_id;
153 conf.mbuf_size = MAX_PACKET_SZ;
154 mb_pool = internals->rx_queues[0].mb_pool;
156 internals->kni = rte_kni_alloc(mb_pool, &conf, NULL);
157 if (internals->kni == NULL) {
159 "Fail to create kni interface for port: %d\n",
168 eth_kni_dev_start(struct rte_eth_dev *dev)
170 struct pmd_internals *internals = dev->data->dev_private;
173 if (internals->is_kni_started == 0) {
174 ret = eth_kni_start(dev);
177 internals->is_kni_started = 1;
180 if (internals->no_request_thread == 0) {
181 ret = pthread_create(&internals->thread, NULL,
182 kni_handle_request, internals);
185 "Fail to create kni request thread\n");
190 dev->data->dev_link.link_status = 1;
196 eth_kni_dev_stop(struct rte_eth_dev *dev)
198 struct pmd_internals *internals = dev->data->dev_private;
201 if (internals->no_request_thread == 0) {
202 internals->stop_thread = 1;
204 ret = pthread_cancel(internals->thread);
206 RTE_LOG(ERR, PMD, "Can't cancel the thread\n");
208 ret = pthread_join(internals->thread, NULL);
210 RTE_LOG(ERR, PMD, "Can't join the thread\n");
212 internals->stop_thread = 0;
215 dev->data->dev_link.link_status = 0;
219 eth_kni_dev_configure(struct rte_eth_dev *dev __rte_unused)
225 eth_kni_dev_info(struct rte_eth_dev *dev __rte_unused,
226 struct rte_eth_dev_info *dev_info)
228 dev_info->max_mac_addrs = 1;
229 dev_info->max_rx_pktlen = UINT32_MAX;
230 dev_info->max_rx_queues = KNI_MAX_QUEUE_PER_PORT;
231 dev_info->max_tx_queues = KNI_MAX_QUEUE_PER_PORT;
232 dev_info->min_rx_bufsize = 0;
233 dev_info->pci_dev = NULL;
237 eth_kni_rx_queue_setup(struct rte_eth_dev *dev,
238 uint16_t rx_queue_id,
239 uint16_t nb_rx_desc __rte_unused,
240 unsigned int socket_id __rte_unused,
241 const struct rte_eth_rxconf *rx_conf __rte_unused,
242 struct rte_mempool *mb_pool)
244 struct pmd_internals *internals = dev->data->dev_private;
247 q = &internals->rx_queues[rx_queue_id];
248 q->internals = internals;
249 q->mb_pool = mb_pool;
251 dev->data->rx_queues[rx_queue_id] = q;
257 eth_kni_tx_queue_setup(struct rte_eth_dev *dev,
258 uint16_t tx_queue_id,
259 uint16_t nb_tx_desc __rte_unused,
260 unsigned int socket_id __rte_unused,
261 const struct rte_eth_txconf *tx_conf __rte_unused)
263 struct pmd_internals *internals = dev->data->dev_private;
266 q = &internals->tx_queues[tx_queue_id];
267 q->internals = internals;
269 dev->data->tx_queues[tx_queue_id] = q;
275 eth_kni_queue_release(void *q __rte_unused)
280 eth_kni_link_update(struct rte_eth_dev *dev __rte_unused,
281 int wait_to_complete __rte_unused)
287 eth_kni_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
289 unsigned long rx_packets_total = 0, rx_bytes_total = 0;
290 unsigned long tx_packets_total = 0, tx_bytes_total = 0;
291 struct rte_eth_dev_data *data = dev->data;
292 unsigned long tx_packets_err_total = 0;
293 unsigned int i, num_stats;
296 num_stats = RTE_MIN((unsigned int)RTE_ETHDEV_QUEUE_STAT_CNTRS,
298 for (i = 0; i < num_stats; i++) {
299 q = data->rx_queues[i];
300 stats->q_ipackets[i] = q->rx.pkts;
301 stats->q_ibytes[i] = q->rx.bytes;
302 rx_packets_total += stats->q_ipackets[i];
303 rx_bytes_total += stats->q_ibytes[i];
306 num_stats = RTE_MIN((unsigned int)RTE_ETHDEV_QUEUE_STAT_CNTRS,
308 for (i = 0; i < num_stats; i++) {
309 q = data->tx_queues[i];
310 stats->q_opackets[i] = q->tx.pkts;
311 stats->q_obytes[i] = q->tx.bytes;
312 stats->q_errors[i] = q->tx.err_pkts;
313 tx_packets_total += stats->q_opackets[i];
314 tx_bytes_total += stats->q_obytes[i];
315 tx_packets_err_total += stats->q_errors[i];
318 stats->ipackets = rx_packets_total;
319 stats->ibytes = rx_bytes_total;
320 stats->opackets = tx_packets_total;
321 stats->obytes = tx_bytes_total;
322 stats->oerrors = tx_packets_err_total;
328 eth_kni_stats_reset(struct rte_eth_dev *dev)
330 struct rte_eth_dev_data *data = dev->data;
334 for (i = 0; i < data->nb_rx_queues; i++) {
335 q = data->rx_queues[i];
339 for (i = 0; i < data->nb_tx_queues; i++) {
340 q = data->tx_queues[i];
347 static const struct eth_dev_ops eth_kni_ops = {
348 .dev_start = eth_kni_dev_start,
349 .dev_stop = eth_kni_dev_stop,
350 .dev_configure = eth_kni_dev_configure,
351 .dev_infos_get = eth_kni_dev_info,
352 .rx_queue_setup = eth_kni_rx_queue_setup,
353 .tx_queue_setup = eth_kni_tx_queue_setup,
354 .rx_queue_release = eth_kni_queue_release,
355 .tx_queue_release = eth_kni_queue_release,
356 .link_update = eth_kni_link_update,
357 .stats_get = eth_kni_stats_get,
358 .stats_reset = eth_kni_stats_reset,
361 static struct rte_vdev_driver eth_kni_drv;
363 static struct rte_eth_dev *
364 eth_kni_create(struct rte_vdev_device *vdev,
365 struct eth_kni_args *args,
366 unsigned int numa_node)
368 struct pmd_internals *internals;
369 struct rte_eth_dev_data *data;
370 struct rte_eth_dev *eth_dev;
373 RTE_LOG(INFO, PMD, "Creating kni ethdev on numa socket %u\n",
376 name = rte_vdev_device_name(vdev);
377 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
381 /* reserve an ethdev entry */
382 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*internals));
383 if (eth_dev == NULL) {
388 internals = eth_dev->data->dev_private;
389 rte_memcpy(data, eth_dev->data, sizeof(*data));
390 data->nb_rx_queues = 1;
391 data->nb_tx_queues = 1;
392 data->dev_link = pmd_link;
393 data->mac_addrs = &internals->eth_addr;
395 eth_random_addr(internals->eth_addr.addr_bytes);
397 eth_dev->data = data;
398 eth_dev->dev_ops = ð_kni_ops;
400 data->dev_flags = RTE_ETH_DEV_DETACHABLE;
402 internals->no_request_thread = args->no_request_thread;
410 if (is_kni_initialized == 0)
411 rte_kni_init(MAX_KNI_PORTS);
413 is_kni_initialized++;
419 eth_kni_kvargs_process(struct eth_kni_args *args, const char *params)
421 struct rte_kvargs *kvlist;
423 kvlist = rte_kvargs_parse(params, valid_arguments);
427 memset(args, 0, sizeof(struct eth_kni_args));
429 if (rte_kvargs_count(kvlist, ETH_KNI_NO_REQUEST_THREAD_ARG) == 1)
430 args->no_request_thread = 1;
432 rte_kvargs_free(kvlist);
438 eth_kni_probe(struct rte_vdev_device *vdev)
440 struct rte_eth_dev *eth_dev;
441 struct eth_kni_args args;
446 name = rte_vdev_device_name(vdev);
447 params = rte_vdev_device_args(vdev);
448 RTE_LOG(INFO, PMD, "Initializing eth_kni for %s\n", name);
450 ret = eth_kni_kvargs_process(&args, params);
458 eth_dev = eth_kni_create(vdev, &args, rte_socket_id());
462 eth_dev->rx_pkt_burst = eth_kni_rx;
463 eth_dev->tx_pkt_burst = eth_kni_tx;
468 is_kni_initialized--;
469 if (is_kni_initialized == 0)
475 eth_kni_remove(struct rte_vdev_device *vdev)
477 struct rte_eth_dev *eth_dev;
478 struct pmd_internals *internals;
481 name = rte_vdev_device_name(vdev);
482 RTE_LOG(INFO, PMD, "Un-Initializing eth_kni for %s\n", name);
484 /* find the ethdev entry */
485 eth_dev = rte_eth_dev_allocated(name);
489 eth_kni_dev_stop(eth_dev);
491 internals = eth_dev->data->dev_private;
492 rte_kni_release(internals->kni);
495 rte_free(eth_dev->data);
497 rte_eth_dev_release_port(eth_dev);
499 is_kni_initialized--;
500 if (is_kni_initialized == 0)
506 static struct rte_vdev_driver eth_kni_drv = {
507 .probe = eth_kni_probe,
508 .remove = eth_kni_remove,
511 RTE_PMD_REGISTER_VDEV(net_kni, eth_kni_drv);
512 RTE_PMD_REGISTER_PARAM_STRING(net_kni, ETH_KNI_NO_REQUEST_THREAD_ARG "=<int>");