ee4af2c0060ec2e479343e7559df7068adf01b36
[vpp.git] / src / plugins / gbp / gbp_endpoint_group.c
1 /*
2  * gbp.h : Group Based Policy
3  *
4  * Copyright (c) 2018 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <plugins/gbp/gbp_endpoint_group.h>
19 #include <plugins/gbp/gbp_endpoint.h>
20 #include <plugins/gbp/gbp_bridge_domain.h>
21 #include <plugins/gbp/gbp_route_domain.h>
22
23 #include <vnet/dpo/dvr_dpo.h>
24 #include <vnet/fib/fib_table.h>
25 #include <vnet/l2/l2_input.h>
26
27 /**
28  * Pool of GBP endpoint_groups
29  */
30 gbp_endpoint_group_t *gbp_endpoint_group_pool;
31
32 /**
33  * DB of endpoint_groups
34  */
35 gbp_endpoint_group_db_t gbp_endpoint_group_db;
36 vlib_log_class_t gg_logger;
37
38 #define GBP_EPG_DBG(...)                           \
39     vlib_log_debug (gg_logger, __VA_ARGS__);
40
41 gbp_endpoint_group_t *
42 gbp_endpoint_group_get (index_t i)
43 {
44   return (pool_elt_at_index (gbp_endpoint_group_pool, i));
45 }
46
47 static void
48 gbp_endpoint_group_lock (index_t i)
49 {
50   gbp_endpoint_group_t *gg;
51
52   gg = gbp_endpoint_group_get (i);
53   gg->gg_locks++;
54 }
55
56 index_t
57 gbp_endpoint_group_find (epg_id_t epg_id)
58 {
59   uword *p;
60
61   p = hash_get (gbp_endpoint_group_db.gg_hash, epg_id);
62
63   if (NULL != p)
64     return p[0];
65
66   return (INDEX_INVALID);
67 }
68
69 index_t
70 gbp_endpoint_group_find_and_lock (epg_id_t epg_id)
71 {
72   uword *p;
73
74   p = hash_get (gbp_endpoint_group_db.gg_hash, epg_id);
75
76   if (NULL != p)
77     {
78       gbp_endpoint_group_lock (p[0]);
79       return p[0];
80     }
81   return (INDEX_INVALID);
82 }
83
84 int
85 gbp_endpoint_group_add_and_lock (epg_id_t epg_id,
86                                  u32 bd_id, u32 rd_id, u32 uplink_sw_if_index)
87 {
88   gbp_endpoint_group_t *gg;
89   index_t ggi;
90
91   ggi = gbp_endpoint_group_find (epg_id);
92
93   if (INDEX_INVALID == ggi)
94     {
95       gbp_bridge_domain_t *gb;
96       fib_protocol_t fproto;
97       index_t gbi, grdi;
98
99       gbi = gbp_bridge_domain_find_and_lock (bd_id);
100
101       if (~0 == gbi)
102         return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
103
104       grdi = gbp_route_domain_find_and_lock (rd_id);
105
106       if (~0 == grdi)
107         {
108           gbp_bridge_domain_unlock (gbi);
109           return (VNET_API_ERROR_NO_SUCH_FIB);
110         }
111
112       gb = gbp_bridge_domain_get (gbi);
113
114       pool_get_zero (gbp_endpoint_group_pool, gg);
115
116       gg->gg_id = epg_id;
117       gg->gg_rd = grdi;
118       gg->gg_gbd = gbi;
119       gg->gg_bd_index = gb->gb_bd_index;
120
121       gg->gg_uplink_sw_if_index = uplink_sw_if_index;
122       gg->gg_locks = 1;
123
124       /*
125        * an egress DVR dpo for internal subnets to use when sending
126        * on the uplink interface
127        */
128       if (~0 != gg->gg_uplink_sw_if_index)
129         {
130           FOR_EACH_FIB_IP_PROTOCOL (fproto)
131           {
132             dvr_dpo_add_or_lock (uplink_sw_if_index,
133                                  fib_proto_to_dpo (fproto),
134                                  &gg->gg_dpo[fproto]);
135           }
136
137           /*
138            * Add the uplink to the BD
139            * packets direct from the uplink have had policy applied
140            */
141           set_int_l2_mode (vlib_get_main (), vnet_get_main (),
142                            MODE_L2_BRIDGE, gg->gg_uplink_sw_if_index,
143                            gg->gg_bd_index, L2_BD_PORT_TYPE_NORMAL, 0, 0);
144           l2input_intf_bitmap_enable (gg->gg_uplink_sw_if_index,
145                                       L2INPUT_FEAT_GBP_NULL_CLASSIFY, 1);
146         }
147
148       hash_set (gbp_endpoint_group_db.gg_hash,
149                 gg->gg_id, gg - gbp_endpoint_group_pool);
150
151     }
152   else
153     {
154       gg = gbp_endpoint_group_get (ggi);
155       gg->gg_locks++;
156     }
157
158   GBP_EPG_DBG ("add: %U", format_gbp_endpoint_group, gg);
159
160   return (0);
161 }
162
163 void
164 gbp_endpoint_group_unlock (index_t ggi)
165 {
166   gbp_endpoint_group_t *gg;
167
168   gg = gbp_endpoint_group_get (ggi);
169
170   gg->gg_locks--;
171
172   if (0 == gg->gg_locks)
173     {
174       fib_protocol_t fproto;
175
176       gg = pool_elt_at_index (gbp_endpoint_group_pool, ggi);
177
178       if (~0 != gg->gg_uplink_sw_if_index)
179         {
180           set_int_l2_mode (vlib_get_main (), vnet_get_main (),
181                            MODE_L3, gg->gg_uplink_sw_if_index,
182                            gg->gg_bd_index, L2_BD_PORT_TYPE_NORMAL, 0, 0);
183
184           l2input_intf_bitmap_enable (gg->gg_uplink_sw_if_index,
185                                       L2INPUT_FEAT_GBP_NULL_CLASSIFY, 0);
186         }
187       FOR_EACH_FIB_IP_PROTOCOL (fproto)
188       {
189         dpo_reset (&gg->gg_dpo[fproto]);
190       }
191       gbp_bridge_domain_unlock (gg->gg_gbd);
192       gbp_route_domain_unlock (gg->gg_rd);
193
194       hash_unset (gbp_endpoint_group_db.gg_hash, gg->gg_id);
195
196       pool_put (gbp_endpoint_group_pool, gg);
197     }
198 }
199
200 int
201 gbp_endpoint_group_delete (epg_id_t epg_id)
202 {
203   index_t ggi;
204
205   ggi = gbp_endpoint_group_find (epg_id);
206
207   if (INDEX_INVALID != ggi)
208     {
209       GBP_EPG_DBG ("del: %U", format_gbp_endpoint_group,
210                    gbp_endpoint_group_get (ggi));
211       gbp_endpoint_group_unlock (ggi);
212
213       return (0);
214     }
215
216   return (VNET_API_ERROR_NO_SUCH_ENTRY);
217 }
218
219 u32
220 gbp_endpoint_group_get_bd_id (const gbp_endpoint_group_t * gg)
221 {
222   const gbp_bridge_domain_t *gb;
223
224   gb = gbp_bridge_domain_get (gg->gg_gbd);
225
226   return (gb->gb_bd_id);
227 }
228
229 index_t
230 gbp_endpoint_group_get_fib_index (gbp_endpoint_group_t * gg,
231                                   fib_protocol_t fproto)
232 {
233   const gbp_route_domain_t *grd;
234
235   grd = gbp_route_domain_get (gg->gg_rd);
236
237   return (grd->grd_fib_index[fproto]);
238 }
239
240 u32
241 gbp_endpoint_group_get_bvi (gbp_endpoint_group_t * gg)
242 {
243   const gbp_bridge_domain_t *gb;
244
245   gb = gbp_bridge_domain_get (gg->gg_gbd);
246
247   return (gb->gb_bvi_sw_if_index);
248 }
249
250 void
251 gbp_endpoint_group_walk (gbp_endpoint_group_cb_t cb, void *ctx)
252 {
253   gbp_endpoint_group_t *gbpe;
254
255   /* *INDENT-OFF* */
256   pool_foreach(gbpe, gbp_endpoint_group_pool,
257   {
258     if (!cb(gbpe, ctx))
259       break;
260   });
261   /* *INDENT-ON* */
262 }
263
264 static clib_error_t *
265 gbp_endpoint_group_cli (vlib_main_t * vm,
266                         unformat_input_t * input, vlib_cli_command_t * cmd)
267 {
268   vnet_main_t *vnm = vnet_get_main ();
269   epg_id_t epg_id = EPG_INVALID;
270   u32 uplink_sw_if_index = ~0;
271   u32 bd_id = ~0;
272   u32 rd_id = ~0;
273   u8 add = 1;
274
275   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
276     {
277       if (unformat (input, "%U", unformat_vnet_sw_interface,
278                     vnm, &uplink_sw_if_index))
279         ;
280       else if (unformat (input, "add"))
281         add = 1;
282       else if (unformat (input, "del"))
283         add = 0;
284       else if (unformat (input, "epg %d", &epg_id))
285         ;
286       else if (unformat (input, "bd %d", &bd_id))
287         ;
288       else if (unformat (input, "rd %d", &rd_id))
289         ;
290       else
291         break;
292     }
293
294   if (EPG_INVALID == epg_id)
295     return clib_error_return (0, "EPG-ID must be specified");
296
297   if (add)
298     {
299       if (~0 == uplink_sw_if_index)
300         return clib_error_return (0, "interface must be specified");
301       if (~0 == bd_id)
302         return clib_error_return (0, "Bridge-domain must be specified");
303       if (~0 == rd_id)
304         return clib_error_return (0, "route-domain must be specified");
305
306       gbp_endpoint_group_add_and_lock (epg_id, bd_id, rd_id,
307                                        uplink_sw_if_index);
308     }
309   else
310     gbp_endpoint_group_delete (epg_id);
311
312   return (NULL);
313 }
314
315 /*?
316  * Configure a GBP Endpoint Group
317  *
318  * @cliexpar
319  * @cliexstart{set gbp endpoint-group [del] epg <ID> bd <ID> <interface>}
320  * @cliexend
321  ?*/
322 /* *INDENT-OFF* */
323 VLIB_CLI_COMMAND (gbp_endpoint_group_cli_node, static) = {
324   .path = "gbp endpoint-group",
325   .short_help = "gbp endpoint-group [del] epg <ID> bd <ID> rd <ID> <interface>",
326   .function = gbp_endpoint_group_cli,
327 };
328
329 u8 *
330 format_gbp_endpoint_group (u8 * s, va_list * args)
331 {
332   gbp_endpoint_group_t *gg = va_arg (*args, gbp_endpoint_group_t*);
333   vnet_main_t *vnm = vnet_get_main ();
334
335   if (NULL != gg)
336     s = format (s, "%d, bd:[%d,%d], rd:[%d] uplink:%U locks:%d",
337                 gg->gg_id,
338                 gbp_endpoint_group_get_bd_id(gg), gg->gg_bd_index,
339                 gg->gg_rd,
340                 format_vnet_sw_if_index_name, vnm, gg->gg_uplink_sw_if_index,
341                 gg->gg_locks);
342   else
343     s = format (s, "NULL");
344
345   return (s);
346 }
347
348 static int
349 gbp_endpoint_group_show_one (gbp_endpoint_group_t *gg, void *ctx)
350 {
351   vlib_main_t *vm;
352
353   vm = ctx;
354   vlib_cli_output (vm, "  %U",format_gbp_endpoint_group, gg);
355
356   return (1);
357 }
358
359 static clib_error_t *
360 gbp_endpoint_group_show (vlib_main_t * vm,
361                    unformat_input_t * input, vlib_cli_command_t * cmd)
362 {
363   vlib_cli_output (vm, "Endpoint-Groups:");
364   gbp_endpoint_group_walk (gbp_endpoint_group_show_one, vm);
365
366   return (NULL);
367 }
368
369
370 /*?
371  * Show Group Based Policy Endpoint_Groups and derived information
372  *
373  * @cliexpar
374  * @cliexstart{show gbp endpoint_group}
375  * @cliexend
376  ?*/
377 /* *INDENT-OFF* */
378 VLIB_CLI_COMMAND (gbp_endpoint_group_show_node, static) = {
379   .path = "show gbp endpoint-group",
380   .short_help = "show gbp endpoint-group\n",
381   .function = gbp_endpoint_group_show,
382 };
383 /* *INDENT-ON* */
384
385 static clib_error_t *
386 gbp_endpoint_group_init (vlib_main_t * vm)
387 {
388   gg_logger = vlib_log_register_class ("gbp", "epg");
389
390   return (NULL);
391 }
392
393 VLIB_INIT_FUNCTION (gbp_endpoint_group_init);
394
395 /*
396  * fd.io coding-style-patch-verification: ON
397  *
398  * Local Variables:
399  * eval: (c-set-style "gnu")
400  * End:
401  */