backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / lib / packet / llc.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
17 """
18 Logical Link Control(LLC, IEEE 802.2) parser/serializer
19 http://standards.ieee.org/getieee802/download/802.2-1998.pdf
20
21
22 LLC format::
23
24     +-----------------+--------------+
25     | DSAP address    | 8 bits       |
26     +-----------------+--------------+
27     | SSAP address    | 8 bits       |
28     +-----------------+--------------+
29     | Control         | 8 or 16 bits |
30     +-----------------+--------------+
31
32
33 DSAP address field::
34
35       LSB
36     +-----+---+---+---+---+---+---+---+
37     | I/G | D | D | D | D | D | D | D |
38     +-----+---+---+---+---+---+---+---+
39      I/G bit = 0 : Individual DSAP
40      I/G bit = 1 : Group DSA
41      D : DSAP address
42
43 SSAP address field::
44
45       LSB
46     +-----+---+---+---+---+---+---+---+
47     | C/R | S | S | S | S | S | S | S |
48     +-----+---+---+---+---+---+---+---+
49      C/R bit = 0 : Command
50      C/R bit = 1 : Response
51      S : SSAP address
52
53
54 Control field:
55
56 Information transfer
57 command/response
58 (I-format PDU)::
59
60       1   2   3   4   5   6   7   8    9   10-16
61     +---+---+---+---+---+---+---+---+-----+------+
62     | 0 |           N(S)            | P/F | N(R) |
63     +---+---+---+---+---+---+---+---+-----+------+
64
65 Supervisory
66 commands/responses
67 (S-format PDUs)::
68
69       1   2   3   4   5   6   7   8    9   10-16
70     +---+---+---+---+---+---+---+---+-----+------+
71     | 1   0 | S   S | 0   0   0   0 | P/F | N(R) |
72     +---+---+---+---+---+---+---+---+-----+------+
73
74 Unnumbered
75 commands/responses
76 (U-format PDUs)::
77
78       1   2   3    4    5    6   7    8
79     +---+---+----+---+-----+---+----+---+
80     | 1   1 | M1  M1 | P/F | M2  M2  M2 |
81     +---+---+----+---+-----+---+----+---+
82
83     N(S) : sender send sequence number (Bit 2=lower-order-bit)
84     N(R) : sender receive sequence number (Bit 10=lower-order-bit)
85     S    : supervisory function bit
86     M1/M2: modifier function bit
87     P/F  : poll bit - command LLC PDUs
88            final bit - response LLC PDUs
89 """
90
91
92 import struct
93 from . import bpdu
94 from . import packet_base
95 from ryu.lib import stringify
96
97
98 SAP_BPDU = 0x42
99
100
101 class llc(packet_base.PacketBase):
102     """LLC(IEEE 802.2) header encoder/decoder class.
103
104     An instance has the following attributes at least.
105     Most of them are same to the on-wire counterparts but in host byte
106     order.
107     __init__ takes the corresponding args in this order.
108
109     .. tabularcolumns:: |l|L|
110
111     =============== ===============================================
112     Attribute       Description
113     =============== ===============================================
114     dsap_addr       Destination service access point address field \
115                     includes I/G bit at least significant bit.
116     ssap_addr       Source service access point address field \
117                     includes C/R bit at least significant bit.
118     control         Control field \
119                     [16 bits for formats that include sequence \
120                     numbering, and 8 bits for formats that do not]. \
121                     Either ryu.lib.packet.llc.ControlFormatI or \
122                     ryu.lib.packet.llc.ControlFormatS or \
123                     ryu.lib.packet.llc.ControlFormatU object.
124     =============== ===============================================
125     """
126
127     _PACK_STR = '!BB'
128     _PACK_LEN = struct.calcsize(_PACK_STR)
129     _CTR_TYPES = {}
130     _CTR_PACK_STR = '!2xB'
131
132     _MIN_LEN = _PACK_LEN
133
134     @staticmethod
135     def register_control_type(register_cls):
136         llc._CTR_TYPES[register_cls.TYPE] = register_cls
137         return register_cls
138
139     def __init__(self, dsap_addr, ssap_addr, control):
140         super(llc, self).__init__()
141
142         assert getattr(control, 'TYPE', None) in self._CTR_TYPES
143
144         self.dsap_addr = dsap_addr
145         self.ssap_addr = ssap_addr
146         self.control = control
147
148     @classmethod
149     def parser(cls, buf):
150         assert len(buf) >= cls._PACK_LEN
151         (dsap_addr, ssap_addr) = struct.unpack_from(cls._PACK_STR, buf)
152
153         (control,) = struct.unpack_from(cls._CTR_PACK_STR, buf)
154         ctrl = cls._get_control(control)
155         control, information = ctrl.parser(buf[cls._PACK_LEN:])
156
157         return (cls(dsap_addr, ssap_addr, control),
158                 cls.get_packet_type(dsap_addr), information)
159
160     def serialize(self, payload, prev):
161         addr = struct.pack(self._PACK_STR, self.dsap_addr, self.ssap_addr)
162         control = self.control.serialize()
163         return addr + control
164
165     @classmethod
166     def _get_control(cls, buf):
167         key = buf & 0b1 if buf & 0b1 == ControlFormatI.TYPE else buf & 0b11
168         return cls._CTR_TYPES[key]
169
170
171 @llc.register_control_type
172 class ControlFormatI(stringify.StringifyMixin):
173     """LLC sub encoder/decoder class for control I-format field.
174
175     An instance has the following attributes at least.
176     Most of them are same to the on-wire counterparts but in host byte
177     order.
178     __init__ takes the corresponding args in this order.
179
180     ======================== ===============================
181     Attribute                Description
182     ======================== ===============================
183     send_sequence_number     sender send sequence number
184     pf_bit                   poll/final bit
185     receive_sequence_number  sender receive sequence number
186     ======================== ===============================
187     """
188     TYPE = 0b0
189     _PACK_STR = '!H'
190     _PACK_LEN = struct.calcsize(_PACK_STR)
191
192     def __init__(self, send_sequence_number=0, pf_bit=0,
193                  receive_sequence_number=0):
194         super(ControlFormatI, self).__init__()
195         self.send_sequence_number = send_sequence_number
196         self.pf_bit = pf_bit
197         self.receive_sequence_number = receive_sequence_number
198
199     @classmethod
200     def parser(cls, buf):
201         assert len(buf) >= cls._PACK_LEN
202         (control,) = struct.unpack_from(cls._PACK_STR, buf)
203         assert (control >> 8) & 0b1 == cls.TYPE
204
205         send_sequence_number = (control >> 9) & 0b1111111
206         pf_bit = (control >> 8) & 0b1
207         receive_sequence_number = (control >> 1) & 0b1111111
208
209         return cls(send_sequence_number, pf_bit,
210                    receive_sequence_number), buf[cls._PACK_LEN:]
211
212     def serialize(self):
213         control = (self.send_sequence_number << 9 |
214                    self.TYPE << 8 |
215                    self.receive_sequence_number << 1 |
216                    self.pf_bit)
217         return struct.pack(self._PACK_STR, control)
218
219
220 @llc.register_control_type
221 class ControlFormatS(stringify.StringifyMixin):
222     """LLC sub encoder/decoder class for control S-format field.
223
224     An instance has the following attributes at least.
225     Most of them are same to the on-wire counterparts but in host byte
226     order.
227     __init__ takes the corresponding args in this order.
228
229     ======================== ===============================
230     Attribute                Description
231     ======================== ===============================
232     supervisory_function     supervisory function bit
233     pf_bit                   poll/final bit
234     receive_sequence_number  sender receive sequence number
235     ======================== ===============================
236     """
237
238     TYPE = 0b01
239     _PACK_STR = '!H'
240     _PACK_LEN = struct.calcsize(_PACK_STR)
241
242     def __init__(self, supervisory_function=0, pf_bit=0,
243                  receive_sequence_number=0):
244         super(ControlFormatS, self).__init__()
245         self.supervisory_function = supervisory_function
246         self.pf_bit = pf_bit
247         self.receive_sequence_number = receive_sequence_number
248
249     @classmethod
250     def parser(cls, buf):
251         assert len(buf) >= cls._PACK_LEN
252         (control,) = struct.unpack_from(cls._PACK_STR, buf)
253
254         assert (control >> 8) & 0b11 == cls.TYPE
255
256         supervisory_function = (control >> 10) & 0b11
257         pf_bit = (control >> 8) & 0b1
258         receive_sequence_number = (control >> 1) & 0b1111111
259
260         return cls(supervisory_function, pf_bit,
261                    receive_sequence_number), buf[cls._PACK_LEN:]
262
263     def serialize(self):
264         control = (self.supervisory_function << 10 |
265                    self.TYPE << 8 |
266                    self.receive_sequence_number << 1 |
267                    self.pf_bit)
268         return struct.pack(self._PACK_STR, control)
269
270
271 @llc.register_control_type
272 class ControlFormatU(stringify.StringifyMixin):
273     """LLC sub encoder/decoder class for control U-format field.
274
275     An instance has the following attributes at least.
276     Most of them are same to the on-wire counterparts but in host byte
277     order.
278     __init__ takes the corresponding args in this order.
279
280     ======================== ===============================
281     Attribute                Description
282     ======================== ===============================
283     modifier_function1       modifier function bit
284     pf_bit                   poll/final bit
285     modifier_function2       modifier function bit
286     ======================== ===============================
287     """
288
289     TYPE = 0b11
290     _PACK_STR = '!B'
291     _PACK_LEN = struct.calcsize(_PACK_STR)
292
293     def __init__(self, modifier_function1=0, pf_bit=0, modifier_function2=0):
294         super(ControlFormatU, self).__init__()
295         self.modifier_function1 = modifier_function1
296         self.pf_bit = pf_bit
297         self.modifier_function2 = modifier_function2
298
299     @classmethod
300     def parser(cls, buf):
301         assert len(buf) >= cls._PACK_LEN
302         (control,) = struct.unpack_from(cls._PACK_STR, buf)
303
304         assert control & 0b11 == cls.TYPE
305
306         modifier_function1 = (control >> 2) & 0b11
307         pf_bit = (control >> 4) & 0b1
308         modifier_function2 = (control >> 5) & 0b111
309
310         return cls(modifier_function1, pf_bit,
311                    modifier_function2), buf[cls._PACK_LEN:]
312
313     def serialize(self):
314         control = (self.modifier_function2 << 5 |
315                    self.pf_bit << 4 |
316                    self.modifier_function1 << 2 |
317                    self.TYPE)
318         return struct.pack(self._PACK_STR, control)
319
320
321 llc.register_packet_type(bpdu.bpdu, SAP_BPDU)
322 llc.set_classes(llc._CTR_TYPES)