Revision: 501
http://vde.svn.sourceforge.net/vde/?rev=501&view=rev
Author: danielel
Date: 2011-06-18 09:47:21 +0000 (Sat, 18 Jun 2011)
Log Message:
-----------
Wirefilter optimizations.
* Introduced 'bit' measure unit as an options.
Wirevalues now can have 'b' appended to indicate that the field is in bits,
e.g. it is possible to use "-b10Mb". Other combination with stocastic
parameters
are still possible, e.g. "-bLR500+10KbN" meaning "From left to right,
bandwidth limit
is 500Kbit +/- 10Kbit with normalized (gaussian) distribution.
Please note that 1Kb = 1000 bits, while 1KB = 1024 Bytes, so even the
multiplier
is re-adopted to the unit.
* Added memory allocation errors.
* Restored hangup detection (e.g. if one endpoint closes the connection)
* Minimum clock for packet handling is one millisecond, to ensure the routines
are not called with the same "now" value (very unlikely to happen, though).
Modified Paths:
--------------
branches/danielinux-wirefilter2/src/wirefilter.c
Modified: branches/danielinux-wirefilter2/src/wirefilter.c
===================================================================
--- branches/danielinux-wirefilter2/src/wirefilter.c 2011-06-17 12:59:21 UTC
(rev 500)
+++ branches/danielinux-wirefilter2/src/wirefilter.c 2011-06-18 09:47:21 UTC
(rev 501)
@@ -94,11 +94,21 @@
#define PIDFILEARG 131
#define LOGSOCKETARG 132
#define LOGIDARG 133
-#define KILO (1<<10)
-#define MEGA (1<<20)
-#define GIGA (1<<30)
+#define KILO ((1<<10) << 3)
+#define MEGA ((1<<20) << 3)
+#define GIGA ((1<<30) << 3)
+#define KILOBIT (1000)
+#define MEGABIT (KILOBIT * KILOBIT)
+#define GIGABIT (KILOBIT * MEGABIT)
+#define VAL_BIT (1)
+#define VAL_BYTE (8)
+
+/* Maximum number of memory allocation fails. */
+#define MEMORY_LIMIT 10
+
+
/* general Markov chain approach */
int markov_numnodes=0;
int markov_current=0;
@@ -591,7 +601,7 @@
double v=0.0;
double vplus=0.0;
int n;
- int mult;
+ int mult = VAL_BYTE, bitmode = 0;
char algo=ALGO_UNIFORM;
n=strlen(s)-1;
while ((s[n] == ' ' || s[n] == '\n' || s[n] == '\t') && n>0)
@@ -621,33 +631,43 @@
break;
}
switch (s[n]) {
+ case 'b':
+ bitmode=1;
+ n--;
+ break;
+ case 'B':
+ bitmode=0;
+ n--;
+ break;
+ }
+ switch (s[n]) {
case 'k':
case 'K':
- mult=KILO;
+ mult = bitmode?KILOBIT:KILO;
break;
case 'm':
case 'M':
- mult=MEGA;
+ mult = bitmode?MEGABIT:MEGA;
break;
case 'g':
case 'G':
- mult=GIGA;
+ mult = bitmode?GIGABIT:GIGA;
break;
default:
- mult=1;
+ mult = bitmode?VAL_BIT:VAL_BYTE;
break;
}
if ((n=sscanf(s,"%lf+%lf",&v,&vplus)) > 0) {
- wv[LR].value=wv[RL].value=v*mult;
- wv[LR].plus=wv[RL].plus=vplus*mult;
- wv[LR].alg=wv[RL].alg=algo;
+ wv[LR].value = wv[RL].value = (v * mult) / VAL_BYTE;
+ wv[LR].plus = wv[RL].plus = (vplus * mult) / VAL_BYTE;
+ wv[LR].alg = wv[RL].alg = algo;
} else if ((n=sscanf(s,"LR%lf+%lf",&v,&vplus)) > 0) {
- wv[LR].value=v*mult;
- wv[LR].plus=vplus*mult;
- wv[LR].alg=algo;
+ wv[LR].value = (v * mult) / VAL_BYTE;
+ wv[LR].plus = vplus * mult / VAL_BYTE;
+ wv[LR].alg = algo;
} else if ((n=sscanf(s,"RL%lf+%lf",&v,&vplus)) > 0) {
- wv[RL].value=v*mult;
- wv[RL].plus=vplus*mult;
+ wv[RL].value = (v * mult) / VAL_BYTE;
+ wv[RL].plus = (vplus * mult) / VAL_BYTE;
wv[RL].alg=algo;
}
return 0;
@@ -827,7 +847,13 @@
struct wf_packet *pkt_in;
if (times > 1) {
pkt_in = malloc(sizeof(struct wf_packet));
- memcpy(pkt_in, pkt, sizeof(struct wf_packet));
+ if (pkt_in)
+ memcpy(pkt_in, pkt, sizeof(struct wf_packet));
+ else {
+ /* Out of memory: send just the original packet
*/
+ times = 1;
+ continue;
+ }
} else
pkt_in = pkt;
@@ -939,8 +965,23 @@
{
struct wf_packet *pkt;
int n;
+ static int oom;
pkt = malloc(sizeof(struct wf_packet));
+ if (!pkt) {
+ /* Out of memory */
+ fprintf(stderr, "Warning: memory limit hit!\n");
+ if (++oom > MEMORY_LIMIT) {
+ fprintf(stderr, "Error: Too much memory used. Try to
reduce buffer sizes.\n");
+ exit(ENOMEM);
+ }
+ if (vdeplug[dir]) {
+ unsigned char discard_buf[BUFSIZE];
+ n=vde_recv(vdeplug[dir],discard_buf,BUFSIZE,0);
+ }
+ return 0;
+ }
+ oom = 0;
pkt->next = NULL;
pkt->dir = dir;
if(vdeplug[dir]) {
@@ -1885,8 +1926,7 @@
initrand();
while(1) {
- unsigned long long delay=nextms();
- unsigned long long now;
+ unsigned long long delay=nextms(), lastcall_time = 0, now;
int markovdelay=markovms();
if (markovdelay >= 0 &&
(markovdelay < delay || delay < 0))
delay=markovdelay;
@@ -1950,12 +1990,20 @@
n--;
}
}
-/* if (n>0) // if there are already pending events, it
means that a ctlfd has hunged up
- exit(0);*/
+ /* if there are still pending events, it means that a
+ ctlfd has hunged up
+ */
+ if (n > 0) {
+ fprintf(stderr, "Connection to the switch was
closed. Exiting...\n");
+ exit(0);
+ }
}
markov_try();
now = gettimeofdayms();
- process_queue_out(now);
- process_queue_in(now);
+ if (now > lastcall_time) {
+ lastcall_time = now;
+ process_queue_out(now);
+ process_queue_in(now);
+ }
}
}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
vde-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/vde-users