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