[VOL-2778] Adding pbits to DHCP packets

Change-Id: I3bfb80c566a08ec69f05f90e33b8196ad401b33d
diff --git a/internal/bbsim/devices/olt.go b/internal/bbsim/devices/olt.go
index 49ea3fe..61cbfcc 100644
--- a/internal/bbsim/devices/olt.go
+++ b/internal/bbsim/devices/olt.go
@@ -766,7 +766,7 @@
 
 			service := s.(*Service)
 
-			doubleTaggedPkt, err := packetHandlers.PushDoubleTag(service.STag, service.CTag, message.Pkt)
+			doubleTaggedPkt, err := packetHandlers.PushDoubleTag(service.STag, service.CTag, message.Pkt, service.UsPonCTagPriority)
 			if err != nil {
 				log.Error("Fail to add double tag to packet")
 			}
diff --git a/internal/bbsim/devices/services.go b/internal/bbsim/devices/services.go
index 15d313a..bee3065 100644
--- a/internal/bbsim/devices/services.go
+++ b/internal/bbsim/devices/services.go
@@ -51,10 +51,10 @@
 	TechnologyProfileID int
 	UniTagMatch         int
 	ConfigureMacAddress bool
-	UsPonCTagPriority   int
-	UsPonSTagPriority   int
-	DsPonCTagPriority   int
-	DsPonSTagPriority   int
+	UsPonCTagPriority   uint8
+	UsPonSTagPriority   uint8
+	DsPonCTagPriority   uint8
+	DsPonSTagPriority   uint8
 
 	// state
 	GemPort       uint32
@@ -66,7 +66,7 @@
 
 func NewService(name string, hwAddress net.HardwareAddr, onu *Onu, cTag int, sTag int,
 	needsEapol bool, needsDchp bool, needsIgmp bool, tpID int, uniTagMatch int, configMacAddress bool,
-	usPonCTagPriority int, usPonSTagPriority int, dsPonCTagPriority int, dsPonSTagPriority int) (*Service, error) {
+	usPonCTagPriority uint8, usPonSTagPriority uint8, dsPonCTagPriority uint8, dsPonSTagPriority uint8) (*Service, error) {
 
 	service := Service{
 		Name:                name,
@@ -262,7 +262,7 @@
 		if msg.Type == packetHandlers.EAPOL {
 			eapol.HandleNextPacket(msg.OnuId, msg.IntfId, s.GemPort, s.Onu.Sn(), s.Onu.PortNo, s.EapolState, msg.Packet, stream, nil)
 		} else if msg.Type == packetHandlers.DHCP {
-			_ = dhcp.HandleNextPacket(s.Onu.PonPort.Olt.ID, s.Onu.ID, s.Onu.PonPortID, s.Onu.Sn(), s.Onu.PortNo, s.CTag, s.GemPort, s.HwAddress, s.DHCPState, msg.Packet, stream)
+			_ = dhcp.HandleNextPacket(s.Onu.PonPort.Olt.ID, s.Onu.ID, s.Onu.PonPortID, s.Onu.Sn(), s.Onu.PortNo, s.CTag, s.GemPort, s.HwAddress, s.DHCPState, msg.Packet, s.UsPonCTagPriority, stream)
 		}
 	}
 }
