c030ee51ada8f9a1d3e30418aec7b1bad019024d
[vpp.git] / src / plugins / gbp / gbp_route_domain.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <plugins/gbp/gbp_route_domain.h>
17 #include <plugins/gbp/gbp_endpoint.h>
18
19 #include <vnet/dpo/dvr_dpo.h>
20 #include <vnet/fib/fib_table.h>
21 #include <vnet/ip/ip_neighbor.h>
22
23 /**
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...
27  */
28 /* *INDENT-OFF* */
29 const static mac_address_t GBP_ROUTED_SRC_MAC = {
30   .bytes = {
31     0x0, 0x22, 0xBD, 0xF8, 0x19, 0xFF,
32   }
33 };
34
35 const static mac_address_t GBP_ROUTED_DST_MAC = {
36   .bytes = {
37     00, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
38   }
39 };
40 /* *INDENT-ON* */
41
42 /**
43  * Pool of GBP route_domains
44  */
45 gbp_route_domain_t *gbp_route_domain_pool;
46
47 /**
48  * DB of route_domains
49  */
50 typedef struct gbp_route_domain_db_t
51 {
52   uword *gbd_by_rd_id;
53 } gbp_route_domain_db_t;
54
55 static gbp_route_domain_db_t gbp_route_domain_db;
56
57 /**
58  * logger
59  */
60 vlib_log_class_t grd_logger;
61
62 #define GBP_BD_DBG(...)                           \
63     vlib_log_debug (grd_logger, __VA_ARGS__);
64
65 gbp_route_domain_t *
66 gbp_route_domain_get (index_t i)
67 {
68   return (pool_elt_at_index (gbp_route_domain_pool, i));
69 }
70
71 static void
72 gbp_route_domain_lock (index_t i)
73 {
74   gbp_route_domain_t *grd;
75
76   grd = gbp_route_domain_get (i);
77   grd->grd_locks++;
78 }
79
80 index_t
81 gbp_route_domain_find (u32 rd_id)
82 {
83   uword *p;
84
85   p = hash_get (gbp_route_domain_db.gbd_by_rd_id, rd_id);
86
87   if (NULL != p)
88     return p[0];
89
90   return (INDEX_INVALID);
91 }
92
93 index_t
94 gbp_route_domain_find_and_lock (u32 rd_id)
95 {
96   index_t grdi;
97
98   grdi = gbp_route_domain_find (rd_id);
99
100   if (INDEX_INVALID != grdi)
101     {
102       gbp_route_domain_lock (grdi);
103     }
104   return (grdi);
105 }
106
107 static void
108 gbp_route_domain_db_add (gbp_route_domain_t * grd)
109 {
110   index_t grdi = grd - gbp_route_domain_pool;
111
112   hash_set (gbp_route_domain_db.gbd_by_rd_id, grd->grd_id, grdi);
113 }
114
115 static void
116 gbp_route_domain_db_remove (gbp_route_domain_t * grd)
117 {
118   hash_unset (gbp_route_domain_db.gbd_by_rd_id, grd->grd_id);
119 }
120
121 int
122 gbp_route_domain_add_and_lock (u32 rd_id,
123                                u32 ip4_table_id,
124                                u32 ip6_table_id,
125                                u32 ip4_uu_sw_if_index, u32 ip6_uu_sw_if_index)
126 {
127   gbp_route_domain_t *grd;
128   index_t grdi;
129
130   grdi = gbp_route_domain_find (rd_id);
131
132   if (INDEX_INVALID == grdi)
133     {
134       fib_protocol_t fproto;
135
136       pool_get_zero (gbp_route_domain_pool, grd);
137
138       grd->grd_id = rd_id;
139       grd->grd_table_id[FIB_PROTOCOL_IP4] = ip4_table_id;
140       grd->grd_table_id[FIB_PROTOCOL_IP6] = ip6_table_id;
141       grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP4] = ip4_uu_sw_if_index;
142       grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP6] = ip6_uu_sw_if_index;
143
144       FOR_EACH_FIB_IP_PROTOCOL (fproto)
145       {
146         grd->grd_fib_index[fproto] =
147           fib_table_find_or_create_and_lock (fproto,
148                                              grd->grd_table_id[fproto],
149                                              FIB_SOURCE_PLUGIN_HI);
150
151         if (~0 != grd->grd_uu_sw_if_index[fproto])
152           {
153             ethernet_header_t *eth;
154             u8 *rewrite;
155
156             rewrite = NULL;
157             vec_validate (rewrite, sizeof (*eth) - 1);
158             eth = (ethernet_header_t *) rewrite;
159
160             eth->type = clib_host_to_net_u16 ((fproto == FIB_PROTOCOL_IP4 ?
161                                                ETHERNET_TYPE_IP4 :
162                                                ETHERNET_TYPE_IP6));
163
164             mac_address_to_bytes (gbp_route_domain_get_local_mac (),
165                                   eth->src_address);
166             mac_address_to_bytes (gbp_route_domain_get_remote_mac (),
167                                   eth->src_address);
168
169             /*
170              * create an adjacency out of the uu-fwd interfaces that will
171              * be used when adding subnet routes.
172              */
173             grd->grd_adj[fproto] =
174               adj_nbr_add_or_lock_w_rewrite (fproto,
175                                              fib_proto_to_link (fproto),
176                                              &ADJ_BCAST_ADDR,
177                                              grd->grd_uu_sw_if_index[fproto],
178                                              rewrite);
179           }
180         else
181           {
182             grd->grd_adj[fproto] = INDEX_INVALID;
183           }
184       }
185
186       gbp_route_domain_db_add (grd);
187     }
188   else
189     {
190       grd = gbp_route_domain_get (grdi);
191     }
192
193   grd->grd_locks++;
194   GBP_BD_DBG ("add: %U", format_gbp_route_domain, grd);
195
196   return (0);
197 }
198
199 void
200 gbp_route_domain_unlock (index_t index)
201 {
202   gbp_route_domain_t *grd;
203
204   grd = gbp_route_domain_get (index);
205
206   grd->grd_locks--;
207
208   if (0 == grd->grd_locks)
209     {
210       fib_protocol_t fproto;
211
212       GBP_BD_DBG ("destroy: %U", format_gbp_route_domain, grd);
213
214       FOR_EACH_FIB_IP_PROTOCOL (fproto)
215       {
216         fib_table_unlock (grd->grd_fib_index[fproto],
217                           fproto, FIB_SOURCE_PLUGIN_HI);
218         if (INDEX_INVALID != grd->grd_adj[fproto])
219           adj_unlock (grd->grd_adj[fproto]);
220       }
221
222       gbp_route_domain_db_remove (grd);
223
224       pool_put (gbp_route_domain_pool, grd);
225     }
226 }
227
228 int
229 gbp_route_domain_delete (u32 rd_id)
230 {
231   index_t grdi;
232
233   GBP_BD_DBG ("del: %d", rd_id);
234   grdi = gbp_route_domain_find (rd_id);
235
236   if (INDEX_INVALID != grdi)
237     {
238       GBP_BD_DBG ("del: %U", format_gbp_route_domain,
239                   gbp_route_domain_get (grdi));
240       gbp_route_domain_unlock (grdi);
241
242       return (0);
243     }
244
245   return (VNET_API_ERROR_NO_SUCH_ENTRY);
246 }
247
248 const mac_address_t *
249 gbp_route_domain_get_local_mac (void)
250 {
251   return (&GBP_ROUTED_SRC_MAC);
252 }
253
254 const mac_address_t *
255 gbp_route_domain_get_remote_mac (void)
256 {
257   return (&GBP_ROUTED_DST_MAC);
258 }
259
260 void
261 gbp_route_domain_walk (gbp_route_domain_cb_t cb, void *ctx)
262 {
263   gbp_route_domain_t *gbpe;
264
265   /* *INDENT-OFF* */
266   pool_foreach(gbpe, gbp_route_domain_pool,
267   {
268     if (!cb(gbpe, ctx))
269       break;
270   });
271   /* *INDENT-ON* */
272 }
273
274 static clib_error_t *
275 gbp_route_domain_cli (vlib_main_t * vm,
276                       unformat_input_t * input, vlib_cli_command_t * cmd)
277 {
278   vnet_main_t *vnm = vnet_get_main ();
279   u32 ip4_uu_sw_if_index = ~0;
280   u32 ip6_uu_sw_if_index = ~0;
281   u32 ip4_table_id = ~0;
282   u32 ip6_table_id = ~0;
283   u32 rd_id = ~0;
284   u8 add = 1;
285
286   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
287     {
288       if (unformat (input, "ip4-uu %U", unformat_vnet_sw_interface,
289                     vnm, &ip4_uu_sw_if_index))
290         ;
291       else if (unformat (input, "ip6-uu %U", unformat_vnet_sw_interface,
292                          vnm, &ip6_uu_sw_if_index))
293         ;
294       else if (unformat (input, "ip4-table-id %d", &ip4_table_id))
295         ;
296       else if (unformat (input, "ip6-table-id %d", &ip6_table_id))
297         ;
298       else if (unformat (input, "add"))
299         add = 1;
300       else if (unformat (input, "del"))
301         add = 0;
302       else if (unformat (input, "rd %d", &rd_id))
303         ;
304       else
305         break;
306     }
307
308   if (~0 == rd_id)
309     return clib_error_return (0, "RD-ID must be specified");
310
311   if (add)
312     {
313       if (~0 == ip4_table_id)
314         return clib_error_return (0, "IP4 table-ID must be specified");
315       if (~0 == ip6_table_id)
316         return clib_error_return (0, "IP6 table-ID must be specified");
317
318       gbp_route_domain_add_and_lock (rd_id, ip4_table_id,
319                                      ip6_table_id,
320                                      ip4_uu_sw_if_index, ip6_uu_sw_if_index);
321     }
322   else
323     gbp_route_domain_delete (rd_id);
324
325   return (NULL);
326 }
327
328 /*?
329  * Configure a GBP route-domain
330  *
331  * @cliexpar
332  * @cliexstart{set gbp route-domain [del] bd <ID> bvi <interface> uu-flood <interface>}
333  * @cliexend
334  ?*/
335 /* *INDENT-OFF* */
336 VLIB_CLI_COMMAND (gbp_route_domain_cli_node, static) = {
337   .path = "gbp route-domain",
338   .short_help = "gbp route-domain [del] epg bd <ID> bvi <interface> uu-flood <interface>",
339   .function = gbp_route_domain_cli,
340 };
341
342 u8 *
343 format_gbp_route_domain (u8 * s, va_list * args)
344 {
345   gbp_route_domain_t *grd = va_arg (*args, gbp_route_domain_t*);
346   vnet_main_t *vnm = vnet_get_main ();
347
348   if (NULL != grd)
349     s = format (s, "[%d] rd:%d ip4-uu:%U ip6-uu:%U locks:%d",
350                 grd - gbp_route_domain_pool,
351                 grd->grd_id,
352                 format_vnet_sw_if_index_name, vnm, grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP4],
353                 format_vnet_sw_if_index_name, vnm, grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP6],
354                 grd->grd_locks);
355   else
356     s = format (s, "NULL");
357
358   return (s);
359 }
360
361 static int
362 gbp_route_domain_show_one (gbp_route_domain_t *gb, void *ctx)
363 {
364   vlib_main_t *vm;
365
366   vm = ctx;
367   vlib_cli_output (vm, "  %U",format_gbp_route_domain, gb);
368
369   return (1);
370 }
371
372 static clib_error_t *
373 gbp_route_domain_show (vlib_main_t * vm,
374                    unformat_input_t * input, vlib_cli_command_t * cmd)
375 {
376   vlib_cli_output (vm, "Route-Domains:");
377   gbp_route_domain_walk (gbp_route_domain_show_one, vm);
378
379   return (NULL);
380 }
381
382 /*?
383  * Show Group Based Policy Route_Domains and derived information
384  *
385  * @cliexpar
386  * @cliexstart{show gbp route_domain}
387  * @cliexend
388  ?*/
389 /* *INDENT-OFF* */
390 VLIB_CLI_COMMAND (gbp_route_domain_show_node, static) = {
391   .path = "show gbp route-domain",
392   .short_help = "show gbp route-domain\n",
393   .function = gbp_route_domain_show,
394 };
395 /* *INDENT-ON* */
396
397 static clib_error_t *
398 gbp_route_domain_init (vlib_main_t * vm)
399 {
400   grd_logger = vlib_log_register_class ("gbp", "rd");
401
402   return (NULL);
403 }
404
405 VLIB_INIT_FUNCTION (gbp_route_domain_init);
406
407 /*
408  * fd.io coding-style-patch-verification: ON
409  *
410  * Local Variables:
411  * eval: (c-set-style "gnu")
412  * End:
413  */