blob: 5c2e34794ece6126ed1707e91058fd8aa9236534 [file] [log] [blame]
Neale Ranns39f9d8b2017-02-16 21:57:05 -08001"""
2 Neighbour Entries
3
4 object abstractions for ARP and ND
5"""
6
7from socket import inet_pton, inet_ntop, AF_INET, AF_INET6
8from vpp_object import *
9from util import mactobinary
10
11
12def find_nbr(test, sw_if_index, ip_addr, is_static=0, inet=AF_INET):
13 nbrs = test.vapi.ip_neighbor_dump(sw_if_index,
14 is_ipv6=1 if AF_INET6 == inet else 0)
15 if inet == AF_INET:
16 s = 4
17 else:
18 s = 16
19 nbr_addr = inet_pton(inet, ip_addr)
20
21 for n in nbrs:
22 if nbr_addr == n.ip_address[:s] \
23 and is_static == n.is_static:
24 return True
25 return False
26
27
28class VppNeighbor(VppObject):
29 """
30 ARP Entry
31 """
32
33 def __init__(self, test, sw_if_index, mac_addr, nbr_addr,
Neale Ranns87df12d2017-02-18 08:16:41 -080034 af=AF_INET, is_static=False):
Neale Ranns39f9d8b2017-02-16 21:57:05 -080035 self._test = test
36 self.sw_if_index = sw_if_index
37 self.mac_addr = mactobinary(mac_addr)
38 self.af = af
39 self.is_static = is_static
40 self.nbr_addr = inet_pton(af, nbr_addr)
41
42 def add_vpp_config(self):
43 self._test.vapi.ip_neighbor_add_del(
44 self.sw_if_index,
45 self.mac_addr,
46 self.nbr_addr,
47 is_add=1,
48 is_ipv6=1 if AF_INET6 == self.af else 0,
49 is_static=self.is_static)
50 self._test.registry.register(self, self._test.logger)
51
52 def remove_vpp_config(self):
53 self._test.vapi.ip_neighbor_add_del(
54 self.sw_if_index,
55 self.mac_addr,
56 self.nbr_addr,
57 is_ipv6=1 if AF_INET6 == self.af else 0,
58 is_add=0,
59 is_static=self.is_static)
60
61 def query_vpp_config(self):
62 dump = self._test.vapi.ip_neighbor_dump(
63 self.sw_if_index,
64 is_ipv6=1 if AF_INET6 == self.af else 0)
65 for n in dump:
66 if self.nbr_addr == n.ip_address \
67 and self.is_static == n.is_static:
68 return True
69 return False
70
71 def __str__(self):
72 return self.object_id()
73
74 def object_id(self):
75 return ("%d:%s"
76 % (self.sw_if_index,
77 inet_ntop(self.af, self.nbr_addr)))