Dave Barach | aff7077 | 2016-10-31 11:59:07 -0400 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * interface_api.c - vnet interface api |
| 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> |
Matus Fabian | d162f3d | 2016-12-05 01:05:35 -0800 | [diff] [blame] | 25 | #include <vnet/ethernet/ethernet.h> |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 26 | #include <vnet/ip/ip.h> |
| 27 | #include <vnet/fib/fib_table.h> |
| 28 | #include <vnet/l2/l2_vtr.h> |
Dave Barach | aff7077 | 2016-10-31 11:59:07 -0400 | [diff] [blame] | 29 | #include <vnet/vnet_msg_enum.h> |
Dave Barach | b5e8a77 | 2016-12-06 12:04:42 -0500 | [diff] [blame] | 30 | #include <vnet/fib/fib_api.h> |
Dave Barach | aff7077 | 2016-10-31 11:59:07 -0400 | [diff] [blame] | 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 | |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 48 | #define foreach_vpe_api_msg \ |
| 49 | _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags) \ |
| 50 | _(SW_INTERFACE_SET_MTU, sw_interface_set_mtu) \ |
| 51 | _(WANT_INTERFACE_EVENTS, want_interface_events) \ |
| 52 | _(SW_INTERFACE_DUMP, sw_interface_dump) \ |
| 53 | _(SW_INTERFACE_DETAILS, sw_interface_details) \ |
| 54 | _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address) \ |
| 55 | _(SW_INTERFACE_SET_TABLE, sw_interface_set_table) \ |
Juraj Sloboda | dfc1923 | 2016-12-05 13:20:37 +0100 | [diff] [blame] | 56 | _(SW_INTERFACE_GET_TABLE, sw_interface_get_table) \ |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 57 | _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered) \ |
| 58 | _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats) \ |
| 59 | _(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del) |
Dave Barach | aff7077 | 2016-10-31 11:59:07 -0400 | [diff] [blame] | 60 | |
| 61 | static void |
| 62 | vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp) |
| 63 | { |
| 64 | vl_api_sw_interface_set_flags_reply_t *rmp; |
| 65 | vnet_main_t *vnm = vnet_get_main (); |
| 66 | int rv = 0; |
| 67 | clib_error_t *error; |
| 68 | u16 flags; |
| 69 | |
| 70 | VALIDATE_SW_IF_INDEX (mp); |
| 71 | |
| 72 | flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0; |
| 73 | |
| 74 | error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags); |
| 75 | if (error) |
| 76 | { |
| 77 | rv = -1; |
| 78 | clib_error_report (error); |
| 79 | } |
| 80 | |
| 81 | BAD_SW_IF_INDEX_LABEL; |
| 82 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY); |
| 83 | } |
| 84 | |
Matus Fabian | d162f3d | 2016-12-05 01:05:35 -0800 | [diff] [blame] | 85 | static void |
| 86 | vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp) |
| 87 | { |
| 88 | vl_api_sw_interface_set_mtu_reply_t *rmp; |
| 89 | vnet_main_t *vnm = vnet_get_main (); |
| 90 | u32 flags = ETHERNET_INTERFACE_FLAG_MTU; |
| 91 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 92 | u16 mtu = ntohs (mp->mtu); |
| 93 | ethernet_main_t *em = ðernet_main; |
| 94 | int rv = 0; |
| 95 | |
| 96 | VALIDATE_SW_IF_INDEX (mp); |
| 97 | |
| 98 | vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, sw_if_index); |
| 99 | ethernet_interface_t *eif = ethernet_get_interface (em, sw_if_index); |
| 100 | |
| 101 | if (!eif) |
| 102 | { |
| 103 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 104 | goto bad_sw_if_index; |
| 105 | } |
| 106 | |
| 107 | if (mtu < hi->min_supported_packet_bytes) |
| 108 | { |
| 109 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 110 | goto bad_sw_if_index; |
| 111 | } |
| 112 | |
| 113 | if (mtu > hi->max_supported_packet_bytes) |
| 114 | { |
| 115 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 116 | goto bad_sw_if_index; |
| 117 | } |
| 118 | |
| 119 | if (hi->max_packet_bytes != mtu) |
| 120 | { |
| 121 | hi->max_packet_bytes = mtu; |
| 122 | ethernet_set_flags (vnm, sw_if_index, flags); |
| 123 | } |
| 124 | |
| 125 | BAD_SW_IF_INDEX_LABEL; |
| 126 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY); |
| 127 | } |
| 128 | |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 129 | static void |
| 130 | send_sw_interface_details (vpe_api_main_t * am, |
| 131 | unix_shared_memory_queue_t * q, |
| 132 | vnet_sw_interface_t * swif, |
| 133 | u8 * interface_name, u32 context) |
| 134 | { |
| 135 | vl_api_sw_interface_details_t *mp; |
| 136 | vnet_main_t *vnm = vnet_get_main (); |
| 137 | vnet_hw_interface_t *hi; |
| 138 | u8 *tag; |
| 139 | |
| 140 | hi = vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index); |
| 141 | |
| 142 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 143 | memset (mp, 0, sizeof (*mp)); |
| 144 | mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS); |
| 145 | mp->sw_if_index = ntohl (swif->sw_if_index); |
| 146 | mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index); |
| 147 | mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0; |
| 148 | mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0; |
| 149 | mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >> |
| 150 | VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT); |
| 151 | mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >> |
| 152 | VNET_HW_INTERFACE_FLAG_SPEED_SHIFT); |
| 153 | mp->link_mtu = ntohs (hi->max_packet_bytes); |
| 154 | mp->context = context; |
| 155 | |
| 156 | strncpy ((char *) mp->interface_name, |
| 157 | (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1); |
| 158 | |
| 159 | /* Send the L2 address for ethernet physical intfcs */ |
| 160 | if (swif->sup_sw_if_index == swif->sw_if_index |
| 161 | && hi->hw_class_index == ethernet_hw_interface_class.index) |
| 162 | { |
| 163 | ethernet_main_t *em = ethernet_get_main (am->vlib_main); |
| 164 | ethernet_interface_t *ei; |
| 165 | |
| 166 | ei = pool_elt_at_index (em->interfaces, hi->hw_instance); |
| 167 | ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address)); |
| 168 | clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address)); |
| 169 | mp->l2_address_length = ntohl (sizeof (ei->address)); |
| 170 | } |
| 171 | else if (swif->sup_sw_if_index != swif->sw_if_index) |
| 172 | { |
| 173 | vnet_sub_interface_t *sub = &swif->sub; |
| 174 | mp->sub_id = ntohl (sub->id); |
| 175 | mp->sub_dot1ad = sub->eth.flags.dot1ad; |
| 176 | mp->sub_number_of_tags = |
| 177 | sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2; |
| 178 | mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id); |
| 179 | mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id); |
| 180 | mp->sub_exact_match = sub->eth.flags.exact_match; |
| 181 | mp->sub_default = sub->eth.flags.default_sub; |
| 182 | mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any; |
| 183 | mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any; |
| 184 | |
| 185 | /* vlan tag rewrite data */ |
| 186 | u32 vtr_op = L2_VTR_DISABLED; |
| 187 | u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0; |
| 188 | |
| 189 | if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index, |
| 190 | &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0) |
| 191 | { |
| 192 | // error - default to disabled |
| 193 | mp->vtr_op = ntohl (L2_VTR_DISABLED); |
| 194 | clib_warning ("cannot get vlan tag rewrite for sw_if_index %d", |
| 195 | swif->sw_if_index); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | mp->vtr_op = ntohl (vtr_op); |
| 200 | mp->vtr_push_dot1q = ntohl (vtr_push_dot1q); |
| 201 | mp->vtr_tag1 = ntohl (vtr_tag1); |
| 202 | mp->vtr_tag2 = ntohl (vtr_tag2); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | tag = vnet_get_sw_interface_tag (vnm, swif->sw_if_index); |
| 207 | if (tag) |
| 208 | strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1); |
| 209 | |
| 210 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 211 | } |
| 212 | |
| 213 | static void |
| 214 | vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp) |
| 215 | { |
| 216 | vpe_api_main_t *am = &vpe_api_main; |
| 217 | vnet_sw_interface_t *swif; |
| 218 | vnet_interface_main_t *im = &am->vnet_main->interface_main; |
| 219 | u8 *filter_string = 0, *name_string = 0; |
| 220 | unix_shared_memory_queue_t *q; |
| 221 | char *strcasestr (char *, char *); /* lnx hdr file botch */ |
| 222 | |
| 223 | q = vl_api_client_index_to_input_queue (mp->client_index); |
| 224 | |
| 225 | if (q == 0) |
| 226 | return; |
| 227 | |
| 228 | if (mp->name_filter_valid) |
| 229 | { |
| 230 | mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0; |
| 231 | filter_string = format (0, "%s%c", mp->name_filter, 0); |
| 232 | } |
| 233 | |
| 234 | /* *INDENT-OFF* */ |
| 235 | pool_foreach (swif, im->sw_interfaces, |
| 236 | ({ |
| 237 | name_string = format (name_string, "%U%c", |
| 238 | format_vnet_sw_interface_name, |
| 239 | am->vnet_main, swif, 0); |
| 240 | |
| 241 | if (mp->name_filter_valid == 0 || |
| 242 | strcasestr((char *) name_string, (char *) filter_string)) { |
| 243 | |
| 244 | send_sw_interface_details (am, q, swif, name_string, mp->context); |
| 245 | } |
| 246 | _vec_len (name_string) = 0; |
| 247 | })); |
| 248 | /* *INDENT-ON* */ |
| 249 | |
| 250 | vec_free (name_string); |
| 251 | vec_free (filter_string); |
| 252 | } |
| 253 | |
| 254 | static void |
| 255 | vl_api_sw_interface_add_del_address_t_handler |
| 256 | (vl_api_sw_interface_add_del_address_t * mp) |
| 257 | { |
| 258 | vlib_main_t *vm = vlib_get_main (); |
| 259 | vl_api_sw_interface_add_del_address_reply_t *rmp; |
| 260 | int rv = 0; |
| 261 | u32 is_del; |
| 262 | |
| 263 | VALIDATE_SW_IF_INDEX (mp); |
| 264 | |
| 265 | is_del = mp->is_add == 0; |
| 266 | |
| 267 | if (mp->del_all) |
| 268 | ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index)); |
| 269 | else if (mp->is_ipv6) |
| 270 | ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index), |
| 271 | (void *) mp->address, |
| 272 | mp->address_length, is_del); |
| 273 | else |
| 274 | ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index), |
| 275 | (void *) mp->address, |
| 276 | mp->address_length, is_del); |
| 277 | |
| 278 | BAD_SW_IF_INDEX_LABEL; |
| 279 | |
| 280 | REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY); |
| 281 | } |
| 282 | |
| 283 | void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak)); |
| 284 | void |
| 285 | stats_dslock_with_hint (int hint, int tag) |
| 286 | { |
| 287 | } |
| 288 | |
| 289 | void stats_dsunlock (void) __attribute__ ((weak)); |
| 290 | void |
| 291 | stats_dsunlock (void) |
| 292 | { |
| 293 | } |
| 294 | |
| 295 | static void |
| 296 | vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp) |
| 297 | { |
| 298 | int rv = 0; |
| 299 | u32 table_id = ntohl (mp->vrf_id); |
| 300 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 301 | vl_api_sw_interface_set_table_reply_t *rmp; |
| 302 | u32 fib_index; |
| 303 | |
| 304 | VALIDATE_SW_IF_INDEX (mp); |
| 305 | |
| 306 | stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ ); |
| 307 | |
| 308 | if (mp->is_ipv6) |
| 309 | { |
| 310 | fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, |
| 311 | table_id); |
| 312 | |
| 313 | vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index); |
| 314 | ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index; |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | |
| 319 | fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, |
| 320 | table_id); |
| 321 | |
| 322 | vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index); |
| 323 | ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index; |
| 324 | } |
| 325 | stats_dsunlock (); |
| 326 | |
| 327 | BAD_SW_IF_INDEX_LABEL; |
| 328 | |
| 329 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY); |
| 330 | } |
| 331 | |
Juraj Sloboda | dfc1923 | 2016-12-05 13:20:37 +0100 | [diff] [blame] | 332 | static void |
| 333 | send_sw_interface_get_table_reply (unix_shared_memory_queue_t * q, |
| 334 | u32 context, int retval, u32 vrf_id) |
| 335 | { |
| 336 | vl_api_sw_interface_get_table_reply_t *mp; |
| 337 | |
| 338 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 339 | memset (mp, 0, sizeof (*mp)); |
| 340 | mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY); |
| 341 | mp->context = context; |
| 342 | mp->retval = htonl (retval); |
| 343 | mp->vrf_id = htonl (vrf_id); |
| 344 | |
| 345 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 346 | } |
| 347 | |
| 348 | static void |
| 349 | vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp) |
| 350 | { |
| 351 | unix_shared_memory_queue_t *q; |
| 352 | fib_table_t *fib_table = 0; |
| 353 | u32 sw_if_index = ~0; |
| 354 | u32 fib_index = ~0; |
| 355 | u32 table_id = ~0; |
| 356 | fib_protocol_t fib_proto = FIB_PROTOCOL_IP4; |
| 357 | int rv = 0; |
| 358 | |
| 359 | q = vl_api_client_index_to_input_queue (mp->client_index); |
| 360 | if (q == 0) |
| 361 | return; |
| 362 | |
| 363 | VALIDATE_SW_IF_INDEX (mp); |
| 364 | |
| 365 | sw_if_index = ntohl (mp->sw_if_index); |
| 366 | |
| 367 | if (mp->is_ipv6) |
| 368 | fib_proto = FIB_PROTOCOL_IP6; |
| 369 | |
| 370 | fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index); |
| 371 | if (fib_index != ~0) |
| 372 | { |
| 373 | fib_table = fib_table_get (fib_index, fib_proto); |
| 374 | table_id = fib_table->ft_table_id; |
| 375 | } |
| 376 | |
| 377 | BAD_SW_IF_INDEX_LABEL; |
| 378 | |
| 379 | send_sw_interface_get_table_reply (q, mp->context, rv, table_id); |
| 380 | } |
| 381 | |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 382 | static void vl_api_sw_interface_set_unnumbered_t_handler |
| 383 | (vl_api_sw_interface_set_unnumbered_t * mp) |
| 384 | { |
| 385 | vl_api_sw_interface_set_unnumbered_reply_t *rmp; |
| 386 | int rv = 0; |
| 387 | vnet_sw_interface_t *si; |
| 388 | vnet_main_t *vnm = vnet_get_main (); |
| 389 | u32 sw_if_index, unnumbered_sw_if_index; |
| 390 | |
| 391 | sw_if_index = ntohl (mp->sw_if_index); |
| 392 | unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index); |
| 393 | |
| 394 | /* |
| 395 | * The API message field names are backwards from |
| 396 | * the underlying data structure names. |
| 397 | * It's not worth changing them now. |
| 398 | */ |
| 399 | if (pool_is_free_index (vnm->interface_main.sw_interfaces, |
| 400 | unnumbered_sw_if_index)) |
| 401 | { |
| 402 | rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 403 | goto done; |
| 404 | } |
| 405 | |
| 406 | /* Only check the "use loop0" field when setting the binding */ |
| 407 | if (mp->is_add && |
| 408 | pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index)) |
| 409 | { |
| 410 | rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2; |
| 411 | goto done; |
| 412 | } |
| 413 | |
| 414 | si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index); |
| 415 | |
| 416 | if (mp->is_add) |
| 417 | { |
| 418 | si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED; |
| 419 | si->unnumbered_sw_if_index = sw_if_index; |
| 420 | ip4_sw_interface_enable_disable (unnumbered_sw_if_index, 1); |
| 421 | ip6_sw_interface_enable_disable (unnumbered_sw_if_index, 1); |
| 422 | } |
| 423 | else |
| 424 | { |
| 425 | si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED); |
| 426 | si->unnumbered_sw_if_index = (u32) ~ 0; |
| 427 | ip4_sw_interface_enable_disable (unnumbered_sw_if_index, 0); |
| 428 | ip6_sw_interface_enable_disable (unnumbered_sw_if_index, 0); |
| 429 | } |
| 430 | |
| 431 | done: |
| 432 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY); |
| 433 | } |
| 434 | |
| 435 | static void |
| 436 | vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t * |
| 437 | mp) |
| 438 | { |
| 439 | vl_api_sw_interface_clear_stats_reply_t *rmp; |
| 440 | |
| 441 | vnet_main_t *vnm = vnet_get_main (); |
| 442 | vnet_interface_main_t *im = &vnm->interface_main; |
| 443 | vlib_simple_counter_main_t *sm; |
| 444 | vlib_combined_counter_main_t *cm; |
| 445 | static vnet_main_t **my_vnet_mains; |
| 446 | int i, j, n_counters; |
| 447 | int rv = 0; |
| 448 | |
| 449 | if (mp->sw_if_index != ~0) |
| 450 | VALIDATE_SW_IF_INDEX (mp); |
| 451 | |
| 452 | vec_reset_length (my_vnet_mains); |
| 453 | |
| 454 | for (i = 0; i < vec_len (vnet_mains); i++) |
| 455 | { |
| 456 | if (vnet_mains[i]) |
| 457 | vec_add1 (my_vnet_mains, vnet_mains[i]); |
| 458 | } |
| 459 | |
| 460 | if (vec_len (vnet_mains) == 0) |
| 461 | vec_add1 (my_vnet_mains, vnm); |
| 462 | |
| 463 | n_counters = vec_len (im->combined_sw_if_counters); |
| 464 | |
| 465 | for (j = 0; j < n_counters; j++) |
| 466 | { |
| 467 | for (i = 0; i < vec_len (my_vnet_mains); i++) |
| 468 | { |
| 469 | im = &my_vnet_mains[i]->interface_main; |
| 470 | cm = im->combined_sw_if_counters + j; |
| 471 | if (mp->sw_if_index == (u32) ~ 0) |
| 472 | vlib_clear_combined_counters (cm); |
| 473 | else |
| 474 | vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index)); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | n_counters = vec_len (im->sw_if_counters); |
| 479 | |
| 480 | for (j = 0; j < n_counters; j++) |
| 481 | { |
| 482 | for (i = 0; i < vec_len (my_vnet_mains); i++) |
| 483 | { |
| 484 | im = &my_vnet_mains[i]->interface_main; |
| 485 | sm = im->sw_if_counters + j; |
| 486 | if (mp->sw_if_index == (u32) ~ 0) |
| 487 | vlib_clear_simple_counters (sm); |
| 488 | else |
| 489 | vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index)); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | BAD_SW_IF_INDEX_LABEL; |
| 494 | |
| 495 | REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY); |
| 496 | } |
| 497 | |
| 498 | #define API_LINK_STATE_EVENT 1 |
| 499 | #define API_ADMIN_UP_DOWN_EVENT 2 |
| 500 | |
| 501 | static int |
| 502 | event_data_cmp (void *a1, void *a2) |
| 503 | { |
| 504 | uword *e1 = a1; |
| 505 | uword *e2 = a2; |
| 506 | |
| 507 | return (word) e1[0] - (word) e2[0]; |
| 508 | } |
| 509 | |
| 510 | static void |
| 511 | send_sw_interface_flags (vpe_api_main_t * am, |
| 512 | unix_shared_memory_queue_t * q, |
| 513 | vnet_sw_interface_t * swif) |
| 514 | { |
| 515 | vl_api_sw_interface_set_flags_t *mp; |
| 516 | vnet_main_t *vnm = am->vnet_main; |
| 517 | |
| 518 | vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, |
| 519 | swif->sw_if_index); |
| 520 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 521 | memset (mp, 0, sizeof (*mp)); |
| 522 | mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SET_FLAGS); |
| 523 | mp->sw_if_index = ntohl (swif->sw_if_index); |
| 524 | |
| 525 | mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0; |
| 526 | mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0; |
| 527 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 528 | } |
| 529 | |
| 530 | static uword |
| 531 | link_state_process (vlib_main_t * vm, |
| 532 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
| 533 | { |
| 534 | vpe_api_main_t *vam = &vpe_api_main; |
| 535 | vnet_main_t *vnm = vam->vnet_main; |
| 536 | vnet_sw_interface_t *swif; |
| 537 | uword *event_data = 0; |
| 538 | vpe_client_registration_t *reg; |
| 539 | int i; |
| 540 | u32 prev_sw_if_index; |
| 541 | unix_shared_memory_queue_t *q; |
| 542 | |
| 543 | vam->link_state_process_up = 1; |
| 544 | |
| 545 | while (1) |
| 546 | { |
| 547 | vlib_process_wait_for_event (vm); |
| 548 | |
| 549 | /* Unified list of changed link or admin state sw_if_indices */ |
| 550 | vlib_process_get_events_with_type |
| 551 | (vm, &event_data, API_LINK_STATE_EVENT); |
| 552 | vlib_process_get_events_with_type |
| 553 | (vm, &event_data, API_ADMIN_UP_DOWN_EVENT); |
| 554 | |
| 555 | /* Sort, so we can eliminate duplicates */ |
| 556 | vec_sort_with_function (event_data, event_data_cmp); |
| 557 | |
| 558 | prev_sw_if_index = ~0; |
| 559 | |
| 560 | for (i = 0; i < vec_len (event_data); i++) |
| 561 | { |
| 562 | /* Only one message per swif */ |
| 563 | if (prev_sw_if_index == event_data[i]) |
| 564 | continue; |
| 565 | prev_sw_if_index = event_data[i]; |
| 566 | |
| 567 | /* *INDENT-OFF* */ |
| 568 | pool_foreach(reg, vam->interface_events_registrations, |
| 569 | ({ |
| 570 | q = vl_api_client_index_to_input_queue (reg->client_index); |
| 571 | if (q) |
| 572 | { |
| 573 | /* sw_interface may be deleted already */ |
| 574 | if (!pool_is_free_index (vnm->interface_main.sw_interfaces, |
| 575 | event_data[i])) |
| 576 | { |
| 577 | swif = vnet_get_sw_interface (vnm, event_data[i]); |
| 578 | send_sw_interface_flags (vam, q, swif); |
| 579 | } |
| 580 | } |
| 581 | })); |
| 582 | /* *INDENT-ON* */ |
| 583 | } |
| 584 | vec_reset_length (event_data); |
| 585 | } |
| 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index, |
| 591 | u32 flags); |
| 592 | static clib_error_t *admin_up_down_function (vnet_main_t * vm, |
| 593 | u32 hw_if_index, u32 flags); |
| 594 | |
| 595 | /* *INDENT-OFF* */ |
| 596 | VLIB_REGISTER_NODE (link_state_process_node,static) = { |
| 597 | .function = link_state_process, |
| 598 | .type = VLIB_NODE_TYPE_PROCESS, |
| 599 | .name = "vpe-link-state-process", |
| 600 | }; |
| 601 | /* *INDENT-ON* */ |
| 602 | |
| 603 | VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function); |
| 604 | VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function); |
| 605 | |
| 606 | static clib_error_t * |
| 607 | link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags) |
| 608 | { |
| 609 | vpe_api_main_t *vam = &vpe_api_main; |
| 610 | vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index); |
| 611 | |
| 612 | if (vam->link_state_process_up) |
| 613 | vlib_process_signal_event (vam->vlib_main, |
| 614 | link_state_process_node.index, |
| 615 | API_LINK_STATE_EVENT, hi->sw_if_index); |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static clib_error_t * |
| 620 | admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags) |
| 621 | { |
| 622 | vpe_api_main_t *vam = &vpe_api_main; |
| 623 | |
| 624 | /* |
| 625 | * Note: it's perfectly fair to set a subif admin up / admin down. |
| 626 | * Note the subtle distinction between this routine and the previous |
| 627 | * routine. |
| 628 | */ |
| 629 | if (vam->link_state_process_up) |
| 630 | vlib_process_signal_event (vam->vlib_main, |
| 631 | link_state_process_node.index, |
| 632 | API_ADMIN_UP_DOWN_EVENT, sw_if_index); |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | static void vl_api_sw_interface_tag_add_del_t_handler |
| 637 | (vl_api_sw_interface_tag_add_del_t * mp) |
| 638 | { |
| 639 | vnet_main_t *vnm = vnet_get_main (); |
| 640 | vl_api_sw_interface_tag_add_del_reply_t *rmp; |
| 641 | int rv = 0; |
| 642 | u8 *tag; |
| 643 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 644 | |
| 645 | VALIDATE_SW_IF_INDEX (mp); |
| 646 | |
| 647 | if (mp->is_add) |
| 648 | { |
| 649 | if (mp->tag[0] == 0) |
| 650 | { |
| 651 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 652 | goto out; |
| 653 | } |
| 654 | |
| 655 | mp->tag[ARRAY_LEN (mp->tag) - 1] = 0; |
| 656 | tag = format (0, "%s%c", mp->tag, 0); |
| 657 | vnet_set_sw_interface_tag (vnm, tag, sw_if_index); |
| 658 | } |
| 659 | else |
| 660 | vnet_clear_sw_interface_tag (vnm, sw_if_index); |
| 661 | |
| 662 | BAD_SW_IF_INDEX_LABEL; |
| 663 | out: |
| 664 | REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY); |
| 665 | } |
| 666 | |
| 667 | static void |
| 668 | vl_api_sw_interface_details_t_handler (vl_api_sw_interface_details_t * mp) |
| 669 | { |
| 670 | clib_warning ("BUG"); |
| 671 | } |
Dave Barach | aff7077 | 2016-10-31 11:59:07 -0400 | [diff] [blame] | 672 | |
| 673 | /* |
| 674 | * vpe_api_hookup |
| 675 | * Add vpe's API message handlers to the table. |
| 676 | * vlib has alread mapped shared memory and |
| 677 | * added the client registration handlers. |
| 678 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 679 | */ |
| 680 | #define vl_msg_name_crc_list |
Dave Barach | b5e8a77 | 2016-12-06 12:04:42 -0500 | [diff] [blame] | 681 | #include <vnet/interface.api.h> |
Dave Barach | aff7077 | 2016-10-31 11:59:07 -0400 | [diff] [blame] | 682 | #undef vl_msg_name_crc_list |
| 683 | |
| 684 | static void |
| 685 | setup_message_id_table (api_main_t * am) |
| 686 | { |
| 687 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 688 | foreach_vl_msg_name_crc_interface; |
| 689 | #undef _ |
| 690 | } |
| 691 | |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 692 | pub_sub_handler (interface_events, INTERFACE_EVENTS); |
| 693 | |
Dave Barach | aff7077 | 2016-10-31 11:59:07 -0400 | [diff] [blame] | 694 | static clib_error_t * |
| 695 | interface_api_hookup (vlib_main_t * vm) |
| 696 | { |
| 697 | api_main_t *am = &api_main; |
| 698 | |
| 699 | #define _(N,n) \ |
| 700 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 701 | vl_api_##n##_t_handler, \ |
| 702 | vl_noop_handler, \ |
| 703 | vl_api_##n##_t_endian, \ |
| 704 | vl_api_##n##_t_print, \ |
| 705 | sizeof(vl_api_##n##_t), 1); |
| 706 | foreach_vpe_api_msg; |
| 707 | #undef _ |
| 708 | |
| 709 | /* |
| 710 | * Set up the (msg_name, crc, message-id) table |
| 711 | */ |
| 712 | setup_message_id_table (am); |
| 713 | |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | VLIB_API_INIT_FUNCTION (interface_api_hookup); |
| 718 | |
| 719 | /* |
| 720 | * fd.io coding-style-patch-verification: ON |
| 721 | * |
| 722 | * Local Variables: |
| 723 | * eval: (c-set-style "gnu") |
| 724 | * End: |
| 725 | */ |