Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * vxlan_gbp_api.c - vxlan gbp api |
| 4 | * |
| 5 | * Copyright (c) 2018 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> |
| 25 | #include <vnet/feature/feature.h> |
| 26 | #include <vnet/vxlan-gbp/vxlan_gbp.h> |
| 27 | #include <vnet/fib/fib_table.h> |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 28 | #include <vnet/ip/ip_types_api.h> |
Filip Tehlar | 26c8a7f | 2021-06-04 13:37:00 +0000 | [diff] [blame] | 29 | #include <vnet/format_fns.h> |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 30 | |
Filip Tehlar | 26c8a7f | 2021-06-04 13:37:00 +0000 | [diff] [blame] | 31 | #include <vxlan-gbp/vxlan_gbp.api_enum.h> |
| 32 | #include <vxlan-gbp/vxlan_gbp.api_types.h> |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 33 | |
Filip Tehlar | 26c8a7f | 2021-06-04 13:37:00 +0000 | [diff] [blame] | 34 | #define REPLY_MSG_ID_BASE msg_id_base |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [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; |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 38 | |
| 39 | static void |
| 40 | vl_api_sw_interface_set_vxlan_gbp_bypass_t_handler |
| 41 | (vl_api_sw_interface_set_vxlan_gbp_bypass_t * mp) |
| 42 | { |
| 43 | vl_api_sw_interface_set_vxlan_gbp_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_gbp_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_GBP_BYPASS_REPLY); |
| 53 | } |
| 54 | |
Neale Ranns | 4dd4cf4 | 2019-03-27 05:06:47 -0700 | [diff] [blame] | 55 | static int |
| 56 | vxlan_gbp_tunnel_mode_decode (vl_api_vxlan_gbp_api_tunnel_mode_t in, |
| 57 | vxlan_gbp_tunnel_mode_t * out) |
| 58 | { |
| 59 | in = clib_net_to_host_u32 (in); |
| 60 | |
| 61 | switch (in) |
| 62 | { |
| 63 | case VXLAN_GBP_API_TUNNEL_MODE_L2: |
| 64 | *out = VXLAN_GBP_TUNNEL_MODE_L2; |
| 65 | return (0); |
| 66 | case VXLAN_GBP_API_TUNNEL_MODE_L3: |
| 67 | *out = VXLAN_GBP_TUNNEL_MODE_L3; |
| 68 | return (0); |
| 69 | } |
Paul Vinciguerra | 9dd9434 | 2019-06-18 10:27:32 -0400 | [diff] [blame] | 70 | return (VNET_API_ERROR_INVALID_VALUE); |
Neale Ranns | 4dd4cf4 | 2019-03-27 05:06:47 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 73 | static void vl_api_vxlan_gbp_tunnel_add_del_t_handler |
| 74 | (vl_api_vxlan_gbp_tunnel_add_del_t * mp) |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 75 | { |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 76 | vl_api_vxlan_gbp_tunnel_add_del_reply_t *rmp; |
Neale Ranns | 4dd4cf4 | 2019-03-27 05:06:47 -0700 | [diff] [blame] | 77 | vxlan_gbp_tunnel_mode_t mode; |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 78 | ip46_address_t src, dst; |
| 79 | ip46_type_t itype; |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 80 | int rv = 0; |
Paul Vinciguerra | aaba610 | 2019-06-25 17:57:31 -0400 | [diff] [blame] | 81 | u32 sw_if_index = ~0; |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 82 | u32 fib_index; |
| 83 | |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 84 | itype = ip_address_decode (&mp->tunnel.src, &src); |
| 85 | itype = ip_address_decode (&mp->tunnel.dst, &dst); |
| 86 | |
| 87 | fib_index = fib_table_find (fib_proto_from_ip46 (itype), |
| 88 | ntohl (mp->tunnel.encap_table_id)); |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 89 | if (fib_index == ~0) |
| 90 | { |
| 91 | rv = VNET_API_ERROR_NO_SUCH_FIB; |
| 92 | goto out; |
| 93 | } |
| 94 | |
Neale Ranns | 4dd4cf4 | 2019-03-27 05:06:47 -0700 | [diff] [blame] | 95 | rv = vxlan_gbp_tunnel_mode_decode (mp->tunnel.mode, &mode); |
| 96 | |
| 97 | if (rv) |
| 98 | goto out; |
| 99 | |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 100 | vnet_vxlan_gbp_tunnel_add_del_args_t a = { |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 101 | .is_add = mp->is_add, |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 102 | .is_ip6 = (itype == IP46_TYPE_IP6), |
| 103 | .instance = ntohl (mp->tunnel.instance), |
| 104 | .mcast_sw_if_index = ntohl (mp->tunnel.mcast_sw_if_index), |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 105 | .encap_fib_index = fib_index, |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 106 | .vni = ntohl (mp->tunnel.vni), |
| 107 | .dst = dst, |
| 108 | .src = src, |
Neale Ranns | 4dd4cf4 | 2019-03-27 05:06:47 -0700 | [diff] [blame] | 109 | .mode = mode, |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | /* Check src & dst are different */ |
| 113 | if (ip46_address_cmp (&a.dst, &a.src) == 0) |
| 114 | { |
| 115 | rv = VNET_API_ERROR_SAME_SRC_DST; |
| 116 | goto out; |
| 117 | } |
| 118 | if (ip46_address_is_multicast (&a.dst) && |
| 119 | !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index)) |
| 120 | { |
| 121 | rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 122 | goto out; |
| 123 | } |
| 124 | |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 125 | rv = vnet_vxlan_gbp_tunnel_add_del (&a, &sw_if_index); |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 126 | |
| 127 | out: |
| 128 | /* *INDENT-OFF* */ |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 129 | REPLY_MACRO2(VL_API_VXLAN_GBP_TUNNEL_ADD_DEL_REPLY, |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 130 | ({ |
| 131 | rmp->sw_if_index = ntohl (sw_if_index); |
| 132 | })); |
| 133 | /* *INDENT-ON* */ |
| 134 | } |
| 135 | |
| 136 | static void send_vxlan_gbp_tunnel_details |
| 137 | (vxlan_gbp_tunnel_t * t, vl_api_registration_t * reg, u32 context) |
| 138 | { |
| 139 | vl_api_vxlan_gbp_tunnel_details_t *rmp; |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 140 | ip46_type_t itype = (ip46_address_is_ip4 (&t->dst) ? |
| 141 | IP46_TYPE_IP4 : IP46_TYPE_IP6); |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 142 | |
| 143 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 144 | clib_memset (rmp, 0, sizeof (*rmp)); |
Filip Tehlar | 26c8a7f | 2021-06-04 13:37:00 +0000 | [diff] [blame] | 145 | rmp->_vl_msg_id = |
| 146 | ntohs (VL_API_VXLAN_GBP_TUNNEL_DETAILS + REPLY_MSG_ID_BASE); |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 147 | |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 148 | ip_address_encode (&t->src, itype, &rmp->tunnel.src); |
| 149 | ip_address_encode (&t->dst, itype, &rmp->tunnel.dst); |
| 150 | rmp->tunnel.encap_table_id = |
| 151 | fib_table_get_table_id (t->encap_fib_index, fib_proto_from_ip46 (itype)); |
| 152 | |
| 153 | rmp->tunnel.instance = htonl (t->user_instance); |
| 154 | rmp->tunnel.mcast_sw_if_index = htonl (t->mcast_sw_if_index); |
| 155 | rmp->tunnel.vni = htonl (t->vni); |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 156 | rmp->tunnel.sw_if_index = htonl (t->sw_if_index); |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 157 | rmp->context = context; |
| 158 | |
| 159 | vl_api_send_msg (reg, (u8 *) rmp); |
| 160 | } |
| 161 | |
| 162 | static void vl_api_vxlan_gbp_tunnel_dump_t_handler |
| 163 | (vl_api_vxlan_gbp_tunnel_dump_t * mp) |
| 164 | { |
| 165 | vl_api_registration_t *reg; |
| 166 | vxlan_gbp_main_t *vxm = &vxlan_gbp_main; |
| 167 | vxlan_gbp_tunnel_t *t; |
| 168 | u32 sw_if_index; |
| 169 | |
| 170 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 171 | if (!reg) |
| 172 | return; |
| 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, vxm->tunnels) |
| 180 | { |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 181 | send_vxlan_gbp_tunnel_details(t, reg, mp->context); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 182 | } |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 183 | /* *INDENT-ON* */ |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) || |
| 188 | (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index])) |
| 189 | { |
| 190 | return; |
| 191 | } |
| 192 | t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]]; |
| 193 | send_vxlan_gbp_tunnel_details (t, reg, mp->context); |
| 194 | } |
| 195 | } |
| 196 | |
Filip Tehlar | 26c8a7f | 2021-06-04 13:37:00 +0000 | [diff] [blame] | 197 | #include <vxlan-gbp/vxlan_gbp.api.c> |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 198 | static clib_error_t * |
| 199 | vxlan_gbp_api_hookup (vlib_main_t * vm) |
| 200 | { |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 201 | /* |
| 202 | * Set up the (msg_name, crc, message-id) table |
| 203 | */ |
Filip Tehlar | 26c8a7f | 2021-06-04 13:37:00 +0000 | [diff] [blame] | 204 | msg_id_base = setup_message_id_table (); |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | VLIB_API_INIT_FUNCTION (vxlan_gbp_api_hookup); |
| 210 | |
| 211 | /* |
| 212 | * fd.io coding-style-patch-verification: ON |
| 213 | * |
| 214 | * Local Variables: |
| 215 | * eval: (c-set-style "gnu") |
| 216 | * End: |
| 217 | */ |