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