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 gbp_route_domain_get_scope (gs->gs_rd),
167 gs->gs_stitched_external.gs_sclass,
168 gs->gs_stitched_external.gs_sw_if_index, &gpd);
170 gs->gs_fei = fib_table_entry_special_dpo_update (gs->gs_key->gsk_fib_index,
171 &gs->gs_key->gsk_pfx,
172 FIB_SOURCE_PLUGIN_HI,
173 (FIB_ENTRY_FLAG_EXCLUSIVE |
174 FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT),
183 gbp_subnet_l3_out_add (gbp_subnet_t * gs, sclass_t sclass, int is_anon)
185 fib_entry_flag_t flags;
186 dpo_id_t gpd = DPO_INVALID;
188 gs->gs_l3_out.gs_sclass = sclass;
190 gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (gs->gs_key->gsk_pfx.fp_proto),
191 gbp_route_domain_get_scope (gs->gs_rd),
192 gs->gs_l3_out.gs_sclass, ~0, &gpd);
194 flags = FIB_ENTRY_FLAG_INTERPOSE;
196 flags |= FIB_ENTRY_FLAG_COVERED_INHERIT;
198 gs->gs_fei = fib_table_entry_special_dpo_add (gs->gs_key->gsk_fib_index,
199 &gs->gs_key->gsk_pfx,
209 gbp_subnet_del_i (index_t gsi)
213 gs = pool_elt_at_index (gbp_subnet_pool, gsi);
215 fib_table_entry_delete_index (gs->gs_fei,
216 (GBP_SUBNET_L3_OUT == gs->gs_type
217 || GBP_SUBNET_ANON_L3_OUT ==
218 gs->gs_type) ? FIB_SOURCE_SPECIAL :
219 FIB_SOURCE_PLUGIN_HI);
221 gbp_subnet_db_del (gs);
222 gbp_route_domain_unlock (gs->gs_rd);
224 pool_put (gbp_subnet_pool, gs);
228 gbp_subnet_del (u32 rd_id, const fib_prefix_t * pfx)
230 gbp_route_domain_t *grd;
234 grdi = gbp_route_domain_find (rd_id);
237 return (VNET_API_ERROR_NO_SUCH_FIB);
239 grd = gbp_route_domain_get (grdi);
240 fib_index = grd->grd_fib_index[pfx->fp_proto];
242 gsi = gbp_subnet_db_find (fib_index, pfx);
244 if (INDEX_INVALID == gsi)
245 return (VNET_API_ERROR_NO_SUCH_ENTRY);
247 gbp_subnet_del_i (gsi);
253 gbp_subnet_add (u32 rd_id,
254 const fib_prefix_t * pfx,
255 gbp_subnet_type_t type, u32 sw_if_index, sclass_t sclass)
257 gbp_route_domain_t *grd;
265 case GBP_SUBNET_TRANSPORT:
266 case GBP_SUBNET_STITCHED_INTERNAL:
267 case GBP_SUBNET_STITCHED_EXTERNAL:
268 case GBP_SUBNET_L3_OUT:
269 case GBP_SUBNET_ANON_L3_OUT:
272 return (VNET_API_ERROR_INCORRECT_ADJACENCY_TYPE);
275 grdi = gbp_route_domain_find_and_lock (rd_id);
278 return (VNET_API_ERROR_NO_SUCH_FIB);
280 grd = gbp_route_domain_get (grdi);
281 fib_index = grd->grd_fib_index[pfx->fp_proto];
283 gsi = gbp_subnet_db_find (fib_index, pfx);
286 * this is an update if the subnet already exists, so remove the old
288 if (INDEX_INVALID != gsi)
289 gbp_subnet_del_i (gsi);
293 pool_get (gbp_subnet_pool, gs);
297 gbp_subnet_db_add (fib_index, pfx, gs);
301 case GBP_SUBNET_STITCHED_INTERNAL:
302 rv = gbp_subnet_internal_add (gs);
304 case GBP_SUBNET_STITCHED_EXTERNAL:
305 rv = gbp_subnet_external_add (gs, sw_if_index, sclass);
307 case GBP_SUBNET_TRANSPORT:
308 rv = gbp_subnet_transport_add (gs);
310 case GBP_SUBNET_L3_OUT:
311 rv = gbp_subnet_l3_out_add (gs, sclass, 0 /* is_anon */ );
313 case GBP_SUBNET_ANON_L3_OUT:
314 rv = gbp_subnet_l3_out_add (gs, sclass, 1 /* is_anon */ );
321 static clib_error_t *
322 gbp_subnet_add_del_cli (vlib_main_t * vm,
323 unformat_input_t * input, vlib_cli_command_t * cmd)
325 unformat_input_t _line_input, *line_input = &_line_input;
326 vnet_main_t *vnm = vnet_get_main ();
327 fib_prefix_t pfx = {.fp_addr = ip46_address_initializer };
330 u32 sw_if_index = ~0;
331 gbp_subnet_type_t type = ~0;
336 /* Get a line of input. */
337 if (!unformat_user (input, unformat_line_input, line_input))
340 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
342 if (unformat (line_input, "del"))
344 else if (unformat (line_input, "rd %d", &rd_id))
348 (line_input, "prefix %U/%d", unformat_ip4_address,
349 &pfx.fp_addr.ip4, &length))
350 pfx.fp_proto = FIB_PROTOCOL_IP4;
353 (line_input, "prefix %U/%d", unformat_ip6_address,
354 &pfx.fp_addr.ip6, &length))
355 pfx.fp_proto = FIB_PROTOCOL_IP6;
356 else if (unformat (line_input, "type transport"))
357 type = GBP_SUBNET_TRANSPORT;
358 else if (unformat (line_input, "type stitched-internal"))
359 type = GBP_SUBNET_STITCHED_INTERNAL;
360 else if (unformat (line_input, "type stitched-external"))
361 type = GBP_SUBNET_STITCHED_EXTERNAL;
362 else if (unformat (line_input, "type anon-l3-out"))
363 type = GBP_SUBNET_ANON_L3_OUT;
364 else if (unformat (line_input, "type l3-out"))
365 type = GBP_SUBNET_L3_OUT;
368 (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index))
370 else if (unformat (line_input, "sclass %u", &sclass))
373 return clib_error_return (0, "unknown input `%U'",
374 format_unformat_error, line_input);
376 unformat_free (line_input);
381 rv = gbp_subnet_add (rd_id, &pfx, type, sw_if_index, sclass);
383 rv = gbp_subnet_del (rd_id, &pfx);
389 case VNET_API_ERROR_NO_SUCH_FIB:
390 return clib_error_return (0, "no such FIB");
393 return clib_error_return (0, "unknown error %d", rv);
397 * Add Group Based Policy Subnets
400 * @cliexstart{gbp subnet [del] rd <ID> prefix <prefix> type <type> [<interface>] [sclass <sclass>]}
404 VLIB_CLI_COMMAND (gbp_subnet_add_del, static) = {
405 .path = "gbp subnet",
406 .short_help = "gbp subnet [del] rd <ID> prefix <prefix> type <type> [<interface>] [sclass <sclass>]\n",
407 .function = gbp_subnet_add_del_cli,
414 gbp_subnet_walk (gbp_subnet_cb_t cb, void *ctx)
416 gbp_route_domain_t *grd;
421 sclass = SCLASS_INVALID;
425 pool_foreach (gs, gbp_subnet_pool,
427 grd = gbp_route_domain_get(gs->gs_rd);
431 case GBP_SUBNET_STITCHED_INTERNAL:
432 case GBP_SUBNET_TRANSPORT:
433 /* use defaults above */
435 case GBP_SUBNET_STITCHED_EXTERNAL:
436 sw_if_index = gs->gs_stitched_external.gs_sw_if_index;
437 sclass = gs->gs_stitched_external.gs_sclass;
439 case GBP_SUBNET_L3_OUT:
440 case GBP_SUBNET_ANON_L3_OUT:
441 sclass = gs->gs_l3_out.gs_sclass;
445 if (WALK_STOP == cb (grd->grd_id, &gs->gs_key->gsk_pfx,
446 gs->gs_type, sw_if_index, sclass, ctx))
452 typedef enum gsb_subnet_show_flags_t_
454 GBP_SUBNET_SHOW_BRIEF,
455 GBP_SUBNET_SHOW_DETAILS,
456 } gsb_subnet_show_flags_t;
459 format_gbp_subnet_type (u8 * s, va_list * args)
461 gbp_subnet_type_t type = va_arg (*args, gbp_subnet_type_t);
465 case GBP_SUBNET_STITCHED_INTERNAL:
466 return (format (s, "stitched-internal"));
467 case GBP_SUBNET_STITCHED_EXTERNAL:
468 return (format (s, "stitched-external"));
469 case GBP_SUBNET_TRANSPORT:
470 return (format (s, "transport"));
471 case GBP_SUBNET_L3_OUT:
472 return (format (s, "l3-out"));
473 case GBP_SUBNET_ANON_L3_OUT:
474 return (format (s, "anon-l3-out"));
477 return (format (s, "unknown"));
481 format_gbp_subnet (u8 * s, va_list * args)
483 index_t gsi = va_arg (*args, index_t);
484 gsb_subnet_show_flags_t flags = va_arg (*args, gsb_subnet_show_flags_t);
488 gs = pool_elt_at_index (gbp_subnet_pool, gsi);
490 table_id = fib_table_get_table_id (gs->gs_key->gsk_fib_index,
491 gs->gs_key->gsk_pfx.fp_proto);
493 s = format (s, "[%d] tbl:%d %U %U", gsi, table_id,
494 format_fib_prefix, &gs->gs_key->gsk_pfx,
495 format_gbp_subnet_type, gs->gs_type);
499 case GBP_SUBNET_STITCHED_INTERNAL:
500 case GBP_SUBNET_TRANSPORT:
502 case GBP_SUBNET_STITCHED_EXTERNAL:
503 s = format (s, " {sclass:%d %U}", gs->gs_stitched_external.gs_sclass,
504 format_vnet_sw_if_index_name,
505 vnet_get_main (), gs->gs_stitched_external.gs_sw_if_index);
507 case GBP_SUBNET_L3_OUT:
508 case GBP_SUBNET_ANON_L3_OUT:
509 s = format (s, " {sclass:%d}", gs->gs_l3_out.gs_sclass);
515 case GBP_SUBNET_SHOW_DETAILS:
517 s = format (s, "\n %U", format_fib_entry, gs->gs_fei,
518 FIB_ENTRY_FORMAT_DETAIL);
520 case GBP_SUBNET_SHOW_BRIEF:
526 static clib_error_t *
527 gbp_subnet_show (vlib_main_t * vm,
528 unformat_input_t * input, vlib_cli_command_t * cmd)
534 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
536 if (unformat (input, "%d", &gsi))
542 if (INDEX_INVALID != gsi)
544 vlib_cli_output (vm, "%U", format_gbp_subnet, gsi,
545 GBP_SUBNET_SHOW_DETAILS);
550 pool_foreach_index(gsi, gbp_subnet_pool,
552 vlib_cli_output (vm, "%U", format_gbp_subnet, gsi,
553 GBP_SUBNET_SHOW_BRIEF);
562 * Show Group Based Policy Subnets
565 * @cliexstart{show gbp subnet}
569 VLIB_CLI_COMMAND (gbp_subnet_show_node, static) = {
570 .path = "show gbp subnet",
571 .short_help = "show gbp subnet\n",
572 .function = gbp_subnet_show,
576 static clib_error_t *
577 gbp_subnet_init (vlib_main_t * vm)
579 gbp_subnet_db = hash_create_mem (0,
580 sizeof (gbp_subnet_key_t), sizeof (u32));
585 VLIB_INIT_FUNCTION (gbp_subnet_init);
588 * fd.io coding-style-patch-verification: ON
591 * eval: (c-set-style "gnu")