GBP: redirect contracts
[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 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 int
70 gbp_endpoint_group_add_and_lock (epg_id_t epg_id,
71                                  u32 bd_id, u32 rd_id, u32 uplink_sw_if_index)
72 {
73   gbp_endpoint_group_t *gg;
74   index_t ggi;
75
76   ggi = gbp_endpoint_group_find (epg_id);
77
78   if (INDEX_INVALID == ggi)
79     {
80       gbp_bridge_domain_t *gb;
81       fib_protocol_t fproto;
82       index_t gbi, grdi;
83
84       gbi = gbp_bridge_domain_find_and_lock (bd_id);
85
86       if (~0 == gbi)
87         return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
88
89       grdi = gbp_route_domain_find_and_lock (rd_id);
90
91       if (~0 == grdi)
92         {
93           gbp_bridge_domain_unlock (gbi);
94           return (VNET_API_ERROR_NO_SUCH_FIB);
95         }
96
97       gb = gbp_bridge_domain_get (gbi);
98
99       pool_get_zero (gbp_endpoint_group_pool, gg);
100
101       gg->gg_id = epg_id;
102       gg->gg_rd = grdi;
103       gg->gg_gbd = gbi;
104       gg->gg_bd_index = gb->gb_bd_index;
105
106       gg->gg_uplink_sw_if_index = uplink_sw_if_index;
107       gg->gg_locks = 1;
108
109       /*
110        * an egress DVR dpo for internal subnets to use when sending
111        * on the uplink interface
112        */
113       if (~0 != gg->gg_uplink_sw_if_index)
114         {
115           FOR_EACH_FIB_IP_PROTOCOL (fproto)
116           {
117             dvr_dpo_add_or_lock (uplink_sw_if_index,
118                                  fib_proto_to_dpo (fproto),
119                                  &gg->gg_dpo[fproto]);
120           }
121
122           /*
123            * Add the uplink to the BD
124            * packets direct from the uplink have had policy applied
125            */
126           set_int_l2_mode (vlib_get_main (), vnet_get_main (),
127                            MODE_L2_BRIDGE, gg->gg_uplink_sw_if_index,
128                            gg->gg_bd_index, L2_BD_PORT_TYPE_NORMAL, 0, 0);
129           l2input_intf_bitmap_enable (gg->gg_uplink_sw_if_index,
130                                       L2INPUT_FEAT_GBP_NULL_CLASSIFY, 1);
131         }
132
133       hash_set (gbp_endpoint_group_db.gg_hash,
134                 gg->gg_id, gg - gbp_endpoint_group_pool);
135
136     }
137   else
138     {
139       gg = gbp_endpoint_group_get (ggi);
140       gg->gg_locks++;
141     }
142
143   GBP_EPG_DBG ("add: %U", format_gbp_endpoint_group, gg);
144
145   return (0);
146 }
147
148 void
149 gbp_endpoint_group_unlock (index_t ggi)
150 {
151   gbp_endpoint_group_t *gg;
152
153   if (INDEX_INVALID == ggi)
154     return;
155
156   gg = gbp_endpoint_group_get (ggi);
157
158   gg->gg_locks--;
159
160   if (0 == gg->gg_locks)
161     {
162       fib_protocol_t fproto;
163
164       gg = pool_elt_at_index (gbp_endpoint_group_pool, ggi);
165
166       if (~0 != gg->gg_uplink_sw_if_index)
167         {
168           set_int_l2_mode (vlib_get_main (), vnet_get_main (),
169                            MODE_L3, gg->gg_uplink_sw_if_index,
170                            gg->gg_bd_index, L2_BD_PORT_TYPE_NORMAL, 0, 0);
171
172           l2input_intf_bitmap_enable (gg->gg_uplink_sw_if_index,
173                                       L2INPUT_FEAT_GBP_NULL_CLASSIFY, 0);
174         }
175       FOR_EACH_FIB_IP_PROTOCOL (fproto)
176       {
177         dpo_reset (&gg->gg_dpo[fproto]);
178       }
179       gbp_bridge_domain_unlock (gg->gg_gbd);
180       gbp_route_domain_unlock (gg->gg_rd);
181
182       hash_unset (gbp_endpoint_group_db.gg_hash, gg->gg_id);
183
184       pool_put (gbp_endpoint_group_pool, gg);
185     }
186 }
187
188 int
189 gbp_endpoint_group_delete (epg_id_t epg_id)
190 {
191   index_t ggi;
192
193   ggi = gbp_endpoint_group_find (epg_id);
194
195   if (INDEX_INVALID != ggi)
196     {
197       GBP_EPG_DBG ("del: %U", format_gbp_endpoint_group,
198                    gbp_endpoint_group_get (ggi));
199       gbp_endpoint_group_unlock (ggi);
200
201       return (0);
202     }
203
204   return (VNET_API_ERROR_NO_SUCH_ENTRY);
205 }
206
207 u32
208 gbp_endpoint_group_get_bd_id (const gbp_endpoint_group_t * gg)
209 {
210   const gbp_bridge_domain_t *gb;
211
212   gb = gbp_bridge_domain_get (gg->gg_gbd);
213
214   return (gb->gb_bd_id);
215 }
216
217 index_t
218 gbp_endpoint_group_get_fib_index (const gbp_endpoint_group_t * gg,
219                                   fib_protocol_t fproto)
220 {
221   const gbp_route_domain_t *grd;
222
223   grd = gbp_route_domain_get (gg->gg_rd);
224
225   return (grd->grd_fib_index[fproto]);
226 }
227
228 void
229 gbp_endpoint_group_walk (gbp_endpoint_group_cb_t cb, void *ctx)
230 {
231   gbp_endpoint_group_t *gbpe;
232
233   /* *INDENT-OFF* */
234   pool_foreach(gbpe, gbp_endpoint_group_pool,
235   {
236     if (!cb(gbpe, ctx))
237       break;
238   });
239   /* *INDENT-ON* */
240 }
241
242 static clib_error_t *
243 gbp_endpoint_group_cli (vlib_main_t * vm,
244                         unformat_input_t * input, vlib_cli_command_t * cmd)
245 {
246   vnet_main_t *vnm = vnet_get_main ();
247   epg_id_t epg_id = EPG_INVALID;
248   u32 uplink_sw_if_index = ~0;
249   u32 bd_id = ~0;
250   u32 rd_id = ~0;
251   u8 add = 1;
252
253   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
254     {
255       if (unformat (input, "%U", unformat_vnet_sw_interface,
256                     vnm, &uplink_sw_if_index))
257         ;
258       else if (unformat (input, "add"))
259         add = 1;
260       else if (unformat (input, "del"))
261         add = 0;
262       else if (unformat (input, "epg %d", &epg_id))
263         ;
264       else if (unformat (input, "bd %d", &bd_id))
265         ;
266       else if (unformat (input, "rd %d", &rd_id))
267         ;
268       else
269         break;
270     }
271
272   if (EPG_INVALID == epg_id)
273     return clib_error_return (0, "EPG-ID must be specified");
274
275   if (add)
276     {
277       if (~0 == uplink_sw_if_index)
278         return clib_error_return (0, "interface must be specified");
279       if (~0 == bd_id)
280         return clib_error_return (0, "Bridge-domain must be specified");
281       if (~0 == rd_id)
282         return clib_error_return (0, "route-domain must be specified");
283
284       gbp_endpoint_group_add_and_lock (epg_id, bd_id, rd_id,
285                                        uplink_sw_if_index);
286     }
287   else
288     gbp_endpoint_group_delete (epg_id);
289
290   return (NULL);
291 }
292
293 /*?
294  * Configure a GBP Endpoint Group
295  *
296  * @cliexpar
297  * @cliexstart{set gbp endpoint-group [del] epg <ID> bd <ID> <interface>}
298  * @cliexend
299  ?*/
300 /* *INDENT-OFF* */
301 VLIB_CLI_COMMAND (gbp_endpoint_group_cli_node, static) = {
302   .path = "gbp endpoint-group",
303   .short_help = "gbp endpoint-group [del] epg <ID> bd <ID> rd <ID> <interface>",
304   .function = gbp_endpoint_group_cli,
305 };
306
307 u8 *
308 format_gbp_endpoint_group (u8 * s, va_list * args)
309 {
310   gbp_endpoint_group_t *gg = va_arg (*args, gbp_endpoint_group_t*);
311   vnet_main_t *vnm = vnet_get_main ();
312
313   if (NULL != gg)
314     s = format (s, "%d, bd:[%d,%d], rd:[%d] uplink:%U locks:%d",
315                 gg->gg_id,
316                 gbp_endpoint_group_get_bd_id(gg), gg->gg_bd_index,
317                 gg->gg_rd,
318                 format_vnet_sw_if_index_name, vnm, gg->gg_uplink_sw_if_index,
319                 gg->gg_locks);
320   else
321     s = format (s, "NULL");
322
323   return (s);
324 }
325
326 static int
327 gbp_endpoint_group_show_one (gbp_endpoint_group_t *gg, void *ctx)
328 {
329   vlib_main_t *vm;
330
331   vm = ctx;
332   vlib_cli_output (vm, "  %U",format_gbp_endpoint_group, gg);
333
334   return (1);
335 }
336
337 static clib_error_t *
338 gbp_endpoint_group_show (vlib_main_t * vm,
339                    unformat_input_t * input, vlib_cli_command_t * cmd)
340 {
341   vlib_cli_output (vm, "Endpoint-Groups:");
342   gbp_endpoint_group_walk (gbp_endpoint_group_show_one, vm);
343
344   return (NULL);
345 }
346
347
348 /*?
349  * Show Group Based Policy Endpoint_Groups and derived information
350  *
351  * @cliexpar
352  * @cliexstart{show gbp endpoint_group}
353  * @cliexend
354  ?*/
355 /* *INDENT-OFF* */
356 VLIB_CLI_COMMAND (gbp_endpoint_group_show_node, static) = {
357   .path = "show gbp endpoint-group",
358   .short_help = "show gbp endpoint-group\n",
359   .function = gbp_endpoint_group_show,
360 };
361 /* *INDENT-ON* */
362
363 static clib_error_t *
364 gbp_endpoint_group_init (vlib_main_t * vm)
365 {
366   gg_logger = vlib_log_register_class ("gbp", "epg");
367
368   return (NULL);
369 }
370
371 VLIB_INIT_FUNCTION (gbp_endpoint_group_init);
372
373 /*
374  * fd.io coding-style-patch-verification: ON
375  *
376  * Local Variables:
377  * eval: (c-set-style "gnu")
378  * End:
379  */