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