c72a3965d9ef5d7c3e34c4a0c0b18a8b948d3b25
[vpp.git] / test / util.py
1 ## @package util
2 #  Module with common functions that should be used by the test cases.
3 #
4 #  The module provides a set of tools for setup the test environment
5
6 from scapy.layers.l2 import Ether, ARP
7 from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6NDOptSrcLLAddr
8
9
10 ## Util class
11 #
12 #  Test cases that want to use methods defined in Util class should
13 #  inherit this class.
14 #
15 #  class Example(Util, VppTestCase):
16 #      pass
17 class Util(object):
18
19     ## Class method to send ARP Request for each VPP IPv4 address in
20     #  order to determine VPP interface MAC address to IPv4 bindings.
21     #
22     #  Resolved MAC address is saved to the VPP_MACS dictionary with interface
23     #  index as a key. ARP Request is sent from MAC in MY_MACS dictionary with
24     #  interface index as a key.
25     #  @param cls The class pointer.
26     #  @param args List variable to store indices of VPP interfaces.
27     @classmethod
28     def resolve_arp(cls, args):
29         for i in args:
30             ip = cls.VPP_IP4S[i]
31             cls.log("Sending ARP request for %s on port %u" % (ip, i))
32             arp_req = (Ether(dst="ff:ff:ff:ff:ff:ff", src=cls.MY_MACS[i]) /
33                        ARP(op=ARP.who_has, pdst=ip,
34                            psrc=cls.MY_IP4S[i], hwsrc=cls.MY_MACS[i]))
35             cls.pg_add_stream(i, arp_req)
36             cls.pg_enable_capture([i])
37
38             cls.cli(2, "trace add pg-input 1")
39             cls.pg_start()
40             arp_reply = cls.pg_get_capture(i)[0]
41             if arp_reply[ARP].op == ARP.is_at:
42                 cls.log("VPP pg%u MAC address is %s " % (i, arp_reply[ARP].hwsrc))
43                 cls.VPP_MACS[i] = arp_reply[ARP].hwsrc
44             else:
45                 cls.log("No ARP received on port %u" % i)
46             cls.cli(2, "show trace")
47             ## @var ip
48             #  <TODO add description>
49             ## @var arp_req
50             #  <TODO add description>
51             ## @var arp_reply
52             #  <TODO add description>
53             ## @var VPP_MACS
54             #  <TODO add description>
55
56     ## Class method to send ND request for each VPP IPv6 address in
57     #  order to determine VPP MAC address to IPv6 bindings.
58     #
59     #  Resolved MAC address is saved to the VPP_MACS dictionary with interface
60     #  index as a key. ND Request is sent from MAC in MY_MACS dictionary with
61     #  interface index as a key.
62     #  @param cls The class pointer.
63     #  @param args List variable to store indices of VPP interfaces.
64     @classmethod
65     def resolve_icmpv6_nd(cls, args):
66         for i in args:
67             ip = cls.VPP_IP6S[i]
68             cls.log("Sending ICMPv6ND_NS request for %s on port %u" % (ip, i))
69             nd_req = (Ether(dst="ff:ff:ff:ff:ff:ff", src=cls.MY_MACS[i]) /
70                       IPv6(src=cls.MY_IP6S[i], dst=ip) /
71                       ICMPv6ND_NS(tgt=ip) /
72                       ICMPv6NDOptSrcLLAddr(lladdr=cls.MY_MACS[i]))
73             cls.pg_add_stream(i, nd_req)
74             cls.pg_enable_capture([i])
75
76             cls.cli(2, "trace add pg-input 1")
77             cls.pg_start()
78             nd_reply = cls.pg_get_capture(i)[0]
79             icmpv6_na = nd_reply['ICMPv6 Neighbor Discovery - Neighbor Advertisement']
80             dst_ll_addr = icmpv6_na['ICMPv6 Neighbor Discovery Option - Destination Link-Layer Address']
81             cls.VPP_MACS[i] = dst_ll_addr.lladdr
82             ## @var ip
83             #  <TODO add description>
84             ## @var nd_req
85             #  <TODO add description>
86             ## @var nd_reply
87             #  <TODO add description>
88             ## @var icmpv6_na
89             #  <TODO add description>
90             ## @var dst_ll_addr
91             #  <TODO add description>
92             ## @var VPP_MACS
93             #  <TODO add description>
94
95     ## Class method to configure IPv4 addresses on VPP interfaces.
96     #
97     #  Set dictionary variables MY_IP4S and VPP_IP4S to IPv4 addresses
98     #  calculated using interface VPP interface index as a parameter.
99     #  /24 IPv4 prefix is used, with VPP interface address host part set
100     #  to .1 and MY address set to .2.
101     #  Used IPv4 prefix scheme: 172.16.{VPP-interface-index}.0/24.
102     #  @param cls The class pointer.
103     #  @param args List variable to store indices of VPP interfaces.
104     @classmethod
105     def config_ip4(cls, args):
106         for i in args:
107             cls.MY_IP4S[i] = "172.16.%u.2" % i
108             cls.VPP_IP4S[i] = "172.16.%u.1" % i
109             cls.api("sw_interface_add_del_address pg%u %s/24" % (i, cls.VPP_IP4S[i]))
110             cls.log("My IPv4 address is %s" % (cls.MY_IP4S[i]))
111             ## @var MY_IP4S
112             #  Dictionary variable to store host IPv4 addresses connected to packet
113             #  generator interfaces.
114             ## @var VPP_IP4S
115             #  Dictionary variable to store VPP IPv4 addresses of the packet
116             #  generator interfaces.
117
118     ## Class method to configure IPv6 addresses on VPP interfaces.
119     #
120     #  Set dictionary variables MY_IP6S and VPP_IP6S to IPv6 addresses
121     #  calculated using interface VPP interface index as a parameter.
122     #  /64 IPv6 prefix is used, with VPP interface address host part set
123     #  to ::1 and MY address set to ::2.
124     #  Used IPv6 prefix scheme: fd10:{VPP-interface-index}::0/64.
125     #  @param cls The class pointer.
126     #  @param args List variable to store indices of VPP interfaces.
127     @classmethod
128     def config_ip6(cls, args):
129         for i in args:
130             cls.MY_IP6S[i] = "fd10:%u::2" % i
131             cls.VPP_IP6S[i] = "fd10:%u::1" % i
132             cls.api("sw_interface_add_del_address pg%u %s/64" % (i, cls.VPP_IP6S[i]))
133             cls.log("My IPv6 address is %s" % (cls.MY_IP6S[i]))
134             ## @var MY_IP6S
135             #  Dictionary variable to store host IPv6 addresses connected to packet
136             #  generator interfaces.
137             ## @var VPP_IP6S
138             #  Dictionary variable to store VPP IPv6 addresses of the packet
139             #  generator interfaces.