flowprobe: fix sending L2 flows using L2_IP6 template 33/40033/2
authorAlexander Chernavin <achernavin@netgate.com>
Wed, 11 Oct 2023 12:15:55 +0000 (12:15 +0000)
committerDave Wallace <dwallacelf@gmail.com>
Fri, 1 Dec 2023 19:19:44 +0000 (19:19 +0000)
Currently, L2 flows are exported using L2_IP6 template if L3 or L4
recording is enabled on L2 datapath. That occurs because during feature
enable, L2 template is added and its ID is not saved immediately. Then
L2_IP4 and L2_IP6 templates are added overwriting "template_id" each
time. And in the end, the current value of "template_id" is saved for L2
template. The problem is that "template_id" at that point contains the
ID of L2_IP6 template.

With this fix, save the template ID immediately after adding a template
for all variants (datapaths). Also, cover the case with a test.

Type: fix
Change-Id: Id27288043b3b8f0e89e77f45ae9a01fa7439e20e
Signed-off-by: Alexander Chernavin <achernavin@netgate.com>
(cherry picked from commit 120095d3d33bfac64c1f3c870f8a332eeaf638f0)

src/plugins/flowprobe/flowprobe.c
test/test_flowprobe.py

index 058a642..3b060be 100644 (file)
@@ -554,6 +554,7 @@ flowprobe_interface_add_del_feature (flowprobe_main_t *fm, u32 sw_if_index,
                                               flowprobe_data_callback_l2,
                                               flowprobe_template_rewrite_l2,
                                               is_add, &template_id);
+             fm->template_reports[flags] = (is_add) ? template_id : 0;
            }
          if (fm->record & FLOW_RECORD_L3 || fm->record & FLOW_RECORD_L4)
            {
@@ -576,20 +577,22 @@ flowprobe_interface_add_del_feature (flowprobe_main_t *fm, u32 sw_if_index,
                flags | FLOW_RECORD_L2_IP4;
              fm->context[FLOW_VARIANT_L2_IP6].flags =
                flags | FLOW_RECORD_L2_IP6;
-
-             fm->template_reports[flags] = template_id;
            }
        }
       else if (which == FLOW_VARIANT_IP4)
-       rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
-                                        flowprobe_data_callback_ip4,
-                                        flowprobe_template_rewrite_ip4,
-                                        is_add, &template_id);
+       {
+         rv = flowprobe_template_add_del (
+           1, UDP_DST_PORT_ipfix, flags, flowprobe_data_callback_ip4,
+           flowprobe_template_rewrite_ip4, is_add, &template_id);
+         fm->template_reports[flags] = (is_add) ? template_id : 0;
+       }
       else if (which == FLOW_VARIANT_IP6)
-       rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
-                                        flowprobe_data_callback_ip6,
-                                        flowprobe_template_rewrite_ip6,
-                                        is_add, &template_id);
+       {
+         rv = flowprobe_template_add_del (
+           1, UDP_DST_PORT_ipfix, flags, flowprobe_data_callback_ip6,
+           flowprobe_template_rewrite_ip6, is_add, &template_id);
+         fm->template_reports[flags] = (is_add) ? template_id : 0;
+       }
     }
   if (rv && rv != VNET_API_ERROR_VALUE_EXIST)
     {
@@ -600,7 +603,6 @@ flowprobe_interface_add_del_feature (flowprobe_main_t *fm, u32 sw_if_index,
   if (which != (u8) ~ 0)
     {
       fm->context[which].flags = fm->record;
-      fm->template_reports[flags] = (is_add) ? template_id : 0;
     }
 
   if (direction == FLOW_DIRECTION_RX || direction == FLOW_DIRECTION_BOTH)
index 1d565dc..19571f7 100644 (file)
@@ -11,6 +11,7 @@ from scapy.packet import Raw
 from scapy.layers.l2 import Ether
 from scapy.layers.inet import IP, TCP, UDP
 from scapy.layers.inet6 import IPv6
+from scapy.contrib.lacp import SlowProtocol, LACP
 
 from config import config
 from framework import tag_fixme_vpp_workers, tag_fixme_ubuntu2204, tag_fixme_debian11
@@ -273,9 +274,9 @@ class MethodHolder(VppTestCase):
             if self.debug_print:
                 print(data)
             if ip_ver == "v4":
-                ip_layer = capture[0][IP]
+                ip_layer = capture[0][IP] if capture[0].haslayer(IP) else None
             else:
-                ip_layer = capture[0][IPv6]
+                ip_layer = capture[0][IPv6] if capture[0].haslayer(IPv6) else None
             if data_set is not None:
                 for record in data:
                     # skip flow if ingress/egress interface is 0
@@ -682,6 +683,71 @@ class DatapathTestsHolder(object):
         ipfix.remove_vpp_config()
         self.logger.info("FFP_TEST_FINISH_0002")
 
+    def test_L23onL2(self):
+        """L2/3 data on L2 datapath"""
+        self.pg_enable_capture(self.pg_interfaces)
+        self.pkts = []
+
+        ipfix = VppCFLOW(
+            test=self, intf=self.intf1, layer="l2 l3", direction=self.direction
+        )
+        ipfix.add_vpp_config()
+
+        ipfix_decoder = IPFIXDecoder()
+        # template packet should arrive immediately
+        templates = ipfix.verify_templates(ipfix_decoder, count=3)
+
+        # verify IPv4 and IPv6 flows
+        for ip_ver in ("v4", "v6"):
+            self.create_stream(packets=1, ip_ver=ip_ver)
+            capture = self.send_packets()
+
+            # make sure the one packet we expect actually showed up
+            self.vapi.ipfix_flush()
+            cflow = self.wait_for_cflow_packet(
+                self.collector, templates[1 if ip_ver == "v4" else 2]
+            )
+            src_ip_id = 8 if ip_ver == "v4" else 27
+            dst_ip_id = 12 if ip_ver == "v4" else 28
+            self.verify_cflow_data_detail(
+                ipfix_decoder,
+                capture,
+                cflow,
+                {
+                    2: "packets",
+                    256: 8 if ip_ver == "v4" else 56710,
+                    4: 17,
+                    src_ip_id: "src_ip",
+                    dst_ip_id: "dst_ip",
+                    61: (self.direction == "tx"),
+                },
+                ip_ver=ip_ver,
+            )
+
+        # verify non-IP flow
+        self.pkts = [
+            (
+                Ether(dst=self.pg2.local_mac, src=self.pg1.remote_mac)
+                / SlowProtocol()
+                / LACP()
+            )
+        ]
+        capture = self.send_packets()
+
+        # make sure the one packet we expect actually showed up
+        self.vapi.ipfix_flush()
+        cflow = self.wait_for_cflow_packet(self.collector, templates[0])
+        self.verify_cflow_data_detail(
+            ipfix_decoder,
+            capture,
+            cflow,
+            {2: "packets", 256: 2440, 61: (self.direction == "tx")},
+        )
+
+        self.collector.get_capture(6)
+
+        ipfix.remove_vpp_config()
+
     def test_L4onL2(self):
         """L4 data on L2 datapath"""
         self.logger.info("FFP_TEST_START_0003")