blob: e8ba3b28823cad9ed1594fc0d107b7dc3aa4a557 [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 Ranns2a3ea492017-04-19 05:24:40 -070034 af=AF_INET, is_static=False, is_no_fib_entry=0):
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
Neale Rannsb3b2de72017-03-08 05:17:22 -080040 self.is_no_fib_entry = is_no_fib_entry
Neale Rannsf0404e92018-03-27 13:25:17 -070041 self.nbr_addr = nbr_addr
42 self.nbr_addr_n = inet_pton(af, nbr_addr)
Neale Ranns39f9d8b2017-02-16 21:57:05 -080043
44 def add_vpp_config(self):
45 self._test.vapi.ip_neighbor_add_del(
46 self.sw_if_index,
47 self.mac_addr,
Neale Rannsf0404e92018-03-27 13:25:17 -070048 self.nbr_addr_n,
Neale Ranns39f9d8b2017-02-16 21:57:05 -080049 is_add=1,
50 is_ipv6=1 if AF_INET6 == self.af else 0,
Neale Rannsb3b2de72017-03-08 05:17:22 -080051 is_static=self.is_static,
52 is_no_adj_fib=self.is_no_fib_entry)
Neale Ranns39f9d8b2017-02-16 21:57:05 -080053 self._test.registry.register(self, self._test.logger)
54
55 def remove_vpp_config(self):
56 self._test.vapi.ip_neighbor_add_del(
57 self.sw_if_index,
58 self.mac_addr,
Neale Rannsf0404e92018-03-27 13:25:17 -070059 self.nbr_addr_n,
Neale Ranns39f9d8b2017-02-16 21:57:05 -080060 is_ipv6=1 if AF_INET6 == self.af else 0,
61 is_add=0,
62 is_static=self.is_static)
63
64 def query_vpp_config(self):
Neale Rannsf0404e92018-03-27 13:25:17 -070065 return find_nbr(self._test,
66 self.sw_if_index,
67 self.nbr_addr,
68 self.is_static,
69 self.af)
Neale Ranns39f9d8b2017-02-16 21:57:05 -080070
71 def __str__(self):
72 return self.object_id()
73
74 def object_id(self):
Neale Rannsf0404e92018-03-27 13:25:17 -070075 return ("%d:%s" % (self.sw_if_index, self.nbr_addr))