Line data Source code
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 :
29 : #include <plugins/linux-cp/lcp_interface.h>
30 :
31 : static clib_error_t *
32 0 : lcp_itf_pair_create_command_fn (vlib_main_t *vm, unformat_input_t *input,
33 : vlib_cli_command_t *cmd)
34 : {
35 0 : unformat_input_t _line_input, *line_input = &_line_input;
36 0 : vnet_main_t *vnm = vnet_get_main ();
37 0 : u32 sw_if_index = ~0;
38 0 : u8 *host_if_name = NULL;
39 0 : lip_host_type_t host_if_type = LCP_ITF_HOST_TAP;
40 0 : u8 *ns = NULL;
41 0 : clib_error_t *error = NULL;
42 :
43 0 : if (unformat_user (input, unformat_line_input, line_input))
44 : {
45 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
46 : {
47 0 : if (unformat (line_input, "%d", &sw_if_index))
48 : ;
49 0 : else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
50 : &sw_if_index))
51 : ;
52 0 : else if (unformat (line_input, "host-if %s", &host_if_name))
53 : ;
54 0 : else if (unformat (line_input, "netns %s", &ns))
55 : ;
56 0 : else if (unformat (line_input, "tun"))
57 0 : host_if_type = LCP_ITF_HOST_TUN;
58 : else
59 : {
60 0 : error = clib_error_return (0, "unknown input `%U'",
61 : format_unformat_error, line_input);
62 0 : break;
63 : }
64 : }
65 0 : unformat_free (line_input);
66 : }
67 :
68 0 : if (error)
69 : ;
70 0 : else if (sw_if_index == ~0)
71 0 : error = clib_error_return (0, "interface name or sw_if_index required");
72 0 : else if (!host_if_name)
73 0 : error = clib_error_return (0, "host interface name required");
74 0 : else if (vec_len (ns) >= LCP_NS_LEN)
75 0 : error = clib_error_return (
76 : 0, "Namespace name should be fewer than %d characters", LCP_NS_LEN);
77 : else
78 : {
79 : int r;
80 :
81 0 : r = lcp_itf_pair_create (sw_if_index, host_if_name, host_if_type, ns,
82 : NULL);
83 0 : if (r)
84 0 : error = clib_error_return (0, "linux-cp pair creation failed (%d)", r);
85 : }
86 :
87 0 : vec_free (host_if_name);
88 0 : vec_free (ns);
89 :
90 0 : return error;
91 : }
92 :
93 606 : VLIB_CLI_COMMAND (lcp_itf_pair_create_command, static) = {
94 : .path = "lcp create",
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,
98 : };
99 :
100 : static clib_error_t *
101 0 : lcp_sync_command_fn (vlib_main_t *vm, unformat_input_t *input,
102 : vlib_cli_command_t *cmd)
103 : {
104 0 : unformat_input_t _line_input, *line_input = &_line_input;
105 :
106 0 : if (!unformat_user (input, unformat_line_input, line_input))
107 0 : return 0;
108 :
109 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
110 : {
111 0 : if (unformat (line_input, "on") || unformat (line_input, "enable"))
112 0 : lcp_set_sync (1);
113 0 : else if (unformat (line_input, "off") ||
114 0 : unformat (line_input, "disable"))
115 0 : lcp_set_sync (0);
116 : else
117 0 : return clib_error_return (0, "unknown input `%U'",
118 : format_unformat_error, line_input);
119 : }
120 :
121 0 : unformat_free (line_input);
122 0 : return 0;
123 : }
124 :
125 606 : 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,
129 : };
130 :
131 : static clib_error_t *
132 0 : lcp_auto_subint_command_fn (vlib_main_t *vm, unformat_input_t *input,
133 : vlib_cli_command_t *cmd)
134 : {
135 0 : unformat_input_t _line_input, *line_input = &_line_input;
136 :
137 0 : if (!unformat_user (input, unformat_line_input, line_input))
138 0 : return 0;
139 :
140 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
141 : {
142 0 : if (unformat (line_input, "on") || unformat (line_input, "enable"))
143 0 : lcp_set_auto_subint (1);
144 0 : else if (unformat (line_input, "off") ||
145 0 : unformat (line_input, "disable"))
146 0 : lcp_set_auto_subint (0);
147 : else
148 0 : return clib_error_return (0, "unknown input `%U'",
149 : format_unformat_error, line_input);
150 : }
151 :
152 0 : unformat_free (line_input);
153 0 : return 0;
154 : }
155 :
156 606 : 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,
160 : };
161 :
162 : static clib_error_t *
163 0 : lcp_param_command_fn (vlib_main_t *vm, unformat_input_t *input,
164 : vlib_cli_command_t *cmd)
165 : {
166 0 : unformat_input_t _line_input, *line_input = &_line_input;
167 :
168 0 : if (!unformat_user (input, unformat_line_input, line_input))
169 0 : return 0;
170 :
171 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
172 : {
173 0 : if (unformat (line_input, "del-static-on-link-down"))
174 : {
175 0 : if (unformat (line_input, "on") || unformat (line_input, "enable"))
176 0 : lcp_set_del_static_on_link_down (1 /* is_del */);
177 0 : else if (unformat (line_input, "off") ||
178 0 : unformat (line_input, "disable"))
179 0 : lcp_set_del_static_on_link_down (0 /* is_del */);
180 : else
181 0 : return clib_error_return (0, "unknown input `%U'",
182 : format_unformat_error, line_input);
183 : }
184 0 : else if (unformat (line_input, "del-dynamic-on-link-down"))
185 : {
186 0 : if (unformat (line_input, "on") || unformat (line_input, "enable"))
187 0 : lcp_set_del_dynamic_on_link_down (1 /* is_del */);
188 0 : else if (unformat (line_input, "off") ||
189 0 : unformat (line_input, "disable"))
190 0 : lcp_set_del_dynamic_on_link_down (0 /* is_del */);
191 : else
192 0 : return clib_error_return (0, "unknown input `%U'",
193 : format_unformat_error, line_input);
194 : }
195 : else
196 0 : return clib_error_return (0, "unknown input `%U'",
197 : format_unformat_error, line_input);
198 : }
199 :
200 0 : unformat_free (line_input);
201 0 : return 0;
202 : }
203 :
204 606 : VLIB_CLI_COMMAND (lcp_param_command, static) = {
205 : .path = "lcp param",
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,
209 : };
210 :
211 : static clib_error_t *
212 0 : lcp_default_netns_command_fn (vlib_main_t *vm, unformat_input_t *input,
213 : vlib_cli_command_t *cmd)
214 : {
215 0 : unformat_input_t _line_input, *line_input = &_line_input;
216 : u8 *ns;
217 : int r;
218 0 : clib_error_t *error = NULL;
219 :
220 0 : if (!unformat_user (input, unformat_line_input, line_input))
221 0 : return 0;
222 :
223 0 : ns = 0;
224 :
225 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
226 : {
227 0 : if (unformat (line_input, "netns %s", &ns))
228 : ;
229 0 : else if (unformat (line_input, "clear netns"))
230 : ;
231 : else
232 : {
233 0 : vec_free (ns);
234 0 : error = clib_error_return (0, "unknown input `%U'",
235 : format_unformat_error, line_input);
236 0 : goto done;
237 : }
238 : }
239 :
240 0 : vlib_cli_output (vm, "lcp set default netns '%s'\n", (char *) ns);
241 :
242 0 : r = lcp_set_default_ns (ns);
243 :
244 0 : if (r)
245 0 : return clib_error_return (0, "linux-cp set default netns failed (%d)", r);
246 :
247 0 : done:
248 0 : unformat_free (line_input);
249 :
250 0 : return error;
251 : }
252 :
253 606 : 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,
257 : };
258 :
259 : static clib_error_t *
260 0 : lcp_itf_pair_delete_command_fn (vlib_main_t *vm, unformat_input_t *input,
261 : vlib_cli_command_t *cmd)
262 : {
263 0 : vnet_main_t *vnm = vnet_get_main ();
264 0 : unformat_input_t _line_input, *line_input = &_line_input;
265 0 : u32 sw_if_index = ~0;
266 0 : clib_error_t *error = NULL;
267 :
268 0 : if (unformat_user (input, unformat_line_input, line_input))
269 : {
270 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
271 : {
272 0 : if (unformat (line_input, "%d", &sw_if_index))
273 : ;
274 0 : else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
275 : &sw_if_index))
276 : ;
277 : else
278 : {
279 0 : error = clib_error_return (0, "unknown input `%U'",
280 : format_unformat_error, line_input);
281 0 : break;
282 : }
283 : }
284 0 : unformat_free (line_input);
285 : }
286 :
287 0 : if (error)
288 : ;
289 0 : else if (sw_if_index == ~0)
290 0 : error = clib_error_return (0, "interface name or sw_if_index required");
291 : else
292 : {
293 : int r;
294 :
295 0 : r = lcp_itf_pair_delete (sw_if_index);
296 0 : if (r)
297 0 : error = clib_error_return (0, "linux-cp pair deletion failed (%d)", r);
298 : }
299 :
300 0 : return error;
301 : }
302 :
303 606 : 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,
307 : };
308 :
309 : static clib_error_t *
310 3 : lcp_itf_pair_show_cmd (vlib_main_t *vm, unformat_input_t *input,
311 : vlib_cli_command_t *cmd)
312 : {
313 3 : vnet_main_t *vnm = vnet_get_main ();
314 : u32 phy_sw_if_index;
315 :
316 3 : phy_sw_if_index = ~0;
317 :
318 3 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
319 : {
320 0 : if (unformat (input, "phy %U", unformat_vnet_sw_interface, vnm,
321 : &phy_sw_if_index))
322 : ;
323 : else
324 0 : return clib_error_return (0, "unknown input '%U'",
325 : format_unformat_error, input);
326 : }
327 :
328 3 : lcp_itf_pair_show (phy_sw_if_index);
329 :
330 3 : return 0;
331 : }
332 :
333 606 : VLIB_CLI_COMMAND (lcp_itf_pair_show_cmd_node, static) = {
334 : .path = "show lcp",
335 : .function = lcp_itf_pair_show_cmd,
336 : .short_help = "show lcp [phy <interface>]",
337 : .is_mp_safe = 1,
338 : };
339 :
340 : clib_error_t *
341 2 : lcp_cli_init (vlib_main_t *vm)
342 : {
343 2 : return 0;
344 : }
345 :
346 12 : VLIB_INIT_FUNCTION (lcp_cli_init);
347 :
348 : /*
349 : * fd.io coding-style-patch-verification: ON
350 : *
351 : * Local Variables:
352 : * eval: (c-set-style "gnu")
353 : * End:
354 : */
|