summaryrefslogtreecommitdiffstats
path: root/src/plugins/linux-cp/lcp_cli.c
blob: 0dcf600b3015cb34e6ce10a1ce39e4c8a357f830 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* Hey Emacs use -*- mode: C -*- */
/*
 * Copyright 2020 Rubicon Communications, LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <sys/socket.h>
#include <linux/if.h>

#include <vnet/vnet.h>
#include <vnet/plugin/plugin.h>

#include <vlibapi/api.h>
#include <vlibmemory/api.h>
#include <vpp/app/version.h>
#include <vnet/format_fns.h>

#include <plugins/linux-cp/lcp_interface.h>

static clib_error_t *
lcp_itf_pair_create_command_fn (vlib_main_t *vm, unformat_input_t *input,
				vlib_cli_command_t *cmd)
{
  unformat_input_t _line_input, *line_input = &_line_input;
  vnet_main_t *vnm = vnet_get_main ();
  u32 sw_if_index = ~0;
  u8 *host_if_name = NULL;
  lip_host_type_t host_if_type = LCP_ITF_HOST_TAP;
  u8 *ns = NULL;
  clib_error_t *error = NULL;

  if (unformat_user (input, unformat_line_input, line_input))
    {
      while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
	{
	  if (unformat (line_input, "%d", &sw_if_index))
	    ;
	  else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
			     &sw_if_index))
	    ;
	  else if (unformat (line_input, "host-if %s", &host_if_name))
	    ;
	  else if (unformat (line_input, "netns %s", &ns))
	    ;
	  else if (unformat (line_input, "tun"))
	    host_if_type = LCP_ITF_HOST_TUN;
	  else
	    {
	      error = clib_error_return (0, "unknown input `%U'",
					 format_unformat_error, line_input);
	      break;
	    }
	}
      unformat_free (line_input);
    }

  if (error)
    ;
  else if (sw_if_index == ~0)
    error = clib_error_return (0, "interface name or sw_if_index required");
  else if (!host_if_name)
    error = clib_error_return (0, "host interface name required");
  else if (vec_len (ns) >= LCP_NS_LEN)
    error = clib_error_return (
      0, "Namespace name should be fewer than %d characters", LCP_NS_LEN);
  else
    {
      int r;

      r = lcp_itf_pair_create (sw_if_index, host_if_name, host_if_type, ns,
			       NULL);
      if (r)
	error = clib_error_return (0, "linux-cp pair creation failed (%d)", r);
    }

  vec_free (host_if_name);
  vec_free (ns);

  return error;
}

VLIB_CLI_COMMAND (lcp_itf_pair_create_command, static) = {
  .path = "lcp create",
  .short_help = "lcp create <sw_if_index>|<if-name> host-if <host-if-name> "
		"netns <namespace> [tun]",
  .function = lcp_itf_pair_create_command_fn,
};

static clib_error_t *
lcp_sync_command_fn (vlib_main_t *vm, unformat_input_t *input,
		     vlib_cli_command_t *cmd)
{
  unformat_input_t _line_input, *line_input = &_line_input;

  if (!unformat_user (input, unformat_line_input, line_input))
    return 0;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (line_input, "on") || unformat (line_input, "enable"))
	lcp_set_sync (1);
      else if (unformat (line_input, "off") ||
	       unformat (line_input, "disable"))
	lcp_set_sync (0);
      else
	return clib_error_return (0, "unknown input `%U'",
				  format_unformat_error, line_input);
    }

  unformat_free (line_input);
  return 0;
}

VLIB_CLI_COMMAND (lcp_sync_command, static) = {
  .path = "lcp lcp-sync",
  .short_help = "lcp lcp-sync [on|enable|off|disable]",
  .function = lcp_sync_command_fn,
};

static clib_error_t *
lcp_auto_subint_command_fn (vlib_main_t *vm, unformat_input_t *input,
			    vlib_cli_command_t *cmd)
{
  unformat_input_t _line_input, *line_input = &_line_input;

  if (!unformat_user (input, unformat_line_input, line_input))
    return 0;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (line_input, "on") || unformat (line_input, "enable"))
	lcp_set_auto_subint (1);
      else if (unformat (line_input, "off") ||
	       unformat (line_input, "disable"))
	lcp_set_auto_subint (0);
      else
	return clib_error_return (0, "unknown input `%U'",
				  format_unformat_error, line_input);
    }

  unformat_free (line_input);
  return 0;
}

VLIB_CLI_COMMAND (lcp_auto_subint_command, static) = {
  .path = "lcp lcp-auto-subint",
  .short_help = "lcp lcp-auto-subint [on|enable|off|disable]",
  .function = lcp_auto_subint_command_fn,
};

static clib_error_t *
lcp_param_command_fn (vlib_main_t *vm, unformat_input_t *input,
		      vlib_cli_command_t *cmd)
{
  unformat_input_t _line_input, *line_input = &_line_input;

  if (!unformat_user (input, unformat_line_input, line_input))
    return 0;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (line_input, "del-static-on-link-down"))
	{
	  if (unformat (line_input, "on") || unformat (line_input, "enable"))
	    lcp_set_del_static_on_link_down (1 /* is_del */);
	  else if (unformat (line_input, "off") ||
		   unformat (line_input, "disable"))
	    lcp_set_del_static_on_link_down (0 /* is_del */);
	  else
	    return clib_error_return (0, "unknown input `%U'",
				      format_unformat_error, line_input);
	}
      else if (unformat (line_input, "del-dynamic-on-link-down"))
	{
	  if (unformat (line_input, "on") || unformat (line_input, "enable"))
	    lcp_set_del_dynamic_on_link_down (1 /* is_del */);
	  else if (unformat (line_input, "off") ||
		   unformat (line_input, "disable"))
	    lcp_set_del_dynamic_on_link_down (0 /* is_del */);
	  else
	    return clib_error_return (0, "unknown input `%U'",
				      format_unformat_error, line_input);
	}
      else
	return clib_error_return (0, "unknown input `%U'",
				  format_unformat_error, line_input);
    }

  unformat_free (line_input);
  return 0;
}

