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;
219 if (!unformat_user (input, unformat_line_input, line_input))
224 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
226 if (unformat (line_input, "netns %s", &ns))
228 else if (unformat (line_input, "clear netns"))
232 unformat_free (line_input);
234 vlib_cli_output (vm, "lcp set default netns '%s'\n", (char *) ns);
236 r = lcp_set_default_ns (ns);
239 return clib_error_return (0, "linux-cp set default netns failed (%d)", r);
244 VLIB_CLI_COMMAND (lcp_default_netns_command, static) = {
245 .path = "lcp default",
246 .short_help = "lcp default netns [<namespace>]",
247 .function = lcp_default_netns_command_fn,
250 static clib_error_t *
251 lcp_itf_pair_delete_command_fn (vlib_main_t *vm, unformat_input_t *input,
252 vlib_cli_command_t *cmd)
254 vnet_main_t *vnm = vnet_get_main ();
255 unformat_input_t _line_input, *line_input = &_line_input;
256 u32 sw_if_index = ~0;
257 clib_error_t *error = NULL;
259 if (unformat_user (input, unformat_line_input, line_input))
261 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
263 if (unformat (line_input, "%d", &sw_if_index))
265 else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
270 error = clib_error_return (0, "unknown input `%U'",
271 format_unformat_error, line_input);
275 unformat_free (line_input);
280 else if (sw_if_index == ~0)
281 error = clib_error_return (0, "interface name or sw_if_index required");
286 r = lcp_itf_pair_delete (sw_if_index);
288 error = clib_error_return (0, "linux-cp pair deletion failed (%d)", r);
294 VLIB_CLI_COMMAND (lcp_itf_pair_delete_command, static) = {
295 .path = "lcp delete",
296 .short_help = "lcp delete <sw_if_index>|<if-name>",
297 .function = lcp_itf_pair_delete_command_fn,
300 static clib_error_t *
301 lcp_itf_pair_show_cmd (vlib_main_t *vm, unformat_input_t *input,
302 vlib_cli_command_t *cmd)
304 vnet_main_t *vnm = vnet_get_main ();
307 phy_sw_if_index = ~0;
309 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
311 if (unformat (input, "phy %U", unformat_vnet_sw_interface, vnm,
315 return clib_error_return (0, "unknown input '%U'",
316 format_unformat_error, input);
319 lcp_itf_pair_show (phy_sw_if_index);
324 VLIB_CLI_COMMAND (lcp_itf_pair_show_cmd_node, static) = {
326 .function = lcp_itf_pair_show_cmd,
327 .short_help = "show lcp [phy <interface>]",
332 lcp_cli_init (vlib_main_t *vm)
337 VLIB_INIT_FUNCTION (lcp_cli_init);
340 * fd.io coding-style-patch-verification: ON
343 * eval: (c-set-style "gnu")