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