npftest.c revision 1.11 1 /* $NetBSD: npftest.c,v 1.11 2013/09/24 02:04:21 rmind Exp $ */
2
3 /*
4 * NPF testing framework.
5 *
6 * Public Domain.
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdbool.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <assert.h>
15 #include <fcntl.h>
16 #include <err.h>
17
18 #include <sys/ioctl.h>
19 #include <net/if.h>
20 #include <arpa/inet.h>
21
22 #include <rump/rump.h>
23 #include <rump/rump_syscalls.h>
24
25 #include "npftest.h"
26
27 static bool verbose, quiet;
28
29 __dead static void
30 usage(void)
31 {
32 printf("usage:\n"
33 " %s [ -q | -v ] [ -c <config> ] "
34 "[ -i <interface> ] < -b | -t | -s file >\n"
35 " %s -T <testname> -c <config>\n"
36 " %s -L\n"
37 "where:\n"
38 "\t-b: benchmark\n"
39 "\t-t: regression test\n"
40 "\t-T <testname>: specific test\n"
41 "\t-s <file>: pcap stream\n"
42 "\t-c <config>: NPF configuration file\n"
43 "\t-i <interface>: primary interface\n"
44 "\t-L: list testnames and description for -T\n"
45 "\t-q: quiet mode\n"
46 "\t-v: verbose mode\n",
47 getprogname(), getprogname(), getprogname());
48 exit(EXIT_FAILURE);
49 }
50
51 __dead static void
52 describe_tests(void)
53 {
54 printf( "nbuf\tbasic npf mbuf handling\n"
55 "bpf\tBPF coprocessor\n"
56 "table\ttable handling\n"
57 "state\tstate handling and processing\n"
58 "rule\trule processing\n"
59 "nat\tNAT rule processing\n");
60 exit(EXIT_SUCCESS);
61 }
62
63 static bool
64 result(const char *testcase, bool ok)
65 {
66 if (!quiet) {
67 printf("NPF %-10s\t%s\n", testcase, ok ? "OK" : "fail");
68 }
69 if (verbose) {
70 puts("-----");
71 }
72 return !ok;
73 }
74
75 static void
76 load_npf_config_ifs(prop_dictionary_t dbg_dict)
77 {
78 prop_dictionary_t ifdict;
79 prop_object_iterator_t it;
80 prop_array_t iflist;
81
82 iflist = prop_dictionary_get(dbg_dict, "interfaces");
83 it = prop_array_iterator(iflist);
84 while ((ifdict = prop_object_iterator_next(it)) != NULL) {
85 const char *ifname;
86 unsigned if_idx;
87
88 prop_dictionary_get_cstring_nocopy(ifdict, "name", &ifname);
89 prop_dictionary_get_uint32(ifdict, "idx", &if_idx);
90 (void)rumpns_npf_test_addif(ifname, if_idx, verbose);
91 }
92 prop_object_iterator_release(it);
93 }
94
95 static void
96 load_npf_config(const char *config)
97 {
98 prop_dictionary_t npf_dict, dbg_dict;
99 void *xml;
100 int error;
101
102 /* Read the configuration from the specified file. */
103 npf_dict = prop_dictionary_internalize_from_file(config);
104 if (!npf_dict) {
105 err(EXIT_FAILURE, "prop_dictionary_internalize_from_file");
106 }
107 xml = prop_dictionary_externalize(npf_dict);
108
109 /* Inspect the debug data. Create the interfaces, if any. */
110 dbg_dict = prop_dictionary_get(npf_dict, "debug");
111 if (dbg_dict) {
112 load_npf_config_ifs(dbg_dict);
113 }
114 prop_object_release(npf_dict);
115
116 /* Pass the XML configuration for NPF kernel component to load. */
117 error = rumpns_npf_test_load(xml);
118 if (error) {
119 errx(EXIT_FAILURE, "npf_test_load: %s\n", strerror(error));
120 }
121 free(xml);
122
123 if (verbose) {
124 printf("Loaded NPF config at '%s'\n", config);
125 }
126 }
127
128 /*
129 * Need to override for cprng_fast32(), since RUMP uses arc4random() for it.
130 */
131 uint32_t
132 arc4random(void)
133 {
134 return random();
135 }
136
137 int
138 main(int argc, char **argv)
139 {
140 bool benchmark, test, ok, fail, tname_matched;
141 char *config, *interface, *stream, *testname;
142 unsigned nthreads = 0;
143 int idx = -1, ch;
144
145 benchmark = false;
146 test = false;
147
148 tname_matched = false;
149 testname = NULL;
150 config = NULL;
151 interface = NULL;
152 stream = NULL;
153
154 verbose = false;
155 quiet = false;
156
157 while ((ch = getopt(argc, argv, "bqvc:i:s:tT:Lp:")) != -1) {
158 switch (ch) {
159 case 'b':
160 benchmark = true;
161 break;
162 case 'q':
163 quiet = true;
164 break;
165 case 'v':
166 verbose = true;
167 break;
168 case 'c':
169 config = optarg;
170 break;
171 case 'i':
172 interface = optarg;
173 break;
174 case 's':
175 stream = optarg;
176 break;
177 case 't':
178 test = true;
179 break;
180 case 'T':
181 test = true;
182 testname = optarg;
183 break;
184 case 'L':
185 describe_tests();
186 break;
187 case 'p':
188 /* Note: RUMP_NCPU must be high enough. */
189 if ((nthreads = atoi(optarg)) > 0 &&
190 getenv("RUMP_NCPU") == NULL) {
191 char *val;
192 asprintf(&val, "%u", nthreads + 1);
193 setenv("RUMP_NCPU", val, 1);
194 free(val);
195 }
196 break;
197 default:
198 usage();
199 }
200 }
201
202 /*
203 * Either benchmark or test. If stream analysis, then the
204 * interface should be specified. If benchmark, then the
205 * config should be loaded.
206 */
207 if (benchmark == test && (stream && !interface)) {
208 usage();
209 }
210 if (benchmark && (!config || !nthreads)) {
211 errx(EXIT_FAILURE, "missing config for the benchmark or "
212 "invalid thread count");
213 }
214
215 /* XXX rn_init */
216 extern int rumpns_max_keylen;
217 rumpns_max_keylen = 1;
218
219 rump_init();
220 rump_schedule();
221
222 rumpns_npf_test_init();
223
224 if (config) {
225 load_npf_config(config);
226 }
227 if (interface && (idx = rumpns_npf_test_getif(interface)) == 0) {
228 errx(EXIT_FAILURE, "failed to find the interface");
229 }
230
231 srandom(1);
232 fail = false;
233
234 if (test) {
235 if (!testname || strcmp("nbuf", testname) == 0) {
236 ok = rumpns_npf_nbuf_test(verbose);
237 fail |= result("nbuf", ok);
238 tname_matched = true;
239 }
240
241 if (!testname || strcmp("bpf", testname) == 0) {
242 ok = rumpns_npf_bpf_test(verbose);
243 fail |= result("bpf", ok);
244 tname_matched = true;
245 }
246
247 if (!testname || strcmp("table", testname) == 0) {
248 ok = rumpns_npf_table_test(verbose);
249 fail |= result("table", ok);
250 tname_matched = true;
251 }
252
253 if (!testname || strcmp("state", testname) == 0) {
254 ok = rumpns_npf_state_test(verbose);
255 fail |= result("state", ok);
256 tname_matched = true;
257 }
258 }
259
260 if (test && config) {
261 if (!testname || strcmp("rule", testname) == 0) {
262 ok = rumpns_npf_rule_test(verbose);
263 fail |= result("rule", ok);
264 tname_matched = true;
265 }
266
267 if (!testname || strcmp("nat", testname) == 0) {
268 ok = rumpns_npf_nat_test(verbose);
269 fail |= result("nat", ok);
270 tname_matched = true;
271 }
272 }
273
274 if (stream) {
275 process_stream(stream, NULL, idx);
276 }
277
278 if (benchmark) {
279 rumpns_npf_test_conc(nthreads);
280 }
281
282 rump_unschedule();
283
284 if (testname && !tname_matched)
285 errx(EXIT_FAILURE, "test \"%s\" unknown", testname);
286
287 return fail ? EXIT_FAILURE : EXIT_SUCCESS;
288 }
289