blob: 2c24571c3504918abb34b24f1ea8e13cb6a44aa2 [file] [log] [blame]
Klement Sekeraacb9b8e2017-02-14 02:55:31 +01001""" test framework utilities """
2
Paul Vinciguerra2f156312020-05-02 22:34:40 -04003import ipaddress
Paul Vinciguerra9beabd82019-12-01 22:24:28 -05004import logging
Matej Klotton0178d522016-11-04 11:11:44 +01005import socket
Paul Vinciguerrae8fece82019-02-28 15:34:00 -08006from socket import AF_INET6
juraj.linkes40dd73b2018-09-21 13:55:16 +02007import os.path
Klement Sekeraad3187f2022-02-18 10:34:35 +00008from copy import deepcopy
Klement Sekera26cd0242022-02-18 10:35:08 +00009from collections import UserDict
Klement Sekera7bb873a2016-11-18 07:38:42 +010010
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -070011import scapy.compat
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +020012from scapy.layers.l2 import Ether
Klement Sekera75e7d132017-09-20 08:26:30 +020013from scapy.layers.inet import IP
14from scapy.layers.inet6 import IPv6, IPv6ExtHdrFragment, IPv6ExtHdrRouting,\
15 IPv6ExtHdrHopByHop
Paul Vinciguerrae8fece82019-02-28 15:34:00 -080016from scapy.packet import Raw
Klement Sekera611864f2018-09-26 11:19:00 +020017from scapy.utils import hexdump
Paul Vinciguerrae8fece82019-02-28 15:34:00 -080018from scapy.utils6 import in6_mactoifaceid
19
Ole Troan7f991832018-12-06 17:35:12 +010020from io import BytesIO
Ole Troan8006c6a2018-12-17 12:02:26 +010021from vpp_papi import mac_pton
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +020022
Paul Vinciguerra9beabd82019-12-01 22:24:28 -050023# Set up an empty logger for the testcase that can be overridden as necessary
24null_logger = logging.getLogger('VppTestCase.util')
25null_logger.addHandler(logging.NullHandler())
26
Klement Sekera7bb873a2016-11-18 07:38:42 +010027
Klement Sekera26cd0242022-02-18 10:35:08 +000028def pr(packet):
29 return packet.__repr__()
30
31
Klement Sekera7bb873a2016-11-18 07:38:42 +010032def ppp(headline, packet):
Klement Sekera26cd0242022-02-18 10:35:08 +000033 """ Return string containing headline and output of scapy packet.show() """
Paul Vinciguerrab951ad82019-01-08 21:36:39 -080034 return '%s\n%s\n\n%s\n' % (headline,
35 hexdump(packet, dump=True),
36 packet.show(dump=True))
Klement Sekera7bb873a2016-11-18 07:38:42 +010037
Damjan Marionf56b77a2016-10-03 19:44:57 +020038
Klement Sekera9225dee2016-12-12 08:36:58 +010039def ppc(headline, capture, limit=10):
40 """ Return string containing ppp() printout for a capture.
41
42 :param headline: printed as first line of output
43 :param capture: packets to print
44 :param limit: limit the print to # of packets
45 """
46 if not capture:
47 return headline
Klement Sekeradab231a2016-12-21 08:50:14 +010048 tail = ""
Klement Sekera9225dee2016-12-12 08:36:58 +010049 if limit < len(capture):
Klement Sekeradab231a2016-12-21 08:50:14 +010050 tail = "\nPrint limit reached, %s out of %s packets printed" % (
Klement Sekera75e7d132017-09-20 08:26:30 +020051 limit, len(capture))
Klement Sekeradab231a2016-12-21 08:50:14 +010052 body = "".join([ppp("Packet #%s:" % count, p)
53 for count, p in zip(range(0, limit), capture)])
54 return "%s\n%s%s" % (headline, body, tail)
Klement Sekera9225dee2016-12-12 08:36:58 +010055
56
Eyal Barid81da8c2017-01-11 13:39:54 +020057def ip4_range(ip4, s, e):
58 tmp = ip4.rsplit('.', 1)[0]
59 return ("%s.%d" % (tmp, i) for i in range(s, e))
60
61
Paul Vinciguerra2f156312020-05-02 22:34:40 -040062def mcast_ip_to_mac(ip):
63 ip = ipaddress.ip_address(ip)
64 if not ip.is_multicast:
65 raise ValueError("Must be multicast address.")
66 ip_as_int = int(ip)
67 if ip.version == 4:
68 mcast_mac = "01:00:5e:%02x:%02x:%02x" % ((ip_as_int >> 16) & 0x7f,
69 (ip_as_int >> 8) & 0xff,
70 ip_as_int & 0xff)
71 else:
72 mcast_mac = "33:33:%02x:%02x:%02x:%02x" % ((ip_as_int >> 24) & 0xff,
73 (ip_as_int >> 16) & 0xff,
74 (ip_as_int >> 8) & 0xff,
75 ip_as_int & 0xff)
76 return mcast_mac
Eyal Barid81da8c2017-01-11 13:39:54 +020077
78
Paul Vinciguerrae8fece82019-02-28 15:34:00 -080079# wrapper around scapy library function.
Neale Ranns2a3ea492017-04-19 05:24:40 -070080def mk_ll_addr(mac):
Ole Troan6ed154f2019-10-15 19:31:55 +020081 euid = in6_mactoifaceid(str(mac))
Neale Ranns2a3ea492017-04-19 05:24:40 -070082 addr = "fe80::" + euid
83 return addr
84
85
Juraj Sloboda4b9669d2018-01-15 10:39:21 +010086def ip6_normalize(ip6):
87 return socket.inet_ntop(socket.AF_INET6,
88 socket.inet_pton(socket.AF_INET6, ip6))
89
90
juraj.linkes40dd73b2018-09-21 13:55:16 +020091def get_core_path(tempdir):
92 return "%s/%s" % (tempdir, get_core_pattern())
93
94
95def is_core_present(tempdir):
96 return os.path.isfile(get_core_path(tempdir))
97
98
99def get_core_pattern():
Andrew Yourtchenko57612eb2018-03-28 15:32:10 +0200100 with open("/proc/sys/kernel/core_pattern", "r") as f:
juraj.linkes40dd73b2018-09-21 13:55:16 +0200101 corefmt = f.read().strip()
102 return corefmt
103
104
105def check_core_path(logger, core_path):
106 corefmt = get_core_pattern()
107 if corefmt.startswith("|"):
108 logger.error(
109 "WARNING: redirecting the core dump through a"
110 " filter may result in truncated dumps.")
111 logger.error(
112 " You may want to check the filter settings"
113 " or uninstall it and edit the"
114 " /proc/sys/kernel/core_pattern accordingly.")
115 logger.error(
116 " current core pattern is: %s" % corefmt)
Andrew Yourtchenko57612eb2018-03-28 15:32:10 +0200117
118
Paul Vinciguerrae061dad2020-12-04 14:57:51 -0500119class NumericConstant:
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200120
121 desc_dict = {}
122
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200123 def __init__(self, value):
124 self._value = value
125
126 def __int__(self):
127 return self._value
128
129 def __long__(self):
130 return self._value
131
132 def __str__(self):
133 if self._value in self.desc_dict:
134 return self.desc_dict[self._value]
135 return ""
136
137
Paul Vinciguerrae061dad2020-12-04 14:57:51 -0500138class Host:
Matej Klotton0178d522016-11-04 11:11:44 +0100139 """ Generic test host "connected" to VPPs interface. """
Damjan Marionf56b77a2016-10-03 19:44:57 +0200140
Klement Sekeraf62ae122016-10-11 11:47:09 +0200141 @property
142 def mac(self):
143 """ MAC address """
144 return self._mac
Damjan Marionf56b77a2016-10-03 19:44:57 +0200145
Klement Sekeraf62ae122016-10-11 11:47:09 +0200146 @property
Eyal Baric86e5922017-07-02 18:33:16 +0300147 def bin_mac(self):
148 """ MAC address """
Ole Troan8006c6a2018-12-17 12:02:26 +0100149 return mac_pton(self._mac)
Eyal Baric86e5922017-07-02 18:33:16 +0300150
151 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +0200152 def ip4(self):
Klement Sekera46a87ad2017-01-02 08:22:23 +0100153 """ IPv4 address - string """
Klement Sekeraf62ae122016-10-11 11:47:09 +0200154 return self._ip4
Damjan Marionf56b77a2016-10-03 19:44:57 +0200155
Klement Sekeraf62ae122016-10-11 11:47:09 +0200156 @property
Matej Klotton0178d522016-11-04 11:11:44 +0100157 def ip4n(self):
Klement Sekera46a87ad2017-01-02 08:22:23 +0100158 """ IPv4 address of remote host - raw, suitable as API parameter."""
Matej Klotton0178d522016-11-04 11:11:44 +0100159 return socket.inet_pton(socket.AF_INET, self._ip4)
160
161 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +0200162 def ip6(self):
Klement Sekera46a87ad2017-01-02 08:22:23 +0100163 """ IPv6 address - string """
Klement Sekeraf62ae122016-10-11 11:47:09 +0200164 return self._ip6
Damjan Marionf56b77a2016-10-03 19:44:57 +0200165
Klement Sekera46a87ad2017-01-02 08:22:23 +0100166 @property
167 def ip6n(self):
168 """ IPv6 address of remote host - raw, suitable as API parameter."""
169 return socket.inet_pton(socket.AF_INET6, self._ip6)
170
Neale Ranns2a3ea492017-04-19 05:24:40 -0700171 @property
172 def ip6_ll(self):
173 """ IPv6 link-local address - string """
174 return self._ip6_ll
175
176 @property
177 def ip6n_ll(self):
178 """ IPv6 link-local address of remote host -
179 raw, suitable as API parameter."""
180 return socket.inet_pton(socket.AF_INET6, self._ip6_ll)
181
Eyal Baric86e5922017-07-02 18:33:16 +0300182 def __eq__(self, other):
183 if isinstance(other, Host):
184 return (self.mac == other.mac and
185 self.ip4 == other.ip4 and
186 self.ip6 == other.ip6 and
187 self.ip6_ll == other.ip6_ll)
188 else:
189 return False
190
191 def __ne__(self, other):
192 return not self.__eq__(other)
193
194 def __repr__(self):
195 return "Host { mac:%s ip4:%s ip6:%s ip6_ll:%s }" % (self.mac,
196 self.ip4,
197 self.ip6,
198 self.ip6_ll)
199
200 def __hash__(self):
201 return hash(self.__repr__())
202
Neale Ranns2a3ea492017-04-19 05:24:40 -0700203 def __init__(self, mac=None, ip4=None, ip6=None, ip6_ll=None):
Klement Sekeraf62ae122016-10-11 11:47:09 +0200204 self._mac = mac
205 self._ip4 = ip4
206 self._ip6 = ip6
Neale Ranns2a3ea492017-04-19 05:24:40 -0700207 self._ip6_ll = ip6_ll
Filip Tehlar770e89e2017-01-31 10:39:16 +0100208
209
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200210class L4_Conn():
211 """ L4 'connection' tied to two VPP interfaces """
Klement Sekera75e7d132017-09-20 08:26:30 +0200212
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200213 def __init__(self, testcase, if1, if2, af, l4proto, port1, port2):
214 self.testcase = testcase
215 self.ifs = [None, None]
216 self.ifs[0] = if1
217 self.ifs[1] = if2
218 self.address_family = af
219 self.l4proto = l4proto
220 self.ports = [None, None]
221 self.ports[0] = port1
222 self.ports[1] = port2
223 self
224
225 def pkt(self, side, l4args={}, payload="x"):
226 is_ip6 = 1 if self.address_family == AF_INET6 else 0
227 s0 = side
Klement Sekera75e7d132017-09-20 08:26:30 +0200228 s1 = 1 - side
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200229 src_if = self.ifs[s0]
230 dst_if = self.ifs[s1]
231 layer_3 = [IP(src=src_if.remote_ip4, dst=dst_if.remote_ip4),
232 IPv6(src=src_if.remote_ip6, dst=dst_if.remote_ip6)]
233 merged_l4args = {'sport': self.ports[s0], 'dport': self.ports[s1]}
234 merged_l4args.update(l4args)
235 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
236 layer_3[is_ip6] /
237 self.l4proto(**merged_l4args) /
238 Raw(payload))
239 return p
240
241 def send(self, side, flags=None, payload=""):
242 l4args = {}
243 if flags is not None:
244 l4args['flags'] = flags
245 self.ifs[side].add_stream(self.pkt(side,
246 l4args=l4args, payload=payload))
Klement Sekera75e7d132017-09-20 08:26:30 +0200247 self.ifs[1 - side].enable_capture()
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200248 self.testcase.pg_start()
249
250 def recv(self, side):
251 p = self.ifs[side].wait_for_packet(1)
252 return p
253
254 def send_through(self, side, flags=None, payload=""):
255 self.send(side, flags, payload)
Klement Sekera75e7d132017-09-20 08:26:30 +0200256 p = self.recv(1 - side)
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200257 return p
258
259 def send_pingpong(self, side, flags1=None, flags2=None):
260 p1 = self.send_through(side, flags1)
Klement Sekera75e7d132017-09-20 08:26:30 +0200261 p2 = self.send_through(1 - side, flags2)
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200262 return [p1, p2]
263
264
265class L4_CONN_SIDE:
266 L4_CONN_SIDE_ZERO = 0
267 L4_CONN_SIDE_ONE = 1
Klement Sekera75e7d132017-09-20 08:26:30 +0200268
269
Paul Vinciguerra9beabd82019-12-01 22:24:28 -0500270def fragment_rfc791(packet, fragsize, logger=null_logger):
Klement Sekera75e7d132017-09-20 08:26:30 +0200271 """
272 Fragment an IPv4 packet per RFC 791
273 :param packet: packet to fragment
274 :param fragsize: size at which to fragment
275 :note: IP options are not supported
276 :returns: list of fragments
277 """
Klement Sekera75e7d132017-09-20 08:26:30 +0200278 logger.debug(ppp("Fragmenting packet:", packet))
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700279 packet = packet.__class__(scapy.compat.raw(packet)) # recalc. all values
Klement Sekera75e7d132017-09-20 08:26:30 +0200280 if len(packet[IP].options) > 0:
281 raise Exception("Not implemented")
282 if len(packet) <= fragsize:
283 return [packet]
284
285 pre_ip_len = len(packet) - len(packet[IP])
286 ip_header_len = packet[IP].ihl * 4
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700287 hex_packet = scapy.compat.raw(packet)
Klement Sekera75e7d132017-09-20 08:26:30 +0200288 hex_headers = hex_packet[:(pre_ip_len + ip_header_len)]
289 hex_payload = hex_packet[(pre_ip_len + ip_header_len):]
290
291 pkts = []
292 ihl = packet[IP].ihl
293 otl = len(packet[IP])
snaramre3030bea2019-10-17 16:39:03 +0000294 nfb = int((fragsize - pre_ip_len - ihl * 4) / 8)
Klement Sekera75e7d132017-09-20 08:26:30 +0200295 fo = packet[IP].frag
296
297 p = packet.__class__(hex_headers + hex_payload[:nfb * 8])
298 p[IP].flags = "MF"
299 p[IP].frag = fo
300 p[IP].len = ihl * 4 + nfb * 8
301 del p[IP].chksum
302 pkts.append(p)
303
304 p = packet.__class__(hex_headers + hex_payload[nfb * 8:])
305 p[IP].len = otl - nfb * 8
306 p[IP].frag = fo + nfb
307 del p[IP].chksum
308
Paul Vinciguerra9beabd82019-12-01 22:24:28 -0500309 more_fragments = fragment_rfc791(p, fragsize, logger)
Klement Sekera75e7d132017-09-20 08:26:30 +0200310 pkts.extend(more_fragments)
311
312 return pkts
313
314
Paul Vinciguerra9beabd82019-12-01 22:24:28 -0500315def fragment_rfc8200(packet, identification, fragsize, logger=null_logger):
Klement Sekera75e7d132017-09-20 08:26:30 +0200316 """
317 Fragment an IPv6 packet per RFC 8200
318 :param packet: packet to fragment
319 :param fragsize: size at which to fragment
320 :note: IP options are not supported
321 :returns: list of fragments
322 """
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700323 packet = packet.__class__(scapy.compat.raw(packet)) # recalc. all values
Klement Sekera75e7d132017-09-20 08:26:30 +0200324 if len(packet) <= fragsize:
325 return [packet]
326 logger.debug(ppp("Fragmenting packet:", packet))
327 pkts = []
328 counter = 0
329 routing_hdr = None
330 hop_by_hop_hdr = None
331 upper_layer = None
332 seen_ipv6 = False
333 ipv6_nr = -1
334 l = packet.getlayer(counter)
335 while l is not None:
336 if l.__class__ is IPv6:
337 if seen_ipv6:
338 # ignore 2nd IPv6 header and everything below..
339 break
340 ipv6_nr = counter
341 seen_ipv6 = True
342 elif l.__class__ is IPv6ExtHdrFragment:
343 raise Exception("Already fragmented")
344 elif l.__class__ is IPv6ExtHdrRouting:
345 routing_hdr = counter
346 elif l.__class__ is IPv6ExtHdrHopByHop:
347 hop_by_hop_hdr = counter
348 elif seen_ipv6 and not upper_layer and \
349 not l.__class__.__name__.startswith('IPv6ExtHdr'):
350 upper_layer = counter
351 counter = counter + 1
352 l = packet.getlayer(counter)
353
354 logger.debug(
355 "Layers seen: IPv6(#%s), Routing(#%s), HopByHop(#%s), upper(#%s)" %
356 (ipv6_nr, routing_hdr, hop_by_hop_hdr, upper_layer))
357
358 if upper_layer is None:
359 raise Exception("Upper layer header not found in IPv6 packet")
360
361 last_per_fragment_hdr = ipv6_nr
362 if routing_hdr is None:
363 if hop_by_hop_hdr is not None:
364 last_per_fragment_hdr = hop_by_hop_hdr
365 else:
366 last_per_fragment_hdr = routing_hdr
367 logger.debug("Last per-fragment hdr is #%s" % (last_per_fragment_hdr))
368
369 per_fragment_headers = packet.copy()
370 per_fragment_headers[last_per_fragment_hdr].remove_payload()
371 logger.debug(ppp("Per-fragment headers:", per_fragment_headers))
372
373 ext_and_upper_layer = packet.getlayer(last_per_fragment_hdr)[1]
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700374 hex_payload = scapy.compat.raw(ext_and_upper_layer)
Klement Sekera75e7d132017-09-20 08:26:30 +0200375 logger.debug("Payload length is %s" % len(hex_payload))
376 logger.debug(ppp("Ext and upper layer:", ext_and_upper_layer))
377
378 fragment_ext_hdr = IPv6ExtHdrFragment()
379 logger.debug(ppp("Fragment header:", fragment_ext_hdr))
380
Neale Ranns14046982019-07-29 14:49:52 +0000381 len_ext_and_upper_layer_payload = len(ext_and_upper_layer.payload)
382 if not len_ext_and_upper_layer_payload and \
383 hasattr(ext_and_upper_layer, "data"):
384 len_ext_and_upper_layer_payload = len(ext_and_upper_layer.data)
385
Klement Sekera75e7d132017-09-20 08:26:30 +0200386 if len(per_fragment_headers) + len(fragment_ext_hdr) +\
Neale Ranns14046982019-07-29 14:49:52 +0000387 len(ext_and_upper_layer) - len_ext_and_upper_layer_payload\
Klement Sekera75e7d132017-09-20 08:26:30 +0200388 > fragsize:
389 raise Exception("Cannot fragment this packet - MTU too small "
390 "(%s, %s, %s, %s, %s)" % (
391 len(per_fragment_headers), len(fragment_ext_hdr),
392 len(ext_and_upper_layer),
Neale Ranns14046982019-07-29 14:49:52 +0000393 len_ext_and_upper_layer_payload, fragsize))
Klement Sekera75e7d132017-09-20 08:26:30 +0200394
395 orig_nh = packet[IPv6].nh
396 p = per_fragment_headers
397 del p[IPv6].plen
398 del p[IPv6].nh
399 p = p / fragment_ext_hdr
400 del p[IPv6ExtHdrFragment].nh
snaramre3030bea2019-10-17 16:39:03 +0000401 first_payload_len_nfb = int((fragsize - len(p)) / 8)
Klement Sekera75e7d132017-09-20 08:26:30 +0200402 p = p / Raw(hex_payload[:first_payload_len_nfb * 8])
403 del p[IPv6].plen
404 p[IPv6ExtHdrFragment].nh = orig_nh
405 p[IPv6ExtHdrFragment].id = identification
406 p[IPv6ExtHdrFragment].offset = 0
407 p[IPv6ExtHdrFragment].m = 1
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700408 p = p.__class__(scapy.compat.raw(p))
Klement Sekera75e7d132017-09-20 08:26:30 +0200409 logger.debug(ppp("Fragment %s:" % len(pkts), p))
410 pkts.append(p)
411 offset = first_payload_len_nfb * 8
412 logger.debug("Offset after first fragment: %s" % offset)
413 while len(hex_payload) > offset:
414 p = per_fragment_headers
415 del p[IPv6].plen
416 del p[IPv6].nh
417 p = p / fragment_ext_hdr
418 del p[IPv6ExtHdrFragment].nh
snaramre3030bea2019-10-17 16:39:03 +0000419 l_nfb = int((fragsize - len(p)) / 8)
Klement Sekera75e7d132017-09-20 08:26:30 +0200420 p = p / Raw(hex_payload[offset:offset + l_nfb * 8])
421 p[IPv6ExtHdrFragment].nh = orig_nh
422 p[IPv6ExtHdrFragment].id = identification
snaramre3030bea2019-10-17 16:39:03 +0000423 p[IPv6ExtHdrFragment].offset = int(offset / 8)
Klement Sekera75e7d132017-09-20 08:26:30 +0200424 p[IPv6ExtHdrFragment].m = 1
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700425 p = p.__class__(scapy.compat.raw(p))
Klement Sekera75e7d132017-09-20 08:26:30 +0200426 logger.debug(ppp("Fragment %s:" % len(pkts), p))
427 pkts.append(p)
428 offset = offset + l_nfb * 8
429
430 pkts[-1][IPv6ExtHdrFragment].m = 0 # reset more-flags in last fragment
431
432 return pkts
Ole Troan7f991832018-12-06 17:35:12 +0100433
434
435def reassemble4_core(listoffragments, return_ip):
436 buffer = BytesIO()
437 first = listoffragments[0]
438 buffer.seek(20)
439 for pkt in listoffragments:
440 buffer.seek(pkt[IP].frag*8)
441 buffer.write(bytes(pkt[IP].payload))
442 first.len = len(buffer.getvalue()) + 20
443 first.flags = 0
444 del(first.chksum)
445 if return_ip:
446 header = bytes(first[IP])[:20]
447 return first[IP].__class__(header + buffer.getvalue())
448 else:
449 header = bytes(first[Ether])[:34]
450 return first[Ether].__class__(header + buffer.getvalue())
451
452
453def reassemble4_ether(listoffragments):
454 return reassemble4_core(listoffragments, False)
455
456
457def reassemble4(listoffragments):
458 return reassemble4_core(listoffragments, True)
Klement Sekeraad3187f2022-02-18 10:34:35 +0000459
460
Klement Sekera26cd0242022-02-18 10:35:08 +0000461class UnexpectedPacketError(Exception):
462 def __init__(self, packet, msg=""):
463 self.packet = packet
464 self.msg = msg
465
466 def __str__(self):
467 return f"\nUnexpected packet:\n{pr(self.packet)}{self.msg}"
468
469
Klement Sekeraad3187f2022-02-18 10:34:35 +0000470def recursive_dict_merge(dict_base, dict_update):
471 """Recursively merge base dict with update dict, return merged dict"""
472 for key in dict_update:
473 if key in dict_base:
474 if type(dict_update[key]) is dict:
475 dict_base[key] = recursive_dict_merge(dict_base[key],
476 dict_update[key])
477 else:
478 dict_base[key] = dict_update[key]
479 else:
480 dict_base[key] = dict_update[key]
481 return dict_base
482
483
Klement Sekera26cd0242022-02-18 10:35:08 +0000484class StatsDiff(UserDict):
Klement Sekeraad3187f2022-02-18 10:34:35 +0000485 """
486 Diff dictionary is a dictionary of dictionaries of interesting stats:
487
488 diff_dictionary =
489 {
490 "err" : { '/error/counter1' : 4, },
491 sw_if_index1 : { '/stat/segment/counter1' : 5,
492 '/stat/segment/counter2' : 6,
493 },
494 sw_if_index2 : { '/stat/segment/counter1' : 7,
495 },
496 }
497
498 It describes a per sw-if-index diffset, where each key is stat segment
499 path and value is the expected change for that counter for sw-if-index.
500 Special case string "err" is used for error counters, which are not per
501 sw-if-index.
502 """
503
Klement Sekera26cd0242022-02-18 10:35:08 +0000504 __slots__ = () # prevent setting properties to act like a dictionary
Klement Sekeraad3187f2022-02-18 10:34:35 +0000505
Klement Sekera26cd0242022-02-18 10:35:08 +0000506 def __init__(self, data):
507 super().__init__(data)
Klement Sekeraad3187f2022-02-18 10:34:35 +0000508
509 def __or__(self, other):
Klement Sekera26cd0242022-02-18 10:35:08 +0000510 return recursive_dict_merge(deepcopy(self.data), other)