FD.io VPP  v16.06
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  int r;
44 
45  /* Get a line of input. */
46  if (! unformat_user (input, unformat_line_input, line_input))
47  return 0;
48 
49  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
50  {
51  if (unformat (line_input, "name %s", &host_if_name))
52  ;
53  else if (unformat (line_input, "hw-addr %U", unformat_ethernet_address, hwaddr))
54  hw_addr_ptr = hwaddr;
55  else
56  return clib_error_return (0, "unknown input `%U'", format_unformat_error, input);
57  }
58  unformat_free (line_input);
59 
60  if (host_if_name == NULL)
61  return clib_error_return (0, "missing host interface name");
62 
63  r = af_packet_create_if(vm, host_if_name, hw_addr_ptr);
64 
65  if (r == VNET_API_ERROR_SYSCALL_ERROR_1)
66  return clib_error_return(0, "%s (errno %d)", strerror (errno), errno);
67 
68  if (r == VNET_API_ERROR_INVALID_INTERFACE)
69  return clib_error_return(0, "Invalid interface name");
70 
71  if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
72  return clib_error_return(0, "Interface elready exists");
73 
74  return 0;
75 }
76 
77 VLIB_CLI_COMMAND (af_packet_create_command, static) = {
78  .path = "create host-interface",
79  .short_help = "create host-interface name <interface name> [hw-addr <mac>]",
80  .function = af_packet_create_command_fn,
81 };
82 
83 static clib_error_t *
85  vlib_cli_command_t * cmd)
86 {
87  unformat_input_t _line_input, * line_input = &_line_input;
88  u8 * host_if_name = NULL;
89 
90  /* Get a line of input. */
91  if (! unformat_user (input, unformat_line_input, line_input))
92  return 0;
93 
94  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
95  {
96  if (unformat (line_input, "name %s", &host_if_name))
97  ;
98  else
99  return clib_error_return (0, "unknown input `%U'", format_unformat_error, input);
100  }
101  unformat_free (line_input);
102 
103  if (host_if_name == NULL)
104  return clib_error_return (0, "missing host interface name");
105 
106  af_packet_delete_if(vm, host_if_name);
107 
108  return 0;
109 }
110 
111 VLIB_CLI_COMMAND (af_packet_delete_command, static) = {
112  .path = "delete host-interface",
113  .short_help = "delete host-interface name <interface name>",
114  .function = af_packet_delete_command_fn,
115 };
116 
117 clib_error_t *
119 {
120  return 0;
121 }
122 
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
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
int af_packet_create_if(vlib_main_t *vm, u8 *host_if_name, u8 *hw_addr_set)
Definition: af_packet.c:165
always_inline void unformat_free(unformat_input_t *i)
Definition: format.h:160
#define UNFORMAT_END_OF_INPUT
Definition: format.h:142
#define NULL
Definition: clib.h:55
always_inline uword unformat_check_input(unformat_input_t *i)
Definition: format.h:168
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:953
clib_error_t * af_packet_cli_init(vlib_main_t *vm)
Definition: cli.c:118
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:150
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:187
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:87
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:84
unsigned char u8
Definition: types.h:56
#define clib_error_return(e, args...)
Definition: error.h:112
struct _unformat_input_t unformat_input_t
unformat_function_t unformat_line_input
Definition: format.h:279
int af_packet_delete_if(vlib_main_t *vm, u8 *host_if_name)
Definition: af_packet.c:272