blob: 9ba87005c463a4bf7cf09918230dfeb3bb79f936 [file] [log] [blame]
Neale Ranns39f9d8b2017-02-16 21:57:05 -08001"""
2 Neighbour Entries
3
4 object abstractions for ARP and ND
5"""
6
Neale Ranns37029302018-08-10 05:30:06 -07007from ipaddress import ip_address
Paul Vinciguerraa279d9c2019-02-28 09:00:09 -08008from vpp_object import VppObject
Neale Ranns37029302018-08-10 05:30:06 -07009from vpp_papi import mac_pton, VppEnum
Paul Vinciguerra1e18eb22018-11-25 16:09:26 -080010try:
11 text_type = unicode
12except NameError:
13 text_type = str
Neale Ranns39f9d8b2017-02-16 21:57:05 -080014
15
Neale Ranns37029302018-08-10 05:30:06 -070016def find_nbr(test, sw_if_index, nbr_addr, is_static=0, mac=None):
Paul Vinciguerra1e18eb22018-11-25 16:09:26 -080017 ip_addr = ip_address(text_type(nbr_addr))
Neale Ranns37029302018-08-10 05:30:06 -070018 e = VppEnum.vl_api_ip_neighbor_flags_t
Paul Vinciguerrae64e5ff2020-04-28 00:27:38 -040019 nbrs = test.vapi.ip_neighbor_dump(sw_if_index=sw_if_index,
20 af=ip_addr.vapi_af)
Neale Ranns39f9d8b2017-02-16 21:57:05 -080021
22 for n in nbrs:
Neale Ranns22eefd72020-09-23 11:25:21 +000023 if sw_if_index == n.neighbor.sw_if_index and \
24 ip_addr == n.neighbor.ip_address and \
Neale Ranns37029302018-08-10 05:30:06 -070025 is_static == (n.neighbor.flags & e.IP_API_NEIGHBOR_FLAG_STATIC):
Neale Ranns59ae61e2018-06-07 18:09:49 -070026 if mac:
Neale Ranns37029302018-08-10 05:30:06 -070027 if mac == str(n.neighbor.mac_address):
Neale Ranns59ae61e2018-06-07 18:09:49 -070028 return True
29 else:
30 return True
Neale Ranns39f9d8b2017-02-16 21:57:05 -080031 return False
32
33
34class VppNeighbor(VppObject):
35 """
36 ARP Entry
37 """
38
39 def __init__(self, test, sw_if_index, mac_addr, nbr_addr,
Neale Ranns37029302018-08-10 05:30:06 -070040 is_static=False, is_no_fib_entry=False):
Neale Ranns39f9d8b2017-02-16 21:57:05 -080041 self._test = test
42 self.sw_if_index = sw_if_index
Neale Ranns37029302018-08-10 05:30:06 -070043 self.mac_addr = mac_addr
Neale Rannsf0404e92018-03-27 13:25:17 -070044 self.nbr_addr = nbr_addr
Neale Ranns37029302018-08-10 05:30:06 -070045
46 e = VppEnum.vl_api_ip_neighbor_flags_t
47 self.flags = e.IP_API_NEIGHBOR_FLAG_NONE
48 if is_static:
49 self.flags |= e.IP_API_NEIGHBOR_FLAG_STATIC
50 if is_no_fib_entry:
51 self.flags |= e.IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY
Neale Ranns39f9d8b2017-02-16 21:57:05 -080052
53 def add_vpp_config(self):
Neale Ranns14260392018-09-28 05:00:57 -070054 r = self._test.vapi.ip_neighbor_add_del(
Neale Ranns39f9d8b2017-02-16 21:57:05 -080055 self.sw_if_index,
56 self.mac_addr,
Neale Ranns37029302018-08-10 05:30:06 -070057 self.nbr_addr,
Neale Ranns39f9d8b2017-02-16 21:57:05 -080058 is_add=1,
Neale Ranns37029302018-08-10 05:30:06 -070059 flags=self.flags)
Neale Ranns14260392018-09-28 05:00:57 -070060 self.stats_index = r.stats_index
Neale Ranns39f9d8b2017-02-16 21:57:05 -080061 self._test.registry.register(self, self._test.logger)
Neale Ranns0b6a8572019-10-30 17:34:14 +000062 return self
Neale Ranns39f9d8b2017-02-16 21:57:05 -080063
64 def remove_vpp_config(self):
65 self._test.vapi.ip_neighbor_add_del(
66 self.sw_if_index,
67 self.mac_addr,
Neale Ranns37029302018-08-10 05:30:06 -070068 self.nbr_addr,
Neale Ranns39f9d8b2017-02-16 21:57:05 -080069 is_add=0,
Neale Ranns37029302018-08-10 05:30:06 -070070 flags=self.flags)
71
72 def is_static(self):
73 e = VppEnum.vl_api_ip_neighbor_flags_t
74 return (self.flags & e.IP_API_NEIGHBOR_FLAG_STATIC)
Neale Ranns39f9d8b2017-02-16 21:57:05 -080075
76 def query_vpp_config(self):
Neale Rannsf0404e92018-03-27 13:25:17 -070077 return find_nbr(self._test,
78 self.sw_if_index,
79 self.nbr_addr,
Neale Ranns37029302018-08-10 05:30:06 -070080 self.is_static())
Neale Ranns39f9d8b2017-02-16 21:57:05 -080081
Neale Ranns39f9d8b2017-02-16 21:57:05 -080082 def object_id(self):
Neale Rannsf0404e92018-03-27 13:25:17 -070083 return ("%d:%s" % (self.sw_if_index, self.nbr_addr))
Neale Ranns14260392018-09-28 05:00:57 -070084
85 def get_stats(self):
Ole Troane66443c2021-03-18 11:12:01 +010086 c = self._test.statistics["/net/adjacency"]
Neale Ranns14260392018-09-28 05:00:57 -070087 return c[0][self.stats_index]