GBP: l3-out subnets
[vpp.git] / src / plugins / gbp / gbp_recirc.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_recirc.h>
17 #include <plugins/gbp/gbp_endpoint_group.h>
18 #include <plugins/gbp/gbp_endpoint.h>
19 #include <plugins/gbp/gbp_itf.h>
20
21 #include <vnet/dpo/dvr_dpo.h>
22 #include <vnet/fib/fib_table.h>
23
24 #include <vlib/unix/plugin.h>
25
26 /**
27  * Pool of GBP recircs
28  */
29 gbp_recirc_t *gbp_recirc_pool;
30
31 /**
32  * Recirc configs keyed by sw_if_index
33  */
34 index_t *gbp_recirc_db;
35
36 /**
37  * logger
38  */
39 vlib_log_class_t gr_logger;
40
41 /**
42  * L2 Emulation enable/disable symbols
43  */
44 static void (*l2e_enable) (u32 sw_if_index);
45 static void (*l2e_disable) (u32 sw_if_index);
46
47 #define GBP_RECIRC_DBG(...)                           \
48     vlib_log_debug (gr_logger, __VA_ARGS__);
49
50 u8 *
51 format_gbp_recirc (u8 * s, va_list * args)
52 {
53   gbp_recirc_t *gr = va_arg (*args, gbp_recirc_t *);
54   vnet_main_t *vnm = vnet_get_main ();
55
56   return format (s, "  %U, epg:%d, ext:%d",
57                  format_vnet_sw_if_index_name, vnm,
58                  gr->gr_sw_if_index, gr->gr_epg, gr->gr_is_ext);
59 }
60
61 int
62 gbp_recirc_add (u32 sw_if_index, epg_id_t epg_id, u8 is_ext)
63 {
64   gbp_recirc_t *gr;
65   index_t gri;
66
67   vec_validate_init_empty (gbp_recirc_db, sw_if_index, INDEX_INVALID);
68
69   gri = gbp_recirc_db[sw_if_index];
70
71   if (INDEX_INVALID == gri)
72     {
73       gbp_endpoint_group_t *gg;
74       fib_protocol_t fproto;
75       index_t ggi;
76
77       ggi = gbp_endpoint_group_find (epg_id);
78
79       if (INDEX_INVALID == ggi)
80         return (VNET_API_ERROR_NO_SUCH_ENTRY);
81
82       gbp_endpoint_group_lock (ggi);
83       pool_get_zero (gbp_recirc_pool, gr);
84       gri = gr - gbp_recirc_pool;
85
86       gr->gr_epg = epg_id;
87       gr->gr_is_ext = is_ext;
88       gr->gr_sw_if_index = sw_if_index;
89
90       /*
91        * IP enable the recirc interface
92        */
93       ip4_sw_interface_enable_disable (gr->gr_sw_if_index, 1);
94       ip6_sw_interface_enable_disable (gr->gr_sw_if_index, 1);
95
96       /*
97        * cache the FIB indicies of the EPG
98        */
99       gr->gr_epgi = ggi;
100
101       gg = gbp_endpoint_group_get (gr->gr_epgi);
102       FOR_EACH_FIB_IP_PROTOCOL (fproto)
103       {
104         gr->gr_fib_index[fib_proto_to_dpo (fproto)] =
105           gbp_endpoint_group_get_fib_index (gg, fproto);
106       }
107
108       /*
109        * bind to the bridge-domain of the EPG
110        */
111       gr->gr_itf = gbp_itf_add_and_lock (gr->gr_sw_if_index, gg->gg_bd_index);
112
113       /*
114        * set the interface into L2 emulation mode
115        */
116       l2e_enable (gr->gr_sw_if_index);
117
118       /*
119        * Packets on the recirculation interface are subject to src-EPG
120        * classification. Recirc interfaces are L2-emulation mode.
121        *   for internal EPGs this is via an LPM on all external subnets.
122        *   for external EPGs this is via a port mapping.
123        */
124       if (gr->gr_is_ext)
125         {
126           mac_address_t mac;
127           /*
128            * recirc is for post-NAT translation packets going into
129            * the external EPG, these are classified to the NAT EPG
130            * based on its port
131            */
132           mac_address_from_bytes (&mac,
133                                   vnet_sw_interface_get_hw_address
134                                   (vnet_get_main (), gr->gr_sw_if_index));
135           gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_CP,
136                                         gr->gr_sw_if_index,
137                                         NULL, &mac, INDEX_INVALID,
138                                         INDEX_INVALID, gr->gr_epg,
139                                         GBP_ENDPOINT_FLAG_NONE,
140                                         NULL, NULL, &gr->gr_ep);
141           vnet_feature_enable_disable ("ip4-unicast",
142                                        "ip4-gbp-src-classify",
143                                        gr->gr_sw_if_index, 1, 0, 0);
144           vnet_feature_enable_disable ("ip6-unicast",
145                                        "ip6-gbp-src-classify",
146                                        gr->gr_sw_if_index, 1, 0, 0);
147         }
148       else
149         {
150           /*
151            * recirc is for pre-NAT translation packets coming from
152            * the external EPG, these are classified based on a LPM
153            * in the EPG's route-domain
154            */
155           vnet_feature_enable_disable ("ip4-unicast",
156                                        "ip4-gbp-lpm-classify",
157                                        gr->gr_sw_if_index, 1, 0, 0);
158           vnet_feature_enable_disable ("ip6-unicast",
159                                        "ip6-gbp-lpm-classify",
160                                        gr->gr_sw_if_index, 1, 0, 0);
161         }
162
163       gbp_recirc_db[sw_if_index] = gri;
164     }
165   else
166     {
167       gr = gbp_recirc_get (gri);
168     }
169
170   GBP_RECIRC_DBG ("add: %U", format_gbp_recirc, gr);
171   return (0);
172 }
173
174 void
175 gbp_recirc_delete (u32 sw_if_index)
176 {
177   gbp_recirc_t *gr;
178   index_t gri;
179
180   gri = gbp_recirc_db[sw_if_index];
181
182   if (INDEX_INVALID != gri)
183     {
184       gr = pool_elt_at_index (gbp_recirc_pool, gri);
185
186       GBP_RECIRC_DBG ("del: %U", format_gbp_recirc, gr);
187
188       if (gr->gr_is_ext)
189         {
190           gbp_endpoint_unlock (GBP_ENDPOINT_SRC_CP, gr->gr_ep);
191           vnet_feature_enable_disable ("ip4-unicast",
192                                        "ip4-gbp-src-classify",
193                                        gr->gr_sw_if_index, 0, 0, 0);
194           vnet_feature_enable_disable ("ip6-unicast",
195                                        "ip6-gbp-src-classify",
196                                        gr->gr_sw_if_index, 0, 0, 0);
197         }
198       else
199         {
200           vnet_feature_enable_disable ("ip4-unicast",
201                                        "ip4-gbp-lpm-classify",
202                                        gr->gr_sw_if_index, 0, 0, 0);
203           vnet_feature_enable_disable ("ip6-unicast",
204                                        "ip6-gbp-lpm-classify",
205                                        gr->gr_sw_if_index, 0, 0, 0);
206         }
207
208       ip4_sw_interface_enable_disable (gr->gr_sw_if_index, 0);
209       ip6_sw_interface_enable_disable (gr->gr_sw_if_index, 0);
210       l2e_disable (gr->gr_sw_if_index);
211
212       gbp_itf_unlock (gr->gr_itf);
213
214       gbp_endpoint_group_unlock (gr->gr_epgi);
215       gbp_recirc_db[sw_if_index] = INDEX_INVALID;
216       pool_put (gbp_recirc_pool, gr);
217     }
218 }
219
220 void
221 gbp_recirc_walk (gbp_recirc_cb_t cb, void *ctx)
222 {
223   gbp_recirc_t *ge;
224
225   /* *INDENT-OFF* */
226   pool_foreach(ge, gbp_recirc_pool,
227   {
228     if (!cb(ge, ctx))
229       break;
230   });
231   /* *INDENT-ON* */
232 }
233
234 static walk_rc_t
235 gbp_recirc_show_one (gbp_recirc_t * gr, void *ctx)
236 {
237   vlib_cli_output (ctx, "  %U", format_gbp_recirc, gr);
238
239   return (WALK_CONTINUE);
240 }
241
242 static clib_error_t *
243 gbp_recirc_show (vlib_main_t * vm,
244                  unformat_input_t * input, vlib_cli_command_t * cmd)
245 {
246   vlib_cli_output (vm, "Recirculation-Interfaces:");
247   gbp_recirc_walk (gbp_recirc_show_one, vm);
248
249   return (NULL);
250 }
251
252 /*?
253  * Show Group Based Policy Recircs and derived information
254  *
255  * @cliexpar
256  * @cliexstart{show gbp recirc}
257  * @cliexend
258  ?*/
259 /* *INDENT-OFF* */
260 VLIB_CLI_COMMAND (gbp_recirc_show_node, static) = {
261   .path = "show gbp recirc",
262   .short_help = "show gbp recirc\n",
263   .function = gbp_recirc_show,
264 };
265 /* *INDENT-ON* */
266
267 static clib_error_t *
268 gbp_recirc_init (vlib_main_t * vm)
269 {
270   gr_logger = vlib_log_register_class ("gbp", "recirc");
271
272   l2e_enable =
273     vlib_get_plugin_symbol ("l2e_plugin.so", "l2_emulation_enable");
274   l2e_disable =
275     vlib_get_plugin_symbol ("l2e_plugin.so", "l2_emulation_disable");
276
277   return (NULL);
278 }
279
280 VLIB_INIT_FUNCTION (gbp_recirc_init);
281
282 /*
283  * fd.io coding-style-patch-verification: ON
284  *
285  * Local Variables:
286  * eval: (c-set-style "gnu")
287  * End:
288  */