FD.io VPP  v19.04.2-12-g66b1689
Vector Packet Processing
gbp_subnet.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 
16 #include <plugins/gbp/gbp.h>
20 
21 #include <vnet/fib/fib_table.h>
22 #include <vnet/dpo/load_balance.h>
23 
24 /**
25  * a key for the DB
26  */
27 typedef struct gbp_subnet_key_t_
28 {
32 
33 /**
34  * Subnet
35  */
36 typedef struct gbp_subnet_t_
37 {
41 
42  union
43  {
44  struct
45  {
48  } gs_stitched_external;
49  struct
50  {
51  sclass_t gs_sclass;
52  } gs_l3_out;
53  };
54 
56 } gbp_subnet_t;
57 
58 /**
59  * A DB of the subnets; key={pfx,fib-index}
60  */
62 
63 /**
64  * pool of subnets
65  */
67 
68 static index_t
69 gbp_subnet_db_find (u32 fib_index, const fib_prefix_t * pfx)
70 {
72  .gsk_pfx = *pfx,
73  .gsk_fib_index = fib_index,
74  };
75  uword *p;
76 
77  p = hash_get_mem (gbp_subnet_db, &key);
78 
79  if (NULL != p)
80  return p[0];
81 
82  return (INDEX_INVALID);
83 }
84 
85 static void
86 gbp_subnet_db_add (u32 fib_index, const fib_prefix_t * pfx, gbp_subnet_t * gs)
87 {
89 
90  key = clib_mem_alloc (sizeof (*key));
91 
92  clib_memcpy (&(key->gsk_pfx), pfx, sizeof (*pfx));
93  key->gsk_fib_index = fib_index;
94 
95  hash_set_mem (gbp_subnet_db, key, (gs - gbp_subnet_pool));
96 
97  gs->gs_key = key;
98 }
99 
100 static void
102 {
104 
105  clib_mem_free (gs->gs_key);
106  gs->gs_key = NULL;
107 }
108 
109 
110 static int
112 {
113  dpo_id_t gfd = DPO_INVALID;
114  gbp_route_domain_t *grd;
115  fib_protocol_t fproto;
116 
117  fproto = gs->gs_key->gsk_pfx.fp_proto;
118  grd = gbp_route_domain_get (gs->gs_rd);
119 
120  if (~0 == grd->grd_uu_sw_if_index[fproto])
121  return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
122 
124  &gs->gs_key->gsk_pfx,
127  fib_proto_to_dpo (fproto),
129  grd->grd_uu_sw_if_index
130  [fproto], ~0, 1, NULL,
132 
133  dpo_reset (&gfd);
134 
135  return (0);
136 }
137 
138 static int
140 {
141  dpo_id_t gfd = DPO_INVALID;
142 
144  &gfd);
145 
147  &gs->gs_key->gsk_pfx,
150  &gfd);
151 
152  dpo_reset (&gfd);
153 
154  return (0);
155 }
156 
157 static int
159 {
160  dpo_id_t gpd = DPO_INVALID;
161 
162  gs->gs_stitched_external.gs_sclass = sclass;
163  gs->gs_stitched_external.gs_sw_if_index = sw_if_index;
164 
166  gs->gs_stitched_external.gs_sclass,
167  gs->gs_stitched_external.gs_sw_if_index, &gpd);
168 
170  &gs->gs_key->gsk_pfx,
174  &gpd);
175 
176  dpo_reset (&gpd);
177 
178  return (0);
179 }
180 
181 static int
183 {
184  dpo_id_t gpd = DPO_INVALID;
185 
186  gs->gs_l3_out.gs_sclass = sclass;
187 
189  gs->gs_l3_out.gs_sclass, ~0, &gpd);
190 
192  &gs->gs_key->gsk_pfx,
195  &gpd);
196 
197  dpo_reset (&gpd);
198 
199  return (0);
200 }
201 
202 static void
204 {
205  gbp_subnet_t *gs;
206 
207  gs = pool_elt_at_index (gbp_subnet_pool, gsi);
208 
209  if (GBP_SUBNET_L3_OUT == gs->gs_type)
211  else
213 
214  gbp_subnet_db_del (gs);
216 
217  pool_put (gbp_subnet_pool, gs);
218 }
219 
220 int
222 {
223  gbp_route_domain_t *grd;
224  index_t gsi, grdi;
225  u32 fib_index;
226 
227  grdi = gbp_route_domain_find (rd_id);
228 
229  if (~0 == grdi)
230  return (VNET_API_ERROR_NO_SUCH_FIB);
231 
232  grd = gbp_route_domain_get (grdi);
233  fib_index = grd->grd_fib_index[pfx->fp_proto];
234 
235  gsi = gbp_subnet_db_find (fib_index, pfx);
236 
237  if (INDEX_INVALID == gsi)
238  return (VNET_API_ERROR_NO_SUCH_ENTRY);
239 
240  gbp_subnet_del_i (gsi);
241 
242  return (0);
243 }
244 
245 int
247  const fib_prefix_t * pfx,
249 {
250  gbp_route_domain_t *grd;
251  index_t grdi, gsi;
252  gbp_subnet_t *gs;
253  u32 fib_index;
254  int rv;
255 
256  grdi = gbp_route_domain_find_and_lock (rd_id);
257 
258  if (~0 == grdi)
259  return (VNET_API_ERROR_NO_SUCH_FIB);
260 
261  grd = gbp_route_domain_get (grdi);
262  fib_index = grd->grd_fib_index[pfx->fp_proto];
263 
264  gsi = gbp_subnet_db_find (fib_index, pfx);
265 
266  /*
267  * this is an update if the subnet already exists, so remove the old
268  */
269  if (INDEX_INVALID != gsi)
270  gbp_subnet_del_i (gsi);
271 
272  rv = -2;
273 
274  pool_get (gbp_subnet_pool, gs);
275 
276  gs->gs_type = type;
277  gs->gs_rd = grdi;
278  gbp_subnet_db_add (fib_index, pfx, gs);
279 
280  switch (type)
281  {
283  rv = gbp_subnet_internal_add (gs);
284  break;
286  rv = gbp_subnet_external_add (gs, sw_if_index, sclass);
287  break;
289  rv = gbp_subnet_transport_add (gs);
290  break;
291  case GBP_SUBNET_L3_OUT:
292  rv = gbp_subnet_l3_out_add (gs, sw_if_index, sclass);
293  break;
294  }
295 
296  return (rv);
297 }
298 
299 void
301 {
302  gbp_route_domain_t *grd;
303  gbp_subnet_t *gs;
306 
307  sclass = SCLASS_INVALID;
308  sw_if_index = ~0;
309 
310  /* *INDENT-OFF* */
311  pool_foreach (gs, gbp_subnet_pool,
312  ({
313  grd = gbp_route_domain_get(gs->gs_rd);
314 
315  switch (gs->gs_type)
316  {
319  /* use defaults above */
320  break;
322  sw_if_index = gs->gs_stitched_external.gs_sw_if_index;
323  sclass = gs->gs_stitched_external.gs_sclass;
324  break;
325  case GBP_SUBNET_L3_OUT:
326  sclass = gs->gs_l3_out.gs_sclass;
327  break;
328  }
329 
330  if (WALK_STOP == cb (grd->grd_id, &gs->gs_key->gsk_pfx,
331  gs->gs_type, sw_if_index, sclass, ctx))
332  break;
333  }));
334  /* *INDENT-ON* */
335 }
336 
338 {
342 
343 static u8 *
344 format_gbp_subnet_type (u8 * s, va_list * args)
345 {
346  gbp_subnet_type_t type = va_arg (*args, gbp_subnet_type_t);
347 
348  switch (type)
349  {
351  return (format (s, "stitched-internal"));
353  return (format (s, "stitched-external"));
355  return (format (s, "transport"));
356  case GBP_SUBNET_L3_OUT:
357  return (format (s, "l3-out"));
358  }
359 
360  return (format (s, "unknown"));
361 }
362 
363 u8 *
364 format_gbp_subnet (u8 * s, va_list * args)
365 {
366  index_t gsi = va_arg (*args, index_t);
368  gbp_subnet_t *gs;
369  u32 table_id;
370 
371  gs = pool_elt_at_index (gbp_subnet_pool, gsi);
372 
374  gs->gs_key->gsk_pfx.fp_proto);
375 
376  s = format (s, "[%d] tbl:%d %U %U", gsi, table_id,
379 
380  switch (gs->gs_type)
381  {
384  break;
386  s = format (s, " {sclass:%d %U}", gs->gs_stitched_external.gs_sclass,
388  vnet_get_main (), gs->gs_stitched_external.gs_sw_if_index);
389  break;
390  case GBP_SUBNET_L3_OUT:
391  s = format (s, " {sclass:%d}", gs->gs_l3_out.gs_sclass);
392  break;
393  }
394 
395  switch (flags)
396  {
398  {
399  s = format (s, "\n %U", format_fib_entry, gs->gs_fei,
401  }
403  break;
404  }
405  return (s);
406 }
407 
408 static clib_error_t *
410  unformat_input_t * input, vlib_cli_command_t * cmd)
411 {
412  u32 gsi;
413 
414  gsi = INDEX_INVALID;
415 
417  {
418  if (unformat (input, "%d", &gsi))
419  ;
420  else
421  break;
422  }
423 
424  if (INDEX_INVALID != gsi)
425  {
426  vlib_cli_output (vm, "%U", format_gbp_subnet, gsi,
428  }
429  else
430  {
431  /* *INDENT-OFF* */
432  pool_foreach_index(gsi, gbp_subnet_pool,
433  ({
434  vlib_cli_output (vm, "%U", format_gbp_subnet, gsi,
436  }));
437  /* *INDENT-ON* */
438  }
439 
440  return (NULL);
441 }
442 
443 /*?
444  * Show Group Based Policy Subnets
445  *
446  * @cliexpar
447  * @cliexstart{show gbp subnet}
448  * @cliexend
449  ?*/
450 /* *INDENT-OFF* */
451 VLIB_CLI_COMMAND (gbp_subnet_show_node, static) = {
452  .path = "show gbp subnet",
453  .short_help = "show gbp subnet\n",
454  .function = gbp_subnet_show,
455 };
456 /* *INDENT-ON* */
457 
458 static clib_error_t *
460 {
462  sizeof (gbp_subnet_key_t), sizeof (u32));
463 
464  return (NULL);
465 }
466 
468 
469 /*
470  * fd.io coding-style-patch-verification: ON
471  *
472  * Local Variables:
473  * eval: (c-set-style "gnu")
474  * End:
475  */
u32 sw_if_index
Definition: ipsec_gre.api:37
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:212
u8 * format_fib_entry(u8 *s, va_list *args)
Definition: fib_entry.c:139
a key for the DB
Definition: gbp_subnet.c:27
void gbp_subnet_walk(gbp_subnet_cb_t cb, void *ctx)
Definition: gbp_subnet.c:300
void gbp_route_domain_unlock(index_t index)
u32 flags
Definition: vhost_user.h:115
u16 sclass_t
Definition: gbp_types.h:24
fib_prefix_t gsk_pfx
Definition: gbp_subnet.c:29
static int gbp_subnet_l3_out_add(gbp_subnet_t *gs, u32 sw_if_index, sclass_t sclass)
Definition: gbp_subnet.c:182
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
gbp_subnet_key_t * gs_key
Definition: gbp_subnet.c:38
static u8 * format_gbp_subnet_type(u8 *s, va_list *args)
Definition: gbp_subnet.c:344
#define FIB_ENTRY_FORMAT_DETAIL
Definition: fib_entry.h:517
Definition: fib_entry.h:286
#define NULL
Definition: clib.h:58
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
sclass_t gs_sclass
Definition: gbp_subnet.c:46
static void gbp_subnet_db_del(gbp_subnet_t *gs)
Definition: gbp_subnet.c:101
u32 rd_id
Definition: gbp.api:270
#define hash_set_mem(h, key, value)
Definition: hash.h:275
index_t gs_rd
Definition: gbp_subnet.c:40
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
static int gbp_subnet_transport_add(gbp_subnet_t *gs)
Definition: gbp_subnet.c:111
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
int gbp_subnet_del(u32 rd_id, const fib_prefix_t *pfx)
Definition: gbp_subnet.c:221
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
#define clib_memcpy(d, s, n)
Definition: string.h:180
fib_node_index_t fib_table_entry_update_one_path(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source, fib_entry_flag_t flags, dpo_proto_t next_hop_proto, const ip46_address_t *next_hop, u32 next_hop_sw_if_index, u32 next_hop_fib_index, u32 next_hop_weight, fib_mpls_label_t *next_hop_labels, fib_route_path_flags_t path_flags)
Update the entry to have just one path.
Definition: fib_table.c:772
static clib_error_t * gbp_subnet_init(vlib_main_t *vm)
Definition: gbp_subnet.c:459
Definition: fib_entry.h:283
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
static int gbp_subnet_internal_add(gbp_subnet_t *gs)
Definition: gbp_subnet.c:139
int gbp_subnet_add(u32 rd_id, const fib_prefix_t *pfx, gbp_subnet_type_t type, u32 sw_if_index, sclass_t sclass)
Definition: gbp_subnet.c:246
u8 * format_fib_prefix(u8 *s, va_list *args)
Definition: fib_types.c:177
A high priority source a plugin can use.
Definition: fib_entry.h:62
Aggregrate type for a prefix.
Definition: fib_types.h:203
enum gsb_subnet_show_flags_t_ gsb_subnet_show_flags_t
walk_rc_t(* gbp_subnet_cb_t)(u32 rd_id, const fib_prefix_t *pfx, gbp_subnet_type_t type, u32 sw_if_index, sclass_t sclass, void *ctx)
Definition: gbp_subnet.h:36
unsigned int u32
Definition: types.h:88
gbp_subnet_t * gbp_subnet_pool
pool of subnets
Definition: gbp_subnet.c:66
static index_t gbp_subnet_db_find(u32 fib_index, const fib_prefix_t *pfx)
Definition: gbp_subnet.c:69
Definition: fib_entry.h:275
u32 gs_sw_if_index
Definition: gbp_subnet.c:47
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:168
#define hash_create_mem(elts, key_bytes, value_bytes)
Definition: hash.h:661
Definition: fib_entry.h:279
gbp_subnet_type_t gs_type
Definition: gbp_subnet.c:39
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
#define hash_unset_mem(h, key)
Definition: hash.h:291
enum gbp_subnet_type_t_ gbp_subnet_type_t
static void gbp_subnet_del_i(index_t gsi)
Definition: gbp_subnet.c:203
long ctx[MAX_CONNS]
Definition: main.c:144
Subnet.
Definition: gbp_subnet.c:36
struct _unformat_input_t unformat_input_t
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
u16 sclass
Definition: gbp.api:118
u32 grd_uu_sw_if_index[FIB_PROTOCOL_IP_MAX]
The interfaces on which to send packets to unnknown EPs.
#define SCLASS_INVALID
Definition: gbp_types.h:25
uword * gbp_subnet_db
A DB of the subnets; key={pfx,fib-index}.
Definition: gbp_subnet.c:61
fib_node_index_t fib_table_entry_special_dpo_update(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source, fib_entry_flag_t flags, const dpo_id_t *dpo)
Update a &#39;special&#39; entry to the FIB that links to the DPO passed A special entry is an entry that the...
Definition: fib_table.c:346
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
vlib_main_t * vm
Definition: buffer.c:312
void fib_table_entry_delete_index(fib_node_index_t fib_entry_index, fib_source_t source)
Delete a FIB entry.
Definition: fib_table.c:877
index_t gbp_route_domain_find(u32 rd_id)
u32 grd_fib_index[FIB_PROTOCOL_IP_MAX]
void gbp_fwd_dpo_add_or_lock(dpo_proto_t dproto, dpo_id_t *dpo)
Definition: gbp_fwd_dpo.c:88
static int gbp_subnet_external_add(gbp_subnet_t *gs, u32 sw_if_index, sclass_t sclass)
Definition: gbp_subnet.c:158
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:30
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:155
struct gbp_subnet_t_::@438::@440 gs_stitched_external
fib_node_index_t fib_table_entry_special_dpo_add(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source, fib_entry_flag_t flags, const dpo_id_t *dpo)
Add a &#39;special&#39; entry to the FIB that links to the DPO passed A special entry is an entry that the FI...
Definition: fib_table.c:307
fib_node_index_t gs_fei
Definition: gbp_subnet.c:55
gbp_route_domain_t * gbp_route_domain_get(index_t i)
u32 fib_table_get_table_id(u32 fib_index, fib_protocol_t proto)
Get the Table-ID of the FIB from protocol and index.
Definition: fib_table.c:1053
static void clib_mem_free(void *p)
Definition: mem.h:205
u8 * format_gbp_subnet(u8 *s, va_list *args)
Definition: gbp_subnet.c:364
dpo_proto_t fib_proto_to_dpo(fib_protocol_t fib_proto)
Definition: fib_types.c:237
static void * clib_mem_alloc(uword size)
Definition: mem.h:132
struct gbp_subnet_t_ gbp_subnet_t
Subnet.
struct gbp_subnet_key_t_ gbp_subnet_key_t
a key for the DB
gsb_subnet_show_flags_t_
Definition: gbp_subnet.c:337
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
u64 uword
Definition: types.h:112
typedef key
Definition: ipsec.api:244
static clib_error_t * gbp_subnet_show(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: gbp_subnet.c:409
#define DPO_INVALID
An initialiser for DPOs declared on the stack.
Definition: dpo.h:195
Special sources.
Definition: fib_entry.h:40
void gbp_policy_dpo_add_or_lock(dpo_proto_t dproto, sclass_t sclass, u32 sw_if_index, dpo_id_t *dpo)
#define hash_get_mem(h, key)
Definition: hash.h:269
static void gbp_subnet_db_add(u32 fib_index, const fib_prefix_t *pfx, gbp_subnet_t *gs)
Definition: gbp_subnet.c:86
u32 grd_id
Route-domain ID.
void dpo_reset(dpo_id_t *dpo)
reset a DPO ID The DPO will be unlocked.
Definition: dpo.c:231
#define pool_foreach_index(i, v, body)
Iterate pool by index.
Definition: pool.h:538
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:762
struct gbp_subnet_t_::@438::@441 gs_l3_out
const ip46_address_t ADJ_BCAST_ADDR
The special broadcast address (to construct a broadcast adjacency.
Definition: adj.c:41
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
index_t gbp_route_domain_find_and_lock(u32 rd_id)
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170