MPLS infrastructure improvments
[vpp.git] / vnet / vnet / mpls / mpls_tunnel.c
1 /*
2  * mpls_tunnel.c: MPLS tunnel interfaces (i.e. for RSVP-TE)
3  *
4  * Copyright (c) 2012 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vnet/vnet.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/mpls/mpls_tunnel.h>
21 #include <vnet/ip/ip.h>
22 #include <vnet/fib/fib_path_list.h>
23 #include <vnet/adj/adj_midchain.h>
24
25 /**
26  * @brief pool of tunnel instances
27  */
28 static mpls_tunnel_t *mpls_tunnel_pool;
29
30 /**
31  * @brief Pool of free tunnel SW indices - i.e. recycled indices
32  */
33 static u32 * mpls_tunnel_free_hw_if_indices;
34
35 /**
36  * @brief DB of SW index to tunnel index
37  */
38 static u32 *mpls_tunnel_db;
39
40 /**
41  * @brief Get a tunnel object from a SW interface index
42  */
43 static mpls_tunnel_t*
44 mpls_tunnel_get_from_sw_if_index (u32 sw_if_index)
45 {
46     if ((vec_len(mpls_tunnel_db) < sw_if_index) ||
47         (~0 == mpls_tunnel_db[sw_if_index]))
48         return (NULL);
49
50     return (pool_elt_at_index(mpls_tunnel_pool,
51                               mpls_tunnel_db[sw_if_index]));
52 }
53
54 /**
55  * @brief Return true if the label stack is imp-null only
56  */
57 static fib_forward_chain_type_t
58 mpls_tunnel_get_fwd_chain_type (const mpls_tunnel_t *mt)
59 {
60     if ((1 == vec_len(mt->mt_label_stack)) &&
61         (mt->mt_label_stack[0] == MPLS_IETF_IMPLICIT_NULL_LABEL))
62     {
63         /*
64          * the only label in the label stack is implicit null
65          * we need to build an IP chain.
66          */
67         if (FIB_PROTOCOL_IP4 == fib_path_list_get_proto(mt->mt_path_list))
68         {
69             return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
70         }
71         else
72         {
73             return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
74         }
75     }
76     else
77     {
78         return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
79     }
80 }
81
82 /**
83  * @brief Build a rewrite string for the MPLS tunnel.
84  *
85  * We have choices here;
86  *  1 - have an Adjacency with a zero length string and stack it on
87  *       MPLS label objects
88  *  2 - put the label header rewrites in the adjacency string.
89  *
90  * We choose 2 since it results in fewer graph nodes in the egress path
91  */
92 static u8*
93 mpls_tunnel_build_rewrite (vnet_main_t * vnm,
94                            u32 sw_if_index,
95                            vnet_link_t link_type,
96                            const void *dst_address)
97 {
98     mpls_unicast_header_t *muh;
99     mpls_tunnel_t *mt;
100     u8 *rewrite;
101     u32 mti, ii;
102
103     rewrite = NULL;
104     mti = mpls_tunnel_db[sw_if_index];
105     mt = pool_elt_at_index(mpls_tunnel_pool, mti);
106
107     vec_validate(rewrite, (sizeof(*muh) * vec_len(mt->mt_label_stack)) - 1);
108     muh = (mpls_unicast_header_t *)rewrite;
109
110     /*
111      * The last (inner most) label in the stack may be EOS, all the rest Non-EOS
112      */
113     for (ii = 0; ii < vec_len(mt->mt_label_stack)-1; ii++)
114     {
115         vnet_mpls_uc_set_label(&muh[ii].label_exp_s_ttl, mt->mt_label_stack[ii]);
116         vnet_mpls_uc_set_ttl(&muh[ii].label_exp_s_ttl, 255);
117         vnet_mpls_uc_set_exp(&muh[ii].label_exp_s_ttl, 0);
118         vnet_mpls_uc_set_s(&muh[ii].label_exp_s_ttl, MPLS_NON_EOS);
119         muh[ii].label_exp_s_ttl = clib_host_to_net_u32(muh[ii].label_exp_s_ttl);
120     }
121
122     vnet_mpls_uc_set_label(&muh[ii].label_exp_s_ttl, mt->mt_label_stack[ii]);
123     vnet_mpls_uc_set_ttl(&muh[ii].label_exp_s_ttl, 255);
124     vnet_mpls_uc_set_exp(&muh[ii].label_exp_s_ttl, 0);
125
126     if (VNET_LINK_MPLS == link_type &&
127         mt->mt_label_stack[ii] != MPLS_IETF_IMPLICIT_NULL_LABEL)
128         vnet_mpls_uc_set_s(&muh[ii].label_exp_s_ttl, MPLS_NON_EOS);
129     else
130         vnet_mpls_uc_set_s(&muh[ii].label_exp_s_ttl, MPLS_EOS);
131
132     muh[ii].label_exp_s_ttl = clib_host_to_net_u32(muh[ii].label_exp_s_ttl);
133
134     return ((u8*)muh);
135 }
136
137 /**
138  * mpls_tunnel_stack
139  *
140  * 'stack' (resolve the recursion for) the tunnel's midchain adjacency
141  */
142 static void
143 mpls_tunnel_stack (adj_index_t ai)
144 {
145     ip_adjacency_t *adj;
146     mpls_tunnel_t *mt;
147     u32 sw_if_index;
148
149     adj = adj_get(ai);
150     sw_if_index = adj->rewrite_header.sw_if_index;
151
152     mt = mpls_tunnel_get_from_sw_if_index(sw_if_index);
153
154     if (NULL == mt)
155         return;
156
157     /*
158      * find the adjacency that is contributed by the FIB path-list
159      * that this tunnel resovles via, and use it as the next adj
160      * in the midchain
161      */
162     if (vnet_hw_interface_get_flags(vnet_get_main(),
163                                     mt->mt_hw_if_index) &
164         VNET_HW_INTERFACE_FLAG_LINK_UP)
165     {
166         dpo_id_t dpo = DPO_INVALID;
167
168         fib_path_list_contribute_forwarding(mt->mt_path_list,
169                                             mpls_tunnel_get_fwd_chain_type(mt),
170                                             &dpo);
171
172         if (DPO_LOAD_BALANCE == dpo.dpoi_type)
173         {
174             /*
175              * we don't support multiple paths, so no need to load-balance.
176              * pull the first and only choice and stack directly on that.
177              */
178             load_balance_t *lb;
179
180             lb = load_balance_get (dpo.dpoi_index);
181
182             ASSERT(1 == lb->lb_n_buckets);
183
184             dpo_copy(&dpo, load_balance_get_bucket_i (lb, 0));
185         }
186
187         adj_nbr_midchain_stack(ai, &dpo);
188         dpo_reset(&dpo);
189     }
190     else
191     {
192         adj_nbr_midchain_unstack(ai);
193     }
194 }
195
196 /**
197  * @brief Call back when restacking all adjacencies on a MPLS interface
198  */
199 static adj_walk_rc_t
200 mpls_adj_walk_cb (adj_index_t ai,
201                  void *ctx)
202 {
203     mpls_tunnel_stack(ai);
204
205     return (ADJ_WALK_RC_CONTINUE);
206 }
207
208 static void
209 mpls_tunnel_restack (mpls_tunnel_t *mt)
210 {
211     fib_protocol_t proto;
212
213     /*
214      * walk all the adjacencies on the MPLS interface and restack them
215      */
216     FOR_EACH_FIB_PROTOCOL(proto)
217     {
218         adj_nbr_walk(mt->mt_sw_if_index,
219                      proto,
220                      mpls_adj_walk_cb,
221                      NULL);
222     }
223 }
224
225 static clib_error_t *
226 mpls_tunnel_admin_up_down (vnet_main_t * vnm,
227                            u32 hw_if_index,
228                            u32 flags)
229 {
230     vnet_hw_interface_t * hi;
231     mpls_tunnel_t *mt;
232
233     hi = vnet_get_hw_interface (vnm, hw_if_index);
234
235     mt = mpls_tunnel_get_from_sw_if_index(hi->sw_if_index);
236
237     if (NULL == mt)
238         return (NULL);
239
240     if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
241         vnet_hw_interface_set_flags (vnm, hw_if_index,
242                                      VNET_HW_INTERFACE_FLAG_LINK_UP);
243     else
244         vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */);
245
246     mpls_tunnel_restack(mt);
247
248     return (NULL);
249 }
250
251 /**
252  * @brief Fixup the adj rewrite post encap. This is a no-op since the
253  * rewrite is a stack of labels.
254  */
255 static void
256 mpls_tunnel_fixup (vlib_main_t *vm,
257                    ip_adjacency_t *adj,
258                    vlib_buffer_t *b0)
259 {
260 }
261
262 static void
263 mpls_tunnel_update_adj (vnet_main_t * vnm,
264                         u32 sw_if_index,
265                         adj_index_t ai)
266 {
267     adj_nbr_midchain_update_rewrite(
268         ai, mpls_tunnel_fixup, 
269         ADJ_MIDCHAIN_FLAG_NONE,
270         mpls_tunnel_build_rewrite(vnm, sw_if_index,
271                                   adj_get_link_type(ai),
272                                   NULL));
273
274     mpls_tunnel_stack(ai);
275 }
276
277 static u8 *
278 format_mpls_tunnel_name (u8 * s, va_list * args)
279 {
280   u32 dev_instance = va_arg (*args, u32);
281   return format (s, "mpls-tunnel%d", dev_instance);
282 }
283
284 static u8 *
285 format_mpls_tunnel_device (u8 * s, va_list * args)
286 {
287   u32 dev_instance = va_arg (*args, u32);
288   CLIB_UNUSED (int verbose) = va_arg (*args, int);
289
290   return (format (s, "MPLS-tunnel: id %d\n", dev_instance));
291 }
292
293 /**
294  * @brief Packet trace structure
295  */
296 typedef struct mpls_tunnel_trace_t_
297 {
298     /**
299    * Tunnel-id / index in tunnel vector
300    */
301   u32 tunnel_id;
302 } mpls_tunnel_trace_t;
303
304 static u8 *
305 format_mpls_tunnel_tx_trace (u8 * s,
306                              va_list * args)
307 {
308   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
309   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
310   mpls_tunnel_trace_t * t = va_arg (*args, mpls_tunnel_trace_t *);
311
312   s = format (s, "MPLS: tunnel %d", t->tunnel_id);
313   return s;
314 }
315
316 /**
317  * @brief TX function. Only called L2. L3 traffic uses the adj-midchains
318  */
319 static uword
320 mpls_tunnel_tx (vlib_main_t * vm,
321                 vlib_node_runtime_t * node,
322                 vlib_frame_t * frame)
323 {
324   u32 next_index;
325   u32 * from, * to_next, n_left_from, n_left_to_next;
326   vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
327   const mpls_tunnel_t *mt;
328
329   mt = pool_elt_at_index(mpls_tunnel_pool, rd->dev_instance);
330
331   /* Vector of buffer / pkt indices we're supposed to process */
332   from = vlib_frame_vector_args (frame);
333
334   /* Number of buffers / pkts */
335   n_left_from = frame->n_vectors;
336
337   /* Speculatively send the first buffer to the last disposition we used */
338   next_index = node->cached_next_index;
339
340   while (n_left_from > 0)
341     {
342       /* set up to enqueue to our disposition with index = next_index */
343       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
344
345       /*
346        * FIXME DUAL LOOP
347        */
348       while (n_left_from > 0 && n_left_to_next > 0)
349         {
350           vlib_buffer_t * b0;
351           u32 bi0;
352
353           bi0 = from[0];
354           to_next[0] = bi0;
355           from += 1;
356           to_next += 1;
357           n_left_from -= 1;
358           n_left_to_next -= 1;
359
360           b0 = vlib_get_buffer(vm, bi0);
361
362           vnet_buffer(b0)->ip.adj_index[VLIB_TX] = mt->mt_l2_adj;
363
364           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
365             {
366               mpls_tunnel_trace_t *tr = vlib_add_trace (vm, node,
367                                                    b0, sizeof (*tr));
368               tr->tunnel_id = rd->dev_instance;
369             }
370
371           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
372                                            to_next, n_left_to_next,
373                                            bi0, mt->mt_l2_tx_arc);
374         }
375
376       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
377     }
378
379   return frame->n_vectors;
380 }
381
382 VNET_DEVICE_CLASS (mpls_tunnel_class) = {
383     .name = "MPLS tunnel device",
384     .format_device_name = format_mpls_tunnel_name,
385     .format_device = format_mpls_tunnel_device,
386     .format_tx_trace = format_mpls_tunnel_tx_trace,
387     .tx_function = mpls_tunnel_tx,
388     .no_flatten_output_chains = 1,
389     .admin_up_down_function = mpls_tunnel_admin_up_down,
390 };
391
392 VNET_HW_INTERFACE_CLASS (mpls_tunnel_hw_interface_class) = {
393   .name = "MPLS-Tunnel",
394 //  .format_header = format_mpls_eth_header_with_length,
395 //  .unformat_header = unformat_mpls_eth_header,
396   .update_adjacency = mpls_tunnel_update_adj,
397   .build_rewrite = mpls_tunnel_build_rewrite,
398   .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
399 };
400
401 const mpls_tunnel_t *
402 mpls_tunnel_get (u32 mti)
403 {
404     return (pool_elt_at_index(mpls_tunnel_pool, mti));
405 }
406
407 /**
408  * @brief Walk all the MPLS tunnels
409  */
410 void
411 mpls_tunnel_walk (mpls_tunnel_walk_cb_t cb,
412                   void *ctx)
413 {
414     u32 mti;
415
416     pool_foreach_index(mti, mpls_tunnel_pool,
417     ({
418         cb(mti, ctx);
419     }));
420 }
421
422 void
423 vnet_mpls_tunnel_del (u32 sw_if_index)
424 {
425     mpls_tunnel_t *mt;
426
427     mt = mpls_tunnel_get_from_sw_if_index(sw_if_index);
428
429     if (NULL == mt)
430         return;
431     
432     fib_path_list_child_remove(mt->mt_path_list,
433                                mt->mt_sibling_index);
434     if (ADJ_INDEX_INVALID != mt->mt_l2_adj)
435         adj_unlock(mt->mt_l2_adj);
436
437     vec_free(mt->mt_label_stack);
438
439     vec_add1 (mpls_tunnel_free_hw_if_indices, mt->mt_hw_if_index);
440     pool_put(mpls_tunnel_pool, mt);
441     mpls_tunnel_db[sw_if_index] = ~0;
442 }
443
444 void
445 vnet_mpls_tunnel_add (fib_route_path_t *rpaths,
446                       mpls_label_t *label_stack,
447                       u8 l2_only,
448                       u32 *sw_if_index)
449 {
450     vnet_hw_interface_t * hi;
451     mpls_tunnel_t *mt;
452     vnet_main_t * vnm;
453     u32 mti;
454
455     vnm = vnet_get_main();
456     pool_get(mpls_tunnel_pool, mt);
457     memset (mt, 0, sizeof (*mt));
458     mti = mt - mpls_tunnel_pool;
459     fib_node_init(&mt->mt_node, FIB_NODE_TYPE_MPLS_TUNNEL);
460     mt->mt_l2_adj = ADJ_INDEX_INVALID;
461
462     /*
463      * Create a new, or re=use and old, tunnel HW interface
464      */
465     if (vec_len (mpls_tunnel_free_hw_if_indices) > 0)
466     {
467         mt->mt_hw_if_index = 
468             mpls_tunnel_free_hw_if_indices[vec_len(mpls_tunnel_free_hw_if_indices)-1];
469         _vec_len (mpls_tunnel_free_hw_if_indices) -= 1;
470         hi = vnet_get_hw_interface (vnm, mt->mt_hw_if_index);
471         hi->hw_instance = mti;
472         hi->dev_instance = mti;
473     }
474     else 
475     {
476         mt->mt_hw_if_index = vnet_register_interface(
477                                  vnm,
478                                  mpls_tunnel_class.index,
479                                  mti,
480                                  mpls_tunnel_hw_interface_class.index,
481                                  mti);
482         hi = vnet_get_hw_interface(vnm, mt->mt_hw_if_index);
483     }
484
485     /*
486      * Add the new tunnel to the tunnel DB - key:SW if index
487      */
488     mt->mt_sw_if_index = hi->sw_if_index;
489     vec_validate_init_empty(mpls_tunnel_db, mt->mt_sw_if_index, ~0);
490     mpls_tunnel_db[mt->mt_sw_if_index] = mti;
491
492     /*
493      * construct a path-list from the path provided
494      */
495     mt->mt_path_list = fib_path_list_create(FIB_PATH_LIST_FLAG_SHARED, rpaths);
496     mt->mt_sibling_index = fib_path_list_child_add(mt->mt_path_list,
497                                                    FIB_NODE_TYPE_MPLS_TUNNEL,
498                                                    mti);
499
500     mt->mt_label_stack = vec_dup(label_stack);
501
502     if (l2_only)
503     {
504         mt->mt_l2_adj =
505             adj_nbr_add_or_lock(fib_path_list_get_proto(mt->mt_path_list),
506                                 VNET_LINK_ETHERNET,
507                                 &zero_addr,
508                                 mt->mt_sw_if_index);
509
510         mt->mt_l2_tx_arc = vlib_node_add_named_next(vlib_get_main(),
511                                                     hi->tx_node_index,
512                                                     "adj-l2-midchain");
513     }
514
515     *sw_if_index = mt->mt_sw_if_index;
516 }
517
518 static clib_error_t *
519 vnet_create_mpls_tunnel_command_fn (vlib_main_t * vm,
520                                     unformat_input_t * input,
521                                     vlib_cli_command_t * cmd)
522 {
523     unformat_input_t _line_input, * line_input = &_line_input;
524     vnet_main_t * vnm = vnet_get_main();
525     u8 is_del = 0;
526     u8 l2_only = 0;
527     fib_route_path_t rpath, *rpaths = NULL;
528     mpls_label_t out_label = MPLS_LABEL_INVALID, *labels = NULL;
529     u32 sw_if_index;
530
531     memset(&rpath, 0, sizeof(rpath));
532
533     /* Get a line of input. */
534     if (! unformat_user (input, unformat_line_input, line_input))
535         return 0;
536
537     while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
538     {
539         if (unformat (line_input, "del %U",
540                       unformat_vnet_sw_interface, vnm,
541                       &sw_if_index))
542             is_del = 1;
543         else if (unformat (line_input, "add"))
544             is_del = 0;
545         else if (unformat (line_input, "out-label %U",
546                            unformat_mpls_unicast_label, &out_label))
547         {
548             vec_add1(labels, out_label);
549         }
550         else if (unformat (line_input, "via %U %U",
551                            unformat_ip4_address,
552                            &rpath.frp_addr.ip4,
553                            unformat_vnet_sw_interface, vnm,
554                            &rpath.frp_sw_if_index))
555         {
556             rpath.frp_weight = 1;
557             rpath.frp_proto = FIB_PROTOCOL_IP4;
558         }
559                          
560         else if (unformat (line_input, "via %U %U",
561                            unformat_ip6_address,
562                            &rpath.frp_addr.ip6,
563                            unformat_vnet_sw_interface, vnm,
564                            &rpath.frp_sw_if_index))
565         {
566             rpath.frp_weight = 1;
567             rpath.frp_proto = FIB_PROTOCOL_IP6;
568         }
569         else if (unformat (line_input, "via %U",
570                            unformat_ip6_address,
571                            &rpath.frp_addr.ip6))
572         {
573             rpath.frp_fib_index = 0;
574             rpath.frp_weight = 1;
575             rpath.frp_sw_if_index = ~0;
576             rpath.frp_proto = FIB_PROTOCOL_IP6;
577         }
578         else if (unformat (line_input, "via %U",
579                            unformat_ip4_address,
580                            &rpath.frp_addr.ip4))
581         {
582             rpath.frp_fib_index = 0;
583             rpath.frp_weight = 1;
584             rpath.frp_sw_if_index = ~0;
585             rpath.frp_proto = FIB_PROTOCOL_IP4;
586         }
587         else if (unformat (line_input, "l2-only"))
588             l2_only = 1;
589         else
590             return clib_error_return (0, "unknown input '%U'",
591                                       format_unformat_error, line_input);
592     }
593
594     if (is_del)
595     {
596         vnet_mpls_tunnel_del(sw_if_index);
597     }
598     else
599     {
600         if (0 == vec_len(labels))
601             return clib_error_return (0, "No Output Labels '%U'",
602                                       format_unformat_error, line_input);
603
604         vec_add1(rpaths, rpath);
605         vnet_mpls_tunnel_add(rpaths, labels, l2_only, &sw_if_index);
606     }
607
608     vec_free(labels);
609     vec_free(rpaths);
610
611     return (NULL);
612 }
613
614 /*?
615  * This command create a uni-directional MPLS tunnel
616  *
617  * @cliexpar
618  * @cliexstart{create mpls tunnel}
619  *  create mpls tunnel via 10.0.0.1 GigEthernet0/8/0 out-label 33 out-label 34
620  * @cliexend
621  ?*/
622 VLIB_CLI_COMMAND (create_mpls_tunnel_command, static) = {
623   .path = "mpls tunnel",
624   .short_help = 
625   "mpls tunnel via [addr] [interface] [out-labels]",
626   .function = vnet_create_mpls_tunnel_command_fn,
627 };
628
629 static u8 *
630 format_mpls_tunnel (u8 * s, va_list * args)
631 {
632     mpls_tunnel_t *mt = va_arg (*args, mpls_tunnel_t *);
633     int ii;
634
635     s = format(s, "mpls_tunnel%d: sw_if_index:%d hw_if_index:%d",
636                mt - mpls_tunnel_pool,
637                mt->mt_sw_if_index,
638                mt->mt_hw_if_index);
639     s = format(s, "\n label-stack:\n  ");
640     for (ii = 0; ii < vec_len(mt->mt_label_stack); ii++)
641     {
642         s = format(s, "%d, ", mt->mt_label_stack[ii]);
643     }
644     s = format(s, "\n via:\n");
645     s = fib_path_list_format(mt->mt_path_list, s);
646     s = format(s, "\n");
647
648     return (s);
649 }
650
651 static clib_error_t *
652 show_mpls_tunnel_command_fn (vlib_main_t * vm,
653                              unformat_input_t * input,
654                              vlib_cli_command_t * cmd)
655 {
656     mpls_tunnel_t * mt;
657     u32 mti = ~0;
658
659     if (pool_elts (mpls_tunnel_pool) == 0)
660         vlib_cli_output (vm, "No MPLS tunnels configured...");
661
662     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
663     {
664         if (unformat (input, "%d", &mti))
665             ;
666         else
667             break;
668     }
669
670     if (~0 == mti)
671     {
672         pool_foreach (mt, mpls_tunnel_pool,
673         ({
674             vlib_cli_output (vm, "[@%d] %U",
675                              mt - mpls_tunnel_pool,
676                              format_mpls_tunnel, mt);
677         }));
678     }
679     else
680     {
681         if (pool_is_free_index(mpls_tunnel_pool, mti))
682             return clib_error_return (0, "Not atunnel index %d", mti);
683
684         mt = pool_elt_at_index(mpls_tunnel_pool, mti);
685
686         vlib_cli_output (vm, "[@%d] %U",
687                          mt - mpls_tunnel_pool,
688                          format_mpls_tunnel, mt);
689     }
690
691     return 0;
692 }
693
694 /*?
695  * This command to show MPLS tunnels
696  *
697  * @cliexpar
698  * @cliexstart{sh mpls tunnel 2}
699  * [@2] mpls_tunnel2: sw_if_index:5 hw_if_index:5
700  *  label-stack:
701  *    3, 
702  *  via:
703  *   index:26 locks:1 proto:ipv4 uPRF-list:26 len:1 itfs:[2, ]
704  *     index:26 pl-index:26 ipv4 weight=1 attached-nexthop:  oper-flags:resolved,
705  *      10.0.0.2 loop0
706  *         [@0]: ipv4 via 10.0.0.2 loop0: IP4: de:ad:00:00:00:00 -> 00:00:11:aa:bb:cc
707  * @cliexend
708  ?*/
709 VLIB_CLI_COMMAND (show_mpls_tunnel_command, static) = {
710     .path = "show mpls tunnel",
711     .function = show_mpls_tunnel_command_fn,
712 };
713
714 static mpls_tunnel_t *
715 mpls_tunnel_from_fib_node (fib_node_t *node)
716 {
717 #if (CLIB_DEBUG > 0)
718     ASSERT(FIB_NODE_TYPE_MPLS_TUNNEL == node->fn_type);
719 #endif
720     return ((mpls_tunnel_t*) (((char*)node) -
721                              STRUCT_OFFSET_OF(mpls_tunnel_t, mt_node)));
722 }
723
724 /**
725  * Function definition to backwalk a FIB node
726  */
727 static fib_node_back_walk_rc_t
728 mpls_tunnel_back_walk (fib_node_t *node,
729                       fib_node_back_walk_ctx_t *ctx)
730 {
731     mpls_tunnel_restack(mpls_tunnel_from_fib_node(node));
732
733     return (FIB_NODE_BACK_WALK_CONTINUE);
734 }
735
736 /**
737  * Function definition to get a FIB node from its index
738  */
739 static fib_node_t*
740 mpls_tunnel_fib_node_get (fib_node_index_t index)
741 {
742     mpls_tunnel_t * mt;
743
744     mt = pool_elt_at_index(mpls_tunnel_pool, index);
745
746     return (&mt->mt_node);
747 }
748
749 /**
750  * Function definition to inform the FIB node that its last lock has gone.
751  */
752 static void
753 mpls_tunnel_last_lock_gone (fib_node_t *node)
754 {
755     /*
756      * The MPLS MPLS tunnel is a root of the graph. As such
757      * it never has children and thus is never locked.
758      */
759     ASSERT(0);
760 }
761
762 /*
763  * Virtual function table registered by MPLS MPLS tunnels
764  * for participation in the FIB object graph.
765  */
766 const static fib_node_vft_t mpls_vft = {
767     .fnv_get = mpls_tunnel_fib_node_get,
768     .fnv_last_lock = mpls_tunnel_last_lock_gone,
769     .fnv_back_walk = mpls_tunnel_back_walk,
770 };
771
772 static clib_error_t *
773 mpls_tunnel_init (vlib_main_t *vm)
774 {
775   fib_node_register_type(FIB_NODE_TYPE_MPLS_TUNNEL, &mpls_vft);
776
777   return 0;
778 }
779 VLIB_INIT_FUNCTION(mpls_tunnel_init);