2 *------------------------------------------------------------------
3 * vxlan_gbp_api.c - vxlan gbp api
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:
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-gbp/vxlan_gbp.h>
27 #include <vnet/fib/fib_table.h>
28 #include <vnet/ip/ip_types_api.h>
29 #include <vnet/format_fns.h>
31 #include <vxlan-gbp/vxlan_gbp.api_enum.h>
32 #include <vxlan-gbp/vxlan_gbp.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_gbp_bypass_t_handler
41 (vl_api_sw_interface_set_vxlan_gbp_bypass_t * mp)
43 vl_api_sw_interface_set_vxlan_gbp_bypass_reply_t *rmp;
45 u32 sw_if_index = ntohl (mp->sw_if_index);
47 VALIDATE_SW_IF_INDEX (mp);
49 vnet_int_vxlan_gbp_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_GBP_BYPASS_REPLY);
56 vxlan_gbp_tunnel_mode_decode (vl_api_vxlan_gbp_api_tunnel_mode_t in,
57 vxlan_gbp_tunnel_mode_t * out)
59 in = clib_net_to_host_u32 (in);
63 case VXLAN_GBP_API_TUNNEL_MODE_L2:
64 *out = VXLAN_GBP_TUNNEL_MODE_L2;
66 case VXLAN_GBP_API_TUNNEL_MODE_L3:
67 *out = VXLAN_GBP_TUNNEL_MODE_L3;
70 return (VNET_API_ERROR_INVALID_VALUE);
73 static void vl_api_vxlan_gbp_tunnel_add_del_t_handler
74 (vl_api_vxlan_gbp_tunnel_add_del_t * mp)
76 vl_api_vxlan_gbp_tunnel_add_del_reply_t *rmp;
77 vxlan_gbp_tunnel_mode_t mode;
78 ip46_address_t src, dst;
84 itype = ip_address_decode (&mp->tunnel.src, &src);
85 itype = ip_address_decode (&mp->tunnel.dst, &dst);
87 fib_index = fib_table_find (fib_proto_from_ip46 (itype),
88 ntohl (mp->tunnel.encap_table_id));
91 rv = VNET_API_ERROR_NO_SUCH_FIB;
95 rv = vxlan_gbp_tunnel_mode_decode (mp->tunnel.mode, &mode);
100 vnet_vxlan_gbp_tunnel_add_del_args_t a = {
101 .is_add = mp->is_add,
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),
105 .encap_fib_index = fib_index,
106 .vni = ntohl (mp->tunnel.vni),
112 /* Check src & dst are different */
113 if (ip46_address_cmp (&a.dst, &a.src) == 0)
115 rv = VNET_API_ERROR_SAME_SRC_DST;
118 if (ip46_address_is_multicast (&a.dst) &&
119 !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
121 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
125 rv = vnet_vxlan_gbp_tunnel_add_del (&a, &sw_if_index);
129 REPLY_MACRO2(VL_API_VXLAN_GBP_TUNNEL_ADD_DEL_REPLY,
131 rmp->sw_if_index = ntohl (sw_if_index);
136 static void send_vxlan_gbp_tunnel_details
137 (vxlan_gbp_tunnel_t * t, vl_api_registration_t * reg, u32 context)
139 vl_api_vxlan_gbp_tunnel_details_t *rmp;
140 ip46_type_t itype = (ip46_address_is_ip4 (&t->dst) ?
141 IP46_TYPE_IP4 : IP46_TYPE_IP6);
143 rmp = vl_msg_api_alloc (sizeof (*rmp));
144 clib_memset (rmp, 0, sizeof (*rmp));
146 ntohs (VL_API_VXLAN_GBP_TUNNEL_DETAILS + REPLY_MSG_ID_BASE);
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));
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);
156 rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
157 rmp->context = context;
159 vl_api_send_msg (reg, (u8 *) rmp);
162 static void vl_api_vxlan_gbp_tunnel_dump_t_handler
163 (vl_api_vxlan_gbp_tunnel_dump_t * mp)
165 vl_api_registration_t *reg;
166 vxlan_gbp_main_t *vxm = &vxlan_gbp_main;
167 vxlan_gbp_tunnel_t *t;
170 reg = vl_api_client_index_to_registration (mp->client_index);
174 sw_if_index = ntohl (mp->sw_if_index);
176 if (~0 == sw_if_index)
179 pool_foreach (t, vxm->tunnels)
181 send_vxlan_gbp_tunnel_details(t, reg, mp->context);
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]))
192 t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
193 send_vxlan_gbp_tunnel_details (t, reg, mp->context);
197 #include <vxlan-gbp/vxlan_gbp.api.c>
198 static clib_error_t *
199 vxlan_gbp_api_hookup (vlib_main_t * vm)
202 * Set up the (msg_name, crc, message-id) table
204 msg_id_base = setup_message_id_table ();
209 VLIB_API_INIT_FUNCTION (vxlan_gbp_api_hookup);
212 * fd.io coding-style-patch-verification: ON
215 * eval: (c-set-style "gnu")