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>
40 #include <rte_kvargs.h>
41 #include <rte_malloc.h>
44 /* Only single queue supported */
45 #define KNI_MAX_QUEUE_PER_PORT 1
47 #define MAX_PACKET_SZ 2048
48 #define MAX_KNI_PORTS 8
50 #define ETH_KNI_NO_REQUEST_THREAD_ARG "no_request_thread"
51 static const char * const valid_arguments[] = {
52 ETH_KNI_NO_REQUEST_THREAD_ARG,
57 int no_request_thread;
60 struct pmd_queue_stats {
67 struct pmd_internals *internals;
68 struct rte_mempool *mb_pool;
70 struct pmd_queue_stats rx;
71 struct pmd_queue_stats tx;
74 struct pmd_internals {
80 int no_request_thread;
82 struct ether_addr eth_addr;
84 struct pmd_queue rx_queues[KNI_MAX_QUEUE_PER_PORT];
85 struct pmd_queue tx_queues[KNI_MAX_QUEUE_PER_PORT];
88 static const struct rte_eth_link pmd_link = {
89 .link_speed = ETH_SPEED_NUM_10G,
90 .link_duplex = ETH_LINK_FULL_DUPLEX,
91 .link_status = ETH_LINK_DOWN,
92 .link_autoneg = ETH_LINK_SPEED_AUTONEG,
94 static int is_kni_initialized;
97 eth_kni_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
99 struct pmd_queue *kni_q = q;
100 struct rte_kni *kni = kni_q->internals->kni;
103 nb_pkts = rte_kni_rx_burst(kni, bufs, nb_bufs);
105 kni_q->rx.pkts += nb_pkts;
106 kni_q->rx.err_pkts += nb_bufs - nb_pkts;
112 eth_kni_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
114 struct pmd_queue *kni_q = q;
115 struct rte_kni *kni = kni_q->internals->kni;
118 nb_pkts = rte_kni_tx_burst(kni, bufs, nb_bufs);
120 kni_q->tx.pkts += nb_pkts;
121 kni_q->tx.err_pkts += nb_bufs - nb_pkts;
127 kni_handle_request(void *param)
129 struct pmd_internals *internals = param;
132 while (!internals->stop_thread) {
133 rte_kni_handle_request(internals->kni);
141 eth_kni_start(struct rte_eth_dev *dev)
143 struct pmd_internals *internals = dev->data->dev_private;
144 uint16_t port_id = dev->data->port_id;
145 struct rte_mempool *mb_pool;
146 struct rte_kni_conf conf;
147 const char *name = dev->data->name + 4; /* remove net_ */
149 snprintf(conf.name, RTE_KNI_NAMESIZE, "%s", name);
151 conf.group_id = port_id;
152 conf.mbuf_size = MAX_PACKET_SZ;
153 mb_pool = internals->rx_queues[0].mb_pool;
155 internals->kni = rte_kni_alloc(mb_pool, &conf, NULL);
156 if (internals->kni == NULL) {
158 "Fail to create kni interface for port: %d\n",
167 eth_kni_dev_start(struct rte_eth_dev *dev)
169 struct pmd_internals *internals = dev->data->dev_private;
172 if (internals->is_kni_started == 0) {
173 ret = eth_kni_start(dev);
176 internals->is_kni_started = 1;
179 if (internals->no_request_thread == 0) {
180 ret = pthread_create(&internals->thread, NULL,
181 kni_handle_request, internals);
184 "Fail to create kni request thread\n");
189 dev->data->dev_link.link_status = 1;
195 eth_kni_dev_stop(struct rte_eth_dev *dev)
197 struct pmd_internals *internals = dev->data->dev_private;
200 if (internals->no_request_thread == 0) {
201 internals->stop_thread = 1;
203 ret = pthread_cancel(internals->thread);
205 RTE_LOG(ERR, PMD, "Can't cancel the thread\n");
207 ret = pthread_join(internals->thread, NULL);
209 RTE_LOG(ERR, PMD, "Can't join the thread\n");
211 internals->stop_thread = 0;
214 dev->data->dev_link.link_status = 0;
218 eth_kni_dev_configure(struct rte_eth_dev *dev __rte_unused)
224 eth_kni_dev_info(struct rte_eth_dev *dev __rte_unused,
225 struct rte_eth_dev_info *dev_info)
227 dev_info->max_mac_addrs = 1;
228 dev_info->max_rx_pktlen = UINT32_MAX;
229 dev_info->max_rx_queues = KNI_MAX_QUEUE_PER_PORT;
230 dev_info->max_tx_queues = KNI_MAX_QUEUE_PER_PORT;
231 dev_info->min_rx_bufsize = 0;
232 dev_info->pci_dev = NULL;
236 eth_kni_rx_queue_setup(struct rte_eth_dev *dev,
237 uint16_t rx_queue_id,
238 uint16_t nb_rx_desc __rte_unused,
239 unsigned int socket_id __rte_unused,
240 const struct rte_eth_rxconf *rx_conf __rte_unused,
241 struct rte_mempool *mb_pool)
243 struct pmd_internals *internals = dev->data->dev_private;
246 q = &internals->rx_queues[rx_queue_id];
247 q->internals = internals;
248 q->mb_pool = mb_pool;
250 dev->data->rx_queues[rx_queue_id] = q;
256 eth_kni_tx_queue_setup(struct rte_eth_dev *dev,
257 uint16_t tx_queue_id,
258 uint16_t nb_tx_desc __rte_unused,
259 unsigned int socket_id __rte_unused,
260 const struct rte_eth_txconf *tx_conf __rte_unused)
262 struct pmd_internals *internals = dev->data->dev_private;
265 q = &internals->tx_queues[tx_queue_id];
266 q->internals = internals;
268 dev->data->tx_queues[tx_queue_id] = q;
274 eth_kni_queue_release(void *q __rte_unused)
279 eth_kni_link_update(struct rte_eth_dev *dev __rte_unused,
280 int wait_to_complete __rte_unused)
286 eth_kni_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
288 unsigned long rx_packets_total = 0, rx_bytes_total = 0;
289 unsigned long tx_packets_total = 0, tx_bytes_total = 0;
290 struct rte_eth_dev_data *data = dev->data;
291 unsigned long tx_packets_err_total = 0;
292 unsigned int i, num_stats;
295 num_stats = RTE_MIN((unsigned int)RTE_ETHDEV_QUEUE_STAT_CNTRS,
297 for (i = 0; i < num_stats; i++) {
298 q = data->rx_queues[i];
299 stats->q_ipackets[i] = q->rx.pkts;
300 stats->q_ibytes[i] = q->rx.bytes;
301 rx_packets_total += stats->q_ipackets[i];
302 rx_bytes_total += stats->q_ibytes[i];
305 num_stats = RTE_MIN((unsigned int)RTE_ETHDEV_QUEUE_STAT_CNTRS,
307 for (i = 0; i < num_stats; i++) {
308 q = data->tx_queues[i];
309 stats->q_opackets[i] = q->tx.pkts;
310 stats->q_obytes[i] = q->tx.bytes;
311 stats->q_errors[i] = q->tx.err_pkts;
312 tx_packets_total += stats->q_opackets[i];
313 tx_bytes_total += stats->q_obytes[i];
314 tx_packets_err_total += stats->q_errors[i];
317 stats->ipackets = rx_packets_total;
318 stats->ibytes = rx_bytes_total;
319 stats->opackets = tx_packets_total;
320 stats->obytes = tx_bytes_total;
321 stats->oerrors = tx_packets_err_total;
325 eth_kni_stats_reset(struct rte_eth_dev *dev)
327 struct rte_eth_dev_data *data = dev->data;
331 for (i = 0; i < data->nb_rx_queues; i++) {
332 q = data->rx_queues[i];
336 for (i = 0; i < data->nb_tx_queues; i++) {
337 q = data->tx_queues[i];
344 static const struct eth_dev_ops eth_kni_ops = {
345 .dev_start = eth_kni_dev_start,
346 .dev_stop = eth_kni_dev_stop,
347 .dev_configure = eth_kni_dev_configure,
348 .dev_infos_get = eth_kni_dev_info,
349 .rx_queue_setup = eth_kni_rx_queue_setup,
350 .tx_queue_setup = eth_kni_tx_queue_setup,
351 .rx_queue_release = eth_kni_queue_release,
352 .tx_queue_release = eth_kni_queue_release,
353 .link_update = eth_kni_link_update,
354 .stats_get = eth_kni_stats_get,
355 .stats_reset = eth_kni_stats_reset,
358 static struct rte_vdev_driver eth_kni_drv;
360 static struct rte_eth_dev *
361 eth_kni_create(const char *name, struct eth_kni_args *args,
362 unsigned int numa_node)
364 struct pmd_internals *internals = NULL;
365 struct rte_eth_dev_data *data;
366 struct rte_eth_dev *eth_dev;
368 RTE_LOG(INFO, PMD, "Creating kni ethdev on numa socket %u\n",
371 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
375 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
376 if (internals == NULL)
379 /* reserve an ethdev entry */
380 eth_dev = rte_eth_dev_allocate(name);
384 data->dev_private = internals;
385 data->port_id = eth_dev->data->port_id;
386 memmove(data->name, eth_dev->data->name, sizeof(data->name));
387 data->nb_rx_queues = 1;
388 data->nb_tx_queues = 1;
389 data->dev_link = pmd_link;
390 data->mac_addrs = &internals->eth_addr;
392 eth_random_addr(internals->eth_addr.addr_bytes);
394 eth_dev->data = data;
395 eth_dev->dev_ops = ð_kni_ops;
396 eth_dev->device->driver = NULL;
398 data->dev_flags = RTE_ETH_DEV_DETACHABLE;
399 data->kdrv = RTE_KDRV_NONE;
400 data->drv_name = eth_kni_drv.driver.name;
401 data->numa_node = numa_node;
403 internals->no_request_thread = args->no_request_thread;
417 if (is_kni_initialized == 0)
418 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 RTE_LOG(INFO, PMD, "Initializing eth_kni for %s\n", name);
457 ret = eth_kni_kvargs_process(&args, params);
465 eth_dev = eth_kni_create(name, &args, rte_socket_id());
469 eth_dev->rx_pkt_burst = eth_kni_rx;
470 eth_dev->tx_pkt_burst = eth_kni_tx;
475 is_kni_initialized--;
476 if (is_kni_initialized == 0)
482 eth_kni_remove(struct rte_vdev_device *vdev)
484 struct rte_eth_dev *eth_dev;
485 struct pmd_internals *internals;
488 name = rte_vdev_device_name(vdev);
489 RTE_LOG(INFO, PMD, "Un-Initializing eth_kni for %s\n", name);
491 /* find the ethdev entry */
492 eth_dev = rte_eth_dev_allocated(name);
496 eth_kni_dev_stop(eth_dev);
499 internals = eth_dev->data->dev_private;
500 rte_kni_release(internals->kni);
504 rte_free(eth_dev->data);
506 rte_eth_dev_release_port(eth_dev);
508 is_kni_initialized--;
509 if (is_kni_initialized == 0)
515 static struct rte_vdev_driver eth_kni_drv = {
516 .probe = eth_kni_probe,
517 .remove = eth_kni_remove,
520 RTE_PMD_REGISTER_VDEV(net_kni, eth_kni_drv);
521 RTE_PMD_REGISTER_PARAM_STRING(net_kni, ETH_KNI_NO_REQUEST_THREAD_ARG "=<int>");