2 * ipip_api.c - ipip api
4 * Copyright (c) 2018 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vlibmemory/api.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/fib/fib_table.h>
21 #include <vnet/interface.h>
22 #include <vnet/ipip/ipip.h>
23 #include <vnet/vnet.h>
24 #include <vnet/ip/ip_types_api.h>
26 #include <vnet/ipip/ipip.api_enum.h>
27 #include <vnet/ipip/ipip.api_types.h>
29 #define REPLY_MSG_ID_BASE im->msg_id_base
30 #include <vlibapi/api_helper_macros.h>
33 vl_api_ipip_add_tunnel_t_handler (vl_api_ipip_add_tunnel_t * mp)
35 ipip_main_t *im = &ipip_main;
36 vl_api_ipip_add_tunnel_reply_t *rmp;
38 u32 fib_index, sw_if_index = ~0;
39 ip46_address_t src, dst;
42 itype[0] = ip_address_decode (&mp->tunnel.src, &src);
43 itype[1] = ip_address_decode (&mp->tunnel.dst, &dst);
45 if (itype[0] != itype[1])
47 rv = VNET_API_ERROR_INVALID_PROTOCOL;
51 if (ip46_address_is_equal (&src, &dst))
53 rv = VNET_API_ERROR_SAME_SRC_DST;
57 fib_index = fib_table_find (fib_proto_from_ip46 (itype[0]),
58 ntohl (mp->tunnel.table_id));
62 rv = VNET_API_ERROR_NO_SUCH_FIB;
66 rv = ipip_add_tunnel ((itype[0] == IP46_TYPE_IP6 ?
69 ntohl (mp->tunnel.instance), &src, &dst,
70 fib_index, mp->tunnel.tc_tos, &sw_if_index);
75 REPLY_MACRO2(VL_API_IPIP_ADD_TUNNEL_REPLY,
77 rmp->sw_if_index = ntohl(sw_if_index);
83 vl_api_ipip_del_tunnel_t_handler (vl_api_ipip_del_tunnel_t * mp)
85 ipip_main_t *im = &ipip_main;
86 vl_api_ipip_del_tunnel_reply_t *rmp;
88 int rv = ipip_del_tunnel (ntohl (mp->sw_if_index));
90 REPLY_MACRO (VL_API_IPIP_DEL_TUNNEL_REPLY);
94 send_ipip_tunnel_details (ipip_tunnel_t * t, vl_api_ipip_tunnel_dump_t * mp)
96 ipip_main_t *im = &ipip_main;
97 vl_api_ipip_tunnel_details_t *rmp;
98 bool is_ipv6 = t->transport == IPIP_TRANSPORT_IP6 ? true : false;
102 ft = fib_table_get (t->fib_index, (is_ipv6 ? FIB_PROTOCOL_IP6 :
106 REPLY_MACRO_DETAILS2(VL_API_IPIP_TUNNEL_DETAILS,
108 ip_address_encode (&t->tunnel_src, IP46_TYPE_ANY, &rmp->tunnel.src);
109 ip_address_encode (&t->tunnel_dst, IP46_TYPE_ANY, &rmp->tunnel.dst);
110 rmp->tunnel.table_id = htonl (ft->ft_table_id);
111 rmp->tunnel.instance = htonl (t->user_instance);
112 rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
118 vl_api_ipip_tunnel_dump_t_handler (vl_api_ipip_tunnel_dump_t * mp)
120 ipip_main_t *im = &ipip_main;
124 sw_if_index = ntohl (mp->sw_if_index);
126 if (sw_if_index == ~0)
129 pool_foreach(t, im->tunnels,
131 send_ipip_tunnel_details(t, mp);
137 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
139 send_ipip_tunnel_details (t, mp);
144 vl_api_ipip_6rd_add_tunnel_t_handler (vl_api_ipip_6rd_add_tunnel_t * mp)
146 ipip_main_t *im = &ipip_main;
147 vl_api_ipip_6rd_add_tunnel_reply_t *rmp;
148 u32 sixrd_tunnel_index, ip4_fib_index, ip6_fib_index;
151 ip4_fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->ip4_table_id));
152 ip6_fib_index = fib_table_find (FIB_PROTOCOL_IP6, ntohl (mp->ip6_table_id));
154 if (~0 == ip4_fib_index || ~0 == ip6_fib_index)
157 rv = VNET_API_ERROR_NO_SUCH_FIB;
161 rv = sixrd_add_tunnel ((ip6_address_t *) & mp->ip6_prefix.address,
163 (ip4_address_t *) & mp->ip4_prefix.address,
165 (ip4_address_t *) & mp->ip4_src,
167 ip4_fib_index, ip6_fib_index,
168 &sixrd_tunnel_index);
172 REPLY_MACRO2 (VL_API_IPIP_6RD_ADD_TUNNEL_REPLY,
174 rmp->sw_if_index = htonl (sixrd_tunnel_index);
180 vl_api_ipip_6rd_del_tunnel_t_handler (vl_api_ipip_6rd_del_tunnel_t * mp)
182 ipip_main_t *im = &ipip_main;
183 vl_api_ipip_6rd_del_tunnel_reply_t *rmp;
185 int rv = sixrd_del_tunnel (ntohl (mp->sw_if_index));
187 REPLY_MACRO (VL_API_IPIP_6RD_DEL_TUNNEL_REPLY);
192 * Add vpe's API message handlers to the table.
193 * vlib has already mapped shared memory and
194 * added the client registration handlers.
195 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
197 /* API definitions */
198 #include <vnet/format_fns.h>
199 #include <vnet/ipip/ipip.api.c>
201 static clib_error_t *
202 ipip_api_hookup (vlib_main_t * vm)
204 ipip_main_t *im = &ipip_main;
207 * Set up the (msg_name, crc, message-id) table
209 im->msg_id_base = setup_message_id_table ();
214 VLIB_API_INIT_FUNCTION (ipip_api_hookup);
217 * fd.io coding-style-patch-verification: ON
220 * eval: (c-set-style "gnu")