linux-cp: fix some CLI error messages
[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 = ~0;
38   u8 *host_if_name = NULL;
39   lip_host_type_t host_if_type = LCP_ITF_HOST_TAP;
40   u8 *ns = NULL;
41   clib_error_t *error = NULL;
42
43   if (unformat_user (input, unformat_line_input, line_input))
44     {
45       while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
46         {
47           if (unformat (line_input, "%d", &sw_if_index))
48             ;
49           else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
50                              &sw_if_index))
51             ;
52           else if (unformat (line_input, "host-if %s", &host_if_name))
53             ;
54           else if (unformat (line_input, "netns %s", &ns))
55             ;
56           else if (unformat (line_input, "tun"))
57             host_if_type = LCP_ITF_HOST_TUN;
58           else
59             {
60               error = clib_error_return (0, "unknown input `%U'",
61                                          format_unformat_error, line_input);
62               break;
63             }
64         }
65       unformat_free (line_input);
66     }
67
68   if (error)
69     ;
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);
77   else
78     {
79       int r;
80
81       r = lcp_itf_pair_create (sw_if_index, host_if_name, host_if_type, ns,
82                                NULL);
83       if (r)
84         error = clib_error_return (0, "linux-cp pair creation failed (%d)", r);
85     }
86
87   vec_free (host_if_name);
88   vec_free (ns);
89
90   return error;
91 }
92
93 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 lcp_sync_command_fn (vlib_main_t *vm, unformat_input_t *input,
102                      vlib_cli_command_t *cmd)
103 {
104   unformat_input_t _line_input, *line_input = &_line_input;
105
106   if (!unformat_user (input, unformat_line_input, line_input))
107     return 0;
108
109   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
110     {
111       if (unformat (line_input, "on") || unformat (line_input, "enable"))
112         lcp_set_sync (1);
113       else if (unformat (line_input, "off") ||
114                unformat (line_input, "disable"))
115         lcp_set_sync (0);
116       else
117         return clib_error_return (0, "unknown input `%U'",
118                                   format_unformat_error, line_input);
119     }
120
121   unformat_free (line_input);
122   return 0;
123 }
124
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,
129 };
130
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)
134 {
135   unformat_input_t _line_input, *line_input = &_line_input;
136
137   if (!unformat_user (input, unformat_line_input, line_input))
138     return 0;
139
140   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
141     {
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);
147       else
148         return clib_error_return (0, "unknown input `%U'",
149                                   format_unformat_error, line_input);
150     }
151
152   unformat_free (line_input);
153   return 0;
154 }
155
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,
160 };
161
162 static clib_error_t *
163 lcp_param_command_fn (vlib_main_t *vm, unformat_input_t *input,
164                       vlib_cli_command_t *cmd)
165 {
166   unformat_input_t _line_input, *line_input = &_line_input;
167
168   if (!unformat_user (input, unformat_line_input, line_input))
169     return 0;
170
171   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
172     {
173       if (unformat (line_input, "del-static-on-link-down"))
174         {
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 */);
180           else
181             return clib_error_return (0, "unknown input `%U'",
182                                       format_unformat_error, line_input);
183         }
184       else if (unformat (line_input, "del-dynamic-on-link-down"))
185         {
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 */);
191           else
192             return clib_error_return (0, "unknown input `%U'",
193                                       format_unformat_error, line_input);
194         }
195       else
196         return clib_error_return (0, "unknown input `%U'",
197                                   format_unformat_error, line_input);
198     }
199
200   unformat_free (line_input);
201   return 0;
202 }
203
204 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 lcp_default_netns_command_fn (vlib_main_t *vm, unformat_input_t *input,
213                               vlib_cli_command_t *cmd)
214 {
215   unformat_input_t _line_input, *line_input = &_line_input;
216   u8 *ns;
217   int r;
218
219   if (!unformat_user (input, unformat_line_input, line_input))
220     return 0;
221
222   ns = 0;
223
224   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
225     {
226       if (unformat (line_input, "netns %s", &ns))
227         ;
228       else if (unformat (line_input, "clear netns"))
229         ;
230     }
231
232   unformat_free (line_input);
233
234   vlib_cli_output (vm, "lcp set default netns '%s'\n", (char *) ns);
235
236   r = lcp_set_default_ns (ns);
237
238   if (r)
239     return clib_error_return (0, "linux-cp set default netns failed (%d)", r);
240
241   return 0;
242 }
243
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,
248 };
249
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)
253 {
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;
258
259   if (unformat_user (input, unformat_line_input, line_input))
260     {
261       while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
262         {
263           if (unformat (line_input, "%d", &sw_if_index))
264             ;
265           else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
266                              &sw_if_index))
267             ;
268           else
269             {
270               error = clib_error_return (0, "unknown input `%U'",
271                                          format_unformat_error, line_input);
272               break;
273             }
274         }
275       unformat_free (line_input);
276     }
277
278   if (error)
279     ;
280   else if (sw_if_index == ~0)
281     error = clib_error_return (0, "interface name or sw_if_index required");
282   else
283     {
284       int r;
285
286       r = lcp_itf_pair_delete (sw_if_index);
287       if (r)
288         error = clib_error_return (0, "linux-cp pair deletion failed (%d)", r);
289     }
290
291   return error;
292 }
293
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,
298 };
299
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)
303 {
304   vnet_main_t *vnm = vnet_get_main ();
305   u32 phy_sw_if_index;
306
307   phy_sw_if_index = ~0;
308
309   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
310     {
311       if (unformat (input, "phy %U", unformat_vnet_sw_interface, vnm,
312                     &phy_sw_if_index))
313         ;
314       else
315         return clib_error_return (0, "unknown input '%U'",
316                                   format_unformat_error, input);
317     }
318
319   lcp_itf_pair_show (phy_sw_if_index);
320
321   return 0;
322 }
323
324 VLIB_CLI_COMMAND (lcp_itf_pair_show_cmd_node, static) = {
325   .path = "show lcp",
326   .function = lcp_itf_pair_show_cmd,
327   .short_help = "show lcp [phy <interface>]",
328   .is_mp_safe = 1,
329 };
330
331 clib_error_t *
332 lcp_cli_init (vlib_main_t *vm)
333 {
334   return 0;
335 }
336
337 VLIB_INIT_FUNCTION (lcp_cli_init);
338
339 /*
340  * fd.io coding-style-patch-verification: ON
341  *
342  * Local Variables:
343  * eval: (c-set-style "gnu")
344  * End:
345  */