blob: be91f7587774581440012a1df21c53bc7a68a02c [file] [log] [blame]
Pavel Kotucek1099b0d2016-12-20 08:13:14 +01001/*
2 *------------------------------------------------------------------
3 * gre_api.c - gre api
4 *
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:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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 *------------------------------------------------------------------
18 */
19
20#include <vnet/vnet.h>
21#include <vlibmemory/api.h>
22
23#include <vnet/interface.h>
24#include <vnet/api_errno.h>
25
26#include <vnet/gre/gre.h>
27#include <vnet/fib/fib_table.h>
Neale Ranns5a8844b2019-04-16 07:15:35 +000028#include <vnet/ip/ip_types_api.h>
Pavel Kotucek1099b0d2016-12-20 08:13:14 +010029
30#include <vnet/vnet_msg_enum.h>
31
32#define vl_typedefs /* define message structures */
33#include <vnet/vnet_all_api_h.h>
34#undef vl_typedefs
35
36#define vl_endianfun /* define message structures */
37#include <vnet/vnet_all_api_h.h>
38#undef vl_endianfun
39
40/* instantiate all the print functions we know about */
41#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42#define vl_printfun
43#include <vnet/vnet_all_api_h.h>
44#undef vl_printfun
45
46#include <vlibapi/api_helper_macros.h>
47
48#define foreach_vpe_api_msg \
Neale Ranns5a8844b2019-04-16 07:15:35 +000049_(GRE_TUNNEL_ADD_DEL, gre_tunnel_add_del) \
Pavel Kotucek1099b0d2016-12-20 08:13:14 +010050_(GRE_TUNNEL_DUMP, gre_tunnel_dump)
51
Neale Ranns5a8844b2019-04-16 07:15:35 +000052static int
53gre_tunnel_type_decode (vl_api_gre_tunnel_type_t in, gre_tunnel_type_t * out)
Pavel Kotucek1099b0d2016-12-20 08:13:14 +010054{
Neale Ranns5a8844b2019-04-16 07:15:35 +000055 switch (in)
56 {
Neale Ranns5f8f6172019-04-18 10:23:56 +000057#define _(n, v) \
58 case GRE_API_TUNNEL_TYPE_##n: \
59 *out = GRE_TUNNEL_TYPE_##n; \
60 return (0);
61 foreach_gre_tunnel_type
62#undef _
Neale Ranns5a8844b2019-04-16 07:15:35 +000063 }
64
65 return (VNET_API_ERROR_INVALID_VALUE);
66}
67
68static vl_api_gre_tunnel_type_t
69gre_tunnel_type_encode (gre_tunnel_type_t in)
70{
71 vl_api_gre_tunnel_type_t out = GRE_API_TUNNEL_TYPE_L3;
72
73 switch (in)
74 {
Neale Ranns5f8f6172019-04-18 10:23:56 +000075#define _(n, v) \
76 case GRE_TUNNEL_TYPE_##n: \
77 out = GRE_API_TUNNEL_TYPE_##n; \
78 break;
79 foreach_gre_tunnel_type
80#undef _
Neale Ranns5a8844b2019-04-16 07:15:35 +000081 }
82
Neale Ranns5f8f6172019-04-18 10:23:56 +000083 return (out);
84}
85
86static int
87gre_tunnel_mode_decode (vl_api_gre_tunnel_mode_t in, gre_tunnel_mode_t * out)
88{
89 switch (in)
90 {
91#define _(n, v) \
92 case GRE_API_TUNNEL_MODE_##n: \
93 *out = GRE_TUNNEL_MODE_##n; \
94 return (0);
95 foreach_gre_tunnel_mode
96#undef _
97 }
98
99 return (VNET_API_ERROR_INVALID_VALUE_2);
100}
101
102static vl_api_gre_tunnel_mode_t
103gre_tunnel_mode_encode (gre_tunnel_mode_t in)
104{
105 vl_api_gre_tunnel_mode_t out = GRE_API_TUNNEL_MODE_P2P;
106
107 switch (in)
108 {
109#define _(n, v) \
110 case GRE_TUNNEL_MODE_##n: \
111 out = GRE_API_TUNNEL_MODE_##n; \
112 break;
113 foreach_gre_tunnel_mode
114#undef _
115 }
Neale Ranns5a8844b2019-04-16 07:15:35 +0000116
117 return (out);
118}
119
120static void vl_api_gre_tunnel_add_del_t_handler
121 (vl_api_gre_tunnel_add_del_t * mp)
122{
123 vnet_gre_tunnel_add_del_args_t _a = { }, *a = &_a;
124 vl_api_gre_tunnel_add_del_reply_t *rmp;
125 u32 sw_if_index = ~0;
126 ip46_type_t itype[2];
127 int rv = 0;
128
129 itype[0] = ip_address_decode (&mp->tunnel.src, &a->src);
130 itype[1] = ip_address_decode (&mp->tunnel.dst, &a->dst);
131
132 if (itype[0] != itype[1])
133 {
134 rv = VNET_API_ERROR_INVALID_PROTOCOL;
135 goto out;
136 }
137
138 if (ip46_address_is_equal (&a->src, &a->dst))
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100139 {
140 rv = VNET_API_ERROR_SAME_SRC_DST;
141 goto out;
142 }
Neale Ranns5a8844b2019-04-16 07:15:35 +0000143
144 rv = gre_tunnel_type_decode (mp->tunnel.type, &a->type);
145
146 if (rv)
147 goto out;
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100148
Neale Ranns5f8f6172019-04-18 10:23:56 +0000149 rv = gre_tunnel_mode_decode (mp->tunnel.mode, &a->mode);
150
151 if (rv)
152 goto out;
153
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100154 a->is_add = mp->is_add;
Neale Ranns5a8844b2019-04-16 07:15:35 +0000155 a->is_ipv6 = (itype[0] == IP46_TYPE_IP6);
156 a->instance = ntohl (mp->tunnel.instance);
157 a->session_id = ntohs (mp->tunnel.session_id);
Neale Ranns5f8f6172019-04-18 10:23:56 +0000158 a->outer_table_id = ntohl (mp->tunnel.outer_table_id);
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100159
Neale Ranns5a8844b2019-04-16 07:15:35 +0000160 rv = vnet_gre_tunnel_add_del (a, &sw_if_index);
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100161
162out:
163 /* *INDENT-OFF* */
Neale Ranns5a8844b2019-04-16 07:15:35 +0000164 REPLY_MACRO2(VL_API_GRE_TUNNEL_ADD_DEL_REPLY,
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100165 ({
166 rmp->sw_if_index = ntohl (sw_if_index);
167 }));
168 /* *INDENT-ON* */
169}
170
171static void send_gre_tunnel_details
Florin Coras6c4dae22018-01-09 06:39:23 -0800172 (gre_tunnel_t * t, vl_api_registration_t * reg, u32 context)
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100173{
174 vl_api_gre_tunnel_details_t *rmp;
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100175
176 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400177 clib_memset (rmp, 0, sizeof (*rmp));
John Loa43ccae2018-02-13 17:15:23 -0500178 rmp->_vl_msg_id = htons (VL_API_GRE_TUNNEL_DETAILS);
Neale Ranns5a8844b2019-04-16 07:15:35 +0000179
180 ip_address_encode (&t->tunnel_src, IP46_TYPE_ANY, &rmp->tunnel.src);
181 ip_address_encode (&t->tunnel_dst.fp_addr, IP46_TYPE_ANY, &rmp->tunnel.dst);
182
Neale Ranns5f8f6172019-04-18 10:23:56 +0000183 rmp->tunnel.outer_table_id =
Neale Ranns5a8844b2019-04-16 07:15:35 +0000184 htonl (fib_table_get_table_id
185 (t->outer_fib_index, t->tunnel_dst.fp_proto));
186
187 rmp->tunnel.type = gre_tunnel_type_encode (t->type);
Neale Ranns5f8f6172019-04-18 10:23:56 +0000188 rmp->tunnel.mode = gre_tunnel_mode_encode (t->mode);
Neale Ranns5a8844b2019-04-16 07:15:35 +0000189 rmp->tunnel.instance = htonl (t->user_instance);
190 rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
191 rmp->tunnel.session_id = htons (t->session_id);
Neale Ranns5f8f6172019-04-18 10:23:56 +0000192
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100193 rmp->context = context;
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100194
Florin Coras6c4dae22018-01-09 06:39:23 -0800195 vl_api_send_msg (reg, (u8 *) rmp);
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100196}
197
198static void
199vl_api_gre_tunnel_dump_t_handler (vl_api_gre_tunnel_dump_t * mp)
200{
Florin Coras6c4dae22018-01-09 06:39:23 -0800201 vl_api_registration_t *reg;
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100202 gre_main_t *gm = &gre_main;
203 gre_tunnel_t *t;
204 u32 sw_if_index;
205
Florin Coras6c4dae22018-01-09 06:39:23 -0800206 reg = vl_api_client_index_to_registration (mp->client_index);
207 if (!reg)
208 return;
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100209
210 sw_if_index = ntohl (mp->sw_if_index);
211
212 if (~0 == sw_if_index)
213 {
214 /* *INDENT-OFF* */
215 pool_foreach (t, gm->tunnels,
216 ({
Florin Coras6c4dae22018-01-09 06:39:23 -0800217 send_gre_tunnel_details(t, reg, mp->context);
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100218 }));
219 /* *INDENT-ON* */
220 }
221 else
222 {
223 if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) ||
224 (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
225 {
226 return;
227 }
228 t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
Florin Coras6c4dae22018-01-09 06:39:23 -0800229 send_gre_tunnel_details (t, reg, mp->context);
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100230 }
231}
232
233/*
234 * gre_api_hookup
235 * Add vpe's API message handlers to the table.
Jim Thompsonf324dec2019-04-08 03:22:21 -0500236 * vlib has already mapped shared memory and
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100237 * added the client registration handlers.
238 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
239 */
240#define vl_msg_name_crc_list
241#include <vnet/vnet_all_api_h.h>
242#undef vl_msg_name_crc_list
243
244static void
245setup_message_id_table (api_main_t * am)
246{
247#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
248 foreach_vl_msg_name_crc_gre;
249#undef _
250}
251
252static clib_error_t *
253gre_api_hookup (vlib_main_t * vm)
254{
Dave Barach39d69112019-11-27 11:42:13 -0500255 api_main_t *am = vlibapi_get_main ();
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100256
257#define _(N,n) \
258 vl_msg_api_set_handlers(VL_API_##N, #n, \
259 vl_api_##n##_t_handler, \
260 vl_noop_handler, \
261 vl_api_##n##_t_endian, \
262 vl_api_##n##_t_print, \
263 sizeof(vl_api_##n##_t), 1);
264 foreach_vpe_api_msg;
265#undef _
266
267 /*
268 * Set up the (msg_name, crc, message-id) table
269 */
270 setup_message_id_table (am);
271
272 return 0;
273}
274
275VLIB_API_INIT_FUNCTION (gre_api_hookup);
276
277/*
278 * fd.io coding-style-patch-verification: ON
279 *
280 * Local Variables:
281 * eval: (c-set-style "gnu")
282 * End:
283 */