Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/npf/npftest Add initial NPF regression tests integr...



details:   https://anonhg.NetBSD.org/src/rev/6a9667156e00
branches:  trunk
changeset: 778821:6a9667156e00
user:      rmind <rmind%NetBSD.org@localhost>
date:      Sat Apr 14 21:57:29 2012 +0000

description:
Add initial NPF regression tests integrated with RUMP framework (running the
kernel part of NPF in userland).  Other tests will be added once converted to
RUMP framework.  All tests are in the public domain.

Some Makefile fixes from christos@.

diffstat:

 usr.sbin/npf/npftest/Makefile                        |   27 ++
 usr.sbin/npf/npftest/libnpftest/Makefile             |   24 ++
 usr.sbin/npf/npftest/libnpftest/npf_mbuf_subr.c      |  110 +++++++++++
 usr.sbin/npf/npftest/libnpftest/npf_nbuf_test.c      |  176 +++++++++++++++++++
 usr.sbin/npf/npftest/libnpftest/npf_processor_test.c |  163 +++++++++++++++++
 usr.sbin/npf/npftest/libnpftest/npf_table_test.c     |  117 ++++++++++++
 usr.sbin/npf/npftest/libnpftest/npf_test.h           |   37 +++
 usr.sbin/npf/npftest/npftest.c                       |   87 +++++++++
 usr.sbin/npf/npftest/npftest.h                       |   16 +
 9 files changed, 757 insertions(+), 0 deletions(-)

diffs (truncated from 793 to 300 lines):

