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