1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
13 #ifndef RTE_LIBRTE_KNI
18 printf("KNI not supported, skipping test\n");
24 #include <rte_string_fns.h>
25 #include <rte_mempool.h>
26 #include <rte_ethdev.h>
27 #include <rte_bus_pci.h>
28 #include <rte_cycles.h>
32 #define MAX_PACKET_SZ 2048
33 #define MBUF_DATA_SZ (MAX_PACKET_SZ + RTE_PKTMBUF_HEADROOM)
34 #define PKT_BURST_SZ 32
35 #define MEMPOOL_CACHE_SZ PKT_BURST_SZ
39 #define KNI_TIMEOUT_MS 5000 /* ms */
41 #define IFCONFIG "/sbin/ifconfig "
42 #define TEST_KNI_PORT "test_kni_port"
43 #define KNI_TEST_MAX_PORTS 4
44 /* The threshold number of mbufs to be transmitted or received. */
45 #define KNI_NUM_MBUF_THRESHOLD 100
46 static int kni_pkt_mtu = 0;
48 struct test_kni_stats {
49 volatile uint64_t ingress;
50 volatile uint64_t egress;
53 static const struct rte_eth_rxconf rx_conf = {
62 static const struct rte_eth_txconf tx_conf = {
72 static const struct rte_eth_conf port_conf = {
74 .mq_mode = ETH_DCB_NONE,
78 static struct rte_kni_ops kni_ops = {
80 .config_network_if = NULL,
81 .config_mac_address = NULL,
82 .config_promiscusity = NULL,
85 static unsigned lcore_master, lcore_ingress, lcore_egress;
86 static struct rte_kni *test_kni_ctx;
87 static struct test_kni_stats stats;
89 static volatile uint32_t test_kni_processing_flag;
91 static struct rte_mempool *
92 test_kni_create_mempool(void)
94 struct rte_mempool * mp;
96 mp = rte_mempool_lookup("kni_mempool");
98 mp = rte_pktmbuf_pool_create("kni_mempool",
100 MEMPOOL_CACHE_SZ, 0, MBUF_DATA_SZ,
106 static struct rte_mempool *
107 test_kni_lookup_mempool(void)
109 return rte_mempool_lookup("kni_mempool");
111 /* Callback for request of changing MTU */
113 kni_change_mtu(uint16_t port_id, unsigned int new_mtu)
115 printf("Change MTU of port %d to %u\n", port_id, new_mtu);
116 kni_pkt_mtu = new_mtu;
117 printf("Change MTU of port %d to %i successfully.\n",
118 port_id, kni_pkt_mtu);
123 test_kni_link_change(void)
130 printf("Error: Failed to fork a process\n");
135 printf("Starting KNI Link status change tests.\n");
136 if (system(IFCONFIG TEST_KNI_PORT" up") == -1) {
141 ret = rte_kni_update_link(test_kni_ctx, 1);
143 printf("Failed to change link state to Up ret=%d.\n",
148 printf("KNI: Set LINKUP, previous state=%d\n", ret);
150 ret = rte_kni_update_link(test_kni_ctx, 0);
153 "Failed! Previous link state should be 1, returned %d.\n",
158 printf("KNI: Set LINKDOWN, previous state=%d\n", ret);
160 ret = rte_kni_update_link(test_kni_ctx, 1);
163 "Failed! Previous link state should be 0, returned %d.\n",
167 printf("KNI: Set LINKUP, previous state=%d\n", ret);
173 if (system(IFCONFIG TEST_KNI_PORT" down") == -1)
176 printf("KNI: Link status change tests: %s.\n",
177 (ret == 0) ? "Passed" : "Failed");
183 p_ret = waitpid(pid, &status, WNOHANG);
185 if (WIFEXITED(status))
186 return WEXITSTATUS(status);
190 rte_kni_handle_request(test_kni_ctx);
195 * This loop fully tests the basic functions of KNI. e.g. transmitting,
196 * receiving to, from kernel space, and kernel requests.
198 * This is the loop to transmit/receive mbufs to/from kernel interface with
199 * supported by KNI kernel module. The ingress lcore will allocate mbufs and
200 * transmit them to kernel space; while the egress lcore will receive the mbufs
201 * from kernel space and free them.
202 * On the master lcore, several commands will be run to check handling the
203 * kernel requests. And it will finally set the flag to exit the KNI
204 * transmitting/receiving to/from the kernel space.
206 * Note: To support this testing, the KNI kernel module needs to be insmodded
207 * in one of its loopback modes.
210 test_kni_loop(__rte_unused void *arg)
213 unsigned nb_rx, nb_tx, num, i;
214 const unsigned lcore_id = rte_lcore_id();
215 struct rte_mbuf *pkts_burst[PKT_BURST_SZ];
217 if (lcore_id == lcore_master) {
218 rte_delay_ms(KNI_TIMEOUT_MS);
219 /* tests of handling kernel request */
220 if (system(IFCONFIG TEST_KNI_PORT" up") == -1)
222 if (system(IFCONFIG TEST_KNI_PORT" mtu 1400") == -1)
224 if (system(IFCONFIG TEST_KNI_PORT" down") == -1)
226 rte_delay_ms(KNI_TIMEOUT_MS);
227 test_kni_processing_flag = 1;
228 } else if (lcore_id == lcore_ingress) {
229 struct rte_mempool *mp = test_kni_lookup_mempool();
235 if (test_kni_processing_flag)
238 for (nb_rx = 0; nb_rx < PKT_BURST_SZ; nb_rx++) {
239 pkts_burst[nb_rx] = rte_pktmbuf_alloc(mp);
240 if (!pkts_burst[nb_rx])
244 num = rte_kni_tx_burst(test_kni_ctx, pkts_burst,
246 stats.ingress += num;
247 rte_kni_handle_request(test_kni_ctx);
249 for (i = num; i < nb_rx; i++) {
250 rte_pktmbuf_free(pkts_burst[i]);
255 } else if (lcore_id == lcore_egress) {
257 if (test_kni_processing_flag)
259 num = rte_kni_rx_burst(test_kni_ctx, pkts_burst,
262 for (nb_tx = 0; nb_tx < num; nb_tx++)
263 rte_pktmbuf_free(pkts_burst[nb_tx]);
272 test_kni_allocate_lcores(void)
274 unsigned i, count = 0;
276 lcore_master = rte_get_master_lcore();
277 printf("master lcore: %u\n", lcore_master);
278 for (i = 0; i < RTE_MAX_LCORE; i++) {
281 if (rte_lcore_is_enabled(i) && i != lcore_master) {
289 printf("count: %u\n", count);
291 return count == 2 ? 0 : -1;
295 test_kni_register_handler_mp(void)
297 #define TEST_KNI_HANDLE_REQ_COUNT 10 /* 5s */
298 #define TEST_KNI_HANDLE_REQ_INTERVAL 500 /* ms */
299 #define TEST_KNI_MTU 1450
300 #define TEST_KNI_MTU_STR " 1450"
305 printf("Failed to fork a process\n");
307 } else if (pid == 0) {
309 struct rte_kni *kni = rte_kni_get(TEST_KNI_PORT);
310 struct rte_kni_ops ops = {
311 .change_mtu = kni_change_mtu,
312 .config_network_if = NULL,
313 .config_mac_address = NULL,
314 .config_promiscusity = NULL,
318 printf("Failed to get KNI named %s\n", TEST_KNI_PORT);
324 /* Check with the invalid parameters */
325 if (rte_kni_register_handlers(kni, NULL) == 0) {
326 printf("Unexpectedly register successuflly "
327 "with NULL ops pointer\n");
330 if (rte_kni_register_handlers(NULL, &ops) == 0) {
331 printf("Unexpectedly register successfully "
332 "to NULL KNI device pointer\n");
336 if (rte_kni_register_handlers(kni, &ops)) {
337 printf("Fail to register ops\n");
341 /* Check registering again after it has been registered */
342 if (rte_kni_register_handlers(kni, &ops) == 0) {
343 printf("Unexpectedly register successfully after "
344 "it has already been registered\n");
349 * Handle the request of setting MTU,
350 * with registered handlers.
352 for (i = 0; i < TEST_KNI_HANDLE_REQ_COUNT; i++) {
353 rte_kni_handle_request(kni);
354 if (kni_pkt_mtu == TEST_KNI_MTU)
356 rte_delay_ms(TEST_KNI_HANDLE_REQ_INTERVAL);
358 if (i >= TEST_KNI_HANDLE_REQ_COUNT) {
359 printf("MTU has not been set\n");
364 if (rte_kni_unregister_handlers(kni) < 0) {
365 printf("Fail to unregister ops\n");
369 /* Check with invalid parameter */
370 if (rte_kni_unregister_handlers(NULL) == 0) {
375 * Handle the request of setting MTU,
376 * without registered handlers.
378 for (i = 0; i < TEST_KNI_HANDLE_REQ_COUNT; i++) {
379 rte_kni_handle_request(kni);
380 if (kni_pkt_mtu != 0)
382 rte_delay_ms(TEST_KNI_HANDLE_REQ_INTERVAL);
384 if (kni_pkt_mtu != 0) {
385 printf("MTU shouldn't be set\n");
394 if (system(IFCONFIG TEST_KNI_PORT " mtu" TEST_KNI_MTU_STR)
399 if (system(IFCONFIG TEST_KNI_PORT " mtu" TEST_KNI_MTU_STR)
403 p_ret = wait(&status);
404 if (!WIFEXITED(status)) {
405 printf("Child process (%d) exit abnormally\n", p_ret);
408 if (WEXITSTATUS(status) != 0) {
409 printf("Child process exit with failure\n");
418 test_kni_processing(uint16_t port_id, struct rte_mempool *mp)
423 struct rte_kni_conf conf;
424 struct rte_eth_dev_info info;
425 struct rte_kni_ops ops;
426 const struct rte_pci_device *pci_dev;
427 const struct rte_bus *bus = NULL;
432 memset(&conf, 0, sizeof(conf));
433 memset(&info, 0, sizeof(info));
434 memset(&ops, 0, sizeof(ops));
436 rte_eth_dev_info_get(port_id, &info);
438 bus = rte_bus_find_by_device(info.device);
439 if (bus && !strcmp(bus->name, "pci")) {
440 pci_dev = RTE_DEV_TO_PCI(info.device);
441 conf.addr = pci_dev->addr;
442 conf.id = pci_dev->id;
444 snprintf(conf.name, sizeof(conf.name), TEST_KNI_PORT);
446 /* core id 1 configured for kernel thread */
449 conf.mbuf_size = MAX_PACKET_SZ;
450 conf.group_id = port_id;
453 ops.port_id = port_id;
455 /* basic test of kni processing */
456 kni = rte_kni_alloc(mp, &conf, &ops);
458 printf("fail to create kni\n");
463 test_kni_processing_flag = 0;
468 * Check multiple processes support on
469 * registerring/unregisterring handlers.
471 if (test_kni_register_handler_mp() < 0) {
472 printf("fail to check multiple process support\n");
477 ret = test_kni_link_change();
481 rte_eal_mp_remote_launch(test_kni_loop, NULL, CALL_MASTER);
482 RTE_LCORE_FOREACH_SLAVE(i) {
483 if (rte_eal_wait_lcore(i) < 0) {
489 * Check if the number of mbufs received from kernel space is equal
490 * to that of transmitted to kernel space
492 if (stats.ingress < KNI_NUM_MBUF_THRESHOLD ||
493 stats.egress < KNI_NUM_MBUF_THRESHOLD) {
494 printf("The ingress/egress number should not be "
495 "less than %u\n", (unsigned)KNI_NUM_MBUF_THRESHOLD);
500 if (rte_kni_release(kni) < 0) {
501 printf("fail to release kni\n");
506 /* test of reusing memzone */
507 kni = rte_kni_alloc(mp, &conf, &ops);
509 printf("fail to create kni\n");
513 /* Release the kni for following testing */
514 if (rte_kni_release(kni) < 0) {
515 printf("fail to release kni\n");
521 if (rte_kni_release(kni) < 0) {
522 printf("fail to release kni\n");
533 uint16_t nb_ports, port_id;
535 struct rte_mempool *mp;
536 struct rte_kni_conf conf;
537 struct rte_eth_dev_info info;
538 struct rte_kni_ops ops;
539 const struct rte_pci_device *pci_dev;
540 const struct rte_bus *bus;
542 /* Initialize KNI subsytem */
543 rte_kni_init(KNI_TEST_MAX_PORTS);
545 if (test_kni_allocate_lcores() < 0) {
546 printf("No enough lcores for kni processing\n");
550 mp = test_kni_create_mempool();
552 printf("fail to create mempool for kni\n");
556 nb_ports = rte_eth_dev_count_avail();
558 printf("no supported nic port found\n");
562 /* configuring port 0 for the test is enough */
564 ret = rte_eth_dev_configure(port_id, 1, 1, &port_conf);
566 printf("fail to configure port %d\n", port_id);
570 ret = rte_eth_rx_queue_setup(port_id, 0, NB_RXD, SOCKET, &rx_conf, mp);
572 printf("fail to setup rx queue for port %d\n", port_id);
576 ret = rte_eth_tx_queue_setup(port_id, 0, NB_TXD, SOCKET, &tx_conf);
578 printf("fail to setup tx queue for port %d\n", port_id);
582 ret = rte_eth_dev_start(port_id);
584 printf("fail to start port %d\n", port_id);
587 rte_eth_promiscuous_enable(port_id);
589 /* basic test of kni processing */
590 ret = test_kni_processing(port_id, mp);
594 /* test of allocating KNI with NULL mempool pointer */
595 memset(&info, 0, sizeof(info));
596 memset(&conf, 0, sizeof(conf));
597 memset(&ops, 0, sizeof(ops));
598 rte_eth_dev_info_get(port_id, &info);
600 bus = rte_bus_find_by_device(info.device);
603 if (bus && !strcmp(bus->name, "pci")) {
604 pci_dev = RTE_DEV_TO_PCI(info.device);
605 conf.addr = pci_dev->addr;
606 conf.id = pci_dev->id;
608 conf.group_id = port_id;
609 conf.mbuf_size = MAX_PACKET_SZ;
612 ops.port_id = port_id;
613 kni = rte_kni_alloc(NULL, &conf, &ops);
616 printf("unexpectedly creates kni successfully with NULL "
617 "mempool pointer\n");
621 /* test of allocating KNI without configurations */
622 kni = rte_kni_alloc(mp, NULL, NULL);
625 printf("Unexpectedly allocate KNI device successfully "
626 "without configurations\n");
630 /* test of allocating KNI without a name */
631 memset(&conf, 0, sizeof(conf));
632 memset(&info, 0, sizeof(info));
633 memset(&ops, 0, sizeof(ops));
634 rte_eth_dev_info_get(port_id, &info);
636 bus = rte_bus_find_by_device(info.device);
639 if (bus && !strcmp(bus->name, "pci")) {
640 pci_dev = RTE_DEV_TO_PCI(info.device);
641 conf.addr = pci_dev->addr;
642 conf.id = pci_dev->id;
644 conf.group_id = port_id;
645 conf.mbuf_size = MAX_PACKET_SZ;
648 ops.port_id = port_id;
649 kni = rte_kni_alloc(mp, &conf, &ops);
652 printf("Unexpectedly allocate a KNI device successfully "
657 /* test of releasing NULL kni context */
658 ret = rte_kni_release(NULL);
661 printf("unexpectedly release kni successfully\n");
665 /* test of handling request on NULL device pointer */
666 ret = rte_kni_handle_request(NULL);
669 printf("Unexpectedly handle request on NULL device pointer\n");
673 /* test of getting KNI device with pointer to NULL */
674 kni = rte_kni_get(NULL);
677 printf("Unexpectedly get a KNI device with "
678 "NULL name pointer\n");
682 /* test of getting KNI device with an zero length name string */
683 memset(&conf, 0, sizeof(conf));
684 kni = rte_kni_get(conf.name);
687 printf("Unexpectedly get a KNI device with "
688 "zero length name string\n");
692 /* test of getting KNI device with an invalid string name */
693 memset(&conf, 0, sizeof(conf));
694 snprintf(conf.name, sizeof(conf.name), "testing");
695 kni = rte_kni_get(conf.name);
698 printf("Unexpectedly get a KNI device with "
699 "a never used name string\n");
705 rte_eth_dev_stop(port_id);
712 REGISTER_TEST_COMMAND(kni_autotest, test_kni);