--- A DIFFUSE feature kernel module which calculates per-flow forward/reverse --- packet count and ratio statistics for use in classification decisions. --- --- Sponsored by: FreeBSD Foundation --- Reviewed by: bz --- MFC after: 1 month --- diff -r c7070be7d5c1 sys/modules/diffuse/Makefile --- a/sys/modules/diffuse/Makefile Sun Sep 25 17:34:54 2011 +1000 +++ b/sys/modules/diffuse/Makefile Sun Sep 25 17:35:42 2011 +1000 @@ -1,7 +1,8 @@ # $FreeBSD$ SUBDIR= diffuse \ diffuse_feature_iat \ - diffuse_feature_iatbd + diffuse_feature_iatbd \ + diffuse_feature_pcnt .include diff -r c7070be7d5c1 sys/modules/diffuse/diffuse_feature_pcnt/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/modules/diffuse/diffuse_feature_pcnt/Makefile Sun Sep 25 17:35:42 2011 +1000 @@ -0,0 +1,17 @@ +# $FreeBSD$ + +.include + +.PATH: ${.CURDIR}/../../../netinet/ipfw +KMOD= diffuse_feature_pcnt +SRCS= diffuse_feature_pcnt.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 c7070be7d5c1 sys/netinet/ipfw/diffuse_feature_pcnt.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/netinet/ipfw/diffuse_feature_pcnt.c Sun Sep 25 17:35:42 2011 +1000 @@ -0,0 +1,364 @@ +/*- + * 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 count feature. + */ + +#include +__FBSDID("$FreeBSD$"); + +#ifndef _KERNEL +#include +#include +#endif + +#ifdef _KERNEL +#include +#include +#include +#include +#include +#endif +#include + +#include + +#include +#include +#include +#include + +#include +#include +#include +#ifdef _KERNEL +#include +#include +#include +#else +#define KPI_USER_COMPAT +#include +#endif /* _KERNEL */ + +/* + * Feature packet count computes number of forward packets, backward packets and + * ratio. + */ + +/* If we are linked from userspace only put the declaration here. */ +#ifdef _KERNEL +PCNT_STAT_NAMES; +#else +PCNT_STAT_NAMES_DECL; +#endif + +/* State for jump windows. */ +typedef struct pcnt_jump_win_state { + uint16_t fpkts; + uint16_t bpkts; + int jump; + int first; +} pcnt_jump_win_state_t; + +/* Per flow data (ring buffer). */ +typedef struct pcnt_fdata { + int full; + int changed; + int index; + uint8_t *pdirs; + pcnt_jump_win_state_t *jstate; +} pcnt_fdata_t; + +static int pcnt_reset_stats(struct cdata *cdata, struct fdata *fdata); + +static int +pcnt_init_instance(struct cdata *cdata, struct di_oid *params) +{ + di_feature_pcnt_cnf_t *cnf, *p; + + cdata->conf = malloc(sizeof(di_feature_pcnt_cnf_t), M_DIFFUSE, + M_NOWAIT | M_ZERO); + cnf = (di_feature_pcnt_cnf_t *)cdata->conf; + + /* Set default configuration. */ + cnf->pcnt_window = DEFAULT_PCNT_WINDOW; + cnf->pcnt_partial_window = 0; + cnf->pcnt_jump_window = 0; + + /* Set configuration */ + if (params != NULL) { + p = (di_feature_pcnt_cnf_t *)params; + + if (p->pcnt_window != -1) + cnf->pcnt_window = p->pcnt_window; + + if (p->pcnt_partial_window != -1) + cnf->pcnt_partial_window = p->pcnt_partial_window; + + if (p->pcnt_jump_window != -1) + cnf->pcnt_jump_window = p->pcnt_jump_window; + } + + return (0); +} + +static int +pcnt_destroy_instance(struct cdata *cdata) +{ + + free(cdata->conf, M_DIFFUSE); + + return (0); +} + +static int +pcnt_get_conf(struct cdata *cdata, struct di_oid *cbuf, int size_only) +{ + + if (!size_only) + memcpy(cbuf, cdata->conf, sizeof(di_feature_pcnt_cnf_t)); + + return (sizeof(di_feature_pcnt_cnf_t)); +} + +static int +pcnt_init_stats(struct cdata *cdata, struct fdata *fdata) +{ + di_feature_pcnt_cnf_t *cnf; + pcnt_fdata_t *data; + + cnf = (di_feature_pcnt_cnf_t *)cdata->conf; + + fdata->data = malloc(sizeof(pcnt_fdata_t), M_DIFFUSE, + M_NOWAIT | M_ZERO); + data = (pcnt_fdata_t *)fdata->data; + + if (!cnf->pcnt_jump_window) { + data->pdirs = malloc(cnf->pcnt_window * sizeof(uint8_t), + M_DIFFUSE, M_NOWAIT | M_ZERO); + } else { + data->jstate = malloc(sizeof(pcnt_jump_win_state_t), M_DIFFUSE, + M_NOWAIT | M_ZERO); + } + fdata->stats = malloc(PCNT_NO_STATS * sizeof(int32_t), M_DIFFUSE, + M_NOWAIT | M_ZERO); + pcnt_reset_stats(cdata, fdata); + + return (0); +} + +static int +pcnt_destroy_stats(struct cdata *cdata, struct fdata *fdata) +{ + di_feature_pcnt_cnf_t *cnf; + pcnt_fdata_t *data; + + cnf = (di_feature_pcnt_cnf_t *)cdata->conf; + data = (pcnt_fdata_t *)fdata->data; + + if (!cnf->pcnt_jump_window) + free(data->pdirs, 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(pcnt_jump_win_state_t *state) +{ + + state->fpkts = 0; + state->bpkts = 0; + state->jump = 0; +} + +static int +pcnt_update_stats(struct cdata *cdata, struct fdata *fdata, struct mbuf *mbuf, + int proto, void *ulp, int dir) +{ + di_feature_pcnt_cnf_t *cnf; + pcnt_fdata_t *data; + + cnf = (di_feature_pcnt_cnf_t *)cdata->conf; + data = (pcnt_fdata_t *)fdata->data; + + if (!cnf->pcnt_jump_window) { + data->pdirs[data->index++] = dir; + } else { + if (dir == MATCH_FORWARD) + data->jstate->fpkts++; + else + data->jstate->bpkts++; + + data->index++; + + if (data->index == cnf->pcnt_window) { + data->jstate->jump = 1; + if (data->jstate->first) + data->jstate->first = 0; + } + } + + if (!data->full && data->index == cnf->pcnt_window) + data->full = 1; + + data->changed = 1; + if (data->index == cnf->pcnt_window) + data->index = 0; + + return (0); +} + +static int +pcnt_reset_stats(struct cdata *cdata, struct fdata *fdata) +{ + di_feature_pcnt_cnf_t *cnf; + pcnt_fdata_t *data; + int i; + + cnf = (di_feature_pcnt_cnf_t *)cdata->conf; + data = (pcnt_fdata_t *)fdata->data; + + for (i = 0; i < PCNT_NO_STATS; i++) + fdata->stats[i] = 0; + + if (!cnf->pcnt_jump_window) { + for (i = 0; i < cnf->pcnt_window; i++) + data->pdirs[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 +pcnt_get_stats(struct cdata *cdata, struct fdata *fdata, int32_t **stats) +{ +#define PC_FPKTS fdata->stats[0] +#define PC_BPKTS fdata->stats[1] +#define PC_RATIO fdata->stats[2] + di_feature_pcnt_cnf_t *cnf; + pcnt_fdata_t *data; + int i, wsize; + + cnf = (di_feature_pcnt_cnf_t *)cdata->conf; + data = (pcnt_fdata_t *)fdata->data; + + if (!data->full && !(cnf->pcnt_partial_window && data->index > 0)) + return (0); /* Window is not full yet. */ + + /* Compute stats only if we need update. */ + if ((!cnf->pcnt_jump_window && data->changed) + || (cnf->pcnt_jump_window && (data->jstate->jump + || (cnf->pcnt_partial_window && data->jstate->first)))) { + wsize = cnf->pcnt_window; + if (!data->full) + wsize = data->index; + + if (!cnf->pcnt_jump_window) { + PC_FPKTS = 0; + PC_BPKTS = 0; + for(i = 0; i < wsize; i++) { + if (data->pdirs[i] == MATCH_FORWARD) + PC_FPKTS++; + else + PC_BPKTS++; + } + } else { + PC_FPKTS = data->jstate->fpkts; + PC_BPKTS = data->jstate->bpkts; + reset_jump_win_state(data->jstate); + } + PC_RATIO = 0; + if (PC_BPKTS > 0) + PC_RATIO = (PC_FPKTS * 1000) / PC_BPKTS; + + data->changed = 0; + } + *stats = fdata->stats; + + return (PCNT_NO_STATS); +} + +static int +pcnt_get_stat(struct cdata *cdata, struct fdata *fdata, int which, int32_t *stat) +{ + int32_t *stats; + + if (which < 0 || which > PCNT_NO_STATS) + return (-1); + + if (!pcnt_get_stats(cdata, fdata, &stats)) + return (0); + + *stat = stats[which]; + + return (1); +} + +static int +pcnt_get_stat_names(char **names[]) +{ + + *names = pcnt_stat_names; + + return (PCNT_NO_STATS); +} + +static struct diffuse_feature_alg diffuse_pcnt_desc = { + _FI( .name = ) PCNT_NAME, + _FI( .type = ) PCNT_TYPE, + _FI( .ref_count = ) 0, + + _FI( .init_instance = ) pcnt_init_instance, + _FI( .destroy_instance = ) pcnt_destroy_instance, + _FI( .init_stats = ) pcnt_init_stats, + _FI( .destroy_stats = ) pcnt_destroy_stats, + _FI( .update_stats = ) pcnt_update_stats, + _FI( .reset_stats = ) pcnt_reset_stats, + _FI( .get_stats = ) pcnt_get_stats, + _FI( .get_stat = ) pcnt_get_stat, + _FI( .get_stat_names = ) pcnt_get_stat_names, + _FI( .get_conf = ) pcnt_get_conf, +}; + +DECLARE_DIFFUSE_FEATURE_MODULE(feature_pcnt, &diffuse_pcnt_desc); diff -r c7070be7d5c1 sys/netinet/ipfw/diffuse_feature_pcnt.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/netinet/ipfw/diffuse_feature_pcnt.h Sun Sep 25 17:35:42 2011 +1000 @@ -0,0 +1,67 @@ +/*- + * 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 count feature. + */ + +#ifndef _NETINET_IPFW_DIFFUSE_FEATURE_PCNT_H_ +#define _NETINET_IPFW_DIFFUSE_FEATURE_PCNT_H_ + +#define DEFAULT_PCNT_WINDOW 15 + +typedef struct di_feature_pcnt_cnf { + struct di_oid oid; + + /* Sliding/jumping window as number of packets. */ + int pcnt_window; + /* Compute stats for partial windows. */ + int pcnt_partial_window; + /* If =1 we have jumping windows. */ + int pcnt_jump_window; +} di_feature_pcnt_cnf_t; + +/* Main properties, used in kernel and userspace. */ +#define PCNT_NAME "pcnt" +#define PCNT_TYPE FEATURE_ALG_BIDIRECTIONAL +#define PCNT_NO_STATS 3 +#define PCNT_STAT_NAMES_DECL char *pcnt_stat_names[PCNT_NO_STATS] +#define PCNT_STAT_NAMES PCNT_STAT_NAMES_DECL = \ +{ \ + "fpckts", \ + "bpckts", \ + "ratio" /* fpckts / bpckts * 1000 */ \ +}; + +struct feature_module *pcnt_module(void); + +#endif /* _NETINET_IPFW_DIFFUSE_FEATURE_PCNT_H_ */