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 | |
| 7 | from socket import inet_pton, inet_ntop, AF_INET, AF_INET6 |
| 8 | from vpp_object import * |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 9 | from vpp_papi import mac_pton |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 10 | |
| 11 | |
Neale Ranns | 59ae61e | 2018-06-07 18:09:49 -0700 | [diff] [blame] | 12 | def find_nbr(test, sw_if_index, ip_addr, is_static=0, inet=AF_INET, mac=None): |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 13 | nbrs = test.vapi.ip_neighbor_dump(sw_if_index, |
| 14 | is_ipv6=1 if AF_INET6 == inet else 0) |
| 15 | if inet == AF_INET: |
| 16 | s = 4 |
| 17 | else: |
| 18 | s = 16 |
| 19 | nbr_addr = inet_pton(inet, ip_addr) |
| 20 | |
| 21 | for n in nbrs: |
| 22 | if nbr_addr == n.ip_address[:s] \ |
| 23 | and is_static == n.is_static: |
Neale Ranns | 59ae61e | 2018-06-07 18:09:49 -0700 | [diff] [blame] | 24 | if mac: |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 25 | if n.mac_address == mac_pton(mac): |
Neale Ranns | 59ae61e | 2018-06-07 18:09:49 -0700 | [diff] [blame] | 26 | return True |
| 27 | else: |
| 28 | return True |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 29 | return False |
| 30 | |
| 31 | |
| 32 | class VppNeighbor(VppObject): |
| 33 | """ |
| 34 | ARP Entry |
| 35 | """ |
| 36 | |
| 37 | def __init__(self, test, sw_if_index, mac_addr, nbr_addr, |
Neale Ranns | 2a3ea49 | 2017-04-19 05:24:40 -0700 | [diff] [blame] | 38 | af=AF_INET, is_static=False, is_no_fib_entry=0): |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 39 | self._test = test |
| 40 | self.sw_if_index = sw_if_index |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 41 | self.mac_addr = mac_pton(mac_addr) |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 42 | self.af = af |
| 43 | self.is_static = is_static |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 44 | self.is_no_fib_entry = is_no_fib_entry |
Neale Ranns | f0404e9 | 2018-03-27 13:25:17 -0700 | [diff] [blame] | 45 | self.nbr_addr = nbr_addr |
| 46 | self.nbr_addr_n = inet_pton(af, nbr_addr) |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 47 | |
| 48 | def add_vpp_config(self): |
Neale Ranns | 1426039 | 2018-09-28 05:00:57 -0700 | [diff] [blame] | 49 | r = self._test.vapi.ip_neighbor_add_del( |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 50 | self.sw_if_index, |
| 51 | self.mac_addr, |
Neale Ranns | f0404e9 | 2018-03-27 13:25:17 -0700 | [diff] [blame] | 52 | self.nbr_addr_n, |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 53 | is_add=1, |
| 54 | is_ipv6=1 if AF_INET6 == self.af else 0, |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 55 | is_static=self.is_static, |
| 56 | is_no_adj_fib=self.is_no_fib_entry) |
Neale Ranns | 1426039 | 2018-09-28 05:00:57 -0700 | [diff] [blame] | 57 | self.stats_index = r.stats_index |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 58 | self._test.registry.register(self, self._test.logger) |
| 59 | |
| 60 | def remove_vpp_config(self): |
| 61 | self._test.vapi.ip_neighbor_add_del( |
| 62 | self.sw_if_index, |
| 63 | self.mac_addr, |
Neale Ranns | f0404e9 | 2018-03-27 13:25:17 -0700 | [diff] [blame] | 64 | self.nbr_addr_n, |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 65 | is_ipv6=1 if AF_INET6 == self.af else 0, |
| 66 | is_add=0, |
| 67 | is_static=self.is_static) |
| 68 | |
| 69 | def query_vpp_config(self): |
Neale Ranns | f0404e9 | 2018-03-27 13:25:17 -0700 | [diff] [blame] | 70 | return find_nbr(self._test, |
| 71 | self.sw_if_index, |
| 72 | self.nbr_addr, |
| 73 | self.is_static, |
| 74 | self.af) |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 75 | |
| 76 | def __str__(self): |
| 77 | return self.object_id() |
| 78 | |
| 79 | def object_id(self): |
Neale Ranns | f0404e9 | 2018-03-27 13:25:17 -0700 | [diff] [blame] | 80 | return ("%d:%s" % (self.sw_if_index, self.nbr_addr)) |
Neale Ranns | 1426039 | 2018-09-28 05:00:57 -0700 | [diff] [blame] | 81 | |
| 82 | def get_stats(self): |
| 83 | c = self._test.statistics.get_counter("/net/adjacency") |
| 84 | return c[0][self.stats_index] |