Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * sr_steering.c: ipv6 segment routing steering into SR policy |
| 3 | * |
| 4 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * @file |
| 20 | * @brief Packet steering into SR Policies |
| 21 | * |
| 22 | * This file is in charge of handling the FIB appropiatly to steer packets |
| 23 | * through SR Policies as defined in 'sr_policy_rewrite.c'. Notice that here |
| 24 | * we are only doing steering. SR policy application is done in |
| 25 | * sr_policy_rewrite.c |
| 26 | * |
| 27 | * Supports: |
| 28 | * - Steering of IPv6 traffic Destination Address based |
| 29 | * - Steering of IPv4 traffic Destination Address based |
| 30 | * - Steering of L2 frames, interface based (sw interface) |
| 31 | */ |
| 32 | |
| 33 | #include <vlib/vlib.h> |
| 34 | #include <vnet/vnet.h> |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 35 | #include <vnet/srv6/sr.h> |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 36 | #include <vnet/ip/ip.h> |
Pablo Camarillo | 5d73eec | 2017-04-24 17:51:56 +0200 | [diff] [blame] | 37 | #include <vnet/srv6/sr_packet.h> |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 38 | #include <vnet/ip/ip6_packet.h> |
| 39 | #include <vnet/fib/ip6_fib.h> |
| 40 | #include <vnet/dpo/dpo.h> |
| 41 | |
| 42 | #include <vppinfra/error.h> |
| 43 | #include <vppinfra/elog.h> |
| 44 | |
| 45 | /** |
| 46 | * @brief Steer traffic L2 and L3 traffic through a given SR policy |
| 47 | * |
| 48 | * @param is_del |
| 49 | * @param bsid is the bindingSID of the SR Policy (alt to sr_policy_index) |
| 50 | * @param sr_policy is the index of the SR Policy (alt to bsid) |
| 51 | * @param table_id is the VRF where to install the FIB entry for the BSID |
| 52 | * @param prefix is the IPv4/v6 address for L3 traffic type |
| 53 | * @param mask_width is the mask for L3 traffic type |
| 54 | * @param sw_if_index is the incoming interface for L2 traffic |
| 55 | * @param traffic_type describes the type of traffic |
| 56 | * |
| 57 | * @return 0 if correct, else error |
| 58 | */ |
| 59 | int |
| 60 | sr_steering_policy (int is_del, ip6_address_t * bsid, u32 sr_policy_index, |
| 61 | u32 table_id, ip46_address_t * prefix, u32 mask_width, |
| 62 | u32 sw_if_index, u8 traffic_type) |
| 63 | { |
| 64 | ip6_sr_main_t *sm = &sr_main; |
Pablo Camarillo | 4521afa | 2017-03-16 10:43:05 +0100 | [diff] [blame] | 65 | sr_steering_key_t key; |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 66 | ip6_sr_steering_policy_t *steer_pl; |
| 67 | fib_prefix_t pfx = { 0 }; |
| 68 | |
| 69 | ip6_sr_policy_t *sr_policy = 0; |
| 70 | uword *p = 0; |
| 71 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 72 | clib_memset (&key, 0, sizeof (sr_steering_key_t)); |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 73 | |
| 74 | /* Compute the steer policy key */ |
Pablo Camarillo | adcaaf1 | 2017-03-29 07:54:40 -0700 | [diff] [blame] | 75 | if (traffic_type == SR_STEER_IPV4 || traffic_type == SR_STEER_IPV6) |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 76 | { |
| 77 | key.l3.prefix.as_u64[0] = prefix->as_u64[0]; |
| 78 | key.l3.prefix.as_u64[1] = prefix->as_u64[1]; |
| 79 | key.l3.mask_width = mask_width; |
| 80 | key.l3.fib_table = (table_id != (u32) ~ 0 ? table_id : 0); |
| 81 | } |
Pablo Camarillo | adcaaf1 | 2017-03-29 07:54:40 -0700 | [diff] [blame] | 82 | else if (traffic_type == SR_STEER_L2) |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 83 | { |
| 84 | key.l2.sw_if_index = sw_if_index; |
| 85 | |
| 86 | /* Sanitise the SW_IF_INDEX */ |
| 87 | if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces, |
| 88 | sw_if_index)) |
| 89 | return -3; |
| 90 | |
| 91 | vnet_sw_interface_t *sw = |
| 92 | vnet_get_sw_interface (sm->vnet_main, sw_if_index); |
| 93 | if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE) |
| 94 | return -3; |
| 95 | } |
Pablo Camarillo | adcaaf1 | 2017-03-29 07:54:40 -0700 | [diff] [blame] | 96 | else |
| 97 | return -1; |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 98 | |
| 99 | key.traffic_type = traffic_type; |
| 100 | |
| 101 | /* Search for the item */ |
Pablo Camarillo | 4521afa | 2017-03-16 10:43:05 +0100 | [diff] [blame] | 102 | p = mhash_get (&sm->sr_steer_policies_hash, &key); |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 103 | |
| 104 | if (p) |
| 105 | { |
| 106 | /* Retrieve Steer Policy function */ |
| 107 | steer_pl = pool_elt_at_index (sm->steer_policies, p[0]); |
| 108 | |
| 109 | if (is_del) |
| 110 | { |
| 111 | if (steer_pl->classify.traffic_type == SR_STEER_IPV6) |
| 112 | { |
| 113 | /* Remove FIB entry */ |
| 114 | pfx.fp_proto = FIB_PROTOCOL_IP6; |
| 115 | pfx.fp_len = steer_pl->classify.l3.mask_width; |
| 116 | pfx.fp_addr.ip6 = steer_pl->classify.l3.prefix.ip6; |
| 117 | |
Neale Ranns | 107e7d4 | 2017-04-11 09:55:19 -0700 | [diff] [blame] | 118 | fib_table_entry_delete (fib_table_find |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 119 | (FIB_PROTOCOL_IP6, |
Neale Ranns | 107e7d4 | 2017-04-11 09:55:19 -0700 | [diff] [blame] | 120 | steer_pl->classify.l3.fib_table), |
| 121 | &pfx, FIB_SOURCE_SR); |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 122 | } |
| 123 | else if (steer_pl->classify.traffic_type == SR_STEER_IPV4) |
| 124 | { |
| 125 | /* Remove FIB entry */ |
| 126 | pfx.fp_proto = FIB_PROTOCOL_IP4; |
| 127 | pfx.fp_len = steer_pl->classify.l3.mask_width; |
| 128 | pfx.fp_addr.ip4 = steer_pl->classify.l3.prefix.ip4; |
| 129 | |
Neale Ranns | 107e7d4 | 2017-04-11 09:55:19 -0700 | [diff] [blame] | 130 | fib_table_entry_delete (fib_table_find |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 131 | (FIB_PROTOCOL_IP4, |
| 132 | steer_pl->classify.l3.fib_table), &pfx, |
| 133 | FIB_SOURCE_SR); |
| 134 | } |
| 135 | else if (steer_pl->classify.traffic_type == SR_STEER_L2) |
| 136 | { |
| 137 | /* Remove HW redirection */ |
Ahmed Abdelsalam | a6b93ea | 2019-10-08 16:27:34 +0000 | [diff] [blame] | 138 | int ret = vnet_feature_enable_disable ("device-input", |
| 139 | "sr-pl-rewrite-encaps-l2", |
| 140 | sw_if_index, 0, 0, 0); |
| 141 | |
| 142 | if (ret != 0) |
| 143 | return -1; |
| 144 | |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 145 | sm->sw_iface_sr_policies[sw_if_index] = ~(u32) 0; |
| 146 | |
| 147 | /* Remove promiscous mode from interface */ |
| 148 | vnet_main_t *vnm = vnet_get_main (); |
| 149 | ethernet_main_t *em = ðernet_main; |
| 150 | ethernet_interface_t *eif = |
| 151 | ethernet_get_interface (em, sw_if_index); |
| 152 | |
| 153 | if (!eif) |
| 154 | goto cleanup_error_redirection; |
| 155 | |
| 156 | ethernet_set_flags (vnm, sw_if_index, 0); |
| 157 | } |
| 158 | |
| 159 | /* Delete SR steering policy entry */ |
| 160 | pool_put (sm->steer_policies, steer_pl); |
Pablo Camarillo | 4521afa | 2017-03-16 10:43:05 +0100 | [diff] [blame] | 161 | mhash_unset (&sm->sr_steer_policies_hash, &key, NULL); |
| 162 | |
| 163 | /* If no more SR policies or steering policies */ |
| 164 | if (!pool_elts (sm->sr_policies) && !pool_elts (sm->steer_policies)) |
| 165 | { |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 166 | fib_table_unlock (sm->fib_table_ip6, |
| 167 | FIB_PROTOCOL_IP6, FIB_SOURCE_SR); |
| 168 | fib_table_unlock (sm->fib_table_ip4, |
| 169 | FIB_PROTOCOL_IP6, FIB_SOURCE_SR); |
Pablo Camarillo | 4521afa | 2017-03-16 10:43:05 +0100 | [diff] [blame] | 170 | sm->fib_table_ip6 = (u32) ~ 0; |
| 171 | sm->fib_table_ip4 = (u32) ~ 0; |
| 172 | } |
| 173 | |
Kris Michielsen | 9107443 | 2017-06-22 13:00:20 +0200 | [diff] [blame] | 174 | return 0; |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 175 | } |
| 176 | else /* It means user requested to update an existing SR steering policy */ |
| 177 | { |
| 178 | /* Retrieve SR steering policy */ |
| 179 | if (bsid) |
| 180 | { |
Pablo Camarillo | 4521afa | 2017-03-16 10:43:05 +0100 | [diff] [blame] | 181 | p = mhash_get (&sm->sr_policies_index_hash, bsid); |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 182 | if (p) |
| 183 | sr_policy = pool_elt_at_index (sm->sr_policies, p[0]); |
| 184 | else |
| 185 | return -2; |
| 186 | } |
| 187 | else |
| 188 | sr_policy = pool_elt_at_index (sm->sr_policies, sr_policy_index); |
| 189 | |
| 190 | if (!sr_policy) |
| 191 | return -2; |
| 192 | |
| 193 | steer_pl->sr_policy = sr_policy - sm->sr_policies; |
| 194 | |
| 195 | /* Remove old FIB/hw redirection and create a new one */ |
| 196 | if (steer_pl->classify.traffic_type == SR_STEER_IPV6) |
| 197 | { |
| 198 | /* Remove FIB entry */ |
| 199 | pfx.fp_proto = FIB_PROTOCOL_IP6; |
| 200 | pfx.fp_len = steer_pl->classify.l3.mask_width; |
| 201 | pfx.fp_addr.ip6 = steer_pl->classify.l3.prefix.ip6; |
| 202 | |
Neale Ranns | 107e7d4 | 2017-04-11 09:55:19 -0700 | [diff] [blame] | 203 | fib_table_entry_delete (fib_table_find |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 204 | (FIB_PROTOCOL_IP6, |
Neale Ranns | 107e7d4 | 2017-04-11 09:55:19 -0700 | [diff] [blame] | 205 | steer_pl->classify.l3.fib_table), |
| 206 | &pfx, FIB_SOURCE_SR); |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 207 | |
| 208 | /* Create a new one */ |
| 209 | goto update_fib; |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 210 | } |
| 211 | else if (steer_pl->classify.traffic_type == SR_STEER_IPV4) |
| 212 | { |
| 213 | /* Remove FIB entry */ |
| 214 | pfx.fp_proto = FIB_PROTOCOL_IP4; |
| 215 | pfx.fp_len = steer_pl->classify.l3.mask_width; |
| 216 | pfx.fp_addr.ip4 = steer_pl->classify.l3.prefix.ip4; |
| 217 | |
Neale Ranns | 107e7d4 | 2017-04-11 09:55:19 -0700 | [diff] [blame] | 218 | fib_table_entry_delete (fib_table_find |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 219 | (FIB_PROTOCOL_IP4, |
Neale Ranns | 107e7d4 | 2017-04-11 09:55:19 -0700 | [diff] [blame] | 220 | steer_pl->classify.l3.fib_table), |
| 221 | &pfx, FIB_SOURCE_SR); |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 222 | |
| 223 | /* Create a new one */ |
| 224 | goto update_fib; |
| 225 | } |
| 226 | else if (steer_pl->classify.traffic_type == SR_STEER_L2) |
| 227 | { |
| 228 | /* Update L2-HW redirection */ |
| 229 | goto update_fib; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | else |
| 234 | /* delete; steering policy does not exist; complain */ |
| 235 | if (is_del) |
| 236 | return -4; |
| 237 | |
| 238 | /* Retrieve SR policy */ |
| 239 | if (bsid) |
| 240 | { |
Pablo Camarillo | 4521afa | 2017-03-16 10:43:05 +0100 | [diff] [blame] | 241 | p = mhash_get (&sm->sr_policies_index_hash, bsid); |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 242 | if (p) |
| 243 | sr_policy = pool_elt_at_index (sm->sr_policies, p[0]); |
| 244 | else |
| 245 | return -2; |
| 246 | } |
| 247 | else |
| 248 | sr_policy = pool_elt_at_index (sm->sr_policies, sr_policy_index); |
| 249 | |
| 250 | /* Create a new steering policy */ |
| 251 | pool_get (sm->steer_policies, steer_pl); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 252 | clib_memset (steer_pl, 0, sizeof (*steer_pl)); |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 253 | |
| 254 | if (traffic_type == SR_STEER_IPV4 || traffic_type == SR_STEER_IPV6) |
| 255 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 256 | clib_memcpy_fast (&steer_pl->classify.l3.prefix, prefix, |
| 257 | sizeof (ip46_address_t)); |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 258 | steer_pl->classify.l3.mask_width = mask_width; |
| 259 | steer_pl->classify.l3.fib_table = |
| 260 | (table_id != (u32) ~ 0 ? table_id : 0); |
| 261 | steer_pl->classify.traffic_type = traffic_type; |
| 262 | } |
| 263 | else if (traffic_type == SR_STEER_L2) |
| 264 | { |
| 265 | steer_pl->classify.l2.sw_if_index = sw_if_index; |
| 266 | steer_pl->classify.traffic_type = traffic_type; |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | /* Incorrect API usage. Should never get here */ |
| 271 | pool_put (sm->steer_policies, steer_pl); |
Pablo Camarillo | 4521afa | 2017-03-16 10:43:05 +0100 | [diff] [blame] | 272 | mhash_unset (&sm->sr_steer_policies_hash, &key, NULL); |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 273 | return -1; |
| 274 | } |
| 275 | steer_pl->sr_policy = sr_policy - sm->sr_policies; |
| 276 | |
| 277 | /* Create and store key */ |
Pablo Camarillo | 4521afa | 2017-03-16 10:43:05 +0100 | [diff] [blame] | 278 | mhash_set (&sm->sr_steer_policies_hash, &key, steer_pl - sm->steer_policies, |
| 279 | NULL); |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 280 | |
| 281 | if (traffic_type == SR_STEER_L2) |
| 282 | { |
| 283 | if (!sr_policy->is_encap) |
| 284 | goto cleanup_error_encap; |
| 285 | |
| 286 | if (vnet_feature_enable_disable |
Pablo Camarillo | adcaaf1 | 2017-03-29 07:54:40 -0700 | [diff] [blame] | 287 | ("device-input", "sr-pl-rewrite-encaps-l2", sw_if_index, 1, 0, 0)) |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 288 | goto cleanup_error_redirection; |
| 289 | |
| 290 | /* Set promiscous mode on interface */ |
| 291 | vnet_main_t *vnm = vnet_get_main (); |
| 292 | ethernet_main_t *em = ðernet_main; |
| 293 | ethernet_interface_t *eif = ethernet_get_interface (em, sw_if_index); |
| 294 | |
| 295 | if (!eif) |
| 296 | goto cleanup_error_redirection; |
| 297 | |
| 298 | ethernet_set_flags (vnm, sw_if_index, |
| 299 | ETHERNET_INTERFACE_FLAG_ACCEPT_ALL); |
| 300 | } |
| 301 | else if (traffic_type == SR_STEER_IPV4) |
| 302 | if (!sr_policy->is_encap) |
| 303 | goto cleanup_error_encap; |
| 304 | |
| 305 | update_fib: |
| 306 | /* FIB API calls - Recursive route through the BindingSID */ |
| 307 | if (traffic_type == SR_STEER_IPV6) |
| 308 | { |
| 309 | pfx.fp_proto = FIB_PROTOCOL_IP6; |
| 310 | pfx.fp_len = steer_pl->classify.l3.mask_width; |
| 311 | pfx.fp_addr.ip6 = steer_pl->classify.l3.prefix.ip6; |
| 312 | |
Neale Ranns | 107e7d4 | 2017-04-11 09:55:19 -0700 | [diff] [blame] | 313 | fib_table_entry_path_add (fib_table_find (FIB_PROTOCOL_IP6, |
| 314 | (table_id != |
| 315 | (u32) ~ 0 ? |
| 316 | table_id : 0)), |
shwethab | b18e0de | 2017-03-29 11:36:37 +0000 | [diff] [blame] | 317 | &pfx, FIB_SOURCE_SR, |
| 318 | FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT, |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 319 | DPO_PROTO_IP6, |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 320 | (ip46_address_t *) & sr_policy->bsid, ~0, |
| 321 | sm->fib_table_ip6, 1, NULL, |
| 322 | FIB_ROUTE_PATH_FLAG_NONE); |
| 323 | } |
| 324 | else if (traffic_type == SR_STEER_IPV4) |
| 325 | { |
| 326 | pfx.fp_proto = FIB_PROTOCOL_IP4; |
| 327 | pfx.fp_len = steer_pl->classify.l3.mask_width; |
| 328 | pfx.fp_addr.ip4 = steer_pl->classify.l3.prefix.ip4; |
| 329 | |
Neale Ranns | 107e7d4 | 2017-04-11 09:55:19 -0700 | [diff] [blame] | 330 | fib_table_entry_path_add (fib_table_find (FIB_PROTOCOL_IP4, |
| 331 | (table_id != |
| 332 | (u32) ~ 0 ? |
| 333 | table_id : 0)), |
shwethab | b18e0de | 2017-03-29 11:36:37 +0000 | [diff] [blame] | 334 | &pfx, FIB_SOURCE_SR, |
| 335 | FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT, |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 336 | DPO_PROTO_IP6, |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 337 | (ip46_address_t *) & sr_policy->bsid, ~0, |
| 338 | sm->fib_table_ip4, 1, NULL, |
| 339 | FIB_ROUTE_PATH_FLAG_NONE); |
| 340 | } |
| 341 | else if (traffic_type == SR_STEER_L2) |
| 342 | { |
| 343 | if (sw_if_index < vec_len (sm->sw_iface_sr_policies)) |
| 344 | sm->sw_iface_sr_policies[sw_if_index] = steer_pl->sr_policy; |
| 345 | else |
| 346 | { |
| 347 | vec_resize (sm->sw_iface_sr_policies, |
| 348 | (pool_len (sm->vnet_main->interface_main.sw_interfaces) |
| 349 | - vec_len (sm->sw_iface_sr_policies))); |
| 350 | sm->sw_iface_sr_policies[sw_if_index] = steer_pl->sr_policy; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return 0; |
| 355 | |
| 356 | cleanup_error_encap: |
| 357 | pool_put (sm->steer_policies, steer_pl); |
Pablo Camarillo | 4521afa | 2017-03-16 10:43:05 +0100 | [diff] [blame] | 358 | mhash_unset (&sm->sr_steer_policies_hash, &key, NULL); |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 359 | return -5; |
| 360 | |
| 361 | cleanup_error_redirection: |
| 362 | pool_put (sm->steer_policies, steer_pl); |
Pablo Camarillo | 4521afa | 2017-03-16 10:43:05 +0100 | [diff] [blame] | 363 | mhash_unset (&sm->sr_steer_policies_hash, &key, NULL); |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 364 | return -3; |
| 365 | } |
| 366 | |
| 367 | static clib_error_t * |
| 368 | sr_steer_policy_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 369 | vlib_cli_command_t * cmd) |
| 370 | { |
| 371 | vnet_main_t *vnm = vnet_get_main (); |
| 372 | |
| 373 | int is_del = 0; |
| 374 | |
| 375 | ip46_address_t prefix; |
| 376 | u32 dst_mask_width = 0; |
| 377 | u32 sw_if_index = (u32) ~ 0; |
| 378 | u8 traffic_type = 0; |
| 379 | u32 fib_table = (u32) ~ 0; |
| 380 | |
| 381 | ip6_address_t bsid; |
| 382 | u32 sr_policy_index = (u32) ~ 0; |
| 383 | |
| 384 | u8 sr_policy_set = 0; |
| 385 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 386 | clib_memset (&prefix, 0, sizeof (ip46_address_t)); |
Pablo Camarillo | 4521afa | 2017-03-16 10:43:05 +0100 | [diff] [blame] | 387 | |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 388 | int rv; |
| 389 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 390 | { |
| 391 | if (unformat (input, "del")) |
| 392 | is_del = 1; |
| 393 | else if (!traffic_type |
| 394 | && unformat (input, "l3 %U/%d", unformat_ip6_address, |
| 395 | &prefix.ip6, &dst_mask_width)) |
| 396 | traffic_type = SR_STEER_IPV6; |
| 397 | else if (!traffic_type |
| 398 | && unformat (input, "l3 %U/%d", unformat_ip4_address, |
| 399 | &prefix.ip4, &dst_mask_width)) |
| 400 | traffic_type = SR_STEER_IPV4; |
| 401 | else if (!traffic_type |
| 402 | && unformat (input, "l2 %U", unformat_vnet_sw_interface, vnm, |
| 403 | &sw_if_index)) |
| 404 | traffic_type = SR_STEER_L2; |
| 405 | else if (!sr_policy_set |
Francois Clad | 4abc5c3 | 2018-02-01 14:51:33 +0100 | [diff] [blame] | 406 | && unformat (input, "via index %d", &sr_policy_index)) |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 407 | sr_policy_set = 1; |
| 408 | else if (!sr_policy_set |
Francois Clad | 4abc5c3 | 2018-02-01 14:51:33 +0100 | [diff] [blame] | 409 | && unformat (input, "via bsid %U", |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 410 | unformat_ip6_address, &bsid)) |
| 411 | sr_policy_set = 1; |
| 412 | else if (fib_table == (u32) ~ 0 |
| 413 | && unformat (input, "fib-table %d", &fib_table)); |
| 414 | else |
| 415 | break; |
| 416 | } |
| 417 | |
| 418 | if (!traffic_type) |
| 419 | return clib_error_return (0, "No L2/L3 traffic specified"); |
| 420 | if (!sr_policy_set) |
| 421 | return clib_error_return (0, "No SR policy specified"); |
| 422 | |
| 423 | /* Make sure that the prefixes are clean */ |
| 424 | if (traffic_type == SR_STEER_IPV4) |
| 425 | { |
| 426 | u32 mask = |
| 427 | (dst_mask_width ? (0xFFFFFFFFu >> (32 - dst_mask_width)) : 0); |
| 428 | prefix.ip4.as_u32 &= mask; |
| 429 | } |
| 430 | else if (traffic_type == SR_STEER_IPV6) |
| 431 | { |
| 432 | ip6_address_t mask; |
| 433 | ip6_address_mask_from_width (&mask, dst_mask_width); |
| 434 | ip6_address_mask (&prefix.ip6, &mask); |
| 435 | } |
| 436 | |
| 437 | rv = |
| 438 | sr_steering_policy (is_del, (sr_policy_index == ~(u32) 0 ? &bsid : NULL), |
| 439 | sr_policy_index, fib_table, &prefix, dst_mask_width, |
| 440 | sw_if_index, traffic_type); |
| 441 | |
| 442 | switch (rv) |
| 443 | { |
| 444 | case 0: |
| 445 | break; |
| 446 | case 1: |
| 447 | return 0; |
| 448 | case -1: |
| 449 | return clib_error_return (0, "Incorrect API usage."); |
| 450 | case -2: |
| 451 | return clib_error_return (0, |
| 452 | "The requested SR policy could not be located. Review the BSID/index."); |
| 453 | case -3: |
| 454 | return clib_error_return (0, |
| 455 | "Unable to do SW redirect. Incorrect interface."); |
| 456 | case -4: |
| 457 | return clib_error_return (0, |
Pablo Camarillo | 4521afa | 2017-03-16 10:43:05 +0100 | [diff] [blame] | 458 | "The requested SR steering policy could not be deleted."); |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 459 | case -5: |
| 460 | return clib_error_return (0, |
| 461 | "The SR policy is not an encapsulation one."); |
| 462 | default: |
| 463 | return clib_error_return (0, "BUG: sr steer policy returns %d", rv); |
| 464 | } |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | /* *INDENT-OFF* */ |
| 469 | VLIB_CLI_COMMAND (sr_steer_policy_command, static) = { |
| 470 | .path = "sr steer", |
Francois Clad | 4abc5c3 | 2018-02-01 14:51:33 +0100 | [diff] [blame] | 471 | .short_help = "sr steer (del) [l3 <ip_addr/mask>|l2 <sf_if>] " |
| 472 | "via [index <sr_policy_index>|bsid <bsid_ip6_addr>] " |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 473 | "(fib-table <fib_table_index>)", |
| 474 | .long_help = |
| 475 | "\tSteer a L2 or L3 traffic through an existing SR policy.\n" |
| 476 | "\tExamples:\n" |
| 477 | "\t\tsr steer l3 2001::/64 via sr_policy index 5\n" |
| 478 | "\t\tsr steer l3 2001::/64 via sr_policy bsid 2010::9999:1\n" |
| 479 | "\t\tsr steer l2 GigabitEthernet0/5/0 via sr_policy index 5\n" |
| 480 | "\t\tsr steer del l3 2001::/64 via sr_policy index 5\n", |
| 481 | .function = sr_steer_policy_command_fn, |
| 482 | }; |
| 483 | /* *INDENT-ON* */ |
| 484 | |
| 485 | static clib_error_t * |
| 486 | show_sr_steering_policies_command_fn (vlib_main_t * vm, |
| 487 | unformat_input_t * input, |
| 488 | vlib_cli_command_t * cmd) |
| 489 | { |
| 490 | ip6_sr_main_t *sm = &sr_main; |
| 491 | ip6_sr_steering_policy_t **steer_policies = 0; |
| 492 | ip6_sr_steering_policy_t *steer_pl; |
| 493 | |
| 494 | vnet_main_t *vnm = vnet_get_main (); |
| 495 | |
| 496 | ip6_sr_policy_t *pl = 0; |
| 497 | int i; |
| 498 | |
| 499 | vlib_cli_output (vm, "SR steering policies:"); |
| 500 | /* *INDENT-OFF* */ |
| 501 | pool_foreach (steer_pl, sm->steer_policies, ({vec_add1(steer_policies, steer_pl);})); |
| 502 | /* *INDENT-ON* */ |
| 503 | vlib_cli_output (vm, "Traffic\t\tSR policy BSID"); |
| 504 | for (i = 0; i < vec_len (steer_policies); i++) |
| 505 | { |
| 506 | steer_pl = steer_policies[i]; |
| 507 | pl = pool_elt_at_index (sm->sr_policies, steer_pl->sr_policy); |
| 508 | if (steer_pl->classify.traffic_type == SR_STEER_L2) |
| 509 | { |
| 510 | vlib_cli_output (vm, "L2 %U\t%U", |
| 511 | format_vnet_sw_if_index_name, vnm, |
| 512 | steer_pl->classify.l2.sw_if_index, |
| 513 | format_ip6_address, &pl->bsid); |
| 514 | } |
| 515 | else if (steer_pl->classify.traffic_type == SR_STEER_IPV4) |
| 516 | { |
| 517 | vlib_cli_output (vm, "L3 %U/%d\t%U", |
| 518 | format_ip4_address, |
| 519 | &steer_pl->classify.l3.prefix.ip4, |
| 520 | steer_pl->classify.l3.mask_width, |
| 521 | format_ip6_address, &pl->bsid); |
| 522 | } |
| 523 | else if (steer_pl->classify.traffic_type == SR_STEER_IPV6) |
| 524 | { |
| 525 | vlib_cli_output (vm, "L3 %U/%d\t%U", |
| 526 | format_ip6_address, |
| 527 | &steer_pl->classify.l3.prefix.ip6, |
| 528 | steer_pl->classify.l3.mask_width, |
| 529 | format_ip6_address, &pl->bsid); |
| 530 | } |
| 531 | } |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | /* *INDENT-OFF* */ |
| 536 | VLIB_CLI_COMMAND (show_sr_steering_policies_command, static) = { |
Francois Clad | 4abc5c3 | 2018-02-01 14:51:33 +0100 | [diff] [blame] | 537 | .path = "show sr steering-policies", |
| 538 | .short_help = "show sr steering-policies", |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 539 | .function = show_sr_steering_policies_command_fn, |
| 540 | }; |
| 541 | /* *INDENT-ON* */ |
| 542 | |
| 543 | clib_error_t * |
| 544 | sr_steering_init (vlib_main_t * vm) |
| 545 | { |
| 546 | ip6_sr_main_t *sm = &sr_main; |
| 547 | |
| 548 | /* Init memory for function keys */ |
Pablo Camarillo | 4521afa | 2017-03-16 10:43:05 +0100 | [diff] [blame] | 549 | mhash_init (&sm->sr_steer_policies_hash, sizeof (uword), |
| 550 | sizeof (sr_steering_key_t)); |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 551 | |
| 552 | sm->sw_iface_sr_policies = 0; |
| 553 | |
| 554 | sm->vnet_main = vnet_get_main (); |
| 555 | |
| 556 | return 0; |
| 557 | } |
| 558 | |
Pablo Camarillo | adcaaf1 | 2017-03-29 07:54:40 -0700 | [diff] [blame] | 559 | /* *INDENT-OFF* */ |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 560 | VLIB_INIT_FUNCTION (sr_steering_init); |
Pablo Camarillo | adcaaf1 | 2017-03-29 07:54:40 -0700 | [diff] [blame] | 561 | /* *INDENT-ON* */ |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 562 | |
Pablo Camarillo | adcaaf1 | 2017-03-29 07:54:40 -0700 | [diff] [blame] | 563 | /* *INDENT-OFF* */ |
| 564 | VNET_FEATURE_INIT (sr_pl_rewrite_encaps_l2, static) = |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 565 | { |
Pablo Camarillo | adcaaf1 | 2017-03-29 07:54:40 -0700 | [diff] [blame] | 566 | .arc_name = "device-input", |
| 567 | .node_name = "sr-pl-rewrite-encaps-l2", |
| 568 | .runs_before = VNET_FEATURES ("ethernet-input"), |
| 569 | }; |
| 570 | /* *INDENT-ON* */ |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 571 | |
| 572 | /* |
| 573 | * fd.io coding-style-patch-verification: ON |
| 574 | * |
| 575 | * Local Variables: |
| 576 | * eval: (c-set-style "gnu") |
| 577 | * End: |
| 578 | */ |