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