blob: e83635a782039321712135a5db61c42a82ee1963 [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
Neale Ranns119c0d72020-11-26 13:12:37 +000031#include <vnet/gre/gre.api_enum.h>
32#include <vnet/gre/gre.api_types.h>
Pavel Kotucek1099b0d2016-12-20 08:13:14 +010033
Neale Ranns119c0d72020-11-26 13:12:37 +000034#define REPLY_MSG_ID_BASE gre_main.msg_id_base
Pavel Kotucek1099b0d2016-12-20 08:13:14 +010035#include <vlibapi/api_helper_macros.h>
36
Neale Ranns5a8844b2019-04-16 07:15:35 +000037static int
38gre_tunnel_type_decode (vl_api_gre_tunnel_type_t in, gre_tunnel_type_t * out)
Pavel Kotucek1099b0d2016-12-20 08:13:14 +010039{
Neale Ranns5a8844b2019-04-16 07:15:35 +000040 switch (in)
41 {
Neale Ranns5f8f6172019-04-18 10:23:56 +000042#define _(n, v) \
43 case GRE_API_TUNNEL_TYPE_##n: \
44 *out = GRE_TUNNEL_TYPE_##n; \
45 return (0);
46 foreach_gre_tunnel_type
47#undef _
Neale Ranns5a8844b2019-04-16 07:15:35 +000048 }
49
50 return (VNET_API_ERROR_INVALID_VALUE);
51}
52
53static vl_api_gre_tunnel_type_t
54gre_tunnel_type_encode (gre_tunnel_type_t in)
55{
56 vl_api_gre_tunnel_type_t out = GRE_API_TUNNEL_TYPE_L3;
57
58 switch (in)
59 {
Neale Ranns5f8f6172019-04-18 10:23:56 +000060#define _(n, v) \
61 case GRE_TUNNEL_TYPE_##n: \
62 out = GRE_API_TUNNEL_TYPE_##n; \
63 break;
64 foreach_gre_tunnel_type
65#undef _
Neale Ranns5a8844b2019-04-16 07:15:35 +000066 }
67
Neale Ranns5f8f6172019-04-18 10:23:56 +000068 return (out);
69}
70
Neale Ranns5a8844b2019-04-16 07:15:35 +000071static void vl_api_gre_tunnel_add_del_t_handler
72 (vl_api_gre_tunnel_add_del_t * mp)
73{
74 vnet_gre_tunnel_add_del_args_t _a = { }, *a = &_a;
75 vl_api_gre_tunnel_add_del_reply_t *rmp;
Neale Rannse5b94dd2019-12-31 05:13:14 +000076 tunnel_encap_decap_flags_t flags;
Neale Ranns5a8844b2019-04-16 07:15:35 +000077 u32 sw_if_index = ~0;
78 ip46_type_t itype[2];
79 int rv = 0;
80
81 itype[0] = ip_address_decode (&mp->tunnel.src, &a->src);
82 itype[1] = ip_address_decode (&mp->tunnel.dst, &a->dst);
83
84 if (itype[0] != itype[1])
85 {
86 rv = VNET_API_ERROR_INVALID_PROTOCOL;
87 goto out;
88 }
89
90 if (ip46_address_is_equal (&a->src, &a->dst))
Pavel Kotucek1099b0d2016-12-20 08:13:14 +010091 {
92 rv = VNET_API_ERROR_SAME_SRC_DST;
93 goto out;
94 }
Neale Ranns5a8844b2019-04-16 07:15:35 +000095
96 rv = gre_tunnel_type_decode (mp->tunnel.type, &a->type);
97
98 if (rv)
99 goto out;
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100100
Neale Ranns59ff9182019-12-29 23:55:18 +0000101 rv = tunnel_mode_decode (mp->tunnel.mode, &a->mode);
Neale Ranns5f8f6172019-04-18 10:23:56 +0000102
103 if (rv)
104 goto out;
105
Neale Rannse5b94dd2019-12-31 05:13:14 +0000106 rv = tunnel_encap_decap_flags_decode (mp->tunnel.flags, &flags);
107
108 if (rv)
109 goto out;
110
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100111 a->is_add = mp->is_add;
Neale Ranns5a8844b2019-04-16 07:15:35 +0000112 a->is_ipv6 = (itype[0] == IP46_TYPE_IP6);
113 a->instance = ntohl (mp->tunnel.instance);
114 a->session_id = ntohs (mp->tunnel.session_id);
Neale Ranns5f8f6172019-04-18 10:23:56 +0000115 a->outer_table_id = ntohl (mp->tunnel.outer_table_id);
Neale Rannse5b94dd2019-12-31 05:13:14 +0000116 a->flags = flags;
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100117
Neale Ranns5a8844b2019-04-16 07:15:35 +0000118 rv = vnet_gre_tunnel_add_del (a, &sw_if_index);
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100119
120out:
121 /* *INDENT-OFF* */
Neale Ranns5a8844b2019-04-16 07:15:35 +0000122 REPLY_MACRO2(VL_API_GRE_TUNNEL_ADD_DEL_REPLY,
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100123 ({
124 rmp->sw_if_index = ntohl (sw_if_index);
125 }));
126 /* *INDENT-ON* */
127}
128
129static void send_gre_tunnel_details
Neale Ranns119c0d72020-11-26 13:12:37 +0000130 (gre_tunnel_t * t, vl_api_gre_tunnel_dump_t * mp)
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100131{
132 vl_api_gre_tunnel_details_t *rmp;
Neale Ranns119c0d72020-11-26 13:12:37 +0000133 int rv = 0;
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100134
Neale Ranns119c0d72020-11-26 13:12:37 +0000135 /* *INDENT-OFF* */
136 REPLY_MACRO_DETAILS2(VL_API_GRE_TUNNEL_DETAILS,
137 ({
138 ip_address_encode (&t->tunnel_src, IP46_TYPE_ANY, &rmp->tunnel.src);
139 ip_address_encode (&t->tunnel_dst.fp_addr, IP46_TYPE_ANY, &rmp->tunnel.dst);
Neale Ranns5a8844b2019-04-16 07:15:35 +0000140
Neale Ranns119c0d72020-11-26 13:12:37 +0000141 rmp->tunnel.outer_table_id =
142 htonl (fib_table_get_table_id
143 (t->outer_fib_index, t->tunnel_dst.fp_proto));
Neale Ranns5a8844b2019-04-16 07:15:35 +0000144
Neale Ranns119c0d72020-11-26 13:12:37 +0000145 rmp->tunnel.type = gre_tunnel_type_encode (t->type);
146 rmp->tunnel.mode = tunnel_mode_encode (t->mode);
147 rmp->tunnel.instance = htonl (t->user_instance);
148 rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
149 rmp->tunnel.session_id = htons (t->session_id);
150 }));
151 /* *INDENT-ON* */
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100152}
153
154static void
155vl_api_gre_tunnel_dump_t_handler (vl_api_gre_tunnel_dump_t * mp)
156{
Florin Coras6c4dae22018-01-09 06:39:23 -0800157 vl_api_registration_t *reg;
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100158 gre_main_t *gm = &gre_main;
159 gre_tunnel_t *t;
160 u32 sw_if_index;
161
Florin Coras6c4dae22018-01-09 06:39:23 -0800162 reg = vl_api_client_index_to_registration (mp->client_index);
163 if (!reg)
164 return;
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100165
166 sw_if_index = ntohl (mp->sw_if_index);
167
168 if (~0 == sw_if_index)
169 {
170 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100171 pool_foreach (t, gm->tunnels)
172 {
Neale Ranns119c0d72020-11-26 13:12:37 +0000173 send_gre_tunnel_details(t, mp);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100174 }
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100175 /* *INDENT-ON* */
176 }
Neale Ranns119c0d72020-11-26 13:12:37 +0000177
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100178 else
179 {
180 if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) ||
181 (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
182 {
183 return;
184 }
185 t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
Neale Ranns119c0d72020-11-26 13:12:37 +0000186 send_gre_tunnel_details (t, mp);
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100187 }
188}
189
190/*
191 * gre_api_hookup
192 * Add vpe's API message handlers to the table.
Jim Thompsonf324dec2019-04-08 03:22:21 -0500193 * vlib has already mapped shared memory and
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100194 * added the client registration handlers.
195 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
196 */
Neale Ranns119c0d72020-11-26 13:12:37 +0000197/* API definitions */
198#include <vnet/format_fns.h>
199#include <vnet/gre/gre.api.c>
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100200
201static clib_error_t *
202gre_api_hookup (vlib_main_t * vm)
203{
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100204 /*
205 * Set up the (msg_name, crc, message-id) table
206 */
Neale Ranns119c0d72020-11-26 13:12:37 +0000207 gre_main.msg_id_base = setup_message_id_table ();
Pavel Kotucek1099b0d2016-12-20 08:13:14 +0100208
209 return 0;
210}
211
212VLIB_API_INIT_FUNCTION (gre_api_hookup);
213
214/*
215 * fd.io coding-style-patch-verification: ON
216 *
217 * Local Variables:
218 * eval: (c-set-style "gnu")
219 * End:
220 */