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/gbp/gbp_ext_itf.h>
17 #include <plugins/gbp/gbp_bridge_domain.h>
18 #include <plugins/gbp/gbp_route_domain.h>
19 #include <plugins/gbp/gbp_itf.h>
22 * Pool of GBP ext_itfs
24 gbp_ext_itf_t *gbp_ext_itf_pool;
27 * external interface configs keyed by sw_if_index
29 index_t *gbp_ext_itf_db;
31 #define GBP_EXT_ITF_ID 0x00000080
36 vlib_log_class_t gx_logger;
38 #define GBP_EXT_ITF_DBG(...) \
39 vlib_log_debug (gx_logger, __VA_ARGS__);
42 format_gbp_ext_itf (u8 * s, va_list * args)
44 gbp_ext_itf_t *gx = va_arg (*args, gbp_ext_itf_t *);
46 return (format (s, "%U%s in %U",
47 format_gbp_itf_hdl, gx->gx_itf,
48 (gx->gx_flags & GBP_EXT_ITF_F_ANON) ? " [anon]" : "",
49 format_gbp_bridge_domain, gx->gx_bd));
53 gbp_ext_itf_add (u32 sw_if_index, u32 bd_id, u32 rd_id, u32 flags)
58 vec_validate_init_empty (gbp_ext_itf_db, sw_if_index, INDEX_INVALID);
60 gxi = gbp_ext_itf_db[sw_if_index];
62 if (INDEX_INVALID == gxi)
64 gbp_route_domain_t *gr;
65 fib_protocol_t fproto;
68 gbi = gbp_bridge_domain_find_and_lock (bd_id);
70 if (INDEX_INVALID == gbi)
71 return (VNET_API_ERROR_NO_SUCH_ENTRY);
73 gri = gbp_route_domain_find_and_lock (rd_id);
75 if (INDEX_INVALID == gri)
77 gbp_bridge_domain_unlock (gbi);
78 return (VNET_API_ERROR_NO_SUCH_ENTRY);
81 pool_get_zero (gbp_ext_itf_pool, gx);
82 gxi = gx - gbp_ext_itf_pool;
84 gr = gbp_route_domain_get (gri);
88 gbp_itf_hdl_reset (&gx->gx_itf);
90 FOR_EACH_FIB_IP_PROTOCOL (fproto)
92 gx->gx_fib_index[fproto] =
93 gr->grd_fib_index[fib_proto_to_dpo (fproto)];
96 if (flags & GBP_EXT_ITF_F_ANON)
98 /* add interface to the BD */
99 gx->gx_itf = gbp_itf_l2_add_and_lock (sw_if_index, gbi);
101 /* setup GBP L2 features on this interface */
102 gbp_itf_l2_set_input_feature (gx->gx_itf,
103 L2INPUT_FEAT_GBP_LPM_ANON_CLASSIFY |
105 gbp_itf_l2_set_output_feature (gx->gx_itf,
106 L2OUTPUT_FEAT_GBP_POLICY_LPM);
109 gx->gx_flags = flags;
111 gbp_ext_itf_db[sw_if_index] = gxi;
113 GBP_EXT_ITF_DBG ("add: %U", format_gbp_ext_itf, gx);
118 return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
122 gbp_ext_itf_delete (u32 sw_if_index)
127 if (vec_len (gbp_ext_itf_db) <= sw_if_index)
128 return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
130 gxi = gbp_ext_itf_db[sw_if_index];
132 if (INDEX_INVALID != gxi)
134 gx = pool_elt_at_index (gbp_ext_itf_pool, gxi);
136 GBP_EXT_ITF_DBG ("del: %U", format_gbp_ext_itf, gx);
138 gbp_itf_unlock (&gx->gx_itf);
139 gbp_route_domain_unlock (gx->gx_rd);
140 gbp_bridge_domain_unlock (gx->gx_bd);
142 gbp_ext_itf_db[sw_if_index] = INDEX_INVALID;
143 pool_put (gbp_ext_itf_pool, gx);
147 return (VNET_API_ERROR_NO_SUCH_ENTRY);
150 static clib_error_t *
151 gbp_ext_itf_add_del_cli (vlib_main_t * vm,
152 unformat_input_t * input, vlib_cli_command_t * cmd)
154 unformat_input_t _line_input, *line_input = &_line_input;
155 u32 sw_if_index = ~0, bd_id = ~0, rd_id = ~0, flags = 0;
159 /* Get a line of input. */
160 if (!unformat_user (input, unformat_line_input, line_input))
163 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
165 if (unformat (line_input, "del"))
169 (line_input, "%U", unformat_vnet_sw_interface, vnet_get_main (),
172 else if (unformat (line_input, "bd %d", &bd_id))
174 else if (unformat (line_input, "rd %d", &rd_id))
176 else if (unformat (line_input, "anon-l3-out"))
177 flags |= GBP_EXT_ITF_F_ANON;
179 return clib_error_return (0, "unknown input `%U'",
180 format_unformat_error, line_input);
182 unformat_free (line_input);
184 if (~0 == sw_if_index)
185 return clib_error_return (0, "interface must be specified");
190 return clib_error_return (0, "BD-ID must be specified");
192 return clib_error_return (0, "RD-ID must be specified");
193 rv = gbp_ext_itf_add (sw_if_index, bd_id, rd_id, flags);
196 rv = gbp_ext_itf_delete (sw_if_index);
202 case VNET_API_ERROR_ENTRY_ALREADY_EXISTS:
203 return clib_error_return (0, "interface already exists");
204 case VNET_API_ERROR_NO_SUCH_ENTRY: /* fallthrough */
205 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
206 return clib_error_return (0, "unknown interface");
208 return clib_error_return (0, "error %d", rv);
216 * Add Group Based Interface as anonymous L3out interface
219 * @cliexstart{gbp interface [del] anon-l3out <interface> bd <ID>}
223 VLIB_CLI_COMMAND (gbp_itf_anon_l3out_add_del_node, static) = {
224 .path = "gbp ext-itf",
225 .short_help = "gbp ext-itf [del] <interface> bd <ID> rd <ID> [anon-l3-out]\n",
226 .function = gbp_ext_itf_add_del_cli,
231 gbp_ext_itf_walk (gbp_ext_itf_cb_t cb, void *ctx)
236 pool_foreach(ge, gbp_ext_itf_pool,
245 gbp_ext_itf_show_one (gbp_ext_itf_t * gx, void *ctx)
247 vlib_cli_output (ctx, " %U", format_gbp_ext_itf, gx);
249 return (WALK_CONTINUE);
252 static clib_error_t *
253 gbp_ext_itf_show (vlib_main_t * vm,
254 unformat_input_t * input, vlib_cli_command_t * cmd)
256 vlib_cli_output (vm, "External-Interfaces:");
257 gbp_ext_itf_walk (gbp_ext_itf_show_one, vm);
263 * Show Group Based Policy external interface and derived information
266 * @cliexstart{show gbp ext-itf}
270 VLIB_CLI_COMMAND (gbp_ext_itf_show_node, static) = {
271 .path = "show gbp ext-itf",
272 .short_help = "show gbp ext-itf\n",
273 .function = gbp_ext_itf_show,
277 static clib_error_t *
278 gbp_ext_itf_init (vlib_main_t * vm)
280 gx_logger = vlib_log_register_class ("gbp", "ext-itf");
285 VLIB_INIT_FUNCTION (gbp_ext_itf_init);
288 * fd.io coding-style-patch-verification: ON
291 * eval: (c-set-style "gnu")