@@ -326,7 +326,7 @@
 	}).Debugf("HandleDHCPStart")
 
 	if err := dhcp.SendDHCPDiscovery(s.Onu.PonPort.Olt.ID, s.Onu.PonPortID, s.Onu.ID, int(s.CTag), s.GemPort,
-		s.Onu.Sn(), s.Onu.PortNo, s.DHCPState, s.HwAddress, stream); err != nil {
+		s.Onu.Sn(), s.Onu.PortNo, s.DHCPState, s.HwAddress, s.UsPonCTagPriority, stream); err != nil {
 		serviceLogger.WithFields(log.Fields{
 			"OnuId":     s.Onu.ID,
 			"IntfId":    s.Onu.PonPortID,
diff --git a/internal/bbsim/packetHandlers/packet_tags.go b/internal/bbsim/packetHandlers/packet_tags.go
index e028adb..1ea7be7 100644
--- a/internal/bbsim/packetHandlers/packet_tags.go
+++ b/internal/bbsim/packetHandlers/packet_tags.go
@@ -23,7 +23,7 @@
 	"github.com/google/gopacket/layers"
 )
 
-func PushSingleTag(tag int, pkt gopacket.Packet) (gopacket.Packet, error) {
+func PushSingleTag(tag int, pkt gopacket.Packet, pbit uint8) (gopacket.Packet, error) {
 	// TODO can this method be semplified?
 	if eth := getEthernetLayer(pkt); eth != nil {
 		ethernetLayer := &layers.Ethernet{
@@ -35,6 +35,7 @@
 		dot1qLayer := &layers.Dot1Q{
 			Type:           eth.EthernetType,
 			VLANIdentifier: uint16(tag),
+			Priority:       pbit,
 		}
 
 		buffer := gopacket.NewSerializeBuffer()
@@ -58,13 +59,13 @@
 	return nil, errors.New("Couldn't extract LayerTypeEthernet from packet")
 }
 
-func PushDoubleTag(stag int, ctag int, pkt gopacket.Packet) (gopacket.Packet, error) {
+func PushDoubleTag(stag int, ctag int, pkt gopacket.Packet, pbit uint8) (gopacket.Packet, error) {
 
-	singleTaggedPkt, err := PushSingleTag(ctag, pkt)
+	singleTaggedPkt, err := PushSingleTag(ctag, pkt, pbit)
 	if err != nil {
 		return nil, err
 	}
-	doubleTaggedPkt, err := PushSingleTag(stag, singleTaggedPkt)
+	doubleTaggedPkt, err := PushSingleTag(stag, singleTaggedPkt, pbit)
 	if err != nil {
 		return nil, err
 	}
@@ -135,3 +136,11 @@
 	}
 	return dot1q.VLANIdentifier, nil
 }
+
+func GetPbit(pkt gopacket.Packet) (uint8, error) {
+	dot1q, err := getDot1QLayer(pkt)
+	if err != nil {
+		return 0, err
+	}
+	return dot1q.Priority, nil
+}
diff --git a/internal/bbsim/packetHandlers/packet_tags_test.go b/internal/bbsim/packetHandlers/packet_tags_test.go
index b2696a6..231cb71 100644
--- a/internal/bbsim/packetHandlers/packet_tags_test.go
+++ b/internal/bbsim/packetHandlers/packet_tags_test.go
@@ -48,14 +48,18 @@
 	)
 
 	untaggedPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeEthernet, gopacket.Default)
-	taggedPkt, err := packetHandlers.PushSingleTag(111, untaggedPkt)
+
+	taggedPkt, err := packetHandlers.PushSingleTag(111, untaggedPkt, 7)
 	if err != nil {
 		t.Fail()
 		t.Logf("Error in PushSingleTag: %v", err)
 	}
 
 	vlan, _ := packetHandlers.GetVlanTag(taggedPkt)
+	pbit, _ := packetHandlers.GetPbit(taggedPkt)
+
 	assert.Equal(t, vlan, uint16(111))
+	assert.Equal(t, pbit, uint8(7))
 }
 
 func TestPushDoubleTag(t *testing.T) {
@@ -80,7 +84,7 @@
 	)
 
 	untaggedPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeEthernet, gopacket.Default)
-	taggedPkt, err := packetHandlers.PushDoubleTag(900, 800, untaggedPkt)
+	taggedPkt, err := packetHandlers.PushDoubleTag(900, 800, untaggedPkt, 0)
 	if err != nil {
 		t.Fail()
 		t.Logf("Error in PushSingleTag: %v", err)
diff --git a/internal/bbsim/responders/dhcp/dhcp.go b/internal/bbsim/responders/dhcp/dhcp.go
index a0f5fff..98a254f 100644
--- a/internal/bbsim/responders/dhcp/dhcp.go
+++ b/internal/bbsim/responders/dhcp/dhcp.go
@@ -18,6 +18,7 @@
 
 import (
 	"context"
+	"encoding/hex"
 	"errors"
 	"fmt"
 	"net"
@@ -149,7 +150,7 @@
 	return &dhcpLayer
 }
 
-func serializeDHCPPacket(intfId uint32, onuId uint32, cTag int, srcMac net.HardwareAddr, dhcp *layers.DHCPv4) ([]byte, error) {
+func serializeDHCPPacket(intfId uint32, onuId uint32, cTag int, srcMac net.HardwareAddr, dhcp *layers.DHCPv4, pbit uint8) ([]byte, error) {
 	buffer := gopacket.NewSerializeBuffer()
 
 	options := gopacket.SerializeOptions{
@@ -185,7 +186,7 @@
 
 	untaggedPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeEthernet, gopacket.Default)
 
-	taggedPkt, err := packetHandlers.PushSingleTag(cTag, untaggedPkt)
+	taggedPkt, err := packetHandlers.PushSingleTag(cTag, untaggedPkt, pbit)
 	if err != nil {
 		dhcpLogger.Error("TagPacket")
 		return nil, err
@@ -241,12 +242,13 @@
 
 func sendDHCPPktIn(msg bbsim.ByteMsg, portNo uint32, gemPortId uint32, stream bbsim.Stream) error {
 
-	log.WithFields(log.Fields{
+	dhcpLogger.WithFields(log.Fields{
 		"OnuId":   msg.OnuId,
 		"IntfId":  msg.IntfId,
 		"GemPort": gemPortId,
 		"Type":    "DHCP",
-	}).Trace("sending-pkt")
+		"Pkt":     hex.EncodeToString(msg.Bytes),
+	}).Trace("Sending DHCP packet in")
 
 	data := &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{
 		IntfType:  "pon",
@@ -263,9 +265,11 @@
 	return nil
 }
 
-func sendDHCPRequest(oltId int, ponPortId uint32, onuId uint32, serialNumber string, portNo uint32, cTag int, gemPortId uint32, onuStateMachine *fsm.FSM, onuHwAddress net.HardwareAddr, offeredIp net.IP, stream bbsim.Stream) error {
+func sendDHCPRequest(oltId int, ponPortId uint32, onuId uint32, serialNumber string, portNo uint32,
+	cTag int, gemPortId uint32, onuStateMachine *fsm.FSM, onuHwAddress net.HardwareAddr,
+	offeredIp net.IP, pbit uint8, stream bbsim.Stream) error {
 	dhcp := createDHCPReq(oltId, ponPortId, onuId, onuHwAddress, offeredIp)
-	pkt, err := serializeDHCPPacket(ponPortId, onuId, cTag, onuHwAddress, dhcp)
+	pkt, err := serializeDHCPPacket(ponPortId, onuId, cTag, onuHwAddress, dhcp, pbit)
 
 	if err != nil {
 		dhcpLogger.WithFields(log.Fields{
@@ -321,9 +325,12 @@
 	return nil
 }
 
-func SendDHCPDiscovery(oltId int, ponPortId uint32, onuId uint32, cTag int, gemPortId uint32, serialNumber string, portNo uint32, stateMachine *fsm.FSM, onuHwAddress net.HardwareAddr, stream bbsim.Stream) error {
+func SendDHCPDiscovery(oltId int, ponPortId uint32, onuId uint32, cTag int, gemPortId uint32,
+	serialNumber string, portNo uint32, stateMachine *fsm.FSM, onuHwAddress net.HardwareAddr,
+	pbit uint8, stream bbsim.Stream) error {
+
 	dhcp := createDHCPDisc(oltId, ponPortId, onuId, gemPortId, onuHwAddress)
-	pkt, err := serializeDHCPPacket(ponPortId, onuId, cTag, onuHwAddress, dhcp)
+	pkt, err := serializeDHCPPacket(ponPortId, onuId, cTag, onuHwAddress, dhcp, pbit)
 
 	if err != nil {
 		dhcpLogger.WithFields(log.Fields{
@@ -372,8 +379,9 @@
 	return nil
 }
 
-// FIXME cTag is not used here
-func HandleNextPacket(oltId int, onuId uint32, ponPortId uint32, serialNumber string, portNo uint32, cTag int, gemPortId uint32, onuHwAddress net.HardwareAddr, onuStateMachine *fsm.FSM, pkt gopacket.Packet, stream bbsim.Stream) error {
+func HandleNextPacket(oltId int, onuId uint32, ponPortId uint32, serialNumber string, portNo uint32,
+	cTag int, gemPortId uint32, onuHwAddress net.HardwareAddr, onuStateMachine *fsm.FSM,
+	pkt gopacket.Packet, pbit uint8, stream bbsim.Stream) error {
 
 	dhcpLayer, err := GetDhcpLayer(pkt)
 	if err != nil {
@@ -403,7 +411,7 @@
 	if dhcpLayer.Operation == layers.DHCPOpReply {
 		if dhcpMessageType == layers.DHCPMsgTypeOffer {
 			offeredIp := dhcpLayer.YourClientIP
-			if err := sendDHCPRequest(oltId, ponPortId, onuId, serialNumber, portNo, cTag, gemPortId, onuStateMachine, onuHwAddress, offeredIp, stream); err != nil {
+			if err := sendDHCPRequest(oltId, ponPortId, onuId, serialNumber, portNo, cTag, gemPortId, onuStateMachine, onuHwAddress, offeredIp, pbit, stream); err != nil {
 				dhcpLogger.WithFields(log.Fields{
 					"OnuId":  onuId,
 					"IntfId": ponPortId,
@@ -521,7 +529,8 @@
 		// double tag the packet and send it to the NNI
 		// we don't really care about the tags as they are stripped before
 		// the packet is sent to the DHCP server
-		doubleTaggedPkt, err := packetHandlers.PushDoubleTag(900, 900, pkt)
+		// in BBR we don't care about the pBit
+		doubleTaggedPkt, err := packetHandlers.PushDoubleTag(900, 900, pkt, 0)
 		if err != nil {
 			dhcpLogger.WithFields(log.Fields{
 				"OnuId":  onuId,
diff --git a/internal/bbsim/responders/dhcp/dhcp_test.go b/internal/bbsim/responders/dhcp/dhcp_test.go
index cc50188..aea2048 100644
--- a/internal/bbsim/responders/dhcp/dhcp_test.go
+++ b/internal/bbsim/responders/dhcp/dhcp_test.go
@@ -74,7 +74,7 @@
 		fail:  false,
 	}
 
-	if err := SendDHCPDiscovery(oltId, ponPortId, onuId, 900, gemPortId, serialNumber, portNo, dhcpStateMachine, mac, stream); err != nil {
+	if err := SendDHCPDiscovery(oltId, ponPortId, onuId, 900, gemPortId, serialNumber, portNo, dhcpStateMachine, mac, 7, stream); err != nil {
 		t.Errorf("SendDHCPDiscovery returned an error: %v", err)
 		t.Fail()
 	}
diff --git a/internal/bbsim/responders/sadis/sadis.go b/internal/bbsim/responders/sadis/sadis.go
index 444fd57..34bf600 100644
--- a/internal/bbsim/responders/sadis/sadis.go
+++ b/internal/bbsim/responders/sadis/sadis.go
@@ -94,10 +94,10 @@
 	IsDhcpRequired             bool   `json:"isDhcpRequired,omitempty"`
 	IsIgmpRequired             bool   `json:"isIgmpRequired,omitempty"`
 	ConfiguredMacAddress       string `json:"configuredMacAddress,omitempty"`
-	UsPonCTagPriority          int    `json:"usPonCTagPriority,omitempty"`
-	UsPonSTagPriority          int    `json:"usPonSTagPriority,omitempty"`
-	DsPonCTagPriority          int    `json:"dsPonCTagPriority,omitempty"`
-	DsPonSTagPriority          int    `json:"dsPonSTagPriority,omitempty"`
+	UsPonCTagPriority          uint8  `json:"usPonCTagPriority,omitempty"`
+	UsPonSTagPriority          uint8  `json:"usPonSTagPriority,omitempty"`
+	DsPonCTagPriority          uint8  `json:"dsPonCTagPriority,omitempty"`
+	DsPonSTagPriority          uint8  `json:"dsPonSTagPriority,omitempty"`
 	ServiceName                string `json:"serviceName,omitempty"`
 }
 
@@ -245,7 +245,6 @@
 	sadisConf := GetSadisConfig(s.olt, vars["version"])
 
 	sadisJSON, _ := json.Marshal(sadisConf)
-	sadisLogger.Tracef("SADIS JSON: %s", sadisJSON)
 
 	_, _ = w.Write([]byte(sadisJSON))
 
diff --git a/internal/common/options.go b/internal/common/options.go
index 567837b..8505de2 100644
--- a/internal/common/options.go
+++ b/internal/common/options.go
@@ -129,10 +129,10 @@
 	TechnologyProfileID int    `yaml:"tp_id"`
 	UniTagMatch         int    `yaml:"uni_tag_match"`
 	ConfigureMacAddress bool   `yaml:"configure_mac_address"`
-	UsPonCTagPriority   int    `yaml:"us_pon_c_tag_priority"`
-	UsPonSTagPriority   int    `yaml:"us_pon_s_tag_priority"`
-	DsPonCTagPriority   int    `yaml:"ds_pon_c_tag_priority"`
-	DsPonSTagPriority   int    `yaml:"ds_pon_s_tag_priority"`
+	UsPonCTagPriority   uint8  `yaml:"us_pon_c_tag_priority"`
+	UsPonSTagPriority   uint8  `yaml:"us_pon_s_tag_priority"`
+	DsPonCTagPriority   uint8  `yaml:"ds_pon_c_tag_priority"`
+	DsPonSTagPriority   uint8  `yaml:"ds_pon_s_tag_priority"`
 }
 type YamlServiceConfig struct {
 	Workflow string