blob: a3f2246f463ab6f5341274f94f87ad073e81a770 [file] [log] [blame]
Mohsin Kazmi61b94c62018-08-20 18:32:39 +02001/*
2 *------------------------------------------------------------------
3 * vxlan_gbp_api.c - vxlan gbp api
4 *
5 * Copyright (c) 2018 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#include <vnet/feature/feature.h>
26#include <vnet/vxlan-gbp/vxlan_gbp.h>
27#include <vnet/fib/fib_table.h>
Neale Ranns79a05f52018-09-11 07:39:43 -070028#include <vnet/ip/ip_types_api.h>
Filip Tehlar26c8a7f2021-06-04 13:37:00 +000029#include <vnet/format_fns.h>
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020030
Filip Tehlar26c8a7f2021-06-04 13:37:00 +000031#include <vxlan-gbp/vxlan_gbp.api_enum.h>
32#include <vxlan-gbp/vxlan_gbp.api_types.h>
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020033
Filip Tehlar26c8a7f2021-06-04 13:37:00 +000034#define REPLY_MSG_ID_BASE msg_id_base
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020035#include <vlibapi/api_helper_macros.h>
36
Filip Tehlar26c8a7f2021-06-04 13:37:00 +000037static u16 msg_id_base;
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020038
39static void
40 vl_api_sw_interface_set_vxlan_gbp_bypass_t_handler
41 (vl_api_sw_interface_set_vxlan_gbp_bypass_t * mp)
42{
43 vl_api_sw_interface_set_vxlan_gbp_bypass_reply_t *rmp;
44 int rv = 0;
45 u32 sw_if_index = ntohl (mp->sw_if_index);
46
47 VALIDATE_SW_IF_INDEX (mp);
48
49 vnet_int_vxlan_gbp_bypass_mode (sw_if_index, mp->is_ipv6, mp->enable);
50 BAD_SW_IF_INDEX_LABEL;
51
52 REPLY_MACRO (VL_API_SW_INTERFACE_SET_VXLAN_GBP_BYPASS_REPLY);
53}
54
Neale Ranns4dd4cf42019-03-27 05:06:47 -070055static int
56vxlan_gbp_tunnel_mode_decode (vl_api_vxlan_gbp_api_tunnel_mode_t in,
57 vxlan_gbp_tunnel_mode_t * out)
58{
59 in = clib_net_to_host_u32 (in);
60
61 switch (in)
62 {
63 case VXLAN_GBP_API_TUNNEL_MODE_L2:
64 *out = VXLAN_GBP_TUNNEL_MODE_L2;
65 return (0);
66 case VXLAN_GBP_API_TUNNEL_MODE_L3:
67 *out = VXLAN_GBP_TUNNEL_MODE_L3;
68 return (0);
69 }
Paul Vinciguerra9dd94342019-06-18 10:27:32 -040070 return (VNET_API_ERROR_INVALID_VALUE);
Neale Ranns4dd4cf42019-03-27 05:06:47 -070071}
72
Neale Ranns79a05f52018-09-11 07:39:43 -070073static void vl_api_vxlan_gbp_tunnel_add_del_t_handler
74 (vl_api_vxlan_gbp_tunnel_add_del_t * mp)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020075{
Neale Ranns79a05f52018-09-11 07:39:43 -070076 vl_api_vxlan_gbp_tunnel_add_del_reply_t *rmp;
Neale Ranns4dd4cf42019-03-27 05:06:47 -070077 vxlan_gbp_tunnel_mode_t mode;
Neale Ranns79a05f52018-09-11 07:39:43 -070078 ip46_address_t src, dst;
79 ip46_type_t itype;
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020080 int rv = 0;
Paul Vinciguerraaaba6102019-06-25 17:57:31 -040081 u32 sw_if_index = ~0;
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020082 u32 fib_index;
83
Neale Ranns79a05f52018-09-11 07:39:43 -070084 itype = ip_address_decode (&mp->tunnel.src, &src);
85 itype = ip_address_decode (&mp->tunnel.dst, &dst);
86
87 fib_index = fib_table_find (fib_proto_from_ip46 (itype),
88 ntohl (mp->tunnel.encap_table_id));
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020089 if (fib_index == ~0)
90 {
91 rv = VNET_API_ERROR_NO_SUCH_FIB;
92 goto out;
93 }
94
Neale Ranns4dd4cf42019-03-27 05:06:47 -070095 rv = vxlan_gbp_tunnel_mode_decode (mp->tunnel.mode, &mode);
96
97 if (rv)
98 goto out;
99
Neale Ranns79a05f52018-09-11 07:39:43 -0700100 vnet_vxlan_gbp_tunnel_add_del_args_t a = {
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200101 .is_add = mp->is_add,
Neale Ranns79a05f52018-09-11 07:39:43 -0700102 .is_ip6 = (itype == IP46_TYPE_IP6),
103 .instance = ntohl (mp->tunnel.instance),
104 .mcast_sw_if_index = ntohl (mp->tunnel.mcast_sw_if_index),
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200105 .encap_fib_index = fib_index,
Neale Ranns79a05f52018-09-11 07:39:43 -0700106 .vni = ntohl (mp->tunnel.vni),
107 .dst = dst,
108 .src = src,
Neale Ranns4dd4cf42019-03-27 05:06:47 -0700109 .mode = mode,
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200110 };
111
112 /* Check src & dst are different */
113 if (ip46_address_cmp (&a.dst, &a.src) == 0)
114 {
115 rv = VNET_API_ERROR_SAME_SRC_DST;
116 goto out;
117 }
118 if (ip46_address_is_multicast (&a.dst) &&
119 !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
120 {
121 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
122 goto out;
123 }
124
Neale Ranns79a05f52018-09-11 07:39:43 -0700125 rv = vnet_vxlan_gbp_tunnel_add_del (&a, &sw_if_index);
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200126
127out:
128 /* *INDENT-OFF* */
Neale Ranns79a05f52018-09-11 07:39:43 -0700129 REPLY_MACRO2(VL_API_VXLAN_GBP_TUNNEL_ADD_DEL_REPLY,
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200130 ({
131 rmp->sw_if_index = ntohl (sw_if_index);
132 }));
133 /* *INDENT-ON* */
134}
135
136static void send_vxlan_gbp_tunnel_details
137 (vxlan_gbp_tunnel_t * t, vl_api_registration_t * reg, u32 context)
138{
139 vl_api_vxlan_gbp_tunnel_details_t *rmp;
Neale Ranns79a05f52018-09-11 07:39:43 -0700140 ip46_type_t itype = (ip46_address_is_ip4 (&t->dst) ?
141 IP46_TYPE_IP4 : IP46_TYPE_IP6);
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200142
143 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400144 clib_memset (rmp, 0, sizeof (*rmp));
Filip Tehlar26c8a7f2021-06-04 13:37:00 +0000145 rmp->_vl_msg_id =
146 ntohs (VL_API_VXLAN_GBP_TUNNEL_DETAILS + REPLY_MSG_ID_BASE);
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200147
Neale Ranns79a05f52018-09-11 07:39:43 -0700148 ip_address_encode (&t->src, itype, &rmp->tunnel.src);
149 ip_address_encode (&t->dst, itype, &rmp->tunnel.dst);
150 rmp->tunnel.encap_table_id =
151 fib_table_get_table_id (t->encap_fib_index, fib_proto_from_ip46 (itype));
152
153 rmp->tunnel.instance = htonl (t->user_instance);
154 rmp->tunnel.mcast_sw_if_index = htonl (t->mcast_sw_if_index);
155 rmp->tunnel.vni = htonl (t->vni);
Neale Ranns79a05f52018-09-11 07:39:43 -0700156 rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200157 rmp->context = context;
158
159 vl_api_send_msg (reg, (u8 *) rmp);
160}
161
162static void vl_api_vxlan_gbp_tunnel_dump_t_handler
163 (vl_api_vxlan_gbp_tunnel_dump_t * mp)
164{
165 vl_api_registration_t *reg;
166 vxlan_gbp_main_t *vxm = &vxlan_gbp_main;
167 vxlan_gbp_tunnel_t *t;
168 u32 sw_if_index;
169
170 reg = vl_api_client_index_to_registration (mp->client_index);
171 if (!reg)
172 return;
173
174 sw_if_index = ntohl (mp->sw_if_index);
175
176 if (~0 == sw_if_index)
177 {
178 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100179 pool_foreach (t, vxm->tunnels)
180 {
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200181 send_vxlan_gbp_tunnel_details(t, reg, mp->context);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100182 }
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200183 /* *INDENT-ON* */
184 }
185 else
186 {
187 if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) ||
188 (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index]))
189 {
190 return;
191 }
192 t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
193 send_vxlan_gbp_tunnel_details (t, reg, mp->context);
194 }
195}
196
Filip Tehlar26c8a7f2021-06-04 13:37:00 +0000197#include <vxlan-gbp/vxlan_gbp.api.c>
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200198static clib_error_t *
199vxlan_gbp_api_hookup (vlib_main_t * vm)
200{
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200201 /*
202 * Set up the (msg_name, crc, message-id) table
203 */
Filip Tehlar26c8a7f2021-06-04 13:37:00 +0000204 msg_id_base = setup_message_id_table ();
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200205
206 return 0;
207}
208
209VLIB_API_INIT_FUNCTION (vxlan_gbp_api_hookup);
210
211/*
212 * fd.io coding-style-patch-verification: ON
213 *
214 * Local Variables:
215 * eval: (c-set-style "gnu")
216 * End:
217 */