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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/adj/adj.h>
17 #include <vnet/adj/adj_internal.h>
18 #include <vnet/fib/fib_walk.h>
21 * The 'DB' of all glean adjs.
22 * There is one glean per-{interface, protocol, connected prefix}
24 static uword **adj_gleans[FIB_PROTOCOL_IP_MAX];
27 adj_get_glean_node (fib_protocol_t proto)
30 case FIB_PROTOCOL_IP4:
31 return (ip4_glean_node.index);
32 case FIB_PROTOCOL_IP6:
33 return (ip6_glean_node.index);
34 case FIB_PROTOCOL_MPLS:
42 adj_glean_db_lookup (fib_protocol_t proto,
44 const ip46_address_t *nh_addr)
48 if ((proto >= FIB_PROTOCOL_IP_MAX) || vec_len(adj_gleans[proto]) <= sw_if_index)
49 return (ADJ_INDEX_INVALID);
51 p = hash_get_mem (adj_gleans[proto][sw_if_index], nh_addr);
56 return (ADJ_INDEX_INVALID);
60 adj_glean_db_insert (fib_protocol_t proto,
62 const ip46_address_t *nh_addr,
65 vlib_main_t *vm = vlib_get_main();
67 vlib_worker_thread_barrier_sync(vm);
69 ASSERT(proto < FIB_PROTOCOL_IP_MAX);
70 vec_validate(adj_gleans[proto], sw_if_index);
72 if (NULL == adj_gleans[proto][sw_if_index])
74 adj_gleans[proto][sw_if_index] =
75 hash_create_mem (0, sizeof(ip46_address_t), sizeof(adj_index_t));
78 hash_set_mem_alloc (&adj_gleans[proto][sw_if_index],
81 vlib_worker_thread_barrier_release(vm);
85 adj_glean_db_remove (fib_protocol_t proto,
87 const ip46_address_t *nh_addr)
89 vlib_main_t *vm = vlib_get_main();
91 vlib_worker_thread_barrier_sync(vm);
93 ASSERT(ADJ_INDEX_INVALID != adj_glean_db_lookup(proto, sw_if_index, nh_addr));
94 hash_unset_mem_free (&adj_gleans[proto][sw_if_index],
97 if (0 == hash_elts(adj_gleans[proto][sw_if_index]))
99 hash_free(adj_gleans[proto][sw_if_index]);
100 adj_gleans[proto][sw_if_index] = NULL;
102 vlib_worker_thread_barrier_release(vm);
106 * adj_glean_add_or_lock
108 * The next_hop address here is used for source address selection in the DP.
109 * The glean adj is added to an interface's connected prefix, the next-hop
110 * passed here is the local prefix on the same interface.
113 adj_glean_add_or_lock (fib_protocol_t proto,
116 const fib_prefix_t *conn)
118 ip_adjacency_t * adj;
121 ai = adj_glean_db_lookup(proto, sw_if_index, &conn->fp_addr);
123 if (ADJ_INDEX_INVALID == ai)
125 adj = adj_alloc(proto);
127 adj->lookup_next_index = IP_LOOKUP_NEXT_GLEAN;
128 adj->ia_nh_proto = proto;
129 adj->ia_link = linkt;
130 adj->ia_node_index = adj_get_glean_node(proto);
131 ai = adj_get_index(adj);
135 fib_prefix_normalize(conn, &adj->sub_type.glean.rx_pfx);
136 adj->rewrite_header.sw_if_index = sw_if_index;
137 adj->rewrite_header.data_bytes = 0;
138 adj->rewrite_header.max_l3_packet_bytes =
139 vnet_sw_interface_get_mtu(vnet_get_main(), sw_if_index,
140 vnet_link_to_mtu(linkt));
142 vnet_update_adjacency_for_sw_interface(vnet_get_main(),
146 adj_glean_db_insert(proto, sw_if_index,
147 &adj->sub_type.glean.rx_pfx.fp_addr, ai);
155 adj_delegate_adj_created(adj);
161 * adj_glean_update_rewrite
164 adj_glean_update_rewrite (adj_index_t adj_index)
168 ASSERT(ADJ_INDEX_INVALID != adj_index);
170 adj = adj_get(adj_index);
172 vnet_rewrite_for_sw_interface(vnet_get_main(),
173 adj_fib_proto_2_nd(adj->ia_nh_proto),
174 adj->rewrite_header.sw_if_index,
176 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
177 &adj->rewrite_header,
178 sizeof (adj->rewrite_data));
182 adj_glean_update_rewrite_walk (adj_index_t ai,
185 adj_glean_update_rewrite(ai);
187 return (ADJ_WALK_RC_CONTINUE);
191 adj_glean_walk_proto (fib_protocol_t proto,
196 adj_index_t ai, *aip, *ais = NULL;
197 ip46_address_t *conn;
199 ASSERT(proto < FIB_PROTOCOL_IP_MAX);
200 if (vec_len(adj_gleans[proto]) <= sw_if_index ||
201 NULL == adj_gleans[proto][sw_if_index])
205 * Walk first to collect the indices
206 * then walk the collection. This is safe
207 * to modifications of the hash table
209 hash_foreach_mem(conn, ai, adj_gleans[proto][sw_if_index],
214 vec_foreach(aip, ais)
216 if (ADJ_WALK_RC_STOP == cb(*aip, data))
223 adj_glean_walk (u32 sw_if_index,
227 fib_protocol_t proto;
229 FOR_EACH_FIB_IP_PROTOCOL(proto)
231 adj_glean_walk_proto (proto, sw_if_index, cb, data);
236 adj_glean_get (fib_protocol_t proto,
238 const ip46_address_t *nh)
242 return adj_glean_db_lookup(proto, sw_if_index, nh);
246 ip46_address_t *conn;
249 ASSERT(proto < FIB_PROTOCOL_IP_MAX);
250 if (vec_len(adj_gleans[proto]) <= sw_if_index ||
251 NULL == adj_gleans[proto][sw_if_index])
252 return (ADJ_INDEX_INVALID);
254 hash_foreach_mem(conn, ai, adj_gleans[proto][sw_if_index],
259 return (ADJ_INDEX_INVALID);
262 const ip46_address_t *
263 adj_glean_get_src (fib_protocol_t proto,
265 const ip46_address_t *nh)
267 const ip46_address_t *conn, *source;
268 const ip_adjacency_t *adj;
271 ASSERT(proto < FIB_PROTOCOL_IP_MAX);
272 if (vec_len(adj_gleans[proto]) <= sw_if_index ||
273 NULL == adj_gleans[proto][sw_if_index])
277 .fp_len = fib_prefix_get_host_length(proto),
285 * An interface can have more than one glean address. Where
286 * possible we want to return a source address from the same
287 * subnet as the destination. If this is not possible then any address
292 hash_foreach_mem(conn, ai, adj_gleans[proto][sw_if_index],
296 if (adj->sub_type.glean.rx_pfx.fp_len > 0)
298 source = &adj->sub_type.glean.rx_pfx.fp_addr;
300 /* if no destination is specified use the just glean */
304 /* check the clean covers the desintation */
305 if (fib_prefix_is_cover(&adj->sub_type.glean.rx_pfx, &pfx))
314 adj_glean_remove (ip_adjacency_t *adj)
318 fib_prefix_normalize(&adj->sub_type.glean.rx_pfx,
320 adj_glean_db_remove(adj->ia_nh_proto,
321 adj->rewrite_header.sw_if_index,
326 adj_glean_start_backwalk (adj_index_t ai,
329 fib_node_back_walk_ctx_t bw_ctx = *(fib_node_back_walk_ctx_t*) data;
331 fib_walk_sync(FIB_NODE_TYPE_ADJ, ai, &bw_ctx);
333 return (ADJ_WALK_RC_CONTINUE);
336 static clib_error_t *
337 adj_glean_interface_state_change (vnet_main_t * vnm,
342 * for each glean on the interface trigger a walk back to the children
344 fib_node_back_walk_ctx_t bw_ctx = {
345 .fnbw_reason = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ?
346 FIB_NODE_BW_REASON_FLAG_INTERFACE_UP :
347 FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN),
350 adj_glean_walk (sw_if_index, adj_glean_start_backwalk, &bw_ctx);
355 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(adj_glean_interface_state_change);
358 * @brief Invoked on each SW interface of a HW interface when the
359 * HW interface state changes
362 adj_nbr_hw_sw_interface_state_change (vnet_main_t * vnm,
366 adj_glean_interface_state_change(vnm, sw_if_index, (uword) arg);
368 return (WALK_CONTINUE);
372 * @brief Registered callback for HW interface state changes
374 static clib_error_t *
375 adj_glean_hw_interface_state_change (vnet_main_t * vnm,
380 * walk SW interfaces on the HW
384 sw_flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
385 VNET_SW_INTERFACE_FLAG_ADMIN_UP :
388 vnet_hw_interface_walk_sw(vnm, hw_if_index,
389 adj_nbr_hw_sw_interface_state_change,
395 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
396 adj_glean_hw_interface_state_change);
398 static clib_error_t *
399 adj_glean_interface_delete (vnet_main_t * vnm,
406 * not interested in interface additions. we will not back walk
407 * to resolve paths through newly added interfaces. Why? The control
408 * plane should have the brains to add interfaces first, then routes.
409 * So the case where there are paths with a interface that matches
410 * one just created is the case where the path resolved through an
411 * interface that was deleted, and still has not been removed. The
412 * new interface added, is NO GUARANTEE that the interface being
413 * added now, even though it may have the same sw_if_index, is the
414 * same interface that the path needs. So tough!
415 * If the control plane wants these routes to resolve it needs to
416 * remove and add them again.
422 * for each glean on the interface trigger a walk back to the children
424 fib_node_back_walk_ctx_t bw_ctx = {
425 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE,
428 adj_glean_walk (sw_if_index, adj_glean_start_backwalk, &bw_ctx);
433 VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_glean_interface_delete);
436 * Callback function invoked when an interface's MAC Address changes
439 adj_glean_ethernet_change_mac (ethernet_main_t * em,
443 adj_glean_walk (sw_if_index, adj_glean_update_rewrite_walk, NULL);
447 adj_glean_table_bind (fib_protocol_t fproto,
452 * for each glean on the interface trigger a walk back to the children
454 fib_node_back_walk_ctx_t bw_ctx = {
455 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_INTERFACE_BIND,
457 .fnbw_to_fib_index = itf_fib_index,
461 adj_glean_walk_proto (fproto, sw_if_index, adj_glean_start_backwalk, &bw_ctx);
466 * Callback function invoked when an interface's IPv6 Table
470 adj_glean_ip6_table_bind (ip6_main_t * im,
476 adj_glean_table_bind (FIB_PROTOCOL_IP6, sw_if_index, new_fib_index);
480 * Callback function invoked when an interface's IPv4 Table
484 adj_glean_ip4_table_bind (ip4_main_t * im,
490 adj_glean_table_bind (FIB_PROTOCOL_IP4, sw_if_index, new_fib_index);
494 format_adj_glean (u8* s, va_list *ap)
496 index_t index = va_arg(*ap, index_t);
497 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
498 ip_adjacency_t * adj = adj_get(index);
500 s = format(s, "%U-glean: [src:%U] %U",
501 format_fib_protocol, adj->ia_nh_proto,
502 format_fib_prefix, &adj->sub_type.glean.rx_pfx,
504 &adj->rewrite_header, sizeof (adj->rewrite_data), 0);
510 adj_glean_db_size (void)
512 fib_protocol_t proto;
516 FOR_EACH_FIB_IP_PROTOCOL(proto)
518 vec_foreach_index(sw_if_index, adj_gleans[proto])
520 if (NULL != adj_gleans[proto][sw_if_index])
522 count += hash_elts(adj_gleans[proto][sw_if_index]);
530 adj_dpo_lock (dpo_id_t *dpo)
532 adj_lock(dpo->dpoi_index);
535 adj_dpo_unlock (dpo_id_t *dpo)
537 adj_unlock(dpo->dpoi_index);
540 const static dpo_vft_t adj_glean_dpo_vft = {
541 .dv_lock = adj_dpo_lock,
542 .dv_unlock = adj_dpo_unlock,
543 .dv_format = format_adj_glean,
544 .dv_get_urpf = adj_dpo_get_urpf,
545 .dv_get_mtu = adj_dpo_get_mtu,
549 * @brief The per-protocol VLIB graph nodes that are assigned to a glean
552 * this means that these graph nodes are ones from which a glean is the
553 * parent object in the DPO-graph.
555 const static char* const glean_ip4_nodes[] =
560 const static char* const glean_ip6_nodes[] =
566 const static char* const * const glean_nodes[DPO_PROTO_NUM] =
568 [DPO_PROTO_IP4] = glean_ip4_nodes,
569 [DPO_PROTO_IP6] = glean_ip6_nodes,
570 [DPO_PROTO_MPLS] = NULL,
574 adj_glean_module_init (void)
576 dpo_register(DPO_ADJACENCY_GLEAN, &adj_glean_dpo_vft, glean_nodes);
578 ethernet_address_change_ctx_t ctx = {
579 .function = adj_glean_ethernet_change_mac,
580 .function_opaque = 0,
582 vec_add1 (ethernet_main.address_change_callbacks, ctx);
584 ip6_table_bind_callback_t cbt6 = {
585 .function = adj_glean_ip6_table_bind,
587 vec_add1 (ip6_main.table_bind_callbacks, cbt6);
589 ip4_table_bind_callback_t cbt4 = {
590 .function = adj_glean_ip4_table_bind,
592 vec_add1 (ip4_main.table_bind_callbacks, cbt4);