2 * gbp.h : Group Based Policy
4 * Copyright (c) 2018 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <plugins/gbp/gbp_endpoint_group.h>
19 #include <plugins/gbp/gbp_endpoint.h>
20 #include <plugins/gbp/gbp_bridge_domain.h>
21 #include <plugins/gbp/gbp_route_domain.h>
23 #include <vnet/dpo/dvr_dpo.h>
24 #include <vnet/fib/fib_table.h>
25 #include <vnet/l2/l2_input.h>
28 * Pool of GBP endpoint_groups
30 gbp_endpoint_group_t *gbp_endpoint_group_pool;
33 * DB of endpoint_groups
35 gbp_endpoint_group_db_t gbp_endpoint_group_db;
40 uword *gbp_epg_sclass_db;
42 vlib_log_class_t gg_logger;
44 #define GBP_EPG_DBG(...) \
45 vlib_log_debug (gg_logger, __VA_ARGS__);
47 gbp_endpoint_group_t *
48 gbp_endpoint_group_get (index_t i)
50 return (pool_elt_at_index (gbp_endpoint_group_pool, i));
54 gbp_endpoint_group_lock (index_t i)
56 gbp_endpoint_group_t *gg;
58 gg = gbp_endpoint_group_get (i);
63 gbp_endpoint_group_find (sclass_t sclass)
67 p = hash_get (gbp_endpoint_group_db.gg_hash_sclass, sclass);
72 return (INDEX_INVALID);
76 gbp_endpoint_group_add_and_lock (vnid_t vnid,
80 u32 uplink_sw_if_index,
81 const gbp_endpoint_retention_t * retention)
83 gbp_endpoint_group_t *gg;
86 ggi = gbp_endpoint_group_find (sclass);
88 if (INDEX_INVALID == ggi)
90 gbp_bridge_domain_t *gb;
91 fib_protocol_t fproto;
94 gbi = gbp_bridge_domain_find_and_lock (bd_id);
97 return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
99 grdi = gbp_route_domain_find_and_lock (rd_id);
103 gbp_bridge_domain_unlock (gbi);
104 return (VNET_API_ERROR_NO_SUCH_FIB);
107 gb = gbp_bridge_domain_get (gbi);
109 pool_get_zero (gbp_endpoint_group_pool, gg);
114 gg->gg_bd_index = gb->gb_bd_index;
116 gg->gg_uplink_sw_if_index = uplink_sw_if_index;
118 gg->gg_sclass = sclass;
119 gg->gg_retention = *retention;
121 if (SCLASS_INVALID != gg->gg_sclass)
122 hash_set (gbp_epg_sclass_db, gg->gg_sclass, gg->gg_vnid);
125 * an egress DVR dpo for internal subnets to use when sending
126 * on the uplink interface
128 if (~0 != gg->gg_uplink_sw_if_index)
130 FOR_EACH_FIB_IP_PROTOCOL (fproto)
132 dvr_dpo_add_or_lock (uplink_sw_if_index,
133 fib_proto_to_dpo (fproto),
134 &gg->gg_dpo[fproto]);
138 * Add the uplink to the BD
139 * packets direct from the uplink have had policy applied
141 gbp_bridge_domain_itf_add (gg->gg_uplink_sw_if_index,
142 gg->gg_bd_index, L2_BD_PORT_TYPE_NORMAL);
143 l2input_intf_bitmap_enable (gg->gg_uplink_sw_if_index,
144 L2INPUT_FEAT_GBP_NULL_CLASSIFY, 1);
147 hash_set (gbp_endpoint_group_db.gg_hash_sclass,
148 gg->gg_sclass, gg - gbp_endpoint_group_pool);
152 gg = gbp_endpoint_group_get (ggi);
156 GBP_EPG_DBG ("add: %U", format_gbp_endpoint_group, gg);
162 gbp_endpoint_group_unlock (index_t ggi)
164 gbp_endpoint_group_t *gg;
166 if (INDEX_INVALID == ggi)
169 gg = gbp_endpoint_group_get (ggi);
173 if (0 == gg->gg_locks)
175 fib_protocol_t fproto;
177 gg = pool_elt_at_index (gbp_endpoint_group_pool, ggi);
179 if (~0 != gg->gg_uplink_sw_if_index)
181 gbp_bridge_domain_itf_del (gg->gg_uplink_sw_if_index,
182 gg->gg_bd_index, L2_BD_PORT_TYPE_NORMAL);
183 l2input_intf_bitmap_enable (gg->gg_uplink_sw_if_index,
184 L2INPUT_FEAT_GBP_NULL_CLASSIFY, 0);
186 FOR_EACH_FIB_IP_PROTOCOL (fproto)
188 dpo_reset (&gg->gg_dpo[fproto]);
190 gbp_bridge_domain_unlock (gg->gg_gbd);
191 gbp_route_domain_unlock (gg->gg_rd);
193 if (SCLASS_INVALID != gg->gg_sclass)
194 hash_unset (gbp_epg_sclass_db, gg->gg_sclass);
195 hash_unset (gbp_endpoint_group_db.gg_hash_sclass, gg->gg_sclass);
197 pool_put (gbp_endpoint_group_pool, gg);
202 gbp_endpoint_group_delete (sclass_t sclass)
206 ggi = gbp_endpoint_group_find (sclass);
208 if (INDEX_INVALID != ggi)
210 GBP_EPG_DBG ("del: %U", format_gbp_endpoint_group,
211 gbp_endpoint_group_get (ggi));
212 gbp_endpoint_group_unlock (ggi);
217 return (VNET_API_ERROR_NO_SUCH_ENTRY);
221 gbp_endpoint_group_get_bd_id (const gbp_endpoint_group_t * gg)
223 const gbp_bridge_domain_t *gb;
225 gb = gbp_bridge_domain_get (gg->gg_gbd);
227 return (gb->gb_bd_id);
231 gbp_endpoint_group_get_fib_index (const gbp_endpoint_group_t * gg,
232 fib_protocol_t fproto)
234 const gbp_route_domain_t *grd;
236 grd = gbp_route_domain_get (gg->gg_rd);
238 return (grd->grd_fib_index[fproto]);
242 gbp_endpoint_group_walk (gbp_endpoint_group_cb_t cb, void *ctx)
244 gbp_endpoint_group_t *gbpe;
247 pool_foreach(gbpe, gbp_endpoint_group_pool,
255 static clib_error_t *
256 gbp_endpoint_group_cli (vlib_main_t * vm,
257 unformat_input_t * input, vlib_cli_command_t * cmd)
259 gbp_endpoint_retention_t retention = { 0 };
260 vnid_t vnid = VNID_INVALID, sclass;
261 vnet_main_t *vnm = vnet_get_main ();
262 u32 uplink_sw_if_index = ~0;
267 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
269 if (unformat (input, "%U", unformat_vnet_sw_interface,
270 vnm, &uplink_sw_if_index))
272 else if (unformat (input, "add"))
274 else if (unformat (input, "del"))
276 else if (unformat (input, "epg %d", &vnid))
278 else if (unformat (input, "sclass %d", &sclass))
280 else if (unformat (input, "bd %d", &bd_id))
282 else if (unformat (input, "rd %d", &rd_id))
288 if (VNID_INVALID == vnid)
289 return clib_error_return (0, "EPG-ID must be specified");
294 return clib_error_return (0, "Bridge-domain must be specified");
296 return clib_error_return (0, "route-domain must be specified");
298 gbp_endpoint_group_add_and_lock (vnid, sclass, bd_id, rd_id,
299 uplink_sw_if_index, &retention);
302 gbp_endpoint_group_delete (vnid);
308 * Configure a GBP Endpoint Group
311 * @cliexstart{gbp endpoint-group [del] epg <ID> bd <ID> rd <ID> [sclass <ID>] [<interface>]}
315 VLIB_CLI_COMMAND (gbp_endpoint_group_cli_node, static) = {
316 .path = "gbp endpoint-group",
317 .short_help = "gbp endpoint-group [del] epg <ID> bd <ID> rd <ID> [sclass <ID>] [<interface>]",
318 .function = gbp_endpoint_group_cli,
322 format_gbp_endpoint_retention (u8 * s, va_list * args)
324 gbp_endpoint_retention_t *rt = va_arg (*args, gbp_endpoint_retention_t*);
326 s = format (s, "[remote-EP-timeout:%d]", rt->remote_ep_timeout);
332 format_gbp_endpoint_group (u8 * s, va_list * args)
334 gbp_endpoint_group_t *gg = va_arg (*args, gbp_endpoint_group_t*);
335 vnet_main_t *vnm = vnet_get_main ();
338 s = format (s, "[%d] %d, sclass:%d bd:[%d,%d] rd:[%d] uplink:%U retention:%U locks:%d",
339 gg - gbp_endpoint_group_pool,
342 gbp_endpoint_group_get_bd_id(gg), gg->gg_bd_index,
344 format_vnet_sw_if_index_name, vnm, gg->gg_uplink_sw_if_index,
345 format_gbp_endpoint_retention, &gg->gg_retention,
348 s = format (s, "NULL");
354 gbp_endpoint_group_show_one (gbp_endpoint_group_t *gg, void *ctx)
359 vlib_cli_output (vm, " %U",format_gbp_endpoint_group, gg);
364 static clib_error_t *
365 gbp_endpoint_group_show (vlib_main_t * vm,
366 unformat_input_t * input, vlib_cli_command_t * cmd)
368 vlib_cli_output (vm, "Endpoint-Groups:");
369 gbp_endpoint_group_walk (gbp_endpoint_group_show_one, vm);
376 * Show Group Based Policy Endpoint_Groups and derived information
379 * @cliexstart{show gbp endpoint_group}
383 VLIB_CLI_COMMAND (gbp_endpoint_group_show_node, static) = {
384 .path = "show gbp endpoint-group",
385 .short_help = "show gbp endpoint-group\n",
386 .function = gbp_endpoint_group_show,
390 static clib_error_t *
391 gbp_endpoint_group_init (vlib_main_t * vm)
393 gg_logger = vlib_log_register_class ("gbp", "epg");
398 VLIB_INIT_FUNCTION (gbp_endpoint_group_init);
401 * fd.io coding-style-patch-verification: ON
404 * eval: (c-set-style "gnu")