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;
56 static fib_source_t gbp_fib_source;
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,
133 u32 ip4_uu_sw_if_index, u32 ip6_uu_sw_if_index)
135 gbp_route_domain_t *grd;
138 grdi = gbp_route_domain_find (rd_id);
140 if (INDEX_INVALID == grdi)
142 fib_protocol_t fproto;
144 pool_get_zero (gbp_route_domain_pool, grd);
147 grd->grd_scope = scope;
148 grd->grd_table_id[FIB_PROTOCOL_IP4] = ip4_table_id;
149 grd->grd_table_id[FIB_PROTOCOL_IP6] = ip6_table_id;
150 grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP4] = ip4_uu_sw_if_index;
151 grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP6] = ip6_uu_sw_if_index;
153 FOR_EACH_FIB_IP_PROTOCOL (fproto)
155 grd->grd_fib_index[fproto] =
156 fib_table_find_or_create_and_lock (fproto,
157 grd->grd_table_id[fproto],
160 if (~0 != grd->grd_uu_sw_if_index[fproto])
162 ethernet_header_t *eth;
166 vec_validate (rewrite, sizeof (*eth) - 1);
167 eth = (ethernet_header_t *) rewrite;
169 eth->type = clib_host_to_net_u16 ((fproto == FIB_PROTOCOL_IP4 ?
173 mac_address_to_bytes (gbp_route_domain_get_local_mac (),
175 mac_address_to_bytes (gbp_route_domain_get_remote_mac (),
179 * create an adjacency out of the uu-fwd interfaces that will
180 * be used when adding subnet routes.
182 grd->grd_adj[fproto] =
183 adj_nbr_add_or_lock_w_rewrite (fproto,
184 fib_proto_to_link (fproto),
186 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], fproto, gbp_fib_source);
226 if (INDEX_INVALID != grd->grd_adj[fproto])
227 adj_unlock (grd->grd_adj[fproto]);
230 gbp_route_domain_db_remove (grd);
232 pool_put (gbp_route_domain_pool, grd);
237 gbp_route_domain_get_rd_id (index_t grdi)
239 gbp_route_domain_t *grd;
241 grd = gbp_route_domain_get (grdi);
243 return (grd->grd_id);
247 gbp_route_domain_get_scope (index_t grdi)
249 gbp_route_domain_t *grd;
251 grd = gbp_route_domain_get (grdi);
253 return (grd->grd_scope);
257 gbp_route_domain_delete (u32 rd_id)
261 GBP_BD_DBG ("del: %d", rd_id);
262 grdi = gbp_route_domain_find (rd_id);
264 if (INDEX_INVALID != grdi)
266 GBP_BD_DBG ("del: %U", format_gbp_route_domain,
267 gbp_route_domain_get (grdi));
268 gbp_route_domain_unlock (grdi);
273 return (VNET_API_ERROR_NO_SUCH_ENTRY);
276 const mac_address_t *
277 gbp_route_domain_get_local_mac (void)
279 return (&GBP_ROUTED_SRC_MAC);
282 const mac_address_t *
283 gbp_route_domain_get_remote_mac (void)
285 return (&GBP_ROUTED_DST_MAC);
289 gbp_route_domain_walk (gbp_route_domain_cb_t cb, void *ctx)
291 gbp_route_domain_t *gbpe;
294 pool_foreach(gbpe, gbp_route_domain_pool,
302 static clib_error_t *
303 gbp_route_domain_cli (vlib_main_t * vm,
304 unformat_input_t * input, vlib_cli_command_t * cmd)
306 vnet_main_t *vnm = vnet_get_main ();
307 u32 ip4_uu_sw_if_index = ~0;
308 u32 ip6_uu_sw_if_index = ~0;
309 u32 ip4_table_id = ~0;
310 u32 ip6_table_id = ~0;
315 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
317 if (unformat (input, "ip4-uu %U", unformat_vnet_sw_interface,
318 vnm, &ip4_uu_sw_if_index))
320 else if (unformat (input, "ip6-uu %U", unformat_vnet_sw_interface,
321 vnm, &ip6_uu_sw_if_index))
323 else if (unformat (input, "ip4-table-id %d", &ip4_table_id))
325 else if (unformat (input, "ip6-table-id %d", &ip6_table_id))
327 else if (unformat (input, "add"))
329 else if (unformat (input, "del"))
331 else if (unformat (input, "rd %d", &rd_id))
333 else if (unformat (input, "scope %d", &scope))
340 return clib_error_return (0, "RD-ID must be specified");
344 if (~0 == ip4_table_id)
345 return clib_error_return (0, "IP4 table-ID must be specified");
346 if (~0 == ip6_table_id)
347 return clib_error_return (0, "IP6 table-ID must be specified");
349 gbp_route_domain_add_and_lock (rd_id, scope,
352 ip4_uu_sw_if_index, ip6_uu_sw_if_index);
355 gbp_route_domain_delete (rd_id);
361 * Configure a GBP route-domain
364 * @cliexstart{gbp route-domain [del] rd <ID> ip4-table-id <ID> ip6-table-id <ID> [ip4-uu <interface>] [ip6-uu <interface>]}
368 VLIB_CLI_COMMAND (gbp_route_domain_cli_node, static) = {
369 .path = "gbp route-domain",
370 .short_help = "gbp route-domain [del] rd <ID> ip4-table-id <ID> ip6-table-id <ID> [ip4-uu <interface>] [ip6-uu <interface>]",
371 .function = gbp_route_domain_cli,
375 format_gbp_route_domain (u8 * s, va_list * args)
377 gbp_route_domain_t *grd = va_arg (*args, gbp_route_domain_t*);
378 vnet_main_t *vnm = vnet_get_main ();
381 s = format (s, "[%d] rd:%d ip4-uu:%U ip6-uu:%U locks:%d",
382 grd - gbp_route_domain_pool,
384 format_vnet_sw_if_index_name, vnm, grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP4],
385 format_vnet_sw_if_index_name, vnm, grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP6],
388 s = format (s, "NULL");
394 gbp_route_domain_show_one (gbp_route_domain_t *gb, void *ctx)
399 vlib_cli_output (vm, " %U",format_gbp_route_domain, gb);
404 static clib_error_t *
405 gbp_route_domain_show (vlib_main_t * vm,
406 unformat_input_t * input, vlib_cli_command_t * cmd)
408 vlib_cli_output (vm, "Route-Domains:");
409 gbp_route_domain_walk (gbp_route_domain_show_one, vm);
415 * Show Group Based Policy Route_Domains and derived information
418 * @cliexstart{show gbp route_domain}
422 VLIB_CLI_COMMAND (gbp_route_domain_show_node, static) = {
423 .path = "show gbp route-domain",
424 .short_help = "show gbp route-domain\n",
425 .function = gbp_route_domain_show,
429 static clib_error_t *
430 gbp_route_domain_init (vlib_main_t * vm)
432 grd_logger = vlib_log_register_class ("gbp", "rd");
433 gbp_fib_source = fib_source_allocate ("gbp-rd",
434 FIB_SOURCE_PRIORITY_HI,
440 VLIB_INIT_FUNCTION (gbp_route_domain_init);
443 * fd.io coding-style-patch-verification: ON
446 * eval: (c-set-style "gnu")