2 *------------------------------------------------------------------
3 * vxlan_api.c - vxlan 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/vxlan.h>
27 #include <vnet/fib/fib_table.h>
28 #include <vnet/ip/ip_types_api.h>
29 #include <vnet/udp/udp_local.h>
30 #include <vnet/format_fns.h>
31 #include <vxlan/vxlan.api_enum.h>
32 #include <vxlan/vxlan.api_types.h>
34 static u16 msg_id_base;
36 #define REPLY_MSG_ID_BASE msg_id_base
37 #include <vlibapi/api_helper_macros.h>
40 vl_api_vxlan_offload_rx_t_handler (vl_api_vxlan_offload_rx_t * mp)
42 vl_api_vxlan_offload_rx_reply_t *rmp;
44 u32 hw_if_index = ntohl (mp->hw_if_index);
45 u32 sw_if_index = ntohl (mp->sw_if_index);
47 if (!vnet_hw_interface_is_valid (vnet_get_main (), hw_if_index))
49 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
52 VALIDATE_SW_IF_INDEX (mp);
54 u32 t_index = vnet_vxlan_get_tunnel_index (sw_if_index);
57 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
61 vxlan_main_t *vxm = &vxlan_main;
62 vxlan_tunnel_t *t = pool_elt_at_index (vxm->tunnels, t_index);
63 if (!ip46_address_is_ip4 (&t->dst))
65 rv = VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
69 vnet_main_t *vnm = vnet_get_main ();
70 vnet_hw_interface_t *hw_if = vnet_get_hw_interface (vnm, hw_if_index);
71 ip4_main_t *im = &ip4_main;
73 vec_elt (im->fib_index_by_sw_if_index, hw_if->sw_if_index);
75 if (t->encap_fib_index != rx_fib_index)
77 rv = VNET_API_ERROR_NO_SUCH_FIB;
81 if (vnet_vxlan_add_del_rx_flow (hw_if_index, t_index, mp->enable))
83 rv = VNET_API_ERROR_UNSPECIFIED;
86 BAD_SW_IF_INDEX_LABEL;
89 REPLY_MACRO (VL_API_VXLAN_OFFLOAD_RX_REPLY);
93 vl_api_sw_interface_set_vxlan_bypass_t_handler
94 (vl_api_sw_interface_set_vxlan_bypass_t * mp)
96 vl_api_sw_interface_set_vxlan_bypass_reply_t *rmp;
98 u32 sw_if_index = ntohl (mp->sw_if_index);
100 VALIDATE_SW_IF_INDEX (mp);
102 vnet_int_vxlan_bypass_mode (sw_if_index, mp->is_ipv6, mp->enable);
103 BAD_SW_IF_INDEX_LABEL;
105 REPLY_MACRO (VL_API_SW_INTERFACE_SET_VXLAN_BYPASS_REPLY);
109 vxlan_add_del_tunnel_clean_input (vnet_vxlan_add_del_tunnel_args_t *a,
112 a->is_ip6 = !ip46_address_is_ip4 (&a->src);
114 a->encap_fib_index = fib_table_find (fib_ip_proto (a->is_ip6), encap_vrf_id);
115 if (a->encap_fib_index == ~0)
117 return VNET_API_ERROR_NO_SUCH_FIB;
120 if (ip46_address_is_ip4 (&a->src) != ip46_address_is_ip4 (&a->dst))
122 return VNET_API_ERROR_INVALID_VALUE;
125 /* Check src & dst are different */
126 if (ip46_address_cmp (&a->dst, &a->src) == 0)
128 return VNET_API_ERROR_SAME_SRC_DST;
130 if (ip46_address_is_multicast (&a->dst) &&
131 !vnet_sw_if_index_is_api_valid (a->mcast_sw_if_index))
133 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
139 vl_api_vxlan_add_del_tunnel_t_handler (vl_api_vxlan_add_del_tunnel_t *mp)
141 vl_api_vxlan_add_del_tunnel_reply_t *rmp;
142 u32 sw_if_index = ~0;
145 vnet_vxlan_add_del_tunnel_args_t a = {
146 .is_add = mp->is_add,
147 .instance = ntohl (mp->instance),
148 .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index),
149 .decap_next_index = ntohl (mp->decap_next_index),
150 .vni = ntohl (mp->vni),
152 ip_address_decode (&mp->src_address, &a.src);
153 ip_address_decode (&mp->dst_address, &a.dst);
155 rv = vxlan_add_del_tunnel_clean_input (&a, ntohl (mp->encap_vrf_id));
158 a.dst_port = a.is_ip6 ? UDP_DST_PORT_vxlan6 : UDP_DST_PORT_vxlan,
159 a.src_port = a.is_ip6 ? UDP_DST_PORT_vxlan6 : UDP_DST_PORT_vxlan,
160 rv = vnet_vxlan_add_del_tunnel (&a, &sw_if_index);
163 REPLY_MACRO2(VL_API_VXLAN_ADD_DEL_TUNNEL_REPLY,
165 rmp->sw_if_index = ntohl (sw_if_index);
170 vl_api_vxlan_add_del_tunnel_v2_t_handler (vl_api_vxlan_add_del_tunnel_v2_t *mp)
172 vl_api_vxlan_add_del_tunnel_v2_reply_t *rmp;
173 u32 sw_if_index = ~0;
176 vnet_vxlan_add_del_tunnel_args_t a = {
177 .is_add = mp->is_add,
178 .instance = ntohl (mp->instance),
179 .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index),
180 .decap_next_index = ntohl (mp->decap_next_index),
181 .vni = ntohl (mp->vni),
182 .dst_port = ntohs (mp->dst_port),
183 .src_port = ntohs (mp->src_port),
186 ip_address_decode (&mp->src_address, &a.src);
187 ip_address_decode (&mp->dst_address, &a.dst);
189 rv = vxlan_add_del_tunnel_clean_input (&a, ntohl (mp->encap_vrf_id));
192 rv = vnet_vxlan_add_del_tunnel (&a, &sw_if_index);
194 REPLY_MACRO2 (VL_API_VXLAN_ADD_DEL_TUNNEL_V2_REPLY,
195 ({ rmp->sw_if_index = ntohl (sw_if_index); }));
199 vl_api_vxlan_add_del_tunnel_v3_t_handler (vl_api_vxlan_add_del_tunnel_v3_t *mp)
201 vl_api_vxlan_add_del_tunnel_v3_reply_t *rmp;
202 u32 sw_if_index = ~0;
205 vnet_vxlan_add_del_tunnel_args_t a = {
206 .is_add = mp->is_add,
207 .instance = ntohl (mp->instance),
208 .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index),
209 .decap_next_index = ntohl (mp->decap_next_index),
210 .vni = ntohl (mp->vni),
211 .dst_port = ntohs (mp->dst_port),
212 .src_port = ntohs (mp->src_port),
216 ip_address_decode (&mp->src_address, &a.src);
217 ip_address_decode (&mp->dst_address, &a.dst);
219 rv = vxlan_add_del_tunnel_clean_input (&a, ntohl (mp->encap_vrf_id));
222 rv = vnet_vxlan_add_del_tunnel (&a, &sw_if_index);
224 REPLY_MACRO2 (VL_API_VXLAN_ADD_DEL_TUNNEL_V3_REPLY,
225 ({ rmp->sw_if_index = ntohl (sw_if_index); }));
228 static void send_vxlan_tunnel_details
229 (vxlan_tunnel_t * t, vl_api_registration_t * reg, u32 context)
231 vl_api_vxlan_tunnel_details_t *rmp;
232 ip4_main_t *im4 = &ip4_main;
233 ip6_main_t *im6 = &ip6_main;
235 rmp = vl_msg_api_alloc (sizeof (*rmp));
236 clib_memset (rmp, 0, sizeof (*rmp));
237 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_VXLAN_TUNNEL_DETAILS);
239 ip_address_encode (&t->src, IP46_TYPE_ANY, &rmp->src_address);
240 ip_address_encode (&t->dst, IP46_TYPE_ANY, &rmp->dst_address);
242 if (ip46_address_is_ip4 (&t->dst))
243 rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
245 rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
247 rmp->instance = htonl (t->user_instance);
248 rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
249 rmp->vni = htonl (t->vni);
250 rmp->decap_next_index = htonl (t->decap_next_index);
251 rmp->sw_if_index = htonl (t->sw_if_index);
252 rmp->context = context;
254 vl_api_send_msg (reg, (u8 *) rmp);
257 static void vl_api_vxlan_tunnel_dump_t_handler
258 (vl_api_vxlan_tunnel_dump_t * mp)
260 vl_api_registration_t *reg;
261 vxlan_main_t *vxm = &vxlan_main;
265 reg = vl_api_client_index_to_registration (mp->client_index);
269 sw_if_index = ntohl (mp->sw_if_index);
271 if (~0 == sw_if_index)
273 pool_foreach (t, vxm->tunnels)
274 send_vxlan_tunnel_details(t, reg, mp->context);
278 if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) ||
279 (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index]))
283 t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
284 send_vxlan_tunnel_details (t, reg, mp->context);
289 send_vxlan_tunnel_v2_details (vxlan_tunnel_t *t, vl_api_registration_t *reg,
292 vl_api_vxlan_tunnel_v2_details_t *rmp;
293 ip4_main_t *im4 = &ip4_main;
294 ip6_main_t *im6 = &ip6_main;
296 rmp = vl_msg_api_alloc (sizeof (*rmp));
297 clib_memset (rmp, 0, sizeof (*rmp));
298 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_VXLAN_TUNNEL_V2_DETAILS);
300 ip_address_encode (&t->src, IP46_TYPE_ANY, &rmp->src_address);
301 ip_address_encode (&t->dst, IP46_TYPE_ANY, &rmp->dst_address);
302 rmp->src_port = htons (t->src_port);
303 rmp->dst_port = htons (t->dst_port);
305 if (ip46_address_is_ip4 (&t->dst))
306 rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
308 rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
310 rmp->instance = htonl (t->user_instance);
311 rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
312 rmp->vni = htonl (t->vni);
313 rmp->decap_next_index = htonl (t->decap_next_index);
314 rmp->sw_if_index = htonl (t->sw_if_index);
315 rmp->context = context;
317 vl_api_send_msg (reg, (u8 *) rmp);
321 vl_api_vxlan_tunnel_v2_dump_t_handler (vl_api_vxlan_tunnel_v2_dump_t *mp)
323 vl_api_registration_t *reg;
324 vxlan_main_t *vxm = &vxlan_main;
328 reg = vl_api_client_index_to_registration (mp->client_index);
332 sw_if_index = ntohl (mp->sw_if_index);
334 if (~0 == sw_if_index)
336 pool_foreach (t, vxm->tunnels)
337 send_vxlan_tunnel_v2_details (t, reg, mp->context);
341 if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) ||
342 (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index]))
346 t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
347 send_vxlan_tunnel_v2_details (t, reg, mp->context);
351 #include <vxlan/vxlan.api.c>
352 static clib_error_t *
353 vxlan_api_hookup (vlib_main_t * vm)
355 api_main_t *am = vlibapi_get_main ();
357 vl_api_increase_msg_trace_size (am, VL_API_VXLAN_ADD_DEL_TUNNEL,
361 * Set up the (msg_name, crc, message-id) table
363 msg_id_base = setup_message_id_table ();
368 VLIB_API_INIT_FUNCTION (vxlan_api_hookup);
371 * fd.io coding-style-patch-verification: ON
374 * eval: (c-set-style "gnu")