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 * |
| 9 | from util import mactobinary |
| 10 | |
| 11 | |
| 12 | def find_nbr(test, sw_if_index, ip_addr, is_static=0, inet=AF_INET): |
| 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: |
| 24 | return True |
| 25 | return False |
| 26 | |
| 27 | |
| 28 | class VppNeighbor(VppObject): |
| 29 | """ |
| 30 | ARP Entry |
| 31 | """ |
| 32 | |
| 33 | def __init__(self, test, sw_if_index, mac_addr, nbr_addr, |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 34 | af=AF_INET, is_static=False, is_no_fib_entry=False): |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 35 | self._test = test |
| 36 | self.sw_if_index = sw_if_index |
| 37 | self.mac_addr = mactobinary(mac_addr) |
| 38 | self.af = af |
| 39 | self.is_static = is_static |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 40 | self.is_no_fib_entry = is_no_fib_entry |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 41 | self.nbr_addr = inet_pton(af, nbr_addr) |
| 42 | |
| 43 | def add_vpp_config(self): |
| 44 | self._test.vapi.ip_neighbor_add_del( |
| 45 | self.sw_if_index, |
| 46 | self.mac_addr, |
| 47 | self.nbr_addr, |
| 48 | is_add=1, |
| 49 | is_ipv6=1 if AF_INET6 == self.af else 0, |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 50 | is_static=self.is_static, |
| 51 | is_no_adj_fib=self.is_no_fib_entry) |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 52 | self._test.registry.register(self, self._test.logger) |
| 53 | |
| 54 | def remove_vpp_config(self): |
| 55 | self._test.vapi.ip_neighbor_add_del( |
| 56 | self.sw_if_index, |
| 57 | self.mac_addr, |
| 58 | self.nbr_addr, |
| 59 | is_ipv6=1 if AF_INET6 == self.af else 0, |
| 60 | is_add=0, |
| 61 | is_static=self.is_static) |
| 62 | |
| 63 | def query_vpp_config(self): |
| 64 | dump = self._test.vapi.ip_neighbor_dump( |
| 65 | self.sw_if_index, |
| 66 | is_ipv6=1 if AF_INET6 == self.af else 0) |
| 67 | for n in dump: |
| 68 | if self.nbr_addr == n.ip_address \ |
| 69 | and self.is_static == n.is_static: |
| 70 | return True |
| 71 | return False |
| 72 | |
| 73 | def __str__(self): |
| 74 | return self.object_id() |
| 75 | |
| 76 | def object_id(self): |
| 77 | return ("%d:%s" |
| 78 | % (self.sw_if_index, |
| 79 | inet_ntop(self.af, self.nbr_addr))) |