blob: 2c9589741b7bfb12a10d6630822fa3db65e62500 [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)
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100517 {
518 vec_validate (rpaths, n_paths - 1);
519 }
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +0200520
521 for (ii = 0; ii < n_paths; ii++)
522 {
523 apath = &mp->punt.paths[ii];
524 rpath = &rpaths[ii];
525
526 rv = fib_api_path_decode (apath, rpath);
527
528 if (rv != 0)
529 goto out;
530 }
531
532 ip_punt_redirect_t_handler_common (mp->is_add, rx_sw_if_index, af, rpaths);
533
534out:
535 vec_free (rpaths);
536
537 REPLY_MACRO (VL_API_ADD_DEL_IP_PUNT_REDIRECT_V2_REPLY);
538}
539
Steven Luongc3ed1c92020-07-27 10:06:58 -0700540static clib_error_t *
541call_elf_section_ip_table_callbacks (vnet_main_t * vnm, u32 table_id,
542 u32 flags,
543 _vnet_ip_table_function_list_elt_t **
544 elts)
545{
546 _vnet_ip_table_function_list_elt_t *elt;
547 vnet_ip_table_function_priority_t prio;
548 clib_error_t *error = 0;
549
550 for (prio = VNET_IP_TABLE_FUNC_PRIORITY_LOW;
551 prio <= VNET_IP_TABLE_FUNC_PRIORITY_HIGH; prio++)
552 {
553 elt = elts[prio];
554
555 while (elt)
556 {
557 error = elt->fp (vnm, table_id, flags);
558 if (error)
559 return error;
560 elt = elt->next_ip_table_function;
561 }
562 }
563 return error;
564}
565
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700566void
Neale Ranns15002542017-09-10 04:39:11 -0700567ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
568{
569 u32 fib_index, mfib_index;
Steven Luongc3ed1c92020-07-27 10:06:58 -0700570 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns15002542017-09-10 04:39:11 -0700571
572 /*
573 * ignore action on the default table - this is always present
574 * and cannot be added nor deleted from the API
575 */
576 if (0 != table_id)
577 {
578 /*
579 * The API holds only one lock on the table.
580 * i.e. it can be added many times via the API but needs to be
581 * deleted only once.
582 * The FIB index for unicast and multicast is not necessarily the
583 * same, since internal VPP systesm (like LISP and SR) create
584 * their own unicast tables.
585 */
586 fib_index = fib_table_find (fproto, table_id);
587 mfib_index = mfib_table_find (fproto, table_id);
588
Steven Luongc3ed1c92020-07-27 10:06:58 -0700589 if ((~0 != fib_index) || (~0 != mfib_index))
590 call_elf_section_ip_table_callbacks (vnm, table_id, 0 /* is_add */ ,
591 vnm->ip_table_add_del_functions);
592
Neale Ranns15002542017-09-10 04:39:11 -0700593 if (~0 != fib_index)
594 {
595 fib_table_unlock (fib_index, fproto,
596 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
597 }
598 if (~0 != mfib_index)
599 {
600 mfib_table_unlock (mfib_index, fproto,
601 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
602 }
603 }
604}
605
Aloys Augustin6e4cfb52021-09-16 20:53:14 +0200606/*
607 * Returns an unused table id, and ~0 if it can't find one.
608 */
609u32
610ip_table_get_unused_id (fib_protocol_t fproto)
611{
612 int i, j;
Aloys Augustin400f23f2021-09-26 18:26:15 +0200613 static u32 seed = 0;
Aloys Augustin6e4cfb52021-09-16 20:53:14 +0200614 /* limit to 1M tries */
615 for (j = 0; j < 1 << 10; j++)
616 {
617 seed = random_u32 (&seed);
618 for (i = 0; i < 1 << 10; i++)
619 {
620 /* look around randomly generated id */
621 seed += (2 * (i % 2) - 1) * i;
622 if (seed == ~0)
623 continue;
624 if (fib_table_find (fproto, seed) == ~0)
625 return seed;
626 }
627 }
628
629 return ~0;
630}
631
Neale Ranns15002542017-09-10 04:39:11 -0700632void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700633vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
634{
635 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns097fa662018-05-01 05:17:55 -0700636 fib_protocol_t fproto = (mp->table.is_ip6 ?
637 FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
638 u32 table_id = ntohl (mp->table.table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700639 int rv = 0;
640
Neale Ranns15002542017-09-10 04:39:11 -0700641 if (mp->is_add)
642 {
Neale Ranns097fa662018-05-01 05:17:55 -0700643 ip_table_create (fproto, table_id, 1, mp->table.name);
Neale Ranns15002542017-09-10 04:39:11 -0700644 }
645 else
646 {
647 ip_table_delete (fproto, table_id, 1);
648 }
649
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700650 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
651}
652
Aloys Augustin6e4cfb52021-09-16 20:53:14 +0200653void
654vl_api_ip_table_allocate_t_handler (vl_api_ip_table_allocate_t *mp)
655{
656 vl_api_ip_table_allocate_reply_t *rmp;
657 fib_protocol_t fproto =
658 (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
659 u32 table_id = ntohl (mp->table.table_id);
660 int rv = 0;
661
662 if (~0 == table_id)
663 table_id = ip_table_get_unused_id (fproto);
664
665 if (~0 == table_id)
666 rv = VNET_API_ERROR_EAGAIN;
667 else
668 ip_table_create (fproto, table_id, 1, mp->table.name);
669
670 REPLY_MACRO2 (VL_API_IP_TABLE_ALLOCATE_REPLY, {
671 clib_memcpy_fast (&rmp->table, &mp->table, sizeof (mp->table));
672 rmp->table.table_id = htonl (table_id);
673 })
674}
675
Dave Barachb5e8a772016-12-06 12:04:42 -0500676static int
Neale Ranns097fa662018-05-01 05:17:55 -0700677ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp, u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500678{
Neale Ranns097fa662018-05-01 05:17:55 -0700679 fib_route_path_t *rpaths = NULL, *rpath;
680 fib_entry_flag_t entry_flags;
681 vl_api_fib_path_t *apath;
682 fib_prefix_t pfx;
683 u32 fib_index;
684 int rv, ii;
Dave Barachb5e8a772016-12-06 12:04:42 -0500685
Neale Ranns097fa662018-05-01 05:17:55 -0700686 entry_flags = FIB_ENTRY_FLAG_NONE;
687 ip_prefix_decode (&mp->route.prefix, &pfx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500688
Neale Ranns097fa662018-05-01 05:17:55 -0700689 rv = fib_api_table_id_decode (pfx.fp_proto,
690 ntohl (mp->route.table_id), &fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500691 if (0 != rv)
Neale Ranns097fa662018-05-01 05:17:55 -0700692 goto out;
Dave Barachb5e8a772016-12-06 12:04:42 -0500693
Neale Rannsc2ac2352019-07-02 14:33:29 +0000694 if (0 != mp->route.n_paths)
695 vec_validate (rpaths, mp->route.n_paths - 1);
Neale Ranns097fa662018-05-01 05:17:55 -0700696
697 for (ii = 0; ii < mp->route.n_paths; ii++)
698 {
699 apath = &mp->route.paths[ii];
700 rpath = &rpaths[ii];
701
702 rv = fib_api_path_decode (apath, rpath);
703
704 if ((rpath->frp_flags & FIB_ROUTE_PATH_LOCAL) &&
705 (~0 == rpath->frp_sw_if_index))
706 entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
707
708 if (0 != rv)
709 goto out;
710 }
711
Neale Ranns976b2592019-12-04 06:11:00 +0000712 rv = fib_api_route_add_del (mp->is_add, mp->is_multipath, fib_index, &pfx,
713 FIB_SOURCE_API, entry_flags, rpaths);
714
715 if (mp->is_add && 0 == rv)
716 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
717
718out:
719 vec_free (rpaths);
720
721 return (rv);
722}
723
724static int
725ip_route_add_del_v2_t_handler (vl_api_ip_route_add_del_v2_t *mp,
726 u32 *stats_index)
727{
728 fib_route_path_t *rpaths = NULL, *rpath;
729 fib_entry_flag_t entry_flags;
730 vl_api_fib_path_t *apath;
731 fib_source_t src;
732 fib_prefix_t pfx;
733 u32 fib_index;
734 int rv, ii;
735
736 entry_flags = FIB_ENTRY_FLAG_NONE;
737 ip_prefix_decode (&mp->route.prefix, &pfx);
738
739 rv = fib_api_table_id_decode (pfx.fp_proto, ntohl (mp->route.table_id),
740 &fib_index);
741 if (0 != rv)
742 goto out;
743
744 if (0 != mp->route.n_paths)
745 vec_validate (rpaths, mp->route.n_paths - 1);
746
747 for (ii = 0; ii < mp->route.n_paths; ii++)
748 {
749 apath = &mp->route.paths[ii];
750 rpath = &rpaths[ii];
751
752 rv = fib_api_path_decode (apath, rpath);
753
754 if ((rpath->frp_flags & FIB_ROUTE_PATH_LOCAL) &&
755 (~0 == rpath->frp_sw_if_index))
756 entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
757
758 if (0 != rv)
759 goto out;
760 }
761
762 src = (0 == mp->route.src ? FIB_SOURCE_API : mp->route.src);
763
764 rv = fib_api_route_add_del (mp->is_add, mp->is_multipath, fib_index, &pfx,
765 src, entry_flags, rpaths);
Neale Ranns008dbe12018-09-07 09:32:36 -0700766
767 if (mp->is_add && 0 == rv)
768 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
769
Neale Ranns097fa662018-05-01 05:17:55 -0700770out:
771 vec_free (rpaths);
Neale Ranns008dbe12018-09-07 09:32:36 -0700772
773 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -0500774}
775
776void
Neale Ranns097fa662018-05-01 05:17:55 -0700777vl_api_ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500778{
Neale Ranns097fa662018-05-01 05:17:55 -0700779 vl_api_ip_route_add_del_reply_t *rmp;
780 u32 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -0500781 int rv;
Dave Barachb5e8a772016-12-06 12:04:42 -0500782
Neale Ranns097fa662018-05-01 05:17:55 -0700783 rv = ip_route_add_del_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500784
Neale Ranns008dbe12018-09-07 09:32:36 -0700785 /* *INDENT-OFF* */
Neale Ranns097fa662018-05-01 05:17:55 -0700786 REPLY_MACRO2 (VL_API_IP_ROUTE_ADD_DEL_REPLY,
Neale Ranns008dbe12018-09-07 09:32:36 -0700787 ({
788 rmp->stats_index = htonl (stats_index);
789 }))
790 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500791}
792
Neale Ranns15002542017-09-10 04:39:11 -0700793void
Neale Ranns976b2592019-12-04 06:11:00 +0000794vl_api_ip_route_add_del_v2_t_handler (vl_api_ip_route_add_del_v2_t *mp)
795{
796 vl_api_ip_route_add_del_v2_reply_t *rmp;
797 u32 stats_index = ~0;
798 int rv;
799
800 rv = ip_route_add_del_v2_t_handler (mp, &stats_index);
801
802 /* clang-format off */
803 REPLY_MACRO2 (VL_API_IP_ROUTE_ADD_DEL_V2_REPLY,
804 ({
805 rmp->stats_index = htonl (stats_index);
806 }))
807 /* clang-format on */
808}
809
810void
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400811vl_api_ip_route_lookup_t_handler (vl_api_ip_route_lookup_t * mp)
812{
813 vl_api_ip_route_lookup_reply_t *rmp = NULL;
814 fib_route_path_t *rpaths = NULL, *rpath;
815 const fib_prefix_t *pfx = NULL;
816 fib_prefix_t lookup;
817 vl_api_fib_path_t *fp;
818 fib_node_index_t fib_entry_index;
819 u32 fib_index;
820 int npaths = 0;
821 int rv;
822
823 ip_prefix_decode (&mp->prefix, &lookup);
824 rv = fib_api_table_id_decode (lookup.fp_proto, ntohl (mp->table_id),
825 &fib_index);
826 if (PREDICT_TRUE (!rv))
827 {
828 if (mp->exact)
829 fib_entry_index = fib_table_lookup_exact_match (fib_index, &lookup);
830 else
831 fib_entry_index = fib_table_lookup (fib_index, &lookup);
832 if (fib_entry_index == FIB_NODE_INDEX_INVALID)
833 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
834 else
835 {
836 pfx = fib_entry_get_prefix (fib_entry_index);
837 rpaths = fib_entry_encode (fib_entry_index);
838 npaths = vec_len (rpaths);
839 }
840 }
841
842 /* *INDENT-OFF* */
843 REPLY_MACRO3_ZERO(VL_API_IP_ROUTE_LOOKUP_REPLY,
844 npaths * sizeof (*fp),
845 ({
846 if (!rv)
847 {
848 ip_prefix_encode (pfx, &rmp->route.prefix);
849 rmp->route.table_id = mp->table_id;
850 rmp->route.n_paths = npaths;
851 rmp->route.stats_index = fib_table_entry_get_stats_index (fib_index, pfx);
852 rmp->route.stats_index = htonl (rmp->route.stats_index);
853
854 fp = rmp->route.paths;
855 vec_foreach (rpath, rpaths)
856 {
857 fib_api_path_encode (rpath, fp);
858 fp++;
859 }
860 }
861 }));
862 /* *INDENT-ON* */
863 vec_free (rpaths);
864}
865
866void
Neale Ranns976b2592019-12-04 06:11:00 +0000867vl_api_ip_route_lookup_v2_t_handler (vl_api_ip_route_lookup_v2_t *mp)
868{
869 vl_api_ip_route_lookup_v2_reply_t *rmp = NULL;
870 fib_route_path_t *rpaths = NULL, *rpath;
871 const fib_prefix_t *pfx = NULL;
872 fib_prefix_t lookup;
873 vl_api_fib_path_t *fp;
874 fib_node_index_t fib_entry_index;
875 u32 fib_index;
876 int npaths = 0;
877 fib_source_t src = 0;
878 int rv;
879
880 ip_prefix_decode (&mp->prefix, &lookup);
881 rv = fib_api_table_id_decode (lookup.fp_proto, ntohl (mp->table_id),
882 &fib_index);
883 if (PREDICT_TRUE (!rv))
884 {
885 if (mp->exact)
886 fib_entry_index = fib_table_lookup_exact_match (fib_index, &lookup);
887 else
888 fib_entry_index = fib_table_lookup (fib_index, &lookup);
889 if (fib_entry_index == FIB_NODE_INDEX_INVALID)
890 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
891 else
892 {
893 pfx = fib_entry_get_prefix (fib_entry_index);
894 rpaths = fib_entry_encode (fib_entry_index);
895 npaths = vec_len (rpaths);
896 src = fib_entry_get_best_source (fib_entry_index);
897 }
898 }
899
900 /* clang-format off */
901 REPLY_MACRO3_ZERO(VL_API_IP_ROUTE_LOOKUP_V2_REPLY,
902 npaths * sizeof (*fp),
903 ({
904 if (!rv)
905 {
906 ip_prefix_encode (pfx, &rmp->route.prefix);
907 rmp->route.table_id = mp->table_id;
908 rmp->route.n_paths = npaths;
909 rmp->route.src = src;
910 rmp->route.stats_index = fib_table_entry_get_stats_index (fib_index, pfx);
911 rmp->route.stats_index = htonl (rmp->route.stats_index);
912
913 fp = rmp->route.paths;
914 vec_foreach (rpath, rpaths)
915 {
916 fib_api_path_encode (rpath, fp);
917 fp++;
918 }
919 }
920 }));
921 /* clang-format on */
922 vec_free (rpaths);
923}
924
925void
Neale Ranns2297af02017-09-12 09:45:04 -0700926ip_table_create (fib_protocol_t fproto,
927 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -0700928{
929 u32 fib_index, mfib_index;
Steven Luongc3ed1c92020-07-27 10:06:58 -0700930 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns15002542017-09-10 04:39:11 -0700931
932 /*
933 * ignore action on the default table - this is always present
934 * and cannot be added nor deleted from the API
935 */
936 if (0 != table_id)
937 {
938 /*
939 * The API holds only one lock on the table.
940 * i.e. it can be added many times via the API but needs to be
941 * deleted only once.
942 * The FIB index for unicast and multicast is not necessarily the
943 * same, since internal VPP systesm (like LISP and SR) create
944 * their own unicast tables.
945 */
946 fib_index = fib_table_find (fproto, table_id);
947 mfib_index = mfib_table_find (fproto, table_id);
948
Nathan Skrzypczak275bd792021-09-17 17:29:14 +0200949 /*
950 * Always try to re-lock in case the fib was deleted by an API call
951 * but was not yet freed because some other locks were held
952 */
953 fib_table_find_or_create_and_lock_w_name (
954 fproto, table_id, (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI), name);
955 mfib_table_find_or_create_and_lock_w_name (
956 fproto, table_id, (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI), name);
Steven Luongc3ed1c92020-07-27 10:06:58 -0700957
958 if ((~0 == fib_index) || (~0 == mfib_index))
959 call_elf_section_ip_table_callbacks (vnm, table_id, 1 /* is_add */ ,
960 vnm->ip_table_add_del_functions);
Neale Ranns15002542017-09-10 04:39:11 -0700961 }
962}
963
Neale Ranns097fa662018-05-01 05:17:55 -0700964static u32
Neale Ranns32e1c012016-11-22 17:07:28 +0000965mroute_add_del_handler (u8 is_add,
Neale Ranns097fa662018-05-01 05:17:55 -0700966 u8 is_multipath,
Neale Ranns32e1c012016-11-22 17:07:28 +0000967 u32 fib_index,
968 const mfib_prefix_t * prefix,
969 u32 entry_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700970 u32 rpf_id, fib_route_path_t * rpaths)
Neale Ranns32e1c012016-11-22 17:07:28 +0000971{
Neale Ranns097fa662018-05-01 05:17:55 -0700972 u32 mfib_entry_index = ~0;
Neale Ranns28c142e2018-09-07 09:37:07 -0700973
Neale Ranns097fa662018-05-01 05:17:55 -0700974 if (0 == vec_len (rpaths))
Neale Ranns32e1c012016-11-22 17:07:28 +0000975 {
Neale Ranns28c142e2018-09-07 09:37:07 -0700976 mfib_entry_index = mfib_table_entry_update (fib_index, prefix,
977 MFIB_SOURCE_API,
978 rpf_id, entry_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +0000979 }
980 else
981 {
Neale Ranns097fa662018-05-01 05:17:55 -0700982 if (is_add)
983 {
Paul Atkins8e2b1b12021-10-12 14:32:11 +0100984 mfib_entry_index = mfib_table_entry_paths_update (
985 fib_index, prefix, MFIB_SOURCE_API, entry_flags, rpaths);
Neale Ranns097fa662018-05-01 05:17:55 -0700986 }
987 else
988 {
989 mfib_table_entry_paths_remove (fib_index, prefix,
990 MFIB_SOURCE_API, rpaths);
991 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000992 }
993
Neale Ranns28c142e2018-09-07 09:37:07 -0700994 return (mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000995}
996
997static int
Neale Ranns28c142e2018-09-07 09:37:07 -0700998api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp,
999 u32 * stats_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001000{
Neale Ranns097fa662018-05-01 05:17:55 -07001001 fib_route_path_t *rpath, *rpaths = NULL;
Neale Ranns28c142e2018-09-07 09:37:07 -07001002 fib_node_index_t mfib_entry_index;
Neale Ranns990f6942020-10-20 07:20:17 +00001003 mfib_entry_flags_t eflags;
Neale Ranns097fa662018-05-01 05:17:55 -07001004 mfib_prefix_t pfx;
Neale Ranns32e1c012016-11-22 17:07:28 +00001005 u32 fib_index;
1006 int rv;
Neale Ranns097fa662018-05-01 05:17:55 -07001007 u16 ii;
Neale Ranns32e1c012016-11-22 17:07:28 +00001008
Neale Ranns097fa662018-05-01 05:17:55 -07001009 ip_mprefix_decode (&mp->route.prefix, &pfx);
Neale Ranns32e1c012016-11-22 17:07:28 +00001010
Neale Ranns097fa662018-05-01 05:17:55 -07001011 rv = mfib_api_table_id_decode (pfx.fp_proto,
1012 ntohl (mp->route.table_id), &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001013 if (0 != rv)
Neale Ranns097fa662018-05-01 05:17:55 -07001014 goto out;
Neale Ranns32e1c012016-11-22 17:07:28 +00001015
Neale Ranns097fa662018-05-01 05:17:55 -07001016 vec_validate (rpaths, mp->route.n_paths - 1);
Neale Ranns32e1c012016-11-22 17:07:28 +00001017
Neale Ranns097fa662018-05-01 05:17:55 -07001018 for (ii = 0; ii < mp->route.n_paths; ii++)
Neale Ranns32e1c012016-11-22 17:07:28 +00001019 {
Neale Ranns097fa662018-05-01 05:17:55 -07001020 rpath = &rpaths[ii];
1021
1022 rv = mfib_api_path_decode (&mp->route.paths[ii], rpath);
1023
1024 if (0 != rv)
1025 goto out;
Neale Ranns32e1c012016-11-22 17:07:28 +00001026 }
1027
Neale Ranns990f6942020-10-20 07:20:17 +00001028 eflags = mfib_api_path_entry_flags_decode (mp->route.entry_flags);
Neale Ranns28c142e2018-09-07 09:37:07 -07001029 mfib_entry_index = mroute_add_del_handler (mp->is_add,
Neale Ranns097fa662018-05-01 05:17:55 -07001030 mp->is_add,
Neale Ranns28c142e2018-09-07 09:37:07 -07001031 fib_index, &pfx,
Neale Ranns990f6942020-10-20 07:20:17 +00001032 eflags,
Neale Ranns097fa662018-05-01 05:17:55 -07001033 ntohl (mp->route.rpf_id),
1034 rpaths);
Neale Ranns28c142e2018-09-07 09:37:07 -07001035
1036 if (~0 != mfib_entry_index)
1037 *stats_index = mfib_entry_get_stats_index (mfib_entry_index);
1038
Neale Ranns097fa662018-05-01 05:17:55 -07001039out:
Neale Ranns28c142e2018-09-07 09:37:07 -07001040 return (rv);
Neale Ranns32e1c012016-11-22 17:07:28 +00001041}
1042
1043void
1044vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1045{
1046 vl_api_ip_mroute_add_del_reply_t *rmp;
Neale Ranns097fa662018-05-01 05:17:55 -07001047 u32 stats_index = ~0;
Neale Ranns32e1c012016-11-22 17:07:28 +00001048 int rv;
Neale Ranns32e1c012016-11-22 17:07:28 +00001049
Neale Ranns28c142e2018-09-07 09:37:07 -07001050 rv = api_mroute_add_del_t_handler (mp, &stats_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001051
Neale Ranns28c142e2018-09-07 09:37:07 -07001052 /* *INDENT-OFF* */
1053 REPLY_MACRO2 (VL_API_IP_MROUTE_ADD_DEL_REPLY,
1054 ({
1055 rmp->stats_index = htonl (stats_index);
1056 }));
1057 /* *INDENT-ON* */
Neale Ranns32e1c012016-11-22 17:07:28 +00001058}
1059
Dave Barachb5e8a772016-12-06 12:04:42 -05001060static void
1061send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001062 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1063 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001064{
1065 vl_api_ip_details_t *mp;
1066
1067 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001068 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001069 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -05001070
1071 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001072 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001073 mp->context = context;
1074
Florin Coras6c4dae22018-01-09 06:39:23 -08001075 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001076}
1077
1078static void
1079send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001080 vl_api_registration_t * reg,
Neale Ranns097fa662018-05-01 05:17:55 -07001081 const fib_prefix_t * pfx,
1082 u32 sw_if_index, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001083{
1084 vl_api_ip_address_details_t *mp;
1085
1086 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001087 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001088 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_ADDRESS_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -05001089
Neale Ranns097fa662018-05-01 05:17:55 -07001090 ip_prefix_encode (pfx, &mp->prefix);
Dave Barachb5e8a772016-12-06 12:04:42 -05001091 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001092 mp->sw_if_index = htonl (sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001093
Florin Coras6c4dae22018-01-09 06:39:23 -08001094 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001095}
1096
1097static void
1098vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1099{
1100 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001101 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001102 ip6_main_t *im6 = &ip6_main;
1103 ip4_main_t *im4 = &ip4_main;
1104 ip_lookup_main_t *lm6 = &im6->lookup_main;
1105 ip_lookup_main_t *lm4 = &im4->lookup_main;
1106 ip_interface_address_t *ia = 0;
1107 u32 sw_if_index = ~0;
1108 int rv __attribute__ ((unused)) = 0;
1109
1110 VALIDATE_SW_IF_INDEX (mp);
1111
1112 sw_if_index = ntohl (mp->sw_if_index);
1113
Florin Coras6c4dae22018-01-09 06:39:23 -08001114 reg = vl_api_client_index_to_registration (mp->client_index);
1115 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001116 return;
1117
Dave Barachb5e8a772016-12-06 12:04:42 -05001118 if (mp->is_ipv6)
1119 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001120 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001121 /* Do not send subnet details of the IP-interface for
1122 * unnumbered interfaces. otherwise listening clients
1123 * will be confused that the subnet is applied on more
1124 * than one interface */
1125 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001126 ({
Neale Ranns097fa662018-05-01 05:17:55 -07001127 fib_prefix_t pfx = {
1128 .fp_addr.ip6 = *(ip6_address_t *)ip_interface_address_get_address (lm6, ia),
1129 .fp_len = ia->address_length,
1130 .fp_proto = FIB_PROTOCOL_IP6,
1131 };
1132 send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001133 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001134 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001135 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001136 else
1137 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001138 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001139 foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001140 ({
Neale Ranns097fa662018-05-01 05:17:55 -07001141 fib_prefix_t pfx = {
1142 .fp_addr.ip4 = *(ip4_address_t *)ip_interface_address_get_address (lm4, ia),
1143 .fp_len = ia->address_length,
1144 .fp_proto = FIB_PROTOCOL_IP4,
1145 };
1146
1147 send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001148 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001149 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001150 }
Neale Ranns008dbe12018-09-07 09:32:36 -07001151
Dave Barachb5e8a772016-12-06 12:04:42 -05001152 BAD_SW_IF_INDEX_LABEL;
1153}
1154
1155static void
Neale Ranns9e2f9152018-05-18 02:27:10 -07001156send_ip_unnumbered_details (vpe_api_main_t * am,
1157 vl_api_registration_t * reg,
1158 u32 sw_if_index, u32 ip_sw_if_index, u32 context)
1159{
1160 vl_api_ip_unnumbered_details_t *mp;
1161
1162 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001163 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001164 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_UNNUMBERED_DETAILS);
Neale Ranns9e2f9152018-05-18 02:27:10 -07001165
1166 mp->context = context;
1167 mp->sw_if_index = htonl (sw_if_index);
1168 mp->ip_sw_if_index = htonl (ip_sw_if_index);
1169
1170 vl_api_send_msg (reg, (u8 *) mp);
1171}
1172
1173static void
1174vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
1175{
1176 vnet_main_t *vnm = vnet_get_main ();
1177 vnet_interface_main_t *im = &vnm->interface_main;
1178 int rv __attribute__ ((unused)) = 0;
1179 vpe_api_main_t *am = &vpe_api_main;
1180 vl_api_registration_t *reg;
1181 vnet_sw_interface_t *si;
1182 u32 sw_if_index;
1183
1184 sw_if_index = ntohl (mp->sw_if_index);
1185
1186 reg = vl_api_client_index_to_registration (mp->client_index);
1187 if (!reg)
1188 return;
1189
1190 if (~0 != sw_if_index)
1191 {
1192 VALIDATE_SW_IF_INDEX (mp);
1193
1194 si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
1195
Neale Rannsac3e72c2019-10-06 01:04:26 -07001196 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
Neale Ranns9e2f9152018-05-18 02:27:10 -07001197 {
1198 send_ip_unnumbered_details (am, reg,
1199 sw_if_index,
1200 si->unnumbered_sw_if_index,
1201 mp->context);
1202 }
1203 }
1204 else
1205 {
1206 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001207 pool_foreach (si, im->sw_interfaces)
1208 {
Neale Ranns9e2f9152018-05-18 02:27:10 -07001209 if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1210 {
1211 send_ip_unnumbered_details(am, reg,
1212 si->sw_if_index,
1213 si->unnumbered_sw_if_index,
1214 mp->context);
1215 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001216 }
Neale Ranns9e2f9152018-05-18 02:27:10 -07001217 /* *INDENT-ON* */
1218 }
1219
1220 BAD_SW_IF_INDEX_LABEL;
1221}
1222
1223static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001224vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1225{
1226 vpe_api_main_t *am = &vpe_api_main;
1227 vnet_main_t *vnm = vnet_get_main ();
Neale Rannscbe25aa2019-09-30 10:53:31 +00001228 //vlib_main_t *vm = vlib_get_main ();
Dave Barachb5e8a772016-12-06 12:04:42 -05001229 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001230 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001231 vnet_sw_interface_t *si, *sorted_sis;
1232 u32 sw_if_index = ~0;
1233
Florin Coras6c4dae22018-01-09 06:39:23 -08001234 reg = vl_api_client_index_to_registration (mp->client_index);
1235 if (!reg)
1236 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001237
1238 /* Gather interfaces. */
1239 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
Damjan Marion8bea5892022-04-04 22:40:45 +02001240 vec_set_len (sorted_sis, 0);
Dave Barachb5e8a772016-12-06 12:04:42 -05001241 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001242 pool_foreach (si, im->sw_interfaces)
1243 {
Dave Barachb5e8a772016-12-06 12:04:42 -05001244 vec_add1 (sorted_sis, si[0]);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001245 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001246 /* *INDENT-ON* */
1247
1248 vec_foreach (si, sorted_sis)
1249 {
1250 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1251 {
Neale Rannscbe25aa2019-09-30 10:53:31 +00001252 /* if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index)) */
1253 /* { */
1254 /* continue; */
1255 /* } */
Dave Barachb5e8a772016-12-06 12:04:42 -05001256 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001257 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001258 }
1259 }
Matthew Smith6d5f6592019-09-18 13:51:46 -05001260
1261 vec_free (sorted_sis);
Dave Barachb5e8a772016-12-06 12:04:42 -05001262}
1263
1264static void
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001265vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t *mp)
Dave Barachb5e8a772016-12-06 12:04:42 -05001266{
1267 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001268 int rv;
1269 u32 table_id;
1270 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001271
Neale Ranns227038a2017-04-21 01:07:59 -07001272 table_id = ntohl (mp->vrf_id);
1273
1274#define _(a,b) if (mp->a) flow_hash_config |= b;
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001275 foreach_flow_hash_bit_v1;
Neale Ranns227038a2017-04-21 01:07:59 -07001276#undef _
1277
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001278 rv = ip_flow_hash_set ((mp->is_ipv6 ? AF_IP6 : AF_IP4), table_id,
1279 flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001280
1281 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1282}
1283
1284static void
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001285vl_api_set_ip_flow_hash_v2_t_handler (vl_api_set_ip_flow_hash_v2_t *mp)
Dave Barachb5e8a772016-12-06 12:04:42 -05001286{
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001287 vl_api_set_ip_flow_hash_v2_reply_t *rmp;
1288 ip_address_family_t af;
Dave Barachb5e8a772016-12-06 12:04:42 -05001289 int rv;
Dave Barachb5e8a772016-12-06 12:04:42 -05001290
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001291 rv = ip_address_family_decode (mp->af, &af);
Dave Barachb5e8a772016-12-06 12:04:42 -05001292
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001293 if (!rv)
1294 rv = ip_flow_hash_set (af, htonl (mp->table_id),
1295 htonl (mp->flow_hash_config));
Dave Barachb5e8a772016-12-06 12:04:42 -05001296
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001297 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_V2_REPLY);
Dave Barachb5e8a772016-12-06 12:04:42 -05001298}
1299
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001300static void
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001301vl_api_set_ip_flow_hash_v3_t_handler (vl_api_set_ip_flow_hash_v3_t *mp)
1302{
1303 vl_api_set_ip_flow_hash_v3_reply_t *rmp;
1304 ip_address_family_t af;
1305 int rv;
1306
1307 rv = ip_address_family_decode (mp->af, &af);
1308
1309 if (!rv)
1310 rv = ip_flow_hash_set (af, htonl (mp->table_id),
1311 htonl (mp->flow_hash_config));
1312
1313 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_V3_REPLY);
1314}
1315
1316static void
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001317vl_api_set_ip_flow_hash_router_id_t_handler (
1318 vl_api_set_ip_flow_hash_router_id_t *mp)
1319{
1320 vl_api_set_ip_flow_hash_router_id_reply_t *rmp;
1321 int rv = 0;
1322
1323 ip_flow_hash_router_id_set (ntohl (mp->router_id));
1324
1325 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_ROUTER_ID_REPLY);
1326}
1327
Neale Ranns32e1c012016-11-22 17:07:28 +00001328void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001329vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001330 u32 context, const mfib_signal_t * mfs)
1331{
1332 vl_api_mfib_signal_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -08001333 const mfib_prefix_t *prefix;
Neale Ranns32e1c012016-11-22 17:07:28 +00001334 mfib_table_t *mfib;
1335 mfib_itf_t *mfi;
1336
1337 mp = vl_msg_api_alloc (sizeof (*mp));
1338
Dave Barachb7b92992018-10-17 10:38:51 -04001339 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001340 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_MFIB_SIGNAL_DETAILS);
Neale Ranns32e1c012016-11-22 17:07:28 +00001341 mp->context = context;
1342
1343 mfi = mfib_itf_get (mfs->mfs_itf);
Neale Ranns9e829a82018-12-17 05:50:32 -08001344 prefix = mfib_entry_get_prefix (mfs->mfs_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +00001345 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
Neale Ranns9e829a82018-12-17 05:50:32 -08001346 prefix->fp_proto);
Neale Ranns32e1c012016-11-22 17:07:28 +00001347 mp->table_id = ntohl (mfib->mft_table_id);
1348 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1349
Neale Ranns097fa662018-05-01 05:17:55 -07001350 ip_mprefix_encode (prefix, &mp->prefix);
Neale Ranns32e1c012016-11-22 17:07:28 +00001351
1352 if (0 != mfs->mfs_buffer_len)
1353 {
1354 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1355
1356 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1357 }
1358 else
1359 {
1360 mp->ip_packet_len = 0;
1361 }
1362
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001363 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001364}
1365
1366static void
1367vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1368{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001369 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001370
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001371 reg = vl_api_client_index_to_registration (mp->client_index);
1372 if (!reg)
1373 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001374
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001375 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001376 ;
1377}
Dave Barachb5e8a772016-12-06 12:04:42 -05001378
Florin Coras595992c2017-11-06 17:17:08 -08001379static void
1380 vl_api_ip_container_proxy_add_del_t_handler
1381 (vl_api_ip_container_proxy_add_del_t * mp)
1382{
1383 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1384 vnet_ip_container_proxy_args_t args;
1385 int rv = 0;
1386 clib_error_t *error;
1387
Dave Barachb7b92992018-10-17 10:38:51 -04001388 clib_memset (&args, 0, sizeof (args));
Neale Ranns37029302018-08-10 05:30:06 -07001389
1390 ip_prefix_decode (&mp->pfx, &args.prefix);
1391
Florin Coras595992c2017-11-06 17:17:08 -08001392 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1393 args.is_add = mp->is_add;
1394 if ((error = vnet_ip_container_proxy_add_del (&args)))
1395 {
1396 rv = clib_error_get_code (error);
1397 clib_error_report (error);
1398 }
1399
1400 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1401}
1402
Neale Ranns8f5fef22020-12-21 08:29:34 +00001403typedef struct ip_walk_ctx_t_
Matus Fabian75b9f452018-10-02 23:27:21 -07001404{
1405 vl_api_registration_t *reg;
1406 u32 context;
Neale Ranns8f5fef22020-12-21 08:29:34 +00001407} ip_walk_ctx_t;
Matus Fabian75b9f452018-10-02 23:27:21 -07001408
1409static int
1410ip_container_proxy_send_details (const fib_prefix_t * pfx, u32 sw_if_index,
1411 void *args)
1412{
1413 vl_api_ip_container_proxy_details_t *mp;
Neale Ranns8f5fef22020-12-21 08:29:34 +00001414 ip_walk_ctx_t *ctx = args;
Matus Fabian75b9f452018-10-02 23:27:21 -07001415
1416 mp = vl_msg_api_alloc (sizeof (*mp));
1417 if (!mp)
1418 return 1;
1419
Dave Barachb7b92992018-10-17 10:38:51 -04001420 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001421 mp->_vl_msg_id =
1422 ntohs (REPLY_MSG_ID_BASE + VL_API_IP_CONTAINER_PROXY_DETAILS);
Matus Fabian75b9f452018-10-02 23:27:21 -07001423 mp->context = ctx->context;
1424
1425 mp->sw_if_index = ntohl (sw_if_index);
1426 ip_prefix_encode (pfx, &mp->prefix);
1427
1428 vl_api_send_msg (ctx->reg, (u8 *) mp);
1429
1430 return 1;
1431}
1432
1433static void
1434vl_api_ip_container_proxy_dump_t_handler (vl_api_ip_container_proxy_dump_t *
1435 mp)
1436{
1437 vl_api_registration_t *reg;
1438
1439 reg = vl_api_client_index_to_registration (mp->client_index);
1440 if (!reg)
1441 return;
1442
Neale Ranns8f5fef22020-12-21 08:29:34 +00001443 ip_walk_ctx_t ctx = {
Matus Fabian75b9f452018-10-02 23:27:21 -07001444 .context = mp->context,
1445 .reg = reg,
1446 };
1447
1448 ip_container_proxy_walk (ip_container_proxy_send_details, &ctx);
1449}
1450
Neale Rannsb8d44812017-11-10 06:53:54 -08001451static void
1452vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1453{
1454 int rv = 0;
1455 vl_api_ioam_enable_reply_t *rmp;
1456 clib_error_t *error;
1457
1458 /* Ignoring the profile id as currently a single profile
1459 * is supported */
1460 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1461 mp->seqno, mp->analyse);
1462 if (error)
1463 {
1464 clib_error_report (error);
1465 rv = clib_error_get_code (error);
1466 }
1467
1468 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1469}
1470
1471static void
1472vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1473{
1474 int rv = 0;
1475 vl_api_ioam_disable_reply_t *rmp;
1476 clib_error_t *error;
1477
1478 error = clear_ioam_rewrite_fn ();
1479 if (error)
1480 {
1481 clib_error_report (error);
1482 rv = clib_error_get_code (error);
1483 }
1484
1485 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1486}
1487
1488static void
1489 vl_api_ip_source_and_port_range_check_add_del_t_handler
1490 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1491{
1492 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1493 int rv = 0;
1494
Neale Rannsb8d44812017-11-10 06:53:54 -08001495 u8 is_add = mp->is_add;
Neale Ranns37029302018-08-10 05:30:06 -07001496 fib_prefix_t pfx;
Neale Rannsb8d44812017-11-10 06:53:54 -08001497 u16 *low_ports = 0;
1498 u16 *high_ports = 0;
1499 u32 vrf_id;
1500 u16 tmp_low, tmp_high;
1501 u8 num_ranges;
1502 int i;
1503
Neale Ranns37029302018-08-10 05:30:06 -07001504 ip_prefix_decode (&mp->prefix, &pfx);
1505
Neale Rannsb8d44812017-11-10 06:53:54 -08001506 // Validate port range
1507 num_ranges = mp->number_of_ranges;
1508 if (num_ranges > 32)
1509 { // This is size of array in VPE.API
1510 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1511 goto reply;
1512 }
1513
1514 vec_reset_length (low_ports);
1515 vec_reset_length (high_ports);
1516
1517 for (i = 0; i < num_ranges; i++)
1518 {
1519 tmp_low = mp->low_ports[i];
1520 tmp_high = mp->high_ports[i];
1521 // If tmp_low <= tmp_high then only need to check tmp_low = 0
1522 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1523 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1524 {
1525 rv = VNET_API_ERROR_INVALID_VALUE;
1526 goto reply;
1527 }
1528 vec_add1 (low_ports, tmp_low);
1529 vec_add1 (high_ports, tmp_high + 1);
1530 }
1531
Neale Rannsb8d44812017-11-10 06:53:54 -08001532 vrf_id = ntohl (mp->vrf_id);
1533
1534 if (vrf_id < 1)
1535 {
1536 rv = VNET_API_ERROR_INVALID_VALUE;
1537 goto reply;
1538 }
1539
1540
Neale Ranns37029302018-08-10 05:30:06 -07001541 if (FIB_PROTOCOL_IP6 == pfx.fp_proto)
Neale Rannsb8d44812017-11-10 06:53:54 -08001542 {
Neale Ranns37029302018-08-10 05:30:06 -07001543 rv = ip6_source_and_port_range_check_add_del (&pfx.fp_addr.ip6,
1544 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08001545 vrf_id,
1546 low_ports,
1547 high_ports, is_add);
1548 }
1549 else
1550 {
Neale Ranns37029302018-08-10 05:30:06 -07001551 rv = ip4_source_and_port_range_check_add_del (&pfx.fp_addr.ip4,
1552 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08001553 vrf_id,
1554 low_ports,
1555 high_ports, is_add);
1556 }
1557
1558reply:
1559 vec_free (low_ports);
1560 vec_free (high_ports);
1561 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
1562}
1563
1564static void
1565 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
1566 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
1567{
1568 vlib_main_t *vm = vlib_get_main ();
1569 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
1570 ip4_main_t *im = &ip4_main;
1571 int rv;
1572 u32 sw_if_index;
1573 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1574 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1575 uword *p = 0;
1576 int i;
1577
1578 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
1579 ntohl (mp->tcp_out_vrf_id);
1580 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
1581 ntohl (mp->udp_out_vrf_id);
1582 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
1583 ntohl (mp->tcp_in_vrf_id);
1584 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
1585 ntohl (mp->udp_in_vrf_id);
1586
1587
1588 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
1589 {
1590 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
1591 {
1592 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
1593
1594 if (p == 0)
1595 {
1596 rv = VNET_API_ERROR_INVALID_VALUE;
1597 goto reply;
1598 }
1599
1600 fib_index[i] = p[0];
1601 }
1602 else
1603 fib_index[i] = ~0;
1604 }
1605 sw_if_index = ntohl (mp->sw_if_index);
1606
1607 VALIDATE_SW_IF_INDEX (mp);
1608
1609 rv =
1610 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
1611 mp->is_add);
1612
1613 BAD_SW_IF_INDEX_LABEL;
1614reply:
1615
1616 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
1617}
1618
Neale Rannscbe25aa2019-09-30 10:53:31 +00001619static void
1620 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1621 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1622{
1623 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1624 ip6_address_t ip;
1625 int rv;
1626
1627 VALIDATE_SW_IF_INDEX (mp);
1628
1629 ip6_address_decode (mp->ip, &ip);
1630
Neale Rannsec40a7d2020-04-23 07:36:12 +00001631 rv = ip6_link_set_local_address (ntohl (mp->sw_if_index), &ip);
Neale Rannscbe25aa2019-09-30 10:53:31 +00001632
1633 BAD_SW_IF_INDEX_LABEL;
1634 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1635}
1636
Neale Ranns9db6ada2019-11-08 12:42:31 +00001637static void
Benoît Ganne58a19152021-01-18 19:24:34 +01001638vl_api_sw_interface_ip6_get_link_local_address_t_handler (
1639 vl_api_sw_interface_ip6_get_link_local_address_t *mp)
1640{
1641 vl_api_sw_interface_ip6_get_link_local_address_reply_t *rmp;
1642 const ip6_address_t *ip = NULL;
1643 int rv = 0;
1644
1645 VALIDATE_SW_IF_INDEX (mp);
1646
1647 ip = ip6_get_link_local_address (ntohl (mp->sw_if_index));
1648 if (NULL == ip)
1649 rv = VNET_API_ERROR_IP6_NOT_ENABLED;
1650
1651 BAD_SW_IF_INDEX_LABEL;
1652 /* clang-format off */
1653 REPLY_MACRO2 (VL_API_SW_INTERFACE_IP6_GET_LINK_LOCAL_ADDRESS_REPLY,
1654 ({
1655 if (!rv)
1656 ip6_address_encode (ip, rmp->ip);
1657 }))
1658 /* clang-format on */
1659}
1660
1661static void
Neale Ranns9db6ada2019-11-08 12:42:31 +00001662vl_api_ip_table_replace_begin_t_handler (vl_api_ip_table_replace_begin_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -08001663{
Neale Ranns9db6ada2019-11-08 12:42:31 +00001664 vl_api_ip_table_replace_begin_reply_t *rmp;
1665 fib_protocol_t fproto;
1666 u32 fib_index;
1667 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001668
Neale Ranns9db6ada2019-11-08 12:42:31 +00001669 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1670 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
Neale Rannsb8d44812017-11-10 06:53:54 -08001671
Neale Ranns9db6ada2019-11-08 12:42:31 +00001672 if (INDEX_INVALID == fib_index)
1673 rv = VNET_API_ERROR_NO_SUCH_FIB;
1674 else
1675 {
1676 fib_table_mark (fib_index, fproto, FIB_SOURCE_API);
1677 mfib_table_mark (mfib_table_find (fproto, ntohl (mp->table.table_id)),
1678 fproto, MFIB_SOURCE_API);
Neale Rannsb8d44812017-11-10 06:53:54 -08001679 }
Neale Ranns9db6ada2019-11-08 12:42:31 +00001680 REPLY_MACRO (VL_API_IP_TABLE_REPLACE_BEGIN_REPLY);
Neale Rannsb8d44812017-11-10 06:53:54 -08001681}
1682
1683static void
Neale Ranns9db6ada2019-11-08 12:42:31 +00001684vl_api_ip_table_replace_end_t_handler (vl_api_ip_table_replace_end_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -08001685{
Neale Ranns9db6ada2019-11-08 12:42:31 +00001686 vl_api_ip_table_replace_end_reply_t *rmp;
1687 fib_protocol_t fproto;
1688 u32 fib_index;
1689 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001690
Neale Ranns9db6ada2019-11-08 12:42:31 +00001691 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1692 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1693
1694 if (INDEX_INVALID == fib_index)
1695 rv = VNET_API_ERROR_NO_SUCH_FIB;
Neale Rannsb8d44812017-11-10 06:53:54 -08001696 else
Neale Ranns9db6ada2019-11-08 12:42:31 +00001697 {
1698 fib_table_sweep (fib_index, fproto, FIB_SOURCE_API);
1699 mfib_table_sweep (mfib_table_find
1700 (fproto, ntohl (mp->table.table_id)), fproto,
1701 MFIB_SOURCE_API);
1702 }
1703 REPLY_MACRO (VL_API_IP_TABLE_REPLACE_END_REPLY);
1704}
Neale Rannsb8d44812017-11-10 06:53:54 -08001705
Neale Ranns9db6ada2019-11-08 12:42:31 +00001706static void
1707vl_api_ip_table_flush_t_handler (vl_api_ip_table_flush_t * mp)
1708{
1709 vl_api_ip_table_flush_reply_t *rmp;
1710 fib_protocol_t fproto;
1711 u32 fib_index;
1712 int rv = 0;
1713
1714 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1715 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1716
1717 if (INDEX_INVALID == fib_index)
1718 rv = VNET_API_ERROR_NO_SUCH_FIB;
1719 else
1720 {
1721 vnet_main_t *vnm = vnet_get_main ();
1722 vnet_interface_main_t *im = &vnm->interface_main;
1723 vnet_sw_interface_t *si;
1724
1725 /* Shut down interfaces in this FIB / clean out intfc routes */
1726 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001727 pool_foreach (si, im->sw_interfaces)
1728 {
Neale Ranns9db6ada2019-11-08 12:42:31 +00001729 if (fib_index == fib_table_get_index_for_sw_if_index (fproto,
1730 si->sw_if_index))
1731 {
1732 u32 flags = si->flags;
1733 flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP;
1734 vnet_sw_interface_set_flags (vnm, si->sw_if_index, flags);
1735 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001736 }
Neale Ranns9db6ada2019-11-08 12:42:31 +00001737 /* *INDENT-ON* */
1738
1739 fib_table_flush (fib_index, fproto, FIB_SOURCE_API);
1740 mfib_table_flush (mfib_table_find (fproto, ntohl (mp->table.table_id)),
1741 fproto, MFIB_SOURCE_API);
1742 }
1743
1744 REPLY_MACRO (VL_API_IP_TABLE_FLUSH_REPLY);
Neale Rannsb8d44812017-11-10 06:53:54 -08001745}
1746
Klement Sekera75e7d132017-09-20 08:26:30 +02001747void
1748vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
1749{
1750 vl_api_ip_reassembly_set_reply_t *rmp;
1751 int rv = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001752 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera75e7d132017-09-20 08:26:30 +02001753 {
Klement Sekerade34c352019-06-25 11:19:22 +00001754 case IP_REASS_TYPE_FULL:
1755 if (mp->is_ip6)
1756 {
1757 rv = ip6_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1758 clib_net_to_host_u32
1759 (mp->max_reassemblies),
1760 clib_net_to_host_u32
1761 (mp->max_reassembly_length),
1762 clib_net_to_host_u32
1763 (mp->expire_walk_interval_ms));
1764 }
1765 else
1766 {
1767 rv = ip4_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1768 clib_net_to_host_u32
1769 (mp->max_reassemblies),
1770 clib_net_to_host_u32
1771 (mp->max_reassembly_length),
1772 clib_net_to_host_u32
1773 (mp->expire_walk_interval_ms));
1774 }
1775 break;
1776 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1777 if (mp->is_ip6)
1778 {
1779 rv =
1780 ip6_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1781 clib_net_to_host_u32 (mp->max_reassemblies),
1782 clib_net_to_host_u32
1783 (mp->max_reassembly_length),
1784 clib_net_to_host_u32
1785 (mp->expire_walk_interval_ms));
1786 }
1787 else
1788 {
1789 rv = ip4_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1790 clib_net_to_host_u32 (mp->max_reassemblies),
1791 clib_net_to_host_u32
1792 (mp->max_reassembly_length),
1793 clib_net_to_host_u32
1794 (mp->expire_walk_interval_ms));
1795 }
1796 break;
Klement Sekera75e7d132017-09-20 08:26:30 +02001797 }
1798
1799 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
1800}
1801
1802void
1803vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
1804{
Ole Troan2e1c8962019-04-10 09:44:23 +02001805 vl_api_registration_t *rp;
Klement Sekera75e7d132017-09-20 08:26:30 +02001806
Ole Troan2e1c8962019-04-10 09:44:23 +02001807 rp = vl_api_client_index_to_registration (mp->client_index);
1808 if (rp == 0)
Klement Sekera75e7d132017-09-20 08:26:30 +02001809 return;
1810
1811 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001812 clib_memset (rmp, 0, sizeof (*rmp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001813 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_REASSEMBLY_GET_REPLY);
Klement Sekera75e7d132017-09-20 08:26:30 +02001814 rmp->context = mp->context;
1815 rmp->retval = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001816 u32 timeout_ms;
1817 u32 max_reassemblies;
1818 u32 max_reassembly_length;
1819 u32 expire_walk_interval_ms;
1820 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera75e7d132017-09-20 08:26:30 +02001821 {
Klement Sekerade34c352019-06-25 11:19:22 +00001822 case IP_REASS_TYPE_FULL:
1823 if (mp->is_ip6)
1824 {
1825 rmp->is_ip6 = 1;
1826 ip6_full_reass_get (&timeout_ms, &max_reassemblies,
1827 &max_reassembly_length,
1828 &expire_walk_interval_ms);
1829 }
1830 else
1831 {
1832 rmp->is_ip6 = 0;
1833 ip4_full_reass_get (&timeout_ms, &max_reassemblies,
1834 &max_reassembly_length,
1835 &expire_walk_interval_ms);
1836 }
1837 break;
1838 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1839 if (mp->is_ip6)
1840 {
1841 rmp->is_ip6 = 1;
1842 ip6_sv_reass_get (&timeout_ms, &max_reassemblies,
1843 &max_reassembly_length, &expire_walk_interval_ms);
1844 }
1845 else
1846 {
1847 rmp->is_ip6 = 0;
1848 ip4_sv_reass_get (&timeout_ms, &max_reassemblies,
1849 &max_reassembly_length, &expire_walk_interval_ms);
1850 }
1851 break;
Klement Sekera75e7d132017-09-20 08:26:30 +02001852 }
Matthew Smithb3174df2019-12-03 12:41:53 -06001853 rmp->timeout_ms = clib_host_to_net_u32 (timeout_ms);
1854 rmp->max_reassemblies = clib_host_to_net_u32 (max_reassemblies);
1855 rmp->max_reassembly_length = clib_host_to_net_u32 (max_reassembly_length);
Klement Sekera75e7d132017-09-20 08:26:30 +02001856 rmp->expire_walk_interval_ms =
Matthew Smithb3174df2019-12-03 12:41:53 -06001857 clib_host_to_net_u32 (expire_walk_interval_ms);
Ole Troan2e1c8962019-04-10 09:44:23 +02001858 vl_api_send_msg (rp, (u8 *) rmp);
Klement Sekera75e7d132017-09-20 08:26:30 +02001859}
1860
Klement Sekera4c533132018-02-22 11:41:12 +01001861void
1862 vl_api_ip_reassembly_enable_disable_t_handler
1863 (vl_api_ip_reassembly_enable_disable_t * mp)
1864{
1865 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
1866 int rv = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001867 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera4c533132018-02-22 11:41:12 +01001868 {
Klement Sekerade34c352019-06-25 11:19:22 +00001869 case IP_REASS_TYPE_FULL:
Klement Sekera896c8962019-06-24 11:52:49 +00001870 rv =
Klement Sekerade34c352019-06-25 11:19:22 +00001871 ip4_full_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1872 mp->enable_ip4);
1873 if (0 == rv)
1874 rv =
1875 ip6_full_reass_enable_disable (clib_net_to_host_u32
1876 (mp->sw_if_index), mp->enable_ip6);
1877 break;
1878 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1879 rv =
1880 ip4_sv_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1881 mp->enable_ip4);
1882 if (0 == rv)
1883 {
1884 rv =
1885 ip6_sv_reass_enable_disable (clib_net_to_host_u32
1886 (mp->sw_if_index), mp->enable_ip6);
1887 }
1888 break;
Klement Sekera4c533132018-02-22 11:41:12 +01001889 }
1890
Choree7f61d12018-11-28 10:27:58 +03301891 REPLY_MACRO (VL_API_IP_REASSEMBLY_ENABLE_DISABLE_REPLY);
Klement Sekera4c533132018-02-22 11:41:12 +01001892}
1893
Klement Sekera01c1fa42021-12-14 18:25:11 +00001894void
1895vl_api_ip_local_reass_enable_disable_t_handler (
1896 vl_api_ip_local_reass_enable_disable_t *mp)
1897{
1898 vl_api_ip_local_reass_enable_disable_reply_t *rmp;
1899 int rv = 0;
1900
1901 ip4_local_full_reass_enable_disable (mp->enable_ip4);
1902 ip6_local_full_reass_enable_disable (mp->enable_ip6);
1903
1904 REPLY_MACRO (VL_API_IP_LOCAL_REASS_ENABLE_DISABLE_REPLY);
1905}
1906
1907void
1908vl_api_ip_local_reass_get_t_handler (vl_api_ip_local_reass_get_t *mp)
1909{
1910 vl_api_ip_local_reass_get_reply_t *rmp;
1911 int rv = 0;
1912 REPLY_MACRO2 (VL_API_IP_LOCAL_REASS_GET, {
1913 rmp->ip4_is_enabled = ip4_local_full_reass_enabled ();
1914 rmp->ip6_is_enabled = ip6_local_full_reass_enabled ();
1915 });
1916}
1917
Neale Ranns92207752019-06-03 13:21:40 +00001918static walk_rc_t
1919send_ip_punt_redirect_details (u32 rx_sw_if_index,
1920 const ip_punt_redirect_rx_t * ipr, void *arg)
1921{
Pavel Kotucek609e1212018-11-27 09:59:44 +01001922 vl_api_ip_punt_redirect_details_t *mp;
Neale Ranns097fa662018-05-01 05:17:55 -07001923 fib_path_encode_ctx_t path_ctx = {
1924 .rpaths = NULL,
1925 };
Neale Ranns8f5fef22020-12-21 08:29:34 +00001926 ip_walk_ctx_t *ctx = arg;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001927
1928 mp = vl_msg_api_alloc (sizeof (*mp));
1929 if (!mp)
Neale Ranns92207752019-06-03 13:21:40 +00001930 return (WALK_STOP);;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001931
1932 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001933 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_PUNT_REDIRECT_DETAILS);
Neale Ranns92207752019-06-03 13:21:40 +00001934 mp->context = ctx->context;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001935
Neale Ranns097fa662018-05-01 05:17:55 -07001936 fib_path_list_walk_w_ext (ipr->pl, NULL, fib_path_encode, &path_ctx);
Neale Ranns92207752019-06-03 13:21:40 +00001937
1938 mp->punt.rx_sw_if_index = htonl (rx_sw_if_index);
Neale Ranns097fa662018-05-01 05:17:55 -07001939 mp->punt.tx_sw_if_index = htonl (path_ctx.rpaths[0].frp_sw_if_index);
Neale Ranns92207752019-06-03 13:21:40 +00001940
Neale Ranns097fa662018-05-01 05:17:55 -07001941 ip_address_encode (&path_ctx.rpaths[0].frp_addr,
Neale Ranns92207752019-06-03 13:21:40 +00001942 fib_proto_to_ip46 (ipr->fproto), &mp->punt.nh);
1943
1944 vl_api_send_msg (ctx->reg, (u8 *) mp);
1945
Neale Ranns097fa662018-05-01 05:17:55 -07001946 vec_free (path_ctx.rpaths);
Neale Ranns92207752019-06-03 13:21:40 +00001947
1948 return (WALK_CONTINUE);
Pavel Kotucek609e1212018-11-27 09:59:44 +01001949}
1950
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02001951static walk_rc_t
1952send_ip_punt_redirect_v2_details (u32 rx_sw_if_index,
1953 const ip_punt_redirect_rx_t *ipr, void *arg)
1954{
1955 vl_api_ip_punt_redirect_v2_details_t *mp;
1956 fib_path_encode_ctx_t path_ctx = {
1957 .rpaths = NULL,
1958 };
1959 fib_route_path_t *rpath;
1960 ip_walk_ctx_t *ctx = arg;
1961 vl_api_fib_path_t *fp;
1962 int n_paths;
1963
1964 fib_path_list_walk_w_ext (ipr->pl, NULL, fib_path_encode, &path_ctx);
1965
1966 n_paths = vec_len (path_ctx.rpaths);
1967 mp = vl_msg_api_alloc (sizeof (*mp) + n_paths * sizeof (*fp));
1968 if (!mp)
1969 return (WALK_STOP);
1970
1971 clib_memset (mp, 0, sizeof (*mp));
1972 mp->_vl_msg_id =
1973 ntohs (REPLY_MSG_ID_BASE + VL_API_IP_PUNT_REDIRECT_V2_DETAILS);
1974 mp->context = ctx->context;
1975 mp->punt.rx_sw_if_index = htonl (rx_sw_if_index);
1976 mp->punt.n_paths = htonl (n_paths);
1977 fp = mp->punt.paths;
1978 vec_foreach (rpath, path_ctx.rpaths)
1979 {
1980 fib_api_path_encode (rpath, fp);
1981 fp++;
1982 }
1983 mp->punt.af = (ipr->fproto == FIB_PROTOCOL_IP6) ? ADDRESS_IP6 : ADDRESS_IP4;
1984
1985 vl_api_send_msg (ctx->reg, (u8 *) mp);
1986
1987 vec_free (path_ctx.rpaths);
1988
1989 return (WALK_CONTINUE);
1990}
1991
1992static void
1993vl_api_ip_punt_redirect_dump_common (ip_walk_ctx_t *ctx, fib_protocol_t fproto,
1994 u32 rx_sw_if_index,
1995 ip_punt_redirect_walk_cb_t cb)
1996{
1997
1998 if ((u32) ~0 != rx_sw_if_index)
1999 {
2000 index_t pri;
2001 pri = ip_punt_redirect_find (fproto, rx_sw_if_index);
2002
2003 if (INDEX_INVALID == pri)
2004 return;
2005
2006 cb (rx_sw_if_index, ip_punt_redirect_get (pri), ctx);
2007 }
2008 else
2009 ip_punt_redirect_walk (fproto, cb, ctx);
2010}
2011
Pavel Kotucek609e1212018-11-27 09:59:44 +01002012static void
2013vl_api_ip_punt_redirect_dump_t_handler (vl_api_ip_punt_redirect_dump_t * mp)
2014{
2015 vl_api_registration_t *reg;
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02002016 fib_protocol_t fproto;
Pavel Kotucek609e1212018-11-27 09:59:44 +01002017
Pavel Kotucek609e1212018-11-27 09:59:44 +01002018 reg = vl_api_client_index_to_registration (mp->client_index);
2019 if (!reg)
2020 return;
2021
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02002022 fproto = (mp->is_ipv6 == 1) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
Pavel Kotucek609e1212018-11-27 09:59:44 +01002023
Neale Ranns8f5fef22020-12-21 08:29:34 +00002024 ip_walk_ctx_t ctx = {
Neale Ranns92207752019-06-03 13:21:40 +00002025 .reg = reg,
2026 .context = mp->context,
2027 };
2028
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02002029 vl_api_ip_punt_redirect_dump_common (&ctx, fproto, ntohl (mp->sw_if_index),
2030 send_ip_punt_redirect_details);
2031}
Neale Ranns92207752019-06-03 13:21:40 +00002032
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02002033static void
2034vl_api_ip_punt_redirect_v2_dump_t_handler (
2035 vl_api_ip_punt_redirect_v2_dump_t *mp)
2036{
2037 vl_api_registration_t *reg;
2038 ip_address_family_t af;
2039 fib_protocol_t fproto;
2040 int rv = 0;
Neale Ranns92207752019-06-03 13:21:40 +00002041
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02002042 reg = vl_api_client_index_to_registration (mp->client_index);
2043 if (!reg)
2044 return;
Neale Ranns92207752019-06-03 13:21:40 +00002045
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02002046 rv = ip_address_family_decode (mp->af, &af);
2047 if (rv != 0)
2048 return;
2049
2050 fproto = (af == AF_IP6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
2051
2052 ip_walk_ctx_t ctx = {
2053 .reg = reg,
2054 .context = mp->context,
2055 };
2056
2057 vl_api_ip_punt_redirect_dump_common (&ctx, fproto, ntohl (mp->sw_if_index),
2058 send_ip_punt_redirect_v2_details);
Pavel Kotucek609e1212018-11-27 09:59:44 +01002059}
2060
Neale Ranns8f5fef22020-12-21 08:29:34 +00002061void
2062vl_api_ip_path_mtu_update_t_handler (vl_api_ip_path_mtu_update_t *mp)
2063{
2064 vl_api_ip_path_mtu_update_reply_t *rmp;
2065 ip_address_t nh;
2066 int rv = 0;
2067
2068 ip_address_decode2 (&mp->pmtu.nh, &nh);
2069
2070 rv = ip_path_mtu_update (&nh, ntohl (mp->pmtu.table_id),
2071 ntohs (mp->pmtu.path_mtu));
2072
2073 REPLY_MACRO (VL_API_IP_PATH_MTU_UPDATE_REPLY);
2074}
2075
2076void
2077vl_api_ip_path_mtu_replace_begin_t_handler (
2078 vl_api_ip_path_mtu_replace_begin_t *mp)
2079{
2080 vl_api_ip_path_mtu_replace_begin_reply_t *rmp;
2081 int rv;
2082
2083 rv = ip_path_mtu_replace_begin ();
2084
2085 REPLY_MACRO (VL_API_IP_PATH_MTU_REPLACE_BEGIN_REPLY);
2086}
2087
2088void
2089vl_api_ip_path_mtu_replace_end_t_handler (vl_api_ip_path_mtu_replace_end_t *mp)
2090{
2091 vl_api_ip_path_mtu_replace_end_reply_t *rmp;
2092 int rv;
2093
2094 rv = ip_path_mtu_replace_end ();
2095
2096 REPLY_MACRO (VL_API_IP_PATH_MTU_REPLACE_END_REPLY);
2097}
2098
2099static void
2100send_ip_path_mtu_details (index_t ipti, vl_api_registration_t *rp, u32 context)
2101{
2102 vl_api_ip_path_mtu_details_t *rmp;
2103 ip_address_t ip;
2104 ip_pmtu_t *ipt;
2105
2106 ipt = ip_path_mtu_get (ipti);
2107
2108 REPLY_MACRO_DETAILS4 (VL_API_IP_PATH_MTU_DETAILS, rp, context, ({
2109 ip_pmtu_get_ip (ipt, &ip);
2110 ip_address_encode2 (&ip, &rmp->pmtu.nh);
2111 rmp->pmtu.table_id =
2112 htonl (ip_pmtu_get_table_id (ipt));
2113 rmp->pmtu.path_mtu = htons (ipt->ipt_cfg_pmtu);
2114 }));
2115}
2116
2117static void
2118vl_api_ip_path_mtu_get_t_handler (vl_api_ip_path_mtu_get_t *mp)
2119{
2120 vl_api_ip_path_mtu_get_reply_t *rmp;
2121 i32 rv = 0;
2122
2123 REPLY_AND_DETAILS_MACRO (
2124 VL_API_IP_PATH_MTU_GET_REPLY, ip_pmtu_pool,
2125 ({ send_ip_path_mtu_details (cursor, rp, mp->context); }));
2126}
2127
Filip Tehlar5ff59a12021-06-23 14:38:38 +00002128#include <vnet/ip/ip.api.c>
Dave Barachb5e8a772016-12-06 12:04:42 -05002129
2130static clib_error_t *
2131ip_api_hookup (vlib_main_t * vm)
2132{
Dave Barach39d69112019-11-27 11:42:13 -05002133 api_main_t *am = vlibapi_get_main ();
Dave Barachb5e8a772016-12-06 12:04:42 -05002134
Dave Barachb5e8a772016-12-06 12:04:42 -05002135 /*
2136 * Set up the (msg_name, crc, message-id) table
2137 */
Filip Tehlar5ff59a12021-06-23 14:38:38 +00002138 REPLY_MSG_ID_BASE = setup_message_id_table ();
Dave Barachb5e8a772016-12-06 12:04:42 -05002139
Vladislav Grishenkoe7c57c42023-03-19 02:57:01 +05002140 /*
2141 * Mark the route add/del API as MP safe
2142 */
2143 vl_api_set_msg_thread_safe (am, REPLY_MSG_ID_BASE + VL_API_IP_ROUTE_ADD_DEL,
2144 1);
2145 vl_api_set_msg_thread_safe (
2146 am, REPLY_MSG_ID_BASE + VL_API_IP_ROUTE_ADD_DEL_REPLY, 1);
2147 vl_api_set_msg_thread_safe (
2148 am, REPLY_MSG_ID_BASE + VL_API_IP_ROUTE_ADD_DEL_V2, 1);
2149 vl_api_set_msg_thread_safe (
2150 am, REPLY_MSG_ID_BASE + VL_API_IP_ROUTE_ADD_DEL_V2_REPLY, 1);
2151
Dave Barachb5e8a772016-12-06 12:04:42 -05002152 return 0;
2153}
2154
2155VLIB_API_INIT_FUNCTION (ip_api_hookup);
2156
2157/*
2158 * fd.io coding-style-patch-verification: ON
2159 *
2160 * Local Variables:
2161 * eval: (c-set-style "gnu")
2162 * End:
2163 */