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);
75 if (sw_if_index == ~0)
77 vec_free (host_if_name);
79 return clib_error_return (0, "interface name or sw_if_index required");
82 if (vec_len (ns) >= LCP_NS_LEN)
84 vec_free (host_if_name);
86 return clib_error_return (
87 0, "Namespace name should be fewer than %d characters", LCP_NS_LEN);
90 r = lcp_itf_pair_create (sw_if_index, host_if_name, host_if_type, ns);
92 vec_free (host_if_name);
96 return clib_error_return (0, "linux-cp pair creation failed (%d)", r);
101 VLIB_CLI_COMMAND (lcp_itf_pair_create_command, static) = {
102 .path = "lcp create",
103 .short_help = "lcp create <sw_if_index>|<if-name> host-if <host-if-name> "
104 "netns <namespace> [tun]",
105 .function = lcp_itf_pair_create_command_fn,
108 static clib_error_t *
109 lcp_default_netns_command_fn (vlib_main_t *vm, unformat_input_t *input,
110 vlib_cli_command_t *cmd)
112 unformat_input_t _line_input, *line_input = &_line_input;
116 if (!unformat_user (input, unformat_line_input, line_input))
121 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
123 if (unformat (line_input, "netns %s", &ns))
125 else if (unformat (line_input, "clear netns"))
129 unformat_free (line_input);
131 vlib_cli_output (vm, "lcp set default netns '%s'\n", (char *) ns);
133 r = lcp_set_default_ns (ns);
136 return clib_error_return (0, "linux-cp set default netns failed (%d)", r);
141 VLIB_CLI_COMMAND (lcp_default_netns_command, static) = {
142 .path = "lcp default",
143 .short_help = "lcp default netns [<namespace>]",
144 .function = lcp_default_netns_command_fn,
147 static clib_error_t *
148 lcp_itf_pair_delete_command_fn (vlib_main_t *vm, unformat_input_t *input,
149 vlib_cli_command_t *cmd)
151 vnet_main_t *vnm = vnet_get_main ();
152 unformat_input_t _line_input, *line_input = &_line_input;
156 if (!unformat_user (input, unformat_line_input, line_input))
161 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
163 if (unformat (line_input, "%d", &sw_if_index))
165 else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
169 return clib_error_return (0, "unknown input `%U'",
170 format_unformat_error, input);
173 unformat_free (line_input);
175 if (sw_if_index == ~0)
176 return clib_error_return (0, "interface name or sw_if_index required");
178 r = lcp_itf_pair_delete (sw_if_index);
181 return clib_error_return (0, "linux-cp pair deletion failed (%d)", r);
185 VLIB_CLI_COMMAND (lcp_itf_pair_delete_command, static) = {
186 .path = "lcp delete",
187 .short_help = "lcp delete <sw_if_index>|<if-name>",
188 .function = lcp_itf_pair_delete_command_fn,
191 static clib_error_t *
192 lcp_itf_pair_show_cmd (vlib_main_t *vm, unformat_input_t *input,
193 vlib_cli_command_t *cmd)
195 vnet_main_t *vnm = vnet_get_main ();
198 phy_sw_if_index = ~0;
200 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
202 if (unformat (input, "phy %U", unformat_vnet_sw_interface, vnm,
206 return clib_error_return (0, "unknown input '%U'",
207 format_unformat_error, input);
210 lcp_itf_pair_show (phy_sw_if_index);
215 VLIB_CLI_COMMAND (lcp_itf_pair_show_cmd_node, static) = {
217 .function = lcp_itf_pair_show_cmd,
218 .short_help = "show lcp [phy <interface>]",
223 lcp_cli_init (vlib_main_t *vm)
228 VLIB_INIT_FUNCTION (lcp_cli_init);
231 * fd.io coding-style-patch-verification: ON
234 * eval: (c-set-style "gnu")