FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
gbp_ext_itf.c
Go to the documentation of this file.
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 
19 #include <plugins/gbp/gbp_itf.h>
20 
21 /**
22  * Pool of GBP ext_itfs
23  */
25 
26 /**
27  * external interface configs keyed by sw_if_index
28  */
30 
31 #define GBP_EXT_ITF_ID 0x00000080
32 
33 /**
34  * logger
35  */
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",
48  (gx->gx_flags & GBP_EXT_ITF_F_ANON) ? " [anon]" : "",
50 }
51 
52 int
54 {
55  gbp_ext_itf_t *gx;
56  index_t gxi;
57 
59 
61 
62  if (INDEX_INVALID == gxi)
63  {
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  {
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;
89 
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  gx->gx_itf = gbp_itf_l2_add_and_lock (sw_if_index, gbi);
100 
101  /* setup GBP L2 features on this interface */
103  L2INPUT_FEAT_GBP_LPM_ANON_CLASSIFY |
104  L2INPUT_FEAT_LEARN);
106  L2OUTPUT_FEAT_GBP_POLICY_LPM);
107  }
108 
109  gx->gx_flags = flags;
110 
112 
113  GBP_EXT_ITF_DBG ("add: %U", format_gbp_ext_itf, gx);
114 
115  return (0);
116  }
117 
118  return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
119 }
120 
121 int
123 {
124  gbp_ext_itf_t *gx;
125  index_t gxi;
126 
127  if (vec_len (gbp_ext_itf_db) <= sw_if_index)
128  return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
129 
131 
132  if (INDEX_INVALID != gxi)
133  {
134  gx = pool_elt_at_index (gbp_ext_itf_pool, gxi);
135 
136  GBP_EXT_ITF_DBG ("del: %U", format_gbp_ext_itf, gx);
137 
138  gbp_itf_unlock (&gx->gx_itf);
141 
143  pool_put (gbp_ext_itf_pool, gx);
144 
145  return (0);
146  }
147  return (VNET_API_ERROR_NO_SUCH_ENTRY);
148 }
149 
150 static clib_error_t *
152  unformat_input_t * input, vlib_cli_command_t * cmd)
153 {
154  unformat_input_t _line_input, *line_input = &_line_input;
155  u32 sw_if_index = ~0, bd_id = ~0, rd_id = ~0, flags = 0;
156  int add = 1;
157  int rv;
158 
159  /* Get a line of input. */
160  if (!unformat_user (input, unformat_line_input, line_input))
161  return 0;
162 
163  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
164  {
165  if (unformat (line_input, "del"))
166  add = 0;
167  else
168  if (unformat
169  (line_input, "%U", unformat_vnet_sw_interface, vnet_get_main (),
170  &sw_if_index))
171  ;
172  else if (unformat (line_input, "bd %d", &bd_id))
173  ;
174  else if (unformat (line_input, "rd %d", &rd_id))
175  ;
176  else if (unformat (line_input, "anon-l3-out"))
178  else
179  return clib_error_return (0, "unknown input `%U'",
180  format_unformat_error, line_input);
181  }
182  unformat_free (line_input);
183 
184  if (~0 == sw_if_index)
185  return clib_error_return (0, "interface must be specified");
186 
187  if (add)
188  {
189  if (~0 == bd_id)
190  return clib_error_return (0, "BD-ID must be specified");
191  if (~0 == rd_id)
192  return clib_error_return (0, "RD-ID must be specified");
193  rv = gbp_ext_itf_add (sw_if_index, bd_id, rd_id, flags);
194  }
195  else
196  rv = gbp_ext_itf_delete (sw_if_index);
197 
198  switch (rv)
199  {
200  case 0:
201  return 0;
202  case VNET_API_ERROR_ENTRY_ALREADY_EXISTS:
203  return clib_error_return (0, "interface already exists");
204  case VNET_API_ERROR_NO_SUCH_ENTRY: /* fallthrough */
205  case VNET_API_ERROR_INVALID_SW_IF_INDEX:
206  return clib_error_return (0, "unknown interface");
207  default:
208  return clib_error_return (0, "error %d", rv);
209  }
210 
211  /* never reached */
212  return 0;
213 }
214 
215 /*?
216  * Add Group Based Interface as anonymous L3out interface
217  *
218  * @cliexpar
219  * @cliexstart{gbp interface [del] anon-l3out <interface> bd <ID>}
220  * @cliexend
221  ?*/
222 /* *INDENT-OFF* */
223 VLIB_CLI_COMMAND (gbp_itf_anon_l3out_add_del_node, static) = {
224  .path = "gbp ext-itf",
225  .short_help = "gbp ext-itf [del] <interface> bd <ID> rd <ID> [anon-l3-out]\n",
226  .function = gbp_ext_itf_add_del_cli,
227 };
228 /* *INDENT-ON* */
229 
230 void
232 {
233  gbp_ext_itf_t *ge;
234 
235  /* *INDENT-OFF* */
236  pool_foreach(ge, gbp_ext_itf_pool,
237  {
238  if (!cb(ge, ctx))
239  break;
240  });
241  /* *INDENT-ON* */
242 }
243 
244 static walk_rc_t
246 {
247  vlib_cli_output (ctx, " %U", format_gbp_ext_itf, gx);
248 
249  return (WALK_CONTINUE);
250 }
251 
252 static clib_error_t *
254  unformat_input_t * input, vlib_cli_command_t * cmd)
255 {
256  vlib_cli_output (vm, "External-Interfaces:");
258 
259  return (NULL);
260 }
261 
262 /*?
263  * Show Group Based Policy external interface and derived information
264  *
265  * @cliexpar
266  * @cliexstart{show gbp ext-itf}
267  * @cliexend
268  ?*/
269 /* *INDENT-OFF* */
270 VLIB_CLI_COMMAND (gbp_ext_itf_show_node, static) = {
271  .path = "show gbp ext-itf",
272  .short_help = "show gbp ext-itf\n",
273  .function = gbp_ext_itf_show,
274 };
275 /* *INDENT-ON* */
276 
277 static clib_error_t *
279 {
280  gx_logger = vlib_log_register_class ("gbp", "ext-itf");
281 
282  return (NULL);
283 }
284 
286 
287 /*
288  * fd.io coding-style-patch-verification: ON
289  *
290  * Local Variables:
291  * eval: (c-set-style "gnu")
292  * End:
293  */
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
Definition: log.c:176
void gbp_route_domain_unlock(index_t index)
u32 flags
Definition: vhost_user.h:141
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define pool_get_zero(P, E)
Allocate an object E from a pool P and zero it.
Definition: pool.h:239
index_t * gbp_ext_itf_db
external interface configs keyed by sw_if_index
Definition: gbp_ext_itf.c:29
void gbp_itf_l2_set_output_feature(gbp_itf_hdl_t gh, l2output_feat_masks_t feats)
Definition: gbp_itf.c:428
gbp_ext_itf_t * gbp_ext_itf_pool
Pool of GBP ext_itfs.
Definition: gbp_ext_itf.c:24
void gbp_itf_unlock(gbp_itf_hdl_t *gh)
Definition: gbp_itf.c:286
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:41
static walk_rc_t gbp_ext_itf_show_one(gbp_ext_itf_t *gx, void *ctx)
Definition: gbp_ext_itf.c:245
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
u32 rd_id
Definition: gbp.api:34
static clib_error_t * gbp_ext_itf_show(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: gbp_ext_itf.c:253
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
unformat_function_t unformat_vnet_sw_interface
gbp_itf_hdl_t gx_itf
The interface.
Definition: gbp_ext_itf.h:38
unsigned char u8
Definition: types.h:56
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
enum walk_rc_t_ walk_rc_t
Walk return code.
void gbp_bridge_domain_unlock(index_t gbdi)
u32 vlib_log_class_t
Definition: vlib.h:51
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
static clib_error_t * gbp_ext_itf_add_del_cli(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: gbp_ext_itf.c:151
walk_rc_t(* gbp_ext_itf_cb_t)(gbp_ext_itf_t *gbpe, void *ctx)
Definition: gbp_ext_itf.h:68
u32 gx_fib_index[DPO_PROTO_NUM]
cached FIB indices from the RD
Definition: gbp_ext_itf.h:53
#define clib_error_return(e, args...)
Definition: error.h:99
unsigned int u32
Definition: types.h:88
index_t gx_bd
The BD this external interface is a member of.
Definition: gbp_ext_itf.h:43
unformat_function_t unformat_line_input
Definition: format.h:283
int gbp_ext_itf_delete(u32 sw_if_index)
Definition: gbp_ext_itf.c:122
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
long ctx[MAX_CONNS]
Definition: main.c:144
u32 bd_id
Definition: gbp.api:274
struct _unformat_input_t unformat_input_t
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
An external interface maps directly to an oflex L3ExternalInterface.
Definition: gbp_ext_itf.h:33
void gbp_itf_hdl_reset(gbp_itf_hdl_t *gh)
Definition: gbp_itf.c:108
static clib_error_t * gbp_ext_itf_init(vlib_main_t *vm)
Definition: gbp_ext_itf.c:278
vlib_log_class_t gx_logger
logger
Definition: gbp_ext_itf.c:36
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
vlib_main_t * vm
Definition: buffer.c:323
gbp_itf_hdl_t gbp_itf_l2_add_and_lock(u32 sw_if_index, index_t gbi)
Definition: gbp_itf.c:196
u8 * format_gbp_bridge_domain(u8 *s, va_list *args)
u32 grd_fib_index[FIB_PROTOCOL_IP_MAX]
index_t gx_rd
The RD this external interface is a member of.
Definition: gbp_ext_itf.h:48
index_t gbp_bridge_domain_find_and_lock(u32 bd_id)
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:161
gbp_route_domain_t * gbp_route_domain_get(index_t i)
u8 * format_gbp_itf_hdl(u8 *s, va_list *args)
Definition: gbp_itf.c:520
dpo_proto_t fib_proto_to_dpo(fib_protocol_t fib_proto)
Definition: fib_types.c:247
#define GBP_EXT_ITF_DBG(...)
Definition: gbp_ext_itf.c:38
void gbp_itf_l2_set_input_feature(gbp_itf_hdl_t gh, l2input_feat_masks_t feats)
Definition: gbp_itf.c:382
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
A route Domain Representation.
#define INDEX_INVALID
Invalid index - used when no index is known blazoned capitals INVALID speak volumes where ~0 does not...
Definition: dpo.h:47
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
u32 gx_flags
The associated flags.
Definition: gbp_ext_itf.h:58
void gbp_ext_itf_walk(gbp_ext_itf_cb_t cb, void *ctx)
Definition: gbp_ext_itf.c:231
#define FOR_EACH_FIB_IP_PROTOCOL(_item)
Definition: fib_types.h:70
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
u8 * format_gbp_ext_itf(u8 *s, va_list *args)
Definition: gbp_ext_itf.c:42
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:486
u8 ge
Definition: ip_types.api:135
int gbp_ext_itf_add(u32 sw_if_index, u32 bd_id, u32 rd_id, u32 flags)
Definition: gbp_ext_itf.c:53
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:772
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
index_t gbp_route_domain_find_and_lock(u32 rd_id)
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171