2 *------------------------------------------------------------------
3 * vxlan_gpe_api.c - vxlan_gpe api
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/feature/feature.h>
26 #include <vnet/vxlan-gpe/vxlan_gpe.h>
27 #include <vnet/fib/fib_table.h>
28 #include <vnet/format_fns.h>
30 #include <vnet/ip/ip_types_api.h>
31 #include <vnet/vxlan-gpe/vxlan_gpe.api_enum.h>
32 #include <vnet/vxlan-gpe/vxlan_gpe.api_types.h>
34 #define REPLY_MSG_ID_BASE msg_id_base
35 #include <vlibapi/api_helper_macros.h>
37 static u16 msg_id_base;
40 vl_api_sw_interface_set_vxlan_gpe_bypass_t_handler
41 (vl_api_sw_interface_set_vxlan_gpe_bypass_t * mp)
43 vl_api_sw_interface_set_vxlan_gpe_bypass_reply_t *rmp;
45 u32 sw_if_index = ntohl (mp->sw_if_index);
47 VALIDATE_SW_IF_INDEX (mp);
49 vnet_int_vxlan_gpe_bypass_mode (sw_if_index, mp->is_ipv6, mp->enable);
50 BAD_SW_IF_INDEX_LABEL;
52 REPLY_MACRO (VL_API_SW_INTERFACE_SET_VXLAN_GPE_BYPASS_REPLY);
56 vl_api_vxlan_gpe_add_del_tunnel_t_handler
57 (vl_api_vxlan_gpe_add_del_tunnel_t * mp)
59 vl_api_vxlan_gpe_add_del_tunnel_reply_t *rmp;
61 vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
62 u32 encap_fib_index, decap_fib_index;
65 ip4_main_t *im = &ip4_main;
68 p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
71 rv = VNET_API_ERROR_NO_SUCH_FIB;
74 encap_fib_index = p[0];
76 protocol = mp->protocol;
78 /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
79 if (protocol == VXLAN_GPE_INPUT_NEXT_IP4_INPUT)
81 p = hash_get (im->fib_index_by_table_id, ntohl (mp->decap_vrf_id));
84 rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
87 decap_fib_index = p[0];
91 decap_fib_index = ntohl (mp->decap_vrf_id);
95 clib_memset (a, 0, sizeof (*a));
97 a->is_add = mp->is_add;
98 ip_address_decode (&mp->local, &a->local);
99 ip_address_decode (&mp->remote, &a->remote);
101 /* Check src & dst are different */
102 if (ip46_address_is_equal (&a->local, &a->remote))
104 rv = VNET_API_ERROR_SAME_SRC_DST;
108 a->is_ip6 = !ip46_address_is_ip4 (&a->local);
109 a->mcast_sw_if_index = ntohl (mp->mcast_sw_if_index);
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);
118 REPLY_MACRO2(VL_API_VXLAN_GPE_ADD_DEL_TUNNEL_REPLY,
120 rmp->sw_if_index = ntohl (sw_if_index);
126 vl_api_vxlan_gpe_add_del_tunnel_v2_t_handler (
127 vl_api_vxlan_gpe_add_del_tunnel_v2_t *mp)
129 vl_api_vxlan_gpe_add_del_tunnel_v2_reply_t *rmp;
131 vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
132 u32 encap_fib_index, decap_fib_index;
135 ip4_main_t *im = &ip4_main;
136 u32 sw_if_index = ~0;
138 p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
141 rv = VNET_API_ERROR_NO_SUCH_FIB;
144 encap_fib_index = p[0];
146 protocol = mp->protocol;
148 /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
149 if (protocol == VXLAN_GPE_INPUT_NEXT_IP4_INPUT)
151 p = hash_get (im->fib_index_by_table_id, ntohl (mp->decap_vrf_id));
154 rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
157 decap_fib_index = p[0];
161 decap_fib_index = ntohl (mp->decap_vrf_id);
164 clib_memset (a, 0, sizeof (*a));
166 a->is_add = mp->is_add;
167 ip_address_decode (&mp->local, &a->local);
168 ip_address_decode (&mp->remote, &a->remote);
170 /* Check src & dst are different */
171 if (ip46_address_is_equal (&a->local, &a->remote))
173 rv = VNET_API_ERROR_SAME_SRC_DST;
177 a->local_port = ntohs (mp->local_port);
178 a->remote_port = ntohs (mp->remote_port);
179 a->is_ip6 = !ip46_address_is_ip4 (&a->local);
180 a->mcast_sw_if_index = ntohl (mp->mcast_sw_if_index);
181 a->encap_fib_index = encap_fib_index;
182 a->decap_fib_index = decap_fib_index;
183 a->protocol = protocol;
184 a->vni = ntohl (mp->vni);
185 rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
188 REPLY_MACRO2 (VL_API_VXLAN_GPE_ADD_DEL_TUNNEL_V2_REPLY,
189 ({ rmp->sw_if_index = ntohl (sw_if_index); }));
192 static void send_vxlan_gpe_tunnel_details
193 (vxlan_gpe_tunnel_t * t, vl_api_registration_t * reg, u32 context)
195 vl_api_vxlan_gpe_tunnel_details_t *rmp;
196 ip4_main_t *im4 = &ip4_main;
197 ip6_main_t *im6 = &ip6_main;
198 u8 is_ipv6 = !(t->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
200 rmp = vl_msg_api_alloc (sizeof (*rmp));
201 clib_memset (rmp, 0, sizeof (*rmp));
203 ntohs (REPLY_MSG_ID_BASE + VL_API_VXLAN_GPE_TUNNEL_DETAILS);
205 ip_address_encode (&t->local, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
207 ip_address_encode (&t->remote, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
210 if (ip46_address_is_ip4 (&t->local))
212 rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
213 rmp->decap_vrf_id = htonl (im4->fibs[t->decap_fib_index].ft_table_id);
217 rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
218 rmp->decap_vrf_id = htonl (im6->fibs[t->decap_fib_index].ft_table_id);
220 rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
221 rmp->vni = htonl (t->vni);
222 rmp->protocol = t->protocol;
223 rmp->sw_if_index = htonl (t->sw_if_index);
224 rmp->context = context;
226 vl_api_send_msg (reg, (u8 *) rmp);
229 static void vl_api_vxlan_gpe_tunnel_dump_t_handler
230 (vl_api_vxlan_gpe_tunnel_dump_t * mp)
232 vl_api_registration_t *reg;
233 vxlan_gpe_main_t *vgm = &vxlan_gpe_main;
234 vxlan_gpe_tunnel_t *t;
237 reg = vl_api_client_index_to_registration (mp->client_index);
241 sw_if_index = ntohl (mp->sw_if_index);
243 if (~0 == sw_if_index)
246 pool_foreach (t, vgm->tunnels)
248 send_vxlan_gpe_tunnel_details (t, reg, mp->context);
254 if ((sw_if_index >= vec_len (vgm->tunnel_index_by_sw_if_index)) ||
255 (~0 == vgm->tunnel_index_by_sw_if_index[sw_if_index]))
259 t = &vgm->tunnels[vgm->tunnel_index_by_sw_if_index[sw_if_index]];
260 send_vxlan_gpe_tunnel_details (t, reg, mp->context);
265 send_vxlan_gpe_tunnel_v2_details (vxlan_gpe_tunnel_t *t,
266 vl_api_registration_t *reg, u32 context)
268 vl_api_vxlan_gpe_tunnel_v2_details_t *rmp;
269 ip4_main_t *im4 = &ip4_main;
270 ip6_main_t *im6 = &ip6_main;
271 u8 is_ipv6 = !(t->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
273 rmp = vl_msg_api_alloc (sizeof (*rmp));
274 clib_memset (rmp, 0, sizeof (*rmp));
276 ntohs (REPLY_MSG_ID_BASE + VL_API_VXLAN_GPE_TUNNEL_V2_DETAILS);
278 ip_address_encode (&t->local, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
280 ip_address_encode (&t->remote, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
282 rmp->local_port = htons (t->local_port);
283 rmp->remote_port = htons (t->remote_port);
285 if (ip46_address_is_ip4 (&t->local))
287 rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
288 rmp->decap_vrf_id = htonl (im4->fibs[t->decap_fib_index].ft_table_id);
292 rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
293 rmp->decap_vrf_id = htonl (im6->fibs[t->decap_fib_index].ft_table_id);
295 rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
296 rmp->vni = htonl (t->vni);
297 rmp->protocol = t->protocol;
298 rmp->sw_if_index = htonl (t->sw_if_index);
299 rmp->context = context;
301 vl_api_send_msg (reg, (u8 *) rmp);
305 vl_api_vxlan_gpe_tunnel_v2_dump_t_handler (
306 vl_api_vxlan_gpe_tunnel_v2_dump_t *mp)
308 vl_api_registration_t *reg;
309 vxlan_gpe_main_t *vgm = &vxlan_gpe_main;
310 vxlan_gpe_tunnel_t *t;
313 reg = vl_api_client_index_to_registration (mp->client_index);
317 sw_if_index = ntohl (mp->sw_if_index);
319 if (~0 == sw_if_index)
321 pool_foreach (t, vgm->tunnels)
323 send_vxlan_gpe_tunnel_v2_details (t, reg, mp->context);
328 if ((sw_if_index >= vec_len (vgm->tunnel_index_by_sw_if_index)) ||
329 (~0 == vgm->tunnel_index_by_sw_if_index[sw_if_index]))
333 t = &vgm->tunnels[vgm->tunnel_index_by_sw_if_index[sw_if_index]];
334 send_vxlan_gpe_tunnel_v2_details (t, reg, mp->context);
338 #include <vxlan-gpe/vxlan_gpe.api.c>
340 static clib_error_t *
341 vxlan_gpe_api_hookup (vlib_main_t * vm)
343 api_main_t *am = vlibapi_get_main ();
345 am->api_trace_cfg[VL_API_VXLAN_GPE_ADD_DEL_TUNNEL].size +=
349 * Set up the (msg_name, crc, message-id) table
351 msg_id_base = setup_message_id_table ();
356 VLIB_API_INIT_FUNCTION (vxlan_gpe_api_hookup);
359 * fd.io coding-style-patch-verification: ON
362 * eval: (c-set-style "gnu")