gbp: add anonymous l3-out external interfaces
[vpp.git] / src / plugins / gbp / gbp_ext_itf.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_ext_itf.h>
17 #include <plugins/gbp/gbp_bridge_domain.h>
18 #include <plugins/gbp/gbp_route_domain.h>
19 #include <plugins/gbp/gbp_itf.h>
20
21 /**
22  * Pool of GBP ext_itfs
23  */
24 gbp_ext_itf_t *gbp_ext_itf_pool;
25
26 /**
27  * external interface configs keyed by sw_if_index
28  */
29 index_t *gbp_ext_itf_db;
30
31 #define GBP_EXT_ITF_ID 0x00000080
32
33 /**
34  * logger
35  */
36 vlib_log_class_t gx_logger;
37
38 #define GBP_EXT_ITF_DBG(...)                           \
39     vlib_log_debug (gx_logger, __VA_ARGS__);
40
41 u8 *
42 format_gbp_ext_itf (u8 * s, va_list * args)
43 {
44   gbp_ext_itf_t *gx = va_arg (*args, gbp_ext_itf_t *);
45
46   return (format (s, "%U in %U",
47                   format_gbp_itf, gx->gx_itf,
48                   format_gbp_bridge_domain, gx->gx_bd));
49 }
50
51 int
52 gbp_ext_itf_add (u32 sw_if_index, u32 bd_id, u32 rd_id)
53 {
54   gbp_ext_itf_t *gx;
55   index_t gxi;
56
57   vec_validate_init_empty (gbp_ext_itf_db, sw_if_index, INDEX_INVALID);
58
59   gxi = gbp_ext_itf_db[sw_if_index];
60
61   if (INDEX_INVALID == gxi)
62     {
63       gbp_route_domain_t *gr;
64       fib_protocol_t fproto;
65       index_t gbi, gri;
66
67       gbi = gbp_bridge_domain_find_and_lock (bd_id);
68
69       if (INDEX_INVALID == gbi)
70         return (VNET_API_ERROR_NO_SUCH_ENTRY);
71
72       gri = gbp_route_domain_find_and_lock (rd_id);
73
74       if (INDEX_INVALID == gri)
75         {
76           gbp_bridge_domain_unlock (gbi);
77           return (VNET_API_ERROR_NO_SUCH_ENTRY);
78         }
79
80       pool_get_zero (gbp_ext_itf_pool, gx);
81       gxi = gx - gbp_ext_itf_pool;
82
83       gr = gbp_route_domain_get (gri);
84
85       gx->gx_bd = gbi;
86       gx->gx_rd = gri;
87       gx->gx_itf = sw_if_index;
88
89       FOR_EACH_FIB_IP_PROTOCOL (fproto)
90       {
91         gx->gx_fib_index[fproto] =
92           gr->grd_fib_index[fib_proto_to_dpo (fproto)];
93       }
94
95       gbp_ext_itf_db[sw_if_index] = gxi;
96
97       GBP_EXT_ITF_DBG ("add: %U", format_gbp_ext_itf, gx);
98
99       return (0);
100     }
101
102   return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
103 }
104
105 int
106 gbp_ext_itf_delete (u32 sw_if_index)
107 {
108   gbp_ext_itf_t *gx;
109   index_t gxi;
110
111   if (vec_len (gbp_ext_itf_db) <= sw_if_index)
112     return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
113
114   gxi = gbp_ext_itf_db[sw_if_index];
115
116   if (INDEX_INVALID != gxi)
117     {
118       gx = pool_elt_at_index (gbp_ext_itf_pool, gxi);
119
120       GBP_EXT_ITF_DBG ("del: %U", format_gbp_ext_itf, gx);
121
122       gbp_route_domain_unlock (gx->gx_rd);
123       gbp_bridge_domain_unlock (gx->gx_bd);
124
125       gbp_ext_itf_db[sw_if_index] = INDEX_INVALID;
126       pool_put (gbp_ext_itf_pool, gx);
127
128       return (0);
129     }
130   return (VNET_API_ERROR_NO_SUCH_ENTRY);
131 }
132
133 int
134 gbp_ext_itf_anon_add (u32 sw_if_index, u32 bd_id, u32 rd_id)
135 {
136   int rv = gbp_ext_itf_add (sw_if_index, bd_id, rd_id);
137   if (rv)
138     return rv;
139   /* add interface to the BD */
140   index_t itf = gbp_itf_add_and_lock (sw_if_index, bd_id);
141   /* setup GBP L2 features on this interface */
142   gbp_itf_set_l2_input_feature (itf, 0,
143                                 L2INPUT_FEAT_GBP_LPM_ANON_CLASSIFY |
144                                 L2INPUT_FEAT_LEARN);
145   gbp_itf_set_l2_output_feature (itf, 0, L2OUTPUT_FEAT_GBP_POLICY_LPM);
146   return 0;
147 }
148
149 int
150 gbp_ext_itf_anon_delete (u32 sw_if_index)
151 {
152   int rv = gbp_ext_itf_delete (sw_if_index);
153   if (rv)
154     return rv;
155   gbp_itf_unlock (sw_if_index);
156   return 0;
157 }
158
159 static clib_error_t *
160 gbp_ext_itf_add_del_cli (vlib_main_t * vm,
161                          unformat_input_t * input, vlib_cli_command_t * cmd)
162 {
163   unformat_input_t _line_input, *line_input = &_line_input;
164   u32 sw_if_index = ~0, bd_id = ~0, rd_id = ~0;
165   int add = 1, anon = 0;
166   int rv;
167
168   /* Get a line of input. */
169   if (!unformat_user (input, unformat_line_input, line_input))
170     return 0;
171
172   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
173     {
174       if (unformat (line_input, "del"))
175         add = 0;
176       else
177         if (unformat
178             (line_input, "%U", unformat_vnet_sw_interface, vnet_get_main (),
179              &sw_if_index))
180         ;
181       else if (unformat (line_input, "bd %d", &bd_id))
182         ;
183       else if (unformat (line_input, "rd %d", &rd_id))
184         ;
185       else if (unformat (line_input, "anon-l3-out"))
186         anon = 1;
187       else
188         return clib_error_return (0, "unknown input `%U'",
189                                   format_unformat_error, line_input);
190     }
191   unformat_free (line_input);
192
193   if (~0 == sw_if_index)
194     return clib_error_return (0, "interface must be specified");
195
196   if (add)
197     {
198       if (~0 == bd_id)
199         return clib_error_return (0, "BD-ID must be specified");
200       if (~0 == rd_id)
201         return clib_error_return (0, "RD-ID must be specified");
202       if (anon)
203         rv = gbp_ext_itf_anon_add (sw_if_index, bd_id, rd_id);
204       else
205         rv = gbp_ext_itf_add (sw_if_index, bd_id, rd_id);
206     }
207   else
208     {
209       if (anon)
210         rv = gbp_ext_itf_anon_delete (sw_if_index);
211       else
212         rv = gbp_ext_itf_delete (sw_if_index);
213     }
214
215   switch (rv)
216     {
217     case 0:
218       return 0;
219     case VNET_API_ERROR_ENTRY_ALREADY_EXISTS:
220       return clib_error_return (0, "interface already exists");
221     case VNET_API_ERROR_NO_SUCH_ENTRY:  /* fallthrough */
222     case VNET_API_ERROR_INVALID_SW_IF_INDEX:
223       return clib_error_return (0, "unknown interface");
224     default:
225       return clib_error_return (0, "error %d", rv);
226     }
227
228   /* never reached */
229   return 0;
230 }
231
232 /*?
233  * Add Group Based Interface as anonymous L3out interface
234  *
235  * @cliexpar
236  * @cliexstart{gbp interface [del] anon-l3out <interface> bd <ID>}
237  * @cliexend
238  ?*/
239 /* *INDENT-OFF* */
240 VLIB_CLI_COMMAND (gbp_itf_anon_l3out_add_del_node, static) = {
241   .path = "gbp ext-itf",
242   .short_help = "gbp ext-itf [del] <interface> bd <ID> rd <ID> [anon-l3-out]\n",
243   .function = gbp_ext_itf_add_del_cli,
244 };
245 /* *INDENT-ON* */
246
247 void
248 gbp_ext_itf_walk (gbp_ext_itf_cb_t cb, void *ctx)
249 {
250   gbp_ext_itf_t *ge;
251
252   /* *INDENT-OFF* */
253   pool_foreach(ge, gbp_ext_itf_pool,
254   {
255     if (!cb(ge, ctx))
256       break;
257   });
258   /* *INDENT-ON* */
259 }
260
261 static walk_rc_t
262 gbp_ext_itf_show_one (gbp_ext_itf_t * gx, void *ctx)
263 {
264   vlib_cli_output (ctx, "  %U", format_gbp_ext_itf, gx);
265
266   return (WALK_CONTINUE);
267 }
268
269 static clib_error_t *
270 gbp_ext_itf_show (vlib_main_t * vm,
271                   unformat_input_t * input, vlib_cli_command_t * cmd)
272 {
273   vlib_cli_output (vm, "External-Interfaces:");
274   gbp_ext_itf_walk (gbp_ext_itf_show_one, vm);
275
276   return (NULL);
277 }
278
279 /*?
280  * Show Group Based Policy external interface and derived information
281  *
282  * @cliexpar
283  * @cliexstart{show gbp ext-itf}
284  * @cliexend
285  ?*/
286 /* *INDENT-OFF* */
287 VLIB_CLI_COMMAND (gbp_ext_itf_show_node, static) = {
288   .path = "show gbp ext-itf",
289   .short_help = "show gbp ext-itf\n",
290   .function = gbp_ext_itf_show,
291 };
292 /* *INDENT-ON* */
293
294 static clib_error_t *
295 gbp_ext_itf_init (vlib_main_t * vm)
296 {
297   gx_logger = vlib_log_register_class ("gbp", "ext-itf");
298
299   return (NULL);
300 }
301
302 VLIB_INIT_FUNCTION (gbp_ext_itf_init);
303
304 /*
305  * fd.io coding-style-patch-verification: ON
306  *
307  * Local Variables:
308  * eval: (c-set-style "gnu")
309  * End:
310  */