blob: 934fc661b47b208f94019695a62a8f1a5eba8210 [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 Ranns59ff9182019-12-29 23:55:18 +000028#include <vnet/tunnel/tunnel_types_api.h>
Neale Ranns5a8844b2019-04-16 07:15:35 +000029#include <vnet/ip/ip_types_api.h>
Pavel Kotucek1099b0d2016-12-20 08:13:14 +010030
31#include <vnet/vnet_msg_enum.h>
32
33#define vl_typedefs /* define message structures */
34#include <vnet/vnet_all_api_h.h>
35#undef vl_typedefs
36
37#define vl_endianfun /* define message structures */
38#include <vnet/vnet_all_api_h.h>
39#undef vl_endianfun
40
41/* instantiate all the print functions we know about */
42#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43#define vl_printfun
44#include <vnet/vnet_all_api_h.h>
45#undef vl_printfun
46
47#include <vlibapi/api_helper_macros.h>
48
49#define foreach_vpe_api_msg \
Neale Ranns5a8844b2019-04-16 07:15:35 +000050_(GRE_TUNNEL_ADD_DEL, gre_tunnel_add_del) \
Pavel Kotucek1099b0d2016-12-20 08:13:14 +010051_(GRE_TUNNEL_DUMP, gre_tunnel_dump)
52
Neale Ranns5a8844b2019-04-16 07:15:35 +000053static int
54gre_tunnel_type_decode (vl_api_gre_tunnel_type_t in, gre_tunnel_type_t * out)
Pavel Kotucek1099b0d2016-12-20 08:13:14 +010055{
Neale Ranns5a8844b2019-04-16 07:15:35 +000056 switch (in)
57 {
Neale Ranns5f8f6172019-04-18 10:23:56 +000058#define _(n, v) \
59 case GRE_API_TUNNEL_TYPE_##n: \
60 *out = GRE_TUNNEL_TYPE_##n; \
61 return (0);
62 foreach_gre_tunnel_type
63#undef _
Neale Ranns5a8844b2019-04-16 07:15:35 +000064 }
65
66 return (VNET_API_ERROR_INVALID_VALUE);
67}
68
69static vl_api_gre_tunnel_type_t
70gre_tunnel_type_encode (gre_tunnel_type_t in)
71{
72 vl_api_gre_tunnel_type_t out = GRE_API_TUNNEL_TYPE_L3;
73
74 switch (in)
75 {
Neale Ranns5f8f6172019-04-18 10:23:56 +000076#define _(n, v) \
77 case GRE_TUNNEL_TYPE_##n: \
78 out = GRE_API_TUNNEL_TYPE_##n; \
79 break;
80 foreach_gre_tunnel_type
81#undef _
Neale Ranns5a8844b2019-04-16 07:15:35 +000082 }
83
Neale Ranns5f8f6172019-04-18 10:23:56 +000084 return (out);
85}
86
Neale Ranns5a8844b2019-04-16 07:15:35 +000087static void vl_api_gre_tunnel_add_del_t_handler
88 (vl_api_gre_tunnel_add_del_t * mp)
89{
90 vnet_gre_tunnel_add_del_args_t _a = { }, *a = &_a;
91 vl_api_gre_tunnel_add_del_reply_t *rmp;
Neale Rannse5b94dd2019-12-31 05:13:14 +000092 tunnel_encap_decap_flags_t flags;
Neale Ranns5a8844b2019-04-16 07:15:35 +000093 u32 sw_if_index = ~0;
94 ip46_type_t itype[2];
95 int rv = 0;
96
97 itype[0] = ip_address_decode (&mp->tunnel.src, &a->src);
98 itype[1] = ip_address_decode (&mp->tunnel.dst, &a->dst);
99
100 if (itype[0] != itype[1])
101 {
102 rv = VNET_API_ERROR_INVALID_PROTOCOL;
103 goto out;
104 }
105
106 if (ip46_address_is_equal (&a->src, &a->dst))
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100107 {
108 rv = VNET_API_ERROR_SAME_SRC_DST;
109 goto out;
110 }
Neale Ranns5a8844b2019-04-16 07:15:35 +0000111
112 rv = gre_tunnel_type_decode (mp->tunnel.type, &a->type);
113
114 if (rv)
115 goto out;
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100116
Neale Ranns59ff9182019-12-29 23:55:18 +0000117 rv = tunnel_mode_decode (mp->tunnel.mode, &a->mode);
Neale Ranns5f8f6172019-04-18 10:23:56 +0000118
119 if (rv)
120 goto out;
121
Neale Rannse5b94dd2019-12-31 05:13:14 +0000122 rv = tunnel_encap_decap_flags_decode (mp->tunnel.flags, &flags);
123
124 if (rv)
125 goto out;
126
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100127 a->is_add = mp->is_add;
Neale Ranns5a8844b2019-04-16 07:15:35 +0000128 a->is_ipv6 = (itype[0] == IP46_TYPE_IP6);
129 a->instance = ntohl (mp->tunnel.instance);
130 a->session_id = ntohs (mp->tunnel.session_id);
Neale Ranns5f8f6172019-04-18 10:23:56 +0000131 a->outer_table_id = ntohl (mp->tunnel.outer_table_id);
Neale Rannse5b94dd2019-12-31 05:13:14 +0000132 a->flags = flags;
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100133
Neale Ranns5a8844b2019-04-16 07:15:35 +0000134 rv = vnet_gre_tunnel_add_del (a, &sw_if_index);
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100135
136out:
137 /* *INDENT-OFF* */
Neale Ranns5a8844b2019-04-16 07:15:35 +0000138 REPLY_MACRO2(VL_API_GRE_TUNNEL_ADD_DEL_REPLY,
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100139 ({
140 rmp->sw_if_index = ntohl (sw_if_index);
141 }));
142 /* *INDENT-ON* */
143}
144
145static void send_gre_tunnel_details
Florin Coras6c4dae22018-01-09 06:39:23 -0800146 (gre_tunnel_t * t, vl_api_registration_t * reg, u32 context)
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100147{
148 vl_api_gre_tunnel_details_t *rmp;
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100149
150 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400151 clib_memset (rmp, 0, sizeof (*rmp));
John Loa43ccae2018-02-13 17:15:23 -0500152 rmp->_vl_msg_id = htons (VL_API_GRE_TUNNEL_DETAILS);
Neale Ranns5a8844b2019-04-16 07:15:35 +0000153
154 ip_address_encode (&t->tunnel_src, IP46_TYPE_ANY, &rmp->tunnel.src);
155 ip_address_encode (&t->tunnel_dst.fp_addr, IP46_TYPE_ANY, &rmp->tunnel.dst);
156
Neale Ranns5f8f6172019-04-18 10:23:56 +0000157 rmp->tunnel.outer_table_id =
Neale Ranns5a8844b2019-04-16 07:15:35 +0000158 htonl (fib_table_get_table_id
159 (t->outer_fib_index, t->tunnel_dst.fp_proto));
160
161 rmp->tunnel.type = gre_tunnel_type_encode (t->type);
Neale Ranns59ff9182019-12-29 23:55:18 +0000162 rmp->tunnel.mode = tunnel_mode_encode (t->mode);
Neale Ranns5a8844b2019-04-16 07:15:35 +0000163 rmp->tunnel.instance = htonl (t->user_instance);
164 rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
165 rmp->tunnel.session_id = htons (t->session_id);
Neale Ranns5f8f6172019-04-18 10:23:56 +0000166
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100167 rmp->context = context;
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100168
Florin Coras6c4dae22018-01-09 06:39:23 -0800169 vl_api_send_msg (reg, (u8 *) rmp);
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100170}
171
172static void
173vl_api_gre_tunnel_dump_t_handler (vl_api_gre_tunnel_dump_t * mp)
174{
Florin Coras6c4dae22018-01-09 06:39:23 -0800175 vl_api_registration_t *reg;
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100176 gre_main_t *gm = &gre_main;
177 gre_tunnel_t *t;
178 u32 sw_if_index;
179
Florin Coras6c4dae22018-01-09 06:39:23 -0800180 reg = vl_api_client_index_to_registration (mp->client_index);
181 if (!reg)
182 return;
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100183
184 sw_if_index = ntohl (mp->sw_if_index);
185
186 if (~0 == sw_if_index)
187 {
188 /* *INDENT-OFF* */
189 pool_foreach (t, gm->tunnels,
190 ({
Florin Coras6c4dae22018-01-09 06:39:23 -0800191 send_gre_tunnel_details(t, reg, mp->context);
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100192 }));
193 /* *INDENT-ON* */
194 }
195 else
196 {
197 if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) ||
198 (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
199 {
200 return;
201 }
202 t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
Florin Coras6c4dae22018-01-09 06:39:23 -0800203 send_gre_tunnel_details (t, reg, mp->context);
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100204 }
205}
206
207/*
208 * gre_api_hookup
209 * Add vpe's API message handlers to the table.
Jim Thompsonf324dec2019-04-08 03:22:21 -0500210 * vlib has already mapped shared memory and
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100211 * added the client registration handlers.
212 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
213 */
214#define vl_msg_name_crc_list
215#include <vnet/vnet_all_api_h.h>
216#undef vl_msg_name_crc_list
217
218static void
219setup_message_id_table (api_main_t * am)
220{
221#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
222 foreach_vl_msg_name_crc_gre;
223#undef _
224}
225
226static clib_error_t *
227gre_api_hookup (vlib_main_t * vm)
228{
Dave Barach39d69112019-11-27 11:42:13 -0500229 api_main_t *am = vlibapi_get_main ();
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100230
231#define _(N,n) \
232 vl_msg_api_set_handlers(VL_API_##N, #n, \
233 vl_api_##n##_t_handler, \
234 vl_noop_handler, \
235 vl_api_##n##_t_endian, \
236 vl_api_##n##_t_print, \
237 sizeof(vl_api_##n##_t), 1);
238 foreach_vpe_api_msg;
239#undef _
240
241 /*
242 * Set up the (msg_name, crc, message-id) table
243 */
244 setup_message_id_table (am);
245
246 return 0;
247}
248
249VLIB_API_INIT_FUNCTION (gre_api_hookup);
250
251/*
252 * fd.io coding-style-patch-verification: ON
253 *
254 * Local Variables:
255 * eval: (c-set-style "gnu")
256 * End:
257 */