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/mpls/mpls.h>
20 #include <vnet/fib/ip4_fib.h>
21 #include <vnet/fib/mpls_fib.h>
23 const static char* mpls_eos_bit_names[] = MPLS_EOS_BITS;
25 mpls_main_t mpls_main;
27 u8 * format_mpls_unicast_label (u8 * s, va_list * args)
29 mpls_label_t label = va_arg (*args, mpls_label_t);
32 case MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL:
33 s = format (s, "%s", MPLS_IETF_IPV4_EXPLICIT_NULL_STRING);
35 case MPLS_IETF_ROUTER_ALERT_LABEL:
36 s = format (s, "%s", MPLS_IETF_ROUTER_ALERT_STRING);
38 case MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL:
39 s = format (s, "%s", MPLS_IETF_IPV6_EXPLICIT_NULL_STRING);
41 case MPLS_IETF_IMPLICIT_NULL_LABEL:
42 s = format (s, "%s", MPLS_IETF_IMPLICIT_NULL_STRING);
44 case MPLS_IETF_ELI_LABEL:
45 s = format (s, "%s", MPLS_IETF_ELI_STRING);
47 case MPLS_IETF_GAL_LABEL:
48 s = format (s, "%s", MPLS_IETF_GAL_STRING);
51 s = format (s, "%d", label);
57 uword unformat_mpls_unicast_label (unformat_input_t * input, va_list * args)
59 mpls_label_t *label = va_arg (*args, mpls_label_t*);
61 if (unformat (input, MPLS_IETF_IPV4_EXPLICIT_NULL_STRING))
62 *label = MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL;
63 else if (unformat (input, MPLS_IETF_IPV6_EXPLICIT_NULL_STRING))
64 *label = MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL;
65 else if (unformat (input, MPLS_IETF_ROUTER_ALERT_STRING))
66 *label = MPLS_IETF_ROUTER_ALERT_LABEL;
67 else if (unformat (input, MPLS_IETF_IMPLICIT_NULL_STRING))
68 *label = MPLS_IETF_IMPLICIT_NULL_LABEL;
69 else if (unformat (input, "%d", label))
75 u8 * format_mpls_eos_bit (u8 * s, va_list * args)
77 mpls_eos_bit_t eb = va_arg (*args, mpls_eos_bit_t);
79 ASSERT(eb <= MPLS_EOS);
81 s = format(s, "%s", mpls_eos_bit_names[eb]);
86 u8 * format_mpls_header (u8 * s, va_list * args)
88 mpls_unicast_header_t hdr = va_arg (*args, mpls_unicast_header_t);
90 return (format(s, "[%U:%d:%d:%U]",
91 format_mpls_unicast_label,
92 vnet_mpls_uc_get_label(hdr.label_exp_s_ttl),
93 vnet_mpls_uc_get_ttl(hdr.label_exp_s_ttl),
94 vnet_mpls_uc_get_exp(hdr.label_exp_s_ttl),
96 vnet_mpls_uc_get_s(hdr.label_exp_s_ttl)));
99 u8 * format_mpls_gre_tx_trace (u8 * s, va_list * args)
101 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
102 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
103 mpls_gre_tx_trace_t * t = va_arg (*args, mpls_gre_tx_trace_t *);
104 mpls_main_t * mm = &mpls_main;
107 s = format (s, "MPLS: lookup miss");
110 s = format (s, "MPLS: tunnel %d labels %U len %d src %U dst %U",
112 format_mpls_encap_index, mm, t->mpls_encap_index,
113 clib_net_to_host_u16 (t->length),
114 format_ip4_address, &t->src.as_u8,
115 format_ip4_address, &t->dst.as_u8);
120 u8 * format_mpls_eth_tx_trace (u8 * s, va_list * args)
122 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
123 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
124 mpls_eth_tx_trace_t * t = va_arg (*args, mpls_eth_tx_trace_t *);
125 mpls_main_t * mm = &mpls_main;
128 s = format (s, "MPLS: lookup miss");
131 s = format (s, "MPLS: tunnel %d labels %U len %d tx_sw_index %d dst %U",
133 format_mpls_encap_index, mm, t->mpls_encap_index,
134 clib_net_to_host_u16 (t->length),
136 format_ethernet_address, t->dst);
141 u8 * format_mpls_eth_header_with_length (u8 * s, va_list * args)
143 ethernet_header_t * h = va_arg (*args, ethernet_header_t *);
144 mpls_unicast_header_t * m = (mpls_unicast_header_t *)(h+1);
145 u32 max_header_bytes = va_arg (*args, u32);
148 header_bytes = sizeof (h[0]);
149 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
150 return format (s, "ethernet header truncated");
153 (s, "ETHERNET-MPLS label %d",
154 vnet_mpls_uc_get_label (clib_net_to_host_u32 (m->label_exp_s_ttl)));
159 u8 * format_mpls_gre_header_with_length (u8 * s, va_list * args)
161 gre_header_t * h = va_arg (*args, gre_header_t *);
162 mpls_unicast_header_t * m = (mpls_unicast_header_t *)(h+1);
163 u32 max_header_bytes = va_arg (*args, u32);
166 header_bytes = sizeof (h[0]);
167 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
168 return format (s, "gre header truncated");
171 (s, "GRE-MPLS label %d",
172 vnet_mpls_uc_get_label (clib_net_to_host_u32 (m->label_exp_s_ttl)));
177 u8 * format_mpls_gre_header (u8 * s, va_list * args)
179 gre_header_t * h = va_arg (*args, gre_header_t *);
180 return format (s, "%U", format_mpls_gre_header_with_length, h, 0);
184 unformat_mpls_gre_header (unformat_input_t * input, va_list * args)
186 u8 ** result = va_arg (*args, u8 **);
187 gre_header_t _g, * g = &_g;
188 mpls_unicast_header_t _h, * h = &_h;
189 u32 label, label_exp_s_ttl;
191 if (! unformat (input, "MPLS %d", &label))
194 g->protocol = clib_host_to_net_u16 (GRE_PROTOCOL_mpls_unicast);
196 label_exp_s_ttl = (label<<12) | (1<<8) /* s-bit */ | 0xFF;
197 h->label_exp_s_ttl = clib_host_to_net_u32 (label_exp_s_ttl);
199 /* Add gre, mpls headers to result. */
202 u32 g_n_bytes = sizeof (g[0]);
203 u32 h_n_bytes = sizeof (h[0]);
205 vec_add2 (*result, p, g_n_bytes);
206 clib_memcpy (p, g, g_n_bytes);
208 vec_add2 (*result, p, h_n_bytes);
209 clib_memcpy (p, h, h_n_bytes);
216 unformat_mpls_label_net_byte_order (unformat_input_t * input,
219 u32 * result = va_arg (*args, u32 *);
222 if (!unformat (input, "MPLS: label %d", &label))
225 label = (label<<12) | (1<<8) /* s-bit set */ | 0xFF /* ttl */;
227 *result = clib_host_to_net_u32 (label);
232 mpls_encap_by_fib_and_dest (mpls_main_t * mm, u32 rx_fib, u32 dst_address)
238 key = ((u64)rx_fib<<32) | ((u64) dst_address);
239 p = hash_get (mm->mpls_encap_by_fib_and_dest, key);
244 e = pool_elt_at_index (mm->encaps, p[0]);
248 int vnet_mpls_add_del_encap (ip4_address_t *dest, u32 fib_id,
249 u32 *labels_host_byte_order,
250 u32 policy_tunnel_index,
251 int no_dst_hash, u32 * indexp, int is_add)
253 mpls_main_t * mm = &mpls_main;
254 ip4_main_t * im = &ip4_main;
256 u32 label_net_byte_order, label_host_byte_order;
262 p = hash_get (im->fib_index_by_table_id, fib_id);
264 return VNET_API_ERROR_NO_SUCH_FIB;
268 key = ((u64)fib_index<<32) | ((u64) dest->as_u32);
272 pool_get (mm->encaps, e);
273 memset (e, 0, sizeof (*e));
275 for (i = 0; i < vec_len (labels_host_byte_order); i++)
277 mpls_unicast_header_t h;
278 label_host_byte_order = labels_host_byte_order[i];
280 /* Reformat label into mpls_unicast_header_t */
281 label_host_byte_order <<= 12;
282 // FIXME NEOS AND EOS
283 //if (i == vec_len(labels_host_byte_order) - 1)
284 // label_host_byte_order |= 1<<8; /* S=1 */
285 label_host_byte_order |= 0xff; /* TTL=FF */
286 label_net_byte_order = clib_host_to_net_u32 (label_host_byte_order);
287 h.label_exp_s_ttl = label_net_byte_order;
288 vec_add1 (e->labels, h);
290 if (no_dst_hash == 0)
291 hash_set (mm->mpls_encap_by_fib_and_dest, key, e - mm->encaps);
293 *indexp = e - mm->encaps;
294 if (policy_tunnel_index != ~0)
295 return vnet_mpls_policy_tunnel_add_rewrite (mm, e, policy_tunnel_index);
299 p = hash_get (mm->mpls_encap_by_fib_and_dest, key);
301 return VNET_API_ERROR_NO_SUCH_LABEL;
303 e = pool_elt_at_index (mm->encaps, p[0]);
305 vec_free (e->labels);
306 vec_free (e->rewrite);
307 pool_put(mm->encaps, e);
309 if (no_dst_hash == 0)
310 hash_unset (mm->mpls_encap_by_fib_and_dest, key);
315 static clib_error_t *
316 mpls_add_encap_command_fn (vlib_main_t * vm,
317 unformat_input_t * input,
318 vlib_cli_command_t * cmd)
324 u32 policy_tunnel_index = ~0;
330 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
332 if (unformat (input, "fib %d", &fib_id))
334 else if (unformat (input, "dest %U", unformat_ip4_address, &dest))
336 else if (unformat (input, "no-dst-hash"))
338 else if (unformat (input, "label %d", &this_label))
339 vec_add1 (labels, this_label);
340 else if (unformat (input, "policy-tunnel %d", &policy_tunnel_index))
347 return clib_error_return (0, "fib-id missing");
349 return clib_error_return (0, "destination IP address missing");
350 if (vec_len (labels) == 0)
351 return clib_error_return (0, "label stack missing");
353 rv = vnet_mpls_add_del_encap (&dest, fib_id, labels,
355 no_dst_hash, 0 /* indexp */,
364 case VNET_API_ERROR_NO_SUCH_FIB:
365 return clib_error_return (0, "fib id %d unknown", fib_id);
368 return clib_error_return (0, "vnet_mpls_add_del_encap returned %d",
375 VLIB_CLI_COMMAND (mpls_add_encap_command, static) = {
376 .path = "mpls encap add",
378 "mpls encap add label <label> ... fib <id> dest <ip4-address>",
379 .function = mpls_add_encap_command_fn,
382 u8 * format_mpls_unicast_header_host_byte_order (u8 * s, va_list * args)
384 mpls_unicast_header_t *h = va_arg(*args, mpls_unicast_header_t *);
385 u32 label = h->label_exp_s_ttl;
387 s = format (s, "label %d exp %d, s %d, ttl %d",
388 vnet_mpls_uc_get_label (label),
389 vnet_mpls_uc_get_exp (label),
390 vnet_mpls_uc_get_s (label),
391 vnet_mpls_uc_get_ttl (label));
395 u8 * format_mpls_unicast_header_net_byte_order (u8 * s, va_list * args)
397 mpls_unicast_header_t *h = va_arg(*args, mpls_unicast_header_t *);
398 mpls_unicast_header_t h_host;
400 h_host.label_exp_s_ttl = clib_net_to_host_u32 (h->label_exp_s_ttl);
402 return format (s, "%U", format_mpls_unicast_header_host_byte_order,
406 static clib_error_t *
407 mpls_del_encap_command_fn (vlib_main_t * vm,
408 unformat_input_t * input,
409 vlib_cli_command_t * cmd)
415 if (unformat (input, "fib %d dest %U", &fib_id,
416 unformat_ip4_address, &dest))
418 rv = vnet_mpls_add_del_encap (&dest, fib_id, 0 /* labels */,
419 ~0 /* policy_tunnel_index */,
425 case VNET_API_ERROR_NO_SUCH_FIB:
426 return clib_error_return (0, "fib id %d unknown", fib_id);
427 case VNET_API_ERROR_NO_SUCH_ENTRY:
428 return clib_error_return (0, "dest %U not in fib %d",
429 format_ip4_address, &dest, fib_id);
436 return clib_error_return (0, "unknown input `%U'",
437 format_unformat_error, input);
440 VLIB_CLI_COMMAND (mpls_del_encap_command, static) = {
441 .path = "mpls encap delete",
442 .short_help = "mpls encap delete fib <id> dest <ip4-address>",
443 .function = mpls_del_encap_command_fn,
446 int vnet_mpls_add_del_decap (u32 rx_fib_id,
448 u32 label_host_byte_order,
449 int s_bit, int next_index, int is_add)
451 mpls_main_t * mm = &mpls_main;
452 ip4_main_t * im = &ip4_main;
454 u32 rx_fib_index, tx_fib_index_or_output_swif_index;
458 p = hash_get (im->fib_index_by_table_id, rx_fib_id);
460 return VNET_API_ERROR_NO_SUCH_FIB;
464 /* L3 decap => transform fib ID to fib index */
465 if (next_index == MPLS_LOOKUP_NEXT_IP4_INPUT)
467 p = hash_get (im->fib_index_by_table_id, tx_fib_id);
469 return VNET_API_ERROR_NO_SUCH_INNER_FIB;
471 tx_fib_index_or_output_swif_index = p[0];
475 /* L2 decap, tx_fib_id is actually the output sw_if_index */
476 tx_fib_index_or_output_swif_index = tx_fib_id;
479 key = ((u64) rx_fib_index<<32) | ((u64) label_host_byte_order<<12)
482 p = hash_get (mm->mpls_decap_by_rx_fib_and_label, key);
484 /* If deleting, or replacing an old entry */
485 if (is_add == 0 || p)
487 if (is_add == 0 && p == 0)
488 return VNET_API_ERROR_NO_SUCH_LABEL;
490 d = pool_elt_at_index (mm->decaps, p[0]);
491 hash_unset (mm->mpls_decap_by_rx_fib_and_label, key);
492 pool_put (mm->decaps, d);
493 /* Deleting, we're done... */
498 /* add decap entry... */
499 pool_get (mm->decaps, d);
500 memset (d, 0, sizeof (*d));
501 d->tx_fib_index = tx_fib_index_or_output_swif_index;
502 d->next_index = next_index;
504 hash_set (mm->mpls_decap_by_rx_fib_and_label, key, d - mm->decaps);
510 unformat_mpls_gre_input_next (unformat_input_t * input, va_list * args)
512 u32 * result = va_arg (*args, u32 *);
515 if (unformat (input, "lookup"))
517 *result = MPLS_LOOKUP_NEXT_IP4_INPUT;
520 else if (unformat (input, "output"))
522 *result = MPLS_LOOKUP_NEXT_L2_OUTPUT;
528 static clib_error_t *
529 mpls_add_decap_command_fn (vlib_main_t * vm,
530 unformat_input_t * input,
531 vlib_cli_command_t * cmd)
533 vnet_main_t * vnm = vnet_get_main();
535 u32 tx_fib_or_sw_if_index;
538 u32 next_index = 1; /* ip4_lookup, see node.c */
539 int tx_fib_id_set = 0;
543 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
545 if (unformat (input, "fib %d", &tx_fib_or_sw_if_index))
547 else if (unformat (input, "sw_if_index %d", &tx_fib_or_sw_if_index))
549 else if (unformat (input, "%U", unformat_vnet_sw_interface, vnm,
550 &tx_fib_or_sw_if_index))
552 else if (unformat (input, "rx-fib %d", &rx_fib_id))
554 else if (unformat (input, "label %d", &label))
556 else if (unformat (input, "s-bit-clear"))
558 else if (unformat (input, "next %U", unformat_mpls_gre_input_next,
565 if (tx_fib_id_set == 0)
566 return clib_error_return (0, "lookup FIB ID not set");
568 return clib_error_return (0, "missing label");
570 rv = vnet_mpls_add_del_decap (rx_fib_id, tx_fib_or_sw_if_index,
571 label, s_bit, next_index, 1 /* is_add */);
577 case VNET_API_ERROR_NO_SUCH_FIB:
578 return clib_error_return (0, "no such rx fib id %d", rx_fib_id);
580 case VNET_API_ERROR_NO_SUCH_INNER_FIB:
581 return clib_error_return (0, "no such tx fib / swif %d",
582 tx_fib_or_sw_if_index);
585 return clib_error_return (0, "vnet_mpls_add_del_decap returned %d",
591 VLIB_CLI_COMMAND (mpls_add_decap_command, static) = {
592 .path = "mpls decap add",
594 "mpls decap add fib <id> label <nn> [s-bit-clear] [next-index <nn>]",
595 .function = mpls_add_decap_command_fn,
598 static clib_error_t *
599 mpls_del_decap_command_fn (vlib_main_t * vm,
600 unformat_input_t * input,
601 vlib_cli_command_t * cmd)
610 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
612 if (unformat (input, "rx-fib %d", &rx_fib_id))
614 else if (unformat (input, "label %d", &label))
616 else if (unformat (input, "s-bit-clear"))
621 return clib_error_return (0, "label not set");
623 rv = vnet_mpls_add_del_decap (rx_fib_id,
624 tx_fib_id /* not interesting */,
626 0 /* next_index not interesting */,
633 case VNET_API_ERROR_NO_SUCH_FIB:
634 return clib_error_return (0, "no such rx fib id %d", rx_fib_id);
636 case VNET_API_ERROR_NO_SUCH_INNER_FIB:
637 return clib_error_return (0, "no such lookup fib id %d", tx_fib_id);
639 case VNET_API_ERROR_NO_SUCH_LABEL:
640 return clib_error_return (0, "no such label %d rx fib id %d",
644 return clib_error_return (0, "vnet_mpls_add_del_decap returned %d",
651 VLIB_CLI_COMMAND (mpls_del_decap_command, static) = {
652 .path = "mpls decap delete",
653 .short_help = "mpls decap delete label <label> rx-fib <id> [s-bit-clear]",
654 .function = mpls_del_decap_command_fn,
658 mpls_dest_cmp(void * a1, void * a2)
660 show_mpls_fib_t * r1 = a1;
661 show_mpls_fib_t * r2 = a2;
663 return clib_net_to_host_u32(r1->dest) - clib_net_to_host_u32(r2->dest);
667 mpls_fib_index_cmp(void * a1, void * a2)
669 show_mpls_fib_t * r1 = a1;
670 show_mpls_fib_t * r2 = a2;
672 return r1->fib_index - r2->fib_index;
676 mpls_label_cmp(void * a1, void * a2)
678 show_mpls_fib_t * r1 = a1;
679 show_mpls_fib_t * r2 = a2;
681 return r1->label - r2->label;
684 static clib_error_t *
685 show_mpls_fib_command_fn (vlib_main_t * vm,
686 unformat_input_t * input,
687 vlib_cli_command_t * cmd)
691 show_mpls_fib_t *records = 0;
693 mpls_main_t * mm = &mpls_main;
696 hash_foreach (key, value, mm->mpls_encap_by_fib_and_dest,
698 vec_add2 (records, s, 1);
699 s->fib_index = (u32)(key>>32);
700 s->dest = (u32)(key & 0xFFFFFFFF);
701 s->entry_index = (u32) value;
704 if (!vec_len(records))
706 vlib_cli_output (vm, "MPLS encap table empty");
708 /* sort output by dst address within fib */
709 vec_sort_with_function (records, mpls_dest_cmp);
710 vec_sort_with_function (records, mpls_fib_index_cmp);
711 vlib_cli_output (vm, "MPLS encap table");
712 vlib_cli_output (vm, "%=6s%=16s%=16s", "Table", "Dest address", "Labels");
713 vec_foreach (s, records)
715 rx_fib = ip4_fib_get (s->fib_index);
716 vlib_cli_output (vm, "%=6d%=16U%=16U", rx_fib->table_id,
717 format_ip4_address, &s->dest,
718 format_mpls_encap_index, mm, s->entry_index);
725 VLIB_CLI_COMMAND (show_mpls_fib_command, static) = {
726 .path = "show mpls encap",
727 .short_help = "show mpls encap",
728 .function = show_mpls_fib_command_fn,
731 static clib_error_t *
732 vnet_mpls_local_label (vlib_main_t * vm,
733 unformat_input_t * input,
734 vlib_cli_command_t * cmd)
736 unformat_input_t _line_input, * line_input = &_line_input;
737 fib_route_path_t *rpaths = NULL, rpath;
738 clib_error_t * error = 0;
739 u32 table_id, is_del, is_ip;
741 mpls_label_t local_label;
748 local_label = MPLS_LABEL_INVALID;
749 memset(&pfx, 0, sizeof(pfx));
751 /* Get a line of input. */
752 if (! unformat_user (input, unformat_line_input, line_input))
755 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
757 memset(&rpath, 0, sizeof(rpath));
759 if (unformat (line_input, "table %d", &table_id))
761 else if (unformat (line_input, "del"))
763 else if (unformat (line_input, "add"))
765 else if (unformat (line_input, "eos"))
766 pfx.fp_eos = MPLS_EOS;
767 else if (unformat (line_input, "non-eos"))
768 pfx.fp_eos = MPLS_NON_EOS;
769 else if (unformat (line_input, "%U/%d",
770 unformat_ip4_address,
774 pfx.fp_proto = FIB_PROTOCOL_IP4;
777 else if (unformat (line_input, "%U/%d",
778 unformat_ip6_address,
782 pfx.fp_proto = FIB_PROTOCOL_IP6;
785 else if (unformat (line_input, "%d", &local_label))
787 else if (unformat (line_input,
788 "ip4-lookup-in-table %d",
789 &rpath.frp_fib_index))
791 rpath.frp_label = MPLS_LABEL_INVALID;
792 rpath.frp_proto = FIB_PROTOCOL_IP4;
793 rpath.frp_sw_if_index = FIB_NODE_INDEX_INVALID;
794 pfx.fp_payload_proto = FIB_PROTOCOL_IP4;
795 vec_add1(rpaths, rpath);
797 else if (unformat (line_input,
798 "ip6-lookup-in-table %d",
799 &rpath.frp_fib_index))
801 rpath.frp_label = MPLS_LABEL_INVALID;
802 rpath.frp_proto = FIB_PROTOCOL_IP6;
803 rpath.frp_sw_if_index = FIB_NODE_INDEX_INVALID;
804 vec_add1(rpaths, rpath);
805 pfx.fp_payload_proto = FIB_PROTOCOL_IP6;
807 else if (unformat (line_input,
808 "mpls-lookup-in-table %d",
809 &rpath.frp_fib_index))
811 rpath.frp_label = MPLS_LABEL_INVALID;
812 rpath.frp_proto = FIB_PROTOCOL_MPLS;
813 rpath.frp_sw_if_index = FIB_NODE_INDEX_INVALID;
814 pfx.fp_payload_proto = FIB_PROTOCOL_MPLS;
815 vec_add1(rpaths, rpath);
819 error = clib_error_return (0, "unkown input: %U",
820 format_unformat_error, input);
826 if (MPLS_LABEL_INVALID == local_label)
828 error = clib_error_return (0, "local-label required: %U",
829 format_unformat_error, input);
836 u32 fib_index = fib_table_find(pfx.fp_proto, table_id);
838 if (FIB_NODE_INDEX_INVALID == fib_index)
840 error = clib_error_return (0, "%U table-id %d does not exist",
841 format_fib_protocol, pfx.fp_proto, table_id);
847 fib_table_entry_local_label_remove(fib_index, &pfx, local_label);
851 fib_table_entry_local_label_add(fib_index, &pfx, local_label);
856 fib_node_index_t lfe, fib_index;
859 pfx.fp_proto = FIB_PROTOCOL_MPLS;
861 pfx.fp_label = local_label;
864 * the CLI parsing stored table Ids, swap to FIB indicies
866 fi = fib_table_id_find_fib_index(pfx.fp_payload_proto,
867 rpaths[0].frp_fib_index);
871 error = clib_error_return(0 , "%U Via table %d does not exist",
872 format_fib_protocol, pfx.fp_payload_proto,
873 rpaths[0].frp_fib_index);
876 rpaths[0].frp_fib_index = fi;
878 fib_index = mpls_fib_index_from_table_id(table_id);
880 if (FIB_NODE_INDEX_INVALID == fib_index)
882 error = clib_error_return (0, "MPLS table-id %d does not exist",
887 lfe = fib_table_entry_path_add2(fib_index,
893 if (FIB_NODE_INDEX_INVALID == lfe)
895 error = clib_error_return (0, "Failed to create %U-%U in MPLS table-id %d",
896 format_mpls_unicast_label, local_label,
897 format_mpls_eos_bit, eos,
907 VLIB_CLI_COMMAND (mpls_local_label_command, static) = {
908 .path = "mpls local-label",
909 .function = vnet_mpls_local_label,
910 .short_help = "Create/Delete MPL local labels",
913 int mpls_fib_reset_labels (u32 fib_id)
917 show_mpls_fib_t *records = 0;
919 mpls_main_t * mm = &mpls_main;
920 ip4_main_t * im = &ip4_main;
924 p = hash_get (im->fib_index_by_table_id, fib_id);
926 return VNET_API_ERROR_NO_SUCH_FIB;
930 hash_foreach (key, value, mm->mpls_encap_by_fib_and_dest,
932 if (fib_index == (u32)(key>>32)) {
933 vec_add2 (records, s, 1);
934 s->dest = (u32)(key & 0xFFFFFFFF);
935 s->entry_index = (u32) value;
939 vec_foreach (s, records)
941 key = ((u64)fib_index<<32) | ((u64) s->dest);
942 hash_unset (mm->mpls_encap_by_fib_and_dest, key);
943 pool_put_index (mm->encaps, s->entry_index);
946 vec_reset_length(records);
948 hash_foreach (key, value, mm->mpls_decap_by_rx_fib_and_label,
950 if (fib_index == (u32) (key>>32)) {
951 vec_add2 (records, s, 1);
952 s->entry_index = value;
953 s->fib_index = fib_index;
954 s->s_bit = key & (1<<8);
955 s->dest = (u32)((key & 0xFFFFFFFF)>>12);
959 vec_foreach (s, records)
961 key = ((u64) fib_index <<32) | ((u64) s->dest<<12) |
964 hash_unset (mm->mpls_decap_by_rx_fib_and_label, key);
965 pool_put_index (mm->decaps, s->entry_index);
972 static clib_error_t * mpls_init (vlib_main_t * vm)
974 mpls_main_t * mm = &mpls_main;
975 clib_error_t * error;
978 mm->vnet_main = vnet_get_main();
980 if ((error = vlib_call_init_function (vm, ip_main_init)))
983 mm->mpls_encap_by_fib_and_dest = hash_create (0, sizeof (uword));
984 mm->mpls_decap_by_rx_fib_and_label = hash_create (0, sizeof (uword));
986 return vlib_call_init_function (vm, mpls_input_init);
989 VLIB_INIT_FUNCTION (mpls_init);
991 mpls_main_t * mpls_get_main (vlib_main_t * vm)
993 vlib_call_init_function (vm, mpls_init);