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.
17 * @brief Common utility functions for IPv4, IPv6 and L2 LISP-GPE adjacencys.
21 #include <vnet/dpo/load_balance.h>
22 #include <lisp/lisp-cp/control.h>
23 #include <lisp/lisp-cp/lisp_types.h>
24 #include <lisp/lisp-gpe/lisp_gpe_sub_interface.h>
25 #include <lisp/lisp-gpe/lisp_gpe_adjacency.h>
26 #include <lisp/lisp-gpe/lisp_gpe_tunnel.h>
27 #include <vnet/fib/fib_entry.h>
28 #include <vnet/adj/adj_midchain.h>
29 #include <vnet/udp/udp_inlines.h>
30 #include <vppinfra/bihash_24_8.h>
31 #include <vppinfra/bihash_template.h>
34 * Memory pool of all adjacencies
36 static lisp_gpe_adjacency_t *lisp_adj_pool;
39 * Hash table of all adjacencies. key:{nh, itf}
40 * We never have an all zeros address since the interfaces are multi-access,
41 * therefore there is no ambiguity between a v4 and v6 next-hop, so we don't
42 * need to add the protocol to the key.
48 #define LISP_ADJ_SET_KEY(_key, _itf, _nh) \
50 _key.key[0] = ip_addr_v6((_nh)).as_u64[0]; \
51 _key.key[1] = ip_addr_v6((_nh)).as_u64[1]; \
52 _key.key[2] = (_itf); \
55 static index_t lisp_adj_find (const ip_address_t * addr, u32 sw_if_index)
57 BVT (clib_bihash_kv) kv;
59 LISP_ADJ_SET_KEY (kv, sw_if_index, addr);
61 if (BV (clib_bihash_search) (&lisp_adj_db, &kv, &kv) < 0)
63 return (INDEX_INVALID);
72 lisp_adj_insert (const ip_address_t * addr, u32 sw_if_index, index_t ai)
74 BVT (clib_bihash_kv) kv;
76 LISP_ADJ_SET_KEY (kv, sw_if_index, addr);
79 BV (clib_bihash_add_del) (&lisp_adj_db, &kv, 1);
83 lisp_adj_remove (const ip_address_t * addr, u32 sw_if_index)
85 BVT (clib_bihash_kv) kv;
87 LISP_ADJ_SET_KEY (kv, sw_if_index, addr);
89 BV (clib_bihash_add_del) (&lisp_adj_db, &kv, 0);
92 static lisp_gpe_adjacency_t *
93 lisp_gpe_adjacency_get_i (index_t lai)
95 return (pool_elt_at_index (lisp_adj_pool, lai));
98 fib_forward_chain_type_t
99 lisp_gpe_adj_get_fib_chain_type (const lisp_gpe_adjacency_t * ladj)
101 switch (ip_addr_version (&ladj->remote_rloc))
104 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
106 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
111 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
115 ip46_address_to_ip_address (const ip46_address_t * a, ip_address_t * b)
117 if (ip46_address_is_ip4 (a))
119 clib_memset (b, 0, sizeof (*b));
120 ip_address_set (b, &a->ip4, AF_IP4);
124 ip_address_set (b, &a->ip6, AF_IP6);
129 * @brief Stack the tunnel's midchain on the IP forwarding chain of the via
132 lisp_gpe_adj_stack_one (lisp_gpe_adjacency_t * ladj, adj_index_t ai)
134 const lisp_gpe_tunnel_t *lgt;
136 lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
138 adj_nbr_midchain_stack_on_fib_entry (ai,
139 lgt->fib_entry_index,
140 lisp_gpe_adj_get_fib_chain_type
145 * @brief Call back when restacking all adjacencies on a GRE interface
148 lisp_gpe_adj_walk_cb (adj_index_t ai, void *ctx)
150 lisp_gpe_adjacency_t *ladj = ctx;
152 lisp_gpe_adj_stack_one (ladj, ai);
154 return (ADJ_WALK_RC_CONTINUE);
158 lisp_gpe_adj_stack (lisp_gpe_adjacency_t * ladj)
160 fib_protocol_t nh_proto;
163 nh_proto = ip_address_to_46 (&ladj->remote_rloc, &nh);
166 * walk all the adjacencies on th lisp interface and restack them
168 adj_nbr_walk_nh (ladj->sw_if_index,
169 nh_proto, &nh, lisp_gpe_adj_walk_cb, ladj);
172 static lisp_gpe_next_protocol_e
173 lisp_gpe_adj_proto_from_vnet_link_type (vnet_link_t linkt)
178 return (LISP_GPE_NEXT_PROTO_IP4);
180 return (LISP_GPE_NEXT_PROTO_IP6);
181 case VNET_LINK_ETHERNET:
182 return (LISP_GPE_NEXT_PROTO_ETHERNET);
184 return (LISP_GPE_NEXT_PROTO_NSH);
188 return (LISP_GPE_NEXT_PROTO_IP4);
191 #define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
194 lisp_afi_from_vnet_link_type (vnet_link_t link)
202 case VNET_LINK_ETHERNET:
205 return LISP_AFI_NO_ADDR;
210 lisp_gpe_increment_stats_counters (lisp_cp_main_t * lcm,
211 const ip_adjacency_t * adj,
214 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
215 lisp_gpe_adjacency_t *ladj;
219 gid_address_t src, dst;
222 ip46_address_to_ip_address (&adj->sub_type.nbr.next_hop, &rloc);
223 si = vnet_buffer (b)->sw_if_index[VLIB_TX];
224 lai = lisp_adj_find (&rloc, si);
225 ASSERT (INDEX_INVALID != lai);
227 ladj = pool_elt_at_index (lisp_adj_pool, lai);
229 u8 *lisp_data = (u8 *) vlib_buffer_get_current (b);
232 if (is_v4_packet (lisp_data))
233 lisp_data += sizeof (ip4_header_t);
235 lisp_data += sizeof (ip6_header_t);
237 /* skip UDP header */
238 lisp_data += sizeof (udp_header_t);
241 /* skip LISP GPE header */
242 lisp_data += sizeof (lisp_gpe_header_t);
244 i16 saved_current_data = b->current_data;
245 b->current_data = lisp_data - b->data;
247 lisp_afi_e afi = lisp_afi_from_vnet_link_type (adj->ia_link);
248 get_src_and_dst_eids_from_buffer (lcm, b, &src, &dst, afi);
249 b->current_data = saved_current_data;
250 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst, &src);
251 if (PREDICT_FALSE (~0 == di))
253 clib_warning ("dst mapping not found (%U, %U)", format_gid_address,
254 &src, format_gid_address, &dst);
258 feip = hash_get (lcm->fwd_entry_by_mapping_index, di);
259 if (PREDICT_FALSE (!feip))
262 lisp_stats_key_t key;
263 clib_memset (&key, 0, sizeof (key));
264 key.fwd_entry_index = feip[0];
265 key.tunnel_index = ladj->tunnel_index;
267 uword *p = hash_get_mem (lgm->lisp_stats_index_by_key, &key);
270 /* compute payload length starting after GPE */
271 u32 bytes = b->current_length - (lisp_data - b->data - b->current_data);
272 vlib_increment_combined_counter (&lgm->counters, vlib_get_thread_index (),
277 lisp_gpe_fixup (vlib_main_t * vm,
278 const ip_adjacency_t * adj,
279 vlib_buffer_t * b, const void *data)
281 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
283 if (lcm->flags & LISP_FLAG_STATS_ENABLED)
284 lisp_gpe_increment_stats_counters (lcm, adj, b);
286 /* Fixup the checksum and len fields in the LISP tunnel encap
287 * that was applied at the midchain node */
288 ip_udp_fixup_one (vm, b, is_v4_packet (vlib_buffer_get_current (b)));
292 * @brief The LISP-GPE interface registered function to update, i.e.
293 * provide an rewrite string for, an adjacency.
296 lisp_gpe_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
298 const lisp_gpe_tunnel_t *lgt;
299 lisp_gpe_adjacency_t *ladj;
307 ip46_address_to_ip_address (&adj->sub_type.nbr.next_hop, &rloc);
310 * find an existing or create a new adj
312 lai = lisp_adj_find (&rloc, sw_if_index);
314 ASSERT (INDEX_INVALID != lai);
316 ladj = pool_elt_at_index (lisp_adj_pool, lai);
317 lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
318 linkt = adj_get_link_type (ai);
319 af = ADJ_FLAG_MIDCHAIN_IP_STACK;
320 if (VNET_LINK_ETHERNET == linkt)
321 af |= ADJ_FLAG_MIDCHAIN_NO_COUNT;
323 adj_nbr_midchain_update_rewrite
324 (ai, lisp_gpe_fixup, NULL, af,
325 lisp_gpe_tunnel_build_rewrite (lgt, ladj,
326 lisp_gpe_adj_proto_from_vnet_link_type
329 lisp_gpe_adj_stack_one (ladj, ai);
333 lisp_gpe_build_rewrite (vnet_main_t * vnm,
335 vnet_link_t link_type, const void *dst_address)
342 lisp_gpe_adjacency_find_or_create_and_lock (const locator_pair_t * pair,
343 u32 overlay_table_id, u32 vni)
345 const lisp_gpe_sub_interface_t *l3s;
346 const lisp_gpe_tunnel_t *lgt;
347 lisp_gpe_adjacency_t *ladj;
351 * first find the L3 sub-interface that corresponds to the loacl-rloc and vni
353 l3si = lisp_gpe_sub_interface_find_or_create_and_lock (&pair->lcl_loc,
356 l3s = lisp_gpe_sub_interface_get (l3si);
359 * find an existing or create a new adj
361 lai = lisp_adj_find (&pair->rmt_loc, l3s->sw_if_index);
363 if (INDEX_INVALID == lai)
366 pool_get (lisp_adj_pool, ladj);
367 clib_memset (ladj, 0, sizeof (*ladj));
368 lai = (ladj - lisp_adj_pool);
370 ip_address_copy (&ladj->remote_rloc, &pair->rmt_loc);
372 /* transfer the lock to the adj */
373 ladj->lisp_l3_sub_index = l3si;
374 ladj->sw_if_index = l3s->sw_if_index;
376 /* if vni is non-default */
378 ladj->flags = LISP_GPE_FLAGS_I;
380 /* work in lisp-gpe not legacy mode */
381 ladj->flags |= LISP_GPE_FLAGS_P;
384 * find the tunnel that will provide the underlying transport
385 * and hence the rewrite.
386 * The RLOC FIB index is default table - always.
388 ladj->tunnel_index = lisp_gpe_tunnel_find_or_create_and_lock (pair, 0);
390 lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
393 * become of child of the RLOC FIB entry so we are updated when
394 * its reachability changes, allowing us to re-stack the midcahins
396 ladj->fib_entry_child_index = fib_entry_child_add (lgt->fib_entry_index,
397 FIB_NODE_TYPE_LISP_ADJ,
400 lisp_adj_insert (&ladj->remote_rloc, ladj->sw_if_index, lai);
404 /* unlock the interface from the find. */
405 lisp_gpe_sub_interface_unlock (l3si);
406 ladj = lisp_gpe_adjacency_get_i (lai);
415 * @brief Get a pointer to a tunnel from a pointer to a FIB node
417 static lisp_gpe_adjacency_t *
418 lisp_gpe_adjacency_from_fib_node (const fib_node_t * node)
420 return ((lisp_gpe_adjacency_t *)
422 STRUCT_OFFSET_OF (lisp_gpe_adjacency_t, fib_node)));
426 lisp_gpe_adjacency_last_lock_gone (lisp_gpe_adjacency_t * ladj)
428 const lisp_gpe_tunnel_t *lgt;
431 * no children so we are not counting locks. no-op.
432 * at least not counting
434 lisp_adj_remove (&ladj->remote_rloc, ladj->sw_if_index);
437 * unlock the resources this adj holds
439 lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
441 fib_entry_child_remove (lgt->fib_entry_index, ladj->fib_entry_child_index);
443 lisp_gpe_tunnel_unlock (ladj->tunnel_index);
444 lisp_gpe_sub_interface_unlock (ladj->lisp_l3_sub_index);
446 pool_put (lisp_adj_pool, ladj);
450 lisp_gpe_adjacency_unlock (index_t lai)
452 lisp_gpe_adjacency_t *ladj;
454 ladj = lisp_gpe_adjacency_get_i (lai);
458 if (0 == ladj->locks)
460 lisp_gpe_adjacency_last_lock_gone (ladj);
464 const lisp_gpe_adjacency_t *
465 lisp_gpe_adjacency_get (index_t lai)
467 return (lisp_gpe_adjacency_get_i (lai));
472 * @brief LISP GPE tunnel back walk
474 * The FIB entry through which this tunnel resolves has been updated.
475 * re-stack the midchain on the new forwarding.
477 static fib_node_back_walk_rc_t
478 lisp_gpe_adjacency_back_walk (fib_node_t * node,
479 fib_node_back_walk_ctx_t * ctx)
481 lisp_gpe_adj_stack (lisp_gpe_adjacency_from_fib_node (node));
483 return (FIB_NODE_BACK_WALK_CONTINUE);
487 lisp_gpe_adjacency_get_fib_node (fib_node_index_t index)
489 lisp_gpe_adjacency_t *ladj;
491 ladj = pool_elt_at_index (lisp_adj_pool, index);
492 return (&ladj->fib_node);
496 lisp_gpe_adjacency_last_fib_lock_gone (fib_node_t * node)
498 lisp_gpe_adjacency_last_lock_gone (lisp_gpe_adjacency_from_fib_node (node));
501 const static fib_node_vft_t lisp_gpe_tuennel_vft = {
502 .fnv_get = lisp_gpe_adjacency_get_fib_node,
503 .fnv_back_walk = lisp_gpe_adjacency_back_walk,
504 .fnv_last_lock = lisp_gpe_adjacency_last_fib_lock_gone,
508 format_lisp_gpe_adjacency (u8 * s, va_list * args)
510 lisp_gpe_adjacency_t *ladj = va_arg (*args, lisp_gpe_adjacency_t *);
511 lisp_gpe_adjacency_format_flags_t flags =
512 va_arg (*args, lisp_gpe_adjacency_format_flags_t);
514 if (flags & LISP_GPE_ADJ_FORMAT_FLAG_DETAIL)
517 format (s, "index %d locks:%d\n", ladj - lisp_adj_pool, ladj->locks);
520 s = format (s, " vni: %d,", ladj->vni);
521 s = format (s, " remote-RLOC: %U,", format_ip_address, &ladj->remote_rloc);
523 if (flags & LISP_GPE_ADJ_FORMAT_FLAG_DETAIL)
525 s = format (s, " %U\n",
526 format_lisp_gpe_sub_interface,
527 lisp_gpe_sub_interface_get (ladj->lisp_l3_sub_index));
528 s = format (s, " %U\n",
529 format_lisp_gpe_tunnel,
530 lisp_gpe_tunnel_get (ladj->tunnel_index));
534 s = format (s, " LISP L3 sub-interface index: %d,",
535 ladj->lisp_l3_sub_index);
536 s = format (s, " LISP tunnel index: %d", ladj->tunnel_index);
543 static clib_error_t *
544 lisp_gpe_adjacency_show (vlib_main_t * vm,
545 unformat_input_t * input, vlib_cli_command_t * cmd)
547 lisp_gpe_adjacency_t *ladj;
550 if (pool_elts (lisp_adj_pool) == 0)
551 vlib_cli_output (vm, "No lisp-gpe Adjacencies");
553 if (unformat (input, "%d", &index))
555 ladj = lisp_gpe_adjacency_get_i (index);
556 vlib_cli_output (vm, "%U", format_lisp_gpe_adjacency, ladj,
557 LISP_GPE_ADJ_FORMAT_FLAG_DETAIL);
562 pool_foreach (ladj, lisp_adj_pool,
564 vlib_cli_output (vm, "[%d] %U\n",
565 ladj - lisp_adj_pool,
566 format_lisp_gpe_adjacency, ladj,
567 LISP_GPE_ADJ_FORMAT_FLAG_NONE);
576 VLIB_CLI_COMMAND (show_lisp_gpe_tunnel_command, static) =
578 .path = "show gpe adjacency",
579 .function = lisp_gpe_adjacency_show,
583 #define LISP_ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS (256)
584 #define LISP_ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE (1<<20)
586 static clib_error_t *
587 lisp_gpe_adj_module_init (vlib_main_t * vm)
589 BV (clib_bihash_init) (&lisp_adj_db,
590 "Adjacency Neighbour table",
591 LISP_ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS,
592 LISP_ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE);
594 fib_node_register_type (FIB_NODE_TYPE_LISP_ADJ, &lisp_gpe_tuennel_vft);
598 VLIB_INIT_FUNCTION (lisp_gpe_adj_module_init);
600 * fd.io coding-style-patch-verification: ON
603 * eval: (c-set-style "gnu")