virtio: fast TAP interfaces with vhost-net backend
[vpp.git] / src / vnet / devices / virtio / 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
28 #include <vnet/vnet_msg_enum.h>
29
30 #define vl_typedefs             /* define message structures */
31 #include <vnet/vnet_all_api_h.h>
32 #undef vl_typedefs
33
34 #define vl_endianfun            /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
36 #undef vl_endianfun
37
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
40 #define vl_printfun
41 #include <vnet/vnet_all_api_h.h>
42 #undef vl_printfun
43
44 #include <vlibapi/api_helper_macros.h>
45 #include <vnet/devices/virtio/tap.h>
46
47 #define foreach_tapv2_api_msg                     \
48 _(TAP_CREATE_V2, tap_create_v2)                   \
49 _(TAP_DELETE_V2, tap_delete_v2)                   \
50 _(SW_INTERFACE_TAP_V2_DUMP, sw_interface_tap_v2_dump)
51
52 static void
53 vl_api_tap_create_v2_t_handler (vl_api_tap_create_v2_t * mp)
54 {
55   vlib_main_t *vm = vlib_get_main ();
56   int rv;
57   vl_api_tap_create_v2_reply_t *rmp;
58   unix_shared_memory_queue_t *q;
59   tap_create_if_args_t _a, *ap = &_a;
60
61   memset (ap, 0, sizeof (*ap));
62
63   ap->name = mp->tap_name;
64   if (!mp->use_random_mac)
65     {
66       clib_memcpy (ap->hw_addr, mp->mac_address, 6);
67       ap->hw_addr_set = 1;
68     }
69   ap->rx_ring_sz = mp->rx_ring_sz;
70   ap->tx_ring_sz = mp->tx_ring_sz;
71   ap->sw_if_index = (u32) ~ 0;
72   if (mp->net_ns_set)
73     ap->net_ns = mp->net_ns;
74
75   rv = tap_create_if (vm, ap);
76
77   q = vl_api_client_index_to_input_queue (mp->client_index);
78   if (!q)
79     return;
80
81   rmp = vl_msg_api_alloc (sizeof (*rmp));
82   rmp->_vl_msg_id = ntohs (VL_API_TAP_CREATE_V2_REPLY);
83   rmp->context = mp->context;
84   rmp->retval = ntohl (rv);
85   rmp->sw_if_index = ntohl (ap->sw_if_index);
86
87   vl_msg_api_send_shmem (q, (u8 *) & rmp);
88 }
89
90 static void
91 tap_send_sw_interface_event_deleted (vpe_api_main_t * am,
92                                      unix_shared_memory_queue_t * q,
93                                      u32 sw_if_index)
94 {
95   vl_api_sw_interface_event_t *mp;
96
97   mp = vl_msg_api_alloc (sizeof (*mp));
98   memset (mp, 0, sizeof (*mp));
99   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
100   mp->sw_if_index = ntohl (sw_if_index);
101
102   mp->admin_up_down = 0;
103   mp->link_up_down = 0;
104   mp->deleted = 1;
105   vl_msg_api_send_shmem (q, (u8 *) & mp);
106 }
107
108 static void
109 vl_api_tap_delete_v2_t_handler (vl_api_tap_delete_v2_t * mp)
110 {
111   vlib_main_t *vm = vlib_get_main ();
112   int rv;
113   vpe_api_main_t *vam = &vpe_api_main;
114   vl_api_tap_delete_v2_reply_t *rmp;
115   unix_shared_memory_queue_t *q;
116   u32 sw_if_index = ntohl (mp->sw_if_index);
117
118   rv = tap_delete_if (vm, sw_if_index);
119
120   q = vl_api_client_index_to_input_queue (mp->client_index);
121   if (!q)
122     return;
123
124   rmp = vl_msg_api_alloc (sizeof (*rmp));
125   rmp->_vl_msg_id = ntohs (VL_API_TAP_DELETE_V2_REPLY);
126   rmp->context = mp->context;
127   rmp->retval = ntohl (rv);
128
129   vl_msg_api_send_shmem (q, (u8 *) & rmp);
130
131   if (!rv)
132     tap_send_sw_interface_event_deleted (vam, q, sw_if_index);
133 }
134
135 static void
136 tap_send_sw_interface_details (vpe_api_main_t * am,
137                                unix_shared_memory_queue_t * q,
138                                tap_interface_details_t * tap_if, u32 context)
139 {
140   vl_api_sw_interface_tap_v2_details_t *mp;
141   mp = vl_msg_api_alloc (sizeof (*mp));
142   memset (mp, 0, sizeof (*mp));
143   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_TAP_V2_DETAILS);
144   mp->sw_if_index = ntohl (tap_if->sw_if_index);
145   clib_memcpy (mp->dev_name, tap_if->dev_name,
146                MIN (ARRAY_LEN (mp->dev_name) - 1,
147                     strlen ((const char *) tap_if->dev_name)));
148   mp->context = context;
149
150   vl_msg_api_send_shmem (q, (u8 *) & mp);
151 }
152
153 static void
154 vl_api_sw_interface_tap_v2_dump_t_handler (vl_api_sw_interface_tap_v2_dump_t *
155                                            mp)
156 {
157   int rv;
158   vpe_api_main_t *am = &vpe_api_main;
159   unix_shared_memory_queue_t *q;
160   tap_interface_details_t *tapifs = NULL;
161   tap_interface_details_t *tap_if = NULL;
162
163   q = vl_api_client_index_to_input_queue (mp->client_index);
164   if (q == 0)
165     return;
166
167   rv = tap_dump_ifs (&tapifs);
168   if (rv)
169     return;
170
171   vec_foreach (tap_if, tapifs)
172   {
173     tap_send_sw_interface_details (am, q, tap_if, mp->context);
174   }
175
176   vec_free (tapifs);
177 }
178
179 #define vl_msg_name_crc_list
180 #include <vnet/vnet_all_api_h.h>
181 #undef vl_msg_name_crc_list
182
183 static void
184 tap_setup_message_id_table (api_main_t * am)
185 {
186 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
187   foreach_vl_msg_name_crc_tapv2;
188 #undef _
189 }
190
191 static clib_error_t *
192 tapv2_api_hookup (vlib_main_t * vm)
193 {
194   api_main_t *am = &api_main;
195
196 #define _(N,n)                                                  \
197     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
198                            vl_api_##n##_t_handler,              \
199                            vl_noop_handler,                     \
200                            vl_api_##n##_t_endian,               \
201                            vl_api_##n##_t_print,                \
202                            sizeof(vl_api_##n##_t), 1);
203   foreach_tapv2_api_msg;
204 #undef _
205
206   /*
207    * Set up the (msg_name, crc, message-id) table
208    */
209   tap_setup_message_id_table (am);
210
211   return 0;
212 }
213
214 VLIB_API_INIT_FUNCTION (tapv2_api_hookup);
215
216 /*
217  * fd.io coding-style-patch-verification: ON
218  *
219  * Local Variables:
220  * eval: (c-set-style "gnu")
221  * End:
222  */