2 * sr_steering.c: ipv6 segment routing steering into SR policy
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * @brief Packet steering into SR Policies
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
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)
33 #include <vlib/vlib.h>
34 #include <vnet/vnet.h>
35 #include <vnet/sr/sr.h>
36 #include <vnet/ip/ip.h>
37 #include <vnet/sr/sr_packet.h>
38 #include <vnet/ip/ip6_packet.h>
39 #include <vnet/fib/ip6_fib.h>
40 #include <vnet/dpo/dpo.h>
42 #include <vppinfra/error.h>
43 #include <vppinfra/elog.h>
46 * @brief Steer traffic L2 and L3 traffic through a given SR policy
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
57 * @return 0 if correct, else error
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)
64 ip6_sr_main_t *sm = &sr_main;
65 sr_steering_key_t key;
66 ip6_sr_steering_policy_t *steer_pl;
67 fib_prefix_t pfx = { 0 };
69 ip6_sr_policy_t *sr_policy = 0;
72 memset (&key, 0, sizeof (sr_steering_key_t));
74 /* Compute the steer policy key */
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 if (traffic_type != SR_STEER_IPV4 && traffic_type != SR_STEER_IPV6)
86 key.l2.sw_if_index = sw_if_index;
88 /* Sanitise the SW_IF_INDEX */
89 if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
93 vnet_sw_interface_t *sw =
94 vnet_get_sw_interface (sm->vnet_main, sw_if_index);
95 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
97 if (traffic_type != SR_STEER_L2)
101 key.traffic_type = traffic_type;
103 /* Search for the item */
104 p = mhash_get (&sm->sr_steer_policies_hash, &key);
108 /* Retrieve Steer Policy function */
109 steer_pl = pool_elt_at_index (sm->steer_policies, p[0]);
113 if (steer_pl->classify.traffic_type == SR_STEER_IPV6)
115 /* Remove FIB entry */
116 pfx.fp_proto = FIB_PROTOCOL_IP6;
117 pfx.fp_len = steer_pl->classify.l3.mask_width;
118 pfx.fp_addr.ip6 = steer_pl->classify.l3.prefix.ip6;
120 fib_table_entry_delete (fib_table_id_find_fib_index
122 steer_pl->classify.l3.fib_table), &pfx,
125 else if (steer_pl->classify.traffic_type == SR_STEER_IPV4)
127 /* Remove FIB entry */
128 pfx.fp_proto = FIB_PROTOCOL_IP4;
129 pfx.fp_len = steer_pl->classify.l3.mask_width;
130 pfx.fp_addr.ip4 = steer_pl->classify.l3.prefix.ip4;
132 fib_table_entry_delete (fib_table_id_find_fib_index
134 steer_pl->classify.l3.fib_table), &pfx,
137 else if (steer_pl->classify.traffic_type == SR_STEER_L2)
139 /* Remove HW redirection */
140 vnet_feature_enable_disable ("device-input",
141 "sr-policy-rewrite-encaps-l2",
142 sw_if_index, 0, 0, 0);
143 sm->sw_iface_sr_policies[sw_if_index] = ~(u32) 0;
145 /* Remove promiscous mode from interface */
146 vnet_main_t *vnm = vnet_get_main ();
147 ethernet_main_t *em = ðernet_main;
148 ethernet_interface_t *eif =
149 ethernet_get_interface (em, sw_if_index);
152 goto cleanup_error_redirection;
154 ethernet_set_flags (vnm, sw_if_index, 0);
157 /* Delete SR steering policy entry */
158 pool_put (sm->steer_policies, steer_pl);
159 mhash_unset (&sm->sr_steer_policies_hash, &key, NULL);
161 /* If no more SR policies or steering policies */
162 if (!pool_elts (sm->sr_policies) && !pool_elts (sm->steer_policies))
164 fib_table_unlock (sm->fib_table_ip6, FIB_PROTOCOL_IP6);
165 fib_table_unlock (sm->fib_table_ip4, FIB_PROTOCOL_IP6);
166 sm->fib_table_ip6 = (u32) ~ 0;
167 sm->fib_table_ip4 = (u32) ~ 0;
172 else /* It means user requested to update an existing SR steering policy */
174 /* Retrieve SR steering policy */
177 p = mhash_get (&sm->sr_policies_index_hash, bsid);
179 sr_policy = pool_elt_at_index (sm->sr_policies, p[0]);
184 sr_policy = pool_elt_at_index (sm->sr_policies, sr_policy_index);
189 steer_pl->sr_policy = sr_policy - sm->sr_policies;
191 /* Remove old FIB/hw redirection and create a new one */
192 if (steer_pl->classify.traffic_type == SR_STEER_IPV6)
194 /* Remove FIB entry */
195 pfx.fp_proto = FIB_PROTOCOL_IP6;
196 pfx.fp_len = steer_pl->classify.l3.mask_width;
197 pfx.fp_addr.ip6 = steer_pl->classify.l3.prefix.ip6;
199 fib_table_entry_delete (fib_table_id_find_fib_index
201 steer_pl->classify.l3.fib_table), &pfx,
204 /* Create a new one */
207 else if (steer_pl->classify.traffic_type == SR_STEER_IPV4)
209 /* Remove FIB entry */
210 pfx.fp_proto = FIB_PROTOCOL_IP4;
211 pfx.fp_len = steer_pl->classify.l3.mask_width;
212 pfx.fp_addr.ip4 = steer_pl->classify.l3.prefix.ip4;
214 fib_table_entry_delete (fib_table_id_find_fib_index
216 steer_pl->classify.l3.fib_table), &pfx,
219 /* Create a new one */
222 else if (steer_pl->classify.traffic_type == SR_STEER_L2)
224 /* Update L2-HW redirection */
230 /* delete; steering policy does not exist; complain */
234 /* Retrieve SR policy */
237 p = mhash_get (&sm->sr_policies_index_hash, bsid);
239 sr_policy = pool_elt_at_index (sm->sr_policies, p[0]);
244 sr_policy = pool_elt_at_index (sm->sr_policies, sr_policy_index);
246 /* Create a new steering policy */
247 pool_get (sm->steer_policies, steer_pl);
248 memset (steer_pl, 0, sizeof (*steer_pl));
250 if (traffic_type == SR_STEER_IPV4 || traffic_type == SR_STEER_IPV6)
252 clib_memcpy (&steer_pl->classify.l3.prefix, prefix,
253 sizeof (ip46_address_t));
254 steer_pl->classify.l3.mask_width = mask_width;
255 steer_pl->classify.l3.fib_table =
256 (table_id != (u32) ~ 0 ? table_id : 0);
257 steer_pl->classify.traffic_type = traffic_type;
259 else if (traffic_type == SR_STEER_L2)
261 steer_pl->classify.l2.sw_if_index = sw_if_index;
262 steer_pl->classify.traffic_type = traffic_type;
266 /* Incorrect API usage. Should never get here */
267 pool_put (sm->steer_policies, steer_pl);
268 mhash_unset (&sm->sr_steer_policies_hash, &key, NULL);
271 steer_pl->sr_policy = sr_policy - sm->sr_policies;
273 /* Create and store key */
274 mhash_set (&sm->sr_steer_policies_hash, &key, steer_pl - sm->steer_policies,
277 if (traffic_type == SR_STEER_L2)
279 if (!sr_policy->is_encap)
280 goto cleanup_error_encap;
282 if (vnet_feature_enable_disable
283 ("device-input", "sr-policy-rewrite-encaps-l2", sw_if_index, 1, 0,
285 goto cleanup_error_redirection;
287 /* Set promiscous mode on interface */
288 vnet_main_t *vnm = vnet_get_main ();
289 ethernet_main_t *em = ðernet_main;
290 ethernet_interface_t *eif = ethernet_get_interface (em, sw_if_index);
293 goto cleanup_error_redirection;
295 ethernet_set_flags (vnm, sw_if_index,
296 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
298 else if (traffic_type == SR_STEER_IPV4)
299 if (!sr_policy->is_encap)
300 goto cleanup_error_encap;
303 /* FIB API calls - Recursive route through the BindingSID */
304 if (traffic_type == SR_STEER_IPV6)
306 pfx.fp_proto = FIB_PROTOCOL_IP6;
307 pfx.fp_len = steer_pl->classify.l3.mask_width;
308 pfx.fp_addr.ip6 = steer_pl->classify.l3.prefix.ip6;
310 fib_table_entry_path_add (fib_table_id_find_fib_index (FIB_PROTOCOL_IP6,
314 &pfx, FIB_SOURCE_CLI, FIB_ENTRY_FLAG_NONE,
316 (ip46_address_t *) & sr_policy->bsid, ~0,
317 sm->fib_table_ip6, 1, NULL,
318 FIB_ROUTE_PATH_FLAG_NONE);
320 else if (traffic_type == SR_STEER_IPV4)
322 pfx.fp_proto = FIB_PROTOCOL_IP4;
323 pfx.fp_len = steer_pl->classify.l3.mask_width;
324 pfx.fp_addr.ip4 = steer_pl->classify.l3.prefix.ip4;
326 fib_table_entry_path_add (fib_table_id_find_fib_index (FIB_PROTOCOL_IP4,
330 &pfx, FIB_SOURCE_CLI, FIB_ENTRY_FLAG_NONE,
332 (ip46_address_t *) & sr_policy->bsid, ~0,
333 sm->fib_table_ip4, 1, NULL,
334 FIB_ROUTE_PATH_FLAG_NONE);
336 else if (traffic_type == SR_STEER_L2)
338 if (sw_if_index < vec_len (sm->sw_iface_sr_policies))
339 sm->sw_iface_sr_policies[sw_if_index] = steer_pl->sr_policy;
342 vec_resize (sm->sw_iface_sr_policies,
343 (pool_len (sm->vnet_main->interface_main.sw_interfaces)
344 - vec_len (sm->sw_iface_sr_policies)));
345 sm->sw_iface_sr_policies[sw_if_index] = steer_pl->sr_policy;
352 pool_put (sm->steer_policies, steer_pl);
353 mhash_unset (&sm->sr_steer_policies_hash, &key, NULL);
356 cleanup_error_redirection:
357 pool_put (sm->steer_policies, steer_pl);
358 mhash_unset (&sm->sr_steer_policies_hash, &key, NULL);
362 static clib_error_t *
363 sr_steer_policy_command_fn (vlib_main_t * vm, unformat_input_t * input,
364 vlib_cli_command_t * cmd)
366 vnet_main_t *vnm = vnet_get_main ();
370 ip46_address_t prefix;
371 u32 dst_mask_width = 0;
372 u32 sw_if_index = (u32) ~ 0;
374 u32 fib_table = (u32) ~ 0;
377 u32 sr_policy_index = (u32) ~ 0;
379 u8 sr_policy_set = 0;
381 memset (&prefix, 0, sizeof (ip46_address_t));
384 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
386 if (unformat (input, "del"))
388 else if (!traffic_type
389 && unformat (input, "l3 %U/%d", unformat_ip6_address,
390 &prefix.ip6, &dst_mask_width))
391 traffic_type = SR_STEER_IPV6;
392 else if (!traffic_type
393 && unformat (input, "l3 %U/%d", unformat_ip4_address,
394 &prefix.ip4, &dst_mask_width))
395 traffic_type = SR_STEER_IPV4;
396 else if (!traffic_type
397 && unformat (input, "l2 %U", unformat_vnet_sw_interface, vnm,
399 traffic_type = SR_STEER_L2;
400 else if (!sr_policy_set
401 && unformat (input, "via sr policy index %d",
404 else if (!sr_policy_set
405 && unformat (input, "via sr policy bsid %U",
406 unformat_ip6_address, &bsid))
408 else if (fib_table == (u32) ~ 0
409 && unformat (input, "fib-table %d", &fib_table));
415 return clib_error_return (0, "No L2/L3 traffic specified");
417 return clib_error_return (0, "No SR policy specified");
419 /* Make sure that the prefixes are clean */
420 if (traffic_type == SR_STEER_IPV4)
423 (dst_mask_width ? (0xFFFFFFFFu >> (32 - dst_mask_width)) : 0);
424 prefix.ip4.as_u32 &= mask;
426 else if (traffic_type == SR_STEER_IPV6)
429 ip6_address_mask_from_width (&mask, dst_mask_width);
430 ip6_address_mask (&prefix.ip6, &mask);
434 sr_steering_policy (is_del, (sr_policy_index == ~(u32) 0 ? &bsid : NULL),
435 sr_policy_index, fib_table, &prefix, dst_mask_width,
436 sw_if_index, traffic_type);
445 return clib_error_return (0, "Incorrect API usage.");
447 return clib_error_return (0,
448 "The requested SR policy could not be located. Review the BSID/index.");
450 return clib_error_return (0,
451 "Unable to do SW redirect. Incorrect interface.");
453 return clib_error_return (0,
454 "The requested SR steering policy could not be deleted.");
456 return clib_error_return (0,
457 "The SR policy is not an encapsulation one.");
459 return clib_error_return (0, "BUG: sr steer policy returns %d", rv);
465 VLIB_CLI_COMMAND (sr_steer_policy_command, static) = {
467 .short_help = "sr steer (del) [l3 <ip_addr/mask>|l2 <sf_if>]"
468 "via sr policy [index <sr_policy_index>|bsid <bsid_ip6_addr>]"
469 "(fib-table <fib_table_index>)",
471 "\tSteer a L2 or L3 traffic through an existing SR policy.\n"
473 "\t\tsr steer l3 2001::/64 via sr_policy index 5\n"
474 "\t\tsr steer l3 2001::/64 via sr_policy bsid 2010::9999:1\n"
475 "\t\tsr steer l2 GigabitEthernet0/5/0 via sr_policy index 5\n"
476 "\t\tsr steer del l3 2001::/64 via sr_policy index 5\n",
477 .function = sr_steer_policy_command_fn,
481 static clib_error_t *
482 show_sr_steering_policies_command_fn (vlib_main_t * vm,
483 unformat_input_t * input,
484 vlib_cli_command_t * cmd)
486 ip6_sr_main_t *sm = &sr_main;
487 ip6_sr_steering_policy_t **steer_policies = 0;
488 ip6_sr_steering_policy_t *steer_pl;
490 vnet_main_t *vnm = vnet_get_main ();
492 ip6_sr_policy_t *pl = 0;
495 vlib_cli_output (vm, "SR steering policies:");
497 pool_foreach (steer_pl, sm->steer_policies, ({vec_add1(steer_policies, steer_pl);}));
499 vlib_cli_output (vm, "Traffic\t\tSR policy BSID");
500 for (i = 0; i < vec_len (steer_policies); i++)
502 steer_pl = steer_policies[i];
503 pl = pool_elt_at_index (sm->sr_policies, steer_pl->sr_policy);
504 if (steer_pl->classify.traffic_type == SR_STEER_L2)
506 vlib_cli_output (vm, "L2 %U\t%U",
507 format_vnet_sw_if_index_name, vnm,
508 steer_pl->classify.l2.sw_if_index,
509 format_ip6_address, &pl->bsid);
511 else if (steer_pl->classify.traffic_type == SR_STEER_IPV4)
513 vlib_cli_output (vm, "L3 %U/%d\t%U",
515 &steer_pl->classify.l3.prefix.ip4,
516 steer_pl->classify.l3.mask_width,
517 format_ip6_address, &pl->bsid);
519 else if (steer_pl->classify.traffic_type == SR_STEER_IPV6)
521 vlib_cli_output (vm, "L3 %U/%d\t%U",
523 &steer_pl->classify.l3.prefix.ip6,
524 steer_pl->classify.l3.mask_width,
525 format_ip6_address, &pl->bsid);
532 VLIB_CLI_COMMAND (show_sr_steering_policies_command, static) = {
533 .path = "show sr steering policies",
534 .short_help = "show sr steering policies",
535 .function = show_sr_steering_policies_command_fn,
540 sr_steering_init (vlib_main_t * vm)
542 ip6_sr_main_t *sm = &sr_main;
544 /* Init memory for function keys */
545 mhash_init (&sm->sr_steer_policies_hash, sizeof (uword),
546 sizeof (sr_steering_key_t));
548 sm->sw_iface_sr_policies = 0;
550 sm->vnet_main = vnet_get_main ();
555 VLIB_INIT_FUNCTION (sr_steering_init);
557 VNET_FEATURE_INIT (sr_policy_rewrite_encaps_l2, static) =
559 .arc_name = "device-input",.node_name =
560 "sr-pl-rewrite-encaps-l2",.runs_before =
561 VNET_FEATURES ("ethernet-input"),};
564 * fd.io coding-style-patch-verification: ON
567 * eval: (c-set-style "gnu")