blob: cc74e1f58d4a50a8657648cad2087e4b47f89d42 [file] [log] [blame]
Pavel Kotucek296b2012016-12-19 14:47:09 +01001/*
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 Ni8a0a0ae2017-05-27 20:23:09 +080025#include <vnet/feature/feature.h>
Pavel Kotucek296b2012016-12-19 14:47:09 +010026#include <vnet/vxlan-gpe/vxlan_gpe.h>
27#include <vnet/fib/fib_table.h>
Filip Tehlar26c8a7f2021-06-04 13:37:00 +000028#include <vnet/format_fns.h>
Pavel Kotucek296b2012016-12-19 14:47:09 +010029
Jakub Grajciar1c2002a2020-01-31 10:45:30 +010030#include <vnet/ip/ip_types_api.h>
Filip Tehlar26c8a7f2021-06-04 13:37:00 +000031#include <vnet/vxlan-gpe/vxlan_gpe.api_enum.h>
32#include <vnet/vxlan-gpe/vxlan_gpe.api_types.h>
Jakub Grajciar1c2002a2020-01-31 10:45:30 +010033
Filip Tehlar26c8a7f2021-06-04 13:37:00 +000034#define REPLY_MSG_ID_BASE msg_id_base
Pavel Kotucek296b2012016-12-19 14:47:09 +010035#include <vlibapi/api_helper_macros.h>
36
Filip Tehlar26c8a7f2021-06-04 13:37:00 +000037static u16 msg_id_base;
Pavel Kotucek296b2012016-12-19 14:47:09 +010038
39static void
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080040 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
55static void
Pavel Kotucek296b2012016-12-19 14:47:09 +010056 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 Kotucek296b2012016-12-19 14:47:09 +010068 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 Grajciar1c2002a2020-01-31 10:45:30 +010094
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 Kotucek296b2012016-12-19 14:47:09 +0100101 /* Check src & dst are different */
Jakub Grajciar1c2002a2020-01-31 10:45:30 +0100102 if (ip46_address_is_equal (&a->local, &a->remote))
Pavel Kotucek296b2012016-12-19 14:47:09 +0100103 {
104 rv = VNET_API_ERROR_SAME_SRC_DST;
105 goto out;
106 }
Pavel Kotucek296b2012016-12-19 14:47:09 +0100107
Jakub Grajciar1c2002a2020-01-31 10:45:30 +0100108 a->is_ip6 = !ip46_address_is_ip4 (&a->local);
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800109 a->mcast_sw_if_index = ntohl (mp->mcast_sw_if_index);
Pavel Kotucek296b2012016-12-19 14:47:09 +0100110 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
116out:
Pavel Kotucek296b2012016-12-19 14:47:09 +0100117 REPLY_MACRO2(VL_API_VXLAN_GPE_ADD_DEL_TUNNEL_REPLY,
118 ({
119 rmp->sw_if_index = ntohl (sw_if_index);
120 }));
Pavel Kotucek296b2012016-12-19 14:47:09 +0100121}
122
Artem Glazychevea962922021-05-28 19:09:14 +0700123static void
124vl_api_vxlan_gpe_add_del_tunnel_v2_t_handler (
125 vl_api_vxlan_gpe_add_del_tunnel_v2_t *mp)
126{
127 vl_api_vxlan_gpe_add_del_tunnel_v2_reply_t *rmp;
128 int rv = 0;
129 vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
130 u32 encap_fib_index, decap_fib_index;
131 u8 protocol;
132 uword *p;
133 ip4_main_t *im = &ip4_main;
134 u32 sw_if_index = ~0;
135
136 p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
137 if (!p)
138 {
139 rv = VNET_API_ERROR_NO_SUCH_FIB;
140 goto out;
141 }
142 encap_fib_index = p[0];
143
144 protocol = mp->protocol;
145
146 /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
147 if (protocol == VXLAN_GPE_INPUT_NEXT_IP4_INPUT)
148 {
149 p = hash_get (im->fib_index_by_table_id, ntohl (mp->decap_vrf_id));
150 if (!p)
151 {
152 rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
153 goto out;
154 }
155 decap_fib_index = p[0];
156 }
157 else
158 {
159 decap_fib_index = ntohl (mp->decap_vrf_id);
160 }
161
162 clib_memset (a, 0, sizeof (*a));
163
164 a->is_add = mp->is_add;
165 ip_address_decode (&mp->local, &a->local);
166 ip_address_decode (&mp->remote, &a->remote);
167
168 /* Check src & dst are different */
169 if (ip46_address_is_equal (&a->local, &a->remote))
170 {
171 rv = VNET_API_ERROR_SAME_SRC_DST;
172 goto out;
173 }
174
175 a->local_port = ntohs (mp->local_port);
176 a->remote_port = ntohs (mp->remote_port);
177 a->is_ip6 = !ip46_address_is_ip4 (&a->local);
178 a->mcast_sw_if_index = ntohl (mp->mcast_sw_if_index);
179 a->encap_fib_index = encap_fib_index;
180 a->decap_fib_index = decap_fib_index;
181 a->protocol = protocol;
182 a->vni = ntohl (mp->vni);
183 rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
184
185out:
186 REPLY_MACRO2 (VL_API_VXLAN_GPE_ADD_DEL_TUNNEL_V2_REPLY,
187 ({ rmp->sw_if_index = ntohl (sw_if_index); }));
188}
189
Pavel Kotucek296b2012016-12-19 14:47:09 +0100190static void send_vxlan_gpe_tunnel_details
Florin Coras6c4dae22018-01-09 06:39:23 -0800191 (vxlan_gpe_tunnel_t * t, vl_api_registration_t * reg, u32 context)
Pavel Kotucek296b2012016-12-19 14:47:09 +0100192{
193 vl_api_vxlan_gpe_tunnel_details_t *rmp;
194 ip4_main_t *im4 = &ip4_main;
195 ip6_main_t *im6 = &ip6_main;
196 u8 is_ipv6 = !(t->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
197
198 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400199 clib_memset (rmp, 0, sizeof (*rmp));
Filip Tehlar26c8a7f2021-06-04 13:37:00 +0000200 rmp->_vl_msg_id =
201 ntohs (REPLY_MSG_ID_BASE + VL_API_VXLAN_GPE_TUNNEL_DETAILS);
Jakub Grajciar1c2002a2020-01-31 10:45:30 +0100202
203 ip_address_encode (&t->local, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
204 &rmp->local);
205 ip_address_encode (&t->remote, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
206 &rmp->remote);
207
208 if (ip46_address_is_ip4 (&t->local))
Pavel Kotucek296b2012016-12-19 14:47:09 +0100209 {
Jakub Grajciar1c2002a2020-01-31 10:45:30 +0100210 rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
211 rmp->decap_vrf_id = htonl (im4->fibs[t->decap_fib_index].ft_table_id);
Pavel Kotucek296b2012016-12-19 14:47:09 +0100212 }
213 else
214 {
Jakub Grajciar1c2002a2020-01-31 10:45:30 +0100215 rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
216 rmp->decap_vrf_id = htonl (im6->fibs[t->decap_fib_index].ft_table_id);
Pavel Kotucek296b2012016-12-19 14:47:09 +0100217 }
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800218 rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
Pavel Kotucek296b2012016-12-19 14:47:09 +0100219 rmp->vni = htonl (t->vni);
220 rmp->protocol = t->protocol;
221 rmp->sw_if_index = htonl (t->sw_if_index);
Pavel Kotucek296b2012016-12-19 14:47:09 +0100222 rmp->context = context;
223
Florin Coras6c4dae22018-01-09 06:39:23 -0800224 vl_api_send_msg (reg, (u8 *) rmp);
Pavel Kotucek296b2012016-12-19 14:47:09 +0100225}
226
227static void vl_api_vxlan_gpe_tunnel_dump_t_handler
228 (vl_api_vxlan_gpe_tunnel_dump_t * mp)
229{
Florin Coras6c4dae22018-01-09 06:39:23 -0800230 vl_api_registration_t *reg;
Pavel Kotucek296b2012016-12-19 14:47:09 +0100231 vxlan_gpe_main_t *vgm = &vxlan_gpe_main;
232 vxlan_gpe_tunnel_t *t;
233 u32 sw_if_index;
234
Florin Coras6c4dae22018-01-09 06:39:23 -0800235 reg = vl_api_client_index_to_registration (mp->client_index);
236 if (!reg)
237 return;
Pavel Kotucek296b2012016-12-19 14:47:09 +0100238
239 sw_if_index = ntohl (mp->sw_if_index);
240
241 if (~0 == sw_if_index)
242 {
Damjan Marionb2c31b62020-12-13 21:47:40 +0100243 pool_foreach (t, vgm->tunnels)
Artem Glazychevea962922021-05-28 19:09:14 +0700244 {
245 send_vxlan_gpe_tunnel_details (t, reg, mp->context);
246 }
Pavel Kotucek296b2012016-12-19 14:47:09 +0100247 }
248 else
249 {
250 if ((sw_if_index >= vec_len (vgm->tunnel_index_by_sw_if_index)) ||
251 (~0 == vgm->tunnel_index_by_sw_if_index[sw_if_index]))
252 {
253 return;
254 }
255 t = &vgm->tunnels[vgm->tunnel_index_by_sw_if_index[sw_if_index]];
Florin Coras6c4dae22018-01-09 06:39:23 -0800256 send_vxlan_gpe_tunnel_details (t, reg, mp->context);
Pavel Kotucek296b2012016-12-19 14:47:09 +0100257 }
258}
259
Artem Glazychevea962922021-05-28 19:09:14 +0700260static void
261send_vxlan_gpe_tunnel_v2_details (vxlan_gpe_tunnel_t *t,
262 vl_api_registration_t *reg, u32 context)
263{
264 vl_api_vxlan_gpe_tunnel_v2_details_t *rmp;
265 ip4_main_t *im4 = &ip4_main;
266 ip6_main_t *im6 = &ip6_main;
267 u8 is_ipv6 = !(t->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
268
269 rmp = vl_msg_api_alloc (sizeof (*rmp));
270 clib_memset (rmp, 0, sizeof (*rmp));
271 rmp->_vl_msg_id =
272 ntohs (REPLY_MSG_ID_BASE + VL_API_VXLAN_GPE_TUNNEL_V2_DETAILS);
273
274 ip_address_encode (&t->local, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
275 &rmp->local);
276 ip_address_encode (&t->remote, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
277 &rmp->remote);
278 rmp->local_port = htons (t->local_port);
279 rmp->remote_port = htons (t->remote_port);
280
281 if (ip46_address_is_ip4 (&t->local))
282 {
283 rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
284 rmp->decap_vrf_id = htonl (im4->fibs[t->decap_fib_index].ft_table_id);
285 }
286 else
287 {
288 rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
289 rmp->decap_vrf_id = htonl (im6->fibs[t->decap_fib_index].ft_table_id);
290 }
291 rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
292 rmp->vni = htonl (t->vni);
293 rmp->protocol = t->protocol;
294 rmp->sw_if_index = htonl (t->sw_if_index);
295 rmp->context = context;
296
297 vl_api_send_msg (reg, (u8 *) rmp);
298}
299
300static void
301vl_api_vxlan_gpe_tunnel_v2_dump_t_handler (
302 vl_api_vxlan_gpe_tunnel_v2_dump_t *mp)
303{
304 vl_api_registration_t *reg;
305 vxlan_gpe_main_t *vgm = &vxlan_gpe_main;
306 vxlan_gpe_tunnel_t *t;
307 u32 sw_if_index;
308
309 reg = vl_api_client_index_to_registration (mp->client_index);
310 if (!reg)
311 return;
312
313 sw_if_index = ntohl (mp->sw_if_index);
314
315 if (~0 == sw_if_index)
316 {
317 pool_foreach (t, vgm->tunnels)
318 {
319 send_vxlan_gpe_tunnel_v2_details (t, reg, mp->context);
320 }
321 }
322 else
323 {
324 if ((sw_if_index >= vec_len (vgm->tunnel_index_by_sw_if_index)) ||
325 (~0 == vgm->tunnel_index_by_sw_if_index[sw_if_index]))
326 {
327 return;
328 }
329 t = &vgm->tunnels[vgm->tunnel_index_by_sw_if_index[sw_if_index]];
330 send_vxlan_gpe_tunnel_v2_details (t, reg, mp->context);
331 }
332}
333
Filip Tehlar26c8a7f2021-06-04 13:37:00 +0000334#include <vxlan-gpe/vxlan_gpe.api.c>
Pavel Kotucek296b2012016-12-19 14:47:09 +0100335
336static clib_error_t *
337vxlan_gpe_api_hookup (vlib_main_t * vm)
338{
Dave Barach39d69112019-11-27 11:42:13 -0500339 api_main_t *am = vlibapi_get_main ();
Pavel Kotucek296b2012016-12-19 14:47:09 +0100340
Damjan Marioncada9eb2022-05-18 22:16:11 +0200341 vl_api_increase_msg_trace_size (am, VL_API_VXLAN_GPE_ADD_DEL_TUNNEL,
342 17 * sizeof (u32));
Hongjun Ni04ffd0ad2017-06-23 00:18:40 +0800343
Pavel Kotucek296b2012016-12-19 14:47:09 +0100344 /*
345 * Set up the (msg_name, crc, message-id) table
346 */
Filip Tehlar26c8a7f2021-06-04 13:37:00 +0000347 msg_id_base = setup_message_id_table ();
Pavel Kotucek296b2012016-12-19 14:47:09 +0100348
349 return 0;
350}
351
352VLIB_API_INIT_FUNCTION (vxlan_gpe_api_hookup);
353
354/*
355 * fd.io coding-style-patch-verification: ON
356 *
357 * Local Variables:
358 * eval: (c-set-style "gnu")
359 * End:
360 */