dpdk-cryptodev: fix coverity issues
[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   clib_error_t *error = NULL;
219
220   if (!unformat_user (input, unformat_line_input, line_input))
221     return 0;
222
223   ns = 0;
224
225   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
226     {
227       if (unformat (line_input, "netns %s", &ns))
228         ;
229       else if (unformat (line_input, "clear netns"))
230         ;
231       else
232         {
233           vec_free (ns);
234           error = clib_error_return (0, "unknown input `%U'",
235                                      format_unformat_error, line_input);
236           goto done;
237         }
238     }
239
240   vlib_cli_output (vm, "lcp set default netns '%s'\n", (char *) ns);
241
242   r = lcp_set_default_ns (ns);
243
244   if (r)
245     return clib_error_return (0, "linux-cp set default netns failed (%d)", r);
246
247 done:
248   unformat_free (line_input);
249
250   return error;
251 }
252
253 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 lcp_itf_pair_delete_command_fn (vlib_main_t *vm, unformat_input_t *input,
261                                 vlib_cli_command_t *cmd)
262 {
263   vnet_main_t *vnm = vnet_get_main ();
264   unformat_input_t _line_input, *line_input = &_line_input;
265   u32 sw_if_index = ~0;
266   clib_error_t *error = NULL;
267
268   if (unformat_user (input, unformat_line_input, line_input))
269     {
270       while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
271         {
272           if (unformat (line_input, "%d", &sw_if_index))
273             ;
274           else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
275                              &sw_if_index))
276             ;
277           else
278             {
279               error = clib_error_return (0, "unknown input `%U'",
280                                          format_unformat_error, line_input);
281               break;
282             }
283         }
284       unformat_free (line_input);
285     }
286
287   if (error)
288     ;
289   else if (sw_if_index == ~0)
290     error = clib_error_return (0, "interface name or sw_if_index required");
291   else
292     {
293       int r;
294
295       r = lcp_itf_pair_delete (sw_if_index);
296       if (r)
297         error = clib_error_return (0, "linux-cp pair deletion failed (%d)", r);
298     }
299
300   return error;
301 }
302
303 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 lcp_itf_pair_show_cmd (vlib_main_t *vm, unformat_input_t *input,
311                        vlib_cli_command_t *cmd)
312 {
313   vnet_main_t *vnm = vnet_get_main ();
314   u32 phy_sw_if_index;
315
316   phy_sw_if_index = ~0;
317
318   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
319     {
320       if (unformat (input, "phy %U", unformat_vnet_sw_interface, vnm,
321                     &phy_sw_if_index))
322         ;
323       else
324         return clib_error_return (0, "unknown input '%U'",
325                                   format_unformat_error, input);
326     }
327
328   lcp_itf_pair_show (phy_sw_if_index);
329
330   return 0;
331 }
332
333 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 lcp_cli_init (vlib_main_t *vm)
342 {
343   return 0;
344 }
345
346 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  */