blob: 1ab5c1f2350213d25579a145e3f32536f9ac0e9a [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
Klement Sekera7bb873a2016-11-18 07:38:42 +01007from cStringIO import StringIO
Neale Ranns2bc94022018-02-25 12:27:18 -08008from scapy.utils6 import in6_mactoifaceid
Klement Sekera7bb873a2016-11-18 07:38:42 +01009
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +020010from scapy.layers.l2 import Ether
11from scapy.packet import Raw
Klement Sekera75e7d132017-09-20 08:26:30 +020012from scapy.layers.inet import IP
13from scapy.layers.inet6 import IPv6, IPv6ExtHdrFragment, IPv6ExtHdrRouting,\
14 IPv6ExtHdrHopByHop
Klement Sekera611864f2018-09-26 11:19:00 +020015from scapy.utils import hexdump
Klement Sekera75e7d132017-09-20 08:26:30 +020016from socket import AF_INET6
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +020017
Klement Sekera7bb873a2016-11-18 07:38:42 +010018
19def ppp(headline, packet):
20 """ Return string containing the output of scapy packet.show() call. """
21 o = StringIO()
22 old_stdout = sys.stdout
23 sys.stdout = o
24 print(headline)
Klement Sekera611864f2018-09-26 11:19:00 +020025 hexdump(packet)
26 print("")
Klement Sekera7bb873a2016-11-18 07:38:42 +010027 packet.show()
28 sys.stdout = old_stdout
29 return o.getvalue()
30
Damjan Marionf56b77a2016-10-03 19:44:57 +020031
Klement Sekera9225dee2016-12-12 08:36:58 +010032def ppc(headline, capture, limit=10):
33 """ Return string containing ppp() printout for a capture.
34
35 :param headline: printed as first line of output
36 :param capture: packets to print
37 :param limit: limit the print to # of packets
38 """
39 if not capture:
40 return headline
Klement Sekeradab231a2016-12-21 08:50:14 +010041 tail = ""
Klement Sekera9225dee2016-12-12 08:36:58 +010042 if limit < len(capture):
Klement Sekeradab231a2016-12-21 08:50:14 +010043 tail = "\nPrint limit reached, %s out of %s packets printed" % (
Klement Sekera75e7d132017-09-20 08:26:30 +020044 limit, len(capture))
Klement Sekeradab231a2016-12-21 08:50:14 +010045 body = "".join([ppp("Packet #%s:" % count, p)
46 for count, p in zip(range(0, limit), capture)])
47 return "%s\n%s%s" % (headline, body, tail)
Klement Sekera9225dee2016-12-12 08:36:58 +010048
49
Eyal Barid81da8c2017-01-11 13:39:54 +020050def ip4_range(ip4, s, e):
51 tmp = ip4.rsplit('.', 1)[0]
52 return ("%s.%d" % (tmp, i) for i in range(s, e))
53
54
55def ip4n_range(ip4n, s, e):
56 ip4 = socket.inet_ntop(socket.AF_INET, ip4n)
Klement Sekera72715ee2017-01-17 10:37:05 +010057 return (socket.inet_pton(socket.AF_INET, ip)
58 for ip in ip4_range(ip4, s, e))
Eyal Barid81da8c2017-01-11 13:39:54 +020059
60
Neale Ranns39f9d8b2017-02-16 21:57:05 -080061def mactobinary(mac):
62 """ Convert the : separated format into binary packet data for the API """
63 return mac.replace(':', '').decode('hex')
64
65
Neale Ranns2a3ea492017-04-19 05:24:40 -070066def mk_ll_addr(mac):
67 euid = in6_mactoifaceid(mac)
68 addr = "fe80::" + euid
69 return addr
70
71
Juraj Sloboda4b9669d2018-01-15 10:39:21 +010072def ip6_normalize(ip6):
73 return socket.inet_ntop(socket.AF_INET6,
74 socket.inet_pton(socket.AF_INET6, ip6))
75
76
juraj.linkes40dd73b2018-09-21 13:55:16 +020077def get_core_path(tempdir):
78 return "%s/%s" % (tempdir, get_core_pattern())
79
80
81def is_core_present(tempdir):
82 return os.path.isfile(get_core_path(tempdir))
83
84
85def get_core_pattern():
Andrew Yourtchenko57612eb2018-03-28 15:32:10 +020086 with open("/proc/sys/kernel/core_pattern", "r") as f:
juraj.linkes40dd73b2018-09-21 13:55:16 +020087 corefmt = f.read().strip()
88 return corefmt
89
90
91def check_core_path(logger, core_path):
92 corefmt = get_core_pattern()
93 if corefmt.startswith("|"):
94 logger.error(
95 "WARNING: redirecting the core dump through a"
96 " filter may result in truncated dumps.")
97 logger.error(
98 " You may want to check the filter settings"
99 " or uninstall it and edit the"
100 " /proc/sys/kernel/core_pattern accordingly.")
101 logger.error(
102 " current core pattern is: %s" % corefmt)
Andrew Yourtchenko57612eb2018-03-28 15:32:10 +0200103
104
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200105class NumericConstant(object):
106 __metaclass__ = ABCMeta
107
108 desc_dict = {}
109
110 @abstractmethod
111 def __init__(self, value):
112 self._value = value
113
114 def __int__(self):
115 return self._value
116
117 def __long__(self):
118 return self._value
119
120 def __str__(self):
121 if self._value in self.desc_dict:
122 return self.desc_dict[self._value]
123 return ""
124
125
Matej Klotton0178d522016-11-04 11:11:44 +0100126class Host(object):
127 """ Generic test host "connected" to VPPs interface. """
Damjan Marionf56b77a2016-10-03 19:44:57 +0200128
Klement Sekeraf62ae122016-10-11 11:47:09 +0200129 @property
130 def mac(self):
131 """ MAC address """
132 return self._mac
Damjan Marionf56b77a2016-10-03 19:44:57 +0200133
Klement Sekeraf62ae122016-10-11 11:47:09 +0200134 @property
Eyal Baric86e5922017-07-02 18:33:16 +0300135 def bin_mac(self):
136 """ MAC address """
137 return mactobinary(self._mac)
138
139 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +0200140 def ip4(self):
Klement Sekera46a87ad2017-01-02 08:22:23 +0100141 """ IPv4 address - string """
Klement Sekeraf62ae122016-10-11 11:47:09 +0200142 return self._ip4
Damjan Marionf56b77a2016-10-03 19:44:57 +0200143
Klement Sekeraf62ae122016-10-11 11:47:09 +0200144 @property
Matej Klotton0178d522016-11-04 11:11:44 +0100145 def ip4n(self):
Klement Sekera46a87ad2017-01-02 08:22:23 +0100146 """ IPv4 address of remote host - raw, suitable as API parameter."""
Matej Klotton0178d522016-11-04 11:11:44 +0100147 return socket.inet_pton(socket.AF_INET, self._ip4)
148
149 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +0200150 def ip6(self):
Klement Sekera46a87ad2017-01-02 08:22:23 +0100151 """ IPv6 address - string """
Klement Sekeraf62ae122016-10-11 11:47:09 +0200152 return self._ip6
Damjan Marionf56b77a2016-10-03 19:44:57 +0200153
Klement Sekera46a87ad2017-01-02 08:22:23 +0100154 @property
155 def ip6n(self):
156 """ IPv6 address of remote host - raw, suitable as API parameter."""
157 return socket.inet_pton(socket.AF_INET6, self._ip6)
158
Neale Ranns2a3ea492017-04-19 05:24:40 -0700159 @property
160 def ip6_ll(self):
161 """ IPv6 link-local address - string """
162 return self._ip6_ll
163
164 @property
165 def ip6n_ll(self):
166 """ IPv6 link-local address of remote host -
167 raw, suitable as API parameter."""
168 return socket.inet_pton(socket.AF_INET6, self._ip6_ll)
169
Eyal Baric86e5922017-07-02 18:33:16 +0300170 def __eq__(self, other):
171 if isinstance(other, Host):
172 return (self.mac == other.mac and
173 self.ip4 == other.ip4 and
174 self.ip6 == other.ip6 and
175 self.ip6_ll == other.ip6_ll)
176 else:
177 return False
178
179 def __ne__(self, other):
180 return not self.__eq__(other)
181
182 def __repr__(self):
183 return "Host { mac:%s ip4:%s ip6:%s ip6_ll:%s }" % (self.mac,
184 self.ip4,
185 self.ip6,
186 self.ip6_ll)
187
188 def __hash__(self):
189 return hash(self.__repr__())
190
Neale Ranns2a3ea492017-04-19 05:24:40 -0700191 def __init__(self, mac=None, ip4=None, ip6=None, ip6_ll=None):
Klement Sekeraf62ae122016-10-11 11:47:09 +0200192 self._mac = mac
193 self._ip4 = ip4
194 self._ip6 = ip6
Neale Ranns2a3ea492017-04-19 05:24:40 -0700195 self._ip6_ll = ip6_ll
Filip Tehlar770e89e2017-01-31 10:39:16 +0100196
197
198class ForeignAddressFactory(object):
199 count = 0
200 prefix_len = 24
201 net_template = '10.10.10.{}'
202 net = net_template.format(0) + '/' + str(prefix_len)
203
204 def get_ip4(self):
205 if self.count > 255:
206 raise Exception("Network host address exhaustion")
207 self.count += 1
208 return self.net_template.format(self.count)
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200209
210
211class L4_Conn():
212 """ L4 'connection' tied to two VPP interfaces """
Klement Sekera75e7d132017-09-20 08:26:30 +0200213
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200214 def __init__(self, testcase, if1, if2, af, l4proto, port1, port2):
215 self.testcase = testcase
216 self.ifs = [None, None]
217 self.ifs[0] = if1
218 self.ifs[1] = if2
219 self.address_family = af
220 self.l4proto = l4proto
221 self.ports = [None, None]
222 self.ports[0] = port1
223 self.ports[1] = port2
224 self
225
226 def pkt(self, side, l4args={}, payload="x"):
227 is_ip6 = 1 if self.address_family == AF_INET6 else 0
228 s0 = side
Klement Sekera75e7d132017-09-20 08:26:30 +0200229 s1 = 1 - side
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200230 src_if = self.ifs[s0]
231 dst_if = self.ifs[s1]
232 layer_3 = [IP(src=src_if.remote_ip4, dst=dst_if.remote_ip4),
233 IPv6(src=src_if.remote_ip6, dst=dst_if.remote_ip6)]
234 merged_l4args = {'sport': self.ports[s0], 'dport': self.ports[s1]}
235 merged_l4args.update(l4args)
236 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
237 layer_3[is_ip6] /
238 self.l4proto(**merged_l4args) /
239 Raw(payload))
240 return p
241
242 def send(self, side, flags=None, payload=""):
243 l4args = {}
244 if flags is not None:
245 l4args['flags'] = flags
246 self.ifs[side].add_stream(self.pkt(side,
247 l4args=l4args, payload=payload))
Klement Sekera75e7d132017-09-20 08:26:30 +0200248 self.ifs[1 - side].enable_capture()
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200249 self.testcase.pg_start()
250
251 def recv(self, side):
252 p = self.ifs[side].wait_for_packet(1)
253 return p
254
255 def send_through(self, side, flags=None, payload=""):
256 self.send(side, flags, payload)
Klement Sekera75e7d132017-09-20 08:26:30 +0200257 p = self.recv(1 - side)
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200258 return p
259
260 def send_pingpong(self, side, flags1=None, flags2=None):
261 p1 = self.send_through(side, flags1)
Klement Sekera75e7d132017-09-20 08:26:30 +0200262 p2 = self.send_through(1 - side, flags2)
Andrew Yourtchenko92dc12a2017-09-07 13:22:24 +0200263 return [p1, p2]
264
265
266class L4_CONN_SIDE:
267 L4_CONN_SIDE_ZERO = 0
268 L4_CONN_SIDE_ONE = 1
Klement Sekera75e7d132017-09-20 08:26:30 +0200269
270
271class LoggerWrapper(object):
272 def __init__(self, logger=None):
273 self._logger = logger
274
275 def debug(self, *args, **kwargs):
276 if self._logger:
277 self._logger.debug(*args, **kwargs)
278
279 def error(self, *args, **kwargs):
280 if self._logger:
281 self._logger.error(*args, **kwargs)
282
283
284def fragment_rfc791(packet, fragsize, _logger=None):
285 """
286 Fragment an IPv4 packet per RFC 791
287 :param packet: packet to fragment
288 :param fragsize: size at which to fragment
289 :note: IP options are not supported
290 :returns: list of fragments
291 """
292 logger = LoggerWrapper(_logger)
293 logger.debug(ppp("Fragmenting packet:", packet))
294 packet = packet.__class__(str(packet)) # recalculate all values
295 if len(packet[IP].options) > 0:
296 raise Exception("Not implemented")
297 if len(packet) <= fragsize:
298 return [packet]
299
300 pre_ip_len = len(packet) - len(packet[IP])
301 ip_header_len = packet[IP].ihl * 4
302 hex_packet = str(packet)
303 hex_headers = hex_packet[:(pre_ip_len + ip_header_len)]
304 hex_payload = hex_packet[(pre_ip_len + ip_header_len):]
305
306 pkts = []
307 ihl = packet[IP].ihl
308 otl = len(packet[IP])
309 nfb = (fragsize - pre_ip_len - ihl * 4) / 8
310 fo = packet[IP].frag
311
312 p = packet.__class__(hex_headers + hex_payload[:nfb * 8])
313 p[IP].flags = "MF"
314 p[IP].frag = fo
315 p[IP].len = ihl * 4 + nfb * 8
316 del p[IP].chksum
317 pkts.append(p)
318
319 p = packet.__class__(hex_headers + hex_payload[nfb * 8:])
320 p[IP].len = otl - nfb * 8
321 p[IP].frag = fo + nfb
322 del p[IP].chksum
323
324 more_fragments = fragment_rfc791(p, fragsize, _logger)
325 pkts.extend(more_fragments)
326
327 return pkts
328
329
330def fragment_rfc8200(packet, identification, fragsize, _logger=None):
331 """
332 Fragment an IPv6 packet per RFC 8200
333 :param packet: packet to fragment
334 :param fragsize: size at which to fragment
335 :note: IP options are not supported
336 :returns: list of fragments
337 """
338 logger = LoggerWrapper(_logger)
339 packet = packet.__class__(str(packet)) # recalculate all values
340 if len(packet) <= fragsize:
341 return [packet]
342 logger.debug(ppp("Fragmenting packet:", packet))
343 pkts = []
344 counter = 0
345 routing_hdr = None
346 hop_by_hop_hdr = None
347 upper_layer = None
348 seen_ipv6 = False
349 ipv6_nr = -1
350 l = packet.getlayer(counter)
351 while l is not None:
352 if l.__class__ is IPv6:
353 if seen_ipv6:
354 # ignore 2nd IPv6 header and everything below..
355 break
356 ipv6_nr = counter
357 seen_ipv6 = True
358 elif l.__class__ is IPv6ExtHdrFragment:
359 raise Exception("Already fragmented")
360 elif l.__class__ is IPv6ExtHdrRouting:
361 routing_hdr = counter
362 elif l.__class__ is IPv6ExtHdrHopByHop:
363 hop_by_hop_hdr = counter
364 elif seen_ipv6 and not upper_layer and \
365 not l.__class__.__name__.startswith('IPv6ExtHdr'):
366 upper_layer = counter
367 counter = counter + 1
368 l = packet.getlayer(counter)
369
370 logger.debug(
371 "Layers seen: IPv6(#%s), Routing(#%s), HopByHop(#%s), upper(#%s)" %
372 (ipv6_nr, routing_hdr, hop_by_hop_hdr, upper_layer))
373
374 if upper_layer is None:
375 raise Exception("Upper layer header not found in IPv6 packet")
376
377 last_per_fragment_hdr = ipv6_nr
378 if routing_hdr is None:
379 if hop_by_hop_hdr is not None:
380 last_per_fragment_hdr = hop_by_hop_hdr
381 else:
382 last_per_fragment_hdr = routing_hdr
383 logger.debug("Last per-fragment hdr is #%s" % (last_per_fragment_hdr))
384
385 per_fragment_headers = packet.copy()
386 per_fragment_headers[last_per_fragment_hdr].remove_payload()
387 logger.debug(ppp("Per-fragment headers:", per_fragment_headers))
388
389 ext_and_upper_layer = packet.getlayer(last_per_fragment_hdr)[1]
390 hex_payload = str(ext_and_upper_layer)
391 logger.debug("Payload length is %s" % len(hex_payload))
392 logger.debug(ppp("Ext and upper layer:", ext_and_upper_layer))
393
394 fragment_ext_hdr = IPv6ExtHdrFragment()
395 logger.debug(ppp("Fragment header:", fragment_ext_hdr))
396
397 if len(per_fragment_headers) + len(fragment_ext_hdr) +\
398 len(ext_and_upper_layer) - len(ext_and_upper_layer.payload)\
399 > fragsize:
400 raise Exception("Cannot fragment this packet - MTU too small "
401 "(%s, %s, %s, %s, %s)" % (
402 len(per_fragment_headers), len(fragment_ext_hdr),
403 len(ext_and_upper_layer),
404 len(ext_and_upper_layer.payload), fragsize))
405
406 orig_nh = packet[IPv6].nh
407 p = per_fragment_headers
408 del p[IPv6].plen
409 del p[IPv6].nh
410 p = p / fragment_ext_hdr
411 del p[IPv6ExtHdrFragment].nh
412 first_payload_len_nfb = (fragsize - len(p)) / 8
413 p = p / Raw(hex_payload[:first_payload_len_nfb * 8])
414 del p[IPv6].plen
415 p[IPv6ExtHdrFragment].nh = orig_nh
416 p[IPv6ExtHdrFragment].id = identification
417 p[IPv6ExtHdrFragment].offset = 0
418 p[IPv6ExtHdrFragment].m = 1
419 p = p.__class__(str(p))
420 logger.debug(ppp("Fragment %s:" % len(pkts), p))
421 pkts.append(p)
422 offset = first_payload_len_nfb * 8
423 logger.debug("Offset after first fragment: %s" % offset)
424 while len(hex_payload) > offset:
425 p = per_fragment_headers
426 del p[IPv6].plen
427 del p[IPv6].nh
428 p = p / fragment_ext_hdr
429 del p[IPv6ExtHdrFragment].nh
430 l_nfb = (fragsize - len(p)) / 8
431 p = p / Raw(hex_payload[offset:offset + l_nfb * 8])
432 p[IPv6ExtHdrFragment].nh = orig_nh
433 p[IPv6ExtHdrFragment].id = identification
434 p[IPv6ExtHdrFragment].offset = offset / 8
435 p[IPv6ExtHdrFragment].m = 1
436 p = p.__class__(str(p))
437 logger.debug(ppp("Fragment %s:" % len(pkts), p))
438 pkts.append(p)
439 offset = offset + l_nfb * 8
440
441 pkts[-1][IPv6ExtHdrFragment].m = 0 # reset more-flags in last fragment
442
443 return pkts