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, "pop");
54 s = format (s, "%d", label);
60 uword unformat_mpls_unicast_label (unformat_input_t * input, va_list * args)
62 mpls_label_t *label = va_arg (*args, mpls_label_t*);
64 if (unformat (input, MPLS_IETF_IPV4_EXPLICIT_NULL_STRING))
65 *label = MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL;
66 else if (unformat (input, MPLS_IETF_IPV6_EXPLICIT_NULL_STRING))
67 *label = MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL;
68 else if (unformat (input, MPLS_IETF_ROUTER_ALERT_STRING))
69 *label = MPLS_IETF_ROUTER_ALERT_LABEL;
70 else if (unformat (input, MPLS_IETF_IMPLICIT_NULL_STRING))
71 *label = MPLS_IETF_IMPLICIT_NULL_LABEL;
72 else if (unformat (input, MPLS_IETF_IPV4_EXPLICIT_NULL_BRIEF_STRING))
73 *label = MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL;
74 else if (unformat (input, MPLS_IETF_IPV6_EXPLICIT_NULL_BRIEF_STRING))
75 *label = MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL;
76 else if (unformat (input, MPLS_IETF_ROUTER_ALERT_BRIEF_STRING))
77 *label = MPLS_IETF_ROUTER_ALERT_LABEL;
78 else if (unformat (input, MPLS_IETF_IMPLICIT_NULL_BRIEF_STRING))
79 *label = MPLS_IETF_IMPLICIT_NULL_LABEL;
80 else if (unformat (input, "%d", label))
88 u8 * format_mpls_eos_bit (u8 * s, va_list * args)
90 mpls_eos_bit_t eb = va_arg (*args, mpls_eos_bit_t);
92 ASSERT(eb <= MPLS_EOS);
94 s = format(s, "%s", mpls_eos_bit_names[eb]);
99 u8 * format_mpls_header (u8 * s, va_list * args)
101 mpls_unicast_header_t hdr = va_arg (*args, mpls_unicast_header_t);
103 return (format(s, "[%U:%d:%d:%U]",
104 format_mpls_unicast_label,
105 vnet_mpls_uc_get_label(hdr.label_exp_s_ttl),
106 vnet_mpls_uc_get_ttl(hdr.label_exp_s_ttl),
107 vnet_mpls_uc_get_exp(hdr.label_exp_s_ttl),
109 vnet_mpls_uc_get_s(hdr.label_exp_s_ttl)));
113 unformat_mpls_header (unformat_input_t * input, va_list * args)
115 u8 ** result = va_arg (*args, u8 **);
116 mpls_unicast_header_t _h, * h = &_h;
117 u32 label, label_exp_s_ttl;
119 if (! unformat (input, "MPLS %d", &label))
122 label_exp_s_ttl = (label<<12) | (1<<8) /* s-bit */ | 0xFF;
123 h->label_exp_s_ttl = clib_host_to_net_u32 (label_exp_s_ttl);
125 /* Add gre, mpls headers to result. */
128 u32 h_n_bytes = sizeof (h[0]);
130 vec_add2 (*result, p, h_n_bytes);
131 clib_memcpy (p, h, h_n_bytes);
138 unformat_mpls_label_net_byte_order (unformat_input_t * input,
141 u32 * result = va_arg (*args, u32 *);
144 if (!unformat (input, "MPLS: label %d", &label))
147 label = (label<<12) | (1<<8) /* s-bit set */ | 0xFF /* ttl */;
149 *result = clib_host_to_net_u32 (label);
153 u8 * format_mpls_unicast_header_host_byte_order (u8 * s, va_list * args)
155 mpls_unicast_header_t *h = va_arg(*args, mpls_unicast_header_t *);
156 u32 label = h->label_exp_s_ttl;
158 s = format (s, "label %d exp %d, s %d, ttl %d",
159 vnet_mpls_uc_get_label (label),
160 vnet_mpls_uc_get_exp (label),
161 vnet_mpls_uc_get_s (label),
162 vnet_mpls_uc_get_ttl (label));
166 u8 * format_mpls_unicast_header_net_byte_order (u8 * s, va_list * args)
168 mpls_unicast_header_t *h = va_arg(*args, mpls_unicast_header_t *);
169 mpls_unicast_header_t h_host;
171 h_host.label_exp_s_ttl = clib_net_to_host_u32 (h->label_exp_s_ttl);
173 return format (s, "%U", format_mpls_unicast_header_host_byte_order,
186 mpls_dest_cmp(void * a1, void * a2)
188 show_mpls_fib_t * r1 = a1;
189 show_mpls_fib_t * r2 = a2;
191 return clib_net_to_host_u32(r1->dest) - clib_net_to_host_u32(r2->dest);
195 mpls_fib_index_cmp(void * a1, void * a2)
197 show_mpls_fib_t * r1 = a1;
198 show_mpls_fib_t * r2 = a2;
200 return r1->fib_index - r2->fib_index;
204 mpls_label_cmp(void * a1, void * a2)
206 show_mpls_fib_t * r1 = a1;
207 show_mpls_fib_t * r2 = a2;
209 return r1->label - r2->label;
212 static clib_error_t *
213 vnet_mpls_local_label (vlib_main_t * vm,
214 unformat_input_t * input,
215 vlib_cli_command_t * cmd)
217 unformat_input_t _line_input, * line_input = &_line_input;
218 u32 table_id, is_del, is_ip, payload_proto;
219 fib_route_path_t *rpaths = NULL, rpath;
220 mpls_label_t local_label;
221 clib_error_t * error;
230 local_label = MPLS_LABEL_INVALID;
231 memset(&pfx, 0, sizeof(pfx));
232 payload_proto = DPO_PROTO_MPLS;
234 /* Get a line of input. */
235 if (! unformat_user (input, unformat_line_input, line_input))
238 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
240 if (unformat (line_input, "table %d", &table_id))
242 else if (unformat (line_input, "del"))
244 else if (unformat (line_input, "add"))
246 else if (unformat (line_input, "eos"))
247 pfx.fp_eos = MPLS_EOS;
248 else if (unformat (line_input, "non-eos"))
249 pfx.fp_eos = MPLS_NON_EOS;
250 else if (unformat (line_input, "%U/%d",
251 unformat_ip4_address,
255 pfx.fp_proto = FIB_PROTOCOL_IP4;
258 else if (unformat (line_input, "%U/%d",
259 unformat_ip6_address,
263 pfx.fp_proto = FIB_PROTOCOL_IP6;
266 else if (unformat (line_input, "via %U",
267 unformat_fib_route_path,
268 &rpath, &payload_proto))
270 pfx.fp_payload_proto = payload_proto;
271 vec_add1(rpaths, rpath);
273 else if (unformat (line_input, "%d", &local_label))
277 error = clib_error_return (0, "unknown input: %U",
278 format_unformat_error, line_input);
284 if (MPLS_LABEL_INVALID == local_label)
286 error = clib_error_return (0, "local-label required: %U",
287 format_unformat_error, input);
294 u32 fib_index = fib_table_find(pfx.fp_proto, table_id);
296 if (FIB_NODE_INDEX_INVALID == fib_index)
298 error = clib_error_return (0, "%U table-id %d does not exist",
299 format_fib_protocol, pfx.fp_proto, table_id);
305 fib_table_entry_local_label_remove(fib_index, &pfx, local_label);
309 fib_table_entry_local_label_add(fib_index, &pfx, local_label);
314 fib_node_index_t fib_index;
318 error = clib_error_return(0 , "no paths");
322 pfx.fp_proto = FIB_PROTOCOL_MPLS;
324 pfx.fp_label = local_label;
325 pfx.fp_payload_proto = rpaths[0].frp_proto;
327 fib_index = mpls_fib_index_from_table_id(table_id);
329 if (FIB_NODE_INDEX_INVALID == fib_index)
331 error = clib_error_return (0, "MPLS table-id %d does not exist",
338 fib_table_entry_path_remove2(fib_index,
345 fib_node_index_t lfe;
347 lfe = fib_table_entry_path_add2(fib_index,
353 if (FIB_NODE_INDEX_INVALID == lfe)
355 error = clib_error_return (0, "Failed to create %U-%U in MPLS table-id %d",
356 format_mpls_unicast_label, local_label,
357 format_mpls_eos_bit, eos,
365 unformat_free (line_input);
370 VLIB_CLI_COMMAND (mpls_local_label_command, static) = {
371 .path = "mpls local-label",
372 .function = vnet_mpls_local_label,
373 .short_help = "mpls local-label [add|del] <label-value> [eos|non-eos] via [next-hop-address] [next-hop-interface] [next-hop-table <value>] [weight <value>] [preference <value>] [udp-encap-id <value>] [ip4-lookup-in-table <value>] [ip6-lookup-in-table <value>] [mpls-lookup-in-table <value>] [resolve-via-host] [resolve-via-attached] [rx-ip4 <interface>] [out-labels <value value value>]",
377 vnet_mpls_table_cmd (vlib_main_t * vm,
378 unformat_input_t * main_input,
379 vlib_cli_command_t * cmdo)
381 unformat_input_t _line_input, *line_input = &_line_input;
382 clib_error_t *error = NULL;
383 u32 table_id, is_add;
389 /* Get a line of input. */
390 if (!unformat_user (main_input, unformat_line_input, line_input))
393 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
395 if (unformat (line_input, "%d", &table_id))
397 else if (unformat (line_input, "del"))
399 else if (unformat (line_input, "add"))
401 else if (unformat (line_input, "name %s", &name))
405 error = unformat_parse_error (line_input);
412 error = clib_error_return (0, "No table id");
419 mpls_table_create (table_id, 0, name);
423 mpls_table_delete (table_id, 0);
428 unformat_free (line_input);
434 * This command is used to add or delete MPLS Tables. All
435 * Tables must be explicitly added before that can be used,
436 * Including the default table.
439 VLIB_CLI_COMMAND (mpls_table_command, static) = {
440 .path = "mpls table",
441 .short_help = "mpls table [add|del] <table-id>",
442 .function = vnet_mpls_table_cmd,
447 mpls_fib_reset_labels (u32 fib_id)
453 static clib_error_t *
454 mpls_init (vlib_main_t * vm)
456 clib_error_t * error;
458 if ((error = vlib_call_init_function (vm, ip_main_init)))
461 return vlib_call_init_function (vm, mpls_input_init);
464 VLIB_INIT_FUNCTION (mpls_init);