diff -r c83116a37dba -r 6a9667156e00 usr.sbin/npf/npftest/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.sbin/npf/npftest/Makefile     Sat Apr 14 21:57:29 2012 +0000
@@ -0,0 +1,27 @@
+# $NetBSD$
+#
+# Public Domain
+#
+
+PROG=          npftest
+
+SRCS=          npftest.c
+CPPFLAGS+=     -I${.CURDIR}
+
+LIBNPFTEST!=   cd ${.CURDIR}/libnpftest && ${MAKE} -V .OBJDIR
+DPADD+=                ${LIBNPFTEST}/libnpftest.a
+LDADD+=                -L${LIBNPFTEST} -lnpftest
+
+LDADD+=                -lrump -lrumpvfs -lrumpnet -lrump -lrumpnet_net
+LDADD+=                -lrumpnet_virtif -lrumpdev_npf -lpthread
+
+WARNS=         4
+NOMAN=         # no man page
+NOLINT=                # disabled (note: deliberately)
+
+SUBDIR+=       libnpftest
+
+${LIBNPFTEST}/libnpftest.a: all-libnpftest
+
+.include <bsd.subdir.mk>
+.include <bsd.prog.mk>
diff -r c83116a37dba -r 6a9667156e00 usr.sbin/npf/npftest/libnpftest/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.sbin/npf/npftest/libnpftest/Makefile  Sat Apr 14 21:57:29 2012 +0000
@@ -0,0 +1,24 @@
+# $NetBSD$
+#
+# Public Domain
+#
+
+RUMPTOP=       ${.CURDIR}/../../../../sys/rump
+
+LIB=           npftest
+LIBISPRIVATE=  yes
+
+SRCS+=         npf_mbuf_subr.c
+
+SRCS+=         npf_nbuf_test.c
+SRCS+=         npf_processor_test.c
+SRCS+=         npf_table_test.c
+
+CPPFLAGS+=     -I${.CURDIR}/../../../../sys/net/npf
+CPPFLAGS+=     -I${RUMPTOP}/librump/rumpkern
+
+WARNS=         4
+
+.include "${RUMPTOP}/Makefile.rump"
+.include <bsd.lib.mk>
+.include <bsd.klinks.mk>
diff -r c83116a37dba -r 6a9667156e00 usr.sbin/npf/npftest/libnpftest/npf_mbuf_subr.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.sbin/npf/npftest/libnpftest/npf_mbuf_subr.c   Sat Apr 14 21:57:29 2012 +0000
@@ -0,0 +1,110 @@
+/*     $NetBSD$        */
+
+/*
+ * NPF testing - helper routines.
+ *
+ * Public Domain.
+ */
+
+#include <sys/types.h>
+#include <sys/kmem.h>
+
+#include "npf_impl.h"
+#include "npf_test.h"
+
+struct mbuf *
+mbuf_getwithdata(void *data, size_t len)
+{
+       struct mbuf *m;
+
+       m = kmem_zalloc(sizeof(struct mbuf), KM_SLEEP);
+       assert(m != NULL);
+       m->m_data = data;
+       m->m_len = len;
+       return m;
+}
+
+struct mbuf *
+mbuf_construct_ether(int proto)
+{
+       struct mbuf *m0, *m1;
+       struct ether_header *ethdr;
+
+       m0 = m_gethdr(M_WAITOK, MT_HEADER);
+       ethdr = mtod(m0, struct ether_header *);
+       ethdr->ether_type = htons(ETHERTYPE_IP);
+       m0->m_len = sizeof(struct ether_header);
+
+       m1 = mbuf_construct(proto);
+       m0->m_next = m1;
+       m1->m_next = NULL;
+       return m0;
+}
+
+struct mbuf *
+mbuf_construct(int proto)
+{
+       struct mbuf *m;
+       struct ip *iphdr;
+       struct tcphdr *th;
+       int size;
+
+       m = m_gethdr(M_WAITOK, MT_HEADER);
+       iphdr = mtod(m, struct ip *);
+
+       iphdr->ip_v = IPVERSION;
+       iphdr->ip_hl = sizeof(struct ip) >> 2;
+       iphdr->ip_off = 0;
+       iphdr->ip_ttl = 64;
+       iphdr->ip_p = proto;
+
+       size = sizeof(struct ip);
+
+       switch (proto) {
+       case IPPROTO_TCP:
+               th = (void *)(iphdr + 1);
+               th->th_off = sizeof(struct tcphdr) >> 2;
+               size += sizeof(struct tcphdr);
+               break;
+       case IPPROTO_UDP:
+               size += sizeof(struct udphdr);
+               break;
+       case IPPROTO_ICMP:
+               size += offsetof(struct icmp, icmp_data);
+               break;
+       }
+       iphdr->ip_len = htons(size);
+
+       m->m_len = size;
+       m->m_next = NULL;
+       return m;
+}
+
+void *
+mbuf_return_hdrs(struct mbuf *m, bool ether, struct ip **ip)
+{
+       struct ip *iphdr;
+
+       if (ether) {
+               struct mbuf *mn = m->m_next;
+               iphdr = mtod(mn, struct ip *);
+       } else {
+               iphdr = mtod(m, struct ip *);
+       }
+       *ip = iphdr;
+       return (void *)(iphdr + 1);
+}
+
+void
+mbuf_icmp_append(struct mbuf *m, struct mbuf *m_orig)
+{
+       struct ip *iphdr = mtod(m, struct ip *);
+       const size_t hlen = iphdr->ip_hl << 2;
+       struct icmp *ic = (struct icmp *)((uint8_t *)iphdr + hlen);
+       const size_t addlen = m_orig->m_len;
+
+       iphdr->ip_len = htons(ntohs(iphdr->ip_len) + addlen);
+       memcpy(&ic->icmp_ip, mtod(m_orig, struct ip *), addlen);
+       m->m_len += addlen;
+       m_freem(m_orig);
+}
diff -r c83116a37dba -r 6a9667156e00 usr.sbin/npf/npftest/libnpftest/npf_nbuf_test.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.sbin/npf/npftest/libnpftest/npf_nbuf_test.c   Sat Apr 14 21:57:29 2012 +0000
@@ -0,0 +1,176 @@
+/*     $NetBSD: npf_nbuf_test.c,v 1.1 2012/04/14 21:57:29 rmind Exp $  */
+
+/*
+ * NPF nbuf interface test.
+ *
+ * Public Domain.
+ */
+
+#include <sys/types.h>
+#include <sys/kmem.h>
+
+#include "npf_impl.h"
+#include "npf_test.h"
+
+#define        MBUF_CHAIN_LEN          128
+
+CTASSERT((MBUF_CHAIN_LEN % sizeof(uint32_t)) == 0);
+
+static char *
+parse_nbuf_chain(void *nbuf, void *n_ptr)
+{
+       char *s = kmem_zalloc(MBUF_CHAIN_LEN + 1, KM_SLEEP);
+       int n, error;
+
+       for (n = 0; ; ) {
+               char d[4 + 1];
+
+               error = nbuf_fetch_datum(nbuf, n_ptr, sizeof(uint32_t), d);
+               if (error) {
+                       return NULL;
+               }
+               d[sizeof(d) - 1] = '\0';
+               strcat(s, d);
+
+               if (n + sizeof(uint32_t) == MBUF_CHAIN_LEN) {
+                       assert(nbuf_advance(&nbuf, n_ptr,
+                           sizeof(uint32_t) - 1));
+                       break;
+               }
+               n_ptr = nbuf_advance(&nbuf, n_ptr, sizeof(uint32_t));
+               if (n_ptr == NULL) {
+                       return NULL;
+               }
+               n += sizeof(uint32_t);
+       }
+       return s;
+}
+
+static char *
+mbuf_getstring(struct mbuf *m)
+{
+       char *s = kmem_zalloc(MBUF_CHAIN_LEN + 1, KM_SLEEP);
+       u_int tlen = 0;
+
+       while (m) {
+               int len = m->m_len;
+               char *d = m->m_data;
+               while (len--) {
+                       s[tlen++] = *d++;
+               }
+               m = m->m_next;
+       }
+       return s;
+}
+
+static struct mbuf *
+mbuf_alloc_with_off(size_t off, int len)
+{
+       struct mbuf *m;
+
+       m = kmem_zalloc(sizeof(struct mbuf) + off + len, KM_SLEEP);
+       m->m_data = (char *)m + sizeof(struct mbuf) + off;
+       m->m_len = len;
+       return m;
+}
+
+/*
+ * Create an mbuf chain, each of 1 byte size.
+ */
+static struct mbuf *
+mbuf_bytesize(size_t clen)
+{
+       struct mbuf *m0 = NULL, *m = NULL;
+       u_int i, n;
+
+       /* Chain of clen (e.g. 128) mbufs, each storing 1 byte of data. */
+       for (i = 0, n = 0; i < clen; i++) {
+               /* Range of offset: 0 .. 15. */
+               m0 = mbuf_alloc_with_off(n & 0xf, 1);
+
+               /* Fill data with letters from 'a' to 'z'. */
+               memset(m0->m_data, 'a' + n, 1);
+               n = ('a' + n) != 'z' ? n + 1 : 0;
+
+               /* Next mbuf.. */
+               m0->m_next = m;
+               m = m0;
+       }
+       return m0;
+}
+
+/*
+ * Generate random amount of mbufs, with random offsets and lengths.
+ */
+static struct mbuf *
+mbuf_random_len(size_t chain_len)
+{
+       struct mbuf *m0 = NULL, *m = NULL;
+       u_int tlen = 0, n = 0;
+
+       while (tlen < chain_len) {
+               u_int off, len;
+               char *d;
+
+               /* Random offset and length range: 1 .. 16. */
+               off = (random() % 16) + 1;
+               len = (random() % 16) + 1;
+
+               /* Do not exceed 128 bytes of total length. */
+               if (tlen + len > chain_len) {
+                       len = chain_len - tlen;
+               }
+               tlen += len;



Home | Main Index | Thread Index | Old Index