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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <plugins/svs/svs.h>
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>
27 u32 *svs_itf_db[FIB_PROTOCOL_IP_MAX];
30 svs_table_add (fib_protocol_t fproto, u32 table_id)
32 fib_table_find_or_create_and_lock (fproto, table_id, FIB_SOURCE_PLUGIN_LOW);
38 svs_table_delete (fib_protocol_t fproto, u32 table_id)
42 fib_index = fib_table_find (fproto, table_id);
45 return VNET_API_ERROR_NO_SUCH_FIB;
47 fib_table_unlock (fib_index, fproto, FIB_SOURCE_PLUGIN_LOW);
53 svs_route_add_i (u32 fib_index, const fib_prefix_t * pfx, u32 src_fib_index)
55 dpo_id_t dpo = DPO_INVALID;
58 lookup_dpo_add_or_lock_w_fib_index (src_fib_index,
59 fib_proto_to_dpo (pfx->fp_proto),
61 LOOKUP_INPUT_SRC_ADDR,
62 LOOKUP_TABLE_FROM_CONFIG, &dpo);
64 fib_table_entry_special_dpo_add (fib_index, pfx,
65 FIB_SOURCE_PLUGIN_LOW,
66 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
74 svs_route_add (u32 table_id, const fib_prefix_t * pfx, u32 source_table_id)
76 u32 fib_index, src_fib_index;
79 fib_index = fib_table_find (pfx->fp_proto, table_id);
82 return VNET_API_ERROR_NO_SUCH_FIB;
84 src_fib_index = fib_table_find (pfx->fp_proto, source_table_id);
86 if (~0 == src_fib_index)
87 return (VNET_API_ERROR_NO_SUCH_FIB);
89 rv = svs_route_add_i (fib_index, pfx, src_fib_index);
95 svs_route_delete (u32 table_id, const fib_prefix_t * pfx)
99 fib_index = fib_table_find (pfx->fp_proto, table_id);
102 return VNET_API_ERROR_NO_SUCH_FIB;
104 fib_table_entry_special_remove (fib_index, pfx, FIB_SOURCE_PLUGIN_LOW);
110 svs_enable (fib_protocol_t fproto, u32 table_id, u32 sw_if_index)
117 fib_index = fib_table_find (fproto, table_id);
120 return VNET_API_ERROR_NO_SUCH_FIB;
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.
126 svs_route_add_i (fib_index, &pfx,
127 fib_table_get_index_for_sw_if_index (fproto, sw_if_index));
129 vec_validate_init_empty (svs_itf_db[fproto], sw_if_index, ~0);
131 svs_itf_db[fproto][sw_if_index] = fib_index;
133 vnet_feature_enable_disable ((FIB_PROTOCOL_IP4 == fproto ?
136 (FIB_PROTOCOL_IP4 == fproto ?
138 "svs-ip6"), sw_if_index, 1, NULL, 0);
144 svs_table_bind (fib_protocol_t fproto, u32 sw_if_index, u32 itf_fib_index)
147 * update the default route to use the interface's newly bound FIB
151 if (sw_if_index >= vec_len (svs_itf_db[FIB_PROTOCOL_IP6]))
154 svs_fib_index = svs_itf_db[FIB_PROTOCOL_IP6][sw_if_index];
156 if (~0 != svs_fib_index)
162 svs_route_add (svs_fib_index, &pfx, itf_fib_index);
166 * no SVS enable on this interface
171 svs_ip6_table_bind (ip6_main_t * im,
173 u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
175 svs_table_bind (FIB_PROTOCOL_IP6, sw_if_index, new_fib_index);
179 svs_ip4_table_bind (ip4_main_t * im,
181 u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
183 svs_table_bind (FIB_PROTOCOL_IP4, sw_if_index, new_fib_index);
187 svs_disable (fib_protocol_t fproto, u32 table_id, u32 sw_if_index)
191 fib_index = fib_table_find (fproto, table_id);
194 return VNET_API_ERROR_NO_SUCH_FIB;
196 if (sw_if_index <= vec_len (svs_itf_db[fproto]))
197 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
199 svs_itf_db[fproto][sw_if_index] = ~0;
201 vnet_feature_enable_disable ((FIB_PROTOCOL_IP4 == fproto ?
204 (FIB_PROTOCOL_IP4 == fproto ?
206 "svs-ip6"), sw_if_index, 0, NULL, 0);
212 svs_walk (svs_walk_fn_t fn, void *ctx)
214 fib_protocol_t fproto;
217 FOR_EACH_FIB_IP_PROTOCOL (fproto)
219 vec_foreach_index (ii, svs_itf_db[fproto])
221 fib_index = svs_itf_db[fproto][ii];
225 if (WALK_CONTINUE != fn (fproto,
226 fib_table_get_table_id (fib_index, fproto),
234 typedef enum svs_next_t_
240 typedef struct svs_input_trace_t_
246 svs_input_inline (vlib_main_t * vm,
247 vlib_node_runtime_t * node,
248 vlib_frame_t * frame, fib_protocol_t fproto)
250 u32 n_left_from, *from, *to_next, next_index;
252 from = vlib_frame_vector_args (frame);
253 n_left_from = frame->n_vectors;
254 next_index = node->cached_next_index;
256 while (n_left_from > 0)
260 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
262 while (n_left_from > 0 && n_left_to_next > 0)
264 const load_balance_t *lb0;
265 const lookup_dpo_t *lk0;
266 u32 bi0, sw_if_index0;
267 const dpo_id_t *dpo0;
279 b0 = vlib_get_buffer (vm, bi0);
280 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
282 if (FIB_PROTOCOL_IP4 == fproto)
286 ip0 = vlib_buffer_get_current (b0);
288 ip4_fib_forwarding_lookup (svs_itf_db[fproto][sw_if_index0],
295 ip0 = vlib_buffer_get_current (b0);
296 lbi0 = ip6_fib_table_fwding_lookup (&ip6_main,
301 lb0 = load_balance_get (lbi0);
302 dpo0 = load_balance_get_fwd_bucket (lb0, 0);
303 lk0 = lookup_dpo_get (dpo0->dpoi_index);
305 vnet_buffer (b0)->sw_if_index[VLIB_TX] = lk0->lkd_fib_index;
307 vnet_feature_next (&next0, b0);
309 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
311 svs_input_trace_t *tr;
313 tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
314 tr->fib_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
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,
323 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
326 return frame->n_vectors;
330 svs_input_ip4 (vlib_main_t * vm,
331 vlib_node_runtime_t * node, vlib_frame_t * frame)
333 return svs_input_inline (vm, node, frame, FIB_PROTOCOL_IP4);
337 svs_input_ip6 (vlib_main_t * vm,
338 vlib_node_runtime_t * node, vlib_frame_t * frame)
340 return svs_input_inline (vm, node, frame, FIB_PROTOCOL_IP6);
344 format_svs_input_trace (u8 * s, va_list * args)
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 *);
350 s = format (s, " fib_index %d", t->fib_index);
355 VLIB_REGISTER_NODE (svs_ip4_node) =
357 .function = svs_input_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,
365 [SVS_NEXT_DROP] = "error-drop",
369 VLIB_REGISTER_NODE (svs_ip6_node) =
371 .function = svs_input_ip6,
373 .vector_size = sizeof (u32),
374 .format_trace = format_svs_input_trace,
375 .type = VLIB_NODE_TYPE_INTERNAL,
378 [SVS_NEXT_DROP] = "error-drop",
382 VNET_FEATURE_INIT (svs_ip4_feat, static) =
384 .arc_name = "ip4-unicast",
385 .node_name = "svs-ip4",
388 VNET_FEATURE_INIT (svs_ip6_feat, static) =
390 .arc_name = "ip6-unicast",
391 .node_name = "svs-ip6",
395 static clib_error_t *
396 svs_table_cli (vlib_main_t * vm,
397 unformat_input_t * input, vlib_cli_command_t * cmd)
399 fib_protocol_t fproto;
403 fproto = FIB_PROTOCOL_IP4;
407 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
409 if (unformat (input, "add"))
411 else if (unformat (input, "del"))
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))
424 return clib_error_return (0, "table-id must be specified");
427 svs_table_add (fproto, table_id);
429 svs_table_delete (fproto, table_id);
435 VLIB_CLI_COMMAND (svs_table_cmd_cli, static) = {
437 .short_help = "Source VRF select table [add|delete] [ip4|ip6] table-id X",
438 .function = svs_table_cli,
442 static clib_error_t *
443 svs_enable_cli (vlib_main_t * vm,
444 unformat_input_t * input, vlib_cli_command_t * cmd)
446 u32 sw_if_index, table_id;
447 fib_protocol_t fproto;
451 vnm = vnet_get_main ();
452 sw_if_index = table_id = ~0;
453 fproto = FIB_PROTOCOL_IP4;
456 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
458 if (unformat (input, "%U", unformat_vnet_sw_interface,
461 else if (unformat (input, "enable"))
463 else if (unformat (input, "disable"))
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))
475 if (~0 == sw_if_index)
476 return clib_error_return (0, "interface must be specified");
478 return clib_error_return (0, "table-id must be specified");
481 svs_enable (fproto, table_id, sw_if_index);
483 svs_disable (fproto, table_id, sw_if_index);
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,
496 static clib_error_t *
497 svs_route_cli (vlib_main_t * vm,
498 unformat_input_t * input, vlib_cli_command_t * cmd)
500 u32 table_id, src_table_id;
505 src_table_id = table_id = ~0;
508 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
510 if (unformat (input, "add"))
512 else if (unformat (input, "del"))
514 else if (unformat (input, "table-id %d", &table_id))
516 else if (unformat (input, "src-table-id %d", &src_table_id))
518 else if (unformat (input, "%U/%d",
519 unformat_ip4_address, &pfx.fp_addr.ip4, &pfx.fp_len))
521 pfx.fp_proto = FIB_PROTOCOL_IP4;
523 else if (unformat (input, "%U/%d",
524 unformat_ip6_address, &pfx.fp_addr.ip6, &pfx.fp_len))
526 pfx.fp_proto = FIB_PROTOCOL_IP6;
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");
538 rv = svs_route_add (table_id, &pfx, src_table_id);
540 rv = svs_route_delete (table_id, &pfx);
543 return clib_error_return (0,
545 (int) rv, format_vnet_api_errno, rv);
551 VLIB_CLI_COMMAND (svs_route_cmd_cli, static) = {
553 .short_help = "Source VRF select route [add|delete] <table-id> <prefix> <src-table-id>",
554 .function = svs_route_cli,
558 static clib_error_t *
559 svs_show_cli (vlib_main_t * vm,
560 unformat_input_t * input, vlib_cli_command_t * cmd)
562 fib_protocol_t fproto;
565 vlib_cli_output (vm, "Source VRF select interface to fib-index mappings:");
566 FOR_EACH_FIB_IP_PROTOCOL (fproto)
568 vlib_cli_output (vm, " %U", format_fib_protocol, fproto);
569 vec_foreach_index (ii, svs_itf_db[fproto])
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]);
580 VLIB_CLI_COMMAND (svs_show_cli_cmd, static) = {
582 .short_help = "Source VRF select show",
583 .function = svs_show_cli,
587 static clib_error_t *
588 svs_init (vlib_main_t * vm)
590 ip6_table_bind_callback_t cbt6 = {
591 .function = svs_ip6_table_bind,
593 vec_add1 (ip6_main.table_bind_callbacks, cbt6);
595 ip4_table_bind_callback_t cbt4 = {
596 .function = svs_ip4_table_bind,
598 vec_add1 (ip4_main.table_bind_callbacks, cbt4);
603 VLIB_INIT_FUNCTION (svs_init);
606 * fd.io coding-style-patch-verification: ON
609 * eval: (c-set-style "gnu")