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.h>
17 #include <plugins/gbp/gbp_fwd_dpo.h>
18 #include <plugins/gbp/gbp_policy_dpo.h>
19 #include <plugins/gbp/gbp_route_domain.h>
21 #include <vnet/fib/fib_table.h>
22 #include <vnet/dpo/load_balance.h>
27 typedef struct gbp_subnet_key_t_
36 typedef struct gbp_subnet_t_
38 gbp_subnet_key_t *gs_key;
39 gbp_subnet_type_t gs_type;
48 } gs_stitched_external;
53 * A DB of the subnets; key={pfx,fib-index}
60 gbp_subnet_t *gbp_subnet_pool;
63 gbp_subnet_db_find (u32 fib_index, const fib_prefix_t * pfx)
65 gbp_subnet_key_t key = {
67 .gsk_fib_index = fib_index,
71 p = hash_get_mem (gbp_subnet_db, &key);
76 return (INDEX_INVALID);
80 gbp_subnet_db_add (u32 fib_index, const fib_prefix_t * pfx, gbp_subnet_t * gs)
82 gbp_subnet_key_t *key;
84 key = clib_mem_alloc (sizeof (*key));
86 clib_memcpy (&(key->gsk_pfx), pfx, sizeof (*pfx));
87 key->gsk_fib_index = fib_index;
89 hash_set_mem (gbp_subnet_db, key, (gs - gbp_subnet_pool));
95 gbp_subnet_db_del (gbp_subnet_t * gs)
97 hash_unset_mem (gbp_subnet_db, gs->gs_key);
99 clib_mem_free (gs->gs_key);
105 gbp_subnet_transport_add (const gbp_subnet_t * gs)
107 dpo_id_t gfd = DPO_INVALID;
108 gbp_route_domain_t *grd;
109 fib_protocol_t fproto;
111 fproto = gs->gs_key->gsk_pfx.fp_proto;
112 grd = gbp_route_domain_get (gs->gs_rd);
114 fib_table_entry_update_one_path (gs->gs_key->gsk_fib_index,
115 &gs->gs_key->gsk_pfx,
116 FIB_SOURCE_PLUGIN_HI,
118 fib_proto_to_dpo (fproto),
120 grd->grd_uu_sw_if_index[fproto],
121 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
129 gbp_subnet_internal_add (const gbp_subnet_t * gs)
131 dpo_id_t gfd = DPO_INVALID;
133 gbp_fwd_dpo_add_or_lock (fib_proto_to_dpo (gs->gs_key->gsk_pfx.fp_proto),
136 fib_table_entry_special_dpo_update (gs->gs_key->gsk_fib_index,
137 &gs->gs_key->gsk_pfx,
138 FIB_SOURCE_PLUGIN_HI,
139 FIB_ENTRY_FLAG_EXCLUSIVE, &gfd);
147 gbp_subnet_external_add (gbp_subnet_t * gs, u32 sw_if_index, epg_id_t epg)
149 dpo_id_t gpd = DPO_INVALID;
151 gs->gs_stitched_external.gs_epg = epg;
152 gs->gs_stitched_external.gs_sw_if_index = sw_if_index;
154 gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (gs->gs_key->gsk_pfx.fp_proto),
155 gs->gs_stitched_external.gs_epg,
156 gs->gs_stitched_external.gs_sw_if_index, &gpd);
158 fib_table_entry_special_dpo_update (gs->gs_key->gsk_fib_index,
159 &gs->gs_key->gsk_pfx,
160 FIB_SOURCE_PLUGIN_HI,
161 (FIB_ENTRY_FLAG_EXCLUSIVE |
162 FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT),
171 gbp_subnet_del (u32 rd_id, const fib_prefix_t * pfx)
173 gbp_route_domain_t *grd;
178 grdi = gbp_route_domain_find (rd_id);
181 return (VNET_API_ERROR_NO_SUCH_FIB);
183 grd = gbp_route_domain_get (grdi);
184 fib_index = grd->grd_fib_index[pfx->fp_proto];
186 gsi = gbp_subnet_db_find (fib_index, pfx);
188 if (INDEX_INVALID == gsi)
189 return (VNET_API_ERROR_NO_SUCH_ENTRY);
191 gs = pool_elt_at_index (gbp_subnet_pool, gsi);
193 fib_table_entry_delete (fib_index, pfx, FIB_SOURCE_PLUGIN_HI);
195 gbp_subnet_db_del (gs);
196 gbp_route_domain_unlock (gs->gs_rd);
198 pool_put (gbp_subnet_pool, gs);
204 gbp_subnet_add (u32 rd_id,
205 const fib_prefix_t * pfx,
206 gbp_subnet_type_t type, u32 sw_if_index, epg_id_t epg)
208 gbp_route_domain_t *grd;
214 grdi = gbp_route_domain_find_and_lock (rd_id);
217 return (VNET_API_ERROR_NO_SUCH_FIB);
219 grd = gbp_route_domain_get (grdi);
220 fib_index = grd->grd_fib_index[pfx->fp_proto];
222 gsi = gbp_subnet_db_find (fib_index, pfx);
224 if (INDEX_INVALID != gsi)
225 return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
229 pool_get (gbp_subnet_pool, gs);
233 gbp_subnet_db_add (fib_index, pfx, gs);
237 case GBP_SUBNET_STITCHED_INTERNAL:
238 rv = gbp_subnet_internal_add (gs);
240 case GBP_SUBNET_STITCHED_EXTERNAL:
241 rv = gbp_subnet_external_add (gs, sw_if_index, epg);
243 case GBP_SUBNET_TRANSPORT:
244 rv = gbp_subnet_transport_add (gs);
252 gbp_subnet_walk (gbp_subnet_cb_t cb, void *ctx)
254 gbp_route_domain_t *grd;
263 pool_foreach (gs, gbp_subnet_pool,
265 grd = gbp_route_domain_get(gs->gs_rd);
269 case GBP_SUBNET_STITCHED_INTERNAL:
270 case GBP_SUBNET_TRANSPORT:
271 /* use defaults above */
273 case GBP_SUBNET_STITCHED_EXTERNAL:
274 sw_if_index = gs->gs_stitched_external.gs_sw_if_index;
275 epg = gs->gs_stitched_external.gs_epg;
279 if (WALK_STOP == cb (grd->grd_id, &gs->gs_key->gsk_pfx,
280 gs->gs_type, epg, sw_if_index, ctx))
286 typedef enum gsb_subnet_show_flags_t_
288 GBP_SUBNET_SHOW_BRIEF,
289 GBP_SUBNET_SHOW_DETAILS,
290 } gsb_subnet_show_flags_t;
293 format_gbp_subnet_type (u8 * s, va_list * args)
295 gbp_subnet_type_t type = va_arg (*args, gbp_subnet_type_t);
299 case GBP_SUBNET_STITCHED_INTERNAL:
300 return (format (s, "stitched-internal"));
301 case GBP_SUBNET_STITCHED_EXTERNAL:
302 return (format (s, "stitched-external"));
303 case GBP_SUBNET_TRANSPORT:
304 return (format (s, "transport"));
307 return (format (s, "unknown"));
311 format_gbp_subnet (u8 * s, va_list * args)
313 index_t gsi = va_arg (*args, index_t);
314 gsb_subnet_show_flags_t flags = va_arg (*args, gsb_subnet_show_flags_t);
318 gs = pool_elt_at_index (gbp_subnet_pool, gsi);
320 table_id = fib_table_get_table_id (gs->gs_key->gsk_fib_index,
321 gs->gs_key->gsk_pfx.fp_proto);
323 s = format (s, "[%d] tbl:%d %U %U", gsi, table_id,
324 format_fib_prefix, &gs->gs_key->gsk_pfx,
325 format_gbp_subnet_type, gs->gs_type);
329 case GBP_SUBNET_STITCHED_INTERNAL:
330 case GBP_SUBNET_TRANSPORT:
332 case GBP_SUBNET_STITCHED_EXTERNAL:
333 s = format (s, " {epg:%d %U}", gs->gs_stitched_external.gs_epg,
334 format_vnet_sw_if_index_name,
335 vnet_get_main (), gs->gs_stitched_external.gs_sw_if_index);
341 case GBP_SUBNET_SHOW_DETAILS:
343 fib_node_index_t fei;
345 fei = fib_table_lookup_exact_match (gs->gs_key->gsk_fib_index,
346 &gs->gs_key->gsk_pfx);
349 format (s, "\n %U", format_fib_entry, fei,
350 FIB_ENTRY_FORMAT_DETAIL);
352 case GBP_SUBNET_SHOW_BRIEF:
358 static clib_error_t *
359 gbp_subnet_show (vlib_main_t * vm,
360 unformat_input_t * input, vlib_cli_command_t * cmd)
366 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
368 if (unformat (input, "%d", &gsi))
374 if (INDEX_INVALID != gsi)
376 vlib_cli_output (vm, "%U", format_gbp_subnet, gsi,
377 GBP_SUBNET_SHOW_DETAILS);
382 pool_foreach_index(gsi, gbp_subnet_pool,
384 vlib_cli_output (vm, "%U", format_gbp_subnet, gsi,
385 GBP_SUBNET_SHOW_BRIEF);
394 * Show Group Based Policy Subnets
397 * @cliexstart{show gbp subnet}
401 VLIB_CLI_COMMAND (gbp_subnet_show_node, static) = {
402 .path = "show gbp subnet",
403 .short_help = "show gbp subnet\n",
404 .function = gbp_subnet_show,
408 static clib_error_t *
409 gbp_subnet_init (vlib_main_t * vm)
411 gbp_subnet_db = hash_create_mem (0,
412 sizeof (gbp_subnet_key_t), sizeof (u32));
417 VLIB_INIT_FUNCTION (gbp_subnet_init);
420 * fd.io coding-style-patch-verification: ON
423 * eval: (c-set-style "gnu")