4 * Copyright(c) 2010-2014 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.
43 #include <rte_string_fns.h>
44 #include <rte_mempool.h>
45 #include <rte_ethdev.h>
46 #include <rte_cycles.h>
50 #define MAX_PACKET_SZ 2048
52 (MAX_PACKET_SZ + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
53 #define PKT_BURST_SZ 32
54 #define MEMPOOL_CACHE_SZ PKT_BURST_SZ
58 #define KNI_TIMEOUT_MS 5000 /* ms */
60 #define IFCONFIG "/sbin/ifconfig "
61 #define TEST_KNI_PORT "test_kni_port"
63 /* The threshold number of mbufs to be transmitted or received. */
64 #define KNI_NUM_MBUF_THRESHOLD 100
65 static int kni_pkt_mtu = 0;
67 struct test_kni_stats {
68 volatile uint64_t ingress;
69 volatile uint64_t egress;
72 static const struct rte_eth_rxconf rx_conf = {
81 static const struct rte_eth_txconf tx_conf = {
91 static const struct rte_eth_conf port_conf = {
100 .mq_mode = ETH_DCB_NONE,
104 static struct rte_kni_ops kni_ops = {
106 .config_network_if = NULL,
109 static unsigned lcore_master, lcore_ingress, lcore_egress;
110 static struct rte_kni *test_kni_ctx;
111 static struct test_kni_stats stats;
113 static volatile uint32_t test_kni_processing_flag;
115 static struct rte_mempool *
116 test_kni_create_mempool(void)
118 struct rte_mempool * mp;
120 mp = rte_mempool_lookup("kni_mempool");
122 mp = rte_mempool_create("kni_mempool",
126 sizeof(struct rte_pktmbuf_pool_private),
127 rte_pktmbuf_pool_init,
137 static struct rte_mempool *
138 test_kni_lookup_mempool(void)
140 return rte_mempool_lookup("kni_mempool");
142 /* Callback for request of changing MTU */
144 kni_change_mtu(uint8_t port_id, unsigned new_mtu)
146 printf("Change MTU of port %d to %u\n", port_id, new_mtu);
147 kni_pkt_mtu = new_mtu;
148 printf("Change MTU of port %d to %i successfully.\n",
149 port_id, kni_pkt_mtu);
153 * This loop fully tests the basic functions of KNI. e.g. transmitting,
154 * receiving to, from kernel space, and kernel requests.
156 * This is the loop to transmit/receive mbufs to/from kernel interface with
157 * supported by KNI kernel module. The ingress lcore will allocate mbufs and
158 * transmit them to kernel space; while the egress lcore will receive the mbufs
159 * from kernel space and free them.
160 * On the master lcore, several commands will be run to check handling the
161 * kernel requests. And it will finally set the flag to exit the KNI
162 * transmitting/receiving to/from the kernel space.
164 * Note: To support this testing, the KNI kernel module needs to be insmodded
165 * in one of its loopback modes.
168 test_kni_loop(__rte_unused void *arg)
171 unsigned nb_rx, nb_tx, num, i;
172 const unsigned lcore_id = rte_lcore_id();
173 struct rte_mbuf *pkts_burst[PKT_BURST_SZ];
175 if (lcore_id == lcore_master) {
176 rte_delay_ms(KNI_TIMEOUT_MS);
177 /* tests of handling kernel request */
178 if (system(IFCONFIG TEST_KNI_PORT" up") == -1)
180 if (system(IFCONFIG TEST_KNI_PORT" mtu 1400") == -1)
182 if (system(IFCONFIG TEST_KNI_PORT" down") == -1)
184 rte_delay_ms(KNI_TIMEOUT_MS);
185 test_kni_processing_flag = 1;
186 } else if (lcore_id == lcore_ingress) {
187 struct rte_mempool *mp = test_kni_lookup_mempool();
193 if (test_kni_processing_flag)
196 for (nb_rx = 0; nb_rx < PKT_BURST_SZ; nb_rx++) {
197 pkts_burst[nb_rx] = rte_pktmbuf_alloc(mp);
198 if (!pkts_burst[nb_rx])
202 num = rte_kni_tx_burst(test_kni_ctx, pkts_burst,
204 stats.ingress += num;
205 rte_kni_handle_request(test_kni_ctx);
207 for (i = num; i < nb_rx; i++) {
208 rte_pktmbuf_free(pkts_burst[i]);
213 } else if (lcore_id == lcore_egress) {
215 if (test_kni_processing_flag)
217 num = rte_kni_rx_burst(test_kni_ctx, pkts_burst,
220 for (nb_tx = 0; nb_tx < num; nb_tx++)
221 rte_pktmbuf_free(pkts_burst[nb_tx]);
230 test_kni_allocate_lcores(void)
232 unsigned i, count = 0;
234 lcore_master = rte_get_master_lcore();
235 printf("master lcore: %u\n", lcore_master);
236 for (i = 0; i < RTE_MAX_LCORE; i++) {
239 if (rte_lcore_is_enabled(i) && i != lcore_master) {
247 printf("count: %u\n", count);
249 return (count == 2 ? 0 : -1);
253 test_kni_register_handler_mp(void)
255 #define TEST_KNI_HANDLE_REQ_COUNT 10 /* 5s */
256 #define TEST_KNI_HANDLE_REQ_INTERVAL 500 /* ms */
257 #define TEST_KNI_MTU 1450
258 #define TEST_KNI_MTU_STR " 1450"
263 printf("Failed to fork a process\n");
265 } else if (pid == 0) {
267 struct rte_kni *kni = rte_kni_get(TEST_KNI_PORT);
268 struct rte_kni_ops ops = {
269 .change_mtu = kni_change_mtu,
270 .config_network_if = NULL,
274 printf("Failed to get KNI named %s\n", TEST_KNI_PORT);
280 /* Check with the invalid parameters */
281 if (rte_kni_register_handlers(kni, NULL) == 0) {
282 printf("Unexpectedly register successuflly "
283 "with NULL ops pointer\n");
286 if (rte_kni_register_handlers(NULL, &ops) == 0) {
287 printf("Unexpectedly register successfully "
288 "to NULL KNI device pointer\n");
292 if (rte_kni_register_handlers(kni, &ops)) {
293 printf("Fail to register ops\n");
297 /* Check registering again after it has been registered */
298 if (rte_kni_register_handlers(kni, &ops) == 0) {
299 printf("Unexpectedly register successfully after "
300 "it has already been registered\n");
305 * Handle the request of setting MTU,
306 * with registered handlers.
308 for (i = 0; i < TEST_KNI_HANDLE_REQ_COUNT; i++) {
309 rte_kni_handle_request(kni);
310 if (kni_pkt_mtu == TEST_KNI_MTU)
312 rte_delay_ms(TEST_KNI_HANDLE_REQ_INTERVAL);
314 if (i >= TEST_KNI_HANDLE_REQ_COUNT) {
315 printf("MTU has not been set\n");
320 if (rte_kni_unregister_handlers(kni) < 0) {
321 printf("Fail to unregister ops\n");
325 /* Check with invalid parameter */
326 if (rte_kni_unregister_handlers(NULL) == 0) {
331 * Handle the request of setting MTU,
332 * without registered handlers.
334 for (i = 0; i < TEST_KNI_HANDLE_REQ_COUNT; i++) {
335 rte_kni_handle_request(kni);
336 if (kni_pkt_mtu != 0)
338 rte_delay_ms(TEST_KNI_HANDLE_REQ_INTERVAL);
340 if (kni_pkt_mtu != 0) {
341 printf("MTU shouldn't be set\n");
350 if (system(IFCONFIG TEST_KNI_PORT " mtu" TEST_KNI_MTU_STR)
355 if (system(IFCONFIG TEST_KNI_PORT " mtu" TEST_KNI_MTU_STR)
359 p_ret = wait(&status);
360 if (!WIFEXITED(status)) {
361 printf("Child process (%d) exit abnormally\n", p_ret);
364 if (WEXITSTATUS(status) != 0) {
365 printf("Child process exit with failure\n");
374 test_kni_processing(uint8_t port_id, struct rte_mempool *mp)
379 struct rte_kni_conf conf;
380 struct rte_eth_dev_info info;
381 struct rte_kni_ops ops;
386 memset(&conf, 0, sizeof(conf));
387 memset(&info, 0, sizeof(info));
388 memset(&ops, 0, sizeof(ops));
390 rte_eth_dev_info_get(port_id, &info);
391 conf.addr = info.pci_dev->addr;
392 conf.id = info.pci_dev->id;
393 rte_snprintf(conf.name, sizeof(conf.name), TEST_KNI_PORT);
395 /* core id 1 configured for kernel thread */
398 conf.mbuf_size = MAX_PACKET_SZ;
399 conf.group_id = (uint16_t)port_id;
402 ops.port_id = port_id;
404 /* basic test of kni processing */
405 kni = rte_kni_alloc(mp, &conf, &ops);
407 printf("fail to create kni\n");
410 if (rte_kni_get_port_id(kni) != port_id) {
411 printf("fail to get port id\n");
416 if (rte_kni_info_get(RTE_MAX_ETHPORTS)) {
417 printf("Unexpectedly get a KNI successfully\n");
423 test_kni_processing_flag = 0;
428 * Check multiple processes support on
429 * registerring/unregisterring handlers.
431 if (test_kni_register_handler_mp() < 0) {
432 printf("fail to check multiple process support\n");
437 rte_eal_mp_remote_launch(test_kni_loop, NULL, CALL_MASTER);
438 RTE_LCORE_FOREACH_SLAVE(i) {
439 if (rte_eal_wait_lcore(i) < 0) {
445 * Check if the number of mbufs received from kernel space is equal
446 * to that of transmitted to kernel space
448 if (stats.ingress < KNI_NUM_MBUF_THRESHOLD ||
449 stats.egress < KNI_NUM_MBUF_THRESHOLD) {
450 printf("The ingress/egress number should not be "
451 "less than %u\n", (unsigned)KNI_NUM_MBUF_THRESHOLD);
456 if (rte_kni_release(kni) < 0) {
457 printf("fail to release kni\n");
462 /* test of releasing a released kni device */
463 if (rte_kni_release(kni) == 0) {
464 printf("should not release a released kni device\n");
468 /* test of reusing memzone */
469 kni = rte_kni_alloc(mp, &conf, &ops);
471 printf("fail to create kni\n");
475 /* Release the kni for following testing */
476 if (rte_kni_release(kni) < 0) {
477 printf("fail to release kni\n");
483 if (rte_kni_release(kni) < 0) {
484 printf("fail to release kni\n");
495 uint8_t nb_ports, port_id;
497 struct rte_mempool *mp;
498 struct rte_kni_conf conf;
499 struct rte_eth_dev_info info;
500 struct rte_kni_ops ops;
502 if (test_kni_allocate_lcores() < 0) {
503 printf("No enough lcores for kni processing\n");
507 mp = test_kni_create_mempool();
509 printf("fail to create mempool for kni\n");
512 ret = rte_eal_pci_probe();
514 printf("fail to probe PCI devices\n");
518 nb_ports = rte_eth_dev_count();
520 printf("no supported nic port found\n");
524 /* configuring port 0 for the test is enough */
526 ret = rte_eth_dev_configure(port_id, 1, 1, &port_conf);
528 printf("fail to configure port %d\n", port_id);
532 ret = rte_eth_rx_queue_setup(port_id, 0, NB_RXD, SOCKET, &rx_conf, mp);
534 printf("fail to setup rx queue for port %d\n", port_id);
538 ret = rte_eth_tx_queue_setup(port_id, 0, NB_TXD, SOCKET, &tx_conf);
540 printf("fail to setup tx queue for port %d\n", port_id);
544 ret = rte_eth_dev_start(port_id);
546 printf("fail to start port %d\n", port_id);
549 rte_eth_promiscuous_enable(port_id);
551 /* basic test of kni processing */
552 ret = test_kni_processing(port_id, mp);
556 /* test of allocating KNI with NULL mempool pointer */
557 memset(&info, 0, sizeof(info));
558 memset(&conf, 0, sizeof(conf));
559 memset(&ops, 0, sizeof(ops));
560 rte_eth_dev_info_get(port_id, &info);
561 conf.addr = info.pci_dev->addr;
562 conf.id = info.pci_dev->id;
563 conf.group_id = (uint16_t)port_id;
564 conf.mbuf_size = MAX_PACKET_SZ;
567 ops.port_id = port_id;
568 kni = rte_kni_alloc(NULL, &conf, &ops);
571 printf("unexpectedly creates kni successfully with NULL "
572 "mempool pointer\n");
576 /* test of allocating KNI without configurations */
577 kni = rte_kni_alloc(mp, NULL, NULL);
580 printf("Unexpectedly allocate KNI device successfully "
581 "without configurations\n");
585 /* test of allocating KNI without a name */
586 memset(&conf, 0, sizeof(conf));
587 memset(&info, 0, sizeof(info));
588 memset(&ops, 0, sizeof(ops));
589 rte_eth_dev_info_get(port_id, &info);
590 conf.addr = info.pci_dev->addr;
591 conf.id = info.pci_dev->id;
592 conf.group_id = (uint16_t)port_id;
593 conf.mbuf_size = MAX_PACKET_SZ;
596 ops.port_id = port_id;
597 kni = rte_kni_alloc(mp, &conf, &ops);
600 printf("Unexpectedly allocate a KNI device successfully "
605 /* test of getting port id according to NULL kni context */
606 if (rte_kni_get_port_id(NULL) < RTE_MAX_ETHPORTS) {
608 printf("unexpectedly get port id successfully by NULL kni "
613 /* test of releasing NULL kni context */
614 ret = rte_kni_release(NULL);
617 printf("unexpectedly release kni successfully\n");
621 /* test of handling request on NULL device pointer */
622 ret = rte_kni_handle_request(NULL);
625 printf("Unexpectedly handle request on NULL device pointer\n");
629 /* test of getting KNI device with pointer to NULL */
630 kni = rte_kni_get(NULL);
633 printf("Unexpectedly get a KNI device with "
634 "NULL name pointer\n");
638 /* test of getting KNI device with an zero length name string */
639 memset(&conf, 0, sizeof(conf));
640 kni = rte_kni_get(conf.name);
643 printf("Unexpectedly get a KNI device with "
644 "zero length name string\n");
648 /* test of getting KNI device with an invalid string name */
649 memset(&conf, 0, sizeof(conf));
650 rte_snprintf(conf.name, sizeof(conf.name), "testing");
651 kni = rte_kni_get(conf.name);
654 printf("Unexpectedly get a KNI device with "
655 "a never used name string\n");
659 /* test the interface of creating a KNI, for backward compatibility */
660 memset(&ops, 0, sizeof(ops));
662 kni = rte_kni_create(port_id, MAX_PACKET_SZ, mp, &ops);
665 printf("Fail to create a KNI device for port %d\n", port_id);
669 ret = rte_kni_release(kni);
671 printf("Fail to release a KNI device\n");
678 rte_eth_dev_stop(port_id);
683 #else /* RTE_LIBRTE_KNI */
688 printf("The KNI library is not included in this build\n");
692 #endif /* RTE_LIBRTE_KNI */