65006b81f1dddb4dcec75d85ff0c71a753ef4f46
[vpp.git] / src / 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/load_balance.h>
22 #include <vnet/lisp-cp/lisp_types.h>
23 #include <vnet/lisp-gpe/lisp_gpe_sub_interface.h>
24 #include <vnet/lisp-gpe/lisp_gpe_adjacency.h>
25 #include <vnet/lisp-gpe/lisp_gpe_tunnel.h>
26 #include <vnet/fib/fib_entry.h>
27 #include <vnet/adj/adj_midchain.h>
28
29 /**
30  * Memory pool of all adjacencies
31  */
32 static lisp_gpe_adjacency_t *lisp_adj_pool;
33
34 /**
35  * Hash table of all adjacencies. key:{nh, itf}
36  * We never have an all zeros address since the interfaces are multi-access,
37  * therefore there is no ambiguity between a v4 and v6 next-hop, so we don't
38  * need to add the protocol to the key.
39  */
40 static
41 BVT (clib_bihash)
42   lisp_adj_db;
43
44 #define LISP_ADJ_SET_KEY(_key, _itf, _nh)       \
45 {                                               \
46   _key.key[0] = (_nh)->ip.v6.as_u64[0];         \
47   _key.key[1] = (_nh)->ip.v6.as_u64[1];         \
48   _key.key[2] = (_itf);                         \
49 }
50
51      static index_t lisp_adj_find (const ip_address_t * addr, u32 sw_if_index)
52 {
53   BVT (clib_bihash_kv) kv;
54
55   LISP_ADJ_SET_KEY (kv, sw_if_index, addr);
56
57   if (BV (clib_bihash_search) (&lisp_adj_db, &kv, &kv) < 0)
58     {
59       return (INDEX_INVALID);
60     }
61   else
62     {
63       return (kv.value);
64     }
65 }
66
67 static void
68 lisp_adj_insert (const ip_address_t * addr, u32 sw_if_index, index_t ai)
69 {
70   BVT (clib_bihash_kv) kv;
71
72   LISP_ADJ_SET_KEY (kv, sw_if_index, addr);
73   kv.value = ai;
74
75   BV (clib_bihash_add_del) (&lisp_adj_db, &kv, 1);
76 }
77
78 static void
79 lisp_adj_remove (const ip_address_t * addr, u32 sw_if_index)
80 {
81   BVT (clib_bihash_kv) kv;
82
83   LISP_ADJ_SET_KEY (kv, sw_if_index, addr);
84
85   BV (clib_bihash_add_del) (&lisp_adj_db, &kv, 0);
86 }
87
88 static lisp_gpe_adjacency_t *
89 lisp_gpe_adjacency_get_i (index_t lai)
90 {
91   return (pool_elt_at_index (lisp_adj_pool, lai));
92 }
93
94 fib_forward_chain_type_t
95 lisp_gpe_adj_get_fib_chain_type (const lisp_gpe_adjacency_t * ladj)
96 {
97   switch (ip_addr_version (&ladj->remote_rloc))
98     {
99     case IP4:
100       return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
101     case IP6:
102       return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
103     default:
104       ASSERT (0);
105       break;
106     }
107   return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
108 }
109
110 static void
111 ip46_address_to_ip_address (const ip46_address_t * a, ip_address_t * b)
112 {
113   if (ip46_address_is_ip4 (a))
114     {
115       memset (b, 0, sizeof (*b));
116       ip_address_set (b, &a->ip4, IP4);
117     }
118   else
119     {
120       ip_address_set (b, &a->ip6, IP6);
121     }
122 }
123
124 /**
125  * @brief Stack the tunnel's midchain on the IP forwarding chain of the via
126  */
127 static void
128 lisp_gpe_adj_stack_one (lisp_gpe_adjacency_t * ladj, adj_index_t ai)
129 {
130   const lisp_gpe_tunnel_t *lgt;
131   dpo_id_t tmp = DPO_INVALID;
132
133   lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
134   fib_entry_contribute_forwarding (lgt->fib_entry_index,
135                                    lisp_gpe_adj_get_fib_chain_type (ladj),
136                                    &tmp);
137
138   if (DPO_LOAD_BALANCE == tmp.dpoi_type)
139     {
140       /*
141        * post LISP rewrite we will load-balance. However, the LISP encap
142        * is always the same for this adjacency/tunnel and hence the IP/UDP src,dst
143        * hash is always the same result too. So we do that hash now and
144        * stack on the choice.
145        * If the choice is an incomplete adj then we will need a poke when
146        * it becomes complete. This happens since the adj update walk propagates
147        * as far a recursive paths.
148        */
149       const dpo_id_t *choice;
150       load_balance_t *lb;
151       int hash;
152
153       lb = load_balance_get (tmp.dpoi_index);
154
155       if (IP4 == ip_addr_version (&ladj->remote_rloc))
156         {
157           hash = ip4_compute_flow_hash ((ip4_header_t *) adj_get_rewrite (ai),
158                                         lb->lb_hash_config);
159         }
160       else
161         {
162           hash = ip6_compute_flow_hash ((ip6_header_t *) adj_get_rewrite (ai),
163                                         lb->lb_hash_config);
164         }
165
166       choice =
167         load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
168       dpo_copy (&tmp, choice);
169     }
170
171   adj_nbr_midchain_stack (ai, &tmp);
172   dpo_reset (&tmp);
173 }
174
175 /**
176  * @brief Call back when restacking all adjacencies on a GRE interface
177  */
178 static adj_walk_rc_t
179 lisp_gpe_adj_walk_cb (adj_index_t ai, void *ctx)
180 {
181   lisp_gpe_adjacency_t *ladj = ctx;
182
183   lisp_gpe_adj_stack_one (ladj, ai);
184
185   return (ADJ_WALK_RC_CONTINUE);
186 }
187
188 static void
189 lisp_gpe_adj_stack (lisp_gpe_adjacency_t * ladj)
190 {
191   fib_protocol_t nh_proto;
192   ip46_address_t nh;
193
194   ip_address_to_46 (&ladj->remote_rloc, &nh, &nh_proto);
195
196   /*
197    * walk all the adjacencies on th lisp interface and restack them
198    */
199   adj_nbr_walk_nh (ladj->sw_if_index,
200                    nh_proto, &nh, lisp_gpe_adj_walk_cb, ladj);
201 }
202
203 static lisp_gpe_next_protocol_e
204 lisp_gpe_adj_proto_from_vnet_link_type (vnet_link_t linkt)
205 {
206   switch (linkt)
207     {
208     case VNET_LINK_IP4:
209       return (LISP_GPE_NEXT_PROTO_IP4);
210     case VNET_LINK_IP6:
211       return (LISP_GPE_NEXT_PROTO_IP6);
212     case VNET_LINK_ETHERNET:
213       return (LISP_GPE_NEXT_PROTO_ETHERNET);
214     case VNET_LINK_NSH:
215       return (LISP_GPE_NEXT_PROTO_NSH);
216     default:
217       ASSERT (0);
218     }
219   return (LISP_GPE_NEXT_PROTO_IP4);
220 }
221
222 #define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
223
224 static void
225 lisp_gpe_fixup (vlib_main_t * vm, ip_adjacency_t * adj, vlib_buffer_t * b)
226 {
227   /* Fixup the checksum and len fields in the LISP tunnel encap
228    * that was applied at the midchain node */
229   ip_udp_fixup_one (vm, b, is_v4_packet (vlib_buffer_get_current (b)));
230 }
231
232 /**
233  * @brief The LISP-GPE interface registered function to update, i.e.
234  * provide an rewrite string for, an adjacency.
235  */
236 void
237 lisp_gpe_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
238 {
239   const lisp_gpe_tunnel_t *lgt;
240   lisp_gpe_adjacency_t *ladj;
241   ip_adjacency_t *adj;
242   ip_address_t rloc;
243   vnet_link_t linkt;
244   index_t lai;
245
246   adj = adj_get (ai);
247   ip46_address_to_ip_address (&adj->sub_type.nbr.next_hop, &rloc);
248
249   /*
250    * find an existing or create a new adj
251    */
252   lai = lisp_adj_find (&rloc, sw_if_index);
253
254   ASSERT (INDEX_INVALID != lai);
255
256   ladj = pool_elt_at_index (lisp_adj_pool, lai);
257   lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
258   linkt = adj_get_link_type (ai);
259   adj_nbr_midchain_update_rewrite
260     (ai, lisp_gpe_fixup,
261      (VNET_LINK_ETHERNET == linkt ?
262       ADJ_MIDCHAIN_FLAG_NO_COUNT :
263       ADJ_MIDCHAIN_FLAG_NONE),
264      lisp_gpe_tunnel_build_rewrite (lgt, ladj,
265                                     lisp_gpe_adj_proto_from_vnet_link_type
266                                     (linkt)));
267
268   lisp_gpe_adj_stack_one (ladj, ai);
269 }
270
271 u8 *
272 lisp_gpe_build_rewrite (vnet_main_t * vnm,
273                         u32 sw_if_index,
274                         vnet_link_t link_type, const void *dst_address)
275 {
276   ASSERT (0);
277   return (NULL);
278 }
279
280 index_t
281 lisp_gpe_adjacency_find_or_create_and_lock (const locator_pair_t * pair,
282                                             u32 overlay_table_id, u32 vni)
283 {
284   const lisp_gpe_sub_interface_t *l3s;
285   const lisp_gpe_tunnel_t *lgt;
286   lisp_gpe_adjacency_t *ladj;
287   index_t lai, l3si;
288
289   /*
290    * first find the L3 sub-interface that corresponds to the loacl-rloc and vni
291    */
292   l3si = lisp_gpe_sub_interface_find_or_create_and_lock (&pair->lcl_loc,
293                                                          overlay_table_id,
294                                                          vni);
295   l3s = lisp_gpe_sub_interface_get (l3si);
296
297   /*
298    * find an existing or create a new adj
299    */
300   lai = lisp_adj_find (&pair->rmt_loc, l3s->sw_if_index);
301
302   if (INDEX_INVALID == lai)
303     {
304
305       pool_get (lisp_adj_pool, ladj);
306       memset (ladj, 0, sizeof (*ladj));
307       lai = (ladj - lisp_adj_pool);
308
309       ip_address_copy (&ladj->remote_rloc, &pair->rmt_loc);
310       ladj->vni = vni;
311       /* transfer the lock to the adj */
312       ladj->lisp_l3_sub_index = l3si;
313       ladj->sw_if_index = l3s->sw_if_index;
314
315       /* if vni is non-default */
316       if (ladj->vni)
317         ladj->flags = LISP_GPE_FLAGS_I;
318
319       /* work in lisp-gpe not legacy mode */
320       ladj->flags |= LISP_GPE_FLAGS_P;
321
322       /*
323        * find the tunnel that will provide the underlying transport
324        * and hence the rewrite.
325        * The RLOC FIB index is default table - always.
326        */
327       ladj->tunnel_index = lisp_gpe_tunnel_find_or_create_and_lock (pair, 0);
328
329       lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
330
331       /*
332        * become of child of the RLOC FIB entry so we are updated when
333        * its reachability changes, allowing us to re-stack the midcahins
334        */
335       ladj->fib_entry_child_index = fib_entry_child_add (lgt->fib_entry_index,
336                                                          FIB_NODE_TYPE_LISP_ADJ,
337                                                          lai);
338
339       lisp_adj_insert (&ladj->remote_rloc, ladj->sw_if_index, lai);
340     }
341   else
342     {
343       /* unlock the interface from the find. */
344       lisp_gpe_sub_interface_unlock (l3si);
345       ladj = lisp_gpe_adjacency_get_i (lai);
346     }
347
348   ladj->locks++;
349
350   return (lai);
351 }
352
353 /**
354  * @brief Get a pointer to a tunnel from a pointer to a FIB node
355  */
356 static lisp_gpe_adjacency_t *
357 lisp_gpe_adjacency_from_fib_node (const fib_node_t * node)
358 {
359   return ((lisp_gpe_adjacency_t *)
360           ((char *) node -
361            STRUCT_OFFSET_OF (lisp_gpe_adjacency_t, fib_node)));
362 }
363
364 static void
365 lisp_gpe_adjacency_last_lock_gone (lisp_gpe_adjacency_t * ladj)
366 {
367   const lisp_gpe_tunnel_t *lgt;
368
369   /*
370    * no children so we are not counting locks. no-op.
371    * at least not counting
372    */
373   lisp_adj_remove (&ladj->remote_rloc, ladj->sw_if_index);
374
375   /*
376    * unlock the resources this adj holds
377    */
378   lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
379
380   fib_entry_child_remove (lgt->fib_entry_index, ladj->fib_entry_child_index);
381
382   lisp_gpe_tunnel_unlock (ladj->tunnel_index);
383   lisp_gpe_sub_interface_unlock (ladj->lisp_l3_sub_index);
384
385   pool_put (lisp_adj_pool, ladj);
386 }
387
388 void
389 lisp_gpe_adjacency_unlock (index_t lai)
390 {
391   lisp_gpe_adjacency_t *ladj;
392
393   ladj = lisp_gpe_adjacency_get_i (lai);
394
395   ladj->locks--;
396
397   if (0 == ladj->locks)
398     {
399       lisp_gpe_adjacency_last_lock_gone (ladj);
400     }
401 }
402
403 const lisp_gpe_adjacency_t *
404 lisp_gpe_adjacency_get (index_t lai)
405 {
406   return (lisp_gpe_adjacency_get_i (lai));
407 }
408
409
410 /**
411  * @brief LISP GPE tunnel back walk
412  *
413  * The FIB entry through which this tunnel resolves has been updated.
414  * re-stack the midchain on the new forwarding.
415  */
416 static fib_node_back_walk_rc_t
417 lisp_gpe_adjacency_back_walk (fib_node_t * node,
418                               fib_node_back_walk_ctx_t * ctx)
419 {
420   lisp_gpe_adj_stack (lisp_gpe_adjacency_from_fib_node (node));
421
422   return (FIB_NODE_BACK_WALK_CONTINUE);
423 }
424
425 static fib_node_t *
426 lisp_gpe_adjacency_get_fib_node (fib_node_index_t index)
427 {
428   lisp_gpe_adjacency_t *ladj;
429
430   ladj = pool_elt_at_index (lisp_adj_pool, index);
431   return (&ladj->fib_node);
432 }
433
434 static void
435 lisp_gpe_adjacency_last_fib_lock_gone (fib_node_t * node)
436 {
437   lisp_gpe_adjacency_last_lock_gone (lisp_gpe_adjacency_from_fib_node (node));
438 }
439
440 const static fib_node_vft_t lisp_gpe_tuennel_vft = {
441   .fnv_get = lisp_gpe_adjacency_get_fib_node,
442   .fnv_back_walk = lisp_gpe_adjacency_back_walk,
443   .fnv_last_lock = lisp_gpe_adjacency_last_fib_lock_gone,
444 };
445
446 u8 *
447 format_lisp_gpe_adjacency (u8 * s, va_list * args)
448 {
449   lisp_gpe_adjacency_t *ladj = va_arg (*args, lisp_gpe_adjacency_t *);
450   lisp_gpe_adjacency_format_flags_t flags =
451     va_arg (*args, lisp_gpe_adjacency_format_flags_t);
452
453   if (flags & LISP_GPE_ADJ_FORMAT_FLAG_DETAIL)
454     {
455       s =
456         format (s, "index %d locks:%d\n", ladj - lisp_adj_pool, ladj->locks);
457     }
458
459   s = format (s, " vni: %d,", ladj->vni);
460   s = format (s, " remote-RLOC: %U,", format_ip_address, &ladj->remote_rloc);
461
462   if (flags & LISP_GPE_ADJ_FORMAT_FLAG_DETAIL)
463     {
464       s = format (s, " %U\n",
465                   format_lisp_gpe_sub_interface,
466                   lisp_gpe_sub_interface_get (ladj->lisp_l3_sub_index));
467       s = format (s, " %U\n",
468                   format_lisp_gpe_tunnel,
469                   lisp_gpe_tunnel_get (ladj->tunnel_index));
470     }
471   else
472     {
473       s = format (s, " LISP L3 sub-interface index: %d,",
474                   ladj->lisp_l3_sub_index);
475       s = format (s, " LISP tunnel index: %d", ladj->tunnel_index);
476     }
477
478
479   return (s);
480 }
481
482 static clib_error_t *
483 lisp_gpe_adjacency_show (vlib_main_t * vm,
484                          unformat_input_t * input, vlib_cli_command_t * cmd)
485 {
486   lisp_gpe_adjacency_t *ladj;
487   index_t index;
488
489   if (pool_elts (lisp_adj_pool) == 0)
490     vlib_cli_output (vm, "No lisp-gpe Adjacencies");
491
492   if (unformat (input, "%d", &index))
493     {
494       ladj = lisp_gpe_adjacency_get_i (index);
495       vlib_cli_output (vm, "%U", format_lisp_gpe_adjacency, ladj,
496                        LISP_GPE_ADJ_FORMAT_FLAG_DETAIL);
497     }
498   else
499     {
500       /* *INDENT-OFF* */
501       pool_foreach (ladj, lisp_adj_pool,
502       ({
503         vlib_cli_output (vm, "[%d] %U\n",
504                          ladj - lisp_adj_pool,
505                          format_lisp_gpe_adjacency, ladj,
506                          LISP_GPE_ADJ_FORMAT_FLAG_NONE);
507       }));
508       /* *INDENT-ON* */
509     }
510
511   return 0;
512 }
513
514 /* *INDENT-OFF* */
515 VLIB_CLI_COMMAND (show_lisp_gpe_tunnel_command, static) =
516 {
517   .path = "show gpe adjacency",
518   .function = lisp_gpe_adjacency_show,
519 };
520 /* *INDENT-ON* */
521
522 #define LISP_ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS (256)
523 #define LISP_ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE (1<<20)
524
525 static clib_error_t *
526 lisp_gpe_adj_module_init (vlib_main_t * vm)
527 {
528   BV (clib_bihash_init) (&lisp_adj_db,
529                          "Adjacency Neighbour table",
530                          LISP_ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS,
531                          LISP_ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE);
532
533   fib_node_register_type (FIB_NODE_TYPE_LISP_ADJ, &lisp_gpe_tuennel_vft);
534   return (NULL);
535 }
536
537 VLIB_INIT_FUNCTION (lisp_gpe_adj_module_init)
538 /*
539  * fd.io coding-style-patch-verification: ON
540  *
541  * Local Variables:
542  * eval: (c-set-style "gnu")
543  * End:
544  */