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 in %U",
47 format_gbp_itf, gx->gx_itf,
48 format_gbp_bridge_domain, gx->gx_bd));
52 gbp_ext_itf_add (u32 sw_if_index, u32 bd_id, u32 rd_id)
57 vec_validate_init_empty (gbp_ext_itf_db, sw_if_index, INDEX_INVALID);
59 gxi = gbp_ext_itf_db[sw_if_index];
61 if (INDEX_INVALID == gxi)
63 gbp_route_domain_t *gr;
64 fib_protocol_t fproto;
67 gbi = gbp_bridge_domain_find_and_lock (bd_id);
69 if (INDEX_INVALID == gbi)
70 return (VNET_API_ERROR_NO_SUCH_ENTRY);
72 gri = gbp_route_domain_find_and_lock (rd_id);
74 if (INDEX_INVALID == gri)
76 gbp_bridge_domain_unlock (gbi);
77 return (VNET_API_ERROR_NO_SUCH_ENTRY);
80 pool_get_zero (gbp_ext_itf_pool, gx);
81 gxi = gx - gbp_ext_itf_pool;
83 gr = gbp_route_domain_get (gri);
87 gx->gx_itf = sw_if_index;
89 FOR_EACH_FIB_IP_PROTOCOL (fproto)
91 gx->gx_fib_index[fproto] =
92 gr->grd_fib_index[fib_proto_to_dpo (fproto)];
95 gbp_ext_itf_db[sw_if_index] = gxi;
97 GBP_EXT_ITF_DBG ("add: %U", format_gbp_ext_itf, gx);
102 return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
106 gbp_ext_itf_delete (u32 sw_if_index)
111 if (vec_len (gbp_ext_itf_db) <= sw_if_index)
112 return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
114 gxi = gbp_ext_itf_db[sw_if_index];
116 if (INDEX_INVALID != gxi)
118 gx = pool_elt_at_index (gbp_ext_itf_pool, gxi);
120 GBP_EXT_ITF_DBG ("del: %U", format_gbp_ext_itf, gx);
122 gbp_route_domain_unlock (gx->gx_rd);
123 gbp_bridge_domain_unlock (gx->gx_bd);
125 gbp_ext_itf_db[sw_if_index] = INDEX_INVALID;
126 pool_put (gbp_ext_itf_pool, gx);
130 return (VNET_API_ERROR_NO_SUCH_ENTRY);
134 gbp_ext_itf_walk (gbp_ext_itf_cb_t cb, void *ctx)
139 pool_foreach(ge, gbp_ext_itf_pool,
148 gbp_ext_itf_show_one (gbp_ext_itf_t * gx, void *ctx)
150 vlib_cli_output (ctx, " %U", format_gbp_ext_itf, gx);
152 return (WALK_CONTINUE);
155 static clib_error_t *
156 gbp_ext_itf_show (vlib_main_t * vm,
157 unformat_input_t * input, vlib_cli_command_t * cmd)
159 vlib_cli_output (vm, "External-Interfaces:");
160 gbp_ext_itf_walk (gbp_ext_itf_show_one, vm);
166 * Show Group Based Policy external interface and derived information
169 * @cliexstart{show gbp ext-itf}
173 VLIB_CLI_COMMAND (gbp_ext_itf_show_node, static) = {
174 .path = "show gbp ext-itf",
175 .short_help = "show gbp ext-itf\n",
176 .function = gbp_ext_itf_show,
180 static clib_error_t *
181 gbp_ext_itf_init (vlib_main_t * vm)
183 gx_logger = vlib_log_register_class ("gbp", "ext-itf");
188 VLIB_INIT_FUNCTION (gbp_ext_itf_init);
191 * fd.io coding-style-patch-verification: ON
194 * eval: (c-set-style "gnu")