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