2 * mpls_tunnel.c: MPLS tunnel interfaces (i.e. for RSVP-TE)
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
26 * @brief pool of tunnel instances
28 static mpls_tunnel_t *mpls_tunnel_pool;
31 * @brief Pool of free tunnel SW indices - i.e. recycled indices
33 static u32 * mpls_tunnel_free_hw_if_indices;
36 * @brief DB of SW index to tunnel index
38 static u32 *mpls_tunnel_db;
41 * @brief Get a tunnel object from a SW interface index
44 mpls_tunnel_get_from_sw_if_index (u32 sw_if_index)
46 if ((vec_len(mpls_tunnel_db) < sw_if_index) ||
47 (~0 == mpls_tunnel_db[sw_if_index]))
50 return (pool_elt_at_index(mpls_tunnel_pool,
51 mpls_tunnel_db[sw_if_index]));
55 * @brief Return true if the label stack is imp-null only
57 static fib_forward_chain_type_t
58 mpls_tunnel_get_fwd_chain_type (const mpls_tunnel_t *mt)
60 if ((1 == vec_len(mt->mt_label_stack)) &&
61 (mt->mt_label_stack[0] == MPLS_IETF_IMPLICIT_NULL_LABEL))
64 * the only label in the label stack is implicit null
65 * we need to build an IP chain.
67 if (FIB_PROTOCOL_IP4 == fib_path_list_get_proto(mt->mt_path_list))
69 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
73 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
78 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
83 * @brief Build a rewrite string for the MPLS tunnel.
85 * We have choices here;
86 * 1 - have an Adjacency with a zero length string and stack it on
88 * 2 - put the label header rewrites in the adjacency string.
90 * We choose 2 since it results in fewer graph nodes in the egress path
93 mpls_tunnel_build_rewrite (vnet_main_t * vnm,
95 vnet_link_t link_type,
96 const void *dst_address)
98 mpls_unicast_header_t *muh;
104 mti = mpls_tunnel_db[sw_if_index];
105 mt = pool_elt_at_index(mpls_tunnel_pool, mti);
108 * The vector must be allocated as u8 so the length is correct
110 ASSERT(0 < vec_len(mt->mt_label_stack));
111 vec_validate(rewrite, (sizeof(*muh) * vec_len(mt->mt_label_stack)) - 1);
113 muh = (mpls_unicast_header_t *)rewrite;
116 * The last (inner most) label in the stack may be EOS, all the rest Non-EOS
118 for (ii = 0; ii < vec_len(mt->mt_label_stack)-1; ii++)
120 vnet_mpls_uc_set_label(&muh[ii].label_exp_s_ttl, mt->mt_label_stack[ii]);
121 vnet_mpls_uc_set_ttl(&muh[ii].label_exp_s_ttl, 255);
122 vnet_mpls_uc_set_exp(&muh[ii].label_exp_s_ttl, 0);
123 vnet_mpls_uc_set_s(&muh[ii].label_exp_s_ttl, MPLS_NON_EOS);
124 muh[ii].label_exp_s_ttl = clib_host_to_net_u32(muh[ii].label_exp_s_ttl);
127 vnet_mpls_uc_set_label(&muh[ii].label_exp_s_ttl, mt->mt_label_stack[ii]);
128 vnet_mpls_uc_set_ttl(&muh[ii].label_exp_s_ttl, 255);
129 vnet_mpls_uc_set_exp(&muh[ii].label_exp_s_ttl, 0);
131 if ((VNET_LINK_MPLS == link_type) &&
132 (mt->mt_label_stack[ii] != MPLS_IETF_IMPLICIT_NULL_LABEL))
134 vnet_mpls_uc_set_s(&muh[ii].label_exp_s_ttl, MPLS_NON_EOS);
138 vnet_mpls_uc_set_s(&muh[ii].label_exp_s_ttl, MPLS_EOS);
141 muh[ii].label_exp_s_ttl = clib_host_to_net_u32(muh[ii].label_exp_s_ttl);
149 * 'stack' (resolve the recursion for) the tunnel's midchain adjacency
152 mpls_tunnel_stack (adj_index_t ai)
159 sw_if_index = adj->rewrite_header.sw_if_index;
161 mt = mpls_tunnel_get_from_sw_if_index(sw_if_index);
167 * find the adjacency that is contributed by the FIB path-list
168 * that this tunnel resovles via, and use it as the next adj
171 if (vnet_hw_interface_get_flags(vnet_get_main(),
172 mt->mt_hw_if_index) &
173 VNET_HW_INTERFACE_FLAG_LINK_UP)
175 dpo_id_t dpo = DPO_INVALID;
177 fib_path_list_contribute_forwarding(mt->mt_path_list,
178 mpls_tunnel_get_fwd_chain_type(mt),
181 if (DPO_LOAD_BALANCE == dpo.dpoi_type)
184 * we don't support multiple paths, so no need to load-balance.
185 * pull the first and only choice and stack directly on that.
189 lb = load_balance_get (dpo.dpoi_index);
191 ASSERT(1 == lb->lb_n_buckets);
193 dpo_copy(&dpo, load_balance_get_bucket_i (lb, 0));
196 adj_nbr_midchain_stack(ai, &dpo);
201 adj_nbr_midchain_unstack(ai);
206 * @brief Call back when restacking all adjacencies on a MPLS interface
209 mpls_adj_walk_cb (adj_index_t ai,
212 mpls_tunnel_stack(ai);
214 return (ADJ_WALK_RC_CONTINUE);
218 mpls_tunnel_restack (mpls_tunnel_t *mt)
220 fib_protocol_t proto;
223 * walk all the adjacencies on the MPLS interface and restack them
225 FOR_EACH_FIB_PROTOCOL(proto)
227 adj_nbr_walk(mt->mt_sw_if_index,
234 static clib_error_t *
235 mpls_tunnel_admin_up_down (vnet_main_t * vnm,
239 vnet_hw_interface_t * hi;
242 hi = vnet_get_hw_interface (vnm, hw_if_index);
244 mt = mpls_tunnel_get_from_sw_if_index(hi->sw_if_index);
249 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
250 vnet_hw_interface_set_flags (vnm, hw_if_index,
251 VNET_HW_INTERFACE_FLAG_LINK_UP);
253 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */);
255 mpls_tunnel_restack(mt);
261 * @brief Fixup the adj rewrite post encap. This is a no-op since the
262 * rewrite is a stack of labels.
265 mpls_tunnel_fixup (vlib_main_t *vm,
272 mpls_tunnel_update_adj (vnet_main_t * vnm,
276 adj_nbr_midchain_update_rewrite(
277 ai, mpls_tunnel_fixup,
278 ADJ_MIDCHAIN_FLAG_NONE,
279 mpls_tunnel_build_rewrite(vnm, sw_if_index,
280 adj_get_link_type(ai),
283 mpls_tunnel_stack(ai);
287 format_mpls_tunnel_name (u8 * s, va_list * args)
289 u32 dev_instance = va_arg (*args, u32);
290 return format (s, "mpls-tunnel%d", dev_instance);
294 format_mpls_tunnel_device (u8 * s, va_list * args)
296 u32 dev_instance = va_arg (*args, u32);
297 CLIB_UNUSED (int verbose) = va_arg (*args, int);
299 return (format (s, "MPLS-tunnel: id %d\n", dev_instance));
303 * @brief Packet trace structure
305 typedef struct mpls_tunnel_trace_t_
308 * Tunnel-id / index in tunnel vector
311 } mpls_tunnel_trace_t;
314 format_mpls_tunnel_tx_trace (u8 * s,
317 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
318 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
319 mpls_tunnel_trace_t * t = va_arg (*args, mpls_tunnel_trace_t *);
321 s = format (s, "MPLS: tunnel %d", t->tunnel_id);
326 * @brief TX function. Only called L2. L3 traffic uses the adj-midchains
329 mpls_tunnel_tx (vlib_main_t * vm,
330 vlib_node_runtime_t * node,
331 vlib_frame_t * frame)
334 u32 * from, * to_next, n_left_from, n_left_to_next;
335 vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
336 const mpls_tunnel_t *mt;
338 mt = pool_elt_at_index(mpls_tunnel_pool, rd->dev_instance);
340 /* Vector of buffer / pkt indices we're supposed to process */
341 from = vlib_frame_vector_args (frame);
343 /* Number of buffers / pkts */
344 n_left_from = frame->n_vectors;
346 /* Speculatively send the first buffer to the last disposition we used */
347 next_index = node->cached_next_index;
349 while (n_left_from > 0)
351 /* set up to enqueue to our disposition with index = next_index */
352 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
357 while (n_left_from > 0 && n_left_to_next > 0)
369 b0 = vlib_get_buffer(vm, bi0);
371 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = mt->mt_l2_adj;
373 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
375 mpls_tunnel_trace_t *tr = vlib_add_trace (vm, node,
377 tr->tunnel_id = rd->dev_instance;
380 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
381 to_next, n_left_to_next,
382 bi0, mt->mt_l2_tx_arc);
385 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
388 return frame->n_vectors;
391 VNET_DEVICE_CLASS (mpls_tunnel_class) = {
392 .name = "MPLS tunnel device",
393 .format_device_name = format_mpls_tunnel_name,
394 .format_device = format_mpls_tunnel_device,
395 .format_tx_trace = format_mpls_tunnel_tx_trace,
396 .tx_function = mpls_tunnel_tx,
397 .admin_up_down_function = mpls_tunnel_admin_up_down,
400 VNET_HW_INTERFACE_CLASS (mpls_tunnel_hw_interface_class) = {
401 .name = "MPLS-Tunnel",
402 // .format_header = format_mpls_eth_header_with_length,
403 // .unformat_header = unformat_mpls_eth_header,
404 .update_adjacency = mpls_tunnel_update_adj,
405 .build_rewrite = mpls_tunnel_build_rewrite,
406 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
409 const mpls_tunnel_t *
410 mpls_tunnel_get (u32 mti)
412 return (pool_elt_at_index(mpls_tunnel_pool, mti));
416 * @brief Walk all the MPLS tunnels
419 mpls_tunnel_walk (mpls_tunnel_walk_cb_t cb,
424 pool_foreach_index(mti, mpls_tunnel_pool,
431 vnet_mpls_tunnel_del (u32 sw_if_index)
435 mt = mpls_tunnel_get_from_sw_if_index(sw_if_index);
440 fib_path_list_child_remove(mt->mt_path_list,
441 mt->mt_sibling_index);
442 if (ADJ_INDEX_INVALID != mt->mt_l2_adj)
443 adj_unlock(mt->mt_l2_adj);
445 vec_free(mt->mt_label_stack);
447 vec_add1 (mpls_tunnel_free_hw_if_indices, mt->mt_hw_if_index);
448 pool_put(mpls_tunnel_pool, mt);
449 mpls_tunnel_db[sw_if_index] = ~0;
453 vnet_mpls_tunnel_add (fib_route_path_t *rpaths,
454 mpls_label_t *label_stack,
458 vnet_hw_interface_t * hi;
463 vnm = vnet_get_main();
464 pool_get(mpls_tunnel_pool, mt);
465 memset (mt, 0, sizeof (*mt));
466 mti = mt - mpls_tunnel_pool;
467 fib_node_init(&mt->mt_node, FIB_NODE_TYPE_MPLS_TUNNEL);
468 mt->mt_l2_adj = ADJ_INDEX_INVALID;
471 * Create a new, or re=use and old, tunnel HW interface
473 if (vec_len (mpls_tunnel_free_hw_if_indices) > 0)
476 mpls_tunnel_free_hw_if_indices[vec_len(mpls_tunnel_free_hw_if_indices)-1];
477 _vec_len (mpls_tunnel_free_hw_if_indices) -= 1;
478 hi = vnet_get_hw_interface (vnm, mt->mt_hw_if_index);
479 hi->hw_instance = mti;
480 hi->dev_instance = mti;
484 mt->mt_hw_if_index = vnet_register_interface(
486 mpls_tunnel_class.index,
488 mpls_tunnel_hw_interface_class.index,
490 hi = vnet_get_hw_interface(vnm, mt->mt_hw_if_index);
494 * Add the new tunnel to the tunnel DB - key:SW if index
496 mt->mt_sw_if_index = hi->sw_if_index;
497 vec_validate_init_empty(mpls_tunnel_db, mt->mt_sw_if_index, ~0);
498 mpls_tunnel_db[mt->mt_sw_if_index] = mti;
501 * construct a path-list from the path provided
503 mt->mt_path_list = fib_path_list_create(FIB_PATH_LIST_FLAG_SHARED, rpaths);
504 mt->mt_sibling_index = fib_path_list_child_add(mt->mt_path_list,
505 FIB_NODE_TYPE_MPLS_TUNNEL,
508 mt->mt_label_stack = vec_dup(label_stack);
513 adj_nbr_add_or_lock(fib_path_list_get_proto(mt->mt_path_list),
518 mt->mt_l2_tx_arc = vlib_node_add_named_next(vlib_get_main(),
523 *sw_if_index = mt->mt_sw_if_index;
526 static clib_error_t *
527 vnet_create_mpls_tunnel_command_fn (vlib_main_t * vm,
528 unformat_input_t * input,
529 vlib_cli_command_t * cmd)
531 unformat_input_t _line_input, * line_input = &_line_input;
532 vnet_main_t * vnm = vnet_get_main();
535 fib_route_path_t rpath, *rpaths = NULL;
536 mpls_label_t out_label = MPLS_LABEL_INVALID, *labels = NULL;
538 clib_error_t *error = NULL;
540 memset(&rpath, 0, sizeof(rpath));
542 /* Get a line of input. */
543 if (! unformat_user (input, unformat_line_input, line_input))
546 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
548 if (unformat (line_input, "del %U",
549 unformat_vnet_sw_interface, vnm,
552 else if (unformat (line_input, "add"))
554 else if (unformat (line_input, "out-label %U",
555 unformat_mpls_unicast_label, &out_label))
557 vec_add1(labels, out_label);
559 else if (unformat (line_input, "via %U %U",
560 unformat_ip4_address,
562 unformat_vnet_sw_interface, vnm,
563 &rpath.frp_sw_if_index))
565 rpath.frp_weight = 1;
566 rpath.frp_proto = FIB_PROTOCOL_IP4;
569 else if (unformat (line_input, "via %U %U",
570 unformat_ip6_address,
572 unformat_vnet_sw_interface, vnm,
573 &rpath.frp_sw_if_index))
575 rpath.frp_weight = 1;
576 rpath.frp_proto = FIB_PROTOCOL_IP6;
578 else if (unformat (line_input, "via %U",
579 unformat_ip6_address,
580 &rpath.frp_addr.ip6))
582 rpath.frp_fib_index = 0;
583 rpath.frp_weight = 1;
584 rpath.frp_sw_if_index = ~0;
585 rpath.frp_proto = FIB_PROTOCOL_IP6;
587 else if (unformat (line_input, "via %U",
588 unformat_ip4_address,
589 &rpath.frp_addr.ip4))
591 rpath.frp_fib_index = 0;
592 rpath.frp_weight = 1;
593 rpath.frp_sw_if_index = ~0;
594 rpath.frp_proto = FIB_PROTOCOL_IP4;
596 else if (unformat (line_input, "l2-only"))
600 error = clib_error_return (0, "unknown input '%U'",
601 format_unformat_error, line_input);
608 vnet_mpls_tunnel_del(sw_if_index);
612 if (0 == vec_len(labels))
614 error = clib_error_return (0, "No Output Labels '%U'",
615 format_unformat_error, line_input);
619 vec_add1(rpaths, rpath);
620 vnet_mpls_tunnel_add(rpaths, labels, l2_only, &sw_if_index);
626 unformat_free (line_input);
632 * This command create a uni-directional MPLS tunnel
635 * @cliexstart{create mpls tunnel}
636 * create mpls tunnel via 10.0.0.1 GigEthernet0/8/0 out-label 33 out-label 34
639 VLIB_CLI_COMMAND (create_mpls_tunnel_command, static) = {
640 .path = "mpls tunnel",
642 "mpls tunnel via [addr] [interface] [out-labels]",
643 .function = vnet_create_mpls_tunnel_command_fn,
647 format_mpls_tunnel (u8 * s, va_list * args)
649 mpls_tunnel_t *mt = va_arg (*args, mpls_tunnel_t *);
652 s = format(s, "mpls_tunnel%d: sw_if_index:%d hw_if_index:%d",
653 mt - mpls_tunnel_pool,
656 s = format(s, "\n label-stack:\n ");
657 for (ii = 0; ii < vec_len(mt->mt_label_stack); ii++)
659 s = format(s, "%d, ", mt->mt_label_stack[ii]);
661 s = format(s, "\n via:\n");
662 s = fib_path_list_format(mt->mt_path_list, s);
668 static clib_error_t *
669 show_mpls_tunnel_command_fn (vlib_main_t * vm,
670 unformat_input_t * input,
671 vlib_cli_command_t * cmd)
676 if (pool_elts (mpls_tunnel_pool) == 0)
677 vlib_cli_output (vm, "No MPLS tunnels configured...");
679 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
681 if (unformat (input, "%d", &mti))
689 pool_foreach (mt, mpls_tunnel_pool,
691 vlib_cli_output (vm, "[@%d] %U",
692 mt - mpls_tunnel_pool,
693 format_mpls_tunnel, mt);
698 if (pool_is_free_index(mpls_tunnel_pool, mti))
699 return clib_error_return (0, "Not atunnel index %d", mti);
701 mt = pool_elt_at_index(mpls_tunnel_pool, mti);
703 vlib_cli_output (vm, "[@%d] %U",
704 mt - mpls_tunnel_pool,
705 format_mpls_tunnel, mt);
712 * This command to show MPLS tunnels
715 * @cliexstart{sh mpls tunnel 2}
716 * [@2] mpls_tunnel2: sw_if_index:5 hw_if_index:5
720 * index:26 locks:1 proto:ipv4 uPRF-list:26 len:1 itfs:[2, ]
721 * index:26 pl-index:26 ipv4 weight=1 attached-nexthop: oper-flags:resolved,
723 * [@0]: ipv4 via 10.0.0.2 loop0: IP4: de:ad:00:00:00:00 -> 00:00:11:aa:bb:cc
726 VLIB_CLI_COMMAND (show_mpls_tunnel_command, static) = {
727 .path = "show mpls tunnel",
728 .function = show_mpls_tunnel_command_fn,
731 static mpls_tunnel_t *
732 mpls_tunnel_from_fib_node (fib_node_t *node)
735 ASSERT(FIB_NODE_TYPE_MPLS_TUNNEL == node->fn_type);
737 return ((mpls_tunnel_t*) (((char*)node) -
738 STRUCT_OFFSET_OF(mpls_tunnel_t, mt_node)));
742 * Function definition to backwalk a FIB node
744 static fib_node_back_walk_rc_t
745 mpls_tunnel_back_walk (fib_node_t *node,
746 fib_node_back_walk_ctx_t *ctx)
748 mpls_tunnel_restack(mpls_tunnel_from_fib_node(node));
750 return (FIB_NODE_BACK_WALK_CONTINUE);
754 * Function definition to get a FIB node from its index
757 mpls_tunnel_fib_node_get (fib_node_index_t index)
761 mt = pool_elt_at_index(mpls_tunnel_pool, index);
763 return (&mt->mt_node);
767 * Function definition to inform the FIB node that its last lock has gone.
770 mpls_tunnel_last_lock_gone (fib_node_t *node)
773 * The MPLS MPLS tunnel is a root of the graph. As such
774 * it never has children and thus is never locked.
780 * Virtual function table registered by MPLS MPLS tunnels
781 * for participation in the FIB object graph.
783 const static fib_node_vft_t mpls_vft = {
784 .fnv_get = mpls_tunnel_fib_node_get,
785 .fnv_last_lock = mpls_tunnel_last_lock_gone,
786 .fnv_back_walk = mpls_tunnel_back_walk,
789 static clib_error_t *
790 mpls_tunnel_init (vlib_main_t *vm)
792 fib_node_register_type(FIB_NODE_TYPE_MPLS_TUNNEL, &mpls_vft);
796 VLIB_INIT_FUNCTION(mpls_tunnel_init);