blob: 1d10cabb1e3ffa5c65d039986eafb5950af5812a [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Ranns0bfe5d82016-08-25 15:29:12 +010019#include <vnet/mpls/mpls.h>
Neale Rannsa3af3372017-03-28 03:49:52 -070020#include <vnet/fib/mpls_fib.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010021#include <vnet/fib/ip4_fib.h>
22#include <vnet/adj/adj_midchain.h>
23#include <vnet/dpo/classify_dpo.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070024
Ed Warnickecb9cada2015-12-08 15:45:58 -070025
Neale Ranns0bfe5d82016-08-25 15:29:12 +010026u8
27mpls_sw_interface_is_enabled (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -070028{
Neale Ranns0bfe5d82016-08-25 15:29:12 +010029 mpls_main_t * mm = &mpls_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070030
Eyal Baricd307742018-07-22 12:45:15 +030031 if (vec_len(mm->mpls_enabled_by_sw_if_index) <= sw_if_index)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010032 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070033
Neale Ranns0bfe5d82016-08-25 15:29:12 +010034 return (mm->mpls_enabled_by_sw_if_index[sw_if_index]);
35}
36
Neale Ranns15002542017-09-10 04:39:11 -070037int
Neale Ranns0bfe5d82016-08-25 15:29:12 +010038mpls_sw_interface_enable_disable (mpls_main_t * mm,
39 u32 sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -070040 u8 is_enable,
41 u8 is_api)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010042{
Neale Ranns0bfe5d82016-08-25 15:29:12 +010043 fib_node_index_t lfib_index;
John Lo4a302ee2020-05-12 22:34:39 -040044 vnet_main_t *vnm = vnet_get_main ();
45 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010046
47 vec_validate_init_empty (mm->mpls_enabled_by_sw_if_index, sw_if_index, 0);
48
Neale Ranns15002542017-09-10 04:39:11 -070049 lfib_index = fib_table_find(FIB_PROTOCOL_MPLS,
50 MPLS_FIB_DEFAULT_TABLE_ID);
51
52 if (~0 == lfib_index)
53 return VNET_API_ERROR_NO_SUCH_FIB;
54
Neale Ranns0bfe5d82016-08-25 15:29:12 +010055 /*
56 * enable/disable only on the 1<->0 transition
57 */
58 if (is_enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -070059 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +010060 if (1 != ++mm->mpls_enabled_by_sw_if_index[sw_if_index])
Neale Ranns15002542017-09-10 04:39:11 -070061 return (0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010062
Neale Ranns15002542017-09-10 04:39:11 -070063 fib_table_lock(lfib_index, FIB_PROTOCOL_MPLS,
64 (is_api? FIB_SOURCE_API: FIB_SOURCE_CLI));
65
Neale Ranns44cea222018-12-04 09:39:40 +000066 vec_validate(mm->fib_index_by_sw_if_index, sw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010067 mm->fib_index_by_sw_if_index[sw_if_index] = lfib_index;
68 }
69 else
70 {
71 ASSERT(mm->mpls_enabled_by_sw_if_index[sw_if_index] > 0);
72 if (0 != --mm->mpls_enabled_by_sw_if_index[sw_if_index])
Neale Ranns15002542017-09-10 04:39:11 -070073 return (0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010074
75 fib_table_unlock(mm->fib_index_by_sw_if_index[sw_if_index],
Neale Ranns15002542017-09-10 04:39:11 -070076 FIB_PROTOCOL_MPLS,
77 (is_api? FIB_SOURCE_API: FIB_SOURCE_CLI));
Ed Warnickecb9cada2015-12-08 15:45:58 -070078 }
79
Neale Rannsb85e4392017-03-16 16:12:57 -040080 vnet_feature_enable_disable ("mpls-input", "mpls-not-enabled",
81 sw_if_index, !is_enable, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
John Lo4a302ee2020-05-12 22:34:39 -040083 if (is_enable)
84 hi->l3_if_count++;
85 else if (hi->l3_if_count)
86 hi->l3_if_count--;
87
Neale Ranns15002542017-09-10 04:39:11 -070088 return (0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010089}
90
Neale Ranns0bfe5d82016-08-25 15:29:12 +010091static clib_error_t *
92mpls_interface_enable_disable (vlib_main_t * vm,
93 unformat_input_t * input,
94 vlib_cli_command_t * cmd)
95{
96 vnet_main_t * vnm = vnet_get_main();
97 clib_error_t * error = 0;
98 u32 sw_if_index, enable;
Neale Ranns3ce7bcb2017-11-22 02:49:13 -080099 int rv;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100100
101 sw_if_index = ~0;
102
103 if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
104 {
105 error = clib_error_return (0, "unknown interface `%U'",
106 format_unformat_error, input);
107 goto done;
108 }
109
110 if (unformat (input, "enable"))
111 enable = 1;
112 else if (unformat (input, "disable"))
113 enable = 0;
114 else
115 {
116 error = clib_error_return (0, "expected 'enable' or 'disable'",
117 format_unformat_error, input);
118 goto done;
119 }
120
Neale Ranns3ce7bcb2017-11-22 02:49:13 -0800121 rv = mpls_sw_interface_enable_disable(&mpls_main, sw_if_index, enable, 0);
122
123 if (VNET_API_ERROR_NO_SUCH_FIB == rv)
124 error = clib_error_return (0, "default MPLS table must be created first");
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100125
126 done:
127 return error;
128}
129
Neale Rannsad422ed2016-11-02 14:20:04 +0000130/*?
131 * This command enables an interface to accpet MPLS packets
132 *
133 * @cliexpar
134 * @cliexstart{set interface mpls}
135 * set interface mpls GigEthernet0/8/0 enable
136 * @cliexend
137 ?*/
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100138VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
139 .path = "set interface mpls",
140 .function = mpls_interface_enable_disable,
141 .short_help = "Enable/Disable an interface for MPLS forwarding",
142};