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