diff --git a/ethernet/frame.go b/ethernet/frame.go index 392855e..c4ef385 100644 --- a/ethernet/frame.go +++ b/ethernet/frame.go @@ -76,9 +76,6 @@ func (efrm Frame) SetEtherType(v Type) { binary.BigEndian.PutUint16(efrm.buf[12:14], uint16(v)) } -// VLANTag returns the VLAN tag field following the TPID=0x8100. See [VLANTag]. Call [Frame.ValidateSize] to ensure this function does not panic. -func (efrm Frame) VLANTag() VLANTag { return VLANTag(binary.BigEndian.Uint16(efrm.buf[14:16])) } - // SetVLAN sets following 3 fields: // - 12:14 ethernet frame type set to constant [TypeVLAN]. // - 14:16 set to VLANTag argument value vt @@ -97,19 +94,6 @@ func (efrm Frame) VLAN() (VLANTag, Type) { return VLANTag(vt), Type(et) } -// SetVLANTag sets the VLAN tag field of the Ethernet Header. See [VLANTag]. Call [Frame.ValidateSize] to ensure this function does not panic. -func (efrm Frame) SetVLANTag(vt VLANTag) { binary.BigEndian.PutUint16(efrm.buf[14:16], uint16(vt)) } - -// VLANEtherType returns the [EtherType] for a VLAN ethernet frame (octet position 16). Call [Frame.ValidateSize] to ensure this function does not panic. -func (efrm Frame) VLANEtherType() Type { - return Type(binary.BigEndian.Uint16(efrm.buf[16:18])) -} - -// SetVLANEtherType sets the [EtherType] for a VLAN ethernet frame (octet position 16). Call [Frame.ValidateSize] to ensure this function does not panic. -func (efrm Frame) SetVLANEtherType(vt Type) { - binary.BigEndian.PutUint16(efrm.buf[16:18], uint16(vt)) -} - // IsVLAN returns true if the SizeOrEtherType is set to the VLAN tag 0x8100. This // indicates the EthernetHeader is invalid as-is and instead of EtherType the field // contains the first two octets of a 4 octet 802.1Q VLAN tag. In this case 4 more bytes diff --git a/internal/ltesto/ltesto.go b/internal/ltesto/ltesto.go index 2d6f804..85b5bc2 100644 --- a/internal/ltesto/ltesto.go +++ b/internal/ltesto/ltesto.go @@ -45,7 +45,7 @@ func (gen *PacketGen) AppendRandomIPv4TCPPacket(dst []byte, rng *rand.Rand, seg } ri := rng.Int() var ( - isVLAN = ri&(1<<0) != 0 + isVLAN = gen.EnableVLAN && ri&(1<<0) != 0 hasIPOpt = ri&(1<<1) != 0 hasTCPOpt = ri&(1<<2) != 0 ) @@ -55,8 +55,7 @@ func (gen *PacketGen) AppendRandomIPv4TCPPacket(dst []byte, rng *rand.Rand, seg ipOpts = []byte{1, 2, 3, 4} } ethsize := 14 - if gen.EnableVLAN && isVLAN { - etherType = ethernet.TypeVLAN + if isVLAN { ethsize = 18 } var tcpOpts []byte @@ -75,10 +74,10 @@ func (gen *PacketGen) AppendRandomIPv4TCPPacket(dst []byte, rng *rand.Rand, seg *efrm.DestinationHardwareAddr() = gen.DstMAC *efrm.SourceHardwareAddr() = gen.SrcMAC - efrm.SetEtherType(etherType) if isVLAN { - efrm.SetVLANEtherType(ethernet.TypeIPv4) - efrm.SetVLANTag(1 << 4) + efrm.SetVLAN(1<<4, ethernet.TypeIPv4) + } else { + efrm.SetEtherType(etherType) } ethernetPayload := efrm.Payload() ifrm, err := ipv4.NewFrame(ethernetPayload) diff --git a/lneto_test.go b/lneto_test.go index 9ae11a2..c3e3c76 100644 --- a/lneto_test.go +++ b/lneto_test.go @@ -58,11 +58,9 @@ func testMoveTCPPacket(t *testing.T, src, dst []byte) { *efrm2.DestinationHardwareAddr() = *efrm.DestinationHardwareAddr() *efrm2.SourceHardwareAddr() = *efrm.SourceHardwareAddr() efrm2.SetEtherType(efrm.EtherTypeOrSize()) - if efrm.EtherTypeOrSize() == ethernet.TypeVLAN { - efrm2.SetVLANTag(efrm.VLANTag()) - efrm2.SetVLANEtherType(efrm.VLANEtherType()) + if efrm.IsVLAN() { + efrm2.SetVLAN(efrm.VLAN()) } - ifrm2, _ := ipv4.NewFrame(efrm2.Payload()) ifrm2.SetVersionAndIHL(ifrm.VersionAndIHL()) ifrm2.SetToS(ifrm.ToS())