Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 1 | # Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior University |
| 2 | # Copyright (c) 2011, 2012 Open Networking Foundation |
| 3 | # Copyright (c) 2012, 2013 Big Switch Networks, Inc. |
Dan Talayco | f620225 | 2013-07-02 01:00:29 -0700 | [diff] [blame] | 4 | # See the file LICENSE.pyloxi which should have been included in the source distribution |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 5 | """ |
| 6 | Utility functions independent of the protocol version |
| 7 | """ |
| 8 | |
| 9 | # Automatically generated by LOXI from template generic_util.py |
| 10 | # Do not modify |
| 11 | |
| 12 | import loxi |
| 13 | import struct |
| 14 | |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 15 | def pack_list(values): |
| 16 | return "".join([x.pack() for x in values]) |
| 17 | |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 18 | def unpack_list(reader, deserializer): |
| 19 | """ |
| 20 | The deserializer function should take an OFReader and return the new object. |
| 21 | """ |
| 22 | entries = [] |
| 23 | while not reader.is_empty(): |
| 24 | entries.append(deserializer(reader)) |
| 25 | return entries |
| 26 | |
Rich Lane | d53156a | 2013-08-05 17:17:33 -0700 | [diff] [blame] | 27 | def pad_to(alignment, length): |
| 28 | """ |
| 29 | Return a string of zero bytes that will pad a string of length 'length' to |
| 30 | a multiple of 'alignment'. |
| 31 | """ |
| 32 | return "\x00" * ((length + alignment - 1)/alignment*alignment - length) |
| 33 | |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 34 | class OFReader(object): |
| 35 | """ |
| 36 | Cursor over a read-only buffer |
| 37 | |
| 38 | OpenFlow messages are best thought of as a sequence of elements of |
| 39 | variable size, rather than a C-style struct with fixed offsets and |
| 40 | known field lengths. This class supports efficiently reading |
| 41 | fields sequentially and is intended to be used recursively by the |
| 42 | parsers of child objects which will implicitly update the offset. |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 43 | |
| 44 | buf: buffer object |
| 45 | start: initial position in the buffer |
| 46 | length: number of bytes after start |
| 47 | offset: distance from start |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 48 | """ |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 49 | def __init__(self, buf, start=0, length=None): |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 50 | self.buf = buf |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 51 | self.start = start |
| 52 | if length is None: |
| 53 | self.length = len(buf) - start |
| 54 | else: |
| 55 | self.length = length |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 56 | self.offset = 0 |
| 57 | |
| 58 | def read(self, fmt): |
| 59 | st = struct.Struct(fmt) |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 60 | if self.offset + st.size > self.length: |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 61 | raise loxi.ProtocolError("Buffer too short") |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 62 | result = st.unpack_from(self.buf, self.start+self.offset) |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 63 | self.offset += st.size |
| 64 | return result |
| 65 | |
| 66 | def read_all(self): |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 67 | s = self.buf[(self.start+self.offset):(self.start+self.length)] |
| 68 | assert(len(s) == self.length - self.offset) |
| 69 | self.offset = self.length |
| 70 | return s |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 71 | |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 72 | def peek(self, fmt, offset=0): |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 73 | st = struct.Struct(fmt) |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 74 | if self.offset + offset + st.size > self.length: |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 75 | raise loxi.ProtocolError("Buffer too short") |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 76 | result = st.unpack_from(self.buf, self.start + self.offset + offset) |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 77 | return result |
| 78 | |
| 79 | def skip(self, length): |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 80 | if self.offset + length > self.length: |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 81 | raise loxi.ProtocolError("Buffer too short") |
| 82 | self.offset += length |
| 83 | |
Rich Lane | d53156a | 2013-08-05 17:17:33 -0700 | [diff] [blame] | 84 | def skip_align(self): |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 85 | new_offset = ((self.start + self.offset + 7) / 8 * 8) - self.start |
| 86 | if new_offset > self.length: |
Rich Lane | d53156a | 2013-08-05 17:17:33 -0700 | [diff] [blame] | 87 | raise loxi.ProtocolError("Buffer too short") |
| 88 | self.offset = new_offset |
| 89 | |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 90 | def is_empty(self): |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 91 | return self.offset == self.length |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 92 | |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 93 | # Used when parsing objects that have their own length fields |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 94 | def slice(self, length): |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 95 | if self.offset + length > self.length: |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 96 | raise loxi.ProtocolError("Buffer too short") |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 97 | reader = OFReader(self.buf, self.start + self.offset, length) |
Rich Lane | ca3da27 | 2013-05-05 09:07:33 -0700 | [diff] [blame] | 98 | self.offset += length |
Rich Lane | 7dcdf02 | 2013-12-11 14:45:27 -0800 | [diff] [blame] | 99 | return reader |