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_route_domain.h>
17 #include <plugins/gbp/gbp_endpoint.h>
18 #include <plugins/gbp/gbp_sclass.h>
20 #include <vnet/dpo/dvr_dpo.h>
21 #include <vnet/fib/fib_table.h>
22 #include <vnet/ip/ip_neighbor.h>
25 * A fixed MAC address to use as the source MAC for packets L3 switched
26 * onto the routed uu-fwd interfaces.
27 * Magic values - origin lost to the mists of time...
30 const static mac_address_t GBP_ROUTED_SRC_MAC = {
32 0x0, 0x22, 0xBD, 0xF8, 0x19, 0xFF,
36 const static mac_address_t GBP_ROUTED_DST_MAC = {
38 00, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
44 * Pool of GBP route_domains
46 gbp_route_domain_t *gbp_route_domain_pool;
51 typedef struct gbp_route_domain_db_t
54 } gbp_route_domain_db_t;
56 static gbp_route_domain_db_t gbp_route_domain_db;
61 vlib_log_class_t grd_logger;
63 #define GBP_BD_DBG(...) \
64 vlib_log_debug (grd_logger, __VA_ARGS__);
67 gbp_route_domain_index (const gbp_route_domain_t * grd)
69 return (grd - gbp_route_domain_pool);
73 gbp_route_domain_get (index_t i)
75 return (pool_elt_at_index (gbp_route_domain_pool, i));
79 gbp_route_domain_lock (index_t i)
81 gbp_route_domain_t *grd;
83 grd = gbp_route_domain_get (i);
88 gbp_route_domain_find (u32 rd_id)
92 p = hash_get (gbp_route_domain_db.gbd_by_rd_id, rd_id);
97 return (INDEX_INVALID);
101 gbp_route_domain_find_and_lock (u32 rd_id)
105 grdi = gbp_route_domain_find (rd_id);
107 if (INDEX_INVALID != grdi)
109 gbp_route_domain_lock (grdi);
115 gbp_route_domain_db_add (gbp_route_domain_t * grd)
117 index_t grdi = grd - gbp_route_domain_pool;
119 hash_set (gbp_route_domain_db.gbd_by_rd_id, grd->grd_id, grdi);
123 gbp_route_domain_db_remove (gbp_route_domain_t * grd)
125 hash_unset (gbp_route_domain_db.gbd_by_rd_id, grd->grd_id);
129 gbp_route_domain_add_and_lock (u32 rd_id,
132 u32 ip4_uu_sw_if_index, u32 ip6_uu_sw_if_index)
134 gbp_route_domain_t *grd;
137 grdi = gbp_route_domain_find (rd_id);
139 if (INDEX_INVALID == grdi)
141 fib_protocol_t fproto;
143 pool_get_zero (gbp_route_domain_pool, grd);
146 grd->grd_table_id[FIB_PROTOCOL_IP4] = ip4_table_id;
147 grd->grd_table_id[FIB_PROTOCOL_IP6] = ip6_table_id;
148 grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP4] = ip4_uu_sw_if_index;
149 grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP6] = ip6_uu_sw_if_index;
151 FOR_EACH_FIB_IP_PROTOCOL (fproto)
153 grd->grd_fib_index[fproto] =
154 fib_table_find_or_create_and_lock (fproto,
155 grd->grd_table_id[fproto],
156 FIB_SOURCE_PLUGIN_HI);
158 if (~0 != grd->grd_uu_sw_if_index[fproto])
160 ethernet_header_t *eth;
164 vec_validate (rewrite, sizeof (*eth) - 1);
165 eth = (ethernet_header_t *) rewrite;
167 eth->type = clib_host_to_net_u16 ((fproto == FIB_PROTOCOL_IP4 ?
171 mac_address_to_bytes (gbp_route_domain_get_local_mac (),
173 mac_address_to_bytes (gbp_route_domain_get_remote_mac (),
177 * create an adjacency out of the uu-fwd interfaces that will
178 * be used when adding subnet routes.
180 grd->grd_adj[fproto] =
181 adj_nbr_add_or_lock_w_rewrite (fproto,
182 fib_proto_to_link (fproto),
184 grd->grd_uu_sw_if_index[fproto],
187 gbp_sclass_enable_ip (grd->grd_uu_sw_if_index[fproto]);
191 grd->grd_adj[fproto] = INDEX_INVALID;
195 gbp_route_domain_db_add (grd);
199 grd = gbp_route_domain_get (grdi);
203 GBP_BD_DBG ("add: %U", format_gbp_route_domain, grd);
209 gbp_route_domain_unlock (index_t index)
211 gbp_route_domain_t *grd;
213 grd = gbp_route_domain_get (index);
217 if (0 == grd->grd_locks)
219 fib_protocol_t fproto;
221 GBP_BD_DBG ("destroy: %U", format_gbp_route_domain, grd);
223 FOR_EACH_FIB_IP_PROTOCOL (fproto)
225 fib_table_unlock (grd->grd_fib_index[fproto],
226 fproto, FIB_SOURCE_PLUGIN_HI);
227 if (INDEX_INVALID != grd->grd_adj[fproto])
228 adj_unlock (grd->grd_adj[fproto]);
229 if (~0 != grd->grd_uu_sw_if_index[fproto])
230 gbp_sclass_disable_ip (grd->grd_uu_sw_if_index[fproto]);
233 gbp_route_domain_db_remove (grd);
235 pool_put (gbp_route_domain_pool, grd);
240 gbp_route_domain_get_rd_id (index_t grdi)
242 gbp_route_domain_t *grd;
244 grd = gbp_route_domain_get (grdi);
246 return (grd->grd_id);
250 gbp_route_domain_delete (u32 rd_id)
254 GBP_BD_DBG ("del: %d", rd_id);
255 grdi = gbp_route_domain_find (rd_id);
257 if (INDEX_INVALID != grdi)
259 GBP_BD_DBG ("del: %U", format_gbp_route_domain,
260 gbp_route_domain_get (grdi));
261 gbp_route_domain_unlock (grdi);
266 return (VNET_API_ERROR_NO_SUCH_ENTRY);
269 const mac_address_t *
270 gbp_route_domain_get_local_mac (void)
272 return (&GBP_ROUTED_SRC_MAC);
275 const mac_address_t *
276 gbp_route_domain_get_remote_mac (void)
278 return (&GBP_ROUTED_DST_MAC);
282 gbp_route_domain_walk (gbp_route_domain_cb_t cb, void *ctx)
284 gbp_route_domain_t *gbpe;
287 pool_foreach(gbpe, gbp_route_domain_pool,
295 static clib_error_t *
296 gbp_route_domain_cli (vlib_main_t * vm,
297 unformat_input_t * input, vlib_cli_command_t * cmd)
299 vnet_main_t *vnm = vnet_get_main ();
300 u32 ip4_uu_sw_if_index = ~0;
301 u32 ip6_uu_sw_if_index = ~0;
302 u32 ip4_table_id = ~0;
303 u32 ip6_table_id = ~0;
307 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
309 if (unformat (input, "ip4-uu %U", unformat_vnet_sw_interface,
310 vnm, &ip4_uu_sw_if_index))
312 else if (unformat (input, "ip6-uu %U", unformat_vnet_sw_interface,
313 vnm, &ip6_uu_sw_if_index))
315 else if (unformat (input, "ip4-table-id %d", &ip4_table_id))
317 else if (unformat (input, "ip6-table-id %d", &ip6_table_id))
319 else if (unformat (input, "add"))
321 else if (unformat (input, "del"))
323 else if (unformat (input, "rd %d", &rd_id))
330 return clib_error_return (0, "RD-ID must be specified");
334 if (~0 == ip4_table_id)
335 return clib_error_return (0, "IP4 table-ID must be specified");
336 if (~0 == ip6_table_id)
337 return clib_error_return (0, "IP6 table-ID must be specified");
339 gbp_route_domain_add_and_lock (rd_id, ip4_table_id,
341 ip4_uu_sw_if_index, ip6_uu_sw_if_index);
344 gbp_route_domain_delete (rd_id);
350 * Configure a GBP route-domain
353 * @cliexstart{set gbp route-domain [del] bd <ID> bvi <interface> uu-flood <interface>}
357 VLIB_CLI_COMMAND (gbp_route_domain_cli_node, static) = {
358 .path = "gbp route-domain",
359 .short_help = "gbp route-domain [del] epg bd <ID> bvi <interface> uu-flood <interface>",
360 .function = gbp_route_domain_cli,
364 format_gbp_route_domain (u8 * s, va_list * args)
366 gbp_route_domain_t *grd = va_arg (*args, gbp_route_domain_t*);
367 vnet_main_t *vnm = vnet_get_main ();
370 s = format (s, "[%d] rd:%d ip4-uu:%U ip6-uu:%U locks:%d",
371 grd - gbp_route_domain_pool,
373 format_vnet_sw_if_index_name, vnm, grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP4],
374 format_vnet_sw_if_index_name, vnm, grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP6],
377 s = format (s, "NULL");
383 gbp_route_domain_show_one (gbp_route_domain_t *gb, void *ctx)
388 vlib_cli_output (vm, " %U",format_gbp_route_domain, gb);
393 static clib_error_t *
394 gbp_route_domain_show (vlib_main_t * vm,
395 unformat_input_t * input, vlib_cli_command_t * cmd)
397 vlib_cli_output (vm, "Route-Domains:");
398 gbp_route_domain_walk (gbp_route_domain_show_one, vm);
404 * Show Group Based Policy Route_Domains and derived information
407 * @cliexstart{show gbp route_domain}
411 VLIB_CLI_COMMAND (gbp_route_domain_show_node, static) = {
412 .path = "show gbp route-domain",
413 .short_help = "show gbp route-domain\n",
414 .function = gbp_route_domain_show,
418 static clib_error_t *
419 gbp_route_domain_init (vlib_main_t * vm)
421 grd_logger = vlib_log_register_class ("gbp", "rd");
426 VLIB_INIT_FUNCTION (gbp_route_domain_init);
429 * fd.io coding-style-patch-verification: ON
432 * eval: (c-set-style "gnu")