1 /* Hey Emacs use -*- mode: C -*- */
3 * Copyright 2020 Rubicon Communications, LLC.
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/socket.h>
21 #include <vnet/vnet.h>
22 #include <vnet/plugin/plugin.h>
24 #include <vlibapi/api.h>
25 #include <vlibmemory/api.h>
26 #include <vpp/app/version.h>
27 #include <vnet/format_fns.h>
29 #include <plugins/linux-cp/lcp_interface.h>
32 lcp_itf_pair_create_command_fn (vlib_main_t *vm, unformat_input_t *input,
33 vlib_cli_command_t *cmd)
35 unformat_input_t _line_input, *line_input = &_line_input;
36 vnet_main_t *vnm = vnet_get_main ();
38 u8 *host_if_name = NULL;
39 lip_host_type_t host_if_type = LCP_ITF_HOST_TAP;
41 clib_error_t *error = NULL;
43 if (unformat_user (input, unformat_line_input, line_input))
45 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
47 if (unformat (line_input, "%d", &sw_if_index))
49 else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
52 else if (unformat (line_input, "host-if %s", &host_if_name))
54 else if (unformat (line_input, "netns %s", &ns))
56 else if (unformat (line_input, "tun"))
57 host_if_type = LCP_ITF_HOST_TUN;
60 error = clib_error_return (0, "unknown input `%U'",
61 format_unformat_error, line_input);
65 unformat_free (line_input);
70 else if (sw_if_index == ~0)
71 error = clib_error_return (0, "interface name or sw_if_index required");
72 else if (!host_if_name)
73 error = clib_error_return (0, "host interface name required");
74 else if (vec_len (ns) >= LCP_NS_LEN)
75 error = clib_error_return (
76 0, "Namespace name should be fewer than %d characters", LCP_NS_LEN);
81 r = lcp_itf_pair_create (sw_if_index, host_if_name, host_if_type, ns,
84 error = clib_error_return (0, "linux-cp pair creation failed (%d)", r);
87 vec_free (host_if_name);
93 VLIB_CLI_COMMAND (lcp_itf_pair_create_command, static) = {
95 .short_help = "lcp create <sw_if_index>|<if-name> host-if <host-if-name> "
96 "netns <namespace> [tun]",
97 .function = lcp_itf_pair_create_command_fn,
100 static clib_error_t *
101 lcp_sync_command_fn (vlib_main_t *vm, unformat_input_t *input,
102 vlib_cli_command_t *cmd)
104 unformat_input_t _line_input, *line_input = &_line_input;
106 if (!unformat_user (input, unformat_line_input, line_input))
109 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
111 if (unformat (line_input, "on") || unformat (line_input, "enable"))
113 else if (unformat (line_input, "off") ||
114 unformat (line_input, "disable"))
117 return clib_error_return (0, "unknown input `%U'",
118 format_unformat_error, line_input);
121 unformat_free (line_input);
125 VLIB_CLI_COMMAND (lcp_sync_command, static) = {
126 .path = "lcp lcp-sync",
127 .short_help = "lcp lcp-sync [on|enable|off|disable]",
128 .function = lcp_sync_command_fn,
131 static clib_error_t *
132 lcp_auto_subint_command_fn (vlib_main_t *vm, unformat_input_t *input,
133 vlib_cli_command_t *cmd)
135 unformat_input_t _line_input, *line_input = &_line_input;
137 if (!unformat_user (input, unformat_line_input, line_input))
140 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
142 if (unformat (line_input, "on") || unformat (line_input, "enable"))
143 lcp_set_auto_subint (1);
144 else if (unformat (line_input, "off") ||
145 unformat (line_input, "disable"))
146 lcp_set_auto_subint (0);
148 return clib_error_return (0, "unknown input `%U'",
149 format_unformat_error, line_input);
152 unformat_free (line_input);
156 VLIB_CLI_COMMAND (lcp_auto_subint_command, static) = {
157 .path = "lcp lcp-auto-subint",
158 .short_help = "lcp lcp-auto-subint [on|enable|off|disable]",
159 .function = lcp_auto_subint_command_fn,
162 static clib_error_t *
163 lcp_param_command_fn (vlib_main_t *vm, unformat_input_t *input,
164 vlib_cli_command_t *cmd)
166 unformat_input_t _line_input, *line_input = &_line_input;
168 if (!unformat_user (input, unformat_line_input, line_input))
171 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
173 if (unformat (line_input, "del-static-on-link-down"))
175 if (unformat (line_input, "on") || unformat (line_input, "enable"))
176 lcp_set_del_static_on_link_down (1 /* is_del */);
177 else if (unformat (line_input, "off") ||
178 unformat (line_input, "disable"))
179 lcp_set_del_static_on_link_down (0 /* is_del */);
181 return clib_error_return (0, "unknown input `%U'",
182 format_unformat_error, line_input);
184 else if (unformat (line_input, "del-dynamic-on-link-down"))
186 if (unformat (line_input, "on") || unformat (line_input, "enable"))
187 lcp_set_del_dynamic_on_link_down (1 /* is_del */);
188 else if (unformat (line_input, "off") ||
189 unformat (line_input, "disable"))
190 lcp_set_del_dynamic_on_link_down (0 /* is_del */);
192 return clib_error_return (0, "unknown input `%U'",
193 format_unformat_error, line_input);
196 return clib_error_return (0, "unknown input `%U'",
197 format_unformat_error, line_input);
200 unformat_free (line_input);
204 VLIB_CLI_COMMAND (lcp_param_command, static) = {
206 .short_help = "lcp param [del-static-on-link-down (on|enable|off|disable)] "
207 "[del-dynamic-on-link-down (on|enable|off|disable)]",
208 .function = lcp_param_command_fn,
211 static clib_error_t *
212 lcp_default_netns_command_fn (vlib_main_t *vm, unformat_input_t *input,
213 vlib_cli_command_t *cmd)
215 unformat_input_t _line_input, *line_input = &_line_input;
218 clib_error_t *error = NULL;
220 if (!unformat_user (input, unformat_line_input, line_input))
225 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
227 if (unformat (line_input, "netns %s", &ns))
229 else if (unformat (line_input, "clear netns"))
234 error = clib_error_return (0, "unknown input `%U'",
235 format_unformat_error, line_input);
240 vlib_cli_output (vm, "lcp set default netns '%s'\n", (char *) ns);
242 r = lcp_set_default_ns (ns);
245 return clib_error_return (0, "linux-cp set default netns failed (%d)", r);
248 unformat_free (line_input);
253 VLIB_CLI_COMMAND (lcp_default_netns_command, static) = {
254 .path = "lcp default",
255 .short_help = "lcp default netns [<namespace>]",
256 .function = lcp_default_netns_command_fn,
259 static clib_error_t *
260 lcp_itf_pair_delete_command_fn (vlib_main_t *vm, unformat_input_t *input,
261 vlib_cli_command_t *cmd)
263 vnet_main_t *vnm = vnet_get_main ();
264 unformat_input_t _line_input, *line_input = &_line_input;
265 u32 sw_if_index = ~0;
266 clib_error_t *error = NULL;
268 if (unformat_user (input, unformat_line_input, line_input))
270 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
272 if (unformat (line_input, "%d", &sw_if_index))
274 else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
279 error = clib_error_return (0, "unknown input `%U'",
280 format_unformat_error, line_input);
284 unformat_free (line_input);
289 else if (sw_if_index == ~0)
290 error = clib_error_return (0, "interface name or sw_if_index required");
295 r = lcp_itf_pair_delete (sw_if_index);
297 error = clib_error_return (0, "linux-cp pair deletion failed (%d)", r);
303 VLIB_CLI_COMMAND (lcp_itf_pair_delete_command, static) = {
304 .path = "lcp delete",
305 .short_help = "lcp delete <sw_if_index>|<if-name>",
306 .function = lcp_itf_pair_delete_command_fn,
309 static clib_error_t *
310 lcp_itf_pair_show_cmd (vlib_main_t *vm, unformat_input_t *input,
311 vlib_cli_command_t *cmd)
313 vnet_main_t *vnm = vnet_get_main ();
316 phy_sw_if_index = ~0;
318 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
320 if (unformat (input, "phy %U", unformat_vnet_sw_interface, vnm,
324 return clib_error_return (0, "unknown input '%U'",
325 format_unformat_error, input);
328 lcp_itf_pair_show (phy_sw_if_index);
333 VLIB_CLI_COMMAND (lcp_itf_pair_show_cmd_node, static) = {
335 .function = lcp_itf_pair_show_cmd,
336 .short_help = "show lcp [phy <interface>]",
341 lcp_cli_init (vlib_main_t *vm)
346 VLIB_INIT_FUNCTION (lcp_cli_init);
349 * fd.io coding-style-patch-verification: ON
352 * eval: (c-set-style "gnu")