From 734c8055471407605a364edb5cdde1da52630be1 Mon Sep 17 00:00:00 2001 From: Shivansh Rai Date: Mon, 20 Mar 2017 11:13:16 +0530 Subject: [PATCH] Port tests and scripts from luigirizzo/dummynet@8e87ee720b --- configuration/README | 14 + configuration/change_rules.sh | 159 +++++++ configuration/change_rules_linux.sh | 12 + configuration/ipfw.conf | 29 ++ configuration/ipfw.rules | 12 + configuration/rc.firewall | 400 +++++++++++++++++ test/Makefile | 53 +++ test/basic_ipfw.sh | 72 ++++ test/dn_test.h | 157 +++++++ test/dynrules.sh | 20 + test/interpolation.c | 335 +++++++++++++++ test/main.c | 636 ++++++++++++++++++++++++++++ test/memory_leak.sh | 26 ++ test/mylist.h | 49 +++ test/profile_bench1 | 26 ++ test/profile_bench2 | 7 + test/profile_bench3 | 5 + test/test_dn_heap.c | 162 +++++++ test/test_dn_sched.c | 89 ++++ 19 files changed, 2263 insertions(+) create mode 100644 configuration/README create mode 100755 configuration/change_rules.sh create mode 100755 configuration/change_rules_linux.sh create mode 100644 configuration/ipfw.conf create mode 100755 configuration/ipfw.rules create mode 100755 configuration/rc.firewall create mode 100644 test/Makefile create mode 100755 test/basic_ipfw.sh create mode 100644 test/dn_test.h create mode 100644 test/dynrules.sh create mode 100644 test/interpolation.c create mode 100644 test/main.c create mode 100644 test/memory_leak.sh create mode 100644 test/mylist.h create mode 100644 test/profile_bench1 create mode 100644 test/profile_bench2 create mode 100644 test/profile_bench3 create mode 100644 test/test_dn_heap.c create mode 100644 test/test_dn_sched.c diff --git a/configuration/README b/configuration/README new file mode 100644 index 0000000..778f7aa --- /dev/null +++ b/configuration/README @@ -0,0 +1,14 @@ +This directorty contains some ipfw configurations and a scripts +to safely change the firewall rules. + +The firewall configuration comes from the FreeBSD initial script. +The change_rules_linux.sh allows to change the ipfw rules and +in case os a misconfiguration which prevents to reach the remote +host, to restore the old ruleset. + +To configure the firewall behavior, edit the ipfw.conf file and +execute the ./change_rules_linux.sh script. + +The ipfw program executable should be located in /sbin (XXX) + +XXX seems we use something which is not compatible with dash diff --git a/configuration/change_rules.sh b/configuration/change_rules.sh new file mode 100755 index 0000000..8f23369 --- /dev/null +++ b/configuration/change_rules.sh @@ -0,0 +1,159 @@ +#!/bin/sh +# +# Copyright (c) 2000 Alexandre Peixoto +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD: src/share/examples/ipfw/change_rules.sh,v 1.6 2003/09/07 07:52:56 jmg Exp $ + +# Change ipfw(8) rules with safety guarantees for remote operation +# +# Invoke this script to edit ${firewall_script}. It will call ${EDITOR}, +# or vi(1) if the environment variable is not set, for you to edit +# ${firewall_script}, ask for confirmation, and then run +# ${firewall_script}. You can then examine the output of ipfw list and +# confirm whether you want the new version or not. +# +# If no answer is received in 30 seconds, the previous +# ${firewall_script} is run, restoring the old rules (this assumes ipfw +# flush is present in it). +# +# If the new rules are confirmed, they'll replace ${firewall_script} and +# the previous ones will be copied to ${firewall_script}.{date}. Mail +# will also be sent to root with a unified diff of the rule change. +# +# Unapproved rules are kept in ${firewall_script}.new, and you are +# offered the option of changing them instead of the present rules when +# you call this script. +# +# This script could be improved by using version control +# software. + +# XXX on linux /etc/rc.conf defines: +# firewall_type and firewall_script + +if [ -r /etc/defaults/rc.conf ]; then + . /etc/defaults/rc.conf + source_rc_confs +elif [ -r /etc/rc.conf ]; then + . /etc/rc.conf +fi + +EDITOR=${EDITOR:-/usr/bin/vi} +PAGER=${PAGER:-/usr/bin/more} + +# on linux the default mktemp invocation behavior +# is different, we should change the temporary file creation +tempfoo=`basename $0` +#TMPFILE=`mktemp -t ${tempfoo}` || exit 1 +TMPFILE=`mktemp -t ${tempfoo}.XXXXX` || exit 1 + +get_yes_no() { + while true + do + echo -n "$1 (Y/N) ? " + read -t 30 a + if [ $? != 0 ]; then + a="No"; + return; + fi + case $a in + [Yy]) a="Yes"; + return;; + [Nn]) a="No"; + return;; + *);; + esac + done +} + +restore_rules() { + nohup sh ${firewall_script} /dev/null 2>&1 + rm ${TMPFILE} + exit 1 +} + +case "${firewall_type}" in +[Cc][Ll][Ii][Ee][Nn][Tt]|\ +[Cc][Ll][Oo][Ss][Ee][Dd]|\ +[Oo][Pp][Ee][Nn]|\ +[Ss][Ii][Mm][Pp][Ll][Ee]|\ +[Uu][Nn][Kk][Nn][Oo][Ww][Nn]) + edit_file="${firewall_script}" + rules_edit=no + ;; +*) + if [ -r "${firewall_type}" ]; then + edit_file="${firewall_type}" + rules_edit=yes + fi + ;; +esac + +if [ -f ${edit_file}.new ]; then + get_yes_no "A new rules file already exists, do you want to use it" + [ $a = 'No' ] && cp ${edit_file} ${edit_file}.new +else + cp ${edit_file} ${edit_file}.new +fi + +trap restore_rules SIGHUP + +${EDITOR} ${edit_file}.new + +get_yes_no "Do you want to install the new rules" + +[ $a = 'No' ] && exit 1 + +cat < ${TMPFILE} 2>&1 +else + nohup sh ${firewall_script}.new \ + < /dev/null > ${TMPFILE} 2>&1 +fi +sleep 2; +get_yes_no "Would you like to see the resulting new rules" +[ $a = 'Yes' ] && ${PAGER} ${TMPFILE} +get_yes_no "Type y to keep the new rules" +[ $a != 'Yes' ] && restore_rules + +DATE=`date "+%Y%m%d%H%M"` +cp ${edit_file} ${edit_file}.$DATE +mv ${edit_file}.new ${edit_file} +cat </dev/null + fi + ${fwcmd} add deny $log ip from any to any + ;; + +[Cc][Ll][Oo][Ss][Ee][Dd]) + ${fwcmd} add 65000 deny ip from any to any + ;; +[Uu][Nn][Kk][Nn][Oo][Ww][Nn]) + ;; +*) + if [ -r "${firewall_type}" ]; then + ${fwcmd} ${firewall_flags} ${firewall_type} + fi + ;; +esac diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..9ed47f8 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,53 @@ +# +# $Id: Makefile 5626 2010-03-04 21:55:22Z luigi $ +# +# Makefile for building userland tests +# this is written in a form compatible with gmake + +SCHED_SRCS = test_dn_sched.c +SCHED_SRCS += dn_sched_fifo.c +SCHED_SRCS += dn_sched_wf2q.c +SCHED_SRCS += dn_sched_qfq.c +SCHED_SRCS += dn_sched_rr.c +SCHED_SRCS += dn_heap.c +SCHED_SRCS += main.c + +SCHED_OBJS=$(SCHED_SRCS:.c=.o) + +HEAP_SRCS = dn_heap.c test_dn_heap.c +HEAP_OBJS=$(HEAP_SRCS:.c=.o) + +VPATH= .:../dummynet2 + +#CFLAGS = -I../dummynet2/include -I. -Wall -Werror -O3 -DIPFW +CFLAGS = -I. -I../dummynet2/include/netinet/ipfw -DIPFW +CFLAGS += -Wall -Werror +CFLAGS += -g -O3 +TARGETS= test_sched # no test_heap by default + +all: $(TARGETS) + +test_heap : $(HEAP_OBJS) + $(CC) -o $@ $(HEAP_OBJS) + +test_sched : $(SCHED_OBJS) + $(CC) -o $@ $(SCHED_OBJS) + +$(SCHED_OBJS): dn_test.h +main.o: mylist.h + +clean: + - rm *.o $(TARGETS) *.core + +ALLSRCS = $(SCHED_SRCS) dn_test.h mylist.h \ + dn_sched.h dn_heap.h ip_dn_private.h Makefile +TMPBASE = /tmp/testXYZ +TMPDIR = $(TMPBASE)/test + +tgz: + -rm -rf $(TMPDIR) + mkdir -p $(TMPDIR) + -cp -p $(ALLSRCS) $(TMPDIR) + -(cd ..; cp -p $(ALLSRCS) $(TMPDIR)) + ls -la $(TMPDIR) + (cd $(TMPBASE); tar cvzf /tmp/test.tgz test) diff --git a/test/basic_ipfw.sh b/test/basic_ipfw.sh new file mode 100755 index 0000000..08b66f9 --- /dev/null +++ b/test/basic_ipfw.sh @@ -0,0 +1,72 @@ +#!/bin/sh + +IPFW=./ipfw/ipfw +PING=/bin/ping +RH=127.0.0.1 # remote host +R=10 # test rule number +P=1 # test pipe number + +abort() +{ +echo $* +} + +#insmod dummynet2/ipfw_mod.ko +#$IPFW show > /dev/null +#$IPFW pipe show +echo "Flushing rules, do you agree ?" +$IPFW flush + +# test_msg rule counter +clean() +{ + $IPFW delete $R 2> /dev/null + $IPFW pipe $P delete 2> /dev/null +} + +# simple counter/allow test +echo -n "counter/allow test..." +clean +$IPFW add $R allow icmp from any to 127.0.0.1 > /dev/null +$PING -f -c100 $RH > /dev/null +counter=`$IPFW show | grep $R | head -n 1 | cut -d " " -f3` +[ ! $counter -eq 400 ] && abort "Wrong counter $counter 400" +echo "...OK" + +# simple drop test +echo -n "deny test..." +clean +$IPFW add $R deny icmp from any to 127.0.0.1 > /dev/null +$PING -f -c10 -W 1 $RH > /dev/null +counter=`$IPFW show | grep $R | head -n 1 | cut -d " " -f4` +[ ! $counter -eq 10 ] && abort "Wrong counter $counter 10" +echo "...OK" + +# pipe delay test +echo -n "pipe delay test..." +clean +$IPFW pipe $P config delay 2000ms >/dev/null +$IPFW add $R pipe $P icmp from any to $RH >/dev/null +$PING -f -c10 -W 1 $RH > /dev/null +counter1=`$IPFW show | grep $R | head -n 1 | cut -d " " -f4` +sleep 2 +counter2=`$IPFW show | grep $R | head -n 1 | cut -d " " -f4` +[ ! $counter1 -eq 10 ] && abort "Wrong counter $counter 10" +[ ! $counter2 -eq 20 ] && abort "Wrong counter $counter 20" +echo "...OK" + +# pipe bw test +echo -n "pipe bw test..." +clean +$IPFW pipe $P config bw 2Kbit/s >/dev/null +$IPFW add $R pipe $P icmp from any to $RH >/dev/null +$PING -i 0.1 -c10 -W 1 $RH > /dev/null +counter=`$IPFW show | grep $R | head -n 1 | cut -d " " -f4` +[ $counter -gt 30 ] && abort "Wrong counter $counter should be < 30" +sleep 1 +counter=`$IPFW show | grep $R | head -n 1 | cut -d " " -f4` +[ $counter -gt 30 ] && abort "Wrong counter $counter should be < 30" +echo "...OK" + +# Final clean +clean diff --git a/test/dn_test.h b/test/dn_test.h new file mode 100644 index 0000000..f2a4a51 --- /dev/null +++ b/test/dn_test.h @@ -0,0 +1,157 @@ +/* + * $Id: dn_test.h 5626 2010-03-04 21:55:22Z luigi $ + * + * userspace compatibility code for dummynet schedulers + */ + +#ifndef _DN_TEST_H +#define _DN_TEST_H +#include +#include +#include +#include /* bzero, ffs, ... */ +#include /* strcmp */ +#include +#include +#include + +extern int debug; +#define ND(fmt, args...) do {} while (0) +#define D1(fmt, args...) do {} while (0) +#define D(fmt, args...) fprintf(stderr, "%-8s " fmt "\n", \ + __FUNCTION__, ## args) +#define DX(lev, fmt, args...) do { \ + if (debug > lev) D(fmt, ## args); } while (0) + + +#define offsetof(t,m) (int)((&((t *)0L)->m)) + +#include + +/* prevent include of other system headers */ +#define _NETINET_IP_VAR_H_ /* ip_fw_args */ +#define _IPFW2_H +#define _SYS_MBUF_H_ + +enum { + DN_QUEUE, +}; + +enum { + DN_SCHED_FIFO, + DN_SCHED_WF2QP, +}; + +struct dn_id { + int type, subtype, len, id; +}; +struct dn_fs { + int par[4]; /* flowset parameters */ + + /* simulation entries. + * 'index' is not strictly necessary + * y is used for the inverse mapping , + */ + int index; + int y; /* inverse mapping */ + int base_y; /* inverse mapping */ + int next_y; /* inverse mapping */ + int n_flows; + int first_flow; + int next_flow; /* first_flow + n_flows */ + /* + * when generating, let 'cur' go from 0 to n_flows-1, + * then point to flow first_flow + cur + */ + int cur; +}; +struct dn_sch { +}; +struct dn_flow { + struct dn_id oid; + int length; + int len_bytes; + int drops; + uint64_t tot_bytes; + uint32_t flow_id; + struct list_head h; /* used by the generator */ +}; +struct dn_link { +}; + +struct ip_fw_args { +}; + +struct mbuf { + struct { + int len; + } m_pkthdr; + struct mbuf *m_nextpkt; + int flow_id; /* for testing, index of a flow */ + //int flowset_id; /* for testing, index of a flowset */ + void *cfg; /* config args */ +}; + +#define MALLOC_DECLARE(x) +#define KASSERT(x, y) do { if (!(x)) printf y ; exit(0); } while (0) +struct ipfw_flow_id { +}; + +typedef void * module_t; +struct _md_t { + const char *name; + int (*f)(module_t, int, void *); + void *p; +}; +typedef struct _md_t moduledata_t; +#define DECLARE_MODULE(name, b, c, d) \ + moduledata_t *_g_##name = & b +#define MODULE_DEPEND(a, b, c, d, e) + +#ifdef IPFW +#include +#include +#include +#else +struct dn_queue { + struct dn_fsk *fs; /* parent flowset. */ + struct dn_sch_inst *_si; /* parent sched instance. */ +}; +struct dn_schk { +}; +struct dn_fsk { + struct dn_fs fs; + struct dn_schk *sched; +}; +struct dn_sch_inst { + struct dn_schk *sched; +}; +struct dn_alg { + int type; + const char *name; + void *enqueue, *dequeue; + int q_datalen, si_datalen, schk_datalen; + int (*config)(struct dn_schk *); + int (*new_sched)(struct dn_sch_inst *); + int (*new_fsk)(struct dn_fsk *); + int (*new_queue)(struct dn_queue *q); +}; + +#endif + +#ifndef __FreeBSD__ +int fls(int); +#endif + +static inline void +mq_append(struct mq *q, struct mbuf *m) +{ + if (q->head == NULL) + q->head = m; + else + q->tail->m_nextpkt = m; + q->tail = m; + m->m_nextpkt = NULL; +} + +#endif /* _DN_TEST_H */ diff --git a/test/dynrules.sh b/test/dynrules.sh new file mode 100644 index 0000000..98f5fe6 --- /dev/null +++ b/test/dynrules.sh @@ -0,0 +1,20 @@ +#!/bin/sh +# +# 20100507 marta, quick test for dyn rules +# ./ipfw/ipfw -d show |grep \ 80 + +IPFW_MOD=dummynet2/ipfw_mod.ko +IPFW=ipfw/ipfw + +# main +# remove any previous loaded module +/sbin/rmmod ipfw_mod +/sbin/insmod ${IPFW_MOD} +echo "25" > /sys/module/ipfw_mod/parameters/dyn_ack_lifetime +${IPFW} add 1 check-state +${IPFW} add 9 allow all from any to any keep-state +${IPFW} add 10 allow all from any to onelab1.iet.unipi.it keep-state + +telnet 72.14.234.104 80 + + diff --git a/test/interpolation.c b/test/interpolation.c new file mode 100644 index 0000000..d6731f1 --- /dev/null +++ b/test/interpolation.c @@ -0,0 +1,335 @@ +#include +#include +#include + +/* gcc interpolation.c -o interpolation */ + +void +err(int eval, const char *fmt, ...) +{ +} +void +errx(int eval, const char *fmt, ...) +{ +} + + +#define ED_MAX_SAMPLES_NO 1000 +#define ED_MAX_LINE_LEN 128 +#define EX_DATAERR 1 +#define EX_UNAVAILABLE 3 +#define ED_TOK_DELAY "delay" +#define ED_TOK_PROB "prob" +#define ED_SEPARATORS " \t\n" +#define ED_TOK_PROFILE_NO "profile_no" + + +struct point { + double prob; /* y */ + double delay; /* x */ +}; + +struct profile { + char filename[128]; /* profile filename */ + int samples[ED_MAX_SAMPLES_NO+1]; /* may be shorter */ + int samples_no; /* actual len of samples[] */ +}; + +/* + * returns 1 if s is a non-negative number, with at least one '.' + */ +static int +is_valid_number(const char *s) +{ +#if 0 + int i, dots_found = 0; + int len = strlen(s); + + for (i = 0; i 1)) + return 0; +#endif + return 1; +} + +static int +compare_points(const void *vp1, const void *vp2) +{ + const struct point *p1 = vp1; + const struct point *p2 = vp2; + double res = 0; + + res = p1->prob - p2->prob; + if (res == 0) + res = p1->delay - p2->delay; + if (res < 0) + return -1; + else if (res > 0) + return 1; + else + return 0; +} + +#define ED_EFMT(s) 1,"error in %s at line %d: "#s,filename,lineno + +/* + * The points defined by the user are stored in the ponts structure. + * The number of user defined points is stored in points_no. + * We assume that The last point for the '1' value of the + * probability should be defined. (XXX add checks for this) + * The user defined sampling value is stored in samples_no. + * The resulting samples are in the "samples" pointer. + */ +static void +interpolate_samples(struct point *p, int points_no, + int *samples, int samples_no, const char *filename) +{ + double dy; /* delta on the y axis */ + double y; /* current value of y */ + double x; /* current value of x */ + double m; /* the y slope */ + int i; /* samples index */ + int curr; /* points current index */ + + dy = 1.0/samples_no; + y = 0; + + for (i=0, curr = 0; i < samples_no; i++, y+=dy) { + /* This statment move the curr pointer to the next point + * skipping the points with the same x value. We are + * guaranteed to exit from the loop because the + * last possible value of y is stricly less than 1 + * and the last possible value of the y points is 1 */ + while ( y >= p[curr+1].prob ) curr++; + + /* compute the slope of the curve */ + m = (p[curr+1].delay - p[curr].delay) / (p[curr+1].prob - p[curr].prob); + /* compute the x value starting from the current point */ + x = p[curr].delay + (y - p[curr].prob) * m; + samples[i] = x; + } + + /* add the last sample */ + samples[i] = p[curr+1].delay; +} + +#if 0 +static void +interpolate_samples_old(struct point *points, int points_no, + int *samples, int samples_no, const char *filename) +{ + int i; /* pointer to the sampled array */ + int j = 0; /* pointer to user defined samples */ + double dy; /* delta y */ + double y; /* current value of y */ + int x; /* computed value of x */ + double m; /* slope of the line */ + double y1, x1, y2, x2; /* two points of the current line */ + + /* make sure that there are enough points. */ + /* XXX Duplicated shoule be removed */ + if (points_no < 3) + errx(EX_DATAERR, "%s too few samples, need at least %d", + filename, 3); + + qsort(points, points_no, sizeof(struct point), compare_points); + + samples_no--; + dy = 1.0/samples_no; + printf("\nsamples no is %d dy is %f ", samples_no, dy); + + /* start with the first two points */ + y1 = points[j].prob * samples_no; + x1 = points[j].delay; + j++; + y2 = points[j].prob * samples_no; + x2 = points[j].delay; + + m = (y2-y1)/(x2-x1); + printf("\nStart"); + printf("\n\tCurrent points x1 y1 %f %f next point x2y2 %f %f m %f\n", + x1, y1, x2, y2, m); + + y = 0; + x = x1; + + for(i=0; i < samples_no+1; i++, y+=dy) { + printf("\ni:%d j:%d y:%f real y:%f", i, j, y, y*samples_no); + if ( (y*samples_no) >= y2 ) { /* move to the next point */ + j++; + if ( j >= points_no ) { + printf("\n\tNo more points, exit with j: %d i: %d and y:%f %f\n", + j, i, y, (y*samples_no)); + break; /* no more user defined points */ + } + /* load a new point */ + y1 = y2; + x1 = x2; + y2 = points[j].prob * samples_no; + x2 = points[j].delay; + m = (y2-y1)/(x2-x1); + if (x1==x2) { /* m = infinito */ + m = -1; + x = x2; + } + /* very small m problem */ + printf ("\ndelta %f\n", (y1 - y2)); + if (abs(y1 - y2) < 0.00001) { /* m = 0 XXX Should this magic number depend on samples_no ? */ + m = 0; + x = x2; + } + printf("\n\tCurrent points x1 y1 %f %f next point x2y2 %f %f (%f/%f)=m \n", + x1, y1, x2, y2, (y2-y1), (x2-x1), m); + } + printf("\n\tcompute step y %f x[%d]=%d ", + y, i, x); + if ((m != -1) && ( m != 0 )) { + x = x + (dy * samples_no)/m; + } + samples[i] = x; + printf(" dy %f x new %d\n", dy*samples_no, x); + printf(" m %f (dy * samples_no)/m %f \n", m, (dy * samples_no)/m); + } + + x = samples[i-1]; + printf("Finish i is %d samples_no is %d\n", i, samples_no); + /* The last point has a probability less than 1 */ + for (; i <= samples_no; i++) + samples[i] = x; +} +#endif + +static void +load_profile(struct profile *p) +{ + FILE *f; /* file handler */ + char line[ED_MAX_LINE_LEN]; + int lineno = 0; + int do_points = 0; + int delay_first = -1; + int i; + + struct point points[1000]; /* MAX_POINTS_NO */ + int points_no = 0; + + char *filename = p->filename; + f = fopen(filename, "r"); + if (f == NULL) { + err(EX_UNAVAILABLE, "fopen: %s", filename); + } + + + while (fgets(line, ED_MAX_LINE_LEN, f)) { /* read commands */ + char *s, *cur = line, *name = NULL, *arg = NULL; + + ++lineno; + + /* parse the line */ + while (cur) { + s = strsep(&cur, ED_SEPARATORS); + if (s == NULL || *s == '#') + break; + if (*s == '\0') + continue; + if (arg) + errx(ED_EFMT("too many arguments")); + if (name == NULL) + name = s; + else + arg = s; + } + + if (name == NULL) + continue; + + if (!strcasecmp(name, ED_TOK_DELAY)) { + if (do_points) + errx(ED_EFMT("duplicated token: %s"), name); + delay_first = 1; + do_points = 1; + continue; + } else if (!strcasecmp(name, ED_TOK_PROB)) { + if (do_points) + errx(ED_EFMT("duplicated token: %s"), name); + delay_first = 0; + do_points = 1; + continue; + } + if (!strcasecmp(name, ED_TOK_PROFILE_NO)) { + int p_no = atof(arg); + if (p_no <= 0) { + p_no = 100; + printf("invalid interpolation samples, using %d\n", + p_no); + } + if (p_no > ED_MAX_SAMPLES_NO) { + p_no = ED_MAX_SAMPLES_NO; + printf("invalid interpolation samples, using %d\n", + p_no); + } + + p->samples_no = p_no; + continue; + + } else if (do_points) { + if (!is_valid_number(name) || !is_valid_number(arg)) + errx(ED_EFMT("invalid point found")); + if (delay_first) { + points[points_no].delay = atof(name); + points[points_no].prob = atof(arg); + } else { + points[points_no].delay = atof(arg); + points[points_no].prob = atof(name); + } + if (points[points_no].prob > 1.0) + errx(ED_EFMT("probability greater than 1.0")); + ++points_no; + /* XXX no more that 1000 */ + continue; + } else { + errx(ED_EFMT("unrecognised command '%s'"), name); + } + } + + for(i=0; i < p->samples_no; i++) { + p->samples[i] = 666; + } + + /* This code assume the user define a value of X for the sampling value, + * and that: + * - the value stored in the emulator structure is X; + * - the allocated structure for the samples is X+1; + */ + interpolate_samples(points, points_no, p->samples, p->samples_no, filename); + + // User defined samples + printf("\nLoaded %d points:\n", points_no); + for(i=0; i < points_no; i++) { + printf("%f %f\n", points[i].prob, points[i].delay); + } + printf("\n"); + printf("The sample value is %d \n", p->samples_no); + +} + +int main(int argc, char **argv) +{ + if (argc < 2) { + printf("Usage: ./interpolation \n"); + return -1; + } + + char *filename; + filename = argv[1]; + + struct profile p; + int i; + + strncpy(p.filename, filename, 128); + load_profile(&p); + printf("-----------\n"); + for (i=0; i<=p.samples_no; i++) + printf("%d %d\n", i, p.samples[i]); + printf("-----------\n"); + return 0; +} diff --git a/test/main.c b/test/main.c new file mode 100644 index 0000000..85fc621 --- /dev/null +++ b/test/main.c @@ -0,0 +1,636 @@ +/* + * $Id: main.c 5626 2010-03-04 21:55:22Z luigi $ + * + * Testing program for schedulers + * + * The framework include a simple controller which, at each + * iteration, decides whether we can enqueue and/or dequeue. + * Then the mainloop runs the required number of tests, + * keeping track of statistics. + */ + +#include "dn_test.h" + +struct q_list { + struct list_head h; +}; + +struct cfg_s { + int ac; + char * const *av; + + const char *name; + int loops; + struct timeval time; + + /* running counters */ + uint32_t _enqueue; + uint32_t drop; + uint32_t pending; + uint32_t dequeue; + + /* generator parameters */ + int th_min, th_max; + int maxburst; + int lmin, lmax; /* packet len */ + int flows; /* number of flows */ + int flowsets; /* number of flowsets */ + int wsum; /* sum of weights of all flows */ + int max_y; /* max random number in the generation */ + int cur_y, cur_fs; /* used in generation, between 0 and max_y - 1 */ + const char *fs_config; /* flowset config */ + int can_dequeue; + int burst; /* count of packets sent in a burst */ + struct mbuf *tosend; /* packet to send -- also flag to enqueue */ + + struct mbuf *freelist; + + struct mbuf *head, *tail; /* a simple tailq */ + + /* scheduler hooks */ + int (*enq)(struct dn_sch_inst *, struct dn_queue *, + struct mbuf *); + struct mbuf * (*deq)(struct dn_sch_inst *); + /* size of the three fields including sched-specific areas */ + int schk_len; + int q_len; /* size of a queue including sched-fields */ + int si_len; /* size of a sch_inst including sched-fields */ + char *q; /* array of flow queues */ + /* use a char* because size is variable */ + struct dn_fsk *fs; /* array of flowsets */ + struct dn_sch_inst *si; + struct dn_schk *sched; + + /* generator state */ + int state; /* 0 = going up, 1: going down */ + + /* + * We keep lists for each backlog level, and always serve + * the one with shortest backlog. llmask contains a bitmap + * of lists, and ll are the heads of the lists. The last + * entry (BACKLOG) contains all entries considered 'full' + * XXX to optimize things, entry i could contain queues with + * 2^{i-1}+1 .. 2^i entries. + */ +#define BACKLOG 30 + uint32_t llmask; + struct list_head ll[BACKLOG + 10]; +}; + +/* FI2Q and Q2FI converts from flow_id to dn_queue and back. + * We cannot easily use pointer arithmetic because it is variable size. + */ +#define FI2Q(c, i) ((struct dn_queue *)((c)->q + (c)->q_len * (i))) +#define Q2FI(c, q) (((char *)(q) - (c)->q)/(c)->q_len) + +int debug = 0; + +struct dn_parms dn_cfg; + +static void controller(struct cfg_s *c); + +/* release a packet: put the mbuf in the freelist, and the queue in + * the bucket. + */ +int +drop(struct cfg_s *c, struct mbuf *m) +{ + struct dn_queue *q; + int i; + + c->drop++; + q = FI2Q(c, m->flow_id); + i = q->ni.length; // XXX or ffs... + + ND("q %p id %d current length %d", q, m->flow_id, i); + if (i < BACKLOG) { + struct list_head *h = &q->ni.h; + c->llmask &= ~(1<<(i+1)); + c->llmask |= (1<<(i)); + list_del(h); + list_add_tail(h, &c->ll[i]); + } + m->m_nextpkt = c->freelist; + c->freelist = m; + return 0; +} + +/* dequeue returns NON-NULL when a packet is dropped */ +static int +enqueue(struct cfg_s *c, void *_m) +{ + struct mbuf *m = _m; + if (c->enq) + return c->enq(c->si, FI2Q(c, m->flow_id), m); + if (c->head == NULL) + c->head = m; + else + c->tail->m_nextpkt = m; + c->tail = m; + return 0; /* default - success */ +} + +/* dequeue returns NON-NULL when a packet is available */ +static void * +dequeue(struct cfg_s *c) +{ + struct mbuf *m; + if (c->deq) + return c->deq(c->si); + if ((m = c->head)) { + m = c->head; + c->head = m->m_nextpkt; + m->m_nextpkt = NULL; + } + return m; +} + +static int +mainloop(struct cfg_s *c) +{ + int i; + struct mbuf *m; + + for (i=0; i < c->loops; i++) { + /* implement histeresis */ + controller(c); + DX(3, "loop %d enq %d send %p rx %d", + i, c->_enqueue, c->tosend, c->can_dequeue); + if ( (m = c->tosend) ) { + c->_enqueue++; + if (enqueue(c, m)) { + drop(c, m); + ND("loop %d enqueue fail", i ); + } else { + ND("enqueue ok"); + c->pending++; + } + } + if (c->can_dequeue) { + c->dequeue++; + if ((m = dequeue(c))) { + c->pending--; + drop(c, m); + c->drop--; /* compensate */ + } + } + } + DX(1, "mainloop ends %d", i); + return 0; +} + +int +dump(struct cfg_s *c) +{ + int i; + struct dn_queue *q; + + for (i=0; i < c->flows; i++) { + q = FI2Q(c, i); + DX(1, "queue %4d tot %10lld", i, q->ni.tot_bytes); + } + DX(1, "done %d loops\n", c->loops); + return 0; +} + +/* interpret a number in human form */ +static long +getnum(const char *s, char **next, const char *key) +{ + char *end = NULL; + long l; + + if (next) /* default */ + *next = NULL; + if (s && *s) { + DX(3, "token is <%s> %s", s, key ? key : "-"); + l = strtol(s, &end, 0); + } else { + DX(3, "empty string"); + l = -1; + } + if (l < 0) { + DX(2, "invalid %s for %s", s ? s : "NULL", (key ? key : "") ); + return 0; // invalid + } + if (!end || !*end) + return l; + if (*end == 'n') + l = -l; /* multiply by n */ + else if (*end == 'K') + l = l*1000; + else if (*end == 'M') + l = l*1000000; + else if (*end == 'k') + l = l*1024; + else if (*end == 'm') + l = l*1024*1024; + else if (*end == 'w') + ; + else {/* not recognized */ + D("suffix %s for %s, next %p", end, key, next); + end--; + } + end++; + DX(3, "suffix now %s for %s, next %p", end, key, next); + if (next && *end) { + DX(3, "setting next to %s for %s", end, key); + *next = end; + } + return l; +} + +/* + * flowsets are a comma-separated list of + * weight:maxlen:flows + * indicating how many flows are hooked to that fs. + * Both weight and range can be min-max-steps. + * In a first pass we just count the number of flowsets and flows, + * in a second pass we complete the setup. + */ +static void +parse_flowsets(struct cfg_s *c, const char *fs, int pass) +{ + char *s, *cur, *next; + int n_flows = 0, n_fs = 0, wsum = 0; + int i, j; + struct dn_fs *prev = NULL; + + DX(3, "--- pass %d flows %d flowsets %d", pass, c->flows, c->flowsets); + if (pass == 0) + c->fs_config = fs; + s = c->fs_config ? strdup(c->fs_config) : NULL; + if (s == NULL) { + if (pass == 0) + D("no fsconfig"); + return; + } + for (next = s; (cur = strsep(&next, ","));) { + char *p = NULL; + int w, w_h, w_steps, wi; + int len, len_h, l_steps, li; + int flows; + + w = getnum(strsep(&cur, ":"), &p, "weight"); + if (w <= 0) + w = 1; + w_h = p ? getnum(p+1, &p, "weight_max") : w; + w_steps = p ? getnum(p+1, &p, "w_steps") : (w_h == w ?1:2); + len = getnum(strsep(&cur, ":"), &p, "len"); + if (len <= 0) + len = 1000; + len_h = p ? getnum(p+1, &p, "len_max") : len; + l_steps = p ? getnum(p+1, &p, "l_steps") : (len_h == len ? 1 : 2); + flows = getnum(strsep(&cur, ":"), NULL, "flows"); + if (flows == 0) + flows = 1; + DX(4, "weight %d..%d (%d) len %d..%d (%d) flows %d", + w, w_h, w_steps, len, len_h, l_steps, flows); + if (w == 0 || w_h < w || len == 0 || len_h < len || + flows == 0) { + DX(4,"wrong parameters %s", fs); + return; + } + n_flows += flows * w_steps * l_steps; + for (i = 0; i < w_steps; i++) { + wi = w + ((w_h - w)* i)/(w_steps == 1 ? 1 : (w_steps-1)); + for (j = 0; j < l_steps; j++, n_fs++) { + struct dn_fs *fs = &c->fs[n_fs].fs; // tentative + int x; + + li = len + ((len_h - len)* j)/(l_steps == 1 ? 1 : (l_steps-1)); + x = (wi*2048)/li; + DX(3, "----- fs %4d weight %4d lmax %4d X %4d flows %d", + n_fs, wi, li, x, flows); + if (pass == 0) + continue; + if (c->fs == NULL || c->flowsets <= n_fs) { + D("error in number of flowsets"); + return; + } + wsum += wi * flows; + fs->par[0] = wi; + fs->par[1] = li; + fs->index = n_fs; + fs->n_flows = flows; + fs->cur = fs->first_flow = prev==NULL ? 0 : prev->next_flow; + fs->next_flow = fs->first_flow + fs->n_flows; + fs->y = x * flows; + fs->base_y = (prev == NULL) ? 0 : prev->next_y; + fs->next_y = fs->base_y + fs->y; + prev = fs; + } + } + } + c->max_y = prev ? prev->base_y + prev->y : 0; + c->flows = n_flows; + c->flowsets = n_fs; + c->wsum = wsum; + if (pass == 0) + return; + + /* now link all flows to their parent flowsets */ + DX(1,"%d flows on %d flowsets max_y %d", c->flows, c->flowsets, c->max_y); + for (i=0; i < c->flowsets; i++) { + struct dn_fs *fs = &c->fs[i].fs; + DX(1, "fs %3d w %5d l %4d flow %5d .. %5d y %6d .. %6d", + i, fs->par[0], fs->par[1], + fs->first_flow, fs->next_flow, + fs->base_y, fs->next_y); + for (j = fs->first_flow; j < fs->next_flow; j++) { + struct dn_queue *q = FI2Q(c, j); + q->fs = &c->fs[i]; + } + } +} + +static int +init(struct cfg_s *c) +{ + int i; + int ac = c->ac; + char * const *av = c->av; + + c->si_len = sizeof(struct dn_sch_inst); + c->q_len = sizeof(struct dn_queue); + moduledata_t *mod = NULL; + struct dn_alg *p = NULL; + + c->th_min = 0; + c->th_max = -20;/* 20 packets per flow */ + c->lmin = c->lmax = 1280; /* packet len */ + c->flows = 1; + c->flowsets = 1; + c->name = "null"; + ac--; av++; + while (ac > 1) { + if (!strcmp(*av, "-n")) { + c->loops = getnum(av[1], NULL, av[0]); + } else if (!strcmp(*av, "-d")) { + debug = atoi(av[1]); + } else if (!strcmp(*av, "-alg")) { + extern moduledata_t *_g_dn_fifo; + extern moduledata_t *_g_dn_wf2qp; + extern moduledata_t *_g_dn_rr; + extern moduledata_t *_g_dn_qfq; +#ifdef WITH_KPS + extern moduledata_t *_g_dn_kps; +#endif + if (!strcmp(av[1], "rr")) + mod = _g_dn_rr; + else if (!strcmp(av[1], "wf2qp")) + mod = _g_dn_wf2qp; + else if (!strcmp(av[1], "fifo")) + mod = _g_dn_fifo; + else if (!strcmp(av[1], "qfq")) + mod = _g_dn_qfq; +#ifdef WITH_KPS + else if (!strcmp(av[1], "kps")) + mod = _g_dn_kps; +#endif + else + mod = NULL; + c->name = mod ? mod->name : "NULL"; + DX(3, "using scheduler %s", c->name); + } else if (!strcmp(*av, "-len")) { + c->lmin = getnum(av[1], NULL, av[0]); + c->lmax = c->lmin; + DX(3, "setting max to %d", c->th_max); + } else if (!strcmp(*av, "-burst")) { + c->maxburst = getnum(av[1], NULL, av[0]); + DX(3, "setting max to %d", c->th_max); + } else if (!strcmp(*av, "-qmax")) { + c->th_max = getnum(av[1], NULL, av[0]); + DX(3, "setting max to %d", c->th_max); + } else if (!strcmp(*av, "-qmin")) { + c->th_min = getnum(av[1], NULL, av[0]); + DX(3, "setting min to %d", c->th_min); + } else if (!strcmp(*av, "-flows")) { + c->flows = getnum(av[1], NULL, av[0]); + DX(3, "setting flows to %d", c->flows); + } else if (!strcmp(*av, "-flowsets")) { + parse_flowsets(c, av[1], 0); + DX(3, "setting flowsets to %d", c->flowsets); + } else { + D("option %s not recognised, ignore", *av); + } + ac -= 2; av += 2; + } + if (c->maxburst <= 0) + c->maxburst = 1; + if (c->loops <= 0) + c->loops = 1; + if (c->flows <= 0) + c->flows = 1; + if (c->flowsets <= 0) + c->flowsets = 1; + if (c->lmin <= 0) + c->lmin = 1; + if (c->lmax <= 0) + c->lmax = 1; + /* multiply by N */ + if (c->th_min < 0) + c->th_min = c->flows * -c->th_min; + if (c->th_max < 0) + c->th_max = c->flows * -c->th_max; + if (c->th_max <= c->th_min) + c->th_max = c->th_min + 1; + if (mod) { + p = mod->p; + DX(3, "using module %s f %p p %p", mod->name, mod->f, mod->p); + DX(3, "modname %s ty %d", p->name, p->type); + c->enq = p->enqueue; + c->deq = p->dequeue; + c->si_len += p->si_datalen; + c->q_len += p->q_datalen; + c->schk_len += p->schk_datalen; + } + /* allocate queues, flowsets and one scheduler */ + c->q = calloc(c->flows, c->q_len); + c->fs = calloc(c->flowsets, sizeof(struct dn_fsk)); + c->si = calloc(1, c->si_len); + c->sched = calloc(c->flows, c->schk_len); + if (c->q == NULL || c->fs == NULL) { + D("error allocating memory for flows"); + exit(1); + } + c->si->sched = c->sched; + if (p) { + if (p->config) + p->config(c->sched); + if (p->new_sched) + p->new_sched(c->si); + } + /* parse_flowsets links queues to their flowsets */ + parse_flowsets(c, av[1], 1); + /* complete the work calling new_fsk */ + for (i = 0; i < c->flowsets; i++) { + if (c->fs[i].fs.par[1] == 0) + c->fs[i].fs.par[1] = 1000; /* default pkt len */ + c->fs[i].sched = c->sched; + if (p && p->new_fsk) + p->new_fsk(&c->fs[i]); + } + + /* initialize the lists for the generator, and put + * all flows in the list for backlog = 0 + */ + for (i=0; i <= BACKLOG+5; i++) + INIT_LIST_HEAD(&c->ll[i]); + + for (i = 0; i < c->flows; i++) { + struct dn_queue *q = FI2Q(c, i); + if (q->fs == NULL) + q->fs = &c->fs[0]; /* XXX */ + q->_si = c->si; + if (p && p->new_queue) + p->new_queue(q); + INIT_LIST_HEAD(&q->ni.h); + list_add_tail(&q->ni.h, &c->ll[0]); + } + c->llmask = 1; + return 0; +} + + +int +main(int ac, char *av[]) +{ + struct cfg_s c; + struct timeval end; + double ll; + int i; + char msg[40]; + + bzero(&c, sizeof(c)); + c.ac = ac; + c.av = av; + init(&c); + gettimeofday(&c.time, NULL); + mainloop(&c); + gettimeofday(&end, NULL); + end.tv_sec -= c.time.tv_sec; + end.tv_usec -= c.time.tv_usec; + if (end.tv_usec < 0) { + end.tv_usec += 1000000; + end.tv_sec--; + } + c.time = end; + ll = end.tv_sec*1000000 + end.tv_usec; + ll *= 1000; /* convert to nanoseconds */ + ll /= c._enqueue; + sprintf(msg, "1::%d", c.flows); + D("%-8s n %d %d time %d.%06d %8.3f qlen %d %d flows %s drops %d", + c.name, c._enqueue, c.loops, + (int)c.time.tv_sec, (int)c.time.tv_usec, ll, + c.th_min, c.th_max, + c.fs_config ? c.fs_config : msg, c.drop); + dump(&c); + DX(1, "done ac %d av %p", ac, av); + for (i=0; i < ac; i++) + DX(1, "arg %d %s", i, av[i]); + return 0; +} + +/* + * The controller decides whether in this iteration we should send + * (the packet is in c->tosend) and/or receive (flag c->can_dequeue) + */ +static void +controller(struct cfg_s *c) +{ + struct mbuf *m; + struct dn_fs *fs; + int flow_id; + + /* histeresis between max and min */ + if (c->state == 0 && c->pending >= c->th_max) + c->state = 1; + else if (c->state == 1 && c->pending <= c->th_min) + c->state = 0; + ND(1, "state %d pending %2d", c->state, c->pending); + c->can_dequeue = c->state; + c->tosend = NULL; + if (c->state) + return; + + if (1) { + int i; + struct dn_queue *q; + struct list_head *h; + + i = ffs(c->llmask) - 1; + if (i < 0) { + DX(2, "no candidate"); + c->can_dequeue = 1; + return; + } + h = &c->ll[i]; + ND(1, "backlog %d p %p prev %p next %p", i, h, h->prev, h->next); + q = list_first_entry(h, struct dn_queue, ni.h); + list_del(&q->ni.h); + flow_id = Q2FI(c, q); + DX(2, "extracted flow %p %d backlog %d", q, flow_id, i); + if (list_empty(h)) { + ND(2, "backlog %d empty", i); + c->llmask &= ~(1<ni.h, h+1); + ND(1, " after %d p %p prev %p next %p", i+1, h+1, h[1].prev, h[1].next); + if (i < BACKLOG) { + ND(2, "backlog %d full", i+1); + c->llmask |= 1<<(1+i); + } + fs = &q->fs->fs; + c->cur_fs = q->fs - c->fs; + fs->cur = flow_id; + } else { + /* XXX this does not work ? */ + /* now decide whom to send the packet, and the length */ + /* lookup in the flow table */ + if (c->cur_y >= c->max_y) { /* handle wraparound */ + c->cur_y = 0; + c->cur_fs = 0; + } + fs = &c->fs[c->cur_fs].fs; + flow_id = fs->cur++; + if (fs->cur >= fs->next_flow) + fs->cur = fs->first_flow; + c->cur_y++; + if (c->cur_y >= fs->next_y) + c->cur_fs++; + } + + /* construct a packet */ + if (c->freelist) { + m = c->tosend = c->freelist; + c->freelist = c->freelist->m_nextpkt; + } else { + m = c->tosend = calloc(1, sizeof(struct mbuf)); + } + if (m == NULL) + return; + + m->cfg = c; + m->m_nextpkt = NULL; + m->m_pkthdr.len = fs->par[1]; // XXX maxlen + m->flow_id = flow_id; + + ND(2,"y %6d flow %5d fs %3d weight %4d len %4d", + c->cur_y, m->flow_id, c->cur_fs, + fs->par[0], m->m_pkthdr.len); + +} + +/* +Packet allocation: +to achieve a distribution that matches weights, for each X=w/lmax class +we should generate a number of packets proportional to Y = X times the number +of flows in the class. +So we construct an array with the cumulative distribution of Y's, +and use it to identify the flow via inverse mapping (if the Y's are +not too many we can use an array for the lookup). In practice, +each flow will have X entries [virtually] pointing to it. + +*/ diff --git a/test/memory_leak.sh b/test/memory_leak.sh new file mode 100644 index 0000000..9bdf093 --- /dev/null +++ b/test/memory_leak.sh @@ -0,0 +1,26 @@ +#!/bin/sh +# this script execute N times the command CMD +# collecting the memory usage on a file. +# The value of the Dirty memory should not increase +# between tests. + +BASE_NAME=ipfw_r5808_ +N=10000 +CMD1="/sbin/insmod ../dummynet2/ipfw_mod.ko" +CMD2="/sbin/rmmod ipfw_mod" + +# main +# remove any previous loaded module +/sbin/rmmod ipfw_mod + +# pre + +for n in `seq $N`; do + $CMD1 + $CMD2 + [ $n = 10 ] && cat /proc/meminfo > /tmp/${BASE_NAME}_${n} + [ $n = 100 ] && cat /proc/meminfo > /tmp/${BASE_NAME}_${n} + [ $n = 1000 ] && cat /proc/meminfo > /tmp/${BASE_NAME}_${n} +done; + +# post diff --git a/test/mylist.h b/test/mylist.h new file mode 100644 index 0000000..b546fc2 --- /dev/null +++ b/test/mylist.h @@ -0,0 +1,49 @@ +/* + * $Id: mylist.h 5626 2010-03-04 21:55:22Z luigi $ + * + * linux-like bidirectional lists + */ + +#ifndef _MYLIST_H +#define _MYLIST_H +struct list_head { + struct list_head *prev, *next; +}; + +#define INIT_LIST_HEAD(l) do { (l)->prev = (l)->next = (l); } while (0) +#define list_empty(l) ( (l)->next == l ) +static inline void +__list_add(struct list_head *o, struct list_head *prev, + struct list_head *next) +{ + next->prev = o; + o->next = next; + o->prev = prev; + prev->next = o; +} + +static inline void +list_add_tail(struct list_head *o, struct list_head *head) +{ + __list_add(o, head->prev, head); +} + +#define list_first_entry(pL, ty, member) \ + (ty *)((char *)((pL)->next) - offsetof(ty, member)) + +static inline void +__list_del(struct list_head *prev, struct list_head *next) +{ + next->prev = prev; + prev->next = next; +} + +static inline void +list_del(struct list_head *entry) +{ + ND("called on %p", entry); + __list_del(entry->prev, entry->next); + entry->next = entry->prev = NULL; +} + +#endif /* _MYLIST_H */ diff --git a/test/profile_bench1 b/test/profile_bench1 new file mode 100644 index 0000000..797650f --- /dev/null +++ b/test/profile_bench1 @@ -0,0 +1,26 @@ +profile_no 100 +delay prob +207 0.000264 +255 0.034117 +270 0.072280 +279 0.106749 +288 0.148604 +298 0.184304 +302 0.202194 +353 0.384541 +423 0.588842 +510 0.782126 +516 0.800970 +545 0.845706 +553 0.861411 +573 0.889430 +586 0.912117 +620 0.920003 +661 0.938308 +695 0.944191 +740 0.949112 +765 0.952598 +848 0.957109 +1379 0.983768 +1555 0.983778 +1649 1 diff --git a/test/profile_bench2 b/test/profile_bench2 new file mode 100644 index 0000000..c733868 --- /dev/null +++ b/test/profile_bench2 @@ -0,0 +1,7 @@ +samples 10 +delay prob +0 0 +250 0 +250 0.5 +500 0.5 +500 1 diff --git a/test/profile_bench3 b/test/profile_bench3 new file mode 100644 index 0000000..5d1722e --- /dev/null +++ b/test/profile_bench3 @@ -0,0 +1,5 @@ +profile_no 100 +delay prob +0 0 +50 0.5 +100 1 diff --git a/test/test_dn_heap.c b/test/test_dn_heap.c new file mode 100644 index 0000000..7d3dc05 --- /dev/null +++ b/test/test_dn_heap.c @@ -0,0 +1,162 @@ +/*- + * Copyright (c) 1998-2002,2010 Luigi Rizzo, Universita` di Pisa + * All rights reserved + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Userland code for testing binary heaps and hash tables + * + * $Id: test_dn_heap.c 6131 2010-04-22 15:37:36Z svn_panicucci $ + */ + +#include +#include + +#include +#include +#include +#include "dn_test.h" +#include "dn_heap.h" +#define log(x, arg...) fprintf(stderr, ## arg) +#define panic(x...) fprintf(stderr, ## x), exit(1) + +#include + +struct x { + struct x *ht_link; + char buf[0]; +}; + +uint32_t hf(uintptr_t key, int flags, void *arg) +{ + return (flags & DNHT_KEY_IS_OBJ) ? + ((struct x *)key)->buf[0] : *(char *)key; +} + +int matchf(void *obj, uintptr_t key, int flags, void *arg) +{ + char *s = (flags & DNHT_KEY_IS_OBJ) ? + ((struct x *)key)->buf : (char *)key; + return (strcmp(((struct x *)obj)->buf, s) == 0); +} + +void *newfn(uintptr_t key, int flags, void *arg) +{ + char *s = (char *)key; + struct x *p = malloc(sizeof(*p) + 1 + strlen(s)); + if (p) + strcpy(p->buf, s); + return p; +} + +char *strings[] = { + "undici", "unico", "doppio", "devoto", + "uno", "due", "tre", "quattro", "cinque", "sei", + "uno", "due", "tre", "quattro", "cinque", "sei", + NULL, +}; + +int doprint(void *_x, void *arg) +{ + struct x *x = _x; + printf("found element <%s>\n", x->buf); + return (int)arg; +} + +static void +test_hash() +{ + char **p; + struct dn_ht *h; + uintptr_t x = 0; + uintptr_t x1 = 0; + + /* first, find and allocate */ + h = dn_ht_init(NULL, 10, 0, hf, matchf, newfn); + + for (p = strings; *p; p++) { + dn_ht_find(h, (uintptr_t)*p, DNHT_INSERT, NULL); + } + dn_ht_scan(h, doprint, 0); + printf("/* second -- find without allocate */\n"); + h = dn_ht_init(NULL, 10, 0, hf, matchf, NULL); + for (p = strings; *p; p++) { + void **y = newfn((uintptr_t)*p, 0, NULL); + if (x == 0) + x = (uintptr_t)y; + else { + if (x1 == 0) + x1 = (uintptr_t)*p; + } + dn_ht_find(h, (uintptr_t)y, DNHT_INSERT | DNHT_KEY_IS_OBJ, NULL); + } + dn_ht_scan(h, doprint, 0); + printf("remove %p gives %p\n", (void *)x, + dn_ht_find(h, x, DNHT_KEY_IS_OBJ | DNHT_REMOVE, NULL)); + printf("remove %p gives %p\n", (void *)x, + dn_ht_find(h, x, DNHT_KEY_IS_OBJ | DNHT_REMOVE, NULL)); + printf("remove %p gives %p\n", (void *)x, + dn_ht_find(h, x1, DNHT_REMOVE, NULL)); + printf("remove %p gives %p\n", (void *)x, + dn_ht_find(h, x1, DNHT_REMOVE, NULL)); + dn_ht_scan(h, doprint, 0); +} + +int +main(int argc, char *argv[]) +{ + struct dn_heap h; + int i, n, n2, n3; + + test_hash(); + return 0; + + /* n = elements, n2 = cycles */ + n = (argc > 1) ? atoi(argv[1]) : 0; + if (n <= 0 || n > 1000000) + n = 100; + n2 = (argc > 2) ? atoi(argv[2]) : 0; + if (n2 <= 0) + n = 1000000; + n3 = (argc > 3) ? atoi(argv[3]) : 0; + bzero(&h, sizeof(h)); + heap_init(&h, n, -1); + while (n2-- > 0) { + uint64_t prevk = 0; + for (i=0; i < n; i++) + heap_insert(&h, n3 ? n-i: random(), (void *)(100+i)); + + for (i=0; h.elements > 0; i++) { + uint64_t k = h.p[0].key; + if (k < prevk) + panic("wrong sequence\n"); + prevk = k; + if (0) + printf("%d key %llu, val %p\n", + i, h.p[0].key, h.p[0].object); + heap_extract(&h, NULL); + } + } + return 0; +} diff --git a/test/test_dn_sched.c b/test/test_dn_sched.c new file mode 100644 index 0000000..65bbf18 --- /dev/null +++ b/test/test_dn_sched.c @@ -0,0 +1,89 @@ +/* + * $Id: test_dn_sched.c 5626 2010-03-04 21:55:22Z luigi $ + * + * library functions for userland testing of dummynet schedulers + */ + +#include "dn_test.h" + +void +m_freem(struct mbuf *m) +{ + printf("free %p\n", m); +} + +int +dn_sched_modevent(module_t mod, int cmd, void *arg) +{ + return 0; +} + +void +dn_free_pkts(struct mbuf *m) +{ + struct mbuf *x; + while ( (x = m) ) { + m = m->m_nextpkt; + m_freem(x); + } +} + +int +dn_delete_queue(void *_q, void *do_free) +{ + struct dn_queue *q = _q; + if (q->mq.head) + dn_free_pkts(q->mq.head); + free(q); + return 0; +} + +/* + * This is a simplified function for testing purposes, which does + * not implement statistics or random loss. + * Enqueue a packet in q, subject to space and queue management policy + * (whose parameters are in q->fs). + * Update stats for the queue and the scheduler. + * Return 0 on success, 1 on drop. The packet is consumed anyways. + */ +int +dn_enqueue(struct dn_queue *q, struct mbuf* m, int drop) +{ + if (drop) + goto drop; + if (q->ni.length >= 200) + goto drop; + mq_append(&q->mq, m); + q->ni.length++; + q->ni.tot_bytes += m->m_pkthdr.len; + return 0; + +drop: + q->ni.drops++; + return 1; +} + +int +ipdn_bound_var(int *v, int dflt, int lo, int hi, const char *msg) +{ + if (*v < lo) { + *v = dflt; + } else if (*v > hi) { + *v = hi; + } + return *v; +} + +#ifndef __FreeBSD__ +int +fls(int mask) +{ + int bit; + + if (mask == 0) + return (0); + for (bit = 1; mask != 1; bit++) + mask = (unsigned int)mask >> 1; + return (bit); +} +#endif