1 # Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
19 from . import packet_base
28 from . import ether_types as ether
31 @six.add_metaclass(abc.ABCMeta)
32 class _vlan(packet_base.PacketBase):
34 _MIN_LEN = struct.calcsize(_PACK_STR)
37 def __init__(self, pcp, cfi, vid, ethertype):
38 super(_vlan, self).__init__()
42 self.ethertype = ethertype
46 tci, ethertype = struct.unpack_from(cls._PACK_STR, buf)
49 vid = tci & ((1 << 12) - 1)
50 return (cls(pcp, cfi, vid, ethertype),
51 vlan.get_packet_type(ethertype), buf[vlan._MIN_LEN:])
53 def serialize(self, payload, prev):
54 tci = self.pcp << 13 | self.cfi << 12 | self.vid
55 return struct.pack(vlan._PACK_STR, tci, self.ethertype)
59 """VLAN (IEEE 802.1Q) header encoder/decoder class.
61 An instance has the following attributes at least.
62 Most of them are same to the on-wire counterparts but in host byte order.
63 __init__ takes the corresponding args in this order.
65 ============== ====================
67 ============== ====================
68 pcp Priority Code Point
69 cfi Canonical Format Indicator
72 ============== ====================
75 def __init__(self, pcp=0, cfi=0, vid=0, ethertype=ether.ETH_TYPE_IP):
76 super(vlan, self).__init__(pcp, cfi, vid, ethertype)
79 def get_packet_type(cls, type_):
80 """Override method for the Length/Type field (self.ethertype).
81 The Length/Type field means Length or Type interpretation,
82 same as ethernet IEEE802.3.
83 If the value of Length/Type field is less than or equal to
84 1500 decimal(05DC hexadecimal), it means Length interpretation
85 and be passed to the LLC sublayer."""
86 if type_ <= ether.ETH_TYPE_IEEE802_3:
87 type_ = ether.ETH_TYPE_IEEE802_3
88 return cls._TYPES.get(type_)
92 """S-VLAN (IEEE 802.1ad) header encoder/decoder class.
95 An instance has the following attributes at least.
96 Most of them are same to the on-wire counterparts but in host byte order.
97 __init__ takes the corresponding args in this order.
99 .. tabularcolumns:: |l|L|
101 ============== ====================
102 Attribute Description
103 ============== ====================
104 pcp Priority Code Point
105 cfi Canonical Format Indicator.
106 In a case to be used as B-TAG,
107 this field means DEI(Drop Eligible Indication).
110 ============== ====================
113 def __init__(self, pcp=0, cfi=0, vid=0, ethertype=ether.ETH_TYPE_8021Q):
114 super(svlan, self).__init__(pcp, cfi, vid, ethertype)
117 def get_packet_type(cls, type_):
118 return cls._TYPES.get(type_)
121 vlan.register_packet_type(arp.arp, ether.ETH_TYPE_ARP)
122 vlan.register_packet_type(ipv4.ipv4, ether.ETH_TYPE_IP)
123 vlan.register_packet_type(ipv6.ipv6, ether.ETH_TYPE_IPV6)
124 vlan.register_packet_type(lldp.lldp, ether.ETH_TYPE_LLDP)
125 vlan.register_packet_type(slow.slow, ether.ETH_TYPE_SLOW)
126 vlan.register_packet_type(llc.llc, ether.ETH_TYPE_IEEE802_3)
127 vlan.register_packet_type(cfm.cfm, ether.ETH_TYPE_CFM)
129 svlan.register_packet_type(vlan, ether.ETH_TYPE_8021Q)
130 svlan.register_packet_type(pbb.itag, ether.ETH_TYPE_8021AH)