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);
44 vec_foreach_index (ii, svs_itf_db[fproto])
46 if (svs_itf_db[fproto][ii] == fib_index)
47 return VNET_API_ERROR_INSTANCE_IN_USE;
51 return VNET_API_ERROR_NO_SUCH_FIB;
53 fib_table_unlock (fib_index, fproto, FIB_SOURCE_PLUGIN_LOW);
59 svs_route_add_i (u32 fib_index, const fib_prefix_t * pfx, u32 src_fib_index)
61 dpo_id_t dpo = DPO_INVALID;
64 lookup_dpo_add_or_lock_w_fib_index (src_fib_index,
65 fib_proto_to_dpo (pfx->fp_proto),
67 LOOKUP_INPUT_SRC_ADDR,
68 LOOKUP_TABLE_FROM_CONFIG, &dpo);
70 fib_table_entry_special_dpo_add (fib_index, pfx,
71 FIB_SOURCE_PLUGIN_LOW,
72 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
80 svs_route_add (u32 table_id, const fib_prefix_t * pfx, u32 source_table_id)
82 u32 fib_index, src_fib_index;
85 fib_index = fib_table_find (pfx->fp_proto, table_id);
88 return VNET_API_ERROR_NO_SUCH_FIB;
90 src_fib_index = fib_table_find (pfx->fp_proto, source_table_id);
92 if (~0 == src_fib_index)
93 return (VNET_API_ERROR_NO_SUCH_FIB);
95 rv = svs_route_add_i (fib_index, pfx, src_fib_index);
101 svs_route_delete (u32 table_id, const fib_prefix_t * pfx)
105 fib_index = fib_table_find (pfx->fp_proto, table_id);
108 return VNET_API_ERROR_NO_SUCH_FIB;
110 fib_table_entry_special_remove (fib_index, pfx, FIB_SOURCE_PLUGIN_LOW);
116 svs_enable (fib_protocol_t fproto, u32 table_id, u32 sw_if_index)
123 fib_index = fib_table_find (fproto, table_id);
126 return VNET_API_ERROR_NO_SUCH_FIB;
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.
132 svs_route_add_i (fib_index, &pfx,
133 fib_table_get_index_for_sw_if_index (fproto, sw_if_index));
135 vec_validate_init_empty (svs_itf_db[fproto], sw_if_index, ~0);
137 svs_itf_db[fproto][sw_if_index] = fib_index;
139 vnet_feature_enable_disable ((FIB_PROTOCOL_IP4 == fproto ?
142 (FIB_PROTOCOL_IP4 == fproto ?
144 "svs-ip6"), sw_if_index, 1, NULL, 0);
150 svs_table_bind (fib_protocol_t fproto, u32 sw_if_index, u32 itf_fib_index)
153 * update the default route to use the interface's newly bound FIB
157 if (sw_if_index >= vec_len (svs_itf_db[FIB_PROTOCOL_IP6]))
160 svs_fib_index = svs_itf_db[FIB_PROTOCOL_IP6][sw_if_index];
162 if (~0 != svs_fib_index)
168 svs_route_add (svs_fib_index, &pfx, itf_fib_index);
172 * no SVS enable on this interface
177 svs_ip6_table_bind (ip6_main_t * im,
179 u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
181 svs_table_bind (FIB_PROTOCOL_IP6, sw_if_index, new_fib_index);
185 svs_ip4_table_bind (ip4_main_t * im,
187 u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
189 svs_table_bind (FIB_PROTOCOL_IP4, sw_if_index, new_fib_index);
193 svs_disable (fib_protocol_t fproto, u32 table_id, u32 sw_if_index)
200 fib_index = fib_table_find (fproto, table_id);
203 return VNET_API_ERROR_NO_SUCH_FIB;
205 if (sw_if_index >= vec_len (svs_itf_db[fproto]))
206 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
208 svs_itf_db[fproto][sw_if_index] = ~0;
210 vnet_feature_enable_disable ((FIB_PROTOCOL_IP4 == fproto ?
213 (FIB_PROTOCOL_IP4 == fproto ?
215 "svs-ip6"), sw_if_index, 0, NULL, 0);
217 fib_table_entry_special_remove (fib_index, &pfx, FIB_SOURCE_PLUGIN_LOW);
223 svs_walk (svs_walk_fn_t fn, void *ctx)
225 fib_protocol_t fproto;
228 FOR_EACH_FIB_IP_PROTOCOL (fproto)
230 vec_foreach_index (ii, svs_itf_db[fproto])
232 fib_index = svs_itf_db[fproto][ii];
236 if (WALK_CONTINUE != fn (fproto,
237 fib_table_get_table_id (fib_index, fproto),
245 typedef enum svs_next_t_
251 typedef struct svs_input_trace_t_
257 svs_input_inline (vlib_main_t * vm,
258 vlib_node_runtime_t * node,
259 vlib_frame_t * frame, fib_protocol_t fproto)
261 u32 n_left_from, *from, *to_next, next_index;
263 from = vlib_frame_vector_args (frame);
264 n_left_from = frame->n_vectors;
265 next_index = node->cached_next_index;
267 while (n_left_from > 0)
271 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
273 while (n_left_from > 0 && n_left_to_next > 0)
275 const load_balance_t *lb0;
276 const lookup_dpo_t *lk0;
277 u32 bi0, sw_if_index0;
278 const dpo_id_t *dpo0;
290 b0 = vlib_get_buffer (vm, bi0);
291 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
293 if (FIB_PROTOCOL_IP4 == fproto)
297 ip0 = vlib_buffer_get_current (b0);
299 ip4_fib_forwarding_lookup (svs_itf_db[fproto][sw_if_index0],
306 ip0 = vlib_buffer_get_current (b0);
307 lbi0 = ip6_fib_table_fwding_lookup (&ip6_main,
312 lb0 = load_balance_get (lbi0);
313 dpo0 = load_balance_get_fwd_bucket (lb0, 0);
314 lk0 = lookup_dpo_get (dpo0->dpoi_index);
316 vnet_buffer (b0)->sw_if_index[VLIB_TX] = lk0->lkd_fib_index;
318 vnet_feature_next (&next0, b0);
320 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
322 svs_input_trace_t *tr;
324 tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
325 tr->fib_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
328 /* verify speculative enqueue, maybe switch current next frame */
329 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
330 to_next, n_left_to_next, bi0,
334 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
337 return frame->n_vectors;
341 svs_input_ip4 (vlib_main_t * vm,
342 vlib_node_runtime_t * node, vlib_frame_t * frame)
344 return svs_input_inline (vm, node, frame, FIB_PROTOCOL_IP4);
348 svs_input_ip6 (vlib_main_t * vm,
349 vlib_node_runtime_t * node, vlib_frame_t * frame)
351 return svs_input_inline (vm, node, frame, FIB_PROTOCOL_IP6);
355 format_svs_input_trace (u8 * s, va_list * args)
357 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
358 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
359 svs_input_trace_t *t = va_arg (*args, svs_input_trace_t *);
361 s = format (s, " fib_index %d", t->fib_index);
366 VLIB_REGISTER_NODE (svs_ip4_node) =
368 .function = svs_input_ip4,
370 .vector_size = sizeof (u32),
371 .format_trace = format_svs_input_trace,
372 .type = VLIB_NODE_TYPE_INTERNAL,
373 .n_next_nodes = SVS_N_NEXT,
376 [SVS_NEXT_DROP] = "error-drop",
380 VLIB_REGISTER_NODE (svs_ip6_node) =
382 .function = svs_input_ip6,
384 .vector_size = sizeof (u32),
385 .format_trace = format_svs_input_trace,
386 .type = VLIB_NODE_TYPE_INTERNAL,
389 [SVS_NEXT_DROP] = "error-drop",
393 VNET_FEATURE_INIT (svs_ip4_feat, static) =
395 .arc_name = "ip4-unicast",
396 .node_name = "svs-ip4",
399 VNET_FEATURE_INIT (svs_ip6_feat, static) =
401 .arc_name = "ip6-unicast",
402 .node_name = "svs-ip6",
406 static clib_error_t *
407 svs_table_cli (vlib_main_t * vm,
408 unformat_input_t * input, vlib_cli_command_t * cmd)
410 fib_protocol_t fproto;
414 fproto = FIB_PROTOCOL_IP4;
418 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
420 if (unformat (input, "add"))
422 else if (unformat (input, "del"))
424 else if (unformat (input, "ip4"))
425 fproto = FIB_PROTOCOL_IP4;
426 else if (unformat (input, "ip6"))
427 fproto = FIB_PROTOCOL_IP6;
428 else if (unformat (input, "table-id %d", &table_id))
435 return clib_error_return (0, "table-id must be specified");
438 svs_table_add (fproto, table_id);
440 svs_table_delete (fproto, table_id);
446 VLIB_CLI_COMMAND (svs_table_cmd_cli, static) = {
448 .short_help = "Source VRF select table [add|delete] [ip4|ip6] table-id X",
449 .function = svs_table_cli,
453 static clib_error_t *
454 svs_enable_cli (vlib_main_t * vm,
455 unformat_input_t * input, vlib_cli_command_t * cmd)
457 u32 sw_if_index, table_id;
458 fib_protocol_t fproto;
462 vnm = vnet_get_main ();
463 sw_if_index = table_id = ~0;
464 fproto = FIB_PROTOCOL_IP4;
467 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
469 if (unformat (input, "%U", unformat_vnet_sw_interface,
472 else if (unformat (input, "enable"))
474 else if (unformat (input, "disable"))
476 else if (unformat (input, "ip4"))
477 fproto = FIB_PROTOCOL_IP4;
478 else if (unformat (input, "ip6"))
479 fproto = FIB_PROTOCOL_IP6;
480 else if (unformat (input, "table-id %d", &table_id))
486 if (~0 == sw_if_index)
487 return clib_error_return (0, "interface must be specified");
489 return clib_error_return (0, "table-id must be specified");
492 svs_enable (fproto, table_id, sw_if_index);
494 svs_disable (fproto, table_id, sw_if_index);
500 VLIB_CLI_COMMAND (svs_enable_cli_cmd, static) = {
501 .path = "svs enable",
502 .short_help = "Source VRF select [enable|disable] [ip4|ip6] <table-id> X <interface>",
503 .function = svs_enable_cli,
507 static clib_error_t *
508 svs_route_cli (vlib_main_t * vm,
509 unformat_input_t * input, vlib_cli_command_t * cmd)
511 u32 table_id, src_table_id;
516 src_table_id = table_id = ~0;
519 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
521 if (unformat (input, "add"))
523 else if (unformat (input, "del"))
525 else if (unformat (input, "table-id %d", &table_id))
527 else if (unformat (input, "src-table-id %d", &src_table_id))
529 else if (unformat (input, "%U/%d",
530 unformat_ip4_address, &pfx.fp_addr.ip4, &pfx.fp_len))
532 pfx.fp_proto = FIB_PROTOCOL_IP4;
534 else if (unformat (input, "%U/%d",
535 unformat_ip6_address, &pfx.fp_addr.ip6, &pfx.fp_len))
537 pfx.fp_proto = FIB_PROTOCOL_IP6;
544 return clib_error_return (0, "table-id must be specified");
545 if (~0 == src_table_id)
546 return clib_error_return (0, "src-table-id must be specified");
549 rv = svs_route_add (table_id, &pfx, src_table_id);
551 rv = svs_route_delete (table_id, &pfx);
554 return clib_error_return (0,
556 (int) rv, format_vnet_api_errno, rv);
562 VLIB_CLI_COMMAND (svs_route_cmd_cli, static) = {
564 .short_help = "Source VRF select route [add|delete] <table-id> <prefix> <src-table-id>",
565 .function = svs_route_cli,
569 static clib_error_t *
570 svs_show_cli (vlib_main_t * vm,
571 unformat_input_t * input, vlib_cli_command_t * cmd)
573 fib_protocol_t fproto;
576 vlib_cli_output (vm, "Source VRF select interface to fib-index mappings:");
577 FOR_EACH_FIB_IP_PROTOCOL (fproto)
579 vlib_cli_output (vm, " %U", format_fib_protocol, fproto);
580 vec_foreach_index (ii, svs_itf_db[fproto])
582 if (~0 != svs_itf_db[fproto][ii])
583 vlib_cli_output (vm, " %U -> %d", format_vnet_sw_if_index_name,
584 vnet_get_main (), ii, svs_itf_db[fproto][ii]);
591 VLIB_CLI_COMMAND (svs_show_cli_cmd, static) = {
593 .short_help = "Source VRF select show",
594 .function = svs_show_cli,
598 static clib_error_t *
599 svs_init (vlib_main_t * vm)
601 ip6_table_bind_callback_t cbt6 = {
602 .function = svs_ip6_table_bind,
604 vec_add1 (ip6_main.table_bind_callbacks, cbt6);
606 ip4_table_bind_callback_t cbt4 = {
607 .function = svs_ip4_table_bind,
609 vec_add1 (ip4_main.table_bind_callbacks, cbt4);
614 VLIB_INIT_FUNCTION (svs_init);
617 * fd.io coding-style-patch-verification: ON
620 * eval: (c-set-style "gnu")