Dave Barach | 496e127 | 2016-12-07 09:15:58 -0500 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 61 | static void |
Neale Ranns | a07bd70 | 2017-08-07 07:53:49 -0700 | [diff] [blame] | 62 | send_sw_interface_event_deleted (vpe_api_main_t * am, |
Dave Barach | 496e127 | 2016-12-07 09:15:58 -0500 | [diff] [blame] | 63 | unix_shared_memory_queue_t * q, |
| 64 | u32 sw_if_index) |
| 65 | { |
Neale Ranns | a07bd70 | 2017-08-07 07:53:49 -0700 | [diff] [blame] | 66 | vl_api_sw_interface_event_t *mp; |
Dave Barach | 496e127 | 2016-12-07 09:15:58 -0500 | [diff] [blame] | 67 | |
| 68 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 69 | memset (mp, 0, sizeof (*mp)); |
Neale Ranns | 0714541 | 2017-08-18 02:34:28 -0700 | [diff] [blame] | 70 | mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT); |
Dave Barach | 496e127 | 2016-12-07 09:15:58 -0500 | [diff] [blame] | 71 | mp->sw_if_index = ntohl (sw_if_index); |
| 72 | |
| 73 | mp->admin_up_down = 0; |
| 74 | mp->link_up_down = 0; |
| 75 | mp->deleted = 1; |
| 76 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 77 | } |
| 78 | |
| 79 | static void |
| 80 | vl_api_tap_connect_t_handler (vl_api_tap_connect_t * mp) |
| 81 | { |
| 82 | vlib_main_t *vm = vlib_get_main (); |
| 83 | int rv; |
| 84 | vl_api_tap_connect_reply_t *rmp; |
| 85 | vnet_main_t *vnm = vnet_get_main (); |
| 86 | unix_shared_memory_queue_t *q; |
| 87 | u32 sw_if_index = (u32) ~ 0; |
| 88 | u8 *tag; |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 89 | vnet_tap_connect_args_t _a, *ap = &_a; |
Dave Barach | 496e127 | 2016-12-07 09:15:58 -0500 | [diff] [blame] | 90 | |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 91 | memset (ap, 0, sizeof (*ap)); |
| 92 | |
| 93 | ap->intfc_name = mp->tap_name; |
| 94 | if (!mp->use_random_mac) |
| 95 | ap->hwaddr_arg = mp->mac_address; |
| 96 | ap->renumber = mp->renumber; |
| 97 | ap->sw_if_indexp = &sw_if_index; |
| 98 | ap->custom_dev_instance = ntohl (mp->custom_dev_instance); |
| 99 | if (mp->ip4_address_set) |
| 100 | { |
| 101 | ap->ip4_address = (ip4_address_t *) mp->ip4_address; |
| 102 | ap->ip4_mask_width = mp->ip4_mask_width; |
| 103 | ap->ip4_address_set = 1; |
| 104 | } |
| 105 | if (mp->ip6_address_set) |
| 106 | { |
| 107 | ap->ip6_address = (ip6_address_t *) mp->ip6_address; |
| 108 | ap->ip6_mask_width = mp->ip6_mask_width; |
| 109 | ap->ip6_address_set = 1; |
| 110 | } |
| 111 | |
| 112 | rv = vnet_tap_connect_renumber (vm, ap); |
Dave Barach | 496e127 | 2016-12-07 09:15:58 -0500 | [diff] [blame] | 113 | |
| 114 | /* Add tag if supplied */ |
| 115 | if (rv == 0 && mp->tag[0]) |
| 116 | { |
| 117 | mp->tag[ARRAY_LEN (mp->tag) - 1] = 0; |
| 118 | tag = format (0, "%s%c", mp->tag, 0); |
| 119 | vnet_set_sw_interface_tag (vnm, tag, sw_if_index); |
| 120 | } |
| 121 | |
| 122 | q = vl_api_client_index_to_input_queue (mp->client_index); |
| 123 | if (!q) |
| 124 | return; |
| 125 | |
| 126 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 127 | rmp->_vl_msg_id = ntohs (VL_API_TAP_CONNECT_REPLY); |
| 128 | rmp->context = mp->context; |
| 129 | rmp->retval = ntohl (rv); |
| 130 | rmp->sw_if_index = ntohl (sw_if_index); |
| 131 | |
| 132 | vl_msg_api_send_shmem (q, (u8 *) & rmp); |
| 133 | } |
| 134 | |
| 135 | static void |
| 136 | vl_api_tap_modify_t_handler (vl_api_tap_modify_t * mp) |
| 137 | { |
| 138 | int rv; |
| 139 | vl_api_tap_modify_reply_t *rmp; |
| 140 | unix_shared_memory_queue_t *q; |
| 141 | u32 sw_if_index = (u32) ~ 0; |
| 142 | vlib_main_t *vm = vlib_get_main (); |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 143 | vnet_tap_connect_args_t _a, *ap = &_a; |
Dave Barach | 496e127 | 2016-12-07 09:15:58 -0500 | [diff] [blame] | 144 | |
Dave Barach | 2feaffc | 2017-01-14 10:30:50 -0500 | [diff] [blame] | 145 | memset (ap, 0, sizeof (*ap)); |
| 146 | |
| 147 | ap->orig_sw_if_index = ntohl (mp->sw_if_index); |
| 148 | ap->intfc_name = mp->tap_name; |
| 149 | if (!mp->use_random_mac) |
| 150 | ap->hwaddr_arg = mp->mac_address; |
| 151 | ap->sw_if_indexp = &sw_if_index; |
| 152 | ap->renumber = mp->renumber; |
| 153 | ap->custom_dev_instance = ntohl (mp->custom_dev_instance); |
| 154 | |
| 155 | rv = vnet_tap_modify (vm, ap); |
Dave Barach | 496e127 | 2016-12-07 09:15:58 -0500 | [diff] [blame] | 156 | |
| 157 | q = vl_api_client_index_to_input_queue (mp->client_index); |
| 158 | if (!q) |
| 159 | return; |
| 160 | |
| 161 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 162 | rmp->_vl_msg_id = ntohs (VL_API_TAP_MODIFY_REPLY); |
| 163 | rmp->context = mp->context; |
| 164 | rmp->retval = ntohl (rv); |
| 165 | rmp->sw_if_index = ntohl (sw_if_index); |
| 166 | |
| 167 | vl_msg_api_send_shmem (q, (u8 *) & rmp); |
| 168 | } |
| 169 | |
| 170 | static void |
| 171 | vl_api_tap_delete_t_handler (vl_api_tap_delete_t * mp) |
| 172 | { |
| 173 | vlib_main_t *vm = vlib_get_main (); |
| 174 | int rv; |
| 175 | vpe_api_main_t *vam = &vpe_api_main; |
| 176 | vl_api_tap_delete_reply_t *rmp; |
| 177 | unix_shared_memory_queue_t *q; |
| 178 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 179 | |
| 180 | rv = vnet_tap_delete (vm, sw_if_index); |
| 181 | if (!rv) |
| 182 | { |
| 183 | vnet_main_t *vnm = vnet_get_main (); |
| 184 | vnet_clear_sw_interface_tag (vnm, sw_if_index); |
| 185 | } |
| 186 | |
| 187 | q = vl_api_client_index_to_input_queue (mp->client_index); |
| 188 | if (!q) |
| 189 | return; |
| 190 | |
| 191 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 192 | rmp->_vl_msg_id = ntohs (VL_API_TAP_DELETE_REPLY); |
| 193 | rmp->context = mp->context; |
| 194 | rmp->retval = ntohl (rv); |
| 195 | |
| 196 | vl_msg_api_send_shmem (q, (u8 *) & rmp); |
| 197 | |
| 198 | if (!rv) |
Neale Ranns | a07bd70 | 2017-08-07 07:53:49 -0700 | [diff] [blame] | 199 | send_sw_interface_event_deleted (vam, q, sw_if_index); |
Dave Barach | 496e127 | 2016-12-07 09:15:58 -0500 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static void |
| 203 | send_sw_interface_tap_details (vpe_api_main_t * am, |
| 204 | unix_shared_memory_queue_t * q, |
| 205 | tapcli_interface_details_t * tap_if, |
| 206 | u32 context) |
| 207 | { |
| 208 | vl_api_sw_interface_tap_details_t *mp; |
| 209 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 210 | memset (mp, 0, sizeof (*mp)); |
| 211 | mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_TAP_DETAILS); |
| 212 | mp->sw_if_index = ntohl (tap_if->sw_if_index); |
| 213 | strncpy ((char *) mp->dev_name, |
| 214 | (char *) tap_if->dev_name, ARRAY_LEN (mp->dev_name) - 1); |
| 215 | mp->context = context; |
| 216 | |
| 217 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 218 | } |
| 219 | |
| 220 | static void |
| 221 | vl_api_sw_interface_tap_dump_t_handler (vl_api_sw_interface_tap_dump_t * mp) |
| 222 | { |
| 223 | int rv = 0; |
| 224 | vpe_api_main_t *am = &vpe_api_main; |
| 225 | unix_shared_memory_queue_t *q; |
| 226 | tapcli_interface_details_t *tapifs = NULL; |
| 227 | tapcli_interface_details_t *tap_if = NULL; |
| 228 | |
| 229 | q = vl_api_client_index_to_input_queue (mp->client_index); |
| 230 | if (q == 0) |
| 231 | return; |
| 232 | |
| 233 | rv = vnet_tap_dump_ifs (&tapifs); |
| 234 | if (rv) |
| 235 | return; |
| 236 | |
| 237 | vec_foreach (tap_if, tapifs) |
| 238 | { |
| 239 | send_sw_interface_tap_details (am, q, tap_if, mp->context); |
| 240 | } |
| 241 | |
| 242 | vec_free (tapifs); |
| 243 | } |
| 244 | |
| 245 | static void |
| 246 | setup_message_id_table (api_main_t * am) |
| 247 | { |
| 248 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 249 | foreach_vl_msg_name_crc_tap; |
| 250 | #undef _ |
| 251 | } |
| 252 | |
| 253 | static clib_error_t * |
| 254 | tap_api_hookup (vlib_main_t * vm) |
| 255 | { |
| 256 | api_main_t *am = &api_main; |
| 257 | |
| 258 | #define _(N,n) \ |
| 259 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 260 | vl_api_##n##_t_handler, \ |
| 261 | vl_noop_handler, \ |
| 262 | vl_api_##n##_t_endian, \ |
| 263 | vl_api_##n##_t_print, \ |
| 264 | sizeof(vl_api_##n##_t), 1); |
| 265 | foreach_tap_api_msg; |
| 266 | #undef _ |
| 267 | |
| 268 | /* |
| 269 | * Set up the (msg_name, crc, message-id) table |
| 270 | */ |
| 271 | setup_message_id_table (am); |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | VLIB_API_INIT_FUNCTION (tap_api_hookup); |
| 277 | |
| 278 | /* |
| 279 | * fd.io coding-style-patch-verification: ON |
| 280 | * |
| 281 | * Local Variables: |
| 282 | * eval: (c-set-style "gnu") |
| 283 | * End: |
| 284 | */ |