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/vnet_msg_enum.h>
26 #define vl_typedefs /* define message structures */
27 #include <vnet/vnet_all_api_h.h>
30 #define vl_endianfun /* define message structures */
31 #include <vnet/vnet_all_api_h.h>
34 /* instantiate all the print functions we know about */
35 #define vl_print(handle, ...) vlib_cli_output(handle, __VA_ARGS__)
37 #include <vnet/vnet_all_api_h.h>
40 #include <vlibapi/api_helper_macros.h>
42 #define foreach_vpe_api_msg \
43 _(IPIP_ADD_TUNNEL, ipip_add_tunnel) \
44 _(IPIP_DEL_TUNNEL, ipip_del_tunnel) \
45 _(IPIP_6RD_ADD_TUNNEL, ipip_6rd_add_tunnel) \
46 _(IPIP_6RD_DEL_TUNNEL, ipip_6rd_del_tunnel) \
47 _(IPIP_TUNNEL_DUMP, ipip_tunnel_dump)
50 vl_api_ipip_add_tunnel_t_handler (vl_api_ipip_add_tunnel_t * mp)
52 vl_api_ipip_add_tunnel_reply_t *rmp;
54 u32 fib_index, sw_if_index = ~0;
55 ip46_address_t src = ip46_address_initializer, dst =
56 ip46_address_initializer;
58 /* ip addresses sent in network byte order */
61 clib_memcpy (&src.ip6, mp->src_address, 16);
62 clib_memcpy (&dst.ip6, mp->dst_address, 16);
66 clib_memcpy (&src.ip4, mp->src_address, 4);
67 clib_memcpy (&dst.ip4, mp->dst_address, 4);
71 fib_table_find (fib_ip_proto (mp->is_ipv6), ntohl (mp->table_id));
75 rv = VNET_API_ERROR_NO_SUCH_FIB;
79 rv = ipip_add_tunnel ((mp->is_ipv6 ?
82 ntohl (mp->instance), &src, &dst,
83 fib_index, mp->tc_tos, &sw_if_index);
87 REPLY_MACRO2(VL_API_IPIP_ADD_TUNNEL_REPLY,
89 rmp->sw_if_index = ntohl(sw_if_index);
95 vl_api_ipip_del_tunnel_t_handler (vl_api_ipip_del_tunnel_t * mp)
97 vl_api_ipip_del_tunnel_reply_t *rmp;
99 int rv = ipip_del_tunnel (ntohl (mp->sw_if_index));
101 REPLY_MACRO (VL_API_IPIP_DEL_TUNNEL_REPLY);
105 send_ipip_tunnel_details (ipip_tunnel_t * t,
106 vl_api_registration_t * reg, u32 context)
108 vl_api_ipip_tunnel_details_t *rmp;
109 bool is_ipv6 = t->transport == IPIP_TRANSPORT_IP6 ? true : false;
112 rmp = vl_msg_api_alloc (sizeof (*rmp));
113 clib_memset (rmp, 0, sizeof (*rmp));
114 rmp->_vl_msg_id = htons (VL_API_IPIP_TUNNEL_DETAILS);
117 clib_memcpy (rmp->src_address, &t->tunnel_src.ip6.as_u8, 16);
118 clib_memcpy (rmp->dst_address, &t->tunnel_dst.ip6.as_u8, 16);
119 ft = fib_table_get (t->fib_index, FIB_PROTOCOL_IP6);
120 rmp->fib_index = htonl (ft->ft_table_id);
124 clib_memcpy (rmp->src_address, &t->tunnel_src.ip4.as_u8, 4);
125 clib_memcpy (rmp->dst_address, &t->tunnel_dst.ip4.as_u8, 4);
126 ft = fib_table_get (t->fib_index, FIB_PROTOCOL_IP4);
127 rmp->fib_index = htonl (ft->ft_table_id);
129 rmp->instance = htonl (t->user_instance);
130 rmp->sw_if_index = htonl (t->sw_if_index);
131 rmp->context = context;
132 rmp->is_ipv6 = is_ipv6;
134 vl_api_send_msg (reg, (u8 *) rmp);
138 vl_api_ipip_tunnel_dump_t_handler (vl_api_ipip_tunnel_dump_t * mp)
140 vl_api_registration_t *reg;
141 ipip_main_t *gm = &ipip_main;
145 reg = vl_api_client_index_to_registration (mp->client_index);
149 sw_if_index = ntohl (mp->sw_if_index);
151 if (sw_if_index == ~0)
154 pool_foreach(t, gm->tunnels,
155 ({ send_ipip_tunnel_details(t, reg, mp->context); }));
160 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
162 send_ipip_tunnel_details (t, reg, mp->context);
167 vl_api_ipip_6rd_add_tunnel_t_handler (vl_api_ipip_6rd_add_tunnel_t * mp)
169 vl_api_ipip_6rd_add_tunnel_reply_t *rmp;
170 u32 sixrd_tunnel_index, ip4_fib_index, ip6_fib_index;
173 ip4_fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->ip4_table_id));
174 ip6_fib_index = fib_table_find (FIB_PROTOCOL_IP6, ntohl (mp->ip6_table_id));
176 if (~0 == ip4_fib_index || ~0 == ip6_fib_index)
179 rv = VNET_API_ERROR_NO_SUCH_FIB;
183 rv = sixrd_add_tunnel ((ip6_address_t *) & mp->ip6_prefix,
185 (ip4_address_t *) & mp->ip4_prefix,
187 (ip4_address_t *) & mp->ip4_src,
189 ip4_fib_index, ip6_fib_index,
190 &sixrd_tunnel_index);
194 REPLY_MACRO2 (VL_API_IPIP_6RD_ADD_TUNNEL_REPLY,
196 rmp->sw_if_index = htonl (sixrd_tunnel_index);
202 vl_api_ipip_6rd_del_tunnel_t_handler (vl_api_ipip_6rd_del_tunnel_t * mp)
204 vl_api_ipip_6rd_del_tunnel_reply_t *rmp;
206 int rv = sixrd_del_tunnel (ntohl (mp->sw_if_index));
208 REPLY_MACRO (VL_API_IPIP_6RD_DEL_TUNNEL_REPLY);
213 * Add vpe's API message handlers to the table.
214 * vlib has alread mapped shared memory and
215 * added the client registration handlers.
216 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
218 #define vl_msg_name_crc_list
219 #include <vnet/vnet_all_api_h.h>
220 #undef vl_msg_name_crc_list
223 setup_message_id_table (api_main_t * am)
225 #define _(id, n, crc) vl_msg_api_add_msg_name_crc(am, #n "_" #crc, id);
226 foreach_vl_msg_name_crc_ipip;
230 static clib_error_t *
231 ipip_api_hookup (vlib_main_t * vm)
233 api_main_t *am = &api_main;
236 vl_msg_api_set_handlers(VL_API_##N, #n, vl_api_##n##_t_handler, \
237 vl_noop_handler, vl_api_##n##_t_endian, \
238 vl_api_##n##_t_print, sizeof(vl_api_##n##_t), 1);
243 * Set up the (msg_name, crc, message-id) table
245 setup_message_id_table (am);
250 VLIB_API_INIT_FUNCTION (ipip_api_hookup);
253 * fd.io coding-style-patch-verification: ON
256 * eval: (c-set-style "gnu")