NAT64: Move IPv6-IPv4 virtual reassembly code from MAP-T to common library (VPP-708)
[vpp.git] / src / vnet / sr / sr_steering.c
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>
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>
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;
65   sr_steering_key_t key;
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
72   memset (&key, 0, sizeof (sr_steering_key_t));
73
74   /* Compute the steer policy key */
75   if (traffic_type == SR_STEER_IPV4 || traffic_type == SR_STEER_IPV6)
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     }
82   else if (traffic_type == SR_STEER_L2)
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     }
96   else
97     return -1;
98
99   key.traffic_type = traffic_type;
100
101   /* Search for the item */
102   p = mhash_get (&sm->sr_steer_policies_hash, &key);
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
118               fib_table_entry_delete (fib_table_find
119                                       (FIB_PROTOCOL_IP6,
120                                        steer_pl->classify.l3.fib_table),
121                                       &pfx, FIB_SOURCE_SR);
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
130               fib_table_entry_delete (fib_table_find
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 */
138               vnet_feature_enable_disable ("device-input",
139                                            "sr-policy-rewrite-encaps-l2",
140                                            sw_if_index, 0, 0, 0);
141               sm->sw_iface_sr_policies[sw_if_index] = ~(u32) 0;
142
143               /* Remove promiscous mode from interface */
144               vnet_main_t *vnm = vnet_get_main ();
145               ethernet_main_t *em = &ethernet_main;
146               ethernet_interface_t *eif =
147                 ethernet_get_interface (em, sw_if_index);
148
149               if (!eif)
150                 goto cleanup_error_redirection;
151
152               ethernet_set_flags (vnm, sw_if_index, 0);
153             }
154
155           /* Delete SR steering policy entry */
156           pool_put (sm->steer_policies, steer_pl);
157           mhash_unset (&sm->sr_steer_policies_hash, &key, NULL);
158
159           /* If no more SR policies or steering policies */
160           if (!pool_elts (sm->sr_policies) && !pool_elts (sm->steer_policies))
161             {
162               fib_table_unlock (sm->fib_table_ip6, FIB_PROTOCOL_IP6);
163               fib_table_unlock (sm->fib_table_ip4, FIB_PROTOCOL_IP6);
164               sm->fib_table_ip6 = (u32) ~ 0;
165               sm->fib_table_ip4 = (u32) ~ 0;
166             }
167
168           return 1;
169         }
170       else                      /* It means user requested to update an existing SR steering policy */
171         {
172           /* Retrieve SR steering policy */
173           if (bsid)
174             {
175               p = mhash_get (&sm->sr_policies_index_hash, bsid);
176               if (p)
177                 sr_policy = pool_elt_at_index (sm->sr_policies, p[0]);
178               else
179                 return -2;
180             }
181           else
182             sr_policy = pool_elt_at_index (sm->sr_policies, sr_policy_index);
183
184           if (!sr_policy)
185             return -2;
186
187           steer_pl->sr_policy = sr_policy - sm->sr_policies;
188
189           /* Remove old FIB/hw redirection and create a new one */
190           if (steer_pl->classify.traffic_type == SR_STEER_IPV6)
191             {
192               /* Remove FIB entry */
193               pfx.fp_proto = FIB_PROTOCOL_IP6;
194               pfx.fp_len = steer_pl->classify.l3.mask_width;
195               pfx.fp_addr.ip6 = steer_pl->classify.l3.prefix.ip6;
196
197               fib_table_entry_delete (fib_table_find
198                                       (FIB_PROTOCOL_IP6,
199                                        steer_pl->classify.l3.fib_table),
200                                       &pfx, FIB_SOURCE_SR);
201
202               /* Create a new one */
203               goto update_fib;
204             }
205           else if (steer_pl->classify.traffic_type == SR_STEER_IPV4)
206             {
207               /* Remove FIB entry */
208               pfx.fp_proto = FIB_PROTOCOL_IP4;
209               pfx.fp_len = steer_pl->classify.l3.mask_width;
210               pfx.fp_addr.ip4 = steer_pl->classify.l3.prefix.ip4;
211
212               fib_table_entry_delete (fib_table_find
213                                       (FIB_PROTOCOL_IP4,
214                                        steer_pl->classify.l3.fib_table),
215                                       &pfx, FIB_SOURCE_SR);
216
217               /* Create a new one */
218               goto update_fib;
219             }
220           else if (steer_pl->classify.traffic_type == SR_STEER_L2)
221             {
222               /* Update L2-HW redirection */
223               goto update_fib;
224             }
225         }
226     }
227   else
228     /* delete; steering policy does not exist; complain */
229   if (is_del)
230     return -4;
231
232   /* Retrieve SR policy */
233   if (bsid)
234     {
235       p = mhash_get (&sm->sr_policies_index_hash, bsid);
236       if (p)
237         sr_policy = pool_elt_at_index (sm->sr_policies, p[0]);
238       else
239         return -2;
240     }
241   else
242     sr_policy = pool_elt_at_index (sm->sr_policies, sr_policy_index);
243
244   /* Create a new steering policy */
245   pool_get (sm->steer_policies, steer_pl);
246   memset (steer_pl, 0, sizeof (*steer_pl));
247
248   if (traffic_type == SR_STEER_IPV4 || traffic_type == SR_STEER_IPV6)
249     {
250       clib_memcpy (&steer_pl->classify.l3.prefix, prefix,
251                    sizeof (ip46_address_t));
252       steer_pl->classify.l3.mask_width = mask_width;
253       steer_pl->classify.l3.fib_table =
254         (table_id != (u32) ~ 0 ? table_id : 0);
255       steer_pl->classify.traffic_type = traffic_type;
256     }
257   else if (traffic_type == SR_STEER_L2)
258     {
259       steer_pl->classify.l2.sw_if_index = sw_if_index;
260       steer_pl->classify.traffic_type = traffic_type;
261     }
262   else
263     {
264       /* Incorrect API usage. Should never get here */
265       pool_put (sm->steer_policies, steer_pl);
266       mhash_unset (&sm->sr_steer_policies_hash, &key, NULL);
267       return -1;
268     }
269   steer_pl->sr_policy = sr_policy - sm->sr_policies;
270
271   /* Create and store key */
272   mhash_set (&sm->sr_steer_policies_hash, &key, steer_pl - sm->steer_policies,
273              NULL);
274
275   if (traffic_type == SR_STEER_L2)
276     {
277       if (!sr_policy->is_encap)
278         goto cleanup_error_encap;
279
280       if (vnet_feature_enable_disable
281           ("device-input", "sr-pl-rewrite-encaps-l2", sw_if_index, 1, 0, 0))
282         goto cleanup_error_redirection;
283
284       /* Set promiscous mode on interface */
285       vnet_main_t *vnm = vnet_get_main ();
286       ethernet_main_t *em = &ethernet_main;
287       ethernet_interface_t *eif = ethernet_get_interface (em, sw_if_index);
288
289       if (!eif)
290         goto cleanup_error_redirection;
291
292       ethernet_set_flags (vnm, sw_if_index,
293                           ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
294     }
295   else if (traffic_type == SR_STEER_IPV4)
296     if (!sr_policy->is_encap)
297       goto cleanup_error_encap;
298
299 update_fib:
300   /* FIB API calls - Recursive route through the BindingSID */
301   if (traffic_type == SR_STEER_IPV6)
302     {
303       pfx.fp_proto = FIB_PROTOCOL_IP6;
304       pfx.fp_len = steer_pl->classify.l3.mask_width;
305       pfx.fp_addr.ip6 = steer_pl->classify.l3.prefix.ip6;
306
307       fib_table_entry_path_add (fib_table_find (FIB_PROTOCOL_IP6,
308                                                 (table_id !=
309                                                  (u32) ~ 0 ?
310                                                  table_id : 0)),
311                                 &pfx, FIB_SOURCE_SR,
312                                 FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT,
313                                 FIB_PROTOCOL_IP6,
314                                 (ip46_address_t *) & sr_policy->bsid, ~0,
315                                 sm->fib_table_ip6, 1, NULL,
316                                 FIB_ROUTE_PATH_FLAG_NONE);
317     }
318   else if (traffic_type == SR_STEER_IPV4)
319     {
320       pfx.fp_proto = FIB_PROTOCOL_IP4;
321       pfx.fp_len = steer_pl->classify.l3.mask_width;
322       pfx.fp_addr.ip4 = steer_pl->classify.l3.prefix.ip4;
323
324       fib_table_entry_path_add (fib_table_find (FIB_PROTOCOL_IP4,
325                                                 (table_id !=
326                                                  (u32) ~ 0 ?
327                                                  table_id : 0)),
328                                 &pfx, FIB_SOURCE_SR,
329                                 FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT,
330                                 FIB_PROTOCOL_IP6,
331                                 (ip46_address_t *) & sr_policy->bsid, ~0,
332                                 sm->fib_table_ip4, 1, NULL,
333                                 FIB_ROUTE_PATH_FLAG_NONE);
334     }
335   else if (traffic_type == SR_STEER_L2)
336     {
337       if (sw_if_index < vec_len (sm->sw_iface_sr_policies))
338         sm->sw_iface_sr_policies[sw_if_index] = steer_pl->sr_policy;
339       else
340         {
341           vec_resize (sm->sw_iface_sr_policies,
342                       (pool_len (sm->vnet_main->interface_main.sw_interfaces)
343                        - vec_len (sm->sw_iface_sr_policies)));
344           sm->sw_iface_sr_policies[sw_if_index] = steer_pl->sr_policy;
345         }
346     }
347
348   return 0;
349
350 cleanup_error_encap:
351   pool_put (sm->steer_policies, steer_pl);
352   mhash_unset (&sm->sr_steer_policies_hash, &key, NULL);
353   return -5;
354
355 cleanup_error_redirection:
356   pool_put (sm->steer_policies, steer_pl);
357   mhash_unset (&sm->sr_steer_policies_hash, &key, NULL);
358   return -3;
359 }
360
361 static clib_error_t *
362 sr_steer_policy_command_fn (vlib_main_t * vm, unformat_input_t * input,
363                             vlib_cli_command_t * cmd)
364 {
365   vnet_main_t *vnm = vnet_get_main ();
366
367   int is_del = 0;
368
369   ip46_address_t prefix;
370   u32 dst_mask_width = 0;
371   u32 sw_if_index = (u32) ~ 0;
372   u8 traffic_type = 0;
373   u32 fib_table = (u32) ~ 0;
374
375   ip6_address_t bsid;
376   u32 sr_policy_index = (u32) ~ 0;
377
378   u8 sr_policy_set = 0;
379
380   memset (&prefix, 0, sizeof (ip46_address_t));
381
382   int rv;
383   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
384     {
385       if (unformat (input, "del"))
386         is_del = 1;
387       else if (!traffic_type
388                && unformat (input, "l3 %U/%d", unformat_ip6_address,
389                             &prefix.ip6, &dst_mask_width))
390         traffic_type = SR_STEER_IPV6;
391       else if (!traffic_type
392                && unformat (input, "l3 %U/%d", unformat_ip4_address,
393                             &prefix.ip4, &dst_mask_width))
394         traffic_type = SR_STEER_IPV4;
395       else if (!traffic_type
396                && unformat (input, "l2 %U", unformat_vnet_sw_interface, vnm,
397                             &sw_if_index))
398         traffic_type = SR_STEER_L2;
399       else if (!sr_policy_set
400                && unformat (input, "via sr policy index %d",
401                             &sr_policy_index))
402         sr_policy_set = 1;
403       else if (!sr_policy_set
404                && unformat (input, "via sr policy bsid %U",
405                             unformat_ip6_address, &bsid))
406         sr_policy_set = 1;
407       else if (fib_table == (u32) ~ 0
408                && unformat (input, "fib-table %d", &fib_table));
409       else
410         break;
411     }
412
413   if (!traffic_type)
414     return clib_error_return (0, "No L2/L3 traffic specified");
415   if (!sr_policy_set)
416     return clib_error_return (0, "No SR policy specified");
417
418   /* Make sure that the prefixes are clean */
419   if (traffic_type == SR_STEER_IPV4)
420     {
421       u32 mask =
422         (dst_mask_width ? (0xFFFFFFFFu >> (32 - dst_mask_width)) : 0);
423       prefix.ip4.as_u32 &= mask;
424     }
425   else if (traffic_type == SR_STEER_IPV6)
426     {
427       ip6_address_t mask;
428       ip6_address_mask_from_width (&mask, dst_mask_width);
429       ip6_address_mask (&prefix.ip6, &mask);
430     }
431
432   rv =
433     sr_steering_policy (is_del, (sr_policy_index == ~(u32) 0 ? &bsid : NULL),
434                         sr_policy_index, fib_table, &prefix, dst_mask_width,
435                         sw_if_index, traffic_type);
436
437   switch (rv)
438     {
439     case 0:
440       break;
441     case 1:
442       return 0;
443     case -1:
444       return clib_error_return (0, "Incorrect API usage.");
445     case -2:
446       return clib_error_return (0,
447                                 "The requested SR policy could not be located. Review the BSID/index.");
448     case -3:
449       return clib_error_return (0,
450                                 "Unable to do SW redirect. Incorrect interface.");
451     case -4:
452       return clib_error_return (0,
453                                 "The requested SR steering policy could not be deleted.");
454     case -5:
455       return clib_error_return (0,
456                                 "The SR policy is not an encapsulation one.");
457     default:
458       return clib_error_return (0, "BUG: sr steer policy returns %d", rv);
459     }
460   return 0;
461 }
462
463 /* *INDENT-OFF* */
464 VLIB_CLI_COMMAND (sr_steer_policy_command, static) = {
465   .path = "sr steer",
466   .short_help = "sr steer (del) [l3 <ip_addr/mask>|l2 <sf_if>]"
467     "via sr policy [index <sr_policy_index>|bsid <bsid_ip6_addr>]"
468     "(fib-table <fib_table_index>)",
469   .long_help =
470     "\tSteer a L2 or L3 traffic through an existing SR policy.\n"
471     "\tExamples:\n"
472     "\t\tsr steer l3 2001::/64 via sr_policy index 5\n"
473     "\t\tsr steer l3 2001::/64 via sr_policy bsid 2010::9999:1\n"
474     "\t\tsr steer l2 GigabitEthernet0/5/0 via sr_policy index 5\n"
475     "\t\tsr steer del l3 2001::/64 via sr_policy index 5\n",
476   .function = sr_steer_policy_command_fn,
477 };
478 /* *INDENT-ON* */
479
480 static clib_error_t *
481 show_sr_steering_policies_command_fn (vlib_main_t * vm,
482                                       unformat_input_t * input,
483                                       vlib_cli_command_t * cmd)
484 {
485   ip6_sr_main_t *sm = &sr_main;
486   ip6_sr_steering_policy_t **steer_policies = 0;
487   ip6_sr_steering_policy_t *steer_pl;
488
489   vnet_main_t *vnm = vnet_get_main ();
490
491   ip6_sr_policy_t *pl = 0;
492   int i;
493
494   vlib_cli_output (vm, "SR steering policies:");
495   /* *INDENT-OFF* */
496   pool_foreach (steer_pl, sm->steer_policies, ({vec_add1(steer_policies, steer_pl);}));
497   /* *INDENT-ON* */
498   vlib_cli_output (vm, "Traffic\t\tSR policy BSID");
499   for (i = 0; i < vec_len (steer_policies); i++)
500     {
501       steer_pl = steer_policies[i];
502       pl = pool_elt_at_index (sm->sr_policies, steer_pl->sr_policy);
503       if (steer_pl->classify.traffic_type == SR_STEER_L2)
504         {
505           vlib_cli_output (vm, "L2 %U\t%U",
506                            format_vnet_sw_if_index_name, vnm,
507                            steer_pl->classify.l2.sw_if_index,
508                            format_ip6_address, &pl->bsid);
509         }
510       else if (steer_pl->classify.traffic_type == SR_STEER_IPV4)
511         {
512           vlib_cli_output (vm, "L3 %U/%d\t%U",
513                            format_ip4_address,
514                            &steer_pl->classify.l3.prefix.ip4,
515                            steer_pl->classify.l3.mask_width,
516                            format_ip6_address, &pl->bsid);
517         }
518       else if (steer_pl->classify.traffic_type == SR_STEER_IPV6)
519         {
520           vlib_cli_output (vm, "L3 %U/%d\t%U",
521                            format_ip6_address,
522                            &steer_pl->classify.l3.prefix.ip6,
523                            steer_pl->classify.l3.mask_width,
524                            format_ip6_address, &pl->bsid);
525         }
526     }
527   return 0;
528 }
529
530 /* *INDENT-OFF* */
531 VLIB_CLI_COMMAND (show_sr_steering_policies_command, static) = {
532   .path = "show sr steering policies",
533   .short_help = "show sr steering policies",
534   .function = show_sr_steering_policies_command_fn,
535 };
536 /* *INDENT-ON* */
537
538 clib_error_t *
539 sr_steering_init (vlib_main_t * vm)
540 {
541   ip6_sr_main_t *sm = &sr_main;
542
543   /* Init memory for function keys */
544   mhash_init (&sm->sr_steer_policies_hash, sizeof (uword),
545               sizeof (sr_steering_key_t));
546
547   sm->sw_iface_sr_policies = 0;
548
549   sm->vnet_main = vnet_get_main ();
550
551   return 0;
552 }
553
554 /* *INDENT-OFF* */
555 VLIB_INIT_FUNCTION (sr_steering_init);
556 /* *INDENT-ON* */
557
558 /* *INDENT-OFF* */
559 VNET_FEATURE_INIT (sr_pl_rewrite_encaps_l2, static) =
560 {
561   .arc_name = "device-input",
562   .node_name = "sr-pl-rewrite-encaps-l2",
563   .runs_before = VNET_FEATURES ("ethernet-input"),
564 };
565 /* *INDENT-ON* */
566
567 /*
568 * fd.io coding-style-patch-verification: ON
569 *
570 * Local Variables:
571 * eval: (c-set-style "gnu")
572 * End:
573 */