blob: a085aaa2f19c1724537080f031dece04948920af [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>
19#include <vnet/pg/pg.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010020#include <vnet/mpls/mpls.h>
Neale Rannsa3af3372017-03-28 03:49:52 -070021#include <vnet/fib/mpls_fib.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010022#include <vnet/fib/ip4_fib.h>
23#include <vnet/adj/adj_midchain.h>
24#include <vnet/dpo/classify_dpo.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070025
Ed Warnickecb9cada2015-12-08 15:45:58 -070026
Neale Ranns0bfe5d82016-08-25 15:29:12 +010027u8
28mpls_sw_interface_is_enabled (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -070029{
Neale Ranns0bfe5d82016-08-25 15:29:12 +010030 mpls_main_t * mm = &mpls_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070031
Neale Ranns0bfe5d82016-08-25 15:29:12 +010032 if (vec_len(mm->mpls_enabled_by_sw_if_index) < sw_if_index)
33 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070034
Neale Ranns0bfe5d82016-08-25 15:29:12 +010035 return (mm->mpls_enabled_by_sw_if_index[sw_if_index]);
36}
37
38void
39mpls_sw_interface_enable_disable (mpls_main_t * mm,
40 u32 sw_if_index,
41 u8 is_enable)
42{
Neale Ranns0bfe5d82016-08-25 15:29:12 +010043 fib_node_index_t lfib_index;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010044
45 vec_validate_init_empty (mm->mpls_enabled_by_sw_if_index, sw_if_index, 0);
46
47 /*
48 * enable/disable only on the 1<->0 transition
49 */
50 if (is_enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -070051 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +010052 if (1 != ++mm->mpls_enabled_by_sw_if_index[sw_if_index])
53 return;
54
55 lfib_index = fib_table_find_or_create_and_lock(FIB_PROTOCOL_MPLS,
56 MPLS_FIB_DEFAULT_TABLE_ID);
57 vec_validate(mm->fib_index_by_sw_if_index, 0);
58 mm->fib_index_by_sw_if_index[sw_if_index] = lfib_index;
59 }
60 else
61 {
62 ASSERT(mm->mpls_enabled_by_sw_if_index[sw_if_index] > 0);
63 if (0 != --mm->mpls_enabled_by_sw_if_index[sw_if_index])
64 return;
65
66 fib_table_unlock(mm->fib_index_by_sw_if_index[sw_if_index],
67 FIB_PROTOCOL_MPLS);
Ed Warnickecb9cada2015-12-08 15:45:58 -070068 }
69
Neale Rannsb85e4392017-03-16 16:12:57 -040070 vnet_feature_enable_disable ("mpls-input", "mpls-not-enabled",
71 sw_if_index, !is_enable, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070072
Neale Ranns0bfe5d82016-08-25 15:29:12 +010073}
74
Neale Ranns0bfe5d82016-08-25 15:29:12 +010075static clib_error_t *
76mpls_interface_enable_disable (vlib_main_t * vm,
77 unformat_input_t * input,
78 vlib_cli_command_t * cmd)
79{
80 vnet_main_t * vnm = vnet_get_main();
81 clib_error_t * error = 0;
82 u32 sw_if_index, enable;
83
84 sw_if_index = ~0;
85
86 if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
87 {
88 error = clib_error_return (0, "unknown interface `%U'",
89 format_unformat_error, input);
90 goto done;
91 }
92
93 if (unformat (input, "enable"))
94 enable = 1;
95 else if (unformat (input, "disable"))
96 enable = 0;
97 else
98 {
99 error = clib_error_return (0, "expected 'enable' or 'disable'",
100 format_unformat_error, input);
101 goto done;
102 }
103
104 mpls_sw_interface_enable_disable(&mpls_main, sw_if_index, enable);
105
106 done:
107 return error;
108}
109
Neale Rannsad422ed2016-11-02 14:20:04 +0000110/*?
111 * This command enables an interface to accpet MPLS packets
112 *
113 * @cliexpar
114 * @cliexstart{set interface mpls}
115 * set interface mpls GigEthernet0/8/0 enable
116 * @cliexend
117 ?*/
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100118VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
119 .path = "set interface mpls",
120 .function = mpls_interface_enable_disable,
121 .short_help = "Enable/Disable an interface for MPLS forwarding",
122};