blob: 5ced88fec2e7a81740a5c625c2d8d4db950e2429 [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
Damjan Marionb2c31b62020-12-13 21:47:40 +0100109 pool_foreach (fib_table, ip4_main.fibs)
110 {
Neale Ranns097fa662018-05-01 05:17:55 -0700111 send_ip_table_details(am, reg, mp->context, fib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100112 }
113 pool_foreach (fib_table, ip6_main.fibs)
114 {
Neale Ranns097fa662018-05-01 05:17:55 -0700115 /* don't send link locals */
116 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
117 continue;
118 send_ip_table_details(am, reg, mp->context, fib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100119 }
Neale Ranns097fa662018-05-01 05:17:55 -0700120}
121
Neale Rannsa3af3372017-03-28 03:49:52 -0700122typedef struct vl_api_ip_fib_dump_walk_ctx_t_
123{
124 fib_node_index_t *feis;
125} vl_api_ip_fib_dump_walk_ctx_t;
126
Neale Ranns89541992017-04-06 04:41:02 -0700127static fib_table_walk_rc_t
Neale Rannsa3af3372017-03-28 03:49:52 -0700128vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
129{
130 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
131
132 vec_add1 (ctx->feis, fei);
133
Neale Ranns89541992017-04-06 04:41:02 -0700134 return (FIB_TABLE_WALK_CONTINUE);
Neale Rannsa3af3372017-03-28 03:49:52 -0700135}
136
Dave Barachb5e8a772016-12-06 12:04:42 -0500137static void
Neale Ranns097fa662018-05-01 05:17:55 -0700138send_ip_route_details (vpe_api_main_t * am,
139 vl_api_registration_t * reg,
140 u32 context, fib_node_index_t fib_entry_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500141{
Neale Ranns097fa662018-05-01 05:17:55 -0700142 fib_route_path_t *rpaths, *rpath;
143 vl_api_ip_route_details_t *mp;
Neale Rannsc5d43172018-07-30 08:04:40 -0700144 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500145 vl_api_fib_path_t *fp;
146 int path_count;
147
Neale Ranns097fa662018-05-01 05:17:55 -0700148 rpaths = NULL;
149 pfx = fib_entry_get_prefix (fib_entry_index);
150 rpaths = fib_entry_encode (fib_entry_index);
151
152 path_count = vec_len (rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500153 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
154 if (!mp)
155 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400156 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +0000157 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_ROUTE_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -0500158 mp->context = context;
159
Neale Ranns097fa662018-05-01 05:17:55 -0700160 ip_prefix_encode (pfx, &mp->route.prefix);
161 mp->route.table_id =
162 htonl (fib_table_get_table_id
163 (fib_entry_get_fib_index (fib_entry_index), pfx->fp_proto));
164 mp->route.n_paths = path_count;
165 mp->route.stats_index =
166 htonl (fib_table_entry_get_stats_index
167 (fib_entry_get_fib_index (fib_entry_index), pfx));
Dave Barachb5e8a772016-12-06 12:04:42 -0500168
Neale Ranns097fa662018-05-01 05:17:55 -0700169 fp = mp->route.paths;
170 vec_foreach (rpath, rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500171 {
Neale Ranns097fa662018-05-01 05:17:55 -0700172 fib_api_path_encode (rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500173 fp++;
174 }
175
Florin Coras6c4dae22018-01-09 06:39:23 -0800176 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns9db6ada2019-11-08 12:42:31 +0000177 vec_free (rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500178}
179
Neale Ranns976b2592019-12-04 06:11:00 +0000180static void
181send_ip_route_v2_details (vpe_api_main_t *am, vl_api_registration_t *reg,
182 u32 context, fib_node_index_t fib_entry_index)
183{
184 fib_route_path_t *rpaths, *rpath;
185 vl_api_ip_route_v2_details_t *mp;
186 const fib_prefix_t *pfx;
187 vl_api_fib_path_t *fp;
188 int path_count;
189
190 rpaths = NULL;
191 pfx = fib_entry_get_prefix (fib_entry_index);
192 rpaths = fib_entry_encode (fib_entry_index);
193
194 path_count = vec_len (rpaths);
195 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
196 if (!mp)
197 return;
198 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +0000199 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_ROUTE_V2_DETAILS);
Neale Ranns976b2592019-12-04 06:11:00 +0000200 mp->context = context;
201
202 ip_prefix_encode (pfx, &mp->route.prefix);
203 mp->route.table_id = htonl (fib_table_get_table_id (
204 fib_entry_get_fib_index (fib_entry_index), pfx->fp_proto));
205 mp->route.n_paths = path_count;
206 mp->route.src = fib_entry_get_best_source (fib_entry_index);
207 mp->route.stats_index = htonl (fib_table_entry_get_stats_index (
208 fib_entry_get_fib_index (fib_entry_index), pfx));
209
210 fp = mp->route.paths;
211 vec_foreach (rpath, rpaths)
212 {
213 fib_api_path_encode (rpath, fp);
214 fp++;
215 }
216
217 vl_api_send_msg (reg, (u8 *) mp);
218 vec_free (rpaths);
219}
220
Dave Barachd7cb1b52016-12-09 09:52:16 -0500221typedef struct apt_ip6_fib_show_ctx_t_
222{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500223 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500224} api_ip6_fib_show_ctx_t;
225
Dave Barachb5e8a772016-12-06 12:04:42 -0500226static void
Neale Ranns097fa662018-05-01 05:17:55 -0700227vl_api_ip_route_dump_t_handler (vl_api_ip_route_dump_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500228{
229 vpe_api_main_t *am = &vpe_api_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500230 fib_node_index_t *fib_entry_index;
Florin Coras6c4dae22018-01-09 06:39:23 -0800231 vl_api_registration_t *reg;
Neale Ranns097fa662018-05-01 05:17:55 -0700232 fib_protocol_t fproto;
233 u32 fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -0500234
Florin Coras6c4dae22018-01-09 06:39:23 -0800235 reg = vl_api_client_index_to_registration (mp->client_index);
236 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500237 return;
238
Neale Ranns097fa662018-05-01 05:17:55 -0700239 vl_api_ip_fib_dump_walk_ctx_t ctx = {
240 .feis = NULL,
241 };
Neale Ranns81458422018-03-12 06:59:36 -0700242
Neale Ranns097fa662018-05-01 05:17:55 -0700243 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
244 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
245
246 if (INDEX_INVALID == fib_index)
247 return;
248
249 fib_table_walk (fib_index, fproto, vl_api_ip_fib_dump_walk, &ctx);
250
251 vec_foreach (fib_entry_index, ctx.feis)
252 {
253 send_ip_route_details (am, reg, mp->context, *fib_entry_index);
254 }
255
256 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500257}
258
259static void
Neale Ranns976b2592019-12-04 06:11:00 +0000260vl_api_ip_route_v2_dump_t_handler (vl_api_ip_route_v2_dump_t *mp)
261{
262 vpe_api_main_t *am = &vpe_api_main;
263 fib_node_index_t *fib_entry_index;
264 vl_api_registration_t *reg;
265 fib_protocol_t fproto;
266 fib_source_t src;
267 u32 fib_index;
268
269 reg = vl_api_client_index_to_registration (mp->client_index);
270 if (!reg)
271 return;
272
273 vl_api_ip_fib_dump_walk_ctx_t ctx = {
274 .feis = NULL,
275 };
276
277 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
278 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
279 src = mp->src;
280
281 if (INDEX_INVALID == fib_index)
282 return;
283
284 if (src)
285 fib_table_walk_w_src (fib_index, fproto, src, vl_api_ip_fib_dump_walk,
286 &ctx);
287 else
288 fib_table_walk (fib_index, fproto, vl_api_ip_fib_dump_walk, &ctx);
289
290 vec_foreach (fib_entry_index, ctx.feis)
291 {
292 send_ip_route_v2_details (am, reg, mp->context, *fib_entry_index);
293 }
294
295 vec_free (ctx.feis);
296}
297
298static void
Neale Ranns097fa662018-05-01 05:17:55 -0700299send_ip_mtable_details (vl_api_registration_t * reg,
300 u32 context, const mfib_table_t * mfib_table)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800301{
Neale Ranns097fa662018-05-01 05:17:55 -0700302 vl_api_ip_mtable_details_t *mp;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800303
Neale Ranns097fa662018-05-01 05:17:55 -0700304 mp = vl_msg_api_alloc (sizeof (*mp));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800305 if (!mp)
306 return;
Neale Ranns097fa662018-05-01 05:17:55 -0700307 memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +0000308 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_MTABLE_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800309 mp->context = context;
310
Neale Ranns097fa662018-05-01 05:17:55 -0700311 mp->table.table_id = htonl (mfib_table->mft_table_id);
312 mp->table.is_ip6 = (FIB_PROTOCOL_IP6 == mfib_table->mft_proto);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800313
Florin Coras6c4dae22018-01-09 06:39:23 -0800314 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800315}
316
Neale Ranns5a8123b2017-01-26 01:18:23 -0800317static void
Neale Ranns097fa662018-05-01 05:17:55 -0700318vl_api_ip_mtable_dump_t_handler (vl_api_ip_mtable_dump_t * mp)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800319{
Florin Coras6c4dae22018-01-09 06:39:23 -0800320 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800321 mfib_table_t *mfib_table;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800322
Florin Coras6c4dae22018-01-09 06:39:23 -0800323 reg = vl_api_client_index_to_registration (mp->client_index);
324 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800325 return;
326
Damjan Marionb2c31b62020-12-13 21:47:40 +0100327 pool_foreach (mfib_table, ip4_main.mfibs)
328 {
Neale Ranns097fa662018-05-01 05:17:55 -0700329 send_ip_mtable_details (reg, mp->context, mfib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100330 }
331 pool_foreach (mfib_table, ip6_main.mfibs)
332 {
Neale Ranns097fa662018-05-01 05:17:55 -0700333 send_ip_mtable_details (reg, mp->context, mfib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100334 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800335}
336
Neale Ranns097fa662018-05-01 05:17:55 -0700337typedef struct vl_api_ip_mfib_dump_ctx_t_
Neale Ranns5a8123b2017-01-26 01:18:23 -0800338{
339 fib_node_index_t *entries;
Neale Ranns097fa662018-05-01 05:17:55 -0700340} vl_api_ip_mfib_dump_ctx_t;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800341
Neale Ranns9db6ada2019-11-08 12:42:31 +0000342static walk_rc_t
Neale Ranns097fa662018-05-01 05:17:55 -0700343mfib_route_dump_walk (fib_node_index_t fei, void *arg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800344{
Neale Ranns097fa662018-05-01 05:17:55 -0700345 vl_api_ip_mfib_dump_ctx_t *ctx = arg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800346
347 vec_add1 (ctx->entries, fei);
348
Neale Ranns9db6ada2019-11-08 12:42:31 +0000349 return (WALK_CONTINUE);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800350}
351
352static void
Neale Ranns097fa662018-05-01 05:17:55 -0700353send_ip_mroute_details (vpe_api_main_t * am,
354 vl_api_registration_t * reg,
355 u32 context, fib_node_index_t mfib_entry_index)
356{
357 fib_route_path_t *rpaths, *rpath;
358 vl_api_ip_mroute_details_t *mp;
359 const mfib_prefix_t *pfx;
360 vl_api_mfib_path_t *fp;
Matthew Smitha2bbeb82020-09-17 16:33:10 -0500361 u8 path_count;
Neale Ranns097fa662018-05-01 05:17:55 -0700362
363 rpaths = NULL;
364 pfx = mfib_entry_get_prefix (mfib_entry_index);
365 rpaths = mfib_entry_encode (mfib_entry_index);
366
367 path_count = vec_len (rpaths);
368 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
369 if (!mp)
370 return;
371 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +0000372 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_MROUTE_DETAILS);
Neale Ranns097fa662018-05-01 05:17:55 -0700373 mp->context = context;
374
375 ip_mprefix_encode (pfx, &mp->route.prefix);
376 mp->route.table_id =
377 htonl (mfib_table_get_table_id
378 (mfib_entry_get_fib_index (mfib_entry_index), pfx->fp_proto));
Matthew Smitha2bbeb82020-09-17 16:33:10 -0500379 mp->route.n_paths = path_count;
Neale Ranns097fa662018-05-01 05:17:55 -0700380 fp = mp->route.paths;
381 vec_foreach (rpath, rpaths)
382 {
383 mfib_api_path_encode (rpath, fp);
384 fp++;
385 }
386
387 vl_api_send_msg (reg, (u8 *) mp);
388 vec_free (rpaths);
389}
390
391static void
392vl_api_ip_mroute_dump_t_handler (vl_api_ip_mroute_dump_t * mp)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800393{
394 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800395 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800396 fib_node_index_t *mfeip;
Neale Ranns097fa662018-05-01 05:17:55 -0700397 fib_protocol_t fproto;
398 u32 fib_index;
399
400 vl_api_ip_mfib_dump_ctx_t ctx = {
Neale Ranns5a8123b2017-01-26 01:18:23 -0800401 .entries = NULL,
402 };
403
Florin Coras6c4dae22018-01-09 06:39:23 -0800404 reg = vl_api_client_index_to_registration (mp->client_index);
405 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800406 return;
407
Neale Ranns097fa662018-05-01 05:17:55 -0700408 fproto = fib_ip_proto (mp->table.is_ip6);
409 fib_index = mfib_table_find (fproto, ntohl (mp->table.table_id));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800410
Neale Ranns097fa662018-05-01 05:17:55 -0700411 if (INDEX_INVALID == fib_index)
412 return;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800413
Neale Ranns097fa662018-05-01 05:17:55 -0700414 mfib_table_walk (fib_index, fproto, mfib_route_dump_walk, &ctx);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800415
Neale Ranns097fa662018-05-01 05:17:55 -0700416 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800417
Neale Ranns097fa662018-05-01 05:17:55 -0700418 vec_foreach (mfeip, ctx.entries)
419 {
420 send_ip_mroute_details (am, reg, mp->context, *mfeip);
421 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800422
423 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800424}
425
426static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700427vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
428 vlib_main_t * vm)
429{
430 vl_api_ip_punt_police_reply_t *rmp;
431 int rv = 0;
432
433 if (mp->is_ip6)
434 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
435 else
436 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
437
438 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
439}
440
441static void
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +0200442ip_punt_redirect_t_handler_common (u8 is_add, u32 rx_sw_if_index,
443 ip_address_family_t af,
444 const fib_route_path_t *rpaths)
445{
446 if (is_add)
447 {
448 if (af == AF_IP6)
449 ip6_punt_redirect_add_paths (rx_sw_if_index, rpaths);
450 else if (af == AF_IP4)
451 ip4_punt_redirect_add_paths (rx_sw_if_index, rpaths);
452 }
453 else
454 {
455 if (af == AF_IP6)
456 ip6_punt_redirect_del (rx_sw_if_index);
457 else if (af == AF_IP4)
458 ip4_punt_redirect_del (rx_sw_if_index);
459 }
460}
461
462static void
463vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t *mp,
464 vlib_main_t *vm)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700465{
466 vl_api_ip_punt_redirect_reply_t *rmp;
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +0200467 fib_route_path_t *rpaths = NULL, rpath = {
468 .frp_weight = 1,
469 .frp_fib_index = ~0,
470 };
471 ip_address_family_t af;
Pavel Kotucek609e1212018-11-27 09:59:44 +0100472 ip46_type_t ipv;
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +0200473 u32 rx_sw_if_index;
474 int rv = 0;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700475
Pavel Kotucek609e1212018-11-27 09:59:44 +0100476 if (!vnet_sw_if_index_is_api_valid (ntohl (mp->punt.tx_sw_if_index)))
477 goto bad_sw_if_index;
478
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +0200479 ipv = ip_address_decode (&mp->punt.nh, &rpath.frp_addr);
480 af = (ipv == IP46_TYPE_IP6) ? AF_IP6 : AF_IP4;
481 rpath.frp_proto = (ipv == IP46_TYPE_IP6) ? DPO_PROTO_IP6 : DPO_PROTO_IP4;
482 rpath.frp_sw_if_index = ntohl (mp->punt.tx_sw_if_index);
483 rx_sw_if_index = ntohl (mp->punt.rx_sw_if_index);
484
485 vec_add1 (rpaths, rpath);
486 ip_punt_redirect_t_handler_common (mp->is_add, rx_sw_if_index, af, rpaths);
487 vec_free (rpaths);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700488
Pavel Kotucek609e1212018-11-27 09:59:44 +0100489 BAD_SW_IF_INDEX_LABEL;
490
Neale Rannsd91c1db2017-07-31 02:30:50 -0700491 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
492}
493
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +0200494static void
495vl_api_add_del_ip_punt_redirect_v2_t_handler (
496 vl_api_add_del_ip_punt_redirect_v2_t *mp, vlib_main_t *vm)
497{
498 vl_api_add_del_ip_punt_redirect_v2_reply_t *rmp;
499 fib_route_path_t *rpaths = NULL, *rpath;
500 vl_api_fib_path_t *apath;
501 ip_address_family_t af;
502 u32 rx_sw_if_index, n_paths;
503 int rv = 0, ii;
504
505 rx_sw_if_index = ntohl (mp->punt.rx_sw_if_index);
506 n_paths = ntohl (mp->punt.n_paths);
507
508 rv = ip_address_family_decode (mp->punt.af, &af);
509 if (rv != 0)
510 goto out;
511
512 if (0 != n_paths)
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100513 {
514 vec_validate (rpaths, n_paths - 1);
515 }
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +0200516
517 for (ii = 0; ii < n_paths; ii++)
518 {
519 apath = &mp->punt.paths[ii];
520 rpath = &rpaths[ii];
521
522 rv = fib_api_path_decode (apath, rpath);
523
524 if (rv != 0)
525 goto out;
526 }
527
528 ip_punt_redirect_t_handler_common (mp->is_add, rx_sw_if_index, af, rpaths);
529
530out:
531 vec_free (rpaths);
532
533 REPLY_MACRO (VL_API_ADD_DEL_IP_PUNT_REDIRECT_V2_REPLY);
534}
535
Steven Luongc3ed1c92020-07-27 10:06:58 -0700536static clib_error_t *
537call_elf_section_ip_table_callbacks (vnet_main_t * vnm, u32 table_id,
538 u32 flags,
539 _vnet_ip_table_function_list_elt_t **
540 elts)
541{
542 _vnet_ip_table_function_list_elt_t *elt;
543 vnet_ip_table_function_priority_t prio;
544 clib_error_t *error = 0;
545
546 for (prio = VNET_IP_TABLE_FUNC_PRIORITY_LOW;
547 prio <= VNET_IP_TABLE_FUNC_PRIORITY_HIGH; prio++)
548 {
549 elt = elts[prio];
550
551 while (elt)
552 {
553 error = elt->fp (vnm, table_id, flags);
554 if (error)
555 return error;
556 elt = elt->next_ip_table_function;
557 }
558 }
559 return error;
560}
561
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700562void
Neale Ranns15002542017-09-10 04:39:11 -0700563ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
564{
565 u32 fib_index, mfib_index;
Steven Luongc3ed1c92020-07-27 10:06:58 -0700566 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns15002542017-09-10 04:39:11 -0700567
568 /*
569 * ignore action on the default table - this is always present
570 * and cannot be added nor deleted from the API
571 */
572 if (0 != table_id)
573 {
574 /*
575 * The API holds only one lock on the table.
576 * i.e. it can be added many times via the API but needs to be
577 * deleted only once.
578 * The FIB index for unicast and multicast is not necessarily the
579 * same, since internal VPP systesm (like LISP and SR) create
580 * their own unicast tables.
581 */
582 fib_index = fib_table_find (fproto, table_id);
583 mfib_index = mfib_table_find (fproto, table_id);
584
Steven Luongc3ed1c92020-07-27 10:06:58 -0700585 if ((~0 != fib_index) || (~0 != mfib_index))
586 call_elf_section_ip_table_callbacks (vnm, table_id, 0 /* is_add */ ,
587 vnm->ip_table_add_del_functions);
588
Neale Ranns15002542017-09-10 04:39:11 -0700589 if (~0 != fib_index)
590 {
591 fib_table_unlock (fib_index, fproto,
592 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
593 }
594 if (~0 != mfib_index)
595 {
596 mfib_table_unlock (mfib_index, fproto,
597 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
598 }
599 }
600}
601
Aloys Augustin6e4cfb52021-09-16 20:53:14 +0200602/*
603 * Returns an unused table id, and ~0 if it can't find one.
604 */
605u32
606ip_table_get_unused_id (fib_protocol_t fproto)
607{
608 int i, j;
Aloys Augustin400f23f2021-09-26 18:26:15 +0200609 static u32 seed = 0;
Aloys Augustin6e4cfb52021-09-16 20:53:14 +0200610 /* limit to 1M tries */
611 for (j = 0; j < 1 << 10; j++)
612 {
613 seed = random_u32 (&seed);
614 for (i = 0; i < 1 << 10; i++)
615 {
616 /* look around randomly generated id */
617 seed += (2 * (i % 2) - 1) * i;
618 if (seed == ~0)
619 continue;
620 if (fib_table_find (fproto, seed) == ~0)
621 return seed;
622 }
623 }
624
625 return ~0;
626}
627
Neale Ranns15002542017-09-10 04:39:11 -0700628void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700629vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
630{
631 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns097fa662018-05-01 05:17:55 -0700632 fib_protocol_t fproto = (mp->table.is_ip6 ?
633 FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
634 u32 table_id = ntohl (mp->table.table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700635 int rv = 0;
636
Neale Ranns15002542017-09-10 04:39:11 -0700637 if (mp->is_add)
638 {
Benoît Ganneff570d32024-04-16 09:36:05 +0200639 ip_table_create (fproto, table_id, 1 /* is_api */, 1 /* create_mfib */,
640 mp->table.name);
Neale Ranns15002542017-09-10 04:39:11 -0700641 }
642 else
643 {
644 ip_table_delete (fproto, table_id, 1);
645 }
646
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700647 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
648}
649
Aloys Augustin6e4cfb52021-09-16 20:53:14 +0200650void
Benoît Ganneff570d32024-04-16 09:36:05 +0200651vl_api_ip_table_add_del_v2_t_handler (vl_api_ip_table_add_del_v2_t *mp)
652{
653 vl_api_ip_table_add_del_v2_reply_t *rmp;
654 fib_protocol_t fproto =
655 (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
656 u32 table_id = ntohl (mp->table.table_id);
657 int rv = 0;
658
659 if (mp->is_add)
660 {
661 ip_table_create (fproto, table_id, 1 /* is_api */, mp->create_mfib,
662 mp->table.name);
663 }
664 else
665 {
666 ip_table_delete (fproto, table_id, 1);
667 }
668
669 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_V2_REPLY);
670}
671
672void
Aloys Augustin6e4cfb52021-09-16 20:53:14 +0200673vl_api_ip_table_allocate_t_handler (vl_api_ip_table_allocate_t *mp)
674{
675 vl_api_ip_table_allocate_reply_t *rmp;
676 fib_protocol_t fproto =
677 (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
678 u32 table_id = ntohl (mp->table.table_id);
679 int rv = 0;
680
681 if (~0 == table_id)
682 table_id = ip_table_get_unused_id (fproto);
683
684 if (~0 == table_id)
685 rv = VNET_API_ERROR_EAGAIN;
686 else
Benoît Ganneff570d32024-04-16 09:36:05 +0200687 ip_table_create (fproto, table_id, 1 /* is_api */, 1 /* create_mfib */,
688 mp->table.name);
Aloys Augustin6e4cfb52021-09-16 20:53:14 +0200689
690 REPLY_MACRO2 (VL_API_IP_TABLE_ALLOCATE_REPLY, {
691 clib_memcpy_fast (&rmp->table, &mp->table, sizeof (mp->table));
692 rmp->table.table_id = htonl (table_id);
693 })
694}
695
Dave Barachb5e8a772016-12-06 12:04:42 -0500696static int
Neale Ranns097fa662018-05-01 05:17:55 -0700697ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp, u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500698{
Neale Ranns097fa662018-05-01 05:17:55 -0700699 fib_route_path_t *rpaths = NULL, *rpath;
700 fib_entry_flag_t entry_flags;
701 vl_api_fib_path_t *apath;
702 fib_prefix_t pfx;
703 u32 fib_index;
704 int rv, ii;
Dave Barachb5e8a772016-12-06 12:04:42 -0500705
Neale Ranns097fa662018-05-01 05:17:55 -0700706 entry_flags = FIB_ENTRY_FLAG_NONE;
707 ip_prefix_decode (&mp->route.prefix, &pfx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500708
Neale Ranns097fa662018-05-01 05:17:55 -0700709 rv = fib_api_table_id_decode (pfx.fp_proto,
710 ntohl (mp->route.table_id), &fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500711 if (0 != rv)
Neale Ranns097fa662018-05-01 05:17:55 -0700712 goto out;
Dave Barachb5e8a772016-12-06 12:04:42 -0500713
Neale Rannsc2ac2352019-07-02 14:33:29 +0000714 if (0 != mp->route.n_paths)
715 vec_validate (rpaths, mp->route.n_paths - 1);
Neale Ranns097fa662018-05-01 05:17:55 -0700716
717 for (ii = 0; ii < mp->route.n_paths; ii++)
718 {
719 apath = &mp->route.paths[ii];
720 rpath = &rpaths[ii];
721
722 rv = fib_api_path_decode (apath, rpath);
723
724 if ((rpath->frp_flags & FIB_ROUTE_PATH_LOCAL) &&
725 (~0 == rpath->frp_sw_if_index))
726 entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
727
728 if (0 != rv)
729 goto out;
730 }
731
Neale Ranns976b2592019-12-04 06:11:00 +0000732 rv = fib_api_route_add_del (mp->is_add, mp->is_multipath, fib_index, &pfx,
733 FIB_SOURCE_API, entry_flags, rpaths);
734
735 if (mp->is_add && 0 == rv)
736 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
737
738out:
739 vec_free (rpaths);
740
741 return (rv);
742}
743
744static int
745ip_route_add_del_v2_t_handler (vl_api_ip_route_add_del_v2_t *mp,
746 u32 *stats_index)
747{
748 fib_route_path_t *rpaths = NULL, *rpath;
749 fib_entry_flag_t entry_flags;
750 vl_api_fib_path_t *apath;
751 fib_source_t src;
752 fib_prefix_t pfx;
753 u32 fib_index;
754 int rv, ii;
755
756 entry_flags = FIB_ENTRY_FLAG_NONE;
757 ip_prefix_decode (&mp->route.prefix, &pfx);
758
759 rv = fib_api_table_id_decode (pfx.fp_proto, ntohl (mp->route.table_id),
760 &fib_index);
761 if (0 != rv)
762 goto out;
763
764 if (0 != mp->route.n_paths)
765 vec_validate (rpaths, mp->route.n_paths - 1);
766
767 for (ii = 0; ii < mp->route.n_paths; ii++)
768 {
769 apath = &mp->route.paths[ii];
770 rpath = &rpaths[ii];
771
772 rv = fib_api_path_decode (apath, rpath);
773
774 if ((rpath->frp_flags & FIB_ROUTE_PATH_LOCAL) &&
775 (~0 == rpath->frp_sw_if_index))
776 entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
777
778 if (0 != rv)
779 goto out;
780 }
781
782 src = (0 == mp->route.src ? FIB_SOURCE_API : mp->route.src);
783
784 rv = fib_api_route_add_del (mp->is_add, mp->is_multipath, fib_index, &pfx,
785 src, entry_flags, rpaths);
Neale Ranns008dbe12018-09-07 09:32:36 -0700786
787 if (mp->is_add && 0 == rv)
788 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
789
Neale Ranns097fa662018-05-01 05:17:55 -0700790out:
791 vec_free (rpaths);
Neale Ranns008dbe12018-09-07 09:32:36 -0700792
793 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -0500794}
795
796void
Neale Ranns097fa662018-05-01 05:17:55 -0700797vl_api_ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500798{
Neale Ranns097fa662018-05-01 05:17:55 -0700799 vl_api_ip_route_add_del_reply_t *rmp;
800 u32 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -0500801 int rv;
Dave Barachb5e8a772016-12-06 12:04:42 -0500802
Neale Ranns097fa662018-05-01 05:17:55 -0700803 rv = ip_route_add_del_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500804
Neale Ranns097fa662018-05-01 05:17:55 -0700805 REPLY_MACRO2 (VL_API_IP_ROUTE_ADD_DEL_REPLY,
Neale Ranns008dbe12018-09-07 09:32:36 -0700806 ({
807 rmp->stats_index = htonl (stats_index);
808 }))
Dave Barachb5e8a772016-12-06 12:04:42 -0500809}
810
Neale Ranns15002542017-09-10 04:39:11 -0700811void
Neale Ranns976b2592019-12-04 06:11:00 +0000812vl_api_ip_route_add_del_v2_t_handler (vl_api_ip_route_add_del_v2_t *mp)
813{
814 vl_api_ip_route_add_del_v2_reply_t *rmp;
815 u32 stats_index = ~0;
816 int rv;
817
818 rv = ip_route_add_del_v2_t_handler (mp, &stats_index);
819
820 /* clang-format off */
821 REPLY_MACRO2 (VL_API_IP_ROUTE_ADD_DEL_V2_REPLY,
822 ({
823 rmp->stats_index = htonl (stats_index);
824 }))
825 /* clang-format on */
826}
827
828void
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400829vl_api_ip_route_lookup_t_handler (vl_api_ip_route_lookup_t * mp)
830{
831 vl_api_ip_route_lookup_reply_t *rmp = NULL;
832 fib_route_path_t *rpaths = NULL, *rpath;
833 const fib_prefix_t *pfx = NULL;
834 fib_prefix_t lookup;
835 vl_api_fib_path_t *fp;
836 fib_node_index_t fib_entry_index;
837 u32 fib_index;
838 int npaths = 0;
839 int rv;
840
841 ip_prefix_decode (&mp->prefix, &lookup);
842 rv = fib_api_table_id_decode (lookup.fp_proto, ntohl (mp->table_id),
843 &fib_index);
844 if (PREDICT_TRUE (!rv))
845 {
846 if (mp->exact)
847 fib_entry_index = fib_table_lookup_exact_match (fib_index, &lookup);
848 else
849 fib_entry_index = fib_table_lookup (fib_index, &lookup);
850 if (fib_entry_index == FIB_NODE_INDEX_INVALID)
851 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
852 else
853 {
854 pfx = fib_entry_get_prefix (fib_entry_index);
855 rpaths = fib_entry_encode (fib_entry_index);
856 npaths = vec_len (rpaths);
857 }
858 }
859
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400860 REPLY_MACRO3_ZERO(VL_API_IP_ROUTE_LOOKUP_REPLY,
861 npaths * sizeof (*fp),
862 ({
863 if (!rv)
864 {
865 ip_prefix_encode (pfx, &rmp->route.prefix);
866 rmp->route.table_id = mp->table_id;
867 rmp->route.n_paths = npaths;
868 rmp->route.stats_index = fib_table_entry_get_stats_index (fib_index, pfx);
869 rmp->route.stats_index = htonl (rmp->route.stats_index);
870
871 fp = rmp->route.paths;
872 vec_foreach (rpath, rpaths)
873 {
874 fib_api_path_encode (rpath, fp);
875 fp++;
876 }
877 }
878 }));
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400879 vec_free (rpaths);
880}
881
882void
Neale Ranns976b2592019-12-04 06:11:00 +0000883vl_api_ip_route_lookup_v2_t_handler (vl_api_ip_route_lookup_v2_t *mp)
884{
885 vl_api_ip_route_lookup_v2_reply_t *rmp = NULL;
886 fib_route_path_t *rpaths = NULL, *rpath;
887 const fib_prefix_t *pfx = NULL;
888 fib_prefix_t lookup;
889 vl_api_fib_path_t *fp;
890 fib_node_index_t fib_entry_index;
891 u32 fib_index;
892 int npaths = 0;
893 fib_source_t src = 0;
894 int rv;
895
896 ip_prefix_decode (&mp->prefix, &lookup);
897 rv = fib_api_table_id_decode (lookup.fp_proto, ntohl (mp->table_id),
898 &fib_index);
899 if (PREDICT_TRUE (!rv))
900 {
901 if (mp->exact)
902 fib_entry_index = fib_table_lookup_exact_match (fib_index, &lookup);
903 else
904 fib_entry_index = fib_table_lookup (fib_index, &lookup);
905 if (fib_entry_index == FIB_NODE_INDEX_INVALID)
906 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
907 else
908 {
909 pfx = fib_entry_get_prefix (fib_entry_index);
910 rpaths = fib_entry_encode (fib_entry_index);
911 npaths = vec_len (rpaths);
912 src = fib_entry_get_best_source (fib_entry_index);
913 }
914 }
915
916 /* clang-format off */
917 REPLY_MACRO3_ZERO(VL_API_IP_ROUTE_LOOKUP_V2_REPLY,
918 npaths * sizeof (*fp),
919 ({
920 if (!rv)
921 {
922 ip_prefix_encode (pfx, &rmp->route.prefix);
923 rmp->route.table_id = mp->table_id;
924 rmp->route.n_paths = npaths;
925 rmp->route.src = src;
926 rmp->route.stats_index = fib_table_entry_get_stats_index (fib_index, pfx);
927 rmp->route.stats_index = htonl (rmp->route.stats_index);
928
929 fp = rmp->route.paths;
930 vec_foreach (rpath, rpaths)
931 {
932 fib_api_path_encode (rpath, fp);
933 fp++;
934 }
935 }
936 }));
937 /* clang-format on */
938 vec_free (rpaths);
939}
940
941void
Benoît Ganneff570d32024-04-16 09:36:05 +0200942ip_table_create (fib_protocol_t fproto, u32 table_id, u8 is_api,
943 u8 create_mfib, const u8 *name)
Neale Ranns15002542017-09-10 04:39:11 -0700944{
945 u32 fib_index, mfib_index;
Steven Luongc3ed1c92020-07-27 10:06:58 -0700946 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns15002542017-09-10 04:39:11 -0700947
948 /*
949 * ignore action on the default table - this is always present
950 * and cannot be added nor deleted from the API
951 */
952 if (0 != table_id)
953 {
954 /*
955 * The API holds only one lock on the table.
956 * i.e. it can be added many times via the API but needs to be
957 * deleted only once.
958 * The FIB index for unicast and multicast is not necessarily the
959 * same, since internal VPP systesm (like LISP and SR) create
960 * their own unicast tables.
961 */
962 fib_index = fib_table_find (fproto, table_id);
Nathan Skrzypczak275bd792021-09-17 17:29:14 +0200963 /*
964 * Always try to re-lock in case the fib was deleted by an API call
965 * but was not yet freed because some other locks were held
966 */
967 fib_table_find_or_create_and_lock_w_name (
968 fproto, table_id, (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI), name);
Benoît Ganneff570d32024-04-16 09:36:05 +0200969
970 if (create_mfib)
971 {
972 /* same for mfib, if needs be */
973 mfib_index = mfib_table_find (fproto, table_id);
974 mfib_table_find_or_create_and_lock_w_name (
975 fproto, table_id, (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI),
976 name);
977 }
978 else
979 mfib_index = 0;
Steven Luongc3ed1c92020-07-27 10:06:58 -0700980
981 if ((~0 == fib_index) || (~0 == mfib_index))
982 call_elf_section_ip_table_callbacks (vnm, table_id, 1 /* is_add */ ,
983 vnm->ip_table_add_del_functions);
Neale Ranns15002542017-09-10 04:39:11 -0700984 }
985}
986
Neale Ranns097fa662018-05-01 05:17:55 -0700987static u32
Neale Ranns32e1c012016-11-22 17:07:28 +0000988mroute_add_del_handler (u8 is_add,
Neale Ranns097fa662018-05-01 05:17:55 -0700989 u8 is_multipath,
Neale Ranns32e1c012016-11-22 17:07:28 +0000990 u32 fib_index,
991 const mfib_prefix_t * prefix,
992 u32 entry_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700993 u32 rpf_id, fib_route_path_t * rpaths)
Neale Ranns32e1c012016-11-22 17:07:28 +0000994{
Neale Ranns097fa662018-05-01 05:17:55 -0700995 u32 mfib_entry_index = ~0;
Neale Ranns28c142e2018-09-07 09:37:07 -0700996
Neale Ranns097fa662018-05-01 05:17:55 -0700997 if (0 == vec_len (rpaths))
Neale Ranns32e1c012016-11-22 17:07:28 +0000998 {
Neale Ranns28c142e2018-09-07 09:37:07 -0700999 mfib_entry_index = mfib_table_entry_update (fib_index, prefix,
1000 MFIB_SOURCE_API,
1001 rpf_id, entry_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001002 }
1003 else
1004 {
Neale Ranns097fa662018-05-01 05:17:55 -07001005 if (is_add)
1006 {
Paul Atkins8e2b1b12021-10-12 14:32:11 +01001007 mfib_entry_index = mfib_table_entry_paths_update (
1008 fib_index, prefix, MFIB_SOURCE_API, entry_flags, rpaths);
Neale Ranns097fa662018-05-01 05:17:55 -07001009 }
1010 else
1011 {
1012 mfib_table_entry_paths_remove (fib_index, prefix,
1013 MFIB_SOURCE_API, rpaths);
1014 }
Neale Ranns32e1c012016-11-22 17:07:28 +00001015 }
1016
Neale Ranns28c142e2018-09-07 09:37:07 -07001017 return (mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001018}
1019
1020static int
Neale Ranns28c142e2018-09-07 09:37:07 -07001021api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp,
1022 u32 * stats_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001023{
Neale Ranns097fa662018-05-01 05:17:55 -07001024 fib_route_path_t *rpath, *rpaths = NULL;
Neale Ranns28c142e2018-09-07 09:37:07 -07001025 fib_node_index_t mfib_entry_index;
Neale Ranns990f6942020-10-20 07:20:17 +00001026 mfib_entry_flags_t eflags;
Neale Ranns097fa662018-05-01 05:17:55 -07001027 mfib_prefix_t pfx;
Neale Ranns32e1c012016-11-22 17:07:28 +00001028 u32 fib_index;
1029 int rv;
Neale Ranns097fa662018-05-01 05:17:55 -07001030 u16 ii;
Neale Ranns32e1c012016-11-22 17:07:28 +00001031
Neale Ranns097fa662018-05-01 05:17:55 -07001032 ip_mprefix_decode (&mp->route.prefix, &pfx);
Neale Ranns32e1c012016-11-22 17:07:28 +00001033
Neale Ranns097fa662018-05-01 05:17:55 -07001034 rv = mfib_api_table_id_decode (pfx.fp_proto,
1035 ntohl (mp->route.table_id), &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001036 if (0 != rv)
Neale Ranns097fa662018-05-01 05:17:55 -07001037 goto out;
Neale Ranns32e1c012016-11-22 17:07:28 +00001038
Neale Ranns097fa662018-05-01 05:17:55 -07001039 vec_validate (rpaths, mp->route.n_paths - 1);
Neale Ranns32e1c012016-11-22 17:07:28 +00001040
Neale Ranns097fa662018-05-01 05:17:55 -07001041 for (ii = 0; ii < mp->route.n_paths; ii++)
Neale Ranns32e1c012016-11-22 17:07:28 +00001042 {
Neale Ranns097fa662018-05-01 05:17:55 -07001043 rpath = &rpaths[ii];
1044
1045 rv = mfib_api_path_decode (&mp->route.paths[ii], rpath);
1046
1047 if (0 != rv)
1048 goto out;
Neale Ranns32e1c012016-11-22 17:07:28 +00001049 }
1050
Neale Ranns990f6942020-10-20 07:20:17 +00001051 eflags = mfib_api_path_entry_flags_decode (mp->route.entry_flags);
Neale Ranns28c142e2018-09-07 09:37:07 -07001052 mfib_entry_index = mroute_add_del_handler (mp->is_add,
Neale Ranns097fa662018-05-01 05:17:55 -07001053 mp->is_add,
Neale Ranns28c142e2018-09-07 09:37:07 -07001054 fib_index, &pfx,
Neale Ranns990f6942020-10-20 07:20:17 +00001055 eflags,
Neale Ranns097fa662018-05-01 05:17:55 -07001056 ntohl (mp->route.rpf_id),
1057 rpaths);
Neale Ranns28c142e2018-09-07 09:37:07 -07001058
1059 if (~0 != mfib_entry_index)
1060 *stats_index = mfib_entry_get_stats_index (mfib_entry_index);
1061
Neale Ranns097fa662018-05-01 05:17:55 -07001062out:
Neale Ranns28c142e2018-09-07 09:37:07 -07001063 return (rv);
Neale Ranns32e1c012016-11-22 17:07:28 +00001064}
1065
1066void
1067vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1068{
1069 vl_api_ip_mroute_add_del_reply_t *rmp;
Neale Ranns097fa662018-05-01 05:17:55 -07001070 u32 stats_index = ~0;
Neale Ranns32e1c012016-11-22 17:07:28 +00001071 int rv;
Neale Ranns32e1c012016-11-22 17:07:28 +00001072
Neale Ranns28c142e2018-09-07 09:37:07 -07001073 rv = api_mroute_add_del_t_handler (mp, &stats_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001074
Neale Ranns28c142e2018-09-07 09:37:07 -07001075 REPLY_MACRO2 (VL_API_IP_MROUTE_ADD_DEL_REPLY,
1076 ({
1077 rmp->stats_index = htonl (stats_index);
1078 }));
Neale Ranns32e1c012016-11-22 17:07:28 +00001079}
1080
Dave Barachb5e8a772016-12-06 12:04:42 -05001081static void
1082send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001083 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1084 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001085{
1086 vl_api_ip_details_t *mp;
1087
1088 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001089 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001090 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -05001091
1092 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001093 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001094 mp->context = context;
1095
Florin Coras6c4dae22018-01-09 06:39:23 -08001096 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001097}
1098
1099static void
1100send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001101 vl_api_registration_t * reg,
Neale Ranns097fa662018-05-01 05:17:55 -07001102 const fib_prefix_t * pfx,
1103 u32 sw_if_index, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001104{
1105 vl_api_ip_address_details_t *mp;
1106
1107 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001108 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001109 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_ADDRESS_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -05001110
Neale Ranns097fa662018-05-01 05:17:55 -07001111 ip_prefix_encode (pfx, &mp->prefix);
Dave Barachb5e8a772016-12-06 12:04:42 -05001112 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001113 mp->sw_if_index = htonl (sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001114
Florin Coras6c4dae22018-01-09 06:39:23 -08001115 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001116}
1117
1118static void
1119vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1120{
1121 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001122 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001123 ip6_main_t *im6 = &ip6_main;
1124 ip4_main_t *im4 = &ip4_main;
1125 ip_lookup_main_t *lm6 = &im6->lookup_main;
1126 ip_lookup_main_t *lm4 = &im4->lookup_main;
1127 ip_interface_address_t *ia = 0;
1128 u32 sw_if_index = ~0;
1129 int rv __attribute__ ((unused)) = 0;
1130
1131 VALIDATE_SW_IF_INDEX (mp);
1132
1133 sw_if_index = ntohl (mp->sw_if_index);
1134
Florin Coras6c4dae22018-01-09 06:39:23 -08001135 reg = vl_api_client_index_to_registration (mp->client_index);
1136 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001137 return;
1138
Dave Barachb5e8a772016-12-06 12:04:42 -05001139 if (mp->is_ipv6)
1140 {
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001141 /* Do not send subnet details of the IP-interface for
1142 * unnumbered interfaces. otherwise listening clients
1143 * will be confused that the subnet is applied on more
1144 * than one interface */
1145 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001146 ({
Neale Ranns097fa662018-05-01 05:17:55 -07001147 fib_prefix_t pfx = {
1148 .fp_addr.ip6 = *(ip6_address_t *)ip_interface_address_get_address (lm6, ia),
1149 .fp_len = ia->address_length,
1150 .fp_proto = FIB_PROTOCOL_IP6,
1151 };
1152 send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001153 }));
1154 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001155 else
1156 {
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001157 foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001158 ({
Neale Ranns097fa662018-05-01 05:17:55 -07001159 fib_prefix_t pfx = {
1160 .fp_addr.ip4 = *(ip4_address_t *)ip_interface_address_get_address (lm4, ia),
1161 .fp_len = ia->address_length,
1162 .fp_proto = FIB_PROTOCOL_IP4,
1163 };
1164
1165 send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001166 }));
1167 }
Neale Ranns008dbe12018-09-07 09:32:36 -07001168
Dave Barachb5e8a772016-12-06 12:04:42 -05001169 BAD_SW_IF_INDEX_LABEL;
1170}
1171
1172static void
Neale Ranns9e2f9152018-05-18 02:27:10 -07001173send_ip_unnumbered_details (vpe_api_main_t * am,
1174 vl_api_registration_t * reg,
1175 u32 sw_if_index, u32 ip_sw_if_index, u32 context)
1176{
1177 vl_api_ip_unnumbered_details_t *mp;
1178
1179 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001180 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001181 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_UNNUMBERED_DETAILS);
Neale Ranns9e2f9152018-05-18 02:27:10 -07001182
1183 mp->context = context;
1184 mp->sw_if_index = htonl (sw_if_index);
1185 mp->ip_sw_if_index = htonl (ip_sw_if_index);
1186
1187 vl_api_send_msg (reg, (u8 *) mp);
1188}
1189
1190static void
1191vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
1192{
1193 vnet_main_t *vnm = vnet_get_main ();
1194 vnet_interface_main_t *im = &vnm->interface_main;
1195 int rv __attribute__ ((unused)) = 0;
1196 vpe_api_main_t *am = &vpe_api_main;
1197 vl_api_registration_t *reg;
1198 vnet_sw_interface_t *si;
1199 u32 sw_if_index;
1200
1201 sw_if_index = ntohl (mp->sw_if_index);
1202
1203 reg = vl_api_client_index_to_registration (mp->client_index);
1204 if (!reg)
1205 return;
1206
1207 if (~0 != sw_if_index)
1208 {
1209 VALIDATE_SW_IF_INDEX (mp);
1210
1211 si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
1212
Neale Rannsac3e72c2019-10-06 01:04:26 -07001213 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
Neale Ranns9e2f9152018-05-18 02:27:10 -07001214 {
1215 send_ip_unnumbered_details (am, reg,
1216 sw_if_index,
1217 si->unnumbered_sw_if_index,
1218 mp->context);
1219 }
1220 }
1221 else
1222 {
Damjan Marionb2c31b62020-12-13 21:47:40 +01001223 pool_foreach (si, im->sw_interfaces)
1224 {
Neale Ranns9e2f9152018-05-18 02:27:10 -07001225 if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1226 {
1227 send_ip_unnumbered_details(am, reg,
1228 si->sw_if_index,
1229 si->unnumbered_sw_if_index,
1230 mp->context);
1231 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001232 }
Neale Ranns9e2f9152018-05-18 02:27:10 -07001233 }
1234
1235 BAD_SW_IF_INDEX_LABEL;
1236}
1237
1238static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001239vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1240{
1241 vpe_api_main_t *am = &vpe_api_main;
1242 vnet_main_t *vnm = vnet_get_main ();
Neale Rannscbe25aa2019-09-30 10:53:31 +00001243 //vlib_main_t *vm = vlib_get_main ();
Dave Barachb5e8a772016-12-06 12:04:42 -05001244 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001245 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001246 vnet_sw_interface_t *si, *sorted_sis;
1247 u32 sw_if_index = ~0;
1248
Florin Coras6c4dae22018-01-09 06:39:23 -08001249 reg = vl_api_client_index_to_registration (mp->client_index);
1250 if (!reg)
1251 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001252
1253 /* Gather interfaces. */
1254 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
Damjan Marion8bea5892022-04-04 22:40:45 +02001255 vec_set_len (sorted_sis, 0);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001256 pool_foreach (si, im->sw_interfaces)
1257 {
Dave Barachb5e8a772016-12-06 12:04:42 -05001258 vec_add1 (sorted_sis, si[0]);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001259 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001260
1261 vec_foreach (si, sorted_sis)
1262 {
1263 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1264 {
Neale Rannscbe25aa2019-09-30 10:53:31 +00001265 /* if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index)) */
1266 /* { */
1267 /* continue; */
1268 /* } */
Dave Barachb5e8a772016-12-06 12:04:42 -05001269 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001270 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001271 }
1272 }
Matthew Smith6d5f6592019-09-18 13:51:46 -05001273
1274 vec_free (sorted_sis);
Dave Barachb5e8a772016-12-06 12:04:42 -05001275}
1276
1277static void
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001278vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t *mp)
Dave Barachb5e8a772016-12-06 12:04:42 -05001279{
1280 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001281 int rv;
1282 u32 table_id;
1283 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001284
Neale Ranns227038a2017-04-21 01:07:59 -07001285 table_id = ntohl (mp->vrf_id);
1286
1287#define _(a,b) if (mp->a) flow_hash_config |= b;
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001288 foreach_flow_hash_bit_v1;
Neale Ranns227038a2017-04-21 01:07:59 -07001289#undef _
1290
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001291 rv = ip_flow_hash_set ((mp->is_ipv6 ? AF_IP6 : AF_IP4), table_id,
1292 flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001293
1294 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1295}
1296
1297static void
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001298vl_api_set_ip_flow_hash_v2_t_handler (vl_api_set_ip_flow_hash_v2_t *mp)
Dave Barachb5e8a772016-12-06 12:04:42 -05001299{
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001300 vl_api_set_ip_flow_hash_v2_reply_t *rmp;
1301 ip_address_family_t af;
Dave Barachb5e8a772016-12-06 12:04:42 -05001302 int rv;
Dave Barachb5e8a772016-12-06 12:04:42 -05001303
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001304 rv = ip_address_family_decode (mp->af, &af);
Dave Barachb5e8a772016-12-06 12:04:42 -05001305
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001306 if (!rv)
1307 rv = ip_flow_hash_set (af, htonl (mp->table_id),
1308 htonl (mp->flow_hash_config));
Dave Barachb5e8a772016-12-06 12:04:42 -05001309
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001310 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_V2_REPLY);
Dave Barachb5e8a772016-12-06 12:04:42 -05001311}
1312
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001313static void
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001314vl_api_set_ip_flow_hash_v3_t_handler (vl_api_set_ip_flow_hash_v3_t *mp)
1315{
1316 vl_api_set_ip_flow_hash_v3_reply_t *rmp;
1317 ip_address_family_t af;
1318 int rv;
1319
1320 rv = ip_address_family_decode (mp->af, &af);
1321
1322 if (!rv)
1323 rv = ip_flow_hash_set (af, htonl (mp->table_id),
1324 htonl (mp->flow_hash_config));
1325
1326 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_V3_REPLY);
1327}
1328
1329static void
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001330vl_api_set_ip_flow_hash_router_id_t_handler (
1331 vl_api_set_ip_flow_hash_router_id_t *mp)
1332{
1333 vl_api_set_ip_flow_hash_router_id_reply_t *rmp;
1334 int rv = 0;
1335
1336 ip_flow_hash_router_id_set (ntohl (mp->router_id));
1337
1338 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_ROUTER_ID_REPLY);
1339}
1340
Neale Ranns32e1c012016-11-22 17:07:28 +00001341void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001342vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001343 u32 context, const mfib_signal_t * mfs)
1344{
1345 vl_api_mfib_signal_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -08001346 const mfib_prefix_t *prefix;
Neale Ranns32e1c012016-11-22 17:07:28 +00001347 mfib_table_t *mfib;
1348 mfib_itf_t *mfi;
1349
1350 mp = vl_msg_api_alloc (sizeof (*mp));
1351
Dave Barachb7b92992018-10-17 10:38:51 -04001352 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001353 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_MFIB_SIGNAL_DETAILS);
Neale Ranns32e1c012016-11-22 17:07:28 +00001354 mp->context = context;
1355
1356 mfi = mfib_itf_get (mfs->mfs_itf);
Neale Ranns9e829a82018-12-17 05:50:32 -08001357 prefix = mfib_entry_get_prefix (mfs->mfs_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +00001358 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
Neale Ranns9e829a82018-12-17 05:50:32 -08001359 prefix->fp_proto);
Neale Ranns32e1c012016-11-22 17:07:28 +00001360 mp->table_id = ntohl (mfib->mft_table_id);
1361 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1362
Neale Ranns097fa662018-05-01 05:17:55 -07001363 ip_mprefix_encode (prefix, &mp->prefix);
Neale Ranns32e1c012016-11-22 17:07:28 +00001364
1365 if (0 != mfs->mfs_buffer_len)
1366 {
1367 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1368
1369 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1370 }
1371 else
1372 {
1373 mp->ip_packet_len = 0;
1374 }
1375
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001376 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001377}
1378
1379static void
1380vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1381{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001382 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001383
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001384 reg = vl_api_client_index_to_registration (mp->client_index);
1385 if (!reg)
1386 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001387
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001388 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001389 ;
1390}
Dave Barachb5e8a772016-12-06 12:04:42 -05001391
Florin Coras595992c2017-11-06 17:17:08 -08001392static void
1393 vl_api_ip_container_proxy_add_del_t_handler
1394 (vl_api_ip_container_proxy_add_del_t * mp)
1395{
1396 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1397 vnet_ip_container_proxy_args_t args;
1398 int rv = 0;
1399 clib_error_t *error;
1400
Dave Barachb7b92992018-10-17 10:38:51 -04001401 clib_memset (&args, 0, sizeof (args));
Neale Ranns37029302018-08-10 05:30:06 -07001402
1403 ip_prefix_decode (&mp->pfx, &args.prefix);
1404
Florin Coras595992c2017-11-06 17:17:08 -08001405 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1406 args.is_add = mp->is_add;
1407 if ((error = vnet_ip_container_proxy_add_del (&args)))
1408 {
1409 rv = clib_error_get_code (error);
1410 clib_error_report (error);
1411 }
1412
1413 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1414}
1415
Neale Ranns8f5fef22020-12-21 08:29:34 +00001416typedef struct ip_walk_ctx_t_
Matus Fabian75b9f452018-10-02 23:27:21 -07001417{
1418 vl_api_registration_t *reg;
1419 u32 context;
Neale Ranns8f5fef22020-12-21 08:29:34 +00001420} ip_walk_ctx_t;
Matus Fabian75b9f452018-10-02 23:27:21 -07001421
1422static int
1423ip_container_proxy_send_details (const fib_prefix_t * pfx, u32 sw_if_index,
1424 void *args)
1425{
1426 vl_api_ip_container_proxy_details_t *mp;
Neale Ranns8f5fef22020-12-21 08:29:34 +00001427 ip_walk_ctx_t *ctx = args;
Matus Fabian75b9f452018-10-02 23:27:21 -07001428
1429 mp = vl_msg_api_alloc (sizeof (*mp));
1430 if (!mp)
1431 return 1;
1432
Dave Barachb7b92992018-10-17 10:38:51 -04001433 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001434 mp->_vl_msg_id =
1435 ntohs (REPLY_MSG_ID_BASE + VL_API_IP_CONTAINER_PROXY_DETAILS);
Matus Fabian75b9f452018-10-02 23:27:21 -07001436 mp->context = ctx->context;
1437
1438 mp->sw_if_index = ntohl (sw_if_index);
1439 ip_prefix_encode (pfx, &mp->prefix);
1440
1441 vl_api_send_msg (ctx->reg, (u8 *) mp);
1442
1443 return 1;
1444}
1445
1446static void
1447vl_api_ip_container_proxy_dump_t_handler (vl_api_ip_container_proxy_dump_t *
1448 mp)
1449{
1450 vl_api_registration_t *reg;
1451
1452 reg = vl_api_client_index_to_registration (mp->client_index);
1453 if (!reg)
1454 return;
1455
Neale Ranns8f5fef22020-12-21 08:29:34 +00001456 ip_walk_ctx_t ctx = {
Matus Fabian75b9f452018-10-02 23:27:21 -07001457 .context = mp->context,
1458 .reg = reg,
1459 };
1460
1461 ip_container_proxy_walk (ip_container_proxy_send_details, &ctx);
1462}
1463
Neale Rannsb8d44812017-11-10 06:53:54 -08001464static void
1465vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1466{
1467 int rv = 0;
1468 vl_api_ioam_enable_reply_t *rmp;
1469 clib_error_t *error;
1470
1471 /* Ignoring the profile id as currently a single profile
1472 * is supported */
1473 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1474 mp->seqno, mp->analyse);
1475 if (error)
1476 {
1477 clib_error_report (error);
1478 rv = clib_error_get_code (error);
1479 }
1480
1481 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1482}
1483
1484static void
1485vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1486{
1487 int rv = 0;
1488 vl_api_ioam_disable_reply_t *rmp;
1489 clib_error_t *error;
1490
1491 error = clear_ioam_rewrite_fn ();
1492 if (error)
1493 {
1494 clib_error_report (error);
1495 rv = clib_error_get_code (error);
1496 }
1497
1498 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1499}
1500
1501static void
1502 vl_api_ip_source_and_port_range_check_add_del_t_handler
1503 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1504{
1505 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1506 int rv = 0;
1507
Neale Rannsb8d44812017-11-10 06:53:54 -08001508 u8 is_add = mp->is_add;
Neale Ranns37029302018-08-10 05:30:06 -07001509 fib_prefix_t pfx;
Neale Rannsb8d44812017-11-10 06:53:54 -08001510 u16 *low_ports = 0;
1511 u16 *high_ports = 0;
1512 u32 vrf_id;
1513 u16 tmp_low, tmp_high;
1514 u8 num_ranges;
1515 int i;
1516
Neale Ranns37029302018-08-10 05:30:06 -07001517 ip_prefix_decode (&mp->prefix, &pfx);
1518
Neale Rannsb8d44812017-11-10 06:53:54 -08001519 // Validate port range
1520 num_ranges = mp->number_of_ranges;
1521 if (num_ranges > 32)
1522 { // This is size of array in VPE.API
1523 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1524 goto reply;
1525 }
1526
1527 vec_reset_length (low_ports);
1528 vec_reset_length (high_ports);
1529
1530 for (i = 0; i < num_ranges; i++)
1531 {
1532 tmp_low = mp->low_ports[i];
1533 tmp_high = mp->high_ports[i];
1534 // If tmp_low <= tmp_high then only need to check tmp_low = 0
1535 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1536 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1537 {
1538 rv = VNET_API_ERROR_INVALID_VALUE;
1539 goto reply;
1540 }
1541 vec_add1 (low_ports, tmp_low);
1542 vec_add1 (high_ports, tmp_high + 1);
1543 }
1544
Neale Rannsb8d44812017-11-10 06:53:54 -08001545 vrf_id = ntohl (mp->vrf_id);
1546
1547 if (vrf_id < 1)
1548 {
1549 rv = VNET_API_ERROR_INVALID_VALUE;
1550 goto reply;
1551 }
1552
1553
Neale Ranns37029302018-08-10 05:30:06 -07001554 if (FIB_PROTOCOL_IP6 == pfx.fp_proto)
Neale Rannsb8d44812017-11-10 06:53:54 -08001555 {
Neale Ranns37029302018-08-10 05:30:06 -07001556 rv = ip6_source_and_port_range_check_add_del (&pfx.fp_addr.ip6,
1557 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08001558 vrf_id,
1559 low_ports,
1560 high_ports, is_add);
1561 }
1562 else
1563 {
Neale Ranns37029302018-08-10 05:30:06 -07001564 rv = ip4_source_and_port_range_check_add_del (&pfx.fp_addr.ip4,
1565 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08001566 vrf_id,
1567 low_ports,
1568 high_ports, is_add);
1569 }
1570
1571reply:
1572 vec_free (low_ports);
1573 vec_free (high_ports);
1574 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
1575}
1576
1577static void
1578 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
1579 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
1580{
1581 vlib_main_t *vm = vlib_get_main ();
1582 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
1583 ip4_main_t *im = &ip4_main;
1584 int rv;
1585 u32 sw_if_index;
1586 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1587 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1588 uword *p = 0;
1589 int i;
1590
1591 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
1592 ntohl (mp->tcp_out_vrf_id);
1593 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
1594 ntohl (mp->udp_out_vrf_id);
1595 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
1596 ntohl (mp->tcp_in_vrf_id);
1597 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
1598 ntohl (mp->udp_in_vrf_id);
1599
1600
1601 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
1602 {
1603 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
1604 {
1605 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
1606
1607 if (p == 0)
1608 {
1609 rv = VNET_API_ERROR_INVALID_VALUE;
1610 goto reply;
1611 }
1612
1613 fib_index[i] = p[0];
1614 }
1615 else
1616 fib_index[i] = ~0;
1617 }
1618 sw_if_index = ntohl (mp->sw_if_index);
1619
1620 VALIDATE_SW_IF_INDEX (mp);
1621
1622 rv =
1623 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
1624 mp->is_add);
1625
1626 BAD_SW_IF_INDEX_LABEL;
1627reply:
1628
1629 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
1630}
1631
Neale Rannscbe25aa2019-09-30 10:53:31 +00001632static void
1633 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1634 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1635{
1636 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1637 ip6_address_t ip;
1638 int rv;
1639
1640 VALIDATE_SW_IF_INDEX (mp);
1641
1642 ip6_address_decode (mp->ip, &ip);
1643
Neale Rannsec40a7d2020-04-23 07:36:12 +00001644 rv = ip6_link_set_local_address (ntohl (mp->sw_if_index), &ip);
Neale Rannscbe25aa2019-09-30 10:53:31 +00001645
1646 BAD_SW_IF_INDEX_LABEL;
1647 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1648}
1649
Neale Ranns9db6ada2019-11-08 12:42:31 +00001650static void
Benoît Ganne58a19152021-01-18 19:24:34 +01001651vl_api_sw_interface_ip6_get_link_local_address_t_handler (
1652 vl_api_sw_interface_ip6_get_link_local_address_t *mp)
1653{
1654 vl_api_sw_interface_ip6_get_link_local_address_reply_t *rmp;
1655 const ip6_address_t *ip = NULL;
1656 int rv = 0;
1657
1658 VALIDATE_SW_IF_INDEX (mp);
1659
1660 ip = ip6_get_link_local_address (ntohl (mp->sw_if_index));
1661 if (NULL == ip)
1662 rv = VNET_API_ERROR_IP6_NOT_ENABLED;
1663
1664 BAD_SW_IF_INDEX_LABEL;
1665 /* clang-format off */
1666 REPLY_MACRO2 (VL_API_SW_INTERFACE_IP6_GET_LINK_LOCAL_ADDRESS_REPLY,
1667 ({
1668 if (!rv)
1669 ip6_address_encode (ip, rmp->ip);
1670 }))
1671 /* clang-format on */
1672}
1673
1674static void
Neale Ranns9db6ada2019-11-08 12:42:31 +00001675vl_api_ip_table_replace_begin_t_handler (vl_api_ip_table_replace_begin_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -08001676{
Neale Ranns9db6ada2019-11-08 12:42:31 +00001677 vl_api_ip_table_replace_begin_reply_t *rmp;
1678 fib_protocol_t fproto;
1679 u32 fib_index;
1680 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001681
Neale Ranns9db6ada2019-11-08 12:42:31 +00001682 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1683 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
Neale Rannsb8d44812017-11-10 06:53:54 -08001684
Neale Ranns9db6ada2019-11-08 12:42:31 +00001685 if (INDEX_INVALID == fib_index)
1686 rv = VNET_API_ERROR_NO_SUCH_FIB;
1687 else
1688 {
Benoît Ganneff570d32024-04-16 09:36:05 +02001689 u32 mfib_index = mfib_table_find (fproto, ntohl (mp->table.table_id));
Neale Ranns9db6ada2019-11-08 12:42:31 +00001690 fib_table_mark (fib_index, fproto, FIB_SOURCE_API);
Benoît Ganneff570d32024-04-16 09:36:05 +02001691 if (mfib_index != INDEX_INVALID)
1692 mfib_table_mark (mfib_index, fproto, MFIB_SOURCE_API);
Neale Rannsb8d44812017-11-10 06:53:54 -08001693 }
Neale Ranns9db6ada2019-11-08 12:42:31 +00001694 REPLY_MACRO (VL_API_IP_TABLE_REPLACE_BEGIN_REPLY);
Neale Rannsb8d44812017-11-10 06:53:54 -08001695}
1696
1697static void
Neale Ranns9db6ada2019-11-08 12:42:31 +00001698vl_api_ip_table_replace_end_t_handler (vl_api_ip_table_replace_end_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -08001699{
Neale Ranns9db6ada2019-11-08 12:42:31 +00001700 vl_api_ip_table_replace_end_reply_t *rmp;
1701 fib_protocol_t fproto;
1702 u32 fib_index;
1703 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001704
Neale Ranns9db6ada2019-11-08 12:42:31 +00001705 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1706 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1707
1708 if (INDEX_INVALID == fib_index)
1709 rv = VNET_API_ERROR_NO_SUCH_FIB;
Neale Rannsb8d44812017-11-10 06:53:54 -08001710 else
Neale Ranns9db6ada2019-11-08 12:42:31 +00001711 {
Benoît Ganneff570d32024-04-16 09:36:05 +02001712 u32 mfib_index = mfib_table_find (fproto, ntohl (mp->table.table_id));
Neale Ranns9db6ada2019-11-08 12:42:31 +00001713 fib_table_sweep (fib_index, fproto, FIB_SOURCE_API);
Benoît Ganneff570d32024-04-16 09:36:05 +02001714 if (mfib_index != INDEX_INVALID)
1715 mfib_table_sweep (mfib_index, fproto, MFIB_SOURCE_API);
Neale Ranns9db6ada2019-11-08 12:42:31 +00001716 }
1717 REPLY_MACRO (VL_API_IP_TABLE_REPLACE_END_REPLY);
1718}
Neale Rannsb8d44812017-11-10 06:53:54 -08001719
Neale Ranns9db6ada2019-11-08 12:42:31 +00001720static void
1721vl_api_ip_table_flush_t_handler (vl_api_ip_table_flush_t * mp)
1722{
1723 vl_api_ip_table_flush_reply_t *rmp;
1724 fib_protocol_t fproto;
1725 u32 fib_index;
1726 int rv = 0;
1727
1728 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1729 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1730
1731 if (INDEX_INVALID == fib_index)
1732 rv = VNET_API_ERROR_NO_SUCH_FIB;
1733 else
1734 {
1735 vnet_main_t *vnm = vnet_get_main ();
1736 vnet_interface_main_t *im = &vnm->interface_main;
1737 vnet_sw_interface_t *si;
Benoît Ganneff570d32024-04-16 09:36:05 +02001738 u32 mfib_index;
Neale Ranns9db6ada2019-11-08 12:42:31 +00001739
1740 /* Shut down interfaces in this FIB / clean out intfc routes */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001741 pool_foreach (si, im->sw_interfaces)
1742 {
Neale Ranns9db6ada2019-11-08 12:42:31 +00001743 if (fib_index == fib_table_get_index_for_sw_if_index (fproto,
1744 si->sw_if_index))
1745 {
1746 u32 flags = si->flags;
1747 flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP;
1748 vnet_sw_interface_set_flags (vnm, si->sw_if_index, flags);
1749 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001750 }
Neale Ranns9db6ada2019-11-08 12:42:31 +00001751
1752 fib_table_flush (fib_index, fproto, FIB_SOURCE_API);
Benoît Ganneff570d32024-04-16 09:36:05 +02001753
1754 mfib_index = mfib_table_find (fproto, ntohl (mp->table.table_id));
1755 if (mfib_index != INDEX_INVALID)
1756 mfib_table_flush (mfib_index, fproto, MFIB_SOURCE_API);
Neale Ranns9db6ada2019-11-08 12:42:31 +00001757 }
1758
1759 REPLY_MACRO (VL_API_IP_TABLE_FLUSH_REPLY);
Neale Rannsb8d44812017-11-10 06:53:54 -08001760}
1761
Klement Sekera75e7d132017-09-20 08:26:30 +02001762void
1763vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
1764{
1765 vl_api_ip_reassembly_set_reply_t *rmp;
1766 int rv = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001767 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera75e7d132017-09-20 08:26:30 +02001768 {
Klement Sekerade34c352019-06-25 11:19:22 +00001769 case IP_REASS_TYPE_FULL:
1770 if (mp->is_ip6)
1771 {
1772 rv = ip6_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1773 clib_net_to_host_u32
1774 (mp->max_reassemblies),
1775 clib_net_to_host_u32
1776 (mp->max_reassembly_length),
1777 clib_net_to_host_u32
1778 (mp->expire_walk_interval_ms));
1779 }
1780 else
1781 {
1782 rv = ip4_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1783 clib_net_to_host_u32
1784 (mp->max_reassemblies),
1785 clib_net_to_host_u32
1786 (mp->max_reassembly_length),
1787 clib_net_to_host_u32
1788 (mp->expire_walk_interval_ms));
1789 }
1790 break;
1791 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1792 if (mp->is_ip6)
1793 {
1794 rv =
1795 ip6_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1796 clib_net_to_host_u32 (mp->max_reassemblies),
1797 clib_net_to_host_u32
1798 (mp->max_reassembly_length),
1799 clib_net_to_host_u32
1800 (mp->expire_walk_interval_ms));
1801 }
1802 else
1803 {
1804 rv = ip4_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1805 clib_net_to_host_u32 (mp->max_reassemblies),
1806 clib_net_to_host_u32
1807 (mp->max_reassembly_length),
1808 clib_net_to_host_u32
1809 (mp->expire_walk_interval_ms));
1810 }
1811 break;
Klement Sekera75e7d132017-09-20 08:26:30 +02001812 }
1813
1814 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
1815}
1816
1817void
1818vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
1819{
Ole Troan2e1c8962019-04-10 09:44:23 +02001820 vl_api_registration_t *rp;
Klement Sekera75e7d132017-09-20 08:26:30 +02001821
Ole Troan2e1c8962019-04-10 09:44:23 +02001822 rp = vl_api_client_index_to_registration (mp->client_index);
1823 if (rp == 0)
Klement Sekera75e7d132017-09-20 08:26:30 +02001824 return;
1825
1826 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001827 clib_memset (rmp, 0, sizeof (*rmp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001828 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_REASSEMBLY_GET_REPLY);
Klement Sekera75e7d132017-09-20 08:26:30 +02001829 rmp->context = mp->context;
1830 rmp->retval = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001831 u32 timeout_ms;
1832 u32 max_reassemblies;
1833 u32 max_reassembly_length;
1834 u32 expire_walk_interval_ms;
1835 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera75e7d132017-09-20 08:26:30 +02001836 {
Klement Sekerade34c352019-06-25 11:19:22 +00001837 case IP_REASS_TYPE_FULL:
1838 if (mp->is_ip6)
1839 {
1840 rmp->is_ip6 = 1;
1841 ip6_full_reass_get (&timeout_ms, &max_reassemblies,
1842 &max_reassembly_length,
1843 &expire_walk_interval_ms);
1844 }
1845 else
1846 {
1847 rmp->is_ip6 = 0;
1848 ip4_full_reass_get (&timeout_ms, &max_reassemblies,
1849 &max_reassembly_length,
1850 &expire_walk_interval_ms);
1851 }
1852 break;
1853 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1854 if (mp->is_ip6)
1855 {
1856 rmp->is_ip6 = 1;
1857 ip6_sv_reass_get (&timeout_ms, &max_reassemblies,
1858 &max_reassembly_length, &expire_walk_interval_ms);
1859 }
1860 else
1861 {
1862 rmp->is_ip6 = 0;
1863 ip4_sv_reass_get (&timeout_ms, &max_reassemblies,
1864 &max_reassembly_length, &expire_walk_interval_ms);
1865 }
1866 break;
Klement Sekera75e7d132017-09-20 08:26:30 +02001867 }
Matthew Smithb3174df2019-12-03 12:41:53 -06001868 rmp->timeout_ms = clib_host_to_net_u32 (timeout_ms);
1869 rmp->max_reassemblies = clib_host_to_net_u32 (max_reassemblies);
1870 rmp->max_reassembly_length = clib_host_to_net_u32 (max_reassembly_length);
Klement Sekera75e7d132017-09-20 08:26:30 +02001871 rmp->expire_walk_interval_ms =
Matthew Smithb3174df2019-12-03 12:41:53 -06001872 clib_host_to_net_u32 (expire_walk_interval_ms);
Ole Troan2e1c8962019-04-10 09:44:23 +02001873 vl_api_send_msg (rp, (u8 *) rmp);
Klement Sekera75e7d132017-09-20 08:26:30 +02001874}
1875
Klement Sekera4c533132018-02-22 11:41:12 +01001876void
1877 vl_api_ip_reassembly_enable_disable_t_handler
1878 (vl_api_ip_reassembly_enable_disable_t * mp)
1879{
1880 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
1881 int rv = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001882 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera4c533132018-02-22 11:41:12 +01001883 {
Klement Sekerade34c352019-06-25 11:19:22 +00001884 case IP_REASS_TYPE_FULL:
Klement Sekera896c8962019-06-24 11:52:49 +00001885 rv =
Klement Sekerade34c352019-06-25 11:19:22 +00001886 ip4_full_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1887 mp->enable_ip4);
1888 if (0 == rv)
1889 rv =
1890 ip6_full_reass_enable_disable (clib_net_to_host_u32
1891 (mp->sw_if_index), mp->enable_ip6);
1892 break;
1893 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1894 rv =
1895 ip4_sv_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1896 mp->enable_ip4);
1897 if (0 == rv)
1898 {
1899 rv =
1900 ip6_sv_reass_enable_disable (clib_net_to_host_u32
1901 (mp->sw_if_index), mp->enable_ip6);
1902 }
1903 break;
Klement Sekera4c533132018-02-22 11:41:12 +01001904 }
1905
Choree7f61d12018-11-28 10:27:58 +03301906 REPLY_MACRO (VL_API_IP_REASSEMBLY_ENABLE_DISABLE_REPLY);
Klement Sekera4c533132018-02-22 11:41:12 +01001907}
1908
Klement Sekera01c1fa42021-12-14 18:25:11 +00001909void
1910vl_api_ip_local_reass_enable_disable_t_handler (
1911 vl_api_ip_local_reass_enable_disable_t *mp)
1912{
1913 vl_api_ip_local_reass_enable_disable_reply_t *rmp;
1914 int rv = 0;
1915
1916 ip4_local_full_reass_enable_disable (mp->enable_ip4);
1917 ip6_local_full_reass_enable_disable (mp->enable_ip6);
1918
1919 REPLY_MACRO (VL_API_IP_LOCAL_REASS_ENABLE_DISABLE_REPLY);
1920}
1921
1922void
1923vl_api_ip_local_reass_get_t_handler (vl_api_ip_local_reass_get_t *mp)
1924{
1925 vl_api_ip_local_reass_get_reply_t *rmp;
1926 int rv = 0;
1927 REPLY_MACRO2 (VL_API_IP_LOCAL_REASS_GET, {
1928 rmp->ip4_is_enabled = ip4_local_full_reass_enabled ();
1929 rmp->ip6_is_enabled = ip6_local_full_reass_enabled ();
1930 });
1931}
1932
Neale Ranns92207752019-06-03 13:21:40 +00001933static walk_rc_t
1934send_ip_punt_redirect_details (u32 rx_sw_if_index,
1935 const ip_punt_redirect_rx_t * ipr, void *arg)
1936{
Pavel Kotucek609e1212018-11-27 09:59:44 +01001937 vl_api_ip_punt_redirect_details_t *mp;
Neale Ranns097fa662018-05-01 05:17:55 -07001938 fib_path_encode_ctx_t path_ctx = {
1939 .rpaths = NULL,
1940 };
Neale Ranns8f5fef22020-12-21 08:29:34 +00001941 ip_walk_ctx_t *ctx = arg;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001942
1943 mp = vl_msg_api_alloc (sizeof (*mp));
1944 if (!mp)
Neale Ranns92207752019-06-03 13:21:40 +00001945 return (WALK_STOP);;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001946
1947 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001948 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_PUNT_REDIRECT_DETAILS);
Neale Ranns92207752019-06-03 13:21:40 +00001949 mp->context = ctx->context;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001950
Neale Ranns097fa662018-05-01 05:17:55 -07001951 fib_path_list_walk_w_ext (ipr->pl, NULL, fib_path_encode, &path_ctx);
Neale Ranns92207752019-06-03 13:21:40 +00001952
1953 mp->punt.rx_sw_if_index = htonl (rx_sw_if_index);
Neale Ranns097fa662018-05-01 05:17:55 -07001954 mp->punt.tx_sw_if_index = htonl (path_ctx.rpaths[0].frp_sw_if_index);
Neale Ranns92207752019-06-03 13:21:40 +00001955
Neale Ranns097fa662018-05-01 05:17:55 -07001956 ip_address_encode (&path_ctx.rpaths[0].frp_addr,
Neale Ranns92207752019-06-03 13:21:40 +00001957 fib_proto_to_ip46 (ipr->fproto), &mp->punt.nh);
1958
1959 vl_api_send_msg (ctx->reg, (u8 *) mp);
1960
Neale Ranns097fa662018-05-01 05:17:55 -07001961 vec_free (path_ctx.rpaths);
Neale Ranns92207752019-06-03 13:21:40 +00001962
1963 return (WALK_CONTINUE);
Pavel Kotucek609e1212018-11-27 09:59:44 +01001964}
1965
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02001966static walk_rc_t
1967send_ip_punt_redirect_v2_details (u32 rx_sw_if_index,
1968 const ip_punt_redirect_rx_t *ipr, void *arg)
1969{
1970 vl_api_ip_punt_redirect_v2_details_t *mp;
1971 fib_path_encode_ctx_t path_ctx = {
1972 .rpaths = NULL,
1973 };
1974 fib_route_path_t *rpath;
1975 ip_walk_ctx_t *ctx = arg;
1976 vl_api_fib_path_t *fp;
1977 int n_paths;
1978
1979 fib_path_list_walk_w_ext (ipr->pl, NULL, fib_path_encode, &path_ctx);
1980
1981 n_paths = vec_len (path_ctx.rpaths);
1982 mp = vl_msg_api_alloc (sizeof (*mp) + n_paths * sizeof (*fp));
1983 if (!mp)
1984 return (WALK_STOP);
1985
1986 clib_memset (mp, 0, sizeof (*mp));
1987 mp->_vl_msg_id =
1988 ntohs (REPLY_MSG_ID_BASE + VL_API_IP_PUNT_REDIRECT_V2_DETAILS);
1989 mp->context = ctx->context;
1990 mp->punt.rx_sw_if_index = htonl (rx_sw_if_index);
1991 mp->punt.n_paths = htonl (n_paths);
1992 fp = mp->punt.paths;
1993 vec_foreach (rpath, path_ctx.rpaths)
1994 {
1995 fib_api_path_encode (rpath, fp);
1996 fp++;
1997 }
1998 mp->punt.af = (ipr->fproto == FIB_PROTOCOL_IP6) ? ADDRESS_IP6 : ADDRESS_IP4;
1999
2000 vl_api_send_msg (ctx->reg, (u8 *) mp);
2001
2002 vec_free (path_ctx.rpaths);
2003
2004 return (WALK_CONTINUE);
2005}
2006
2007static void
2008vl_api_ip_punt_redirect_dump_common (ip_walk_ctx_t *ctx, fib_protocol_t fproto,
2009 u32 rx_sw_if_index,
2010 ip_punt_redirect_walk_cb_t cb)
2011{
2012
2013 if ((u32) ~0 != rx_sw_if_index)
2014 {
2015 index_t pri;
2016 pri = ip_punt_redirect_find (fproto, rx_sw_if_index);
2017
2018 if (INDEX_INVALID == pri)
2019 return;
2020
2021 cb (rx_sw_if_index, ip_punt_redirect_get (pri), ctx);
2022 }
2023 else
2024 ip_punt_redirect_walk (fproto, cb, ctx);
2025}
2026
Pavel Kotucek609e1212018-11-27 09:59:44 +01002027static void
2028vl_api_ip_punt_redirect_dump_t_handler (vl_api_ip_punt_redirect_dump_t * mp)
2029{
2030 vl_api_registration_t *reg;
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02002031 fib_protocol_t fproto;
Pavel Kotucek609e1212018-11-27 09:59:44 +01002032
Pavel Kotucek609e1212018-11-27 09:59:44 +01002033 reg = vl_api_client_index_to_registration (mp->client_index);
2034 if (!reg)
2035 return;
2036
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02002037 fproto = (mp->is_ipv6 == 1) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
Pavel Kotucek609e1212018-11-27 09:59:44 +01002038
Neale Ranns8f5fef22020-12-21 08:29:34 +00002039 ip_walk_ctx_t ctx = {
Neale Ranns92207752019-06-03 13:21:40 +00002040 .reg = reg,
2041 .context = mp->context,
2042 };
2043
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02002044 vl_api_ip_punt_redirect_dump_common (&ctx, fproto, ntohl (mp->sw_if_index),
2045 send_ip_punt_redirect_details);
2046}
Neale Ranns92207752019-06-03 13:21:40 +00002047
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02002048static void
2049vl_api_ip_punt_redirect_v2_dump_t_handler (
2050 vl_api_ip_punt_redirect_v2_dump_t *mp)
2051{
2052 vl_api_registration_t *reg;
2053 ip_address_family_t af;
2054 fib_protocol_t fproto;
2055 int rv = 0;
Neale Ranns92207752019-06-03 13:21:40 +00002056
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02002057 reg = vl_api_client_index_to_registration (mp->client_index);
2058 if (!reg)
2059 return;
Neale Ranns92207752019-06-03 13:21:40 +00002060
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02002061 rv = ip_address_family_decode (mp->af, &af);
2062 if (rv != 0)
2063 return;
2064
2065 fproto = (af == AF_IP6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
2066
2067 ip_walk_ctx_t ctx = {
2068 .reg = reg,
2069 .context = mp->context,
2070 };
2071
2072 vl_api_ip_punt_redirect_dump_common (&ctx, fproto, ntohl (mp->sw_if_index),
2073 send_ip_punt_redirect_v2_details);
Pavel Kotucek609e1212018-11-27 09:59:44 +01002074}
2075
Neale Ranns8f5fef22020-12-21 08:29:34 +00002076void
2077vl_api_ip_path_mtu_update_t_handler (vl_api_ip_path_mtu_update_t *mp)
2078{
2079 vl_api_ip_path_mtu_update_reply_t *rmp;
2080 ip_address_t nh;
2081 int rv = 0;
2082
2083 ip_address_decode2 (&mp->pmtu.nh, &nh);
2084
2085 rv = ip_path_mtu_update (&nh, ntohl (mp->pmtu.table_id),
2086 ntohs (mp->pmtu.path_mtu));
2087
2088 REPLY_MACRO (VL_API_IP_PATH_MTU_UPDATE_REPLY);
2089}
2090
2091void
2092vl_api_ip_path_mtu_replace_begin_t_handler (
2093 vl_api_ip_path_mtu_replace_begin_t *mp)
2094{
2095 vl_api_ip_path_mtu_replace_begin_reply_t *rmp;
2096 int rv;
2097
2098 rv = ip_path_mtu_replace_begin ();
2099
2100 REPLY_MACRO (VL_API_IP_PATH_MTU_REPLACE_BEGIN_REPLY);
2101}
2102
2103void
2104vl_api_ip_path_mtu_replace_end_t_handler (vl_api_ip_path_mtu_replace_end_t *mp)
2105{
2106 vl_api_ip_path_mtu_replace_end_reply_t *rmp;
2107 int rv;
2108
2109 rv = ip_path_mtu_replace_end ();
2110
2111 REPLY_MACRO (VL_API_IP_PATH_MTU_REPLACE_END_REPLY);
2112}
2113
2114static void
2115send_ip_path_mtu_details (index_t ipti, vl_api_registration_t *rp, u32 context)
2116{
2117 vl_api_ip_path_mtu_details_t *rmp;
2118 ip_address_t ip;
2119 ip_pmtu_t *ipt;
2120
2121 ipt = ip_path_mtu_get (ipti);
2122
2123 REPLY_MACRO_DETAILS4 (VL_API_IP_PATH_MTU_DETAILS, rp, context, ({
2124 ip_pmtu_get_ip (ipt, &ip);
2125 ip_address_encode2 (&ip, &rmp->pmtu.nh);
2126 rmp->pmtu.table_id =
2127 htonl (ip_pmtu_get_table_id (ipt));
2128 rmp->pmtu.path_mtu = htons (ipt->ipt_cfg_pmtu);
2129 }));
2130}
2131
2132static void
2133vl_api_ip_path_mtu_get_t_handler (vl_api_ip_path_mtu_get_t *mp)
2134{
2135 vl_api_ip_path_mtu_get_reply_t *rmp;
2136 i32 rv = 0;
2137
2138 REPLY_AND_DETAILS_MACRO (
2139 VL_API_IP_PATH_MTU_GET_REPLY, ip_pmtu_pool,
2140 ({ send_ip_path_mtu_details (cursor, rp, mp->context); }));
2141}
2142
Filip Tehlar5ff59a12021-06-23 14:38:38 +00002143#include <vnet/ip/ip.api.c>
Dave Barachb5e8a772016-12-06 12:04:42 -05002144
2145static clib_error_t *
2146ip_api_hookup (vlib_main_t * vm)
2147{
Dave Barach39d69112019-11-27 11:42:13 -05002148 api_main_t *am = vlibapi_get_main ();
Dave Barachb5e8a772016-12-06 12:04:42 -05002149
Dave Barachb5e8a772016-12-06 12:04:42 -05002150 /*
2151 * Set up the (msg_name, crc, message-id) table
2152 */
Filip Tehlar5ff59a12021-06-23 14:38:38 +00002153 REPLY_MSG_ID_BASE = setup_message_id_table ();
Dave Barachb5e8a772016-12-06 12:04:42 -05002154
Vladislav Grishenkoe7c57c42023-03-19 02:57:01 +05002155 /*
2156 * Mark the route add/del API as MP safe
2157 */
2158 vl_api_set_msg_thread_safe (am, REPLY_MSG_ID_BASE + VL_API_IP_ROUTE_ADD_DEL,
2159 1);
2160 vl_api_set_msg_thread_safe (
2161 am, REPLY_MSG_ID_BASE + VL_API_IP_ROUTE_ADD_DEL_REPLY, 1);
2162 vl_api_set_msg_thread_safe (
2163 am, REPLY_MSG_ID_BASE + VL_API_IP_ROUTE_ADD_DEL_V2, 1);
2164 vl_api_set_msg_thread_safe (
2165 am, REPLY_MSG_ID_BASE + VL_API_IP_ROUTE_ADD_DEL_V2_REPLY, 1);
Vladislav Grishenkof876b692021-07-28 23:33:00 +05002166 vl_api_set_msg_thread_safe (am, REPLY_MSG_ID_BASE + VL_API_IP_ADDRESS_DUMP,
2167 1);
Vladislav Grishenkoe7c57c42023-03-19 02:57:01 +05002168
Dave Barachb5e8a772016-12-06 12:04:42 -05002169 return 0;
2170}
2171
2172VLIB_API_INIT_FUNCTION (ip_api_hookup);
2173
2174/*
2175 * fd.io coding-style-patch-verification: ON
2176 *
2177 * Local Variables:
2178 * eval: (c-set-style "gnu")
2179 * End:
2180 */