L2 over LISP and GRE (VPP-457)
[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_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_LINK (linkt)
125   {
126     if (FIB_LINK_MPLS == linkt)
127       continue;
128     adj_nbr_midchain_stack (ladj->adjs[linkt], &tmp);
129   }
130   dpo_reset (&tmp);
131 }
132
133 static lisp_gpe_next_protocol_e
134 lisp_gpe_adj_proto_from_fib_link_type (fib_link_t linkt)
135 {
136   switch (linkt)
137     {
138     case FIB_LINK_IP4:
139       return (LISP_GPE_NEXT_PROTO_IP4);
140     case FIB_LINK_IP6:
141       return (LISP_GPE_NEXT_PROTO_IP6);
142     case FIB_LINK_ETHERNET:
143       return (LISP_GPE_NEXT_PROTO_ETHERNET);
144     default:
145       ASSERT (0);
146     }
147   return (LISP_GPE_NEXT_PROTO_IP4);
148 }
149
150 #define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
151
152 static void
153 lisp_gpe_fixup (vlib_main_t * vm, ip_adjacency_t * adj, vlib_buffer_t * b)
154 {
155   /* Fixup the checksum and len fields in the LISP tunnel encap
156    * that was applied at the midchain node */
157   ip_udp_fixup_one (vm, b, is_v4_packet (vlib_buffer_get_current (b)));
158 }
159
160 index_t
161 lisp_gpe_adjacency_find_or_create_and_lock (const locator_pair_t * pair,
162                                             u32 overlay_table_id, u32 vni)
163 {
164   const lisp_gpe_tunnel_t *lgt;
165   lisp_gpe_adjacency_t *ladj;
166   index_t lai, l3si;
167
168   /*
169    * first find the L3 sub-interface that corresponds to the loacl-rloc and vni
170    */
171   l3si = lisp_gpe_sub_interface_find_or_create_and_lock (&pair->lcl_loc,
172                                                          overlay_table_id,
173                                                          vni);
174
175   /*
176    * find an existing or create a new adj
177    */
178   lai = lisp_adj_find (&pair->rmt_loc, l3si);
179
180   if (INDEX_INVALID == lai)
181     {
182       const lisp_gpe_sub_interface_t *l3s;
183       u8 *rewrite = NULL;
184       fib_link_t linkt;
185       fib_prefix_t nh;
186
187       pool_get (lisp_adj_pool, ladj);
188       memset (ladj, 0, sizeof (*ladj));
189       lai = (ladj - lisp_adj_pool);
190
191       ladj->remote_rloc = pair->rmt_loc;
192       ladj->vni = vni;
193       /* transfer the lock to the adj */
194       ladj->lisp_l3_sub_index = l3si;
195
196       l3s = lisp_gpe_sub_interface_get (l3si);
197       ladj->sw_if_index = l3s->sw_if_index;
198
199       /* if vni is non-default */
200       if (ladj->vni)
201         ladj->flags = LISP_GPE_FLAGS_I;
202
203       /* work in lisp-gpe not legacy mode */
204       ladj->flags |= LISP_GPE_FLAGS_P;
205
206       /*
207        * find the tunnel that will provide the underlying transport
208        * and hence the rewrite.
209        * The RLOC FIB index is default table - always.
210        */
211       ladj->tunnel_index = lisp_gpe_tunnel_find_or_create_and_lock (pair, 0);
212
213       lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
214
215       /*
216        * become of child of the RLOC FIB entry so we are updated when
217        * its reachability changes, allowing us to re-stack the midcahins
218        */
219       ladj->fib_entry_child_index = fib_entry_child_add (lgt->fib_entry_index,
220                                                          FIB_NODE_TYPE_LISP_ADJ,
221                                                          lai);
222       ip_address_to_fib_prefix (&pair->rmt_loc, &nh);
223
224       /*
225        * construct and stack the FIB midchain adjacencies
226        */
227       FOR_EACH_FIB_LINK (linkt)
228       {
229         if (FIB_LINK_MPLS == linkt)
230           continue;
231
232         ladj->adjs[linkt] = adj_nbr_add_or_lock (nh.fp_proto,
233                                                  linkt,
234                                                  &nh.fp_addr,
235                                                  ladj->sw_if_index);
236
237         rewrite =
238           lisp_gpe_tunnel_build_rewrite (lgt, ladj,
239                                          lisp_gpe_adj_proto_from_fib_link_type
240                                          (linkt));
241
242         adj_nbr_midchain_update_rewrite (ladj->adjs[linkt],
243                                          lisp_gpe_fixup,
244                                          (FIB_LINK_ETHERNET == linkt ?
245                                           ADJ_MIDCHAIN_FLAG_NO_COUNT :
246                                           ADJ_MIDCHAIN_FLAG_NONE), rewrite);
247
248         vec_free (rewrite);
249       }
250
251       lisp_gpe_adj_stack (ladj);
252
253       lisp_adj_insert (&ladj->remote_rloc, ladj->lisp_l3_sub_index, lai);
254     }
255   else
256     {
257       /* unlock the interface from the find. */
258       lisp_gpe_sub_interface_unlock (l3si);
259       ladj = lisp_gpe_adjacency_get_i (lai);
260     }
261
262   ladj->locks++;
263
264   return (lai);
265 }
266
267 /**
268  * @brief Get a pointer to a tunnel from a pointer to a FIB node
269  */
270 static lisp_gpe_adjacency_t *
271 lisp_gpe_adjacency_from_fib_node (const fib_node_t * node)
272 {
273   return ((lisp_gpe_adjacency_t *)
274           ((char *) node -
275            STRUCT_OFFSET_OF (lisp_gpe_adjacency_t, fib_node)));
276 }
277
278 static void
279 lisp_gpe_adjacency_last_lock_gone (lisp_gpe_adjacency_t * ladj)
280 {
281   /*
282    * no children so we are not counting locks. no-op.
283    * at least not counting
284    */
285   lisp_adj_remove (&ladj->remote_rloc, ladj->lisp_l3_sub_index);
286
287   /*
288    * unlock the resources this adj holds
289    */
290   lisp_gpe_tunnel_unlock (ladj->tunnel_index);
291   lisp_gpe_sub_interface_unlock (ladj->lisp_l3_sub_index);
292
293   pool_put (lisp_adj_pool, ladj);
294 }
295
296 void
297 lisp_gpe_adjacency_unlock (index_t lai)
298 {
299   lisp_gpe_adjacency_t *ladj;
300
301   ladj = lisp_gpe_adjacency_get_i (lai);
302
303   ladj->locks--;
304
305   if (0 == ladj->locks)
306     {
307       lisp_gpe_adjacency_last_lock_gone (ladj);
308     }
309 }
310
311 const lisp_gpe_adjacency_t *
312 lisp_gpe_adjacency_get (index_t lai)
313 {
314   return (lisp_gpe_adjacency_get_i (lai));
315 }
316
317
318 /**
319  * @brief LISP GPE tunnel back walk
320  *
321  * The FIB entry through which this tunnel resolves has been updated.
322  * re-stack the midchain on the new forwarding.
323  */
324 static fib_node_back_walk_rc_t
325 lisp_gpe_adjacency_back_walk (fib_node_t * node,
326                               fib_node_back_walk_ctx_t * ctx)
327 {
328   lisp_gpe_adj_stack (lisp_gpe_adjacency_from_fib_node (node));
329
330   return (FIB_NODE_BACK_WALK_CONTINUE);
331 }
332
333 static fib_node_t *
334 lisp_gpe_adjacency_get_fib_node (fib_node_index_t index)
335 {
336   lisp_gpe_adjacency_t *ladj;
337
338   ladj = pool_elt_at_index (lisp_adj_pool, index);
339   return (&ladj->fib_node);
340 }
341
342 static void
343 lisp_gpe_adjacency_last_fib_lock_gone (fib_node_t * node)
344 {
345   lisp_gpe_adjacency_last_lock_gone (lisp_gpe_adjacency_from_fib_node (node));
346 }
347
348 const static fib_node_vft_t lisp_gpe_tuennel_vft = {
349   .fnv_get = lisp_gpe_adjacency_get_fib_node,
350   .fnv_back_walk = lisp_gpe_adjacency_back_walk,
351   .fnv_last_lock = lisp_gpe_adjacency_last_fib_lock_gone,
352 };
353
354 u8 *
355 format_lisp_gpe_adjacency (u8 * s, va_list * args)
356 {
357   lisp_gpe_adjacency_t *ladj = va_arg (*args, lisp_gpe_adjacency_t *);
358   lisp_gpe_adjacency_format_flags_t flags =
359     va_arg (args, lisp_gpe_adjacency_format_flags_t);
360
361   if (flags & LISP_GPE_ADJ_FORMAT_FLAG_DETAIL)
362     {
363       s =
364         format (s, "index %d locks:%d\n", ladj - lisp_adj_pool, ladj->locks);
365     }
366
367   s = format (s, " vni: %d,", ladj->vni);
368   s = format (s, " remote-RLOC: %U,", format_ip_address, &ladj->remote_rloc);
369
370   if (flags & LISP_GPE_ADJ_FORMAT_FLAG_DETAIL)
371     {
372       s = format (s, " %U\n",
373                   format_lisp_gpe_sub_interface,
374                   lisp_gpe_sub_interface_get (ladj->lisp_l3_sub_index));
375       s = format (s, " %U\n",
376                   format_lisp_gpe_tunnel,
377                   lisp_gpe_tunnel_get (ladj->tunnel_index));
378       s = format (s, " FIB adjacencies: IPV4:%d IPv6:%d L2:%d\n",
379                   ladj->adjs[FIB_LINK_IP4],
380                   ladj->adjs[FIB_LINK_IP6], ladj->adjs[FIB_LINK_ETHERNET]);
381     }
382   else
383     {
384       s = format (s, " LISP L3 sub-interface index: %d,",
385                   ladj->lisp_l3_sub_index);
386       s = format (s, " LISP tunnel index: %d", ladj->tunnel_index);
387     }
388
389
390   return (s);
391 }
392
393 static clib_error_t *
394 lisp_gpe_adjacency_show (vlib_main_t * vm,
395                          unformat_input_t * input, vlib_cli_command_t * cmd)
396 {
397   lisp_gpe_adjacency_t *ladj;
398   index_t index;
399
400   if (pool_elts (lisp_adj_pool) == 0)
401     vlib_cli_output (vm, "No lisp-gpe Adjacencies");
402
403   if (unformat (input, "%d", &index))
404     {
405       ladj = lisp_gpe_adjacency_get_i (index);
406       vlib_cli_output (vm, "%U", format_lisp_gpe_adjacency, ladj,
407                        LISP_GPE_ADJ_FORMAT_FLAG_DETAIL);
408     }
409   else
410     {
411       /* *INDENT-OFF* */
412       pool_foreach (ladj, lisp_adj_pool,
413       ({
414         vlib_cli_output (vm, "[%d] %U\n",
415                          ladj - lisp_adj_pool,
416                          format_lisp_gpe_adjacency, ladj,
417                          LISP_GPE_ADJ_FORMAT_FLAG_NONE);
418       }));
419       /* *INDENT-ON* */
420     }
421
422   return 0;
423 }
424
425 /* *INDENT-OFF* */
426 VLIB_CLI_COMMAND (show_lisp_gpe_tunnel_command, static) =
427 {
428   .path = "show lisp gpe adjacency",
429   .function = lisp_gpe_adjacency_show,
430 };
431 /* *INDENT-ON* */
432
433 #define LISP_ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS (256)
434 #define LISP_ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE (1<<20)
435
436 static clib_error_t *
437 lisp_gpe_adj_module_init (vlib_main_t * vm)
438 {
439   BV (clib_bihash_init) (&lisp_adj_db,
440                          "Adjacency Neighbour table",
441                          LISP_ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS,
442                          LISP_ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE);
443
444   fib_node_register_type (FIB_NODE_TYPE_LISP_ADJ, &lisp_gpe_tuennel_vft);
445   return (NULL);
446 }
447
448 VLIB_INIT_FUNCTION (lisp_gpe_adj_module_init)
449 /*
450  * fd.io coding-style-patch-verification: ON
451  *
452  * Local Variables:
453  * eval: (c-set-style "gnu")
454  * End:
455  */