Source VRF Select
[vpp.git] / src / plugins / svs / svs.c
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <plugins/svs/svs.h>
17
18 #include <vlib/vlib.h>
19 #include <vnet/plugin/plugin.h>
20 #include <vnet/fib/fib_table.h>
21 #include <vnet/fib/ip6_fib.h>
22 #include <vnet/fib/ip4_fib.h>
23 #include <vnet/dpo/lookup_dpo.h>
24 #include <vnet/dpo/load_balance.h>
25 #include <vnet/dpo/load_balance_map.h>
26
27 u32 *svs_itf_db[FIB_PROTOCOL_IP_MAX];
28
29 int
30 svs_table_add (fib_protocol_t fproto, u32 table_id)
31 {
32   fib_table_find_or_create_and_lock (fproto, table_id, FIB_SOURCE_PLUGIN_LOW);
33
34   return (0);
35 }
36
37 int
38 svs_table_delete (fib_protocol_t fproto, u32 table_id)
39 {
40   u32 fib_index;
41
42   fib_index = fib_table_find (fproto, table_id);
43
44   if (~0 == fib_index)
45     return VNET_API_ERROR_NO_SUCH_FIB;
46
47   fib_table_unlock (fib_index, fproto, FIB_SOURCE_PLUGIN_LOW);
48
49   return (0);
50 }
51
52 static int
53 svs_route_add_i (u32 fib_index, const fib_prefix_t * pfx, u32 src_fib_index)
54 {
55   dpo_id_t dpo = DPO_INVALID;
56
57
58   lookup_dpo_add_or_lock_w_fib_index (src_fib_index,
59                                       fib_proto_to_dpo (pfx->fp_proto),
60                                       LOOKUP_UNICAST,
61                                       LOOKUP_INPUT_SRC_ADDR,
62                                       LOOKUP_TABLE_FROM_CONFIG, &dpo);
63
64   fib_table_entry_special_dpo_add (fib_index, pfx,
65                                    FIB_SOURCE_PLUGIN_LOW,
66                                    FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
67
68   dpo_unlock (&dpo);
69
70   return (0);
71 }
72
73 int
74 svs_route_add (u32 table_id, const fib_prefix_t * pfx, u32 source_table_id)
75 {
76   u32 fib_index, src_fib_index;
77   int rv;
78
79   fib_index = fib_table_find (pfx->fp_proto, table_id);
80
81   if (~0 == fib_index)
82     return VNET_API_ERROR_NO_SUCH_FIB;
83
84   src_fib_index = fib_table_find (pfx->fp_proto, source_table_id);
85
86   if (~0 == src_fib_index)
87     return (VNET_API_ERROR_NO_SUCH_FIB);
88
89   rv = svs_route_add_i (fib_index, pfx, src_fib_index);
90
91   return (rv);
92 }
93
94 int
95 svs_route_delete (u32 table_id, const fib_prefix_t * pfx)
96 {
97   u32 fib_index;
98
99   fib_index = fib_table_find (pfx->fp_proto, table_id);
100
101   if (~0 == fib_index)
102     return VNET_API_ERROR_NO_SUCH_FIB;
103
104   fib_table_entry_special_remove (fib_index, pfx, FIB_SOURCE_PLUGIN_LOW);
105
106   return (0);
107 }
108
109 int
110 svs_enable (fib_protocol_t fproto, u32 table_id, u32 sw_if_index)
111 {
112   fib_prefix_t pfx = {
113     .fp_proto = fproto,
114   };
115   u32 fib_index;
116
117   fib_index = fib_table_find (fproto, table_id);
118
119   if (~0 == fib_index)
120     return VNET_API_ERROR_NO_SUCH_FIB;
121
122   /*
123    * now we know which interface the table will serve, we can add the default
124    * route to use the table that the interface is bound to.
125    */
126   svs_route_add_i (fib_index, &pfx,
127                    fib_table_get_index_for_sw_if_index (fproto, sw_if_index));
128
129   vec_validate_init_empty (svs_itf_db[fproto], sw_if_index, ~0);
130
131   svs_itf_db[fproto][sw_if_index] = fib_index;
132
133   vnet_feature_enable_disable ((FIB_PROTOCOL_IP4 == fproto ?
134                                 "ip4-unicast" :
135                                 "ip6-unicast"),
136                                (FIB_PROTOCOL_IP4 == fproto ?
137                                 "svs-ip4" :
138                                 "svs-ip6"), sw_if_index, 1, NULL, 0);
139
140   return (0);
141 }
142
143 static void
144 svs_table_bind (fib_protocol_t fproto, u32 sw_if_index, u32 itf_fib_index)
145 {
146   /*
147    * update the default route to use the interface's newly bound FIB
148    */
149   u32 svs_fib_index;
150
151   if (sw_if_index >= vec_len (svs_itf_db[FIB_PROTOCOL_IP6]))
152     return;
153
154   svs_fib_index = svs_itf_db[FIB_PROTOCOL_IP6][sw_if_index];
155
156   if (~0 != svs_fib_index)
157     {
158       fib_prefix_t pfx = {
159         .fp_proto = fproto,
160       };
161
162       svs_route_add (svs_fib_index, &pfx, itf_fib_index);
163     }
164   /*
165    * else
166    *  no SVS enable on this interface
167    */
168 }
169
170 static void
171 svs_ip6_table_bind (ip6_main_t * im,
172                     uword opaque,
173                     u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
174 {
175   svs_table_bind (FIB_PROTOCOL_IP6, sw_if_index, new_fib_index);
176 }
177
178 static void
179 svs_ip4_table_bind (ip4_main_t * im,
180                     uword opaque,
181                     u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
182 {
183   svs_table_bind (FIB_PROTOCOL_IP4, sw_if_index, new_fib_index);
184 }
185
186 int
187 svs_disable (fib_protocol_t fproto, u32 table_id, u32 sw_if_index)
188 {
189   u32 fib_index;
190
191   fib_index = fib_table_find (fproto, table_id);
192
193   if (~0 == fib_index)
194     return VNET_API_ERROR_NO_SUCH_FIB;
195
196   if (sw_if_index <= vec_len (svs_itf_db[fproto]))
197     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
198
199   svs_itf_db[fproto][sw_if_index] = ~0;
200
201   vnet_feature_enable_disable ((FIB_PROTOCOL_IP4 == fproto ?
202                                 "ip4-unicast" :
203                                 "ip6-unicast"),
204                                (FIB_PROTOCOL_IP4 == fproto ?
205                                 "svs-ip4" :
206                                 "svs-ip6"), sw_if_index, 0, NULL, 0);
207
208   return (0);
209 }
210
211 void
212 svs_walk (svs_walk_fn_t fn, void *ctx)
213 {
214   fib_protocol_t fproto;
215   u32 ii, fib_index;
216
217   FOR_EACH_FIB_IP_PROTOCOL (fproto)
218   {
219     vec_foreach_index (ii, svs_itf_db[fproto])
220     {
221       fib_index = svs_itf_db[fproto][ii];
222
223       if (~0 != fib_index)
224         {
225           if (WALK_CONTINUE != fn (fproto,
226                                    fib_table_get_table_id (fib_index, fproto),
227                                    ii, ctx))
228             return;
229         }
230     }
231   }
232 }
233
234 typedef enum svs_next_t_
235 {
236   SVS_NEXT_DROP,
237   SVS_N_NEXT,
238 } svs_next_t;
239
240 typedef struct svs_input_trace_t_
241 {
242   u32 fib_index;
243 } svs_input_trace_t;
244
245 always_inline uword
246 svs_input_inline (vlib_main_t * vm,
247                   vlib_node_runtime_t * node,
248                   vlib_frame_t * frame, fib_protocol_t fproto)
249 {
250   u32 n_left_from, *from, *to_next, next_index;
251
252   from = vlib_frame_vector_args (frame);
253   n_left_from = frame->n_vectors;
254   next_index = node->cached_next_index;
255
256   while (n_left_from > 0)
257     {
258       u32 n_left_to_next;
259
260       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
261
262       while (n_left_from > 0 && n_left_to_next > 0)
263         {
264           const load_balance_t *lb0;
265           const lookup_dpo_t *lk0;
266           u32 bi0, sw_if_index0;
267           const dpo_id_t *dpo0;
268           vlib_buffer_t *b0;
269           svs_next_t next0;
270           index_t lbi0;
271
272           bi0 = from[0];
273           to_next[0] = bi0;
274           from += 1;
275           to_next += 1;
276           n_left_from -= 1;
277           n_left_to_next -= 1;
278
279           b0 = vlib_get_buffer (vm, bi0);
280           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
281
282           if (FIB_PROTOCOL_IP4 == fproto)
283             {
284               ip4_header_t *ip0;
285
286               ip0 = vlib_buffer_get_current (b0);
287               lbi0 =
288                 ip4_fib_forwarding_lookup (svs_itf_db[fproto][sw_if_index0],
289                                            &ip0->src_address);
290             }
291           else
292             {
293               ip6_header_t *ip0;
294
295               ip0 = vlib_buffer_get_current (b0);
296               lbi0 = ip6_fib_table_fwding_lookup (&ip6_main,
297                                                   svs_itf_db[fproto]
298                                                   [sw_if_index0],
299                                                   &ip0->src_address);
300             }
301           lb0 = load_balance_get (lbi0);
302           dpo0 = load_balance_get_fwd_bucket (lb0, 0);
303           lk0 = lookup_dpo_get (dpo0->dpoi_index);
304
305           vnet_buffer (b0)->sw_if_index[VLIB_TX] = lk0->lkd_fib_index;
306
307           vnet_feature_next (&next0, b0);
308
309           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
310             {
311               svs_input_trace_t *tr;
312
313               tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
314               tr->fib_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
315             }
316
317           /* verify speculative enqueue, maybe switch current next frame */
318           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
319                                            to_next, n_left_to_next, bi0,
320                                            next0);
321         }
322
323       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
324     }
325
326   return frame->n_vectors;
327 }
328
329 static uword
330 svs_input_ip4 (vlib_main_t * vm,
331                vlib_node_runtime_t * node, vlib_frame_t * frame)
332 {
333   return svs_input_inline (vm, node, frame, FIB_PROTOCOL_IP4);
334 }
335
336 static uword
337 svs_input_ip6 (vlib_main_t * vm,
338                vlib_node_runtime_t * node, vlib_frame_t * frame)
339 {
340   return svs_input_inline (vm, node, frame, FIB_PROTOCOL_IP6);
341 }
342
343 static u8 *
344 format_svs_input_trace (u8 * s, va_list * args)
345 {
346   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
347   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
348   svs_input_trace_t *t = va_arg (*args, svs_input_trace_t *);
349
350   s = format (s, " fib_index %d", t->fib_index);
351   return s;
352 }
353
354 /* *INDENT-OFF* */
355 VLIB_REGISTER_NODE (svs_ip4_node) =
356 {
357   .function = svs_input_ip4,
358   .name = "svs-ip4",
359   .vector_size = sizeof (u32),
360   .format_trace = format_svs_input_trace,
361   .type = VLIB_NODE_TYPE_INTERNAL,
362   .n_next_nodes = SVS_N_NEXT,
363   .next_nodes =
364   {
365     [SVS_NEXT_DROP] = "error-drop",
366   }
367 };
368
369 VLIB_REGISTER_NODE (svs_ip6_node) =
370 {
371   .function = svs_input_ip6,
372   .name = "svs-ip6",
373   .vector_size = sizeof (u32),
374   .format_trace = format_svs_input_trace,
375   .type = VLIB_NODE_TYPE_INTERNAL,
376   .next_nodes =
377   {
378     [SVS_NEXT_DROP] = "error-drop",
379   }
380 };
381
382 VNET_FEATURE_INIT (svs_ip4_feat, static) =
383 {
384   .arc_name = "ip4-unicast",
385   .node_name = "svs-ip4",
386 };
387
388 VNET_FEATURE_INIT (svs_ip6_feat, static) =
389 {
390   .arc_name = "ip6-unicast",
391   .node_name = "svs-ip6",
392 };
393 /* *INDENT-ON* */
394
395 static clib_error_t *
396 svs_table_cli (vlib_main_t * vm,
397                unformat_input_t * input, vlib_cli_command_t * cmd)
398 {
399   fib_protocol_t fproto;
400   u32 table_id;
401   u8 add;
402
403   fproto = FIB_PROTOCOL_IP4;
404   table_id = ~0;
405   add = 1;
406
407   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
408     {
409       if (unformat (input, "add"))
410         add = 1;
411       else if (unformat (input, "del"))
412         add = 0;
413       else if (unformat (input, "ip4"))
414         fproto = FIB_PROTOCOL_IP4;
415       else if (unformat (input, "ip6"))
416         fproto = FIB_PROTOCOL_IP6;
417       else if (unformat (input, "table-id %d", &table_id))
418         ;
419       else
420         break;
421     }
422
423   if (~0 == table_id)
424     return clib_error_return (0, "table-id must be specified");
425
426   if (add)
427     svs_table_add (fproto, table_id);
428   else
429     svs_table_delete (fproto, table_id);
430
431   return (NULL);
432 }
433
434 /* *INDENT-OFF* */
435 VLIB_CLI_COMMAND (svs_table_cmd_cli, static) = {
436     .path = "svs table",
437     .short_help = "Source VRF select table [add|delete] [ip4|ip6] table-id X",
438     .function = svs_table_cli,
439 };
440 /* *INDENT-ON* */
441
442 static clib_error_t *
443 svs_enable_cli (vlib_main_t * vm,
444                 unformat_input_t * input, vlib_cli_command_t * cmd)
445 {
446   u32 sw_if_index, table_id;
447   fib_protocol_t fproto;
448   vnet_main_t *vnm;
449   u8 enable;
450
451   vnm = vnet_get_main ();
452   sw_if_index = table_id = ~0;
453   fproto = FIB_PROTOCOL_IP4;
454   enable = 1;
455
456   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
457     {
458       if (unformat (input, "%U", unformat_vnet_sw_interface,
459                     vnm, &sw_if_index))
460         ;
461       else if (unformat (input, "enable"))
462         enable = 1;
463       else if (unformat (input, "disable"))
464         enable = 0;
465       else if (unformat (input, "ip4"))
466         fproto = FIB_PROTOCOL_IP4;
467       else if (unformat (input, "ip6"))
468         fproto = FIB_PROTOCOL_IP6;
469       else if (unformat (input, "table-id %d", &table_id))
470         ;
471       else
472         break;
473     }
474
475   if (~0 == sw_if_index)
476     return clib_error_return (0, "interface must be specified");
477   if (~0 == table_id)
478     return clib_error_return (0, "table-id must be specified");
479
480   if (enable)
481     svs_enable (fproto, table_id, sw_if_index);
482   else
483     svs_disable (fproto, table_id, sw_if_index);
484
485   return (NULL);
486 }
487
488 /* *INDENT-OFF* */
489 VLIB_CLI_COMMAND (svs_enable_cli_cmd, static) = {
490     .path = "svs enable",
491     .short_help = "Source VRF select [enable|disable] [ip4|ip6] <table-id> X <interface>",
492     .function = svs_enable_cli,
493 };
494 /* *INDENT-ON* */
495
496 static clib_error_t *
497 svs_route_cli (vlib_main_t * vm,
498                unformat_input_t * input, vlib_cli_command_t * cmd)
499 {
500   u32 table_id, src_table_id;
501   fib_prefix_t pfx;
502   int rv;
503   u8 add;
504
505   src_table_id = table_id = ~0;
506   add = 1;
507
508   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
509     {
510       if (unformat (input, "add"))
511         add = 1;
512       else if (unformat (input, "del"))
513         add = 0;
514       else if (unformat (input, "table-id %d", &table_id))
515         ;
516       else if (unformat (input, "src-table-id %d", &src_table_id))
517         ;
518       else if (unformat (input, "%U/%d",
519                          unformat_ip4_address, &pfx.fp_addr.ip4, &pfx.fp_len))
520         {
521           pfx.fp_proto = FIB_PROTOCOL_IP4;
522         }
523       else if (unformat (input, "%U/%d",
524                          unformat_ip6_address, &pfx.fp_addr.ip6, &pfx.fp_len))
525         {
526           pfx.fp_proto = FIB_PROTOCOL_IP6;
527         }
528       else
529         break;
530     }
531
532   if (~0 == table_id)
533     return clib_error_return (0, "table-id must be specified");
534   if (~0 == src_table_id)
535     return clib_error_return (0, "src-table-id must be specified");
536
537   if (add)
538     rv = svs_route_add (table_id, &pfx, src_table_id);
539   else
540     rv = svs_route_delete (table_id, &pfx);
541
542   if (rv != 0)
543     return clib_error_return (0,
544                               "failed, rv=%d:%U",
545                               (int) rv, format_vnet_api_errno, rv);
546
547   return (NULL);
548 }
549
550 /* *INDENT-OFF* */
551 VLIB_CLI_COMMAND (svs_route_cmd_cli, static) = {
552     .path = "svs route",
553     .short_help = "Source VRF select route [add|delete] <table-id> <prefix> <src-table-id>",
554     .function = svs_route_cli,
555 };
556 /* *INDENT-ON* */
557
558 static clib_error_t *
559 svs_show_cli (vlib_main_t * vm,
560               unformat_input_t * input, vlib_cli_command_t * cmd)
561 {
562   fib_protocol_t fproto;
563   u32 ii;
564
565   vlib_cli_output (vm, "Source VRF select interface to fib-index mappings:");
566   FOR_EACH_FIB_IP_PROTOCOL (fproto)
567   {
568     vlib_cli_output (vm, " %U", format_fib_protocol, fproto);
569     vec_foreach_index (ii, svs_itf_db[fproto])
570     {
571       if (~0 != svs_itf_db[fproto][ii])
572         vlib_cli_output (vm, "  %U -> %d", format_vnet_sw_if_index_name,
573                          vnet_get_main (), ii, svs_itf_db[fproto][ii]);
574     }
575   }
576   return (NULL);
577 }
578
579 /* *INDENT-OFF* */
580 VLIB_CLI_COMMAND (svs_show_cli_cmd, static) = {
581   .path = "show svs",
582   .short_help = "Source VRF select show",
583   .function = svs_show_cli,
584 };
585 /* *INDENT-ON* */
586
587 static clib_error_t *
588 svs_init (vlib_main_t * vm)
589 {
590   ip6_table_bind_callback_t cbt6 = {
591     .function = svs_ip6_table_bind,
592   };
593   vec_add1 (ip6_main.table_bind_callbacks, cbt6);
594
595   ip4_table_bind_callback_t cbt4 = {
596     .function = svs_ip4_table_bind,
597   };
598   vec_add1 (ip4_main.table_bind_callbacks, cbt4);
599
600   return (NULL);
601 }
602
603 VLIB_INIT_FUNCTION (svs_init);
604
605 /*
606  * fd.io coding-style-patch-verification: ON
607  *
608  * Local Variables:
609  * eval: (c-set-style "gnu")
610  * End:
611  */