aboutsummaryrefslogtreecommitdiff
path: root/tests/sys/netinet/libalias/perf.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/sys/netinet/libalias/perf.c')
-rw-r--r--tests/sys/netinet/libalias/perf.c213
1 files changed, 145 insertions, 68 deletions
diff --git a/tests/sys/netinet/libalias/perf.c b/tests/sys/netinet/libalias/perf.c
index 365b8f2e9fcc..f238846bdb16 100644
--- a/tests/sys/netinet/libalias/perf.c
+++ b/tests/sys/netinet/libalias/perf.c
@@ -38,10 +38,7 @@
#include "util.h"
#include <alias.h>
-/* common ip ranges */
-static struct in_addr masq = { htonl(0x01020304) };
-static struct in_addr prv = { htonl(0x0a000000) };
-static struct in_addr ext = { htonl(0x12000000) };
+static void usage(void);
#define timevalcmp(tv, uv, cmp) \
(((tv).tv_sec == (uv).tv_sec) \
@@ -52,10 +49,24 @@ static struct in_addr ext = { htonl(0x12000000) };
(((n).tv_sec - (o).tv_sec)*1000000l + \
((n).tv_usec - (o).tv_usec))
+#define check_timeout() do { \
+ if (check_timeout_cnt++ > 1000) { \
+ check_timeout_cnt = 0; \
+ gettimeofday(&now, NULL); \
+ if (timevalcmp(now, timeout, >=)) \
+ goto out; \
+ } } while(0)
+
+static void
+usage(void) {
+ printf("Usage: perf [max_seconds [batch_size [random_size [attack_size [redir_size]]]]]\n");
+ exit(1);
+}
+
int main(int argc, char ** argv)
{
struct libalias *la;
- struct timeval timeout;
+ struct timeval timeout, now, start;
struct ip *p;
struct udphdr *u;
struct {
@@ -64,34 +75,59 @@ int main(int argc, char ** argv)
} *batch;
struct {
unsigned long ok, fail;
- } nat, unnat, random, attack;
- int max_seconds, batch_size, random_size, attack_length, round, cnt;
-
- if(argc != 5 ||
- 0 > (max_seconds = atoi(argv[1])) ||
- 0 >= (batch_size = atoi(argv[2])) ||
- 0 >= (random_size = atoi(argv[3])) ||
- 0 >= (attack_length = atoi(argv[4]))) {
- printf("Usage: %s max_seconds batch_size random_size attack_length\n", argv[0]);
- return 1;
+ } nat, usenat, unnat, random, attack;
+ int i, round, check_timeout_cnt = 0;
+ int max_seconds = 90, batch_size = 2000,
+ random_size = 1000, attack_size = 1000,
+ redir_size = 2000;
+
+ if (argc >= 2) {
+ char * end;
+
+ max_seconds = strtol(argv[1], &end, 10);
+ if (max_seconds < 2 || end[0] != '\0')
+ usage();
}
+ if (argc > 2 && (batch_size = atoi(argv[2])) < 0) usage();
+ if (argc > 3 && (random_size = atoi(argv[3])) < 0) usage();
+ if (argc > 4 && (attack_size = atoi(argv[4])) < 0) usage();
+ if (argc > 5 && (redir_size = atoi(argv[5])) < 0) usage();
+
+ printf("Running perfomance test with parameters:\n");
+ printf(" Maximum Runtime (max_seconds) = %d\n", max_seconds);
+ printf(" Amount of valid connections (batch_size) = %d\n", batch_size);
+ printf(" Amount of random, incoming packets (batch_size) = %d\n", random_size);
+ printf(" Repeat count of a random, incoming packet (attack_size) = %d\n", attack_size);
+ printf(" Amount of open port forwardings (redir_size) = %d\n", redir_size);
+ printf("\n");
+
if (NULL == (la = LibAliasInit(NULL))) {
perror("LibAliasInit");
return -1;
}
bzero(&nat, sizeof(nat));
+ bzero(&usenat, sizeof(usenat));
bzero(&unnat, sizeof(unnat));
bzero(&random, sizeof(random));
bzero(&attack, sizeof(attack));
LibAliasSetAddress(la, masq);
- LibAliasSetMode(la, PKT_ALIAS_DENY_INCOMING, PKT_ALIAS_DENY_INCOMING);
+ LibAliasSetMode(la, PKT_ALIAS_SAME_PORTS | PKT_ALIAS_DENY_INCOMING, ~0);
- prv.s_addr &= htonl(0xffff0000);
+ prv1.s_addr &= htonl(0xffff0000);
ext.s_addr &= htonl(0xffff0000);
- p = ip_packet(prv, ext, 0, 64);
+ for (i = 0; i < redir_size; i++) {
+ int aport = htons(rand_range(1000, 2000));
+ int sport = htons(rand_range(1000, 2000));
+
+ prv2.s_addr &= htonl(0xffff0000);
+ prv2.s_addr |= rand_range(0, 0xffff);
+ LibAliasRedirectPort(la, prv2, sport, ANY_ADDR, 0, masq, aport, IPPROTO_UDP);
+ }
+
+ p = ip_packet(0, 64);
u = set_udp(p, 0, 0);
if (NULL == (batch = calloc(batch_size, sizeof(*batch)))) {
@@ -102,17 +138,16 @@ int main(int argc, char ** argv)
gettimeofday(&timeout, NULL);
timeout.tv_sec += max_seconds;
- printf("RND SECND NAT RND ATT UNA\n");
+ printf("RND SECOND newNAT RANDOM ATTACK useNAT\n");
for (round = 0; ; round++) {
- int i, res;
- struct timeval now, start;
+ int res, cnt;
printf("%3d ", round+1);
gettimeofday(&start, NULL);
- printf("%5.1f ", max_seconds - timevaldiff(timeout, start)/1000000.0f);
+ printf("%6.1f ", max_seconds - timevaldiff(timeout, start)/1000000.0f);
for (cnt = i = 0; i < batch_size; i++, cnt++) {
- batch[i].src.s_addr = prv.s_addr | htonl(rand_range(0, 0xffff));
+ batch[i].src.s_addr = prv1.s_addr | htonl(rand_range(0, 0xffff));
batch[i].dst.s_addr = ext.s_addr | htonl(rand_range(0, 0xffff));
batch[i].sport = rand_range(1000, 60000);
batch[i].dport = rand_range(1000, 60000);
@@ -132,12 +167,13 @@ int main(int argc, char ** argv)
else
nat.fail++;
- gettimeofday(&now, NULL);
- if(timevalcmp(now, timeout, >=))
- goto out;
+ check_timeout();
}
+ gettimeofday(&now, NULL);
if (cnt > 0)
- printf("%3.0f ", timevaldiff(now, start) / cnt);
+ printf("%6.2f ", timevaldiff(now, start) / cnt);
+ else
+ printf("------ ");
start = now;
for (cnt = i = 0; i < random_size; i++, cnt++) {
@@ -153,19 +189,20 @@ int main(int argc, char ** argv)
else
random.fail++;
- gettimeofday(&now, NULL);
- if(timevalcmp(now, timeout, >=))
- goto out;
+ check_timeout();
}
+ gettimeofday(&now, NULL);
if (cnt > 0)
- printf("%3.0f ", timevaldiff(now, start) / cnt);
+ printf("%6.2f ", timevaldiff(now, start) / cnt);
+ else
+ printf("------ ");
start = now;
p->ip_src.s_addr = ext.s_addr & htonl(0xfff00000);
p->ip_src.s_addr |= htonl(rand_range(0, 0xffff));
p->ip_dst = masq;
u = set_udp(p, rand_range(1, 0xffff), rand_range(1, 0xffff));
- for (cnt = i = 0; i < attack_length; i++, cnt++) {
+ for (cnt = i = 0; i < attack_size; i++, cnt++) {
res = LibAliasIn(la, p, 64);
if (res == PKT_ALIAS_OK)
@@ -173,59 +210,99 @@ int main(int argc, char ** argv)
else
attack.fail++;
- gettimeofday(&now, NULL);
- if(timevalcmp(now, timeout, >=))
- goto out;
+ check_timeout();
}
+ gettimeofday(&now, NULL);
if (cnt > 0)
- printf("%3.0f ", timevaldiff(now, start) / cnt);
+ printf("%6.2f ", timevaldiff(now, start) / cnt);
+ else
+ printf("------ ");
qsort(batch, batch_size, sizeof(*batch), randcmp);
gettimeofday(&start, NULL);
- for (cnt = i = 0; i < batch_size; i++, cnt++) {
- p->ip_src = batch[i].dst;
- p->ip_dst = masq;
- u = set_udp(p, batch[i].dport, batch[i].aport);
+ for (cnt = i = 0; i < batch_size; i++) {
+ int j;
- res = LibAliasIn(la, p, 64);
- batch[i].aport = htons(u->uh_sport);
+ /* random communication length */
+ for(j = rand_range(1, 150); j-- > 0; cnt++) {
+ int k;
- if (res == PKT_ALIAS_OK &&
- u->uh_sport == htons(batch[i].dport) &&
- u->uh_dport == htons(batch[i].sport) &&
- addr_eq(p->ip_dst, batch[i].src) &&
- addr_eq(p->ip_src, batch[i].dst))
- unnat.ok++;
- else
- unnat.fail++;
+ /* a random flow out of rolling window */
+ k = rand_range(i, i + 25);
+ if (k >= batch_size)
+ k = i;
+
+ /* 10% outgoing, 90% incoming */
+ if (rand_range(0, 100) > 10) {
+ p->ip_src = batch[k].dst;
+ p->ip_dst = masq;
+ u = set_udp(p, batch[k].dport, batch[k].aport);
- gettimeofday(&now, NULL);
- if(timevalcmp(now, timeout, >=))
- goto out;
+ res = LibAliasIn(la, p, 64);
+ if (res == PKT_ALIAS_OK &&
+ u->uh_sport == htons(batch[k].dport) &&
+ u->uh_dport == htons(batch[k].sport) &&
+ addr_eq(p->ip_dst, batch[k].src) &&
+ addr_eq(p->ip_src, batch[k].dst))
+ unnat.ok++;
+ else
+ unnat.fail++;
+ } else {
+ p->ip_src = batch[k].src;
+ p->ip_dst = batch[k].dst;
+ u = set_udp(p, batch[k].sport, batch[k].dport);
+
+ res = LibAliasOut(la, p, 64);
+ if (res == PKT_ALIAS_OK &&
+ u->uh_sport == htons(batch[k].aport) &&
+ u->uh_dport == htons(batch[k].dport) &&
+ addr_eq(p->ip_dst, batch[k].dst) &&
+ addr_eq(p->ip_src, masq))
+ usenat.ok++;
+ else
+ usenat.fail++;
+ }
+ check_timeout();
+ }
}
+ gettimeofday(&now, NULL);
if (cnt > 0)
- printf("%3.0f\n", timevaldiff(now, start) / cnt);
+ printf("%6.2f ", timevaldiff(now, start) / cnt);
+ else
+ printf("------ ");
+
+ printf("\n");
}
out:
printf("\n\n");
free(batch);
free(p);
- LibAliasUninit(la);
printf("Results\n");
- printf(" Rounds : %7u\n", round);
- printf(" NAT ok : %7lu\n", nat.ok);
- printf(" NAT fail: %7lu\n", nat.fail);
- printf(" UNNAT ok : %7lu\n", unnat.ok);
- printf(" UNNAT fail: %7lu\n", unnat.fail);
- printf("RANDOM ok : %7lu\n", random.ok);
- printf("RANDOM fail: %7lu\n", random.fail);
- printf("ATTACK ok : %7lu\n", attack.ok);
- printf("ATTACK fail: %7lu\n", attack.fail);
- printf(" -------------------\n");
- printf(" Total: %7lu\n",
- nat.ok + nat.fail + unnat.ok + unnat.fail +
- random.ok + random.fail + attack.ok + attack.fail);
+ printf(" Rounds : %9u\n", round);
+ printf("newNAT ok : %9lu\n", nat.ok);
+ printf("newNAT fail: %9lu\n", nat.fail);
+ printf("useNAT ok : %9lu (out)\n", usenat.ok);
+ printf("useNAT fail: %9lu (out)\n", usenat.fail);
+ printf("useNAT ok : %9lu (in)\n", unnat.ok);
+ printf("useNAT fail: %9lu (in)\n", unnat.fail);
+ printf("RANDOM ok : %9lu\n", random.ok);
+ printf("RANDOM fail: %9lu\n", random.fail);
+ printf("ATTACK ok : %9lu\n", attack.ok);
+ printf("ATTACK fail: %9lu\n", attack.fail);
+ printf(" ---------\n");
+ printf(" Total: %9lu\n",
+ nat.ok + nat.fail +
+ unnat.ok + unnat.fail +
+ usenat.ok + usenat.fail +
+ random.ok + random.fail +
+ attack.ok + attack.fail);
+
+ gettimeofday(&start, NULL);
+ printf("\n Cleanup : ");
+ LibAliasUninit(la);
+ gettimeofday(&now, NULL);
+ printf("%.2fs\n", timevaldiff(now, start)/1000000l);
return (0);
}