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/l3xc/l3xc.h>
18 #include <vlib/vlib.h>
19 #include <vnet/plugin/plugin.h>
20 #include <vnet/fib/fib_path_list.h>
23 * FIB node type the attachment is registered
25 fib_node_type_t l3xc_fib_node_type;
28 * Pool of L3XC objects
35 static u32 *l3xc_db[FIB_PROTOCOL_IP_MAX];
38 l3xc_find (u32 sw_if_index, fib_protocol_t fproto)
40 if (vec_len (l3xc_db[fproto]) <= sw_if_index)
43 return (l3xc_db[fproto][sw_if_index]);
47 l3xc_db_add (u32 sw_if_index, fib_protocol_t fproto, index_t l3xci)
49 vec_validate_init_empty (l3xc_db[fproto], sw_if_index, ~0);
51 l3xc_db[fproto][sw_if_index] = l3xci;
55 l3xc_db_remove (u32 sw_if_index, fib_protocol_t fproto)
57 vec_validate_init_empty (l3xc_db[fproto], sw_if_index, ~0);
59 l3xc_db[fproto][sw_if_index] = ~0;
63 l3xc_stack (l3xc_t * l3xc)
66 * stack the DPO on the forwarding contributed by the path-list
68 dpo_id_t via_dpo = DPO_INVALID;
70 fib_path_list_contribute_forwarding (
72 (FIB_PROTOCOL_IP4 == l3xc->l3xc_proto ? FIB_FORW_CHAIN_TYPE_UNICAST_IP4 :
73 FIB_FORW_CHAIN_TYPE_UNICAST_IP6),
74 FIB_PATH_LIST_FWD_FLAG_COLLAPSE, &via_dpo);
76 dpo_stack_from_node ((FIB_PROTOCOL_IP4 == l3xc->l3xc_proto ?
78 l3xc_ip6_node.index), &l3xc->l3xc_dpo, &via_dpo);
83 l3xc_update (u32 sw_if_index, u8 is_ip6, const fib_route_path_t * rpaths)
85 fib_protocol_t fproto;
89 fproto = (is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
91 l3xci = l3xc_find (sw_if_index, fproto);
93 if (INDEX_INVALID == l3xci)
96 * create a new x-connect
98 pool_get_aligned_zero (l3xc_pool, l3xc, CLIB_CACHE_LINE_BYTES);
100 l3xci = l3xc - l3xc_pool;
101 fib_node_init (&l3xc->l3xc_node, l3xc_fib_node_type);
102 l3xc->l3xc_sw_if_index = sw_if_index;
103 l3xc->l3xc_proto = fproto;
106 * create and become a child of a path list so we get poked when
107 * the forwarding changes and stack on the DPO the path-list provides
109 l3xc->l3xc_pl = fib_path_list_create ((FIB_PATH_LIST_FLAG_SHARED |
110 FIB_PATH_LIST_FLAG_NO_URPF),
112 l3xc->l3xc_sibling = fib_path_list_child_add (l3xc->l3xc_pl,
118 * add this new policy to the DB and enable the feature on input interface
120 l3xc_db_add (sw_if_index, fproto, l3xci);
122 vnet_feature_enable_disable ((FIB_PROTOCOL_IP4 == fproto ?
125 (FIB_PROTOCOL_IP4 == fproto ?
128 l3xc->l3xc_sw_if_index,
129 1, &l3xci, sizeof (l3xci));
134 * update an existing x-connect.
135 * - add the path to the path-list and swap our ancestry
137 l3xc = l3xc_get (l3xci);
139 if (FIB_NODE_INDEX_INVALID != l3xc->l3xc_pl)
141 fib_path_list_child_remove (l3xc->l3xc_pl, l3xc->l3xc_sibling);
144 l3xc->l3xc_pl = fib_path_list_create ((FIB_PATH_LIST_FLAG_SHARED |
145 FIB_PATH_LIST_FLAG_NO_URPF),
148 l3xc->l3xc_sibling = fib_path_list_child_add (l3xc->l3xc_pl,
156 l3xc_delete (u32 sw_if_index, u8 is_ip6)
158 fib_protocol_t fproto;
162 fproto = (is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
164 l3xci = l3xc_find (sw_if_index, fproto);
166 if (INDEX_INVALID == l3xci)
171 return (VNET_API_ERROR_INVALID_VALUE);
175 l3xc = l3xc_get (l3xci);
177 vnet_feature_enable_disable ((FIB_PROTOCOL_IP4 == fproto ?
180 (FIB_PROTOCOL_IP4 == fproto ?
183 l3xc->l3xc_sw_if_index,
184 0, &l3xci, sizeof (l3xci));
186 fib_path_list_child_remove (l3xc->l3xc_pl, l3xc->l3xc_sibling);
187 dpo_reset (&l3xc->l3xc_dpo);
189 l3xc_db_remove (l3xc->l3xc_sw_if_index, fproto);
190 pool_put (l3xc_pool, l3xc);
196 static clib_error_t *
197 l3xc_cmd (vlib_main_t * vm,
198 unformat_input_t * main_input, vlib_cli_command_t * cmd)
200 unformat_input_t _line_input, *line_input = &_line_input;
201 fib_route_path_t *rpaths = NULL, rpath;
202 u32 sw_if_index, is_del, is_ip6;
203 dpo_proto_t payload_proto;
209 vnm = vnet_get_main ();
211 /* Get a line of input. */
212 if (!unformat_user (main_input, unformat_line_input, line_input))
215 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
218 (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
220 else if (unformat (line_input, "ip6"))
222 else if (unformat (line_input, "ip4"))
224 else if (unformat (line_input, "del"))
226 else if (unformat (line_input, "add"))
228 else if (unformat (line_input, "via %U",
229 unformat_fib_route_path, &rpath, &payload_proto))
230 vec_add1 (rpaths, rpath);
232 return (clib_error_return (0, "unknown input '%U'",
233 format_unformat_error, line_input));
236 if (~0 == sw_if_index)
238 vlib_cli_output (vm, "Specify an input interface");
241 if (vec_len (rpaths) == 0)
243 vlib_cli_output (vm, "Specify some paths");
249 rv = l3xc_update (sw_if_index, is_ip6, rpaths);
253 vlib_cli_output (vm, "Failed: %d", rv);
259 l3xc_delete (sw_if_index, is_ip6);
263 unformat_free (line_input);
269 * Create an L3XC policy.
271 VLIB_CLI_COMMAND (l3xc_cmd_node, static) = {
273 .function = l3xc_cmd,
274 .short_help = "l3xc [add|del] <INTERFACE> via ...",
280 format_l3xc (u8 * s, va_list * args)
282 l3xc_t *l3xc = va_arg (*args, l3xc_t *);
283 vnet_main_t *vnm = vnet_get_main ();
285 s = format (s, "l3xc:[%d]: %U",
286 l3xc - l3xc_pool, format_vnet_sw_if_index_name,
287 vnm, l3xc->l3xc_sw_if_index);
288 s = format (s, "\n");
289 if (FIB_NODE_INDEX_INVALID == l3xc->l3xc_pl)
291 s = format (s, "no forwarding");
295 s = fib_path_list_format (l3xc->l3xc_pl, s);
297 s = format (s, "\n %U", format_dpo_id, &l3xc->l3xc_dpo, 4);
304 l3xc_walk (l3xc_walk_cb_t cb, void *ctx)
309 pool_foreach_index (l3xci, l3xc_pool)
317 static clib_error_t *
318 l3xc_show_cmd (vlib_main_t * vm,
319 unformat_input_t * input, vlib_cli_command_t * cmd)
324 pool_foreach (l3xc, l3xc_pool)
326 vlib_cli_output(vm, "%U", format_l3xc, l3xc);
334 VLIB_CLI_COMMAND (l3xc_show_cmd_node, static) = {
336 .function = l3xc_show_cmd,
337 .short_help = "show l3xc",
343 l3xc_get_node (fib_node_index_t index)
345 l3xc_t *l3xc = l3xc_get (index);
346 return (&(l3xc->l3xc_node));
350 l3xc_get_from_node (fib_node_t * node)
352 return ((l3xc_t *) (((char *) node) -
353 STRUCT_OFFSET_OF (l3xc_t, l3xc_node)));
357 l3xc_last_lock_gone (fib_node_t * node)
362 * A back walk has reached this L3XC policy
364 static fib_node_back_walk_rc_t
365 l3xc_back_walk_notify (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
367 l3xc_stack (l3xc_get_from_node (node));
369 return (FIB_NODE_BACK_WALK_CONTINUE);
373 * The BIER fmask's graph node virtual function table
375 static const fib_node_vft_t l3xc_vft = {
376 .fnv_get = l3xc_get_node,
377 .fnv_last_lock = l3xc_last_lock_gone,
378 .fnv_back_walk = l3xc_back_walk_notify,
381 static clib_error_t *
382 l3xc_init (vlib_main_t * vm)
384 l3xc_fib_node_type = fib_node_register_new_type ("l3xc", &l3xc_vft);
389 VLIB_INIT_FUNCTION (l3xc_init);
392 * fd.io coding-style-patch-verification: ON
395 * eval: (c-set-style "gnu")