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.
42 #include <rte_string_fns.h>
43 #include <rte_mempool.h>
44 #include <rte_ethdev.h>
45 #include <rte_bus_pci.h>
46 #include <rte_cycles.h>
50 #define MAX_PACKET_SZ 2048
51 #define MBUF_DATA_SZ (MAX_PACKET_SZ + RTE_PKTMBUF_HEADROOM)
52 #define PKT_BURST_SZ 32
53 #define MEMPOOL_CACHE_SZ PKT_BURST_SZ
57 #define KNI_TIMEOUT_MS 5000 /* ms */
59 #define IFCONFIG "/sbin/ifconfig "
60 #define TEST_KNI_PORT "test_kni_port"
61 #define KNI_TEST_MAX_PORTS 4
62 /* The threshold number of mbufs to be transmitted or received. */
63 #define KNI_NUM_MBUF_THRESHOLD 100
64 static int kni_pkt_mtu = 0;
66 struct test_kni_stats {
67 volatile uint64_t ingress;
68 volatile uint64_t egress;
71 static const struct rte_eth_rxconf rx_conf = {
80 static const struct rte_eth_txconf tx_conf = {
90 static const struct rte_eth_conf port_conf = {
99 .mq_mode = ETH_DCB_NONE,
103 static struct rte_kni_ops kni_ops = {
105 .config_network_if = NULL,
108 static unsigned lcore_master, lcore_ingress, lcore_egress;
109 static struct rte_kni *test_kni_ctx;
110 static struct test_kni_stats stats;
112 static volatile uint32_t test_kni_processing_flag;
114 static struct rte_mempool *
115 test_kni_create_mempool(void)
117 struct rte_mempool * mp;
119 mp = rte_mempool_lookup("kni_mempool");
121 mp = rte_pktmbuf_pool_create("kni_mempool",
123 MEMPOOL_CACHE_SZ, 0, MBUF_DATA_SZ,
129 static struct rte_mempool *
130 test_kni_lookup_mempool(void)
132 return rte_mempool_lookup("kni_mempool");
134 /* Callback for request of changing MTU */
136 kni_change_mtu(uint16_t port_id, unsigned int new_mtu)
138 printf("Change MTU of port %d to %u\n", port_id, new_mtu);
139 kni_pkt_mtu = new_mtu;
140 printf("Change MTU of port %d to %i successfully.\n",
141 port_id, kni_pkt_mtu);
145 * This loop fully tests the basic functions of KNI. e.g. transmitting,
146 * receiving to, from kernel space, and kernel requests.
148 * This is the loop to transmit/receive mbufs to/from kernel interface with
149 * supported by KNI kernel module. The ingress lcore will allocate mbufs and
150 * transmit them to kernel space; while the egress lcore will receive the mbufs
151 * from kernel space and free them.
152 * On the master lcore, several commands will be run to check handling the
153 * kernel requests. And it will finally set the flag to exit the KNI
154 * transmitting/receiving to/from the kernel space.
156 * Note: To support this testing, the KNI kernel module needs to be insmodded
157 * in one of its loopback modes.
160 test_kni_loop(__rte_unused void *arg)
163 unsigned nb_rx, nb_tx, num, i;
164 const unsigned lcore_id = rte_lcore_id();
165 struct rte_mbuf *pkts_burst[PKT_BURST_SZ];
167 if (lcore_id == lcore_master) {
168 rte_delay_ms(KNI_TIMEOUT_MS);
169 /* tests of handling kernel request */
170 if (system(IFCONFIG TEST_KNI_PORT" up") == -1)
172 if (system(IFCONFIG TEST_KNI_PORT" mtu 1400") == -1)
174 if (system(IFCONFIG TEST_KNI_PORT" down") == -1)
176 rte_delay_ms(KNI_TIMEOUT_MS);
177 test_kni_processing_flag = 1;
178 } else if (lcore_id == lcore_ingress) {
179 struct rte_mempool *mp = test_kni_lookup_mempool();
185 if (test_kni_processing_flag)
188 for (nb_rx = 0; nb_rx < PKT_BURST_SZ; nb_rx++) {
189 pkts_burst[nb_rx] = rte_pktmbuf_alloc(mp);
190 if (!pkts_burst[nb_rx])
194 num = rte_kni_tx_burst(test_kni_ctx, pkts_burst,
196 stats.ingress += num;
197 rte_kni_handle_request(test_kni_ctx);
199 for (i = num; i < nb_rx; i++) {
200 rte_pktmbuf_free(pkts_burst[i]);
205 } else if (lcore_id == lcore_egress) {
207 if (test_kni_processing_flag)
209 num = rte_kni_rx_burst(test_kni_ctx, pkts_burst,
212 for (nb_tx = 0; nb_tx < num; nb_tx++)
213 rte_pktmbuf_free(pkts_burst[nb_tx]);
222 test_kni_allocate_lcores(void)
224 unsigned i, count = 0;
226 lcore_master = rte_get_master_lcore();
227 printf("master lcore: %u\n", lcore_master);
228 for (i = 0; i < RTE_MAX_LCORE; i++) {
231 if (rte_lcore_is_enabled(i) && i != lcore_master) {
239 printf("count: %u\n", count);
241 return count == 2 ? 0 : -1;
245 test_kni_register_handler_mp(void)
247 #define TEST_KNI_HANDLE_REQ_COUNT 10 /* 5s */
248 #define TEST_KNI_HANDLE_REQ_INTERVAL 500 /* ms */
249 #define TEST_KNI_MTU 1450
250 #define TEST_KNI_MTU_STR " 1450"
255 printf("Failed to fork a process\n");
257 } else if (pid == 0) {
259 struct rte_kni *kni = rte_kni_get(TEST_KNI_PORT);
260 struct rte_kni_ops ops = {
261 .change_mtu = kni_change_mtu,
262 .config_network_if = NULL,
266 printf("Failed to get KNI named %s\n", TEST_KNI_PORT);
272 /* Check with the invalid parameters */
273 if (rte_kni_register_handlers(kni, NULL) == 0) {
274 printf("Unexpectedly register successuflly "
275 "with NULL ops pointer\n");
278 if (rte_kni_register_handlers(NULL, &ops) == 0) {
279 printf("Unexpectedly register successfully "
280 "to NULL KNI device pointer\n");
284 if (rte_kni_register_handlers(kni, &ops)) {
285 printf("Fail to register ops\n");
289 /* Check registering again after it has been registered */
290 if (rte_kni_register_handlers(kni, &ops) == 0) {
291 printf("Unexpectedly register successfully after "
292 "it has already been registered\n");
297 * Handle the request of setting MTU,
298 * with registered handlers.
300 for (i = 0; i < TEST_KNI_HANDLE_REQ_COUNT; i++) {
301 rte_kni_handle_request(kni);
302 if (kni_pkt_mtu == TEST_KNI_MTU)
304 rte_delay_ms(TEST_KNI_HANDLE_REQ_INTERVAL);
306 if (i >= TEST_KNI_HANDLE_REQ_COUNT) {
307 printf("MTU has not been set\n");
312 if (rte_kni_unregister_handlers(kni) < 0) {
313 printf("Fail to unregister ops\n");
317 /* Check with invalid parameter */
318 if (rte_kni_unregister_handlers(NULL) == 0) {
323 * Handle the request of setting MTU,
324 * without registered handlers.
326 for (i = 0; i < TEST_KNI_HANDLE_REQ_COUNT; i++) {
327 rte_kni_handle_request(kni);
328 if (kni_pkt_mtu != 0)
330 rte_delay_ms(TEST_KNI_HANDLE_REQ_INTERVAL);
332 if (kni_pkt_mtu != 0) {
333 printf("MTU shouldn't be set\n");
342 if (system(IFCONFIG TEST_KNI_PORT " mtu" TEST_KNI_MTU_STR)
347 if (system(IFCONFIG TEST_KNI_PORT " mtu" TEST_KNI_MTU_STR)
351 p_ret = wait(&status);
352 if (!WIFEXITED(status)) {
353 printf("Child process (%d) exit abnormally\n", p_ret);
356 if (WEXITSTATUS(status) != 0) {
357 printf("Child process exit with failure\n");
366 test_kni_processing(uint16_t port_id, struct rte_mempool *mp)
371 struct rte_kni_conf conf;
372 struct rte_eth_dev_info info;
373 struct rte_kni_ops ops;
378 memset(&conf, 0, sizeof(conf));
379 memset(&info, 0, sizeof(info));
380 memset(&ops, 0, sizeof(ops));
382 rte_eth_dev_info_get(port_id, &info);
383 conf.addr = info.pci_dev->addr;
384 conf.id = info.pci_dev->id;
385 snprintf(conf.name, sizeof(conf.name), TEST_KNI_PORT);
387 /* core id 1 configured for kernel thread */
390 conf.mbuf_size = MAX_PACKET_SZ;
391 conf.group_id = port_id;
394 ops.port_id = port_id;
396 /* basic test of kni processing */
397 kni = rte_kni_alloc(mp, &conf, &ops);
399 printf("fail to create kni\n");
404 test_kni_processing_flag = 0;
409 * Check multiple processes support on
410 * registerring/unregisterring handlers.
412 if (test_kni_register_handler_mp() < 0) {
413 printf("fail to check multiple process support\n");
418 rte_eal_mp_remote_launch(test_kni_loop, NULL, CALL_MASTER);
419 RTE_LCORE_FOREACH_SLAVE(i) {
420 if (rte_eal_wait_lcore(i) < 0) {
426 * Check if the number of mbufs received from kernel space is equal
427 * to that of transmitted to kernel space
429 if (stats.ingress < KNI_NUM_MBUF_THRESHOLD ||
430 stats.egress < KNI_NUM_MBUF_THRESHOLD) {
431 printf("The ingress/egress number should not be "
432 "less than %u\n", (unsigned)KNI_NUM_MBUF_THRESHOLD);
437 if (rte_kni_release(kni) < 0) {
438 printf("fail to release kni\n");
443 /* test of releasing a released kni device */
444 if (rte_kni_release(kni) == 0) {
445 printf("should not release a released kni device\n");
449 /* test of reusing memzone */
450 kni = rte_kni_alloc(mp, &conf, &ops);
452 printf("fail to create kni\n");
456 /* Release the kni for following testing */
457 if (rte_kni_release(kni) < 0) {
458 printf("fail to release kni\n");
464 if (rte_kni_release(kni) < 0) {
465 printf("fail to release kni\n");
476 uint16_t nb_ports, port_id;
478 struct rte_mempool *mp;
479 struct rte_kni_conf conf;
480 struct rte_eth_dev_info info;
481 struct rte_kni_ops ops;
483 /* Initialize KNI subsytem */
484 rte_kni_init(KNI_TEST_MAX_PORTS);
486 if (test_kni_allocate_lcores() < 0) {
487 printf("No enough lcores for kni processing\n");
491 mp = test_kni_create_mempool();
493 printf("fail to create mempool for kni\n");
497 nb_ports = rte_eth_dev_count();
499 printf("no supported nic port found\n");
503 /* configuring port 0 for the test is enough */
505 ret = rte_eth_dev_configure(port_id, 1, 1, &port_conf);
507 printf("fail to configure port %d\n", port_id);
511 ret = rte_eth_rx_queue_setup(port_id, 0, NB_RXD, SOCKET, &rx_conf, mp);
513 printf("fail to setup rx queue for port %d\n", port_id);
517 ret = rte_eth_tx_queue_setup(port_id, 0, NB_TXD, SOCKET, &tx_conf);
519 printf("fail to setup tx queue for port %d\n", port_id);
523 ret = rte_eth_dev_start(port_id);
525 printf("fail to start port %d\n", port_id);
528 rte_eth_promiscuous_enable(port_id);
530 /* basic test of kni processing */
531 ret = test_kni_processing(port_id, mp);
535 /* test of allocating KNI with NULL mempool pointer */
536 memset(&info, 0, sizeof(info));
537 memset(&conf, 0, sizeof(conf));
538 memset(&ops, 0, sizeof(ops));
539 rte_eth_dev_info_get(port_id, &info);
540 conf.addr = info.pci_dev->addr;
541 conf.id = info.pci_dev->id;
542 conf.group_id = port_id;
543 conf.mbuf_size = MAX_PACKET_SZ;
546 ops.port_id = port_id;
547 kni = rte_kni_alloc(NULL, &conf, &ops);
550 printf("unexpectedly creates kni successfully with NULL "
551 "mempool pointer\n");
555 /* test of allocating KNI without configurations */
556 kni = rte_kni_alloc(mp, NULL, NULL);
559 printf("Unexpectedly allocate KNI device successfully "
560 "without configurations\n");
564 /* test of allocating KNI without a name */
565 memset(&conf, 0, sizeof(conf));
566 memset(&info, 0, sizeof(info));
567 memset(&ops, 0, sizeof(ops));
568 rte_eth_dev_info_get(port_id, &info);
569 conf.addr = info.pci_dev->addr;
570 conf.id = info.pci_dev->id;
571 conf.group_id = port_id;
572 conf.mbuf_size = MAX_PACKET_SZ;
575 ops.port_id = port_id;
576 kni = rte_kni_alloc(mp, &conf, &ops);
579 printf("Unexpectedly allocate a KNI device successfully "
584 /* test of releasing NULL kni context */
585 ret = rte_kni_release(NULL);
588 printf("unexpectedly release kni successfully\n");
592 /* test of handling request on NULL device pointer */
593 ret = rte_kni_handle_request(NULL);
596 printf("Unexpectedly handle request on NULL device pointer\n");
600 /* test of getting KNI device with pointer to NULL */
601 kni = rte_kni_get(NULL);
604 printf("Unexpectedly get a KNI device with "
605 "NULL name pointer\n");
609 /* test of getting KNI device with an zero length name string */
610 memset(&conf, 0, sizeof(conf));
611 kni = rte_kni_get(conf.name);
614 printf("Unexpectedly get a KNI device with "
615 "zero length name string\n");
619 /* test of getting KNI device with an invalid string name */
620 memset(&conf, 0, sizeof(conf));
621 snprintf(conf.name, sizeof(conf.name), "testing");
622 kni = rte_kni_get(conf.name);
625 printf("Unexpectedly get a KNI device with "
626 "a never used name string\n");
632 rte_eth_dev_stop(port_id);
637 REGISTER_TEST_COMMAND(kni_autotest, test_kni);