1 from abc import abstractmethod, ABCMeta
3 from logging import info
8 class VppInterface(object):
12 __metaclass__ = ABCMeta
15 def sw_if_index(self):
16 """Interface index assigned by VPP"""
17 return self._sw_if_index
21 """MAC-address of the remote interface "connected" to this interface"""
22 return self._remote_hosts[0].mac
26 """MAC-address of the VPP interface"""
27 return self._local_mac
31 """Local IPv4 address on VPP interface (string)"""
32 return self._local_ip4
36 """Local IPv4 address - raw, suitable as API parameter"""
37 return socket.inet_pton(socket.AF_INET, self._local_ip4)
41 """IPv4 address of remote peer "connected" to this interface"""
42 return self._remote_hosts[0].ip4
45 def remote_ip4n(self):
46 """IPv4 address of remote peer - raw, suitable as API parameter"""
47 return socket.inet_pton(socket.AF_INET, self.remote_ip4)
51 """Local IPv6 address on VPP interface (string)"""
52 return self._local_ip6
56 """Local IPv6 address - raw, suitable as API parameter"""
57 return socket.inet_pton(socket.AF_INET6, self.local_ip6)
61 """IPv6 address of remote peer "connected" to this interface"""
62 return self._remote_hosts[0].ip6
65 def remote_ip6n(self):
66 """IPv6 address of remote peer - raw, suitable as API parameter"""
67 return socket.inet_pton(socket.AF_INET6, self.remote_ip6)
71 """Name of the interface"""
76 """Raw result of sw_interface_dump for this interface"""
81 """Test case creating this interface"""
85 def remote_hosts(self):
86 """Remote hosts list"""
87 return self._remote_hosts
90 def remote_hosts(self, value):
91 self._remote_hosts = value
92 #TODO: set hosts_by dicts
94 def host_by_mac(self, mac):
95 return self._hosts_by_mac[mac]
97 def host_by_ip4(self, ip):
98 return self._hosts_by_ip4[ip]
100 def host_by_ip6(self, ip):
101 return self._hosts_by_ip6[ip]
103 def generate_remote_hosts(self, count=1):
104 """Generate and add remote hosts for the interface."""
105 self._remote_hosts = []
106 self._hosts_by_mac = {}
107 self._hosts_by_ip4 = {}
108 self._hosts_by_ip6 = {}
109 for i in range(2, count+2): # 0: network address, 1: local vpp address
110 mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i)
111 ip4 = "172.16.%u.%u" % (self.sw_if_index, i)
112 ip6 = "fd01:%04x::%04x" % (self.sw_if_index, i)
113 host = Host(mac, ip4, ip6)
114 self._remote_hosts.append(host)
115 self._hosts_by_mac[mac] = host
116 self._hosts_by_ip4[ip4] = host
117 self._hosts_by_ip6[ip6] = host
119 def post_init_setup(self):
120 """Additional setup run after creating an interface object"""
122 self.generate_remote_hosts()
124 self._local_ip4 = "172.16.%u.1" % self.sw_if_index
125 self._local_ip4n = socket.inet_pton(socket.AF_INET, self.local_ip4)
127 self._local_ip6 = "fd01:%04x::1" % self.sw_if_index
128 self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6)
130 r = self.test.vapi.sw_interface_dump()
132 if intf.sw_if_index == self.sw_if_index:
133 self._name = intf.interface_name.split(b'\0', 1)[0]
134 self._local_mac = ':'.join(intf.l2_address.encode('hex')[i:i + 2]
135 for i in range(0, 12, 2))
140 "Could not find interface with sw_if_index %d "
141 "in interface dump %s" %
142 (self.sw_if_index, repr(r)))
145 def __init__(self, test, index):
147 self.post_init_setup()
148 info("New %s, MAC=%s, remote_ip4=%s, local_ip4=%s" %
149 (self.__name__, self.remote_mac, self.remote_ip4, self.local_ip4))
151 def config_ip4(self):
152 """Configure IPv4 address on the VPP interface"""
153 addr = self.local_ip4n
155 self.test.vapi.sw_interface_add_del_address(
156 self.sw_if_index, addr, addr_len)
158 def configure_extend_ipv4_mac_binding(self):
159 """Configure neighbor MAC to IPv4 addresses."""
160 for host in self._remote_hosts:
161 macn = host.mac.replace(":", "").decode('hex')
163 self.test.vapi.ip_neighbor_add_del(self.sw_if_index, macn, ipn)
165 def config_ip6(self):
166 """Configure IPv6 address on the VPP interface"""
167 addr = self._local_ip6n
169 self.test.vapi.sw_interface_add_del_address(
170 self.sw_if_index, addr, addr_len, is_ipv6=1)
172 def set_table_ip4(self, table_id):
173 """Set the interface in a IPv4 Table.
174 Must be called before configuring IP4 addresses"""
175 self.test.vapi.sw_interface_set_table(
176 self.sw_if_index, 0, table_id)
178 def set_table_ip6(self, table_id):
179 """Set the interface in a IPv6 Table.
180 Must be called before configuring IP6 addresses"""
181 self.test.vapi.sw_interface_set_table(
182 self.sw_if_index, 1, table_id)
184 def disable_ipv6_ra(self):
185 """Configure IPv6 RA suppress on the VPP interface"""
186 self.test.vapi.sw_interface_ra_suppress(self.sw_if_index)
189 """ Put interface ADMIN-UP """
190 self.test.vapi.sw_interface_set_flags(self.sw_if_index, admin_up_down=1)
192 def add_sub_if(self, sub_if):
194 Register a sub-interface with this interface
196 :param sub_if: sub-interface
199 if not hasattr(self, 'sub_if'):
202 if isinstance(self.sub_if, list):
203 self.sub_if.append(sub_if)
207 def enable_mpls(self):
208 """Enable MPLS on the VPP interface"""
209 self.test.vapi.sw_interface_enable_disable_mpls(