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>
19 #include <vnet/dpo/dvr_dpo.h>
20 #include <vnet/fib/fib_table.h>
21 #include <vnet/ip/ip_neighbor.h>
24 * A fixed MAC address to use as the source MAC for packets L3 switched
25 * onto the routed uu-fwd interfaces.
26 * Magic values - origin lost to the mists of time...
29 const static mac_address_t GBP_ROUTED_SRC_MAC = {
31 0x0, 0x22, 0xBD, 0xF8, 0x19, 0xFF,
35 const static mac_address_t GBP_ROUTED_DST_MAC = {
37 00, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
43 * Pool of GBP route_domains
45 gbp_route_domain_t *gbp_route_domain_pool;
50 typedef struct gbp_route_domain_db_t
53 } gbp_route_domain_db_t;
55 static gbp_route_domain_db_t gbp_route_domain_db;
60 vlib_log_class_t grd_logger;
62 #define GBP_BD_DBG(...) \
63 vlib_log_debug (grd_logger, __VA_ARGS__);
66 gbp_route_domain_index (const gbp_route_domain_t * grd)
68 return (grd - gbp_route_domain_pool);
72 gbp_route_domain_get (index_t i)
74 return (pool_elt_at_index (gbp_route_domain_pool, i));
78 gbp_route_domain_lock (index_t i)
80 gbp_route_domain_t *grd;
82 grd = gbp_route_domain_get (i);
87 gbp_route_domain_find (u32 rd_id)
91 p = hash_get (gbp_route_domain_db.gbd_by_rd_id, rd_id);
96 return (INDEX_INVALID);
100 gbp_route_domain_find_and_lock (u32 rd_id)
104 grdi = gbp_route_domain_find (rd_id);
106 if (INDEX_INVALID != grdi)
108 gbp_route_domain_lock (grdi);
114 gbp_route_domain_db_add (gbp_route_domain_t * grd)
116 index_t grdi = grd - gbp_route_domain_pool;
118 hash_set (gbp_route_domain_db.gbd_by_rd_id, grd->grd_id, grdi);
122 gbp_route_domain_db_remove (gbp_route_domain_t * grd)
124 hash_unset (gbp_route_domain_db.gbd_by_rd_id, grd->grd_id);
128 gbp_route_domain_add_and_lock (u32 rd_id,
131 u32 ip4_uu_sw_if_index, u32 ip6_uu_sw_if_index)
133 gbp_route_domain_t *grd;
136 grdi = gbp_route_domain_find (rd_id);
138 if (INDEX_INVALID == grdi)
140 fib_protocol_t fproto;
142 pool_get_zero (gbp_route_domain_pool, grd);
145 grd->grd_table_id[FIB_PROTOCOL_IP4] = ip4_table_id;
146 grd->grd_table_id[FIB_PROTOCOL_IP6] = ip6_table_id;
147 grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP4] = ip4_uu_sw_if_index;
148 grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP6] = ip6_uu_sw_if_index;
150 FOR_EACH_FIB_IP_PROTOCOL (fproto)
152 grd->grd_fib_index[fproto] =
153 fib_table_find_or_create_and_lock (fproto,
154 grd->grd_table_id[fproto],
155 FIB_SOURCE_PLUGIN_HI);
157 if (~0 != grd->grd_uu_sw_if_index[fproto])
159 ethernet_header_t *eth;
163 vec_validate (rewrite, sizeof (*eth) - 1);
164 eth = (ethernet_header_t *) rewrite;
166 eth->type = clib_host_to_net_u16 ((fproto == FIB_PROTOCOL_IP4 ?
170 mac_address_to_bytes (gbp_route_domain_get_local_mac (),
172 mac_address_to_bytes (gbp_route_domain_get_remote_mac (),
176 * create an adjacency out of the uu-fwd interfaces that will
177 * be used when adding subnet routes.
179 grd->grd_adj[fproto] =
180 adj_nbr_add_or_lock_w_rewrite (fproto,
181 fib_proto_to_link (fproto),
183 grd->grd_uu_sw_if_index[fproto],
188 grd->grd_adj[fproto] = INDEX_INVALID;
192 gbp_route_domain_db_add (grd);
196 grd = gbp_route_domain_get (grdi);
200 GBP_BD_DBG ("add: %U", format_gbp_route_domain, grd);
206 gbp_route_domain_unlock (index_t index)
208 gbp_route_domain_t *grd;
210 grd = gbp_route_domain_get (index);
214 if (0 == grd->grd_locks)
216 fib_protocol_t fproto;
218 GBP_BD_DBG ("destroy: %U", format_gbp_route_domain, grd);
220 FOR_EACH_FIB_IP_PROTOCOL (fproto)
222 fib_table_unlock (grd->grd_fib_index[fproto],
223 fproto, FIB_SOURCE_PLUGIN_HI);
224 if (INDEX_INVALID != grd->grd_adj[fproto])
225 adj_unlock (grd->grd_adj[fproto]);
228 gbp_route_domain_db_remove (grd);
230 pool_put (gbp_route_domain_pool, grd);
235 gbp_route_domain_get_rd_id (index_t grdi)
237 gbp_route_domain_t *grd;
239 grd = gbp_route_domain_get (grdi);
241 return (grd->grd_id);
245 gbp_route_domain_delete (u32 rd_id)
249 GBP_BD_DBG ("del: %d", rd_id);
250 grdi = gbp_route_domain_find (rd_id);
252 if (INDEX_INVALID != grdi)
254 GBP_BD_DBG ("del: %U", format_gbp_route_domain,
255 gbp_route_domain_get (grdi));
256 gbp_route_domain_unlock (grdi);
261 return (VNET_API_ERROR_NO_SUCH_ENTRY);
264 const mac_address_t *
265 gbp_route_domain_get_local_mac (void)
267 return (&GBP_ROUTED_SRC_MAC);
270 const mac_address_t *
271 gbp_route_domain_get_remote_mac (void)
273 return (&GBP_ROUTED_DST_MAC);
277 gbp_route_domain_walk (gbp_route_domain_cb_t cb, void *ctx)
279 gbp_route_domain_t *gbpe;
282 pool_foreach(gbpe, gbp_route_domain_pool,
290 static clib_error_t *
291 gbp_route_domain_cli (vlib_main_t * vm,
292 unformat_input_t * input, vlib_cli_command_t * cmd)
294 vnet_main_t *vnm = vnet_get_main ();
295 u32 ip4_uu_sw_if_index = ~0;
296 u32 ip6_uu_sw_if_index = ~0;
297 u32 ip4_table_id = ~0;
298 u32 ip6_table_id = ~0;
302 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
304 if (unformat (input, "ip4-uu %U", unformat_vnet_sw_interface,
305 vnm, &ip4_uu_sw_if_index))
307 else if (unformat (input, "ip6-uu %U", unformat_vnet_sw_interface,
308 vnm, &ip6_uu_sw_if_index))
310 else if (unformat (input, "ip4-table-id %d", &ip4_table_id))
312 else if (unformat (input, "ip6-table-id %d", &ip6_table_id))
314 else if (unformat (input, "add"))
316 else if (unformat (input, "del"))
318 else if (unformat (input, "rd %d", &rd_id))
325 return clib_error_return (0, "RD-ID must be specified");
329 if (~0 == ip4_table_id)
330 return clib_error_return (0, "IP4 table-ID must be specified");
331 if (~0 == ip6_table_id)
332 return clib_error_return (0, "IP6 table-ID must be specified");
334 gbp_route_domain_add_and_lock (rd_id, ip4_table_id,
336 ip4_uu_sw_if_index, ip6_uu_sw_if_index);
339 gbp_route_domain_delete (rd_id);
345 * Configure a GBP route-domain
348 * @cliexstart{set gbp route-domain [del] bd <ID> bvi <interface> uu-flood <interface>}
352 VLIB_CLI_COMMAND (gbp_route_domain_cli_node, static) = {
353 .path = "gbp route-domain",
354 .short_help = "gbp route-domain [del] epg bd <ID> bvi <interface> uu-flood <interface>",
355 .function = gbp_route_domain_cli,
359 format_gbp_route_domain (u8 * s, va_list * args)
361 gbp_route_domain_t *grd = va_arg (*args, gbp_route_domain_t*);
362 vnet_main_t *vnm = vnet_get_main ();
365 s = format (s, "[%d] rd:%d ip4-uu:%U ip6-uu:%U locks:%d",
366 grd - gbp_route_domain_pool,
368 format_vnet_sw_if_index_name, vnm, grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP4],
369 format_vnet_sw_if_index_name, vnm, grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP6],
372 s = format (s, "NULL");
378 gbp_route_domain_show_one (gbp_route_domain_t *gb, void *ctx)
383 vlib_cli_output (vm, " %U",format_gbp_route_domain, gb);
388 static clib_error_t *
389 gbp_route_domain_show (vlib_main_t * vm,
390 unformat_input_t * input, vlib_cli_command_t * cmd)
392 vlib_cli_output (vm, "Route-Domains:");
393 gbp_route_domain_walk (gbp_route_domain_show_one, vm);
399 * Show Group Based Policy Route_Domains and derived information
402 * @cliexstart{show gbp route_domain}
406 VLIB_CLI_COMMAND (gbp_route_domain_show_node, static) = {
407 .path = "show gbp route-domain",
408 .short_help = "show gbp route-domain\n",
409 .function = gbp_route_domain_show,
413 static clib_error_t *
414 gbp_route_domain_init (vlib_main_t * vm)
416 grd_logger = vlib_log_register_class ("gbp", "rd");
421 VLIB_INIT_FUNCTION (gbp_route_domain_init);
424 * fd.io coding-style-patch-verification: ON
427 * eval: (c-set-style "gnu")