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 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 10 | |
Paul Vinciguerra | 1e18eb2 | 2018-11-25 16:09:26 -0800 | [diff] [blame] | 11 | try: |
| 12 | text_type = unicode |
| 13 | except NameError: |
| 14 | text_type = str |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 15 | |
| 16 | |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 17 | 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] | 18 | ip_addr = ip_address(text_type(nbr_addr)) |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 19 | e = VppEnum.vl_api_ip_neighbor_flags_t |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 20 | nbrs = test.vapi.ip_neighbor_dump(sw_if_index=sw_if_index, af=ip_addr.vapi_af) |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 21 | |
| 22 | for n in nbrs: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 23 | 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 Ranns | 59ae61e | 2018-06-07 18:09:49 -0700 | [diff] [blame] | 28 | if mac: |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 29 | if mac == str(n.neighbor.mac_address): |
Neale Ranns | 59ae61e | 2018-06-07 18:09:49 -0700 | [diff] [blame] | 30 | return True |
| 31 | else: |
| 32 | return True |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 33 | return False |
| 34 | |
| 35 | |
| 36 | class VppNeighbor(VppObject): |
| 37 | """ |
| 38 | ARP Entry |
| 39 | """ |
| 40 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 41 | 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 Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 50 | self._test = test |
| 51 | self.sw_if_index = sw_if_index |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 52 | self.mac_addr = mac_addr |
Neale Ranns | f0404e9 | 2018-03-27 13:25:17 -0700 | [diff] [blame] | 53 | self.nbr_addr = nbr_addr |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 54 | |
| 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 Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 61 | |
| 62 | def add_vpp_config(self): |
Neale Ranns | 1426039 | 2018-09-28 05:00:57 -0700 | [diff] [blame] | 63 | r = self._test.vapi.ip_neighbor_add_del( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 64 | self.sw_if_index, self.mac_addr, self.nbr_addr, is_add=1, flags=self.flags |
| 65 | ) |
Neale Ranns | 1426039 | 2018-09-28 05:00:57 -0700 | [diff] [blame] | 66 | self.stats_index = r.stats_index |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 67 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | 0b6a857 | 2019-10-30 17:34:14 +0000 | [diff] [blame] | 68 | return self |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 69 | |
| 70 | def remove_vpp_config(self): |
| 71 | self._test.vapi.ip_neighbor_add_del( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 72 | self.sw_if_index, self.mac_addr, self.nbr_addr, is_add=0, flags=self.flags |
| 73 | ) |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 74 | |
| 75 | def is_static(self): |
| 76 | e = VppEnum.vl_api_ip_neighbor_flags_t |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 77 | return self.flags & e.IP_API_NEIGHBOR_FLAG_STATIC |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 78 | |
| 79 | def query_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 80 | return find_nbr(self._test, self.sw_if_index, self.nbr_addr, 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): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [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] |