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