FD.io VPP  v21.06
Vector Packet Processing
lcp_cli.c
Go to the documentation of this file.
1 /* Hey Emacs use -*- mode: C -*- */
2 /*
3  * Copyright 2020 Rubicon Communications, LLC.
4  *
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 <sys/socket.h>
19 #include <linux/if.h>
20 
21 #include <vnet/vnet.h>
22 #include <vnet/plugin/plugin.h>
23 
24 #include <vlibapi/api.h>
25 #include <vlibmemory/api.h>
26 #include <vpp/app/version.h>
27 #include <vnet/format_fns.h>
28 
30 
31 static clib_error_t *
33  vlib_cli_command_t *cmd)
34 {
35  unformat_input_t _line_input, *line_input = &_line_input;
36  vnet_main_t *vnm = vnet_get_main ();
38  u8 *host_if_name;
39  lip_host_type_t host_if_type;
40  u8 *ns;
41  int r;
42 
43  if (!unformat_user (input, unformat_line_input, line_input))
44  return 0;
45 
46  sw_if_index = ~0;
47  host_if_name = ns = NULL;
48  host_if_type = LCP_ITF_HOST_TAP;
49 
50  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
51  {
52  if (unformat (line_input, "%d", &sw_if_index))
53  ;
54  else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
55  &sw_if_index))
56  ;
57  else if (unformat (line_input, "host-if %s", &host_if_name))
58  ;
59  else if (unformat (line_input, "netns %s", &ns))
60  ;
61  else if (unformat (line_input, "tun"))
62  host_if_type = LCP_ITF_HOST_TUN;
63  else
64  {
65  unformat_free (line_input);
66  vec_free (host_if_name);
67  vec_free (ns);
68  return clib_error_return (0, "unknown input `%U'",
69  format_unformat_error, input);
70  }
71  }
72 
73  unformat_free (line_input);
74 
75  if (!host_if_name)
76  {
77  vec_free (ns);
78  return clib_error_return (0, "host interface name required");
79  }
80 
81  if (sw_if_index == ~0)
82  {
83  vec_free (host_if_name);
84  vec_free (ns);
85  return clib_error_return (0, "interface name or sw_if_index required");
86  }
87 
88  if (vec_len (ns) >= LCP_NS_LEN)
89  {
90  vec_free (host_if_name);
91  vec_free (ns);
92  return clib_error_return (
93  0, "Namespace name should be fewer than %d characters", LCP_NS_LEN);
94  }
95 
96  r = lcp_itf_pair_create (sw_if_index, host_if_name, host_if_type, ns, NULL);
97 
98  vec_free (host_if_name);
99  vec_free (ns);
100 
101  if (r)
102  return clib_error_return (0, "linux-cp pair creation failed (%d)", r);
103 
104  return 0;
105 }
106 
107 VLIB_CLI_COMMAND (lcp_itf_pair_create_command, static) = {
108  .path = "lcp create",
109  .short_help = "lcp create <sw_if_index>|<if-name> host-if <host-if-name> "
110  "netns <namespace> [tun]",
111  .function = lcp_itf_pair_create_command_fn,
112 };
113 
114 static clib_error_t *
116  vlib_cli_command_t *cmd)
117 {
118  unformat_input_t _line_input, *line_input = &_line_input;
119  u8 *ns;
120  int r;
121 
122  if (!unformat_user (input, unformat_line_input, line_input))
123  return 0;
124 
125  ns = 0;
126 
127  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
128  {
129  if (unformat (line_input, "netns %s", &ns))
130  ;
131  else if (unformat (line_input, "clear netns"))
132  ;
133  }
134 
135  unformat_free (line_input);
136 
137  vlib_cli_output (vm, "lcp set default netns '%s'\n", (char *) ns);
138 
139  r = lcp_set_default_ns (ns);
140 
141  if (r)
142  return clib_error_return (0, "linux-cp set default netns failed (%d)", r);
143 
144  return 0;
145 }
146 
147 VLIB_CLI_COMMAND (lcp_default_netns_command, static) = {
148  .path = "lcp default",
149  .short_help = "lcp default netns [<namespace>]",
150  .function = lcp_default_netns_command_fn,
151 };
152 
153 static clib_error_t *
155  vlib_cli_command_t *cmd)
156 {
157  vnet_main_t *vnm = vnet_get_main ();
158  unformat_input_t _line_input, *line_input = &_line_input;
160  int r;
161 
162  if (!unformat_user (input, unformat_line_input, line_input))
163  return 0;
164 
165  sw_if_index = ~0;
166 
167  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
168  {
169  if (unformat (line_input, "%d", &sw_if_index))
170  ;
171  else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
172  &sw_if_index))
173  ;
174  else
175  return clib_error_return (0, "unknown input `%U'",
176  format_unformat_error, input);
177  }
178 
179  unformat_free (line_input);
180 
181  if (sw_if_index == ~0)
182  return clib_error_return (0, "interface name or sw_if_index required");
183 
184  r = lcp_itf_pair_delete (sw_if_index);
185 
186  if (r)
187  return clib_error_return (0, "linux-cp pair deletion failed (%d)", r);
188  return 0;
189 }
190 
191 VLIB_CLI_COMMAND (lcp_itf_pair_delete_command, static) = {
192  .path = "lcp delete",
193  .short_help = "lcp delete <sw_if_index>|<if-name>",
194  .function = lcp_itf_pair_delete_command_fn,
195 };
196 
197 static clib_error_t *
199  vlib_cli_command_t *cmd)
200 {
201  vnet_main_t *vnm = vnet_get_main ();
202  u32 phy_sw_if_index;
203 
204  phy_sw_if_index = ~0;
205 
207  {
208  if (unformat (input, "phy %U", unformat_vnet_sw_interface, vnm,
209  &phy_sw_if_index))
210  ;
211  else
212  return clib_error_return (0, "unknown input '%U'",
213  format_unformat_error, input);
214  }
215 
216  lcp_itf_pair_show (phy_sw_if_index);
217 
218  return 0;
219 }
220 
221 VLIB_CLI_COMMAND (lcp_itf_pair_show_cmd_node, static) = {
222  .path = "show lcp",
223  .function = lcp_itf_pair_show_cmd,
224  .short_help = "show lcp [phy <interface>]",
225  .is_mp_safe = 1,
226 };
227 
228 clib_error_t *
230 {
231  return 0;
232 }
233 
235 
236 /*
237  * fd.io coding-style-patch-verification: ON
238  *
239  * Local Variables:
240  * eval: (c-set-style "gnu")
241  * End:
242  */
lip_host_type_t
Definition: lcp_interface.h:33
vnet_hw_if_output_node_runtime_t * r
#define LCP_NS_LEN
Definition: lcp.h:20
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
int lcp_set_default_ns(u8 *ns)
Get/Set the default namespace for LCP host taps.
Definition: lcp.c:48
unformat_function_t unformat_vnet_sw_interface
static clib_error_t * lcp_itf_pair_delete_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: lcp_cli.c:154
unsigned char u8
Definition: types.h:56
unsigned int u32
Definition: types.h:88
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
#define clib_error_return(e, args...)
Definition: error.h:99
vnet_main_t * vnet_get_main(void)
unformat_function_t unformat_line_input
Definition: format.h:275
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
int lcp_itf_pair_create(u32 phy_sw_if_index, u8 *host_if_name, lip_host_type_t host_if_type, u8 *ns, u32 *host_sw_if_indexp)
Create an interface-pair from PHY sw_if_index and tap name.
struct _unformat_input_t unformat_input_t
clib_error_t * lcp_cli_init(vlib_main_t *vm)
Definition: lcp_cli.c:229
int lcp_itf_pair_delete(u32 phy_sw_if_index)
Delete a LCP_ITF_PAIR.
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:395
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
static clib_error_t * lcp_itf_pair_create_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: lcp_cli.c:32
static clib_error_t * lcp_itf_pair_show_cmd(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: lcp_cli.c:198
void lcp_itf_pair_show(u32 phy_sw_if_index)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static void unformat_free(unformat_input_t *i)
Definition: format.h:155
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
static clib_error_t * lcp_default_netns_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: lcp_cli.c:115
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163