FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
interface.c
Go to the documentation of this file.
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>
20 #include <vnet/mpls/mpls.h>
21 #include <vnet/fib/mpls_fib.h>
22 #include <vnet/fib/ip4_fib.h>
23 #include <vnet/adj/adj_midchain.h>
24 #include <vnet/dpo/classify_dpo.h>
25 
26 
27 u8
29 {
30  mpls_main_t * mm = &mpls_main;
31 
32  if (vec_len(mm->mpls_enabled_by_sw_if_index) <= sw_if_index)
33  return (0);
34 
35  return (mm->mpls_enabled_by_sw_if_index[sw_if_index]);
36 }
37 
38 int
41  u8 is_enable,
42  u8 is_api)
43 {
44  fib_node_index_t lfib_index;
45  vnet_main_t *vnm = vnet_get_main ();
46  vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
47 
49 
50  lfib_index = fib_table_find(FIB_PROTOCOL_MPLS,
52 
53  if (~0 == lfib_index)
54  return VNET_API_ERROR_NO_SUCH_FIB;
55 
56  /*
57  * enable/disable only on the 1<->0 transition
58  */
59  if (is_enable)
60  {
61  if (1 != ++mm->mpls_enabled_by_sw_if_index[sw_if_index])
62  return (0);
63 
65  (is_api? FIB_SOURCE_API: FIB_SOURCE_CLI));
66 
67  vec_validate(mm->fib_index_by_sw_if_index, sw_if_index);
68  mm->fib_index_by_sw_if_index[sw_if_index] = lfib_index;
69  }
70  else
71  {
72  ASSERT(mm->mpls_enabled_by_sw_if_index[sw_if_index] > 0);
73  if (0 != --mm->mpls_enabled_by_sw_if_index[sw_if_index])
74  return (0);
75 
78  (is_api? FIB_SOURCE_API: FIB_SOURCE_CLI));
79  }
80 
81  vnet_feature_enable_disable ("mpls-input", "mpls-not-enabled",
82  sw_if_index, !is_enable, 0, 0);
83 
84  if (is_enable)
85  hi->l3_if_count++;
86  else if (hi->l3_if_count)
87  hi->l3_if_count--;
88 
89  return (0);
90 }
91 
92 static clib_error_t *
94  unformat_input_t * input,
95  vlib_cli_command_t * cmd)
96 {
97  vnet_main_t * vnm = vnet_get_main();
98  clib_error_t * error = 0;
99  u32 sw_if_index, enable;
100  int rv;
101 
102  sw_if_index = ~0;
103 
104  if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
105  {
106  error = clib_error_return (0, "unknown interface `%U'",
107  format_unformat_error, input);
108  goto done;
109  }
110 
111  if (unformat (input, "enable"))
112  enable = 1;
113  else if (unformat (input, "disable"))
114  enable = 0;
115  else
116  {
117  error = clib_error_return (0, "expected 'enable' or 'disable'",
118  format_unformat_error, input);
119  goto done;
120  }
121 
122  rv = mpls_sw_interface_enable_disable(&mpls_main, sw_if_index, enable, 0);
123 
124  if (VNET_API_ERROR_NO_SUCH_FIB == rv)
125  error = clib_error_return (0, "default MPLS table must be created first");
126 
127  done:
128  return error;
129 }
130 
131 /*?
132  * This command enables an interface to accpet MPLS packets
133  *
134  * @cliexpar
135  * @cliexstart{set interface mpls}
136  * set interface mpls GigEthernet0/8/0 enable
137  * @cliexend
138  ?*/
139 VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
140  .path = "set interface mpls",
141  .function = mpls_interface_enable_disable,
142  .short_help = "Enable/Disable an interface for MPLS forwarding",
143 };
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:509
static clib_error_t * mpls_interface_enable_disable(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: interface.c:93
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
u32 * fib_index_by_sw_if_index
Definition: mpls.h:44
u8 mpls_sw_interface_is_enabled(u32 sw_if_index)
Definition: interface.c:28
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
vlib_main_t * vm
Definition: in2out_ed.c:1582
unformat_function_t unformat_vnet_sw_interface
unsigned char u8
Definition: types.h:56
u8 * mpls_enabled_by_sw_if_index
Definition: mpls.h:60
#define clib_error_return(e, args...)
Definition: error.h:99
unsigned int u32
Definition: types.h:88
u32 fib_table_find(fib_protocol_t proto, u32 table_id)
Get the index of the FIB for a Table-ID.
Definition: fib_table.c:1097
struct _unformat_input_t unformat_input_t
void fib_table_unlock(u32 fib_index, fib_protocol_t proto, fib_source_t source)
Take a reference counting lock on the table.
Definition: fib_table.c:1291
#define MPLS_FIB_DEFAULT_TABLE_ID
Definition: mpls_fib.h:28
From the CLI.
Definition: fib_source.h:76
mpls_main_t mpls_main
Definition: mpls.c:25
int mpls_sw_interface_enable_disable(mpls_main_t *mm, u32 sw_if_index, u8 is_enable, u8 is_api)
Definition: interface.c:39
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:30
void fib_table_lock(u32 fib_index, fib_protocol_t proto, fib_source_t source)
Release a reference counting lock on the table.
Definition: fib_table.c:1310
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:158
#define ASSERT(truth)
From the control plane API.
Definition: fib_source.h:72
vl_api_ip4_address_t hi
Definition: arp.api:37
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:556
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:33
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
int vnet_feature_enable_disable(const char *arc_name, const char *node_name, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes)
Definition: feature.c:303