blob: 9652b803f1dfcba106048c546bb869cecbce0cec [file] [log] [blame]
Klement Sekeraacb9b8e2017-02-14 02:55:31 +01001""" test framework utilities """
2
Matej Klotton0178d522016-11-04 11:11:44 +01003import socket
Klement Sekera7bb873a2016-11-18 07:38:42 +01004import sys
juraj.linkes40dd73b2018-09-21 13:55:16 +02005import os.path
Klement Sekera0e3c0de2016-09-29 14:43:44 +02006from abc import abstractmethod, ABCMeta
Neale Ranns2bc94022018-02-25 12:27:18 -08007from scapy.utils6 import in6_mactoifaceid
Klement Sekera7bb873a2016-11-18 07:38:42 +01008
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +02009from scapy.layers.l2 import Ether
10from scapy.packet import Raw
Klement Sekera75e7d132017-09-20 08:26:30 +020011from scapy.layers.inet import IP
12from scapy.layers.inet6 import IPv6, IPv6ExtHdrFragment, IPv6ExtHdrRouting,\
13 IPv6ExtHdrHopByHop
Klement Sekera611864f2018-09-26 11:19:00 +020014from scapy.utils import hexdump
Klement Sekera75e7d132017-09-20 08:26:30 +020015from socket import AF_INET6
Ole Troan7f991832018-12-06 17:35:12 +010016from io import BytesIO
Ole Troan8006c6a2018-12-17 12:02:26 +010017from vpp_papi import mac_pton
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +020018
Klement Sekera7bb873a2016-11-18 07:38:42 +010019
20def ppp(headline, packet):
21 """ Return string containing the output of scapy packet.show() call. """
Ole Troan7f991832018-12-06 17:35:12 +010022 o = BytesIO()
Klement Sekera7bb873a2016-11-18 07:38:42 +010023 old_stdout = sys.stdout
24 sys.stdout = o
25 print(headline)
Klement Sekera611864f2018-09-26 11:19:00 +020026 hexdump(packet)
27 print("")
Klement Sekera7bb873a2016-11-18 07:38:42 +010028 packet.show()
29 sys.stdout = old_stdout
30 return o.getvalue()
31
Damjan Marionf56b77a2016-10-03 19:44:57 +020032
Klement Sekera9225dee2016-12-12 08:36:58 +010033def ppc(headline, capture, limit=10):
34 """ Return string containing ppp() printout for a capture.
35
36 :param headline: printed as first line of output
37 :param capture: packets to print
38 :param limit: limit the print to # of packets
39 """
40 if not capture:
41 return headline
Klement Sekeradab231a2016-12-21 08:50:14 +010042 tail = ""
Klement Sekera9225dee2016-12-12 08:36:58 +010043 if limit < len(capture):
Klement Sekeradab231a2016-12-21 08:50:14 +010044 tail = "\nPrint limit reached, %s out of %s packets printed" % (
Klement Sekera75e7d132017-09-20 08:26:30 +020045 limit, len(capture))
Klement Sekeradab231a2016-12-21 08:50:14 +010046 body = "".join([ppp("Packet #%s:" % count, p)
47 for count, p in zip(range(0, limit), capture)])
48 return "%s\n%s%s" % (headline, body, tail)
Klement Sekera9225dee2016-12-12 08:36:58 +010049
50
Eyal Barid81da8c2017-01-11 13:39:54 +020051def ip4_range(ip4, s, e):
52 tmp = ip4.rsplit('.', 1)[0]
53 return ("%s.%d" % (tmp, i) for i in range(s, e))
54
55
56def ip4n_range(ip4n, s, e):
57 ip4 = socket.inet_ntop(socket.AF_INET, ip4n)
Klement Sekera72715ee2017-01-17 10:37:05 +010058 return (socket.inet_pton(socket.AF_INET, ip)
59 for ip in ip4_range(ip4, s, e))
Eyal Barid81da8c2017-01-11 13:39:54 +020060
61
Neale Ranns2a3ea492017-04-19 05:24:40 -070062def mk_ll_addr(mac):
63 euid = in6_mactoifaceid(mac)
64 addr = "fe80::" + euid
65 return addr
66
67
Juraj Sloboda4b9669d2018-01-15 10:39:21 +010068def ip6_normalize(ip6):
69 return socket.inet_ntop(socket.AF_INET6,
70 socket.inet_pton(socket.AF_INET6, ip6))
71
72
juraj.linkes40dd73b2018-09-21 13:55:16 +020073def get_core_path(tempdir):
74 return "%s/%s" % (tempdir, get_core_pattern())
75
76
77def is_core_present(tempdir):
78 return os.path.isfile(get_core_path(tempdir))
79
80
81def get_core_pattern():
Andrew Yourtchenko57612eb2018-03-28 15:32:10 +020082 with open("/proc/sys/kernel/core_pattern", "r") as f:
juraj.linkes40dd73b2018-09-21 13:55:16 +020083 corefmt = f.read().strip()
84 return corefmt
85
86
87def check_core_path(logger, core_path):
88 corefmt = get_core_pattern()
89 if corefmt.startswith("|"):
90 logger.error(
91 "WARNING: redirecting the core dump through a"
92 " filter may result in truncated dumps.")
93 logger.error(
94 " You may want to check the filter settings"
95 " or uninstall it and edit the"
96 " /proc/sys/kernel/core_pattern accordingly.")
97 logger.error(
98 " current core pattern is: %s" % corefmt)
Andrew Yourtchenko57612eb2018-03-28 15:32:10 +020099
100
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200101class NumericConstant(object):
102 __metaclass__ = ABCMeta
103
104 desc_dict = {}
105
106 @abstractmethod
107 def __init__(self, value):
108 self._value = value
109
110 def __int__(self):
111 return self._value
112
113 def __long__(self):
114 return self._value
115
116 def __str__(self):
117 if self._value in self.desc_dict:
118 return self.desc_dict[self._value]
119 return ""
120
121
Matej Klotton0178d522016-11-04 11:11:44 +0100122class Host(object):
123 """ Generic test host "connected" to VPPs interface. """
Damjan Marionf56b77a2016-10-03 19:44:57 +0200124
Klement Sekeraf62ae122016-10-11 11:47:09 +0200125 @property
126 def mac(self):
127 """ MAC address """
128 return self._mac
Damjan Marionf56b77a2016-10-03 19:44:57 +0200129
Klement Sekeraf62ae122016-10-11 11:47:09 +0200130 @property
Eyal Baric86e5922017-07-02 18:33:16 +0300131 def bin_mac(self):
132 """ MAC address """
Ole Troan8006c6a2018-12-17 12:02:26 +0100133 return mac_pton(self._mac)
Eyal Baric86e5922017-07-02 18:33:16 +0300134
135 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +0200136 def ip4(self):
Klement Sekera46a87ad2017-01-02 08:22:23 +0100137 """ IPv4 address - string """
Klement Sekeraf62ae122016-10-11 11:47:09 +0200138 return self._ip4
Damjan Marionf56b77a2016-10-03 19:44:57 +0200139
Klement Sekeraf62ae122016-10-11 11:47:09 +0200140 @property
Matej Klotton0178d522016-11-04 11:11:44 +0100141 def ip4n(self):
Klement Sekera46a87ad2017-01-02 08:22:23 +0100142 """ IPv4 address of remote host - raw, suitable as API parameter."""
Matej Klotton0178d522016-11-04 11:11:44 +0100143 return socket.inet_pton(socket.AF_INET, self._ip4)
144
145 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +0200146 def ip6(self):
Klement Sekera46a87ad2017-01-02 08:22:23 +0100147 """ IPv6 address - string """
Klement Sekeraf62ae122016-10-11 11:47:09 +0200148 return self._ip6
Damjan Marionf56b77a2016-10-03 19:44:57 +0200149
Klement Sekera46a87ad2017-01-02 08:22:23 +0100150 @property
151 def ip6n(self):
152 """ IPv6 address of remote host - raw, suitable as API parameter."""
153 return socket.inet_pton(socket.AF_INET6, self._ip6)
154
Neale Ranns2a3ea492017-04-19 05:24:40 -0700155 @property
156 def ip6_ll(self):
157 """ IPv6 link-local address - string """
158 return self._ip6_ll
159
160 @property
161 def ip6n_ll(self):
162 """ IPv6 link-local address of remote host -
163 raw, suitable as API parameter."""
164 return socket.inet_pton(socket.AF_INET6, self._ip6_ll)
165
Eyal Baric86e5922017-07-02 18:33:16 +0300166 def __eq__(self, other):
167 if isinstance(other, Host):
168 return (self.mac == other.mac and
169 self.ip4 == other.ip4 and
170 self.ip6 == other.ip6 and
171 self.ip6_ll == other.ip6_ll)
172 else:
173 return False
174
175 def __ne__(self, other):
176 return not self.__eq__(other)
177
178 def __repr__(self):
179 return "Host { mac:%s ip4:%s ip6:%s ip6_ll:%s }" % (self.mac,
180 self.ip4,
181 self.ip6,
182 self.ip6_ll)
183
184 def __hash__(self):
185 return hash(self.__repr__())
186
Neale Ranns2a3ea492017-04-19 05:24:40 -0700187 def __init__(self, mac=None, ip4=None, ip6=None, ip6_ll=None):
Klement Sekeraf62ae122016-10-11 11:47:09 +0200188 self._mac = mac
189 self._ip4 = ip4
190 self._ip6 = ip6
Neale Ranns2a3ea492017-04-19 05:24:40 -0700191 self._ip6_ll = ip6_ll
Filip Tehlar770e89e2017-01-31 10:39:16 +0100192
193
194class ForeignAddressFactory(object):
195 count = 0
196 prefix_len = 24
197 net_template = '10.10.10.{}'
198 net = net_template.format(0) + '/' + str(prefix_len)
199
200 def get_ip4(self):
201 if self.count > 255:
202 raise Exception("Network host address exhaustion")
203 self.count += 1
204 return self.net_template.format(self.count)
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200205
206
207class L4_Conn():
208 """ L4 'connection' tied to two VPP interfaces """
Klement Sekera75e7d132017-09-20 08:26:30 +0200209
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200210 def __init__(self, testcase, if1, if2, af, l4proto, port1, port2):
211 self.testcase = testcase
212 self.ifs = [None, None]
213 self.ifs[0] = if1
214 self.ifs[1] = if2
215 self.address_family = af
216 self.l4proto = l4proto
217 self.ports = [None, None]
218 self.ports[0] = port1
219 self.ports[1] = port2
220 self
221
222 def pkt(self, side, l4args={}, payload="x"):
223 is_ip6 = 1 if self.address_family == AF_INET6 else 0
224 s0 = side
Klement Sekera75e7d132017-09-20 08:26:30 +0200225 s1 = 1 - side
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200226 src_if = self.ifs[s0]
227 dst_if = self.ifs[s1]
228 layer_3 = [IP(src=src_if.remote_ip4, dst=dst_if.remote_ip4),
229 IPv6(src=src_if.remote_ip6, dst=dst_if.remote_ip6)]
230 merged_l4args = {'sport': self.ports[s0], 'dport': self.ports[s1]}
231 merged_l4args.update(l4args)
232 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
233 layer_3[is_ip6] /
234 self.l4proto(**merged_l4args) /
235 Raw(payload))
236 return p
237
238 def send(self, side, flags=None, payload=""):
239 l4args = {}
240 if flags is not None:
241 l4args['flags'] = flags
242 self.ifs[side].add_stream(self.pkt(side,
243 l4args=l4args, payload=payload))
Klement Sekera75e7d132017-09-20 08:26:30 +0200244 self.ifs[1 - side].enable_capture()
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200245 self.testcase.pg_start()
246
247 def recv(self, side):
248 p = self.ifs[side].wait_for_packet(1)
249 return p
250
251 def send_through(self, side, flags=None, payload=""):
252 self.send(side, flags, payload)
Klement Sekera75e7d132017-09-20 08:26:30 +0200253 p = self.recv(1 - side)
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200254 return p
255
256 def send_pingpong(self, side, flags1=None, flags2=None):
257 p1 = self.send_through(side, flags1)
Klement Sekera75e7d132017-09-20 08:26:30 +0200258 p2 = self.send_through(1 - side, flags2)
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200259 return [p1, p2]
260
261
262class L4_CONN_SIDE:
263 L4_CONN_SIDE_ZERO = 0
264 L4_CONN_SIDE_ONE = 1
Klement Sekera75e7d132017-09-20 08:26:30 +0200265
266
267class LoggerWrapper(object):
268 def __init__(self, logger=None):
269 self._logger = logger
270
271 def debug(self, *args, **kwargs):
272 if self._logger:
273 self._logger.debug(*args, **kwargs)
274
275 def error(self, *args, **kwargs):
276 if self._logger:
277 self._logger.error(*args, **kwargs)
278
279
280def fragment_rfc791(packet, fragsize, _logger=None):
281 """
282 Fragment an IPv4 packet per RFC 791
283 :param packet: packet to fragment
284 :param fragsize: size at which to fragment
285 :note: IP options are not supported
286 :returns: list of fragments
287 """
288 logger = LoggerWrapper(_logger)
289 logger.debug(ppp("Fragmenting packet:", packet))
290 packet = packet.__class__(str(packet)) # recalculate all values
291 if len(packet[IP].options) > 0:
292 raise Exception("Not implemented")
293 if len(packet) <= fragsize:
294 return [packet]
295
296 pre_ip_len = len(packet) - len(packet[IP])
297 ip_header_len = packet[IP].ihl * 4
298 hex_packet = str(packet)
299 hex_headers = hex_packet[:(pre_ip_len + ip_header_len)]
300 hex_payload = hex_packet[(pre_ip_len + ip_header_len):]
301
302 pkts = []
303 ihl = packet[IP].ihl
304 otl = len(packet[IP])
305 nfb = (fragsize - pre_ip_len - ihl * 4) / 8
306 fo = packet[IP].frag
307
308 p = packet.__class__(hex_headers + hex_payload[:nfb * 8])
309 p[IP].flags = "MF"
310 p[IP].frag = fo
311 p[IP].len = ihl * 4 + nfb * 8
312 del p[IP].chksum
313 pkts.append(p)
314
315 p = packet.__class__(hex_headers + hex_payload[nfb * 8:])
316 p[IP].len = otl - nfb * 8
317 p[IP].frag = fo + nfb
318 del p[IP].chksum
319
320 more_fragments = fragment_rfc791(p, fragsize, _logger)
321 pkts.extend(more_fragments)
322
323 return pkts
324
325
326def fragment_rfc8200(packet, identification, fragsize, _logger=None):
327 """
328 Fragment an IPv6 packet per RFC 8200
329 :param packet: packet to fragment
330 :param fragsize: size at which to fragment
331 :note: IP options are not supported
332 :returns: list of fragments
333 """
334 logger = LoggerWrapper(_logger)
335 packet = packet.__class__(str(packet)) # recalculate all values
336 if len(packet) <= fragsize:
337 return [packet]
338 logger.debug(ppp("Fragmenting packet:", packet))
339 pkts = []
340 counter = 0
341 routing_hdr = None
342 hop_by_hop_hdr = None
343 upper_layer = None
344 seen_ipv6 = False
345 ipv6_nr = -1
346 l = packet.getlayer(counter)
347 while l is not None:
348 if l.__class__ is IPv6:
349 if seen_ipv6:
350 # ignore 2nd IPv6 header and everything below..
351 break
352 ipv6_nr = counter
353 seen_ipv6 = True
354 elif l.__class__ is IPv6ExtHdrFragment:
355 raise Exception("Already fragmented")
356 elif l.__class__ is IPv6ExtHdrRouting:
357 routing_hdr = counter
358 elif l.__class__ is IPv6ExtHdrHopByHop:
359 hop_by_hop_hdr = counter
360 elif seen_ipv6 and not upper_layer and \
361 not l.__class__.__name__.startswith('IPv6ExtHdr'):
362 upper_layer = counter
363 counter = counter + 1
364 l = packet.getlayer(counter)
365
366 logger.debug(
367 "Layers seen: IPv6(#%s), Routing(#%s), HopByHop(#%s), upper(#%s)" %
368 (ipv6_nr, routing_hdr, hop_by_hop_hdr, upper_layer))
369
370 if upper_layer is None:
371 raise Exception("Upper layer header not found in IPv6 packet")
372
373 last_per_fragment_hdr = ipv6_nr
374 if routing_hdr is None:
375 if hop_by_hop_hdr is not None:
376 last_per_fragment_hdr = hop_by_hop_hdr
377 else:
378 last_per_fragment_hdr = routing_hdr
379 logger.debug("Last per-fragment hdr is #%s" % (last_per_fragment_hdr))
380
381 per_fragment_headers = packet.copy()
382 per_fragment_headers[last_per_fragment_hdr].remove_payload()
383 logger.debug(ppp("Per-fragment headers:", per_fragment_headers))
384
385 ext_and_upper_layer = packet.getlayer(last_per_fragment_hdr)[1]
386 hex_payload = str(ext_and_upper_layer)
387 logger.debug("Payload length is %s" % len(hex_payload))
388 logger.debug(ppp("Ext and upper layer:", ext_and_upper_layer))
389
390 fragment_ext_hdr = IPv6ExtHdrFragment()
391 logger.debug(ppp("Fragment header:", fragment_ext_hdr))
392
393 if len(per_fragment_headers) + len(fragment_ext_hdr) +\
394 len(ext_and_upper_layer) - len(ext_and_upper_layer.payload)\
395 > fragsize:
396 raise Exception("Cannot fragment this packet - MTU too small "
397 "(%s, %s, %s, %s, %s)" % (
398 len(per_fragment_headers), len(fragment_ext_hdr),
399 len(ext_and_upper_layer),
400 len(ext_and_upper_layer.payload), fragsize))
401
402 orig_nh = packet[IPv6].nh
403 p = per_fragment_headers
404 del p[IPv6].plen
405 del p[IPv6].nh
406 p = p / fragment_ext_hdr
407 del p[IPv6ExtHdrFragment].nh
408 first_payload_len_nfb = (fragsize - len(p)) / 8
409 p = p / Raw(hex_payload[:first_payload_len_nfb * 8])
410 del p[IPv6].plen
411 p[IPv6ExtHdrFragment].nh = orig_nh
412 p[IPv6ExtHdrFragment].id = identification
413 p[IPv6ExtHdrFragment].offset = 0
414 p[IPv6ExtHdrFragment].m = 1
415 p = p.__class__(str(p))
416 logger.debug(ppp("Fragment %s:" % len(pkts), p))
417 pkts.append(p)
418 offset = first_payload_len_nfb * 8
419 logger.debug("Offset after first fragment: %s" % offset)
420 while len(hex_payload) > offset:
421 p = per_fragment_headers
422 del p[IPv6].plen
423 del p[IPv6].nh
424 p = p / fragment_ext_hdr
425 del p[IPv6ExtHdrFragment].nh
426 l_nfb = (fragsize - len(p)) / 8
427 p = p / Raw(hex_payload[offset:offset + l_nfb * 8])
428 p[IPv6ExtHdrFragment].nh = orig_nh
429 p[IPv6ExtHdrFragment].id = identification
430 p[IPv6ExtHdrFragment].offset = offset / 8
431 p[IPv6ExtHdrFragment].m = 1
432 p = p.__class__(str(p))
433 logger.debug(ppp("Fragment %s:" % len(pkts), p))
434 pkts.append(p)
435 offset = offset + l_nfb * 8
436
437 pkts[-1][IPv6ExtHdrFragment].m = 0 # reset more-flags in last fragment
438
439 return pkts
Ole Troan7f991832018-12-06 17:35:12 +0100440
441
442def reassemble4_core(listoffragments, return_ip):
443 buffer = BytesIO()
444 first = listoffragments[0]
445 buffer.seek(20)
446 for pkt in listoffragments:
447 buffer.seek(pkt[IP].frag*8)
448 buffer.write(bytes(pkt[IP].payload))
449 first.len = len(buffer.getvalue()) + 20
450 first.flags = 0
451 del(first.chksum)
452 if return_ip:
453 header = bytes(first[IP])[:20]
454 return first[IP].__class__(header + buffer.getvalue())
455 else:
456 header = bytes(first[Ether])[:34]
457 return first[Ether].__class__(header + buffer.getvalue())
458
459
460def reassemble4_ether(listoffragments):
461 return reassemble4_core(listoffragments, False)
462
463
464def reassemble4(listoffragments):
465 return reassemble4_core(listoffragments, True)