Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 19 | #include <vnet/mpls/mpls.h> |
| 20 | #include <vnet/fib/ip4_fib.h> |
| 21 | #include <vnet/fib/mpls_fib.h> |
| 22 | |
| 23 | const static char* mpls_eos_bit_names[] = MPLS_EOS_BITS; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 24 | |
| 25 | mpls_main_t mpls_main; |
| 26 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 27 | u8 * 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; |
| 50 | default: |
| 51 | s = format (s, "%d", label); |
| 52 | break; |
| 53 | } |
| 54 | return s; |
| 55 | } |
| 56 | |
| 57 | uword unformat_mpls_unicast_label (unformat_input_t * input, va_list * args) |
| 58 | { |
| 59 | mpls_label_t *label = va_arg (*args, mpls_label_t*); |
| 60 | |
| 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; |
Neale Ranns | f73d0e2 | 2017-08-08 13:07:16 -0700 | [diff] [blame] | 69 | else if (unformat (input, MPLS_IETF_IPV4_EXPLICIT_NULL_BRIEF_STRING)) |
| 70 | *label = MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL; |
| 71 | else if (unformat (input, MPLS_IETF_IPV6_EXPLICIT_NULL_BRIEF_STRING)) |
| 72 | *label = MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL; |
| 73 | else if (unformat (input, MPLS_IETF_ROUTER_ALERT_BRIEF_STRING)) |
| 74 | *label = MPLS_IETF_ROUTER_ALERT_LABEL; |
| 75 | else if (unformat (input, MPLS_IETF_IMPLICIT_NULL_BRIEF_STRING)) |
| 76 | *label = MPLS_IETF_IMPLICIT_NULL_LABEL; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 77 | else if (unformat (input, "%d", label)) |
| 78 | ; |
Neale Ranns | f73d0e2 | 2017-08-08 13:07:16 -0700 | [diff] [blame] | 79 | else |
| 80 | return (0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 81 | |
| 82 | return (1); |
| 83 | } |
| 84 | |
| 85 | u8 * format_mpls_eos_bit (u8 * s, va_list * args) |
| 86 | { |
| 87 | mpls_eos_bit_t eb = va_arg (*args, mpls_eos_bit_t); |
| 88 | |
| 89 | ASSERT(eb <= MPLS_EOS); |
| 90 | |
| 91 | s = format(s, "%s", mpls_eos_bit_names[eb]); |
| 92 | |
| 93 | return (s); |
| 94 | } |
| 95 | |
| 96 | u8 * format_mpls_header (u8 * s, va_list * args) |
| 97 | { |
| 98 | mpls_unicast_header_t hdr = va_arg (*args, mpls_unicast_header_t); |
| 99 | |
| 100 | return (format(s, "[%U:%d:%d:%U]", |
| 101 | format_mpls_unicast_label, |
| 102 | vnet_mpls_uc_get_label(hdr.label_exp_s_ttl), |
| 103 | vnet_mpls_uc_get_ttl(hdr.label_exp_s_ttl), |
| 104 | vnet_mpls_uc_get_exp(hdr.label_exp_s_ttl), |
| 105 | format_mpls_eos_bit, |
| 106 | vnet_mpls_uc_get_s(hdr.label_exp_s_ttl))); |
| 107 | } |
| 108 | |
Neale Ranns | 1357f3b | 2016-10-16 12:01:42 -0700 | [diff] [blame] | 109 | uword |
| 110 | unformat_mpls_header (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | { |
Neale Ranns | 1357f3b | 2016-10-16 12:01:42 -0700 | [diff] [blame] | 112 | u8 ** result = va_arg (*args, u8 **); |
| 113 | mpls_unicast_header_t _h, * h = &_h; |
| 114 | u32 label, label_exp_s_ttl; |
| 115 | |
| 116 | if (! unformat (input, "MPLS %d", &label)) |
| 117 | return 0; |
| 118 | |
| 119 | label_exp_s_ttl = (label<<12) | (1<<8) /* s-bit */ | 0xFF; |
| 120 | h->label_exp_s_ttl = clib_host_to_net_u32 (label_exp_s_ttl); |
| 121 | |
| 122 | /* Add gre, mpls headers to result. */ |
| 123 | { |
| 124 | void * p; |
| 125 | u32 h_n_bytes = sizeof (h[0]); |
| 126 | |
| 127 | vec_add2 (*result, p, h_n_bytes); |
| 128 | clib_memcpy (p, h, h_n_bytes); |
| 129 | } |
| 130 | |
| 131 | return 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | uword |
| 135 | unformat_mpls_label_net_byte_order (unformat_input_t * input, |
| 136 | va_list * args) |
| 137 | { |
| 138 | u32 * result = va_arg (*args, u32 *); |
| 139 | u32 label; |
| 140 | |
| 141 | if (!unformat (input, "MPLS: label %d", &label)) |
| 142 | return 0; |
| 143 | |
| 144 | label = (label<<12) | (1<<8) /* s-bit set */ | 0xFF /* ttl */; |
| 145 | |
| 146 | *result = clib_host_to_net_u32 (label); |
| 147 | return 1; |
| 148 | } |
| 149 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | u8 * format_mpls_unicast_header_host_byte_order (u8 * s, va_list * args) |
| 151 | { |
| 152 | mpls_unicast_header_t *h = va_arg(*args, mpls_unicast_header_t *); |
| 153 | u32 label = h->label_exp_s_ttl; |
| 154 | |
| 155 | s = format (s, "label %d exp %d, s %d, ttl %d", |
| 156 | vnet_mpls_uc_get_label (label), |
| 157 | vnet_mpls_uc_get_exp (label), |
| 158 | vnet_mpls_uc_get_s (label), |
| 159 | vnet_mpls_uc_get_ttl (label)); |
| 160 | return s; |
| 161 | } |
| 162 | |
| 163 | u8 * format_mpls_unicast_header_net_byte_order (u8 * s, va_list * args) |
| 164 | { |
| 165 | mpls_unicast_header_t *h = va_arg(*args, mpls_unicast_header_t *); |
| 166 | mpls_unicast_header_t h_host; |
| 167 | |
| 168 | h_host.label_exp_s_ttl = clib_net_to_host_u32 (h->label_exp_s_ttl); |
| 169 | |
| 170 | return format (s, "%U", format_mpls_unicast_header_host_byte_order, |
| 171 | &h_host); |
| 172 | } |
| 173 | |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 174 | typedef struct { |
| 175 | u32 fib_index; |
| 176 | u32 entry_index; |
| 177 | u32 dest; |
| 178 | u32 s_bit; |
| 179 | u32 label; |
| 180 | } show_mpls_fib_t; |
| 181 | |
marek zavodsky | 2c21a9a | 2016-06-21 05:35:16 +0200 | [diff] [blame] | 182 | int |
Matus Fabian | d2dc3df | 2015-12-14 10:31:33 -0500 | [diff] [blame] | 183 | mpls_dest_cmp(void * a1, void * a2) |
| 184 | { |
| 185 | show_mpls_fib_t * r1 = a1; |
| 186 | show_mpls_fib_t * r2 = a2; |
| 187 | |
| 188 | return clib_net_to_host_u32(r1->dest) - clib_net_to_host_u32(r2->dest); |
| 189 | } |
| 190 | |
marek zavodsky | 2c21a9a | 2016-06-21 05:35:16 +0200 | [diff] [blame] | 191 | int |
Matus Fabian | d2dc3df | 2015-12-14 10:31:33 -0500 | [diff] [blame] | 192 | mpls_fib_index_cmp(void * a1, void * a2) |
| 193 | { |
| 194 | show_mpls_fib_t * r1 = a1; |
| 195 | show_mpls_fib_t * r2 = a2; |
| 196 | |
| 197 | return r1->fib_index - r2->fib_index; |
| 198 | } |
| 199 | |
marek zavodsky | 2c21a9a | 2016-06-21 05:35:16 +0200 | [diff] [blame] | 200 | int |
Matus Fabian | d2dc3df | 2015-12-14 10:31:33 -0500 | [diff] [blame] | 201 | mpls_label_cmp(void * a1, void * a2) |
| 202 | { |
| 203 | show_mpls_fib_t * r1 = a1; |
| 204 | show_mpls_fib_t * r2 = a2; |
| 205 | |
| 206 | return r1->label - r2->label; |
| 207 | } |
| 208 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | static clib_error_t * |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 210 | vnet_mpls_local_label (vlib_main_t * vm, |
| 211 | unformat_input_t * input, |
| 212 | vlib_cli_command_t * cmd) |
| 213 | { |
| 214 | unformat_input_t _line_input, * line_input = &_line_input; |
| 215 | fib_route_path_t *rpaths = NULL, rpath; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 216 | u32 table_id, is_del, is_ip; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 217 | mpls_label_t local_label; |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 218 | mpls_label_t out_label; |
| 219 | clib_error_t * error; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 220 | mpls_eos_bit_t eos; |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 221 | vnet_main_t * vnm; |
| 222 | fib_prefix_t pfx; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 223 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 224 | vnm = vnet_get_main(); |
| 225 | error = NULL; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 226 | is_ip = 0; |
| 227 | table_id = 0; |
| 228 | eos = MPLS_EOS; |
Neale Ranns | 5899fde | 2016-10-12 13:51:05 +0100 | [diff] [blame] | 229 | is_del = 0; |
| 230 | local_label = MPLS_LABEL_INVALID; |
John Lo | aeb06f4 | 2016-10-15 17:45:35 -0400 | [diff] [blame] | 231 | memset(&pfx, 0, sizeof(pfx)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 232 | |
| 233 | /* Get a line of input. */ |
| 234 | if (! unformat_user (input, unformat_line_input, line_input)) |
| 235 | return 0; |
| 236 | |
| 237 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 238 | { |
| 239 | memset(&rpath, 0, sizeof(rpath)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 240 | |
| 241 | if (unformat (line_input, "table %d", &table_id)) |
| 242 | ; |
| 243 | else if (unformat (line_input, "del")) |
| 244 | is_del = 1; |
| 245 | else if (unformat (line_input, "add")) |
| 246 | is_del = 0; |
| 247 | else if (unformat (line_input, "eos")) |
John Lo | aeb06f4 | 2016-10-15 17:45:35 -0400 | [diff] [blame] | 248 | pfx.fp_eos = MPLS_EOS; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 249 | else if (unformat (line_input, "non-eos")) |
John Lo | aeb06f4 | 2016-10-15 17:45:35 -0400 | [diff] [blame] | 250 | pfx.fp_eos = MPLS_NON_EOS; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 251 | else if (unformat (line_input, "%U/%d", |
| 252 | unformat_ip4_address, |
| 253 | &pfx.fp_addr.ip4, |
| 254 | &pfx.fp_len)) |
| 255 | { |
| 256 | pfx.fp_proto = FIB_PROTOCOL_IP4; |
| 257 | is_ip = 1; |
| 258 | } |
| 259 | else if (unformat (line_input, "%U/%d", |
| 260 | unformat_ip6_address, |
| 261 | &pfx.fp_addr.ip6, |
| 262 | &pfx.fp_len)) |
| 263 | { |
| 264 | pfx.fp_proto = FIB_PROTOCOL_IP6; |
| 265 | is_ip = 1; |
| 266 | } |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 267 | else if (unformat (line_input, "via %U %U weight %u", |
| 268 | unformat_ip4_address, |
| 269 | &rpath.frp_addr.ip4, |
| 270 | unformat_vnet_sw_interface, vnm, |
| 271 | &rpath.frp_sw_if_index, |
| 272 | &rpath.frp_weight)) |
| 273 | { |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 274 | rpath.frp_proto = DPO_PROTO_IP4; |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 275 | vec_add1(rpaths, rpath); |
| 276 | } |
| 277 | |
| 278 | else if (unformat (line_input, "via %U %U weight %u", |
| 279 | unformat_ip6_address, |
| 280 | &rpath.frp_addr.ip6, |
| 281 | unformat_vnet_sw_interface, vnm, |
| 282 | &rpath.frp_sw_if_index, |
| 283 | &rpath.frp_weight)) |
| 284 | { |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 285 | rpath.frp_proto = DPO_PROTO_IP6; |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 286 | vec_add1(rpaths, rpath); |
| 287 | } |
| 288 | |
| 289 | else if (unformat (line_input, "via %U %U", |
| 290 | unformat_ip4_address, |
| 291 | &rpath.frp_addr.ip4, |
| 292 | unformat_vnet_sw_interface, vnm, |
| 293 | &rpath.frp_sw_if_index)) |
| 294 | { |
| 295 | rpath.frp_weight = 1; |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 296 | rpath.frp_proto = DPO_PROTO_IP4; |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 297 | vec_add1(rpaths, rpath); |
| 298 | } |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 299 | else if (unformat (line_input, "rx-ip4 %U", |
| 300 | unformat_vnet_sw_interface, vnm, |
| 301 | &rpath.frp_sw_if_index)) |
| 302 | { |
| 303 | rpath.frp_weight = 1; |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 304 | rpath.frp_proto = DPO_PROTO_IP4; |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 305 | rpath.frp_flags = FIB_ROUTE_PATH_INTF_RX; |
| 306 | vec_add1(rpaths, rpath); |
| 307 | } |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 308 | else if (unformat (line_input, "via %U %U", |
| 309 | unformat_ip6_address, |
| 310 | &rpath.frp_addr.ip6, |
| 311 | unformat_vnet_sw_interface, vnm, |
| 312 | &rpath.frp_sw_if_index)) |
| 313 | { |
| 314 | rpath.frp_weight = 1; |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 315 | rpath.frp_proto = DPO_PROTO_IP6; |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 316 | vec_add1(rpaths, rpath); |
| 317 | } |
| 318 | else if (unformat (line_input, "via %U next-hop-table %d", |
| 319 | unformat_ip4_address, |
| 320 | &rpath.frp_addr.ip4, |
| 321 | &rpath.frp_fib_index)) |
| 322 | { |
| 323 | rpath.frp_weight = 1; |
| 324 | rpath.frp_sw_if_index = ~0; |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 325 | rpath.frp_proto = DPO_PROTO_IP4; |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 326 | vec_add1(rpaths, rpath); |
| 327 | } |
| 328 | else if (unformat (line_input, "via %U next-hop-table %d", |
| 329 | unformat_ip6_address, |
| 330 | &rpath.frp_addr.ip6, |
| 331 | &rpath.frp_fib_index)) |
| 332 | { |
| 333 | rpath.frp_weight = 1; |
| 334 | rpath.frp_sw_if_index = ~0; |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 335 | rpath.frp_proto = DPO_PROTO_IP6; |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 336 | vec_add1(rpaths, rpath); |
| 337 | } |
| 338 | else if (unformat (line_input, "via %U", |
| 339 | unformat_ip4_address, |
| 340 | &rpath.frp_addr.ip4)) |
| 341 | { |
| 342 | /* |
| 343 | * the recursive next-hops are by default in the same table |
| 344 | * as the prefix |
| 345 | */ |
| 346 | rpath.frp_fib_index = table_id; |
| 347 | rpath.frp_weight = 1; |
| 348 | rpath.frp_sw_if_index = ~0; |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 349 | rpath.frp_proto = DPO_PROTO_IP4; |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 350 | vec_add1(rpaths, rpath); |
| 351 | } |
| 352 | else if (unformat (line_input, "via %U", |
| 353 | unformat_ip6_address, |
| 354 | &rpath.frp_addr.ip6)) |
| 355 | { |
| 356 | rpath.frp_fib_index = table_id; |
| 357 | rpath.frp_weight = 1; |
| 358 | rpath.frp_sw_if_index = ~0; |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 359 | rpath.frp_proto = DPO_PROTO_IP6; |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 360 | vec_add1(rpaths, rpath); |
| 361 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 362 | else if (unformat (line_input, "%d", &local_label)) |
| 363 | ; |
| 364 | else if (unformat (line_input, |
| 365 | "ip4-lookup-in-table %d", |
| 366 | &rpath.frp_fib_index)) |
| 367 | { |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 368 | rpath.frp_proto = DPO_PROTO_IP4; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 369 | rpath.frp_sw_if_index = FIB_NODE_INDEX_INVALID; |
Neale Ranns | 8c1bebe | 2016-10-28 06:31:54 -0700 | [diff] [blame] | 370 | pfx.fp_payload_proto = DPO_PROTO_IP4; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 371 | vec_add1(rpaths, rpath); |
| 372 | } |
| 373 | else if (unformat (line_input, |
| 374 | "ip6-lookup-in-table %d", |
| 375 | &rpath.frp_fib_index)) |
| 376 | { |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 377 | rpath.frp_proto = DPO_PROTO_IP6; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 378 | rpath.frp_sw_if_index = FIB_NODE_INDEX_INVALID; |
| 379 | vec_add1(rpaths, rpath); |
Neale Ranns | 8c1bebe | 2016-10-28 06:31:54 -0700 | [diff] [blame] | 380 | pfx.fp_payload_proto = DPO_PROTO_IP6; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 381 | } |
| 382 | else if (unformat (line_input, |
| 383 | "mpls-lookup-in-table %d", |
| 384 | &rpath.frp_fib_index)) |
| 385 | { |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 386 | rpath.frp_proto = DPO_PROTO_MPLS; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 387 | rpath.frp_sw_if_index = FIB_NODE_INDEX_INVALID; |
Neale Ranns | 8c1bebe | 2016-10-28 06:31:54 -0700 | [diff] [blame] | 388 | pfx.fp_payload_proto = DPO_PROTO_MPLS; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 389 | vec_add1(rpaths, rpath); |
| 390 | } |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 391 | else if (unformat (line_input, |
| 392 | "l2-input-on %U", |
| 393 | unformat_vnet_sw_interface, vnm, |
| 394 | &rpath.frp_sw_if_index)) |
| 395 | { |
| 396 | rpath.frp_proto = DPO_PROTO_ETHERNET; |
| 397 | pfx.fp_payload_proto = DPO_PROTO_ETHERNET; |
| 398 | rpath.frp_flags = FIB_ROUTE_PATH_INTF_RX; |
| 399 | vec_add1(rpaths, rpath); |
| 400 | } |
Neale Ranns | f73d0e2 | 2017-08-08 13:07:16 -0700 | [diff] [blame] | 401 | else if (unformat (line_input, "out-labels")) |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 402 | { |
Neale Ranns | f73d0e2 | 2017-08-08 13:07:16 -0700 | [diff] [blame] | 403 | if (vec_len (rpaths) == 0) |
| 404 | { |
| 405 | error = clib_error_return (0, "Paths then labels"); |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 406 | goto done; |
Neale Ranns | f73d0e2 | 2017-08-08 13:07:16 -0700 | [diff] [blame] | 407 | } |
| 408 | else |
| 409 | { |
| 410 | while (unformat (line_input, "%U", |
| 411 | unformat_mpls_unicast_label, |
| 412 | &out_label)) |
| 413 | { |
| 414 | vec_add1 (rpaths[vec_len (rpaths) - 1].frp_label_stack, |
| 415 | out_label); |
| 416 | } |
| 417 | } |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 418 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 419 | else |
| 420 | { |
| 421 | error = clib_error_return (0, "unkown input: %U", |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 422 | format_unformat_error, line_input); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 423 | goto done; |
| 424 | } |
| 425 | |
| 426 | } |
| 427 | |
Neale Ranns | 5899fde | 2016-10-12 13:51:05 +0100 | [diff] [blame] | 428 | if (MPLS_LABEL_INVALID == local_label) |
| 429 | { |
| 430 | error = clib_error_return (0, "local-label required: %U", |
| 431 | format_unformat_error, input); |
| 432 | goto done; |
| 433 | } |
| 434 | |
| 435 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 436 | if (is_ip) |
| 437 | { |
| 438 | u32 fib_index = fib_table_find(pfx.fp_proto, table_id); |
| 439 | |
| 440 | if (FIB_NODE_INDEX_INVALID == fib_index) |
| 441 | { |
| 442 | error = clib_error_return (0, "%U table-id %d does not exist", |
| 443 | format_fib_protocol, pfx.fp_proto, table_id); |
| 444 | goto done; |
| 445 | } |
| 446 | |
| 447 | if (is_del) |
| 448 | { |
| 449 | fib_table_entry_local_label_remove(fib_index, &pfx, local_label); |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | fib_table_entry_local_label_add(fib_index, &pfx, local_label); |
| 454 | } |
| 455 | } |
| 456 | else |
| 457 | { |
Neale Ranns | 76481d0 | 2017-06-09 12:41:00 -0700 | [diff] [blame] | 458 | fib_node_index_t fib_index; |
John Lo | aeb06f4 | 2016-10-15 17:45:35 -0400 | [diff] [blame] | 459 | u32 fi; |
| 460 | |
Neale Ranns | 8c1bebe | 2016-10-28 06:31:54 -0700 | [diff] [blame] | 461 | if (NULL == rpaths) |
| 462 | { |
| 463 | error = clib_error_return(0 , "no paths"); |
| 464 | goto done; |
| 465 | } |
| 466 | |
Dave Barach | bd6462e | 2016-12-16 09:20:04 -0500 | [diff] [blame] | 467 | pfx.fp_proto = FIB_PROTOCOL_MPLS; |
| 468 | pfx.fp_len = 21; |
| 469 | pfx.fp_label = local_label; |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 470 | pfx.fp_payload_proto = rpaths[0].frp_proto; |
Dave Barach | bd6462e | 2016-12-16 09:20:04 -0500 | [diff] [blame] | 471 | |
John Lo | aeb06f4 | 2016-10-15 17:45:35 -0400 | [diff] [blame] | 472 | /* |
| 473 | * the CLI parsing stored table Ids, swap to FIB indicies |
| 474 | */ |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 475 | if (FIB_NODE_INDEX_INVALID == rpath.frp_sw_if_index) |
John Lo | aeb06f4 | 2016-10-15 17:45:35 -0400 | [diff] [blame] | 476 | { |
Neale Ranns | 107e7d4 | 2017-04-11 09:55:19 -0700 | [diff] [blame] | 477 | fi = fib_table_find(dpo_proto_to_fib(pfx.fp_payload_proto), |
| 478 | rpaths[0].frp_fib_index); |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 479 | |
| 480 | if (~0 == fi) |
| 481 | { |
| 482 | error = clib_error_return(0 , "%U Via table %d does not exist", |
Neale Ranns | 8c2f05c | 2016-12-12 19:35:58 +0000 | [diff] [blame] | 483 | format_dpo_proto, pfx.fp_payload_proto, |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 484 | rpaths[0].frp_fib_index); |
| 485 | goto done; |
| 486 | } |
| 487 | rpaths[0].frp_fib_index = fi; |
John Lo | aeb06f4 | 2016-10-15 17:45:35 -0400 | [diff] [blame] | 488 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 489 | |
| 490 | fib_index = mpls_fib_index_from_table_id(table_id); |
| 491 | |
| 492 | if (FIB_NODE_INDEX_INVALID == fib_index) |
| 493 | { |
| 494 | error = clib_error_return (0, "MPLS table-id %d does not exist", |
| 495 | table_id); |
| 496 | goto done; |
| 497 | } |
| 498 | |
Neale Ranns | 76481d0 | 2017-06-09 12:41:00 -0700 | [diff] [blame] | 499 | if (is_del) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 500 | { |
Neale Ranns | 76481d0 | 2017-06-09 12:41:00 -0700 | [diff] [blame] | 501 | fib_table_entry_path_remove2(fib_index, |
| 502 | &pfx, |
| 503 | FIB_SOURCE_CLI, |
| 504 | rpaths); |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | fib_node_index_t lfe; |
| 509 | |
| 510 | lfe = fib_table_entry_path_add2(fib_index, |
| 511 | &pfx, |
| 512 | FIB_SOURCE_CLI, |
| 513 | FIB_ENTRY_FLAG_NONE, |
| 514 | rpaths); |
| 515 | |
| 516 | if (FIB_NODE_INDEX_INVALID == lfe) |
| 517 | { |
| 518 | error = clib_error_return (0, "Failed to create %U-%U in MPLS table-id %d", |
| 519 | format_mpls_unicast_label, local_label, |
| 520 | format_mpls_eos_bit, eos, |
| 521 | table_id); |
| 522 | goto done; |
| 523 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | done: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 528 | unformat_free (line_input); |
| 529 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 530 | return error; |
| 531 | } |
| 532 | |
| 533 | VLIB_CLI_COMMAND (mpls_local_label_command, static) = { |
| 534 | .path = "mpls local-label", |
| 535 | .function = vnet_mpls_local_label, |
| 536 | .short_help = "Create/Delete MPL local labels", |
| 537 | }; |
| 538 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 539 | clib_error_t * |
| 540 | vnet_mpls_table_cmd (vlib_main_t * vm, |
| 541 | unformat_input_t * main_input, |
| 542 | vlib_cli_command_t * cmdo) |
| 543 | { |
| 544 | unformat_input_t _line_input, *line_input = &_line_input; |
| 545 | clib_error_t *error = NULL; |
| 546 | u32 table_id, is_add; |
Neale Ranns | 2297af0 | 2017-09-12 09:45:04 -0700 | [diff] [blame] | 547 | u8 *name = NULL; |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 548 | |
| 549 | is_add = 1; |
| 550 | table_id = ~0; |
| 551 | |
| 552 | /* Get a line of input. */ |
| 553 | if (!unformat_user (main_input, unformat_line_input, line_input)) |
| 554 | return 0; |
| 555 | |
| 556 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 557 | { |
| 558 | if (unformat (line_input, "%d", &table_id)) |
| 559 | ; |
| 560 | else if (unformat (line_input, "del")) |
| 561 | is_add = 0; |
| 562 | else if (unformat (line_input, "add")) |
| 563 | is_add = 1; |
Neale Ranns | 2297af0 | 2017-09-12 09:45:04 -0700 | [diff] [blame] | 564 | else if (unformat (line_input, "name %s", &name)) |
| 565 | ; |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 566 | else |
| 567 | { |
| 568 | error = unformat_parse_error (line_input); |
| 569 | goto done; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | if (~0 == table_id) |
| 574 | { |
| 575 | error = clib_error_return (0, "No table id"); |
| 576 | goto done; |
| 577 | } |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 578 | else |
| 579 | { |
| 580 | if (is_add) |
| 581 | { |
Neale Ranns | 2297af0 | 2017-09-12 09:45:04 -0700 | [diff] [blame] | 582 | mpls_table_create (table_id, 0, name); |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 583 | } |
| 584 | else |
| 585 | { |
| 586 | mpls_table_delete (table_id, 0); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | done: |
| 591 | unformat_free (line_input); |
| 592 | return error; |
| 593 | } |
| 594 | |
| 595 | /* *INDENT-ON* */ |
| 596 | /*? |
| 597 | * This command is used to add or delete MPLS Tables. All |
| 598 | * Tables must be explicitly added before that can be used, |
| 599 | * Including the default table. |
| 600 | ?*/ |
| 601 | /* *INDENT-OFF* */ |
Neale Ranns | 44f8131 | 2017-09-19 06:25:13 -0700 | [diff] [blame] | 602 | VLIB_CLI_COMMAND (mpls_table_command, static) = { |
| 603 | .path = "mpls table", |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 604 | .short_help = "mpls table [add|del] <table-id>", |
| 605 | .function = vnet_mpls_table_cmd, |
| 606 | .is_mp_safe = 1, |
| 607 | }; |
| 608 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 609 | int |
| 610 | mpls_fib_reset_labels (u32 fib_id) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 611 | { |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 612 | // FIXME |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 613 | return 0; |
| 614 | } |
| 615 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 616 | static clib_error_t * |
| 617 | mpls_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 618 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 619 | clib_error_t * error; |
| 620 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 621 | if ((error = vlib_call_init_function (vm, ip_main_init))) |
| 622 | return error; |
| 623 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 624 | return vlib_call_init_function (vm, mpls_input_init); |
| 625 | } |
| 626 | |
| 627 | VLIB_INIT_FUNCTION (mpls_init); |