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 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 7 | from vpp_object import * |
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 | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 9 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 10 | # from vnet/vnet/mpls/mpls_types.h |
| 11 | MPLS_IETF_MAX_LABEL = 0xfffff |
| 12 | MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1 |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 13 | |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 14 | |
Neale Ranns | 180279b | 2017-03-16 15:49:09 -0400 | [diff] [blame] | 15 | class MRouteItfFlags: |
| 16 | MFIB_ITF_FLAG_NONE = 0 |
| 17 | MFIB_ITF_FLAG_NEGATE_SIGNAL = 1 |
| 18 | MFIB_ITF_FLAG_ACCEPT = 2 |
| 19 | MFIB_ITF_FLAG_FORWARD = 4 |
| 20 | MFIB_ITF_FLAG_SIGNAL_PRESENT = 8 |
| 21 | MFIB_ITF_FLAG_INTERNAL_COPY = 16 |
| 22 | |
| 23 | |
| 24 | class MRouteEntryFlags: |
| 25 | MFIB_ENTRY_FLAG_NONE = 0 |
| 26 | MFIB_ENTRY_FLAG_SIGNAL = 1 |
| 27 | MFIB_ENTRY_FLAG_DROP = 2 |
| 28 | MFIB_ENTRY_FLAG_CONNECTED = 4 |
| 29 | MFIB_ENTRY_FLAG_INHERIT_ACCEPT = 8 |
| 30 | |
| 31 | |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 32 | class DpoProto: |
| 33 | DPO_PROTO_IP4 = 0 |
| 34 | DPO_PROTO_IP6 = 1 |
| 35 | DPO_PROTO_MPLS = 2 |
| 36 | DPO_PROTO_ETHERNET = 3 |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 37 | DPO_PROTO_BIER = 4 |
| 38 | DPO_PROTO_NSH = 5 |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 39 | |
| 40 | |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 41 | class MplsLspMode: |
| 42 | PIPE = 0 |
| 43 | UNIFORM = 1 |
| 44 | |
| 45 | |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 46 | def find_route(test, ip_addr, len, table_id=0, inet=AF_INET): |
| 47 | if inet == AF_INET: |
| 48 | s = 4 |
| 49 | routes = test.vapi.ip_fib_dump() |
| 50 | else: |
| 51 | s = 16 |
| 52 | routes = test.vapi.ip6_fib_dump() |
| 53 | |
| 54 | route_addr = inet_pton(inet, ip_addr) |
| 55 | for e in routes: |
| 56 | if route_addr == e.address[:s] \ |
| 57 | and len == e.address_length \ |
| 58 | and table_id == e.table_id: |
| 59 | return True |
| 60 | return False |
| 61 | |
| 62 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 63 | class VppIpTable(VppObject): |
| 64 | |
| 65 | def __init__(self, |
| 66 | test, |
| 67 | table_id, |
| 68 | is_ip6=0): |
| 69 | self._test = test |
| 70 | self.table_id = table_id |
| 71 | self.is_ip6 = is_ip6 |
| 72 | |
| 73 | def add_vpp_config(self): |
| 74 | self._test.vapi.ip_table_add_del( |
| 75 | self.table_id, |
| 76 | is_ipv6=self.is_ip6, |
| 77 | is_add=1) |
| 78 | self._test.registry.register(self, self._test.logger) |
| 79 | |
| 80 | def remove_vpp_config(self): |
| 81 | self._test.vapi.ip_table_add_del( |
| 82 | self.table_id, |
| 83 | is_ipv6=self.is_ip6, |
| 84 | is_add=0) |
| 85 | |
| 86 | def query_vpp_config(self): |
| 87 | # find the default route |
| 88 | return find_route(self._test, |
| 89 | "::" if self.is_ip6 else "0.0.0.0", |
| 90 | 0, |
| 91 | self.table_id, |
| 92 | inet=AF_INET6 if self.is_ip6 == 1 else AF_INET) |
| 93 | |
| 94 | def __str__(self): |
| 95 | return self.object_id() |
| 96 | |
| 97 | def object_id(self): |
| 98 | return ("table-%s-%d" % |
| 99 | ("v6" if self.is_ip6 == 1 else "v4", |
| 100 | self.table_id)) |
| 101 | |
| 102 | |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 103 | class VppMplsLabel(object): |
| 104 | def __init__(self, value, mode=MplsLspMode.PIPE, ttl=64, exp=0): |
| 105 | self.value = value |
| 106 | self.mode = mode |
| 107 | self.ttl = ttl |
| 108 | self.exp = exp |
| 109 | |
| 110 | def encode(self): |
| 111 | is_uniform = 0 if self.mode is MplsLspMode.PIPE else 1 |
| 112 | return {'label': self.value, |
| 113 | 'ttl': self.ttl, |
| 114 | 'exp': self.exp, |
| 115 | 'is_uniform': is_uniform} |
| 116 | |
| 117 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 118 | class VppRoutePath(object): |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 119 | |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 120 | def __init__( |
| 121 | self, |
| 122 | nh_addr, |
| 123 | nh_sw_if_index, |
| 124 | nh_table_id=0, |
| 125 | labels=[], |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 126 | nh_via_label=MPLS_LABEL_INVALID, |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 127 | rpf_id=0, |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 128 | is_interface_rx=0, |
| 129 | is_resolve_host=0, |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 130 | is_resolve_attached=0, |
Neale Ranns | 054c03a | 2017-10-13 05:15:07 -0700 | [diff] [blame] | 131 | is_source_lookup=0, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 132 | is_udp_encap=0, |
Neale Ranns | f068c3e | 2018-01-03 04:18:48 -0800 | [diff] [blame] | 133 | is_dvr=0, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 134 | next_hop_id=0xffffffff, |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 135 | proto=DpoProto.DPO_PROTO_IP4): |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 136 | self.nh_itf = nh_sw_if_index |
| 137 | self.nh_table_id = nh_table_id |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 138 | self.nh_via_label = nh_via_label |
| 139 | self.nh_labels = labels |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 140 | self.weight = 1 |
| 141 | self.rpf_id = rpf_id |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 142 | self.proto = proto |
| 143 | if self.proto is DpoProto.DPO_PROTO_IP6: |
| 144 | self.nh_addr = inet_pton(AF_INET6, nh_addr) |
| 145 | elif self.proto is DpoProto.DPO_PROTO_IP4: |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 146 | self.nh_addr = inet_pton(AF_INET, nh_addr) |
Neale Ranns | 31426c6 | 2017-05-24 10:32:58 -0700 | [diff] [blame] | 147 | else: |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 148 | self.nh_addr = inet_pton(AF_INET6, "::") |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 149 | self.is_resolve_host = is_resolve_host |
| 150 | self.is_resolve_attached = is_resolve_attached |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 151 | self.is_interface_rx = is_interface_rx |
Neale Ranns | 054c03a | 2017-10-13 05:15:07 -0700 | [diff] [blame] | 152 | self.is_source_lookup = is_source_lookup |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 153 | self.is_rpf_id = 0 |
| 154 | if rpf_id != 0: |
| 155 | self.is_rpf_id = 1 |
| 156 | self.nh_itf = rpf_id |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 157 | self.is_udp_encap = is_udp_encap |
| 158 | self.next_hop_id = next_hop_id |
Neale Ranns | f068c3e | 2018-01-03 04:18:48 -0800 | [diff] [blame] | 159 | self.is_dvr = is_dvr |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 160 | |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 161 | def encode_labels(self): |
| 162 | lstack = [] |
| 163 | for l in self.nh_labels: |
| 164 | if type(l) == VppMplsLabel: |
| 165 | lstack.append(l.encode()) |
| 166 | else: |
| 167 | lstack.append({'label': l, |
| 168 | 'ttl': 255}) |
| 169 | return lstack |
| 170 | |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 171 | def encode(self): |
| 172 | return {'next_hop': self.nh_addr, |
| 173 | 'weight': 1, |
| 174 | 'afi': 0, |
| 175 | 'preference': 0, |
| 176 | 'table_id': self.nh_table_id, |
| 177 | 'next_hop_id': self.next_hop_id, |
| 178 | 'sw_if_index': self.nh_itf, |
| 179 | 'afi': self.proto, |
| 180 | 'is_udp_encap': self.is_udp_encap, |
| 181 | 'n_labels': len(self.nh_labels), |
| 182 | 'label_stack': self.encode_labels()} |
| 183 | |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 184 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 185 | class VppMRoutePath(VppRoutePath): |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 186 | |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 187 | def __init__(self, nh_sw_if_index, flags, |
Neale Ranns | e821ab1 | 2017-06-01 07:45:05 -0700 | [diff] [blame] | 188 | nh=None, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 189 | proto=DpoProto.DPO_PROTO_IP4, |
| 190 | bier_imp=0): |
Neale Ranns | e821ab1 | 2017-06-01 07:45:05 -0700 | [diff] [blame] | 191 | if not nh: |
| 192 | nh = "::" if proto is DpoProto.DPO_PROTO_IP6 else "0.0.0.0" |
| 193 | super(VppMRoutePath, self).__init__(nh, |
| 194 | nh_sw_if_index, |
| 195 | proto=proto) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 196 | self.nh_i_flags = flags |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 197 | self.bier_imp = bier_imp |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 198 | |
| 199 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 200 | class VppIpRoute(VppObject): |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 201 | """ |
| 202 | IP Route |
| 203 | """ |
| 204 | |
| 205 | def __init__(self, test, dest_addr, |
Neale Ranns | 37be736 | 2017-02-21 17:30:26 -0800 | [diff] [blame] | 206 | dest_addr_len, paths, table_id=0, is_ip6=0, is_local=0, |
| 207 | is_unreach=0, is_prohibit=0): |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 208 | self._test = test |
| 209 | self.paths = paths |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 210 | self.dest_addr_len = dest_addr_len |
| 211 | self.table_id = table_id |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 212 | self.is_ip6 = is_ip6 |
| 213 | self.is_local = is_local |
Neale Ranns | 37be736 | 2017-02-21 17:30:26 -0800 | [diff] [blame] | 214 | self.is_unreach = is_unreach |
| 215 | self.is_prohibit = is_prohibit |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 216 | self.dest_addr_p = dest_addr |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 217 | if is_ip6: |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 218 | self.dest_addr = inet_pton(AF_INET6, dest_addr) |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 219 | else: |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 220 | self.dest_addr = inet_pton(AF_INET, dest_addr) |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 221 | |
Neale Ranns | 69b7aa4 | 2017-03-10 03:04:12 -0800 | [diff] [blame] | 222 | def modify(self, paths, is_local=0, |
| 223 | is_unreach=0, is_prohibit=0): |
| 224 | self.paths = paths |
| 225 | self.is_local = is_local |
| 226 | self.is_unreach = is_unreach |
| 227 | self.is_prohibit = is_prohibit |
| 228 | |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 229 | def add_vpp_config(self): |
Neale Ranns | 37be736 | 2017-02-21 17:30:26 -0800 | [diff] [blame] | 230 | if self.is_local or self.is_unreach or self.is_prohibit: |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 231 | self._test.vapi.ip_add_del_route( |
| 232 | self.dest_addr, |
| 233 | self.dest_addr_len, |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 234 | inet_pton(AF_INET6, "::"), |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 235 | 0xffffffff, |
Neale Ranns | 37be736 | 2017-02-21 17:30:26 -0800 | [diff] [blame] | 236 | is_local=self.is_local, |
| 237 | is_unreach=self.is_unreach, |
| 238 | is_prohibit=self.is_prohibit, |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 239 | table_id=self.table_id, |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 240 | is_ipv6=self.is_ip6) |
| 241 | else: |
| 242 | for path in self.paths: |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 243 | lstack = path.encode_labels() |
| 244 | |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 245 | self._test.vapi.ip_add_del_route( |
| 246 | self.dest_addr, |
| 247 | self.dest_addr_len, |
| 248 | path.nh_addr, |
| 249 | path.nh_itf, |
| 250 | table_id=self.table_id, |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 251 | next_hop_out_label_stack=lstack, |
| 252 | next_hop_n_out_labels=len(lstack), |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 253 | next_hop_via_label=path.nh_via_label, |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 254 | next_hop_table_id=path.nh_table_id, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 255 | next_hop_id=path.next_hop_id, |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 256 | is_ipv6=self.is_ip6, |
Neale Ranns | f068c3e | 2018-01-03 04:18:48 -0800 | [diff] [blame] | 257 | is_dvr=path.is_dvr, |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 258 | is_resolve_host=path.is_resolve_host, |
| 259 | is_resolve_attached=path.is_resolve_attached, |
Neale Ranns | 054c03a | 2017-10-13 05:15:07 -0700 | [diff] [blame] | 260 | is_source_lookup=path.is_source_lookup, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 261 | is_udp_encap=path.is_udp_encap, |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 262 | is_multipath=1 if len(self.paths) > 1 else 0) |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 263 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 264 | |
| 265 | def remove_vpp_config(self): |
Neale Ranns | 37be736 | 2017-02-21 17:30:26 -0800 | [diff] [blame] | 266 | if self.is_local or self.is_unreach or self.is_prohibit: |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 267 | self._test.vapi.ip_add_del_route( |
| 268 | self.dest_addr, |
| 269 | self.dest_addr_len, |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 270 | inet_pton(AF_INET6, "::"), |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 271 | 0xffffffff, |
Neale Ranns | 37be736 | 2017-02-21 17:30:26 -0800 | [diff] [blame] | 272 | is_local=self.is_local, |
| 273 | is_unreach=self.is_unreach, |
| 274 | is_prohibit=self.is_prohibit, |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 275 | is_add=0, |
| 276 | table_id=self.table_id, |
| 277 | is_ipv6=self.is_ip6) |
| 278 | else: |
| 279 | for path in self.paths: |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 280 | self._test.vapi.ip_add_del_route( |
| 281 | self.dest_addr, |
| 282 | self.dest_addr_len, |
| 283 | path.nh_addr, |
| 284 | path.nh_itf, |
| 285 | table_id=self.table_id, |
| 286 | next_hop_table_id=path.nh_table_id, |
| 287 | next_hop_via_label=path.nh_via_label, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 288 | next_hop_id=path.next_hop_id, |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 289 | is_add=0, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 290 | is_udp_encap=path.is_udp_encap, |
Neale Ranns | f068c3e | 2018-01-03 04:18:48 -0800 | [diff] [blame] | 291 | is_ipv6=self.is_ip6, |
| 292 | is_dvr=path.is_dvr) |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 293 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 294 | def query_vpp_config(self): |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 295 | return find_route(self._test, |
| 296 | self.dest_addr_p, |
| 297 | self.dest_addr_len, |
| 298 | self.table_id, |
| 299 | inet=AF_INET6 if self.is_ip6 == 1 else AF_INET) |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 300 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 301 | def __str__(self): |
| 302 | return self.object_id() |
| 303 | |
| 304 | def object_id(self): |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 305 | return ("%d:%s/%d" |
| 306 | % (self.table_id, |
| 307 | self.dest_addr_p, |
| 308 | self.dest_addr_len)) |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 309 | |
| 310 | |
| 311 | class VppIpMRoute(VppObject): |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 312 | """ |
| 313 | IP Multicast Route |
| 314 | """ |
| 315 | |
| 316 | def __init__(self, test, src_addr, grp_addr, |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 317 | grp_addr_len, e_flags, paths, table_id=0, |
| 318 | rpf_id=0, is_ip6=0): |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 319 | self._test = test |
| 320 | self.paths = paths |
| 321 | self.grp_addr_len = grp_addr_len |
| 322 | self.table_id = table_id |
| 323 | self.e_flags = e_flags |
| 324 | self.is_ip6 = is_ip6 |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 325 | self.rpf_id = rpf_id |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 326 | |
| 327 | if is_ip6: |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 328 | self.grp_addr = inet_pton(AF_INET6, grp_addr) |
| 329 | self.src_addr = inet_pton(AF_INET6, src_addr) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 330 | else: |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 331 | self.grp_addr = inet_pton(AF_INET, grp_addr) |
| 332 | self.src_addr = inet_pton(AF_INET, src_addr) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 333 | |
| 334 | def add_vpp_config(self): |
| 335 | for path in self.paths: |
| 336 | self._test.vapi.ip_mroute_add_del(self.src_addr, |
| 337 | self.grp_addr, |
| 338 | self.grp_addr_len, |
| 339 | self.e_flags, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 340 | path.proto, |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 341 | path.nh_itf, |
Neale Ranns | e821ab1 | 2017-06-01 07:45:05 -0700 | [diff] [blame] | 342 | path.nh_addr, |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 343 | path.nh_i_flags, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 344 | bier_imp=path.bier_imp, |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 345 | rpf_id=self.rpf_id, |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 346 | table_id=self.table_id, |
| 347 | is_ipv6=self.is_ip6) |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 348 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 349 | |
| 350 | def remove_vpp_config(self): |
| 351 | for path in self.paths: |
| 352 | self._test.vapi.ip_mroute_add_del(self.src_addr, |
| 353 | self.grp_addr, |
| 354 | self.grp_addr_len, |
| 355 | self.e_flags, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 356 | path.proto, |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 357 | path.nh_itf, |
Neale Ranns | e821ab1 | 2017-06-01 07:45:05 -0700 | [diff] [blame] | 358 | path.nh_addr, |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 359 | path.nh_i_flags, |
| 360 | table_id=self.table_id, |
Neale Ranns | eea537a | 2018-01-09 04:11:28 -0800 | [diff] [blame] | 361 | bier_imp=path.bier_imp, |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 362 | is_add=0, |
| 363 | is_ipv6=self.is_ip6) |
| 364 | |
| 365 | def update_entry_flags(self, flags): |
| 366 | self.e_flags = flags |
| 367 | self._test.vapi.ip_mroute_add_del(self.src_addr, |
| 368 | self.grp_addr, |
| 369 | self.grp_addr_len, |
| 370 | self.e_flags, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 371 | 0, |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 372 | 0xffffffff, |
Neale Ranns | e821ab1 | 2017-06-01 07:45:05 -0700 | [diff] [blame] | 373 | "", |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 374 | 0, |
| 375 | table_id=self.table_id, |
| 376 | is_ipv6=self.is_ip6) |
| 377 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 378 | def update_rpf_id(self, rpf_id): |
| 379 | self.rpf_id = rpf_id |
| 380 | self._test.vapi.ip_mroute_add_del(self.src_addr, |
| 381 | self.grp_addr, |
| 382 | self.grp_addr_len, |
| 383 | self.e_flags, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 384 | 0, |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 385 | 0xffffffff, |
Neale Ranns | e821ab1 | 2017-06-01 07:45:05 -0700 | [diff] [blame] | 386 | "", |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 387 | 0, |
| 388 | rpf_id=self.rpf_id, |
| 389 | table_id=self.table_id, |
| 390 | is_ipv6=self.is_ip6) |
| 391 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 392 | def update_path_flags(self, itf, flags): |
| 393 | for path in self.paths: |
| 394 | if path.nh_itf == itf: |
| 395 | path.nh_i_flags = flags |
| 396 | break |
| 397 | self._test.vapi.ip_mroute_add_del(self.src_addr, |
| 398 | self.grp_addr, |
| 399 | self.grp_addr_len, |
| 400 | self.e_flags, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 401 | path.proto, |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 402 | path.nh_itf, |
Neale Ranns | e821ab1 | 2017-06-01 07:45:05 -0700 | [diff] [blame] | 403 | path.nh_addr, |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 404 | path.nh_i_flags, |
| 405 | table_id=self.table_id, |
| 406 | is_ipv6=self.is_ip6) |
| 407 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 408 | def query_vpp_config(self): |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 409 | if self.is_ip6: |
| 410 | dump = self._test.vapi.ip6_mfib_dump() |
| 411 | else: |
| 412 | dump = self._test.vapi.ip_mfib_dump() |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 413 | for e in dump: |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 414 | if self.grp_addr == e.grp_address \ |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 415 | and self.grp_addr_len == e.address_length \ |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 416 | and self.src_addr == e.src_address \ |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 417 | and self.table_id == e.table_id: |
| 418 | return True |
| 419 | return False |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 420 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 421 | def __str__(self): |
| 422 | return self.object_id() |
| 423 | |
| 424 | def object_id(self): |
| 425 | if self.is_ip6: |
| 426 | return ("%d:(%s,%s/%d)" |
| 427 | % (self.table_id, |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 428 | inet_ntop(AF_INET6, self.src_addr), |
| 429 | inet_ntop(AF_INET6, self.grp_addr), |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 430 | self.grp_addr_len)) |
| 431 | else: |
| 432 | return ("%d:(%s,%s/%d)" |
| 433 | % (self.table_id, |
Neale Ranns | b3b2de7 | 2017-03-08 05:17:22 -0800 | [diff] [blame] | 434 | inet_ntop(AF_INET, self.src_addr), |
| 435 | inet_ntop(AF_INET, self.grp_addr), |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 436 | self.grp_addr_len)) |
| 437 | |
| 438 | |
| 439 | class VppMFibSignal(object): |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 440 | def __init__(self, test, route, interface, packet): |
| 441 | self.route = route |
| 442 | self.interface = interface |
| 443 | self.packet = packet |
| 444 | self.test = test |
| 445 | |
| 446 | def compare(self, signal): |
| 447 | self.test.assertEqual(self.interface, signal.sw_if_index) |
| 448 | self.test.assertEqual(self.route.table_id, signal.table_id) |
| 449 | self.test.assertEqual(self.route.grp_addr_len, |
| 450 | signal.grp_address_len) |
| 451 | for i in range(self.route.grp_addr_len / 8): |
| 452 | self.test.assertEqual(self.route.grp_addr[i], |
| 453 | signal.grp_address[i]) |
| 454 | if (self.route.grp_addr_len > 32): |
| 455 | for i in range(4): |
| 456 | self.test.assertEqual(self.route.src_addr[i], |
| 457 | signal.src_address[i]) |
| 458 | |
| 459 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 460 | class VppMplsIpBind(VppObject): |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 461 | """ |
| 462 | MPLS to IP Binding |
| 463 | """ |
| 464 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 465 | def __init__(self, test, local_label, dest_addr, dest_addr_len, |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 466 | table_id=0, ip_table_id=0, is_ip6=0): |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 467 | self._test = test |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 468 | self.dest_addr_len = dest_addr_len |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 469 | self.dest_addr = dest_addr |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 470 | self.local_label = local_label |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 471 | self.table_id = table_id |
| 472 | self.ip_table_id = ip_table_id |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 473 | self.is_ip6 = is_ip6 |
| 474 | if is_ip6: |
| 475 | self.dest_addrn = inet_pton(AF_INET6, dest_addr) |
| 476 | else: |
| 477 | self.dest_addrn = inet_pton(AF_INET, dest_addr) |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 478 | |
| 479 | def add_vpp_config(self): |
| 480 | self._test.vapi.mpls_ip_bind_unbind(self.local_label, |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 481 | self.dest_addrn, |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 482 | self.dest_addr_len, |
| 483 | table_id=self.table_id, |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 484 | ip_table_id=self.ip_table_id, |
| 485 | is_ip4=(self.is_ip6 == 0)) |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 486 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 487 | |
| 488 | def remove_vpp_config(self): |
| 489 | self._test.vapi.mpls_ip_bind_unbind(self.local_label, |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 490 | self.dest_addrn, |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 491 | self.dest_addr_len, |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 492 | table_id=self.table_id, |
| 493 | ip_table_id=self.ip_table_id, |
| 494 | is_bind=0, |
| 495 | is_ip4=(self.is_ip6 == 0)) |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 496 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 497 | def query_vpp_config(self): |
| 498 | dump = self._test.vapi.mpls_fib_dump() |
| 499 | for e in dump: |
| 500 | if self.local_label == e.label \ |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 501 | and self.table_id == e.table_id: |
| 502 | return True |
| 503 | return False |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 504 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 505 | def __str__(self): |
| 506 | return self.object_id() |
| 507 | |
| 508 | def object_id(self): |
| 509 | return ("%d:%s binds %d:%s/%d" |
| 510 | % (self.table_id, |
| 511 | self.local_label, |
| 512 | self.ip_table_id, |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 513 | self.dest_addr, |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 514 | self.dest_addr_len)) |
| 515 | |
| 516 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 517 | class VppMplsTable(VppObject): |
| 518 | |
| 519 | def __init__(self, |
| 520 | test, |
| 521 | table_id): |
| 522 | self._test = test |
| 523 | self.table_id = table_id |
| 524 | |
| 525 | def add_vpp_config(self): |
| 526 | self._test.vapi.mpls_table_add_del( |
| 527 | self.table_id, |
| 528 | is_add=1) |
| 529 | self._test.registry.register(self, self._test.logger) |
| 530 | |
| 531 | def remove_vpp_config(self): |
| 532 | self._test.vapi.mpls_table_add_del( |
| 533 | self.table_id, |
| 534 | is_add=0) |
| 535 | |
| 536 | def query_vpp_config(self): |
| 537 | # find the default route |
| 538 | dump = self._test.vapi.mpls_fib_dump() |
| 539 | if len(dump): |
| 540 | return True |
| 541 | return False |
| 542 | |
| 543 | def __str__(self): |
| 544 | return self.object_id() |
| 545 | |
| 546 | def object_id(self): |
| 547 | return ("table-mpls-%d" % (self.table_id)) |
| 548 | |
| 549 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 550 | class VppMplsRoute(VppObject): |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 551 | """ |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 552 | MPLS Route/LSP |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 553 | """ |
| 554 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 555 | def __init__(self, test, local_label, eos_bit, paths, table_id=0, |
| 556 | is_multicast=0): |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 557 | self._test = test |
| 558 | self.paths = paths |
| 559 | self.local_label = local_label |
| 560 | self.eos_bit = eos_bit |
| 561 | self.table_id = table_id |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 562 | self.is_multicast = is_multicast |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 563 | |
| 564 | def add_vpp_config(self): |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 565 | is_multipath = len(self.paths) > 1 |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 566 | for path in self.paths: |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 567 | lstack = path.encode_labels() |
| 568 | |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 569 | self._test.vapi.mpls_route_add_del( |
| 570 | self.local_label, |
| 571 | self.eos_bit, |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 572 | path.proto, |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 573 | path.nh_addr, |
| 574 | path.nh_itf, |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 575 | is_multicast=self.is_multicast, |
| 576 | is_multipath=is_multipath, |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 577 | table_id=self.table_id, |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 578 | is_interface_rx=path.is_interface_rx, |
| 579 | is_rpf_id=path.is_rpf_id, |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 580 | next_hop_out_label_stack=lstack, |
| 581 | next_hop_n_out_labels=len(lstack), |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 582 | next_hop_via_label=path.nh_via_label, |
| 583 | next_hop_table_id=path.nh_table_id) |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 584 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 585 | |
| 586 | def remove_vpp_config(self): |
| 587 | for path in self.paths: |
| 588 | self._test.vapi.mpls_route_add_del(self.local_label, |
| 589 | self.eos_bit, |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 590 | path.proto, |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 591 | path.nh_addr, |
| 592 | path.nh_itf, |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 593 | is_rpf_id=path.is_rpf_id, |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 594 | table_id=self.table_id, |
| 595 | is_add=0) |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 596 | |
| 597 | def query_vpp_config(self): |
| 598 | dump = self._test.vapi.mpls_fib_dump() |
| 599 | for e in dump: |
| 600 | if self.local_label == e.label \ |
| 601 | and self.eos_bit == e.eos_bit \ |
| 602 | and self.table_id == e.table_id: |
| 603 | return True |
| 604 | return False |
| 605 | |
| 606 | def __str__(self): |
| 607 | return self.object_id() |
| 608 | |
| 609 | def object_id(self): |
| 610 | return ("%d:%s/%d" |
| 611 | % (self.table_id, |
| 612 | self.local_label, |
| 613 | 20+self.eos_bit)) |