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) \ |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 53 | _(HW_INTERFACE_SET_MTU, hw_interface_set_mtu) \ |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 54 | _(SW_INTERFACE_SET_MTU, sw_interface_set_mtu) \ |
| 55 | _(WANT_INTERFACE_EVENTS, want_interface_events) \ |
| 56 | _(SW_INTERFACE_DUMP, sw_interface_dump) \ |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 57 | _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address) \ |
Steven | ad8015b | 2017-10-29 22:10:46 -0700 | [diff] [blame] | 58 | _(SW_INTERFACE_SET_RX_MODE, sw_interface_set_rx_mode) \ |
Mohsin Kazmi | f0b42f4 | 2018-09-10 18:11:00 +0200 | [diff] [blame] | 59 | _(SW_INTERFACE_RX_PLACEMENT_DUMP, sw_interface_rx_placement_dump) \ |
Mohsin Kazmi | 54f7c51 | 2018-08-23 18:28:11 +0200 | [diff] [blame] | 60 | _(SW_INTERFACE_SET_RX_PLACEMENT, sw_interface_set_rx_placement) \ |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 61 | _(SW_INTERFACE_SET_TABLE, sw_interface_set_table) \ |
Juraj Sloboda | dfc1923 | 2016-12-05 13:20:37 +0100 | [diff] [blame] | 62 | _(SW_INTERFACE_GET_TABLE, sw_interface_get_table) \ |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 63 | _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered) \ |
| 64 | _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats) \ |
Jon Loeliger | 10c273b | 2017-03-30 08:39:33 -0500 | [diff] [blame] | 65 | _(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del) \ |
Neale Ranns | b8d4481 | 2017-11-10 06:53:54 -0800 | [diff] [blame] | 66 | _(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address) \ |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 67 | _(SW_INTERFACE_GET_MAC_ADDRESS, sw_interface_get_mac_address) \ |
Neale Ranns | b8d4481 | 2017-11-10 06:53:54 -0800 | [diff] [blame] | 68 | _(CREATE_VLAN_SUBIF, create_vlan_subif) \ |
| 69 | _(CREATE_SUBIF, create_subif) \ |
| 70 | _(DELETE_SUBIF, delete_subif) \ |
| 71 | _(CREATE_LOOPBACK, create_loopback) \ |
| 72 | _(CREATE_LOOPBACK_INSTANCE, create_loopback_instance) \ |
| 73 | _(DELETE_LOOPBACK, delete_loopback) \ |
Neale Ranns | 6f4a6be | 2018-03-16 16:26:21 -0700 | [diff] [blame] | 74 | _(INTERFACE_NAME_RENUMBER, interface_name_renumber) \ |
Neale Ranns | 1855b8e | 2018-07-11 10:31:26 -0700 | [diff] [blame] | 75 | _(COLLECT_DETAILED_INTERFACE_STATS, collect_detailed_interface_stats) \ |
| 76 | _(SW_INTERFACE_SET_IP_DIRECTED_BROADCAST, \ |
| 77 | sw_interface_set_ip_directed_broadcast) |
Dave Barach | aff7077 | 2016-10-31 11:59:07 -0400 | [diff] [blame] | 78 | |
| 79 | static void |
| 80 | vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp) |
| 81 | { |
| 82 | vl_api_sw_interface_set_flags_reply_t *rmp; |
| 83 | vnet_main_t *vnm = vnet_get_main (); |
| 84 | int rv = 0; |
| 85 | clib_error_t *error; |
| 86 | u16 flags; |
| 87 | |
| 88 | VALIDATE_SW_IF_INDEX (mp); |
| 89 | |
| 90 | flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0; |
| 91 | |
| 92 | error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags); |
| 93 | if (error) |
| 94 | { |
| 95 | rv = -1; |
| 96 | clib_error_report (error); |
| 97 | } |
| 98 | |
| 99 | BAD_SW_IF_INDEX_LABEL; |
| 100 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY); |
| 101 | } |
| 102 | |
Matus Fabian | d162f3d | 2016-12-05 01:05:35 -0800 | [diff] [blame] | 103 | static void |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 104 | vl_api_hw_interface_set_mtu_t_handler (vl_api_hw_interface_set_mtu_t * mp) |
Matus Fabian | d162f3d | 2016-12-05 01:05:35 -0800 | [diff] [blame] | 105 | { |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 106 | vl_api_hw_interface_set_mtu_reply_t *rmp; |
Matus Fabian | d162f3d | 2016-12-05 01:05:35 -0800 | [diff] [blame] | 107 | vnet_main_t *vnm = vnet_get_main (); |
Matus Fabian | d162f3d | 2016-12-05 01:05:35 -0800 | [diff] [blame] | 108 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 109 | u16 mtu = ntohs (mp->mtu); |
Damjan Marion | fe7d4a2 | 2018-04-13 19:43:39 +0200 | [diff] [blame] | 110 | ethernet_main_t *em = ðernet_main; |
Matus Fabian | d162f3d | 2016-12-05 01:05:35 -0800 | [diff] [blame] | 111 | int rv = 0; |
| 112 | |
| 113 | VALIDATE_SW_IF_INDEX (mp); |
| 114 | |
Damjan Marion | fe7d4a2 | 2018-04-13 19:43:39 +0200 | [diff] [blame] | 115 | vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index); |
| 116 | if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE) |
| 117 | { |
| 118 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 119 | goto bad_sw_if_index; |
| 120 | } |
| 121 | |
| 122 | vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index); |
| 123 | ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index); |
| 124 | |
| 125 | if (!eif) |
| 126 | { |
| 127 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 128 | goto bad_sw_if_index; |
| 129 | } |
| 130 | |
| 131 | if (mtu < hi->min_supported_packet_bytes) |
| 132 | { |
| 133 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 134 | goto bad_sw_if_index; |
| 135 | } |
| 136 | |
| 137 | if (mtu > hi->max_supported_packet_bytes) |
| 138 | { |
| 139 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 140 | goto bad_sw_if_index; |
| 141 | } |
| 142 | |
| 143 | vnet_hw_interface_set_mtu (vnm, si->hw_if_index, mtu); |
Matus Fabian | d162f3d | 2016-12-05 01:05:35 -0800 | [diff] [blame] | 144 | |
| 145 | BAD_SW_IF_INDEX_LABEL; |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 146 | REPLY_MACRO (VL_API_HW_INTERFACE_SET_MTU_REPLY); |
| 147 | } |
| 148 | |
| 149 | static void |
| 150 | vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp) |
| 151 | { |
| 152 | vl_api_sw_interface_set_mtu_reply_t *rmp; |
| 153 | vnet_main_t *vnm = vnet_get_main (); |
| 154 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 155 | int rv = 0; |
| 156 | int i; |
| 157 | u32 per_protocol_mtu[VNET_N_MTU]; |
| 158 | |
| 159 | VALIDATE_SW_IF_INDEX (mp); |
| 160 | |
| 161 | for (i = 0; i < VNET_N_MTU; i++) |
| 162 | per_protocol_mtu[i] = ntohl (mp->mtu[i]); |
| 163 | |
| 164 | vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, per_protocol_mtu); |
| 165 | |
| 166 | BAD_SW_IF_INDEX_LABEL; |
Matus Fabian | d162f3d | 2016-12-05 01:05:35 -0800 | [diff] [blame] | 167 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY); |
| 168 | } |
| 169 | |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 170 | static void |
Neale Ranns | 1855b8e | 2018-07-11 10:31:26 -0700 | [diff] [blame] | 171 | vl_api_sw_interface_set_ip_directed_broadcast_t_handler |
| 172 | (vl_api_sw_interface_set_ip_directed_broadcast_t * mp) |
| 173 | { |
| 174 | vl_api_sw_interface_set_ip_directed_broadcast_reply_t *rmp; |
| 175 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 176 | int rv = 0; |
| 177 | |
| 178 | VALIDATE_SW_IF_INDEX (mp); |
| 179 | |
| 180 | vnet_sw_interface_ip_directed_broadcast (vnet_get_main (), |
| 181 | sw_if_index, mp->enable); |
| 182 | |
| 183 | BAD_SW_IF_INDEX_LABEL; |
| 184 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_IP_DIRECTED_BROADCAST_REPLY); |
| 185 | } |
| 186 | |
| 187 | static void |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 188 | send_sw_interface_details (vpe_api_main_t * am, |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 189 | vl_api_registration_t * rp, |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 190 | vnet_sw_interface_t * swif, |
| 191 | u8 * interface_name, u32 context) |
| 192 | { |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 193 | vnet_hw_interface_t *hi = |
| 194 | vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 195 | |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 196 | 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] | 197 | memset (mp, 0, sizeof (*mp)); |
| 198 | mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS); |
| 199 | mp->sw_if_index = ntohl (swif->sw_if_index); |
| 200 | mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index); |
| 201 | mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0; |
| 202 | mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0; |
| 203 | mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >> |
| 204 | VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT); |
| 205 | mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >> |
| 206 | VNET_HW_INTERFACE_FLAG_SPEED_SHIFT); |
Damjan Marion | fe7d4a2 | 2018-04-13 19:43:39 +0200 | [diff] [blame] | 207 | mp->link_mtu = ntohs (hi->max_packet_bytes); |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 208 | mp->mtu[VNET_MTU_L3] = ntohl (swif->mtu[VNET_MTU_L3]); |
| 209 | mp->mtu[VNET_MTU_IP4] = ntohl (swif->mtu[VNET_MTU_IP4]); |
| 210 | mp->mtu[VNET_MTU_IP6] = ntohl (swif->mtu[VNET_MTU_IP6]); |
| 211 | mp->mtu[VNET_MTU_MPLS] = ntohl (swif->mtu[VNET_MTU_MPLS]); |
| 212 | |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 213 | mp->context = context; |
| 214 | |
| 215 | strncpy ((char *) mp->interface_name, |
| 216 | (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1); |
| 217 | |
| 218 | /* Send the L2 address for ethernet physical intfcs */ |
| 219 | if (swif->sup_sw_if_index == swif->sw_if_index |
| 220 | && hi->hw_class_index == ethernet_hw_interface_class.index) |
| 221 | { |
| 222 | ethernet_main_t *em = ethernet_get_main (am->vlib_main); |
| 223 | ethernet_interface_t *ei; |
| 224 | |
| 225 | ei = pool_elt_at_index (em->interfaces, hi->hw_instance); |
| 226 | ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address)); |
| 227 | clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address)); |
| 228 | mp->l2_address_length = ntohl (sizeof (ei->address)); |
| 229 | } |
| 230 | else if (swif->sup_sw_if_index != swif->sw_if_index) |
| 231 | { |
| 232 | vnet_sub_interface_t *sub = &swif->sub; |
| 233 | mp->sub_id = ntohl (sub->id); |
| 234 | mp->sub_dot1ad = sub->eth.flags.dot1ad; |
| 235 | mp->sub_number_of_tags = |
| 236 | sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2; |
| 237 | mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id); |
| 238 | mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id); |
| 239 | mp->sub_exact_match = sub->eth.flags.exact_match; |
| 240 | mp->sub_default = sub->eth.flags.default_sub; |
| 241 | mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any; |
| 242 | mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any; |
| 243 | |
| 244 | /* vlan tag rewrite data */ |
| 245 | u32 vtr_op = L2_VTR_DISABLED; |
| 246 | u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0; |
| 247 | |
| 248 | if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index, |
| 249 | &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0) |
| 250 | { |
| 251 | // error - default to disabled |
| 252 | mp->vtr_op = ntohl (L2_VTR_DISABLED); |
| 253 | clib_warning ("cannot get vlan tag rewrite for sw_if_index %d", |
| 254 | swif->sw_if_index); |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | mp->vtr_op = ntohl (vtr_op); |
| 259 | mp->vtr_push_dot1q = ntohl (vtr_push_dot1q); |
| 260 | mp->vtr_tag1 = ntohl (vtr_tag1); |
| 261 | mp->vtr_tag2 = ntohl (vtr_tag2); |
| 262 | } |
| 263 | } |
| 264 | |
Pavel Kotucek | 65e8457 | 2017-01-16 17:01:56 +0100 | [diff] [blame] | 265 | /* pbb tag rewrite data */ |
Gabriel Ganne | f7f2a9f | 2017-03-06 15:19:40 +0100 | [diff] [blame] | 266 | ethernet_header_t eth_hdr; |
Pavel Kotucek | 65e8457 | 2017-01-16 17:01:56 +0100 | [diff] [blame] | 267 | u32 vtr_op = L2_VTR_DISABLED; |
| 268 | u16 outer_tag = 0; |
Pavel Kotucek | 65e8457 | 2017-01-16 17:01:56 +0100 | [diff] [blame] | 269 | u16 b_vlanid = 0; |
| 270 | u32 i_sid = 0; |
Gabriel Ganne | f7f2a9f | 2017-03-06 15:19:40 +0100 | [diff] [blame] | 271 | memset (ð_hdr, 0, sizeof (eth_hdr)); |
Pavel Kotucek | 65e8457 | 2017-01-16 17:01:56 +0100 | [diff] [blame] | 272 | |
| 273 | 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] | 274 | &vtr_op, &outer_tag, ð_hdr, &b_vlanid, &i_sid)) |
Pavel Kotucek | 65e8457 | 2017-01-16 17:01:56 +0100 | [diff] [blame] | 275 | { |
| 276 | mp->sub_dot1ah = 1; |
Gabriel Ganne | f7f2a9f | 2017-03-06 15:19:40 +0100 | [diff] [blame] | 277 | clib_memcpy (mp->b_dmac, eth_hdr.dst_address, |
| 278 | sizeof (eth_hdr.dst_address)); |
| 279 | clib_memcpy (mp->b_smac, eth_hdr.src_address, |
| 280 | sizeof (eth_hdr.src_address)); |
Pavel Kotucek | 65e8457 | 2017-01-16 17:01:56 +0100 | [diff] [blame] | 281 | mp->b_vlanid = b_vlanid; |
| 282 | mp->i_sid = i_sid; |
| 283 | } |
| 284 | |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 285 | 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] | 286 | if (tag) |
| 287 | strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1); |
| 288 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 289 | vl_api_send_msg (rp, (u8 *) mp); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static void |
| 293 | vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp) |
| 294 | { |
| 295 | vpe_api_main_t *am = &vpe_api_main; |
| 296 | vnet_sw_interface_t *swif; |
| 297 | vnet_interface_main_t *im = &am->vnet_main->interface_main; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 298 | vl_api_registration_t *rp; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 299 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 300 | rp = vl_api_client_index_to_registration (mp->client_index); |
| 301 | |
| 302 | if (rp == 0) |
| 303 | { |
| 304 | clib_warning ("Client %d AWOL", mp->client_index); |
| 305 | return; |
| 306 | } |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 307 | |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 308 | u8 *filter = 0, *name = 0; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 309 | if (mp->name_filter_valid) |
| 310 | { |
| 311 | mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0; |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 312 | filter = format (0, "%s%c", mp->name_filter, 0); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 313 | } |
| 314 | |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 315 | char *strcasestr (char *, char *); /* lnx hdr file botch */ |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 316 | /* *INDENT-OFF* */ |
| 317 | pool_foreach (swif, im->sw_interfaces, |
| 318 | ({ |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 319 | if (!vnet_swif_is_api_visible (swif)) |
| 320 | continue; |
| 321 | vec_reset_length(name); |
| 322 | name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main, |
| 323 | swif, 0); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 324 | |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 325 | if (filter && !strcasestr((char *) name, (char *) filter)) |
| 326 | continue; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 327 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 328 | send_sw_interface_details (am, rp, swif, name, mp->context); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 329 | })); |
| 330 | /* *INDENT-ON* */ |
| 331 | |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 332 | vec_free (name); |
| 333 | vec_free (filter); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static void |
| 337 | vl_api_sw_interface_add_del_address_t_handler |
| 338 | (vl_api_sw_interface_add_del_address_t * mp) |
| 339 | { |
| 340 | vlib_main_t *vm = vlib_get_main (); |
Jon Loeliger | 35ffa3e | 2017-09-28 13:54:16 -0500 | [diff] [blame] | 341 | vnet_main_t *vnm = vnet_get_main (); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 342 | vl_api_sw_interface_add_del_address_reply_t *rmp; |
| 343 | int rv = 0; |
| 344 | u32 is_del; |
Jon Loeliger | 35ffa3e | 2017-09-28 13:54:16 -0500 | [diff] [blame] | 345 | clib_error_t *error = 0; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 346 | |
| 347 | VALIDATE_SW_IF_INDEX (mp); |
| 348 | |
| 349 | is_del = mp->is_add == 0; |
Jon Loeliger | 35ffa3e | 2017-09-28 13:54:16 -0500 | [diff] [blame] | 350 | vnm->api_errno = 0; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 351 | |
| 352 | if (mp->del_all) |
| 353 | ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index)); |
| 354 | else if (mp->is_ipv6) |
Jon Loeliger | 35ffa3e | 2017-09-28 13:54:16 -0500 | [diff] [blame] | 355 | error = ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index), |
| 356 | (void *) mp->address, |
| 357 | mp->address_length, is_del); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 358 | else |
Jon Loeliger | 35ffa3e | 2017-09-28 13:54:16 -0500 | [diff] [blame] | 359 | error = ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index), |
| 360 | (void *) mp->address, |
| 361 | mp->address_length, is_del); |
| 362 | |
| 363 | if (error) |
| 364 | { |
| 365 | rv = vnm->api_errno; |
| 366 | clib_error_report (error); |
| 367 | goto done; |
| 368 | } |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 369 | |
| 370 | BAD_SW_IF_INDEX_LABEL; |
| 371 | |
Jon Loeliger | 35ffa3e | 2017-09-28 13:54:16 -0500 | [diff] [blame] | 372 | done: |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 373 | REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY); |
| 374 | } |
| 375 | |
| 376 | void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak)); |
| 377 | void |
| 378 | stats_dslock_with_hint (int hint, int tag) |
| 379 | { |
| 380 | } |
| 381 | |
| 382 | void stats_dsunlock (void) __attribute__ ((weak)); |
| 383 | void |
| 384 | stats_dsunlock (void) |
| 385 | { |
| 386 | } |
| 387 | |
| 388 | static void |
| 389 | vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp) |
| 390 | { |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 391 | vl_api_sw_interface_set_table_reply_t *rmp; |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 392 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 393 | u32 table_id = ntohl (mp->vrf_id); |
| 394 | int rv = 0; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 395 | |
| 396 | VALIDATE_SW_IF_INDEX (mp); |
| 397 | |
| 398 | stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ ); |
| 399 | |
| 400 | if (mp->is_ipv6) |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 401 | 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] | 402 | else |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 403 | 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] | 404 | |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 405 | stats_dsunlock (); |
| 406 | |
| 407 | BAD_SW_IF_INDEX_LABEL; |
| 408 | |
| 409 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY); |
| 410 | } |
| 411 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 412 | int |
| 413 | ip_table_bind (fib_protocol_t fproto, |
Neale Ranns | 6b3a8ef | 2017-09-11 10:34:33 -0700 | [diff] [blame] | 414 | u32 sw_if_index, u32 table_id, u8 is_api) |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 415 | { |
| 416 | CLIB_UNUSED (ip_interface_address_t * ia); |
| 417 | u32 fib_index, mfib_index; |
| 418 | fib_source_t src; |
| 419 | mfib_source_t msrc; |
| 420 | |
| 421 | if (is_api) |
| 422 | { |
| 423 | src = FIB_SOURCE_API; |
| 424 | msrc = MFIB_SOURCE_API; |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | src = FIB_SOURCE_CLI; |
| 429 | msrc = MFIB_SOURCE_CLI; |
| 430 | } |
| 431 | |
Florin Coras | d0a5972 | 2017-10-15 17:41:21 +0000 | [diff] [blame] | 432 | /* |
Florin Coras | d0a5972 | 2017-10-15 17:41:21 +0000 | [diff] [blame] | 433 | * This if table does not exist = error is what we want in the end. |
| 434 | */ |
Neale Ranns | 8f6dd32 | 2018-05-17 06:34:24 -0700 | [diff] [blame] | 435 | fib_index = fib_table_find (fproto, table_id); |
| 436 | mfib_index = mfib_table_find (fproto, table_id); |
Florin Coras | d0a5972 | 2017-10-15 17:41:21 +0000 | [diff] [blame] | 437 | |
Neale Ranns | 8f6dd32 | 2018-05-17 06:34:24 -0700 | [diff] [blame] | 438 | if (~0 == fib_index || ~0 == mfib_index) |
| 439 | { |
| 440 | return (VNET_API_ERROR_NO_SUCH_FIB); |
| 441 | } |
Florin Coras | d0a5972 | 2017-10-15 17:41:21 +0000 | [diff] [blame] | 442 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 443 | if (FIB_PROTOCOL_IP6 == fproto) |
| 444 | { |
| 445 | /* |
| 446 | * If the interface already has in IP address, then a change int |
| 447 | * VRF is not allowed. The IP address applied must first be removed. |
| 448 | * We do not do that automatically here, since VPP has no knowledge |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 449 | * of whether those subnets are valid in the destination VRF. |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 450 | */ |
| 451 | /* *INDENT-OFF* */ |
| 452 | foreach_ip_interface_address (&ip6_main.lookup_main, |
| 453 | ia, sw_if_index, |
| 454 | 1 /* honor unnumbered */ , |
| 455 | ({ |
| 456 | return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE); |
| 457 | })); |
| 458 | /* *INDENT-ON* */ |
| 459 | |
| 460 | vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index); |
| 461 | vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index); |
| 462 | |
| 463 | /* |
| 464 | * tell those that are interested that the binding is changing. |
| 465 | */ |
| 466 | ip6_table_bind_callback_t *cb; |
| 467 | vec_foreach (cb, ip6_main.table_bind_callbacks) |
| 468 | cb->function (&ip6_main, cb->function_opaque, |
| 469 | sw_if_index, |
| 470 | fib_index, |
| 471 | ip6_main.fib_index_by_sw_if_index[sw_if_index]); |
| 472 | |
| 473 | if (0 == table_id) |
| 474 | { |
| 475 | /* reset back to default */ |
| 476 | if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index]) |
| 477 | fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index], |
| 478 | FIB_PROTOCOL_IP6, src); |
| 479 | if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index]) |
| 480 | mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index |
| 481 | [sw_if_index], FIB_PROTOCOL_IP6, msrc); |
| 482 | |
| 483 | } |
| 484 | else |
| 485 | { |
| 486 | /* we need to lock the table now it's inuse */ |
| 487 | fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src); |
| 488 | mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc); |
| 489 | } |
| 490 | |
| 491 | ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index; |
| 492 | ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index; |
| 493 | } |
| 494 | else |
| 495 | { |
| 496 | /* |
| 497 | * If the interface already has in IP address, then a change int |
| 498 | * VRF is not allowed. The IP address applied must first be removed. |
| 499 | * We do not do that automatically here, since VPP has no knowledge |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 500 | * of whether those subnets are valid in the destination VRF. |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 501 | */ |
| 502 | /* *INDENT-OFF* */ |
| 503 | foreach_ip_interface_address (&ip4_main.lookup_main, |
| 504 | ia, sw_if_index, |
| 505 | 1 /* honor unnumbered */ , |
| 506 | ({ |
| 507 | return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE); |
| 508 | })); |
| 509 | /* *INDENT-ON* */ |
| 510 | |
| 511 | vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index); |
| 512 | vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index); |
| 513 | |
| 514 | /* |
| 515 | * tell those that are interested that the binding is changing. |
| 516 | */ |
| 517 | ip4_table_bind_callback_t *cb; |
| 518 | vec_foreach (cb, ip4_main.table_bind_callbacks) |
| 519 | cb->function (&ip4_main, cb->function_opaque, |
| 520 | sw_if_index, |
| 521 | fib_index, |
| 522 | ip4_main.fib_index_by_sw_if_index[sw_if_index]); |
| 523 | |
| 524 | if (0 == table_id) |
| 525 | { |
| 526 | /* reset back to default */ |
| 527 | if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index]) |
| 528 | fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index], |
| 529 | FIB_PROTOCOL_IP4, src); |
| 530 | if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index]) |
| 531 | mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index |
| 532 | [sw_if_index], FIB_PROTOCOL_IP4, msrc); |
| 533 | |
| 534 | } |
| 535 | else |
| 536 | { |
| 537 | /* we need to lock the table now it's inuse */ |
| 538 | fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, |
| 539 | table_id, src); |
| 540 | |
| 541 | mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, |
| 542 | table_id, msrc); |
| 543 | } |
| 544 | |
| 545 | ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index; |
| 546 | ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index; |
| 547 | } |
| 548 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 549 | return (0); |
| 550 | } |
| 551 | |
Juraj Sloboda | dfc1923 | 2016-12-05 13:20:37 +0100 | [diff] [blame] | 552 | static void |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 553 | send_sw_interface_get_table_reply (vl_api_registration_t * reg, |
Juraj Sloboda | dfc1923 | 2016-12-05 13:20:37 +0100 | [diff] [blame] | 554 | u32 context, int retval, u32 vrf_id) |
| 555 | { |
| 556 | vl_api_sw_interface_get_table_reply_t *mp; |
| 557 | |
| 558 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 559 | memset (mp, 0, sizeof (*mp)); |
| 560 | mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY); |
| 561 | mp->context = context; |
| 562 | mp->retval = htonl (retval); |
| 563 | mp->vrf_id = htonl (vrf_id); |
| 564 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 565 | vl_api_send_msg (reg, (u8 *) mp); |
Juraj Sloboda | dfc1923 | 2016-12-05 13:20:37 +0100 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | static void |
| 569 | vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp) |
| 570 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 571 | vl_api_registration_t *reg; |
Juraj Sloboda | dfc1923 | 2016-12-05 13:20:37 +0100 | [diff] [blame] | 572 | fib_table_t *fib_table = 0; |
| 573 | u32 sw_if_index = ~0; |
| 574 | u32 fib_index = ~0; |
| 575 | u32 table_id = ~0; |
| 576 | fib_protocol_t fib_proto = FIB_PROTOCOL_IP4; |
| 577 | int rv = 0; |
| 578 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 579 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 580 | if (!reg) |
Juraj Sloboda | dfc1923 | 2016-12-05 13:20:37 +0100 | [diff] [blame] | 581 | return; |
| 582 | |
| 583 | VALIDATE_SW_IF_INDEX (mp); |
| 584 | |
| 585 | sw_if_index = ntohl (mp->sw_if_index); |
| 586 | |
| 587 | if (mp->is_ipv6) |
| 588 | fib_proto = FIB_PROTOCOL_IP6; |
| 589 | |
| 590 | fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index); |
| 591 | if (fib_index != ~0) |
| 592 | { |
| 593 | fib_table = fib_table_get (fib_index, fib_proto); |
| 594 | table_id = fib_table->ft_table_id; |
| 595 | } |
| 596 | |
| 597 | BAD_SW_IF_INDEX_LABEL; |
| 598 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 599 | send_sw_interface_get_table_reply (reg, mp->context, rv, table_id); |
Juraj Sloboda | dfc1923 | 2016-12-05 13:20:37 +0100 | [diff] [blame] | 600 | } |
| 601 | |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 602 | static void vl_api_sw_interface_set_unnumbered_t_handler |
| 603 | (vl_api_sw_interface_set_unnumbered_t * mp) |
| 604 | { |
| 605 | vl_api_sw_interface_set_unnumbered_reply_t *rmp; |
| 606 | int rv = 0; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 607 | vnet_main_t *vnm = vnet_get_main (); |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 608 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 609 | u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 610 | |
| 611 | /* |
| 612 | * The API message field names are backwards from |
| 613 | * the underlying data structure names. |
| 614 | * It's not worth changing them now. |
| 615 | */ |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 616 | if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index)) |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 617 | { |
| 618 | rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 619 | goto done; |
| 620 | } |
| 621 | |
| 622 | /* Only check the "use loop0" field when setting the binding */ |
Eyal Bari | 1c82cd4 | 2017-03-14 14:39:51 +0200 | [diff] [blame] | 623 | 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] | 624 | { |
| 625 | rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2; |
| 626 | goto done; |
| 627 | } |
| 628 | |
Neale Ranns | 2ae2bc5 | 2018-03-16 03:22:39 -0700 | [diff] [blame] | 629 | vnet_sw_interface_update_unnumbered (unnumbered_sw_if_index, |
| 630 | sw_if_index, mp->is_add); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 631 | done: |
| 632 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY); |
| 633 | } |
| 634 | |
| 635 | static void |
| 636 | vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t * |
| 637 | mp) |
| 638 | { |
| 639 | vl_api_sw_interface_clear_stats_reply_t *rmp; |
| 640 | |
| 641 | vnet_main_t *vnm = vnet_get_main (); |
| 642 | vnet_interface_main_t *im = &vnm->interface_main; |
| 643 | vlib_simple_counter_main_t *sm; |
| 644 | vlib_combined_counter_main_t *cm; |
| 645 | static vnet_main_t **my_vnet_mains; |
| 646 | int i, j, n_counters; |
| 647 | int rv = 0; |
| 648 | |
| 649 | if (mp->sw_if_index != ~0) |
| 650 | VALIDATE_SW_IF_INDEX (mp); |
| 651 | |
| 652 | vec_reset_length (my_vnet_mains); |
| 653 | |
| 654 | for (i = 0; i < vec_len (vnet_mains); i++) |
| 655 | { |
| 656 | if (vnet_mains[i]) |
| 657 | vec_add1 (my_vnet_mains, vnet_mains[i]); |
| 658 | } |
| 659 | |
| 660 | if (vec_len (vnet_mains) == 0) |
| 661 | vec_add1 (my_vnet_mains, vnm); |
| 662 | |
| 663 | n_counters = vec_len (im->combined_sw_if_counters); |
| 664 | |
| 665 | for (j = 0; j < n_counters; j++) |
| 666 | { |
| 667 | for (i = 0; i < vec_len (my_vnet_mains); i++) |
| 668 | { |
| 669 | im = &my_vnet_mains[i]->interface_main; |
| 670 | cm = im->combined_sw_if_counters + j; |
| 671 | if (mp->sw_if_index == (u32) ~ 0) |
| 672 | vlib_clear_combined_counters (cm); |
| 673 | else |
| 674 | vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index)); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | n_counters = vec_len (im->sw_if_counters); |
| 679 | |
| 680 | for (j = 0; j < n_counters; j++) |
| 681 | { |
| 682 | for (i = 0; i < vec_len (my_vnet_mains); i++) |
| 683 | { |
| 684 | im = &my_vnet_mains[i]->interface_main; |
| 685 | sm = im->sw_if_counters + j; |
| 686 | if (mp->sw_if_index == (u32) ~ 0) |
| 687 | vlib_clear_simple_counters (sm); |
| 688 | else |
| 689 | vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index)); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | BAD_SW_IF_INDEX_LABEL; |
| 694 | |
| 695 | REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY); |
| 696 | } |
| 697 | |
| 698 | #define API_LINK_STATE_EVENT 1 |
| 699 | #define API_ADMIN_UP_DOWN_EVENT 2 |
| 700 | |
| 701 | static int |
| 702 | event_data_cmp (void *a1, void *a2) |
| 703 | { |
| 704 | uword *e1 = a1; |
| 705 | uword *e2 = a2; |
| 706 | |
| 707 | return (word) e1[0] - (word) e2[0]; |
| 708 | } |
| 709 | |
| 710 | static void |
Neale Ranns | a07bd70 | 2017-08-07 07:53:49 -0700 | [diff] [blame] | 711 | send_sw_interface_event (vpe_api_main_t * am, |
Neale Ranns | d292ab1 | 2017-08-15 12:29:48 -0700 | [diff] [blame] | 712 | vpe_client_registration_t * reg, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 713 | vl_api_registration_t * vl_reg, |
| 714 | vnet_sw_interface_t * swif) |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 715 | { |
Neale Ranns | a07bd70 | 2017-08-07 07:53:49 -0700 | [diff] [blame] | 716 | vl_api_sw_interface_event_t *mp; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 717 | vnet_main_t *vnm = am->vnet_main; |
| 718 | |
| 719 | vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, |
| 720 | swif->sw_if_index); |
| 721 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 722 | memset (mp, 0, sizeof (*mp)); |
Neale Ranns | a07bd70 | 2017-08-07 07:53:49 -0700 | [diff] [blame] | 723 | mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 724 | mp->sw_if_index = ntohl (swif->sw_if_index); |
Neale Ranns | d292ab1 | 2017-08-15 12:29:48 -0700 | [diff] [blame] | 725 | mp->client_index = reg->client_index; |
| 726 | mp->pid = reg->client_pid; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 727 | |
| 728 | mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0; |
| 729 | mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0; |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 730 | vl_api_send_msg (vl_reg, (u8 *) mp); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | static uword |
| 734 | link_state_process (vlib_main_t * vm, |
| 735 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
| 736 | { |
| 737 | vpe_api_main_t *vam = &vpe_api_main; |
| 738 | vnet_main_t *vnm = vam->vnet_main; |
| 739 | vnet_sw_interface_t *swif; |
| 740 | uword *event_data = 0; |
| 741 | vpe_client_registration_t *reg; |
| 742 | int i; |
| 743 | u32 prev_sw_if_index; |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 744 | vl_api_registration_t *vl_reg; |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 745 | |
| 746 | vam->link_state_process_up = 1; |
| 747 | |
| 748 | while (1) |
| 749 | { |
| 750 | vlib_process_wait_for_event (vm); |
| 751 | |
| 752 | /* Unified list of changed link or admin state sw_if_indices */ |
| 753 | vlib_process_get_events_with_type |
| 754 | (vm, &event_data, API_LINK_STATE_EVENT); |
| 755 | vlib_process_get_events_with_type |
| 756 | (vm, &event_data, API_ADMIN_UP_DOWN_EVENT); |
| 757 | |
| 758 | /* Sort, so we can eliminate duplicates */ |
| 759 | vec_sort_with_function (event_data, event_data_cmp); |
| 760 | |
| 761 | prev_sw_if_index = ~0; |
| 762 | |
| 763 | for (i = 0; i < vec_len (event_data); i++) |
| 764 | { |
| 765 | /* Only one message per swif */ |
| 766 | if (prev_sw_if_index == event_data[i]) |
| 767 | continue; |
| 768 | prev_sw_if_index = event_data[i]; |
| 769 | |
| 770 | /* *INDENT-OFF* */ |
| 771 | pool_foreach(reg, vam->interface_events_registrations, |
| 772 | ({ |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 773 | vl_reg = vl_api_client_index_to_registration (reg->client_index); |
| 774 | if (vl_reg) |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 775 | { |
| 776 | /* sw_interface may be deleted already */ |
| 777 | if (!pool_is_free_index (vnm->interface_main.sw_interfaces, |
| 778 | event_data[i])) |
| 779 | { |
| 780 | swif = vnet_get_sw_interface (vnm, event_data[i]); |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 781 | send_sw_interface_event (vam, reg, vl_reg, swif); |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | })); |
| 785 | /* *INDENT-ON* */ |
| 786 | } |
| 787 | vec_reset_length (event_data); |
| 788 | } |
| 789 | |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index, |
| 794 | u32 flags); |
| 795 | static clib_error_t *admin_up_down_function (vnet_main_t * vm, |
| 796 | u32 hw_if_index, u32 flags); |
| 797 | |
| 798 | /* *INDENT-OFF* */ |
| 799 | VLIB_REGISTER_NODE (link_state_process_node,static) = { |
| 800 | .function = link_state_process, |
| 801 | .type = VLIB_NODE_TYPE_PROCESS, |
| 802 | .name = "vpe-link-state-process", |
| 803 | }; |
| 804 | /* *INDENT-ON* */ |
| 805 | |
| 806 | VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function); |
| 807 | VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function); |
| 808 | |
| 809 | static clib_error_t * |
| 810 | link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags) |
| 811 | { |
| 812 | vpe_api_main_t *vam = &vpe_api_main; |
| 813 | vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index); |
| 814 | |
| 815 | if (vam->link_state_process_up) |
| 816 | vlib_process_signal_event (vam->vlib_main, |
| 817 | link_state_process_node.index, |
| 818 | API_LINK_STATE_EVENT, hi->sw_if_index); |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | static clib_error_t * |
| 823 | admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags) |
| 824 | { |
| 825 | vpe_api_main_t *vam = &vpe_api_main; |
| 826 | |
| 827 | /* |
| 828 | * Note: it's perfectly fair to set a subif admin up / admin down. |
| 829 | * Note the subtle distinction between this routine and the previous |
| 830 | * routine. |
| 831 | */ |
| 832 | if (vam->link_state_process_up) |
| 833 | vlib_process_signal_event (vam->vlib_main, |
| 834 | link_state_process_node.index, |
| 835 | API_ADMIN_UP_DOWN_EVENT, sw_if_index); |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | static void vl_api_sw_interface_tag_add_del_t_handler |
| 840 | (vl_api_sw_interface_tag_add_del_t * mp) |
| 841 | { |
| 842 | vnet_main_t *vnm = vnet_get_main (); |
| 843 | vl_api_sw_interface_tag_add_del_reply_t *rmp; |
| 844 | int rv = 0; |
| 845 | u8 *tag; |
| 846 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 847 | |
| 848 | VALIDATE_SW_IF_INDEX (mp); |
| 849 | |
| 850 | if (mp->is_add) |
| 851 | { |
| 852 | if (mp->tag[0] == 0) |
| 853 | { |
| 854 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 855 | goto out; |
| 856 | } |
| 857 | |
| 858 | mp->tag[ARRAY_LEN (mp->tag) - 1] = 0; |
| 859 | tag = format (0, "%s%c", mp->tag, 0); |
| 860 | vnet_set_sw_interface_tag (vnm, tag, sw_if_index); |
| 861 | } |
| 862 | else |
| 863 | vnet_clear_sw_interface_tag (vnm, sw_if_index); |
| 864 | |
| 865 | BAD_SW_IF_INDEX_LABEL; |
| 866 | out: |
| 867 | REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY); |
| 868 | } |
| 869 | |
Jon Loeliger | 10c273b | 2017-03-30 08:39:33 -0500 | [diff] [blame] | 870 | static void vl_api_sw_interface_set_mac_address_t_handler |
| 871 | (vl_api_sw_interface_set_mac_address_t * mp) |
| 872 | { |
| 873 | vl_api_sw_interface_set_mac_address_reply_t *rmp; |
| 874 | vnet_main_t *vnm = vnet_get_main (); |
| 875 | u32 sw_if_index = ntohl (mp->sw_if_index); |
Neale Ranns | d867a7c | 2017-10-04 02:29:07 -0700 | [diff] [blame] | 876 | vnet_sw_interface_t *si; |
Jon Loeliger | 10c273b | 2017-03-30 08:39:33 -0500 | [diff] [blame] | 877 | clib_error_t *error; |
| 878 | int rv = 0; |
| 879 | |
| 880 | VALIDATE_SW_IF_INDEX (mp); |
| 881 | |
Neale Ranns | d867a7c | 2017-10-04 02:29:07 -0700 | [diff] [blame] | 882 | si = vnet_get_sw_interface (vnm, sw_if_index); |
John Lo | 62fcc0a | 2017-10-31 14:31:10 -0400 | [diff] [blame] | 883 | error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index, |
| 884 | mp->mac_address); |
Jon Loeliger | 10c273b | 2017-03-30 08:39:33 -0500 | [diff] [blame] | 885 | if (error) |
| 886 | { |
| 887 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 888 | clib_error_report (error); |
| 889 | goto out; |
| 890 | } |
| 891 | |
| 892 | BAD_SW_IF_INDEX_LABEL; |
| 893 | out: |
| 894 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY); |
| 895 | } |
| 896 | |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 897 | static void vl_api_sw_interface_get_mac_address_t_handler |
| 898 | (vl_api_sw_interface_get_mac_address_t * mp) |
| 899 | { |
| 900 | vl_api_sw_interface_get_mac_address_reply_t *rmp; |
| 901 | vl_api_registration_t *reg; |
| 902 | vnet_main_t *vnm = vnet_get_main (); |
| 903 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 904 | vnet_sw_interface_t *si; |
| 905 | ethernet_interface_t *eth_if = 0; |
| 906 | int rv = 0; |
| 907 | |
| 908 | VALIDATE_SW_IF_INDEX (mp); |
| 909 | |
| 910 | si = vnet_get_sup_sw_interface (vnm, sw_if_index); |
| 911 | if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE) |
| 912 | eth_if = ethernet_get_interface (ðernet_main, si->hw_if_index); |
| 913 | |
| 914 | BAD_SW_IF_INDEX_LABEL; |
| 915 | |
| 916 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 917 | if (!reg) |
| 918 | return; |
| 919 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 920 | rmp->_vl_msg_id = htons (VL_API_SW_INTERFACE_GET_MAC_ADDRESS_REPLY); |
| 921 | rmp->context = mp->context; |
| 922 | rmp->retval = htonl (rv); |
| 923 | if (!rv && eth_if) |
| 924 | memcpy (rmp->mac_address, eth_if->address, 6); |
| 925 | vl_api_send_msg (reg, (u8 *) rmp); |
| 926 | } |
| 927 | |
Steven | ad8015b | 2017-10-29 22:10:46 -0700 | [diff] [blame] | 928 | static void vl_api_sw_interface_set_rx_mode_t_handler |
| 929 | (vl_api_sw_interface_set_rx_mode_t * mp) |
| 930 | { |
| 931 | vl_api_sw_interface_set_rx_mode_reply_t *rmp; |
| 932 | vnet_main_t *vnm = vnet_get_main (); |
| 933 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 934 | vnet_sw_interface_t *si; |
| 935 | clib_error_t *error; |
| 936 | int rv = 0; |
| 937 | |
| 938 | VALIDATE_SW_IF_INDEX (mp); |
| 939 | |
| 940 | si = vnet_get_sw_interface (vnm, sw_if_index); |
Mohsin Kazmi | 54f7c51 | 2018-08-23 18:28:11 +0200 | [diff] [blame] | 941 | if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE) |
| 942 | { |
| 943 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 944 | goto bad_sw_if_index; |
| 945 | } |
| 946 | |
Steven | ad8015b | 2017-10-29 22:10:46 -0700 | [diff] [blame] | 947 | error = set_hw_interface_change_rx_mode (vnm, si->hw_if_index, |
| 948 | mp->queue_id_valid, |
| 949 | ntohl (mp->queue_id), mp->mode); |
| 950 | if (error) |
| 951 | { |
| 952 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 953 | clib_error_report (error); |
| 954 | goto out; |
| 955 | } |
| 956 | |
| 957 | BAD_SW_IF_INDEX_LABEL; |
| 958 | out: |
| 959 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_MODE_REPLY); |
| 960 | } |
| 961 | |
Mohsin Kazmi | f0b42f4 | 2018-09-10 18:11:00 +0200 | [diff] [blame] | 962 | static void |
| 963 | send_interface_rx_placement_details (vpe_api_main_t * am, |
| 964 | vl_api_registration_t * rp, |
| 965 | u32 sw_if_index, u32 worker_id, |
| 966 | u32 queue_id, u8 mode, u32 context) |
| 967 | { |
| 968 | vl_api_sw_interface_rx_placement_details_t *mp; |
| 969 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 970 | memset (mp, 0, sizeof (*mp)); |
| 971 | |
| 972 | mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_RX_PLACEMENT_DETAILS); |
| 973 | mp->sw_if_index = htonl (sw_if_index); |
| 974 | mp->queue_id = htonl (queue_id); |
| 975 | mp->worker_id = htonl (worker_id); |
| 976 | mp->mode = mode; |
| 977 | mp->context = context; |
| 978 | |
| 979 | vl_api_send_msg (rp, (u8 *) mp); |
| 980 | } |
| 981 | |
| 982 | static void vl_api_sw_interface_rx_placement_dump_t_handler |
| 983 | (vl_api_sw_interface_rx_placement_dump_t * mp) |
| 984 | { |
| 985 | vnet_main_t *vnm = vnet_get_main (); |
| 986 | vpe_api_main_t *am = &vpe_api_main; |
| 987 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 988 | vl_api_registration_t *reg; |
| 989 | |
| 990 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 991 | if (!reg) |
| 992 | return; |
| 993 | |
| 994 | if (sw_if_index == ~0) |
| 995 | { |
| 996 | vnet_device_input_runtime_t *rt; |
| 997 | vnet_device_and_queue_t *dq; |
| 998 | vlib_node_t *pn = vlib_get_node_by_name (am->vlib_main, |
| 999 | (u8 *) "device-input"); |
| 1000 | uword si; |
| 1001 | int index = 0; |
| 1002 | |
| 1003 | /* *INDENT-OFF* */ |
| 1004 | foreach_vlib_main (({ |
| 1005 | clib_bitmap_foreach (si, pn->sibling_bitmap, |
| 1006 | ({ |
| 1007 | rt = vlib_node_get_runtime_data (this_vlib_main, si); |
| 1008 | vec_foreach (dq, rt->devices_and_queues) |
| 1009 | { |
| 1010 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, |
| 1011 | dq->hw_if_index); |
| 1012 | send_interface_rx_placement_details (am, reg, hw->sw_if_index, index, |
| 1013 | dq->queue_id, dq->mode, mp->context); |
| 1014 | } |
| 1015 | })); |
| 1016 | index++; |
| 1017 | })); |
| 1018 | /* *INDENT-ON* */ |
| 1019 | } |
| 1020 | else |
| 1021 | { |
| 1022 | int i; |
| 1023 | vnet_sw_interface_t *si; |
| 1024 | |
| 1025 | if (!vnet_sw_if_index_is_api_valid (sw_if_index)) |
| 1026 | { |
| 1027 | clib_warning ("sw_if_index %u does not exist", sw_if_index); |
| 1028 | goto bad_sw_if_index; |
| 1029 | } |
| 1030 | |
| 1031 | si = vnet_get_sw_interface (vnm, sw_if_index); |
| 1032 | if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE) |
| 1033 | { |
| 1034 | clib_warning ("interface type is not HARDWARE! P2P, PIPE and SUB" |
| 1035 | " interfaces are not supported"); |
| 1036 | goto bad_sw_if_index; |
| 1037 | } |
| 1038 | |
| 1039 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, si->hw_if_index); |
| 1040 | |
| 1041 | for (i = 0; i < vec_len (hw->dq_runtime_index_by_queue); i++) |
| 1042 | { |
| 1043 | send_interface_rx_placement_details (am, reg, hw->sw_if_index, |
| 1044 | hw->input_node_thread_index_by_queue |
| 1045 | [i], i, |
| 1046 | hw->rx_mode_by_queue[i], |
| 1047 | mp->context); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | BAD_SW_IF_INDEX_LABEL; |
| 1052 | } |
| 1053 | |
Mohsin Kazmi | 54f7c51 | 2018-08-23 18:28:11 +0200 | [diff] [blame] | 1054 | static void vl_api_sw_interface_set_rx_placement_t_handler |
| 1055 | (vl_api_sw_interface_set_rx_placement_t * mp) |
| 1056 | { |
| 1057 | vl_api_sw_interface_set_rx_placement_reply_t *rmp; |
| 1058 | vnet_main_t *vnm = vnet_get_main (); |
| 1059 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 1060 | vnet_sw_interface_t *si; |
| 1061 | clib_error_t *error = 0; |
| 1062 | int rv = 0; |
| 1063 | |
| 1064 | VALIDATE_SW_IF_INDEX (mp); |
| 1065 | |
| 1066 | si = vnet_get_sw_interface (vnm, sw_if_index); |
| 1067 | if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE) |
| 1068 | { |
| 1069 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1070 | goto bad_sw_if_index; |
| 1071 | } |
| 1072 | |
| 1073 | error = set_hw_interface_rx_placement (si->hw_if_index, |
| 1074 | ntohl (mp->queue_id), |
| 1075 | ntohl (mp->worker_id), mp->is_main); |
| 1076 | if (error) |
| 1077 | { |
| 1078 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 1079 | clib_error_report (error); |
| 1080 | goto out; |
| 1081 | } |
| 1082 | |
| 1083 | BAD_SW_IF_INDEX_LABEL; |
| 1084 | out: |
| 1085 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_PLACEMENT_REPLY); |
| 1086 | } |
| 1087 | |
Neale Ranns | b8d4481 | 2017-11-10 06:53:54 -0800 | [diff] [blame] | 1088 | static void |
| 1089 | vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp) |
| 1090 | { |
| 1091 | vl_api_create_vlan_subif_reply_t *rmp; |
| 1092 | vnet_main_t *vnm = vnet_get_main (); |
| 1093 | u32 sw_if_index = (u32) ~ 0; |
| 1094 | vnet_hw_interface_t *hi; |
| 1095 | int rv = 0; |
| 1096 | u32 id; |
| 1097 | vnet_sw_interface_t template; |
| 1098 | uword *p; |
| 1099 | vnet_interface_main_t *im = &vnm->interface_main; |
| 1100 | u64 sup_and_sub_key; |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 1101 | vl_api_registration_t *reg; |
Neale Ranns | b8d4481 | 2017-11-10 06:53:54 -0800 | [diff] [blame] | 1102 | clib_error_t *error; |
| 1103 | |
| 1104 | VALIDATE_SW_IF_INDEX (mp); |
| 1105 | |
| 1106 | hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index)); |
| 1107 | |
| 1108 | if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE) |
| 1109 | { |
| 1110 | rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED; |
| 1111 | goto out; |
| 1112 | } |
| 1113 | |
| 1114 | id = ntohl (mp->vlan_id); |
| 1115 | if (id == 0 || id > 4095) |
| 1116 | { |
| 1117 | rv = VNET_API_ERROR_INVALID_VLAN; |
| 1118 | goto out; |
| 1119 | } |
| 1120 | |
| 1121 | sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id; |
| 1122 | |
| 1123 | p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key); |
| 1124 | if (p) |
| 1125 | { |
| 1126 | rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS; |
| 1127 | goto out; |
| 1128 | } |
| 1129 | |
| 1130 | memset (&template, 0, sizeof (template)); |
| 1131 | template.type = VNET_SW_INTERFACE_TYPE_SUB; |
| 1132 | template.sup_sw_if_index = hi->sw_if_index; |
| 1133 | template.sub.id = id; |
| 1134 | template.sub.eth.raw_flags = 0; |
| 1135 | template.sub.eth.flags.one_tag = 1; |
| 1136 | template.sub.eth.outer_vlan_id = id; |
| 1137 | template.sub.eth.flags.exact_match = 1; |
| 1138 | |
| 1139 | error = vnet_create_sw_interface (vnm, &template, &sw_if_index); |
| 1140 | if (error) |
| 1141 | { |
| 1142 | clib_error_report (error); |
| 1143 | rv = VNET_API_ERROR_INVALID_REGISTRATION; |
| 1144 | goto out; |
| 1145 | } |
| 1146 | |
| 1147 | u64 *kp = clib_mem_alloc (sizeof (*kp)); |
| 1148 | *kp = sup_and_sub_key; |
| 1149 | |
| 1150 | hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index); |
| 1151 | hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index); |
| 1152 | |
| 1153 | BAD_SW_IF_INDEX_LABEL; |
| 1154 | |
| 1155 | out: |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 1156 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 1157 | if (!reg) |
Neale Ranns | b8d4481 | 2017-11-10 06:53:54 -0800 | [diff] [blame] | 1158 | return; |
| 1159 | |
| 1160 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 1161 | rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY); |
| 1162 | rmp->context = mp->context; |
| 1163 | rmp->retval = htonl (rv); |
| 1164 | rmp->sw_if_index = htonl (sw_if_index); |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 1165 | vl_api_send_msg (reg, (u8 *) rmp); |
Neale Ranns | b8d4481 | 2017-11-10 06:53:54 -0800 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | static void |
| 1169 | vl_api_create_subif_t_handler (vl_api_create_subif_t * mp) |
| 1170 | { |
| 1171 | vl_api_create_subif_reply_t *rmp; |
| 1172 | vnet_main_t *vnm = vnet_get_main (); |
| 1173 | u32 sw_if_index = ~0; |
| 1174 | int rv = 0; |
| 1175 | u32 sub_id; |
| 1176 | vnet_sw_interface_t *si; |
| 1177 | vnet_hw_interface_t *hi; |
| 1178 | vnet_sw_interface_t template; |
| 1179 | uword *p; |
| 1180 | vnet_interface_main_t *im = &vnm->interface_main; |
| 1181 | u64 sup_and_sub_key; |
| 1182 | clib_error_t *error; |
| 1183 | |
| 1184 | VALIDATE_SW_IF_INDEX (mp); |
| 1185 | |
| 1186 | si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index)); |
| 1187 | hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index)); |
| 1188 | |
| 1189 | if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE) |
| 1190 | { |
| 1191 | rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED; |
| 1192 | goto out; |
| 1193 | } |
| 1194 | |
| 1195 | sw_if_index = si->sw_if_index; |
| 1196 | sub_id = ntohl (mp->sub_id); |
| 1197 | |
| 1198 | sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id; |
| 1199 | |
| 1200 | p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key); |
| 1201 | if (p) |
| 1202 | { |
| 1203 | if (CLIB_DEBUG > 0) |
| 1204 | clib_warning ("sup sw_if_index %d, sub id %d already exists\n", |
| 1205 | sw_if_index, sub_id); |
| 1206 | rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS; |
| 1207 | goto out; |
| 1208 | } |
| 1209 | |
| 1210 | memset (&template, 0, sizeof (template)); |
| 1211 | template.type = VNET_SW_INTERFACE_TYPE_SUB; |
| 1212 | template.sup_sw_if_index = sw_if_index; |
| 1213 | template.sub.id = sub_id; |
| 1214 | template.sub.eth.flags.no_tags = mp->no_tags; |
| 1215 | template.sub.eth.flags.one_tag = mp->one_tag; |
| 1216 | template.sub.eth.flags.two_tags = mp->two_tags; |
| 1217 | template.sub.eth.flags.dot1ad = mp->dot1ad; |
| 1218 | template.sub.eth.flags.exact_match = mp->exact_match; |
| 1219 | template.sub.eth.flags.default_sub = mp->default_sub; |
| 1220 | template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any; |
| 1221 | template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any; |
| 1222 | template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id); |
| 1223 | template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id); |
| 1224 | |
| 1225 | error = vnet_create_sw_interface (vnm, &template, &sw_if_index); |
| 1226 | if (error) |
| 1227 | { |
| 1228 | clib_error_report (error); |
| 1229 | rv = VNET_API_ERROR_SUBIF_CREATE_FAILED; |
| 1230 | goto out; |
| 1231 | } |
| 1232 | |
| 1233 | u64 *kp = clib_mem_alloc (sizeof (*kp)); |
| 1234 | *kp = sup_and_sub_key; |
| 1235 | |
| 1236 | hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index); |
| 1237 | hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index); |
| 1238 | |
| 1239 | BAD_SW_IF_INDEX_LABEL; |
| 1240 | |
| 1241 | out: |
| 1242 | |
| 1243 | /* *INDENT-OFF* */ |
| 1244 | REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY, |
| 1245 | ({ |
| 1246 | rmp->sw_if_index = ntohl(sw_if_index); |
| 1247 | })); |
| 1248 | /* *INDENT-ON* */ |
| 1249 | } |
| 1250 | |
| 1251 | static void |
| 1252 | vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp) |
| 1253 | { |
| 1254 | vl_api_delete_subif_reply_t *rmp; |
| 1255 | int rv; |
| 1256 | |
| 1257 | rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index)); |
| 1258 | |
| 1259 | REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY); |
| 1260 | } |
| 1261 | |
| 1262 | static void |
| 1263 | vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t * |
| 1264 | mp) |
| 1265 | { |
| 1266 | vl_api_interface_name_renumber_reply_t *rmp; |
| 1267 | int rv = 0; |
| 1268 | |
| 1269 | VALIDATE_SW_IF_INDEX (mp); |
| 1270 | |
| 1271 | rv = vnet_interface_name_renumber |
| 1272 | (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance)); |
| 1273 | |
| 1274 | BAD_SW_IF_INDEX_LABEL; |
| 1275 | |
| 1276 | REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY); |
| 1277 | } |
| 1278 | |
| 1279 | static void |
| 1280 | vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp) |
| 1281 | { |
| 1282 | vl_api_create_loopback_reply_t *rmp; |
| 1283 | u32 sw_if_index; |
| 1284 | int rv; |
| 1285 | |
| 1286 | rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address, 0, 0); |
| 1287 | |
| 1288 | /* *INDENT-OFF* */ |
| 1289 | REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY, |
| 1290 | ({ |
| 1291 | rmp->sw_if_index = ntohl (sw_if_index); |
| 1292 | })); |
| 1293 | /* *INDENT-ON* */ |
| 1294 | } |
| 1295 | |
| 1296 | static void vl_api_create_loopback_instance_t_handler |
| 1297 | (vl_api_create_loopback_instance_t * mp) |
| 1298 | { |
| 1299 | vl_api_create_loopback_instance_reply_t *rmp; |
| 1300 | u32 sw_if_index; |
| 1301 | u8 is_specified = mp->is_specified; |
| 1302 | u32 user_instance = ntohl (mp->user_instance); |
| 1303 | int rv; |
| 1304 | |
| 1305 | rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address, |
| 1306 | is_specified, user_instance); |
| 1307 | |
| 1308 | /* *INDENT-OFF* */ |
| 1309 | REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY, |
| 1310 | ({ |
| 1311 | rmp->sw_if_index = ntohl (sw_if_index); |
| 1312 | })); |
| 1313 | /* *INDENT-ON* */ |
| 1314 | } |
| 1315 | |
| 1316 | static void |
| 1317 | vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp) |
| 1318 | { |
| 1319 | vl_api_delete_loopback_reply_t *rmp; |
| 1320 | u32 sw_if_index; |
| 1321 | int rv; |
| 1322 | |
| 1323 | sw_if_index = ntohl (mp->sw_if_index); |
| 1324 | rv = vnet_delete_loopback_interface (sw_if_index); |
| 1325 | |
| 1326 | REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY); |
| 1327 | } |
| 1328 | |
Neale Ranns | 6f4a6be | 2018-03-16 16:26:21 -0700 | [diff] [blame] | 1329 | static void |
| 1330 | vl_api_collect_detailed_interface_stats_t_handler |
| 1331 | (vl_api_collect_detailed_interface_stats_t * mp) |
| 1332 | { |
| 1333 | vl_api_collect_detailed_interface_stats_reply_t *rmp; |
| 1334 | int rv = 0; |
| 1335 | |
Neale Ranns | 871dc42 | 2018-03-29 01:28:09 -0700 | [diff] [blame] | 1336 | rv = |
| 1337 | vnet_sw_interface_stats_collect_enable_disable (ntohl (mp->sw_if_index), |
| 1338 | mp->enable_disable); |
Neale Ranns | 6f4a6be | 2018-03-16 16:26:21 -0700 | [diff] [blame] | 1339 | |
| 1340 | REPLY_MACRO (VL_API_COLLECT_DETAILED_INTERFACE_STATS_REPLY); |
| 1341 | } |
| 1342 | |
Dave Barach | aff7077 | 2016-10-31 11:59:07 -0400 | [diff] [blame] | 1343 | /* |
| 1344 | * vpe_api_hookup |
| 1345 | * Add vpe's API message handlers to the table. |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 1346 | * vlib has already mapped shared memory and |
Dave Barach | aff7077 | 2016-10-31 11:59:07 -0400 | [diff] [blame] | 1347 | * added the client registration handlers. |
| 1348 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 1349 | */ |
| 1350 | #define vl_msg_name_crc_list |
Dave Barach | b5e8a77 | 2016-12-06 12:04:42 -0500 | [diff] [blame] | 1351 | #include <vnet/interface.api.h> |
Dave Barach | aff7077 | 2016-10-31 11:59:07 -0400 | [diff] [blame] | 1352 | #undef vl_msg_name_crc_list |
| 1353 | |
| 1354 | static void |
| 1355 | setup_message_id_table (api_main_t * am) |
| 1356 | { |
| 1357 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 1358 | foreach_vl_msg_name_crc_interface; |
| 1359 | #undef _ |
| 1360 | } |
| 1361 | |
Dave Barach | 6d963c2 | 2016-12-05 09:50:05 -0500 | [diff] [blame] | 1362 | pub_sub_handler (interface_events, INTERFACE_EVENTS); |
| 1363 | |
Dave Barach | aff7077 | 2016-10-31 11:59:07 -0400 | [diff] [blame] | 1364 | static clib_error_t * |
| 1365 | interface_api_hookup (vlib_main_t * vm) |
| 1366 | { |
| 1367 | api_main_t *am = &api_main; |
| 1368 | |
| 1369 | #define _(N,n) \ |
| 1370 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 1371 | vl_api_##n##_t_handler, \ |
| 1372 | vl_noop_handler, \ |
| 1373 | vl_api_##n##_t_endian, \ |
| 1374 | vl_api_##n##_t_print, \ |
| 1375 | sizeof(vl_api_##n##_t), 1); |
| 1376 | foreach_vpe_api_msg; |
| 1377 | #undef _ |
| 1378 | |
| 1379 | /* |
| 1380 | * Set up the (msg_name, crc, message-id) table |
| 1381 | */ |
| 1382 | setup_message_id_table (am); |
| 1383 | |
| 1384 | return 0; |
| 1385 | } |
| 1386 | |
| 1387 | VLIB_API_INIT_FUNCTION (interface_api_hookup); |
| 1388 | |
| 1389 | /* |
| 1390 | * fd.io coding-style-patch-verification: ON |
| 1391 | * |
| 1392 | * Local Variables: |
| 1393 | * eval: (c-set-style "gnu") |
| 1394 | * End: |
| 1395 | */ |