From 2bd6c2b6803ca506bf725a19b9d71db17730232f Mon Sep 17 00:00:00 2001 From: Paul Elsner Date: Fri, 24 Jul 2020 13:22:14 +0200 Subject: [PATCH] Add simple burst loss capability --- src/drop.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/drop.c b/src/drop.c index be0e93b..2177953 100644 --- a/src/drop.c +++ b/src/drop.c @@ -5,11 +5,13 @@ #include "common.h" #define NAME "drop" -static Ihandle *inboundCheckbox, *outboundCheckbox, *chanceInput; +static Ihandle *inboundCheckbox, *outboundCheckbox, *chanceInput, *burstLossInput; static volatile short dropEnabled = 0, dropInbound = 1, dropOutbound = 1, - chance = 1000; // [0-10000] + chance = 1000, // [0-10000] + consecutiveDropCount = 0, + bulkLossChance = 0; static Ihandle* dropSetupUI() { @@ -18,6 +20,8 @@ static Ihandle* dropSetupUI() { outboundCheckbox = IupToggle("Outbound", NULL), IupLabel("Chance(%):"), chanceInput = IupText(NULL), + IupLabel("Burst(%):"), + burstLossInput = IupText(NULL), NULL ); @@ -25,6 +29,10 @@ static Ihandle* dropSetupUI() { IupSetAttribute(chanceInput, "VALUE", "10.0"); IupSetCallback(chanceInput, "VALUECHANGED_CB", uiSyncChance); IupSetAttribute(chanceInput, SYNCED_VALUE, (char*)&chance); + IupSetAttribute(burstLossInput, "VISIBLECOLUMNS", "6"); + IupSetAttribute(burstLossInput, "VALUE", "0.0"); + IupSetCallback(burstLossInput, "VALUECHANGED_CB", uiSyncChance); + IupSetAttribute(burstLossInput, SYNCED_VALUE, (char*)&bulkLossChance); IupSetCallback(inboundCheckbox, "ACTION", (Icallback)uiSyncToggle); IupSetAttribute(inboundCheckbox, SYNCED_VALUE, (char*)&dropInbound); IupSetCallback(outboundCheckbox, "ACTION", (Icallback)uiSyncToggle); @@ -58,15 +66,20 @@ static short dropProcess(PacketNode *head, PacketNode* tail) { while (head->next != tail) { PacketNode *pac = head->next; // chance in range of [0, 10000] - if (checkDirection(pac->addr.Direction, dropInbound, dropOutbound) - && calcChance(chance)) { - LOG("dropped with chance %.1f%%, direction %s", - chance/100.0, BOUND_TEXT(pac->addr.Direction)); - freeNode(popNode(pac)); - ++dropped; - } else { - head = head->next; + if (checkDirection(pac->addr.Direction, dropInbound, dropOutbound)) { + if ((consecutiveDropCount > 0 && calcChance(bulkLossChance)) || calcChance(chance)) { + LOG("dropped with chance %.1f%%, direction %s", + chance / 100.0, BOUND_TEXT(pac->addr.Direction)); + freeNode(popNode(pac)); + ++dropped; + ++consecutiveDropCount; + continue; + } + else { + consecutiveDropCount = 0; + } } + head = head->next; } return dropped > 0;