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)));
100 unformat_mpls_header (unformat_input_t * input, va_list * args)
102 u8 ** result = va_arg (*args, u8 **);
103 mpls_unicast_header_t _h, * h = &_h;
104 u32 label, label_exp_s_ttl;
106 if (! unformat (input, "MPLS %d", &label))
109 label_exp_s_ttl = (label<<12) | (1<<8) /* s-bit */ | 0xFF;
110 h->label_exp_s_ttl = clib_host_to_net_u32 (label_exp_s_ttl);
112 /* Add gre, mpls headers to result. */
115 u32 h_n_bytes = sizeof (h[0]);
117 vec_add2 (*result, p, h_n_bytes);
118 clib_memcpy (p, h, h_n_bytes);
125 unformat_mpls_label_net_byte_order (unformat_input_t * input,
128 u32 * result = va_arg (*args, u32 *);
131 if (!unformat (input, "MPLS: label %d", &label))
134 label = (label<<12) | (1<<8) /* s-bit set */ | 0xFF /* ttl */;
136 *result = clib_host_to_net_u32 (label);
140 u8 * format_mpls_unicast_header_host_byte_order (u8 * s, va_list * args)
142 mpls_unicast_header_t *h = va_arg(*args, mpls_unicast_header_t *);
143 u32 label = h->label_exp_s_ttl;
145 s = format (s, "label %d exp %d, s %d, ttl %d",
146 vnet_mpls_uc_get_label (label),
147 vnet_mpls_uc_get_exp (label),
148 vnet_mpls_uc_get_s (label),
149 vnet_mpls_uc_get_ttl (label));
153 u8 * format_mpls_unicast_header_net_byte_order (u8 * s, va_list * args)
155 mpls_unicast_header_t *h = va_arg(*args, mpls_unicast_header_t *);
156 mpls_unicast_header_t h_host;
158 h_host.label_exp_s_ttl = clib_net_to_host_u32 (h->label_exp_s_ttl);
160 return format (s, "%U", format_mpls_unicast_header_host_byte_order,
173 mpls_dest_cmp(void * a1, void * a2)
175 show_mpls_fib_t * r1 = a1;
176 show_mpls_fib_t * r2 = a2;
178 return clib_net_to_host_u32(r1->dest) - clib_net_to_host_u32(r2->dest);
182 mpls_fib_index_cmp(void * a1, void * a2)
184 show_mpls_fib_t * r1 = a1;
185 show_mpls_fib_t * r2 = a2;
187 return r1->fib_index - r2->fib_index;
191 mpls_label_cmp(void * a1, void * a2)
193 show_mpls_fib_t * r1 = a1;
194 show_mpls_fib_t * r2 = a2;
196 return r1->label - r2->label;
199 static clib_error_t *
200 vnet_mpls_local_label (vlib_main_t * vm,
201 unformat_input_t * input,
202 vlib_cli_command_t * cmd)
204 unformat_input_t _line_input, * line_input = &_line_input;
205 fib_route_path_t *rpaths = NULL, rpath;
206 u32 table_id, is_del, is_ip;
207 mpls_label_t local_label;
208 mpls_label_t out_label;
209 clib_error_t * error;
214 vnm = vnet_get_main();
220 local_label = MPLS_LABEL_INVALID;
221 memset(&pfx, 0, sizeof(pfx));
223 /* Get a line of input. */
224 if (! unformat_user (input, unformat_line_input, line_input))
227 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
229 memset(&rpath, 0, sizeof(rpath));
231 if (unformat (line_input, "table %d", &table_id))
233 else if (unformat (line_input, "del"))
235 else if (unformat (line_input, "add"))
237 else if (unformat (line_input, "eos"))
238 pfx.fp_eos = MPLS_EOS;
239 else if (unformat (line_input, "non-eos"))
240 pfx.fp_eos = MPLS_NON_EOS;
241 else if (unformat (line_input, "%U/%d",
242 unformat_ip4_address,
246 pfx.fp_proto = FIB_PROTOCOL_IP4;
249 else if (unformat (line_input, "%U/%d",
250 unformat_ip6_address,
254 pfx.fp_proto = FIB_PROTOCOL_IP6;
257 else if (unformat (line_input, "via %U %U weight %u",
258 unformat_ip4_address,
260 unformat_vnet_sw_interface, vnm,
261 &rpath.frp_sw_if_index,
264 rpath.frp_proto = FIB_PROTOCOL_IP4;
265 vec_add1(rpaths, rpath);
268 else if (unformat (line_input, "via %U %U weight %u",
269 unformat_ip6_address,
271 unformat_vnet_sw_interface, vnm,
272 &rpath.frp_sw_if_index,
275 rpath.frp_proto = FIB_PROTOCOL_IP6;
276 vec_add1(rpaths, rpath);
279 else if (unformat (line_input, "via %U %U",
280 unformat_ip4_address,
282 unformat_vnet_sw_interface, vnm,
283 &rpath.frp_sw_if_index))
285 rpath.frp_weight = 1;
286 rpath.frp_proto = FIB_PROTOCOL_IP4;
287 vec_add1(rpaths, rpath);
289 else if (unformat (line_input, "rx-ip4 %U",
290 unformat_vnet_sw_interface, vnm,
291 &rpath.frp_sw_if_index))
293 rpath.frp_weight = 1;
294 rpath.frp_proto = FIB_PROTOCOL_IP4;
295 rpath.frp_flags = FIB_ROUTE_PATH_INTF_RX;
296 vec_add1(rpaths, rpath);
298 else if (unformat (line_input, "via %U %U",
299 unformat_ip6_address,
301 unformat_vnet_sw_interface, vnm,
302 &rpath.frp_sw_if_index))
304 rpath.frp_weight = 1;
305 rpath.frp_proto = FIB_PROTOCOL_IP6;
306 vec_add1(rpaths, rpath);
308 else if (unformat (line_input, "via %U next-hop-table %d",
309 unformat_ip4_address,
311 &rpath.frp_fib_index))
313 rpath.frp_weight = 1;
314 rpath.frp_sw_if_index = ~0;
315 rpath.frp_proto = FIB_PROTOCOL_IP4;
316 vec_add1(rpaths, rpath);
318 else if (unformat (line_input, "via %U next-hop-table %d",
319 unformat_ip6_address,
321 &rpath.frp_fib_index))
323 rpath.frp_weight = 1;
324 rpath.frp_sw_if_index = ~0;
325 rpath.frp_proto = FIB_PROTOCOL_IP6;
326 vec_add1(rpaths, rpath);
328 else if (unformat (line_input, "via %U",
329 unformat_ip4_address,
330 &rpath.frp_addr.ip4))
333 * the recursive next-hops are by default in the same table
336 rpath.frp_fib_index = table_id;
337 rpath.frp_weight = 1;
338 rpath.frp_sw_if_index = ~0;
339 rpath.frp_proto = FIB_PROTOCOL_IP4;
340 vec_add1(rpaths, rpath);
342 else if (unformat (line_input, "via %U",
343 unformat_ip6_address,
344 &rpath.frp_addr.ip6))
346 rpath.frp_fib_index = table_id;
347 rpath.frp_weight = 1;
348 rpath.frp_sw_if_index = ~0;
349 rpath.frp_proto = FIB_PROTOCOL_IP6;
350 vec_add1(rpaths, rpath);
352 else if (unformat (line_input, "%d", &local_label))
354 else if (unformat (line_input,
355 "ip4-lookup-in-table %d",
356 &rpath.frp_fib_index))
358 rpath.frp_proto = FIB_PROTOCOL_IP4;
359 rpath.frp_sw_if_index = FIB_NODE_INDEX_INVALID;
360 pfx.fp_payload_proto = DPO_PROTO_IP4;
361 vec_add1(rpaths, rpath);
363 else if (unformat (line_input,
364 "ip6-lookup-in-table %d",
365 &rpath.frp_fib_index))
367 rpath.frp_proto = FIB_PROTOCOL_IP6;
368 rpath.frp_sw_if_index = FIB_NODE_INDEX_INVALID;
369 vec_add1(rpaths, rpath);
370 pfx.fp_payload_proto = DPO_PROTO_IP6;
372 else if (unformat (line_input,
373 "mpls-lookup-in-table %d",
374 &rpath.frp_fib_index))
376 rpath.frp_proto = FIB_PROTOCOL_MPLS;
377 rpath.frp_sw_if_index = FIB_NODE_INDEX_INVALID;
378 pfx.fp_payload_proto = DPO_PROTO_MPLS;
379 vec_add1(rpaths, rpath);
381 else if (unformat (line_input, "out-label %U",
382 unformat_mpls_unicast_label,
385 if (vec_len(rpaths) == 0)
387 error = clib_error_return(0 , "Paths then labels");
390 vec_add1(rpaths[vec_len(rpaths)-1].frp_label_stack, out_label);
394 error = clib_error_return (0, "unkown input: %U",
395 format_unformat_error, line_input);
401 if (MPLS_LABEL_INVALID == local_label)
403 error = clib_error_return (0, "local-label required: %U",
404 format_unformat_error, input);
411 u32 fib_index = fib_table_find(pfx.fp_proto, table_id);
413 if (FIB_NODE_INDEX_INVALID == fib_index)
415 error = clib_error_return (0, "%U table-id %d does not exist",
416 format_fib_protocol, pfx.fp_proto, table_id);
422 fib_table_entry_local_label_remove(fib_index, &pfx, local_label);
426 fib_table_entry_local_label_add(fib_index, &pfx, local_label);
431 fib_node_index_t lfe, fib_index;
436 error = clib_error_return(0 , "no paths");
440 pfx.fp_proto = FIB_PROTOCOL_MPLS;
442 pfx.fp_label = local_label;
443 pfx.fp_payload_proto = fib_proto_to_dpo(rpaths[0].frp_proto);
446 * the CLI parsing stored table Ids, swap to FIB indicies
448 if (FIB_NODE_INDEX_INVALID == rpath.frp_sw_if_index)
450 fi = fib_table_find(dpo_proto_to_fib(pfx.fp_payload_proto),
451 rpaths[0].frp_fib_index);
455 error = clib_error_return(0 , "%U Via table %d does not exist",
456 format_dpo_proto, pfx.fp_payload_proto,
457 rpaths[0].frp_fib_index);
460 rpaths[0].frp_fib_index = fi;
463 fib_index = mpls_fib_index_from_table_id(table_id);
465 if (FIB_NODE_INDEX_INVALID == fib_index)
467 error = clib_error_return (0, "MPLS table-id %d does not exist",
472 lfe = fib_table_entry_path_add2(fib_index,
478 if (FIB_NODE_INDEX_INVALID == lfe)
480 error = clib_error_return (0, "Failed to create %U-%U in MPLS table-id %d",
481 format_mpls_unicast_label, local_label,
482 format_mpls_eos_bit, eos,
489 unformat_free (line_input);
494 VLIB_CLI_COMMAND (mpls_local_label_command, static) = {
495 .path = "mpls local-label",
496 .function = vnet_mpls_local_label,
497 .short_help = "Create/Delete MPL local labels",
501 mpls_fib_reset_labels (u32 fib_id)
507 static clib_error_t *
508 mpls_init (vlib_main_t * vm)
510 mpls_main_t * mm = &mpls_main;
511 clib_error_t * error;
514 mm->vnet_main = vnet_get_main();
516 if ((error = vlib_call_init_function (vm, ip_main_init)))
519 return vlib_call_init_function (vm, mpls_input_init);
522 VLIB_INIT_FUNCTION (mpls_init);