2 *------------------------------------------------------------------
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>
26 #include <vnet/gre/gre.h>
27 #include <vnet/fib/fib_table.h>
28 #include <vnet/ip/ip_types_api.h>
30 #include <vnet/vnet_msg_enum.h>
32 #define vl_typedefs /* define message structures */
33 #include <vnet/vnet_all_api_h.h>
36 #define vl_endianfun /* define message structures */
37 #include <vnet/vnet_all_api_h.h>
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43 #include <vnet/vnet_all_api_h.h>
46 #include <vlibapi/api_helper_macros.h>
48 #define foreach_vpe_api_msg \
49 _(GRE_TUNNEL_ADD_DEL, gre_tunnel_add_del) \
50 _(GRE_TUNNEL_DUMP, gre_tunnel_dump)
53 gre_tunnel_type_decode (vl_api_gre_tunnel_type_t in, gre_tunnel_type_t * out)
55 in = clib_net_to_host_u32 (in);
59 case GRE_API_TUNNEL_TYPE_L3:
60 *out = GRE_TUNNEL_TYPE_L3;
62 case GRE_API_TUNNEL_TYPE_TEB:
63 *out = GRE_TUNNEL_TYPE_TEB;
65 case GRE_API_TUNNEL_TYPE_ERSPAN:
66 *out = GRE_TUNNEL_TYPE_ERSPAN;
70 return (VNET_API_ERROR_INVALID_VALUE);
73 static vl_api_gre_tunnel_type_t
74 gre_tunnel_type_encode (gre_tunnel_type_t in)
76 vl_api_gre_tunnel_type_t out = GRE_API_TUNNEL_TYPE_L3;
80 case GRE_TUNNEL_TYPE_L3:
81 out = GRE_API_TUNNEL_TYPE_L3;
83 case GRE_TUNNEL_TYPE_TEB:
84 out = GRE_API_TUNNEL_TYPE_TEB;
86 case GRE_TUNNEL_TYPE_ERSPAN:
87 out = GRE_API_TUNNEL_TYPE_ERSPAN;
91 out = clib_net_to_host_u32 (out);
96 static void vl_api_gre_tunnel_add_del_t_handler
97 (vl_api_gre_tunnel_add_del_t * mp)
99 vnet_gre_tunnel_add_del_args_t _a = { }, *a = &_a;
100 vl_api_gre_tunnel_add_del_reply_t *rmp;
101 u32 sw_if_index = ~0;
102 ip46_type_t itype[2];
105 itype[0] = ip_address_decode (&mp->tunnel.src, &a->src);
106 itype[1] = ip_address_decode (&mp->tunnel.dst, &a->dst);
108 if (itype[0] != itype[1])
110 rv = VNET_API_ERROR_INVALID_PROTOCOL;
114 if (ip46_address_is_equal (&a->src, &a->dst))
116 rv = VNET_API_ERROR_SAME_SRC_DST;
120 rv = gre_tunnel_type_decode (mp->tunnel.type, &a->type);
125 a->is_add = mp->is_add;
126 a->is_ipv6 = (itype[0] == IP46_TYPE_IP6);
127 a->instance = ntohl (mp->tunnel.instance);
128 a->session_id = ntohs (mp->tunnel.session_id);
129 a->outer_fib_id = ntohl (mp->tunnel.outer_fib_id);
131 rv = vnet_gre_tunnel_add_del (a, &sw_if_index);
135 REPLY_MACRO2(VL_API_GRE_TUNNEL_ADD_DEL_REPLY,
137 rmp->sw_if_index = ntohl (sw_if_index);
142 static void send_gre_tunnel_details
143 (gre_tunnel_t * t, vl_api_registration_t * reg, u32 context)
145 vl_api_gre_tunnel_details_t *rmp;
147 rmp = vl_msg_api_alloc (sizeof (*rmp));
148 clib_memset (rmp, 0, sizeof (*rmp));
149 rmp->_vl_msg_id = htons (VL_API_GRE_TUNNEL_DETAILS);
151 ip_address_encode (&t->tunnel_src, IP46_TYPE_ANY, &rmp->tunnel.src);
152 ip_address_encode (&t->tunnel_dst.fp_addr, IP46_TYPE_ANY, &rmp->tunnel.dst);
154 rmp->tunnel.outer_fib_id =
155 htonl (fib_table_get_table_id
156 (t->outer_fib_index, t->tunnel_dst.fp_proto));
158 rmp->tunnel.type = gre_tunnel_type_encode (t->type);
159 rmp->tunnel.instance = htonl (t->user_instance);
160 rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
161 rmp->tunnel.session_id = htons (t->session_id);
162 rmp->context = context;
164 vl_api_send_msg (reg, (u8 *) rmp);
168 vl_api_gre_tunnel_dump_t_handler (vl_api_gre_tunnel_dump_t * mp)
170 vl_api_registration_t *reg;
171 gre_main_t *gm = &gre_main;
175 reg = vl_api_client_index_to_registration (mp->client_index);
179 sw_if_index = ntohl (mp->sw_if_index);
181 if (~0 == sw_if_index)
184 pool_foreach (t, gm->tunnels,
186 send_gre_tunnel_details(t, reg, mp->context);
192 if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) ||
193 (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
197 t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
198 send_gre_tunnel_details (t, reg, mp->context);
204 * Add vpe's API message handlers to the table.
205 * vlib has already mapped shared memory and
206 * added the client registration handlers.
207 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
209 #define vl_msg_name_crc_list
210 #include <vnet/vnet_all_api_h.h>
211 #undef vl_msg_name_crc_list
214 setup_message_id_table (api_main_t * am)
216 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
217 foreach_vl_msg_name_crc_gre;
221 static clib_error_t *
222 gre_api_hookup (vlib_main_t * vm)
224 api_main_t *am = &api_main;
227 vl_msg_api_set_handlers(VL_API_##N, #n, \
228 vl_api_##n##_t_handler, \
230 vl_api_##n##_t_endian, \
231 vl_api_##n##_t_print, \
232 sizeof(vl_api_##n##_t), 1);
237 * Set up the (msg_name, crc, message-id) table
239 setup_message_id_table (am);
244 VLIB_API_INIT_FUNCTION (gre_api_hookup);
247 * fd.io coding-style-patch-verification: ON
250 * eval: (c-set-style "gnu")