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> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 19 | #include <vnet/mpls/mpls.h> |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 20 | #include <vnet/fib/mpls_fib.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 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 | |
Eyal Bari | cd30774 | 2018-07-22 12:45:15 +0300 | [diff] [blame] | 31 | if (vec_len(mm->mpls_enabled_by_sw_if_index) <= sw_if_index) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 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 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 37 | int |
Nathan Skrzypczak | 275bd79 | 2021-09-17 17:29:14 +0200 | [diff] [blame^] | 38 | mpls_sw_interface_enable_disable (mpls_main_t *mm, u32 sw_if_index, |
| 39 | u8 is_enable) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 40 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 41 | fib_node_index_t lfib_index; |
John Lo | 4a302ee | 2020-05-12 22:34:39 -0400 | [diff] [blame] | 42 | vnet_main_t *vnm = vnet_get_main (); |
| 43 | vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 44 | |
| 45 | vec_validate_init_empty (mm->mpls_enabled_by_sw_if_index, sw_if_index, 0); |
| 46 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 47 | lfib_index = fib_table_find(FIB_PROTOCOL_MPLS, |
| 48 | MPLS_FIB_DEFAULT_TABLE_ID); |
| 49 | |
| 50 | if (~0 == lfib_index) |
| 51 | return VNET_API_ERROR_NO_SUCH_FIB; |
| 52 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 53 | /* |
| 54 | * enable/disable only on the 1<->0 transition |
| 55 | */ |
| 56 | if (is_enable) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 58 | if (1 != ++mm->mpls_enabled_by_sw_if_index[sw_if_index]) |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 59 | return (0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 60 | |
Nathan Skrzypczak | 275bd79 | 2021-09-17 17:29:14 +0200 | [diff] [blame^] | 61 | fib_table_lock (lfib_index, FIB_PROTOCOL_MPLS, FIB_SOURCE_INTERFACE); |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 62 | |
Neale Ranns | 44cea22 | 2018-12-04 09:39:40 +0000 | [diff] [blame] | 63 | vec_validate(mm->fib_index_by_sw_if_index, sw_if_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 64 | mm->fib_index_by_sw_if_index[sw_if_index] = lfib_index; |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | ASSERT(mm->mpls_enabled_by_sw_if_index[sw_if_index] > 0); |
| 69 | if (0 != --mm->mpls_enabled_by_sw_if_index[sw_if_index]) |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 70 | return (0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 71 | |
Nathan Skrzypczak | 275bd79 | 2021-09-17 17:29:14 +0200 | [diff] [blame^] | 72 | fib_table_unlock (mm->fib_index_by_sw_if_index[sw_if_index], |
| 73 | FIB_PROTOCOL_MPLS, FIB_SOURCE_INTERFACE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Neale Ranns | b85e439 | 2017-03-16 16:12:57 -0400 | [diff] [blame] | 76 | vnet_feature_enable_disable ("mpls-input", "mpls-not-enabled", |
| 77 | sw_if_index, !is_enable, 0, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | |
John Lo | 4a302ee | 2020-05-12 22:34:39 -0400 | [diff] [blame] | 79 | if (is_enable) |
| 80 | hi->l3_if_count++; |
| 81 | else if (hi->l3_if_count) |
| 82 | hi->l3_if_count--; |
| 83 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 84 | return (0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 87 | static clib_error_t * |
| 88 | mpls_interface_enable_disable (vlib_main_t * vm, |
| 89 | unformat_input_t * input, |
| 90 | vlib_cli_command_t * cmd) |
| 91 | { |
| 92 | vnet_main_t * vnm = vnet_get_main(); |
| 93 | clib_error_t * error = 0; |
| 94 | u32 sw_if_index, enable; |
Neale Ranns | 3ce7bcb | 2017-11-22 02:49:13 -0800 | [diff] [blame] | 95 | int rv; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 96 | |
| 97 | sw_if_index = ~0; |
| 98 | |
| 99 | if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 100 | { |
| 101 | error = clib_error_return (0, "unknown interface `%U'", |
| 102 | format_unformat_error, input); |
| 103 | goto done; |
| 104 | } |
| 105 | |
| 106 | if (unformat (input, "enable")) |
| 107 | enable = 1; |
| 108 | else if (unformat (input, "disable")) |
| 109 | enable = 0; |
| 110 | else |
| 111 | { |
| 112 | error = clib_error_return (0, "expected 'enable' or 'disable'", |
| 113 | format_unformat_error, input); |
| 114 | goto done; |
| 115 | } |
| 116 | |
Nathan Skrzypczak | 275bd79 | 2021-09-17 17:29:14 +0200 | [diff] [blame^] | 117 | rv = mpls_sw_interface_enable_disable (&mpls_main, sw_if_index, enable); |
Neale Ranns | 3ce7bcb | 2017-11-22 02:49:13 -0800 | [diff] [blame] | 118 | |
| 119 | if (VNET_API_ERROR_NO_SUCH_FIB == rv) |
| 120 | error = clib_error_return (0, "default MPLS table must be created first"); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 121 | |
| 122 | done: |
| 123 | return error; |
| 124 | } |
| 125 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 126 | /*? |
Nathan Skrzypczak | 2c77ae4 | 2021-09-29 15:36:51 +0200 | [diff] [blame] | 127 | * This command enables an interface to accept MPLS packets |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 128 | * |
| 129 | * @cliexpar |
| 130 | * @cliexstart{set interface mpls} |
| 131 | * set interface mpls GigEthernet0/8/0 enable |
| 132 | * @cliexend |
| 133 | ?*/ |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 134 | VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = { |
| 135 | .path = "set interface mpls", |
| 136 | .function = mpls_interface_enable_disable, |
| 137 | .short_help = "Enable/Disable an interface for MPLS forwarding", |
| 138 | }; |
Neale Ranns | de0b3b5 | 2021-06-15 12:43:12 +0000 | [diff] [blame] | 139 | |
| 140 | static void |
| 141 | show_mpls_one_interface (vnet_main_t *vnm, vlib_main_t *vm, u32 sw_if_index, |
| 142 | bool verbose) |
| 143 | { |
| 144 | mpls_main_t *mm = &mpls_main; |
| 145 | u8 enabled; |
| 146 | |
| 147 | enabled = mm->mpls_enabled_by_sw_if_index[sw_if_index]; |
| 148 | |
| 149 | if (enabled) |
| 150 | { |
| 151 | vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnm, |
| 152 | sw_if_index); |
| 153 | vlib_cli_output (vm, " MPLS enabled"); |
| 154 | } |
| 155 | else if (verbose) |
| 156 | { |
| 157 | vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnm, |
| 158 | sw_if_index); |
| 159 | vlib_cli_output (vm, " MPLS disabled"); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | static walk_rc_t |
| 164 | show_mpls_interface_walk (vnet_main_t *vnm, vnet_sw_interface_t *si, void *ctx) |
| 165 | { |
| 166 | show_mpls_one_interface (vnm, ctx, si->sw_if_index, false); |
| 167 | |
| 168 | return (WALK_CONTINUE); |
| 169 | } |
| 170 | |
| 171 | static clib_error_t * |
| 172 | show_mpls_interface (vlib_main_t *vm, unformat_input_t *input, |
| 173 | vlib_cli_command_t *cmd) |
| 174 | { |
| 175 | vnet_main_t *vnm = vnet_get_main (); |
| 176 | u32 sw_if_index; |
| 177 | |
| 178 | sw_if_index = ~0; |
| 179 | |
| 180 | if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 181 | ; |
| 182 | |
| 183 | if (~0 == sw_if_index) |
| 184 | { |
| 185 | vnet_sw_interface_walk (vnm, show_mpls_interface_walk, vm); |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | show_mpls_one_interface (vnm, vm, sw_if_index, true); |
| 190 | } |
| 191 | |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | /*? |
| 196 | * This command displays the MPLS forwarding state of an interface |
| 197 | * |
| 198 | * @cliexpar |
| 199 | * @cliexstart{show mpls interface} |
| 200 | * set mpls interface GigEthernet0/8/0 |
| 201 | * @cliexend |
| 202 | ?*/ |
| 203 | VLIB_CLI_COMMAND (show_mpls_interface_command, static) = { |
| 204 | .path = "show mpls interface", |
| 205 | .function = show_mpls_interface, |
| 206 | .short_help = "Show MPLS interface forwarding", |
| 207 | }; |