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