Merge "[SEBA-875] Filtering out LLDP packets"
diff --git a/internal/bbsim/devices/nni.go b/internal/bbsim/devices/nni.go
index 3a36287..ad01f2f 100644
--- a/internal/bbsim/devices/nni.go
+++ b/internal/bbsim/devices/nni.go
@@ -74,34 +74,43 @@
 // sendNniPacket will send a packet out of the NNI interface.
 // We will send upstream only DHCP packets and drop anything else
 func sendNniPacket(packet gopacket.Packet) error {
-	if isDhcp := packetHandlers.IsDhcpPacket(packet); !isDhcp {
+	isDhcp := packetHandlers.IsDhcpPacket(packet)
+	isLldp := packetHandlers.IsLldpPacket(packet)
+
+	if isDhcp == false && isLldp == false {
 		nniLogger.WithFields(log.Fields{
 			"packet": packet,
 		}).Trace("Dropping NNI packet as it's not DHCP")
+		return nil
 	}
 
-	packet, err := packetHandlers.PopDoubleTag(packet)
-	if err != nil {
-		nniLogger.WithFields(log.Fields{
-			"packet": packet,
-		}).Errorf("Can't remove double tags from packet: %v", err)
-		return err
-	}
+	if isDhcp {
+		packet, err := packetHandlers.PopDoubleTag(packet)
+		if err != nil {
+			nniLogger.WithFields(log.Fields{
+				"packet": packet,
+			}).Errorf("Can't remove double tags from packet: %v", err)
+			return err
+		}
 
-	handle, err := getVethHandler(nniVeth)
-	if err != nil {
-		return err
-	}
+		handle, err := getVethHandler(nniVeth)
+		if err != nil {
+			return err
+		}
 
-	err = handle.WritePacketData(packet.Data())
-	if err != nil {
-		nniLogger.WithFields(log.Fields{
-			"packet": packet,
-		}).Errorf("Failed to send packet out of the NNI: %s", err)
-		return err
-	}
+		err = handle.WritePacketData(packet.Data())
+		if err != nil {
+			nniLogger.WithFields(log.Fields{
+				"packet": packet,
+			}).Errorf("Failed to send packet out of the NNI: %s", err)
+			return err
+		}
 
-	nniLogger.Infof("Sent packet out of NNI")
+		nniLogger.Infof("Sent packet out of NNI")
+	} else if isLldp {
+		// TODO rework this when BBSim supports data-plane packets
+		nniLogger.Trace("Received LLDP Packet, ignoring it")
+	}
 	return nil
 }
 
diff --git a/internal/bbsim/packetHandlers/filters.go b/internal/bbsim/packetHandlers/filters.go
index b43b677..9ada88c 100644
--- a/internal/bbsim/packetHandlers/filters.go
+++ b/internal/bbsim/packetHandlers/filters.go
@@ -30,6 +30,13 @@
 	return false
 }
 
+func IsLldpPacket(pkt gopacket.Packet) bool {
+	if layer := pkt.Layer(layers.LayerTypeLinkLayerDiscovery); layer != nil {
+		return true
+	}
+	return false
+}
+
 // return true if the packet is coming in the OLT from the NNI port
 // it uses the ack to check if the source is the one we assigned to the
 // dhcp server
diff --git a/internal/bbsim/packetHandlers/filters_test.go b/internal/bbsim/packetHandlers/filters_test.go
index 6064e68..8d5f96a 100644
--- a/internal/bbsim/packetHandlers/filters_test.go
+++ b/internal/bbsim/packetHandlers/filters_test.go
@@ -62,6 +62,50 @@
 	assert.Equal(t, res, false)
 }
 
+func Test_IsLldpPacket_True(t *testing.T) {
+	layer := &layers.LinkLayerDiscovery{
+		PortID: layers.LLDPPortID{
+			ID:      []byte{1, 2, 3, 4},
+			Subtype: layers.LLDPPortIDSubtypeMACAddr,
+		},
+		ChassisID: layers.LLDPChassisID{
+			ID:      []byte{1, 2, 3, 4},
+			Subtype: layers.LLDPChassisIDSubTypeMACAddr,
+		},
+	}
+
+	buffer := gopacket.NewSerializeBuffer()
+	opts := gopacket.SerializeOptions{FixLengths: true}
+	err := gopacket.SerializeLayers(buffer, opts, layer)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	packet := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeLinkLayerDiscovery, gopacket.DecodeOptions{})
+
+	res := packetHandlers.IsLldpPacket(packet)
+	assert.Equal(t, res, true)
+}
+
+func Test_IsLldpPacket_False(t *testing.T) {
+	eth := &layers.Ethernet{
+		DstMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, 0x15, 0x16},
+		SrcMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, 0x15, 0x17},
+	}
+
+	buffer := gopacket.NewSerializeBuffer()
+	opts := gopacket.SerializeOptions{FixLengths: true}
+	err := gopacket.SerializeLayers(buffer, opts, eth)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	ethernetPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeEthernet, gopacket.DecodeOptions{})
+
+	res := packetHandlers.IsLldpPacket(ethernetPkt)
+	assert.Equal(t, res, false)
+}
+
 func Test_IsIncomingPacket_True(t *testing.T) {
 	eth := &layers.IPv4{
 		SrcIP: net.ParseIP("192.168.254.1"),