fib: midchain adjacency optimisations
[vpp.git] / src / vnet / adj / adj_nbr.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 #include <vnet/adj/adj_nbr.h>
17 #include <vnet/adj/adj_internal.h>
18 #include <vnet/ethernet/arp_packet.h>
19 #include <vnet/fib/fib_walk.h>
20
21 #include <vppinfra/bihash_24_8.h>
22
23 /*
24  * Vector Hash tables of neighbour (traditional) adjacencies
25  *  Key: interface(for the vector index), address (and its proto),
26  *       link-type/ether-type.
27  */
28 static BVT(clib_bihash) **adj_nbr_tables[FIB_PROTOCOL_MAX];
29
30 // FIXME SIZE APPROPRIATELY. ASK DAVEB.
31 #define ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS (64 * 64)
32 #define ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE (32<<20)
33
34
35 #define ADJ_NBR_SET_KEY(_key, _lt, _nh)         \
36 {                                               \
37     _key.key[0] = (_nh)->as_u64[0];             \
38     _key.key[1] = (_nh)->as_u64[1];             \
39     _key.key[2] = (_lt);                        \
40 }
41
42 #define ADJ_NBR_ITF_OK(_proto, _itf)                    \
43     (((_itf) < vec_len(adj_nbr_tables[_proto])) &&      \
44      (NULL != adj_nbr_tables[_proto][sw_if_index]))
45
46 static void
47 adj_nbr_insert (fib_protocol_t nh_proto,
48                 vnet_link_t link_type,
49                 const ip46_address_t *nh_addr,
50                 u32 sw_if_index,
51                 adj_index_t adj_index)
52 {
53     BVT(clib_bihash_kv) kv;
54
55     if (sw_if_index >= vec_len(adj_nbr_tables[nh_proto]))
56     {
57         vec_validate(adj_nbr_tables[nh_proto], sw_if_index);
58     }
59     if (NULL == adj_nbr_tables[nh_proto][sw_if_index])
60     {
61         adj_nbr_tables[nh_proto][sw_if_index] =
62             clib_mem_alloc_aligned(sizeof(BVT(clib_bihash)),
63                                    CLIB_CACHE_LINE_BYTES);
64         clib_memset(adj_nbr_tables[nh_proto][sw_if_index],
65                0,
66                sizeof(BVT(clib_bihash)));
67
68         BV(clib_bihash_init) (adj_nbr_tables[nh_proto][sw_if_index],
69                               "Adjacency Neighbour table",
70                               ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS,
71                               ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE);
72     }
73
74     ADJ_NBR_SET_KEY(kv, link_type, nh_addr);
75     kv.value = adj_index;
76
77     BV(clib_bihash_add_del) (adj_nbr_tables[nh_proto][sw_if_index], &kv, 1);
78 }
79
80 void
81 adj_nbr_remove (adj_index_t ai,
82                 fib_protocol_t nh_proto,
83                 vnet_link_t link_type,
84                 const ip46_address_t *nh_addr,
85                 u32 sw_if_index)
86 {
87     BVT(clib_bihash_kv) kv;
88
89     if (!ADJ_NBR_ITF_OK(nh_proto, sw_if_index))
90         return;
91
92     ADJ_NBR_SET_KEY(kv, link_type, nh_addr);
93     kv.value = ai;
94
95     BV(clib_bihash_add_del) (adj_nbr_tables[nh_proto][sw_if_index], &kv, 0);
96 }
97
98 adj_index_t
99 adj_nbr_find (fib_protocol_t nh_proto,
100               vnet_link_t link_type,
101               const ip46_address_t *nh_addr,
102               u32 sw_if_index)
103 {
104     BVT(clib_bihash_kv) kv;
105
106     ADJ_NBR_SET_KEY(kv, link_type, nh_addr);
107
108     if (!ADJ_NBR_ITF_OK(nh_proto, sw_if_index))
109         return (ADJ_INDEX_INVALID);
110
111     if (BV(clib_bihash_search)(adj_nbr_tables[nh_proto][sw_if_index],
112                                &kv, &kv) < 0)
113     {
114         return (ADJ_INDEX_INVALID);
115     }
116     else
117     {
118         return (kv.value);
119     }
120 }
121
122 static inline u32
123 adj_get_nd_node (fib_protocol_t proto)
124 {
125     switch (proto) {
126     case FIB_PROTOCOL_IP4:
127         return (ip4_arp_node.index);
128     case FIB_PROTOCOL_IP6:
129         return (ip6_discover_neighbor_node.index);
130     case FIB_PROTOCOL_MPLS:
131         break;
132     }
133     ASSERT(0);
134     return (ip4_arp_node.index);
135 }
136
137 /**
138  * @brief Check and set feature flags if o/p interface has any o/p features.
139  */
140 static void
141 adj_nbr_evaluate_feature (adj_index_t ai)
142 {
143     ip_adjacency_t *adj;
144     vnet_feature_main_t *fm = &feature_main;
145     i16 feature_count;
146     u8 arc_index;
147     u32 sw_if_index;
148
149     adj = adj_get(ai);
150
151     switch (adj->ia_link)
152     {
153     case VNET_LINK_IP4:
154         arc_index = ip4_main.lookup_main.output_feature_arc_index;
155         break;
156     case VNET_LINK_IP6:
157         arc_index = ip6_main.lookup_main.output_feature_arc_index;
158         break;
159     case VNET_LINK_MPLS:
160         arc_index = mpls_main.output_feature_arc_index;
161         break;
162     default:
163         return;
164     }
165
166     sw_if_index = adj->rewrite_header.sw_if_index;
167     if (vec_len(fm->feature_count_by_sw_if_index[arc_index]) > sw_if_index)
168     {
169         feature_count = fm->feature_count_by_sw_if_index[arc_index][sw_if_index];
170         if (feature_count > 0)
171         {
172             vnet_feature_config_main_t *cm;
173
174             adj->rewrite_header.flags |= VNET_REWRITE_HAS_FEATURES;
175             cm = &fm->feature_config_mains[arc_index];
176
177             adj->ia_cfg_index = vec_elt (cm->config_index_by_sw_if_index,
178                                          sw_if_index);
179         }
180     }
181     return;
182 }
183
184 static ip_adjacency_t*
185 adj_nbr_alloc (fib_protocol_t nh_proto,
186                vnet_link_t link_type,
187                const ip46_address_t *nh_addr,
188                u32 sw_if_index)
189 {
190     ip_adjacency_t *adj;
191
192     adj = adj_alloc(nh_proto);
193
194     adj_nbr_insert(nh_proto, link_type, nh_addr,
195                    sw_if_index,
196                    adj_get_index(adj));
197
198     /*
199      * since we just added the ADJ we have no rewrite string for it,
200      * so its for ARP
201      */
202     adj->lookup_next_index = IP_LOOKUP_NEXT_ARP;
203     adj->sub_type.nbr.next_hop = *nh_addr;
204     adj->ia_link = link_type;
205     adj->ia_nh_proto = nh_proto;
206     adj->rewrite_header.sw_if_index = sw_if_index;
207     vnet_rewrite_update_mtu(vnet_get_main(), adj->ia_link,
208                             &adj->rewrite_header);
209
210     adj_nbr_evaluate_feature (adj_get_index(adj));
211     return (adj);
212 }
213
214 /*
215  * adj_nbr_add_or_lock
216  *
217  * Add an adjacency for the neighbour requested.
218  *
219  * The key for an adj is:
220  *   - the Next-hops protocol (i.e. v4 or v6)
221  *   - the address of the next-hop
222  *   - the interface the next-hop is reachable through
223  */
224 adj_index_t
225 adj_nbr_add_or_lock (fib_protocol_t nh_proto,
226                      vnet_link_t link_type,
227                      const ip46_address_t *nh_addr,
228                      u32 sw_if_index)
229 {
230     adj_index_t adj_index;
231
232     adj_index = adj_nbr_find(nh_proto, link_type, nh_addr, sw_if_index);
233
234     if (ADJ_INDEX_INVALID == adj_index)
235     {
236         ip_adjacency_t *adj;
237         vnet_main_t *vnm;
238
239         vnm = vnet_get_main();
240         adj = adj_nbr_alloc(nh_proto, link_type, nh_addr, sw_if_index);
241         adj_index = adj_get_index(adj);
242         adj_lock(adj_index);
243
244         if (ip46_address_is_equal(&ADJ_BCAST_ADDR, nh_addr))
245         {
246             adj->lookup_next_index = IP_LOOKUP_NEXT_BCAST;
247         }
248
249         vnet_rewrite_init(vnm, sw_if_index, link_type,
250                           adj_get_nd_node(nh_proto),
251                           vnet_tx_node_index_for_sw_interface(vnm, sw_if_index),
252                           &adj->rewrite_header);
253
254         /*
255          * we need a rewrite where the destination IP address is converted
256          * to the appropriate link-layer address. This is interface specific.
257          * So ask the interface to do it.
258          */
259         vnet_update_adjacency_for_sw_interface(vnm, sw_if_index, adj_index);
260     }
261     else
262     {
263         adj_lock(adj_index);
264     }
265
266     adj_delegate_adj_created(adj_get(adj_index));
267     return (adj_index);
268 }
269
270 adj_index_t
271 adj_nbr_add_or_lock_w_rewrite (fib_protocol_t nh_proto,
272                                vnet_link_t link_type,
273                                const ip46_address_t *nh_addr,
274                                u32 sw_if_index,
275                                u8 *rewrite)
276 {
277     adj_index_t adj_index;
278
279     adj_index = adj_nbr_find(nh_proto, link_type, nh_addr, sw_if_index);
280
281     if (ADJ_INDEX_INVALID == adj_index)
282     {
283         ip_adjacency_t *adj;
284
285         adj = adj_nbr_alloc(nh_proto, link_type, nh_addr, sw_if_index);
286         adj->rewrite_header.sw_if_index = sw_if_index;
287         adj_index = adj_get_index(adj);
288     }
289
290     adj_lock(adj_index);
291     adj_nbr_update_rewrite(adj_index,
292                            ADJ_NBR_REWRITE_FLAG_COMPLETE,
293                            rewrite);
294
295     adj_delegate_adj_created(adj_get(adj_index));
296
297     return (adj_index);
298 }
299
300 /**
301  * adj_nbr_update_rewrite
302  *
303  * Update the adjacency's rewrite string. A NULL string implies the
304  * rewrite is reset (i.e. when ARP/ND entry is gone).
305  * NB: the adj being updated may be handling traffic in the DP.
306  */
307 void
308 adj_nbr_update_rewrite (adj_index_t adj_index,
309                         adj_nbr_rewrite_flag_t flags,
310                         u8 *rewrite)
311 {
312     ip_adjacency_t *adj;
313
314     ASSERT(ADJ_INDEX_INVALID != adj_index);
315
316     adj = adj_get(adj_index);
317
318     if (flags & ADJ_NBR_REWRITE_FLAG_COMPLETE)
319     {
320         /*
321          * update the adj's rewrite string and build the arc
322          * from the rewrite node to the interface's TX node
323          */
324         adj_nbr_update_rewrite_internal(adj, IP_LOOKUP_NEXT_REWRITE,
325                                         adj_get_rewrite_node(adj->ia_link),
326                                         vnet_tx_node_index_for_sw_interface(
327                                             vnet_get_main(),
328                                             adj->rewrite_header.sw_if_index),
329                                         rewrite);
330     }
331     else
332     {
333         adj_nbr_update_rewrite_internal(adj, IP_LOOKUP_NEXT_ARP,
334                                         adj_get_nd_node(adj->ia_nh_proto),
335                                         vnet_tx_node_index_for_sw_interface(
336                                             vnet_get_main(),
337                                             adj->rewrite_header.sw_if_index),
338                                         rewrite);
339     }
340 }
341
342 /**
343  * adj_nbr_update_rewrite_internal
344  *
345  * Update the adjacency's rewrite string. A NULL string implies the
346  * rewrite is reset (i.e. when ARP/ND entry is gone).
347  * NB: the adj being updated may be handling traffic in the DP.
348  */
349 void
350 adj_nbr_update_rewrite_internal (ip_adjacency_t *adj,
351                                  ip_lookup_next_t adj_next_index,
352                                  u32 this_node,
353                                  u32 next_node,
354                                  u8 *rewrite)
355 {
356     ip_adjacency_t *walk_adj;
357     adj_index_t walk_ai, ai;
358     vlib_main_t * vm;
359     u32 old_next;
360     int do_walk;
361
362     vm = vlib_get_main();
363     old_next = adj->lookup_next_index;
364
365     ai = walk_ai = adj_get_index(adj);
366     if (VNET_LINK_MPLS == adj->ia_link)
367     {
368         /*
369          * The link type MPLS has no children in the control plane graph, it only
370          * has children in the data-plane graph. The backwalk is up the former.
371          * So we need to walk from its IP cousin.
372          */
373         walk_ai = adj_nbr_find(adj->ia_nh_proto,
374                                fib_proto_to_link(adj->ia_nh_proto),
375                                &adj->sub_type.nbr.next_hop,
376                                adj->rewrite_header.sw_if_index);
377     }
378
379     /*
380      * Don't call the walk re-entrantly
381      */
382     if (ADJ_INDEX_INVALID != walk_ai)
383     {
384         walk_adj = adj_get(walk_ai);
385         if (ADJ_FLAG_SYNC_WALK_ACTIVE & walk_adj->ia_flags)
386         {
387             do_walk = 0;
388         }
389         else
390         {
391             /*
392              * Prevent re-entrant walk of the same adj
393              */
394             walk_adj->ia_flags |= ADJ_FLAG_SYNC_WALK_ACTIVE;
395             do_walk = 1;
396         }
397     }
398     else
399     {
400         do_walk = 0;
401     }
402
403     /*
404      * lock the adjacencies that are affected by updates this walk will provoke.
405      * Since the aim of the walk is to update children to link to a different
406      * DPO, this adj will no longer be in use and its lock count will drop to 0.
407      * We don't want it to be deleted as part of this endeavour.
408      */
409     adj_lock(ai);
410     adj_lock(walk_ai);
411
412     /*
413      * Updating a rewrite string is not atomic;
414      *  - the rewrite string is too long to write in one instruction
415      *  - when swapping from incomplete to complete, we also need to update
416      *    the VLIB graph next-index of the adj.
417      * ideally we would only want to suspend forwarding via this adj whilst we
418      * do this, but we do not have that level of granularity - it's suspend all
419      * worker threads or nothing.
420      * The other choices are:
421      *  - to mark the adj down and back walk so child load-balances drop this adj
422      *    from the set.
423      *  - update the next_node index of this adj to point to error-drop
424      * both of which will mean for MAC change we will drop for this adj
425      * which is not acceptable. However, when the adj changes type (from
426      * complete to incomplete and vice-versa) the child DPOs, which have the
427      * VLIB graph next node index, will be sending packets to the wrong graph
428      * node. So from the options above, updating the next_node of the adj to
429      * be drop will work, but it relies on each graph node v4/v6/mpls, rewrite/
430      * arp/midchain always be valid w.r.t. a mis-match of adj type and node type
431      * (i.e. a rewrite adj in the arp node). This is not enforceable. Getting it
432      * wrong will lead to hard to find bugs since its a race condition. So we
433      * choose the more reliable method of updating the children to use the drop,
434      * then switching adj's type, then updating the children again. Did I mention
435      * that this doesn't happen often...
436      * So we need to distinguish between the two cases:
437      *  1 - mac change
438      *  2 - adj type change
439      */
440     if (do_walk &&
441         old_next != adj_next_index &&
442         ADJ_INDEX_INVALID != walk_ai)
443     {
444         /*
445          * the adj is changing type. we need to fix all children so that they
446          * stack momentarily on a drop, while the adj changes. If we don't do
447          * this  the children will send packets to a VLIB graph node that does
448          * not correspond to the adj's type - and it goes downhill from there.
449          */
450         fib_node_back_walk_ctx_t bw_ctx = {
451             .fnbw_reason = FIB_NODE_BW_REASON_FLAG_ADJ_DOWN,
452             /*
453              * force this walk to be synchronous. if we don't and a node in the graph
454              * (a heavily shared path-list) chooses to back-ground the walk (make it
455              * async) then it will pause and we will do the adj update below, before
456              * all the children are updated. not good.
457              */
458             .fnbw_flags = FIB_NODE_BW_FLAG_FORCE_SYNC,
459         };
460
461         fib_walk_sync(FIB_NODE_TYPE_ADJ, walk_ai, &bw_ctx);
462         /*
463          * fib_walk_sync may allocate a new adjacency and potentially cuase a
464          * realloc for adj_pool. When that happens, adj pointer is no longer
465          * valid here. We refresh the adj pointer accordingly.
466          */
467         adj = adj_get (ai);
468     }
469
470     /*
471      * If we are just updating the MAC string of the adj (which we also can't
472      * do atomically), then we need to stop packets switching through the adj.
473      * We can't do that on a per-adj basis, so it's all the packets.
474      * If we are updating the type, and we walked back to the children above,
475      * then this barrier serves to flush the queues/frames.
476      */
477     vlib_worker_thread_barrier_sync(vm);
478
479     adj->lookup_next_index = adj_next_index;
480     adj->ia_node_index = this_node;
481
482     if (NULL != rewrite)
483     {
484         /*
485          * new rewrite provided.
486          * fill in the adj's rewrite string, and build the VLIB graph arc.
487          */
488         vnet_rewrite_set_data_internal(&adj->rewrite_header,
489                                        sizeof(adj->rewrite_data),
490                                        rewrite,
491                                        vec_len(rewrite));
492         vec_free(rewrite);
493     }
494     else
495     {
496         vnet_rewrite_clear_data_internal(&adj->rewrite_header,
497                                          sizeof(adj->rewrite_data));
498     }
499     adj->rewrite_header.next_index = vlib_node_add_next(vlib_get_main(),
500                                                         this_node,
501                                                         next_node);
502
503     /*
504      * done with the rewrite update - let the workers loose.
505      */
506     vlib_worker_thread_barrier_release(vm);
507
508     if (do_walk &&
509         (old_next != adj->lookup_next_index) &&
510         (ADJ_INDEX_INVALID != walk_ai))
511     {
512         /*
513          * backwalk to the children so they can stack on the now updated
514          * adjacency
515          */
516         fib_node_back_walk_ctx_t bw_ctx = {
517             .fnbw_reason = FIB_NODE_BW_REASON_FLAG_ADJ_UPDATE,
518         };
519
520         fib_walk_sync(FIB_NODE_TYPE_ADJ, walk_ai, &bw_ctx);
521     }
522     /*
523      * Prevent re-entrant walk of the same adj
524      */
525     if (do_walk)
526     {
527         walk_adj = adj_get(walk_ai);
528         walk_adj->ia_flags &= ~ADJ_FLAG_SYNC_WALK_ACTIVE;
529     }
530
531     adj_delegate_adj_modified(adj);
532     adj_unlock(ai);
533     adj_unlock(walk_ai);
534 }
535
536 typedef struct adj_db_count_ctx_t_ {
537     u64 count;
538 } adj_db_count_ctx_t;
539
540 static int
541 adj_db_count (BVT(clib_bihash_kv) * kvp,
542               void *arg)
543 {
544     adj_db_count_ctx_t * ctx = arg;
545     ctx->count++;
546     return (BIHASH_WALK_CONTINUE);
547 }
548
549 u32
550 adj_nbr_db_size (void)
551 {
552     adj_db_count_ctx_t ctx = {
553         .count = 0,
554     };
555     fib_protocol_t proto;
556     u32 sw_if_index = 0;
557
558     for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
559     {
560         vec_foreach_index(sw_if_index, adj_nbr_tables[proto])
561         {
562             if (NULL != adj_nbr_tables[proto][sw_if_index])
563             {
564                 BV(clib_bihash_foreach_key_value_pair) (
565                     adj_nbr_tables[proto][sw_if_index],
566                     adj_db_count,
567                     &ctx);
568             }
569         }
570     }
571     return (ctx.count);
572 }
573
574 /**
575  * @brief Context for a walk of the adjacency neighbour DB
576  */
577 typedef struct adj_walk_ctx_t_
578 {
579     adj_walk_cb_t awc_cb;
580     void *awc_ctx;
581 } adj_walk_ctx_t;
582
583 static int
584 adj_nbr_walk_cb (BVT(clib_bihash_kv) * kvp,
585                  void *arg)
586 {
587     adj_walk_ctx_t *ctx = arg;
588
589     if (ADJ_WALK_RC_STOP == ctx->awc_cb(kvp->value, ctx->awc_ctx))
590         return (BIHASH_WALK_STOP);
591     return (BIHASH_WALK_CONTINUE);
592 }
593
594 void
595 adj_nbr_walk (u32 sw_if_index,
596               fib_protocol_t adj_nh_proto,
597               adj_walk_cb_t cb,
598               void *ctx)
599 {
600     if (!ADJ_NBR_ITF_OK(adj_nh_proto, sw_if_index))
601         return;
602
603     adj_walk_ctx_t awc = {
604         .awc_ctx = ctx,
605         .awc_cb = cb,
606     };
607
608     BV(clib_bihash_foreach_key_value_pair) (
609         adj_nbr_tables[adj_nh_proto][sw_if_index],
610         adj_nbr_walk_cb,
611         &awc);
612 }
613
614 /**
615  * @brief Walk adjacencies on a link with a given v4 next-hop.
616  * that is visit the adjacencies with different link types.
617  */
618 void
619 adj_nbr_walk_nh4 (u32 sw_if_index,
620                  const ip4_address_t *addr,
621                  adj_walk_cb_t cb,
622                  void *ctx)
623 {
624     if (!ADJ_NBR_ITF_OK(FIB_PROTOCOL_IP4, sw_if_index))
625         return;
626
627     ip46_address_t nh = {
628         .ip4 = *addr,
629     };
630     vnet_link_t linkt;
631     adj_index_t ai;
632
633     FOR_EACH_VNET_LINK(linkt)
634     {
635         ai = adj_nbr_find (FIB_PROTOCOL_IP4, linkt, &nh, sw_if_index);
636
637         if (INDEX_INVALID != ai)
638             cb(ai, ctx);
639     }
640 }
641
642 /**
643  * @brief Walk adjacencies on a link with a given v6 next-hop.
644  * that is visit the adjacencies with different link types.
645  */
646 void
647 adj_nbr_walk_nh6 (u32 sw_if_index,
648                  const ip6_address_t *addr,
649                  adj_walk_cb_t cb,
650                  void *ctx)
651 {
652     if (!ADJ_NBR_ITF_OK(FIB_PROTOCOL_IP6, sw_if_index))
653         return;
654
655     ip46_address_t nh = {
656         .ip6 = *addr,
657     };
658     vnet_link_t linkt;
659     adj_index_t ai;
660
661     FOR_EACH_VNET_LINK(linkt)
662     {
663         ai = adj_nbr_find (FIB_PROTOCOL_IP6, linkt, &nh, sw_if_index);
664
665         if (INDEX_INVALID != ai)
666             cb(ai, ctx);
667     }
668 }
669
670 /**
671  * @brief Walk adjacencies on a link with a given next-hop.
672  * that is visit the adjacencies with different link types.
673  */
674 void
675 adj_nbr_walk_nh (u32 sw_if_index,
676                  fib_protocol_t adj_nh_proto,
677                  const ip46_address_t *nh,
678                  adj_walk_cb_t cb,
679                  void *ctx)
680 {
681     if (!ADJ_NBR_ITF_OK(adj_nh_proto, sw_if_index))
682         return;
683
684     switch (adj_nh_proto)
685     {
686     case FIB_PROTOCOL_IP4:
687         adj_nbr_walk_nh4(sw_if_index, &nh->ip4, cb, ctx);
688         break; 
689     case FIB_PROTOCOL_IP6:
690         adj_nbr_walk_nh6(sw_if_index, &nh->ip6, cb, ctx);
691         break;
692     case FIB_PROTOCOL_MPLS:
693         ASSERT(0);
694         break;
695     }
696 }
697
698 /**
699  * Flags associated with the interface state walks
700  */
701 typedef enum adj_nbr_interface_flags_t_
702 {
703     ADJ_NBR_INTERFACE_UP = (1 << 0),
704 } adj_nbr_interface_flags_t;
705
706 /**
707  * Context for the state change walk of the DB
708  */
709 typedef struct adj_nbr_interface_state_change_ctx_t_
710 {
711     /**
712      * Flags on the interface
713      */
714     adj_nbr_interface_flags_t flags;
715 } adj_nbr_interface_state_change_ctx_t;
716
717 static adj_walk_rc_t
718 adj_nbr_interface_state_change_one (adj_index_t ai,
719                                     void *arg)
720 {
721     /*
722      * Back walk the graph to inform the forwarding entries
723      * that this interface state has changed. Do this synchronously
724      * since this is the walk that provides convergence
725      */
726     adj_nbr_interface_state_change_ctx_t *ctx = arg;
727     fib_node_back_walk_ctx_t bw_ctx = {
728         .fnbw_reason = ((ctx->flags & ADJ_NBR_INTERFACE_UP) ?
729                         FIB_NODE_BW_REASON_FLAG_INTERFACE_UP :
730                         FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN),
731         /*
732          * the force sync applies only as far as the first fib_entry.
733          * And it's the fib_entry's we need to converge away from
734          * the adjacencies on the now down link
735          */
736         .fnbw_flags = (!(ctx->flags & ADJ_NBR_INTERFACE_UP) ?
737                        FIB_NODE_BW_FLAG_FORCE_SYNC :
738                        FIB_NODE_BW_FLAG_NONE),
739     };
740     ip_adjacency_t *adj;
741
742     adj = adj_get(ai);
743
744     adj->ia_flags |= ADJ_FLAG_SYNC_WALK_ACTIVE;
745     fib_walk_sync(FIB_NODE_TYPE_ADJ, ai, &bw_ctx);
746     adj->ia_flags &= ~ADJ_FLAG_SYNC_WALK_ACTIVE;
747
748     return (ADJ_WALK_RC_CONTINUE);
749 }
750
751 /**
752  * @brief Registered function for SW interface state changes
753  */
754 static clib_error_t *
755 adj_nbr_sw_interface_state_change (vnet_main_t * vnm,
756                                    u32 sw_if_index,
757                                    u32 flags)
758 {
759     fib_protocol_t proto;
760
761     /*
762      * walk each adj on the interface and trigger a walk from that adj
763      */
764     for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
765     {
766         adj_nbr_interface_state_change_ctx_t ctx = {
767             .flags = ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
768                       ADJ_NBR_INTERFACE_UP :
769                       0),
770         };
771
772         adj_nbr_walk(sw_if_index, proto,
773                      adj_nbr_interface_state_change_one,
774                      &ctx);
775     }
776
777     return (NULL);
778 }
779
780 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION_PRIO(
781     adj_nbr_sw_interface_state_change,
782     VNET_ITF_FUNC_PRIORITY_HIGH);
783
784 /**
785  * @brief Invoked on each SW interface of a HW interface when the
786  * HW interface state changes
787  */
788 static walk_rc_t
789 adj_nbr_hw_sw_interface_state_change (vnet_main_t * vnm,
790                                       u32 sw_if_index,
791                                       void *arg)
792 {
793     adj_nbr_interface_state_change_ctx_t *ctx = arg;
794     fib_protocol_t proto;
795
796     /*
797      * walk each adj on the interface and trigger a walk from that adj
798      */
799     for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
800     {
801         adj_nbr_walk(sw_if_index, proto,
802                      adj_nbr_interface_state_change_one,
803                      ctx);
804     }
805     return (WALK_CONTINUE);
806 }
807
808 /**
809  * @brief Registered callback for HW interface state changes
810  */
811 static clib_error_t *
812 adj_nbr_hw_interface_state_change (vnet_main_t * vnm,
813                                    u32 hw_if_index,
814                                    u32 flags)
815 {
816     /*
817      * walk SW interface on the HW
818      */
819     adj_nbr_interface_state_change_ctx_t ctx = {
820         .flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
821                   ADJ_NBR_INTERFACE_UP :
822                   0),
823     };
824
825     vnet_hw_interface_walk_sw(vnm, hw_if_index,
826                               adj_nbr_hw_sw_interface_state_change,
827                               &ctx);
828
829     return (NULL);
830 }
831
832 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION_PRIO(
833     adj_nbr_hw_interface_state_change,
834     VNET_ITF_FUNC_PRIORITY_HIGH);
835
836 static adj_walk_rc_t
837 adj_nbr_interface_delete_one (adj_index_t ai,
838                               void *arg)
839 {
840     /*
841      * Back walk the graph to inform the forwarding entries
842      * that this interface has been deleted.
843      */
844     fib_node_back_walk_ctx_t bw_ctx = {
845         .fnbw_reason = FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE,
846     };
847     ip_adjacency_t *adj;
848
849     adj_lock(ai);
850
851     adj = adj_get(ai);
852
853     adj->ia_flags |= ADJ_FLAG_SYNC_WALK_ACTIVE;
854     fib_walk_sync(FIB_NODE_TYPE_ADJ, ai, &bw_ctx);
855     adj->ia_flags &= ~ADJ_FLAG_SYNC_WALK_ACTIVE;
856
857     adj_unlock(ai);
858     return (ADJ_WALK_RC_CONTINUE);
859 }
860
861 /**
862  * adj_nbr_interface_add_del
863  *
864  * Registered to receive interface Add and delete notifications
865  */
866 static clib_error_t *
867 adj_nbr_interface_add_del (vnet_main_t * vnm,
868                            u32 sw_if_index,
869                            u32 is_add)
870 {
871     fib_protocol_t proto;
872
873     if (is_add)
874     {
875         /*
876          * not interested in interface additions. we will not back walk
877          * to resolve paths through newly added interfaces. Why? The control
878          * plane should have the brains to add interfaces first, then routes.
879          * So the case where there are paths with a interface that matches
880          * one just created is the case where the path resolved through an
881          * interface that was deleted, and still has not been removed. The
882          * new interface added, is NO GUARANTEE that the interface being
883          * added now, even though it may have the same sw_if_index, is the
884          * same interface that the path needs. So tough!
885          * If the control plane wants these routes to resolve it needs to
886          * remove and add them again.
887          */
888         return (NULL);
889     }
890
891     for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
892     {
893         adj_nbr_walk(sw_if_index, proto,
894                      adj_nbr_interface_delete_one,
895                      NULL);
896     }
897
898     return (NULL);
899    
900 }
901
902 VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_nbr_interface_add_del);
903
904
905 static adj_walk_rc_t
906 adj_nbr_show_one (adj_index_t ai,
907                   void *arg)
908 {
909     vlib_cli_output (arg, "[@%d]  %U",
910                      ai,
911                      format_ip_adjacency, ai,
912                      FORMAT_IP_ADJACENCY_NONE);
913
914     return (ADJ_WALK_RC_CONTINUE);
915 }
916
917 static clib_error_t *
918 adj_nbr_show (vlib_main_t * vm,
919               unformat_input_t * input,
920               vlib_cli_command_t * cmd)
921 {
922     adj_index_t ai = ADJ_INDEX_INVALID;
923     ip46_address_t nh = ip46_address_initializer;
924     u32 sw_if_index = ~0;
925
926     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
927     {
928         if (unformat (input, "%U",
929                       unformat_vnet_sw_interface, vnet_get_main(),
930                       &sw_if_index))
931             ;
932         else if (unformat (input, "%U",
933                            unformat_ip46_address, &nh, IP46_TYPE_ANY))
934             ;
935         else if (unformat (input, "%d", &ai))
936             ;
937         else
938             break;
939     }
940
941     if (ADJ_INDEX_INVALID != ai)
942     {
943         vlib_cli_output (vm, "[@%d] %U",
944                          ai,
945                          format_ip_adjacency, ai,
946                          FORMAT_IP_ADJACENCY_DETAIL);
947     }
948     else if (~0 != sw_if_index)
949     {
950         fib_protocol_t proto;
951
952         if (ip46_address_is_zero(&nh))
953         {
954             for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
955             {
956                 adj_nbr_walk(sw_if_index, proto,
957                              adj_nbr_show_one,
958                              vm);
959             }
960         }
961         else
962         {
963             proto = (ip46_address_is_ip4(&nh) ?
964                      FIB_PROTOCOL_IP4 :
965                      FIB_PROTOCOL_IP6);
966             adj_nbr_walk_nh(sw_if_index, proto, &nh,
967                             adj_nbr_show_one,
968                             vm);
969         }
970     }
971     else
972     {
973         fib_protocol_t proto;
974
975         for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
976         {
977             vec_foreach_index(sw_if_index, adj_nbr_tables[proto])
978             {
979                 adj_nbr_walk(sw_if_index, proto,
980                              adj_nbr_show_one,
981                              vm);
982             }
983         }
984     }
985
986     return 0;
987 }
988
989 /*?
990  * Show all neighbour adjacencies.
991  * @cliexpar
992  * @cliexstart{sh adj nbr}
993  * [@2] ipv4 via 1.0.0.2 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
994  * [@3] mpls via 1.0.0.2 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
995  * [@4] ipv4 via 1.0.0.3 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
996  * [@5] mpls via 1.0.0.3 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
997  * @cliexend
998  ?*/
999 VLIB_CLI_COMMAND (ip4_show_fib_command, static) = {
1000     .path = "show adj nbr",
1001     .short_help = "show adj nbr [<adj_index>] [interface]",
1002     .function = adj_nbr_show,
1003 };
1004
1005 u8*
1006 format_adj_nbr_incomplete (u8* s, va_list *ap)
1007 {
1008     index_t index = va_arg(*ap, index_t);
1009     CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
1010     vnet_main_t * vnm = vnet_get_main();
1011     ip_adjacency_t * adj = adj_get(index);
1012
1013     s = format (s, "arp-%U", format_vnet_link, adj->ia_link);
1014     s = format (s, ": via %U",
1015                 format_ip46_address, &adj->sub_type.nbr.next_hop,
1016                 adj_proto_to_46(adj->ia_nh_proto));
1017     s = format (s, " %U",
1018                 format_vnet_sw_if_index_name,
1019                 vnm, adj->rewrite_header.sw_if_index);
1020
1021     return (s);
1022 }
1023
1024 u8*
1025 format_adj_nbr (u8* s, va_list *ap)
1026 {
1027     index_t index = va_arg(*ap, index_t);
1028     CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
1029     ip_adjacency_t * adj = adj_get(index);
1030
1031     s = format (s, "%U", format_vnet_link, adj->ia_link);
1032     s = format (s, " via %U ",
1033                 format_ip46_address, &adj->sub_type.nbr.next_hop,
1034                 adj_proto_to_46(adj->ia_nh_proto));
1035     s = format (s, "%U",
1036                 format_vnet_rewrite,
1037                 &adj->rewrite_header, sizeof (adj->rewrite_data), 0);
1038
1039     return (s);
1040 }
1041
1042 static void
1043 adj_dpo_lock (dpo_id_t *dpo)
1044 {
1045     adj_lock(dpo->dpoi_index);
1046 }
1047 static void
1048 adj_dpo_unlock (dpo_id_t *dpo)
1049 {
1050     adj_unlock(dpo->dpoi_index);
1051 }
1052
1053 static void
1054 adj_mem_show (void)
1055 {
1056     fib_show_memory_usage("Adjacency",
1057                           pool_elts(adj_pool),
1058                           pool_len(adj_pool),
1059                           sizeof(ip_adjacency_t));
1060 }
1061
1062 const static dpo_vft_t adj_nbr_dpo_vft = {
1063     .dv_lock = adj_dpo_lock,
1064     .dv_unlock = adj_dpo_unlock,
1065     .dv_format = format_adj_nbr,
1066     .dv_mem_show = adj_mem_show,
1067     .dv_get_urpf = adj_dpo_get_urpf,
1068 };
1069 const static dpo_vft_t adj_nbr_incompl_dpo_vft = {
1070     .dv_lock = adj_dpo_lock,
1071     .dv_unlock = adj_dpo_unlock,
1072     .dv_format = format_adj_nbr_incomplete,
1073     .dv_get_urpf = adj_dpo_get_urpf,
1074 };
1075
1076 /**
1077  * @brief The per-protocol VLIB graph nodes that are assigned to an adjacency
1078  *        object.
1079  *
1080  * this means that these graph nodes are ones from which a nbr is the
1081  * parent object in the DPO-graph.
1082  */
1083 const static char* const nbr_ip4_nodes[] =
1084 {
1085     "ip4-rewrite",
1086     NULL,
1087 };
1088 const static char* const nbr_ip6_nodes[] =
1089 {
1090     "ip6-rewrite",
1091     NULL,
1092 };
1093 const static char* const nbr_mpls_nodes[] =
1094 {
1095     "mpls-output",
1096     NULL,
1097 };
1098 const static char* const nbr_ethernet_nodes[] =
1099 {
1100     "adj-l2-rewrite",
1101     NULL,
1102 };
1103 const static char* const * const nbr_nodes[DPO_PROTO_NUM] =
1104 {
1105     [DPO_PROTO_IP4]  = nbr_ip4_nodes,
1106     [DPO_PROTO_IP6]  = nbr_ip6_nodes,
1107     [DPO_PROTO_MPLS] = nbr_mpls_nodes,
1108     [DPO_PROTO_ETHERNET] = nbr_ethernet_nodes,
1109 };
1110
1111 const static char* const nbr_incomplete_ip4_nodes[] =
1112 {
1113     "ip4-arp",
1114     NULL,
1115 };
1116 const static char* const nbr_incomplete_ip6_nodes[] =
1117 {
1118     "ip6-discover-neighbor",
1119     NULL,
1120 };
1121 const static char* const nbr_incomplete_mpls_nodes[] =
1122 {
1123     "mpls-adj-incomplete",
1124     NULL,
1125 };
1126
1127 const static char* const * const nbr_incomplete_nodes[DPO_PROTO_NUM] =
1128 {
1129     [DPO_PROTO_IP4]  = nbr_incomplete_ip4_nodes,
1130     [DPO_PROTO_IP6]  = nbr_incomplete_ip6_nodes,
1131     [DPO_PROTO_MPLS] = nbr_incomplete_mpls_nodes,
1132 };
1133
1134 void
1135 adj_nbr_module_init (void)
1136 {
1137     dpo_register(DPO_ADJACENCY,
1138                  &adj_nbr_dpo_vft,
1139                  nbr_nodes);
1140     dpo_register(DPO_ADJACENCY_INCOMPLETE,
1141                  &adj_nbr_incompl_dpo_vft,
1142                  nbr_incomplete_nodes);
1143 }