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