fib: Move the adjacency midchain nodes into a separate file
[vpp.git] / src / vnet / adj / adj_midchain.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/adj/adj_l2.h>
19 #include <vnet/adj/adj_nsh.h>
20 #include <vnet/adj/adj_midchain.h>
21 #include <vnet/dpo/drop_dpo.h>
22 #include <vnet/dpo/load_balance.h>
23 #include <vnet/fib/fib_walk.h>
24 #include <vnet/fib/fib_entry.h>
25 #include <vnet/ip/ip4_inlines.h>
26 #include <vnet/ip/ip6_inlines.h>
27
28 u8
29 adj_is_midchain (adj_index_t ai)
30 {
31     ip_adjacency_t *adj;
32
33     adj = adj_get(ai);
34
35     switch (adj->lookup_next_index)
36     {
37     case IP_LOOKUP_NEXT_MIDCHAIN:
38     case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
39         return (1);
40     case IP_LOOKUP_NEXT_ARP:
41     case IP_LOOKUP_NEXT_GLEAN:
42     case IP_LOOKUP_NEXT_BCAST:
43     case IP_LOOKUP_NEXT_MCAST:
44     case IP_LOOKUP_NEXT_DROP:
45     case IP_LOOKUP_NEXT_PUNT:
46     case IP_LOOKUP_NEXT_LOCAL:
47     case IP_LOOKUP_NEXT_REWRITE:
48     case IP_LOOKUP_NEXT_ICMP_ERROR:
49     case IP_LOOKUP_N_NEXT:
50         return (0);
51     }
52
53     return (0);
54 }
55
56 static inline u32
57 adj_get_midchain_node (vnet_link_t link)
58 {
59     switch (link) {
60     case VNET_LINK_IP4:
61         return (ip4_midchain_node.index);
62     case VNET_LINK_IP6:
63         return (ip6_midchain_node.index);
64     case VNET_LINK_MPLS:
65         return (mpls_midchain_node.index);
66     case VNET_LINK_ETHERNET:
67         return (adj_l2_midchain_node.index);
68     case VNET_LINK_NSH:
69         return (adj_nsh_midchain_node.index);
70     case VNET_LINK_ARP:
71         break;
72     }
73     ASSERT(0);
74     return (0);
75 }
76
77 static u8
78 adj_midchain_get_feature_arc_index_for_link_type (const ip_adjacency_t *adj)
79 {
80     u8 arc = (u8) ~0;
81     switch (adj->ia_link)
82     {
83     case VNET_LINK_IP4:
84         {
85             arc = ip4_main.lookup_main.output_feature_arc_index;
86             break;
87         }
88     case VNET_LINK_IP6:
89         {
90             arc = ip6_main.lookup_main.output_feature_arc_index;
91             break;
92         }
93     case VNET_LINK_MPLS:
94         {
95             arc = mpls_main.output_feature_arc_index;
96             break;
97         }
98     case VNET_LINK_ETHERNET:
99         {
100             arc = ethernet_main.output_feature_arc_index;
101             break;
102         }
103     case VNET_LINK_NSH:
104         {
105           arc = nsh_main_placeholder.output_feature_arc_index;
106           break;
107         }
108     case VNET_LINK_ARP:
109         ASSERT(0);
110         break;
111     }
112
113     ASSERT (arc != (u8) ~0);
114
115     return (arc);
116 }
117
118 static u32
119 adj_nbr_midchain_get_tx_node (ip_adjacency_t *adj)
120 {
121     return ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_NO_COUNT) ?
122             adj_midchain_tx_no_count_node.index :
123             adj_midchain_tx_node.index);
124 }
125
126 /**
127  * adj_midchain_setup
128  *
129  * Setup the adj as a mid-chain
130  */
131 void
132 adj_midchain_teardown (ip_adjacency_t *adj)
133 {
134     vlib_main_t *vm = vlib_get_main();
135
136     dpo_reset(&adj->sub_type.midchain.next_dpo);
137
138     vlib_worker_thread_barrier_sync(vm);
139     adj->ia_cfg_index = vnet_feature_modify_end_node(
140         adj_midchain_get_feature_arc_index_for_link_type (adj),
141         adj->rewrite_header.sw_if_index,
142         vlib_get_node_by_name (vlib_get_main(),
143                                (u8*) "interface-output")->index);
144     vlib_worker_thread_barrier_release(vm);
145 }
146
147 /**
148  * adj_midchain_setup
149  *
150  * Setup the adj as a mid-chain
151  */
152 void
153 adj_midchain_setup (adj_index_t adj_index,
154                     adj_midchain_fixup_t fixup,
155                     const void *data,
156                     adj_flags_t flags)
157 {
158     vlib_main_t *vm = vlib_get_main();
159     ip_adjacency_t *adj;
160     u32 tx_node;
161
162     ASSERT(ADJ_INDEX_INVALID != adj_index);
163
164     adj = adj_get(adj_index);
165
166     adj->sub_type.midchain.fixup_func = fixup;
167     adj->sub_type.midchain.fixup_data = data;
168     adj->sub_type.midchain.fei = FIB_NODE_INDEX_INVALID;
169     adj->ia_flags |= flags;
170
171     if (flags & ADJ_FLAG_MIDCHAIN_FIXUP_IP4O4_HDR)
172     {
173         adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_IP4_O_4;
174     }
175     else
176     {
177         adj->rewrite_header.flags &= ~VNET_REWRITE_FIXUP_IP4_O_4;
178     }
179     if (!(flags & ADJ_FLAG_MIDCHAIN_FIXUP_FLOW_HASH))
180     {
181         adj->rewrite_header.flags &= ~VNET_REWRITE_FIXUP_FLOW_HASH;
182     }
183
184     tx_node = adj_nbr_midchain_get_tx_node(adj);
185
186     vlib_worker_thread_barrier_sync(vm);
187     adj->ia_cfg_index = vnet_feature_modify_end_node(
188         adj_midchain_get_feature_arc_index_for_link_type (adj),
189         adj->rewrite_header.sw_if_index,
190         tx_node);
191     vlib_worker_thread_barrier_release(vm);
192
193     /*
194      * stack the midchain on the drop so it's ready to forward in the adj-midchain-tx.
195      * The graph arc used/created here is from the midchain-tx node to the
196      * child's registered node. This is because post adj processing the next
197      * node are any output features, then the midchain-tx.  from there we
198      * need to get to the stacked child's node.
199      */
200     dpo_stack_from_node(tx_node,
201                         &adj->sub_type.midchain.next_dpo,
202                         drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
203 }
204
205 /**
206  * adj_nbr_midchain_update_rewrite
207  *
208  * Update the adjacency's rewrite string. A NULL string implies the
209  * rewrite is reset (i.e. when ARP/ND entry is gone).
210  * NB: the adj being updated may be handling traffic in the DP.
211  */
212 void
213 adj_nbr_midchain_update_rewrite (adj_index_t adj_index,
214                                  adj_midchain_fixup_t fixup,
215                                  const void *fixup_data,
216                                  adj_flags_t flags,
217                                  u8 *rewrite)
218 {
219     ip_adjacency_t *adj;
220
221     ASSERT(ADJ_INDEX_INVALID != adj_index);
222
223     adj = adj_get(adj_index);
224
225     /*
226      * one time only update. since we don't support changing the tunnel
227      * src,dst, this is all we need.
228      */
229     if (adj->lookup_next_index != IP_LOOKUP_NEXT_MIDCHAIN &&
230         adj->lookup_next_index != IP_LOOKUP_NEXT_MCAST_MIDCHAIN)
231     {
232         adj_midchain_setup(adj_index, fixup, fixup_data, flags);
233     }
234
235     /*
236      * update the rewrite with the workers paused.
237      */
238     adj_nbr_update_rewrite_internal(adj,
239                                     IP_LOOKUP_NEXT_MIDCHAIN,
240                                     adj_get_midchain_node(adj->ia_link),
241                                     adj_nbr_midchain_get_tx_node(adj),
242                                     rewrite);
243 }
244
245 void
246 adj_nbr_midchain_update_next_node (adj_index_t adj_index,
247                                    u32 next_node)
248 {
249     ip_adjacency_t *adj;
250     vlib_main_t * vm;
251
252     ASSERT(ADJ_INDEX_INVALID != adj_index);
253
254     adj = adj_get(adj_index);
255     vm = vlib_get_main();
256
257     vlib_worker_thread_barrier_sync(vm);
258
259     adj->rewrite_header.next_index = vlib_node_add_next(vlib_get_main(),
260                                                         adj->ia_node_index,
261                                                         next_node);
262
263     adj->ia_cfg_index = vnet_feature_modify_end_node(
264         adj_midchain_get_feature_arc_index_for_link_type (adj),
265         adj->rewrite_header.sw_if_index,
266         next_node);
267
268     vlib_worker_thread_barrier_release(vm);
269 }
270
271 void
272 adj_nbr_midchain_reset_next_node (adj_index_t adj_index)
273 {
274     ip_adjacency_t *adj;
275     vlib_main_t * vm;
276
277     ASSERT(ADJ_INDEX_INVALID != adj_index);
278
279     adj = adj_get(adj_index);
280     vm = vlib_get_main();
281
282     vlib_worker_thread_barrier_sync(vm);
283
284     adj->rewrite_header.next_index =
285         vlib_node_add_next(vlib_get_main(),
286                            adj->ia_node_index,
287                            adj_nbr_midchain_get_tx_node(adj));
288
289     adj->ia_cfg_index = vnet_feature_modify_end_node(
290         adj_midchain_get_feature_arc_index_for_link_type (adj),
291         adj->rewrite_header.sw_if_index,
292         adj_nbr_midchain_get_tx_node(adj));
293
294     vlib_worker_thread_barrier_release(vm);
295 }
296
297 /**
298  * adj_nbr_midchain_unstack
299  *
300  * Unstack the adj. stack it on drop
301  */
302 void
303 adj_nbr_midchain_unstack (adj_index_t adj_index)
304 {
305     fib_node_index_t *entry_indicies, tmp;
306     ip_adjacency_t *adj;
307
308     ASSERT(ADJ_INDEX_INVALID != adj_index);
309     adj = adj_get (adj_index);
310
311     /*
312      * check to see if this unstacking breaks a recursion loop
313      */
314     entry_indicies = NULL;
315     tmp = adj->sub_type.midchain.fei;
316     adj->sub_type.midchain.fei = FIB_NODE_INDEX_INVALID;
317
318     if (FIB_NODE_INDEX_INVALID != tmp)
319     {
320         fib_entry_recursive_loop_detect(tmp, &entry_indicies);
321         vec_free(entry_indicies);
322     }
323
324     /*
325      * stack on the drop
326      */
327     dpo_stack(DPO_ADJACENCY_MIDCHAIN,
328               vnet_link_to_dpo_proto(adj->ia_link),
329               &adj->sub_type.midchain.next_dpo,
330               drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
331     CLIB_MEMORY_BARRIER();
332 }
333
334 void
335 adj_nbr_midchain_stack_on_fib_entry (adj_index_t ai,
336                                      fib_node_index_t fei,
337                                      fib_forward_chain_type_t fct)
338 {
339     fib_node_index_t *entry_indicies;
340     dpo_id_t tmp = DPO_INVALID;
341     ip_adjacency_t *adj;
342
343     adj = adj_get (ai);
344
345     /*
346      * check to see if this stacking will form a recursion loop
347      */
348     entry_indicies = NULL;
349     adj->sub_type.midchain.fei = fei;
350
351     if (fib_entry_recursive_loop_detect(adj->sub_type.midchain.fei, &entry_indicies))
352     {
353         /*
354          * loop formed, stack on the drop.
355          */
356         dpo_copy(&tmp, drop_dpo_get(fib_forw_chain_type_to_dpo_proto(fct)));
357     }
358     else
359     {
360         fib_entry_contribute_forwarding (fei, fct, &tmp);
361
362         if (DPO_LOAD_BALANCE == tmp.dpoi_type)
363         {
364             load_balance_t *lb;
365
366             lb = load_balance_get (tmp.dpoi_index);
367
368             if ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_IP_STACK) ||
369                 lb->lb_n_buckets == 1)
370             {
371                 /*
372                  * do that hash now and stack on the choice.
373                  * If the choice is an incomplete adj then we will need a poke when
374                  * it becomes complete. This happens since the adj update walk propagates
375                  * as far a recursive paths.
376                  */
377                 const dpo_id_t *choice;
378                 int hash;
379
380                 if (FIB_FORW_CHAIN_TYPE_UNICAST_IP4 == fct)
381                 {
382                     hash = ip4_compute_flow_hash ((ip4_header_t *) adj_get_rewrite (ai),
383                                                   lb->lb_hash_config);
384                 }
385                 else if (FIB_FORW_CHAIN_TYPE_UNICAST_IP6 == fct)
386                 {
387                     hash = ip6_compute_flow_hash ((ip6_header_t *) adj_get_rewrite (ai),
388                                                   lb->lb_hash_config);
389                 }
390                 else
391                 {
392                     hash = 0;
393                     ASSERT(0);
394                 }
395
396                 choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
397                 dpo_copy (&tmp, choice);
398             }
399             else if (lb->lb_n_buckets > 1)
400             {
401                 /*
402                  * the client has chosen not to use the stacking to select a
403                  * bucket, and there are more than one buckets. there's no
404                  * value in using the midchain's fixed rewrite string to select
405                  * the path, so force a flow hash on the inner.
406                  */
407                 adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_FLOW_HASH;
408             }
409
410             if (adj->ia_flags & ADJ_FLAG_MIDCHAIN_FIXUP_FLOW_HASH)
411             {
412                 /*
413                  * The client, for reasons unbeknownst to adj, wants to force
414                  * a flow hash on the inner, we will oblige.
415                  */
416                 adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_FLOW_HASH;
417             }
418         }
419     }
420     adj_nbr_midchain_stack (ai, &tmp);
421     dpo_reset(&tmp);
422     vec_free(entry_indicies);
423 }
424
425 /**
426  * adj_nbr_midchain_stack
427  */
428 void
429 adj_nbr_midchain_stack (adj_index_t adj_index,
430                         const dpo_id_t *next)
431 {
432     ip_adjacency_t *adj;
433
434     ASSERT(ADJ_INDEX_INVALID != adj_index);
435
436     adj = adj_get(adj_index);
437
438     ASSERT((IP_LOOKUP_NEXT_MIDCHAIN == adj->lookup_next_index) ||
439            (IP_LOOKUP_NEXT_MCAST_MIDCHAIN == adj->lookup_next_index));
440
441     dpo_stack_from_node(adj_nbr_midchain_get_tx_node(adj),
442                         &adj->sub_type.midchain.next_dpo,
443                         next);
444 }
445
446 int
447 adj_ndr_midchain_recursive_loop_detect (adj_index_t ai,
448                                         fib_node_index_t **entry_indicies)
449 {
450     fib_node_index_t *entry_index, *entries;
451     ip_adjacency_t * adj;
452
453     adj = adj_get(ai);
454     entries = *entry_indicies;
455
456     vec_foreach(entry_index, entries)
457     {
458         if (*entry_index == adj->sub_type.midchain.fei)
459         {
460             /*
461              * The entry this midchain links to is already in the set
462              * of visited entries, this is a loop
463              */
464             adj->ia_flags |= ADJ_FLAG_MIDCHAIN_LOOPED;
465             return (1);
466         }
467     }
468
469     adj->ia_flags &= ~ADJ_FLAG_MIDCHAIN_LOOPED;
470     return (0);
471 }
472
473 u8*
474 format_adj_midchain (u8* s, va_list *ap)
475 {
476     index_t index = va_arg(*ap, index_t);
477     u32 indent = va_arg(*ap, u32);
478     ip_adjacency_t * adj = adj_get(index);
479
480     s = format (s, "%U", format_vnet_link, adj->ia_link);
481     if (adj->rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)
482         s = format(s, " [features]");
483     s = format (s, " via %U",
484                 format_ip46_address, &adj->sub_type.nbr.next_hop,
485                 adj_proto_to_46(adj->ia_nh_proto));
486     s = format (s, " %U",
487                 format_vnet_rewrite,
488                 &adj->rewrite_header, sizeof (adj->rewrite_data), indent);
489     s = format (s, "\n%Ustacked-on",
490                 format_white_space, indent);
491
492     if (FIB_NODE_INDEX_INVALID != adj->sub_type.midchain.fei)
493     {
494         s = format (s, " entry:%d", adj->sub_type.midchain.fei);
495
496     }
497     s = format (s, ":\n%U%U",
498                 format_white_space, indent+2,
499                 format_dpo_id, &adj->sub_type.midchain.next_dpo, indent+2);
500
501     return (s);
502 }
503
504 static void
505 adj_dpo_lock (dpo_id_t *dpo)
506 {
507     adj_lock(dpo->dpoi_index);
508 }
509 static void
510 adj_dpo_unlock (dpo_id_t *dpo)
511 {
512     adj_unlock(dpo->dpoi_index);
513 }
514
515 const static dpo_vft_t adj_midchain_dpo_vft = {
516     .dv_lock = adj_dpo_lock,
517     .dv_unlock = adj_dpo_unlock,
518     .dv_format = format_adj_midchain,
519     .dv_get_urpf = adj_dpo_get_urpf,
520     .dv_get_mtu = adj_dpo_get_mtu,
521 };
522
523 /**
524  * @brief The per-protocol VLIB graph nodes that are assigned to a midchain
525  *        object.
526  *
527  * this means that these graph nodes are ones from which a midchain is the
528  * parent object in the DPO-graph.
529  */
530 const static char* const midchain_ip4_nodes[] =
531 {
532     "ip4-midchain",
533     NULL,
534 };
535 const static char* const midchain_ip6_nodes[] =
536 {
537     "ip6-midchain",
538     NULL,
539 };
540 const static char* const midchain_mpls_nodes[] =
541 {
542     "mpls-midchain",
543     NULL,
544 };
545 const static char* const midchain_ethernet_nodes[] =
546 {
547     "adj-l2-midchain",
548     NULL,
549 };
550 const static char* const midchain_nsh_nodes[] =
551 {
552     "adj-nsh-midchain",
553     NULL,
554 };
555
556 const static char* const * const midchain_nodes[DPO_PROTO_NUM] =
557 {
558     [DPO_PROTO_IP4]  = midchain_ip4_nodes,
559     [DPO_PROTO_IP6]  = midchain_ip6_nodes,
560     [DPO_PROTO_MPLS] = midchain_mpls_nodes,
561     [DPO_PROTO_ETHERNET] = midchain_ethernet_nodes,
562     [DPO_PROTO_NSH] = midchain_nsh_nodes,
563 };
564
565 void
566 adj_midchain_module_init (void)
567 {
568     dpo_register(DPO_ADJACENCY_MIDCHAIN, &adj_midchain_dpo_vft, midchain_nodes);
569 }