blob: 60fe28f76e4a66a66536f37683f92d0e0fd8ffe2 [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
Dave Wallace8800f732023-08-31 00:47:44 -04009from vpp_papi import VppEnum
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020010
Paul Vinciguerra1e18eb22018-11-25 16:09:26 -080011try:
12 text_type = unicode
13except NameError:
14 text_type = str
Neale Ranns39f9d8b2017-02-16 21:57:05 -080015
16
Neale Ranns37029302018-08-10 05:30:06 -070017def find_nbr(test, sw_if_index, nbr_addr, is_static=0, mac=None):
Paul Vinciguerra1e18eb22018-11-25 16:09:26 -080018 ip_addr = ip_address(text_type(nbr_addr))
Neale Ranns37029302018-08-10 05:30:06 -070019 e = VppEnum.vl_api_ip_neighbor_flags_t
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020020 nbrs = test.vapi.ip_neighbor_dump(sw_if_index=sw_if_index, af=ip_addr.vapi_af)
Neale Ranns39f9d8b2017-02-16 21:57:05 -080021
22 for n in nbrs:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020023 if (
24 sw_if_index == n.neighbor.sw_if_index
25 and ip_addr == n.neighbor.ip_address
26 and is_static == (n.neighbor.flags & e.IP_API_NEIGHBOR_FLAG_STATIC)
27 ):
Neale Ranns59ae61e2018-06-07 18:09:49 -070028 if mac:
Neale Ranns37029302018-08-10 05:30:06 -070029 if mac == str(n.neighbor.mac_address):
Neale Ranns59ae61e2018-06-07 18:09:49 -070030 return True
31 else:
32 return True
Neale Ranns39f9d8b2017-02-16 21:57:05 -080033 return False
34
35
36class VppNeighbor(VppObject):
37 """
38 ARP Entry
39 """
40
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020041 def __init__(
42 self,
43 test,
44 sw_if_index,
45 mac_addr,
46 nbr_addr,
47 is_static=False,
48 is_no_fib_entry=False,
49 ):
Neale Ranns39f9d8b2017-02-16 21:57:05 -080050 self._test = test
51 self.sw_if_index = sw_if_index
Neale Ranns37029302018-08-10 05:30:06 -070052 self.mac_addr = mac_addr
Neale Rannsf0404e92018-03-27 13:25:17 -070053 self.nbr_addr = nbr_addr
Neale Ranns37029302018-08-10 05:30:06 -070054
55 e = VppEnum.vl_api_ip_neighbor_flags_t
56 self.flags = e.IP_API_NEIGHBOR_FLAG_NONE
57 if is_static:
58 self.flags |= e.IP_API_NEIGHBOR_FLAG_STATIC
59 if is_no_fib_entry:
60 self.flags |= e.IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY
Neale Ranns39f9d8b2017-02-16 21:57:05 -080061
62 def add_vpp_config(self):
Neale Ranns14260392018-09-28 05:00:57 -070063 r = self._test.vapi.ip_neighbor_add_del(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020064 self.sw_if_index, self.mac_addr, self.nbr_addr, is_add=1, flags=self.flags
65 )
Neale Ranns14260392018-09-28 05:00:57 -070066 self.stats_index = r.stats_index
Neale Ranns39f9d8b2017-02-16 21:57:05 -080067 self._test.registry.register(self, self._test.logger)
Neale Ranns0b6a8572019-10-30 17:34:14 +000068 return self
Neale Ranns39f9d8b2017-02-16 21:57:05 -080069
70 def remove_vpp_config(self):
71 self._test.vapi.ip_neighbor_add_del(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020072 self.sw_if_index, self.mac_addr, self.nbr_addr, is_add=0, flags=self.flags
73 )
Neale Ranns37029302018-08-10 05:30:06 -070074
75 def is_static(self):
76 e = VppEnum.vl_api_ip_neighbor_flags_t
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020077 return self.flags & e.IP_API_NEIGHBOR_FLAG_STATIC
Neale Ranns39f9d8b2017-02-16 21:57:05 -080078
79 def query_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020080 return find_nbr(self._test, self.sw_if_index, self.nbr_addr, self.is_static())
Neale Ranns39f9d8b2017-02-16 21:57:05 -080081
Neale Ranns39f9d8b2017-02-16 21:57:05 -080082 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020083 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]