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