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