A Protocol Independent Hierarchical FIB (VPP-352)
[vpp.git] / vnet / vnet / lisp-gpe / lisp_gpe_adjacency.c
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  * @file
17  * @brief Common utility functions for IPv4, IPv6 and L2 LISP-GPE adjacencys.
18  *
19  */
20
21 #include <vnet/dpo/dpo.h>
22 #include <vnet/lisp-gpe/lisp_gpe_sub_interface.h>
23 #include <vnet/lisp-gpe/lisp_gpe_adjacency.h>
24 #include <vnet/lisp-gpe/lisp_gpe_tunnel.h>
25 #include <vnet/fib/fib_entry.h>
26 #include <vnet/adj/adj_midchain.h>
27
28 /**
29  * Memory pool of all adjacencies
30  */
31 static lisp_gpe_adjacency_t *lisp_adj_pool;
32
33 /**
34  * Hash table of all adjacencies. key:{nh, itf}
35  * We never have an all zeros address since the interfaces are multi-access,
36  * therefore there is no ambiguity between a v4 and v6 next-hop, so we don't
37  * need to add the protocol to the key.
38  */
39 static
40 BVT (clib_bihash)
41   lisp_adj_db;
42
43 #define LISP_ADJ_SET_KEY(_key, _itf, _nh)       \
44 {                                               \
45   _key.key[0] = (_nh)->ip.v6.as_u64[0];         \
46   _key.key[1] = (_nh)->ip.v6.as_u64[1];         \
47   _key.key[2] = (_itf);                         \
48 }
49
50      static index_t lisp_adj_find (const ip_address_t * addr, u32 sw_if_index)
51 {
52   BVT (clib_bihash_kv) kv;
53
54   LISP_ADJ_SET_KEY (kv, sw_if_index, addr);
55
56   if (BV (clib_bihash_search) (&lisp_adj_db, &kv, &kv) < 0)
57     {
58       return (INDEX_INVALID);
59     }
60   else
61     {
62       return (kv.value);
63     }
64 }
65
66 static void
67 lisp_adj_insert (const ip_address_t * addr, u32 sw_if_index, index_t ai)
68 {
69   BVT (clib_bihash_kv) kv;
70
71   LISP_ADJ_SET_KEY (kv, sw_if_index, addr);
72   kv.value = ai;
73
74   BV (clib_bihash_add_del) (&lisp_adj_db, &kv, 1);
75 }
76
77 static void
78 lisp_adj_remove (const ip_address_t * addr, u32 sw_if_index)
79 {
80   BVT (clib_bihash_kv) kv;
81
82   LISP_ADJ_SET_KEY (kv, sw_if_index, addr);
83
84   BV (clib_bihash_add_del) (&lisp_adj_db, &kv, 0);
85 }
86
87 static lisp_gpe_adjacency_t *
88 lisp_gpe_adjacency_get_i (index_t lai)
89 {
90   return (pool_elt_at_index (lisp_adj_pool, lai));
91 }
92
93 fib_forward_chain_type_t
94 lisp_gpe_adj_get_fib_chain_type (const lisp_gpe_adjacency_t * ladj)
95 {
96   switch (ip_addr_version (&ladj->remote_rloc))
97     {
98     case IP4:
99       return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
100     case IP6:
101       return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
102     default:
103       ASSERT (0);
104       break;
105     }
106   return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
107 }
108
109 /**
110  * @brief Stack the tunnel's midchain on the IP forwarding chain of the via
111  */
112 static void
113 lisp_gpe_adj_stack (lisp_gpe_adjacency_t * ladj)
114 {
115   const lisp_gpe_tunnel_2_t *lgt;
116   dpo_id_t tmp = DPO_NULL;
117   fib_link_t linkt;
118
119   lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
120   fib_entry_contribute_forwarding (lgt->fib_entry_index,
121                                    lisp_gpe_adj_get_fib_chain_type (ladj),
122                                    &tmp);
123
124   FOR_EACH_FIB_IP_LINK (linkt)
125   {
126     adj_nbr_midchain_stack (ladj->adjs[linkt], &tmp);
127   }
128   dpo_reset (&tmp);
129 }
130
131 static lisp_gpe_next_protocol_e
132 lisp_gpe_adj_proto_from_fib_link_type (fib_link_t linkt)
133 {
134   switch (linkt)
135     {
136     case FIB_LINK_IP4:
137       return (LISP_GPE_INPUT_NEXT_IP4_INPUT);
138     case FIB_LINK_IP6:
139       return (LISP_GPE_INPUT_NEXT_IP6_INPUT);
140     default:
141       ASSERT (0);
142     }
143   return (LISP_GPE_INPUT_NEXT_DROP);
144 }
145
146 index_t
147 lisp_gpe_adjacency_find_or_create_and_lock (const locator_pair_t * pair,
148                                             u32 overlay_table_id, u32 vni)
149 {
150   const lisp_gpe_tunnel_2_t *lgt;
151   lisp_gpe_adjacency_t *ladj;
152   index_t lai, l3si;
153
154   /*
155    * first find the L3 sub-interface that corresponds to the loacl-rloc and vni
156    */
157   l3si = lisp_gpe_sub_interface_find_or_create_and_lock (&pair->lcl_loc,
158                                                          overlay_table_id,
159                                                          vni);
160
161   /*
162    * find an existing or create a new adj
163    */
164   lai = lisp_adj_find (&pair->rmt_loc, l3si);
165
166   if (INDEX_INVALID == lai)
167     {
168       const lisp_gpe_sub_interface_t *l3s;
169       u8 *rewrite = NULL;
170       fib_link_t linkt;
171       fib_prefix_t nh;
172
173       pool_get (lisp_adj_pool, ladj);
174       memset (ladj, 0, sizeof (*ladj));
175       lai = (ladj - lisp_adj_pool);
176
177       ladj->remote_rloc = pair->rmt_loc;
178       ladj->vni = vni;
179       /* transfer the lock to the adj */
180       ladj->lisp_l3_sub_index = l3si;
181
182       l3s = lisp_gpe_sub_interface_get (l3si);
183       ladj->sw_if_index = l3s->sw_if_index;
184
185       /* if vni is non-default */
186       if (ladj->vni)
187         ladj->flags = LISP_GPE_FLAGS_I;
188
189       /* work in lisp-gpe not legacy mode */
190       ladj->flags |= LISP_GPE_FLAGS_P;
191
192       /*
193        * find the tunnel that will provide the underlying transport
194        * and hence the rewrite.
195        * The RLOC FIB index is default table - always.
196        */
197       ladj->tunnel_index = lisp_gpe_tunnel_find_or_create_and_lock (pair, 0);
198
199       lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
200
201       /*
202        * become of child of the RLOC FIB entry so we are updated when
203        * its reachability changes, allowing us to re-stack the midcahins
204        */
205       ladj->fib_entry_child_index = fib_entry_child_add (lgt->fib_entry_index,
206                                                          FIB_NODE_TYPE_LISP_ADJ,
207                                                          lai);
208       ip_address_to_fib_prefix (&pair->rmt_loc, &nh);
209
210       /*
211        * construct and stack the FIB midchain adjacencies
212        */
213       FOR_EACH_FIB_IP_LINK (linkt)
214       {
215         ladj->adjs[linkt] = adj_nbr_add_or_lock (nh.fp_proto,
216                                                  linkt,
217                                                  &nh.fp_addr,
218                                                  ladj->sw_if_index);
219
220         rewrite =
221           lisp_gpe_tunnel_build_rewrite (lgt, ladj,
222                                          lisp_gpe_adj_proto_from_fib_link_type
223                                          (linkt));
224
225         adj_nbr_midchain_update_rewrite (ladj->adjs[linkt],
226                                          vnet_get_sup_hw_interface
227                                          (vnet_get_main (),
228                                           ladj->sw_if_index)->tx_node_index,
229                                          rewrite);
230
231         vec_free (rewrite);
232       }
233
234       lisp_gpe_adj_stack (ladj);
235
236       lisp_adj_insert (&ladj->remote_rloc, ladj->lisp_l3_sub_index, lai);
237     }
238   else
239     {
240       /* unlock the interface from the find. */
241       lisp_gpe_sub_interface_unlock (l3si);
242       ladj = lisp_gpe_adjacency_get_i (lai);
243     }
244
245   ladj->locks++;
246
247   return (lai);
248 }
249
250 /**
251  * @brief Get a pointer to a tunnel from a pointer to a FIB node
252  */
253 static lisp_gpe_adjacency_t *
254 lisp_gpe_adjacency_from_fib_node (const fib_node_t * node)
255 {
256   return ((lisp_gpe_adjacency_t *)
257           ((char *) node -
258            STRUCT_OFFSET_OF (lisp_gpe_adjacency_t, fib_node)));
259 }
260
261 static void
262 lisp_gpe_adjacency_last_lock_gone (lisp_gpe_adjacency_t * ladj)
263 {
264   /*
265    * no children so we are not counting locks. no-op.
266    * at least not counting
267    */
268   lisp_adj_remove (&ladj->remote_rloc, ladj->lisp_l3_sub_index);
269
270   /*
271    * unlock the resources this adj holds
272    */
273   lisp_gpe_tunnel_unlock (ladj->tunnel_index);
274   lisp_gpe_sub_interface_unlock (ladj->lisp_l3_sub_index);
275
276   pool_put (lisp_adj_pool, ladj);
277 }
278
279 void
280 lisp_gpe_adjacency_unlock (index_t lai)
281 {
282   lisp_gpe_adjacency_t *ladj;
283
284   ladj = lisp_gpe_adjacency_get_i (lai);
285
286   ladj->locks--;
287
288   if (0 == ladj->locks)
289     {
290       lisp_gpe_adjacency_last_lock_gone (ladj);
291     }
292 }
293
294 const lisp_gpe_adjacency_t *
295 lisp_gpe_adjacency_get (index_t lai)
296 {
297   return (lisp_gpe_adjacency_get_i (lai));
298 }
299
300
301 /**
302  * @brief LISP GPE tunnel back walk
303  *
304  * The FIB entry through which this tunnel resolves has been updated.
305  * re-stack the midchain on the new forwarding.
306  */
307 static fib_node_back_walk_rc_t
308 lisp_gpe_adjacency_back_walk (fib_node_t * node,
309                               fib_node_back_walk_ctx_t * ctx)
310 {
311   lisp_gpe_adj_stack (lisp_gpe_adjacency_from_fib_node (node));
312
313   return (FIB_NODE_BACK_WALK_CONTINUE);
314 }
315
316 static fib_node_t *
317 lisp_gpe_adjacency_get_fib_node (fib_node_index_t index)
318 {
319   lisp_gpe_adjacency_t *ladj;
320
321   ladj = pool_elt_at_index (lisp_adj_pool, index);
322   return (&ladj->fib_node);
323 }
324
325 static void
326 lisp_gpe_adjacency_last_fib_lock_gone (fib_node_t * node)
327 {
328   lisp_gpe_adjacency_last_lock_gone (lisp_gpe_adjacency_from_fib_node (node));
329 }
330
331 const static fib_node_vft_t lisp_gpe_tuennel_vft = {
332   .fnv_get = lisp_gpe_adjacency_get_fib_node,
333   .fnv_back_walk = lisp_gpe_adjacency_back_walk,
334   .fnv_last_lock = lisp_gpe_adjacency_last_fib_lock_gone,
335 };
336
337 u8 *
338 format_lisp_gpe_adjacency (u8 * s, va_list * args)
339 {
340   lisp_gpe_adjacency_t *ladj = va_arg (*args, lisp_gpe_adjacency_t *);
341   lisp_gpe_adjacency_format_flags_t flags =
342     va_arg (args, lisp_gpe_adjacency_format_flags_t);
343
344   if (flags & LISP_GPE_ADJ_FORMAT_FLAG_DETAIL)
345     {
346       s =
347         format (s, "index %d locks:%d\n", ladj - lisp_adj_pool, ladj->locks);
348     }
349
350   s = format (s, " vni: %d,", ladj->vni);
351   s = format (s, " remote-RLOC: %U,", format_ip_address, &ladj->remote_rloc);
352
353   if (flags & LISP_GPE_ADJ_FORMAT_FLAG_DETAIL)
354     {
355       s = format (s, " %U\n",
356                   format_lisp_gpe_sub_interface,
357                   lisp_gpe_sub_interface_get (ladj->lisp_l3_sub_index));
358       s = format (s, " %U\n",
359                   format_lisp_gpe_tunnel,
360                   lisp_gpe_tunnel_get (ladj->tunnel_index));
361       s = format (s, " FIB adjacencies: IPV4:%d IPv6:%d\n",
362                   ladj->adjs[FIB_LINK_IP4], ladj->adjs[FIB_LINK_IP6]);
363     }
364   else
365     {
366       s = format (s, " LISP L3 sub-interface index: %d,",
367                   ladj->lisp_l3_sub_index);
368       s = format (s, " LISP tunnel index: %d", ladj->tunnel_index);
369     }
370
371
372   return (s);
373 }
374
375 static clib_error_t *
376 lisp_gpe_adjacency_show (vlib_main_t * vm,
377                          unformat_input_t * input, vlib_cli_command_t * cmd)
378 {
379   lisp_gpe_adjacency_t *ladj;
380   index_t index;
381
382   if (pool_elts (lisp_adj_pool) == 0)
383     vlib_cli_output (vm, "No lisp-gpe Adjacencies");
384
385   if (unformat (input, "%d", &index))
386     {
387       ladj = lisp_gpe_adjacency_get_i (index);
388       vlib_cli_output (vm, "%U", format_lisp_gpe_adjacency, ladj,
389                        LISP_GPE_ADJ_FORMAT_FLAG_DETAIL);
390     }
391   else
392     {
393       /* *INDENT-OFF* */
394       pool_foreach (ladj, lisp_adj_pool,
395       ({
396         vlib_cli_output (vm, "[%d] %U\n",
397                          ladj - lisp_adj_pool,
398                          format_lisp_gpe_adjacency, ladj,
399                          LISP_GPE_ADJ_FORMAT_FLAG_NONE);
400       }));
401       /* *INDENT-ON* */
402     }
403
404   return 0;
405 }
406
407 /* *INDENT-OFF* */
408 VLIB_CLI_COMMAND (show_lisp_gpe_tunnel_command, static) =
409 {
410   .path = "show lisp gpe adjacency",
411   .function = lisp_gpe_adjacency_show,
412 };
413 /* *INDENT-ON* */
414
415 #define LISP_ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS (256)
416 #define LISP_ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE (1<<20)
417
418 static clib_error_t *
419 lisp_gpe_adj_module_init (vlib_main_t * vm)
420 {
421   BV (clib_bihash_init) (&lisp_adj_db,
422                          "Adjacency Neighbour table",
423                          LISP_ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS,
424                          LISP_ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE);
425
426   fib_node_register_type (FIB_NODE_TYPE_LISP_ADJ, &lisp_gpe_tuennel_vft);
427   return (NULL);
428 }
429
430 VLIB_INIT_FUNCTION (lisp_gpe_adj_module_init)
431 /*
432  * fd.io coding-style-patch-verification: ON
433  *
434  * Local Variables:
435  * eval: (c-set-style "gnu")
436  * End:
437  */