Rich Lane | 15cbe84 | 2013-04-26 16:04:11 -0700 | [diff] [blame] | 1 | :: # Copyright 2013, Big Switch Networks, Inc. |
| 2 | :: # |
| 3 | :: # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with |
| 4 | :: # the following special exception: |
| 5 | :: # |
| 6 | :: # LOXI Exception |
| 7 | :: # |
| 8 | :: # As a special exception to the terms of the EPL, you may distribute libraries |
| 9 | :: # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided |
| 10 | :: # that copyright and licensing notices generated by LoxiGen are not altered or removed |
| 11 | :: # from the LoxiGen Libraries and the notice provided below is (i) included in |
| 12 | :: # the LoxiGen Libraries, if distributed in source code form and (ii) included in any |
| 13 | :: # documentation for the LoxiGen Libraries, if distributed in binary form. |
| 14 | :: # |
| 15 | :: # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler." |
| 16 | :: # |
| 17 | :: # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain |
| 18 | :: # a copy of the EPL at: |
| 19 | :: # |
| 20 | :: # http://www.eclipse.org/legal/epl-v10.html |
| 21 | :: # |
| 22 | :: # Unless required by applicable law or agreed to in writing, software |
| 23 | :: # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | :: # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 25 | :: # EPL for the specific language governing permissions and limitations |
| 26 | :: # under the EPL. |
| 27 | :: |
| 28 | :: include('_copyright.py') |
| 29 | """ |
| 30 | Utility functions independent of the protocol version |
| 31 | """ |
| 32 | |
| 33 | :: include('_autogen.py') |
| 34 | |
| 35 | import loxi |
| 36 | import struct |
| 37 | |
Rich Lane | f0bac29 | 2013-12-01 15:55:21 -0800 | [diff] [blame] | 38 | def pack_list(values): |
| 39 | return "".join([x.pack() for x in values]) |
| 40 | |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 41 | def unpack_list(reader, deserializer): |
Rich Lane | 15cbe84 | 2013-04-26 16:04:11 -0700 | [diff] [blame] | 42 | """ |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 43 | The deserializer function should take an OFReader and return the new object. |
Rich Lane | 15cbe84 | 2013-04-26 16:04:11 -0700 | [diff] [blame] | 44 | """ |
| 45 | entries = [] |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 46 | while not reader.is_empty(): |
| 47 | entries.append(deserializer(reader)) |
Rich Lane | 15cbe84 | 2013-04-26 16:04:11 -0700 | [diff] [blame] | 48 | return entries |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 49 | |
Rich Lane | 5a72bc3 | 2013-07-23 14:10:21 -0700 | [diff] [blame] | 50 | def pad_to(alignment, length): |
| 51 | """ |
| 52 | Return a string of zero bytes that will pad a string of length 'length' to |
| 53 | a multiple of 'alignment'. |
| 54 | """ |
| 55 | return "\x00" * ((length + alignment - 1)/alignment*alignment - length) |
| 56 | |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 57 | class OFReader(object): |
| 58 | """ |
| 59 | Cursor over a read-only buffer |
| 60 | |
| 61 | OpenFlow messages are best thought of as a sequence of elements of |
| 62 | variable size, rather than a C-style struct with fixed offsets and |
| 63 | known field lengths. This class supports efficiently reading |
| 64 | fields sequentially and is intended to be used recursively by the |
| 65 | parsers of child objects which will implicitly update the offset. |
Rich Lane | 5df3fd8 | 2013-12-01 14:53:06 -0800 | [diff] [blame] | 66 | |
| 67 | buf: buffer object |
| 68 | start: initial position in the buffer |
| 69 | length: number of bytes after start |
| 70 | offset: distance from start |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 71 | """ |
Rich Lane | 5df3fd8 | 2013-12-01 14:53:06 -0800 | [diff] [blame] | 72 | def __init__(self, buf, start=0, length=None): |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 73 | self.buf = buf |
Rich Lane | 5df3fd8 | 2013-12-01 14:53:06 -0800 | [diff] [blame] | 74 | self.start = start |
| 75 | if length is None: |
| 76 | self.length = len(buf) - start |
| 77 | else: |
| 78 | self.length = length |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 79 | self.offset = 0 |
| 80 | |
| 81 | def read(self, fmt): |
| 82 | st = struct.Struct(fmt) |
Rich Lane | 5df3fd8 | 2013-12-01 14:53:06 -0800 | [diff] [blame] | 83 | if self.offset + st.size > self.length: |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 84 | raise loxi.ProtocolError("Buffer too short") |
Rich Lane | 5df3fd8 | 2013-12-01 14:53:06 -0800 | [diff] [blame] | 85 | result = st.unpack_from(self.buf, self.start+self.offset) |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 86 | self.offset += st.size |
| 87 | return result |
| 88 | |
| 89 | def read_all(self): |
Rich Lane | 5df3fd8 | 2013-12-01 14:53:06 -0800 | [diff] [blame] | 90 | s = self.buf[(self.start+self.offset):(self.start+self.length)] |
| 91 | assert(len(s) == self.length - self.offset) |
| 92 | self.offset = self.length |
| 93 | return s |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 94 | |
Rich Lane | 78a3bc4 | 2013-11-29 19:22:08 -0800 | [diff] [blame] | 95 | def peek(self, fmt, offset=0): |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 96 | st = struct.Struct(fmt) |
Rich Lane | 5df3fd8 | 2013-12-01 14:53:06 -0800 | [diff] [blame] | 97 | if self.offset + offset + st.size > self.length: |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 98 | raise loxi.ProtocolError("Buffer too short") |
Rich Lane | 5df3fd8 | 2013-12-01 14:53:06 -0800 | [diff] [blame] | 99 | result = st.unpack_from(self.buf, self.start + self.offset + offset) |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 100 | return result |
| 101 | |
| 102 | def skip(self, length): |
Rich Lane | 5df3fd8 | 2013-12-01 14:53:06 -0800 | [diff] [blame] | 103 | if self.offset + length > self.length: |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 104 | raise loxi.ProtocolError("Buffer too short") |
| 105 | self.offset += length |
| 106 | |
Rich Lane | b564efc | 2013-07-23 14:19:29 -0700 | [diff] [blame] | 107 | def skip_align(self): |
Rich Lane | 5df3fd8 | 2013-12-01 14:53:06 -0800 | [diff] [blame] | 108 | new_offset = ((self.start + self.offset + 7) / 8 * 8) - self.start |
| 109 | if new_offset > self.length: |
Rich Lane | b564efc | 2013-07-23 14:19:29 -0700 | [diff] [blame] | 110 | raise loxi.ProtocolError("Buffer too short") |
| 111 | self.offset = new_offset |
| 112 | |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 113 | def is_empty(self): |
Rich Lane | 5df3fd8 | 2013-12-01 14:53:06 -0800 | [diff] [blame] | 114 | return self.offset == self.length |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 115 | |
Rich Lane | 5df3fd8 | 2013-12-01 14:53:06 -0800 | [diff] [blame] | 116 | # Used when parsing objects that have their own length fields |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 117 | def slice(self, length): |
Rich Lane | 5df3fd8 | 2013-12-01 14:53:06 -0800 | [diff] [blame] | 118 | if self.offset + length > self.length: |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 119 | raise loxi.ProtocolError("Buffer too short") |
Rich Lane | 5df3fd8 | 2013-12-01 14:53:06 -0800 | [diff] [blame] | 120 | reader = OFReader(self.buf, self.start + self.offset, length) |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 121 | self.offset += length |
Rich Lane | 5df3fd8 | 2013-12-01 14:53:06 -0800 | [diff] [blame] | 122 | return reader |