VLIB_CLI_COMMAND (lcp_param_command, static) = {
  .path = "lcp param",
  .short_help = "lcp param [del-static-on-link-down (on|enable|off|disable)] "
		"[del-dynamic-on-link-down (on|enable|off|disable)]",
  .function = lcp_param_command_fn,
};

static clib_error_t *
lcp_default_netns_command_fn (vlib_main_t *vm, unformat_input_t *input,
			      vlib_cli_command_t *cmd)
{
  unformat_input_t _line_input, *line_input = &_line_input;
  u8 *ns;
  int r;
  clib_error_t *error = NULL;

  if (!unformat_user (input, unformat_line_input, line_input))
    return 0;

  ns = 0;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (line_input, "netns %s", &ns))
	;
      else if (unformat (line_input, "clear netns"))
	;
      else
	{
	  vec_free (ns);
	  error = clib_error_return (0, "unknown input `%U'",
				     format_unformat_error, line_input);
	  goto done;
	}
    }

  vlib_cli_output (vm, "lcp set default netns '%s'\n", (char *) ns);

  r = lcp_set_default_ns (ns);

  if (r)
    return clib_error_return (0, "linux-cp set default netns failed (%d)", r);

done:
  unformat_free (line_input);

  return error;
}

VLIB_CLI_COMMAND (lcp_default_netns_command, static) = {
  .path = "lcp default",
  .short_help = "lcp default netns [<namespace>]",
  .function = lcp_default_netns_command_fn,
};

static clib_error_t *
lcp_itf_pair_delete_command_fn (vlib_main_t *vm, unformat_input_t *input,
				vlib_cli_command_t *cmd)
{
  vnet_main_t *vnm = vnet_get_main ();
  unformat_input_t _line_input, *line_input = &_line_input;
  u32 sw_if_index = ~0;
  clib_error_t *error = NULL;

  if (unformat_user (input, unformat_line_input, line_input))
    {
      while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
	{
	  if (unformat (line_input, "%d", &sw_if_index))
	    ;
	  else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
			     &sw_if_index))
	    ;
	  else
	    {
	      error = clib_error_return (0, "unknown input `%U'",
					 format_unformat_error, line_input);
	      break;
	    }
	}
      unformat_free (line_input);
    }

  if (error)
    ;
  else if (sw_if_index == ~0)
    error = clib_error_return (0, "interface name or sw_if_index required");
  else
    {
      int r;

      r = lcp_itf_pair_delete (sw_if_index);
      if (r)
	error = clib_error_return (0, "linux-cp pair deletion failed (%d)", r);
    }

  return error;
}

VLIB_CLI_COMMAND (lcp_itf_pair_delete_command, static) = {
  .path = "lcp delete",
  .short_help = "lcp delete <sw_if_index>|<if-name>",
  .function = lcp_itf_pair_delete_command_fn,
};

static clib_error_t *
lcp_itf_pair_show_cmd (vlib_main_t *vm, unformat_input_t *input,
		       vlib_cli_command_t *cmd)
{
  vnet_main_t *vnm = vnet_get_main ();
  u32 phy_sw_if_index;

  phy_sw_if_index = ~0;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "phy %U", unformat_vnet_sw_interface, vnm,
		    &phy_sw_if_index))
	;
      else
	return clib_error_return (0, "unknown input '%U'",
				  format_unformat_error, input);
    }

  lcp_itf_pair_show (phy_sw_if_index);

  return 0;
}

VLIB_CLI_COMMAND (lcp_itf_pair_show_cmd_node, static) = {
  .path = "show lcp",
  .function = lcp_itf_pair_show_cmd,
  .short_help = "show lcp [phy <interface>]",
  .is_mp_safe = 1,
};

clib_error_t *
lcp_cli_init (vlib_main_t *vm)
{
  return 0;
}

VLIB_INIT_FUNCTION (lcp_cli_init);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */