blob: 8c4a4cadc28b00c8133f6bfa09ebf71e02b9308e [file] [log] [blame]
Marco Varleseb598f1d2017-09-19 14:25:28 +02001/*
2 * Copyright (c) 2017 SUSE LLC.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *------------------------------------------------------------------
15 */
16
17#include <vnet/vnet.h>
18#include <vlibmemory/api.h>
19
20#include <vnet/interface.h>
21#include <vnet/api_errno.h>
22#include <vnet/feature/feature.h>
23#include <vnet/geneve/geneve.h>
24#include <vnet/fib/fib_table.h>
Jakub Grajciar2d3282e2019-10-01 12:04:56 +020025#include <vnet/ip/ip_types_api.h>
Marco Varleseb598f1d2017-09-19 14:25:28 +020026
Ole Troan3d812672020-09-15 10:53:34 +020027#include <vnet/geneve/geneve.api_enum.h>
28#include <vnet/geneve/geneve.api_types.h>
Marco Varleseb598f1d2017-09-19 14:25:28 +020029
Ole Troan3d812672020-09-15 10:53:34 +020030#define REPLY_MSG_ID_BASE gm->msg_id_base
Marco Varleseb598f1d2017-09-19 14:25:28 +020031#include <vlibapi/api_helper_macros.h>
32
Marco Varleseb598f1d2017-09-19 14:25:28 +020033static void
34 vl_api_sw_interface_set_geneve_bypass_t_handler
35 (vl_api_sw_interface_set_geneve_bypass_t * mp)
36{
Ole Troan3d812672020-09-15 10:53:34 +020037 geneve_main_t *gm = &geneve_main;
Marco Varleseb598f1d2017-09-19 14:25:28 +020038 vl_api_sw_interface_set_geneve_bypass_reply_t *rmp;
39 int rv = 0;
40 u32 sw_if_index = ntohl (mp->sw_if_index);
41
42 VALIDATE_SW_IF_INDEX (mp);
43
44 vnet_int_geneve_bypass_mode (sw_if_index, mp->is_ipv6, mp->enable);
45 BAD_SW_IF_INDEX_LABEL;
46
47 REPLY_MACRO (VL_API_SW_INTERFACE_SET_GENEVE_BYPASS_REPLY);
48}
49
50static void vl_api_geneve_add_del_tunnel_t_handler
51 (vl_api_geneve_add_del_tunnel_t * mp)
52{
Ole Troan3d812672020-09-15 10:53:34 +020053 geneve_main_t *gm = &geneve_main;
Marco Varleseb598f1d2017-09-19 14:25:28 +020054 vl_api_geneve_add_del_tunnel_reply_t *rmp;
55 int rv = 0;
56 ip4_main_t *im = &ip4_main;
57
58 uword *p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
59 if (!p)
60 {
61 rv = VNET_API_ERROR_NO_SUCH_FIB;
62 goto out;
63 }
64
65 vnet_geneve_add_del_tunnel_args_t a = {
66 .is_add = mp->is_add,
Jakub Grajciar2d3282e2019-10-01 12:04:56 +020067 .is_ip6 = mp->remote_address.af,
Marco Varleseb598f1d2017-09-19 14:25:28 +020068 .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index),
69 .encap_fib_index = p[0],
70 .decap_next_index = ntohl (mp->decap_next_index),
71 .vni = ntohl (mp->vni),
Marco Varleseb598f1d2017-09-19 14:25:28 +020072 };
73
Jakub Grajciar2d3282e2019-10-01 12:04:56 +020074 ip_address_decode (&mp->remote_address, &a.remote);
75 ip_address_decode (&mp->local_address, &a.local);
76
Marco Varleseb598f1d2017-09-19 14:25:28 +020077 /* Check src & dst are different */
78 if (ip46_address_cmp (&a.remote, &a.local) == 0)
79 {
80 rv = VNET_API_ERROR_SAME_SRC_DST;
81 goto out;
82 }
83 if (ip46_address_is_multicast (&a.remote) &&
84 !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
85 {
86 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
87 goto out;
88 }
89
90 u32 sw_if_index = ~0;
91 rv = vnet_geneve_add_del_tunnel (&a, &sw_if_index);
92
93out:
94 /* *INDENT-OFF* */
95 REPLY_MACRO2(VL_API_GENEVE_ADD_DEL_TUNNEL_REPLY,
96 ({
97 rmp->sw_if_index = ntohl (sw_if_index);
98 }));
99 /* *INDENT-ON* */
100}
101
Ole Troan7fc88cf2020-06-17 22:57:13 +0200102static void vl_api_geneve_add_del_tunnel2_t_handler
103 (vl_api_geneve_add_del_tunnel2_t * mp)
104{
Ole Troan3d812672020-09-15 10:53:34 +0200105 geneve_main_t *gm = &geneve_main;
Ole Troan7fc88cf2020-06-17 22:57:13 +0200106 vl_api_geneve_add_del_tunnel2_reply_t *rmp;
107 int rv = 0;
108 ip4_main_t *im = &ip4_main;
109
110 uword *p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
111 if (!p)
112 {
113 rv = VNET_API_ERROR_NO_SUCH_FIB;
114 goto out;
115 }
116
117 vnet_geneve_add_del_tunnel_args_t a = {
118 .is_add = mp->is_add,
119 .is_ip6 = mp->remote_address.af,
120 .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index),
121 .encap_fib_index = p[0],
122 .decap_next_index = ntohl (mp->decap_next_index),
123 .vni = ntohl (mp->vni),
124 .l3_mode = mp->l3_mode,
125 };
126
127 ip_address_decode (&mp->remote_address, &a.remote);
128 ip_address_decode (&mp->local_address, &a.local);
129
130 /* Check src & dst are different */
131 if (ip46_address_cmp (&a.remote, &a.local) == 0)
132 {
133 rv = VNET_API_ERROR_SAME_SRC_DST;
134 goto out;
135 }
136 if (ip46_address_is_multicast (&a.remote) &&
137 !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
138 {
139 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
140 goto out;
141 }
142
143 u32 sw_if_index = ~0;
144 rv = vnet_geneve_add_del_tunnel (&a, &sw_if_index);
145
146out:
147 /* *INDENT-OFF* */
148 REPLY_MACRO2(VL_API_GENEVE_ADD_DEL_TUNNEL2_REPLY,
149 ({
150 rmp->sw_if_index = ntohl (sw_if_index);
151 }));
152 /* *INDENT-ON* */
153}
154
Marco Varleseb598f1d2017-09-19 14:25:28 +0200155static void send_geneve_tunnel_details
Florin Coras6c4dae22018-01-09 06:39:23 -0800156 (geneve_tunnel_t * t, vl_api_registration_t * reg, u32 context)
Marco Varleseb598f1d2017-09-19 14:25:28 +0200157{
Ole Troan3d812672020-09-15 10:53:34 +0200158 geneve_main_t *gm = &geneve_main;
Marco Varleseb598f1d2017-09-19 14:25:28 +0200159 vl_api_geneve_tunnel_details_t *rmp;
160 ip4_main_t *im4 = &ip4_main;
161 ip6_main_t *im6 = &ip6_main;
162 u8 is_ipv6 = !ip46_address_is_ip4 (&t->remote);
163
164 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400165 clib_memset (rmp, 0, sizeof (*rmp));
Ole Troan3d812672020-09-15 10:53:34 +0200166 rmp->_vl_msg_id = ntohs (VL_API_GENEVE_TUNNEL_DETAILS + gm->msg_id_base);
Jakub Grajciar2d3282e2019-10-01 12:04:56 +0200167 ip_address_encode (&t->local, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
168 &rmp->src_address);
169 ip_address_encode (&t->remote, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
170 &rmp->dst_address);
171 rmp->encap_vrf_id =
172 htonl (is_ipv6 ? im6->fibs[t->encap_fib_index].
173 ft_table_id : im4->fibs[t->encap_fib_index].ft_table_id);
174
Marco Varleseb598f1d2017-09-19 14:25:28 +0200175 rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
176 rmp->vni = htonl (t->vni);
177 rmp->decap_next_index = htonl (t->decap_next_index);
178 rmp->sw_if_index = htonl (t->sw_if_index);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200179 rmp->context = context;
180
Florin Coras6c4dae22018-01-09 06:39:23 -0800181 vl_api_send_msg (reg, (u8 *) rmp);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200182}
183
184static void vl_api_geneve_tunnel_dump_t_handler
185 (vl_api_geneve_tunnel_dump_t * mp)
186{
Florin Coras6c4dae22018-01-09 06:39:23 -0800187 vl_api_registration_t *reg;
Marco Varleseb598f1d2017-09-19 14:25:28 +0200188 geneve_main_t *vxm = &geneve_main;
189 geneve_tunnel_t *t;
190 u32 sw_if_index;
191
Florin Coras6c4dae22018-01-09 06:39:23 -0800192 reg = vl_api_client_index_to_registration (mp->client_index);
193 if (!reg)
194 return;
Marco Varleseb598f1d2017-09-19 14:25:28 +0200195
196 sw_if_index = ntohl (mp->sw_if_index);
197
198 if (~0 == sw_if_index)
199 {
200 /* *INDENT-OFF* */
201 pool_foreach (t, vxm->tunnels,
202 ({
Florin Coras6c4dae22018-01-09 06:39:23 -0800203 send_geneve_tunnel_details(t, reg, mp->context);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200204 }));
205 /* *INDENT-ON* */
206 }
207 else
208 {
209 if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) ||
210 (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index]))
211 {
212 return;
213 }
214 t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
Florin Coras6c4dae22018-01-09 06:39:23 -0800215 send_geneve_tunnel_details (t, reg, mp->context);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200216 }
217}
218
219/*
Ole Troan3d812672020-09-15 10:53:34 +0200220 * geneve_api_hookup
221 * Add geneve's API message handlers to the table.
Marco Varleseb598f1d2017-09-19 14:25:28 +0200222 */
Ole Troan3d812672020-09-15 10:53:34 +0200223/* API definitions */
224#include <vnet/format_fns.h>
225#include <vnet/geneve/geneve.api.c>
Marco Varleseb598f1d2017-09-19 14:25:28 +0200226
227static clib_error_t *
228geneve_api_hookup (vlib_main_t * vm)
229{
Ole Troan3d812672020-09-15 10:53:34 +0200230 geneve_main_t *gm = &geneve_main;
Marco Varleseb598f1d2017-09-19 14:25:28 +0200231
232 /*
233 * Set up the (msg_name, crc, message-id) table
234 */
Ole Troan3d812672020-09-15 10:53:34 +0200235 gm->msg_id_base = setup_message_id_table ();
Marco Varleseb598f1d2017-09-19 14:25:28 +0200236
237 return 0;
238}
239
240VLIB_API_INIT_FUNCTION (geneve_api_hookup);
241
242/*
243 * fd.io coding-style-patch-verification: ON
244 *
245 * Local Variables:
246 * eval: (c-set-style "gnu")
247 * End:
248 */