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;
55 fib_node_index_t gs_fei;
59 * A DB of the subnets; key={pfx,fib-index}
66 gbp_subnet_t *gbp_subnet_pool;
69 gbp_subnet_db_find (u32 fib_index, const fib_prefix_t * pfx)
71 gbp_subnet_key_t key = {
73 .gsk_fib_index = fib_index,
77 p = hash_get_mem (gbp_subnet_db, &key);
82 return (INDEX_INVALID);
86 gbp_subnet_db_add (u32 fib_index, const fib_prefix_t * pfx, gbp_subnet_t * gs)
88 gbp_subnet_key_t *key;
90 key = clib_mem_alloc (sizeof (*key));
92 clib_memcpy (&(key->gsk_pfx), pfx, sizeof (*pfx));
93 key->gsk_fib_index = fib_index;
95 hash_set_mem (gbp_subnet_db, key, (gs - gbp_subnet_pool));
101 gbp_subnet_db_del (gbp_subnet_t * gs)
103 hash_unset_mem (gbp_subnet_db, gs->gs_key);
105 clib_mem_free (gs->gs_key);
111 gbp_subnet_transport_add (gbp_subnet_t * gs)
113 dpo_id_t gfd = DPO_INVALID;
114 gbp_route_domain_t *grd;
115 fib_protocol_t fproto;
117 fproto = gs->gs_key->gsk_pfx.fp_proto;
118 grd = gbp_route_domain_get (gs->gs_rd);
120 if (~0 == grd->grd_uu_sw_if_index[fproto])
121 return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
123 gs->gs_fei = fib_table_entry_update_one_path (gs->gs_key->gsk_fib_index,
124 &gs->gs_key->gsk_pfx,
125 FIB_SOURCE_PLUGIN_HI,
127 fib_proto_to_dpo (fproto),
129 grd->grd_uu_sw_if_index
130 [fproto], ~0, 1, NULL,
131 FIB_ROUTE_PATH_FLAG_NONE);
139 gbp_subnet_internal_add (gbp_subnet_t * gs)
141 dpo_id_t gfd = DPO_INVALID;
143 gbp_fwd_dpo_add_or_lock (fib_proto_to_dpo (gs->gs_key->gsk_pfx.fp_proto),
146 gs->gs_fei = fib_table_entry_special_dpo_update (gs->gs_key->gsk_fib_index,
147 &gs->gs_key->gsk_pfx,
148 FIB_SOURCE_PLUGIN_HI,
149 FIB_ENTRY_FLAG_EXCLUSIVE,
158 gbp_subnet_external_add (gbp_subnet_t * gs, u32 sw_if_index, sclass_t sclass)
160 dpo_id_t gpd = DPO_INVALID;
162 gs->gs_stitched_external.gs_sclass = sclass;
163 gs->gs_stitched_external.gs_sw_if_index = sw_if_index;
165 gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (gs->gs_key->gsk_pfx.fp_proto),
166 gs->gs_stitched_external.gs_sclass,
167 gs->gs_stitched_external.gs_sw_if_index, &gpd);
169 gs->gs_fei = fib_table_entry_special_dpo_update (gs->gs_key->gsk_fib_index,
170 &gs->gs_key->gsk_pfx,
171 FIB_SOURCE_PLUGIN_HI,
172 (FIB_ENTRY_FLAG_EXCLUSIVE |
173 FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT),
182 gbp_subnet_l3_out_add (gbp_subnet_t * gs, u32 sw_if_index, sclass_t sclass)
184 dpo_id_t gpd = DPO_INVALID;
186 gs->gs_l3_out.gs_sclass = sclass;
188 gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (gs->gs_key->gsk_pfx.fp_proto),
189 gs->gs_l3_out.gs_sclass, ~0, &gpd);
191 gs->gs_fei = fib_table_entry_special_dpo_add (gs->gs_key->gsk_fib_index,
192 &gs->gs_key->gsk_pfx,
194 FIB_ENTRY_FLAG_INTERPOSE,
203 gbp_subnet_del_i (index_t gsi)
207 gs = pool_elt_at_index (gbp_subnet_pool, gsi);
209 if (GBP_SUBNET_L3_OUT == gs->gs_type)
210 fib_table_entry_delete_index (gs->gs_fei, FIB_SOURCE_SPECIAL);
212 fib_table_entry_delete_index (gs->gs_fei, FIB_SOURCE_PLUGIN_HI);
214 gbp_subnet_db_del (gs);
215 gbp_route_domain_unlock (gs->gs_rd);
217 pool_put (gbp_subnet_pool, gs);
221 gbp_subnet_del (u32 rd_id, const fib_prefix_t * pfx)
223 gbp_route_domain_t *grd;
227 grdi = gbp_route_domain_find (rd_id);
230 return (VNET_API_ERROR_NO_SUCH_FIB);
232 grd = gbp_route_domain_get (grdi);
233 fib_index = grd->grd_fib_index[pfx->fp_proto];
235 gsi = gbp_subnet_db_find (fib_index, pfx);
237 if (INDEX_INVALID == gsi)
238 return (VNET_API_ERROR_NO_SUCH_ENTRY);
240 gbp_subnet_del_i (gsi);
246 gbp_subnet_add (u32 rd_id,
247 const fib_prefix_t * pfx,
248 gbp_subnet_type_t type, u32 sw_if_index, sclass_t sclass)
250 gbp_route_domain_t *grd;
256 grdi = gbp_route_domain_find_and_lock (rd_id);
259 return (VNET_API_ERROR_NO_SUCH_FIB);
261 grd = gbp_route_domain_get (grdi);
262 fib_index = grd->grd_fib_index[pfx->fp_proto];
264 gsi = gbp_subnet_db_find (fib_index, pfx);
267 * this is an update if the subnet already exists, so remove the old
269 if (INDEX_INVALID != gsi)
270 gbp_subnet_del_i (gsi);
274 pool_get (gbp_subnet_pool, gs);
278 gbp_subnet_db_add (fib_index, pfx, gs);
282 case GBP_SUBNET_STITCHED_INTERNAL:
283 rv = gbp_subnet_internal_add (gs);
285 case GBP_SUBNET_STITCHED_EXTERNAL:
286 rv = gbp_subnet_external_add (gs, sw_if_index, sclass);
288 case GBP_SUBNET_TRANSPORT:
289 rv = gbp_subnet_transport_add (gs);
291 case GBP_SUBNET_L3_OUT:
292 rv = gbp_subnet_l3_out_add (gs, sw_if_index, sclass);
300 gbp_subnet_walk (gbp_subnet_cb_t cb, void *ctx)
302 gbp_route_domain_t *grd;
307 sclass = SCLASS_INVALID;
311 pool_foreach (gs, gbp_subnet_pool,
313 grd = gbp_route_domain_get(gs->gs_rd);
317 case GBP_SUBNET_STITCHED_INTERNAL:
318 case GBP_SUBNET_TRANSPORT:
319 /* use defaults above */
321 case GBP_SUBNET_STITCHED_EXTERNAL:
322 sw_if_index = gs->gs_stitched_external.gs_sw_if_index;
323 sclass = gs->gs_stitched_external.gs_sclass;
325 case GBP_SUBNET_L3_OUT:
326 sclass = gs->gs_l3_out.gs_sclass;
330 if (WALK_STOP == cb (grd->grd_id, &gs->gs_key->gsk_pfx,
331 gs->gs_type, sw_if_index, sclass, ctx))
337 typedef enum gsb_subnet_show_flags_t_
339 GBP_SUBNET_SHOW_BRIEF,
340 GBP_SUBNET_SHOW_DETAILS,
341 } gsb_subnet_show_flags_t;
344 format_gbp_subnet_type (u8 * s, va_list * args)
346 gbp_subnet_type_t type = va_arg (*args, gbp_subnet_type_t);
350 case GBP_SUBNET_STITCHED_INTERNAL:
351 return (format (s, "stitched-internal"));
352 case GBP_SUBNET_STITCHED_EXTERNAL:
353 return (format (s, "stitched-external"));
354 case GBP_SUBNET_TRANSPORT:
355 return (format (s, "transport"));
356 case GBP_SUBNET_L3_OUT:
357 return (format (s, "l3-out"));
360 return (format (s, "unknown"));
364 format_gbp_subnet (u8 * s, va_list * args)
366 index_t gsi = va_arg (*args, index_t);
367 gsb_subnet_show_flags_t flags = va_arg (*args, gsb_subnet_show_flags_t);
371 gs = pool_elt_at_index (gbp_subnet_pool, gsi);
373 table_id = fib_table_get_table_id (gs->gs_key->gsk_fib_index,
374 gs->gs_key->gsk_pfx.fp_proto);
376 s = format (s, "[%d] tbl:%d %U %U", gsi, table_id,
377 format_fib_prefix, &gs->gs_key->gsk_pfx,
378 format_gbp_subnet_type, gs->gs_type);
382 case GBP_SUBNET_STITCHED_INTERNAL:
383 case GBP_SUBNET_TRANSPORT:
385 case GBP_SUBNET_STITCHED_EXTERNAL:
386 s = format (s, " {sclass:%d %U}", gs->gs_stitched_external.gs_sclass,
387 format_vnet_sw_if_index_name,
388 vnet_get_main (), gs->gs_stitched_external.gs_sw_if_index);
390 case GBP_SUBNET_L3_OUT:
391 s = format (s, " {sclass:%d}", gs->gs_l3_out.gs_sclass);
397 case GBP_SUBNET_SHOW_DETAILS:
399 s = format (s, "\n %U", format_fib_entry, gs->gs_fei,
400 FIB_ENTRY_FORMAT_DETAIL);
402 case GBP_SUBNET_SHOW_BRIEF:
408 static clib_error_t *
409 gbp_subnet_show (vlib_main_t * vm,
410 unformat_input_t * input, vlib_cli_command_t * cmd)
416 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
418 if (unformat (input, "%d", &gsi))
424 if (INDEX_INVALID != gsi)
426 vlib_cli_output (vm, "%U", format_gbp_subnet, gsi,
427 GBP_SUBNET_SHOW_DETAILS);
432 pool_foreach_index(gsi, gbp_subnet_pool,
434 vlib_cli_output (vm, "%U", format_gbp_subnet, gsi,
435 GBP_SUBNET_SHOW_BRIEF);
444 * Show Group Based Policy Subnets
447 * @cliexstart{show gbp subnet}
451 VLIB_CLI_COMMAND (gbp_subnet_show_node, static) = {
452 .path = "show gbp subnet",
453 .short_help = "show gbp subnet\n",
454 .function = gbp_subnet_show,
458 static clib_error_t *
459 gbp_subnet_init (vlib_main_t * vm)
461 gbp_subnet_db = hash_create_mem (0,
462 sizeof (gbp_subnet_key_t), sizeof (u32));
467 VLIB_INIT_FUNCTION (gbp_subnet_init);
470 * fd.io coding-style-patch-verification: ON
473 * eval: (c-set-style "gnu")