FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
adj_mcast.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 #include <vnet/ip/ip.h>
20 
21 /*
22  * The 'DB' of all mcast adjs.
23  * There is only one mcast per-interface per-protocol, so this is a per-interface
24  * vector
25  */
27 
28 static u32
30 {
31  switch (proto) {
32  case FIB_PROTOCOL_IP4:
33  return (ip4_rewrite_mcast_node.index);
34  case FIB_PROTOCOL_IP6:
35  return (ip6_rewrite_mcast_node.index);
36  case FIB_PROTOCOL_MPLS:
37  break;
38  }
39  ASSERT(0);
40  return (0);
41 }
42 
43 /*
44  * adj_mcast_add_or_lock
45  *
46  * The next_hop address here is used for source address selection in the DP.
47  * The mcast adj is added to an interface's connected prefix, the next-hop
48  * passed here is the local prefix on the same interface.
49  */
52  vnet_link_t link_type,
53  u32 sw_if_index)
54 {
55  ip_adjacency_t * adj;
56 
58 
59  if (ADJ_INDEX_INVALID == adj_mcasts[proto][sw_if_index])
60  {
61  vnet_main_t *vnm;
62 
63  vnm = vnet_get_main();
64  adj = adj_alloc(proto);
65 
67  adj->ia_nh_proto = proto;
68  adj->ia_link = link_type;
69  adj_mcasts[proto][sw_if_index] = adj_get_index(adj);
70  adj_lock(adj_get_index(adj));
71 
72  vnet_rewrite_init(vnm, sw_if_index,
73  adj_get_mcast_node(proto),
74  vnet_tx_node_index_for_sw_interface(vnm, sw_if_index),
75  &adj->rewrite_header);
76 
77  /*
78  * we need a rewrite where the destination IP address is converted
79  * to the appropriate link-layer address. This is interface specific.
80  * So ask the interface to do it.
81  */
83  adj_get_index(adj));
84  }
85  else
86  {
87  adj = adj_get(adj_mcasts[proto][sw_if_index]);
88  adj_lock(adj_get_index(adj));
89  }
90 
91  return (adj_get_index(adj));
92 }
93 
94 /**
95  * adj_mcast_update_rewrite
96  *
97  * Update the adjacency's rewrite string. A NULL string implies the
98  * rewirte is reset (i.e. when ARP/ND etnry is gone).
99  * NB: the adj being updated may be handling traffic in the DP.
100  */
101 void
103  u8 *rewrite)
104 {
105  ip_adjacency_t *adj;
106 
107  ASSERT(ADJ_INDEX_INVALID != adj_index);
108 
109  adj = adj_get(adj_index);
110 
111  /*
112  * update the adj's rewrite string and build the arc
113  * from the rewrite node to the interface's TX node
114  */
118  vnet_get_main(),
119  adj->rewrite_header.sw_if_index),
120  rewrite);
121 }
122 
123 void
125  u32 sw_if_index)
126 {
127  ASSERT(sw_if_index < vec_len(adj_mcasts[proto]));
128 
129  adj_mcasts[proto][sw_if_index] = ADJ_INDEX_INVALID;
130 }
131 
132 static clib_error_t *
134  u32 sw_if_index,
135  u32 flags)
136 {
137  /*
138  * for each mcast on the interface trigger a walk back to the children
139  */
140  fib_protocol_t proto;
141  ip_adjacency_t *adj;
142 
143 
144  for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
145  {
146  if (sw_if_index >= vec_len(adj_mcasts[proto]) ||
147  ADJ_INDEX_INVALID == adj_mcasts[proto][sw_if_index])
148  continue;
149 
150  adj = adj_get(adj_mcasts[proto][sw_if_index]);
151 
152  fib_node_back_walk_ctx_t bw_ctx = {
156  };
157 
159  }
160 
161  return (NULL);
162 }
163 
165 
166 /**
167  * @brief Invoked on each SW interface of a HW interface when the
168  * HW interface state changes
169  */
170 static void
172  u32 sw_if_index,
173  void *arg)
174 {
175  adj_mcast_interface_state_change(vnm, sw_if_index, (uword) arg);
176 }
177 
178 /**
179  * @brief Registered callback for HW interface state changes
180  */
181 static clib_error_t *
183  u32 hw_if_index,
184  u32 flags)
185 {
186  /*
187  * walk SW interfaces on the HW
188  */
189  uword sw_flags;
190 
191  sw_flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
193  0);
194 
195  vnet_hw_interface_walk_sw(vnm, hw_if_index,
197  (void*) sw_flags);
198 
199  return (NULL);
200 }
201 
204 
205 static clib_error_t *
207  u32 sw_if_index,
208  u32 is_add)
209 {
210  /*
211  * for each mcast on the interface trigger a walk back to the children
212  */
213  fib_protocol_t proto;
214  ip_adjacency_t *adj;
215 
216  if (is_add)
217  {
218  /*
219  * not interested in interface additions. we will not back walk
220  * to resolve paths through newly added interfaces. Why? The control
221  * plane should have the brains to add interfaces first, then routes.
222  * So the case where there are paths with a interface that matches
223  * one just created is the case where the path resolved through an
224  * interface that was deleted, and still has not been removed. The
225  * new interface added, is NO GUARANTEE that the interface being
226  * added now, even though it may have the same sw_if_index, is the
227  * same interface that the path needs. So tough!
228  * If the control plane wants these routes to resolve it needs to
229  * remove and add them again.
230  */
231  return (NULL);
232  }
233 
234  for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
235  {
236  if (sw_if_index >= vec_len(adj_mcasts[proto]) ||
237  ADJ_INDEX_INVALID == adj_mcasts[proto][sw_if_index])
238  continue;
239 
240  adj = adj_get(adj_mcasts[proto][sw_if_index]);
241 
242  fib_node_back_walk_ctx_t bw_ctx = {
244  };
245 
247  }
248 
249  return (NULL);
250 }
251 
253 
254 u8*
255 format_adj_mcast (u8* s, va_list *ap)
256 {
257  index_t index = va_arg(*ap, index_t);
258  CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
259  ip_adjacency_t * adj = adj_get(index);
260 
261  s = format(s, "%U-mcast: ",
263  s = format (s, "%U",
265  &adj->rewrite_header, sizeof (adj->rewrite_data), 0);
266 
267  return (s);
268 }
269 
270 
271 static void
273 {
274  adj_lock(dpo->dpoi_index);
275 }
276 static void
278 {
279  adj_unlock(dpo->dpoi_index);
280 }
281 
282 const static dpo_vft_t adj_mcast_dpo_vft = {
284  .dv_unlock = adj_dpo_unlock,
285  .dv_format = format_adj_mcast,
286 };
287 
288 /**
289  * @brief The per-protocol VLIB graph nodes that are assigned to a mcast
290  * object.
291  *
292  * this means that these graph nodes are ones from which a mcast is the
293  * parent object in the DPO-graph.
294  */
295 const static char* const adj_mcast_ip4_nodes[] =
296 {
297  "ip4-rewrite-mcast",
298  NULL,
299 };
300 const static char* const adj_mcast_ip6_nodes[] =
301 {
302  "ip6-rewrite-mcast",
303  NULL,
304 };
305 
306 const static char* const * const adj_mcast_nodes[DPO_PROTO_NUM] =
307 {
310  [DPO_PROTO_MPLS] = NULL,
311 };
312 
313 /**
314  * @brief Return the size of the adj DB.
315  * This is only for testing purposes so an efficient implementation is not needed
316  */
317 u32
319 {
320  u32 n_adjs, sw_if_index;
321  fib_protocol_t proto;
322 
323  n_adjs = 0;
324  for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
325  {
326  for (sw_if_index = 0;
327  sw_if_index < vec_len(adj_mcasts[proto]);
328  sw_if_index++)
329  {
330  if (ADJ_INDEX_INVALID != adj_mcasts[proto][sw_if_index])
331  {
332  n_adjs++;
333  }
334  }
335  }
336 
337  return (n_adjs);
338 }
339 
340 void
342 {
343  dpo_register(DPO_ADJACENCY_MCAST, &adj_mcast_dpo_vft, adj_mcast_nodes);
344 }
dpo_lock_fn_t dv_lock
A reference counting lock function.
Definition: dpo.h:335
void vnet_rewrite_init(vnet_main_t *vnm, u32 sw_if_index, u32 this_node, u32 next_node, vnet_rewrite_header_t *rw)
Definition: rewrite.c:103
#define CLIB_UNUSED(x)
Definition: clib.h:79
A virtual function table regisitered for a DPO type.
Definition: dpo.h:330
static const char *const adj_mcast_ip6_nodes[]
Definition: adj_mcast.c:300
void adj_lock(adj_index_t adj_index)
An adjacency is a representation of an attached L3 peer.
Definition: adj.c:198
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
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:937
#define NULL
Definition: clib.h:55
IP unicast adjacency.
Definition: lookup.h:193
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
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:379
void adj_mcast_remove(fib_protocol_t proto, u32 sw_if_index)
Definition: adj_mcast.c:124
void adj_nbr_update_rewrite_internal(ip_adjacency_t *adj, ip_lookup_next_t adj_next_index, u32 complete_next_index, u32 next_index, u8 *rewrite)
Multicast Adjacency.
Definition: lookup.h:95
static void adj_dpo_lock(dpo_id_t *dpo)
Definition: adj_mcast.c:272
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
vnet_link_t ia_link
Definition: lookup.h:214
vlib_node_registration_t ip4_rewrite_mcast_node
(constructor) VLIB_REGISTER_NODE (ip4_rewrite_mcast_node)
Definition: ip4_forward.c:2778
vlib_node_registration_t ip6_rewrite_mcast_node
(constructor) VLIB_REGISTER_NODE (ip6_rewrite_mcast_node)
Definition: ip6_forward.c:2292
void adj_mcast_update_rewrite(adj_index_t adj_index, u8 *rewrite)
adj_mcast_update_rewrite
Definition: adj_mcast.c:102
static ip_adjacency_t * adj_get(adj_index_t adj_index)
Get a pointer to an adjacency object from its index.
Definition: adj.h:128
static u32 adj_get_mcast_node(fib_protocol_t proto)
Definition: adj_mcast.c:29
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:249
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:674
static clib_error_t * adj_mcast_interface_delete(vnet_main_t *vnm, u32 sw_if_index, u32 is_add)
Definition: adj_mcast.c:206
void adj_unlock(adj_index_t adj_index)
Release a reference counting lock on the adjacency.
Definition: adj.c:215
void vnet_update_adjacency_for_sw_interface(vnet_main_t *vnm, u32 sw_if_index, u32 ai)
Definition: rewrite.c:217
format_function_t format_vnet_rewrite
Definition: rewrite.h:340
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:146
fib_node_bw_reason_flag_t fnbw_reason
The reason/trigger for the backwalk.
Definition: fib_node.h:194
#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:85
static const char *const *const adj_mcast_nodes[DPO_PROTO_NUM]
Definition: adj_mcast.c:306
VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(adj_mcast_hw_interface_state_change)
static void 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_mcast.c:171
static clib_error_t * adj_mcast_interface_state_change(vnet_main_t *vnm, u32 sw_if_index, u32 flags)
Definition: adj_mcast.c:133
u32 vnet_tx_node_index_for_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
Definition: rewrite.c:96
static const char *const adj_mcast_ip4_nodes[]
The per-protocol VLIB graph nodes that are assigned to a mcast object.
Definition: adj_mcast.c:295
void adj_mcast_module_init(void)
Module initialisation.
Definition: adj_mcast.c:341
static clib_error_t * adj_mcast_hw_interface_state_change(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Registered callback for HW interface state changes.
Definition: adj_mcast.c:182
u32 adj_index_t
An index for adjacencies.
Definition: adj_types.h:30
u8 * format_adj_mcast(u8 *s, va_list *ap)
Format/display a mcast adjacency.
Definition: adj_mcast.c:255
Context passed between object during a back walk.
Definition: fib_node.h:190
u32 adj_mcast_db_size(void)
Return the size of the adj DB.
Definition: adj_mcast.c:318
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:536
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_mcast_interface_delete)
enum vnet_link_t_ vnet_link_t
Link Type: A description of the protocol of packets on the link.
u8 * format_fib_protocol(u8 *s, va_list ap)
Definition: fib_types.c:30
u64 uword
Definition: types.h:112
#define DPO_PROTO_NUM
Definition: dpo.h:73
static void adj_dpo_unlock(dpo_id_t *dpo)
Definition: adj_mcast.c:277
index_t dpoi_index
the index of objects of that type
Definition: dpo.h:162
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
ip_lookup_next_t lookup_next_index
Definition: lookup.h:204
ip_adjacency_t * adj_alloc(fib_protocol_t proto)
Definition: adj.c:54
static adj_index_t * adj_mcasts[FIB_PROTOCOL_MAX]
Definition: adj_mcast.c:26
#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:50
adj_index_t adj_mcast_add_or_lock(fib_protocol_t proto, vnet_link_t link_type, u32 sw_if_index)
Mcast Adjacency.
Definition: adj_mcast.c:51
u32 flags
Definition: vhost-user.h:78
#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:485
VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(adj_mcast_interface_state_change)