blob: 7391447ff9b6cfc69ab6f7c47d5b2d2a895f34e9 [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
Neale Ranns39f9d8b2017-02-16 21:57:05 -08008from vpp_object import *
Neale Ranns37029302018-08-10 05:30:06 -07009from vpp_papi import mac_pton, VppEnum
Neale Ranns39f9d8b2017-02-16 21:57:05 -080010
11
Neale Ranns37029302018-08-10 05:30:06 -070012def find_nbr(test, sw_if_index, nbr_addr, is_static=0, mac=None):
13 ip_addr = ip_address(unicode(nbr_addr))
14 e = VppEnum.vl_api_ip_neighbor_flags_t
Neale Ranns39f9d8b2017-02-16 21:57:05 -080015 nbrs = test.vapi.ip_neighbor_dump(sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -070016 is_ipv6=(6 is ip_addr.version))
Neale Ranns39f9d8b2017-02-16 21:57:05 -080017
18 for n in nbrs:
Neale Ranns37029302018-08-10 05:30:06 -070019 if ip_addr == n.neighbor.ip_address and \
20 is_static == (n.neighbor.flags & e.IP_API_NEIGHBOR_FLAG_STATIC):
Neale Ranns59ae61e2018-06-07 18:09:49 -070021 if mac:
Neale Ranns37029302018-08-10 05:30:06 -070022 if mac == str(n.neighbor.mac_address):
Neale Ranns59ae61e2018-06-07 18:09:49 -070023 return True
24 else:
25 return True
Neale Ranns39f9d8b2017-02-16 21:57:05 -080026 return False
27
28
29class VppNeighbor(VppObject):
30 """
31 ARP Entry
32 """
33
34 def __init__(self, test, sw_if_index, mac_addr, nbr_addr,
Neale Ranns37029302018-08-10 05:30:06 -070035 is_static=False, is_no_fib_entry=False):
Neale Ranns39f9d8b2017-02-16 21:57:05 -080036 self._test = test
37 self.sw_if_index = sw_if_index
Neale Ranns37029302018-08-10 05:30:06 -070038 self.mac_addr = mac_addr
Neale Rannsf0404e92018-03-27 13:25:17 -070039 self.nbr_addr = nbr_addr
Neale Ranns37029302018-08-10 05:30:06 -070040
41 e = VppEnum.vl_api_ip_neighbor_flags_t
42 self.flags = e.IP_API_NEIGHBOR_FLAG_NONE
43 if is_static:
44 self.flags |= e.IP_API_NEIGHBOR_FLAG_STATIC
45 if is_no_fib_entry:
46 self.flags |= e.IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY
Neale Ranns39f9d8b2017-02-16 21:57:05 -080047
48 def add_vpp_config(self):
Neale Ranns14260392018-09-28 05:00:57 -070049 r = self._test.vapi.ip_neighbor_add_del(
Neale Ranns39f9d8b2017-02-16 21:57:05 -080050 self.sw_if_index,
51 self.mac_addr,
Neale Ranns37029302018-08-10 05:30:06 -070052 self.nbr_addr,
Neale Ranns39f9d8b2017-02-16 21:57:05 -080053 is_add=1,
Neale Ranns37029302018-08-10 05:30:06 -070054 flags=self.flags)
Neale Ranns14260392018-09-28 05:00:57 -070055 self.stats_index = r.stats_index
Neale Ranns39f9d8b2017-02-16 21:57:05 -080056 self._test.registry.register(self, self._test.logger)
57
58 def remove_vpp_config(self):
59 self._test.vapi.ip_neighbor_add_del(
60 self.sw_if_index,
61 self.mac_addr,
Neale Ranns37029302018-08-10 05:30:06 -070062 self.nbr_addr,
Neale Ranns39f9d8b2017-02-16 21:57:05 -080063 is_add=0,
Neale Ranns37029302018-08-10 05:30:06 -070064 flags=self.flags)
65
66 def is_static(self):
67 e = VppEnum.vl_api_ip_neighbor_flags_t
68 return (self.flags & e.IP_API_NEIGHBOR_FLAG_STATIC)
Neale Ranns39f9d8b2017-02-16 21:57:05 -080069
70 def query_vpp_config(self):
Neale Rannsf0404e92018-03-27 13:25:17 -070071 return find_nbr(self._test,
72 self.sw_if_index,
73 self.nbr_addr,
Neale Ranns37029302018-08-10 05:30:06 -070074 self.is_static())
Neale Ranns39f9d8b2017-02-16 21:57:05 -080075
76 def __str__(self):
77 return self.object_id()
78
79 def object_id(self):
Neale Rannsf0404e92018-03-27 13:25:17 -070080 return ("%d:%s" % (self.sw_if_index, self.nbr_addr))
Neale Ranns14260392018-09-28 05:00:57 -070081
82 def get_stats(self):
83 c = self._test.statistics.get_counter("/net/adjacency")
84 return c[0][self.stats_index]