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>
22 #include <plugins/gbp/gbp_itf.h>
24 #include <vnet/dpo/dvr_dpo.h>
25 #include <vnet/fib/fib_table.h>
26 #include <vnet/l2/l2_input.h>
29 * Pool of GBP endpoint_groups
31 gbp_endpoint_group_t *gbp_endpoint_group_pool;
34 * DB of endpoint_groups
36 gbp_endpoint_group_db_t gbp_endpoint_group_db;
41 uword *gbp_epg_sclass_db;
43 vlib_log_class_t gg_logger;
45 #define GBP_EPG_DBG(...) \
46 vlib_log_debug (gg_logger, __VA_ARGS__);
48 gbp_endpoint_group_t *
49 gbp_endpoint_group_get (index_t i)
51 return (pool_elt_at_index (gbp_endpoint_group_pool, i));
55 gbp_endpoint_group_lock (index_t i)
57 gbp_endpoint_group_t *gg;
59 gg = gbp_endpoint_group_get (i);
64 gbp_endpoint_group_find (sclass_t sclass)
68 p = hash_get (gbp_endpoint_group_db.gg_hash_sclass, sclass);
73 return (INDEX_INVALID);
77 gbp_endpoint_group_add_and_lock (vnid_t vnid,
81 u32 uplink_sw_if_index,
82 const gbp_endpoint_retention_t * retention)
84 gbp_endpoint_group_t *gg;
87 ggi = gbp_endpoint_group_find (sclass);
89 if (INDEX_INVALID == ggi)
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 pool_get_zero (gbp_endpoint_group_pool, gg);
113 gg->gg_uplink_sw_if_index = uplink_sw_if_index;
114 gbp_itf_hdl_reset (&gg->gg_uplink_itf);
116 gg->gg_sclass = sclass;
117 gg->gg_retention = *retention;
119 if (SCLASS_INVALID != gg->gg_sclass)
120 hash_set (gbp_epg_sclass_db, gg->gg_sclass, gg->gg_vnid);
123 * an egress DVR dpo for internal subnets to use when sending
124 * on the uplink interface
126 if (~0 != gg->gg_uplink_sw_if_index)
128 FOR_EACH_FIB_IP_PROTOCOL (fproto)
130 dvr_dpo_add_or_lock (uplink_sw_if_index,
131 fib_proto_to_dpo (fproto),
132 &gg->gg_dpo[fproto]);
136 * Add the uplink to the BD
137 * packets direct from the uplink have had policy applied
140 gbp_itf_l2_add_and_lock (gg->gg_uplink_sw_if_index, gbi);
142 gbp_itf_l2_set_input_feature (gg->gg_uplink_itf,
143 L2INPUT_FEAT_GBP_NULL_CLASSIFY);
146 hash_set (gbp_endpoint_group_db.gg_hash_sclass,
147 gg->gg_sclass, gg - gbp_endpoint_group_pool);
151 gg = gbp_endpoint_group_get (ggi);
155 GBP_EPG_DBG ("add: %U", format_gbp_endpoint_group, gg);
161 gbp_endpoint_group_unlock (index_t ggi)
163 gbp_endpoint_group_t *gg;
165 if (INDEX_INVALID == ggi)
168 gg = gbp_endpoint_group_get (ggi);
172 if (0 == gg->gg_locks)
174 fib_protocol_t fproto;
176 gg = pool_elt_at_index (gbp_endpoint_group_pool, ggi);
178 gbp_itf_unlock (&gg->gg_uplink_itf);
180 FOR_EACH_FIB_IP_PROTOCOL (fproto)
182 dpo_reset (&gg->gg_dpo[fproto]);
184 gbp_bridge_domain_unlock (gg->gg_gbd);
185 gbp_route_domain_unlock (gg->gg_rd);
187 if (SCLASS_INVALID != gg->gg_sclass)
188 hash_unset (gbp_epg_sclass_db, gg->gg_sclass);
189 hash_unset (gbp_endpoint_group_db.gg_hash_sclass, gg->gg_sclass);
191 pool_put (gbp_endpoint_group_pool, gg);
196 gbp_endpoint_group_delete (sclass_t sclass)
200 ggi = gbp_endpoint_group_find (sclass);
202 if (INDEX_INVALID != ggi)
204 GBP_EPG_DBG ("del: %U", format_gbp_endpoint_group,
205 gbp_endpoint_group_get (ggi));
206 gbp_endpoint_group_unlock (ggi);
211 return (VNET_API_ERROR_NO_SUCH_ENTRY);
215 gbp_endpoint_group_get_bd_id (const gbp_endpoint_group_t * gg)
217 const gbp_bridge_domain_t *gb;
219 gb = gbp_bridge_domain_get (gg->gg_gbd);
221 return (gb->gb_bd_id);
225 gbp_endpoint_group_get_fib_index (const gbp_endpoint_group_t * gg,
226 fib_protocol_t fproto)
228 const gbp_route_domain_t *grd;
230 grd = gbp_route_domain_get (gg->gg_rd);
232 return (grd->grd_fib_index[fproto]);
236 gbp_endpoint_group_walk (gbp_endpoint_group_cb_t cb, void *ctx)
238 gbp_endpoint_group_t *gbpe;
241 pool_foreach(gbpe, gbp_endpoint_group_pool,
249 static clib_error_t *
250 gbp_endpoint_group_cli (vlib_main_t * vm,
251 unformat_input_t * input, vlib_cli_command_t * cmd)
253 gbp_endpoint_retention_t retention = { 0 };
254 vnid_t vnid = VNID_INVALID, sclass;
255 vnet_main_t *vnm = vnet_get_main ();
256 u32 uplink_sw_if_index = ~0;
261 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
263 if (unformat (input, "%U", unformat_vnet_sw_interface,
264 vnm, &uplink_sw_if_index))
266 else if (unformat (input, "add"))
268 else if (unformat (input, "del"))
270 else if (unformat (input, "epg %d", &vnid))
272 else if (unformat (input, "sclass %d", &sclass))
274 else if (unformat (input, "bd %d", &bd_id))
276 else if (unformat (input, "rd %d", &rd_id))
282 if (VNID_INVALID == vnid)
283 return clib_error_return (0, "EPG-ID must be specified");
288 return clib_error_return (0, "Bridge-domain must be specified");
290 return clib_error_return (0, "route-domain must be specified");
292 gbp_endpoint_group_add_and_lock (vnid, sclass, bd_id, rd_id,
293 uplink_sw_if_index, &retention);
296 gbp_endpoint_group_delete (vnid);
302 * Configure a GBP Endpoint Group
305 * @cliexstart{gbp endpoint-group [del] epg <ID> bd <ID> rd <ID> [sclass <ID>] [<interface>]}
309 VLIB_CLI_COMMAND (gbp_endpoint_group_cli_node, static) = {
310 .path = "gbp endpoint-group",
311 .short_help = "gbp endpoint-group [del] epg <ID> bd <ID> rd <ID> [sclass <ID>] [<interface>]",
312 .function = gbp_endpoint_group_cli,
316 format_gbp_endpoint_retention (u8 * s, va_list * args)
318 gbp_endpoint_retention_t *rt = va_arg (*args, gbp_endpoint_retention_t*);
320 s = format (s, "[remote-EP-timeout:%d]", rt->remote_ep_timeout);
326 format_gbp_endpoint_group (u8 * s, va_list * args)
328 gbp_endpoint_group_t *gg = va_arg (*args, gbp_endpoint_group_t*);
331 s = format (s, "[%d] %d, sclass:%d bd:%d rd:%d uplink:%U retention:%U locks:%d",
332 gg - gbp_endpoint_group_pool,
337 format_gbp_itf_hdl, gg->gg_uplink_itf,
338 format_gbp_endpoint_retention, &gg->gg_retention,
341 s = format (s, "NULL");
347 gbp_endpoint_group_show_one (gbp_endpoint_group_t *gg, void *ctx)
352 vlib_cli_output (vm, " %U",format_gbp_endpoint_group, gg);
357 static clib_error_t *
358 gbp_endpoint_group_show (vlib_main_t * vm,
359 unformat_input_t * input, vlib_cli_command_t * cmd)
361 vlib_cli_output (vm, "Endpoint-Groups:");
362 gbp_endpoint_group_walk (gbp_endpoint_group_show_one, vm);
369 * Show Group Based Policy Endpoint_Groups and derived information
372 * @cliexstart{show gbp endpoint_group}
376 VLIB_CLI_COMMAND (gbp_endpoint_group_show_node, static) = {
377 .path = "show gbp endpoint-group",
378 .short_help = "show gbp endpoint-group\n",
379 .function = gbp_endpoint_group_show,
383 static clib_error_t *
384 gbp_endpoint_group_init (vlib_main_t * vm)
386 gg_logger = vlib_log_register_class ("gbp", "epg");
391 VLIB_INIT_FUNCTION (gbp_endpoint_group_init);
394 * fd.io coding-style-patch-verification: ON
397 * eval: (c-set-style "gnu")