blob: 05361de505d1ebb9cd9810eb102845d538b6d05c [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * mpls.c: mpls
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>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010019#include <vnet/mpls/mpls.h>
20#include <vnet/fib/ip4_fib.h>
21#include <vnet/fib/mpls_fib.h>
22
23const static char* mpls_eos_bit_names[] = MPLS_EOS_BITS;
Ed Warnickecb9cada2015-12-08 15:45:58 -070024
25mpls_main_t mpls_main;
26
Neale Ranns0bfe5d82016-08-25 15:29:12 +010027u8 * format_mpls_unicast_label (u8 * s, va_list * args)
28{
29 mpls_label_t label = va_arg (*args, mpls_label_t);
30
31 switch (label) {
32 case MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL:
33 s = format (s, "%s", MPLS_IETF_IPV4_EXPLICIT_NULL_STRING);
34 break;
35 case MPLS_IETF_ROUTER_ALERT_LABEL:
36 s = format (s, "%s", MPLS_IETF_ROUTER_ALERT_STRING);
37 break;
38 case MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL:
39 s = format (s, "%s", MPLS_IETF_IPV6_EXPLICIT_NULL_STRING);
40 break;
41 case MPLS_IETF_IMPLICIT_NULL_LABEL:
42 s = format (s, "%s", MPLS_IETF_IMPLICIT_NULL_STRING);
43 break;
44 case MPLS_IETF_ELI_LABEL:
45 s = format (s, "%s", MPLS_IETF_ELI_STRING);
46 break;
47 case MPLS_IETF_GAL_LABEL:
48 s = format (s, "%s", MPLS_IETF_GAL_STRING);
49 break;
Neale Ranns31ed7442018-02-23 05:29:09 -080050 case MPLS_LABEL_POP:
51 s = format (s, "pop");
52 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010053 default:
54 s = format (s, "%d", label);
55 break;
56 }
57 return s;
58}
59
60uword unformat_mpls_unicast_label (unformat_input_t * input, va_list * args)
61{
62 mpls_label_t *label = va_arg (*args, mpls_label_t*);
63
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;
Neale Rannsf73d0e22017-08-08 13:07:16 -070072 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;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010080 else if (unformat (input, "%d", label))
81 ;
Neale Rannsf73d0e22017-08-08 13:07:16 -070082 else
83 return (0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010084
85 return (1);
86}
87
88u8 * format_mpls_eos_bit (u8 * s, va_list * args)
89{
90 mpls_eos_bit_t eb = va_arg (*args, mpls_eos_bit_t);
91
92 ASSERT(eb <= MPLS_EOS);
93
94 s = format(s, "%s", mpls_eos_bit_names[eb]);
95
96 return (s);
97}
98
99u8 * format_mpls_header (u8 * s, va_list * args)
100{
101 mpls_unicast_header_t hdr = va_arg (*args, mpls_unicast_header_t);
102
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),
108 format_mpls_eos_bit,
109 vnet_mpls_uc_get_s(hdr.label_exp_s_ttl)));
110}
111
Neale Ranns1357f3b2016-10-16 12:01:42 -0700112uword
113unformat_mpls_header (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114{
Neale Ranns1357f3b2016-10-16 12:01:42 -0700115 u8 ** result = va_arg (*args, u8 **);
116 mpls_unicast_header_t _h, * h = &_h;
117 u32 label, label_exp_s_ttl;
118
119 if (! unformat (input, "MPLS %d", &label))
120 return 0;
121
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);
124
125 /* Add gre, mpls headers to result. */
126 {
127 void * p;
128 u32 h_n_bytes = sizeof (h[0]);
129
130 vec_add2 (*result, p, h_n_bytes);
131 clib_memcpy (p, h, h_n_bytes);
132 }
133
134 return 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135}
136
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137uword
138unformat_mpls_label_net_byte_order (unformat_input_t * input,
139 va_list * args)
140{
141 u32 * result = va_arg (*args, u32 *);
142 u32 label;
143
144 if (!unformat (input, "MPLS: label %d", &label))
145 return 0;
146
147 label = (label<<12) | (1<<8) /* s-bit set */ | 0xFF /* ttl */;
148
149 *result = clib_host_to_net_u32 (label);
150 return 1;
151}
152
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153u8 * format_mpls_unicast_header_host_byte_order (u8 * s, va_list * args)
154{
155 mpls_unicast_header_t *h = va_arg(*args, mpls_unicast_header_t *);
156 u32 label = h->label_exp_s_ttl;
157
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));
163 return s;
164}
165
166u8 * format_mpls_unicast_header_net_byte_order (u8 * s, va_list * args)
167{
168 mpls_unicast_header_t *h = va_arg(*args, mpls_unicast_header_t *);
169 mpls_unicast_header_t h_host;
170
171 h_host.label_exp_s_ttl = clib_net_to_host_u32 (h->label_exp_s_ttl);
172
173 return format (s, "%U", format_mpls_unicast_header_host_byte_order,
174 &h_host);
175}
176
Neale Ranns696e88d2017-03-16 07:34:55 -0400177typedef struct {
178 u32 fib_index;
179 u32 entry_index;
180 u32 dest;
181 u32 s_bit;
182 u32 label;
183} show_mpls_fib_t;
184
marek zavodsky2c21a9a2016-06-21 05:35:16 +0200185int
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500186mpls_dest_cmp(void * a1, void * a2)
187{
188 show_mpls_fib_t * r1 = a1;
189 show_mpls_fib_t * r2 = a2;
190
191 return clib_net_to_host_u32(r1->dest) - clib_net_to_host_u32(r2->dest);
192}
193
marek zavodsky2c21a9a2016-06-21 05:35:16 +0200194int
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500195mpls_fib_index_cmp(void * a1, void * a2)
196{
197 show_mpls_fib_t * r1 = a1;
198 show_mpls_fib_t * r2 = a2;
199
200 return r1->fib_index - r2->fib_index;
201}
202
marek zavodsky2c21a9a2016-06-21 05:35:16 +0200203int
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500204mpls_label_cmp(void * a1, void * a2)
205{
206 show_mpls_fib_t * r1 = a1;
207 show_mpls_fib_t * r2 = a2;
208
209 return r1->label - r2->label;
210}
211
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212static clib_error_t *
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100213vnet_mpls_local_label (vlib_main_t * vm,
214 unformat_input_t * input,
215 vlib_cli_command_t * cmd)
216{
217 unformat_input_t _line_input, * line_input = &_line_input;
Neale Ranns70ed8ae2017-11-15 12:54:46 -0800218 u32 table_id, is_del, is_ip, payload_proto;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100219 fib_route_path_t *rpaths = NULL, rpath;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100220 mpls_label_t local_label;
Neale Rannsad422ed2016-11-02 14:20:04 +0000221 clib_error_t * error;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100222 mpls_eos_bit_t eos;
Neale Rannsad422ed2016-11-02 14:20:04 +0000223 fib_prefix_t pfx;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100224
Neale Rannsad422ed2016-11-02 14:20:04 +0000225 error = NULL;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100226 is_ip = 0;
227 table_id = 0;
228 eos = MPLS_EOS;
Neale Ranns5899fde2016-10-12 13:51:05 +0100229 is_del = 0;
230 local_label = MPLS_LABEL_INVALID;
Dave Barachb7b92992018-10-17 10:38:51 -0400231 clib_memset(&pfx, 0, sizeof(pfx));
Neale Ranns70ed8ae2017-11-15 12:54:46 -0800232 payload_proto = DPO_PROTO_MPLS;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100233
234 /* Get a line of input. */
235 if (! unformat_user (input, unformat_line_input, line_input))
236 return 0;
237
238 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
239 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100240 if (unformat (line_input, "table %d", &table_id))
241 ;
242 else if (unformat (line_input, "del"))
243 is_del = 1;
244 else if (unformat (line_input, "add"))
245 is_del = 0;
246 else if (unformat (line_input, "eos"))
John Loaeb06f42016-10-15 17:45:35 -0400247 pfx.fp_eos = MPLS_EOS;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100248 else if (unformat (line_input, "non-eos"))
John Loaeb06f42016-10-15 17:45:35 -0400249 pfx.fp_eos = MPLS_NON_EOS;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100250 else if (unformat (line_input, "%U/%d",
251 unformat_ip4_address,
252 &pfx.fp_addr.ip4,
253 &pfx.fp_len))
254 {
255 pfx.fp_proto = FIB_PROTOCOL_IP4;
256 is_ip = 1;
257 }
258 else if (unformat (line_input, "%U/%d",
259 unformat_ip6_address,
260 &pfx.fp_addr.ip6,
261 &pfx.fp_len))
262 {
263 pfx.fp_proto = FIB_PROTOCOL_IP6;
264 is_ip = 1;
265 }
Neale Rannsad422ed2016-11-02 14:20:04 +0000266 else if (unformat (line_input, "via %U",
Neale Ranns70ed8ae2017-11-15 12:54:46 -0800267 unformat_fib_route_path,
268 &rpath, &payload_proto))
Neale Rannsad422ed2016-11-02 14:20:04 +0000269 {
Neale Ranns70ed8ae2017-11-15 12:54:46 -0800270 pfx.fp_payload_proto = payload_proto;
Neale Rannsad422ed2016-11-02 14:20:04 +0000271 vec_add1(rpaths, rpath);
272 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100273 else if (unformat (line_input, "%d", &local_label))
274 ;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100275 else
276 {
Andrey "Zed" Zaikin701625b2018-04-18 17:07:07 +0300277 error = clib_error_return (0, "unknown input: %U",
Neale Rannsad422ed2016-11-02 14:20:04 +0000278 format_unformat_error, line_input);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100279 goto done;
280 }
281
282 }
283
Neale Ranns5899fde2016-10-12 13:51:05 +0100284 if (MPLS_LABEL_INVALID == local_label)
285 {
286 error = clib_error_return (0, "local-label required: %U",
287 format_unformat_error, input);
288 goto done;
289 }
290
291
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100292 if (is_ip)
293 {
294 u32 fib_index = fib_table_find(pfx.fp_proto, table_id);
295
296 if (FIB_NODE_INDEX_INVALID == fib_index)
297 {
298 error = clib_error_return (0, "%U table-id %d does not exist",
299 format_fib_protocol, pfx.fp_proto, table_id);
300 goto done;
301 }
302
303 if (is_del)
304 {
305 fib_table_entry_local_label_remove(fib_index, &pfx, local_label);
306 }
307 else
308 {
309 fib_table_entry_local_label_add(fib_index, &pfx, local_label);
310 }
311 }
312 else
313 {
Neale Ranns76481d02017-06-09 12:41:00 -0700314 fib_node_index_t fib_index;
John Loaeb06f42016-10-15 17:45:35 -0400315
Neale Ranns8c1bebe2016-10-28 06:31:54 -0700316 if (NULL == rpaths)
317 {
318 error = clib_error_return(0 , "no paths");
319 goto done;
320 }
321
Dave Barachbd6462e2016-12-16 09:20:04 -0500322 pfx.fp_proto = FIB_PROTOCOL_MPLS;
323 pfx.fp_len = 21;
324 pfx.fp_label = local_label;
Neale Rannsda78f952017-05-24 09:15:43 -0700325 pfx.fp_payload_proto = rpaths[0].frp_proto;
Dave Barachbd6462e2016-12-16 09:20:04 -0500326
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100327 fib_index = mpls_fib_index_from_table_id(table_id);
328
329 if (FIB_NODE_INDEX_INVALID == fib_index)
330 {
331 error = clib_error_return (0, "MPLS table-id %d does not exist",
332 table_id);
333 goto done;
334 }
335
Neale Ranns76481d02017-06-09 12:41:00 -0700336 if (is_del)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100337 {
Neale Ranns76481d02017-06-09 12:41:00 -0700338 fib_table_entry_path_remove2(fib_index,
339 &pfx,
340 FIB_SOURCE_CLI,
341 rpaths);
342 }
343 else
344 {
345 fib_node_index_t lfe;
346
347 lfe = fib_table_entry_path_add2(fib_index,
348 &pfx,
349 FIB_SOURCE_CLI,
350 FIB_ENTRY_FLAG_NONE,
351 rpaths);
352
353 if (FIB_NODE_INDEX_INVALID == lfe)
354 {
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,
358 table_id);
359 goto done;
360 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100361 }
362 }
363
364done:
Billy McFalla9a20e72017-02-15 11:39:12 -0500365 unformat_free (line_input);
366
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100367 return error;
368}
369
370VLIB_CLI_COMMAND (mpls_local_label_command, static) = {
Neale Ranns6cfe6432017-11-22 03:05:29 -0800371 .path = "mpls local-label",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100372 .function = vnet_mpls_local_label,
Marek Gradzki5ff506a2017-12-19 13:15:39 +0100373 .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>]",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100374};
375
Neale Ranns15002542017-09-10 04:39:11 -0700376clib_error_t *
377vnet_mpls_table_cmd (vlib_main_t * vm,
378 unformat_input_t * main_input,
379 vlib_cli_command_t * cmdo)
380{
381 unformat_input_t _line_input, *line_input = &_line_input;
382 clib_error_t *error = NULL;
383 u32 table_id, is_add;
Neale Ranns2297af02017-09-12 09:45:04 -0700384 u8 *name = NULL;
Neale Ranns15002542017-09-10 04:39:11 -0700385
386 is_add = 1;
387 table_id = ~0;
388
389 /* Get a line of input. */
390 if (!unformat_user (main_input, unformat_line_input, line_input))
391 return 0;
392
393 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
394 {
395 if (unformat (line_input, "%d", &table_id))
396 ;
397 else if (unformat (line_input, "del"))
398 is_add = 0;
399 else if (unformat (line_input, "add"))
400 is_add = 1;
Neale Ranns2297af02017-09-12 09:45:04 -0700401 else if (unformat (line_input, "name %s", &name))
402 ;
Neale Ranns15002542017-09-10 04:39:11 -0700403 else
404 {
405 error = unformat_parse_error (line_input);
406 goto done;
407 }
408 }
409
410 if (~0 == table_id)
411 {
412 error = clib_error_return (0, "No table id");
413 goto done;
414 }
Neale Ranns15002542017-09-10 04:39:11 -0700415 else
416 {
417 if (is_add)
418 {
Neale Ranns2297af02017-09-12 09:45:04 -0700419 mpls_table_create (table_id, 0, name);
Neale Ranns15002542017-09-10 04:39:11 -0700420 }
421 else
422 {
423 mpls_table_delete (table_id, 0);
424 }
425 }
426
427 done:
428 unformat_free (line_input);
429 return error;
430}
431
432/* *INDENT-ON* */
433/*?
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.
437 ?*/
438/* *INDENT-OFF* */
Neale Ranns44f81312017-09-19 06:25:13 -0700439VLIB_CLI_COMMAND (mpls_table_command, static) = {
440 .path = "mpls table",
Neale Ranns15002542017-09-10 04:39:11 -0700441 .short_help = "mpls table [add|del] <table-id>",
442 .function = vnet_mpls_table_cmd,
443 .is_mp_safe = 1,
444};
445
Neale Rannsad422ed2016-11-02 14:20:04 +0000446int
447mpls_fib_reset_labels (u32 fib_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448{
Neale Rannsad422ed2016-11-02 14:20:04 +0000449 // FIXME
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450 return 0;
451}
452
Neale Rannsad422ed2016-11-02 14:20:04 +0000453static clib_error_t *
454mpls_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456 clib_error_t * error;
457
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458 if ((error = vlib_call_init_function (vm, ip_main_init)))
459 return error;
460
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461 return vlib_call_init_function (vm, mpls_input_init);
462}
463
464VLIB_INIT_FUNCTION (mpls_init);