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