--- A DIFFUSE feature kernel module which calculates per-flow minimum, mean, --- maximum and standard deviation interarrival time statistics for use in --- classification decisions. --- --- Sponsored by: FreeBSD Foundation --- Reviewed by: bz --- MFC after: 1 month --- diff -r 072dbd4da936 sys/modules/diffuse/Makefile --- a/sys/modules/diffuse/Makefile Sun Sep 25 17:27:48 2011 +1000 +++ b/sys/modules/diffuse/Makefile Sun Sep 25 17:32:04 2011 +1000 @@ -1,5 +1,6 @@ # $FreeBSD$ -SUBDIR= diffuse +SUBDIR= diffuse \ + diffuse_feature_iat .include diff -r 072dbd4da936 sys/modules/diffuse/diffuse_feature_iat/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/modules/diffuse/diffuse_feature_iat/Makefile Sun Sep 25 17:32:04 2011 +1000 @@ -0,0 +1,17 @@ +# $FreeBSD$ + +.include + +.PATH: ${.CURDIR}/../../../netinet/ipfw +KMOD= diffuse_feature_iat +SRCS= diffuse_feature_iat.c \ + opt_inet6.h + +.if !defined(KERNBUILDDIR) +.if ${MK_INET6_SUPPORT} != "no" +opt_inet6.h: + echo "#define INET6 1" > ${.TARGET} +.endif +.endif + +.include diff -r 072dbd4da936 sys/netinet/ipfw/diffuse_feature_iat.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/netinet/ipfw/diffuse_feature_iat.c Sun Sep 25 17:32:04 2011 +1000 @@ -0,0 +1,447 @@ +/*- + * Copyright (c) 2010-2011 + * Swinburne University of Technology, Melbourne, Australia. + * All rights reserved. + * + * This software was developed at the Centre for Advanced Internet + * Architectures, Swinburne University of Technology, by Sebastian Zander, made + * possible in part by a gift from The Cisco University Research Program Fund, a + * corporate advised fund of Silicon Valley Community Foundation. + * + * 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. + */ + +/* + * DIFFUSE packet inter-arrival time feature + */ + +#include +__FBSDID("$FreeBSD$"); + +#ifndef _KERNEL +#include +#include +#endif + +#ifdef _KERNEL +#include +#include +#include +#include +#include +#endif /* _KERNEL */ +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#ifdef _KERNEL +#include +#include +#else +#define KPI_USER_COMPAT +#include +#endif /* _KERNEL */ + +/* + * Feature inter-arrival time computes minimum, mean, maximum and std deviation + * of inter-arrival time (time between two consecutive packets). + */ + +/* If we are linked from userspace, only put the declaration here. */ +#ifdef _KERNEL +IAT_STAT_NAMES; +#else +IAT_STAT_NAMES_DECL; +#endif + +/* State for jump windows. */ +typedef struct iat_jump_win_state { + uint32_t min; + uint32_t max; + uint64_t sum; + uint64_t sqsum; + int jump; + int first; +} iat_jump_win_state_t; + +/* Per flow data (ring buffer). */ +typedef struct iat_fdata { + int full; + int changed; + int index; + struct timeval last_abs_time; + /* + * We can only store intervals <=4294 seconds and stats are signed so + * can only be <=2147 seconds, but default flow timeouts are much + * smaller. + */ + uint32_t *iats; + iat_jump_win_state_t *jstate; +} iat_fdata_t; + +static int iat_reset_stats(struct cdata *cdata, struct fdata *fdata); + +static int +iat_init_instance(struct cdata *cdata, struct di_oid *params) +{ + di_feature_iat_cnf_t *cnf, *p; + + cdata->conf = malloc(sizeof(di_feature_iat_cnf_t), M_DIFFUSE, + M_NOWAIT | M_ZERO); + cnf = (di_feature_iat_cnf_t *)cdata->conf; + + /* Set default configuration. */ + cnf->iat_window = DEFAULT_IAT_WINDOW; + cnf->iat_partial_window = 0; + cnf->iat_ts_acc = IAT_TS_ACC_NORMAL; + cnf->iat_jump_window = 0; + cnf->iat_prec = 1; + + /* Set configuration. */ + if (params != NULL) { + p = (di_feature_iat_cnf_t *)params; + + if (p->iat_window != -1) + cnf->iat_window = p->iat_window; + + if (p->iat_partial_window != -1) + cnf->iat_partial_window = p->iat_partial_window; + + if (p->iat_ts_acc != -1) + cnf->iat_ts_acc = p->iat_ts_acc; + + if (p->iat_jump_window != -1) + cnf->iat_jump_window = p->iat_jump_window; + + if (p->iat_prec != -1) + cnf->iat_prec = p->iat_prec; + } + + return (0); +} + +static int +iat_destroy_instance(struct cdata *cdata) +{ + + free(cdata->conf, M_DIFFUSE); + + return (0); +} + + static int +iat_get_conf(struct cdata *cdata, struct di_oid *cbuf, int size_only) +{ + + if (!size_only) + memcpy(cbuf, cdata->conf, sizeof(di_feature_iat_cnf_t)); + + return (sizeof(di_feature_iat_cnf_t)); +} + +static int +iat_init_stats(struct cdata *cdata, struct fdata *fdata) +{ + di_feature_iat_cnf_t *cnf; + iat_fdata_t *data; + + cnf = (di_feature_iat_cnf_t *)cdata->conf; + + fdata->data = malloc(sizeof(iat_fdata_t), M_DIFFUSE, M_NOWAIT | M_ZERO); + data = (iat_fdata_t *)fdata->data; + + if (!cnf->iat_jump_window) + data->iats = malloc((cnf->iat_window - 1) * sizeof(uint32_t), + M_DIFFUSE, M_NOWAIT | M_ZERO); + else + data->jstate = malloc(sizeof(iat_jump_win_state_t), M_DIFFUSE, + M_NOWAIT | M_ZERO); + + fdata->stats = malloc(IAT_NO_STATS * sizeof(int32_t), M_DIFFUSE, + M_NOWAIT | M_ZERO); + iat_reset_stats(cdata, fdata); + + return (0); +} + +static int +iat_destroy_stats(struct cdata *cdata, struct fdata *fdata) +{ + di_feature_iat_cnf_t *cnf; + iat_fdata_t *data; + + cnf = (di_feature_iat_cnf_t *)cdata->conf; + data = (iat_fdata_t *)fdata->data; + + if (!cnf->iat_jump_window) + free(data->iats, M_DIFFUSE); + else + free(data->jstate, M_DIFFUSE); + + free(fdata->data, M_DIFFUSE); + free(fdata->stats, M_DIFFUSE); + + return (0); +} + +static void +reset_jump_win_state(iat_jump_win_state_t *state) +{ + + state->min = 0x7FFFFFFF; + state->max = 0; + state->sum = 0; + state->sqsum = 0; + state->jump = 0; +} + +static int +iat_update_stats(struct cdata *cdata, struct fdata *fdata, struct mbuf *mbuf, + int proto, void *ulp, int dir) +{ + di_feature_iat_cnf_t *cnf; + iat_fdata_t *data; + struct timeval t, tv_diff; + uint32_t diff; + + cnf = (di_feature_iat_cnf_t *)cdata->conf; + data = (iat_fdata_t *)fdata->data; + t.tv_sec = t.tv_usec = 0; + +#if !defined(__linux__) && defined(_KERNEL) + /* Get from external source if possible (hardware), see bpf.c. */ + if (mbuf != NULL) { + struct m_tag *tag = m_tag_locate(mbuf, MTAG_BPF, + MTAG_BPF_TIMESTAMP, NULL); + if (tag != NULL) { + struct bintime bt = *(struct bintime *)(tag + 1); + bintime2timeval(&bt, &t); + } + } +#endif + + if (t.tv_sec == 0 && t.tv_usec == 0) { + if (cnf->iat_ts_acc == IAT_TS_ACC_ACCURATE) + getmicrouptime(&t); + else + microuptime(&t); + } + + if (data->last_abs_time.tv_sec > 0 || data->last_abs_time.tv_usec > 0) { + /* Get inter-arrival time. */ + tv_diff = tv_sub0(&t, &data->last_abs_time); + diff = (tv_diff.tv_sec * 1000000 + tv_diff.tv_usec) + / cnf->iat_prec; /* XXX: round() */ + + if (!cnf->iat_jump_window) { + data->iats[data->index++] = diff; + } else { + if (diff < data->jstate->min) + data->jstate->min = diff; + + if (diff > data->jstate->max) + data->jstate->max = diff; + + data->jstate->sum += diff; + data->jstate->sqsum += (uint64_t)diff * diff; + data->index++; + + if (data->index == (cnf->iat_window - 1)) { + data->jstate->jump = 1; + if (data->jstate->first) + data->jstate->first = 0; + } + } + + if (!data->full && data->index == (cnf->iat_window - 1)) + data->full = 1; + + data->changed = 1; + if (data->index == cnf->iat_window - 1) + data->index = 0; + } + data->last_abs_time = t; + + return (0); +} + +static int +iat_reset_stats(struct cdata *cdata, struct fdata *fdata) +{ + di_feature_iat_cnf_t *cnf; + iat_fdata_t *data; + int i; + + cnf = (di_feature_iat_cnf_t *)cdata->conf; + data = (iat_fdata_t *)fdata->data; + + fdata->stats[0] = 0x7FFFFFFF; + for (i = 1; i < IAT_NO_STATS; i++) + fdata->stats[i] = 0; + + if (!cnf->iat_jump_window) { + for (i = 0; i < (cnf->iat_window - 1); i++) + data->iats[i] = 0; + } else { + reset_jump_win_state(data->jstate); + data->jstate->first = 1; + } + data->full = 0; + data->changed = 0; + data->index = 0; + + return (0); +} + +/* + * XXX: Potential overflow of square sum for jumping windows otherwise overflow + * only possible if flow timeouts would be set much higher than default. + */ +static int +iat_get_stats(struct cdata *cdata, struct fdata *fdata, int32_t **stats) +{ +#define IAT_MIN fdata->stats[0] +#define IAT_MEAN fdata->stats[1] +#define IAT_MAX fdata->stats[2] +#define IAT_STDEV fdata->stats[3] + di_feature_iat_cnf_t *cnf; + iat_fdata_t *data; + uint64_t sqsum, sum; + int i, wsize; + + cnf = (di_feature_iat_cnf_t *)cdata->conf; + data = (iat_fdata_t *)fdata->data; + + if (!data->full && !(cnf->iat_partial_window && data->index > 1)) + return (0); /* Window is not full yet. */ + + /* Compute stats only if we need update. */ + if ((!cnf->iat_jump_window && data->changed) + || (cnf->iat_jump_window && (data->jstate->jump + || (cnf->iat_partial_window && data->jstate->first)))) { + wsize = cnf->iat_window - 1; + if (!data->full) + wsize = data->index; + + if (!cnf->iat_jump_window) { + sum = 0; + sqsum = 0; + + IAT_MIN = 0x7FFFFFFF; + IAT_MAX = 0; + for (i = 0; i < wsize; i++) { + if (data->iats[i] < IAT_MIN) + IAT_MIN = data->iats[i]; + + if (data->iats[i] > IAT_MAX) + IAT_MAX = data->iats[i]; + + sum += data->iats[i]; + } + + IAT_MEAN = sum / wsize; + if (wsize > 1) { + /* Need a second loop or sqsum may overflow. */ + for (i = 0; i < wsize; i++) { + sqsum += + ((int64_t)data->iats[i] - IAT_MEAN)* + ((int64_t)data->iats[i] - IAT_MEAN); + } + IAT_STDEV = fixp_sqrt(sqsum/wsize); + } + } else { + IAT_MIN = data->jstate->min; + IAT_MAX = data->jstate->max; + IAT_MEAN = data->jstate->sum / wsize; + if (wsize > 1) { + IAT_STDEV = + fixp_sqrt((data->jstate->sqsum - + (data->jstate->sum*data->jstate->sum/wsize)) + / (wsize - 1)); + } + + reset_jump_win_state(data->jstate); + } + data->changed = 0; + } + *stats = fdata->stats; + + return (IAT_NO_STATS); +} + +static int +iat_get_stat(struct cdata *cdata, struct fdata *fdata, int which, int32_t *stat) +{ + int32_t *stats; + + if (which < 0 || which > IAT_NO_STATS) + return (-1); + + if (!iat_get_stats(cdata, fdata, &stats)) + return (0); + + *stat = stats[which]; + + return (1); +} + +static int +iat_get_stat_names(char **names[]) +{ + + *names = iat_stat_names; + + return (IAT_NO_STATS); +} + +static struct diffuse_feature_alg diffuse_iat_desc = { + _FI( .name = ) IAT_NAME, + _FI( .type = ) IAT_TYPE, + _FI( .ref_count = ) 0, + + _FI( .init_instance = ) iat_init_instance, + _FI( .destroy_instance = ) iat_destroy_instance, + _FI( .init_stats = ) iat_init_stats, + _FI( .destroy_stats = ) iat_destroy_stats, + _FI( .update_stats = ) iat_update_stats, + _FI( .reset_stats = ) iat_reset_stats, + _FI( .get_stats = ) iat_get_stats, + _FI( .get_stat = ) iat_get_stat, + _FI( .get_stat_names = ) iat_get_stat_names, + _FI( .get_conf = ) iat_get_conf, +}; + +DECLARE_DIFFUSE_FEATURE_MODULE(feature_iat, &diffuse_iat_desc); diff -r 072dbd4da936 sys/netinet/ipfw/diffuse_feature_iat.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/netinet/ipfw/diffuse_feature_iat.h Sun Sep 25 17:32:04 2011 +1000 @@ -0,0 +1,59 @@ +/*- + * Copyright (c) 2010-2011 + * Swinburne University of Technology, Melbourne, Australia. + * All rights reserved. + * + * This software was developed at the Centre for Advanced Internet + * Architectures, Swinburne University of Technology, by Sebastian Zander, made + * possible in part by a gift from The Cisco University Research Program Fund, a + * corporate advised fund of Silicon Valley Community Foundation. + * + * 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$ + */ + +/* + * DIFFUSE packet inter-arrival time feature + */ + +#ifndef _NETINET_IPFW_DIFFUSE_FEATURE_IAT_H_ +#define _NETINET_IPFW_DIFFUSE_FEATURE_IAT_H_ + +#define DEFAULT_IAT_WINDOW 15 + +/* Main properties, used in kernel and userspace. */ +#define IAT_NAME "iat" +#define IAT_TYPE FEATURE_ALG_UNIDIRECTIONAL +#define IAT_NO_STATS 4 +#define IAT_STAT_NAMES_DECL char *iat_stat_names[IAT_NO_STATS] +#define IAT_STAT_NAMES IAT_STAT_NAMES_DECL = \ +{ \ + "min", \ + "mean", \ + "max", \ + "stdev" \ +}; + +struct feature_module *iat_module(void); + +#endif /* _NETINET_IPFW_DIFFUSE_FEATURE_IAT_H_ */ diff -r 072dbd4da936 sys/netinet/ipfw/diffuse_feature_iat_common.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/netinet/ipfw/diffuse_feature_iat_common.h Sun Sep 25 17:32:04 2011 +1000 @@ -0,0 +1,58 @@ +/*- + * Copyright (c) 2010-2011 + * Swinburne University of Technology, Melbourne, Australia. + * All rights reserved. + * + * This software was developed at the Centre for Advanced Internet + * Architectures, Swinburne University of Technology, by Sebastian Zander, made + * possible in part by a gift from The Cisco University Research Program Fund, a + * corporate advised fund of Silicon Valley Community Foundation. + * + * 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$ + */ + +#ifndef _NETINET_IPFW_DIFFUSE_FEATURE_IAT_COMMON_H_ +#define _NETINET_IPFW_DIFFUSE_FEATURE_IAT_COMMON_H_ + +typedef enum { + IAT_TS_ACC_NORMAL = 0, + IAT_TS_ACC_ACCURATE +} iat_ts_acc_t; + +typedef struct di_feature_iat_cnf { + struct di_oid oid; + + /* Sliding/jumping window as number of packets. */ + int iat_window; + /* Compute stats for partial windows. */ + int iat_partial_window; + /* Accuracy of timestamp. */ + int iat_ts_acc; + /* If = 1 we have jumping windows. */ + int iat_jump_window; + /* Precision in us. */ + int iat_prec; +} di_feature_iat_cnf_t; + +#endif /* _NETINET_IPFW_DIFFUSE_FEATURE_IAT_COMMON_H_ */