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