Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 1 | # |
| 2 | # Copyright (c) 2018 Cisco and/or its affiliates. |
| 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 implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 15 | import collections |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 16 | import logging |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 17 | import socket |
Paul Vinciguerra | ae8819f | 2019-06-07 13:35:37 -0400 | [diff] [blame] | 18 | import struct |
| 19 | import sys |
Paul Vinciguerra | 42bdba8 | 2019-03-07 11:26:23 -0800 | [diff] [blame] | 20 | |
| 21 | if sys.version_info <= (3, 4): |
Paul Vinciguerra | ae8819f | 2019-06-07 13:35:37 -0400 | [diff] [blame] | 22 | from aenum import IntEnum # noqa: F401 |
Ole Troan | 48ae19e | 2019-03-07 11:28:32 +0100 | [diff] [blame] | 23 | else: |
Paul Vinciguerra | ae8819f | 2019-06-07 13:35:37 -0400 | [diff] [blame] | 24 | from enum import IntEnum # noqa: F401 |
Paul Vinciguerra | 42bdba8 | 2019-03-07 11:26:23 -0800 | [diff] [blame] | 25 | |
| 26 | if sys.version_info <= (3, 6): |
Paul Vinciguerra | ae8819f | 2019-06-07 13:35:37 -0400 | [diff] [blame] | 27 | from aenum import IntFlag # noqa: F401 |
Paul Vinciguerra | 42bdba8 | 2019-03-07 11:26:23 -0800 | [diff] [blame] | 28 | else: |
Paul Vinciguerra | 42bdba8 | 2019-03-07 11:26:23 -0800 | [diff] [blame] | 29 | |
Paul Vinciguerra | ae8819f | 2019-06-07 13:35:37 -0400 | [diff] [blame] | 30 | from enum import IntFlag # noqa: F401 |
| 31 | |
| 32 | from . import vpp_format # noqa: E402 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 33 | |
| 34 | # |
| 35 | # Set log-level in application by doing e.g.: |
| 36 | # logger = logging.getLogger('vpp_serializer') |
| 37 | # logger.setLevel(logging.DEBUG) |
| 38 | # |
| 39 | logger = logging.getLogger(__name__) |
| 40 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 41 | if sys.version[0] == '2': |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 42 | def check(d): type(d) is dict |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 43 | else: |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 44 | def check(d): type(d) is dict or type(d) is bytes |
| 45 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 46 | |
| 47 | def conversion_required(data, field_type): |
| 48 | if check(data): |
| 49 | return False |
| 50 | try: |
| 51 | if type(data).__name__ in vpp_format.conversion_table[field_type]: |
| 52 | return True |
| 53 | except KeyError: |
| 54 | return False |
| 55 | |
| 56 | |
| 57 | def conversion_packer(data, field_type): |
| 58 | t = type(data).__name__ |
| 59 | return types[field_type].pack(vpp_format. |
| 60 | conversion_table[field_type][t](data)) |
| 61 | |
| 62 | |
| 63 | def conversion_unpacker(data, field_type): |
| 64 | if field_type not in vpp_format.conversion_unpacker_table: |
| 65 | return data |
| 66 | return vpp_format.conversion_unpacker_table[field_type](data) |
| 67 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 68 | |
Paul Vinciguerra | 7e713f1 | 2018-11-26 12:04:48 -0800 | [diff] [blame] | 69 | class BaseTypes(object): |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 70 | def __init__(self, type, elements=0, options=None): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 71 | base_types = {'u8': '>B', |
Paul Vinciguerra | 6ce0f35 | 2019-07-02 13:06:21 -0400 | [diff] [blame] | 72 | 'i8': '>b', |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 73 | 'string': '>s', |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 74 | 'u16': '>H', |
Paul Vinciguerra | 6ce0f35 | 2019-07-02 13:06:21 -0400 | [diff] [blame] | 75 | 'i16': '>h', |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 76 | 'u32': '>I', |
| 77 | 'i32': '>i', |
| 78 | 'u64': '>Q', |
Paul Vinciguerra | 6ce0f35 | 2019-07-02 13:06:21 -0400 | [diff] [blame] | 79 | 'i64': '>q', |
Paul Vinciguerra | 888640a | 2019-05-14 21:01:28 -0400 | [diff] [blame] | 80 | 'f64': '=d', |
Ole Troan | f47e9b6 | 2018-10-16 15:14:03 +0200 | [diff] [blame] | 81 | 'bool': '>?', |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 82 | 'header': '>HI'} |
| 83 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 84 | if elements > 0 and (type == 'u8' or type == 'string'): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 85 | self.packer = struct.Struct('>%ss' % elements) |
| 86 | else: |
| 87 | self.packer = struct.Struct(base_types[type]) |
| 88 | self.size = self.packer.size |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 89 | self.options = options |
| 90 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 91 | def pack(self, data, kwargs=None): |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 92 | if data is None: # Default to zero if not specified |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 93 | if self.options and 'default' in self.options: |
| 94 | data = self.options['default'] |
| 95 | else: |
| 96 | data = 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 97 | return self.packer.pack(data) |
| 98 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 99 | def unpack(self, data, offset, result=None, ntc=False): |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 100 | return self.packer.unpack_from(data, offset)[0], self.packer.size |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 101 | |
| 102 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 103 | class String(object): |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 104 | def __init__(self, name, num, options): |
| 105 | self.name = name |
| 106 | self.num = num |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 107 | self.size = 1 |
| 108 | self.length_field_packer = BaseTypes('u32') |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 109 | self.limit = options['limit'] if 'limit' in options else num |
| 110 | self.fixed = True if num else False |
| 111 | if self.fixed and not self.limit: |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 112 | raise VPPSerializerValueError( |
| 113 | "Invalid argument length for: {}, {} maximum {}". |
| 114 | format(list, len(list), self.limit)) |
| 115 | |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 116 | def pack(self, list, kwargs=None): |
| 117 | if not list: |
| 118 | if self.fixed: |
| 119 | return b"\x00" * self.limit |
| 120 | return self.length_field_packer.pack(0) + b"" |
| 121 | if self.limit and len(list) > self.limit - 1: |
| 122 | raise VPPSerializerValueError( |
| 123 | "Invalid argument length for: {}, {} maximum {}". |
| 124 | format(list, len(list), self.limit - 1)) |
| 125 | if self.fixed: |
| 126 | return list.encode('ascii').ljust(self.limit, b'\x00') |
| 127 | return self.length_field_packer.pack(len(list)) + list.encode('ascii') |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 128 | |
| 129 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 130 | if self.fixed: |
| 131 | p = BaseTypes('u8', self.num) |
| 132 | s = p.unpack(data, offset) |
| 133 | s2 = s[0].split(b'\0', 1)[0] |
| 134 | return (s2.decode('ascii'), self.num) |
| 135 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 136 | length, length_field_size = self.length_field_packer.unpack(data, |
| 137 | offset) |
| 138 | if length == 0: |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 139 | return '', 0 |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 140 | p = BaseTypes('u8', length) |
| 141 | x, size = p.unpack(data, offset + length_field_size) |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 142 | #x2 = x.split(b'\0', 1)[0] |
| 143 | return (x.decode('ascii', errors='replace'), size + length_field_size) |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 144 | |
| 145 | |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 146 | types = {'u8': BaseTypes('u8'), 'u16': BaseTypes('u16'), |
| 147 | 'u32': BaseTypes('u32'), 'i32': BaseTypes('i32'), |
| 148 | 'u64': BaseTypes('u64'), 'f64': BaseTypes('f64'), |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 149 | 'bool': BaseTypes('bool'), 'string': String} |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 150 | |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 151 | class_types = {} |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 152 | |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 153 | def vpp_get_type(name): |
| 154 | try: |
| 155 | return types[name] |
| 156 | except KeyError: |
| 157 | return None |
| 158 | |
| 159 | |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 160 | class VPPSerializerValueError(ValueError): |
| 161 | pass |
| 162 | |
| 163 | |
Paul Vinciguerra | 7e713f1 | 2018-11-26 12:04:48 -0800 | [diff] [blame] | 164 | class FixedList_u8(object): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 165 | def __init__(self, name, field_type, num): |
| 166 | self.name = name |
| 167 | self.num = num |
| 168 | self.packer = BaseTypes(field_type, num) |
| 169 | self.size = self.packer.size |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 170 | self.field_type = field_type |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 171 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 172 | def pack(self, data, kwargs=None): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 173 | """Packs a fixed length bytestring. Left-pads with zeros |
| 174 | if input data is too short.""" |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 175 | if not data: |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 176 | return b'\x00' * self.size |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 177 | |
| 178 | if len(data) > self.num: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 179 | raise VPPSerializerValueError( |
| 180 | 'Fixed list length error for "{}", got: {}' |
| 181 | ' expected: {}' |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 182 | .format(self.name, len(data), self.num)) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 183 | |
Ole Troan | 64e978b | 2019-10-17 21:40:36 +0200 | [diff] [blame] | 184 | try: |
| 185 | return self.packer.pack(data) |
| 186 | except struct.error: |
| 187 | raise VPPSerializerValueError( |
| 188 | 'Packing failed for "{}" {}' |
| 189 | .format(self.name, kwargs)) |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 190 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 191 | if len(data[offset:]) < self.num: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 192 | raise VPPSerializerValueError( |
| 193 | 'Invalid array length for "{}" got {}' |
| 194 | ' expected {}' |
| 195 | .format(self.name, len(data[offset:]), self.num)) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 196 | return self.packer.unpack(data, offset) |
| 197 | |
| 198 | |
Paul Vinciguerra | 7e713f1 | 2018-11-26 12:04:48 -0800 | [diff] [blame] | 199 | class FixedList(object): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 200 | def __init__(self, name, field_type, num): |
| 201 | self.num = num |
| 202 | self.packer = types[field_type] |
| 203 | self.size = self.packer.size * num |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 204 | self.name = name |
| 205 | self.field_type = field_type |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 206 | |
| 207 | def pack(self, list, kwargs): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 208 | if len(list) != self.num: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 209 | raise VPPSerializerValueError( |
| 210 | 'Fixed list length error, got: {} expected: {}' |
| 211 | .format(len(list), self.num)) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 212 | b = bytes() |
| 213 | for e in list: |
| 214 | b += self.packer.pack(e) |
| 215 | return b |
| 216 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 217 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 218 | # Return a list of arguments |
| 219 | result = [] |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 220 | total = 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 221 | for e in range(self.num): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 222 | x, size = self.packer.unpack(data, offset, ntc=ntc) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 223 | result.append(x) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 224 | offset += size |
| 225 | total += size |
| 226 | return result, total |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 227 | |
| 228 | |
Paul Vinciguerra | 7e713f1 | 2018-11-26 12:04:48 -0800 | [diff] [blame] | 229 | class VLAList(object): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 230 | def __init__(self, name, field_type, len_field_name, index): |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 231 | self.name = name |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 232 | self.field_type = field_type |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 233 | self.index = index |
| 234 | self.packer = types[field_type] |
| 235 | self.size = self.packer.size |
| 236 | self.length_field = len_field_name |
| 237 | |
Ole Troan | 2267429 | 2019-10-21 18:59:11 +0200 | [diff] [blame] | 238 | def pack(self, lst, kwargs=None): |
| 239 | if not lst: |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 240 | return b"" |
Ole Troan | 2267429 | 2019-10-21 18:59:11 +0200 | [diff] [blame] | 241 | if len(lst) != kwargs[self.length_field]: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 242 | raise VPPSerializerValueError( |
| 243 | 'Variable length error, got: {} expected: {}' |
Ole Troan | 2267429 | 2019-10-21 18:59:11 +0200 | [diff] [blame] | 244 | .format(len(lst), kwargs[self.length_field])) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 245 | |
| 246 | # u8 array |
| 247 | if self.packer.size == 1: |
Ole Troan | 2267429 | 2019-10-21 18:59:11 +0200 | [diff] [blame] | 248 | if isinstance(lst, list): |
| 249 | return b''.join(lst) |
| 250 | return bytes(lst) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 251 | |
Ole Troan | 2267429 | 2019-10-21 18:59:11 +0200 | [diff] [blame] | 252 | b = bytes() |
| 253 | for e in lst: |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 254 | b += self.packer.pack(e) |
| 255 | return b |
| 256 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 257 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 258 | # Return a list of arguments |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 259 | total = 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 260 | |
| 261 | # u8 array |
| 262 | if self.packer.size == 1: |
| 263 | if result[self.index] == 0: |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 264 | return b'', 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 265 | p = BaseTypes('u8', result[self.index]) |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 266 | return p.unpack(data, offset, ntc=ntc) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 267 | |
| 268 | r = [] |
| 269 | for e in range(result[self.index]): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 270 | x, size = self.packer.unpack(data, offset, ntc=ntc) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 271 | r.append(x) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 272 | offset += size |
| 273 | total += size |
| 274 | return r, total |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 275 | |
| 276 | |
| 277 | class VLAList_legacy(): |
| 278 | def __init__(self, name, field_type): |
| 279 | self.packer = types[field_type] |
| 280 | self.size = self.packer.size |
| 281 | |
| 282 | def pack(self, list, kwargs=None): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 283 | if self.packer.size == 1: |
| 284 | return bytes(list) |
| 285 | |
| 286 | b = bytes() |
| 287 | for e in list: |
| 288 | b += self.packer.pack(e) |
| 289 | return b |
| 290 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 291 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 292 | total = 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 293 | # Return a list of arguments |
| 294 | if (len(data) - offset) % self.packer.size: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 295 | raise VPPSerializerValueError( |
| 296 | 'Legacy Variable Length Array length mismatch.') |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 297 | elements = int((len(data) - offset) / self.packer.size) |
| 298 | r = [] |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 299 | for e in range(elements): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 300 | x, size = self.packer.unpack(data, offset, ntc=ntc) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 301 | r.append(x) |
| 302 | offset += self.packer.size |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 303 | total += size |
| 304 | return r, total |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 305 | |
| 306 | |
Paul Vinciguerra | 7e713f1 | 2018-11-26 12:04:48 -0800 | [diff] [blame] | 307 | class VPPEnumType(object): |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 308 | def __init__(self, name, msgdef, options=None): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 309 | self.size = types['u32'].size |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 310 | self.name = name |
Andrew Yourtchenko | 5c4b9f1 | 2019-03-19 14:52:29 +0100 | [diff] [blame] | 311 | self.enumtype = 'u32' |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 312 | self.msgdef = msgdef |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 313 | e_hash = {} |
| 314 | for f in msgdef: |
| 315 | if type(f) is dict and 'enumtype' in f: |
| 316 | if f['enumtype'] != 'u32': |
Andrew Yourtchenko | 5c4b9f1 | 2019-03-19 14:52:29 +0100 | [diff] [blame] | 317 | self.size = types[f['enumtype']].size |
| 318 | self.enumtype = f['enumtype'] |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 319 | continue |
| 320 | ename, evalue = f |
| 321 | e_hash[ename] = evalue |
Ole Troan | 48ae19e | 2019-03-07 11:28:32 +0100 | [diff] [blame] | 322 | self.enum = IntFlag(name, e_hash) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 323 | types[name] = self |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 324 | class_types[name] = VPPEnumType |
| 325 | self.options = options |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 326 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 327 | def __getattr__(self, name): |
| 328 | return self.enum[name] |
| 329 | |
Paul Vinciguerra | a089ae1 | 2019-07-08 15:41:07 -0400 | [diff] [blame] | 330 | def __bool__(self): |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 331 | return True |
| 332 | |
Paul Vinciguerra | a089ae1 | 2019-07-08 15:41:07 -0400 | [diff] [blame] | 333 | if sys.version[0] == '2': |
| 334 | __nonzero__ = __bool__ |
| 335 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 336 | def pack(self, data, kwargs=None): |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 337 | if data is None: # Default to zero if not specified |
| 338 | if self.options and 'default' in self.options: |
| 339 | data = self.options['default'] |
| 340 | else: |
| 341 | data = 0 |
| 342 | |
Andrew Yourtchenko | 5c4b9f1 | 2019-03-19 14:52:29 +0100 | [diff] [blame] | 343 | return types[self.enumtype].pack(data) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 344 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 345 | def unpack(self, data, offset=0, result=None, ntc=False): |
Andrew Yourtchenko | 5c4b9f1 | 2019-03-19 14:52:29 +0100 | [diff] [blame] | 346 | x, size = types[self.enumtype].unpack(data, offset) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 347 | return self.enum(x), size |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 348 | |
| 349 | |
Paul Vinciguerra | 7e713f1 | 2018-11-26 12:04:48 -0800 | [diff] [blame] | 350 | class VPPUnionType(object): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 351 | def __init__(self, name, msgdef): |
| 352 | self.name = name |
| 353 | self.size = 0 |
| 354 | self.maxindex = 0 |
| 355 | fields = [] |
| 356 | self.packers = collections.OrderedDict() |
| 357 | for i, f in enumerate(msgdef): |
| 358 | if type(f) is dict and 'crc' in f: |
| 359 | self.crc = f['crc'] |
| 360 | continue |
| 361 | f_type, f_name = f |
| 362 | if f_type not in types: |
| 363 | logger.debug('Unknown union type {}'.format(f_type)) |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 364 | raise VPPSerializerValueError( |
| 365 | 'Unknown message type {}'.format(f_type)) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 366 | fields.append(f_name) |
| 367 | size = types[f_type].size |
| 368 | self.packers[f_name] = types[f_type] |
| 369 | if size > self.size: |
| 370 | self.size = size |
| 371 | self.maxindex = i |
| 372 | |
| 373 | types[name] = self |
| 374 | self.tuple = collections.namedtuple(name, fields, rename=True) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 375 | |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 376 | # Union of variable length? |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 377 | def pack(self, data, kwargs=None): |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 378 | if not data: |
| 379 | return b'\x00' * self.size |
| 380 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 381 | for k, v in data.items(): |
| 382 | logger.debug("Key: {} Value: {}".format(k, v)) |
| 383 | b = self.packers[k].pack(v, kwargs) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 384 | break |
| 385 | r = bytearray(self.size) |
Ole Troan | b199e98 | 2018-08-02 19:19:21 +0200 | [diff] [blame] | 386 | r[:len(b)] = b |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 387 | return r |
| 388 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 389 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 390 | r = [] |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 391 | maxsize = 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 392 | for k, p in self.packers.items(): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 393 | x, size = p.unpack(data, offset, ntc=ntc) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 394 | if size > maxsize: |
| 395 | maxsize = size |
| 396 | r.append(x) |
| 397 | return self.tuple._make(r), maxsize |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 398 | |
| 399 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 400 | class VPPTypeAlias(object): |
| 401 | def __init__(self, name, msgdef): |
| 402 | self.name = name |
| 403 | t = vpp_get_type(msgdef['type']) |
| 404 | if not t: |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 405 | raise ValueError('No such type: {}'.format(msgdef['type'])) |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 406 | if 'length' in msgdef: |
| 407 | if msgdef['length'] == 0: |
| 408 | raise ValueError() |
| 409 | if msgdef['type'] == 'u8': |
| 410 | self.packer = FixedList_u8(name, msgdef['type'], |
| 411 | msgdef['length']) |
| 412 | self.size = self.packer.size |
| 413 | else: |
| 414 | self.packer = FixedList(name, msgdef['type'], msgdef['length']) |
Ole Troan | 8c8acc0 | 2018-11-27 10:05:23 +0100 | [diff] [blame] | 415 | else: |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 416 | self.packer = t |
| 417 | self.size = t.size |
| 418 | |
| 419 | types[name] = self |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 420 | self.toplevelconversion = False |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 421 | |
| 422 | def pack(self, data, kwargs=None): |
| 423 | if data and conversion_required(data, self.name): |
| 424 | try: |
| 425 | return conversion_packer(data, self.name) |
| 426 | # Python 2 and 3 raises different exceptions from inet_pton |
| 427 | except(OSError, socket.error, TypeError): |
| 428 | pass |
| 429 | |
| 430 | return self.packer.pack(data, kwargs) |
| 431 | |
| 432 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 433 | if ntc == False and self.name in vpp_format.conversion_unpacker_table: |
| 434 | # Disable type conversion for dependent types |
| 435 | ntc = True |
| 436 | self.toplevelconversion = True |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 437 | t, size = self.packer.unpack(data, offset, result, ntc=ntc) |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 438 | if self.toplevelconversion: |
| 439 | self.toplevelconversion = False |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 440 | return conversion_unpacker(t, self.name), size |
| 441 | return t, size |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 442 | |
| 443 | |
Paul Vinciguerra | 7e713f1 | 2018-11-26 12:04:48 -0800 | [diff] [blame] | 444 | class VPPType(object): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 445 | # Set everything up to be able to pack / unpack |
| 446 | def __init__(self, name, msgdef): |
| 447 | self.name = name |
| 448 | self.msgdef = msgdef |
| 449 | self.packers = [] |
| 450 | self.fields = [] |
| 451 | self.fieldtypes = [] |
| 452 | self.field_by_name = {} |
| 453 | size = 0 |
| 454 | for i, f in enumerate(msgdef): |
| 455 | if type(f) is dict and 'crc' in f: |
| 456 | self.crc = f['crc'] |
| 457 | continue |
| 458 | f_type, f_name = f[:2] |
| 459 | self.fields.append(f_name) |
| 460 | self.field_by_name[f_name] = None |
| 461 | self.fieldtypes.append(f_type) |
| 462 | if f_type not in types: |
| 463 | logger.debug('Unknown type {}'.format(f_type)) |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 464 | raise VPPSerializerValueError( |
| 465 | 'Unknown message type {}'.format(f_type)) |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 466 | |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 467 | fieldlen = len(f) |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 468 | options = [x for x in f if type(x) is dict] |
| 469 | if len(options): |
| 470 | self.options = options[0] |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 471 | fieldlen -= 1 |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 472 | else: |
| 473 | self.options = {} |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 474 | if fieldlen == 3: # list |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 475 | list_elements = f[2] |
| 476 | if list_elements == 0: |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 477 | if f_type == 'string': |
| 478 | p = String(f_name, 0, self.options) |
| 479 | else: |
| 480 | p = VLAList_legacy(f_name, f_type) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 481 | self.packers.append(p) |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 482 | elif f_type == 'u8': |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 483 | p = FixedList_u8(f_name, f_type, list_elements) |
| 484 | self.packers.append(p) |
| 485 | size += p.size |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 486 | elif f_type == 'string': |
| 487 | p = String(f_name, list_elements, self.options) |
| 488 | self.packers.append(p) |
| 489 | size += p.size |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 490 | else: |
| 491 | p = FixedList(f_name, f_type, list_elements) |
| 492 | self.packers.append(p) |
| 493 | size += p.size |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 494 | elif fieldlen == 4: # Variable length list |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 495 | length_index = self.fields.index(f[3]) |
| 496 | p = VLAList(f_name, f_type, f[3], length_index) |
| 497 | self.packers.append(p) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 498 | else: |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 499 | # Support default for basetypes and enums |
| 500 | if 'default' in self.options: |
| 501 | try: |
| 502 | p = BaseTypes(f_type, 0, self.options) |
| 503 | except KeyError: |
| 504 | p = class_types[f_type](f_name, types[f_type].msgdef, self.options) |
| 505 | else: |
| 506 | p = types[f_type] |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 507 | self.packers.append(p) |
| 508 | size += p.size |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 509 | |
| 510 | self.size = size |
| 511 | self.tuple = collections.namedtuple(name, self.fields, rename=True) |
| 512 | types[name] = self |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 513 | self.toplevelconversion = False |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 514 | |
| 515 | def pack(self, data, kwargs=None): |
| 516 | if not kwargs: |
| 517 | kwargs = data |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 518 | b = bytes() |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 519 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 520 | # Try one of the format functions |
| 521 | if data and conversion_required(data, self.name): |
| 522 | return conversion_packer(data, self.name) |
| 523 | |
| 524 | for i, a in enumerate(self.fields): |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 525 | if data and type(data) is not dict and a not in data: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 526 | raise VPPSerializerValueError( |
| 527 | "Invalid argument: {} expected {}.{}". |
| 528 | format(data, self.name, a)) |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 529 | |
| 530 | # Defaulting to zero. |
| 531 | if not data or a not in data: # Default to 0 |
| 532 | arg = None |
| 533 | kwarg = None # No default for VLA |
| 534 | else: |
| 535 | arg = data[a] |
| 536 | kwarg = kwargs[a] if a in kwargs else None |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 537 | if isinstance(self.packers[i], VPPType): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 538 | b += self.packers[i].pack(arg, kwarg) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 539 | else: |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 540 | b += self.packers[i].pack(arg, kwargs) |
| 541 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 542 | return b |
| 543 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 544 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 545 | # Return a list of arguments |
| 546 | result = [] |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 547 | total = 0 |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 548 | if ntc == False and self.name in vpp_format.conversion_unpacker_table: |
| 549 | # Disable type conversion for dependent types |
| 550 | ntc = True |
| 551 | self.toplevelconversion = True |
| 552 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 553 | for p in self.packers: |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 554 | x, size = p.unpack(data, offset, result, ntc) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 555 | if type(x) is tuple and len(x) == 1: |
| 556 | x = x[0] |
| 557 | result.append(x) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 558 | offset += size |
| 559 | total += size |
| 560 | t = self.tuple._make(result) |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 561 | |
| 562 | if self.toplevelconversion: |
| 563 | self.toplevelconversion = False |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 564 | t = conversion_unpacker(t, self.name) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 565 | return t, total |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 566 | |
| 567 | |
| 568 | class VPPMessage(VPPType): |
| 569 | pass |