blob: e197057d8c567aca79683149a08ca52cbbb6daa5 [file] [log] [blame]
Dave Barachb5e8a772016-12-06 12:04:42 -05001/*
2 *------------------------------------------------------------------
3 * ip_api.c - vnet ip 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#include <vnet/ethernet/ethernet.h>
Neale Ranns37029302018-08-10 05:30:06 -070026#include <vnet/ethernet/ethernet_types_api.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050027#include <vnet/ip/ip.h>
Neale Ranns37029302018-08-10 05:30:06 -070028#include <vnet/ip/ip_types_api.h>
Pavel Kotucek609e1212018-11-27 09:59:44 +010029#include <vnet/ip/ip_punt_drop.h>
Neale Ranns097fa662018-05-01 05:17:55 -070030#include <vnet/ip/ip_types_api.h>
Neale Ranns8f5fef22020-12-21 08:29:34 +000031#include <vnet/ip/ip_path_mtu.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050032#include <vnet/fib/fib_table.h>
33#include <vnet/fib/fib_api.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050034#include <vnet/ethernet/arp_packet.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080035#include <vnet/mfib/ip6_mfib.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000036#include <vnet/mfib/ip4_mfib.h>
37#include <vnet/mfib/mfib_signal.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080038#include <vnet/mfib/mfib_entry.h>
Neale Ranns097fa662018-05-01 05:17:55 -070039#include <vnet/mfib/mfib_api.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080040#include <vnet/ip/ip_source_and_port_range_check.h>
Neale Ranns92207752019-06-03 13:21:40 +000041#include <vnet/fib/fib_path_list.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080042#include <vnet/ip/ip6_hop_by_hop.h>
Neale Rannscbe25aa2019-09-30 10:53:31 +000043#include <vnet/ip/ip6_link.h>
Klement Sekerade34c352019-06-25 11:19:22 +000044#include <vnet/ip/reass/ip4_sv_reass.h>
Klement Sekera896c8962019-06-24 11:52:49 +000045#include <vnet/ip/reass/ip4_full_reass.h>
Klement Sekerade34c352019-06-25 11:19:22 +000046#include <vnet/ip/reass/ip6_sv_reass.h>
Klement Sekera896c8962019-06-24 11:52:49 +000047#include <vnet/ip/reass/ip6_full_reass.h>
Steven Luongc3ed1c92020-07-27 10:06:58 -070048#include <vnet/ip/ip_table.h>
Neale Rannse4031132020-10-26 13:00:06 +000049#include <vnet/ip/ip_container_proxy.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050050
Ole Troan33a58172019-09-04 09:12:29 +020051#include <vnet/format_fns.h>
Filip Tehlar5ff59a12021-06-23 14:38:38 +000052#include <vnet/ip/ip.api_enum.h>
53#include <vnet/ip/ip.api_types.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080054
Filip Tehlar5ff59a12021-06-23 14:38:38 +000055#define REPLY_MSG_ID_BASE ip4_main.msg_id_base
56#include <vlibapi/api_helper_macros.h>
Pavel Kotucek609e1212018-11-27 09:59:44 +010057
Dave Barachb5e8a772016-12-06 12:04:42 -050058static void
Neale Rannscbe25aa2019-09-30 10:53:31 +000059 vl_api_sw_interface_ip6_enable_disable_t_handler
60 (vl_api_sw_interface_ip6_enable_disable_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -050061{
Neale Rannscbe25aa2019-09-30 10:53:31 +000062 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
63 int rv = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -050064
Neale Rannscbe25aa2019-09-30 10:53:31 +000065 VALIDATE_SW_IF_INDEX (mp);
Neale Ranns37029302018-08-10 05:30:06 -070066
Neale Rannscbe25aa2019-09-30 10:53:31 +000067 rv = ((mp->enable == 1) ?
Neale Rannsec40a7d2020-04-23 07:36:12 +000068 ip6_link_enable (ntohl (mp->sw_if_index), NULL) :
Neale Rannscbe25aa2019-09-30 10:53:31 +000069 ip6_link_disable (ntohl (mp->sw_if_index)));
Dave Barachb5e8a772016-12-06 12:04:42 -050070
Neale Rannscbe25aa2019-09-30 10:53:31 +000071 BAD_SW_IF_INDEX_LABEL;
Dave Barachb5e8a772016-12-06 12:04:42 -050072
Neale Rannscbe25aa2019-09-30 10:53:31 +000073 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
Dave Barachb5e8a772016-12-06 12:04:42 -050074}
75
Dave Barachb5e8a772016-12-06 12:04:42 -050076static void
Neale Ranns097fa662018-05-01 05:17:55 -070077send_ip_table_details (vpe_api_main_t * am,
78 vl_api_registration_t * reg,
79 u32 context, const fib_table_t * table)
Dave Barachb5e8a772016-12-06 12:04:42 -050080{
Neale Ranns097fa662018-05-01 05:17:55 -070081 vl_api_ip_table_details_t *mp;
Dave Barachb5e8a772016-12-06 12:04:42 -050082
Neale Ranns097fa662018-05-01 05:17:55 -070083 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -050084 if (!mp)
85 return;
Dave Barachb7b92992018-10-17 10:38:51 -040086 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +000087 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_TABLE_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -050088 mp->context = context;
89
Neale Ranns097fa662018-05-01 05:17:55 -070090 mp->table.is_ip6 = (table->ft_proto == FIB_PROTOCOL_IP6);
91 mp->table.table_id = htonl (table->ft_table_id);
92 memcpy (mp->table.name, table->ft_desc,
93 clib_min (vec_len (table->ft_desc), sizeof (mp->table.name)));
Dave Barachb5e8a772016-12-06 12:04:42 -050094
Florin Coras6c4dae22018-01-09 06:39:23 -080095 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -050096}
97
Neale Ranns097fa662018-05-01 05:17:55 -070098static void
99vl_api_ip_table_dump_t_handler (vl_api_ip_table_dump_t * mp)
100{
101 vpe_api_main_t *am = &vpe_api_main;
102 vl_api_registration_t *reg;
103 fib_table_t *fib_table;
104
105 reg = vl_api_client_index_to_registration (mp->client_index);
106 if (!reg)
107 return;
108
109 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100110 pool_foreach (fib_table, ip4_main.fibs)
111 {
Neale Ranns097fa662018-05-01 05:17:55 -0700112 send_ip_table_details(am, reg, mp->context, fib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100113 }
114 pool_foreach (fib_table, ip6_main.fibs)
115 {
Neale Ranns097fa662018-05-01 05:17:55 -0700116 /* don't send link locals */
117 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
118 continue;
119 send_ip_table_details(am, reg, mp->context, fib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100120 }
Neale Ranns097fa662018-05-01 05:17:55 -0700121 /* *INDENT-ON* */
122}
123
Neale Rannsa3af3372017-03-28 03:49:52 -0700124typedef struct vl_api_ip_fib_dump_walk_ctx_t_
125{
126 fib_node_index_t *feis;
127} vl_api_ip_fib_dump_walk_ctx_t;
128
Neale Ranns89541992017-04-06 04:41:02 -0700129static fib_table_walk_rc_t
Neale Rannsa3af3372017-03-28 03:49:52 -0700130vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
131{
132 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
133
134 vec_add1 (ctx->feis, fei);
135
Neale Ranns89541992017-04-06 04:41:02 -0700136 return (FIB_TABLE_WALK_CONTINUE);
Neale Rannsa3af3372017-03-28 03:49:52 -0700137}
138
Dave Barachb5e8a772016-12-06 12:04:42 -0500139static void
Neale Ranns097fa662018-05-01 05:17:55 -0700140send_ip_route_details (vpe_api_main_t * am,
141 vl_api_registration_t * reg,
142 u32 context, fib_node_index_t fib_entry_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500143{
Neale Ranns097fa662018-05-01 05:17:55 -0700144 fib_route_path_t *rpaths, *rpath;
145 vl_api_ip_route_details_t *mp;
Neale Rannsc5d43172018-07-30 08:04:40 -0700146 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500147 vl_api_fib_path_t *fp;
148 int path_count;
149
Neale Ranns097fa662018-05-01 05:17:55 -0700150 rpaths = NULL;
151 pfx = fib_entry_get_prefix (fib_entry_index);
152 rpaths = fib_entry_encode (fib_entry_index);
153
154 path_count = vec_len (rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500155 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
156 if (!mp)
157 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400158 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +0000159 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_ROUTE_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -0500160 mp->context = context;
161
Neale Ranns097fa662018-05-01 05:17:55 -0700162 ip_prefix_encode (pfx, &mp->route.prefix);
163 mp->route.table_id =
164 htonl (fib_table_get_table_id
165 (fib_entry_get_fib_index (fib_entry_index), pfx->fp_proto));
166 mp->route.n_paths = path_count;
167 mp->route.stats_index =
168 htonl (fib_table_entry_get_stats_index
169 (fib_entry_get_fib_index (fib_entry_index), pfx));
Dave Barachb5e8a772016-12-06 12:04:42 -0500170
Neale Ranns097fa662018-05-01 05:17:55 -0700171 fp = mp->route.paths;
172 vec_foreach (rpath, rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500173 {
Neale Ranns097fa662018-05-01 05:17:55 -0700174 fib_api_path_encode (rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500175 fp++;
176 }
177
Florin Coras6c4dae22018-01-09 06:39:23 -0800178 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns9db6ada2019-11-08 12:42:31 +0000179 vec_free (rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500180}
181
Neale Ranns976b2592019-12-04 06:11:00 +0000182static void
183send_ip_route_v2_details (vpe_api_main_t *am, vl_api_registration_t *reg,
184 u32 context, fib_node_index_t fib_entry_index)
185{
186 fib_route_path_t *rpaths, *rpath;
187 vl_api_ip_route_v2_details_t *mp;
188 const fib_prefix_t *pfx;
189 vl_api_fib_path_t *fp;
190 int path_count;
191
192 rpaths = NULL;
193 pfx = fib_entry_get_prefix (fib_entry_index);
194 rpaths = fib_entry_encode (fib_entry_index);
195
196 path_count = vec_len (rpaths);
197 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
198 if (!mp)
199 return;
200 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +0000201 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_ROUTE_V2_DETAILS);
Neale Ranns976b2592019-12-04 06:11:00 +0000202 mp->context = context;
203
204 ip_prefix_encode (pfx, &mp->route.prefix);
205 mp->route.table_id = htonl (fib_table_get_table_id (
206 fib_entry_get_fib_index (fib_entry_index), pfx->fp_proto));
207 mp->route.n_paths = path_count;
208 mp->route.src = fib_entry_get_best_source (fib_entry_index);
209 mp->route.stats_index = htonl (fib_table_entry_get_stats_index (
210 fib_entry_get_fib_index (fib_entry_index), pfx));
211
212 fp = mp->route.paths;
213 vec_foreach (rpath, rpaths)
214 {
215 fib_api_path_encode (rpath, fp);
216 fp++;
217 }
218
219 vl_api_send_msg (reg, (u8 *) mp);
220 vec_free (rpaths);
221}
222
Dave Barachd7cb1b52016-12-09 09:52:16 -0500223typedef struct apt_ip6_fib_show_ctx_t_
224{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500225 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500226} api_ip6_fib_show_ctx_t;
227
Dave Barachb5e8a772016-12-06 12:04:42 -0500228static void
Neale Ranns097fa662018-05-01 05:17:55 -0700229vl_api_ip_route_dump_t_handler (vl_api_ip_route_dump_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500230{
231 vpe_api_main_t *am = &vpe_api_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500232 fib_node_index_t *fib_entry_index;
Florin Coras6c4dae22018-01-09 06:39:23 -0800233 vl_api_registration_t *reg;
Neale Ranns097fa662018-05-01 05:17:55 -0700234 fib_protocol_t fproto;
235 u32 fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -0500236
Florin Coras6c4dae22018-01-09 06:39:23 -0800237 reg = vl_api_client_index_to_registration (mp->client_index);
238 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500239 return;
240
Neale Ranns097fa662018-05-01 05:17:55 -0700241 vl_api_ip_fib_dump_walk_ctx_t ctx = {
242 .feis = NULL,
243 };
Neale Ranns81458422018-03-12 06:59:36 -0700244
Neale Ranns097fa662018-05-01 05:17:55 -0700245 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
246 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
247
248 if (INDEX_INVALID == fib_index)
249 return;
250
251 fib_table_walk (fib_index, fproto, vl_api_ip_fib_dump_walk, &ctx);
252
253 vec_foreach (fib_entry_index, ctx.feis)
254 {
255 send_ip_route_details (am, reg, mp->context, *fib_entry_index);
256 }
257
258 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500259}
260
261static void
Neale Ranns976b2592019-12-04 06:11:00 +0000262vl_api_ip_route_v2_dump_t_handler (vl_api_ip_route_v2_dump_t *mp)
263{
264 vpe_api_main_t *am = &vpe_api_main;
265 fib_node_index_t *fib_entry_index;
266 vl_api_registration_t *reg;
267 fib_protocol_t fproto;
268 fib_source_t src;
269 u32 fib_index;
270
271 reg = vl_api_client_index_to_registration (mp->client_index);
272 if (!reg)
273 return;
274
275 vl_api_ip_fib_dump_walk_ctx_t ctx = {
276 .feis = NULL,
277 };
278
279 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
280 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
281 src = mp->src;
282
283 if (INDEX_INVALID == fib_index)
284 return;
285
286 if (src)
287 fib_table_walk_w_src (fib_index, fproto, src, vl_api_ip_fib_dump_walk,
288 &ctx);
289 else
290 fib_table_walk (fib_index, fproto, vl_api_ip_fib_dump_walk, &ctx);
291
292 vec_foreach (fib_entry_index, ctx.feis)
293 {
294 send_ip_route_v2_details (am, reg, mp->context, *fib_entry_index);
295 }
296
297 vec_free (ctx.feis);
298}
299
300static void
Neale Ranns097fa662018-05-01 05:17:55 -0700301send_ip_mtable_details (vl_api_registration_t * reg,
302 u32 context, const mfib_table_t * mfib_table)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800303{
Neale Ranns097fa662018-05-01 05:17:55 -0700304 vl_api_ip_mtable_details_t *mp;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800305
Neale Ranns097fa662018-05-01 05:17:55 -0700306 mp = vl_msg_api_alloc (sizeof (*mp));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800307 if (!mp)
308 return;
Neale Ranns097fa662018-05-01 05:17:55 -0700309 memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +0000310 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_MTABLE_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800311 mp->context = context;
312
Neale Ranns097fa662018-05-01 05:17:55 -0700313 mp->table.table_id = htonl (mfib_table->mft_table_id);
314 mp->table.is_ip6 = (FIB_PROTOCOL_IP6 == mfib_table->mft_proto);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800315
Florin Coras6c4dae22018-01-09 06:39:23 -0800316 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800317}
318
Neale Ranns5a8123b2017-01-26 01:18:23 -0800319static void
Neale Ranns097fa662018-05-01 05:17:55 -0700320vl_api_ip_mtable_dump_t_handler (vl_api_ip_mtable_dump_t * mp)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800321{
Florin Coras6c4dae22018-01-09 06:39:23 -0800322 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800323 mfib_table_t *mfib_table;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800324
Florin Coras6c4dae22018-01-09 06:39:23 -0800325 reg = vl_api_client_index_to_registration (mp->client_index);
326 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800327 return;
328
Neale Ranns5a8123b2017-01-26 01:18:23 -0800329 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100330 pool_foreach (mfib_table, ip4_main.mfibs)
331 {
Neale Ranns097fa662018-05-01 05:17:55 -0700332 send_ip_mtable_details (reg, mp->context, mfib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100333 }
334 pool_foreach (mfib_table, ip6_main.mfibs)
335 {
Neale Ranns097fa662018-05-01 05:17:55 -0700336 send_ip_mtable_details (reg, mp->context, mfib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100337 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800338 /* *INDENT-ON* */
Neale Ranns5a8123b2017-01-26 01:18:23 -0800339}
340
Neale Ranns097fa662018-05-01 05:17:55 -0700341typedef struct vl_api_ip_mfib_dump_ctx_t_
Neale Ranns5a8123b2017-01-26 01:18:23 -0800342{
343 fib_node_index_t *entries;
Neale Ranns097fa662018-05-01 05:17:55 -0700344} vl_api_ip_mfib_dump_ctx_t;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800345
Neale Ranns9db6ada2019-11-08 12:42:31 +0000346static walk_rc_t
Neale Ranns097fa662018-05-01 05:17:55 -0700347mfib_route_dump_walk (fib_node_index_t fei, void *arg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800348{
Neale Ranns097fa662018-05-01 05:17:55 -0700349 vl_api_ip_mfib_dump_ctx_t *ctx = arg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800350
351 vec_add1 (ctx->entries, fei);
352
Neale Ranns9db6ada2019-11-08 12:42:31 +0000353 return (WALK_CONTINUE);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800354}
355
356static void
Neale Ranns097fa662018-05-01 05:17:55 -0700357send_ip_mroute_details (vpe_api_main_t * am,
358 vl_api_registration_t * reg,
359 u32 context, fib_node_index_t mfib_entry_index)
360{
361 fib_route_path_t *rpaths, *rpath;
362 vl_api_ip_mroute_details_t *mp;
363 const mfib_prefix_t *pfx;
364 vl_api_mfib_path_t *fp;
Matthew Smitha2bbeb82020-09-17 16:33:10 -0500365 u8 path_count;
Neale Ranns097fa662018-05-01 05:17:55 -0700366
367 rpaths = NULL;
368 pfx = mfib_entry_get_prefix (mfib_entry_index);
369 rpaths = mfib_entry_encode (mfib_entry_index);
370
371 path_count = vec_len (rpaths);
372 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
373 if (!mp)
374 return;
375 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +0000376 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_MROUTE_DETAILS);
Neale Ranns097fa662018-05-01 05:17:55 -0700377 mp->context = context;
378
379 ip_mprefix_encode (pfx, &mp->route.prefix);
380 mp->route.table_id =
381 htonl (mfib_table_get_table_id
382 (mfib_entry_get_fib_index (mfib_entry_index), pfx->fp_proto));
Matthew Smitha2bbeb82020-09-17 16:33:10 -0500383 mp->route.n_paths = path_count;
Neale Ranns097fa662018-05-01 05:17:55 -0700384 fp = mp->route.paths;
385 vec_foreach (rpath, rpaths)
386 {
387 mfib_api_path_encode (rpath, fp);
388 fp++;
389 }
390
391 vl_api_send_msg (reg, (u8 *) mp);
392 vec_free (rpaths);
393}
394
395static void
396vl_api_ip_mroute_dump_t_handler (vl_api_ip_mroute_dump_t * mp)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800397{
398 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800399 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800400 fib_node_index_t *mfeip;
Neale Ranns097fa662018-05-01 05:17:55 -0700401 fib_protocol_t fproto;
402 u32 fib_index;
403
404 vl_api_ip_mfib_dump_ctx_t ctx = {
Neale Ranns5a8123b2017-01-26 01:18:23 -0800405 .entries = NULL,
406 };
407
Florin Coras6c4dae22018-01-09 06:39:23 -0800408 reg = vl_api_client_index_to_registration (mp->client_index);
409 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800410 return;
411
Neale Ranns097fa662018-05-01 05:17:55 -0700412 fproto = fib_ip_proto (mp->table.is_ip6);
413 fib_index = mfib_table_find (fproto, ntohl (mp->table.table_id));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800414
Neale Ranns097fa662018-05-01 05:17:55 -0700415 if (INDEX_INVALID == fib_index)
416 return;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800417
Neale Ranns097fa662018-05-01 05:17:55 -0700418 mfib_table_walk (fib_index, fproto, mfib_route_dump_walk, &ctx);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800419
Neale Ranns097fa662018-05-01 05:17:55 -0700420 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800421
Neale Ranns097fa662018-05-01 05:17:55 -0700422 vec_foreach (mfeip, ctx.entries)
423 {
424 send_ip_mroute_details (am, reg, mp->context, *mfeip);
425 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800426
427 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800428}
429
430static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700431vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
432 vlib_main_t * vm)
433{
434 vl_api_ip_punt_police_reply_t *rmp;
435 int rv = 0;
436
437 if (mp->is_ip6)
438 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
439 else
440 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
441
442 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
443}
444
445static void
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +0200446ip_punt_redirect_t_handler_common (u8 is_add, u32 rx_sw_if_index,
447 ip_address_family_t af,
448 const fib_route_path_t *rpaths)
449{
450 if (is_add)
451 {
452 if (af == AF_IP6)
453 ip6_punt_redirect_add_paths (rx_sw_if_index, rpaths);
454 else if (af == AF_IP4)
455 ip4_punt_redirect_add_paths (rx_sw_if_index, rpaths);
456 }
457 else
458 {
459 if (af == AF_IP6)
460 ip6_punt_redirect_del (rx_sw_if_index);
461 else if (af == AF_IP4)
462 ip4_punt_redirect_del (rx_sw_if_index);
463 }
464}
465
466static void
467vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t *mp,
468 vlib_main_t *vm)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700469{
470 vl_api_ip_punt_redirect_reply_t *rmp;
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +0200471 fib_route_path_t *rpaths = NULL, rpath = {
472 .frp_weight = 1,
473 .frp_fib_index = ~0,
474 };
475 ip_address_family_t af;
Pavel Kotucek609e1212018-11-27 09:59:44 +0100476 ip46_type_t ipv;
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +0200477 u32 rx_sw_if_index;
478 int rv = 0;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700479
Pavel Kotucek609e1212018-11-27 09:59:44 +0100480 if (!vnet_sw_if_index_is_api_valid (ntohl (mp->punt.tx_sw_if_index)))
481 goto bad_sw_if_index;
482
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +0200483 ipv = ip_address_decode (&mp->punt.nh, &rpath.frp_addr);
484 af = (ipv == IP46_TYPE_IP6) ? AF_IP6 : AF_IP4;
485 rpath.frp_proto = (ipv == IP46_TYPE_IP6) ? DPO_PROTO_IP6 : DPO_PROTO_IP4;
486 rpath.frp_sw_if_index = ntohl (mp->punt.tx_sw_if_index);
487 rx_sw_if_index = ntohl (mp->punt.rx_sw_if_index);
488
489 vec_add1 (rpaths, rpath);
490 ip_punt_redirect_t_handler_common (mp->is_add, rx_sw_if_index, af, rpaths);
491 vec_free (rpaths);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700492
Pavel Kotucek609e1212018-11-27 09:59:44 +0100493 BAD_SW_IF_INDEX_LABEL;
494
Neale Rannsd91c1db2017-07-31 02:30:50 -0700495 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
496}
497
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +0200498static void
499vl_api_add_del_ip_punt_redirect_v2_t_handler (
500 vl_api_add_del_ip_punt_redirect_v2_t *mp, vlib_main_t *vm)
501{
502 vl_api_add_del_ip_punt_redirect_v2_reply_t *rmp;
503 fib_route_path_t *rpaths = NULL, *rpath;
504 vl_api_fib_path_t *apath;
505 ip_address_family_t af;
506 u32 rx_sw_if_index, n_paths;
507 int rv = 0, ii;
508
509 rx_sw_if_index = ntohl (mp->punt.rx_sw_if_index);
510 n_paths = ntohl (mp->punt.n_paths);
511
512 rv = ip_address_family_decode (mp->punt.af, &af);
513 if (rv != 0)
514 goto out;
515
516 if (0 != n_paths)
517 vec_validate (rpaths, n_paths - 1);
518
519 for (ii = 0; ii < n_paths; ii++)
520 {
521 apath = &mp->punt.paths[ii];
522 rpath = &rpaths[ii];
523
524 rv = fib_api_path_decode (apath, rpath);
525
526 if (rv != 0)
527 goto out;
528 }
529
530 ip_punt_redirect_t_handler_common (mp->is_add, rx_sw_if_index, af, rpaths);
531
532out:
533 vec_free (rpaths);
534
535 REPLY_MACRO (VL_API_ADD_DEL_IP_PUNT_REDIRECT_V2_REPLY);
536}
537
Steven Luongc3ed1c92020-07-27 10:06:58 -0700538static clib_error_t *
539call_elf_section_ip_table_callbacks (vnet_main_t * vnm, u32 table_id,
540 u32 flags,
541 _vnet_ip_table_function_list_elt_t **
542 elts)
543{
544 _vnet_ip_table_function_list_elt_t *elt;
545 vnet_ip_table_function_priority_t prio;
546 clib_error_t *error = 0;
547
548 for (prio = VNET_IP_TABLE_FUNC_PRIORITY_LOW;
549 prio <= VNET_IP_TABLE_FUNC_PRIORITY_HIGH; prio++)
550 {
551 elt = elts[prio];
552
553 while (elt)
554 {
555 error = elt->fp (vnm, table_id, flags);
556 if (error)
557 return error;
558 elt = elt->next_ip_table_function;
559 }
560 }
561 return error;
562}
563
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700564void
Neale Ranns15002542017-09-10 04:39:11 -0700565ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
566{
567 u32 fib_index, mfib_index;
Steven Luongc3ed1c92020-07-27 10:06:58 -0700568 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns15002542017-09-10 04:39:11 -0700569
570 /*
571 * ignore action on the default table - this is always present
572 * and cannot be added nor deleted from the API
573 */
574 if (0 != table_id)
575 {
576 /*
577 * The API holds only one lock on the table.
578 * i.e. it can be added many times via the API but needs to be
579 * deleted only once.
580 * The FIB index for unicast and multicast is not necessarily the
581 * same, since internal VPP systesm (like LISP and SR) create
582 * their own unicast tables.
583 */
584 fib_index = fib_table_find (fproto, table_id);
585 mfib_index = mfib_table_find (fproto, table_id);
586
Steven Luongc3ed1c92020-07-27 10:06:58 -0700587 if ((~0 != fib_index) || (~0 != mfib_index))
588 call_elf_section_ip_table_callbacks (vnm, table_id, 0 /* is_add */ ,
589 vnm->ip_table_add_del_functions);
590
Neale Ranns15002542017-09-10 04:39:11 -0700591 if (~0 != fib_index)
592 {
593 fib_table_unlock (fib_index, fproto,
594 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
595 }
596 if (~0 != mfib_index)
597 {
598 mfib_table_unlock (mfib_index, fproto,
599 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
600 }
601 }
602}
603
Aloys Augustin6e4cfb52021-09-16 20:53:14 +0200604/*
605 * Returns an unused table id, and ~0 if it can't find one.
606 */
607u32
608ip_table_get_unused_id (fib_protocol_t fproto)
609{
610 int i, j;
Aloys Augustin400f23f2021-09-26 18:26:15 +0200611 static u32 seed = 0;
Aloys Augustin6e4cfb52021-09-16 20:53:14 +0200612 /* limit to 1M tries */
613 for (j = 0; j < 1 << 10; j++)
614 {
615 seed = random_u32 (&seed);
616 for (i = 0; i < 1 << 10; i++)
617 {
618 /* look around randomly generated id */
619 seed += (2 * (i % 2) - 1) * i;
620 if (seed == ~0)
621 continue;
622 if (fib_table_find (fproto, seed) == ~0)
623 return seed;
624 }
625 }
626
627 return ~0;
628}
629
Neale Ranns15002542017-09-10 04:39:11 -0700630void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700631vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
632{
633 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns097fa662018-05-01 05:17:55 -0700634 fib_protocol_t fproto = (mp->table.is_ip6 ?
635 FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
636 u32 table_id = ntohl (mp->table.table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700637 int rv = 0;
638
Neale Ranns15002542017-09-10 04:39:11 -0700639 if (mp->is_add)
640 {
Neale Ranns097fa662018-05-01 05:17:55 -0700641 ip_table_create (fproto, table_id, 1, mp->table.name);
Neale Ranns15002542017-09-10 04:39:11 -0700642 }
643 else
644 {
645 ip_table_delete (fproto, table_id, 1);
646 }
647
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700648 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
649}
650
Aloys Augustin6e4cfb52021-09-16 20:53:14 +0200651void
652vl_api_ip_table_allocate_t_handler (vl_api_ip_table_allocate_t *mp)
653{
654 vl_api_ip_table_allocate_reply_t *rmp;
655 fib_protocol_t fproto =
656 (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
657 u32 table_id = ntohl (mp->table.table_id);
658 int rv = 0;
659
660 if (~0 == table_id)
661 table_id = ip_table_get_unused_id (fproto);
662
663 if (~0 == table_id)
664 rv = VNET_API_ERROR_EAGAIN;
665 else
666 ip_table_create (fproto, table_id, 1, mp->table.name);
667
668 REPLY_MACRO2 (VL_API_IP_TABLE_ALLOCATE_REPLY, {
669 clib_memcpy_fast (&rmp->table, &mp->table, sizeof (mp->table));
670 rmp->table.table_id = htonl (table_id);
671 })
672}
673
Dave Barachb5e8a772016-12-06 12:04:42 -0500674static int
Neale Ranns097fa662018-05-01 05:17:55 -0700675ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp, u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500676{
Neale Ranns097fa662018-05-01 05:17:55 -0700677 fib_route_path_t *rpaths = NULL, *rpath;
678 fib_entry_flag_t entry_flags;
679 vl_api_fib_path_t *apath;
680 fib_prefix_t pfx;
681 u32 fib_index;
682 int rv, ii;
Dave Barachb5e8a772016-12-06 12:04:42 -0500683
Neale Ranns097fa662018-05-01 05:17:55 -0700684 entry_flags = FIB_ENTRY_FLAG_NONE;
685 ip_prefix_decode (&mp->route.prefix, &pfx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500686
Neale Ranns097fa662018-05-01 05:17:55 -0700687 rv = fib_api_table_id_decode (pfx.fp_proto,
688 ntohl (mp->route.table_id), &fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500689 if (0 != rv)
Neale Ranns097fa662018-05-01 05:17:55 -0700690 goto out;
Dave Barachb5e8a772016-12-06 12:04:42 -0500691
Neale Rannsc2ac2352019-07-02 14:33:29 +0000692 if (0 != mp->route.n_paths)
693 vec_validate (rpaths, mp->route.n_paths - 1);
Neale Ranns097fa662018-05-01 05:17:55 -0700694
695 for (ii = 0; ii < mp->route.n_paths; ii++)
696 {
697 apath = &mp->route.paths[ii];
698 rpath = &rpaths[ii];
699
700 rv = fib_api_path_decode (apath, rpath);
701
702 if ((rpath->frp_flags & FIB_ROUTE_PATH_LOCAL) &&
703 (~0 == rpath->frp_sw_if_index))
704 entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
705
706 if (0 != rv)
707 goto out;
708 }
709
Neale Ranns976b2592019-12-04 06:11:00 +0000710 rv = fib_api_route_add_del (mp->is_add, mp->is_multipath, fib_index, &pfx,
711 FIB_SOURCE_API, entry_flags, rpaths);
712
713 if (mp->is_add && 0 == rv)
714 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
715
716out:
717 vec_free (rpaths);
718
719 return (rv);
720}
721
722static int
723ip_route_add_del_v2_t_handler (vl_api_ip_route_add_del_v2_t *mp,
724 u32 *stats_index)
725{
726 fib_route_path_t *rpaths = NULL, *rpath;
727 fib_entry_flag_t entry_flags;
728 vl_api_fib_path_t *apath;
729 fib_source_t src;
730 fib_prefix_t pfx;
731 u32 fib_index;
732 int rv, ii;
733
734 entry_flags = FIB_ENTRY_FLAG_NONE;
735 ip_prefix_decode (&mp->route.prefix, &pfx);
736
737 rv = fib_api_table_id_decode (pfx.fp_proto, ntohl (mp->route.table_id),
738 &fib_index);
739 if (0 != rv)
740 goto out;
741
742 if (0 != mp->route.n_paths)
743 vec_validate (rpaths, mp->route.n_paths - 1);
744
745 for (ii = 0; ii < mp->route.n_paths; ii++)
746 {
747 apath = &mp->route.paths[ii];
748 rpath = &rpaths[ii];
749
750 rv = fib_api_path_decode (apath, rpath);
751
752 if ((rpath->frp_flags & FIB_ROUTE_PATH_LOCAL) &&
753 (~0 == rpath->frp_sw_if_index))
754 entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
755
756 if (0 != rv)
757 goto out;
758 }
759
760 src = (0 == mp->route.src ? FIB_SOURCE_API : mp->route.src);
761
762 rv = fib_api_route_add_del (mp->is_add, mp->is_multipath, fib_index, &pfx,
763 src, entry_flags, rpaths);
Neale Ranns008dbe12018-09-07 09:32:36 -0700764
765 if (mp->is_add && 0 == rv)
766 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
767
Neale Ranns097fa662018-05-01 05:17:55 -0700768out:
769 vec_free (rpaths);
Neale Ranns008dbe12018-09-07 09:32:36 -0700770
771 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -0500772}
773
774void
Neale Ranns097fa662018-05-01 05:17:55 -0700775vl_api_ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500776{
Neale Ranns097fa662018-05-01 05:17:55 -0700777 vl_api_ip_route_add_del_reply_t *rmp;
778 u32 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -0500779 int rv;
Dave Barachb5e8a772016-12-06 12:04:42 -0500780
Neale Ranns097fa662018-05-01 05:17:55 -0700781 rv = ip_route_add_del_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500782
Neale Ranns008dbe12018-09-07 09:32:36 -0700783 /* *INDENT-OFF* */
Neale Ranns097fa662018-05-01 05:17:55 -0700784 REPLY_MACRO2 (VL_API_IP_ROUTE_ADD_DEL_REPLY,
Neale Ranns008dbe12018-09-07 09:32:36 -0700785 ({
786 rmp->stats_index = htonl (stats_index);
787 }))
788 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500789}
790
Neale Ranns15002542017-09-10 04:39:11 -0700791void
Neale Ranns976b2592019-12-04 06:11:00 +0000792vl_api_ip_route_add_del_v2_t_handler (vl_api_ip_route_add_del_v2_t *mp)
793{
794 vl_api_ip_route_add_del_v2_reply_t *rmp;
795 u32 stats_index = ~0;
796 int rv;
797
798 rv = ip_route_add_del_v2_t_handler (mp, &stats_index);
799
800 /* clang-format off */
801 REPLY_MACRO2 (VL_API_IP_ROUTE_ADD_DEL_V2_REPLY,
802 ({
803 rmp->stats_index = htonl (stats_index);
804 }))
805 /* clang-format on */
806}
807
808void
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400809vl_api_ip_route_lookup_t_handler (vl_api_ip_route_lookup_t * mp)
810{
811 vl_api_ip_route_lookup_reply_t *rmp = NULL;
812 fib_route_path_t *rpaths = NULL, *rpath;
813 const fib_prefix_t *pfx = NULL;
814 fib_prefix_t lookup;
815 vl_api_fib_path_t *fp;
816 fib_node_index_t fib_entry_index;
817 u32 fib_index;
818 int npaths = 0;
819 int rv;
820
821 ip_prefix_decode (&mp->prefix, &lookup);
822 rv = fib_api_table_id_decode (lookup.fp_proto, ntohl (mp->table_id),
823 &fib_index);
824 if (PREDICT_TRUE (!rv))
825 {
826 if (mp->exact)
827 fib_entry_index = fib_table_lookup_exact_match (fib_index, &lookup);
828 else
829 fib_entry_index = fib_table_lookup (fib_index, &lookup);
830 if (fib_entry_index == FIB_NODE_INDEX_INVALID)
831 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
832 else
833 {
834 pfx = fib_entry_get_prefix (fib_entry_index);
835 rpaths = fib_entry_encode (fib_entry_index);
836 npaths = vec_len (rpaths);
837 }
838 }
839
840 /* *INDENT-OFF* */
841 REPLY_MACRO3_ZERO(VL_API_IP_ROUTE_LOOKUP_REPLY,
842 npaths * sizeof (*fp),
843 ({
844 if (!rv)
845 {
846 ip_prefix_encode (pfx, &rmp->route.prefix);
847 rmp->route.table_id = mp->table_id;
848 rmp->route.n_paths = npaths;
849 rmp->route.stats_index = fib_table_entry_get_stats_index (fib_index, pfx);
850 rmp->route.stats_index = htonl (rmp->route.stats_index);
851
852 fp = rmp->route.paths;
853 vec_foreach (rpath, rpaths)
854 {
855 fib_api_path_encode (rpath, fp);
856 fp++;
857 }
858 }
859 }));
860 /* *INDENT-ON* */
861 vec_free (rpaths);
862}
863
864void
Neale Ranns976b2592019-12-04 06:11:00 +0000865vl_api_ip_route_lookup_v2_t_handler (vl_api_ip_route_lookup_v2_t *mp)
866{
867 vl_api_ip_route_lookup_v2_reply_t *rmp = NULL;
868 fib_route_path_t *rpaths = NULL, *rpath;
869 const fib_prefix_t *pfx = NULL;
870 fib_prefix_t lookup;
871 vl_api_fib_path_t *fp;
872 fib_node_index_t fib_entry_index;
873 u32 fib_index;
874 int npaths = 0;
875 fib_source_t src = 0;
876 int rv;
877
878 ip_prefix_decode (&mp->prefix, &lookup);
879 rv = fib_api_table_id_decode (lookup.fp_proto, ntohl (mp->table_id),
880 &fib_index);
881 if (PREDICT_TRUE (!rv))
882 {
883 if (mp->exact)
884 fib_entry_index = fib_table_lookup_exact_match (fib_index, &lookup);
885 else
886 fib_entry_index = fib_table_lookup (fib_index, &lookup);
887 if (fib_entry_index == FIB_NODE_INDEX_INVALID)
888 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
889 else
890 {
891 pfx = fib_entry_get_prefix (fib_entry_index);
892 rpaths = fib_entry_encode (fib_entry_index);
893 npaths = vec_len (rpaths);
894 src = fib_entry_get_best_source (fib_entry_index);
895 }
896 }
897
898 /* clang-format off */
899 REPLY_MACRO3_ZERO(VL_API_IP_ROUTE_LOOKUP_V2_REPLY,
900 npaths * sizeof (*fp),
901 ({
902 if (!rv)
903 {
904 ip_prefix_encode (pfx, &rmp->route.prefix);
905 rmp->route.table_id = mp->table_id;
906 rmp->route.n_paths = npaths;
907 rmp->route.src = src;
908 rmp->route.stats_index = fib_table_entry_get_stats_index (fib_index, pfx);
909 rmp->route.stats_index = htonl (rmp->route.stats_index);
910
911 fp = rmp->route.paths;
912 vec_foreach (rpath, rpaths)
913 {
914 fib_api_path_encode (rpath, fp);
915 fp++;
916 }
917 }
918 }));
919 /* clang-format on */
920 vec_free (rpaths);
921}
922
923void
Neale Ranns2297af02017-09-12 09:45:04 -0700924ip_table_create (fib_protocol_t fproto,
925 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -0700926{
927 u32 fib_index, mfib_index;
Steven Luongc3ed1c92020-07-27 10:06:58 -0700928 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns15002542017-09-10 04:39:11 -0700929
930 /*
931 * ignore action on the default table - this is always present
932 * and cannot be added nor deleted from the API
933 */
934 if (0 != table_id)
935 {
936 /*
937 * The API holds only one lock on the table.
938 * i.e. it can be added many times via the API but needs to be
939 * deleted only once.
940 * The FIB index for unicast and multicast is not necessarily the
941 * same, since internal VPP systesm (like LISP and SR) create
942 * their own unicast tables.
943 */
944 fib_index = fib_table_find (fproto, table_id);
945 mfib_index = mfib_table_find (fproto, table_id);
946
Nathan Skrzypczak275bd792021-09-17 17:29:14 +0200947 /*
948 * Always try to re-lock in case the fib was deleted by an API call
949 * but was not yet freed because some other locks were held
950 */
951 fib_table_find_or_create_and_lock_w_name (
952 fproto, table_id, (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI), name);
953 mfib_table_find_or_create_and_lock_w_name (
954 fproto, table_id, (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI), name);
Steven Luongc3ed1c92020-07-27 10:06:58 -0700955
956 if ((~0 == fib_index) || (~0 == mfib_index))
957 call_elf_section_ip_table_callbacks (vnm, table_id, 1 /* is_add */ ,
958 vnm->ip_table_add_del_functions);
Neale Ranns15002542017-09-10 04:39:11 -0700959 }
960}
961
Neale Ranns097fa662018-05-01 05:17:55 -0700962static u32
Neale Ranns32e1c012016-11-22 17:07:28 +0000963mroute_add_del_handler (u8 is_add,
Neale Ranns097fa662018-05-01 05:17:55 -0700964 u8 is_multipath,
Neale Ranns32e1c012016-11-22 17:07:28 +0000965 u32 fib_index,
966 const mfib_prefix_t * prefix,
967 u32 entry_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700968 u32 rpf_id, fib_route_path_t * rpaths)
Neale Ranns32e1c012016-11-22 17:07:28 +0000969{
Neale Ranns097fa662018-05-01 05:17:55 -0700970 u32 mfib_entry_index = ~0;
Neale Ranns28c142e2018-09-07 09:37:07 -0700971
Neale Ranns097fa662018-05-01 05:17:55 -0700972 if (0 == vec_len (rpaths))
Neale Ranns32e1c012016-11-22 17:07:28 +0000973 {
Neale Ranns28c142e2018-09-07 09:37:07 -0700974 mfib_entry_index = mfib_table_entry_update (fib_index, prefix,
975 MFIB_SOURCE_API,
976 rpf_id, entry_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +0000977 }
978 else
979 {
Neale Ranns097fa662018-05-01 05:17:55 -0700980 if (is_add)
981 {
Paul Atkins8e2b1b12021-10-12 14:32:11 +0100982 mfib_entry_index = mfib_table_entry_paths_update (
983 fib_index, prefix, MFIB_SOURCE_API, entry_flags, rpaths);
Neale Ranns097fa662018-05-01 05:17:55 -0700984 }
985 else
986 {
987 mfib_table_entry_paths_remove (fib_index, prefix,
988 MFIB_SOURCE_API, rpaths);
989 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000990 }
991
Neale Ranns28c142e2018-09-07 09:37:07 -0700992 return (mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000993}
994
995static int
Neale Ranns28c142e2018-09-07 09:37:07 -0700996api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp,
997 u32 * stats_index)
Neale Ranns32e1c012016-11-22 17:07:28 +0000998{
Neale Ranns097fa662018-05-01 05:17:55 -0700999 fib_route_path_t *rpath, *rpaths = NULL;
Neale Ranns28c142e2018-09-07 09:37:07 -07001000 fib_node_index_t mfib_entry_index;
Neale Ranns990f6942020-10-20 07:20:17 +00001001 mfib_entry_flags_t eflags;
Neale Ranns097fa662018-05-01 05:17:55 -07001002 mfib_prefix_t pfx;
Neale Ranns32e1c012016-11-22 17:07:28 +00001003 u32 fib_index;
1004 int rv;
Neale Ranns097fa662018-05-01 05:17:55 -07001005 u16 ii;
Neale Ranns32e1c012016-11-22 17:07:28 +00001006
Neale Ranns097fa662018-05-01 05:17:55 -07001007 ip_mprefix_decode (&mp->route.prefix, &pfx);
Neale Ranns32e1c012016-11-22 17:07:28 +00001008
Neale Ranns097fa662018-05-01 05:17:55 -07001009 rv = mfib_api_table_id_decode (pfx.fp_proto,
1010 ntohl (mp->route.table_id), &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001011 if (0 != rv)
Neale Ranns097fa662018-05-01 05:17:55 -07001012 goto out;
Neale Ranns32e1c012016-11-22 17:07:28 +00001013
Neale Ranns097fa662018-05-01 05:17:55 -07001014 vec_validate (rpaths, mp->route.n_paths - 1);
Neale Ranns32e1c012016-11-22 17:07:28 +00001015
Neale Ranns097fa662018-05-01 05:17:55 -07001016 for (ii = 0; ii < mp->route.n_paths; ii++)
Neale Ranns32e1c012016-11-22 17:07:28 +00001017 {
Neale Ranns097fa662018-05-01 05:17:55 -07001018 rpath = &rpaths[ii];
1019
1020 rv = mfib_api_path_decode (&mp->route.paths[ii], rpath);
1021
1022 if (0 != rv)
1023 goto out;
Neale Ranns32e1c012016-11-22 17:07:28 +00001024 }
1025
Neale Ranns990f6942020-10-20 07:20:17 +00001026 eflags = mfib_api_path_entry_flags_decode (mp->route.entry_flags);
Neale Ranns28c142e2018-09-07 09:37:07 -07001027 mfib_entry_index = mroute_add_del_handler (mp->is_add,
Neale Ranns097fa662018-05-01 05:17:55 -07001028 mp->is_add,
Neale Ranns28c142e2018-09-07 09:37:07 -07001029 fib_index, &pfx,
Neale Ranns990f6942020-10-20 07:20:17 +00001030 eflags,
Neale Ranns097fa662018-05-01 05:17:55 -07001031 ntohl (mp->route.rpf_id),
1032 rpaths);
Neale Ranns28c142e2018-09-07 09:37:07 -07001033
1034 if (~0 != mfib_entry_index)
1035 *stats_index = mfib_entry_get_stats_index (mfib_entry_index);
1036
Neale Ranns097fa662018-05-01 05:17:55 -07001037out:
Neale Ranns28c142e2018-09-07 09:37:07 -07001038 return (rv);
Neale Ranns32e1c012016-11-22 17:07:28 +00001039}
1040
1041void
1042vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1043{
1044 vl_api_ip_mroute_add_del_reply_t *rmp;
Neale Ranns097fa662018-05-01 05:17:55 -07001045 u32 stats_index = ~0;
Neale Ranns32e1c012016-11-22 17:07:28 +00001046 int rv;
Neale Ranns32e1c012016-11-22 17:07:28 +00001047
Neale Ranns28c142e2018-09-07 09:37:07 -07001048 rv = api_mroute_add_del_t_handler (mp, &stats_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001049
Neale Ranns28c142e2018-09-07 09:37:07 -07001050 /* *INDENT-OFF* */
1051 REPLY_MACRO2 (VL_API_IP_MROUTE_ADD_DEL_REPLY,
1052 ({
1053 rmp->stats_index = htonl (stats_index);
1054 }));
1055 /* *INDENT-ON* */
Neale Ranns32e1c012016-11-22 17:07:28 +00001056}
1057
Dave Barachb5e8a772016-12-06 12:04:42 -05001058static void
1059send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001060 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1061 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001062{
1063 vl_api_ip_details_t *mp;
1064
1065 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001066 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001067 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -05001068
1069 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001070 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001071 mp->context = context;
1072
Florin Coras6c4dae22018-01-09 06:39:23 -08001073 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001074}
1075
1076static void
1077send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001078 vl_api_registration_t * reg,
Neale Ranns097fa662018-05-01 05:17:55 -07001079 const fib_prefix_t * pfx,
1080 u32 sw_if_index, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001081{
1082 vl_api_ip_address_details_t *mp;
1083
1084 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001085 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001086 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_ADDRESS_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -05001087
Neale Ranns097fa662018-05-01 05:17:55 -07001088 ip_prefix_encode (pfx, &mp->prefix);
Dave Barachb5e8a772016-12-06 12:04:42 -05001089 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001090 mp->sw_if_index = htonl (sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001091
Florin Coras6c4dae22018-01-09 06:39:23 -08001092 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001093}
1094
1095static void
1096vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1097{
1098 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001099 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001100 ip6_main_t *im6 = &ip6_main;
1101 ip4_main_t *im4 = &ip4_main;
1102 ip_lookup_main_t *lm6 = &im6->lookup_main;
1103 ip_lookup_main_t *lm4 = &im4->lookup_main;
1104 ip_interface_address_t *ia = 0;
1105 u32 sw_if_index = ~0;
1106 int rv __attribute__ ((unused)) = 0;
1107
1108 VALIDATE_SW_IF_INDEX (mp);
1109
1110 sw_if_index = ntohl (mp->sw_if_index);
1111
Florin Coras6c4dae22018-01-09 06:39:23 -08001112 reg = vl_api_client_index_to_registration (mp->client_index);
1113 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001114 return;
1115
Dave Barachb5e8a772016-12-06 12:04:42 -05001116 if (mp->is_ipv6)
1117 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001118 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001119 /* Do not send subnet details of the IP-interface for
1120 * unnumbered interfaces. otherwise listening clients
1121 * will be confused that the subnet is applied on more
1122 * than one interface */
1123 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001124 ({
Neale Ranns097fa662018-05-01 05:17:55 -07001125 fib_prefix_t pfx = {
1126 .fp_addr.ip6 = *(ip6_address_t *)ip_interface_address_get_address (lm6, ia),
1127 .fp_len = ia->address_length,
1128 .fp_proto = FIB_PROTOCOL_IP6,
1129 };
1130 send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001131 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001132 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001133 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001134 else
1135 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001136 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001137 foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001138 ({
Neale Ranns097fa662018-05-01 05:17:55 -07001139 fib_prefix_t pfx = {
1140 .fp_addr.ip4 = *(ip4_address_t *)ip_interface_address_get_address (lm4, ia),
1141 .fp_len = ia->address_length,
1142 .fp_proto = FIB_PROTOCOL_IP4,
1143 };
1144
1145 send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001146 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001147 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001148 }
Neale Ranns008dbe12018-09-07 09:32:36 -07001149
Dave Barachb5e8a772016-12-06 12:04:42 -05001150 BAD_SW_IF_INDEX_LABEL;
1151}
1152
1153static void
Neale Ranns9e2f9152018-05-18 02:27:10 -07001154send_ip_unnumbered_details (vpe_api_main_t * am,
1155 vl_api_registration_t * reg,
1156 u32 sw_if_index, u32 ip_sw_if_index, u32 context)
1157{
1158 vl_api_ip_unnumbered_details_t *mp;
1159
1160 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001161 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001162 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_UNNUMBERED_DETAILS);
Neale Ranns9e2f9152018-05-18 02:27:10 -07001163
1164 mp->context = context;
1165 mp->sw_if_index = htonl (sw_if_index);
1166 mp->ip_sw_if_index = htonl (ip_sw_if_index);
1167
1168 vl_api_send_msg (reg, (u8 *) mp);
1169}
1170
1171static void
1172vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
1173{
1174 vnet_main_t *vnm = vnet_get_main ();
1175 vnet_interface_main_t *im = &vnm->interface_main;
1176 int rv __attribute__ ((unused)) = 0;
1177 vpe_api_main_t *am = &vpe_api_main;
1178 vl_api_registration_t *reg;
1179 vnet_sw_interface_t *si;
1180 u32 sw_if_index;
1181
1182 sw_if_index = ntohl (mp->sw_if_index);
1183
1184 reg = vl_api_client_index_to_registration (mp->client_index);
1185 if (!reg)
1186 return;
1187
1188 if (~0 != sw_if_index)
1189 {
1190 VALIDATE_SW_IF_INDEX (mp);
1191
1192 si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
1193
Neale Rannsac3e72c2019-10-06 01:04:26 -07001194 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
Neale Ranns9e2f9152018-05-18 02:27:10 -07001195 {
1196 send_ip_unnumbered_details (am, reg,
1197 sw_if_index,
1198 si->unnumbered_sw_if_index,
1199 mp->context);
1200 }
1201 }
1202 else
1203 {
1204 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001205 pool_foreach (si, im->sw_interfaces)
1206 {
Neale Ranns9e2f9152018-05-18 02:27:10 -07001207 if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1208 {
1209 send_ip_unnumbered_details(am, reg,
1210 si->sw_if_index,
1211 si->unnumbered_sw_if_index,
1212 mp->context);
1213 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001214 }
Neale Ranns9e2f9152018-05-18 02:27:10 -07001215 /* *INDENT-ON* */
1216 }
1217
1218 BAD_SW_IF_INDEX_LABEL;
1219}
1220
1221static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001222vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1223{
1224 vpe_api_main_t *am = &vpe_api_main;
1225 vnet_main_t *vnm = vnet_get_main ();
Neale Rannscbe25aa2019-09-30 10:53:31 +00001226 //vlib_main_t *vm = vlib_get_main ();
Dave Barachb5e8a772016-12-06 12:04:42 -05001227 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001228 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001229 vnet_sw_interface_t *si, *sorted_sis;
1230 u32 sw_if_index = ~0;
1231
Florin Coras6c4dae22018-01-09 06:39:23 -08001232 reg = vl_api_client_index_to_registration (mp->client_index);
1233 if (!reg)
1234 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001235
1236 /* Gather interfaces. */
1237 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1238 _vec_len (sorted_sis) = 0;
1239 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001240 pool_foreach (si, im->sw_interfaces)
1241 {
Dave Barachb5e8a772016-12-06 12:04:42 -05001242 vec_add1 (sorted_sis, si[0]);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001243 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001244 /* *INDENT-ON* */
1245
1246 vec_foreach (si, sorted_sis)
1247 {
1248 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1249 {
Neale Rannscbe25aa2019-09-30 10:53:31 +00001250 /* if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index)) */
1251 /* { */
1252 /* continue; */
1253 /* } */
Dave Barachb5e8a772016-12-06 12:04:42 -05001254 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001255 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001256 }
1257 }
Matthew Smith6d5f6592019-09-18 13:51:46 -05001258
1259 vec_free (sorted_sis);
Dave Barachb5e8a772016-12-06 12:04:42 -05001260}
1261
1262static void
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001263vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t *mp)
Dave Barachb5e8a772016-12-06 12:04:42 -05001264{
1265 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001266 int rv;
1267 u32 table_id;
1268 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001269
Neale Ranns227038a2017-04-21 01:07:59 -07001270 table_id = ntohl (mp->vrf_id);
1271
1272#define _(a,b) if (mp->a) flow_hash_config |= b;
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001273 foreach_flow_hash_bit_v1;
Neale Ranns227038a2017-04-21 01:07:59 -07001274#undef _
1275
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001276 rv = ip_flow_hash_set ((mp->is_ipv6 ? AF_IP6 : AF_IP4), table_id,
1277 flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001278
1279 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1280}
1281
1282static void
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001283vl_api_set_ip_flow_hash_v2_t_handler (vl_api_set_ip_flow_hash_v2_t *mp)
Dave Barachb5e8a772016-12-06 12:04:42 -05001284{
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001285 vl_api_set_ip_flow_hash_v2_reply_t *rmp;
1286 ip_address_family_t af;
Dave Barachb5e8a772016-12-06 12:04:42 -05001287 int rv;
Dave Barachb5e8a772016-12-06 12:04:42 -05001288
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001289 rv = ip_address_family_decode (mp->af, &af);
Dave Barachb5e8a772016-12-06 12:04:42 -05001290
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001291 if (!rv)
1292 rv = ip_flow_hash_set (af, htonl (mp->table_id),
1293 htonl (mp->flow_hash_config));
Dave Barachb5e8a772016-12-06 12:04:42 -05001294
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001295 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_V2_REPLY);
Dave Barachb5e8a772016-12-06 12:04:42 -05001296}
1297
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001298static void
1299vl_api_set_ip_flow_hash_router_id_t_handler (
1300 vl_api_set_ip_flow_hash_router_id_t *mp)
1301{
1302 vl_api_set_ip_flow_hash_router_id_reply_t *rmp;
1303 int rv = 0;
1304
1305 ip_flow_hash_router_id_set (ntohl (mp->router_id));
1306
1307 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_ROUTER_ID_REPLY);
1308}
1309
Neale Ranns32e1c012016-11-22 17:07:28 +00001310void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001311vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001312 u32 context, const mfib_signal_t * mfs)
1313{
1314 vl_api_mfib_signal_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -08001315 const mfib_prefix_t *prefix;
Neale Ranns32e1c012016-11-22 17:07:28 +00001316 mfib_table_t *mfib;
1317 mfib_itf_t *mfi;
1318
1319 mp = vl_msg_api_alloc (sizeof (*mp));
1320
Dave Barachb7b92992018-10-17 10:38:51 -04001321 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001322 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_MFIB_SIGNAL_DETAILS);
Neale Ranns32e1c012016-11-22 17:07:28 +00001323 mp->context = context;
1324
1325 mfi = mfib_itf_get (mfs->mfs_itf);
Neale Ranns9e829a82018-12-17 05:50:32 -08001326 prefix = mfib_entry_get_prefix (mfs->mfs_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +00001327 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
Neale Ranns9e829a82018-12-17 05:50:32 -08001328 prefix->fp_proto);
Neale Ranns32e1c012016-11-22 17:07:28 +00001329 mp->table_id = ntohl (mfib->mft_table_id);
1330 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1331
Neale Ranns097fa662018-05-01 05:17:55 -07001332 ip_mprefix_encode (prefix, &mp->prefix);
Neale Ranns32e1c012016-11-22 17:07:28 +00001333
1334 if (0 != mfs->mfs_buffer_len)
1335 {
1336 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1337
1338 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1339 }
1340 else
1341 {
1342 mp->ip_packet_len = 0;
1343 }
1344
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001345 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001346}
1347
1348static void
1349vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1350{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001351 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001352
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001353 reg = vl_api_client_index_to_registration (mp->client_index);
1354 if (!reg)
1355 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001356
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001357 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001358 ;
1359}
Dave Barachb5e8a772016-12-06 12:04:42 -05001360
Florin Coras595992c2017-11-06 17:17:08 -08001361static void
1362 vl_api_ip_container_proxy_add_del_t_handler
1363 (vl_api_ip_container_proxy_add_del_t * mp)
1364{
1365 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1366 vnet_ip_container_proxy_args_t args;
1367 int rv = 0;
1368 clib_error_t *error;
1369
Dave Barachb7b92992018-10-17 10:38:51 -04001370 clib_memset (&args, 0, sizeof (args));
Neale Ranns37029302018-08-10 05:30:06 -07001371
1372 ip_prefix_decode (&mp->pfx, &args.prefix);
1373
Florin Coras595992c2017-11-06 17:17:08 -08001374 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1375 args.is_add = mp->is_add;
1376 if ((error = vnet_ip_container_proxy_add_del (&args)))
1377 {
1378 rv = clib_error_get_code (error);
1379 clib_error_report (error);
1380 }
1381
1382 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1383}
1384
Neale Ranns8f5fef22020-12-21 08:29:34 +00001385typedef struct ip_walk_ctx_t_
Matus Fabian75b9f452018-10-02 23:27:21 -07001386{
1387 vl_api_registration_t *reg;
1388 u32 context;
Neale Ranns8f5fef22020-12-21 08:29:34 +00001389} ip_walk_ctx_t;
Matus Fabian75b9f452018-10-02 23:27:21 -07001390
1391static int
1392ip_container_proxy_send_details (const fib_prefix_t * pfx, u32 sw_if_index,
1393 void *args)
1394{
1395 vl_api_ip_container_proxy_details_t *mp;
Neale Ranns8f5fef22020-12-21 08:29:34 +00001396 ip_walk_ctx_t *ctx = args;
Matus Fabian75b9f452018-10-02 23:27:21 -07001397
1398 mp = vl_msg_api_alloc (sizeof (*mp));
1399 if (!mp)
1400 return 1;
1401
Dave Barachb7b92992018-10-17 10:38:51 -04001402 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001403 mp->_vl_msg_id =
1404 ntohs (REPLY_MSG_ID_BASE + VL_API_IP_CONTAINER_PROXY_DETAILS);
Matus Fabian75b9f452018-10-02 23:27:21 -07001405 mp->context = ctx->context;
1406
1407 mp->sw_if_index = ntohl (sw_if_index);
1408 ip_prefix_encode (pfx, &mp->prefix);
1409
1410 vl_api_send_msg (ctx->reg, (u8 *) mp);
1411
1412 return 1;
1413}
1414
1415static void
1416vl_api_ip_container_proxy_dump_t_handler (vl_api_ip_container_proxy_dump_t *
1417 mp)
1418{
1419 vl_api_registration_t *reg;
1420
1421 reg = vl_api_client_index_to_registration (mp->client_index);
1422 if (!reg)
1423 return;
1424
Neale Ranns8f5fef22020-12-21 08:29:34 +00001425 ip_walk_ctx_t ctx = {
Matus Fabian75b9f452018-10-02 23:27:21 -07001426 .context = mp->context,
1427 .reg = reg,
1428 };
1429
1430 ip_container_proxy_walk (ip_container_proxy_send_details, &ctx);
1431}
1432
Neale Rannsb8d44812017-11-10 06:53:54 -08001433static void
1434vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1435{
1436 int rv = 0;
1437 vl_api_ioam_enable_reply_t *rmp;
1438 clib_error_t *error;
1439
1440 /* Ignoring the profile id as currently a single profile
1441 * is supported */
1442 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1443 mp->seqno, mp->analyse);
1444 if (error)
1445 {
1446 clib_error_report (error);
1447 rv = clib_error_get_code (error);
1448 }
1449
1450 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1451}
1452
1453static void
1454vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1455{
1456 int rv = 0;
1457 vl_api_ioam_disable_reply_t *rmp;
1458 clib_error_t *error;
1459
1460 error = clear_ioam_rewrite_fn ();
1461 if (error)
1462 {
1463 clib_error_report (error);
1464 rv = clib_error_get_code (error);
1465 }
1466
1467 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1468}
1469
1470static void
1471 vl_api_ip_source_and_port_range_check_add_del_t_handler
1472 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1473{
1474 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1475 int rv = 0;
1476
Neale Rannsb8d44812017-11-10 06:53:54 -08001477 u8 is_add = mp->is_add;
Neale Ranns37029302018-08-10 05:30:06 -07001478 fib_prefix_t pfx;
Neale Rannsb8d44812017-11-10 06:53:54 -08001479 u16 *low_ports = 0;
1480 u16 *high_ports = 0;
1481 u32 vrf_id;
1482 u16 tmp_low, tmp_high;
1483 u8 num_ranges;
1484 int i;
1485
Neale Ranns37029302018-08-10 05:30:06 -07001486 ip_prefix_decode (&mp->prefix, &pfx);
1487
Neale Rannsb8d44812017-11-10 06:53:54 -08001488 // Validate port range
1489 num_ranges = mp->number_of_ranges;
1490 if (num_ranges > 32)
1491 { // This is size of array in VPE.API
1492 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1493 goto reply;
1494 }
1495
1496 vec_reset_length (low_ports);
1497 vec_reset_length (high_ports);
1498
1499 for (i = 0; i < num_ranges; i++)
1500 {
1501 tmp_low = mp->low_ports[i];
1502 tmp_high = mp->high_ports[i];
1503 // If tmp_low <= tmp_high then only need to check tmp_low = 0
1504 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1505 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1506 {
1507 rv = VNET_API_ERROR_INVALID_VALUE;
1508 goto reply;
1509 }
1510 vec_add1 (low_ports, tmp_low);
1511 vec_add1 (high_ports, tmp_high + 1);
1512 }
1513
Neale Rannsb8d44812017-11-10 06:53:54 -08001514 vrf_id = ntohl (mp->vrf_id);
1515
1516 if (vrf_id < 1)
1517 {
1518 rv = VNET_API_ERROR_INVALID_VALUE;
1519 goto reply;
1520 }
1521
1522
Neale Ranns37029302018-08-10 05:30:06 -07001523 if (FIB_PROTOCOL_IP6 == pfx.fp_proto)
Neale Rannsb8d44812017-11-10 06:53:54 -08001524 {
Neale Ranns37029302018-08-10 05:30:06 -07001525 rv = ip6_source_and_port_range_check_add_del (&pfx.fp_addr.ip6,
1526 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08001527 vrf_id,
1528 low_ports,
1529 high_ports, is_add);
1530 }
1531 else
1532 {
Neale Ranns37029302018-08-10 05:30:06 -07001533 rv = ip4_source_and_port_range_check_add_del (&pfx.fp_addr.ip4,
1534 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08001535 vrf_id,
1536 low_ports,
1537 high_ports, is_add);
1538 }
1539
1540reply:
1541 vec_free (low_ports);
1542 vec_free (high_ports);
1543 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
1544}
1545
1546static void
1547 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
1548 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
1549{
1550 vlib_main_t *vm = vlib_get_main ();
1551 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
1552 ip4_main_t *im = &ip4_main;
1553 int rv;
1554 u32 sw_if_index;
1555 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1556 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1557 uword *p = 0;
1558 int i;
1559
1560 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
1561 ntohl (mp->tcp_out_vrf_id);
1562 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
1563 ntohl (mp->udp_out_vrf_id);
1564 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
1565 ntohl (mp->tcp_in_vrf_id);
1566 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
1567 ntohl (mp->udp_in_vrf_id);
1568
1569
1570 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
1571 {
1572 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
1573 {
1574 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
1575
1576 if (p == 0)
1577 {
1578 rv = VNET_API_ERROR_INVALID_VALUE;
1579 goto reply;
1580 }
1581
1582 fib_index[i] = p[0];
1583 }
1584 else
1585 fib_index[i] = ~0;
1586 }
1587 sw_if_index = ntohl (mp->sw_if_index);
1588
1589 VALIDATE_SW_IF_INDEX (mp);
1590
1591 rv =
1592 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
1593 mp->is_add);
1594
1595 BAD_SW_IF_INDEX_LABEL;
1596reply:
1597
1598 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
1599}
1600
Neale Rannscbe25aa2019-09-30 10:53:31 +00001601static void
1602 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1603 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1604{
1605 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1606 ip6_address_t ip;
1607 int rv;
1608
1609 VALIDATE_SW_IF_INDEX (mp);
1610
1611 ip6_address_decode (mp->ip, &ip);
1612
Neale Rannsec40a7d2020-04-23 07:36:12 +00001613 rv = ip6_link_set_local_address (ntohl (mp->sw_if_index), &ip);
Neale Rannscbe25aa2019-09-30 10:53:31 +00001614
1615 BAD_SW_IF_INDEX_LABEL;
1616 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1617}
1618
Neale Ranns9db6ada2019-11-08 12:42:31 +00001619static void
Benoît Ganne58a19152021-01-18 19:24:34 +01001620vl_api_sw_interface_ip6_get_link_local_address_t_handler (
1621 vl_api_sw_interface_ip6_get_link_local_address_t *mp)
1622{
1623 vl_api_sw_interface_ip6_get_link_local_address_reply_t *rmp;
1624 const ip6_address_t *ip = NULL;
1625 int rv = 0;
1626
1627 VALIDATE_SW_IF_INDEX (mp);
1628
1629 ip = ip6_get_link_local_address (ntohl (mp->sw_if_index));
1630 if (NULL == ip)
1631 rv = VNET_API_ERROR_IP6_NOT_ENABLED;
1632
1633 BAD_SW_IF_INDEX_LABEL;
1634 /* clang-format off */
1635 REPLY_MACRO2 (VL_API_SW_INTERFACE_IP6_GET_LINK_LOCAL_ADDRESS_REPLY,
1636 ({
1637 if (!rv)
1638 ip6_address_encode (ip, rmp->ip);
1639 }))
1640 /* clang-format on */
1641}
1642
1643static void
Neale Ranns9db6ada2019-11-08 12:42:31 +00001644vl_api_ip_table_replace_begin_t_handler (vl_api_ip_table_replace_begin_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -08001645{
Neale Ranns9db6ada2019-11-08 12:42:31 +00001646 vl_api_ip_table_replace_begin_reply_t *rmp;
1647 fib_protocol_t fproto;
1648 u32 fib_index;
1649 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001650
Neale Ranns9db6ada2019-11-08 12:42:31 +00001651 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1652 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
Neale Rannsb8d44812017-11-10 06:53:54 -08001653
Neale Ranns9db6ada2019-11-08 12:42:31 +00001654 if (INDEX_INVALID == fib_index)
1655 rv = VNET_API_ERROR_NO_SUCH_FIB;
1656 else
1657 {
1658 fib_table_mark (fib_index, fproto, FIB_SOURCE_API);
1659 mfib_table_mark (mfib_table_find (fproto, ntohl (mp->table.table_id)),
1660 fproto, MFIB_SOURCE_API);
Neale Rannsb8d44812017-11-10 06:53:54 -08001661 }
Neale Ranns9db6ada2019-11-08 12:42:31 +00001662 REPLY_MACRO (VL_API_IP_TABLE_REPLACE_BEGIN_REPLY);
Neale Rannsb8d44812017-11-10 06:53:54 -08001663}
1664
1665static void
Neale Ranns9db6ada2019-11-08 12:42:31 +00001666vl_api_ip_table_replace_end_t_handler (vl_api_ip_table_replace_end_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -08001667{
Neale Ranns9db6ada2019-11-08 12:42:31 +00001668 vl_api_ip_table_replace_end_reply_t *rmp;
1669 fib_protocol_t fproto;
1670 u32 fib_index;
1671 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001672
Neale Ranns9db6ada2019-11-08 12:42:31 +00001673 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1674 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1675
1676 if (INDEX_INVALID == fib_index)
1677 rv = VNET_API_ERROR_NO_SUCH_FIB;
Neale Rannsb8d44812017-11-10 06:53:54 -08001678 else
Neale Ranns9db6ada2019-11-08 12:42:31 +00001679 {
1680 fib_table_sweep (fib_index, fproto, FIB_SOURCE_API);
1681 mfib_table_sweep (mfib_table_find
1682 (fproto, ntohl (mp->table.table_id)), fproto,
1683 MFIB_SOURCE_API);
1684 }
1685 REPLY_MACRO (VL_API_IP_TABLE_REPLACE_END_REPLY);
1686}
Neale Rannsb8d44812017-11-10 06:53:54 -08001687
Neale Ranns9db6ada2019-11-08 12:42:31 +00001688static void
1689vl_api_ip_table_flush_t_handler (vl_api_ip_table_flush_t * mp)
1690{
1691 vl_api_ip_table_flush_reply_t *rmp;
1692 fib_protocol_t fproto;
1693 u32 fib_index;
1694 int rv = 0;
1695
1696 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1697 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1698
1699 if (INDEX_INVALID == fib_index)
1700 rv = VNET_API_ERROR_NO_SUCH_FIB;
1701 else
1702 {
1703 vnet_main_t *vnm = vnet_get_main ();
1704 vnet_interface_main_t *im = &vnm->interface_main;
1705 vnet_sw_interface_t *si;
1706
1707 /* Shut down interfaces in this FIB / clean out intfc routes */
1708 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001709 pool_foreach (si, im->sw_interfaces)
1710 {
Neale Ranns9db6ada2019-11-08 12:42:31 +00001711 if (fib_index == fib_table_get_index_for_sw_if_index (fproto,
1712 si->sw_if_index))
1713 {
1714 u32 flags = si->flags;
1715 flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP;
1716 vnet_sw_interface_set_flags (vnm, si->sw_if_index, flags);
1717 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001718 }
Neale Ranns9db6ada2019-11-08 12:42:31 +00001719 /* *INDENT-ON* */
1720
1721 fib_table_flush (fib_index, fproto, FIB_SOURCE_API);
1722 mfib_table_flush (mfib_table_find (fproto, ntohl (mp->table.table_id)),
1723 fproto, MFIB_SOURCE_API);
1724 }
1725
1726 REPLY_MACRO (VL_API_IP_TABLE_FLUSH_REPLY);
Neale Rannsb8d44812017-11-10 06:53:54 -08001727}
1728
Klement Sekera75e7d132017-09-20 08:26:30 +02001729void
1730vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
1731{
1732 vl_api_ip_reassembly_set_reply_t *rmp;
1733 int rv = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001734 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera75e7d132017-09-20 08:26:30 +02001735 {
Klement Sekerade34c352019-06-25 11:19:22 +00001736 case IP_REASS_TYPE_FULL:
1737 if (mp->is_ip6)
1738 {
1739 rv = ip6_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1740 clib_net_to_host_u32
1741 (mp->max_reassemblies),
1742 clib_net_to_host_u32
1743 (mp->max_reassembly_length),
1744 clib_net_to_host_u32
1745 (mp->expire_walk_interval_ms));
1746 }
1747 else
1748 {
1749 rv = ip4_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1750 clib_net_to_host_u32
1751 (mp->max_reassemblies),
1752 clib_net_to_host_u32
1753 (mp->max_reassembly_length),
1754 clib_net_to_host_u32
1755 (mp->expire_walk_interval_ms));
1756 }
1757 break;
1758 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1759 if (mp->is_ip6)
1760 {
1761 rv =
1762 ip6_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1763 clib_net_to_host_u32 (mp->max_reassemblies),
1764 clib_net_to_host_u32
1765 (mp->max_reassembly_length),
1766 clib_net_to_host_u32
1767 (mp->expire_walk_interval_ms));
1768 }
1769 else
1770 {
1771 rv = ip4_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1772 clib_net_to_host_u32 (mp->max_reassemblies),
1773 clib_net_to_host_u32
1774 (mp->max_reassembly_length),
1775 clib_net_to_host_u32
1776 (mp->expire_walk_interval_ms));
1777 }
1778 break;
Klement Sekera75e7d132017-09-20 08:26:30 +02001779 }
1780
1781 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
1782}
1783
1784void
1785vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
1786{
Ole Troan2e1c8962019-04-10 09:44:23 +02001787 vl_api_registration_t *rp;
Klement Sekera75e7d132017-09-20 08:26:30 +02001788
Ole Troan2e1c8962019-04-10 09:44:23 +02001789 rp = vl_api_client_index_to_registration (mp->client_index);
1790 if (rp == 0)
Klement Sekera75e7d132017-09-20 08:26:30 +02001791 return;
1792
1793 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001794 clib_memset (rmp, 0, sizeof (*rmp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001795 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_REASSEMBLY_GET_REPLY);
Klement Sekera75e7d132017-09-20 08:26:30 +02001796 rmp->context = mp->context;
1797 rmp->retval = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001798 u32 timeout_ms;
1799 u32 max_reassemblies;
1800 u32 max_reassembly_length;
1801 u32 expire_walk_interval_ms;
1802 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera75e7d132017-09-20 08:26:30 +02001803 {
Klement Sekerade34c352019-06-25 11:19:22 +00001804 case IP_REASS_TYPE_FULL:
1805 if (mp->is_ip6)
1806 {
1807 rmp->is_ip6 = 1;
1808 ip6_full_reass_get (&timeout_ms, &max_reassemblies,
1809 &max_reassembly_length,
1810 &expire_walk_interval_ms);
1811 }
1812 else
1813 {
1814 rmp->is_ip6 = 0;
1815 ip4_full_reass_get (&timeout_ms, &max_reassemblies,
1816 &max_reassembly_length,
1817 &expire_walk_interval_ms);
1818 }
1819 break;
1820 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1821 if (mp->is_ip6)
1822 {
1823 rmp->is_ip6 = 1;
1824 ip6_sv_reass_get (&timeout_ms, &max_reassemblies,
1825 &max_reassembly_length, &expire_walk_interval_ms);
1826 }
1827 else
1828 {
1829 rmp->is_ip6 = 0;
1830 ip4_sv_reass_get (&timeout_ms, &max_reassemblies,
1831 &max_reassembly_length, &expire_walk_interval_ms);
1832 }
1833 break;
Klement Sekera75e7d132017-09-20 08:26:30 +02001834 }
Matthew Smithb3174df2019-12-03 12:41:53 -06001835 rmp->timeout_ms = clib_host_to_net_u32 (timeout_ms);
1836 rmp->max_reassemblies = clib_host_to_net_u32 (max_reassemblies);
1837 rmp->max_reassembly_length = clib_host_to_net_u32 (max_reassembly_length);
Klement Sekera75e7d132017-09-20 08:26:30 +02001838 rmp->expire_walk_interval_ms =
Matthew Smithb3174df2019-12-03 12:41:53 -06001839 clib_host_to_net_u32 (expire_walk_interval_ms);
Ole Troan2e1c8962019-04-10 09:44:23 +02001840 vl_api_send_msg (rp, (u8 *) rmp);
Klement Sekera75e7d132017-09-20 08:26:30 +02001841}
1842
Klement Sekera4c533132018-02-22 11:41:12 +01001843void
1844 vl_api_ip_reassembly_enable_disable_t_handler
1845 (vl_api_ip_reassembly_enable_disable_t * mp)
1846{
1847 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
1848 int rv = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001849 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera4c533132018-02-22 11:41:12 +01001850 {
Klement Sekerade34c352019-06-25 11:19:22 +00001851 case IP_REASS_TYPE_FULL:
Klement Sekera896c8962019-06-24 11:52:49 +00001852 rv =
Klement Sekerade34c352019-06-25 11:19:22 +00001853 ip4_full_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1854 mp->enable_ip4);
1855 if (0 == rv)
1856 rv =
1857 ip6_full_reass_enable_disable (clib_net_to_host_u32
1858 (mp->sw_if_index), mp->enable_ip6);
1859 break;
1860 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1861 rv =
1862 ip4_sv_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1863 mp->enable_ip4);
1864 if (0 == rv)
1865 {
1866 rv =
1867 ip6_sv_reass_enable_disable (clib_net_to_host_u32
1868 (mp->sw_if_index), mp->enable_ip6);
1869 }
1870 break;
Klement Sekera4c533132018-02-22 11:41:12 +01001871 }
1872
Choree7f61d12018-11-28 10:27:58 +03301873 REPLY_MACRO (VL_API_IP_REASSEMBLY_ENABLE_DISABLE_REPLY);
Klement Sekera4c533132018-02-22 11:41:12 +01001874}
1875
Neale Ranns92207752019-06-03 13:21:40 +00001876static walk_rc_t
1877send_ip_punt_redirect_details (u32 rx_sw_if_index,
1878 const ip_punt_redirect_rx_t * ipr, void *arg)
1879{
Pavel Kotucek609e1212018-11-27 09:59:44 +01001880 vl_api_ip_punt_redirect_details_t *mp;
Neale Ranns097fa662018-05-01 05:17:55 -07001881 fib_path_encode_ctx_t path_ctx = {
1882 .rpaths = NULL,
1883 };
Neale Ranns8f5fef22020-12-21 08:29:34 +00001884 ip_walk_ctx_t *ctx = arg;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001885
1886 mp = vl_msg_api_alloc (sizeof (*mp));
1887 if (!mp)
Neale Ranns92207752019-06-03 13:21:40 +00001888 return (WALK_STOP);;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001889
1890 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001891 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_PUNT_REDIRECT_DETAILS);
Neale Ranns92207752019-06-03 13:21:40 +00001892 mp->context = ctx->context;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001893
Neale Ranns097fa662018-05-01 05:17:55 -07001894 fib_path_list_walk_w_ext (ipr->pl, NULL, fib_path_encode, &path_ctx);
Neale Ranns92207752019-06-03 13:21:40 +00001895
1896 mp->punt.rx_sw_if_index = htonl (rx_sw_if_index);
Neale Ranns097fa662018-05-01 05:17:55 -07001897 mp->punt.tx_sw_if_index = htonl (path_ctx.rpaths[0].frp_sw_if_index);
Neale Ranns92207752019-06-03 13:21:40 +00001898
Neale Ranns097fa662018-05-01 05:17:55 -07001899 ip_address_encode (&path_ctx.rpaths[0].frp_addr,
Neale Ranns92207752019-06-03 13:21:40 +00001900 fib_proto_to_ip46 (ipr->fproto), &mp->punt.nh);
1901
1902 vl_api_send_msg (ctx->reg, (u8 *) mp);
1903
Neale Ranns097fa662018-05-01 05:17:55 -07001904 vec_free (path_ctx.rpaths);
Neale Ranns92207752019-06-03 13:21:40 +00001905
1906 return (WALK_CONTINUE);
Pavel Kotucek609e1212018-11-27 09:59:44 +01001907}
1908
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02001909static walk_rc_t
1910send_ip_punt_redirect_v2_details (u32 rx_sw_if_index,
1911 const ip_punt_redirect_rx_t *ipr, void *arg)
1912{
1913 vl_api_ip_punt_redirect_v2_details_t *mp;
1914 fib_path_encode_ctx_t path_ctx = {
1915 .rpaths = NULL,
1916 };
1917 fib_route_path_t *rpath;
1918 ip_walk_ctx_t *ctx = arg;
1919 vl_api_fib_path_t *fp;
1920 int n_paths;
1921
1922 fib_path_list_walk_w_ext (ipr->pl, NULL, fib_path_encode, &path_ctx);
1923
1924 n_paths = vec_len (path_ctx.rpaths);
1925 mp = vl_msg_api_alloc (sizeof (*mp) + n_paths * sizeof (*fp));
1926 if (!mp)
1927 return (WALK_STOP);
1928
1929 clib_memset (mp, 0, sizeof (*mp));
1930 mp->_vl_msg_id =
1931 ntohs (REPLY_MSG_ID_BASE + VL_API_IP_PUNT_REDIRECT_V2_DETAILS);
1932 mp->context = ctx->context;
1933 mp->punt.rx_sw_if_index = htonl (rx_sw_if_index);
1934 mp->punt.n_paths = htonl (n_paths);
1935 fp = mp->punt.paths;
1936 vec_foreach (rpath, path_ctx.rpaths)
1937 {
1938 fib_api_path_encode (rpath, fp);
1939 fp++;
1940 }
1941 mp->punt.af = (ipr->fproto == FIB_PROTOCOL_IP6) ? ADDRESS_IP6 : ADDRESS_IP4;
1942
1943 vl_api_send_msg (ctx->reg, (u8 *) mp);
1944
1945 vec_free (path_ctx.rpaths);
1946
1947 return (WALK_CONTINUE);
1948}
1949
1950static void
1951vl_api_ip_punt_redirect_dump_common (ip_walk_ctx_t *ctx, fib_protocol_t fproto,
1952 u32 rx_sw_if_index,
1953 ip_punt_redirect_walk_cb_t cb)
1954{
1955
1956 if ((u32) ~0 != rx_sw_if_index)
1957 {
1958 index_t pri;
1959 pri = ip_punt_redirect_find (fproto, rx_sw_if_index);
1960
1961 if (INDEX_INVALID == pri)
1962 return;
1963
1964 cb (rx_sw_if_index, ip_punt_redirect_get (pri), ctx);
1965 }
1966 else
1967 ip_punt_redirect_walk (fproto, cb, ctx);
1968}
1969
Pavel Kotucek609e1212018-11-27 09:59:44 +01001970static void
1971vl_api_ip_punt_redirect_dump_t_handler (vl_api_ip_punt_redirect_dump_t * mp)
1972{
1973 vl_api_registration_t *reg;
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02001974 fib_protocol_t fproto;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001975
Pavel Kotucek609e1212018-11-27 09:59:44 +01001976 reg = vl_api_client_index_to_registration (mp->client_index);
1977 if (!reg)
1978 return;
1979
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02001980 fproto = (mp->is_ipv6 == 1) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001981
Neale Ranns8f5fef22020-12-21 08:29:34 +00001982 ip_walk_ctx_t ctx = {
Neale Ranns92207752019-06-03 13:21:40 +00001983 .reg = reg,
1984 .context = mp->context,
1985 };
1986
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02001987 vl_api_ip_punt_redirect_dump_common (&ctx, fproto, ntohl (mp->sw_if_index),
1988 send_ip_punt_redirect_details);
1989}
Neale Ranns92207752019-06-03 13:21:40 +00001990
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02001991static void
1992vl_api_ip_punt_redirect_v2_dump_t_handler (
1993 vl_api_ip_punt_redirect_v2_dump_t *mp)
1994{
1995 vl_api_registration_t *reg;
1996 ip_address_family_t af;
1997 fib_protocol_t fproto;
1998 int rv = 0;
Neale Ranns92207752019-06-03 13:21:40 +00001999
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02002000 reg = vl_api_client_index_to_registration (mp->client_index);
2001 if (!reg)
2002 return;
Neale Ranns92207752019-06-03 13:21:40 +00002003
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02002004 rv = ip_address_family_decode (mp->af, &af);
2005 if (rv != 0)
2006 return;
2007
2008 fproto = (af == AF_IP6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
2009
2010 ip_walk_ctx_t ctx = {
2011 .reg = reg,
2012 .context = mp->context,
2013 };
2014
2015 vl_api_ip_punt_redirect_dump_common (&ctx, fproto, ntohl (mp->sw_if_index),
2016 send_ip_punt_redirect_v2_details);
Pavel Kotucek609e1212018-11-27 09:59:44 +01002017}
2018
Neale Ranns8f5fef22020-12-21 08:29:34 +00002019void
2020vl_api_ip_path_mtu_update_t_handler (vl_api_ip_path_mtu_update_t *mp)
2021{
2022 vl_api_ip_path_mtu_update_reply_t *rmp;
2023 ip_address_t nh;
2024 int rv = 0;
2025
2026 ip_address_decode2 (&mp->pmtu.nh, &nh);
2027
2028 rv = ip_path_mtu_update (&nh, ntohl (mp->pmtu.table_id),
2029 ntohs (mp->pmtu.path_mtu));
2030
2031 REPLY_MACRO (VL_API_IP_PATH_MTU_UPDATE_REPLY);
2032}
2033
2034void
2035vl_api_ip_path_mtu_replace_begin_t_handler (
2036 vl_api_ip_path_mtu_replace_begin_t *mp)
2037{
2038 vl_api_ip_path_mtu_replace_begin_reply_t *rmp;
2039 int rv;
2040
2041 rv = ip_path_mtu_replace_begin ();
2042
2043 REPLY_MACRO (VL_API_IP_PATH_MTU_REPLACE_BEGIN_REPLY);
2044}
2045
2046void
2047vl_api_ip_path_mtu_replace_end_t_handler (vl_api_ip_path_mtu_replace_end_t *mp)
2048{
2049 vl_api_ip_path_mtu_replace_end_reply_t *rmp;
2050 int rv;
2051
2052 rv = ip_path_mtu_replace_end ();
2053
2054 REPLY_MACRO (VL_API_IP_PATH_MTU_REPLACE_END_REPLY);
2055}
2056
2057static void
2058send_ip_path_mtu_details (index_t ipti, vl_api_registration_t *rp, u32 context)
2059{
2060 vl_api_ip_path_mtu_details_t *rmp;
2061 ip_address_t ip;
2062 ip_pmtu_t *ipt;
2063
2064 ipt = ip_path_mtu_get (ipti);
2065
2066 REPLY_MACRO_DETAILS4 (VL_API_IP_PATH_MTU_DETAILS, rp, context, ({
2067 ip_pmtu_get_ip (ipt, &ip);
2068 ip_address_encode2 (&ip, &rmp->pmtu.nh);
2069 rmp->pmtu.table_id =
2070 htonl (ip_pmtu_get_table_id (ipt));
2071 rmp->pmtu.path_mtu = htons (ipt->ipt_cfg_pmtu);
2072 }));
2073}
2074
2075static void
2076vl_api_ip_path_mtu_get_t_handler (vl_api_ip_path_mtu_get_t *mp)
2077{
2078 vl_api_ip_path_mtu_get_reply_t *rmp;
2079 i32 rv = 0;
2080
2081 REPLY_AND_DETAILS_MACRO (
2082 VL_API_IP_PATH_MTU_GET_REPLY, ip_pmtu_pool,
2083 ({ send_ip_path_mtu_details (cursor, rp, mp->context); }));
2084}
2085
Filip Tehlar5ff59a12021-06-23 14:38:38 +00002086#include <vnet/ip/ip.api.c>
Dave Barachb5e8a772016-12-06 12:04:42 -05002087
2088static clib_error_t *
2089ip_api_hookup (vlib_main_t * vm)
2090{
Dave Barach39d69112019-11-27 11:42:13 -05002091 api_main_t *am = vlibapi_get_main ();
Dave Barachb5e8a772016-12-06 12:04:42 -05002092
Dave Barachb5e8a772016-12-06 12:04:42 -05002093 /*
Neale Ranns6a231a12018-10-17 06:38:00 +00002094 * Mark the route add/del API as MP safe
2095 */
Neale Ranns097fa662018-05-01 05:17:55 -07002096 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL] = 1;
2097 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_REPLY] = 1;
Neale Ranns976b2592019-12-04 06:11:00 +00002098 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_V2] = 1;
2099 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_V2_REPLY] = 1;
Neale Ranns6a231a12018-10-17 06:38:00 +00002100
2101 /*
Dave Barachb5e8a772016-12-06 12:04:42 -05002102 * Set up the (msg_name, crc, message-id) table
2103 */
Filip Tehlar5ff59a12021-06-23 14:38:38 +00002104 REPLY_MSG_ID_BASE = setup_message_id_table ();
Dave Barachb5e8a772016-12-06 12:04:42 -05002105
Dave Barachb5e8a772016-12-06 12:04:42 -05002106 return 0;
2107}
2108
2109VLIB_API_INIT_FUNCTION (ip_api_hookup);
2110
2111/*
2112 * fd.io coding-style-patch-verification: ON
2113 *
2114 * Local Variables:
2115 * eval: (c-set-style "gnu")
2116 * End:
2117 */