Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 SUSE LLC. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | *------------------------------------------------------------------ |
| 15 | */ |
| 16 | |
| 17 | #include <vnet/vnet.h> |
| 18 | #include <vlibmemory/api.h> |
| 19 | |
| 20 | #include <vnet/interface.h> |
| 21 | #include <vnet/api_errno.h> |
| 22 | #include <vnet/feature/feature.h> |
| 23 | #include <vnet/geneve/geneve.h> |
| 24 | #include <vnet/fib/fib_table.h> |
Jakub Grajciar | 2d3282e | 2019-10-01 12:04:56 +0200 | [diff] [blame] | 25 | #include <vnet/ip/ip_types_api.h> |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 26 | |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame^] | 27 | #include <vnet/geneve/geneve.api_enum.h> |
| 28 | #include <vnet/geneve/geneve.api_types.h> |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 29 | |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame^] | 30 | #define REPLY_MSG_ID_BASE gm->msg_id_base |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 31 | #include <vlibapi/api_helper_macros.h> |
| 32 | |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 33 | static void |
| 34 | vl_api_sw_interface_set_geneve_bypass_t_handler |
| 35 | (vl_api_sw_interface_set_geneve_bypass_t * mp) |
| 36 | { |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame^] | 37 | geneve_main_t *gm = &geneve_main; |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 38 | vl_api_sw_interface_set_geneve_bypass_reply_t *rmp; |
| 39 | int rv = 0; |
| 40 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 41 | |
| 42 | VALIDATE_SW_IF_INDEX (mp); |
| 43 | |
| 44 | vnet_int_geneve_bypass_mode (sw_if_index, mp->is_ipv6, mp->enable); |
| 45 | BAD_SW_IF_INDEX_LABEL; |
| 46 | |
| 47 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_GENEVE_BYPASS_REPLY); |
| 48 | } |
| 49 | |
| 50 | static void vl_api_geneve_add_del_tunnel_t_handler |
| 51 | (vl_api_geneve_add_del_tunnel_t * mp) |
| 52 | { |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame^] | 53 | geneve_main_t *gm = &geneve_main; |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 54 | vl_api_geneve_add_del_tunnel_reply_t *rmp; |
| 55 | int rv = 0; |
| 56 | ip4_main_t *im = &ip4_main; |
| 57 | |
| 58 | uword *p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id)); |
| 59 | if (!p) |
| 60 | { |
| 61 | rv = VNET_API_ERROR_NO_SUCH_FIB; |
| 62 | goto out; |
| 63 | } |
| 64 | |
| 65 | vnet_geneve_add_del_tunnel_args_t a = { |
| 66 | .is_add = mp->is_add, |
Jakub Grajciar | 2d3282e | 2019-10-01 12:04:56 +0200 | [diff] [blame] | 67 | .is_ip6 = mp->remote_address.af, |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 68 | .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index), |
| 69 | .encap_fib_index = p[0], |
| 70 | .decap_next_index = ntohl (mp->decap_next_index), |
| 71 | .vni = ntohl (mp->vni), |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 72 | }; |
| 73 | |
Jakub Grajciar | 2d3282e | 2019-10-01 12:04:56 +0200 | [diff] [blame] | 74 | ip_address_decode (&mp->remote_address, &a.remote); |
| 75 | ip_address_decode (&mp->local_address, &a.local); |
| 76 | |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 77 | /* Check src & dst are different */ |
| 78 | if (ip46_address_cmp (&a.remote, &a.local) == 0) |
| 79 | { |
| 80 | rv = VNET_API_ERROR_SAME_SRC_DST; |
| 81 | goto out; |
| 82 | } |
| 83 | if (ip46_address_is_multicast (&a.remote) && |
| 84 | !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index)) |
| 85 | { |
| 86 | rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 87 | goto out; |
| 88 | } |
| 89 | |
| 90 | u32 sw_if_index = ~0; |
| 91 | rv = vnet_geneve_add_del_tunnel (&a, &sw_if_index); |
| 92 | |
| 93 | out: |
| 94 | /* *INDENT-OFF* */ |
| 95 | REPLY_MACRO2(VL_API_GENEVE_ADD_DEL_TUNNEL_REPLY, |
| 96 | ({ |
| 97 | rmp->sw_if_index = ntohl (sw_if_index); |
| 98 | })); |
| 99 | /* *INDENT-ON* */ |
| 100 | } |
| 101 | |
Ole Troan | 7fc88cf | 2020-06-17 22:57:13 +0200 | [diff] [blame] | 102 | static void vl_api_geneve_add_del_tunnel2_t_handler |
| 103 | (vl_api_geneve_add_del_tunnel2_t * mp) |
| 104 | { |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame^] | 105 | geneve_main_t *gm = &geneve_main; |
Ole Troan | 7fc88cf | 2020-06-17 22:57:13 +0200 | [diff] [blame] | 106 | vl_api_geneve_add_del_tunnel2_reply_t *rmp; |
| 107 | int rv = 0; |
| 108 | ip4_main_t *im = &ip4_main; |
| 109 | |
| 110 | uword *p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id)); |
| 111 | if (!p) |
| 112 | { |
| 113 | rv = VNET_API_ERROR_NO_SUCH_FIB; |
| 114 | goto out; |
| 115 | } |
| 116 | |
| 117 | vnet_geneve_add_del_tunnel_args_t a = { |
| 118 | .is_add = mp->is_add, |
| 119 | .is_ip6 = mp->remote_address.af, |
| 120 | .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index), |
| 121 | .encap_fib_index = p[0], |
| 122 | .decap_next_index = ntohl (mp->decap_next_index), |
| 123 | .vni = ntohl (mp->vni), |
| 124 | .l3_mode = mp->l3_mode, |
| 125 | }; |
| 126 | |
| 127 | ip_address_decode (&mp->remote_address, &a.remote); |
| 128 | ip_address_decode (&mp->local_address, &a.local); |
| 129 | |
| 130 | /* Check src & dst are different */ |
| 131 | if (ip46_address_cmp (&a.remote, &a.local) == 0) |
| 132 | { |
| 133 | rv = VNET_API_ERROR_SAME_SRC_DST; |
| 134 | goto out; |
| 135 | } |
| 136 | if (ip46_address_is_multicast (&a.remote) && |
| 137 | !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index)) |
| 138 | { |
| 139 | rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 140 | goto out; |
| 141 | } |
| 142 | |
| 143 | u32 sw_if_index = ~0; |
| 144 | rv = vnet_geneve_add_del_tunnel (&a, &sw_if_index); |
| 145 | |
| 146 | out: |
| 147 | /* *INDENT-OFF* */ |
| 148 | REPLY_MACRO2(VL_API_GENEVE_ADD_DEL_TUNNEL2_REPLY, |
| 149 | ({ |
| 150 | rmp->sw_if_index = ntohl (sw_if_index); |
| 151 | })); |
| 152 | /* *INDENT-ON* */ |
| 153 | } |
| 154 | |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 155 | static void send_geneve_tunnel_details |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 156 | (geneve_tunnel_t * t, vl_api_registration_t * reg, u32 context) |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 157 | { |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame^] | 158 | geneve_main_t *gm = &geneve_main; |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 159 | vl_api_geneve_tunnel_details_t *rmp; |
| 160 | ip4_main_t *im4 = &ip4_main; |
| 161 | ip6_main_t *im6 = &ip6_main; |
| 162 | u8 is_ipv6 = !ip46_address_is_ip4 (&t->remote); |
| 163 | |
| 164 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 165 | clib_memset (rmp, 0, sizeof (*rmp)); |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame^] | 166 | rmp->_vl_msg_id = ntohs (VL_API_GENEVE_TUNNEL_DETAILS + gm->msg_id_base); |
Jakub Grajciar | 2d3282e | 2019-10-01 12:04:56 +0200 | [diff] [blame] | 167 | ip_address_encode (&t->local, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, |
| 168 | &rmp->src_address); |
| 169 | ip_address_encode (&t->remote, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, |
| 170 | &rmp->dst_address); |
| 171 | rmp->encap_vrf_id = |
| 172 | htonl (is_ipv6 ? im6->fibs[t->encap_fib_index]. |
| 173 | ft_table_id : im4->fibs[t->encap_fib_index].ft_table_id); |
| 174 | |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 175 | rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index); |
| 176 | rmp->vni = htonl (t->vni); |
| 177 | rmp->decap_next_index = htonl (t->decap_next_index); |
| 178 | rmp->sw_if_index = htonl (t->sw_if_index); |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 179 | rmp->context = context; |
| 180 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 181 | vl_api_send_msg (reg, (u8 *) rmp); |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static void vl_api_geneve_tunnel_dump_t_handler |
| 185 | (vl_api_geneve_tunnel_dump_t * mp) |
| 186 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 187 | vl_api_registration_t *reg; |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 188 | geneve_main_t *vxm = &geneve_main; |
| 189 | geneve_tunnel_t *t; |
| 190 | u32 sw_if_index; |
| 191 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 192 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 193 | if (!reg) |
| 194 | return; |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 195 | |
| 196 | sw_if_index = ntohl (mp->sw_if_index); |
| 197 | |
| 198 | if (~0 == sw_if_index) |
| 199 | { |
| 200 | /* *INDENT-OFF* */ |
| 201 | pool_foreach (t, vxm->tunnels, |
| 202 | ({ |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 203 | send_geneve_tunnel_details(t, reg, mp->context); |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 204 | })); |
| 205 | /* *INDENT-ON* */ |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) || |
| 210 | (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index])) |
| 211 | { |
| 212 | return; |
| 213 | } |
| 214 | t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]]; |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 215 | send_geneve_tunnel_details (t, reg, mp->context); |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | /* |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame^] | 220 | * geneve_api_hookup |
| 221 | * Add geneve's API message handlers to the table. |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 222 | */ |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame^] | 223 | /* API definitions */ |
| 224 | #include <vnet/format_fns.h> |
| 225 | #include <vnet/geneve/geneve.api.c> |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 226 | |
| 227 | static clib_error_t * |
| 228 | geneve_api_hookup (vlib_main_t * vm) |
| 229 | { |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame^] | 230 | geneve_main_t *gm = &geneve_main; |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 231 | |
| 232 | /* |
| 233 | * Set up the (msg_name, crc, message-id) table |
| 234 | */ |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame^] | 235 | gm->msg_id_base = setup_message_id_table (); |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | VLIB_API_INIT_FUNCTION (geneve_api_hookup); |
| 241 | |
| 242 | /* |
| 243 | * fd.io coding-style-patch-verification: ON |
| 244 | * |
| 245 | * Local Variables: |
| 246 | * eval: (c-set-style "gnu") |
| 247 | * End: |
| 248 | */ |