--- A DIFFUSE feature kernel module which calculates per-flow minimum, mean, --- maximum, standard deviation and sum packet length statistics for use in --- classification decisions. --- --- Sponsored by: FreeBSD Foundation --- Reviewed by: bz --- MFC after: 1 month --- diff -r 0f605f3d1413 sys/modules/diffuse/Makefile --- a/sys/modules/diffuse/Makefile Sun Sep 25 17:35:17 2011 +1000 +++ b/sys/modules/diffuse/Makefile Sun Sep 25 17:35:59 2011 +1000 @@ -1,8 +1,9 @@ # $FreeBSD$ SUBDIR= diffuse \ diffuse_feature_iat \ diffuse_feature_iatbd \ - diffuse_feature_pcnt + diffuse_feature_pcnt \ + diffuse_feature_plen .include diff -r 0f605f3d1413 sys/modules/diffuse/diffuse_feature_plen/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/modules/diffuse/diffuse_feature_plen/Makefile Sun Sep 25 17:35:59 2011 +1000 @@ -0,0 +1,17 @@ +# $FreeBSD$ + +.include + +.PATH: ${.CURDIR}/../../../netinet/ipfw +KMOD= diffuse_feature_plen +SRCS= diffuse_feature_plen.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 0f605f3d1413 sys/netinet/ipfw/diffuse_feature_plen.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/netinet/ipfw/diffuse_feature_plen.c Sun Sep 25 17:35:59 2011 +1000 @@ -0,0 +1,427 @@ +/*- + * 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 length 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 +#ifdef _KERNEL +#include +#include +#else +#define KPI_USER_COMPAT +#include +#endif /* _KERNEL */ + +/* + * Feature packet length computes minimum, mean, maximum and std deviation of + * packet length (length = length of IP data or length of UDP/TCP data). + */ + +/* If we are linked from userspace only put the declaration here. */ +#ifdef _KERNEL +PLEN_STAT_NAMES; +#else +PLEN_STAT_NAMES_DECL; +#endif + +/* State for jump windows. */ +typedef struct plen_jump_win_state { + uint16_t min; + uint16_t max; + uint32_t sum; + uint64_t sqsum; + int jump; + int first; +} plen_jump_win_state_t; + +/* Per flow data (ring buffer). */ +typedef struct plen_fdata { + int full; + int changed; + int index; + uint16_t *plens; + plen_jump_win_state_t *jstate; +} plen_fdata_t; + +static int plen_reset_stats(struct cdata *cdata, struct fdata *fdata); + +static int +plen_init_instance(struct cdata *cdata, struct di_oid *params) +{ + di_feature_plen_cnf_t *cnf, *p; + + cdata->conf = malloc(sizeof(di_feature_plen_cnf_t), M_DIFFUSE, + M_NOWAIT | M_ZERO); + cnf = (di_feature_plen_cnf_t *)cdata->conf; + + /* Set default configuration. */ + cnf->plen_window = DEFAULT_PLEN_WINDOW; + cnf->plen_partial_window = 0; + cnf->plen_len_type = PLEN_LEN_FULL; + cnf->plen_jump_window = 0; + + /* Set configuration. */ + if (params != NULL) { + p = (di_feature_plen_cnf_t *)params; + + if (p->plen_window != -1) + cnf->plen_window = p->plen_window; + + if (p->plen_partial_window != -1) + cnf->plen_partial_window = p->plen_partial_window; + + if (p->plen_len_type != -1) + cnf->plen_len_type = p->plen_len_type; + + if (p->plen_jump_window != -1) + cnf->plen_jump_window = p->plen_jump_window; + } + + return (0); +} + +static int +plen_destroy_instance(struct cdata *cdata) +{ + + free(cdata->conf, M_DIFFUSE); + + return (0); +} + +static int +plen_get_conf(struct cdata *cdata, struct di_oid *cbuf, int size_only) +{ + + if (!size_only) + memcpy(cbuf, cdata->conf, sizeof(di_feature_plen_cnf_t)); + + return (sizeof(di_feature_plen_cnf_t)); +} + +static int +plen_init_stats(struct cdata *cdata, struct fdata *fdata) +{ + di_feature_plen_cnf_t *cnf; + plen_fdata_t *data; + + cnf = (di_feature_plen_cnf_t *)cdata->conf; + + fdata->data = malloc(sizeof(plen_fdata_t), M_DIFFUSE, + M_NOWAIT | M_ZERO); + data = (plen_fdata_t *)fdata->data; + + if (!cnf->plen_jump_window) { + data->plens = malloc(cnf->plen_window * sizeof(uint16_t), + M_DIFFUSE, M_NOWAIT | M_ZERO); + } else { + data->jstate = malloc(sizeof(plen_jump_win_state_t), M_DIFFUSE, + M_NOWAIT | M_ZERO); + } + fdata->stats = malloc(PLEN_NO_STATS * sizeof(int32_t), M_DIFFUSE, + M_NOWAIT | M_ZERO); + plen_reset_stats(cdata, fdata); + + return (0); +} + +static int +plen_destroy_stats(struct cdata *cdata, struct fdata *fdata) +{ + plen_fdata_t *data; + di_feature_plen_cnf_t *cnf; + + cnf = (di_feature_plen_cnf_t *)cdata->conf; + data = (plen_fdata_t *)fdata->data; + + if (!cnf->plen_jump_window) + free(data->plens, 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(plen_jump_win_state_t *state) +{ + state->min = 0xFFFF; + state->max = 0; + state->sum = 0; + state->sqsum = 0; + state->jump = 0; +} + +static int +plen_update_stats(struct cdata *cdata, struct fdata *fdata, struct mbuf *mbuf, + int proto, void *ulp, int dir) +{ + di_feature_plen_cnf_t *cnf; + plen_fdata_t *data; + struct ip *ip; + struct ip6_hdr *ip6; + struct tcphdr *tcp; + uint16_t plen; + int iplen; + + cnf = (di_feature_plen_cnf_t *)cdata->conf; + data = (plen_fdata_t *)fdata->data; + ip = mtod(mbuf, struct ip *); + plen = 0; + + /* Length of data in IP. */ + if (cnf->plen_len_type == PLEN_LEN_FULL) { + plen = m_length(mbuf, NULL); + } else { + iplen = 0; + if (ip->ip_v == 6) { + ip6 = (struct ip6_hdr *)ip; + iplen = ntohs(ip6->ip6_plen); + } else { + iplen = ntohs(ip->ip_len) - ip->ip_hl * 4; + } + if (cnf->plen_len_type == PLEN_LEN_IPDATA) { + plen = iplen; + } else { + /* Length of data in UDP or TCP. */ + if (proto != 0 && ulp != NULL) { + if (proto == IPPROTO_TCP) { + tcp = (struct tcphdr *)ulp; + plen = iplen - tcp->th_off * 4; + } else if (proto == IPPROTO_UDP) { + plen = iplen - sizeof(struct udphdr); + } + } + } + } + + if (!cnf->plen_jump_window) { + data->plens[data->index++] = plen; + } else { + if (plen < data->jstate->min) + data->jstate->min = plen; + + if (plen > data->jstate->max) + data->jstate->max = plen; + + data->jstate->sum += plen; + data->jstate->sqsum += (uint64_t)plen * plen; + data->index++; + + if (data->index == cnf->plen_window) { + data->jstate->jump = 1; + if (data->jstate->first) + data->jstate->first = 0; + } + } + + if (!data->full && data->index == cnf->plen_window) + data->full = 1; + + data->changed = 1; + if (data->index == cnf->plen_window) + data->index = 0; + + return (0); +} + +static int +plen_reset_stats(struct cdata *cdata, struct fdata *fdata) +{ + di_feature_plen_cnf_t *cnf; + plen_fdata_t *data; + int i; + + cnf = (di_feature_plen_cnf_t *)cdata->conf; + data = (plen_fdata_t *)fdata->data; + + fdata->stats[0] = 0x7FFFFFFF; + for (i = 1; i < PLEN_NO_STATS; i++) + fdata->stats[i] = 0; + + if (!cnf->plen_jump_window) { + for (i = 0; i < cnf->plen_window; i++) + data->plens[i] = 0; + } else { + reset_jump_win_state(data->jstate); + data->jstate->first = 1; + } + data->full = 0; + data->changed = 0; + data->index = 0; + + return (0); +} + +static int +plen_get_stats(struct cdata *cdata, struct fdata *fdata, int32_t **stats) +{ +#define PL_MIN fdata->stats[0] +#define PL_MEAN fdata->stats[1] +#define PL_MAX fdata->stats[2] +#define PL_STDEV fdata->stats[3] +#define PL_SUM fdata->stats[4] + di_feature_plen_cnf_t *cnf; + plen_fdata_t *data; + uint64_t sqsum = 0; + uint32_t sum = 0; + int i, wsize; + + cnf = (di_feature_plen_cnf_t *)cdata->conf; + data = (plen_fdata_t *)fdata->data; + sqsum = 0; + sum = 0; + + if (!data->full && !(cnf->plen_partial_window && data->index > 0)) + return (0); /* Window is not full yet. */ + + /* Compute stats only if we need update. */ + if ((!cnf->plen_jump_window && data->changed) + || (cnf->plen_jump_window && (data->jstate->jump + || (cnf->plen_partial_window && data->jstate->first)))) { + wsize = cnf->plen_window; + if (!data->full) + wsize = data->index; + + if (!cnf->plen_jump_window) { + PL_MIN = 0x7FFFFFFF; + PL_MAX = 0; + for (i = 0; i < wsize; i++) { + if (data->plens[i] < PL_MIN) + PL_MIN = data->plens[i]; + + if (data->plens[i] > PL_MAX) + PL_MAX = data->plens[i]; + + sum += data->plens[i]; + sqsum += (uint64_t)data->plens[i] + * data->plens[i]; + } + } else { + PL_MIN = data->jstate->min; + PL_MAX = data->jstate->max; + sum = data->jstate->sum; + sqsum = data->jstate->sqsum; + reset_jump_win_state(data->jstate); + } + PL_MEAN = sum / wsize; + if (wsize > 1) { + PL_STDEV = fixp_sqrt((sqsum + - ((uint64_t)sum * sum / wsize)) /(wsize - 1)); + } + PL_SUM = sum; + data->changed = 0; + } + *stats = fdata->stats; + + return (PLEN_NO_STATS); +} + +static int +plen_get_stat(struct cdata *cdata, struct fdata *fdata, int which, int32_t *stat) +{ + int32_t *stats; + + if (which < 0 || which > PLEN_NO_STATS) + return (-1); + + if (!plen_get_stats(cdata, fdata, &stats)) + return (0); + + *stat = stats[which]; + + return (1); +} + +static int +plen_get_stat_names(char **names[]) +{ + + *names = plen_stat_names; + + return (PLEN_NO_STATS); +} + +static struct diffuse_feature_alg diffuse_plen_desc = { + _FI( .name = ) PLEN_NAME, + _FI( .type = ) PLEN_TYPE, + _FI( .ref_count = ) 0, + + _FI( .init_instance = ) plen_init_instance, + _FI( .destroy_instance = ) plen_destroy_instance, + _FI( .init_stats = ) plen_init_stats, + _FI( .destroy_stats = ) plen_destroy_stats, + _FI( .update_stats = ) plen_update_stats, + _FI( .reset_stats = ) plen_reset_stats, + _FI( .get_stats = ) plen_get_stats, + _FI( .get_stat = ) plen_get_stat, + _FI( .get_stat_names = ) plen_get_stat_names, + _FI( .get_conf = ) plen_get_conf, +}; + +DECLARE_DIFFUSE_FEATURE_MODULE(feature_plen, &diffuse_plen_desc); diff -r 0f605f3d1413 sys/netinet/ipfw/diffuse_feature_plen.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/netinet/ipfw/diffuse_feature_plen.h Sun Sep 25 17:35:59 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. + */ + +/* + * DIFFUSE packet length feature + */ + +#ifndef _NETINET_IPFW_DIFFUSE_FEATURE_PLEN_H_ +#define _NETINET_IPFW_DIFFUSE_FEATURE_PLEN_H_ + +#define DEFAULT_PLEN_WINDOW 15 + +/* Main properties, used in kernel and userspace. */ +#define PLEN_NAME "plen" +#define PLEN_TYPE FEATURE_ALG_UNIDIRECTIONAL +#define PLEN_NO_STATS 5 +#define PLEN_STAT_NAMES_DECL char *plen_stat_names[PLEN_NO_STATS] +#define PLEN_STAT_NAMES PLEN_STAT_NAMES_DECL = \ +{ \ + "min", \ + "mean", \ + "max", \ + "stdev", \ + "sum" \ +}; + +struct feature_module *plen_module(void); + +#endif /* _NETINET_IPFW_DIFFUSE_FEATURE_PLEN_H_ */ diff -r 0f605f3d1413 sys/netinet/ipfw/diffuse_feature_plen_common.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/netinet/ipfw/diffuse_feature_plen_common.h Sun Sep 25 17:35:59 2011 +1000 @@ -0,0 +1,56 @@ +/*- + * 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_PLEN_COMMON_H_ +#define _NETINET_IPFW_DIFFUSE_FEATURE_PLEN_COMMON_H_ + +/* Length computation methods. */ +#define PLEN_LEN_FULL 0 /* Full length including IP header. */ +#define PLEN_LEN_IPDATA 1 /* IP data length. */ +#define PLEN_LEN_PAYLOAD 2 /* UDP/TCP data length. */ + +typedef struct di_feature_plen_cnf { + struct di_oid oid; + + /* Sliding/jumping window as number of packets. */ + int plen_window; + /* Compute stats for partial windows. */ + int plen_partial_window; + /* Type of length (see PLEN_LEN_* defines). */ + int plen_len_type; + /* If =1 we have jumping windows. */ + int plen_jump_window; +} di_feature_plen_cnf_t; + +#endif /* _NETINET_IPFW_DIFFUSE_FEATURE_PLEN_COMMON_H_ */