[bwm-tools-devel] COMMIT - r23 - trunk/lib
Brought to you by:
nkukard
From: SVN C. <sv...@li...> - 2005-01-03 13:40:43
|
Author: nkukard Date: 2005-01-03 15:40:24 +0200 (Mon, 03 Jan 2005) New Revision: 23 Removed: trunk/lib/autoclass.c trunk/lib/flow.c trunk/lib/ipq.c trunk/lib/libipq.c Modified: trunk/lib/Makefile.am trunk/lib/Makefile.in trunk/lib/xmlConf.c Log: * Change 3/4: Depletion of lib Modified: trunk/lib/Makefile.am =================================================================== --- trunk/lib/Makefile.am 2005-01-03 13:39:48 UTC (rev 22) +++ trunk/lib/Makefile.am 2005-01-03 13:40:24 UTC (rev 23) @@ -25,7 +25,7 @@ lib_LTLIBRARIES = libbwm.la -libbwm_la_SOURCES = flow.c ipq.c libipq.c xmlConf.c autoclass.c misc.c +libbwm_la_SOURCES = xmlConf.c misc.c libbwm_la_CFLAGS = -I$(top_srcdir)/include $(GLIB_CFLAGS) $(XML_CFLAGS) $(GMIME_CFLAGS) $(AM_CFLAGS) libbwm_la_LDFLAGS = $(GLIB_LIBS) $(XML_LIBS) $(GMIME_LIBS) -version-info 1:0:0 Modified: trunk/lib/Makefile.in =================================================================== --- trunk/lib/Makefile.in 2005-01-03 13:39:48 UTC (rev 22) +++ trunk/lib/Makefile.in 2005-01-03 13:40:24 UTC (rev 23) @@ -75,9 +75,7 @@ libLTLIBRARIES_INSTALL = $(INSTALL) LTLIBRARIES = $(lib_LTLIBRARIES) libbwm_la_LIBADD = -am_libbwm_la_OBJECTS = libbwm_la-flow.lo libbwm_la-ipq.lo \ - libbwm_la-libipq.lo libbwm_la-xmlConf.lo \ - libbwm_la-autoclass.lo libbwm_la-misc.lo +am_libbwm_la_OBJECTS = libbwm_la-xmlConf.lo libbwm_la-misc.lo libbwm_la_OBJECTS = $(am_libbwm_la_OBJECTS) DEFAULT_INCLUDES = -I. -I$(srcdir) -I$(top_builddir) depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -204,7 +202,7 @@ sysconfdir = @sysconfdir@ target_alias = @target_alias@ lib_LTLIBRARIES = libbwm.la -libbwm_la_SOURCES = flow.c ipq.c libipq.c xmlConf.c autoclass.c misc.c +libbwm_la_SOURCES = xmlConf.c misc.c libbwm_la_CFLAGS = -I$(top_srcdir)/include $(GLIB_CFLAGS) $(XML_CFLAGS) $(GMIME_CFLAGS) $(AM_CFLAGS) libbwm_la_LDFLAGS = $(GLIB_LIBS) $(XML_LIBS) $(GMIME_LIBS) -version-info 1:0:0 all: all-am @@ -276,10 +274,6 @@ distclean-compile: -rm -f *.tab.c -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libbwm_la-autoclass.Plo@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libbwm_la-flow.Plo@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libbwm_la-ipq.Plo@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libbwm_la-libipq.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libbwm_la-misc.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libbwm_la-xmlConf.Plo@am__quote@ @@ -304,27 +298,6 @@ @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(LTCOMPILE) -c -o $@ $< -libbwm_la-flow.lo: flow.c -@am__fastdepCC_TRUE@ if $(LIBTOOL) --mode=compile --tag=CC $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libbwm_la_CFLAGS) $(CFLAGS) -MT libbwm_la-flow.lo -MD -MP -MF "$(DEPDIR)/libbwm_la-flow.Tpo" -c -o libbwm_la-flow.lo `test -f 'flow.c' || echo '$(srcdir)/'`flow.c; \ -@am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/libbwm_la-flow.Tpo" "$(DEPDIR)/libbwm_la-flow.Plo"; else rm -f "$(DEPDIR)/libbwm_la-flow.Tpo"; exit 1; fi -@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='flow.c' object='libbwm_la-flow.lo' libtool=yes @AMDEPBACKSLASH@ -@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ -@am__fastdepCC_FALSE@ $(LIBTOOL) --mode=compile --tag=CC $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libbwm_la_CFLAGS) $(CFLAGS) -c -o libbwm_la-flow.lo `test -f 'flow.c' || echo '$(srcdir)/'`flow.c - -libbwm_la-ipq.lo: ipq.c -@am__fastdepCC_TRUE@ if $(LIBTOOL) --mode=compile --tag=CC $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libbwm_la_CFLAGS) $(CFLAGS) -MT libbwm_la-ipq.lo -MD -MP -MF "$(DEPDIR)/libbwm_la-ipq.Tpo" -c -o libbwm_la-ipq.lo `test -f 'ipq.c' || echo '$(srcdir)/'`ipq.c; \ -@am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/libbwm_la-ipq.Tpo" "$(DEPDIR)/libbwm_la-ipq.Plo"; else rm -f "$(DEPDIR)/libbwm_la-ipq.Tpo"; exit 1; fi -@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='ipq.c' object='libbwm_la-ipq.lo' libtool=yes @AMDEPBACKSLASH@ -@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ -@am__fastdepCC_FALSE@ $(LIBTOOL) --mode=compile --tag=CC $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libbwm_la_CFLAGS) $(CFLAGS) -c -o libbwm_la-ipq.lo `test -f 'ipq.c' || echo '$(srcdir)/'`ipq.c - -libbwm_la-libipq.lo: libipq.c -@am__fastdepCC_TRUE@ if $(LIBTOOL) --mode=compile --tag=CC $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libbwm_la_CFLAGS) $(CFLAGS) -MT libbwm_la-libipq.lo -MD -MP -MF "$(DEPDIR)/libbwm_la-libipq.Tpo" -c -o libbwm_la-libipq.lo `test -f 'libipq.c' || echo '$(srcdir)/'`libipq.c; \ -@am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/libbwm_la-libipq.Tpo" "$(DEPDIR)/libbwm_la-libipq.Plo"; else rm -f "$(DEPDIR)/libbwm_la-libipq.Tpo"; exit 1; fi -@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='libipq.c' object='libbwm_la-libipq.lo' libtool=yes @AMDEPBACKSLASH@ -@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ -@am__fastdepCC_FALSE@ $(LIBTOOL) --mode=compile --tag=CC $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libbwm_la_CFLAGS) $(CFLAGS) -c -o libbwm_la-libipq.lo `test -f 'libipq.c' || echo '$(srcdir)/'`libipq.c - libbwm_la-xmlConf.lo: xmlConf.c @am__fastdepCC_TRUE@ if $(LIBTOOL) --mode=compile --tag=CC $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libbwm_la_CFLAGS) $(CFLAGS) -MT libbwm_la-xmlConf.lo -MD -MP -MF "$(DEPDIR)/libbwm_la-xmlConf.Tpo" -c -o libbwm_la-xmlConf.lo `test -f 'xmlConf.c' || echo '$(srcdir)/'`xmlConf.c; \ @am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/libbwm_la-xmlConf.Tpo" "$(DEPDIR)/libbwm_la-xmlConf.Plo"; else rm -f "$(DEPDIR)/libbwm_la-xmlConf.Tpo"; exit 1; fi @@ -332,13 +305,6 @@ @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(LIBTOOL) --mode=compile --tag=CC $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libbwm_la_CFLAGS) $(CFLAGS) -c -o libbwm_la-xmlConf.lo `test -f 'xmlConf.c' || echo '$(srcdir)/'`xmlConf.c -libbwm_la-autoclass.lo: autoclass.c -@am__fastdepCC_TRUE@ if $(LIBTOOL) --mode=compile --tag=CC $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libbwm_la_CFLAGS) $(CFLAGS) -MT libbwm_la-autoclass.lo -MD -MP -MF "$(DEPDIR)/libbwm_la-autoclass.Tpo" -c -o libbwm_la-autoclass.lo `test -f 'autoclass.c' || echo '$(srcdir)/'`autoclass.c; \ -@am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/libbwm_la-autoclass.Tpo" "$(DEPDIR)/libbwm_la-autoclass.Plo"; else rm -f "$(DEPDIR)/libbwm_la-autoclass.Tpo"; exit 1; fi -@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='autoclass.c' object='libbwm_la-autoclass.lo' libtool=yes @AMDEPBACKSLASH@ -@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ -@am__fastdepCC_FALSE@ $(LIBTOOL) --mode=compile --tag=CC $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libbwm_la_CFLAGS) $(CFLAGS) -c -o libbwm_la-autoclass.lo `test -f 'autoclass.c' || echo '$(srcdir)/'`autoclass.c - libbwm_la-misc.lo: misc.c @am__fastdepCC_TRUE@ if $(LIBTOOL) --mode=compile --tag=CC $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libbwm_la_CFLAGS) $(CFLAGS) -MT libbwm_la-misc.lo -MD -MP -MF "$(DEPDIR)/libbwm_la-misc.Tpo" -c -o libbwm_la-misc.lo `test -f 'misc.c' || echo '$(srcdir)/'`misc.c; \ @am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/libbwm_la-misc.Tpo" "$(DEPDIR)/libbwm_la-misc.Plo"; else rm -f "$(DEPDIR)/libbwm_la-misc.Tpo"; exit 1; fi Deleted: trunk/lib/autoclass.c =================================================================== --- trunk/lib/autoclass.c 2005-01-03 13:39:48 UTC (rev 22) +++ trunk/lib/autoclass.c 2005-01-03 13:40:24 UTC (rev 23) @@ -1,107 +0,0 @@ -/* - * autoclass.c - Automatic packet priority classification - * Copyright (C) 2003-2004, Linux Based Systems Design - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * - * 29/08/2003 - Nigel Kukard <nk...@lb...> - * * Initial design -*/ - -#include "autoclass.h" - - -// Band calculation functions, tcp -long int tcpPortToBand(long int bandNum, u_int16_t port) -{ - long int ret = bandNum; - - - // Decide band to pump packet into - switch (port) - { - // AUTH - case 113: - ret = 20; - break; - // SSH - case 22: - // TELNET - case 23: - ret = 25; - break; - // HTTP - case 80: - // PROXY? - case 8080: - case 3128: - case 3130: - // HTTPS - case 443: - ret = 65; - break; - // CVS - case 2401: - ret = 70; - break; - // POP3 - case 110: - // IMAP - case 143: - ret = 75; - break; - // FTP - case 20: - case 21: - ret = 80; - break; - }; - - return ret; -} - -// Band calculation functions, udp -long int udpPortToBand(long int bandNum, u_int16_t port) -{ - long int ret = bandNum; - - - // Decide band to pump packet into - switch (port) - { - // DNS - case 53: - ret = 10; - break; - // NTP - case 123: - ret = 15; - break; - // RADIUS - case 1645: - case 1646: - case 1812: - case 1813: - ret = 30; - break; - }; - - if (port >= 33434 && port <= 33465) - ret = 5; - - return ret; -} - Deleted: trunk/lib/flow.c =================================================================== --- trunk/lib/flow.c 2005-01-03 13:39:48 UTC (rev 22) +++ trunk/lib/flow.c 2005-01-03 13:40:24 UTC (rev 23) @@ -1,768 +0,0 @@ -/* - * flow.c - Flow handling for bwmd - * Copyright (C) 2003-2004, Linux Based Systems Design - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * - * 15/04/2003 - Nigel Kukard <nk...@lb...> - * * Initial design -*/ - - - -#include <glib.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <time.h> -#include "common.h" -#include "flow.h" -#include "ipq.h" - - - - - -// Create a flow -struct flow_t* createFlow( - char *flowName, - struct flow_t *parentFlow, - long int statsLen, - long int maxQueueSize, - long int maxQueueLen, - long int maxRate, - long int burstRate, - long int nfmark, - float parent_th, - int reportTimeout) -{ - int i; - struct flow_t *flow = (struct flow_t*) malloc(sizeof(struct flow_t)); - - - // Check if its ok... - if (!flow) - { - // FIXME - log error - fprintf(stderr,"Failed to allocate memory for flow\n"); - return NULL; - } - - // Lock entire flow - flow->lock = g_mutex_new(); - - strncpy(flow->flowName,flowName,MAX_REFLEN); // copy string - - flow->statsLen = statsLen; - flow->statsPos = 0; - - flow->parent = parentFlow; - - flow->maxQueueSize = maxQueueSize; - flow->maxQueueLen = maxQueueLen; - flow->maxRate = maxRate; - flow->burstRate = burstRate; - flow->nfmark = nfmark; - - flow->parent_th = parent_th; - - flow->counterTimeout = reportTimeout; - flow->counterRemaining = reportTimeout; - - flow->curQueueSize = 0; - flow->curQueueLen = 0; - - // Lock for our counters only - flow->counterLock = g_mutex_new(); - flow->counter.pktCount = 0; - flow->counter.pktSize = 0; - flow->counter.pktDropped = 0; - flow->counter.pktBursted = 0; - - flow->groups = NULL; - - flow->lastDumpTimestamp = time(NULL); - // Check if we can set our credit - if (flow->maxRate > 0) - { - flow->usCredit = flow->maxRate / 1000000.0; // For micro (millionths) of seconds silly - flow->curCredit = 0; - } - else - { - flow->usCredit = 0; - flow->curCredit = 0; - } - - if (flow->burstRate > 0) - { - flow->usBurstCredit = flow->burstRate / 1000000.0; - flow->curBurstCredit = 0; - } - else - { - flow->usBurstCredit = 0; - flow->curBurstCredit = 0; - } - // Setup throughput stuff... - gettimeofday(&flow->lastThroughputUpdate,NULL); - flow->curThroughputAge = 0; - flow->curThroughput = 0; - // Set last time we calculated credit and the rest... - flow->accumMs = 0; - gettimeofday(&flow->lastCreditCalc,NULL); - // Clear our running counters - flow->running.pktCount = 0; - flow->running.pktSize = 0; - flow->running.pktDropped = 0; - flow->running.pktBursted = 0; - - // Verify everything is ok... - if (flow->statsLen == 0) - flow->statsLen = 10; - - // Create stuff... - flow->pktStats = (struct pktStat_t*) malloc0(sizeof(struct pktStat_t) * flow->statsLen); - if (!flow->pktStats) - { - // FIXME - log error - fprintf(stderr,"Failed to allocate memory for flow->pktStats\n"); - return NULL; - } - - // Calculate the queue length ... etc - if (flow->maxQueueLen == -1) - { - if (flow->maxRate != 0) - flow->maxQueueLen = 5; // Seems an OK value for normal use? - else - flow->maxQueueLen = 0; - } - if (flow->maxQueueSize == -1) - { - if (flow->maxRate != 0) - flow->maxQueueSize = 4096; // Let us queue at least 2 big packets and some small ones - else - flow->maxQueueSize = 0; - } - - // Calculate our thresholds - //flow->min_th = (flow->maxRate / 750); - //flow->max_th = (flow->maxRate / 750) * 4; - - // Blank all queues - for (i = 0; i < NUM_PRIO_BANDS; i++) - { - struct pktQueue_t *pktQueue = flow->pktQueues[i] = (struct pktQueue_t*) malloc(sizeof(struct pktQueue_t)); - - pktQueue->lock = g_mutex_new(); - pktQueue->prio = i; - pktQueue->parentFlow = flow; - pktQueue->nfmark = -1; - pktQueue->curSize = 0; - pktQueue->curLen = 0; - pktQueue->maxSize = 0; - pktQueue->maxLen = 0; - pktQueue->packets = NULL; - } - -// printf("Flow added...\n"); -// printf(" flowName - %s\n",flowName); -// printf(" parent - %p\n",parentFlow); -// printf(" nfmark - %li\n",nfmark); -// printf(" statsLen - %li\n",flow->statsLen); -// printf(" maxQueueSize - %li\n",flow->maxQueueSize); -// printf(" maxQueueLen - %li\n",flow->maxQueueLen); -// printf(" maxRate - %li\n",flow->maxRate); -// printf(" usCredit - %.6f\n",flow->usCredit); -// printf(" curCredit - %u\n",flow->curCredit); -// printf(" usBurstCredit - %.6f\n",flow->usBurstCredit); -// printf(" curBurstCredit - %u\n",flow->curBurstCredit); -// printf(" burstRate - %li\n",flow->burstRate); -// printf("Queue Stuff:\n"); -// printf(" min_th - %li\n",flow->min_th); -// printf(" max_th - %li\n",flow->max_th); - - return flow; -} - - -// Create group -struct group_t *createGroup( - char *groupName, - long int statsLen, - int reportTimeout, - GList *flowList) -{ - struct group_t *group = (struct group_t *) malloc(sizeof(struct group_t)); - - - // Lock entire flow - group->lock = g_mutex_new(); - - strncpy(group->groupName,groupName,MAX_REFLEN); // copy string - - // Stats stuff - group->statsLen = statsLen; - group->statsPos = 0; - - // Clear our running counters - group->running.pktCount = 0; - group->running.pktSize = 0; - group->running.pktDropped = 0; - group->running.pktBursted = 0; - - group->counterLock = g_mutex_new(); - group->counter.pktCount = 0; - group->counter.pktSize = 0; - group->counter.pktDropped = 0; - group->counter.pktBursted = 0; - - // Set dump time - group->lastDumpTimestamp = time(NULL); - - // Report stuff - group->counterTimeout = reportTimeout; - group->counterRemaining = reportTimeout; - - // Our list of flows - group->flowList = flowList; - - // Verify everything is ok... - if (group->statsLen == 0) - group->statsLen = 10; - - group->pktStats = (struct pktStat_t*) malloc0(sizeof(struct pktStat_t) * group->statsLen); - if (!group->pktStats) - { - // FIXME - log error - fprintf(stderr,"Failed to allocate memory for group->pktStats\n"); - return NULL; - } - return group; -} - - -// Create a packet queue for the flow -struct pktQueue_t *createPktQueue( - long int prio, - long int nfmark, - struct flow_t *parentFlow) -{ - struct pktQueue_t *pktQueue = parentFlow->pktQueues[prio]; - - - // Make sure we valid - if (prio >= NUM_PRIO_BANDS || prio < 0) - { - fprintf(stderr,"ERROR: Queue priority must be between 0 and 99\n"); - return NULL; - } - - pktQueue->nfmark = nfmark; -/* - printf(" Queue added...\n"); - printf(" prio - %li\n",prio); - printf(" nfmark - %li\n",nfmark); -*/ - return pktQueue; -} - - -// Function to update all our flow groups -void updateGroups(struct flow_t *flow, int pktCount, int pktSize, int pktDropped, int pktBursted) -{ - GList *groupItem = g_list_first(flow->groups); - - - // Loop while we have summin - while (groupItem) - { - struct group_t *tmpGroup = groupItem->data; - - - // Lock and load! - g_mutex_lock(tmpGroup->lock); - - // Do our stuff - if (pktCount == -1) - tmpGroup->running.pktCount = 0; - else - tmpGroup->running.pktCount += pktCount; - - if (pktSize == -1) - tmpGroup->running.pktSize = 0; - else - tmpGroup->running.pktSize += pktSize; - - if (pktDropped == -1) - tmpGroup->running.pktDropped = 0; - else - tmpGroup->running.pktDropped += pktDropped; - - if (pktBursted == -1) - tmpGroup->running.pktBursted = 0; - else - tmpGroup->running.pktBursted += pktBursted; - - g_mutex_unlock(tmpGroup->lock); - - // Next one plz - groupItem = g_list_next(groupItem); - } -} - - -// Get average stats len, make sure you lock the flow!!! -struct pktStat_t getFlowTotalStats(struct pktStat_t *pktStats, long int pktStatsLen) -{ - struct pktStat_t returnStats; - long int curStat = 0; - - - // Reset return values - returnStats.pktCount = 0; - returnStats.pktSize = 0; - returnStats.pktDropped = 0; - returnStats.pktBursted = 0; - // Gather stats - for (curStat = 0; curStat < pktStatsLen; curStat++) - { - returnStats.pktCount += pktStats[curStat].pktCount; - returnStats.pktSize += pktStats[curStat].pktSize; - returnStats.pktDropped += pktStats[curStat].pktDropped; - returnStats.pktBursted += pktStats[curStat].pktBursted; - } - - // And return - return returnStats; -} - - -/* Set current credit & burst credit */ -static inline void calculate_flow_credit(struct flow_t *flow, unsigned int pktSize) -{ - // Check if we limited by rate - if (flow->maxRate > 0) - { - struct timeval curTime; - - // Grab exact time now - gettimeofday(&curTime,NULL); - - // Calculate credit - if (curTime.tv_sec - flow->lastCreditCalc.tv_sec > 0) - { - flow->curCredit = pktSize; - // If we must keep track of burst credit, do the same - if (flow->burstRate > 0) - flow->curBurstCredit = pktSize; - gettimeofday(&flow->lastCreditCalc,NULL); - } - else - { - unsigned int accumCredit; - - - // Add up accumulated time - flow->accumMs = curTime.tv_usec - flow->lastCreditCalc.tv_usec; - // See if we have at lease 1 byte to add - if ((accumCredit = flow->accumMs * flow->usCredit) > 1) - { - // If so add it to max of maxRate - flow->curCredit += accumCredit; - - // Add on burst credit, if we can - if (flow->burstRate > 0) - flow->curBurstCredit += (flow->accumMs * flow->usBurstCredit); - - // Remove the number of microseconds we used - flow->accumMs -= (accumCredit / flow->usCredit); - - // If we exceeded our max's, bring them back down a bit - if (flow->curCredit > flow->maxRate) - { - flow->curCredit = flow->maxRate; - flow->accumMs = 0; - } - if (flow->curBurstCredit > flow->burstRate) - flow->curBurstCredit = flow->burstRate; - - // Set this as the last time we calculated credit - gettimeofday(&flow->lastCreditCalc,NULL); - } - } - } -} - - -// Check if we will exceed our credit -static inline int credit_will_exceed(struct flow_t *flow, unsigned int pktSize) -{ - // Without the typecasts we always get a result of 0 .... *shrug* - return (flow->maxRate > 0 && (long int) flow->curCredit - (long int) pktSize < 0); -} - - -// Check if we will exceed our credit -static inline int burst_credit_will_exceed(struct flow_t *flow, unsigned int pktSize) -{ - // If burstRate == 0, it means we can burst to infinity (limited by parent) - return (flow->burstRate != 0 && (long int) flow->curBurstCredit - (long int) pktSize < 0); -} - - - -// Function to process a flow queue, returns number of packets accepted -static int processPktQueue(struct runnerData_t *runnerData, struct pktQueue_t *pktQueue) -{ - int status; - GList *packets; - int exceeded = 0; - int i; - // Differences in queue when we done - int acceptLen = 0, queuedLen = 0; - int acceptSize = 0, queuedSize = 0; - - - // Lock, hijack packets, unlock - g_mutex_lock(pktQueue->lock); - packets = pktQueue->packets; - pktQueue->packets = NULL; - g_mutex_unlock(pktQueue->lock); - - // Check that we within our boundaries - while (!exceeded) - { - struct packet_t *packet; - GList *pktQueueItem; - int ok = 1; - struct flow_t *flow; - struct flow_t *childFlow = NULL; - - - // Get head of the queue - pktQueueItem = g_list_first(packets); - // Last item - if (!pktQueueItem) - break; - // We got summin - packet = pktQueueItem->data; - - - childFlow = flow = pktQueue->parentFlow; - // Loop to root - while (flow && ok) - { - int bursted = 0; - - - // Set new credits - calculate_flow_credit(flow,PKT_SIZE(packet)); - - // Check if we have exceeded stuff we shouldn't - if ((i = credit_will_exceed(flow,PKT_SIZE(packet)))) - { - - // If we can burst.... see if exceed anything - if (flow->burstRate != -1) - { - int i; - - /* Check if we will exceed our burst credit */ - if ((i = burst_credit_will_exceed(flow,PKT_SIZE(packet)))) - ok = 0; - else - // Guess we didn't, we bursted - bursted = 1; - } - else - ok = 0; - } - - // If we have a parent, if we do... do a few tests - if (ok && flow->parent) - { - // Check if we havn't exceeded our parent queues - if (will_exceed_flow_queue(flow->parent,PKT_SIZE(packet))) - ok = 0; - - /* - * if (....) - if we here because of bursting, check if we have a - * willing parent to burst to. - * 1. We cannot burst over our burst-threshold to the parent - * 2. We cannot burst to a parent who is bogged - */ - if (ok && bursted) - { - // Calculate parent credits - calculate_flow_credit(flow->parent,PKT_SIZE(packet)); - - // Check if we exceeded - if (credit_will_exceed(flow->parent,PKT_SIZE(packet)) && - burst_credit_will_exceed(flow->parent,PKT_SIZE(packet))) - ok = 0; - else - { - // Parent threshold stuff, if we havn't exceeded the threshold we can burst - // First check if we can use the parent_th against the burst rate - if (flow->parent->burstRate > 0) - { - if (flow->parent_th > 0 && flow->parent_th < (flow->parent->curThroughput / - (double) flow->parent->burstRate) * 100.0) - ok = 0; - } - // If not, against the maxRate - else if (flow->parent->maxRate > 0) - { - if (flow->parent_th > 0 && flow->parent_th < (flow->parent->curThroughput / - (double) flow->parent->maxRate) * 100.0) - ok = 0; - } - } - } - } - - // If packet is still ok to pass through, do our stuff - if (ok) - { - struct timeval curTime; - - - g_mutex_lock(flow->lock); - - flow->running.pktCount++; - flow->running.pktSize += PKT_SIZE(packet); - flow->curCredit -= PKT_SIZE(packet); - flow->curThroughput += PKT_SIZE(packet); - - // If we can burst ... - if (flow->burstRate > 0) - flow->curBurstCredit -= PKT_SIZE(packet); - - // We bursted - if (bursted) - flow->running.pktBursted++; - - // Grab exact time now - gettimeofday(&curTime,NULL); - - // Add up accumulated time - if (curTime.tv_sec - flow->lastThroughputUpdate.tv_sec > 0) - flow->curThroughputAge += (curTime.tv_sec - flow->lastThroughputUpdate.tv_sec) * 1000000; - else - flow->curThroughputAge += curTime.tv_usec - flow->lastThroughputUpdate.tv_usec; - - // 2 seconds - if (flow->curThroughputAge >= 2000000) - { - flow->curThroughputAge -= 2000000; - flow->curThroughput = flow->curThroughputAge / 1000000.0 * flow->curThroughput; - } - - // Set this as the last time we updated our throughput - gettimeofday(&flow->lastThroughputUpdate,NULL); - - - g_mutex_unlock(flow->lock); - - // Time to update our groups - updateGroups(flow,1,PKT_SIZE(packet),0,0); - - // This flow is inbetween, we accepting traffic through, counters must be updated below - childFlow = flow; - flow = flow->parent; - } - } - - - // Can we accept the packet - if (ok) - { - // Re-inject packet for processing, check if it was ok... - g_mutex_lock(runnerData->IPQLock); - status = ipq_set_verdict(runnerData->IPQHandle, PKT_ID(packet), NF_ACCEPT, 0, NULL); - g_mutex_unlock(runnerData->IPQLock); - if (status < 0) - { - // FIXME - send error to log - fprintf(stderr,"Failed to ACCEPT packet\n"); - break; - } - - // Add to our flow counters, remove from queue and free - acceptSize += PKT_SIZE(packet); - acceptLen++; - packets = g_list_remove(packets,packet); - // Save the size - i = PKT_SIZE(packet); - // "Free" the packet - g_mutex_lock(runnerData->pmem.lock); - runnerData->pmem.freeList = g_list_append(runnerData->pmem.freeList,packet); - g_mutex_unlock(runnerData->pmem.lock); - } - else - { - // We got no where - if (flow == childFlow) - exceeded = 1; - else - { - struct pktQueue_t *nextPktQueue = flow->pktQueues[pktQueue->prio]; - - - // We can do this before locking - queuedLen++; - queuedSize += PKT_SIZE(packet); - packets = g_list_remove(packets,packet); - - // Lock everything - g_mutex_lock(nextPktQueue->lock); - g_mutex_lock(flow->lock); - // Add packet to parent queue & remove from child - nextPktQueue->packets = g_list_append(nextPktQueue->packets,packet); - // Update next queue stats - nextPktQueue->curLen++; - nextPktQueue->curSize += PKT_SIZE(packet); - // Update next flow stats - flow->curQueueLen++; - flow->curQueueSize += PKT_SIZE(packet); - // Finally unlock - g_mutex_unlock(flow->lock); - g_mutex_unlock(nextPktQueue->lock); - - // Signal that we just added to the queue - g_mutex_lock(runnerData->bandSignalLock); - // Check if we havn't already gotten the queue listed - // FIXME: check if we can't just add, high flow queue - if (!g_list_find(runnerData->queueChangeList[nextPktQueue->prio],nextPktQueue)) - runnerData->queueChangeList[nextPktQueue->prio] = - g_list_append(runnerData->queueChangeList[nextPktQueue->prio],nextPktQueue); - g_cond_signal(runnerData->bandSignalCond); - g_mutex_unlock(runnerData->bandSignalLock); - } - - } - } - - // Lock our data and change it, insert left over packets aswell - g_mutex_lock(P_FLOW(pktQueue,lock)); - P_FLOW(pktQueue,curQueueLen) -= (acceptLen + queuedLen); - P_FLOW(pktQueue,curQueueSize) -= (acceptSize + queuedSize); - g_mutex_unlock(P_FLOW(pktQueue,lock)); - - // Update queue aswell - g_mutex_lock(pktQueue->lock); - if (packets) - { - pktQueue->packets = g_list_concat(packets,pktQueue->packets); - // Signal that we just added to the queue - g_mutex_lock(runnerData->bandSignalLock); - // Check if we havn't already gotten the queue listed - // FIXME: check if we can't just add, high flow queue - if (!g_list_find(runnerData->queueChangeList[pktQueue->prio],pktQueue)) - runnerData->queueChangeList[pktQueue->prio] = g_list_append(runnerData->queueChangeList[pktQueue->prio],pktQueue); - g_cond_signal(runnerData->bandSignalCond); - g_mutex_unlock(runnerData->bandSignalLock); - } - pktQueue->curLen -= (acceptLen + queuedLen); - pktQueue->curSize -= (acceptSize + queuedLen); - - g_mutex_unlock(pktQueue->lock); - - return(acceptLen); -} - - - -// Main IPQ->QUEUE thread -void *flowRunner(void *data) -{ - struct runnerData_t *runnerData = (struct runnerData_t*) data; - GList *queueChangeList[NUM_PRIO_BANDS]; - int i, pktsProcessed = 0; - GTimeVal mytime; - - - // Our processing function - void processQueue(void *data, void *user_data) - { - struct pktQueue_t *pktQueue = (struct pktQueue_t*) data; - struct runnerData_t *aRunnerData = (struct runnerData_t*) user_data; - - - // Check if we found a flow with a queue - pktsProcessed += processPktQueue(aRunnerData,pktQueue); - } - - - // Allocate queue change list - for (i = 0; i < NUM_PRIO_BANDS; i++) - queueChangeList[i] = NULL; - - - if (!runnerData->daemon) - printf("Flow runner started...\n"); - - // Loop when we get a signal - while (1) - { - g_mutex_lock(runnerData->bandSignalLock); - - // If the queue has changed proceed - if (runnerData->queueChanged == 0) - { - g_get_current_time(&mytime); - g_time_val_add(&mytime,10000); - g_cond_timed_wait(runnerData->bandSignalCond,runnerData->bandSignalLock,&mytime); - } - - - // Hijack the queue change list items - for (i = 0; i < NUM_PRIO_BANDS; i++) - { - // Copy list item over - queueChangeList[i] = runnerData->queueChangeList[i]; - // Zero runner data if it is non-NULL - if (runnerData->queueChangeList[i]) - { - runnerData->queueChangeList[i] = NULL; - } - else - queueChangeList[i] = NULL; - } - - runnerData->queueChanged = 0; - - g_mutex_unlock(runnerData->bandSignalLock); - - - // Process all list - for (i = 0; i < NUM_PRIO_BANDS; i++) - { - // Process list if it is non-NULL - if (queueChangeList[i]) - { - g_list_foreach(queueChangeList[i],processQueue,runnerData); - g_list_free(queueChangeList[i]); - } - } - } - - return(NULL); -} - - Deleted: trunk/lib/ipq.c =================================================================== --- trunk/lib/ipq.c 2005-01-03 13:39:48 UTC (rev 22) +++ trunk/lib/ipq.c 2005-01-03 13:40:24 UTC (rev 23) @@ -1,374 +0,0 @@ -/* - * ipq.c - IPQ handling for bwmd - * Copyright (C) 2003-2004, Linux Based Systems Design - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * - * 15/04/2003 - Nigel Kukard <nk...@lb...> - * * Initial design -*/ - - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include "autoclass.h" -#include "flow.h" -#include "ipq.h" -#include "libipq.h" - - - -// Destroy the ipq handle -static void destroyIPQHandle(struct ipq_handle *h) -{ - ipq_perror("passer"); - ipq_destroy_handle(h); -} - - -// Return the ipq handle -static struct ipq_handle *getIPQHandle() -{ - struct ipq_handle* h; - int result; - - // Create our ipq socket - h = ipq_create_handle(0, PF_INET, 1048576); - if (!h) - { - fprintf(stderr,"Failed to create IPQ handle\n"); - destroyIPQHandle(h); - return NULL; - } - - // Set mode... - result = ipq_set_mode(h, IPQ_COPY_PACKET, IPQ_BUFSIZE); - if (result < 0) - { - fprintf(stderr,"Failed to set IPQ mode\n"); - destroyIPQHandle(h); - return NULL; - } - - return(h); -} - - -// Close the ipq handle -static void closeIPQHandle(struct ipq_handle *h) -{ - ipq_destroy_handle(h); -} - - -// Check if we will exceed our queue -static inline int will_exceed_pkt_queue(struct pktQueue_t *pktQueue, unsigned int pktSize) -{ - return ((pktQueue->curLen >= pktQueue->maxLen && pktQueue->maxLen != 0) || - (pktQueue->curSize + pktSize >= pktQueue->maxSize && pktQueue->maxSize != 0)); -} - - -// Queue a packet -static int queuePacket(struct runnerData_t *runnerData, long int nfmark, struct packet_t *packet) -{ - struct pktQueue_t *foundQueue = NULL; - struct flow_t *foundFlow = NULL; - int status; - int result; - int drop = 0; - - - // Our find functions - void findQueue(void *data, void *user_data) - { - struct pktQueue_t *pktQueue = (struct pktQueue_t*) data; - - - if (foundQueue) return; - - // Check if we found it - if (pktQueue->nfmark == nfmark) - foundQueue = pktQueue; - } - - void findFlow(void *data, void *user_data) - { - struct flow_t *flow = (struct flow_t*) data; - - - if (foundFlow) return; - - // Check if we found it - if (flow->nfmark == nfmark) - foundFlow = flow; - } - - - // Loop to see what we find... we have to have queues if we going to loop with them - if (runnerData->pktQueues) - g_list_foreach(runnerData->pktQueues,findQueue,NULL); - // Now check if we found a flow?? - if (!foundQueue) - g_list_foreach(runnerData->flows,findFlow,NULL); - - // Check if we have something - if (!foundQueue && !foundFlow) - { - fprintf(stderr,"Queue or flow not found in processFlow() for %li\n", nfmark); - return(-1); - } - - // Now lets see if we don't have a queue... this means AUTO! - if (!foundQueue) - { - struct ip_packet_t *ip_packet = (struct ip_packet_t *) packet->payload->payload; - long int bandNum = -1; - -/* - fprintf(stderr,"auto packet queue -> protocol = %i, src = %i.%i.%i.%i, dest = %i.%i.%i.%i, tos = 0x%x\n", ip_packet->protocol, - ip_packet->u_saddr.addr.a, ip_packet->u_saddr.addr.b, ip_packet->u_saddr.addr.c, ip_packet->u_saddr.addr.d, - ip_packet->u_daddr.addr.a, ip_packet->u_daddr.addr.b, ip_packet->u_daddr.addr.c, ip_packet->u_daddr.addr.d, - IPTOS_PREC(ip_packet->tos)); -*/ - // Process a TCP packet - if (ip_packet->protocol == IPPROTO_TCP) - { - struct tcphdr *tcph = (struct tcphdr *) (packet->payload->payload + (ip_packet->ihl * 4)); -/* - fprintf(stderr," tcp -> sport = %i, dport = %i, prec = 0x%x\n", ntohs(tcph->source), - ntohs(tcph->dest), IPTOS_PREC(ip_packet->tos)); -*/ - bandNum = tcpPortToBand(bandNum,ntohs(tcph->dest)); - if (bandNum == -1) - bandNum = tcpPortToBand(bandNum,ntohs(tcph->source)); - - } - - // Process a ICMP packet - if (ip_packet->protocol == IPPROTO_ICMP) - { -/* - struct icmphdr *icmph = (struct icmphdr *) (packet->payload->payload + (ip_packet->ihl * 4)); - - fprintf(stderr,"something: icmp = %i, type = %i\n", icmph->code, icmph->type); -*/ - bandNum = 5; - } - - // Process a UDP packet - if (ip_packet->protocol == IPPROTO_UDP) - { - struct udphdr *udph = (struct udphdr *) (packet->payload->payload + (ip_packet->ihl * 4)); -/* - fprintf(stderr," udp -> sport = %i, dport = %i\n", ntohs(udph->source), - ntohs(udph->dest)); -*/ - bandNum = udpPortToBand(bandNum,ntohs(udph->dest)); - if (bandNum == -1) - bandNum = udpPortToBand(bandNum,ntohs(udph->source)); - } - - // If we didn't get anything set band number to 50, 50/50 - if (bandNum == -1) - bandNum = 50; - - foundQueue = foundFlow->pktQueues[bandNum]; - } - - - // Lock flow before we fuck with it - g_mutex_lock(foundQueue->lock); - g_mutex_lock(P_FLOW(foundQueue,lock)); - - // Check first of all if we fucked over our one of our queue limits - if (will_exceed_pkt_queue(foundQueue,PKT_SIZE(packet)) || - will_exceed_flow_queue(foundQueue->parentFlow,PKT_SIZE(packet))) - drop = 1; - - -#if 0 - // Check second of all if we fucked our min threshold over - else if (TH_EXCEEDED(foundQueue,min_th)) - { - int j = 1 + (int) (10.0 * rand() / (RAND_MAX + 1.0)); - - if (TH_EXCEEDED(foundQueue,max_th)) - { - if (j > 5) - drop = 1; - } - else - { - if (j < 3) - drop = 1; - } - } -#endif - - // Check if we must pass the packet - if (!drop) - { - // Lock, queue... adjust stats - foundQueue->packets = g_list_append(foundQueue->packets,packet); - foundQueue->curSize += PKT_SIZE(packet); - P_FLOW(foundQueue,curQueueSize) += PKT_SIZE(packet); - foundQueue->curLen++; - P_FLOW(foundQueue,curQueueLen)++; - result = 1; // Packet queued - } - else - { - // Tell kernel to drop packet - g_mutex_lock(runnerData->IPQLock); - status = ipq_set_verdict(runnerData->IPQHandle, PKT_ID(packet), NF_DROP, 0, NULL); - g_mutex_unlock(runnerData->IPQLock); - if (status < 0) - { - fprintf(stderr,"Failed to DROP packet\n"); - result = -1; - } - else - { - g_mutex_lock(P_FLOW(foundQueue,counterLock)); - P_FLOW(foundQueue,running).pktDropped++; - g_mutex_unlock(P_FLOW(foundQueue,counterLock)); - result = 2; // Packet dropped - } - // Conserve memory - g_mutex_lock(runnerData->pmem.lock); - runnerData->pmem.freeList = g_list_append(runnerData->pmem.freeList,packet); - g_mutex_unlock(runnerData->pmem.lock); - } - - // Unlock flow - g_mutex_unlock(P_FLOW(foundQueue,lock)); - g_mutex_unlock(foundQueue->lock); - - // If we have queued, signal a runner - if (result == 1) - { - // Signal that we just added to the queue - g_mutex_lock(runnerData->bandSignalLock); - // Check if we havn't already gotten the queue listed - // FIXME: check if we can't just add, high flow queue - if (!g_list_find(runnerData->queueChangeList[foundQueue->prio],foundQueue)) - runnerData->queueChangeList[foundQueue->prio] = g_list_append(runnerData->queueChangeList[foundQueue->prio],foundQueue); - runnerData->queueChanged = 1; // This basically signals immediate attention by the flow runner - g_cond_signal(runnerData->bandSignalCond); - g_mutex_unlock(runnerData->bandSignalLock); - } - - return(result); -} - - -// Main IPQ->QUEUE thread -void *IPQRunner(void *data) -{ - unsigned char buf[IPQ_BUFSIZE]; - int status; - struct runnerData_t *runnerData = (struct runnerData_t*) data; - ipq_packet_msg_t *m; - struct packet_t *packet; - unsigned int pkt_len = 0; - GList *listItem; - - - if (!runnerData->daemon) - printf("IPQ runner started...\n"); - - // Get IPQ handle - runnerData->IPQHandle = getIPQHandle(); - if (!runnerData->IPQHandle) - { - fprintf(stderr,"Failed to get IPQ handle\n"); - return(NULL); - } - - // And loop - while (1) - { - // Only loop when we have a packet - status = ipq_read(runnerData->IPQHandle, buf, IPQ_BUFSIZE, 0); - - // Loop if timeout - if (status == 0) - continue; - - // Fail if error - if (status < 0) - { - fprintf(stderr,"Failed to get packet from IPQ: %s\n",ipq_errstr()); - // Lock, we dead... accept it! - destroyIPQHandle(runnerData->IPQHandle); - return(NULL); - } - - // We must have a packet... check - switch (ipq_message_type(buf)) - { - case NLMSG_ERROR: - fprintf(stderr, "Received error message: IPQ = %s, SYSTEM = %s\n",ipq_errstr(),strerror(errno)); - break; - - case IPQM_PACKET: - - // Get packet details... - g_mutex_lock(runnerData->IPQLock); - m = ipq_get_packet(buf); - g_mutex_unlock(runnerData->IPQLock); - pkt_len = sizeof(ipq_packet_msg_t) + m->data_len; - - // Get a packet or alloc it - g_mutex_lock(runnerData->pmem.lock); - listItem = g_list_first(runnerData->pmem.freeList); - if (listItem == NULL) - { - - packet = (struct packet_t*) malloc(sizeof(struct packet_t)); - packet->payload = (ipq_packet_msg_t*) malloc(IPQ_BUFSIZE); - } - else - { - packet = listItem->data; - runnerData->pmem.freeList = g_list_remove(runnerData->pmem.freeList,packet); - } - g_mutex_unlock(runnerData->pmem.lock); - - // Copy packet - memcpy(packet->payload,m,pkt_len); - - // Queue the packet... - status = queuePacket(runnerData,packet->payload->mark,packet); - if (status < 0) - fprintf(stderr,"Failed to queue packet\n"); - - break; - - default: - fprintf(stderr, "Unknown message type!\n"); - break; - } - } - - closeIPQHandle(runnerData->IPQHandle); - - printf("Exiting IPQ runner thread\n"); -} - - Deleted: trunk/lib/libipq.c =================================================================== --- trunk/lib/libipq.c 2005-01-03 13:39:48 UTC (rev 22) +++ trunk/lib/libipq.c 2005-01-03 13:40:24 UTC (rev 23) @@ -1,385 +0,0 @@ -/* - * libipq.c - * - * IPQ userspace library. - * - * Please note that this library is still developmental, and there may - * be some API changes. - * - * Author: James Morris <jm...@in...> - * - * 07-11-2001 Modified by Fernando Anton to add support for IPv6. - * 21-04-2003 Modified by Nigel Kukard adjusted a few things. - * - * Copyright (c) 2000-2001 Netfilter Core Team - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - */ - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <unistd.h> -#include <sys/time.h> -#include <sys/types.h> - -#include "libipq.h" - -/**************************************************************************** - * - * Private interface - * - ****************************************************************************/ - -enum { - IPQ_ERR_NONE = 0, - IPQ_ERR_IMPL, - IPQ_ERR_HANDLE, - IPQ_ERR_SOCKET, - IPQ_ERR_BIND, - IPQ_ERR_BUFFER, - IPQ_ERR_RECV, - IPQ_ERR_NLEOF, - IPQ_ERR_ADDRLEN, - IPQ_ERR_STRUNC, - IPQ_ERR_RTRUNC, - IPQ_ERR_NLRECV, - IPQ_ERR_SEND, - IPQ_ERR_SUPP, - IPQ_ERR_RECVBUF, - IPQ_ERR_TIMEOUT, - IPQ_ERR_PROTOCOL, - IPQ_ERR_SETSOCKET -}; -#define IPQ_MAXERR IPQ_ERR_PROTOCOL - -struct ipq_errmap_t { - int errcode; - char *message; -} ipq_errmap[] = { - { IPQ_ERR_NONE, "Unknown error" }, - { IPQ_ERR_IMPL, "Implementation error" }, - { IPQ_ERR_HANDLE, "Unable to create netlink handle" }, - { IPQ_ERR_SOCKET, "Unable to create netlink socket" }, - { IPQ_ERR_BIND, "Unable to bind netlink socket" }, - { IPQ_ERR_BUFFER, "Unable to allocate buffer" }, - { IPQ_ERR_RECV, "Failed to receive netlink message" }, - { IPQ_ERR_NLEOF, "Received EOF on netlink socket" }, - { IPQ_ERR_ADDRLEN, "Invalid peer address length" }, - { IPQ_ERR_STRUNC, "Sent message truncated" }, - { IPQ_ERR_RTRUNC, "Received message truncated" }, - { IPQ_ERR_NLRECV, "Received error from netlink" }, - { IPQ_ERR_SEND, "Failed to send netlink message" }, - { IPQ_ERR_SUPP, "Operation not supported" }, - { IPQ_ERR_RECVBUF, "Receive buffer size invalid" }, - { IPQ_ERR_TIMEOUT, "Timeout"}, - { IPQ_ERR_PROTOCOL, "Invalid protocol specified" }, - { IPQ_ERR_SETSOCKET, "Failed to set socket options" } -}; - -static int ipq_errno = IPQ_ERR_NONE; - -static ssize_t ipq_netlink_sendto(const struct ipq_handle *h, - const void *msg, size_t len); - -static ssize_t ipq_netlink_recvfrom(const struct ipq_handle *h, - unsigned char *buf, size_t len, - int timeout); - -static ssize_t ipq_netlink_sendmsg(const struct ipq_handle *h, - const struct msghdr *msg, - unsigned int flags); - -static char *ipq_strerror(int errcode); - -static ssize_t ipq_netlink_sendto(const struct ipq_handle *h, - const void *msg, size_t len) -{ - int status = sendto(h->fd, msg, len, 0, - (struct sockaddr *)&h->peer, sizeof(h->peer)); - if (status < 0) - ipq_errno = IPQ_ERR_SEND; - return status; -} - -static ssize_t ipq_netlink_sendmsg(const struct ipq_handle *h, - const struct msghdr *msg, - unsigned int flags) -{ - int status = sendmsg(h->fd, msg, flags); - if (status < 0) - ipq_errno = IPQ_ERR_SEND; - return status; -} - -static ssize_t ipq_netlink_recvfrom(const struct ipq_handle *h, - unsigned char *buf, size_t len, - int timeout) -{ - int addrlen, status; - struct nlmsghdr *nlh; - - if (len < sizeof(struct nlmsgerr)) { - ipq_errno = IPQ_ERR_RECVBUF; - return -1; - } - addrlen = sizeof(h->peer); - - if (timeout != 0) { - int ret; - struct timeval tv; - fd_set read_fds; - - if (timeout < 0) { - /* non-block non-timeout */ - tv.tv_sec = 0; - tv.tv_usec = 0; - } else { - tv.tv_sec = timeout / 1000000; - tv.tv_usec = timeout % 1000000; - } - - FD_ZERO(&read_fds); - FD_SET(h->fd, &read_fds); - ret = select(h->fd+1, &read_fds, NULL, NULL, &tv); - if (ret < 0) { - if (errno == EINTR) { - return 0; - } else { - ipq_errno = IPQ_ERR_RECV; - return -1; - } - } - if (!FD_ISSET(h->fd, &read_fds)) { - ipq_errno = IPQ_ERR_TIMEOUT; - return 0; - } - } - status = recvfrom(h->fd, buf, len, 0, - (struct sockaddr *)&h->peer, &addrlen); - if (status < 0) { - ipq_errno = IPQ_ERR_RECV; - return status; - } - if (addrlen != sizeof(h->peer)) { - ipq_errno = IPQ_ERR_RECV; - return -1; - } - if (status == 0) { - ipq_errno = IPQ_ERR_NLEOF; - return -1; - } - nlh = (struct nlmsghdr *)buf; - if (nlh->nlmsg_flags & MSG_TRUNC || nlh->nlmsg_len > status) { - ipq_errno = IPQ_ERR_RTRUNC; - return -1; - } - return status; -} - -static char *ipq_strerror(int errcode) -{ - if (errcode < 0 || errcode > IPQ_MAXERR) - errcode = IPQ_ERR_IMPL; - return ipq_errmap[errcode].message; -} - -/**************************************************************************** - * - * Public interface - * - ****************************************************************************/ - -/* - * Create and initialise an ipq handle. - */ -struct ipq_handle *ipq_create_handle(u_int32_t flags, u_int32_t protocol, int recv_buf_size) -{ - int status; - struct ipq_handle *h; - - h = (struct ipq_handle *)malloc(sizeof(struct ipq_handle)); - if (h == NULL) { - ipq_errno = IPQ_ERR_HANDLE; - return NULL; - } - - memset(h, 0, sizeof(struct ipq_handle)); - - if (protocol == PF_INET) - h->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_FIREWALL); - else if (protocol == PF_INET6) - h->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_IP6_FW); - else { - ipq_errno = IPQ_ERR_PROTOCOL; - free(h); - return NULL; - } - - if (h->fd == -1) { - ipq_errno = IPQ_ERR_SOCKET; - close(h->fd); - free(h); - return NULL; - } - - status = setsockopt(h->fd,SOL_SOCKET,SO_RCVBUF,&recv_buf_size,sizeof(int)); - if (status == -1) { - ipq_errno = IPQ_ERR_SETSOCKET; - close(h->fd); - free(h); - return NULL; - } - - memset(&h->local, 0, sizeof(struct sockaddr_nl)); - h->local.nl_family = AF_NETLINK; - h->local.nl_pid = getpid(); - h->local.nl_groups = 0; - status = bind(h->fd, (struct sockaddr *)&h->local, sizeof(h->local)); - if (status == -1) { - ipq_errno = IPQ_ERR_BIND; - close(h->fd); - free(h); - return NULL; - } - memset(&h->peer, 0, sizeof(struct sockaddr_nl)); - h->peer.nl_family = AF_NETLINK; - h->peer.nl_pid = 0; - h->peer.nl_groups = 0; - return h; -} - -/* - * No error condition is checked here at this stage, but it may happen - * if/when reliable messaging is implemented. - */ -int ipq_destroy_handle(struct ipq_handle *h) -{ - if (h) { - close(h->fd); - free(h); - } - return 0; -} - -int ipq_set_mode(const struct ipq_handle *h, - u_int8_t mode, size_t range) -{ - struct { - struct nlmsghdr nlh; - ipq_peer_msg_t pm; - } req; - - memset(&req, 0, sizeof(req)); - req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(req)); - req.nlh.nlmsg_flags = NLM_F_REQUEST; - req.nlh.nlmsg_type = IPQM_MODE; - req.nlh.nlmsg_pid = h->local.nl_pid; - req.pm.msg.mode.value = mode; - req.pm.msg.mode.range = range; - return ipq_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len); -} - -/* - * timeout is in microseconds (1 second is 1000000 (1 million) microseconds) - * - */ -ssize_t ipq_read(const struct ipq_handle *h, - unsigned char *buf, size_t len, int timeout) -{ - return ipq_netlink_recvfrom(h, buf, len, timeout); -} - -int ipq_message_type(const unsigned char *buf) -{ - return ((struct nlmsghdr*)buf)->nlmsg_type; -} - -int ipq_get_msgerr(const unsigned char *buf) -{ - struct nlmsghdr *h = (struct nlmsghdr *)buf; - struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); - return -err->error; -} - -ipq_packet_msg_t *ipq_get_packet(const unsigned char *buf) -{ - return NLMSG_DATA((struct nlmsghdr *)(buf)); -} - -int ipq_set_verdict(const struct ipq_handle *h, - ipq_id_t id, - unsigned int verdict, - size_t data_len, - unsigned char *buf) -{ - unsigned char nvecs; - size_t tlen; - struct nlmsghdr nlh; - ipq_peer_msg_t pm; - struct iovec iov[3]; - struct msghdr msg; - - memset(&nlh, 0, sizeof(nlh)); - nlh.nlmsg_flags = NLM_F_REQUEST; - nlh.nlmsg_type = IPQM_VERDICT; - nlh.nlmsg_pid = h->local.nl_pid; - memset(&pm, 0, sizeof(pm)); - pm.msg.verdict.value = verdict; - pm.msg.verdict.id = id; - pm.msg.verdict.data_len = data_len; - iov[0].iov_base = &nlh; - iov[0].iov_len = sizeof(nlh); - iov[1].iov_base = ± - iov[1].iov_len = sizeof(pm); - tlen = sizeof(nlh) + sizeof(pm); - nvecs = 2; - if (data_len && buf) { - iov[2].iov_base = buf; - iov[2].iov_len = data_len; - tlen += data_len; - nvecs++; - } - msg.msg_name = (void *)&h->peer; - msg.msg_namelen = sizeof(h->peer); - msg.msg_iov = iov; - msg.msg_iovlen = nvecs; - msg.msg_control = NULL; - msg.msg_controllen = 0; - msg.msg_flags = 0; - nlh.nlmsg_len = tlen; - return ipq_netlink_sendmsg(h, &msg, 0); -} - -/* Not implemented yet */ -int ipq_ctl(const struct ipq_handle *h, int request, ...) -{ - return 1; -} - -char *ipq_errstr(void) -{ - return ipq_strerror(ipq_errno); -} - -void ipq_perror(const char *s) -{ - if (s) - fputs(s, stderr); - else - fputs("ERROR", stderr); - if (ipq_errno) - fprintf(stderr, ": %s", ipq_errstr()); - if (errno) - fprintf(stderr, ": %s", strerror(errno)); - fputc('\n', stderr); -} Modified: trunk/lib/xmlConf.c =================================================================== --- trunk/lib/xmlConf.c 2005-01-03 13:39:48 UTC (rev 22) +++ trunk/lib/xmlConf.c 2005-01-03 13:40:24 UTC (rev 23) @@ -26,6 +26,7 @@ #include <stdio.h> #include <string.h> #include <stdlib.h> +#include <time.h> #include <libxml/xmlmemory.h> #include <libxml/parser.h> #include "common.h" @@ -34,54 +35,6 @@ -/* general functions - static struct confACLChain_t *lookupChain(GHashChain *tables, char *tableName) {{{ */ -// Function to get a chain if it exists or to create it -static struct confACLChain_t *lookupChain(GHashTable *chains, char *chainName) -{ - struct confACLChain_t *tmpChain; - - // See if we have a table by this name - tmpChain = g_hash_table_lookup(chains,chainName); - if (tmpChain == NULL) - { - // If not... create everything - tmpChain = (struct confACLChain_t*) malloc(sizeof(struct confACLChain_t)); - tmpChain->name = strdup(chainName); - tmpChain->defaultTarget = NULL; - // If not... start creating everything... - tmpChain->ruleList = NULL; - tmpChain->ruleset = NULL; - g_hash_table_insert(chains,chainName,tmpChain); - } - - // Return chain - return(tmpChain); -} -/* }}} */ - -/* general functions - static struct confACLTable_t *lookupTable(GHashTable *tables, char *tableName) {{{ */ -// Function to get a table if it exists or to create it -static struct confACLTable_t *lookupTable(GHashTable *tables, char *tableName) -{ - struct confACLTable_t *tmpTable; - - // See if we have a table by this name - tmpTable = g_hash_table_lookup(tables,tableName); - if (tmpTable == NULL) - { - // If not... create everything - tmpTable = (struct confACLTable_t*) malloc(sizeof(struct confACLTable_t)); - tmpTable->name = strdup(tableName); - tmpTable->chains = g_hash_table_new(g_str_hash,g_str_equal); - g_hash_table_insert(tables,tableName,tmpTable); - } - - // Return table - return(tmpTable); -} -/* }}} */ - -/* general functions - static GList *splitContents(xmlNodePtr node) {{{ */ // Function to split out the contents of an xml content into a linked list static GList *splitContents(xmlNodePtr node) { @@ -134,9 +87,8 @@ return(result); } -/* }}} */ -/* general functions - static GHashTable *getProperties(xmlNodePtr node) {{{ */ + // Function to get the properties in a hash table of a node static GHashTable *getProperties(xmlNodePtr node) { @@ -163,9 +115,8 @@ // Return our hash return(attribHash); } -/* }}} */ -/* ruleset functions - static GList *createRuleset(GHashTable *classHash, GList *classList, char *chainName, char *target, char *cmd_line1, char *cmd_line2) {{{ */ + // Function to build a ruleset out of a number of classes and an extra char* at the end static GList *createRuleset(GHashTable *classHash, GList *classList, char *chainName, char *target, char *cmd_line1, char *cmd_line2) { @@ -262,9 +213,8 @@ return(result); } -/* }}} */ -/* ruleset functions - static GList *createClassRuleset(struct confClass_t *confClass) {{{ */ + // Create a classes ruleset static GList *createClassRuleset(struct confClass_t *confClass) { @@ -381,9 +331,7 @@ // Returning the rule list return result; } -/* }}} */ -/* ruleset functions - static char *createNATRuleset(GHashTable *properties) {{{ */ // Function which creates our "special" nat extra cmd_line options static char *createNATRuleset(GHashTable *properties) { @@ -437,9 +385,8 @@ // Return the string FIXME - free mem in parent? return buffer; } -/* }}} */ -/* ruleset functions - static char *createTrafficRuleset(GHashTable *properties) {{{ */ + // Function which creates our "special" traffic extra cmd_line options static char *createTrafficRuleset(GHashTable *properties) { @@ -505,9 +452,53 @@ // Return the string FIXME - free mem in parent? return buffer; } -/* }}} */ -/* parsing functions - static GHashTable *moduleLoadList(char *filename) {{{ */ + +// Function to get a chain if it exists or to create it +struct confACLChain_t *lookupChain(GHashTable *chains, char *chainName) +{ + struct confACLChain_t *tmpChain; + + // See if we have a table by this name + tmpChain = g_hash_table_lookup(chains,chainName); + if (tmpChain == NULL) + { + // If not... create everything + tmpChain = (struct confACLChain_t*) malloc(sizeof(struct confACLChain_t)); + tmpChain->name = strdup(chainName); + tmpChain->defaultTarget = NULL; + // If not... start creating everything... + tmpChain->ruleList = NULL; + tmpChain->ruleset = NULL; + g_hash_table_insert(chains,chainName,tmpChain); + } + + // Return chain + return(tmpChain); +} + + +// Function to get a table if it exists or to create it +struct confACLTable_t *lookupTable(GHashTable *tables, char *tableName) +{ + struct confACLTable_t *tmpTable; + + // See if we have a table by this name + tmpTable = g_hash_table_lookup(tables,tableName); + if (tmpTable == NULL) + { + // If not... create everything + tmpTable = (struct confACLTable_t*) malloc(sizeof(struct confACLTable_t)); + tmpTable->name = strdup(tableName); + tmpTable->chains = g_hash_table_new(g_str_hash,g_str_equal); + g_hash_table_insert(tables,tableName,tmpTable); + } + + // Return table + return(tmpTable); +} + + // Parse the GLOBAL section to get modules we must load GHashTable *getModuleLoadHash(char *filename) { @@ -642,611 +633,254 @@ return(moduleHash); } -/* }}} */ -/* parsing functions - static GHashTable *parseGlobal(xmlDocPtr doc, xmlNodePtr cur) {{{ */ -// Parse the GLOBAL section -static GHashTable *parseGlobal(xmlDocPtr doc, xmlNodePtr cur) + +// Create a flow +static struct flow_t* createFlow( + char *flowName, + struct flow_t *parentFlow, + long int statsLen, + long int maxQueueSize, + long int maxQueueLen, + long int maxRate, + long int burstRate, + long int nfmark, + float parent_th, + int reportTimeout) { - char *className; - xmlNodePtr classNode; - GHashTable *classHash; - struct confClass_t *tempClass; - struct confClassAddress_t *tempClassAddress; - + int i; + struct flow_t *flow = (struct flow_t*) malloc(sizeof(struct flow_t)); - classHash = g_hash_table_new(g_str_hash,g_str_equal); - // We don't care what the top level element name is... - cur = cur->xmlChildrenNode; - while (cur) + // Check if its ok... + if (!flow) { - int validTag = 0; + // FIXME - log error + fprintf(stderr,"Failed to allocate memory for flow\n"); + return NULL; + } + // Lock entire flow + flow->lock = g_mutex_new(); + + strncpy(flow->flowName,flowName,MAX_REFLEN); // copy string + + flow->statsLen = statsLen; + flow->statsPos = 0; - // Check if we found a class - if (!xmlStrcmp(cur->name, (const xmlChar *) "class")) - { - className = xmlGetProp(cur, (const xmlChar *) "name"); - // We have found a class!! - if (className) - { - if (g_hash_table_lookup(classHash,className) == NULL) - { - // Allocate our class - tempClass = (struct confClass_t*) malloc(sizeof(struct confClass_t)); - tempClass->name = className; - tempClass->addresses = NULL; - - // Loop with class contents - classNode = cur->xmlChildrenNode; - while (classNode) - { + flow->parent = parentFlow; - // Check if we are infact doing an address - if (!xmlStrcmp(classNode->name, (const xmlChar *) "address")) - { - // Allocate a new address structure - tempClassAddress = (struct confClassAddress_t*) malloc(sizeof(struct confClassAddress_t)); - tempClassAddress->params = getProperties(classNode); - // FIXME - get this things name from the hash above - tempClassAddress->name = NULL; - // FIXME - we should parse the attribs here?? - - tempClass->addresses = g_list_append(tempClass->addresses,tempClassAddress); - } - // Advance "address" - classNode = classNode->next; - } - - // Create our ruleset - tempClass->ruleset = createClassRuleset(tempClass); - - // Add our class to the major list... - g_hash_table_insert(classHash,className,tempClass); - } - else - fprintf(stderr,"ERROR: Duplicate class found: %s\n",className); - } - else - fprintf(stderr,"ERROR: We found a class without a name\n"); - validTag = 1; - } + flow->maxQueueSize = maxQueueSize; + flow->maxQueueLen = maxQueueLen; + flow->maxRate = maxRate; + flow->burstRate = burstRate; + flow->nfmark = nfmark; - // Check if we found modules to load - if (!xmlStrcmp(cur->name, (const xmlChar *) "modules")) - validTag = 1; + flow->parent_th = parent_th; + + flow->counterTimeout = reportTimeout; + flow->counterRemaining = reportTimeout; - // And a plain text tag... - if (!xmlStrcmp(cur->name, (const xmlChar *) "text")) - validTag = 1; + flow->curQueueSize = 0; + flow->curQueueLen = 0; - // Check if we knew what the tag was - if (!validTag) - fprintf(stderr,"WARNING: Unknown tag %s in GLOBAL section\n",cur->name); - - // Advance "class" - cur = cur->next; - } + // Lock for our counters only + flow->counterLock = g_mutex_new(); + flow->counter.pktCount = 0; + flow->counter.pktSize = 0; + flow->counter.pktDropped = 0; + flow->counter.pktBursted = 0; - return(classHash); -} -/* }}} */ - -/* parsing functions - static GHashTab... [truncated message content] |