VPP-279: doxygen documentation for host interface CLI commands
[vpp.git] / src / vnet / devices / af_packet / cli.c
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
33 #include <vnet/devices/af_packet/af_packet.h>
34
35 /**
36  * @file
37  * @brief CLI for Host Interface Device Driver.
38  *
39  * This file contains the source code for CLI for the host interface.
40  */
41
42 static clib_error_t *
43 af_packet_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
44                              vlib_cli_command_t * cmd)
45 {
46   unformat_input_t _line_input, *line_input = &_line_input;
47   u8 *host_if_name = NULL;
48   u8 hwaddr[6];
49   u8 *hw_addr_ptr = 0;
50   u32 sw_if_index;
51   int r;
52
53   /* Get a line of input. */
54   if (!unformat_user (input, unformat_line_input, line_input))
55     return 0;
56
57   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
58     {
59       if (unformat (line_input, "name %s", &host_if_name))
60         ;
61       else
62         if (unformat
63             (line_input, "hw-addr %U", unformat_ethernet_address, hwaddr))
64         hw_addr_ptr = hwaddr;
65       else
66         return clib_error_return (0, "unknown input `%U'",
67                                   format_unformat_error, input);
68     }
69   unformat_free (line_input);
70
71   if (host_if_name == NULL)
72     return clib_error_return (0, "missing host interface name");
73
74   r = af_packet_create_if (vm, host_if_name, hw_addr_ptr, &sw_if_index);
75   vec_free (host_if_name);
76
77   if (r == VNET_API_ERROR_SYSCALL_ERROR_1)
78     return clib_error_return (0, "%s (errno %d)", strerror (errno), errno);
79
80   if (r == VNET_API_ERROR_INVALID_INTERFACE)
81     return clib_error_return (0, "Invalid interface name");
82
83   if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
84     return clib_error_return (0, "Interface elready exists");
85
86   vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
87                    sw_if_index);
88   return 0;
89 }
90
91 /*?
92  * Create a host interface that will attach to a linux AF_PACKET
93  * interface, one side of a veth pair. The veth pair must already
94  * exist. Once created, a new host interface will exist in VPP
95  * with the name '<em>host-<ifname></em>', where '<em><ifname></em>'
96  * is the name of the specified veth pair. Use the
97  * '<em>show interfaces</em>' command to display host interface details.
98  *
99  * @cliexpar
100  * Example of how to create a host interface tied to one side of an
101  * existing linux veth pair named vpp1:
102  * @cliexstart{create host-interface name vpp1}
103  * host-vpp1
104  * @cliexend
105  * Once the host interface is created, enable the interface using:
106  * @cliexcmd{set interface state host-vpp1 up}
107 ?*/
108 /* *INDENT-OFF* */
109 VLIB_CLI_COMMAND (af_packet_create_command, static) = {
110   .path = "create host-interface",
111   .short_help = "create host-interface name <ifname> [hw-addr <mac-addr>]",
112   .function = af_packet_create_command_fn,
113 };
114 /* *INDENT-ON* */
115
116 static clib_error_t *
117 af_packet_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
118                              vlib_cli_command_t * cmd)
119 {
120   unformat_input_t _line_input, *line_input = &_line_input;
121   u8 *host_if_name = NULL;
122
123   /* Get a line of input. */
124   if (!unformat_user (input, unformat_line_input, line_input))
125     return 0;
126
127   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
128     {
129       if (unformat (line_input, "name %s", &host_if_name))
130         ;
131       else
132         return clib_error_return (0, "unknown input `%U'",
133                                   format_unformat_error, input);
134     }
135   unformat_free (line_input);
136
137   if (host_if_name == NULL)
138     return clib_error_return (0, "missing host interface name");
139
140   af_packet_delete_if (vm, host_if_name);
141   vec_free (host_if_name);
142
143   return 0;
144 }
145
146 /*?
147  * Delete a host interface. Use the linux interface name to identify
148  * the host interface to be deleted. In VPP, host interfaces are
149  * named as '<em>host-<ifname></em>', where '<em><ifname></em>'
150  * is the name of the linux interface.
151  *
152  * @cliexpar
153  * Example of how to delete a host interface named host-vpp1:
154  * @cliexcmd{delete host-interface name vpp1}
155 ?*/
156 /* *INDENT-OFF* */
157 VLIB_CLI_COMMAND (af_packet_delete_command, static) = {
158   .path = "delete host-interface",
159   .short_help = "delete host-interface name <ifname>",
160   .function = af_packet_delete_command_fn,
161 };
162 /* *INDENT-ON* */
163
164 clib_error_t *
165 af_packet_cli_init (vlib_main_t * vm)
166 {
167   return 0;
168 }
169
170 VLIB_INIT_FUNCTION (af_packet_cli_init);
171
172 /*
173  * fd.io coding-style-patch-verification: ON
174  *
175  * Local Variables:
176  * eval: (c-set-style "gnu")
177  * End:
178  */