Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 1 | """ |
| 2 | IP Routes |
| 3 | |
| 4 | object abstractions for representing IP routes in VPP |
| 5 | """ |
| 6 | |
Paul Vinciguerra | a279d9c | 2019-02-28 09:00:09 -0800 | [diff] [blame] | 7 | from vpp_object import VppObject |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 8 | from socket import inet_pton, inet_ntop, AF_INET, AF_INET6 |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 9 | from vpp_ip import DpoProto, INVALID_INDEX, VppIpAddressUnion, \ |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 10 | VppIpMPrefix |
Neale Ranns | 59f7113 | 2020-04-08 12:19:38 +0000 | [diff] [blame] | 11 | from ipaddress import ip_network, ip_address, IPv4Network, IPv6Network |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 12 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 13 | # from vnet/vnet/mpls/mpls_types.h |
| 14 | MPLS_IETF_MAX_LABEL = 0xfffff |
| 15 | MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1 |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 16 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 17 | try: |
| 18 | text_type = unicode |
| 19 | except NameError: |
| 20 | text_type = str |
| 21 | |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 22 | |
Neale Ranns | 180279b | 2017-03-16 15:49:09 -0400 | [diff] [blame] | 23 | class MRouteItfFlags: |
| 24 | MFIB_ITF_FLAG_NONE = 0 |
| 25 | MFIB_ITF_FLAG_NEGATE_SIGNAL = 1 |
| 26 | MFIB_ITF_FLAG_ACCEPT = 2 |
| 27 | MFIB_ITF_FLAG_FORWARD = 4 |
| 28 | MFIB_ITF_FLAG_SIGNAL_PRESENT = 8 |
| 29 | MFIB_ITF_FLAG_INTERNAL_COPY = 16 |
| 30 | |
| 31 | |
| 32 | class MRouteEntryFlags: |
| 33 | MFIB_ENTRY_FLAG_NONE = 0 |
| 34 | MFIB_ENTRY_FLAG_SIGNAL = 1 |
| 35 | MFIB_ENTRY_FLAG_DROP = 2 |
| 36 | MFIB_ENTRY_FLAG_CONNECTED = 4 |
| 37 | MFIB_ENTRY_FLAG_INHERIT_ACCEPT = 8 |
| 38 | |
| 39 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 40 | class FibPathProto: |
| 41 | FIB_PATH_NH_PROTO_IP4 = 0 |
| 42 | FIB_PATH_NH_PROTO_IP6 = 1 |
| 43 | FIB_PATH_NH_PROTO_MPLS = 2 |
| 44 | FIB_PATH_NH_PROTO_ETHERNET = 3 |
| 45 | FIB_PATH_NH_PROTO_BIER = 4 |
| 46 | FIB_PATH_NH_PROTO_NSH = 5 |
| 47 | |
| 48 | |
| 49 | class FibPathType: |
| 50 | FIB_PATH_TYPE_NORMAL = 0 |
| 51 | FIB_PATH_TYPE_LOCAL = 1 |
| 52 | FIB_PATH_TYPE_DROP = 2 |
| 53 | FIB_PATH_TYPE_UDP_ENCAP = 3 |
| 54 | FIB_PATH_TYPE_BIER_IMP = 4 |
| 55 | FIB_PATH_TYPE_ICMP_UNREACH = 5 |
| 56 | FIB_PATH_TYPE_ICMP_PROHIBIT = 6 |
| 57 | FIB_PATH_TYPE_SOURCE_LOOKUP = 7 |
| 58 | FIB_PATH_TYPE_DVR = 8 |
| 59 | FIB_PATH_TYPE_INTERFACE_RX = 9 |
| 60 | FIB_PATH_TYPE_CLASSIFY = 10 |
| 61 | |
| 62 | |
| 63 | class FibPathFlags: |
| 64 | FIB_PATH_FLAG_NONE = 0 |
| 65 | FIB_PATH_FLAG_RESOLVE_VIA_ATTACHED = 1 |
| 66 | FIB_PATH_FLAG_RESOLVE_VIA_HOST = 2 |
Neale Ranns | 1dbcf30 | 2019-07-19 11:44:53 +0000 | [diff] [blame] | 67 | FIB_PATH_FLAG_POP_PW_CW = 4 |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 68 | |
| 69 | |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 70 | class MplsLspMode: |
| 71 | PIPE = 0 |
| 72 | UNIFORM = 1 |
| 73 | |
| 74 | |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 75 | def mk_network(addr, len): |
| 76 | if ip_address(text_type(addr)).version == 4: |
| 77 | return IPv4Network("%s/%d" % (addr, len), strict=False) |
| 78 | else: |
| 79 | return IPv6Network("%s/%d" % (addr, len), strict=False) |
| 80 | |
| 81 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 82 | def ip_to_dpo_proto(addr): |
Paul Vinciguerra | beded85 | 2019-03-01 10:35:55 -0800 | [diff] [blame] | 83 | if addr.version == 6: |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 84 | return DpoProto.DPO_PROTO_IP6 |
| 85 | else: |
| 86 | return DpoProto.DPO_PROTO_IP4 |
| 87 | |
| 88 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 89 | def address_proto(ip_addr): |
Ole Troan | 6e6ad64 | 2020-02-04 13:28:13 +0100 | [diff] [blame] | 90 | if ip_addr.ip_addr.version == 4: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 91 | return FibPathProto.FIB_PATH_NH_PROTO_IP4 |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 92 | else: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 93 | return FibPathProto.FIB_PATH_NH_PROTO_IP6 |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 94 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 95 | |
Neale Ranns | 59f7113 | 2020-04-08 12:19:38 +0000 | [diff] [blame] | 96 | def find_route(test, addr, len, table_id=0, sw_if_index=None): |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 97 | prefix = mk_network(addr, len) |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 98 | |
Ole Troan | 6e6ad64 | 2020-02-04 13:28:13 +0100 | [diff] [blame] | 99 | if 4 == prefix.version: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 100 | routes = test.vapi.ip_route_dump(table_id, False) |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 101 | else: |
| 102 | routes = test.vapi.ip_route_dump(table_id, True) |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 103 | |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 104 | for e in routes: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 105 | if table_id == e.route.table_id \ |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 106 | and str(e.route.prefix) == str(prefix): |
Neale Ranns | 59f7113 | 2020-04-08 12:19:38 +0000 | [diff] [blame] | 107 | if not sw_if_index: |
| 108 | return True |
| 109 | else: |
| 110 | # should be only one path if the user is looking |
| 111 | # for the interface the route is reachable through |
| 112 | if e.route.n_paths != 1: |
| 113 | return False |
| 114 | else: |
| 115 | return (e.route.paths[0].sw_if_index == sw_if_index) |
| 116 | |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 117 | return False |
| 118 | |
| 119 | |
Neale Ranns | 9db6ada | 2019-11-08 12:42:31 +0000 | [diff] [blame] | 120 | def find_route_in_dump(dump, route, table): |
| 121 | for r in dump: |
| 122 | if table.table_id == r.route.table_id \ |
| 123 | and route.prefix == r.route.prefix: |
| 124 | if len(route.paths) == r.route.n_paths: |
| 125 | return True |
| 126 | return False |
| 127 | |
| 128 | |
| 129 | def find_mroute_in_dump(dump, route, table): |
| 130 | for r in dump: |
| 131 | if table.table_id == r.route.table_id \ |
| 132 | and route.prefix == r.route.prefix: |
| 133 | return True |
| 134 | return False |
| 135 | |
| 136 | |
Neale Ranns | 947ea62 | 2018-06-07 23:48:20 -0700 | [diff] [blame] | 137 | def find_mroute(test, grp_addr, src_addr, grp_addr_len, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 138 | table_id=0): |
| 139 | ip_mprefix = VppIpMPrefix(text_type(src_addr), |
| 140 | text_type(grp_addr), |
| 141 | grp_addr_len) |
| 142 | |
Ole Troan | 6e6ad64 | 2020-02-04 13:28:13 +0100 | [diff] [blame] | 143 | if 4 == ip_mprefix.version: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 144 | routes = test.vapi.ip_mroute_dump(table_id, False) |
Neale Ranns | 947ea62 | 2018-06-07 23:48:20 -0700 | [diff] [blame] | 145 | else: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 146 | routes = test.vapi.ip_mroute_dump(table_id, True) |
| 147 | |
Neale Ranns | 947ea62 | 2018-06-07 23:48:20 -0700 | [diff] [blame] | 148 | for e in routes: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 149 | if table_id == e.route.table_id and ip_mprefix == e.route.prefix: |
Neale Ranns | 947ea62 | 2018-06-07 23:48:20 -0700 | [diff] [blame] | 150 | return True |
| 151 | return False |
| 152 | |
| 153 | |
Neale Ranns | 775f73c | 2018-12-20 03:01:49 -0800 | [diff] [blame] | 154 | def find_mpls_route(test, table_id, label, eos_bit, paths=None): |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 155 | dump = test.vapi.mpls_route_dump(table_id) |
Neale Ranns | 775f73c | 2018-12-20 03:01:49 -0800 | [diff] [blame] | 156 | for e in dump: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 157 | if label == e.mr_route.mr_label \ |
| 158 | and eos_bit == e.mr_route.mr_eos \ |
| 159 | and table_id == e.mr_route.mr_table_id: |
Neale Ranns | 775f73c | 2018-12-20 03:01:49 -0800 | [diff] [blame] | 160 | if not paths: |
| 161 | return True |
| 162 | else: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 163 | if (len(paths) != len(e.mr_route.mr_paths)): |
Neale Ranns | 775f73c | 2018-12-20 03:01:49 -0800 | [diff] [blame] | 164 | return False |
| 165 | for i in range(len(paths)): |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 166 | if (paths[i] != e.mr_route.mr_paths[i]): |
Neale Ranns | 775f73c | 2018-12-20 03:01:49 -0800 | [diff] [blame] | 167 | return False |
| 168 | return True |
| 169 | return False |
| 170 | |
| 171 | |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 172 | def fib_interface_ip_prefix(test, addr, len, sw_if_index): |
| 173 | # can't use python net here since we need the host bits in the prefix |
| 174 | prefix = "%s/%d" % (addr, len) |
| 175 | addrs = test.vapi.ip_address_dump( |
| 176 | sw_if_index, |
| 177 | is_ipv6=(6 == ip_address(addr).version)) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 178 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 179 | for a in addrs: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 180 | if a.sw_if_index == sw_if_index and \ |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 181 | str(a.prefix) == prefix: |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 182 | return True |
| 183 | return False |
| 184 | |
| 185 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 186 | class VppIpTable(VppObject): |
| 187 | |
| 188 | def __init__(self, |
| 189 | test, |
| 190 | table_id, |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 191 | is_ip6=0, |
| 192 | register=True): |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 193 | self._test = test |
| 194 | self.table_id = table_id |
| 195 | self.is_ip6 = is_ip6 |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 196 | self.register = register |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 197 | |
| 198 | def add_vpp_config(self): |
Neale Ranns | 9db6ada | 2019-11-08 12:42:31 +0000 | [diff] [blame] | 199 | self._test.vapi.ip_table_add_del(is_add=1, |
| 200 | table={'is_ip6': self.is_ip6, |
| 201 | 'table_id': self.table_id}) |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 202 | if self.register: |
| 203 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | 9db6ada | 2019-11-08 12:42:31 +0000 | [diff] [blame] | 204 | return self |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 205 | |
| 206 | def remove_vpp_config(self): |
Neale Ranns | 9db6ada | 2019-11-08 12:42:31 +0000 | [diff] [blame] | 207 | self._test.vapi.ip_table_add_del(is_add=0, |
| 208 | table={'is_ip6': self.is_ip6, |
| 209 | 'table_id': self.table_id}) |
| 210 | |
| 211 | def replace_begin(self): |
| 212 | self._test.vapi.ip_table_replace_begin( |
| 213 | table={'is_ip6': self.is_ip6, |
| 214 | 'table_id': self.table_id}) |
| 215 | |
| 216 | def replace_end(self): |
| 217 | self._test.vapi.ip_table_replace_end( |
| 218 | table={'is_ip6': self.is_ip6, |
| 219 | 'table_id': self.table_id}) |
| 220 | |
| 221 | def flush(self): |
| 222 | self._test.vapi.ip_table_flush(table={'is_ip6': self.is_ip6, |
| 223 | 'table_id': self.table_id}) |
| 224 | |
| 225 | def dump(self): |
| 226 | return self._test.vapi.ip_route_dump(self.table_id, self.is_ip6) |
| 227 | |
| 228 | def mdump(self): |
| 229 | return self._test.vapi.ip_mroute_dump(self.table_id, self.is_ip6) |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 230 | |
| 231 | def query_vpp_config(self): |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 232 | if self.table_id == 0: |
| 233 | # the default table always exists |
| 234 | return False |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 235 | # find the default route |
| 236 | return find_route(self._test, |
| 237 | "::" if self.is_ip6 else "0.0.0.0", |
| 238 | 0, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 239 | self.table_id) |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 240 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 241 | def object_id(self): |
| 242 | return ("table-%s-%d" % |
| 243 | ("v6" if self.is_ip6 == 1 else "v4", |
| 244 | self.table_id)) |
| 245 | |
| 246 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 247 | class VppIpInterfaceAddress(VppObject): |
| 248 | |
Neale Ranns | 59f7113 | 2020-04-08 12:19:38 +0000 | [diff] [blame] | 249 | def __init__(self, test, intf, addr, len, bind=None): |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 250 | self._test = test |
| 251 | self.intf = intf |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 252 | self.addr = addr |
| 253 | self.len = len |
| 254 | self.prefix = "%s/%d" % (addr, len) |
Neale Ranns | 59f7113 | 2020-04-08 12:19:38 +0000 | [diff] [blame] | 255 | self.host_len = ip_network(self.prefix, strict=False).max_prefixlen |
| 256 | self.table_id = 0 |
| 257 | if bind: |
| 258 | self.table_id = bind.table.table_id |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 259 | |
| 260 | def add_vpp_config(self): |
| 261 | self._test.vapi.sw_interface_add_del_address( |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 262 | sw_if_index=self.intf.sw_if_index, prefix=self.prefix, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 263 | is_add=1) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 264 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | 9efcee6 | 2019-11-26 19:30:08 +0000 | [diff] [blame] | 265 | return self |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 266 | |
| 267 | def remove_vpp_config(self): |
| 268 | self._test.vapi.sw_interface_add_del_address( |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 269 | sw_if_index=self.intf.sw_if_index, prefix=self.prefix, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 270 | is_add=0) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 271 | |
| 272 | def query_vpp_config(self): |
Neale Ranns | 59f7113 | 2020-04-08 12:19:38 +0000 | [diff] [blame] | 273 | # search for the IP address mapping and the two expected |
| 274 | # FIB entries |
| 275 | v = ip_address(self.addr).version |
| 276 | |
| 277 | if ((v == 4 and self.len < 31) or (v == 6 and self.len < 127)): |
| 278 | return (fib_interface_ip_prefix(self._test, |
| 279 | self.addr, |
| 280 | self.len, |
| 281 | self.intf.sw_if_index) & |
| 282 | find_route(self._test, |
| 283 | self.addr, |
| 284 | self.len, |
| 285 | table_id=self.table_id, |
| 286 | sw_if_index=self.intf.sw_if_index) & |
| 287 | find_route(self._test, |
| 288 | self.addr, |
| 289 | self.host_len, |
| 290 | table_id=self.table_id, |
| 291 | sw_if_index=self.intf.sw_if_index)) |
| 292 | else: |
| 293 | return (fib_interface_ip_prefix(self._test, |
| 294 | self.addr, |
| 295 | self.len, |
| 296 | self.intf.sw_if_index) & |
| 297 | find_route(self._test, |
| 298 | self.addr, |
| 299 | self.host_len, |
| 300 | table_id=self.table_id, |
| 301 | sw_if_index=self.intf.sw_if_index)) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 302 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 303 | def object_id(self): |
Neale Ranns | 59f7113 | 2020-04-08 12:19:38 +0000 | [diff] [blame] | 304 | return "interface-ip-%s-%d-%s" % (self.intf, |
| 305 | self.table_id, |
| 306 | self.prefix) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 307 | |
| 308 | |
Neale Ranns | ec40a7d | 2020-04-23 07:36:12 +0000 | [diff] [blame] | 309 | class VppIp6LinkLocalAddress(VppObject): |
| 310 | |
| 311 | def __init__(self, test, intf, addr): |
| 312 | self._test = test |
| 313 | self.intf = intf |
| 314 | self.addr = addr |
| 315 | |
| 316 | def add_vpp_config(self): |
| 317 | self._test.vapi.sw_interface_ip6_set_link_local_address( |
| 318 | sw_if_index=self.intf.sw_if_index, ip=self.addr) |
| 319 | self._test.registry.register(self, self._test.logger) |
| 320 | return self |
| 321 | |
| 322 | def remove_vpp_config(self): |
| 323 | # link locals can't be removed, only changed |
| 324 | pass |
| 325 | |
| 326 | def query_vpp_config(self): |
| 327 | # no API to query |
| 328 | return False |
| 329 | |
| 330 | def object_id(self): |
| 331 | return "ip6-link-local-%s-%s" % (self.intf, self.addr) |
| 332 | |
| 333 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 334 | class VppIpInterfaceBind(VppObject): |
| 335 | |
| 336 | def __init__(self, test, intf, table): |
| 337 | self._test = test |
| 338 | self.intf = intf |
| 339 | self.table = table |
| 340 | |
| 341 | def add_vpp_config(self): |
| 342 | if self.table.is_ip6: |
| 343 | self.intf.set_table_ip6(self.table.table_id) |
| 344 | else: |
| 345 | self.intf.set_table_ip4(self.table.table_id) |
| 346 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | 59f7113 | 2020-04-08 12:19:38 +0000 | [diff] [blame] | 347 | return self |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 348 | |
| 349 | def remove_vpp_config(self): |
| 350 | if 0 == self.table.table_id: |
| 351 | return |
| 352 | if self.table.is_ip6: |
| 353 | self.intf.set_table_ip6(0) |
| 354 | else: |
| 355 | self.intf.set_table_ip4(0) |
| 356 | |
| 357 | def query_vpp_config(self): |
| 358 | if 0 == self.table.table_id: |
| 359 | return False |
| 360 | return self._test.vapi.sw_interface_get_table( |
| 361 | self.intf.sw_if_index, |
| 362 | self.table.is_ip6).vrf_id == self.table.table_id |
| 363 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 364 | def object_id(self): |
| 365 | return "interface-bind-%s-%s" % (self.intf, self.table) |
| 366 | |
| 367 | |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 368 | class VppMplsLabel(object): |
| 369 | def __init__(self, value, mode=MplsLspMode.PIPE, ttl=64, exp=0): |
| 370 | self.value = value |
| 371 | self.mode = mode |
| 372 | self.ttl = ttl |
| 373 | self.exp = exp |
| 374 | |
| 375 | def encode(self): |
| 376 | is_uniform = 0 if self.mode is MplsLspMode.PIPE else 1 |
| 377 | return {'label': self.value, |
| 378 | 'ttl': self.ttl, |
| 379 | 'exp': self.exp, |
| 380 | 'is_uniform': is_uniform} |
| 381 | |
Neale Ranns | 775f73c | 2018-12-20 03:01:49 -0800 | [diff] [blame] | 382 | def __eq__(self, other): |
| 383 | if isinstance(other, self.__class__): |
| 384 | return (self.value == other.value and |
| 385 | self.ttl == other.ttl and |
| 386 | self.exp == other.exp and |
| 387 | self.mode == other.mode) |
| 388 | elif hasattr(other, 'label'): |
| 389 | return (self.value == other.label and |
| 390 | self.ttl == other.ttl and |
| 391 | self.exp == other.exp and |
| 392 | (self.mode == MplsLspMode.UNIFORM) == other.is_uniform) |
| 393 | else: |
| 394 | return False |
| 395 | |
| 396 | def __ne__(self, other): |
| 397 | return not (self == other) |
| 398 | |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 399 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 400 | class VppFibPathNextHop(object): |
| 401 | def __init__(self, addr, |
| 402 | via_label=MPLS_LABEL_INVALID, |
| 403 | next_hop_id=INVALID_INDEX): |
| 404 | self.addr = VppIpAddressUnion(addr) |
| 405 | self.via_label = via_label |
| 406 | self.obj_id = next_hop_id |
| 407 | |
| 408 | def encode(self): |
| 409 | if self.via_label is not MPLS_LABEL_INVALID: |
| 410 | return {'via_label': self.via_label} |
| 411 | if self.obj_id is not INVALID_INDEX: |
| 412 | return {'obj_id': self.obj_id} |
| 413 | else: |
| 414 | return {'address': self.addr.encode()} |
| 415 | |
| 416 | def proto(self): |
| 417 | if self.via_label is MPLS_LABEL_INVALID: |
| 418 | return address_proto(self.addr) |
| 419 | else: |
| 420 | return FibPathProto.FIB_PATH_NH_PROTO_MPLS |
| 421 | |
| 422 | def __eq__(self, other): |
| 423 | if not isinstance(other, self.__class__): |
| 424 | # try the other instance's __eq__. |
| 425 | return NotImplemented |
| 426 | return (self.addr == other.addr and |
| 427 | self.via_label == other.via_label and |
| 428 | self.obj_id == other.obj_id) |
| 429 | |
| 430 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 431 | class VppRoutePath(object): |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 432 | |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 433 | def __init__( |
| 434 | self, |
| 435 | nh_addr, |
| 436 | nh_sw_if_index, |
| 437 | nh_table_id=0, |
| 438 | labels=[], |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 439 | nh_via_label=MPLS_LABEL_INVALID, |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 440 | rpf_id=0, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 441 | next_hop_id=INVALID_INDEX, |
| 442 | proto=None, |
| 443 | flags=FibPathFlags.FIB_PATH_FLAG_NONE, |
| 444 | type=FibPathType.FIB_PATH_TYPE_NORMAL): |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 445 | self.nh_itf = nh_sw_if_index |
| 446 | self.nh_table_id = nh_table_id |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 447 | self.nh_labels = labels |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 448 | self.weight = 1 |
| 449 | self.rpf_id = rpf_id |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 450 | self.proto = proto |
| 451 | self.flags = flags |
| 452 | self.type = type |
| 453 | self.nh = VppFibPathNextHop(nh_addr, nh_via_label, next_hop_id) |
| 454 | if proto is None: |
| 455 | self.proto = self.nh.proto() |
Neale Ranns | 31426c6 | 2017-05-24 10:32:58 -0700 | [diff] [blame] | 456 | else: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 457 | self.proto = proto |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 458 | self.next_hop_id = next_hop_id |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 459 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 460 | def encode_labels(self): |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 461 | lstack = [] |
| 462 | for l in self.nh_labels: |
| 463 | if type(l) == VppMplsLabel: |
| 464 | lstack.append(l.encode()) |
| 465 | else: |
| 466 | lstack.append({'label': l, |
| 467 | 'ttl': 255}) |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 468 | while (len(lstack) < 16): |
| 469 | lstack.append({}) |
| 470 | |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 471 | return lstack |
| 472 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 473 | def encode(self): |
| 474 | return {'weight': 1, |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 475 | 'preference': 0, |
| 476 | 'table_id': self.nh_table_id, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 477 | 'nh': self.nh.encode(), |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 478 | 'next_hop_id': self.next_hop_id, |
| 479 | 'sw_if_index': self.nh_itf, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 480 | 'rpf_id': self.rpf_id, |
| 481 | 'proto': self.proto, |
| 482 | 'type': self.type, |
| 483 | 'flags': self.flags, |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 484 | 'n_labels': len(self.nh_labels), |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 485 | 'label_stack': self.encode_labels()} |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 486 | |
Neale Ranns | ef90ed0 | 2018-09-13 08:45:12 -0700 | [diff] [blame] | 487 | def __eq__(self, other): |
Neale Ranns | 775f73c | 2018-12-20 03:01:49 -0800 | [diff] [blame] | 488 | if isinstance(other, self.__class__): |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 489 | return self.nh == other.nh |
Neale Ranns | 775f73c | 2018-12-20 03:01:49 -0800 | [diff] [blame] | 490 | elif hasattr(other, 'sw_if_index'): |
| 491 | # vl_api_fib_path_t |
| 492 | if (len(self.nh_labels) != other.n_labels): |
| 493 | return False |
| 494 | for i in range(len(self.nh_labels)): |
| 495 | if (self.nh_labels[i] != other.label_stack[i]): |
| 496 | return False |
| 497 | return self.nh_itf == other.sw_if_index |
| 498 | else: |
| 499 | return False |
| 500 | |
| 501 | def __ne__(self, other): |
| 502 | return not (self == other) |
Neale Ranns | ef90ed0 | 2018-09-13 08:45:12 -0700 | [diff] [blame] | 503 | |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 504 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 505 | class VppMRoutePath(VppRoutePath): |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 506 | |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 507 | def __init__(self, nh_sw_if_index, flags, |
Neale Ranns | e821ab1 | 2017-06-01 07:45:05 -0700 | [diff] [blame] | 508 | nh=None, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 509 | proto=FibPathProto.FIB_PATH_NH_PROTO_IP4, |
| 510 | type=FibPathType.FIB_PATH_TYPE_NORMAL, |
| 511 | bier_imp=INVALID_INDEX): |
Neale Ranns | e821ab1 | 2017-06-01 07:45:05 -0700 | [diff] [blame] | 512 | if not nh: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 513 | nh = "::" if proto is FibPathProto.FIB_PATH_NH_PROTO_IP6 \ |
| 514 | else "0.0.0.0" |
Neale Ranns | e821ab1 | 2017-06-01 07:45:05 -0700 | [diff] [blame] | 515 | super(VppMRoutePath, self).__init__(nh, |
| 516 | nh_sw_if_index, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 517 | proto=proto, |
| 518 | type=type, |
| 519 | next_hop_id=bier_imp) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 520 | self.nh_i_flags = flags |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 521 | self.bier_imp = bier_imp |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 522 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 523 | def encode(self): |
| 524 | return {'path': super(VppMRoutePath, self).encode(), |
| 525 | 'itf_flags': self.nh_i_flags} |
| 526 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 527 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 528 | class VppIpRoute(VppObject): |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 529 | """ |
| 530 | IP Route |
| 531 | """ |
| 532 | |
| 533 | def __init__(self, test, dest_addr, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 534 | dest_addr_len, paths, table_id=0, register=True): |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 535 | self._test = test |
| 536 | self.paths = paths |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 537 | self.table_id = table_id |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 538 | self.prefix = mk_network(dest_addr, dest_addr_len) |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 539 | self.register = register |
Paul Vinciguerra | 941da4a | 2019-06-18 07:57:53 -0400 | [diff] [blame] | 540 | self.stats_index = None |
Neale Ranns | c2ac235 | 2019-07-02 14:33:29 +0000 | [diff] [blame] | 541 | self.modified = False |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 542 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 543 | self.encoded_paths = [] |
| 544 | for path in self.paths: |
| 545 | self.encoded_paths.append(path.encode()) |
| 546 | |
| 547 | def __eq__(self, other): |
| 548 | if self.table_id == other.table_id and \ |
| 549 | self.prefix == other.prefix: |
| 550 | return True |
| 551 | return False |
| 552 | |
| 553 | def modify(self, paths): |
Neale Ranns | 69b7aa4 | 2017-03-10 03:04:12 -0800 | [diff] [blame] | 554 | self.paths = paths |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 555 | self.encoded_paths = [] |
| 556 | for path in self.paths: |
| 557 | self.encoded_paths.append(path.encode()) |
Neale Ranns | c2ac235 | 2019-07-02 14:33:29 +0000 | [diff] [blame] | 558 | self.modified = True |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 559 | |
| 560 | self._test.vapi.ip_route_add_del(route={'table_id': self.table_id, |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 561 | 'prefix': self.prefix, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 562 | 'n_paths': len( |
| 563 | self.encoded_paths), |
| 564 | 'paths': self.encoded_paths, |
| 565 | }, |
| 566 | is_add=1, |
| 567 | is_multipath=0) |
Neale Ranns | 69b7aa4 | 2017-03-10 03:04:12 -0800 | [diff] [blame] | 568 | |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 569 | def add_vpp_config(self): |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 570 | r = self._test.vapi.ip_route_add_del( |
| 571 | route={'table_id': self.table_id, |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 572 | 'prefix': self.prefix, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 573 | 'n_paths': len(self.encoded_paths), |
| 574 | 'paths': self.encoded_paths, |
| 575 | }, |
| 576 | is_add=1, |
| 577 | is_multipath=0) |
Neale Ranns | 008dbe1 | 2018-09-07 09:32:36 -0700 | [diff] [blame] | 578 | self.stats_index = r.stats_index |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 579 | if self.register: |
| 580 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | 9db6ada | 2019-11-08 12:42:31 +0000 | [diff] [blame] | 581 | return self |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 582 | |
| 583 | def remove_vpp_config(self): |
Neale Ranns | c2ac235 | 2019-07-02 14:33:29 +0000 | [diff] [blame] | 584 | # there's no need to issue different deletes for modified routes |
| 585 | # we do this only to test the two different ways to delete routes |
| 586 | # eiter by passing all the paths to remove and mutlipath=1 or |
| 587 | # passing no paths and multipath=0 |
| 588 | if self.modified: |
| 589 | self._test.vapi.ip_route_add_del( |
| 590 | route={'table_id': self.table_id, |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 591 | 'prefix': self.prefix, |
Neale Ranns | c2ac235 | 2019-07-02 14:33:29 +0000 | [diff] [blame] | 592 | 'n_paths': len( |
| 593 | self.encoded_paths), |
| 594 | 'paths': self.encoded_paths}, |
| 595 | is_add=0, |
| 596 | is_multipath=1) |
| 597 | else: |
| 598 | self._test.vapi.ip_route_add_del( |
| 599 | route={'table_id': self.table_id, |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 600 | 'prefix': self.prefix, |
Neale Ranns | c2ac235 | 2019-07-02 14:33:29 +0000 | [diff] [blame] | 601 | 'n_paths': 0}, |
| 602 | is_add=0, |
| 603 | is_multipath=0) |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 604 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 605 | def query_vpp_config(self): |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 606 | return find_route(self._test, |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 607 | self.prefix.network_address, |
| 608 | self.prefix.prefixlen, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 609 | self.table_id) |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 610 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 611 | def object_id(self): |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 612 | return ("%s:table-%d-%s" % ( |
| 613 | 'ip6-route' if self.prefix.version == 6 else 'ip-route', |
Paul Vinciguerra | 941da4a | 2019-06-18 07:57:53 -0400 | [diff] [blame] | 614 | self.table_id, |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 615 | self.prefix)) |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 616 | |
Neale Ranns | 008dbe1 | 2018-09-07 09:32:36 -0700 | [diff] [blame] | 617 | def get_stats_to(self): |
| 618 | c = self._test.statistics.get_counter("/net/route/to") |
| 619 | return c[0][self.stats_index] |
| 620 | |
| 621 | def get_stats_via(self): |
| 622 | c = self._test.statistics.get_counter("/net/route/via") |
| 623 | return c[0][self.stats_index] |
| 624 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 625 | |
| 626 | class VppIpMRoute(VppObject): |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 627 | """ |
| 628 | IP Multicast Route |
| 629 | """ |
| 630 | |
| 631 | def __init__(self, test, src_addr, grp_addr, |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 632 | grp_addr_len, e_flags, paths, table_id=0, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 633 | rpf_id=0): |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 634 | self._test = test |
| 635 | self.paths = paths |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 636 | self.table_id = table_id |
| 637 | self.e_flags = e_flags |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 638 | self.rpf_id = rpf_id |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 639 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 640 | self.prefix = VppIpMPrefix(src_addr, grp_addr, grp_addr_len) |
| 641 | self.encoded_paths = [] |
| 642 | for path in self.paths: |
| 643 | self.encoded_paths.append(path.encode()) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 644 | |
Paul Vinciguerra | e64e5ff | 2020-04-28 00:27:38 -0400 | [diff] [blame] | 645 | def encode(self, paths=None): |
| 646 | _paths = self.encoded_paths if paths is None else paths |
| 647 | return {'table_id': self.table_id, |
| 648 | 'entry_flags': self.e_flags, |
| 649 | 'rpf_id': self.rpf_id, |
| 650 | 'prefix': self.prefix.encode(), |
| 651 | 'n_paths': len(_paths), |
| 652 | 'paths': _paths, |
| 653 | } |
| 654 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 655 | def add_vpp_config(self): |
Paul Vinciguerra | e64e5ff | 2020-04-28 00:27:38 -0400 | [diff] [blame] | 656 | r = self._test.vapi.ip_mroute_add_del(route=self.encode(), |
| 657 | is_multipath=1, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 658 | is_add=1) |
| 659 | self.stats_index = r.stats_index |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 660 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | 9db6ada | 2019-11-08 12:42:31 +0000 | [diff] [blame] | 661 | return self |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 662 | |
| 663 | def remove_vpp_config(self): |
Paul Vinciguerra | e64e5ff | 2020-04-28 00:27:38 -0400 | [diff] [blame] | 664 | self._test.vapi.ip_mroute_add_del(route=self.encode(), |
| 665 | is_multipath=1, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 666 | is_add=0) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 667 | |
| 668 | def update_entry_flags(self, flags): |
| 669 | self.e_flags = flags |
Paul Vinciguerra | e64e5ff | 2020-04-28 00:27:38 -0400 | [diff] [blame] | 670 | self._test.vapi.ip_mroute_add_del(route=self.encode(paths=[]), |
| 671 | is_multipath=1, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 672 | is_add=1) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 673 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 674 | def update_rpf_id(self, rpf_id): |
| 675 | self.rpf_id = rpf_id |
Paul Vinciguerra | e64e5ff | 2020-04-28 00:27:38 -0400 | [diff] [blame] | 676 | self._test.vapi.ip_mroute_add_del(route=self.encode(paths=[]), |
| 677 | is_multipath=1, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 678 | is_add=1) |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 679 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 680 | def update_path_flags(self, itf, flags): |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 681 | for p in range(len(self.paths)): |
| 682 | if self.paths[p].nh_itf == itf: |
| 683 | self.paths[p].nh_i_flags = flags |
Paul Vinciguerra | 6230b9d | 2020-04-28 11:23:31 -0400 | [diff] [blame] | 684 | self.encoded_paths[p] = self.paths[p].encode() |
| 685 | break |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 686 | |
Paul Vinciguerra | e64e5ff | 2020-04-28 00:27:38 -0400 | [diff] [blame] | 687 | self._test.vapi.ip_mroute_add_del( |
| 688 | route=self.encode( |
| 689 | paths=[self.encoded_paths[p]]), |
| 690 | is_add=1, |
| 691 | is_multipath=0) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 692 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 693 | def query_vpp_config(self): |
Neale Ranns | 947ea62 | 2018-06-07 23:48:20 -0700 | [diff] [blame] | 694 | return find_mroute(self._test, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 695 | self.prefix.gaddr, |
| 696 | self.prefix.saddr, |
| 697 | self.prefix.length, |
| 698 | self.table_id) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 699 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 700 | def object_id(self): |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 701 | return ("%d:(%s,%s/%d)" % (self.table_id, |
| 702 | self.prefix.saddr, |
| 703 | self.prefix.gaddr, |
| 704 | self.prefix.length)) |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 705 | |
Neale Ranns | 28c142e | 2018-09-07 09:37:07 -0700 | [diff] [blame] | 706 | def get_stats(self): |
| 707 | c = self._test.statistics.get_counter("/net/mroute") |
| 708 | return c[0][self.stats_index] |
| 709 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 710 | |
| 711 | class VppMFibSignal(object): |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 712 | def __init__(self, test, route, interface, packet): |
| 713 | self.route = route |
| 714 | self.interface = interface |
| 715 | self.packet = packet |
| 716 | self.test = test |
| 717 | |
| 718 | def compare(self, signal): |
| 719 | self.test.assertEqual(self.interface, signal.sw_if_index) |
| 720 | self.test.assertEqual(self.route.table_id, signal.table_id) |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 721 | self.test.assertEqual(self.route.prefix, signal.prefix) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 722 | |
| 723 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 724 | class VppMplsIpBind(VppObject): |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 725 | """ |
| 726 | MPLS to IP Binding |
| 727 | """ |
| 728 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 729 | def __init__(self, test, local_label, dest_addr, dest_addr_len, |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 730 | table_id=0, ip_table_id=0, is_ip6=0): |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 731 | self._test = test |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 732 | self.dest_addr_len = dest_addr_len |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 733 | self.dest_addr = dest_addr |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 734 | self.ip_addr = ip_address(text_type(dest_addr)) |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 735 | self.local_label = local_label |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 736 | self.table_id = table_id |
| 737 | self.ip_table_id = ip_table_id |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 738 | self.prefix = mk_network(dest_addr, dest_addr_len) |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 739 | |
| 740 | def add_vpp_config(self): |
| 741 | self._test.vapi.mpls_ip_bind_unbind(self.local_label, |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 742 | self.prefix, |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 743 | table_id=self.table_id, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 744 | ip_table_id=self.ip_table_id) |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 745 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 746 | |
| 747 | def remove_vpp_config(self): |
| 748 | self._test.vapi.mpls_ip_bind_unbind(self.local_label, |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 749 | self.prefix, |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 750 | table_id=self.table_id, |
| 751 | ip_table_id=self.ip_table_id, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 752 | is_bind=0) |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 753 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 754 | def query_vpp_config(self): |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 755 | dump = self._test.vapi.mpls_route_dump(self.table_id) |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 756 | for e in dump: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 757 | if self.local_label == e.mr_route.mr_label \ |
| 758 | and self.table_id == e.mr_route.mr_table_id: |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 759 | return True |
| 760 | return False |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 761 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 762 | def object_id(self): |
| 763 | return ("%d:%s binds %d:%s/%d" |
| 764 | % (self.table_id, |
| 765 | self.local_label, |
| 766 | self.ip_table_id, |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 767 | self.dest_addr, |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 768 | self.dest_addr_len)) |
| 769 | |
| 770 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 771 | class VppMplsTable(VppObject): |
| 772 | |
| 773 | def __init__(self, |
| 774 | test, |
| 775 | table_id): |
| 776 | self._test = test |
| 777 | self.table_id = table_id |
| 778 | |
| 779 | def add_vpp_config(self): |
| 780 | self._test.vapi.mpls_table_add_del( |
| 781 | self.table_id, |
| 782 | is_add=1) |
| 783 | self._test.registry.register(self, self._test.logger) |
| 784 | |
| 785 | def remove_vpp_config(self): |
| 786 | self._test.vapi.mpls_table_add_del( |
| 787 | self.table_id, |
| 788 | is_add=0) |
| 789 | |
| 790 | def query_vpp_config(self): |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 791 | dump = self._test.vapi.mpls_table_dump() |
| 792 | for d in dump: |
| 793 | if d.mt_table.mt_table_id == self.table_id: |
| 794 | return True |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 795 | return False |
| 796 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 797 | def object_id(self): |
| 798 | return ("table-mpls-%d" % (self.table_id)) |
| 799 | |
| 800 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 801 | class VppMplsRoute(VppObject): |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 802 | """ |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 803 | MPLS Route/LSP |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 804 | """ |
| 805 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 806 | def __init__(self, test, local_label, eos_bit, paths, table_id=0, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 807 | is_multicast=0, |
| 808 | eos_proto=FibPathProto.FIB_PATH_NH_PROTO_IP4): |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 809 | self._test = test |
| 810 | self.paths = paths |
| 811 | self.local_label = local_label |
| 812 | self.eos_bit = eos_bit |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 813 | self.eos_proto = eos_proto |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 814 | self.table_id = table_id |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 815 | self.is_multicast = is_multicast |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 816 | |
| 817 | def add_vpp_config(self): |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 818 | paths = [] |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 819 | for path in self.paths: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 820 | paths.append(path.encode()) |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 821 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 822 | r = self._test.vapi.mpls_route_add_del(self.table_id, |
| 823 | self.local_label, |
| 824 | self.eos_bit, |
| 825 | self.eos_proto, |
| 826 | self.is_multicast, |
| 827 | paths, 1, 0) |
Neale Ranns | 008dbe1 | 2018-09-07 09:32:36 -0700 | [diff] [blame] | 828 | self.stats_index = r.stats_index |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 829 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 830 | |
| 831 | def remove_vpp_config(self): |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 832 | paths = [] |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 833 | for path in self.paths: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 834 | paths.append(path.encode()) |
| 835 | |
| 836 | self._test.vapi.mpls_route_add_del(self.table_id, |
| 837 | self.local_label, |
| 838 | self.eos_bit, |
| 839 | self.eos_proto, |
| 840 | self.is_multicast, |
| 841 | paths, 0, 0) |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 842 | |
| 843 | def query_vpp_config(self): |
Neale Ranns | 775f73c | 2018-12-20 03:01:49 -0800 | [diff] [blame] | 844 | return find_mpls_route(self._test, self.table_id, |
| 845 | self.local_label, self.eos_bit) |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 846 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 847 | def object_id(self): |
Paul Vinciguerra | 941da4a | 2019-06-18 07:57:53 -0400 | [diff] [blame] | 848 | return ("mpls-route-%d:%s/%d" |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 849 | % (self.table_id, |
| 850 | self.local_label, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 851 | 20 + self.eos_bit)) |
Neale Ranns | 008dbe1 | 2018-09-07 09:32:36 -0700 | [diff] [blame] | 852 | |
| 853 | def get_stats_to(self): |
| 854 | c = self._test.statistics.get_counter("/net/route/to") |
| 855 | return c[0][self.stats_index] |
| 856 | |
| 857 | def get_stats_via(self): |
| 858 | c = self._test.statistics.get_counter("/net/route/via") |
| 859 | return c[0][self.stats_index] |