misc: Break the big IP header files to improve compile time
[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/ethernet/arp_packet.h>
22 #include <vnet/dpo/drop_dpo.h>
23 #include <vnet/dpo/load_balance.h>
24 #include <vnet/fib/fib_walk.h>
25 #include <vnet/fib/fib_entry.h>
26 #include <vnet/ip/ip4_inlines.h>
27 #include <vnet/ip/ip6_inlines.h>
28
29 /**
30  * @brief Trace data for packets traversing the midchain tx node
31  */
32 typedef struct adj_midchain_tx_trace_t_
33 {
34     /**
35      * @brief the midchain adj we are traversing
36      */
37     adj_index_t ai;
38 } adj_midchain_tx_trace_t;
39
40 always_inline uword
41 adj_midchain_tx_inline (vlib_main_t * vm,
42                         vlib_node_runtime_t * node,
43                         vlib_frame_t * frame,
44                         int interface_count)
45 {
46     vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
47     u16 nexts[VLIB_FRAME_SIZE], *next;
48     u32 * from, n_left, thread_index;
49     vnet_main_t *vnm = vnet_get_main ();
50     vnet_interface_main_t *im = &vnm->interface_main;
51
52     thread_index = vm->thread_index;
53     n_left = frame->n_vectors;
54     from = vlib_frame_vector_args (frame);
55
56     vlib_get_buffers (vm, from, bufs, n_left);
57
58     next = nexts;
59     b = bufs;
60
61     while (n_left > 8)
62     {
63         u32 adj_index0, adj_index1, adj_index2, adj_index3;
64         const ip_adjacency_t *adj0, *adj1, *adj2, *adj3;
65         const dpo_id_t *dpo0, *dpo1, *dpo2, *dpo3;
66
67         /* Prefetch next iteration. */
68         {
69             vlib_prefetch_buffer_header (b[4], LOAD);
70             vlib_prefetch_buffer_header (b[5], LOAD);
71             vlib_prefetch_buffer_header (b[6], LOAD);
72             vlib_prefetch_buffer_header (b[7], LOAD);
73         }
74
75         /* Follow the DPO on which the midchain is stacked */
76         adj_index0 = vnet_buffer(b[0])->ip.adj_index[VLIB_TX];
77         adj_index1 = vnet_buffer(b[1])->ip.adj_index[VLIB_TX];
78         adj_index2 = vnet_buffer(b[2])->ip.adj_index[VLIB_TX];
79         adj_index3 = vnet_buffer(b[3])->ip.adj_index[VLIB_TX];
80
81         adj0 = adj_get(adj_index0);
82         adj1 = adj_get(adj_index1);
83         adj2 = adj_get(adj_index2);
84         adj3 = adj_get(adj_index3);
85
86         dpo0 = &adj0->sub_type.midchain.next_dpo;
87         dpo1 = &adj1->sub_type.midchain.next_dpo;
88         dpo2 = &adj2->sub_type.midchain.next_dpo;
89         dpo3 = &adj3->sub_type.midchain.next_dpo;
90
91         next[0] = dpo0->dpoi_next_node;
92         next[1] = dpo1->dpoi_next_node;
93         next[2] = dpo2->dpoi_next_node;
94         next[3] = dpo3->dpoi_next_node;
95
96         vnet_buffer(b[0])->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
97         vnet_buffer(b[1])->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
98         vnet_buffer(b[2])->ip.adj_index[VLIB_TX] = dpo2->dpoi_index;
99         vnet_buffer(b[3])->ip.adj_index[VLIB_TX] = dpo3->dpoi_index;
100
101         if (interface_count)
102         {
103             vlib_increment_combined_counter (im->combined_sw_if_counters
104                                              + VNET_INTERFACE_COUNTER_TX,
105                                              thread_index,
106                                              adj0->rewrite_header.sw_if_index,
107                                              1,
108                                              vlib_buffer_length_in_chain (vm, b[0]));
109             vlib_increment_combined_counter (im->combined_sw_if_counters
110                                              + VNET_INTERFACE_COUNTER_TX,
111                                              thread_index,
112                                              adj1->rewrite_header.sw_if_index,
113                                              1,
114                                              vlib_buffer_length_in_chain (vm, b[1]));
115             vlib_increment_combined_counter (im->combined_sw_if_counters
116                                              + VNET_INTERFACE_COUNTER_TX,
117                                              thread_index,
118                                              adj2->rewrite_header.sw_if_index,
119                                              1,
120                                              vlib_buffer_length_in_chain (vm, b[2]));
121             vlib_increment_combined_counter (im->combined_sw_if_counters
122                                              + VNET_INTERFACE_COUNTER_TX,
123                                              thread_index,
124                                              adj3->rewrite_header.sw_if_index,
125                                              1,
126                                              vlib_buffer_length_in_chain (vm, b[3]));
127         }
128
129         if (PREDICT_FALSE(node->flags & VLIB_NODE_FLAG_TRACE))
130         {
131             if (PREDICT_FALSE(b[0]->flags & VLIB_BUFFER_IS_TRACED))
132             {
133                 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
134                                                               b[0], sizeof (*tr));
135                 tr->ai = adj_index0;
136             }
137             if (PREDICT_FALSE(b[1]->flags & VLIB_BUFFER_IS_TRACED))
138             {
139                 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
140                                                               b[1], sizeof (*tr));
141                 tr->ai = adj_index1;
142             }
143             if (PREDICT_FALSE(b[2]->flags & VLIB_BUFFER_IS_TRACED))
144             {
145                 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
146                                                               b[2], sizeof (*tr));
147                 tr->ai = adj_index2;
148             }
149             if (PREDICT_FALSE(b[3]->flags & VLIB_BUFFER_IS_TRACED))
150             {
151                 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
152                                                               b[3], sizeof (*tr));
153                 tr->ai = adj_index3;
154             }
155         }
156         n_left -= 4;
157         b += 4;
158         next += 4;
159     }
160
161     while (n_left)
162     {
163         const ip_adjacency_t * adj0;
164         const dpo_id_t *dpo0;
165         u32 adj_index0;
166
167         /* Follow the DPO on which the midchain is stacked */
168         adj_index0 = vnet_buffer(b[0])->ip.adj_index[VLIB_TX];
169         adj0 = adj_get(adj_index0);
170         dpo0 = &adj0->sub_type.midchain.next_dpo;
171         next[0] = dpo0->dpoi_next_node;
172         vnet_buffer(b[0])->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
173
174         if (interface_count)
175         {
176             vlib_increment_combined_counter (im->combined_sw_if_counters
177                                              + VNET_INTERFACE_COUNTER_TX,
178                                              thread_index,
179                                              adj0->rewrite_header.sw_if_index,
180                                              1,
181                                              vlib_buffer_length_in_chain (vm, b[0]));
182         }
183
184         if (PREDICT_FALSE(b[0]->flags & VLIB_BUFFER_IS_TRACED))
185         {
186             adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
187                                                           b[0], sizeof (*tr));
188             tr->ai = adj_index0;
189         }
190
191         n_left -= 1;
192         b += 1;
193         next += 1;
194     }
195
196     vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
197
198     return frame->n_vectors;
199 }
200
201 static u8 *
202 format_adj_midchain_tx_trace (u8 * s, va_list * args)
203 {
204     CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
205     CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
206     adj_midchain_tx_trace_t *tr = va_arg (*args, adj_midchain_tx_trace_t*);
207
208     s = format(s, "adj-midchain:[%d]:%U", tr->ai,
209                format_ip_adjacency, tr->ai,
210                FORMAT_IP_ADJACENCY_NONE);
211
212     return (s);
213 }
214
215 static uword
216 adj_midchain_tx (vlib_main_t * vm,
217                  vlib_node_runtime_t * node,
218                  vlib_frame_t * frame)
219 {
220     return (adj_midchain_tx_inline(vm, node, frame, 1));
221 }
222
223 VLIB_REGISTER_NODE (adj_midchain_tx_node) = {
224     .function = adj_midchain_tx,
225     .name = "adj-midchain-tx",
226     .vector_size = sizeof (u32),
227
228     .format_trace = format_adj_midchain_tx_trace,
229
230     .n_next_nodes = 1,
231     .next_nodes = {
232         [0] = "error-drop",
233     },
234 };
235
236 static uword
237 adj_midchain_tx_no_count (vlib_main_t * vm,
238                           vlib_node_runtime_t * node,
239                           vlib_frame_t * frame)
240 {
241     return (adj_midchain_tx_inline(vm, node, frame, 0));
242 }
243
244 VLIB_REGISTER_NODE (adj_midchain_tx_no_count_node) = {
245     .function = adj_midchain_tx_no_count,
246     .name = "adj-midchain-tx-no-count",
247     .vector_size = sizeof (u32),
248
249     .format_trace = format_adj_midchain_tx_trace,
250     .sibling_of = "adj-midchain-tx",
251 };
252
253 #ifndef CLIB_MARCH_VARIANT
254
255 u8
256 adj_is_midchain (adj_index_t ai)
257 {
258     ip_adjacency_t *adj;
259
260     adj = adj_get(ai);
261
262     switch (adj->lookup_next_index)
263     {
264     case IP_LOOKUP_NEXT_MIDCHAIN:
265     case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
266         return (1);
267     case IP_LOOKUP_NEXT_ARP:
268     case IP_LOOKUP_NEXT_GLEAN:
269     case IP_LOOKUP_NEXT_BCAST:
270     case IP_LOOKUP_NEXT_MCAST:
271     case IP_LOOKUP_NEXT_DROP:
272     case IP_LOOKUP_NEXT_PUNT:
273     case IP_LOOKUP_NEXT_LOCAL:
274     case IP_LOOKUP_NEXT_REWRITE:
275     case IP_LOOKUP_NEXT_ICMP_ERROR:
276     case IP_LOOKUP_N_NEXT:
277         return (0);
278     }
279
280     return (0);
281 }
282
283 static inline u32
284 adj_get_midchain_node (vnet_link_t link)
285 {
286     switch (link) {
287     case VNET_LINK_IP4:
288         return (ip4_midchain_node.index);
289     case VNET_LINK_IP6:
290         return (ip6_midchain_node.index);
291     case VNET_LINK_MPLS:
292         return (mpls_midchain_node.index);
293     case VNET_LINK_ETHERNET:
294         return (adj_l2_midchain_node.index);
295     case VNET_LINK_NSH:
296         return (adj_nsh_midchain_node.index);
297     case VNET_LINK_ARP:
298         break;
299     }
300     ASSERT(0);
301     return (0);
302 }
303
304 static u8
305 adj_midchain_get_feature_arc_index_for_link_type (const ip_adjacency_t *adj)
306 {
307     u8 arc = (u8) ~0;
308     switch (adj->ia_link)
309     {
310     case VNET_LINK_IP4:
311         {
312             arc = ip4_main.lookup_main.output_feature_arc_index;
313             break;
314         }
315     case VNET_LINK_IP6:
316         {
317             arc = ip6_main.lookup_main.output_feature_arc_index;
318             break;
319         }
320     case VNET_LINK_MPLS:
321         {
322             arc = mpls_main.output_feature_arc_index;
323             break;
324         }
325     case VNET_LINK_ETHERNET:
326         {
327             arc = ethernet_main.output_feature_arc_index;
328             break;
329         }
330     case VNET_LINK_NSH:
331         {
332           arc = nsh_main_placeholder.output_feature_arc_index;
333           break;
334         }
335     case VNET_LINK_ARP:
336         ASSERT(0);
337         break;
338     }
339
340     ASSERT (arc != (u8) ~0);
341
342     return (arc);
343 }
344
345 static u32
346 adj_nbr_midchain_get_tx_node (ip_adjacency_t *adj)
347 {
348     return ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_NO_COUNT) ?
349             adj_midchain_tx_no_count_node.index :
350             adj_midchain_tx_node.index);
351 }
352
353 /**
354  * adj_midchain_setup
355  *
356  * Setup the adj as a mid-chain
357  */
358 void
359 adj_midchain_teardown (ip_adjacency_t *adj)
360 {
361     vlib_main_t *vm = vlib_get_main();
362
363     dpo_reset(&adj->sub_type.midchain.next_dpo);
364
365     vlib_worker_thread_barrier_sync(vm);
366     adj->ia_cfg_index = vnet_feature_modify_end_node(
367         adj_midchain_get_feature_arc_index_for_link_type (adj),
368         adj->rewrite_header.sw_if_index,
369         vlib_get_node_by_name (vlib_get_main(),
370                                (u8*) "interface-output")->index);
371     vlib_worker_thread_barrier_release(vm);
372 }
373
374 /**
375  * adj_midchain_setup
376  *
377  * Setup the adj as a mid-chain
378  */
379 void
380 adj_midchain_setup (adj_index_t adj_index,
381                     adj_midchain_fixup_t fixup,
382                     const void *data,
383                     adj_flags_t flags)
384 {
385     vlib_main_t *vm = vlib_get_main();
386     ip_adjacency_t *adj;
387     u32 tx_node;
388
389     ASSERT(ADJ_INDEX_INVALID != adj_index);
390
391     adj = adj_get(adj_index);
392
393     adj->sub_type.midchain.fixup_func = fixup;
394     adj->sub_type.midchain.fixup_data = data;
395     adj->sub_type.midchain.fei = FIB_NODE_INDEX_INVALID;
396     adj->ia_flags |= flags;
397
398     if (flags & ADJ_FLAG_MIDCHAIN_FIXUP_IP4O4_HDR)
399     {
400         adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_IP4_O_4;
401     }
402     else
403     {
404         adj->rewrite_header.flags &= ~VNET_REWRITE_FIXUP_IP4_O_4;
405     }
406
407     tx_node = adj_nbr_midchain_get_tx_node(adj);
408
409     vlib_worker_thread_barrier_sync(vm);
410     adj->ia_cfg_index = vnet_feature_modify_end_node(
411         adj_midchain_get_feature_arc_index_for_link_type (adj),
412         adj->rewrite_header.sw_if_index,
413         tx_node);
414     vlib_worker_thread_barrier_release(vm);
415
416     /*
417      * stack the midchain on the drop so it's ready to forward in the adj-midchain-tx.
418      * The graph arc used/created here is from the midchain-tx node to the
419      * child's registered node. This is because post adj processing the next
420      * node are any output features, then the midchain-tx.  from there we
421      * need to get to the stacked child's node.
422      */
423     dpo_stack_from_node(tx_node,
424                         &adj->sub_type.midchain.next_dpo,
425                         drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
426 }
427
428 /**
429  * adj_nbr_midchain_update_rewrite
430  *
431  * Update the adjacency's rewrite string. A NULL string implies the
432  * rewrite is reset (i.e. when ARP/ND entry is gone).
433  * NB: the adj being updated may be handling traffic in the DP.
434  */
435 void
436 adj_nbr_midchain_update_rewrite (adj_index_t adj_index,
437                                  adj_midchain_fixup_t fixup,
438                                  const void *fixup_data,
439                                  adj_flags_t flags,
440                                  u8 *rewrite)
441 {
442     ip_adjacency_t *adj;
443
444     ASSERT(ADJ_INDEX_INVALID != adj_index);
445
446     adj = adj_get(adj_index);
447
448     /*
449      * one time only update. since we don't support changing the tunnel
450      * src,dst, this is all we need.
451      */
452     if (adj->lookup_next_index != IP_LOOKUP_NEXT_MIDCHAIN &&
453         adj->lookup_next_index != IP_LOOKUP_NEXT_MCAST_MIDCHAIN)
454     {
455         adj_midchain_setup(adj_index, fixup, fixup_data, flags);
456     }
457
458     /*
459      * update the rewrite with the workers paused.
460      */
461     adj_nbr_update_rewrite_internal(adj,
462                                     IP_LOOKUP_NEXT_MIDCHAIN,
463                                     adj_get_midchain_node(adj->ia_link),
464                                     adj_nbr_midchain_get_tx_node(adj),
465                                     rewrite);
466 }
467
468 void
469 adj_nbr_midchain_update_next_node (adj_index_t adj_index,
470                                    u32 next_node)
471 {
472     ip_adjacency_t *adj;
473     vlib_main_t * vm;
474
475     ASSERT(ADJ_INDEX_INVALID != adj_index);
476
477     adj = adj_get(adj_index);
478     vm = vlib_get_main();
479
480     vlib_worker_thread_barrier_sync(vm);
481
482     adj->rewrite_header.next_index = vlib_node_add_next(vlib_get_main(),
483                                                         adj->ia_node_index,
484                                                         next_node);
485
486     adj->ia_cfg_index = vnet_feature_modify_end_node(
487         adj_midchain_get_feature_arc_index_for_link_type (adj),
488         adj->rewrite_header.sw_if_index,
489         next_node);
490
491     vlib_worker_thread_barrier_release(vm);
492 }
493
494 void
495 adj_nbr_midchain_reset_next_node (adj_index_t adj_index)
496 {
497     ip_adjacency_t *adj;
498     vlib_main_t * vm;
499
500     ASSERT(ADJ_INDEX_INVALID != adj_index);
501
502     adj = adj_get(adj_index);
503     vm = vlib_get_main();
504
505     vlib_worker_thread_barrier_sync(vm);
506
507     adj->rewrite_header.next_index =
508         vlib_node_add_next(vlib_get_main(),
509                            adj->ia_node_index,
510                            adj_nbr_midchain_get_tx_node(adj));
511
512     adj->ia_cfg_index = vnet_feature_modify_end_node(
513         adj_midchain_get_feature_arc_index_for_link_type (adj),
514         adj->rewrite_header.sw_if_index,
515         adj_nbr_midchain_get_tx_node(adj));
516
517     vlib_worker_thread_barrier_release(vm);
518 }
519
520 /**
521  * adj_nbr_midchain_unstack
522  *
523  * Unstack the adj. stack it on drop
524  */
525 void
526 adj_nbr_midchain_unstack (adj_index_t adj_index)
527 {
528     fib_node_index_t *entry_indicies, tmp;
529     ip_adjacency_t *adj;
530
531     ASSERT(ADJ_INDEX_INVALID != adj_index);
532     adj = adj_get (adj_index);
533
534     /*
535      * check to see if this unstacking breaks a recursion loop
536      */
537     entry_indicies = NULL;
538     tmp = adj->sub_type.midchain.fei;
539     adj->sub_type.midchain.fei = FIB_NODE_INDEX_INVALID;
540
541     if (FIB_NODE_INDEX_INVALID != tmp)
542     {
543         fib_entry_recursive_loop_detect(tmp, &entry_indicies);
544         vec_free(entry_indicies);
545     }
546
547     /*
548      * stack on the drop
549      */
550     dpo_stack(DPO_ADJACENCY_MIDCHAIN,
551               vnet_link_to_dpo_proto(adj->ia_link),
552               &adj->sub_type.midchain.next_dpo,
553               drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
554     CLIB_MEMORY_BARRIER();
555 }
556
557 void
558 adj_nbr_midchain_stack_on_fib_entry (adj_index_t ai,
559                                      fib_node_index_t fei,
560                                      fib_forward_chain_type_t fct)
561 {
562     fib_node_index_t *entry_indicies;
563     dpo_id_t tmp = DPO_INVALID;
564     ip_adjacency_t *adj;
565
566     adj = adj_get (ai);
567
568     /*
569      * check to see if this stacking will form a recursion loop
570      */
571     entry_indicies = NULL;
572     adj->sub_type.midchain.fei = fei;
573
574     if (fib_entry_recursive_loop_detect(adj->sub_type.midchain.fei, &entry_indicies))
575     {
576         /*
577          * loop formed, stack on the drop.
578          */
579         dpo_copy(&tmp, drop_dpo_get(fib_forw_chain_type_to_dpo_proto(fct)));
580     }
581     else
582     {
583         fib_entry_contribute_forwarding (fei, fct, &tmp);
584
585         if ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_IP_STACK) &&
586             (DPO_LOAD_BALANCE == tmp.dpoi_type))
587         {
588             /*
589              * do that hash now and stack on the choice.
590              * If the choice is an incomplete adj then we will need a poke when
591              * it becomes complete. This happens since the adj update walk propagates
592              * as far a recursive paths.
593              */
594             const dpo_id_t *choice;
595             load_balance_t *lb;
596             int hash;
597
598             lb = load_balance_get (tmp.dpoi_index);
599
600             if (FIB_FORW_CHAIN_TYPE_UNICAST_IP4 == fct)
601             {
602                 hash = ip4_compute_flow_hash ((ip4_header_t *) adj_get_rewrite (ai),
603                                               lb->lb_hash_config);
604             }
605             else if (FIB_FORW_CHAIN_TYPE_UNICAST_IP6 == fct)
606             {
607                 hash = ip6_compute_flow_hash ((ip6_header_t *) adj_get_rewrite (ai),
608                                               lb->lb_hash_config);
609             }
610             else
611             {
612                 hash = 0;
613                 ASSERT(0);
614             }
615
616             choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
617             dpo_copy (&tmp, choice);
618         }
619     }
620     adj_nbr_midchain_stack (ai, &tmp);
621     dpo_reset(&tmp);
622     vec_free(entry_indicies);
623 }
624
625 /**
626  * adj_nbr_midchain_stack
627  */
628 void
629 adj_nbr_midchain_stack (adj_index_t adj_index,
630                         const dpo_id_t *next)
631 {
632     ip_adjacency_t *adj;
633
634     ASSERT(ADJ_INDEX_INVALID != adj_index);
635
636     adj = adj_get(adj_index);
637
638     ASSERT((IP_LOOKUP_NEXT_MIDCHAIN == adj->lookup_next_index) ||
639            (IP_LOOKUP_NEXT_MCAST_MIDCHAIN == adj->lookup_next_index));
640
641     dpo_stack_from_node(adj_nbr_midchain_get_tx_node(adj),
642                         &adj->sub_type.midchain.next_dpo,
643                         next);
644 }
645
646 int
647 adj_ndr_midchain_recursive_loop_detect (adj_index_t ai,
648                                         fib_node_index_t **entry_indicies)
649 {
650     fib_node_index_t *entry_index, *entries;
651     ip_adjacency_t * adj;
652
653     adj = adj_get(ai);
654     entries = *entry_indicies;
655
656     vec_foreach(entry_index, entries)
657     {
658         if (*entry_index == adj->sub_type.midchain.fei)
659         {
660             /*
661              * The entry this midchain links to is already in the set
662              * of visited entries, this is a loop
663              */
664             adj->ia_flags |= ADJ_FLAG_MIDCHAIN_LOOPED;
665             return (1);
666         }
667     }
668
669     adj->ia_flags &= ~ADJ_FLAG_MIDCHAIN_LOOPED;
670     return (0);
671 }
672
673 u8*
674 format_adj_midchain (u8* s, va_list *ap)
675 {
676     index_t index = va_arg(*ap, index_t);
677     u32 indent = va_arg(*ap, u32);
678     ip_adjacency_t * adj = adj_get(index);
679
680     s = format (s, "%U", format_vnet_link, adj->ia_link);
681     if (adj->rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)
682         s = format(s, " [features]");
683     s = format (s, " via %U",
684                 format_ip46_address, &adj->sub_type.nbr.next_hop,
685                 adj_proto_to_46(adj->ia_nh_proto));
686     s = format (s, " %U",
687                 format_vnet_rewrite,
688                 &adj->rewrite_header, sizeof (adj->rewrite_data), indent);
689     s = format (s, "\n%Ustacked-on",
690                 format_white_space, indent);
691
692     if (FIB_NODE_INDEX_INVALID != adj->sub_type.midchain.fei)
693     {
694         s = format (s, " entry:%d", adj->sub_type.midchain.fei);
695
696     }
697     s = format (s, ":\n%U%U",
698                 format_white_space, indent+2,
699                 format_dpo_id, &adj->sub_type.midchain.next_dpo, indent+2);
700
701     return (s);
702 }
703
704 static void
705 adj_dpo_lock (dpo_id_t *dpo)
706 {
707     adj_lock(dpo->dpoi_index);
708 }
709 static void
710 adj_dpo_unlock (dpo_id_t *dpo)
711 {
712     adj_unlock(dpo->dpoi_index);
713 }
714
715 const static dpo_vft_t adj_midchain_dpo_vft = {
716     .dv_lock = adj_dpo_lock,
717     .dv_unlock = adj_dpo_unlock,
718     .dv_format = format_adj_midchain,
719     .dv_get_urpf = adj_dpo_get_urpf,
720 };
721
722 /**
723  * @brief The per-protocol VLIB graph nodes that are assigned to a midchain
724  *        object.
725  *
726  * this means that these graph nodes are ones from which a midchain is the
727  * parent object in the DPO-graph.
728  */
729 const static char* const midchain_ip4_nodes[] =
730 {
731     "ip4-midchain",
732     NULL,
733 };
734 const static char* const midchain_ip6_nodes[] =
735 {
736     "ip6-midchain",
737     NULL,
738 };
739 const static char* const midchain_mpls_nodes[] =
740 {
741     "mpls-midchain",
742     NULL,
743 };
744 const static char* const midchain_ethernet_nodes[] =
745 {
746     "adj-l2-midchain",
747     NULL,
748 };
749 const static char* const midchain_nsh_nodes[] =
750 {
751     "adj-nsh-midchain",
752     NULL,
753 };
754
755 const static char* const * const midchain_nodes[DPO_PROTO_NUM] =
756 {
757     [DPO_PROTO_IP4]  = midchain_ip4_nodes,
758     [DPO_PROTO_IP6]  = midchain_ip6_nodes,
759     [DPO_PROTO_MPLS] = midchain_mpls_nodes,
760     [DPO_PROTO_ETHERNET] = midchain_ethernet_nodes,
761     [DPO_PROTO_NSH] = midchain_nsh_nodes,
762 };
763
764 void
765 adj_midchain_module_init (void)
766 {
767     dpo_register(DPO_ADJACENCY_MIDCHAIN, &adj_midchain_dpo_vft, midchain_nodes);
768 }
769
770 #endif