backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / lib / packet / vlan.py
1 # Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
2 #
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
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
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
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import abc
17 import six
18 import struct
19 from . import packet_base
20 from . import arp
21 from . import ipv4
22 from . import ipv6
23 from . import lldp
24 from . import slow
25 from . import llc
26 from . import pbb
27 from . import cfm
28 from . import ether_types as ether
29
30
31 @six.add_metaclass(abc.ABCMeta)
32 class _vlan(packet_base.PacketBase):
33     _PACK_STR = "!HH"
34     _MIN_LEN = struct.calcsize(_PACK_STR)
35
36     @abc.abstractmethod
37     def __init__(self, pcp, cfi, vid, ethertype):
38         super(_vlan, self).__init__()
39         self.pcp = pcp
40         self.cfi = cfi
41         self.vid = vid
42         self.ethertype = ethertype
43
44     @classmethod
45     def parser(cls, buf):
46         tci, ethertype = struct.unpack_from(cls._PACK_STR, buf)
47         pcp = tci >> 13
48         cfi = (tci >> 12) & 1
49         vid = tci & ((1 << 12) - 1)
50         return (cls(pcp, cfi, vid, ethertype),
51                 vlan.get_packet_type(ethertype), buf[vlan._MIN_LEN:])
52
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)
56
57
58 class vlan(_vlan):
59     """VLAN (IEEE 802.1Q) header encoder/decoder class.
60
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.
64
65     ============== ====================
66     Attribute      Description
67     ============== ====================
68     pcp            Priority Code Point
69     cfi            Canonical Format Indicator
70     vid            VLAN Identifier
71     ethertype      EtherType
72     ============== ====================
73     """
74
75     def __init__(self, pcp=0, cfi=0, vid=0, ethertype=ether.ETH_TYPE_IP):
76         super(vlan, self).__init__(pcp, cfi, vid, ethertype)
77
78     @classmethod
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_)
89
90
91 class svlan(_vlan):
92     """S-VLAN (IEEE 802.1ad) header encoder/decoder class.
93
94
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.
98
99     .. tabularcolumns:: |l|L|
100
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).
108     vid            VLAN Identifier
109     ethertype      EtherType
110     ============== ====================
111     """
112
113     def __init__(self, pcp=0, cfi=0, vid=0, ethertype=ether.ETH_TYPE_8021Q):
114         super(svlan, self).__init__(pcp, cfi, vid, ethertype)
115
116     @classmethod
117     def get_packet_type(cls, type_):
118         return cls._TYPES.get(type_)
119
120
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)
128
129 svlan.register_packet_type(vlan, ether.ETH_TYPE_8021Q)
130 svlan.register_packet_type(pbb.itag, ether.ETH_TYPE_8021AH)