devices: tapv2 api cleanup
[vpp.git] / src / vnet / devices / tap / tapv2_api.c
1 /*
2  *------------------------------------------------------------------
3  * tap_api.c - vnet tap device driver API support
4  *
5  * Copyright (c) 2017 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/ethernet/ethernet_types_api.h>
28 #include <vnet/ip/ip_types_api.h>
29 #include <vnet/devices/tap/tap.h>
30
31 #include <vnet/format_fns.h>
32 #include <vnet/devices/tap/tapv2.api_enum.h>
33 #include <vnet/devices/tap/tapv2.api_types.h>
34
35 #define REPLY_MSG_ID_BASE msg_id_base
36 #include <vlibapi/api_helper_macros.h>
37
38 static u16 msg_id_base;
39
40 static void
41 vl_api_tap_create_v2_t_handler (vl_api_tap_create_v2_t * mp)
42 {
43   vl_api_registration_t *reg;
44   reg = vl_api_client_index_to_registration (mp->client_index);
45   if (!reg)
46     return;
47
48   vnet_main_t *vnm = vnet_get_main ();
49   vlib_main_t *vm = vlib_get_main ();
50   vl_api_tap_create_v2_reply_t *rmp;
51
52   tap_create_if_args_t _a, *ap = &_a;
53
54   clib_memset (ap, 0, sizeof (*ap));
55
56   ap->id = ntohl (mp->id);
57   if (!mp->use_random_mac)
58     {
59       mac_address_decode (mp->mac_address, &ap->mac_addr);
60       ap->mac_addr_set = 1;
61     }
62   ap->rx_ring_sz = ntohs (mp->rx_ring_sz);
63   ap->tx_ring_sz = ntohs (mp->tx_ring_sz);
64   ap->sw_if_index = (u32) ~ 0;
65   ap->num_rx_queues = 1;
66
67   if (mp->num_rx_queues > 1)
68     ap->num_rx_queues = mp->num_rx_queues;
69
70   if (mp->host_if_name_set)
71     ap->host_if_name = format (0, "%s%c", mp->host_if_name, 0);
72
73   if (mp->host_mac_addr_set)
74     {
75       mac_address_decode (mp->host_mac_addr, &ap->host_mac_addr);
76     }
77
78   if (mp->host_namespace_set)
79     ap->host_namespace = format (0, "%s%c", mp->host_namespace, 0);
80
81   if (mp->host_bridge_set)
82     ap->host_bridge = format (0, "%s%c", mp->host_bridge, 0);
83
84   if (mp->host_ip4_prefix_set)
85     {
86       ip4_address_decode (mp->host_ip4_prefix.address, &ap->host_ip4_addr);
87       ap->host_ip4_prefix_len = mp->host_ip4_prefix.len;
88     }
89
90   if (mp->host_ip6_prefix_set)
91     {
92       ip6_address_decode (mp->host_ip6_prefix.address, &ap->host_ip6_addr);
93       ap->host_ip6_prefix_len = mp->host_ip6_prefix.len;
94     }
95
96   if (mp->host_ip4_gw_set)
97     {
98       ip4_address_decode (mp->host_ip4_gw, &ap->host_ip4_gw);
99       ap->host_ip4_gw_set = 1;
100     }
101
102   if (mp->host_ip6_gw_set)
103     {
104       ip6_address_decode (mp->host_ip6_gw, &ap->host_ip6_gw);
105       ap->host_ip6_gw_set = 1;
106     }
107
108   if (mp->host_mtu_set)
109     {
110       ap->host_mtu_size = ntohl (mp->host_mtu_size);
111       ap->host_mtu_set = 1;
112     }
113
114   STATIC_ASSERT (((int) TAP_API_FLAG_GSO == (int) TAP_FLAG_GSO),
115                  "tap gso api flag mismatch");
116   STATIC_ASSERT (((int) TAP_API_FLAG_CSUM_OFFLOAD ==
117                   (int) TAP_FLAG_CSUM_OFFLOAD),
118                  "tap checksum offload api flag mismatch");
119   STATIC_ASSERT (((int) TAP_API_FLAG_PERSIST == (int) TAP_FLAG_PERSIST),
120                  "tap persist api flag mismatch");
121   STATIC_ASSERT (((int) TAP_API_FLAG_ATTACH == (int) TAP_FLAG_ATTACH),
122                  "tap attach api flag mismatch");
123   STATIC_ASSERT (((int) TAP_API_FLAG_TUN == (int) TAP_FLAG_TUN),
124                  "tap tun api flag mismatch");
125   STATIC_ASSERT (((int) TAP_API_FLAG_GRO_COALESCE ==
126                   (int) TAP_FLAG_GRO_COALESCE),
127                  "tap gro coalesce api flag mismatch");
128   STATIC_ASSERT (((int) TAP_API_FLAG_PACKED == (int) TAP_FLAG_PACKED),
129                  "tap packed api flag mismatch");
130   STATIC_ASSERT (((int) TAP_API_FLAG_IN_ORDER ==
131                   (int) TAP_FLAG_IN_ORDER), "tap in-order api flag mismatch");
132
133   ap->tap_flags = ntohl (mp->tap_flags);
134
135   tap_create_if (vm, ap);
136
137
138   /* If a tag was supplied... */
139   if (vl_api_string_len (&mp->tag))
140     {
141       u8 *tag = vl_api_from_api_to_new_vec (mp, &mp->tag);
142       vnet_set_sw_interface_tag (vnm, tag, ap->sw_if_index);
143     }
144
145   rmp = vl_msg_api_alloc (sizeof (*rmp));
146   rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_TAP_CREATE_V2_REPLY);
147   rmp->context = mp->context;
148   rmp->retval = ntohl (ap->rv);
149   rmp->sw_if_index = ntohl (ap->sw_if_index);
150
151   vl_api_send_msg (reg, (u8 *) rmp);
152
153   vec_free (ap->host_if_name);
154   vec_free (ap->host_namespace);
155   vec_free (ap->host_bridge);
156
157 }
158
159 static void
160 vl_api_tap_delete_v2_t_handler (vl_api_tap_delete_v2_t * mp)
161 {
162   vl_api_registration_t *reg;
163   reg = vl_api_client_index_to_registration (mp->client_index);
164   if (!reg)
165     return;
166
167   vnet_main_t *vnm = vnet_get_main ();
168   vlib_main_t *vm = vlib_get_main ();
169   int rv;
170   vl_api_tap_delete_v2_reply_t *rmp;
171
172   u32 sw_if_index = ntohl (mp->sw_if_index);
173
174   rv = tap_delete_if (vm, sw_if_index);
175
176   rmp = vl_msg_api_alloc (sizeof (*rmp));
177   rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_TAP_DELETE_V2_REPLY);
178   rmp->context = mp->context;
179   rmp->retval = ntohl (rv);
180
181   vl_api_send_msg (reg, (u8 *) rmp);
182
183   if (!rv)
184     vnet_clear_sw_interface_tag (vnm, sw_if_index);
185 }
186
187 static void
188 tap_send_sw_interface_details (vpe_api_main_t * am,
189                                vl_api_registration_t * reg,
190                                tap_interface_details_t * tap_if, u32 context)
191 {
192   vl_api_sw_interface_tap_v2_details_t *mp;
193   mp = vl_msg_api_alloc (sizeof (*mp));
194   clib_memset (mp, 0, sizeof (*mp));
195   mp->_vl_msg_id =
196     htons (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_TAP_V2_DETAILS);
197   mp->id = htonl (tap_if->id);
198   mp->sw_if_index = htonl (tap_if->sw_if_index);
199   mp->tap_flags = htonl (tap_if->tap_flags);
200   clib_memcpy (mp->dev_name, tap_if->dev_name,
201                MIN (ARRAY_LEN (mp->dev_name) - 1,
202                     strlen ((const char *) tap_if->dev_name)));
203   mp->rx_ring_sz = htons (tap_if->rx_ring_sz);
204   mp->tx_ring_sz = htons (tap_if->tx_ring_sz);
205   mac_address_encode (&tap_if->host_mac_addr, mp->host_mac_addr);
206   clib_memcpy (mp->host_if_name, tap_if->host_if_name,
207                MIN (ARRAY_LEN (mp->host_if_name) - 1,
208                     strlen ((const char *) tap_if->host_if_name)));
209   clib_memcpy (mp->host_namespace, tap_if->host_namespace,
210                MIN (ARRAY_LEN (mp->host_namespace) - 1,
211                     strlen ((const char *) tap_if->host_namespace)));
212   clib_memcpy (mp->host_bridge, tap_if->host_bridge,
213                MIN (ARRAY_LEN (mp->host_bridge) - 1,
214                     strlen ((const char *) tap_if->host_bridge)));
215   mp->host_mtu_size = htonl (tap_if->host_mtu_size);
216   mac_address_encode (&tap_if->host_mac_addr, mp->host_mac_addr);
217
218   if (tap_if->host_ip4_prefix_len)
219     ip4_address_encode (&tap_if->host_ip4_addr, mp->host_ip4_prefix.address);
220   mp->host_ip4_prefix.len = tap_if->host_ip4_prefix_len;
221   if (tap_if->host_ip6_prefix_len)
222     ip6_address_encode (&tap_if->host_ip6_addr, mp->host_ip6_prefix.address);
223   mp->host_ip6_prefix.len = tap_if->host_ip6_prefix_len;
224
225   mp->context = context;
226   vl_api_send_msg (reg, (u8 *) mp);
227 }
228
229 static void
230 vl_api_sw_interface_tap_v2_dump_t_handler (vl_api_sw_interface_tap_v2_dump_t *
231                                            mp)
232 {
233   int rv;
234   vpe_api_main_t *am = &vpe_api_main;
235   vl_api_registration_t *reg;
236   tap_interface_details_t *tapifs = NULL;
237   tap_interface_details_t *tap_if = NULL;
238   u32 filter_sw_if_index;
239
240   reg = vl_api_client_index_to_registration (mp->client_index);
241   if (!reg)
242     return;
243
244   filter_sw_if_index = htonl (mp->sw_if_index);
245   if (mp->sw_if_index != ~0)
246     VALIDATE_SW_IF_INDEX (mp);
247
248   rv = tap_dump_ifs (&tapifs);
249   if (rv)
250     return;
251
252   vec_foreach (tap_if, tapifs)
253   {
254     if ((filter_sw_if_index == ~0)
255         || (tap_if->sw_if_index == filter_sw_if_index))
256       tap_send_sw_interface_details (am, reg, tap_if, mp->context);
257   }
258   BAD_SW_IF_INDEX_LABEL;
259   vec_free (tapifs);
260 }
261
262 #include <vnet/devices/tap/tapv2.api.c>
263 static clib_error_t *
264 tapv2_api_hookup (vlib_main_t * vm)
265 {
266   /*
267    * Set up the (msg_name, crc, message-id) table
268    */
269   REPLY_MSG_ID_BASE = setup_message_id_table ();
270
271   return 0;
272 }
273
274 VLIB_API_INIT_FUNCTION (tapv2_api_hookup);
275
276 /*
277  * fd.io coding-style-patch-verification: ON
278  *
279  * Local Variables:
280  * eval: (c-set-style "gnu")
281  * End:
282  */