Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * vxlan_gpe_api.c - vxlan_gpe 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> |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 25 | #include <vnet/feature/feature.h> |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 26 | #include <vnet/vxlan-gpe/vxlan_gpe.h> |
| 27 | #include <vnet/fib/fib_table.h> |
Filip Tehlar | 26c8a7f | 2021-06-04 13:37:00 +0000 | [diff] [blame] | 28 | #include <vnet/format_fns.h> |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 29 | |
Jakub Grajciar | 1c2002a | 2020-01-31 10:45:30 +0100 | [diff] [blame] | 30 | #include <vnet/ip/ip_types_api.h> |
Filip Tehlar | 26c8a7f | 2021-06-04 13:37:00 +0000 | [diff] [blame] | 31 | #include <vnet/vxlan-gpe/vxlan_gpe.api_enum.h> |
| 32 | #include <vnet/vxlan-gpe/vxlan_gpe.api_types.h> |
Jakub Grajciar | 1c2002a | 2020-01-31 10:45:30 +0100 | [diff] [blame] | 33 | |
Filip Tehlar | 26c8a7f | 2021-06-04 13:37:00 +0000 | [diff] [blame] | 34 | #define REPLY_MSG_ID_BASE msg_id_base |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 35 | #include <vlibapi/api_helper_macros.h> |
| 36 | |
Filip Tehlar | 26c8a7f | 2021-06-04 13:37:00 +0000 | [diff] [blame] | 37 | static u16 msg_id_base; |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 38 | |
| 39 | static void |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 40 | vl_api_sw_interface_set_vxlan_gpe_bypass_t_handler |
| 41 | (vl_api_sw_interface_set_vxlan_gpe_bypass_t * mp) |
| 42 | { |
| 43 | vl_api_sw_interface_set_vxlan_gpe_bypass_reply_t *rmp; |
| 44 | int rv = 0; |
| 45 | u32 sw_if_index = ntohl (mp->sw_if_index); |
| 46 | |
| 47 | VALIDATE_SW_IF_INDEX (mp); |
| 48 | |
| 49 | vnet_int_vxlan_gpe_bypass_mode (sw_if_index, mp->is_ipv6, mp->enable); |
| 50 | BAD_SW_IF_INDEX_LABEL; |
| 51 | |
| 52 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_VXLAN_GPE_BYPASS_REPLY); |
| 53 | } |
| 54 | |
| 55 | static void |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 56 | vl_api_vxlan_gpe_add_del_tunnel_t_handler |
| 57 | (vl_api_vxlan_gpe_add_del_tunnel_t * mp) |
| 58 | { |
| 59 | vl_api_vxlan_gpe_add_del_tunnel_reply_t *rmp; |
| 60 | int rv = 0; |
| 61 | vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a; |
| 62 | u32 encap_fib_index, decap_fib_index; |
| 63 | u8 protocol; |
| 64 | uword *p; |
| 65 | ip4_main_t *im = &ip4_main; |
| 66 | u32 sw_if_index = ~0; |
| 67 | |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 68 | p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id)); |
| 69 | if (!p) |
| 70 | { |
| 71 | rv = VNET_API_ERROR_NO_SUCH_FIB; |
| 72 | goto out; |
| 73 | } |
| 74 | encap_fib_index = p[0]; |
| 75 | |
| 76 | protocol = mp->protocol; |
| 77 | |
| 78 | /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */ |
| 79 | if (protocol == VXLAN_GPE_INPUT_NEXT_IP4_INPUT) |
| 80 | { |
| 81 | p = hash_get (im->fib_index_by_table_id, ntohl (mp->decap_vrf_id)); |
| 82 | if (!p) |
| 83 | { |
| 84 | rv = VNET_API_ERROR_NO_SUCH_INNER_FIB; |
| 85 | goto out; |
| 86 | } |
| 87 | decap_fib_index = p[0]; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | decap_fib_index = ntohl (mp->decap_vrf_id); |
| 92 | } |
| 93 | |
Jakub Grajciar | 1c2002a | 2020-01-31 10:45:30 +0100 | [diff] [blame] | 94 | |
| 95 | clib_memset (a, 0, sizeof (*a)); |
| 96 | |
| 97 | a->is_add = mp->is_add; |
| 98 | ip_address_decode (&mp->local, &a->local); |
| 99 | ip_address_decode (&mp->remote, &a->remote); |
| 100 | |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 101 | /* Check src & dst are different */ |
Jakub Grajciar | 1c2002a | 2020-01-31 10:45:30 +0100 | [diff] [blame] | 102 | if (ip46_address_is_equal (&a->local, &a->remote)) |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 103 | { |
| 104 | rv = VNET_API_ERROR_SAME_SRC_DST; |
| 105 | goto out; |
| 106 | } |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 107 | |
Jakub Grajciar | 1c2002a | 2020-01-31 10:45:30 +0100 | [diff] [blame] | 108 | a->is_ip6 = !ip46_address_is_ip4 (&a->local); |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 109 | a->mcast_sw_if_index = ntohl (mp->mcast_sw_if_index); |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 110 | a->encap_fib_index = encap_fib_index; |
| 111 | a->decap_fib_index = decap_fib_index; |
| 112 | a->protocol = protocol; |
| 113 | a->vni = ntohl (mp->vni); |
| 114 | rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index); |
| 115 | |
| 116 | out: |
| 117 | /* *INDENT-OFF* */ |
| 118 | REPLY_MACRO2(VL_API_VXLAN_GPE_ADD_DEL_TUNNEL_REPLY, |
| 119 | ({ |
| 120 | rmp->sw_if_index = ntohl (sw_if_index); |
| 121 | })); |
| 122 | /* *INDENT-ON* */ |
| 123 | } |
| 124 | |
| 125 | static void send_vxlan_gpe_tunnel_details |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 126 | (vxlan_gpe_tunnel_t * t, vl_api_registration_t * reg, u32 context) |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 127 | { |
| 128 | vl_api_vxlan_gpe_tunnel_details_t *rmp; |
| 129 | ip4_main_t *im4 = &ip4_main; |
| 130 | ip6_main_t *im6 = &ip6_main; |
| 131 | u8 is_ipv6 = !(t->flags & VXLAN_GPE_TUNNEL_IS_IPV4); |
| 132 | |
| 133 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 134 | clib_memset (rmp, 0, sizeof (*rmp)); |
Filip Tehlar | 26c8a7f | 2021-06-04 13:37:00 +0000 | [diff] [blame] | 135 | rmp->_vl_msg_id = |
| 136 | ntohs (REPLY_MSG_ID_BASE + VL_API_VXLAN_GPE_TUNNEL_DETAILS); |
Jakub Grajciar | 1c2002a | 2020-01-31 10:45:30 +0100 | [diff] [blame] | 137 | |
| 138 | ip_address_encode (&t->local, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, |
| 139 | &rmp->local); |
| 140 | ip_address_encode (&t->remote, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, |
| 141 | &rmp->remote); |
| 142 | |
| 143 | if (ip46_address_is_ip4 (&t->local)) |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 144 | { |
Jakub Grajciar | 1c2002a | 2020-01-31 10:45:30 +0100 | [diff] [blame] | 145 | rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id); |
| 146 | rmp->decap_vrf_id = htonl (im4->fibs[t->decap_fib_index].ft_table_id); |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 147 | } |
| 148 | else |
| 149 | { |
Jakub Grajciar | 1c2002a | 2020-01-31 10:45:30 +0100 | [diff] [blame] | 150 | rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id); |
| 151 | rmp->decap_vrf_id = htonl (im6->fibs[t->decap_fib_index].ft_table_id); |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 152 | } |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 153 | rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index); |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 154 | rmp->vni = htonl (t->vni); |
| 155 | rmp->protocol = t->protocol; |
| 156 | rmp->sw_if_index = htonl (t->sw_if_index); |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 157 | rmp->context = context; |
| 158 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 159 | vl_api_send_msg (reg, (u8 *) rmp); |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static void vl_api_vxlan_gpe_tunnel_dump_t_handler |
| 163 | (vl_api_vxlan_gpe_tunnel_dump_t * mp) |
| 164 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 165 | vl_api_registration_t *reg; |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 166 | vxlan_gpe_main_t *vgm = &vxlan_gpe_main; |
| 167 | vxlan_gpe_tunnel_t *t; |
| 168 | u32 sw_if_index; |
| 169 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 170 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 171 | if (!reg) |
| 172 | return; |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 173 | |
| 174 | sw_if_index = ntohl (mp->sw_if_index); |
| 175 | |
| 176 | if (~0 == sw_if_index) |
| 177 | { |
| 178 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 179 | pool_foreach (t, vgm->tunnels) |
| 180 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 181 | send_vxlan_gpe_tunnel_details(t, reg, mp->context); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 182 | } |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 183 | /* *INDENT-ON* */ |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | if ((sw_if_index >= vec_len (vgm->tunnel_index_by_sw_if_index)) || |
| 188 | (~0 == vgm->tunnel_index_by_sw_if_index[sw_if_index])) |
| 189 | { |
| 190 | return; |
| 191 | } |
| 192 | t = &vgm->tunnels[vgm->tunnel_index_by_sw_if_index[sw_if_index]]; |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 193 | send_vxlan_gpe_tunnel_details (t, reg, mp->context); |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Filip Tehlar | 26c8a7f | 2021-06-04 13:37:00 +0000 | [diff] [blame] | 197 | #include <vxlan-gpe/vxlan_gpe.api.c> |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 198 | |
| 199 | static clib_error_t * |
| 200 | vxlan_gpe_api_hookup (vlib_main_t * vm) |
| 201 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 202 | api_main_t *am = vlibapi_get_main (); |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 203 | |
Hongjun Ni | 04ffd0ad | 2017-06-23 00:18:40 +0800 | [diff] [blame] | 204 | am->api_trace_cfg[VL_API_VXLAN_GPE_ADD_DEL_TUNNEL].size += |
| 205 | 17 * sizeof (u32); |
| 206 | |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 207 | /* |
| 208 | * Set up the (msg_name, crc, message-id) table |
| 209 | */ |
Filip Tehlar | 26c8a7f | 2021-06-04 13:37:00 +0000 | [diff] [blame] | 210 | msg_id_base = setup_message_id_table (); |
Pavel Kotucek | 296b201 | 2016-12-19 14:47:09 +0100 | [diff] [blame] | 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | VLIB_API_INIT_FUNCTION (vxlan_gpe_api_hookup); |
| 216 | |
| 217 | /* |
| 218 | * fd.io coding-style-patch-verification: ON |
| 219 | * |
| 220 | * Local Variables: |
| 221 | * eval: (c-set-style "gnu") |
| 222 | * End: |
| 223 | */ |