blob: 8437c6a270b1905ff9cf94b97679bcea077865d4 [file] [log] [blame]
Dave Barach496e1272016-12-07 09:15:58 -05001/*
2 *------------------------------------------------------------------
3 * tap_api.c - vnet tap device driver API support
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 <vnet/vnet.h>
21#include <vlibmemory/api.h>
22
23#include <vnet/interface.h>
24#include <vnet/api_errno.h>
25#include <vnet/ethernet/ethernet.h>
26#include <vnet/ip/ip.h>
27#include <vnet/unix/tuntap.h>
28#include <vnet/unix/tapcli.h>
29
30#include <vnet/vnet_msg_enum.h>
31
32#define vl_typedefs /* define message structures */
33#include <vnet/vnet_all_api_h.h>
34#undef vl_typedefs
35
36#define vl_endianfun /* define message structures */
37#include <vnet/vnet_all_api_h.h>
38#undef vl_endianfun
39
40/* instantiate all the print functions we know about */
41#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42#define vl_printfun
43#include <vnet/vnet_all_api_h.h>
44#undef vl_printfun
45
46#include <vlibapi/api_helper_macros.h>
47
48#define foreach_tap_api_msg \
49_(TAP_CONNECT, tap_connect) \
50_(TAP_MODIFY, tap_modify) \
51_(TAP_DELETE, tap_delete) \
52_(SW_INTERFACE_TAP_DUMP, sw_interface_tap_dump)
53
54#define vl_msg_name_crc_list
55#include <vnet/unix/tap.api.h>
56#undef vl_msg_name_crc_list
57
58/*
59 * WARNING: replicated pending api refactor completion
60 */
61static void
Neale Rannsa07bd702017-08-07 07:53:49 -070062send_sw_interface_event_deleted (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -080063 vl_api_registration_t * reg, u32 sw_if_index)
Dave Barach496e1272016-12-07 09:15:58 -050064{
Neale Rannsa07bd702017-08-07 07:53:49 -070065 vl_api_sw_interface_event_t *mp;
Dave Barach496e1272016-12-07 09:15:58 -050066
67 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -040068 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns07145412017-08-18 02:34:28 -070069 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
Dave Barach496e1272016-12-07 09:15:58 -050070 mp->sw_if_index = ntohl (sw_if_index);
71
72 mp->admin_up_down = 0;
73 mp->link_up_down = 0;
74 mp->deleted = 1;
Florin Coras6c4dae22018-01-09 06:39:23 -080075 vl_api_send_msg (reg, (u8 *) mp);
Dave Barach496e1272016-12-07 09:15:58 -050076}
77
78static void
79vl_api_tap_connect_t_handler (vl_api_tap_connect_t * mp)
80{
81 vlib_main_t *vm = vlib_get_main ();
82 int rv;
83 vl_api_tap_connect_reply_t *rmp;
84 vnet_main_t *vnm = vnet_get_main ();
Florin Coras6c4dae22018-01-09 06:39:23 -080085 vl_api_registration_t *reg;
Dave Barach496e1272016-12-07 09:15:58 -050086 u32 sw_if_index = (u32) ~ 0;
87 u8 *tag;
Dave Barach2feaffc2017-01-14 10:30:50 -050088 vnet_tap_connect_args_t _a, *ap = &_a;
Dave Barach496e1272016-12-07 09:15:58 -050089
Dave Barachb7b92992018-10-17 10:38:51 -040090 clib_memset (ap, 0, sizeof (*ap));
Dave Barach2feaffc2017-01-14 10:30:50 -050091
92 ap->intfc_name = mp->tap_name;
93 if (!mp->use_random_mac)
94 ap->hwaddr_arg = mp->mac_address;
95 ap->renumber = mp->renumber;
96 ap->sw_if_indexp = &sw_if_index;
97 ap->custom_dev_instance = ntohl (mp->custom_dev_instance);
98 if (mp->ip4_address_set)
99 {
100 ap->ip4_address = (ip4_address_t *) mp->ip4_address;
101 ap->ip4_mask_width = mp->ip4_mask_width;
102 ap->ip4_address_set = 1;
103 }
104 if (mp->ip6_address_set)
105 {
106 ap->ip6_address = (ip6_address_t *) mp->ip6_address;
107 ap->ip6_mask_width = mp->ip6_mask_width;
108 ap->ip6_address_set = 1;
109 }
110
111 rv = vnet_tap_connect_renumber (vm, ap);
Dave Barach496e1272016-12-07 09:15:58 -0500112
113 /* Add tag if supplied */
114 if (rv == 0 && mp->tag[0])
115 {
116 mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
117 tag = format (0, "%s%c", mp->tag, 0);
118 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
119 }
120
Florin Coras6c4dae22018-01-09 06:39:23 -0800121 reg = vl_api_client_index_to_registration (mp->client_index);
122 if (!reg)
Dave Barach496e1272016-12-07 09:15:58 -0500123 return;
124
125 rmp = vl_msg_api_alloc (sizeof (*rmp));
126 rmp->_vl_msg_id = ntohs (VL_API_TAP_CONNECT_REPLY);
127 rmp->context = mp->context;
128 rmp->retval = ntohl (rv);
129 rmp->sw_if_index = ntohl (sw_if_index);
130
Florin Coras6c4dae22018-01-09 06:39:23 -0800131 vl_api_send_msg (reg, (u8 *) rmp);
Dave Barach496e1272016-12-07 09:15:58 -0500132}
133
134static void
135vl_api_tap_modify_t_handler (vl_api_tap_modify_t * mp)
136{
137 int rv;
138 vl_api_tap_modify_reply_t *rmp;
Florin Coras6c4dae22018-01-09 06:39:23 -0800139 vl_api_registration_t *reg;
Dave Barach496e1272016-12-07 09:15:58 -0500140 u32 sw_if_index = (u32) ~ 0;
141 vlib_main_t *vm = vlib_get_main ();
Dave Barach2feaffc2017-01-14 10:30:50 -0500142 vnet_tap_connect_args_t _a, *ap = &_a;
Dave Barach496e1272016-12-07 09:15:58 -0500143
Dave Barachb7b92992018-10-17 10:38:51 -0400144 clib_memset (ap, 0, sizeof (*ap));
Dave Barach2feaffc2017-01-14 10:30:50 -0500145
146 ap->orig_sw_if_index = ntohl (mp->sw_if_index);
147 ap->intfc_name = mp->tap_name;
148 if (!mp->use_random_mac)
149 ap->hwaddr_arg = mp->mac_address;
150 ap->sw_if_indexp = &sw_if_index;
151 ap->renumber = mp->renumber;
152 ap->custom_dev_instance = ntohl (mp->custom_dev_instance);
153
154 rv = vnet_tap_modify (vm, ap);
Dave Barach496e1272016-12-07 09:15:58 -0500155
Florin Coras6c4dae22018-01-09 06:39:23 -0800156 reg = vl_api_client_index_to_registration (mp->client_index);
157 if (!reg)
Dave Barach496e1272016-12-07 09:15:58 -0500158 return;
159
160 rmp = vl_msg_api_alloc (sizeof (*rmp));
161 rmp->_vl_msg_id = ntohs (VL_API_TAP_MODIFY_REPLY);
162 rmp->context = mp->context;
163 rmp->retval = ntohl (rv);
164 rmp->sw_if_index = ntohl (sw_if_index);
165
Florin Coras6c4dae22018-01-09 06:39:23 -0800166 vl_api_send_msg (reg, (u8 *) rmp);
Dave Barach496e1272016-12-07 09:15:58 -0500167}
168
169static void
170vl_api_tap_delete_t_handler (vl_api_tap_delete_t * mp)
171{
172 vlib_main_t *vm = vlib_get_main ();
173 int rv;
174 vpe_api_main_t *vam = &vpe_api_main;
175 vl_api_tap_delete_reply_t *rmp;
Florin Coras6c4dae22018-01-09 06:39:23 -0800176 vl_api_registration_t *reg;
Dave Barach496e1272016-12-07 09:15:58 -0500177 u32 sw_if_index = ntohl (mp->sw_if_index);
178
179 rv = vnet_tap_delete (vm, sw_if_index);
180 if (!rv)
181 {
182 vnet_main_t *vnm = vnet_get_main ();
183 vnet_clear_sw_interface_tag (vnm, sw_if_index);
184 }
185
Florin Coras6c4dae22018-01-09 06:39:23 -0800186 reg = vl_api_client_index_to_registration (mp->client_index);
187 if (!reg)
Dave Barach496e1272016-12-07 09:15:58 -0500188 return;
189
190 rmp = vl_msg_api_alloc (sizeof (*rmp));
191 rmp->_vl_msg_id = ntohs (VL_API_TAP_DELETE_REPLY);
192 rmp->context = mp->context;
193 rmp->retval = ntohl (rv);
194
Florin Coras6c4dae22018-01-09 06:39:23 -0800195 vl_api_send_msg (reg, (u8 *) rmp);
Dave Barach496e1272016-12-07 09:15:58 -0500196
197 if (!rv)
Florin Coras6c4dae22018-01-09 06:39:23 -0800198 send_sw_interface_event_deleted (vam, reg, sw_if_index);
Dave Barach496e1272016-12-07 09:15:58 -0500199}
200
201static void
202send_sw_interface_tap_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800203 vl_api_registration_t * reg,
Dave Barach496e1272016-12-07 09:15:58 -0500204 tapcli_interface_details_t * tap_if,
205 u32 context)
206{
207 vl_api_sw_interface_tap_details_t *mp;
208 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400209 clib_memset (mp, 0, sizeof (*mp));
Dave Barach496e1272016-12-07 09:15:58 -0500210 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_TAP_DETAILS);
211 mp->sw_if_index = ntohl (tap_if->sw_if_index);
212 strncpy ((char *) mp->dev_name,
213 (char *) tap_if->dev_name, ARRAY_LEN (mp->dev_name) - 1);
214 mp->context = context;
215
Florin Coras6c4dae22018-01-09 06:39:23 -0800216 vl_api_send_msg (reg, (u8 *) mp);
Dave Barach496e1272016-12-07 09:15:58 -0500217}
218
219static void
220vl_api_sw_interface_tap_dump_t_handler (vl_api_sw_interface_tap_dump_t * mp)
221{
222 int rv = 0;
223 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800224 vl_api_registration_t *reg;
Dave Barach496e1272016-12-07 09:15:58 -0500225 tapcli_interface_details_t *tapifs = NULL;
226 tapcli_interface_details_t *tap_if = NULL;
227
Florin Coras6c4dae22018-01-09 06:39:23 -0800228 reg = vl_api_client_index_to_registration (mp->client_index);
229 if (!reg)
Dave Barach496e1272016-12-07 09:15:58 -0500230 return;
231
232 rv = vnet_tap_dump_ifs (&tapifs);
233 if (rv)
234 return;
235
236 vec_foreach (tap_if, tapifs)
237 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800238 send_sw_interface_tap_details (am, reg, tap_if, mp->context);
Dave Barach496e1272016-12-07 09:15:58 -0500239 }
240
241 vec_free (tapifs);
242}
243
244static void
245setup_message_id_table (api_main_t * am)
246{
247#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
248 foreach_vl_msg_name_crc_tap;
249#undef _
250}
251
252static clib_error_t *
253tap_api_hookup (vlib_main_t * vm)
254{
255 api_main_t *am = &api_main;
256
257#define _(N,n) \
258 vl_msg_api_set_handlers(VL_API_##N, #n, \
259 vl_api_##n##_t_handler, \
260 vl_noop_handler, \
261 vl_api_##n##_t_endian, \
262 vl_api_##n##_t_print, \
263 sizeof(vl_api_##n##_t), 1);
264 foreach_tap_api_msg;
265#undef _
266
267 /*
268 * Set up the (msg_name, crc, message-id) table
269 */
270 setup_message_id_table (am);
271
272 return 0;
273}
274
275VLIB_API_INIT_FUNCTION (tap_api_hookup);
276
277/*
278 * fd.io coding-style-patch-verification: ON
279 *
280 * Local Variables:
281 * eval: (c-set-style "gnu")
282 * End:
283 */