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 ();
39 lip_host_type_t host_if_type;
43 if (!unformat_user (input, unformat_line_input, line_input))
47 host_if_name = ns = NULL;
48 host_if_type = LCP_ITF_HOST_TAP;
50 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
52 if (unformat (line_input, "%d", &sw_if_index))
54 else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
57 else if (unformat (line_input, "host-if %s", &host_if_name))
59 else if (unformat (line_input, "netns %s", &ns))
61 else if (unformat (line_input, "tun"))
62 host_if_type = LCP_ITF_HOST_TUN;
65 unformat_free (line_input);
66 vec_free (host_if_name);
68 return clib_error_return (0, "unknown input `%U'",
69 format_unformat_error, input);
73 unformat_free (line_input);
78 return clib_error_return (0, "host interface name required");
81 if (sw_if_index == ~0)
83 vec_free (host_if_name);
85 return clib_error_return (0, "interface name or sw_if_index required");
88 if (vec_len (ns) >= LCP_NS_LEN)
90 vec_free (host_if_name);
92 return clib_error_return (
93 0, "Namespace name should be fewer than %d characters", LCP_NS_LEN);
96 r = lcp_itf_pair_create (sw_if_index, host_if_name, host_if_type, ns, NULL);
98 vec_free (host_if_name);
102 return clib_error_return (0, "linux-cp pair creation failed (%d)", r);
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,
114 static clib_error_t *
115 lcp_sync_command_fn (vlib_main_t *vm, unformat_input_t *input,
116 vlib_cli_command_t *cmd)
118 unformat_input_t _line_input, *line_input = &_line_input;
120 if (!unformat_user (input, unformat_line_input, line_input))
123 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
125 if (unformat (line_input, "on") || unformat (line_input, "enable"))
127 else if (unformat (line_input, "off") ||
128 unformat (line_input, "disable"))
131 return clib_error_return (0, "unknown input `%U'",
132 format_unformat_error, line_input);
135 unformat_free (line_input);
139 VLIB_CLI_COMMAND (lcp_sync_command, static) = {
140 .path = "lcp lcp-sync",
141 .short_help = "lcp lcp-sync [on|enable|off|disable]",
142 .function = lcp_sync_command_fn,
145 static clib_error_t *
146 lcp_auto_subint_command_fn (vlib_main_t *vm, unformat_input_t *input,
147 vlib_cli_command_t *cmd)
149 unformat_input_t _line_input, *line_input = &_line_input;
151 if (!unformat_user (input, unformat_line_input, line_input))
154 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
156 if (unformat (line_input, "on") || unformat (line_input, "enable"))
157 lcp_set_auto_subint (1);
158 else if (unformat (line_input, "off") ||
159 unformat (line_input, "disable"))
160 lcp_set_auto_subint (0);
162 return clib_error_return (0, "unknown input `%U'",
163 format_unformat_error, line_input);
166 unformat_free (line_input);
170 VLIB_CLI_COMMAND (lcp_auto_subint_command, static) = {
171 .path = "lcp lcp-auto-subint",
172 .short_help = "lcp lcp-auto-subint [on|enable|off|disable]",
173 .function = lcp_auto_subint_command_fn,
176 static clib_error_t *
177 lcp_default_netns_command_fn (vlib_main_t *vm, unformat_input_t *input,
178 vlib_cli_command_t *cmd)
180 unformat_input_t _line_input, *line_input = &_line_input;
184 if (!unformat_user (input, unformat_line_input, line_input))
189 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
191 if (unformat (line_input, "netns %s", &ns))
193 else if (unformat (line_input, "clear netns"))
197 unformat_free (line_input);
199 vlib_cli_output (vm, "lcp set default netns '%s'\n", (char *) ns);
201 r = lcp_set_default_ns (ns);
204 return clib_error_return (0, "linux-cp set default netns failed (%d)", r);
209 VLIB_CLI_COMMAND (lcp_default_netns_command, static) = {
210 .path = "lcp default",
211 .short_help = "lcp default netns [<namespace>]",
212 .function = lcp_default_netns_command_fn,
215 static clib_error_t *
216 lcp_itf_pair_delete_command_fn (vlib_main_t *vm, unformat_input_t *input,
217 vlib_cli_command_t *cmd)
219 vnet_main_t *vnm = vnet_get_main ();
220 unformat_input_t _line_input, *line_input = &_line_input;
224 if (!unformat_user (input, unformat_line_input, line_input))
229 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
231 if (unformat (line_input, "%d", &sw_if_index))
233 else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
237 return clib_error_return (0, "unknown input `%U'",
238 format_unformat_error, input);
241 unformat_free (line_input);
243 if (sw_if_index == ~0)
244 return clib_error_return (0, "interface name or sw_if_index required");
246 r = lcp_itf_pair_delete (sw_if_index);
249 return clib_error_return (0, "linux-cp pair deletion failed (%d)", r);
253 VLIB_CLI_COMMAND (lcp_itf_pair_delete_command, static) = {
254 .path = "lcp delete",
255 .short_help = "lcp delete <sw_if_index>|<if-name>",
256 .function = lcp_itf_pair_delete_command_fn,
259 static clib_error_t *
260 lcp_itf_pair_show_cmd (vlib_main_t *vm, unformat_input_t *input,
261 vlib_cli_command_t *cmd)
263 vnet_main_t *vnm = vnet_get_main ();
266 phy_sw_if_index = ~0;
268 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
270 if (unformat (input, "phy %U", unformat_vnet_sw_interface, vnm,
274 return clib_error_return (0, "unknown input '%U'",
275 format_unformat_error, input);
278 lcp_itf_pair_show (phy_sw_if_index);
283 VLIB_CLI_COMMAND (lcp_itf_pair_show_cmd_node, static) = {
285 .function = lcp_itf_pair_show_cmd,
286 .short_help = "show lcp [phy <interface>]",
291 lcp_cli_init (vlib_main_t *vm)
296 VLIB_INIT_FUNCTION (lcp_cli_init);
299 * fd.io coding-style-patch-verification: ON
302 * eval: (c-set-style "gnu")