Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * interface.c: mpls interfaces |
| 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> |
| 19 | #include <vnet/pg/pg.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 20 | #include <vnet/mpls/mpls.h> |
| 21 | #include <vnet/fib/ip4_fib.h> |
| 22 | #include <vnet/adj/adj_midchain.h> |
| 23 | #include <vnet/dpo/classify_dpo.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 24 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 25 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 26 | u8 |
| 27 | mpls_sw_interface_is_enabled (u32 sw_if_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 28 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 29 | mpls_main_t * mm = &mpls_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 31 | if (vec_len(mm->mpls_enabled_by_sw_if_index) < sw_if_index) |
| 32 | return (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 33 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 34 | return (mm->mpls_enabled_by_sw_if_index[sw_if_index]); |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | mpls_sw_interface_enable_disable (mpls_main_t * mm, |
| 39 | u32 sw_if_index, |
| 40 | u8 is_enable) |
| 41 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 42 | fib_node_index_t lfib_index; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 43 | |
| 44 | vec_validate_init_empty (mm->mpls_enabled_by_sw_if_index, sw_if_index, 0); |
| 45 | |
| 46 | /* |
| 47 | * enable/disable only on the 1<->0 transition |
| 48 | */ |
| 49 | if (is_enable) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 51 | if (1 != ++mm->mpls_enabled_by_sw_if_index[sw_if_index]) |
| 52 | return; |
| 53 | |
| 54 | lfib_index = fib_table_find_or_create_and_lock(FIB_PROTOCOL_MPLS, |
| 55 | MPLS_FIB_DEFAULT_TABLE_ID); |
| 56 | vec_validate(mm->fib_index_by_sw_if_index, 0); |
| 57 | mm->fib_index_by_sw_if_index[sw_if_index] = lfib_index; |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | ASSERT(mm->mpls_enabled_by_sw_if_index[sw_if_index] > 0); |
| 62 | if (0 != --mm->mpls_enabled_by_sw_if_index[sw_if_index]) |
| 63 | return; |
| 64 | |
| 65 | fib_table_unlock(mm->fib_index_by_sw_if_index[sw_if_index], |
| 66 | FIB_PROTOCOL_MPLS); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 69 | vnet_feature_enable_disable ("mpls-input", "mpls-lookup", sw_if_index, |
| 70 | is_enable, 0, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 74 | static clib_error_t * |
| 75 | mpls_interface_enable_disable (vlib_main_t * vm, |
| 76 | unformat_input_t * input, |
| 77 | vlib_cli_command_t * cmd) |
| 78 | { |
| 79 | vnet_main_t * vnm = vnet_get_main(); |
| 80 | clib_error_t * error = 0; |
| 81 | u32 sw_if_index, enable; |
| 82 | |
| 83 | sw_if_index = ~0; |
| 84 | |
| 85 | if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 86 | { |
| 87 | error = clib_error_return (0, "unknown interface `%U'", |
| 88 | format_unformat_error, input); |
| 89 | goto done; |
| 90 | } |
| 91 | |
| 92 | if (unformat (input, "enable")) |
| 93 | enable = 1; |
| 94 | else if (unformat (input, "disable")) |
| 95 | enable = 0; |
| 96 | else |
| 97 | { |
| 98 | error = clib_error_return (0, "expected 'enable' or 'disable'", |
| 99 | format_unformat_error, input); |
| 100 | goto done; |
| 101 | } |
| 102 | |
| 103 | mpls_sw_interface_enable_disable(&mpls_main, sw_if_index, enable); |
| 104 | |
| 105 | done: |
| 106 | return error; |
| 107 | } |
| 108 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 109 | /*? |
| 110 | * This command enables an interface to accpet MPLS packets |
| 111 | * |
| 112 | * @cliexpar |
| 113 | * @cliexstart{set interface mpls} |
| 114 | * set interface mpls GigEthernet0/8/0 enable |
| 115 | * @cliexend |
| 116 | ?*/ |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 117 | VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = { |
| 118 | .path = "set interface mpls", |
| 119 | .function = mpls_interface_enable_disable, |
| 120 | .short_help = "Enable/Disable an interface for MPLS forwarding", |
| 121 | }; |