ethernet: refactor VLAN API

This commit is contained in:
Patricio Whittingslow
2026-02-18 16:58:52 -03:00
parent bf2d07d9f0
commit 1a07928ca8
3 changed files with 7 additions and 26 deletions
-16
View File
@@ -76,9 +76,6 @@ func (efrm Frame) SetEtherType(v Type) {
binary.BigEndian.PutUint16(efrm.buf[12:14], uint16(v)) 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: // SetVLAN sets following 3 fields:
// - 12:14 ethernet frame type set to constant [TypeVLAN]. // - 12:14 ethernet frame type set to constant [TypeVLAN].
// - 14:16 set to VLANTag argument value vt // - 14:16 set to VLANTag argument value vt
@@ -97,19 +94,6 @@ func (efrm Frame) VLAN() (VLANTag, Type) {
return VLANTag(vt), Type(et) 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 // 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 // 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 // contains the first two octets of a 4 octet 802.1Q VLAN tag. In this case 4 more bytes
+5 -6
View File
@@ -45,7 +45,7 @@ func (gen *PacketGen) AppendRandomIPv4TCPPacket(dst []byte, rng *rand.Rand, seg
} }
ri := rng.Int() ri := rng.Int()
var ( var (
isVLAN = ri&(1<<0) != 0 isVLAN = gen.EnableVLAN && ri&(1<<0) != 0
hasIPOpt = ri&(1<<1) != 0 hasIPOpt = ri&(1<<1) != 0
hasTCPOpt = ri&(1<<2) != 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} ipOpts = []byte{1, 2, 3, 4}
} }
ethsize := 14 ethsize := 14
if gen.EnableVLAN && isVLAN { if isVLAN {
etherType = ethernet.TypeVLAN
ethsize = 18 ethsize = 18
} }
var tcpOpts []byte var tcpOpts []byte
@@ -75,10 +74,10 @@ func (gen *PacketGen) AppendRandomIPv4TCPPacket(dst []byte, rng *rand.Rand, seg
*efrm.DestinationHardwareAddr() = gen.DstMAC *efrm.DestinationHardwareAddr() = gen.DstMAC
*efrm.SourceHardwareAddr() = gen.SrcMAC *efrm.SourceHardwareAddr() = gen.SrcMAC
efrm.SetEtherType(etherType)
if isVLAN { if isVLAN {
efrm.SetVLANEtherType(ethernet.TypeIPv4) efrm.SetVLAN(1<<4, ethernet.TypeIPv4)
efrm.SetVLANTag(1 << 4) } else {
efrm.SetEtherType(etherType)
} }
ethernetPayload := efrm.Payload() ethernetPayload := efrm.Payload()
ifrm, err := ipv4.NewFrame(ethernetPayload) ifrm, err := ipv4.NewFrame(ethernetPayload)
+2 -4
View File
@@ -58,11 +58,9 @@ func testMoveTCPPacket(t *testing.T, src, dst []byte) {
*efrm2.DestinationHardwareAddr() = *efrm.DestinationHardwareAddr() *efrm2.DestinationHardwareAddr() = *efrm.DestinationHardwareAddr()
*efrm2.SourceHardwareAddr() = *efrm.SourceHardwareAddr() *efrm2.SourceHardwareAddr() = *efrm.SourceHardwareAddr()
efrm2.SetEtherType(efrm.EtherTypeOrSize()) efrm2.SetEtherType(efrm.EtherTypeOrSize())
if efrm.EtherTypeOrSize() == ethernet.TypeVLAN { if efrm.IsVLAN() {
efrm2.SetVLANTag(efrm.VLANTag()) efrm2.SetVLAN(efrm.VLAN())
efrm2.SetVLANEtherType(efrm.VLANEtherType())
} }
ifrm2, _ := ipv4.NewFrame(efrm2.Payload()) ifrm2, _ := ipv4.NewFrame(efrm2.Payload())
ifrm2.SetVersionAndIHL(ifrm.VersionAndIHL()) ifrm2.SetVersionAndIHL(ifrm.VersionAndIHL())
ifrm2.SetToS(ifrm.ToS()) ifrm2.SetToS(ifrm.ToS())