Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 1 | """ |
| 2 | Neighbour Entries |
| 3 | |
| 4 | object abstractions for ARP and ND |
| 5 | """ |
| 6 | |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 7 | from ipaddress import ip_address |
Paul Vinciguerra | a279d9c | 2019-02-28 09:00:09 -0800 | [diff] [blame] | 8 | from vpp_object import VppObject |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 9 | from vpp_papi import mac_pton, VppEnum |
Paul Vinciguerra | 1e18eb2 | 2018-11-25 16:09:26 -0800 | [diff] [blame] | 10 | try: |
| 11 | text_type = unicode |
| 12 | except NameError: |
| 13 | text_type = str |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 14 | |
| 15 | |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 16 | def find_nbr(test, sw_if_index, nbr_addr, is_static=0, mac=None): |
Paul Vinciguerra | 1e18eb2 | 2018-11-25 16:09:26 -0800 | [diff] [blame] | 17 | ip_addr = ip_address(text_type(nbr_addr)) |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 18 | e = VppEnum.vl_api_ip_neighbor_flags_t |
Paul Vinciguerra | e64e5ff | 2020-04-28 00:27:38 -0400 | [diff] [blame] | 19 | nbrs = test.vapi.ip_neighbor_dump(sw_if_index=sw_if_index, |
| 20 | af=ip_addr.vapi_af) |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 21 | |
| 22 | for n in nbrs: |
Neale Ranns | 22eefd7 | 2020-09-23 11:25:21 +0000 | [diff] [blame] | 23 | if sw_if_index == n.neighbor.sw_if_index and \ |
| 24 | ip_addr == n.neighbor.ip_address and \ |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 25 | is_static == (n.neighbor.flags & e.IP_API_NEIGHBOR_FLAG_STATIC): |
Neale Ranns | 59ae61e | 2018-06-07 18:09:49 -0700 | [diff] [blame] | 26 | if mac: |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 27 | if mac == str(n.neighbor.mac_address): |
Neale Ranns | 59ae61e | 2018-06-07 18:09:49 -0700 | [diff] [blame] | 28 | return True |
| 29 | else: |
| 30 | return True |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 31 | return False |
| 32 | |
| 33 | |
| 34 | class VppNeighbor(VppObject): |
| 35 | """ |
| 36 | ARP Entry |
| 37 | """ |
| 38 | |
| 39 | def __init__(self, test, sw_if_index, mac_addr, nbr_addr, |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 40 | is_static=False, is_no_fib_entry=False): |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 41 | self._test = test |
| 42 | self.sw_if_index = sw_if_index |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 43 | self.mac_addr = mac_addr |
Neale Ranns | f0404e9 | 2018-03-27 13:25:17 -0700 | [diff] [blame] | 44 | self.nbr_addr = nbr_addr |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 45 | |
| 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 Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 52 | |
| 53 | def add_vpp_config(self): |
Neale Ranns | 1426039 | 2018-09-28 05:00:57 -0700 | [diff] [blame] | 54 | r = self._test.vapi.ip_neighbor_add_del( |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 55 | self.sw_if_index, |
| 56 | self.mac_addr, |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 57 | self.nbr_addr, |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 58 | is_add=1, |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 59 | flags=self.flags) |
Neale Ranns | 1426039 | 2018-09-28 05:00:57 -0700 | [diff] [blame] | 60 | self.stats_index = r.stats_index |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 61 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | 0b6a857 | 2019-10-30 17:34:14 +0000 | [diff] [blame] | 62 | return self |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 63 | |
| 64 | def remove_vpp_config(self): |
| 65 | self._test.vapi.ip_neighbor_add_del( |
| 66 | self.sw_if_index, |
| 67 | self.mac_addr, |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 68 | self.nbr_addr, |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 69 | is_add=0, |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 70 | 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 Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 75 | |
| 76 | def query_vpp_config(self): |
Neale Ranns | f0404e9 | 2018-03-27 13:25:17 -0700 | [diff] [blame] | 77 | return find_nbr(self._test, |
| 78 | self.sw_if_index, |
| 79 | self.nbr_addr, |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 80 | self.is_static()) |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 81 | |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 82 | def object_id(self): |
Neale Ranns | f0404e9 | 2018-03-27 13:25:17 -0700 | [diff] [blame] | 83 | return ("%d:%s" % (self.sw_if_index, self.nbr_addr)) |
Neale Ranns | 1426039 | 2018-09-28 05:00:57 -0700 | [diff] [blame] | 84 | |
| 85 | def get_stats(self): |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame^] | 86 | c = self._test.statistics["/net/adjacency"] |
Neale Ranns | 1426039 | 2018-09-28 05:00:57 -0700 | [diff] [blame] | 87 | return c[0][self.stats_index] |