From owner-dev-commits-src-main@freebsd.org Sat May 22 21:50:03 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 5AA5763811D; Sat, 22 May 2021 21:50:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4FncZz1sbTz3hZH; Sat, 22 May 2021 21:50:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 28F083ABC; Sat, 22 May 2021 21:50:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 14MLo3Pm076009; Sat, 22 May 2021 21:50:03 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 14MLo3DS076006; Sat, 22 May 2021 21:50:03 GMT (envelope-from git) Date: Sat, 22 May 2021 21:50:03 GMT Message-Id: <202105222150.14MLo3DS076006@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Lutz Donnerhacke Subject: git: 33c1bdfc3e09 - main - tests/libalias: Add perfomance test utility MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: donner X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 33c1bdfc3e098862100bab7a8dc729d8c78ffa7c Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 22 May 2021 21:50:03 -0000 The branch main has been updated by donner: URL: https://cgit.FreeBSD.org/src/commit/?id=33c1bdfc3e098862100bab7a8dc729d8c78ffa7c commit 33c1bdfc3e098862100bab7a8dc729d8c78ffa7c Author: Lutz Donnerhacke AuthorDate: 2021-05-21 14:54:24 +0000 Commit: Lutz Donnerhacke CommitDate: 2021-05-22 21:45:42 +0000 tests/libalias: Add perfomance test utility In order to compare upcoming changes for their effectivness, measure performance by counting opertions and the runtime of each operation over the time. Accumulate all tests in a single instance, so make it complicated over the time. If you wait long enough, you will notice the expiry of old flows. Reviewed by: kp (earlier version) MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D30379 --- tests/sys/netinet/libalias/Makefile | 3 + tests/sys/netinet/libalias/perf.c | 198 ++++++++++++++++++++++++++++++++++++ tests/sys/netinet/libalias/util.c | 17 +++- 3 files changed, 213 insertions(+), 5 deletions(-) diff --git a/tests/sys/netinet/libalias/Makefile b/tests/sys/netinet/libalias/Makefile index 79922d7d4b13..6ee2fc0e0e1f 100644 --- a/tests/sys/netinet/libalias/Makefile +++ b/tests/sys/netinet/libalias/Makefile @@ -8,10 +8,13 @@ BINDIR= ${TESTSDIR} ATF_TESTS_C+= 1_instance \ 2_natout \ +PROGS+= perf + LIBADD+= alias SRCS.1_instance=1_instance.c util.c SRCS.2_natout= 2_natout.c util.c +SRCS.perf= perf.c util.c .include diff --git a/tests/sys/netinet/libalias/perf.c b/tests/sys/netinet/libalias/perf.c new file mode 100644 index 000000000000..ac4796cf9649 --- /dev/null +++ b/tests/sys/netinet/libalias/perf.c @@ -0,0 +1,198 @@ +#include +#include +#include +#include +#include "util.h" +#include + +/* common ip ranges */ +static struct in_addr masq = { htonl(0x01020304) }; +static struct in_addr prv = { htonl(0x0a000000) }; +static struct in_addr ext = { htonl(0x12000000) }; + +#define timevalcmp(tv, uv, cmp) \ + (((tv).tv_sec == (uv).tv_sec) \ + ? ((tv).tv_usec cmp (uv).tv_usec) \ + : ((tv).tv_sec cmp (uv).tv_sec)) + +#define timevaldiff(n, o) \ + (((n).tv_sec - (o).tv_sec)*1000000l + \ + ((n).tv_usec - (o).tv_usec)) + +int main(int argc, char ** argv) +{ + struct libalias *la; + struct timeval timeout; + struct ip *p; + struct udphdr *u; + struct { + struct in_addr src, dst; + uint16_t sport, dport, aport; + } *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; + } + if (NULL == (la = LibAliasInit(NULL))) { + perror("LibAliasInit"); + return -1; + } + + bzero(&nat, sizeof(nat)); + 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); + + prv.s_addr &= htonl(0xffff0000); + ext.s_addr &= htonl(0xffff0000); + + p = ip_packet(prv, ext, 0, 64); + u = set_udp(p, 0, 0); + + if (NULL == (batch = calloc(batch_size, sizeof(*batch)))) { + perror("calloc(batch)"); + return -1; + } + + gettimeofday(&timeout, NULL); + timeout.tv_sec += max_seconds; + + printf("RND SEC NAT RND ATT UNA\n"); + for (round = 0; ; round++) { + int i, res; + struct timeval now, start; + + printf("%3d ", round+1); + + gettimeofday(&start, NULL); + printf("%3ld ", max_seconds - (timeout.tv_sec - start.tv_sec)); + for (cnt = i = 0; i < batch_size; i++, cnt++) { + batch[i].src.s_addr = prv.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); + + p->ip_src = batch[i].src; + p->ip_dst = batch[i].dst; + u = set_udp(p, batch[i].sport, batch[i].dport); + + res = LibAliasOut(la, p, 64); + batch[i].aport = htons(u->uh_sport); + + if (res == PKT_ALIAS_OK && + u->uh_dport == htons(batch[i].dport) && + addr_eq(p->ip_dst, batch[i].dst) && + addr_eq(p->ip_src, masq)) + nat.ok++; + else + nat.fail++; + + gettimeofday(&now, NULL); + if(timevalcmp(now, timeout, >=)) + goto out; + } + if (cnt > 0) + printf("%3lu ", timevaldiff(now, start) / cnt); + + start = now; + for (cnt = i = 0; i < random_size; i++, cnt++) { + 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)); + + res = LibAliasIn(la, p, 64); + + if (res == PKT_ALIAS_OK) + random.ok++; + else + random.fail++; + + gettimeofday(&now, NULL); + if(timevalcmp(now, timeout, >=)) + goto out; + } + if (cnt > 0) + printf("%3lu ", timevaldiff(now, start) / cnt); + + 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++) { + res = LibAliasIn(la, p, 64); + + if (res == PKT_ALIAS_OK) + attack.ok++; + else + attack.fail++; + + gettimeofday(&now, NULL); + if(timevalcmp(now, timeout, >=)) + goto out; + } + if (cnt > 0) + printf("%3lu ", timevaldiff(now, start) / cnt); + + 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); + + res = LibAliasIn(la, p, 64); + batch[i].aport = htons(u->uh_sport); + + 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++; + + gettimeofday(&now, NULL); + if(timevalcmp(now, timeout, >=)) + goto out; + } + if (cnt > 0) + printf("%3lu\n", timevaldiff(now, start) / cnt); + } +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); + return (0); +} diff --git a/tests/sys/netinet/libalias/util.c b/tests/sys/netinet/libalias/util.c index e0e5e08eb23e..5e880752d261 100644 --- a/tests/sys/netinet/libalias/util.c +++ b/tests/sys/netinet/libalias/util.c @@ -1,4 +1,3 @@ -#include #include #include @@ -6,6 +5,14 @@ #include "util.h" +#define REQUIRE(x) do { \ + if (!(x)) { \ + fprintf(stderr, "Failed in %s %s:%d.\n",\ + __FUNCTION__, __FILE__, __LINE__); \ + exit(-1); \ + } \ +} while(0) + int randcmp(const void *a, const void *b) { @@ -42,10 +49,10 @@ ip_packet(struct in_addr src, struct in_addr dst, u_char protocol, size_t len) { struct ip * p; - ATF_REQUIRE(len >= 64 && len <= IP_MAXPACKET); + REQUIRE(len >= 64 && len <= IP_MAXPACKET); p = calloc(1, len); - ATF_REQUIRE(p != NULL); + REQUIRE(p != NULL); p->ip_v = IPVERSION; p->ip_hl = sizeof(*p)/4; @@ -54,7 +61,7 @@ ip_packet(struct in_addr src, struct in_addr dst, u_char protocol, size_t len) p->ip_src = src; p->ip_dst = dst; p->ip_p = protocol; - ATF_REQUIRE(p->ip_hl == 5); + REQUIRE(p->ip_hl == 5); return (p); } @@ -65,7 +72,7 @@ set_udp(struct ip *p, u_short sport, u_short dport) { struct udphdr *u = (void *)&(up[p->ip_hl]); int payload = ntohs(p->ip_len) - 4*p->ip_hl; - ATF_REQUIRE(payload >= (int)sizeof(*u)); + REQUIRE(payload >= (int)sizeof(*u)); p->ip_p = IPPROTO_UDP; u->uh_sport = htons(sport); u->uh_dport = htons(dport);