FD.io VPP  v18.07-34-g55fbdb9
Vector Packet Processing
adj_glean.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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 <vnet/adj/adj.h>
17 #include <vnet/adj/adj_internal.h>
18 #include <vnet/fib/fib_walk.h>
19 
20 /*
21  * The 'DB' of all glean adjs.
22  * There is only one glean per-interface per-protocol, so this is a per-interface
23  * vector
24  */
26 
27 static inline vlib_node_registration_t*
29 {
30  switch (proto) {
31  case FIB_PROTOCOL_IP4:
32  return (&ip4_glean_node);
33  case FIB_PROTOCOL_IP6:
34  return (&ip6_glean_node);
35  case FIB_PROTOCOL_MPLS:
36  break;
37  }
38  ASSERT(0);
39  return (NULL);
40 }
41 
42 /*
43  * adj_glean_add_or_lock
44  *
45  * The next_hop address here is used for source address selection in the DP.
46  * The glean adj is added to an interface's connected prefix, the next-hop
47  * passed here is the local prefix on the same interface.
48  */
51  vnet_link_t linkt,
52  u32 sw_if_index,
53  const ip46_address_t *nh_addr)
54 {
55  ip_adjacency_t * adj;
56 
58 
59  if (ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
60  {
61  adj = adj_alloc(proto);
62 
64  adj->ia_nh_proto = proto;
65  adj->ia_link = linkt;
66  adj_gleans[proto][sw_if_index] = adj_get_index(adj);
67 
68  if (NULL != nh_addr)
69  {
70  adj->sub_type.glean.receive_addr = *nh_addr;
71  }
72  else
73  {
74  adj->sub_type.glean.receive_addr = zero_addr;
75  }
76 
77  adj->rewrite_header.sw_if_index = sw_if_index;
78  adj->rewrite_header.data_bytes = 0;
79  adj->rewrite_header.max_l3_packet_bytes =
81  vnet_link_to_mtu(linkt));
82  adj_lock(adj_get_index(adj));
83 
85  sw_if_index,
86  adj_get_index(adj));
87  }
88  else
89  {
90  adj = adj_get(adj_gleans[proto][sw_if_index]);
91  adj_lock(adj_get_index(adj));
92  }
93 
94  return (adj_get_index(adj));
95 }
96 
97 /**
98  * adj_glean_update_rewrite
99  */
100 void
102 {
103  ip_adjacency_t *adj;
104 
105  ASSERT(ADJ_INDEX_INVALID != adj_index);
106 
107  adj = adj_get(adj_index);
108 
111  adj->rewrite_header.sw_if_index,
112  adj_get_glean_node(adj->ia_nh_proto)->index,
114  &adj->rewrite_header,
115  sizeof (adj->rewrite_data));
116 }
117 
120  u32 sw_if_index)
121 {
122  if (sw_if_index < vec_len(adj_gleans[proto]))
123  {
124  return (adj_gleans[proto][sw_if_index]);
125  }
126  return (ADJ_INDEX_INVALID);
127 }
128 
129 void
131  u32 sw_if_index)
132 {
133  ASSERT(sw_if_index < vec_len(adj_gleans[proto]));
134 
135  adj_gleans[proto][sw_if_index] = ADJ_INDEX_INVALID;
136 }
137 
138 static clib_error_t *
140  u32 sw_if_index,
141  u32 flags)
142 {
143  /*
144  * for each glean on the interface trigger a walk back to the children
145  */
146  fib_protocol_t proto;
147  ip_adjacency_t *adj;
148 
149 
150  for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
151  {
152  if (sw_if_index >= vec_len(adj_gleans[proto]) ||
153  ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
154  continue;
155 
156  adj = adj_get(adj_gleans[proto][sw_if_index]);
157 
158  fib_node_back_walk_ctx_t bw_ctx = {
162  };
163 
165  }
166 
167  return (NULL);
168 }
169 
171 
172 /**
173  * @brief Invoked on each SW interface of a HW interface when the
174  * HW interface state changes
175  */
176 static walk_rc_t
178  u32 sw_if_index,
179  void *arg)
180 {
181  adj_glean_interface_state_change(vnm, sw_if_index, (uword) arg);
182 
183  return (WALK_CONTINUE);
184 }
185 
186 /**
187  * @brief Registered callback for HW interface state changes
188  */
189 static clib_error_t *
191  u32 hw_if_index,
192  u32 flags)
193 {
194  /*
195  * walk SW interfaces on the HW
196  */
197  uword sw_flags;
198 
199  sw_flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
201  0);
202 
203  vnet_hw_interface_walk_sw(vnm, hw_if_index,
205  (void*) sw_flags);
206 
207  return (NULL);
208 }
209 
212 
213 static clib_error_t *
215  u32 sw_if_index,
216  u32 is_add)
217 {
218  /*
219  * for each glean on the interface trigger a walk back to the children
220  */
221  fib_protocol_t proto;
222  ip_adjacency_t *adj;
223 
224  if (is_add)
225  {
226  /*
227  * not interested in interface additions. we will not back walk
228  * to resolve paths through newly added interfaces. Why? The control
229  * plane should have the brains to add interfaces first, then routes.
230  * So the case where there are paths with a interface that matches
231  * one just created is the case where the path resolved through an
232  * interface that was deleted, and still has not been removed. The
233  * new interface added, is NO GUARANTEE that the interface being
234  * added now, even though it may have the same sw_if_index, is the
235  * same interface that the path needs. So tough!
236  * If the control plane wants these routes to resolve it needs to
237  * remove and add them again.
238  */
239  return (NULL);
240  }
241 
242  for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
243  {
244  if (sw_if_index >= vec_len(adj_gleans[proto]) ||
245  ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
246  continue;
247 
248  adj = adj_get(adj_gleans[proto][sw_if_index]);
249 
250  fib_node_back_walk_ctx_t bw_ctx = {
252  };
253 
255  }
256 
257  return (NULL);
258 }
259 
261 
262 u8*
263 format_adj_glean (u8* s, va_list *ap)
264 {
265  index_t index = va_arg(*ap, index_t);
266  CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
267  ip_adjacency_t * adj = adj_get(index);
268 
269  s = format(s, "%U-glean: %U",
272  &adj->rewrite_header, sizeof (adj->rewrite_data), 0);
273 
274  return (s);
275 }
276 
277 
278 static void
280 {
281  adj_lock(dpo->dpoi_index);
282 }
283 static void
285 {
286  adj_unlock(dpo->dpoi_index);
287 }
288 
289 const static dpo_vft_t adj_glean_dpo_vft = {
291  .dv_unlock = adj_dpo_unlock,
292  .dv_format = format_adj_glean,
293  .dv_get_urpf = adj_dpo_get_urpf,
294 };
295 
296 /**
297  * @brief The per-protocol VLIB graph nodes that are assigned to a glean
298  * object.
299  *
300  * this means that these graph nodes are ones from which a glean is the
301  * parent object in the DPO-graph.
302  */
303 const static char* const glean_ip4_nodes[] =
304 {
305  "ip4-glean",
306  NULL,
307 };
308 const static char* const glean_ip6_nodes[] =
309 {
310  "ip6-glean",
311  NULL,
312 };
313 
314 const static char* const * const glean_nodes[DPO_PROTO_NUM] =
315 {
318  [DPO_PROTO_MPLS] = NULL,
319 };
320 
321 void
323 {
324  dpo_register(DPO_ADJACENCY_GLEAN, &adj_glean_dpo_vft, glean_nodes);
325 }
dpo_lock_fn_t dv_lock
A reference counting lock function.
Definition: dpo.h:404
void adj_glean_remove(fib_protocol_t proto, u32 sw_if_index)
Definition: adj_glean.c:130
#define VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST
Definition: rewrite.h:302
#define CLIB_UNUSED(x)
Definition: clib.h:79
A virtual function table regisitered for a DPO type.
Definition: dpo.h:399
void adj_lock(adj_index_t adj_index)
Take a reference counting lock on the adjacency.
Definition: adj.c:223
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
void vnet_hw_interface_walk_sw(vnet_main_t *vnm, u32 hw_if_index, vnet_hw_sw_interface_walk_t fn, void *ctx)
Walk the SW interfaces on a HW interface - this is the super interface and any sub-interfaces.
Definition: interface.c:1037
#define NULL
Definition: clib.h:55
IP unicast adjacency.
Definition: adj.h:175
union ip_adjacency_t_::@45 sub_type
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
void vnet_rewrite_for_sw_interface(vnet_main_t *vnm, vnet_link_t link_type, u32 sw_if_index, u32 node_index, void *dst_address, vnet_rewrite_header_t *rw, u32 max_rewrite_bytes)
Deprecated.
Definition: rewrite.c:125
adj_index_t adj_glean_get(fib_protocol_t proto, u32 sw_if_index)
Get an existing glean.
Definition: adj_glean.c:119
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
static u32 vnet_sw_interface_get_mtu(vnet_main_t *vnm, u32 sw_if_index, vnet_mtu_t af)
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:458
u32 adj_dpo_get_urpf(const dpo_id_t *dpo)
Definition: adj.c:213
unsigned char u8
Definition: types.h:56
u8 * format_fib_protocol(u8 *s, va_list *ap)
Definition: fib_types.c:32
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
vnet_link_t ia_link
link/ether-type 1 bytes
Definition: adj.h:196
enum walk_rc_t_ walk_rc_t
Walk return code.
void adj_glean_update_rewrite(adj_index_t adj_index)
adj_glean_update_rewrite
Definition: adj_glean.c:101
static ip_adjacency_t * adj_get(adj_index_t adj_index)
Get a pointer to an adjacency object from its index.
Definition: adj.h:370
void dpo_register(dpo_type_t type, const dpo_vft_t *vft, const char *const *const *nodes)
For a given DPO type Register:
Definition: dpo.c:321
static walk_rc_t adj_nbr_hw_sw_interface_state_change(vnet_main_t *vnm, u32 sw_if_index, void *arg)
Invoked on each SW interface of a HW interface when the HW interface state changes.
Definition: adj_glean.c:177
void fib_walk_sync(fib_node_type_t parent_type, fib_node_index_t parent_index, fib_node_back_walk_ctx_t *ctx)
Back walk all the children of a FIB node.
Definition: fib_walk.c:727
static adj_index_t * adj_gleans[FIB_PROTOCOL_MAX]
Definition: adj_glean.c:25
void adj_unlock(adj_index_t adj_index)
Release a reference counting lock on the adjacency.
Definition: adj.c:240
void vnet_update_adjacency_for_sw_interface(vnet_main_t *vnm, u32 sw_if_index, u32 ai)
Definition: rewrite.c:228
unsigned int u32
Definition: types.h:88
format_function_t format_vnet_rewrite
Definition: rewrite.h:331
static clib_error_t * adj_glean_interface_delete(vnet_main_t *vnm, u32 sw_if_index, u32 is_add)
Definition: adj_glean.c:214
struct ip_adjacency_t_::@45::@48 glean
IP_LOOKUP_NEXT_GLEAN.
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
fib_node_bw_reason_flag_t fnbw_reason
The reason/trigger for the backwalk.
Definition: fib_node.h:204
#define ADJ_INDEX_INVALID
Invalid ADJ index - used when no adj is known likewise blazoned capitals INVALID speak volumes where ...
Definition: adj_types.h:36
static adj_index_t adj_get_index(ip_adjacency_t *adj)
Get a pointer to an adjacency object from its index.
Definition: adj_internal.h:101
static clib_error_t * adj_glean_hw_interface_state_change(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Registered callback for HW interface state changes.
Definition: adj_glean.c:190
vlib_node_registration_t ip6_glean_node
(constructor) VLIB_REGISTER_NODE (ip6_glean_node)
This packet matches an "interface route" and packets need to be passed to ARP to find rewrite string ...
Definition: adj.h:68
static void adj_dpo_unlock(dpo_id_t *dpo)
Definition: adj_glean.c:284
void adj_glean_module_init(void)
Module initialisation.
Definition: adj_glean.c:322
u32 flags
Definition: vhost_user.h:110
static const char *const glean_ip4_nodes[]
The per-protocol VLIB graph nodes that are assigned to a glean object.
Definition: adj_glean.c:303
static clib_error_t * adj_glean_interface_state_change(vnet_main_t *vnm, u32 sw_if_index, u32 flags)
Definition: adj_glean.c:139
static void adj_dpo_lock(dpo_id_t *dpo)
Definition: adj_glean.c:279
u8 * format_adj_glean(u8 *s, va_list *ap)
Format/display a glean adjacency.
Definition: adj_glean.c:263
u32 adj_index_t
An index for adjacencies.
Definition: adj_types.h:30
static const char *const *const glean_nodes[DPO_PROTO_NUM]
Definition: adj_glean.c:314
static vlib_node_registration_t * adj_get_glean_node(fib_protocol_t proto)
Definition: adj_glean.c:28
VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(adj_glean_hw_interface_state_change)
Context passed between object during a back walk.
Definition: fib_node.h:200
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:661
#define ASSERT(truth)
enum vnet_link_t_ vnet_link_t
Link Type: A description of the protocol of packets on the link.
VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_glean_interface_delete)
static const char *const glean_ip6_nodes[]
Definition: adj_glean.c:308
struct _vlib_node_registration vlib_node_registration_t
fib_protocol_t ia_nh_proto
The protocol of the neighbor/peer.
Definition: adj.h:203
#define DPO_PROTO_NUM
Definition: dpo.h:70
index_t dpoi_index
the index of objects of that type
Definition: dpo.h:184
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
ip_lookup_next_t lookup_next_index
Next hop after ip4-lookup.
Definition: adj.h:190
vnet_mtu_t vnet_link_to_mtu(vnet_link_t link)
Definition: interface.c:1576
u64 uword
Definition: types.h:112
ip_adjacency_t * adj_alloc(fib_protocol_t proto)
Definition: adj.c:48
adj_index_t adj_glean_add_or_lock(fib_protocol_t proto, vnet_link_t linkt, u32 sw_if_index, const ip46_address_t *nh_addr)
Glean Adjacency.
Definition: adj_glean.c:50
#define FIB_PROTOCOL_MAX
Definition outside of enum so it does not need to be included in non-defaulted switch statements...
Definition: fib_types.h:52
vlib_node_registration_t ip4_glean_node
(constructor) VLIB_REGISTER_NODE (ip4_glean_node)
Definition: ip4_forward.c:1880
static vnet_link_t adj_fib_proto_2_nd(fib_protocol_t fp)
Definition: adj_internal.h:67
#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
const ip46_address_t zero_addr
Definition: lookup.c:318
VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(adj_glean_interface_state_change)