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->data->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;
326 eth_kni_stats_reset(struct rte_eth_dev *dev)
328 struct rte_eth_dev_data *data = dev->data;
332 for (i = 0; i < data->nb_rx_queues; i++) {
333 q = data->rx_queues[i];
337 for (i = 0; i < data->nb_tx_queues; i++) {
338 q = data->tx_queues[i];
345 static const struct eth_dev_ops eth_kni_ops = {
346 .dev_start = eth_kni_dev_start,
347 .dev_stop = eth_kni_dev_stop,
348 .dev_configure = eth_kni_dev_configure,
349 .dev_infos_get = eth_kni_dev_info,
350 .rx_queue_setup = eth_kni_rx_queue_setup,
351 .tx_queue_setup = eth_kni_tx_queue_setup,
352 .rx_queue_release = eth_kni_queue_release,
353 .tx_queue_release = eth_kni_queue_release,
354 .link_update = eth_kni_link_update,
355 .stats_get = eth_kni_stats_get,
356 .stats_reset = eth_kni_stats_reset,
359 static struct rte_vdev_driver eth_kni_drv;
361 static struct rte_eth_dev *
362 eth_kni_create(struct rte_vdev_device *vdev,
363 struct eth_kni_args *args,
364 unsigned int numa_node)
366 struct pmd_internals *internals;
367 struct rte_eth_dev_data *data;
368 struct rte_eth_dev *eth_dev;
371 RTE_LOG(INFO, PMD, "Creating kni ethdev on numa socket %u\n",
374 name = rte_vdev_device_name(vdev);
375 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
379 /* reserve an ethdev entry */
380 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*internals));
381 if (eth_dev == NULL) {
386 internals = eth_dev->data->dev_private;
387 rte_memcpy(data, eth_dev->data, sizeof(*data));
388 data->nb_rx_queues = 1;
389 data->nb_tx_queues = 1;
390 data->dev_link = pmd_link;
391 data->mac_addrs = &internals->eth_addr;
393 eth_random_addr(internals->eth_addr.addr_bytes);
395 eth_dev->data = data;
396 eth_dev->dev_ops = ð_kni_ops;
398 data->dev_flags = RTE_ETH_DEV_DETACHABLE;
400 internals->no_request_thread = args->no_request_thread;
408 if (is_kni_initialized == 0)
409 rte_kni_init(MAX_KNI_PORTS);
411 is_kni_initialized++;
417 eth_kni_kvargs_process(struct eth_kni_args *args, const char *params)
419 struct rte_kvargs *kvlist;
421 kvlist = rte_kvargs_parse(params, valid_arguments);
425 memset(args, 0, sizeof(struct eth_kni_args));
427 if (rte_kvargs_count(kvlist, ETH_KNI_NO_REQUEST_THREAD_ARG) == 1)
428 args->no_request_thread = 1;
430 rte_kvargs_free(kvlist);
436 eth_kni_probe(struct rte_vdev_device *vdev)
438 struct rte_eth_dev *eth_dev;
439 struct eth_kni_args args;
444 name = rte_vdev_device_name(vdev);
445 params = rte_vdev_device_args(vdev);
446 RTE_LOG(INFO, PMD, "Initializing eth_kni for %s\n", name);
448 ret = eth_kni_kvargs_process(&args, params);
456 eth_dev = eth_kni_create(vdev, &args, rte_socket_id());
460 eth_dev->rx_pkt_burst = eth_kni_rx;
461 eth_dev->tx_pkt_burst = eth_kni_tx;
466 is_kni_initialized--;
467 if (is_kni_initialized == 0)
473 eth_kni_remove(struct rte_vdev_device *vdev)
475 struct rte_eth_dev *eth_dev;
476 struct pmd_internals *internals;
479 name = rte_vdev_device_name(vdev);
480 RTE_LOG(INFO, PMD, "Un-Initializing eth_kni for %s\n", name);
482 /* find the ethdev entry */
483 eth_dev = rte_eth_dev_allocated(name);
487 eth_kni_dev_stop(eth_dev);
490 internals = eth_dev->data->dev_private;
491 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>");