1 """ test framework utilities """
5 from abc import abstractmethod, ABCMeta
6 from cStringIO import StringIO
7 from scapy.utils6 import in6_mactoifaceid
9 from scapy.layers.l2 import Ether
10 from scapy.packet import Raw
11 from scapy.layers.inet import IP
12 from scapy.layers.inet6 import IPv6, IPv6ExtHdrFragment, IPv6ExtHdrRouting,\
14 from socket import AF_INET6
17 def ppp(headline, packet):
18 """ Return string containing the output of scapy packet.show() call. """
20 old_stdout = sys.stdout
24 sys.stdout = old_stdout
28 def ppc(headline, capture, limit=10):
29 """ Return string containing ppp() printout for a capture.
31 :param headline: printed as first line of output
32 :param capture: packets to print
33 :param limit: limit the print to # of packets
38 if limit < len(capture):
39 tail = "\nPrint limit reached, %s out of %s packets printed" % (
41 body = "".join([ppp("Packet #%s:" % count, p)
42 for count, p in zip(range(0, limit), capture)])
43 return "%s\n%s%s" % (headline, body, tail)
46 def ip4_range(ip4, s, e):
47 tmp = ip4.rsplit('.', 1)[0]
48 return ("%s.%d" % (tmp, i) for i in range(s, e))
51 def ip4n_range(ip4n, s, e):
52 ip4 = socket.inet_ntop(socket.AF_INET, ip4n)
53 return (socket.inet_pton(socket.AF_INET, ip)
54 for ip in ip4_range(ip4, s, e))
58 """ Convert the : separated format into binary packet data for the API """
59 return mac.replace(':', '').decode('hex')
63 euid = in6_mactoifaceid(mac)
64 addr = "fe80::" + euid
68 def ip6_normalize(ip6):
69 return socket.inet_ntop(socket.AF_INET6,
70 socket.inet_pton(socket.AF_INET6, ip6))
73 def check_core_path(logger, core_path):
74 with open("/proc/sys/kernel/core_pattern", "r") as f:
76 if corefmt.startswith("|"):
78 "WARNING: redirecting the core dump through a"
79 " filter may result in truncated dumps.")
81 " You may want to check the filter settings"
82 " or uninstall it and edit the"
83 " /proc/sys/kernel/core_pattern accordingly.")
85 " current core pattern is: %s" % corefmt)
88 class NumericConstant(object):
89 __metaclass__ = ABCMeta
94 def __init__(self, value):
104 if self._value in self.desc_dict:
105 return self.desc_dict[self._value]
110 """ Generic test host "connected" to VPPs interface. """
120 return mactobinary(self._mac)
124 """ IPv4 address - string """
129 """ IPv4 address of remote host - raw, suitable as API parameter."""
130 return socket.inet_pton(socket.AF_INET, self._ip4)
134 """ IPv6 address - string """
139 """ IPv6 address of remote host - raw, suitable as API parameter."""
140 return socket.inet_pton(socket.AF_INET6, self._ip6)
144 """ IPv6 link-local address - string """
149 """ IPv6 link-local address of remote host -
150 raw, suitable as API parameter."""
151 return socket.inet_pton(socket.AF_INET6, self._ip6_ll)
153 def __eq__(self, other):
154 if isinstance(other, Host):
155 return (self.mac == other.mac and
156 self.ip4 == other.ip4 and
157 self.ip6 == other.ip6 and
158 self.ip6_ll == other.ip6_ll)
162 def __ne__(self, other):
163 return not self.__eq__(other)
166 return "Host { mac:%s ip4:%s ip6:%s ip6_ll:%s }" % (self.mac,
172 return hash(self.__repr__())
174 def __init__(self, mac=None, ip4=None, ip6=None, ip6_ll=None):
178 self._ip6_ll = ip6_ll
181 class ForeignAddressFactory(object):
184 net_template = '10.10.10.{}'
185 net = net_template.format(0) + '/' + str(prefix_len)
189 raise Exception("Network host address exhaustion")
191 return self.net_template.format(self.count)
195 """ L4 'connection' tied to two VPP interfaces """
197 def __init__(self, testcase, if1, if2, af, l4proto, port1, port2):
198 self.testcase = testcase
199 self.ifs = [None, None]
202 self.address_family = af
203 self.l4proto = l4proto
204 self.ports = [None, None]
205 self.ports[0] = port1
206 self.ports[1] = port2
209 def pkt(self, side, l4args={}, payload="x"):
210 is_ip6 = 1 if self.address_family == AF_INET6 else 0
213 src_if = self.ifs[s0]
214 dst_if = self.ifs[s1]
215 layer_3 = [IP(src=src_if.remote_ip4, dst=dst_if.remote_ip4),
216 IPv6(src=src_if.remote_ip6, dst=dst_if.remote_ip6)]
217 merged_l4args = {'sport': self.ports[s0], 'dport': self.ports[s1]}
218 merged_l4args.update(l4args)
219 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
221 self.l4proto(**merged_l4args) /
225 def send(self, side, flags=None, payload=""):
227 if flags is not None:
228 l4args['flags'] = flags
229 self.ifs[side].add_stream(self.pkt(side,
230 l4args=l4args, payload=payload))
231 self.ifs[1 - side].enable_capture()
232 self.testcase.pg_start()
234 def recv(self, side):
235 p = self.ifs[side].wait_for_packet(1)
238 def send_through(self, side, flags=None, payload=""):
239 self.send(side, flags, payload)
240 p = self.recv(1 - side)
243 def send_pingpong(self, side, flags1=None, flags2=None):
244 p1 = self.send_through(side, flags1)
245 p2 = self.send_through(1 - side, flags2)
250 L4_CONN_SIDE_ZERO = 0
254 class LoggerWrapper(object):
255 def __init__(self, logger=None):
256 self._logger = logger
258 def debug(self, *args, **kwargs):
260 self._logger.debug(*args, **kwargs)
262 def error(self, *args, **kwargs):
264 self._logger.error(*args, **kwargs)
267 def fragment_rfc791(packet, fragsize, _logger=None):
269 Fragment an IPv4 packet per RFC 791
270 :param packet: packet to fragment
271 :param fragsize: size at which to fragment
272 :note: IP options are not supported
273 :returns: list of fragments
275 logger = LoggerWrapper(_logger)
276 logger.debug(ppp("Fragmenting packet:", packet))
277 packet = packet.__class__(str(packet)) # recalculate all values
278 if len(packet[IP].options) > 0:
279 raise Exception("Not implemented")
280 if len(packet) <= fragsize:
283 pre_ip_len = len(packet) - len(packet[IP])
284 ip_header_len = packet[IP].ihl * 4
285 hex_packet = str(packet)
286 hex_headers = hex_packet[:(pre_ip_len + ip_header_len)]
287 hex_payload = hex_packet[(pre_ip_len + ip_header_len):]
291 otl = len(packet[IP])
292 nfb = (fragsize - pre_ip_len - ihl * 4) / 8
295 p = packet.__class__(hex_headers + hex_payload[:nfb * 8])
298 p[IP].len = ihl * 4 + nfb * 8
302 p = packet.__class__(hex_headers + hex_payload[nfb * 8:])
303 p[IP].len = otl - nfb * 8
304 p[IP].frag = fo + nfb
307 more_fragments = fragment_rfc791(p, fragsize, _logger)
308 pkts.extend(more_fragments)
313 def fragment_rfc8200(packet, identification, fragsize, _logger=None):
315 Fragment an IPv6 packet per RFC 8200
316 :param packet: packet to fragment
317 :param fragsize: size at which to fragment
318 :note: IP options are not supported
319 :returns: list of fragments
321 logger = LoggerWrapper(_logger)
322 packet = packet.__class__(str(packet)) # recalculate all values
323 if len(packet) <= fragsize:
325 logger.debug(ppp("Fragmenting packet:", packet))
329 hop_by_hop_hdr = None
333 l = packet.getlayer(counter)
335 if l.__class__ is IPv6:
337 # ignore 2nd IPv6 header and everything below..
341 elif l.__class__ is IPv6ExtHdrFragment:
342 raise Exception("Already fragmented")
343 elif l.__class__ is IPv6ExtHdrRouting:
344 routing_hdr = counter
345 elif l.__class__ is IPv6ExtHdrHopByHop:
346 hop_by_hop_hdr = counter
347 elif seen_ipv6 and not upper_layer and \
348 not l.__class__.__name__.startswith('IPv6ExtHdr'):
349 upper_layer = counter
350 counter = counter + 1
351 l = packet.getlayer(counter)
354 "Layers seen: IPv6(#%s), Routing(#%s), HopByHop(#%s), upper(#%s)" %
355 (ipv6_nr, routing_hdr, hop_by_hop_hdr, upper_layer))
357 if upper_layer is None:
358 raise Exception("Upper layer header not found in IPv6 packet")
360 last_per_fragment_hdr = ipv6_nr
361 if routing_hdr is None:
362 if hop_by_hop_hdr is not None:
363 last_per_fragment_hdr = hop_by_hop_hdr
365 last_per_fragment_hdr = routing_hdr
366 logger.debug("Last per-fragment hdr is #%s" % (last_per_fragment_hdr))
368 per_fragment_headers = packet.copy()
369 per_fragment_headers[last_per_fragment_hdr].remove_payload()
370 logger.debug(ppp("Per-fragment headers:", per_fragment_headers))
372 ext_and_upper_layer = packet.getlayer(last_per_fragment_hdr)[1]
373 hex_payload = str(ext_and_upper_layer)
374 logger.debug("Payload length is %s" % len(hex_payload))
375 logger.debug(ppp("Ext and upper layer:", ext_and_upper_layer))
377 fragment_ext_hdr = IPv6ExtHdrFragment()
378 logger.debug(ppp("Fragment header:", fragment_ext_hdr))
380 if len(per_fragment_headers) + len(fragment_ext_hdr) +\
381 len(ext_and_upper_layer) - len(ext_and_upper_layer.payload)\
383 raise Exception("Cannot fragment this packet - MTU too small "
384 "(%s, %s, %s, %s, %s)" % (
385 len(per_fragment_headers), len(fragment_ext_hdr),
386 len(ext_and_upper_layer),
387 len(ext_and_upper_layer.payload), fragsize))
389 orig_nh = packet[IPv6].nh
390 p = per_fragment_headers
393 p = p / fragment_ext_hdr
394 del p[IPv6ExtHdrFragment].nh
395 first_payload_len_nfb = (fragsize - len(p)) / 8
396 p = p / Raw(hex_payload[:first_payload_len_nfb * 8])
398 p[IPv6ExtHdrFragment].nh = orig_nh
399 p[IPv6ExtHdrFragment].id = identification
400 p[IPv6ExtHdrFragment].offset = 0
401 p[IPv6ExtHdrFragment].m = 1
402 p = p.__class__(str(p))
403 logger.debug(ppp("Fragment %s:" % len(pkts), p))
405 offset = first_payload_len_nfb * 8
406 logger.debug("Offset after first fragment: %s" % offset)
407 while len(hex_payload) > offset:
408 p = per_fragment_headers
411 p = p / fragment_ext_hdr
412 del p[IPv6ExtHdrFragment].nh
413 l_nfb = (fragsize - len(p)) / 8
414 p = p / Raw(hex_payload[offset:offset + l_nfb * 8])
415 p[IPv6ExtHdrFragment].nh = orig_nh
416 p[IPv6ExtHdrFragment].id = identification
417 p[IPv6ExtHdrFragment].offset = offset / 8
418 p[IPv6ExtHdrFragment].m = 1
419 p = p.__class__(str(p))
420 logger.debug(ppp("Fragment %s:" % len(pkts), p))
422 offset = offset + l_nfb * 8
424 pkts[-1][IPv6ExtHdrFragment].m = 0 # reset more-flags in last fragment