backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / lib / packet / pbb.py
1 # Copyright (C) 2013 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 struct
17 from ryu.lib.packet import packet_base
18
19
20 class itag(packet_base.PacketBase):
21     """I-TAG (IEEE 802.1ah-2008) header encoder/decoder class.
22
23     An instance has the following attributes at least.
24     Most of them are same to the on-wire counterparts but in host byte order.
25     __init__ takes the corresponding args in this order.
26
27     ============== ====================
28     Attribute      Description
29     ============== ====================
30     pcp            Priority Code Point
31     dei            Drop Eligible Indication
32     uca            Use Customer Address
33     sid            Service Instance ID
34     ============== ====================
35     """
36
37     _PACK_STR = "!I"
38     _MIN_LEN = struct.calcsize(_PACK_STR)
39
40     def __init__(self, pcp=0, dei=0, uca=0, sid=0):
41         super(itag, self).__init__()
42         self.pcp = pcp
43         self.dei = dei
44         self.uca = uca
45         self.sid = sid
46
47     @classmethod
48     def parser(cls, buf):
49         (data, ) = struct.unpack_from(cls._PACK_STR, buf)
50         pcp = data >> 29
51         dei = data >> 28 & 1
52         uca = data >> 27 & 1
53         sid = data & 0x00ffffff
54         # circular import: ethernet -> vlan -> pbb
55         from ryu.lib.packet import ethernet
56         return (cls(pcp, dei, uca, sid), ethernet.ethernet,
57                 buf[cls._MIN_LEN:])
58
59     def serialize(self, payload, prev):
60         data = self.pcp << 29 | self.dei << 28 | self.uca << 27 | self.sid
61         return struct.pack(self._PACK_STR, data)