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>
23 * A fixed MAC address to use as the source MAC for packets L3 switched
24 * onto the routed uu-fwd interfaces.
25 * Magic values - origin lost to the mists of time...
28 const static mac_address_t GBP_ROUTED_SRC_MAC = {
30 0x0, 0x22, 0xBD, 0xF8, 0x19, 0xFF,
34 const static mac_address_t GBP_ROUTED_DST_MAC = {
36 00, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
42 * Pool of GBP route_domains
44 gbp_route_domain_t *gbp_route_domain_pool;
49 typedef struct gbp_route_domain_db_t
52 } gbp_route_domain_db_t;
54 static gbp_route_domain_db_t gbp_route_domain_db;
55 static fib_source_t gbp_fib_source;
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,
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_scope = scope;
147 grd->grd_table_id[FIB_PROTOCOL_IP4] = ip4_table_id;
148 grd->grd_table_id[FIB_PROTOCOL_IP6] = ip6_table_id;
149 grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP4] = ip4_uu_sw_if_index;
150 grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP6] = ip6_uu_sw_if_index;
152 FOR_EACH_FIB_IP_PROTOCOL (fproto)
154 grd->grd_fib_index[fproto] =
155 fib_table_find_or_create_and_lock (fproto,
156 grd->grd_table_id[fproto],
159 if (~0 != grd->grd_uu_sw_if_index[fproto])
161 ethernet_header_t *eth;
165 vec_validate (rewrite, sizeof (*eth) - 1);
166 eth = (ethernet_header_t *) rewrite;
168 eth->type = clib_host_to_net_u16 ((fproto == FIB_PROTOCOL_IP4 ?
172 mac_address_to_bytes (gbp_route_domain_get_local_mac (),
174 mac_address_to_bytes (gbp_route_domain_get_remote_mac (),
178 * create an adjacency out of the uu-fwd interfaces that will
179 * be used when adding subnet routes.
181 grd->grd_adj[fproto] =
182 adj_nbr_add_or_lock_w_rewrite (fproto,
183 fib_proto_to_link (fproto),
185 grd->grd_uu_sw_if_index[fproto],
190 grd->grd_adj[fproto] = INDEX_INVALID;
194 gbp_route_domain_db_add (grd);
198 grd = gbp_route_domain_get (grdi);
202 GBP_BD_DBG ("add: %U", format_gbp_route_domain, grd);
208 gbp_route_domain_unlock (index_t index)
210 gbp_route_domain_t *grd;
212 grd = gbp_route_domain_get (index);
216 if (0 == grd->grd_locks)
218 fib_protocol_t fproto;
220 GBP_BD_DBG ("destroy: %U", format_gbp_route_domain, grd);
222 FOR_EACH_FIB_IP_PROTOCOL (fproto)
224 fib_table_unlock (grd->grd_fib_index[fproto], fproto, gbp_fib_source);
225 if (INDEX_INVALID != grd->grd_adj[fproto])
226 adj_unlock (grd->grd_adj[fproto]);
229 gbp_route_domain_db_remove (grd);
231 pool_put (gbp_route_domain_pool, grd);
236 gbp_route_domain_get_rd_id (index_t grdi)
238 gbp_route_domain_t *grd;
240 grd = gbp_route_domain_get (grdi);
242 return (grd->grd_id);
246 gbp_route_domain_get_scope (index_t grdi)
248 gbp_route_domain_t *grd;
250 grd = gbp_route_domain_get (grdi);
252 return (grd->grd_scope);
256 gbp_route_domain_delete (u32 rd_id)
260 GBP_BD_DBG ("del: %d", rd_id);
261 grdi = gbp_route_domain_find (rd_id);
263 if (INDEX_INVALID != grdi)
265 GBP_BD_DBG ("del: %U", format_gbp_route_domain,
266 gbp_route_domain_get (grdi));
267 gbp_route_domain_unlock (grdi);
272 return (VNET_API_ERROR_NO_SUCH_ENTRY);
275 const mac_address_t *
276 gbp_route_domain_get_local_mac (void)
278 return (&GBP_ROUTED_SRC_MAC);
281 const mac_address_t *
282 gbp_route_domain_get_remote_mac (void)
284 return (&GBP_ROUTED_DST_MAC);
288 gbp_route_domain_walk (gbp_route_domain_cb_t cb, void *ctx)
290 gbp_route_domain_t *gbpe;
293 pool_foreach (gbpe, gbp_route_domain_pool)
301 static clib_error_t *
302 gbp_route_domain_cli (vlib_main_t * vm,
303 unformat_input_t * input, vlib_cli_command_t * cmd)
305 vnet_main_t *vnm = vnet_get_main ();
306 u32 ip4_uu_sw_if_index = ~0;
307 u32 ip6_uu_sw_if_index = ~0;
308 u32 ip4_table_id = ~0;
309 u32 ip6_table_id = ~0;
314 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
316 if (unformat (input, "ip4-uu %U", unformat_vnet_sw_interface,
317 vnm, &ip4_uu_sw_if_index))
319 else if (unformat (input, "ip6-uu %U", unformat_vnet_sw_interface,
320 vnm, &ip6_uu_sw_if_index))
322 else if (unformat (input, "ip4-table-id %d", &ip4_table_id))
324 else if (unformat (input, "ip6-table-id %d", &ip6_table_id))
326 else if (unformat (input, "add"))
328 else if (unformat (input, "del"))
330 else if (unformat (input, "rd %d", &rd_id))
332 else if (unformat (input, "scope %d", &scope))
339 return clib_error_return (0, "RD-ID must be specified");
343 if (~0 == ip4_table_id)
344 return clib_error_return (0, "IP4 table-ID must be specified");
345 if (~0 == ip6_table_id)
346 return clib_error_return (0, "IP6 table-ID must be specified");
348 gbp_route_domain_add_and_lock (rd_id, scope,
351 ip4_uu_sw_if_index, ip6_uu_sw_if_index);
354 gbp_route_domain_delete (rd_id);
360 * Configure a GBP route-domain
363 * @cliexstart{gbp route-domain [del] rd <ID> ip4-table-id <ID> ip6-table-id <ID> [ip4-uu <interface>] [ip6-uu <interface>]}
367 VLIB_CLI_COMMAND (gbp_route_domain_cli_node, static) = {
368 .path = "gbp route-domain",
369 .short_help = "gbp route-domain [del] rd <ID> ip4-table-id <ID> ip6-table-id <ID> [ip4-uu <interface>] [ip6-uu <interface>]",
370 .function = gbp_route_domain_cli,
374 format_gbp_route_domain (u8 * s, va_list * args)
376 gbp_route_domain_t *grd = va_arg (*args, gbp_route_domain_t*);
377 vnet_main_t *vnm = vnet_get_main ();
380 s = format (s, "[%d] rd:%d ip4-uu:%U ip6-uu:%U locks:%d",
381 grd - gbp_route_domain_pool,
383 format_vnet_sw_if_index_name, vnm, grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP4],
384 format_vnet_sw_if_index_name, vnm, grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP6],
387 s = format (s, "NULL");
393 gbp_route_domain_show_one (gbp_route_domain_t *gb, void *ctx)
398 vlib_cli_output (vm, " %U",format_gbp_route_domain, gb);
403 static clib_error_t *
404 gbp_route_domain_show (vlib_main_t * vm,
405 unformat_input_t * input, vlib_cli_command_t * cmd)
407 vlib_cli_output (vm, "Route-Domains:");
408 gbp_route_domain_walk (gbp_route_domain_show_one, vm);
414 * Show Group Based Policy Route_Domains and derived information
417 * @cliexstart{show gbp route_domain}
421 VLIB_CLI_COMMAND (gbp_route_domain_show_node, static) = {
422 .path = "show gbp route-domain",
423 .short_help = "show gbp route-domain\n",
424 .function = gbp_route_domain_show,
428 static clib_error_t *
429 gbp_route_domain_init (vlib_main_t * vm)
431 grd_logger = vlib_log_register_class ("gbp", "rd");
432 gbp_fib_source = fib_source_allocate ("gbp-rd",
433 FIB_SOURCE_PRIORITY_HI,
439 VLIB_INIT_FUNCTION (gbp_route_domain_init);
442 * fd.io coding-style-patch-verification: ON
445 * eval: (c-set-style "gnu")