2 * Copyright (c) 2017 SUSE LLC.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *------------------------------------------------------------------
17 #include <vnet/vnet.h>
18 #include <vlibmemory/api.h>
20 #include <vnet/interface.h>
21 #include <vnet/api_errno.h>
22 #include <vnet/feature/feature.h>
23 #include <vnet/geneve/geneve.h>
24 #include <vnet/fib/fib_table.h>
25 #include <vnet/ip/ip_types_api.h>
27 #include <vnet/geneve/geneve.api_enum.h>
28 #include <vnet/geneve/geneve.api_types.h>
30 #define REPLY_MSG_ID_BASE gm->msg_id_base
31 #include <vlibapi/api_helper_macros.h>
34 vl_api_sw_interface_set_geneve_bypass_t_handler
35 (vl_api_sw_interface_set_geneve_bypass_t * mp)
37 geneve_main_t *gm = &geneve_main;
38 vl_api_sw_interface_set_geneve_bypass_reply_t *rmp;
40 u32 sw_if_index = ntohl (mp->sw_if_index);
42 VALIDATE_SW_IF_INDEX (mp);
44 vnet_int_geneve_bypass_mode (sw_if_index, mp->is_ipv6, mp->enable);
45 BAD_SW_IF_INDEX_LABEL;
47 REPLY_MACRO (VL_API_SW_INTERFACE_SET_GENEVE_BYPASS_REPLY);
50 static void vl_api_geneve_add_del_tunnel_t_handler
51 (vl_api_geneve_add_del_tunnel_t * mp)
53 geneve_main_t *gm = &geneve_main;
54 vl_api_geneve_add_del_tunnel_reply_t *rmp;
56 ip4_main_t *im = &ip4_main;
58 uword *p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
61 rv = VNET_API_ERROR_NO_SUCH_FIB;
65 vnet_geneve_add_del_tunnel_args_t a = {
67 .is_ip6 = mp->remote_address.af,
68 .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index),
69 .encap_fib_index = p[0],
70 .decap_next_index = ntohl (mp->decap_next_index),
71 .vni = ntohl (mp->vni),
74 ip_address_decode (&mp->remote_address, &a.remote);
75 ip_address_decode (&mp->local_address, &a.local);
77 /* Check src & dst are different */
78 if (ip46_address_cmp (&a.remote, &a.local) == 0)
80 rv = VNET_API_ERROR_SAME_SRC_DST;
83 if (ip46_address_is_multicast (&a.remote) &&
84 !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
86 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
91 rv = vnet_geneve_add_del_tunnel (&a, &sw_if_index);
95 REPLY_MACRO2(VL_API_GENEVE_ADD_DEL_TUNNEL_REPLY,
97 rmp->sw_if_index = ntohl (sw_if_index);
102 static void vl_api_geneve_add_del_tunnel2_t_handler
103 (vl_api_geneve_add_del_tunnel2_t * mp)
105 geneve_main_t *gm = &geneve_main;
106 vl_api_geneve_add_del_tunnel2_reply_t *rmp;
108 ip4_main_t *im = &ip4_main;
110 uword *p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
113 rv = VNET_API_ERROR_NO_SUCH_FIB;
117 vnet_geneve_add_del_tunnel_args_t a = {
118 .is_add = mp->is_add,
119 .is_ip6 = mp->remote_address.af,
120 .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index),
121 .encap_fib_index = p[0],
122 .decap_next_index = ntohl (mp->decap_next_index),
123 .vni = ntohl (mp->vni),
124 .l3_mode = mp->l3_mode,
127 ip_address_decode (&mp->remote_address, &a.remote);
128 ip_address_decode (&mp->local_address, &a.local);
130 /* Check src & dst are different */
131 if (ip46_address_cmp (&a.remote, &a.local) == 0)
133 rv = VNET_API_ERROR_SAME_SRC_DST;
136 if (ip46_address_is_multicast (&a.remote) &&
137 !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
139 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
143 u32 sw_if_index = ~0;
144 rv = vnet_geneve_add_del_tunnel (&a, &sw_if_index);
148 REPLY_MACRO2(VL_API_GENEVE_ADD_DEL_TUNNEL2_REPLY,
150 rmp->sw_if_index = ntohl (sw_if_index);
155 static void send_geneve_tunnel_details
156 (geneve_tunnel_t * t, vl_api_registration_t * reg, u32 context)
158 geneve_main_t *gm = &geneve_main;
159 vl_api_geneve_tunnel_details_t *rmp;
160 ip4_main_t *im4 = &ip4_main;
161 ip6_main_t *im6 = &ip6_main;
162 u8 is_ipv6 = !ip46_address_is_ip4 (&t->remote);
164 rmp = vl_msg_api_alloc (sizeof (*rmp));
165 clib_memset (rmp, 0, sizeof (*rmp));
166 rmp->_vl_msg_id = ntohs (VL_API_GENEVE_TUNNEL_DETAILS + gm->msg_id_base);
167 ip_address_encode (&t->local, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
169 ip_address_encode (&t->remote, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
172 htonl (is_ipv6 ? im6->fibs[t->encap_fib_index].
173 ft_table_id : im4->fibs[t->encap_fib_index].ft_table_id);
175 rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
176 rmp->vni = htonl (t->vni);
177 rmp->decap_next_index = htonl (t->decap_next_index);
178 rmp->sw_if_index = htonl (t->sw_if_index);
179 rmp->context = context;
181 vl_api_send_msg (reg, (u8 *) rmp);
184 static void vl_api_geneve_tunnel_dump_t_handler
185 (vl_api_geneve_tunnel_dump_t * mp)
187 vl_api_registration_t *reg;
188 geneve_main_t *vxm = &geneve_main;
192 reg = vl_api_client_index_to_registration (mp->client_index);
196 sw_if_index = ntohl (mp->sw_if_index);
198 if (~0 == sw_if_index)
201 pool_foreach (t, vxm->tunnels,
203 send_geneve_tunnel_details(t, reg, mp->context);
209 if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) ||
210 (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index]))
214 t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
215 send_geneve_tunnel_details (t, reg, mp->context);
221 * Add geneve's API message handlers to the table.
223 /* API definitions */
224 #include <vnet/format_fns.h>
225 #include <vnet/geneve/geneve.api.c>
227 static clib_error_t *
228 geneve_api_hookup (vlib_main_t * vm)
230 geneve_main_t *gm = &geneve_main;
233 * Set up the (msg_name, crc, message-id) table
235 gm->msg_id_base = setup_message_id_table ();
240 VLIB_API_INIT_FUNCTION (geneve_api_hookup);
243 * fd.io coding-style-patch-verification: ON
246 * eval: (c-set-style "gnu")