FD.io VPP  v17.01.1-3-gc6833f8
Vector Packet Processing
cli.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * af_packet.c - linux kernel packet interface
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19 
20 #include <fcntl.h> /* for open */
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <sys/uio.h> /* for iovec */
26 #include <netinet/in.h>
27 
28 #include <vlib/vlib.h>
29 #include <vlib/unix/unix.h>
30 #include <vnet/ip/ip.h>
31 #include <vnet/ethernet/ethernet.h>
32 
34 
35 static clib_error_t *
37  vlib_cli_command_t * cmd)
38 {
39  unformat_input_t _line_input, *line_input = &_line_input;
40  u8 *host_if_name = NULL;
41  u8 hwaddr[6];
42  u8 *hw_addr_ptr = 0;
43  u32 sw_if_index;
44  int r;
45 
46  /* Get a line of input. */
47  if (!unformat_user (input, unformat_line_input, line_input))
48  return 0;
49 
50  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
51  {
52  if (unformat (line_input, "name %s", &host_if_name))
53  ;
54  else
55  if (unformat
56  (line_input, "hw-addr %U", unformat_ethernet_address, hwaddr))
57  hw_addr_ptr = hwaddr;
58  else
59  return clib_error_return (0, "unknown input `%U'",
60  format_unformat_error, input);
61  }
62  unformat_free (line_input);
63 
64  if (host_if_name == NULL)
65  return clib_error_return (0, "missing host interface name");
66 
67  r = af_packet_create_if (vm, host_if_name, hw_addr_ptr, &sw_if_index);
68  vec_free (host_if_name);
69 
70  if (r == VNET_API_ERROR_SYSCALL_ERROR_1)
71  return clib_error_return (0, "%s (errno %d)", strerror (errno), errno);
72 
73  if (r == VNET_API_ERROR_INVALID_INTERFACE)
74  return clib_error_return (0, "Invalid interface name");
75 
76  if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
77  return clib_error_return (0, "Interface elready exists");
78 
80  sw_if_index);
81  return 0;
82 }
83 
84 /* *INDENT-OFF* */
85 VLIB_CLI_COMMAND (af_packet_create_command, static) = {
86  .path = "create host-interface",
87  .short_help = "create host-interface name <interface name> [hw-addr <mac>]",
88  .function = af_packet_create_command_fn,
89 };
90 /* *INDENT-ON* */
91 
92 static clib_error_t *
94  vlib_cli_command_t * cmd)
95 {
96  unformat_input_t _line_input, *line_input = &_line_input;
97  u8 *host_if_name = NULL;
98 
99  /* Get a line of input. */
100  if (!unformat_user (input, unformat_line_input, line_input))
101  return 0;
102 
103  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
104  {
105  if (unformat (line_input, "name %s", &host_if_name))
106  ;
107  else
108  return clib_error_return (0, "unknown input `%U'",
109  format_unformat_error, input);
110  }
111  unformat_free (line_input);
112 
113  if (host_if_name == NULL)
114  return clib_error_return (0, "missing host interface name");
115 
116  af_packet_delete_if (vm, host_if_name);
117  vec_free (host_if_name);
118 
119  return 0;
120 }
121 
122 /* *INDENT-OFF* */
123 VLIB_CLI_COMMAND (af_packet_delete_command, static) = {
124  .path = "delete host-interface",
125  .short_help = "delete host-interface name <interface name>",
126  .function = af_packet_delete_command_fn,
127 };
128 /* *INDENT-ON* */
129 
130 clib_error_t *
132 {
133  return 0;
134 }
135 
137 
138 /*
139  * fd.io coding-style-patch-verification: ON
140  *
141  * Local Variables:
142  * eval: (c-set-style "gnu")
143  * End:
144  */
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
static clib_error_t * af_packet_create_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:36
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
#define NULL
Definition: clib.h:55
format_function_t format_vnet_sw_if_index_name
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:977
clib_error_t * af_packet_cli_init(vlib_main_t *vm)
Definition: cli.c:131
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:576
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:245
unsigned int u32
Definition: types.h:88
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
static clib_error_t * af_packet_delete_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:93
unsigned char u8
Definition: types.h:56
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
#define clib_error_return(e, args...)
Definition: error.h:111
struct _unformat_input_t unformat_input_t
int af_packet_create_if(vlib_main_t *vm, u8 *host_if_name, u8 *hw_addr_set, u32 *sw_if_index)
Definition: af_packet.c:175
unformat_function_t unformat_line_input
Definition: format.h:281
int af_packet_delete_if(vlib_main_t *vm, u8 *host_if_name)
Definition: af_packet.c:286