From a4e156f4eab41f18dad4afc13a1c31f3e080c6ba Mon Sep 17 00:00:00 2001
From: David Schmitz <schmitz@lrz.de>
Date: Wed, 21 Feb 2024 13:58:37 +0000
Subject: [PATCH] fix/wrong_ratelimit_stats: fix broken snmp updates; fix for
 too verbose snmp updates; fix for support of matched vs. dropped packets

---
 flowspec/snmpstats.py                         |  28 ++---
 templates/flowspy/route_details.html          | 100 ++++++++++--------
 vnet_router/fod_vnet_router                   |   6 +-
 vnet_router/snmp/pass_persisttest_bgpflowspec |   2 +-
 4 files changed, 73 insertions(+), 63 deletions(-)

diff --git a/flowspec/snmpstats.py b/flowspec/snmpstats.py
index c649375f..7544416d 100644
--- a/flowspec/snmpstats.py
+++ b/flowspec/snmpstats.py
@@ -43,7 +43,7 @@ def snmpCallback(snmpEngine, sendRequestHandle, errorIndication,
           errorStatus, errorIndex, varBindTable, cbCtx):
   try:
     (authData, transportTarget, results) = cbCtx
-    logger.error('snmpCallback(): called')
+    #logger.info('snmpCallback(): called {}'.format(transportTarget))
 
     # debug - which router replies:
     #print('%s via %s' % (authData, transportTarget))
@@ -71,7 +71,7 @@ def snmpCallback(snmpEngine, sendRequestHandle, errorIndication,
                 logger.debug('snmpCallback(): Finished {}.'.format(transportTarget))
                 return 0
 
-            last_snmp_var_got__from__transportTarget__hash[tabstoptr(transportTarget)]=name
+            last_snmp_var_got__from__transportTarget__hash[str(transportTarget)]=name
 
             ident = name[identoffset:]
             ordvals = [int(i) for i in ident.split(".")]
@@ -84,13 +84,13 @@ def snmpCallback(snmpEngine, sendRequestHandle, errorIndication,
                 len2 = ordvals[len1] + 1
                 routename = "".join([chr(i) for i in ordvals[len1 + 1:len1 + len2]])
 
-                logger.error("routename="+str(routename))
+                #logger.info("routename="+str(routename))
                 xtype='counter'
-                if re.match(r'^[0-9]+[Mk]_', routename):
+                if re.match(r'^[0-9]+[MmKk]_', routename):
                     ary=re.split(r'_', routename, maxsplit=1)
                     xtype=ary[0]
                     routename=ary[1]
-                logger.error("=> routename="+str(routename)+" xtype="+str(xtype))
+                #logger.info("=> routename="+str(routename)+" xtype="+str(xtype))
 
                 # add value into dict
                 if routename in results:
@@ -271,7 +271,7 @@ def helper_get_countertype_of_rule(ruleobj):
    for thenaction in ruleobj.then.all():
        if thenaction.action and thenaction.action=='rate-limit':
            limit_rate=thenaction.action_value
-           xtype=str(limit_rate)
+           xtype=str(limit_rate).upper()
    return xtype
 
 #
@@ -398,8 +398,8 @@ def poll_snmp_statistics():
               counter_null = {"ts": rule_last_updated.isoformat(), "value": null_measurement }
               counter_zero = {"ts": rule_last_updated.isoformat(), "value": zero_measurement }
             else:
-              counter_null = {"ts": rule_last_updated.isoformat(), "value": null_measurement, "value_dropped": null_measurement }
-              counter_zero = {"ts": rule_last_updated.isoformat(), "value": zero_measurement, "value_dropped": zero_measurement }
+              counter_null = {"ts": rule_last_updated.isoformat(), "value": null_measurement, "value_matched": null_measurement }
+              counter_zero = {"ts": rule_last_updated.isoformat(), "value": zero_measurement, "value_matched": zero_measurement }
 
             #logger.info("snmpstats: STATISTICS_PER_RULE ruleobj="+str(ruleobj))
             #logger.info("snmpstats: STATISTICS_PER_RULE ruleobj.type="+str(type(ruleobj)))
@@ -416,14 +416,14 @@ def poll_snmp_statistics():
                 else:
                   logger.info("poll_snmp_statistics(): 1b STATISTICS_PER_RULE rule_id="+str(rule_id))
                   try:
-                    val1 = newdata[flowspec_params_str][xtype_default]
+                    val_dropped = newdata[flowspec_params_str][xtype_default]
                   except Exception:
-                    val1 = 1
+                    val_dropped = 1
                   try:
-                    val2 = newdata[flowspec_params_str][xtype]
+                    val_matched = newdata[flowspec_params_str][xtype]
                   except Exception:
-                    val2 = 1
-                  counter = {"ts": nowstr, "value": val1, "value_dropped": val2}
+                    val_matched = 1
+                  counter = { "ts": nowstr, "value": val_dropped, "value_matched": val_matched }
 
                 counter_is_null = False
               except Exception as e:
@@ -536,7 +536,7 @@ def add_initial_zero_value(rule_id, route_obj, zero_or_null=True):
     if xtype==xtype_default:
       counter = {"ts": nowstr, "value": zero_measurement }
     else:
-      counter = {"ts": nowstr, "value": zero_measurement, "value_dropped": zero_measurement }
+      counter = {"ts": nowstr, "value": zero_measurement, "value_matched": zero_measurement }
         
     samplecount = settings.SNMP_MAX_SAMPLECOUNT
 
diff --git a/templates/flowspy/route_details.html b/templates/flowspy/route_details.html
index f05f4f71..45080c6b 100644
--- a/templates/flowspy/route_details.html
+++ b/templates/flowspy/route_details.html
@@ -152,12 +152,12 @@ function plotGraph(route_then_action, data)
    var ybytesdata = Array();
    var ybytesdatarel = Array();
 
-   var ydroppedpkgdata = Array();
-   var ydroppedpkgdatarel = Array();
-   var ydroppedbytesdata = Array();
-   var ydroppedbytesdatarel = Array();
+   var ymatchedpkgdata = Array();
+   var ymatchedpkgdatarel = Array();
+   var ymatchedbytesdata = Array();
+   var ymatchedbytesdatarel = Array();
 
-   var ydropped_available = false;
+   var ymatched_available = false;
 
    for (i=0; i<data["data"].length; i++) {
        var d = data["data"][data["data"].length - 1 - i];
@@ -178,21 +178,29 @@ function plotGraph(route_then_action, data)
            ybytesdatarel[i] = (bytesdelta===undefined || bytesdelta>=0) ? bytesdelta : 0;
        }
 
-       if (d.value_dropped!=undefined) {
-         ydropped_available=true
+       if (d.value_matched!=undefined) {
+         ymatched_available=true
+	 ymatched_packets = d.value_matched.packets;
+	 ymatched_bytes = d.value_matched.bytes;
+       } else if (d.value_dropped!=undefined) { // support for ".value_dropped" which actually really effectively was matched bytes/packets (as having been wrongly used/named, and which was in use for a short time in 2024-02)
+         ymatched_available=true
+	 ymatched_packets = d.value_dropped.packets;
+	 ymatched_bytes = d.value_dropped.bytes;
+       }
 
-         ydroppedpkgdata[i] = d.value_dropped.packets;
-         ydroppedbytesdata[i] = d.value_dropped.bytes;
+       if (ymatched_available) {
+         ymatchedpkgdata[i] = ymatched_packets;
+         ymatchedbytesdata[i] = ymatched_bytes;
   
          if (i == 0) {
-             ydroppedpkgdatarel[i] = 0;
-             ydroppedbytesdatarel[i] = 0;
+             ymatchedpkgdatarel[i] = 0;
+             ymatchedbytesdatarel[i] = 0;
          } else {
-             delta = (ydroppedpkgdata[i]===undefined) ? undefined : (ydroppedpkgdata[i-1]===undefined) ? ydroppedpkgdata[i] : (ydroppedpkgdata[i] - ydroppedpkgdata[i-1]);
-             ydroppedpkgdatarel[i] = (delta===undefined || delta>=0) ? delta : 0;
+             delta = (ymatchedpkgdata[i]===undefined) ? undefined : (ymatchedpkgdata[i-1]===undefined) ? ymatchedpkgdata[i] : (ymatchedpkgdata[i] - ymatchedpkgdata[i-1]);
+             ymatchedpkgdatarel[i] = (delta===undefined || delta>=0) ? delta : 0;
   
-             bytesdelta = (ydroppedbytesdata[i]===undefined) ? undefined : (ydroppedbytesdata[i-1]===undefined) ? ydroppedbytesdata[i] : (ydroppedbytesdata[i] - ydroppedbytesdata[i-1]);
-             ydroppedbytesdatarel[i] = (bytesdelta===undefined || bytesdelta>=0) ? bytesdelta : 0;
+             bytesdelta = (ymatchedbytesdata[i]===undefined) ? undefined : (ymatchedbytesdata[i-1]===undefined) ? ymatchedbytesdata[i] : (ymatchedbytesdata[i] - ymatchedbytesdata[i-1]);
+             ymatchedbytesdatarel[i] = (bytesdelta===undefined || bytesdelta>=0) ? bytesdelta : 0;
          }
   
   
@@ -228,24 +236,24 @@ function plotGraph(route_then_action, data)
 
 
    if (is_drop_rule) {
-     matched_text = "matched and dropped";
+     dropped_text = "matched and dropped";
      value1__borderColor = drop__borderColor;
      value1__pointbackgroundColor = drop__pointbackgroundColor;
      value1__backgroundColor = drop__backgroundColor;
    } else if (is_accept_rule) {
-     matched_text = "matched and accepted";
+     dropped_text = "matched and accepted";
      value1__borderColor = accept__borderColor;
      value1__pointbackgroundColor = accept__pointbackgroundColor;
      value1__backgroundColor = accept__backgroundColor;
    } else { 
-     matched_text = "matched";
-     value1__borderColor = matched__borderColor;
-     value1__pointbackgroundColor = matched__pointbackgroundColor;
-     value1__backgroundColor = matched__backgroundColor;
+     dropped_text = "dropped";
+     value1__borderColor = drop__borderColor;
+     value1__pointbackgroundColor = drop__pointbackgroundColor;
+     value1__backgroundColor = drop__backgroundColor;
    }
   
    var ypkg_datasets = [{
-           label: '# packets '+matched_text,
+           label: '# packets '+dropped_text,
            data: ypkgdata,
            borderWidth: 2,
            borderColor: value1__borderColor,
@@ -256,7 +264,7 @@ function plotGraph(route_then_action, data)
            //backgroundColor: "#99bfff"
        }];
    var ypkgrel_datasets = [{
-           label: '# packets '+matched_text,
+           label: '# packets '+dropped_text,
            data: ypkgdatarel,
            borderWidth: 2,
            borderColor: value1__borderColor,
@@ -267,7 +275,7 @@ function plotGraph(route_then_action, data)
            //backgroundColor: "#ff877a"
        }];
    var ybytes_datasets = [{
-           label: '# bytes '+matched_text,
+           label: '# bytes '+dropped_text,
            data: ybytesdata,
            borderWidth: 2,
            borderColor: value1__borderColor,
@@ -278,7 +286,7 @@ function plotGraph(route_then_action, data)
            //backgroundColor: "#99bfff"
        }];
     var ybytesrel_datasets = [{
-           label: '# bytes '+matched_text,
+           label: '# bytes '+dropped_text,
            data: ybytesdatarel,
            borderWidth: 2,
            borderColor: value1__borderColor,
@@ -290,38 +298,38 @@ function plotGraph(route_then_action, data)
        }];
 
 
-   if (ydropped_available) {
+   if (ymatched_available) {
      ypkg_datasets.push({
-           label: '# packets dropped',
-           data: ydroppedpkgdata,
+           label: '# packets matched',
+           data: ymatchedpkgdata,
            borderWidth: 2,
-           borderColor: drop__borderColor,
-           pointBackgroundColor: drop__pointbackgroundColor,
-           backgroundColor: drop__backgroundColor
+           borderColor: matched__borderColor,
+           pointBackgroundColor: matched__pointbackgroundColor,
+           backgroundColor: matched__backgroundColor
        });
      ypkgrel_datasets.push({
-           label: '# packets dropped',
-           data: ydroppedpkgdatarel,
+           label: '# packets matched',
+           data: ymatchedpkgdatarel,
            borderWidth: 2,
-           borderColor: drop__borderColor,
-           pointBackgroundColor: drop__pointbackgroundColor,
-           backgroundColor: drop__backgroundColor
+           borderColor: matched__borderColor,
+           pointBackgroundColor: matched__pointbackgroundColor,
+           backgroundColor: matched__backgroundColor
        });
      ybytes_datasets.push({
-           label: '# bytes dropped',
-           data: ydroppedbytesdata,
+           label: '# bytes matched',
+           data: ymatchedbytesdata,
            borderWidth: 2,
-           borderColor: drop__borderColor,
-           pointBackgroundColor: drop__pointbackgroundColor,
-           backgroundColor: drop__backgroundColor
+           borderColor: matched__borderColor,
+           pointBackgroundColor: matched__pointbackgroundColor,
+           backgroundColor: matched__backgroundColor
        });
      ybytesrel_datasets.push({
-           label: '# bytes dropped',
-           data: ydroppedbytesdatarel,
+           label: '# bytes matched',
+           data: ymatchedbytesdatarel,
            borderWidth: 2,
-           borderColor: drop__borderColor,
-           pointBackgroundColor: drop__pointbackgroundColor,
-           backgroundColor: drop__backgroundColor
+           borderColor: matched__borderColor,
+           pointBackgroundColor: matched__pointbackgroundColor,
+           backgroundColor: matched__backgroundColor
        });
    }
 
diff --git a/vnet_router/fod_vnet_router b/vnet_router/fod_vnet_router
index f41566c6..68fdec4c 100755
--- a/vnet_router/fod_vnet_router
+++ b/vnet_router/fod_vnet_router
@@ -948,8 +948,10 @@ elif [ "$1" = "--process_ruleinfo" ]; then #arg
 
       fi
       
-      counter_values_read="$random_value_read_bytes $random_value_read_pkgs"
-      counter_values_drop="$random_value_drop_bytes $random_value_drop_pkgs"
+      #counter_values_read="$random_value_read_bytes $random_value_read_pkgs"
+      #counter_values_drop="$random_value_drop_bytes $random_value_drop_pkgs"
+      counter_values_drop="$random_value_read_bytes $random_value_read_pkgs"
+      counter_values_read="$random_value_drop_bytes $random_value_drop_pkgs"
     fi
 
     ##
diff --git a/vnet_router/snmp/pass_persisttest_bgpflowspec b/vnet_router/snmp/pass_persisttest_bgpflowspec
index 06ab9153..930f5aa5 100755
--- a/vnet_router/snmp/pass_persisttest_bgpflowspec
+++ b/vnet_router/snmp/pass_persisttest_bgpflowspec
@@ -152,7 +152,7 @@ sub get_snmp_rulename
   if (defined($thenaction)) {
     if ($thenaction eq 'discard') {
     } elsif ($thenaction =~ /^rate-limit-([0-9]+([Mk]?))$/) {
-      $rulename = $1."_".$rulename;
+      $rulename = uc($1)."_".$rulename;
       $counter_type_value = 3; # policer
     }
   }
-- 
GitLab