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> |
Dave Barach | f158661 | 2017-03-30 09:33:01 -0400 | [diff] [blame] | 49 | vpe_api_main_t vpe_api_main; |
Dave Barach | aff7077 | 2016-10-31 11:59:07 -0400 | [diff] [blame] | 50 | |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 51 | #define foreach_vpe_api_msg \ |
| 52 | _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags) \ |
| 53 | _(SW_INTERFACE_SET_MTU, sw_interface_set_mtu) \ |
| 54 | _(WANT_INTERFACE_EVENTS, want_interface_events) \ |
| 55 | _(SW_INTERFACE_DUMP, sw_interface_dump) \ |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 56 | _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address) \ |
Steven | ad8015b | 2017-10-29 22:10:46 -0700 | [diff] [blame] | 57 | _(SW_INTERFACE_SET_RX_MODE, sw_interface_set_rx_mode) \ |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 58 | _(SW_INTERFACE_SET_TABLE, sw_interface_set_table) \ |
Juraj Sloboda | dfc1923 | 2016-12-05 13:20:37 +0100 | [diff] [blame] | 59 | _(SW_INTERFACE_GET_TABLE, sw_interface_get_table) \ |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 60 | _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered) \ |
| 61 | _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats) \ |
Jon Loeliger | 10c273b | 2017-03-30 08:39:33 -0500 | [diff] [blame] | 62 | _(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del) \ |
| 63 | _(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address) |
Dave Barach | aff7077 | 2016-10-31 11:59:07 -0400 | [diff] [blame] | 64 | |
| 65 | static void |
| 66 | vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp) |
| 67 | { |
| 68 | vl_api_sw_interface_set_flags_reply_t *rmp; |
| 69 | vnet_main_t *vnm = vnet_get_main (); |
| 70 | int rv = 0; |
| 71 | clib_error_t *error; |
| 72 | u16 flags; |
| 73 | |
| 74 | VALIDATE_SW_IF_INDEX (mp); |
| 75 | |
| 76 | flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0; |
| 77 | |
| 78 | error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags); |
| 79 | if (error) |
| 80 | { |
| 81 | rv = -1; |
| 82 | clib_error_report (error); |
| 83 | } |
| 84 | |
| 85 | BAD_SW_IF_INDEX_LABEL; |
| 86 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY); |
| 87 | } |
| 88 | |
Matus Fabian | d162f3d | 2016-12-05 01:05:35 -0800 | [diff] [blame] | 89 | static void |
| 90 | vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp) |
| 91 | { |
| 92 | vl_api_sw_interface_set_mtu_reply_t *rmp; |
| 93 | vnet_main_t *vnm = vnet_get_main (); |
| 94 | u32 flags = ETHERNET_INTERFACE_FLAG_MTU; |
| 95 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 96 | u16 mtu = ntohs (mp->mtu); |
| 97 | ethernet_main_t *em = ðernet_main; |
| 98 | int rv = 0; |
| 99 | |
| 100 | VALIDATE_SW_IF_INDEX (mp); |
| 101 | |
Andrew Yourtchenko | 81d1e27 | 2017-08-22 13:10:01 +0200 | [diff] [blame] | 102 | vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index); |
| 103 | if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE) |
| 104 | { |
| 105 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 106 | goto bad_sw_if_index; |
| 107 | } |
| 108 | |
| 109 | vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index); |
| 110 | ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index); |
Matus Fabian | d162f3d | 2016-12-05 01:05:35 -0800 | [diff] [blame] | 111 | |
| 112 | if (!eif) |
| 113 | { |
| 114 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 115 | goto bad_sw_if_index; |
| 116 | } |
| 117 | |
| 118 | if (mtu < hi->min_supported_packet_bytes) |
| 119 | { |
| 120 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 121 | goto bad_sw_if_index; |
| 122 | } |
| 123 | |
| 124 | if (mtu > hi->max_supported_packet_bytes) |
| 125 | { |
| 126 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 127 | goto bad_sw_if_index; |
| 128 | } |
| 129 | |
| 130 | if (hi->max_packet_bytes != mtu) |
| 131 | { |
| 132 | hi->max_packet_bytes = mtu; |
Andrew Yourtchenko | 81d1e27 | 2017-08-22 13:10:01 +0200 | [diff] [blame] | 133 | ethernet_set_flags (vnm, si->hw_if_index, flags); |
Matus Fabian | d162f3d | 2016-12-05 01:05:35 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | BAD_SW_IF_INDEX_LABEL; |
| 137 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY); |
| 138 | } |
| 139 | |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 140 | static void |
| 141 | send_sw_interface_details (vpe_api_main_t * am, |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 142 | vl_api_registration_t * rp, |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 143 | vnet_sw_interface_t * swif, |
| 144 | u8 * interface_name, u32 context) |
| 145 | { |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 146 | vnet_hw_interface_t *hi = |
| 147 | vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 148 | |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 149 | 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] | 150 | memset (mp, 0, sizeof (*mp)); |
| 151 | mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS); |
| 152 | mp->sw_if_index = ntohl (swif->sw_if_index); |
| 153 | mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index); |
| 154 | mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0; |
| 155 | mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0; |
| 156 | mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >> |
| 157 | VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT); |
| 158 | mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >> |
| 159 | VNET_HW_INTERFACE_FLAG_SPEED_SHIFT); |
| 160 | mp->link_mtu = ntohs (hi->max_packet_bytes); |
| 161 | mp->context = context; |
| 162 | |
| 163 | strncpy ((char *) mp->interface_name, |
| 164 | (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1); |
| 165 | |
| 166 | /* Send the L2 address for ethernet physical intfcs */ |
| 167 | if (swif->sup_sw_if_index == swif->sw_if_index |
| 168 | && hi->hw_class_index == ethernet_hw_interface_class.index) |
| 169 | { |
| 170 | ethernet_main_t *em = ethernet_get_main (am->vlib_main); |
| 171 | ethernet_interface_t *ei; |
| 172 | |
| 173 | ei = pool_elt_at_index (em->interfaces, hi->hw_instance); |
| 174 | ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address)); |
| 175 | clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address)); |
| 176 | mp->l2_address_length = ntohl (sizeof (ei->address)); |
| 177 | } |
| 178 | else if (swif->sup_sw_if_index != swif->sw_if_index) |
| 179 | { |
| 180 | vnet_sub_interface_t *sub = &swif->sub; |
| 181 | mp->sub_id = ntohl (sub->id); |
| 182 | mp->sub_dot1ad = sub->eth.flags.dot1ad; |
| 183 | mp->sub_number_of_tags = |
| 184 | sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2; |
| 185 | mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id); |
| 186 | mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id); |
| 187 | mp->sub_exact_match = sub->eth.flags.exact_match; |
| 188 | mp->sub_default = sub->eth.flags.default_sub; |
| 189 | mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any; |
| 190 | mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any; |
| 191 | |
| 192 | /* vlan tag rewrite data */ |
| 193 | u32 vtr_op = L2_VTR_DISABLED; |
| 194 | u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0; |
| 195 | |
| 196 | if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index, |
| 197 | &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0) |
| 198 | { |
| 199 | // error - default to disabled |
| 200 | mp->vtr_op = ntohl (L2_VTR_DISABLED); |
| 201 | clib_warning ("cannot get vlan tag rewrite for sw_if_index %d", |
| 202 | swif->sw_if_index); |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | mp->vtr_op = ntohl (vtr_op); |
| 207 | mp->vtr_push_dot1q = ntohl (vtr_push_dot1q); |
| 208 | mp->vtr_tag1 = ntohl (vtr_tag1); |
| 209 | mp->vtr_tag2 = ntohl (vtr_tag2); |
| 210 | } |
| 211 | } |
| 212 | |
Pavel Kotucek | 65e8457 | 2017-01-16 17:01:56 +0100 | [diff] [blame] | 213 | /* pbb tag rewrite data */ |
Gabriel Ganne | f7f2a9f | 2017-03-06 15:19:40 +0100 | [diff] [blame] | 214 | ethernet_header_t eth_hdr; |
Pavel Kotucek | 65e8457 | 2017-01-16 17:01:56 +0100 | [diff] [blame] | 215 | u32 vtr_op = L2_VTR_DISABLED; |
| 216 | u16 outer_tag = 0; |
Pavel Kotucek | 65e8457 | 2017-01-16 17:01:56 +0100 | [diff] [blame] | 217 | u16 b_vlanid = 0; |
| 218 | u32 i_sid = 0; |
Gabriel Ganne | f7f2a9f | 2017-03-06 15:19:40 +0100 | [diff] [blame] | 219 | memset (ð_hdr, 0, sizeof (eth_hdr)); |
Pavel Kotucek | 65e8457 | 2017-01-16 17:01:56 +0100 | [diff] [blame] | 220 | |
| 221 | 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] | 222 | &vtr_op, &outer_tag, ð_hdr, &b_vlanid, &i_sid)) |
Pavel Kotucek | 65e8457 | 2017-01-16 17:01:56 +0100 | [diff] [blame] | 223 | { |
| 224 | mp->sub_dot1ah = 1; |
Gabriel Ganne | f7f2a9f | 2017-03-06 15:19:40 +0100 | [diff] [blame] | 225 | clib_memcpy (mp->b_dmac, eth_hdr.dst_address, |
| 226 | sizeof (eth_hdr.dst_address)); |
| 227 | clib_memcpy (mp->b_smac, eth_hdr.src_address, |
| 228 | sizeof (eth_hdr.src_address)); |
Pavel Kotucek | 65e8457 | 2017-01-16 17:01:56 +0100 | [diff] [blame] | 229 | mp->b_vlanid = b_vlanid; |
| 230 | mp->i_sid = i_sid; |
| 231 | } |
| 232 | |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 233 | 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] | 234 | if (tag) |
| 235 | strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1); |
| 236 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 237 | vl_msg_api_send (rp, (u8 *) mp); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static void |
| 241 | vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp) |
| 242 | { |
| 243 | vpe_api_main_t *am = &vpe_api_main; |
| 244 | vnet_sw_interface_t *swif; |
| 245 | vnet_interface_main_t *im = &am->vnet_main->interface_main; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 246 | vl_api_registration_t *rp; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 247 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 248 | rp = vl_api_client_index_to_registration (mp->client_index); |
| 249 | |
| 250 | if (rp == 0) |
| 251 | { |
| 252 | clib_warning ("Client %d AWOL", mp->client_index); |
| 253 | return; |
| 254 | } |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 255 | |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 256 | u8 *filter = 0, *name = 0; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 257 | if (mp->name_filter_valid) |
| 258 | { |
| 259 | mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0; |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 260 | filter = format (0, "%s%c", mp->name_filter, 0); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 261 | } |
| 262 | |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 263 | char *strcasestr (char *, char *); /* lnx hdr file botch */ |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 264 | /* *INDENT-OFF* */ |
| 265 | pool_foreach (swif, im->sw_interfaces, |
| 266 | ({ |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 267 | if (!vnet_swif_is_api_visible (swif)) |
| 268 | continue; |
| 269 | vec_reset_length(name); |
| 270 | name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main, |
| 271 | swif, 0); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 272 | |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 273 | if (filter && !strcasestr((char *) name, (char *) filter)) |
| 274 | continue; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 275 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 276 | send_sw_interface_details (am, rp, swif, name, mp->context); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 277 | })); |
| 278 | /* *INDENT-ON* */ |
| 279 | |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 280 | vec_free (name); |
| 281 | vec_free (filter); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static void |
| 285 | vl_api_sw_interface_add_del_address_t_handler |
| 286 | (vl_api_sw_interface_add_del_address_t * mp) |
| 287 | { |
| 288 | vlib_main_t *vm = vlib_get_main (); |
Jon Loeliger | 35ffa3e | 2017-09-28 13:54:16 -0500 | [diff] [blame] | 289 | vnet_main_t *vnm = vnet_get_main (); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 290 | vl_api_sw_interface_add_del_address_reply_t *rmp; |
| 291 | int rv = 0; |
| 292 | u32 is_del; |
Jon Loeliger | 35ffa3e | 2017-09-28 13:54:16 -0500 | [diff] [blame] | 293 | clib_error_t *error = 0; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 294 | |
| 295 | VALIDATE_SW_IF_INDEX (mp); |
| 296 | |
| 297 | is_del = mp->is_add == 0; |
Jon Loeliger | 35ffa3e | 2017-09-28 13:54:16 -0500 | [diff] [blame] | 298 | vnm->api_errno = 0; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 299 | |
| 300 | if (mp->del_all) |
| 301 | ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index)); |
| 302 | else if (mp->is_ipv6) |
Jon Loeliger | 35ffa3e | 2017-09-28 13:54:16 -0500 | [diff] [blame] | 303 | error = ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index), |
| 304 | (void *) mp->address, |
| 305 | mp->address_length, is_del); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 306 | else |
Jon Loeliger | 35ffa3e | 2017-09-28 13:54:16 -0500 | [diff] [blame] | 307 | error = ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index), |
| 308 | (void *) mp->address, |
| 309 | mp->address_length, is_del); |
| 310 | |
| 311 | if (error) |
| 312 | { |
| 313 | rv = vnm->api_errno; |
| 314 | clib_error_report (error); |
| 315 | goto done; |
| 316 | } |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 317 | |
| 318 | BAD_SW_IF_INDEX_LABEL; |
| 319 | |
Jon Loeliger | 35ffa3e | 2017-09-28 13:54:16 -0500 | [diff] [blame] | 320 | done: |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 321 | REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY); |
| 322 | } |
| 323 | |
| 324 | void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak)); |
| 325 | void |
| 326 | stats_dslock_with_hint (int hint, int tag) |
| 327 | { |
| 328 | } |
| 329 | |
| 330 | void stats_dsunlock (void) __attribute__ ((weak)); |
| 331 | void |
| 332 | stats_dsunlock (void) |
| 333 | { |
| 334 | } |
| 335 | |
| 336 | static void |
| 337 | vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp) |
| 338 | { |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 339 | vl_api_sw_interface_set_table_reply_t *rmp; |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 340 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 341 | u32 table_id = ntohl (mp->vrf_id); |
| 342 | int rv = 0; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 343 | |
| 344 | VALIDATE_SW_IF_INDEX (mp); |
| 345 | |
| 346 | stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ ); |
| 347 | |
| 348 | if (mp->is_ipv6) |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 349 | rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 350 | else |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 351 | rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 352 | |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 353 | stats_dsunlock (); |
| 354 | |
| 355 | BAD_SW_IF_INDEX_LABEL; |
| 356 | |
| 357 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY); |
| 358 | } |
| 359 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 360 | int |
| 361 | ip_table_bind (fib_protocol_t fproto, |
Neale Ranns | 6b3a8ef | 2017-09-11 10:34:33 -0700 | [diff] [blame] | 362 | u32 sw_if_index, u32 table_id, u8 is_api) |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 363 | { |
| 364 | CLIB_UNUSED (ip_interface_address_t * ia); |
| 365 | u32 fib_index, mfib_index; |
| 366 | fib_source_t src; |
| 367 | mfib_source_t msrc; |
| 368 | |
| 369 | if (is_api) |
| 370 | { |
| 371 | src = FIB_SOURCE_API; |
| 372 | msrc = MFIB_SOURCE_API; |
| 373 | } |
| 374 | else |
| 375 | { |
| 376 | src = FIB_SOURCE_CLI; |
| 377 | msrc = MFIB_SOURCE_CLI; |
| 378 | } |
| 379 | |
Florin Coras | d0a5972 | 2017-10-15 17:41:21 +0000 | [diff] [blame] | 380 | /* |
| 381 | * This is temporary whilst I do the song and dance with the CSIT version |
| 382 | */ |
| 383 | if (0 != table_id) |
| 384 | { |
| 385 | fib_index = fib_table_find_or_create_and_lock (fproto, table_id, src); |
| 386 | mfib_index = |
| 387 | mfib_table_find_or_create_and_lock (fproto, table_id, msrc); |
| 388 | } |
| 389 | else |
| 390 | { |
| 391 | fib_index = 0; |
| 392 | mfib_index = 0; |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * This if table does not exist = error is what we want in the end. |
| 397 | */ |
| 398 | /* fib_index = fib_table_find (fproto, table_id); */ |
| 399 | /* mfib_index = mfib_table_find (fproto, table_id); */ |
| 400 | |
| 401 | /* if (~0 == fib_index || ~0 == mfib_index) */ |
| 402 | /* { */ |
| 403 | /* return (VNET_API_ERROR_NO_SUCH_FIB); */ |
| 404 | /* } */ |
| 405 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 406 | if (FIB_PROTOCOL_IP6 == fproto) |
| 407 | { |
| 408 | /* |
| 409 | * If the interface already has in IP address, then a change int |
| 410 | * VRF is not allowed. The IP address applied must first be removed. |
| 411 | * We do not do that automatically here, since VPP has no knowledge |
| 412 | * of whether thoses subnets are valid in the destination VRF. |
| 413 | */ |
| 414 | /* *INDENT-OFF* */ |
| 415 | foreach_ip_interface_address (&ip6_main.lookup_main, |
| 416 | ia, sw_if_index, |
| 417 | 1 /* honor unnumbered */ , |
| 418 | ({ |
| 419 | return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE); |
| 420 | })); |
| 421 | /* *INDENT-ON* */ |
| 422 | |
| 423 | vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index); |
| 424 | vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index); |
| 425 | |
| 426 | /* |
| 427 | * tell those that are interested that the binding is changing. |
| 428 | */ |
| 429 | ip6_table_bind_callback_t *cb; |
| 430 | vec_foreach (cb, ip6_main.table_bind_callbacks) |
| 431 | cb->function (&ip6_main, cb->function_opaque, |
| 432 | sw_if_index, |
| 433 | fib_index, |
| 434 | ip6_main.fib_index_by_sw_if_index[sw_if_index]); |
| 435 | |
| 436 | if (0 == table_id) |
| 437 | { |
| 438 | /* reset back to default */ |
| 439 | if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index]) |
| 440 | fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index], |
| 441 | FIB_PROTOCOL_IP6, src); |
| 442 | if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index]) |
| 443 | mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index |
| 444 | [sw_if_index], FIB_PROTOCOL_IP6, msrc); |
| 445 | |
| 446 | } |
| 447 | else |
| 448 | { |
| 449 | /* we need to lock the table now it's inuse */ |
| 450 | fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src); |
| 451 | mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc); |
| 452 | } |
| 453 | |
| 454 | ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index; |
| 455 | ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index; |
| 456 | } |
| 457 | else |
| 458 | { |
| 459 | /* |
| 460 | * If the interface already has in IP address, then a change int |
| 461 | * VRF is not allowed. The IP address applied must first be removed. |
| 462 | * We do not do that automatically here, since VPP has no knowledge |
| 463 | * of whether thoses subnets are valid in the destination VRF. |
| 464 | */ |
| 465 | /* *INDENT-OFF* */ |
| 466 | foreach_ip_interface_address (&ip4_main.lookup_main, |
| 467 | ia, sw_if_index, |
| 468 | 1 /* honor unnumbered */ , |
| 469 | ({ |
| 470 | return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE); |
| 471 | })); |
| 472 | /* *INDENT-ON* */ |
| 473 | |
| 474 | vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index); |
| 475 | vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index); |
| 476 | |
| 477 | /* |
| 478 | * tell those that are interested that the binding is changing. |
| 479 | */ |
| 480 | ip4_table_bind_callback_t *cb; |
| 481 | vec_foreach (cb, ip4_main.table_bind_callbacks) |
| 482 | cb->function (&ip4_main, cb->function_opaque, |
| 483 | sw_if_index, |
| 484 | fib_index, |
| 485 | ip4_main.fib_index_by_sw_if_index[sw_if_index]); |
| 486 | |
| 487 | if (0 == table_id) |
| 488 | { |
| 489 | /* reset back to default */ |
| 490 | if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index]) |
| 491 | fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index], |
| 492 | FIB_PROTOCOL_IP4, src); |
| 493 | if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index]) |
| 494 | mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index |
| 495 | [sw_if_index], FIB_PROTOCOL_IP4, msrc); |
| 496 | |
| 497 | } |
| 498 | else |
| 499 | { |
| 500 | /* we need to lock the table now it's inuse */ |
| 501 | fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, |
| 502 | table_id, src); |
| 503 | |
| 504 | mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, |
| 505 | table_id, msrc); |
| 506 | } |
| 507 | |
| 508 | ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index; |
| 509 | ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index; |
| 510 | } |
| 511 | |
Florin Coras | d0a5972 | 2017-10-15 17:41:21 +0000 | [diff] [blame] | 512 | /* |
| 513 | * Temporary. undo the locks from the find and create at the staart |
| 514 | */ |
| 515 | if (0 != table_id) |
| 516 | { |
| 517 | fib_table_unlock (fib_index, fproto, src); |
| 518 | mfib_table_unlock (mfib_index, fproto, msrc); |
| 519 | } |
| 520 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 521 | return (0); |
| 522 | } |
| 523 | |
Juraj Sloboda | dfc1923 | 2016-12-05 13:20:37 +0100 | [diff] [blame] | 524 | static void |
| 525 | send_sw_interface_get_table_reply (unix_shared_memory_queue_t * q, |
| 526 | u32 context, int retval, u32 vrf_id) |
| 527 | { |
| 528 | vl_api_sw_interface_get_table_reply_t *mp; |
| 529 | |
| 530 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 531 | memset (mp, 0, sizeof (*mp)); |
| 532 | mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY); |
| 533 | mp->context = context; |
| 534 | mp->retval = htonl (retval); |
| 535 | mp->vrf_id = htonl (vrf_id); |
| 536 | |
| 537 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 538 | } |
| 539 | |
| 540 | static void |
| 541 | vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp) |
| 542 | { |
| 543 | unix_shared_memory_queue_t *q; |
| 544 | fib_table_t *fib_table = 0; |
| 545 | u32 sw_if_index = ~0; |
| 546 | u32 fib_index = ~0; |
| 547 | u32 table_id = ~0; |
| 548 | fib_protocol_t fib_proto = FIB_PROTOCOL_IP4; |
| 549 | int rv = 0; |
| 550 | |
| 551 | q = vl_api_client_index_to_input_queue (mp->client_index); |
| 552 | if (q == 0) |
| 553 | return; |
| 554 | |
| 555 | VALIDATE_SW_IF_INDEX (mp); |
| 556 | |
| 557 | sw_if_index = ntohl (mp->sw_if_index); |
| 558 | |
| 559 | if (mp->is_ipv6) |
| 560 | fib_proto = FIB_PROTOCOL_IP6; |
| 561 | |
| 562 | fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index); |
| 563 | if (fib_index != ~0) |
| 564 | { |
| 565 | fib_table = fib_table_get (fib_index, fib_proto); |
| 566 | table_id = fib_table->ft_table_id; |
| 567 | } |
| 568 | |
| 569 | BAD_SW_IF_INDEX_LABEL; |
| 570 | |
| 571 | send_sw_interface_get_table_reply (q, mp->context, rv, table_id); |
| 572 | } |
| 573 | |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 574 | static void vl_api_sw_interface_set_unnumbered_t_handler |
| 575 | (vl_api_sw_interface_set_unnumbered_t * mp) |
| 576 | { |
| 577 | vl_api_sw_interface_set_unnumbered_reply_t *rmp; |
| 578 | int rv = 0; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 579 | vnet_main_t *vnm = vnet_get_main (); |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 580 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 581 | u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index); |
Neale Ranns | 898273f | 2017-03-18 02:57:38 -0700 | [diff] [blame] | 582 | u32 was_unnum; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 583 | |
| 584 | /* |
| 585 | * The API message field names are backwards from |
| 586 | * the underlying data structure names. |
| 587 | * It's not worth changing them now. |
| 588 | */ |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 589 | if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index)) |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 590 | { |
| 591 | rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 592 | goto done; |
| 593 | } |
| 594 | |
| 595 | /* Only check the "use loop0" field when setting the binding */ |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 596 | 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] | 597 | { |
| 598 | rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2; |
| 599 | goto done; |
| 600 | } |
| 601 | |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 602 | vnet_sw_interface_t *si = |
| 603 | vnet_get_sw_interface (vnm, unnumbered_sw_if_index); |
Neale Ranns | 898273f | 2017-03-18 02:57:38 -0700 | [diff] [blame] | 604 | was_unnum = (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 605 | |
| 606 | if (mp->is_add) |
| 607 | { |
| 608 | si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED; |
| 609 | si->unnumbered_sw_if_index = sw_if_index; |
Neale Ranns | 4b919a5 | 2017-03-11 05:55:21 -0800 | [diff] [blame] | 610 | |
| 611 | ip4_main.lookup_main.if_address_pool_index_by_sw_if_index |
| 612 | [unnumbered_sw_if_index] = |
| 613 | ip4_main. |
| 614 | lookup_main.if_address_pool_index_by_sw_if_index[sw_if_index]; |
| 615 | ip6_main. |
| 616 | lookup_main.if_address_pool_index_by_sw_if_index |
| 617 | [unnumbered_sw_if_index] = |
| 618 | ip6_main. |
| 619 | lookup_main.if_address_pool_index_by_sw_if_index[sw_if_index]; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 620 | } |
| 621 | else |
| 622 | { |
| 623 | si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED); |
| 624 | si->unnumbered_sw_if_index = (u32) ~ 0; |
Neale Ranns | 4b919a5 | 2017-03-11 05:55:21 -0800 | [diff] [blame] | 625 | |
| 626 | ip4_main.lookup_main.if_address_pool_index_by_sw_if_index |
| 627 | [unnumbered_sw_if_index] = ~0; |
| 628 | ip6_main.lookup_main.if_address_pool_index_by_sw_if_index |
| 629 | [unnumbered_sw_if_index] = ~0; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 630 | } |
Neale Ranns | 898273f | 2017-03-18 02:57:38 -0700 | [diff] [blame] | 631 | |
| 632 | if (was_unnum != (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)) |
| 633 | { |
| 634 | ip4_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add); |
| 635 | ip6_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add); |
| 636 | } |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 637 | |
| 638 | done: |
| 639 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY); |
| 640 | } |
| 641 | |
| 642 | static void |
| 643 | vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t * |
| 644 | mp) |
| 645 | { |
| 646 | vl_api_sw_interface_clear_stats_reply_t *rmp; |
| 647 | |
| 648 | vnet_main_t *vnm = vnet_get_main (); |
| 649 | vnet_interface_main_t *im = &vnm->interface_main; |
| 650 | vlib_simple_counter_main_t *sm; |
| 651 | vlib_combined_counter_main_t *cm; |
| 652 | static vnet_main_t **my_vnet_mains; |
| 653 | int i, j, n_counters; |
| 654 | int rv = 0; |
| 655 | |
| 656 | if (mp->sw_if_index != ~0) |
| 657 | VALIDATE_SW_IF_INDEX (mp); |
| 658 | |
| 659 | vec_reset_length (my_vnet_mains); |
| 660 | |
| 661 | for (i = 0; i < vec_len (vnet_mains); i++) |
| 662 | { |
| 663 | if (vnet_mains[i]) |
| 664 | vec_add1 (my_vnet_mains, vnet_mains[i]); |
| 665 | } |
| 666 | |
| 667 | if (vec_len (vnet_mains) == 0) |
| 668 | vec_add1 (my_vnet_mains, vnm); |
| 669 | |
| 670 | n_counters = vec_len (im->combined_sw_if_counters); |
| 671 | |
| 672 | for (j = 0; j < n_counters; j++) |
| 673 | { |
| 674 | for (i = 0; i < vec_len (my_vnet_mains); i++) |
| 675 | { |
| 676 | im = &my_vnet_mains[i]->interface_main; |
| 677 | cm = im->combined_sw_if_counters + j; |
| 678 | if (mp->sw_if_index == (u32) ~ 0) |
| 679 | vlib_clear_combined_counters (cm); |
| 680 | else |
| 681 | vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index)); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | n_counters = vec_len (im->sw_if_counters); |
| 686 | |
| 687 | for (j = 0; j < n_counters; j++) |
| 688 | { |
| 689 | for (i = 0; i < vec_len (my_vnet_mains); i++) |
| 690 | { |
| 691 | im = &my_vnet_mains[i]->interface_main; |
| 692 | sm = im->sw_if_counters + j; |
| 693 | if (mp->sw_if_index == (u32) ~ 0) |
| 694 | vlib_clear_simple_counters (sm); |
| 695 | else |
| 696 | vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index)); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | BAD_SW_IF_INDEX_LABEL; |
| 701 | |
| 702 | REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY); |
| 703 | } |
| 704 | |
| 705 | #define API_LINK_STATE_EVENT 1 |
| 706 | #define API_ADMIN_UP_DOWN_EVENT 2 |
| 707 | |
| 708 | static int |
| 709 | event_data_cmp (void *a1, void *a2) |
| 710 | { |
| 711 | uword *e1 = a1; |
| 712 | uword *e2 = a2; |
| 713 | |
| 714 | return (word) e1[0] - (word) e2[0]; |
| 715 | } |
| 716 | |
| 717 | static void |
Neale Ranns | a07bd70 | 2017-08-07 07:53:49 -0700 | [diff] [blame] | 718 | send_sw_interface_event (vpe_api_main_t * am, |
Neale Ranns | d292ab1 | 2017-08-15 12:29:48 -0700 | [diff] [blame] | 719 | vpe_client_registration_t * reg, |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 720 | unix_shared_memory_queue_t * q, |
| 721 | vnet_sw_interface_t * swif) |
| 722 | { |
Neale Ranns | a07bd70 | 2017-08-07 07:53:49 -0700 | [diff] [blame] | 723 | vl_api_sw_interface_event_t *mp; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 724 | vnet_main_t *vnm = am->vnet_main; |
| 725 | |
| 726 | vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, |
| 727 | swif->sw_if_index); |
| 728 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 729 | memset (mp, 0, sizeof (*mp)); |
Neale Ranns | a07bd70 | 2017-08-07 07:53:49 -0700 | [diff] [blame] | 730 | mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 731 | mp->sw_if_index = ntohl (swif->sw_if_index); |
Neale Ranns | d292ab1 | 2017-08-15 12:29:48 -0700 | [diff] [blame] | 732 | mp->client_index = reg->client_index; |
| 733 | mp->pid = reg->client_pid; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 734 | |
| 735 | mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0; |
| 736 | mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0; |
| 737 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 738 | } |
| 739 | |
| 740 | static uword |
| 741 | link_state_process (vlib_main_t * vm, |
| 742 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
| 743 | { |
| 744 | vpe_api_main_t *vam = &vpe_api_main; |
| 745 | vnet_main_t *vnm = vam->vnet_main; |
| 746 | vnet_sw_interface_t *swif; |
| 747 | uword *event_data = 0; |
| 748 | vpe_client_registration_t *reg; |
| 749 | int i; |
| 750 | u32 prev_sw_if_index; |
| 751 | unix_shared_memory_queue_t *q; |
| 752 | |
| 753 | vam->link_state_process_up = 1; |
| 754 | |
| 755 | while (1) |
| 756 | { |
| 757 | vlib_process_wait_for_event (vm); |
| 758 | |
| 759 | /* Unified list of changed link or admin state sw_if_indices */ |
| 760 | vlib_process_get_events_with_type |
| 761 | (vm, &event_data, API_LINK_STATE_EVENT); |
| 762 | vlib_process_get_events_with_type |
| 763 | (vm, &event_data, API_ADMIN_UP_DOWN_EVENT); |
| 764 | |
| 765 | /* Sort, so we can eliminate duplicates */ |
| 766 | vec_sort_with_function (event_data, event_data_cmp); |
| 767 | |
| 768 | prev_sw_if_index = ~0; |
| 769 | |
| 770 | for (i = 0; i < vec_len (event_data); i++) |
| 771 | { |
| 772 | /* Only one message per swif */ |
| 773 | if (prev_sw_if_index == event_data[i]) |
| 774 | continue; |
| 775 | prev_sw_if_index = event_data[i]; |
| 776 | |
| 777 | /* *INDENT-OFF* */ |
| 778 | pool_foreach(reg, vam->interface_events_registrations, |
| 779 | ({ |
| 780 | q = vl_api_client_index_to_input_queue (reg->client_index); |
| 781 | if (q) |
| 782 | { |
| 783 | /* sw_interface may be deleted already */ |
| 784 | if (!pool_is_free_index (vnm->interface_main.sw_interfaces, |
| 785 | event_data[i])) |
| 786 | { |
| 787 | swif = vnet_get_sw_interface (vnm, event_data[i]); |
Neale Ranns | d292ab1 | 2017-08-15 12:29:48 -0700 | [diff] [blame] | 788 | send_sw_interface_event (vam, reg, q, swif); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | })); |
| 792 | /* *INDENT-ON* */ |
| 793 | } |
| 794 | vec_reset_length (event_data); |
| 795 | } |
| 796 | |
| 797 | return 0; |
| 798 | } |
| 799 | |
| 800 | static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index, |
| 801 | u32 flags); |
| 802 | static clib_error_t *admin_up_down_function (vnet_main_t * vm, |
| 803 | u32 hw_if_index, u32 flags); |
| 804 | |
| 805 | /* *INDENT-OFF* */ |
| 806 | VLIB_REGISTER_NODE (link_state_process_node,static) = { |
| 807 | .function = link_state_process, |
| 808 | .type = VLIB_NODE_TYPE_PROCESS, |
| 809 | .name = "vpe-link-state-process", |
| 810 | }; |
| 811 | /* *INDENT-ON* */ |
| 812 | |
| 813 | VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function); |
| 814 | VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function); |
| 815 | |
| 816 | static clib_error_t * |
| 817 | link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags) |
| 818 | { |
| 819 | vpe_api_main_t *vam = &vpe_api_main; |
| 820 | vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index); |
| 821 | |
| 822 | if (vam->link_state_process_up) |
| 823 | vlib_process_signal_event (vam->vlib_main, |
| 824 | link_state_process_node.index, |
| 825 | API_LINK_STATE_EVENT, hi->sw_if_index); |
| 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | static clib_error_t * |
| 830 | admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags) |
| 831 | { |
| 832 | vpe_api_main_t *vam = &vpe_api_main; |
| 833 | |
| 834 | /* |
| 835 | * Note: it's perfectly fair to set a subif admin up / admin down. |
| 836 | * Note the subtle distinction between this routine and the previous |
| 837 | * routine. |
| 838 | */ |
| 839 | if (vam->link_state_process_up) |
| 840 | vlib_process_signal_event (vam->vlib_main, |
| 841 | link_state_process_node.index, |
| 842 | API_ADMIN_UP_DOWN_EVENT, sw_if_index); |
| 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | static void vl_api_sw_interface_tag_add_del_t_handler |
| 847 | (vl_api_sw_interface_tag_add_del_t * mp) |
| 848 | { |
| 849 | vnet_main_t *vnm = vnet_get_main (); |
| 850 | vl_api_sw_interface_tag_add_del_reply_t *rmp; |
| 851 | int rv = 0; |
| 852 | u8 *tag; |
| 853 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 854 | |
| 855 | VALIDATE_SW_IF_INDEX (mp); |
| 856 | |
| 857 | if (mp->is_add) |
| 858 | { |
| 859 | if (mp->tag[0] == 0) |
| 860 | { |
| 861 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 862 | goto out; |
| 863 | } |
| 864 | |
| 865 | mp->tag[ARRAY_LEN (mp->tag) - 1] = 0; |
| 866 | tag = format (0, "%s%c", mp->tag, 0); |
| 867 | vnet_set_sw_interface_tag (vnm, tag, sw_if_index); |
| 868 | } |
| 869 | else |
| 870 | vnet_clear_sw_interface_tag (vnm, sw_if_index); |
| 871 | |
| 872 | BAD_SW_IF_INDEX_LABEL; |
| 873 | out: |
| 874 | REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY); |
| 875 | } |
| 876 | |
Jon Loeliger | 10c273b | 2017-03-30 08:39:33 -0500 | [diff] [blame] | 877 | static void vl_api_sw_interface_set_mac_address_t_handler |
| 878 | (vl_api_sw_interface_set_mac_address_t * mp) |
| 879 | { |
| 880 | vl_api_sw_interface_set_mac_address_reply_t *rmp; |
| 881 | vnet_main_t *vnm = vnet_get_main (); |
| 882 | u32 sw_if_index = ntohl (mp->sw_if_index); |
Neale Ranns | d867a7c | 2017-10-04 02:29:07 -0700 | [diff] [blame] | 883 | vnet_sw_interface_t *si; |
Jon Loeliger | 10c273b | 2017-03-30 08:39:33 -0500 | [diff] [blame] | 884 | clib_error_t *error; |
| 885 | int rv = 0; |
| 886 | |
| 887 | VALIDATE_SW_IF_INDEX (mp); |
| 888 | |
Neale Ranns | d867a7c | 2017-10-04 02:29:07 -0700 | [diff] [blame] | 889 | si = vnet_get_sw_interface (vnm, sw_if_index); |
John Lo | 62fcc0a | 2017-10-31 14:31:10 -0400 | [diff] [blame] | 890 | error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index, |
| 891 | mp->mac_address); |
Jon Loeliger | 10c273b | 2017-03-30 08:39:33 -0500 | [diff] [blame] | 892 | if (error) |
| 893 | { |
| 894 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 895 | clib_error_report (error); |
| 896 | goto out; |
| 897 | } |
| 898 | |
| 899 | BAD_SW_IF_INDEX_LABEL; |
| 900 | out: |
| 901 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY); |
| 902 | } |
| 903 | |
Steven | ad8015b | 2017-10-29 22:10:46 -0700 | [diff] [blame] | 904 | static void vl_api_sw_interface_set_rx_mode_t_handler |
| 905 | (vl_api_sw_interface_set_rx_mode_t * mp) |
| 906 | { |
| 907 | vl_api_sw_interface_set_rx_mode_reply_t *rmp; |
| 908 | vnet_main_t *vnm = vnet_get_main (); |
| 909 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 910 | vnet_sw_interface_t *si; |
| 911 | clib_error_t *error; |
| 912 | int rv = 0; |
| 913 | |
| 914 | VALIDATE_SW_IF_INDEX (mp); |
| 915 | |
| 916 | si = vnet_get_sw_interface (vnm, sw_if_index); |
| 917 | error = set_hw_interface_change_rx_mode (vnm, si->hw_if_index, |
| 918 | mp->queue_id_valid, |
| 919 | ntohl (mp->queue_id), mp->mode); |
| 920 | if (error) |
| 921 | { |
| 922 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 923 | clib_error_report (error); |
| 924 | goto out; |
| 925 | } |
| 926 | |
| 927 | BAD_SW_IF_INDEX_LABEL; |
| 928 | out: |
| 929 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_MODE_REPLY); |
| 930 | } |
| 931 | |
Dave Barach | aff7077 | 2016-10-31 11:59:07 -0400 | [diff] [blame] | 932 | /* |
| 933 | * vpe_api_hookup |
| 934 | * Add vpe's API message handlers to the table. |
| 935 | * vlib has alread mapped shared memory and |
| 936 | * added the client registration handlers. |
| 937 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 938 | */ |
| 939 | #define vl_msg_name_crc_list |
Dave Barach | b5e8a77 | 2016-12-06 12:04:42 -0500 | [diff] [blame] | 940 | #include <vnet/interface.api.h> |
Dave Barach | aff7077 | 2016-10-31 11:59:07 -0400 | [diff] [blame] | 941 | #undef vl_msg_name_crc_list |
| 942 | |
| 943 | static void |
| 944 | setup_message_id_table (api_main_t * am) |
| 945 | { |
| 946 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 947 | foreach_vl_msg_name_crc_interface; |
| 948 | #undef _ |
| 949 | } |
| 950 | |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 951 | pub_sub_handler (interface_events, INTERFACE_EVENTS); |
| 952 | |
Dave Barach | aff7077 | 2016-10-31 11:59:07 -0400 | [diff] [blame] | 953 | static clib_error_t * |
| 954 | interface_api_hookup (vlib_main_t * vm) |
| 955 | { |
| 956 | api_main_t *am = &api_main; |
| 957 | |
| 958 | #define _(N,n) \ |
| 959 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 960 | vl_api_##n##_t_handler, \ |
| 961 | vl_noop_handler, \ |
| 962 | vl_api_##n##_t_endian, \ |
| 963 | vl_api_##n##_t_print, \ |
| 964 | sizeof(vl_api_##n##_t), 1); |
| 965 | foreach_vpe_api_msg; |
| 966 | #undef _ |
| 967 | |
| 968 | /* |
| 969 | * Set up the (msg_name, crc, message-id) table |
| 970 | */ |
| 971 | setup_message_id_table (am); |
| 972 | |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | VLIB_API_INIT_FUNCTION (interface_api_hookup); |
| 977 | |
| 978 | /* |
| 979 | * fd.io coding-style-patch-verification: ON |
| 980 | * |
| 981 | * Local Variables: |
| 982 | * eval: (c-set-style "gnu") |
| 983 | * End: |
| 984 | */ |