update Intel copyright years to 2014
[dpdk.git] / app / test / test_kni.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
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
16  *       distribution.
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.
20  * 
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.
32  */
33
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <sys/wait.h>
39
40 #include <cmdline_parse.h>
41
42 #include "test.h"
43
44 #ifdef RTE_LIBRTE_KNI
45 #include <rte_string_fns.h>
46 #include <rte_mempool.h>
47 #include <rte_ethdev.h>
48 #include <rte_cycles.h>
49 #include <rte_kni.h>
50
51 #define NB_MBUF          8192
52 #define MAX_PACKET_SZ    2048
53 #define MBUF_SZ \
54         (MAX_PACKET_SZ + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
55 #define PKT_BURST_SZ     32
56 #define MEMPOOL_CACHE_SZ PKT_BURST_SZ
57 #define SOCKET           0
58 #define NB_RXD           128
59 #define NB_TXD           512
60 #define KNI_TIMEOUT_MS   5000 /* ms */
61
62 #define IFCONFIG      "/sbin/ifconfig "
63 #define TEST_KNI_PORT "test_kni_port"
64
65 /* The threshold number of mbufs to be transmitted or received. */
66 #define KNI_NUM_MBUF_THRESHOLD 100
67 static int kni_pkt_mtu = 0;
68
69 struct test_kni_stats {
70         volatile uint64_t ingress;
71         volatile uint64_t egress;
72 };
73
74 static const struct rte_eth_rxconf rx_conf = {
75         .rx_thresh = {
76                 .pthresh = 8,
77                 .hthresh = 8,
78                 .wthresh = 4,
79         },
80         .rx_free_thresh = 0,
81 };
82
83 static const struct rte_eth_txconf tx_conf = {
84         .tx_thresh = {
85                 .pthresh = 36,
86                 .hthresh = 0,
87                 .wthresh = 0,
88         },
89         .tx_free_thresh = 0,
90         .tx_rs_thresh = 0,
91 };
92
93 static const struct rte_eth_conf port_conf = {
94         .rxmode = {
95                 .header_split = 0,
96                 .hw_ip_checksum = 0,
97                 .hw_vlan_filter = 0,
98                 .jumbo_frame = 0,
99                 .hw_strip_crc = 0,
100         },
101         .txmode = {
102                 .mq_mode = ETH_DCB_NONE,
103         },
104 };
105
106 static struct rte_kni_ops kni_ops = {
107         .change_mtu = NULL,
108         .config_network_if = NULL,
109 };
110
111 static unsigned lcore_master, lcore_ingress, lcore_egress;
112 static struct rte_kni *test_kni_ctx;
113 static struct test_kni_stats stats;
114
115 static volatile uint32_t test_kni_processing_flag;
116
117 static struct rte_mempool *
118 test_kni_create_mempool(void)
119 {
120         struct rte_mempool * mp;
121
122         mp = rte_mempool_lookup("kni_mempool");
123         if (!mp)
124                 mp = rte_mempool_create("kni_mempool",
125                                 NB_MBUF,
126                                 MBUF_SZ,
127                                 MEMPOOL_CACHE_SZ,
128                                 sizeof(struct rte_pktmbuf_pool_private),
129                                 rte_pktmbuf_pool_init,
130                                 NULL,
131                                 rte_pktmbuf_init,
132                                 NULL,
133                                 SOCKET,
134                                 0);
135
136         return mp;
137 }
138
139 static struct rte_mempool *
140 test_kni_lookup_mempool(void)
141 {
142         return rte_mempool_lookup("kni_mempool");
143 }
144 /* Callback for request of changing MTU */
145 static int
146 kni_change_mtu(uint8_t port_id, unsigned new_mtu)
147 {
148         printf("Change MTU of port %d to %u\n", port_id, new_mtu);
149         kni_pkt_mtu = new_mtu;  
150         printf("Change MTU of port %d to %i successfully.\n",
151                                          port_id, kni_pkt_mtu);
152         return 0;
153 }
154 /**
155  * This loop fully tests the basic functions of KNI. e.g. transmitting,
156  * receiving to, from kernel space, and kernel requests.
157  *
158  * This is the loop to transmit/receive mbufs to/from kernel interface with
159  * supported by KNI kernel module. The ingress lcore will allocate mbufs and
160  * transmit them to kernel space; while the egress lcore will receive the mbufs
161  * from kernel space and free them.
162  * On the master lcore, several commands will be run to check handling the
163  * kernel requests. And it will finally set the flag to exit the KNI
164  * transmitting/receiving to/from the kernel space.
165  *
166  * Note: To support this testing, the KNI kernel module needs to be insmodded
167  * in one of its loopback modes.
168  */
169 static int
170 test_kni_loop(__rte_unused void *arg)
171 {
172         int ret = 0;
173         unsigned nb_rx, nb_tx, num, i;
174         const unsigned lcore_id = rte_lcore_id();
175         struct rte_mbuf *pkts_burst[PKT_BURST_SZ];
176
177         if (lcore_id == lcore_master) {
178                 rte_delay_ms(KNI_TIMEOUT_MS);
179                 /* tests of handling kernel request */
180                 if (system(IFCONFIG TEST_KNI_PORT" up") == -1)
181                         ret = -1;
182                 if (system(IFCONFIG TEST_KNI_PORT" mtu 1400") == -1)
183                         ret = -1;
184                 if (system(IFCONFIG TEST_KNI_PORT" down") == -1)
185                         ret = -1;
186                 rte_delay_ms(KNI_TIMEOUT_MS);
187                 test_kni_processing_flag = 1;
188         } else if (lcore_id == lcore_ingress) {
189                 struct rte_mempool *mp = test_kni_lookup_mempool();
190
191                 if (mp == NULL)
192                         return -1;
193
194                 while (1) {
195                         if (test_kni_processing_flag)
196                                 break;
197
198                         for (nb_rx = 0; nb_rx < PKT_BURST_SZ; nb_rx++) {
199                                 pkts_burst[nb_rx] = rte_pktmbuf_alloc(mp);
200                                 if (!pkts_burst[nb_rx])
201                                         break;
202                         }
203
204                         num = rte_kni_tx_burst(test_kni_ctx, pkts_burst,
205                                                                 nb_rx);
206                         stats.ingress += num;
207                         rte_kni_handle_request(test_kni_ctx);
208                         if (num < nb_rx) {
209                                 for (i = num; i < nb_rx; i++) {
210                                         rte_pktmbuf_free(pkts_burst[i]);
211                                 }
212                         }
213                 }
214         } else if (lcore_id == lcore_egress) {
215                 while (1) {
216                         if (test_kni_processing_flag)
217                                 break;
218                         num = rte_kni_rx_burst(test_kni_ctx, pkts_burst,
219                                                         PKT_BURST_SZ);
220                         stats.egress += num;
221                         for (nb_tx = 0; nb_tx < num; nb_tx++)
222                                 rte_pktmbuf_free(pkts_burst[nb_tx]);
223                 }
224         }
225
226         return ret;
227 }
228
229 static int
230 test_kni_allocate_lcores(void)
231 {
232         unsigned i, count = 0;
233
234         lcore_master = rte_get_master_lcore();
235         printf("master lcore: %u\n", lcore_master);
236         for (i = 0; i < RTE_MAX_LCORE; i++) {
237                 if (count >=2 )
238                         break;
239                 if (rte_lcore_is_enabled(i) && i != lcore_master) {
240                         count ++;
241                         if (count == 1)
242                                 lcore_ingress = i;
243                         else if (count == 2)
244                                 lcore_egress = i;
245                 }
246         }
247         printf("count: %u\n", count);
248
249         return (count == 2 ? 0 : -1);
250 }
251
252 static int
253 test_kni_register_handler_mp(void)
254 {
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"
259         int pid;
260
261         pid = fork();
262         if (pid < 0) {
263                 printf("Failed to fork a process\n");
264                 return -1;
265         } else if (pid == 0) {
266                 int i;
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,
271                 };
272
273                 if (!kni) {
274                         printf("Failed to get KNI named %s\n", TEST_KNI_PORT);
275                         exit(-1);
276                 }
277
278                 kni_pkt_mtu = 0;
279
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");
284                         exit(-1);
285                 }
286                 if (rte_kni_register_handlers(NULL, &ops) == 0) {
287                         printf("Unexpectedly register successfully "
288                                         "to NULL KNI device pointer\n");
289                         exit(-1);
290                 }
291
292                 if (rte_kni_register_handlers(kni, &ops)) {
293                         printf("Fail to register ops\n");
294                         exit(-1);
295                 }
296
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");
301                         exit(-1);
302                 }
303
304                 /**
305                  * Handle the request of setting MTU,
306                  * with registered handlers.
307                  */
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)
311                                 break;
312                         rte_delay_ms(TEST_KNI_HANDLE_REQ_INTERVAL);
313                 }
314                 if (i >= TEST_KNI_HANDLE_REQ_COUNT) {
315                         printf("MTU has not been set\n");
316                         exit(-1);
317                 }
318
319                 kni_pkt_mtu = 0;
320                 if (rte_kni_unregister_handlers(kni) < 0) {
321                         printf("Fail to unregister ops\n");
322                         exit(-1);
323                 }
324
325                 /* Check with invalid parameter */
326                 if (rte_kni_unregister_handlers(NULL) == 0) {
327                         exit(-1);
328                 }
329
330                 /**
331                  * Handle the request of setting MTU,
332                  * without registered handlers.
333                  */
334                 for (i = 0; i < TEST_KNI_HANDLE_REQ_COUNT; i++) {
335                         rte_kni_handle_request(kni);
336                         if (kni_pkt_mtu != 0)
337                                 break;
338                         rte_delay_ms(TEST_KNI_HANDLE_REQ_INTERVAL);
339                 }
340                 if (kni_pkt_mtu != 0) {
341                         printf("MTU shouldn't be set\n");
342                         exit(-1);
343                 }
344
345                 exit(0);
346         } else {
347                 int p_ret, status;
348
349                 rte_delay_ms(1000);
350                 if (system(IFCONFIG TEST_KNI_PORT " mtu" TEST_KNI_MTU_STR)
351                                                                 == -1)
352                         return -1;
353
354                 rte_delay_ms(1000);
355                 if (system(IFCONFIG TEST_KNI_PORT " mtu" TEST_KNI_MTU_STR)
356                                                                 == -1)
357                         return -1;
358
359                 p_ret = wait(&status);
360                 if (!WIFEXITED(status)) {
361                         printf("Child process (%d) exit abnormally\n", p_ret);
362                         return -1;
363                 }
364                 if (WEXITSTATUS(status) != 0) {
365                         printf("Child process exit with failure\n");
366                         return -1;
367                 }
368         }
369
370         return 0;
371 }
372
373 static int
374 test_kni_processing(uint8_t port_id, struct rte_mempool *mp)
375 {
376         int ret = 0;
377         unsigned i;
378         struct rte_kni *kni;
379         struct rte_kni_conf conf;
380         struct rte_eth_dev_info info;
381         struct rte_kni_ops ops;
382
383         if (!mp)
384                 return -1;
385
386         memset(&conf, 0, sizeof(conf));
387         memset(&info, 0, sizeof(info));
388         memset(&ops, 0, sizeof(ops));
389
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);
394
395         /* core id 1 configured for kernel thread */
396         conf.core_id = 1;
397         conf.force_bind = 1;
398         conf.mbuf_size = MAX_PACKET_SZ;
399         conf.group_id = (uint16_t)port_id;
400
401         ops = kni_ops;
402         ops.port_id = port_id;
403
404         /* basic test of kni processing */
405         kni = rte_kni_alloc(mp, &conf, &ops);
406         if (!kni) {
407                 printf("fail to create kni\n");
408                 return -1;
409         }
410         if (rte_kni_get_port_id(kni) != port_id) {
411                 printf("fail to get port id\n");
412                 ret = -1;
413                 goto fail_kni;
414         }
415
416         if (rte_kni_info_get(RTE_MAX_ETHPORTS)) {
417                 printf("Unexpectedly get a KNI successfully\n");
418                 ret = -1;
419                 goto fail_kni;
420         }
421
422         test_kni_ctx = kni;
423         test_kni_processing_flag = 0;
424         stats.ingress = 0;
425         stats.egress = 0;
426
427         /**
428          * Check multiple processes support on
429          * registerring/unregisterring handlers.
430          */
431         if (test_kni_register_handler_mp() < 0) {
432                 printf("fail to check multiple process support\n");
433                 ret = -1;
434                 goto fail_kni;
435         }
436
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) {
440                         ret = -1;
441                         goto fail_kni;
442                 }
443         }
444         /**
445          * Check if the number of mbufs received from kernel space is equal
446          * to that of transmitted to kernel space
447          */
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);
452                 ret = -1;
453                 goto fail_kni;
454         }
455
456         if (rte_kni_release(kni) < 0) {
457                 printf("fail to release kni\n");
458                 return -1;
459         }
460         test_kni_ctx = NULL;
461         
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");
465                 return -1;
466         }
467
468         /* test of reusing memzone */
469         kni = rte_kni_alloc(mp, &conf, &ops);
470         if (!kni) {
471                 printf("fail to create kni\n");
472                 return -1;
473         }
474
475         /* Release the kni for following testing */
476         if (rte_kni_release(kni) < 0) {
477                 printf("fail to release kni\n");
478                 return -1;
479         }
480         
481         return ret;
482 fail_kni:
483         if (rte_kni_release(kni) < 0) {
484                 printf("fail to release kni\n");
485                 ret = -1;
486         }
487
488         return ret;
489 }
490
491 int
492 test_kni(void)
493 {
494         int ret = -1;
495         uint8_t nb_ports, port_id;
496         struct rte_kni *kni;
497         struct rte_mempool *mp;
498         struct rte_kni_conf conf;
499         struct rte_eth_dev_info info;
500         struct rte_kni_ops ops;
501
502         if (test_kni_allocate_lcores() < 0) {
503                 printf("No enough lcores for kni processing\n");
504                 return -1;
505         }
506
507         mp = test_kni_create_mempool();
508         if (!mp) {
509                 printf("fail to create mempool for kni\n");
510                 return -1;
511         }
512         ret = rte_pmd_init_all();
513         if (ret < 0) {
514                 printf("fail to initialize PMD\n");
515                 return -1;
516         }
517         ret = rte_eal_pci_probe();
518         if (ret < 0) {
519                 printf("fail to probe PCI devices\n");
520                 return -1;
521         }
522
523         nb_ports = rte_eth_dev_count();
524         if (nb_ports == 0) {
525                 printf("no supported nic port found\n");
526                 return -1;
527         }
528
529         /* configuring port 0 for the test is enough */
530         port_id = 0;
531         ret = rte_eth_dev_configure(port_id, 1, 1, &port_conf);
532         if (ret < 0) {
533                 printf("fail to configure port %d\n", port_id);
534                 return -1;
535         }
536
537         ret = rte_eth_rx_queue_setup(port_id, 0, NB_RXD, SOCKET, &rx_conf, mp);
538         if (ret < 0) {
539                 printf("fail to setup rx queue for port %d\n", port_id);
540                 return -1;
541         }
542
543         ret = rte_eth_tx_queue_setup(port_id, 0, NB_TXD, SOCKET, &tx_conf);
544         if (ret < 0) {
545                 printf("fail to setup tx queue for port %d\n", port_id);
546                 return -1;
547         }
548
549         ret = rte_eth_dev_start(port_id);
550         if (ret < 0) {
551                 printf("fail to start port %d\n", port_id);
552                 return -1;
553         }
554         rte_eth_promiscuous_enable(port_id);
555
556         /* basic test of kni processing */
557         ret = test_kni_processing(port_id, mp);
558         if (ret < 0)
559                 goto fail;
560
561         /* test of allocating KNI with NULL mempool pointer */
562         memset(&info, 0, sizeof(info));
563         memset(&conf, 0, sizeof(conf));
564         memset(&ops, 0, sizeof(ops));
565         rte_eth_dev_info_get(port_id, &info);
566         conf.addr = info.pci_dev->addr;
567         conf.id = info.pci_dev->id;
568         conf.group_id = (uint16_t)port_id;
569         conf.mbuf_size = MAX_PACKET_SZ;
570
571         ops = kni_ops;
572         ops.port_id = port_id;
573         kni = rte_kni_alloc(NULL, &conf, &ops);
574         if (kni) {
575                 ret = -1;
576                 printf("unexpectedly creates kni successfully with NULL "
577                                                         "mempool pointer\n");
578                 goto fail;
579         }
580
581         /* test of allocating KNI without configurations */
582         kni = rte_kni_alloc(mp, NULL, NULL);
583         if (kni) {
584                 ret = -1;
585                 printf("Unexpectedly allocate KNI device successfully "
586                                         "without configurations\n");
587                 goto fail;
588         }
589
590         /* test of allocating KNI without a name */
591         memset(&conf, 0, sizeof(conf));
592         memset(&info, 0, sizeof(info));
593         memset(&ops, 0, sizeof(ops));
594         rte_eth_dev_info_get(port_id, &info);
595         conf.addr = info.pci_dev->addr;
596         conf.id = info.pci_dev->id;
597         conf.group_id = (uint16_t)port_id;
598         conf.mbuf_size = MAX_PACKET_SZ;
599
600         ops = kni_ops;
601         ops.port_id = port_id;
602         kni = rte_kni_alloc(mp, &conf, &ops);
603         if (kni) {
604                 ret = -1;
605                 printf("Unexpectedly allocate a KNI device successfully "
606                                                 "without a name\n");
607                 goto fail;
608         }
609
610         /* test of getting port id according to NULL kni context */
611         if (rte_kni_get_port_id(NULL) < RTE_MAX_ETHPORTS) {
612                 ret = -1;
613                 printf("unexpectedly get port id successfully by NULL kni "
614                                                                 "pointer\n");
615                 goto fail;
616         }
617
618         /* test of releasing NULL kni context */
619         ret = rte_kni_release(NULL);
620         if (ret == 0) {
621                 ret = -1;
622                 printf("unexpectedly release kni successfully\n");
623                 goto fail;
624         }
625
626         /* test of handling request on NULL device pointer */
627         ret = rte_kni_handle_request(NULL);
628         if (ret == 0) {
629                 ret = -1;
630                 printf("Unexpectedly handle request on NULL device pointer\n");
631                 goto fail;
632         }
633
634         /* test of getting KNI device with pointer to NULL */
635         kni = rte_kni_get(NULL);
636         if (kni) {
637                 ret = -1;
638                 printf("Unexpectedly get a KNI device with "
639                                         "NULL name pointer\n");
640                 goto fail;
641         }
642
643         /* test of getting KNI device with an zero length name string */
644         memset(&conf, 0, sizeof(conf));
645         kni = rte_kni_get(conf.name);
646         if (kni) {
647                 ret = -1;
648                 printf("Unexpectedly get a KNI device with "
649                                 "zero length name string\n");
650                 goto fail;
651         }
652
653         /* test of getting KNI device with an invalid string name */
654         memset(&conf, 0, sizeof(conf));
655         rte_snprintf(conf.name, sizeof(conf.name), "testing");
656         kni = rte_kni_get(conf.name);
657         if (kni) {
658                 ret = -1;
659                 printf("Unexpectedly get a KNI device with "
660                                 "a never used name string\n");
661                 goto fail;
662         }
663
664         /* test the interface of creating a KNI, for backward compatibility */
665         memset(&ops, 0, sizeof(ops));
666         ops = kni_ops;
667         kni = rte_kni_create(port_id, MAX_PACKET_SZ, mp, &ops);
668         if (!kni) {
669                 ret = -1;
670                 printf("Fail to create a KNI device for port %d\n", port_id);
671                 goto fail;
672         }
673
674         ret = rte_kni_release(kni);
675         if (ret < 0) {
676                 printf("Fail to release a KNI device\n");
677                 goto fail;
678         }
679
680         ret = 0;
681
682 fail:
683         rte_eth_dev_stop(port_id);
684
685         return ret;
686 }
687
688 #else /* RTE_LIBRTE_KNI */
689
690 int
691 test_kni(void)
692 {
693         printf("The KNI library is not included in this build\n");
694         return 0;
695 }
696
697 #endif /* RTE_LIBRTE_KNI */