blob: f9f9ac783d9c523e70548251b59c7e8f9d50941e [file] [log] [blame]
Dave Barachb5e8a772016-12-06 12:04:42 -05001/*
2 *------------------------------------------------------------------
3 * ip_api.c - vnet ip api
4 *
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <vnet/vnet.h>
21#include <vlibmemory/api.h>
22
23#include <vnet/interface.h>
24#include <vnet/api_errno.h>
25#include <vnet/ethernet/ethernet.h>
Neale Ranns37029302018-08-10 05:30:06 -070026#include <vnet/ethernet/ethernet_types_api.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050027#include <vnet/ip/ip.h>
Neale Ranns37029302018-08-10 05:30:06 -070028#include <vnet/ip/ip_types_api.h>
Pavel Kotucek609e1212018-11-27 09:59:44 +010029#include <vnet/ip/ip_punt_drop.h>
Neale Ranns097fa662018-05-01 05:17:55 -070030#include <vnet/ip/ip_types_api.h>
Neale Ranns8f5fef22020-12-21 08:29:34 +000031#include <vnet/ip/ip_path_mtu.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050032#include <vnet/fib/fib_table.h>
33#include <vnet/fib/fib_api.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050034#include <vnet/ethernet/arp_packet.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080035#include <vnet/mfib/ip6_mfib.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000036#include <vnet/mfib/ip4_mfib.h>
37#include <vnet/mfib/mfib_signal.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080038#include <vnet/mfib/mfib_entry.h>
Neale Ranns097fa662018-05-01 05:17:55 -070039#include <vnet/mfib/mfib_api.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080040#include <vnet/ip/ip_source_and_port_range_check.h>
Neale Ranns92207752019-06-03 13:21:40 +000041#include <vnet/fib/fib_path_list.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080042#include <vnet/ip/ip6_hop_by_hop.h>
Neale Rannscbe25aa2019-09-30 10:53:31 +000043#include <vnet/ip/ip6_link.h>
Klement Sekerade34c352019-06-25 11:19:22 +000044#include <vnet/ip/reass/ip4_sv_reass.h>
Klement Sekera896c8962019-06-24 11:52:49 +000045#include <vnet/ip/reass/ip4_full_reass.h>
Klement Sekerade34c352019-06-25 11:19:22 +000046#include <vnet/ip/reass/ip6_sv_reass.h>
Klement Sekera896c8962019-06-24 11:52:49 +000047#include <vnet/ip/reass/ip6_full_reass.h>
Steven Luongc3ed1c92020-07-27 10:06:58 -070048#include <vnet/ip/ip_table.h>
Neale Rannse4031132020-10-26 13:00:06 +000049#include <vnet/ip/ip_container_proxy.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050050
Ole Troan33a58172019-09-04 09:12:29 +020051#include <vnet/format_fns.h>
Filip Tehlar5ff59a12021-06-23 14:38:38 +000052#include <vnet/ip/ip.api_enum.h>
53#include <vnet/ip/ip.api_types.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080054
Filip Tehlar5ff59a12021-06-23 14:38:38 +000055#define REPLY_MSG_ID_BASE ip4_main.msg_id_base
56#include <vlibapi/api_helper_macros.h>
Pavel Kotucek609e1212018-11-27 09:59:44 +010057
Dave Barachb5e8a772016-12-06 12:04:42 -050058static void
Neale Rannscbe25aa2019-09-30 10:53:31 +000059 vl_api_sw_interface_ip6_enable_disable_t_handler
60 (vl_api_sw_interface_ip6_enable_disable_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -050061{
Neale Rannscbe25aa2019-09-30 10:53:31 +000062 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
63 int rv = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -050064
Neale Rannscbe25aa2019-09-30 10:53:31 +000065 VALIDATE_SW_IF_INDEX (mp);
Neale Ranns37029302018-08-10 05:30:06 -070066
Neale Rannscbe25aa2019-09-30 10:53:31 +000067 rv = ((mp->enable == 1) ?
Neale Rannsec40a7d2020-04-23 07:36:12 +000068 ip6_link_enable (ntohl (mp->sw_if_index), NULL) :
Neale Rannscbe25aa2019-09-30 10:53:31 +000069 ip6_link_disable (ntohl (mp->sw_if_index)));
Dave Barachb5e8a772016-12-06 12:04:42 -050070
Neale Rannscbe25aa2019-09-30 10:53:31 +000071 BAD_SW_IF_INDEX_LABEL;
Dave Barachb5e8a772016-12-06 12:04:42 -050072
Neale Rannscbe25aa2019-09-30 10:53:31 +000073 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
Dave Barachb5e8a772016-12-06 12:04:42 -050074}
75
Dave Barachb5e8a772016-12-06 12:04:42 -050076static void
Neale Ranns097fa662018-05-01 05:17:55 -070077send_ip_table_details (vpe_api_main_t * am,
78 vl_api_registration_t * reg,
79 u32 context, const fib_table_t * table)
Dave Barachb5e8a772016-12-06 12:04:42 -050080{
Neale Ranns097fa662018-05-01 05:17:55 -070081 vl_api_ip_table_details_t *mp;
Dave Barachb5e8a772016-12-06 12:04:42 -050082
Neale Ranns097fa662018-05-01 05:17:55 -070083 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -050084 if (!mp)
85 return;
Dave Barachb7b92992018-10-17 10:38:51 -040086 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +000087 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_TABLE_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -050088 mp->context = context;
89
Neale Ranns097fa662018-05-01 05:17:55 -070090 mp->table.is_ip6 = (table->ft_proto == FIB_PROTOCOL_IP6);
91 mp->table.table_id = htonl (table->ft_table_id);
92 memcpy (mp->table.name, table->ft_desc,
93 clib_min (vec_len (table->ft_desc), sizeof (mp->table.name)));
Dave Barachb5e8a772016-12-06 12:04:42 -050094
Florin Coras6c4dae22018-01-09 06:39:23 -080095 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -050096}
97
Neale Ranns097fa662018-05-01 05:17:55 -070098static void
99vl_api_ip_table_dump_t_handler (vl_api_ip_table_dump_t * mp)
100{
101 vpe_api_main_t *am = &vpe_api_main;
102 vl_api_registration_t *reg;
103 fib_table_t *fib_table;
104
105 reg = vl_api_client_index_to_registration (mp->client_index);
106 if (!reg)
107 return;
108
109 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100110 pool_foreach (fib_table, ip4_main.fibs)
111 {
Neale Ranns097fa662018-05-01 05:17:55 -0700112 send_ip_table_details(am, reg, mp->context, fib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100113 }
114 pool_foreach (fib_table, ip6_main.fibs)
115 {
Neale Ranns097fa662018-05-01 05:17:55 -0700116 /* don't send link locals */
117 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
118 continue;
119 send_ip_table_details(am, reg, mp->context, fib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100120 }
Neale Ranns097fa662018-05-01 05:17:55 -0700121 /* *INDENT-ON* */
122}
123
Neale Rannsa3af3372017-03-28 03:49:52 -0700124typedef struct vl_api_ip_fib_dump_walk_ctx_t_
125{
126 fib_node_index_t *feis;
127} vl_api_ip_fib_dump_walk_ctx_t;
128
Neale Ranns89541992017-04-06 04:41:02 -0700129static fib_table_walk_rc_t
Neale Rannsa3af3372017-03-28 03:49:52 -0700130vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
131{
132 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
133
134 vec_add1 (ctx->feis, fei);
135
Neale Ranns89541992017-04-06 04:41:02 -0700136 return (FIB_TABLE_WALK_CONTINUE);
Neale Rannsa3af3372017-03-28 03:49:52 -0700137}
138
Dave Barachb5e8a772016-12-06 12:04:42 -0500139static void
Neale Ranns097fa662018-05-01 05:17:55 -0700140send_ip_route_details (vpe_api_main_t * am,
141 vl_api_registration_t * reg,
142 u32 context, fib_node_index_t fib_entry_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500143{
Neale Ranns097fa662018-05-01 05:17:55 -0700144 fib_route_path_t *rpaths, *rpath;
145 vl_api_ip_route_details_t *mp;
Neale Rannsc5d43172018-07-30 08:04:40 -0700146 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500147 vl_api_fib_path_t *fp;
148 int path_count;
149
Neale Ranns097fa662018-05-01 05:17:55 -0700150 rpaths = NULL;
151 pfx = fib_entry_get_prefix (fib_entry_index);
152 rpaths = fib_entry_encode (fib_entry_index);
153
154 path_count = vec_len (rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500155 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
156 if (!mp)
157 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400158 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +0000159 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_ROUTE_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -0500160 mp->context = context;
161
Neale Ranns097fa662018-05-01 05:17:55 -0700162 ip_prefix_encode (pfx, &mp->route.prefix);
163 mp->route.table_id =
164 htonl (fib_table_get_table_id
165 (fib_entry_get_fib_index (fib_entry_index), pfx->fp_proto));
166 mp->route.n_paths = path_count;
167 mp->route.stats_index =
168 htonl (fib_table_entry_get_stats_index
169 (fib_entry_get_fib_index (fib_entry_index), pfx));
Dave Barachb5e8a772016-12-06 12:04:42 -0500170
Neale Ranns097fa662018-05-01 05:17:55 -0700171 fp = mp->route.paths;
172 vec_foreach (rpath, rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500173 {
Neale Ranns097fa662018-05-01 05:17:55 -0700174 fib_api_path_encode (rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500175 fp++;
176 }
177
Florin Coras6c4dae22018-01-09 06:39:23 -0800178 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns9db6ada2019-11-08 12:42:31 +0000179 vec_free (rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500180}
181
Neale Ranns976b2592019-12-04 06:11:00 +0000182static void
183send_ip_route_v2_details (vpe_api_main_t *am, vl_api_registration_t *reg,
184 u32 context, fib_node_index_t fib_entry_index)
185{
186 fib_route_path_t *rpaths, *rpath;
187 vl_api_ip_route_v2_details_t *mp;
188 const fib_prefix_t *pfx;
189 vl_api_fib_path_t *fp;
190 int path_count;
191
192 rpaths = NULL;
193 pfx = fib_entry_get_prefix (fib_entry_index);
194 rpaths = fib_entry_encode (fib_entry_index);
195
196 path_count = vec_len (rpaths);
197 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
198 if (!mp)
199 return;
200 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +0000201 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_ROUTE_V2_DETAILS);
Neale Ranns976b2592019-12-04 06:11:00 +0000202 mp->context = context;
203
204 ip_prefix_encode (pfx, &mp->route.prefix);
205 mp->route.table_id = htonl (fib_table_get_table_id (
206 fib_entry_get_fib_index (fib_entry_index), pfx->fp_proto));
207 mp->route.n_paths = path_count;
208 mp->route.src = fib_entry_get_best_source (fib_entry_index);
209 mp->route.stats_index = htonl (fib_table_entry_get_stats_index (
210 fib_entry_get_fib_index (fib_entry_index), pfx));
211
212 fp = mp->route.paths;
213 vec_foreach (rpath, rpaths)
214 {
215 fib_api_path_encode (rpath, fp);
216 fp++;
217 }
218
219 vl_api_send_msg (reg, (u8 *) mp);
220 vec_free (rpaths);
221}
222
Dave Barachd7cb1b52016-12-09 09:52:16 -0500223typedef struct apt_ip6_fib_show_ctx_t_
224{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500225 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500226} api_ip6_fib_show_ctx_t;
227
Dave Barachb5e8a772016-12-06 12:04:42 -0500228static void
Neale Ranns097fa662018-05-01 05:17:55 -0700229vl_api_ip_route_dump_t_handler (vl_api_ip_route_dump_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500230{
231 vpe_api_main_t *am = &vpe_api_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500232 fib_node_index_t *fib_entry_index;
Florin Coras6c4dae22018-01-09 06:39:23 -0800233 vl_api_registration_t *reg;
Neale Ranns097fa662018-05-01 05:17:55 -0700234 fib_protocol_t fproto;
235 u32 fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -0500236
Florin Coras6c4dae22018-01-09 06:39:23 -0800237 reg = vl_api_client_index_to_registration (mp->client_index);
238 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500239 return;
240
Neale Ranns097fa662018-05-01 05:17:55 -0700241 vl_api_ip_fib_dump_walk_ctx_t ctx = {
242 .feis = NULL,
243 };
Neale Ranns81458422018-03-12 06:59:36 -0700244
Neale Ranns097fa662018-05-01 05:17:55 -0700245 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
246 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
247
248 if (INDEX_INVALID == fib_index)
249 return;
250
251 fib_table_walk (fib_index, fproto, vl_api_ip_fib_dump_walk, &ctx);
252
253 vec_foreach (fib_entry_index, ctx.feis)
254 {
255 send_ip_route_details (am, reg, mp->context, *fib_entry_index);
256 }
257
258 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500259}
260
261static void
Neale Ranns976b2592019-12-04 06:11:00 +0000262vl_api_ip_route_v2_dump_t_handler (vl_api_ip_route_v2_dump_t *mp)
263{
264 vpe_api_main_t *am = &vpe_api_main;
265 fib_node_index_t *fib_entry_index;
266 vl_api_registration_t *reg;
267 fib_protocol_t fproto;
268 fib_source_t src;
269 u32 fib_index;
270
271 reg = vl_api_client_index_to_registration (mp->client_index);
272 if (!reg)
273 return;
274
275 vl_api_ip_fib_dump_walk_ctx_t ctx = {
276 .feis = NULL,
277 };
278
279 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
280 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
281 src = mp->src;
282
283 if (INDEX_INVALID == fib_index)
284 return;
285
286 if (src)
287 fib_table_walk_w_src (fib_index, fproto, src, vl_api_ip_fib_dump_walk,
288 &ctx);
289 else
290 fib_table_walk (fib_index, fproto, vl_api_ip_fib_dump_walk, &ctx);
291
292 vec_foreach (fib_entry_index, ctx.feis)
293 {
294 send_ip_route_v2_details (am, reg, mp->context, *fib_entry_index);
295 }
296
297 vec_free (ctx.feis);
298}
299
300static void
Neale Ranns097fa662018-05-01 05:17:55 -0700301send_ip_mtable_details (vl_api_registration_t * reg,
302 u32 context, const mfib_table_t * mfib_table)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800303{
Neale Ranns097fa662018-05-01 05:17:55 -0700304 vl_api_ip_mtable_details_t *mp;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800305
Neale Ranns097fa662018-05-01 05:17:55 -0700306 mp = vl_msg_api_alloc (sizeof (*mp));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800307 if (!mp)
308 return;
Neale Ranns097fa662018-05-01 05:17:55 -0700309 memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +0000310 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_MTABLE_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800311 mp->context = context;
312
Neale Ranns097fa662018-05-01 05:17:55 -0700313 mp->table.table_id = htonl (mfib_table->mft_table_id);
314 mp->table.is_ip6 = (FIB_PROTOCOL_IP6 == mfib_table->mft_proto);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800315
Florin Coras6c4dae22018-01-09 06:39:23 -0800316 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800317}
318
Neale Ranns5a8123b2017-01-26 01:18:23 -0800319static void
Neale Ranns097fa662018-05-01 05:17:55 -0700320vl_api_ip_mtable_dump_t_handler (vl_api_ip_mtable_dump_t * mp)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800321{
Florin Coras6c4dae22018-01-09 06:39:23 -0800322 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800323 mfib_table_t *mfib_table;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800324
Florin Coras6c4dae22018-01-09 06:39:23 -0800325 reg = vl_api_client_index_to_registration (mp->client_index);
326 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800327 return;
328
Neale Ranns5a8123b2017-01-26 01:18:23 -0800329 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100330 pool_foreach (mfib_table, ip4_main.mfibs)
331 {
Neale Ranns097fa662018-05-01 05:17:55 -0700332 send_ip_mtable_details (reg, mp->context, mfib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100333 }
334 pool_foreach (mfib_table, ip6_main.mfibs)
335 {
Neale Ranns097fa662018-05-01 05:17:55 -0700336 send_ip_mtable_details (reg, mp->context, mfib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100337 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800338 /* *INDENT-ON* */
Neale Ranns5a8123b2017-01-26 01:18:23 -0800339}
340
Neale Ranns097fa662018-05-01 05:17:55 -0700341typedef struct vl_api_ip_mfib_dump_ctx_t_
Neale Ranns5a8123b2017-01-26 01:18:23 -0800342{
343 fib_node_index_t *entries;
Neale Ranns097fa662018-05-01 05:17:55 -0700344} vl_api_ip_mfib_dump_ctx_t;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800345
Neale Ranns9db6ada2019-11-08 12:42:31 +0000346static walk_rc_t
Neale Ranns097fa662018-05-01 05:17:55 -0700347mfib_route_dump_walk (fib_node_index_t fei, void *arg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800348{
Neale Ranns097fa662018-05-01 05:17:55 -0700349 vl_api_ip_mfib_dump_ctx_t *ctx = arg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800350
351 vec_add1 (ctx->entries, fei);
352
Neale Ranns9db6ada2019-11-08 12:42:31 +0000353 return (WALK_CONTINUE);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800354}
355
356static void
Neale Ranns097fa662018-05-01 05:17:55 -0700357send_ip_mroute_details (vpe_api_main_t * am,
358 vl_api_registration_t * reg,
359 u32 context, fib_node_index_t mfib_entry_index)
360{
361 fib_route_path_t *rpaths, *rpath;
362 vl_api_ip_mroute_details_t *mp;
363 const mfib_prefix_t *pfx;
364 vl_api_mfib_path_t *fp;
Matthew Smitha2bbeb82020-09-17 16:33:10 -0500365 u8 path_count;
Neale Ranns097fa662018-05-01 05:17:55 -0700366
367 rpaths = NULL;
368 pfx = mfib_entry_get_prefix (mfib_entry_index);
369 rpaths = mfib_entry_encode (mfib_entry_index);
370
371 path_count = vec_len (rpaths);
372 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
373 if (!mp)
374 return;
375 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +0000376 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_MROUTE_DETAILS);
Neale Ranns097fa662018-05-01 05:17:55 -0700377 mp->context = context;
378
379 ip_mprefix_encode (pfx, &mp->route.prefix);
380 mp->route.table_id =
381 htonl (mfib_table_get_table_id
382 (mfib_entry_get_fib_index (mfib_entry_index), pfx->fp_proto));
Matthew Smitha2bbeb82020-09-17 16:33:10 -0500383 mp->route.n_paths = path_count;
Neale Ranns097fa662018-05-01 05:17:55 -0700384 fp = mp->route.paths;
385 vec_foreach (rpath, rpaths)
386 {
387 mfib_api_path_encode (rpath, fp);
388 fp++;
389 }
390
391 vl_api_send_msg (reg, (u8 *) mp);
392 vec_free (rpaths);
393}
394
395static void
396vl_api_ip_mroute_dump_t_handler (vl_api_ip_mroute_dump_t * mp)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800397{
398 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800399 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800400 fib_node_index_t *mfeip;
Neale Ranns097fa662018-05-01 05:17:55 -0700401 fib_protocol_t fproto;
402 u32 fib_index;
403
404 vl_api_ip_mfib_dump_ctx_t ctx = {
Neale Ranns5a8123b2017-01-26 01:18:23 -0800405 .entries = NULL,
406 };
407
Florin Coras6c4dae22018-01-09 06:39:23 -0800408 reg = vl_api_client_index_to_registration (mp->client_index);
409 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800410 return;
411
Neale Ranns097fa662018-05-01 05:17:55 -0700412 fproto = fib_ip_proto (mp->table.is_ip6);
413 fib_index = mfib_table_find (fproto, ntohl (mp->table.table_id));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800414
Neale Ranns097fa662018-05-01 05:17:55 -0700415 if (INDEX_INVALID == fib_index)
416 return;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800417
Neale Ranns097fa662018-05-01 05:17:55 -0700418 mfib_table_walk (fib_index, fproto, mfib_route_dump_walk, &ctx);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800419
Neale Ranns097fa662018-05-01 05:17:55 -0700420 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800421
Neale Ranns097fa662018-05-01 05:17:55 -0700422 vec_foreach (mfeip, ctx.entries)
423 {
424 send_ip_mroute_details (am, reg, mp->context, *mfeip);
425 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800426
427 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800428}
429
430static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700431vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
432 vlib_main_t * vm)
433{
434 vl_api_ip_punt_police_reply_t *rmp;
435 int rv = 0;
436
437 if (mp->is_ip6)
438 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
439 else
440 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
441
442 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
443}
444
445static void
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +0200446ip_punt_redirect_t_handler_common (u8 is_add, u32 rx_sw_if_index,
447 ip_address_family_t af,
448 const fib_route_path_t *rpaths)
449{
450 if (is_add)
451 {
452 if (af == AF_IP6)
453 ip6_punt_redirect_add_paths (rx_sw_if_index, rpaths);
454 else if (af == AF_IP4)
455 ip4_punt_redirect_add_paths (rx_sw_if_index, rpaths);
456 }
457 else
458 {
459 if (af == AF_IP6)
460 ip6_punt_redirect_del (rx_sw_if_index);
461 else if (af == AF_IP4)
462 ip4_punt_redirect_del (rx_sw_if_index);
463 }
464}
465
466static void
467vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t *mp,
468 vlib_main_t *vm)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700469{
470 vl_api_ip_punt_redirect_reply_t *rmp;
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +0200471 fib_route_path_t *rpaths = NULL, rpath = {
472 .frp_weight = 1,
473 .frp_fib_index = ~0,
474 };
475 ip_address_family_t af;
Pavel Kotucek609e1212018-11-27 09:59:44 +0100476 ip46_type_t ipv;
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +0200477 u32 rx_sw_if_index;
478 int rv = 0;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700479
Pavel Kotucek609e1212018-11-27 09:59:44 +0100480 if (!vnet_sw_if_index_is_api_valid (ntohl (mp->punt.tx_sw_if_index)))
481 goto bad_sw_if_index;
482
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +0200483 ipv = ip_address_decode (&mp->punt.nh, &rpath.frp_addr);
484 af = (ipv == IP46_TYPE_IP6) ? AF_IP6 : AF_IP4;
485 rpath.frp_proto = (ipv == IP46_TYPE_IP6) ? DPO_PROTO_IP6 : DPO_PROTO_IP4;
486 rpath.frp_sw_if_index = ntohl (mp->punt.tx_sw_if_index);
487 rx_sw_if_index = ntohl (mp->punt.rx_sw_if_index);
488
489 vec_add1 (rpaths, rpath);
490 ip_punt_redirect_t_handler_common (mp->is_add, rx_sw_if_index, af, rpaths);
491 vec_free (rpaths);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700492
Pavel Kotucek609e1212018-11-27 09:59:44 +0100493 BAD_SW_IF_INDEX_LABEL;
494
Neale Rannsd91c1db2017-07-31 02:30:50 -0700495 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
496}
497
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +0200498static void
499vl_api_add_del_ip_punt_redirect_v2_t_handler (
500 vl_api_add_del_ip_punt_redirect_v2_t *mp, vlib_main_t *vm)
501{
502 vl_api_add_del_ip_punt_redirect_v2_reply_t *rmp;
503 fib_route_path_t *rpaths = NULL, *rpath;
504 vl_api_fib_path_t *apath;
505 ip_address_family_t af;
506 u32 rx_sw_if_index, n_paths;
507 int rv = 0, ii;
508
509 rx_sw_if_index = ntohl (mp->punt.rx_sw_if_index);
510 n_paths = ntohl (mp->punt.n_paths);
511
512 rv = ip_address_family_decode (mp->punt.af, &af);
513 if (rv != 0)
514 goto out;
515
516 if (0 != n_paths)
517 vec_validate (rpaths, n_paths - 1);
518
519 for (ii = 0; ii < n_paths; ii++)
520 {
521 apath = &mp->punt.paths[ii];
522 rpath = &rpaths[ii];
523
524 rv = fib_api_path_decode (apath, rpath);
525
526 if (rv != 0)
527 goto out;
528 }
529
530 ip_punt_redirect_t_handler_common (mp->is_add, rx_sw_if_index, af, rpaths);
531
532out:
533 vec_free (rpaths);
534
535 REPLY_MACRO (VL_API_ADD_DEL_IP_PUNT_REDIRECT_V2_REPLY);
536}
537
Steven Luongc3ed1c92020-07-27 10:06:58 -0700538static clib_error_t *
539call_elf_section_ip_table_callbacks (vnet_main_t * vnm, u32 table_id,
540 u32 flags,
541 _vnet_ip_table_function_list_elt_t **
542 elts)
543{
544 _vnet_ip_table_function_list_elt_t *elt;
545 vnet_ip_table_function_priority_t prio;
546 clib_error_t *error = 0;
547
548 for (prio = VNET_IP_TABLE_FUNC_PRIORITY_LOW;
549 prio <= VNET_IP_TABLE_FUNC_PRIORITY_HIGH; prio++)
550 {
551 elt = elts[prio];
552
553 while (elt)
554 {
555 error = elt->fp (vnm, table_id, flags);
556 if (error)
557 return error;
558 elt = elt->next_ip_table_function;
559 }
560 }
561 return error;
562}
563
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700564void
Neale Ranns15002542017-09-10 04:39:11 -0700565ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
566{
567 u32 fib_index, mfib_index;
Steven Luongc3ed1c92020-07-27 10:06:58 -0700568 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns15002542017-09-10 04:39:11 -0700569
570 /*
571 * ignore action on the default table - this is always present
572 * and cannot be added nor deleted from the API
573 */
574 if (0 != table_id)
575 {
576 /*
577 * The API holds only one lock on the table.
578 * i.e. it can be added many times via the API but needs to be
579 * deleted only once.
580 * The FIB index for unicast and multicast is not necessarily the
581 * same, since internal VPP systesm (like LISP and SR) create
582 * their own unicast tables.
583 */
584 fib_index = fib_table_find (fproto, table_id);
585 mfib_index = mfib_table_find (fproto, table_id);
586
Steven Luongc3ed1c92020-07-27 10:06:58 -0700587 if ((~0 != fib_index) || (~0 != mfib_index))
588 call_elf_section_ip_table_callbacks (vnm, table_id, 0 /* is_add */ ,
589 vnm->ip_table_add_del_functions);
590
Neale Ranns15002542017-09-10 04:39:11 -0700591 if (~0 != fib_index)
592 {
593 fib_table_unlock (fib_index, fproto,
594 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
595 }
596 if (~0 != mfib_index)
597 {
598 mfib_table_unlock (mfib_index, fproto,
599 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
600 }
601 }
602}
603
604void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700605vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
606{
607 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns097fa662018-05-01 05:17:55 -0700608 fib_protocol_t fproto = (mp->table.is_ip6 ?
609 FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
610 u32 table_id = ntohl (mp->table.table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700611 int rv = 0;
612
Neale Ranns15002542017-09-10 04:39:11 -0700613 if (mp->is_add)
614 {
Neale Ranns097fa662018-05-01 05:17:55 -0700615 ip_table_create (fproto, table_id, 1, mp->table.name);
Neale Ranns15002542017-09-10 04:39:11 -0700616 }
617 else
618 {
619 ip_table_delete (fproto, table_id, 1);
620 }
621
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700622 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
623}
624
Dave Barachb5e8a772016-12-06 12:04:42 -0500625static int
Neale Ranns097fa662018-05-01 05:17:55 -0700626ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp, u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500627{
Neale Ranns097fa662018-05-01 05:17:55 -0700628 fib_route_path_t *rpaths = NULL, *rpath;
629 fib_entry_flag_t entry_flags;
630 vl_api_fib_path_t *apath;
631 fib_prefix_t pfx;
632 u32 fib_index;
633 int rv, ii;
Dave Barachb5e8a772016-12-06 12:04:42 -0500634
Neale Ranns097fa662018-05-01 05:17:55 -0700635 entry_flags = FIB_ENTRY_FLAG_NONE;
636 ip_prefix_decode (&mp->route.prefix, &pfx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500637
Neale Ranns097fa662018-05-01 05:17:55 -0700638 rv = fib_api_table_id_decode (pfx.fp_proto,
639 ntohl (mp->route.table_id), &fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500640 if (0 != rv)
Neale Ranns097fa662018-05-01 05:17:55 -0700641 goto out;
Dave Barachb5e8a772016-12-06 12:04:42 -0500642
Neale Rannsc2ac2352019-07-02 14:33:29 +0000643 if (0 != mp->route.n_paths)
644 vec_validate (rpaths, mp->route.n_paths - 1);
Neale Ranns097fa662018-05-01 05:17:55 -0700645
646 for (ii = 0; ii < mp->route.n_paths; ii++)
647 {
648 apath = &mp->route.paths[ii];
649 rpath = &rpaths[ii];
650
651 rv = fib_api_path_decode (apath, rpath);
652
653 if ((rpath->frp_flags & FIB_ROUTE_PATH_LOCAL) &&
654 (~0 == rpath->frp_sw_if_index))
655 entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
656
657 if (0 != rv)
658 goto out;
659 }
660
Neale Ranns976b2592019-12-04 06:11:00 +0000661 rv = fib_api_route_add_del (mp->is_add, mp->is_multipath, fib_index, &pfx,
662 FIB_SOURCE_API, entry_flags, rpaths);
663
664 if (mp->is_add && 0 == rv)
665 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
666
667out:
668 vec_free (rpaths);
669
670 return (rv);
671}
672
673static int
674ip_route_add_del_v2_t_handler (vl_api_ip_route_add_del_v2_t *mp,
675 u32 *stats_index)
676{
677 fib_route_path_t *rpaths = NULL, *rpath;
678 fib_entry_flag_t entry_flags;
679 vl_api_fib_path_t *apath;
680 fib_source_t src;
681 fib_prefix_t pfx;
682 u32 fib_index;
683 int rv, ii;
684
685 entry_flags = FIB_ENTRY_FLAG_NONE;
686 ip_prefix_decode (&mp->route.prefix, &pfx);
687
688 rv = fib_api_table_id_decode (pfx.fp_proto, ntohl (mp->route.table_id),
689 &fib_index);
690 if (0 != rv)
691 goto out;
692
693 if (0 != mp->route.n_paths)
694 vec_validate (rpaths, mp->route.n_paths - 1);
695
696 for (ii = 0; ii < mp->route.n_paths; ii++)
697 {
698 apath = &mp->route.paths[ii];
699 rpath = &rpaths[ii];
700
701 rv = fib_api_path_decode (apath, rpath);
702
703 if ((rpath->frp_flags & FIB_ROUTE_PATH_LOCAL) &&
704 (~0 == rpath->frp_sw_if_index))
705 entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
706
707 if (0 != rv)
708 goto out;
709 }
710
711 src = (0 == mp->route.src ? FIB_SOURCE_API : mp->route.src);
712
713 rv = fib_api_route_add_del (mp->is_add, mp->is_multipath, fib_index, &pfx,
714 src, entry_flags, rpaths);
Neale Ranns008dbe12018-09-07 09:32:36 -0700715
716 if (mp->is_add && 0 == rv)
717 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
718
Neale Ranns097fa662018-05-01 05:17:55 -0700719out:
720 vec_free (rpaths);
Neale Ranns008dbe12018-09-07 09:32:36 -0700721
722 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -0500723}
724
725void
Neale Ranns097fa662018-05-01 05:17:55 -0700726vl_api_ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500727{
Neale Ranns097fa662018-05-01 05:17:55 -0700728 vl_api_ip_route_add_del_reply_t *rmp;
729 u32 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -0500730 int rv;
Dave Barachb5e8a772016-12-06 12:04:42 -0500731
Neale Ranns097fa662018-05-01 05:17:55 -0700732 rv = ip_route_add_del_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500733
Neale Ranns008dbe12018-09-07 09:32:36 -0700734 /* *INDENT-OFF* */
Neale Ranns097fa662018-05-01 05:17:55 -0700735 REPLY_MACRO2 (VL_API_IP_ROUTE_ADD_DEL_REPLY,
Neale Ranns008dbe12018-09-07 09:32:36 -0700736 ({
737 rmp->stats_index = htonl (stats_index);
738 }))
739 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500740}
741
Neale Ranns15002542017-09-10 04:39:11 -0700742void
Neale Ranns976b2592019-12-04 06:11:00 +0000743vl_api_ip_route_add_del_v2_t_handler (vl_api_ip_route_add_del_v2_t *mp)
744{
745 vl_api_ip_route_add_del_v2_reply_t *rmp;
746 u32 stats_index = ~0;
747 int rv;
748
749 rv = ip_route_add_del_v2_t_handler (mp, &stats_index);
750
751 /* clang-format off */
752 REPLY_MACRO2 (VL_API_IP_ROUTE_ADD_DEL_V2_REPLY,
753 ({
754 rmp->stats_index = htonl (stats_index);
755 }))
756 /* clang-format on */
757}
758
759void
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400760vl_api_ip_route_lookup_t_handler (vl_api_ip_route_lookup_t * mp)
761{
762 vl_api_ip_route_lookup_reply_t *rmp = NULL;
763 fib_route_path_t *rpaths = NULL, *rpath;
764 const fib_prefix_t *pfx = NULL;
765 fib_prefix_t lookup;
766 vl_api_fib_path_t *fp;
767 fib_node_index_t fib_entry_index;
768 u32 fib_index;
769 int npaths = 0;
770 int rv;
771
772 ip_prefix_decode (&mp->prefix, &lookup);
773 rv = fib_api_table_id_decode (lookup.fp_proto, ntohl (mp->table_id),
774 &fib_index);
775 if (PREDICT_TRUE (!rv))
776 {
777 if (mp->exact)
778 fib_entry_index = fib_table_lookup_exact_match (fib_index, &lookup);
779 else
780 fib_entry_index = fib_table_lookup (fib_index, &lookup);
781 if (fib_entry_index == FIB_NODE_INDEX_INVALID)
782 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
783 else
784 {
785 pfx = fib_entry_get_prefix (fib_entry_index);
786 rpaths = fib_entry_encode (fib_entry_index);
787 npaths = vec_len (rpaths);
788 }
789 }
790
791 /* *INDENT-OFF* */
792 REPLY_MACRO3_ZERO(VL_API_IP_ROUTE_LOOKUP_REPLY,
793 npaths * sizeof (*fp),
794 ({
795 if (!rv)
796 {
797 ip_prefix_encode (pfx, &rmp->route.prefix);
798 rmp->route.table_id = mp->table_id;
799 rmp->route.n_paths = npaths;
800 rmp->route.stats_index = fib_table_entry_get_stats_index (fib_index, pfx);
801 rmp->route.stats_index = htonl (rmp->route.stats_index);
802
803 fp = rmp->route.paths;
804 vec_foreach (rpath, rpaths)
805 {
806 fib_api_path_encode (rpath, fp);
807 fp++;
808 }
809 }
810 }));
811 /* *INDENT-ON* */
812 vec_free (rpaths);
813}
814
815void
Neale Ranns976b2592019-12-04 06:11:00 +0000816vl_api_ip_route_lookup_v2_t_handler (vl_api_ip_route_lookup_v2_t *mp)
817{
818 vl_api_ip_route_lookup_v2_reply_t *rmp = NULL;
819 fib_route_path_t *rpaths = NULL, *rpath;
820 const fib_prefix_t *pfx = NULL;
821 fib_prefix_t lookup;
822 vl_api_fib_path_t *fp;
823 fib_node_index_t fib_entry_index;
824 u32 fib_index;
825 int npaths = 0;
826 fib_source_t src = 0;
827 int rv;
828
829 ip_prefix_decode (&mp->prefix, &lookup);
830 rv = fib_api_table_id_decode (lookup.fp_proto, ntohl (mp->table_id),
831 &fib_index);
832 if (PREDICT_TRUE (!rv))
833 {
834 if (mp->exact)
835 fib_entry_index = fib_table_lookup_exact_match (fib_index, &lookup);
836 else
837 fib_entry_index = fib_table_lookup (fib_index, &lookup);
838 if (fib_entry_index == FIB_NODE_INDEX_INVALID)
839 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
840 else
841 {
842 pfx = fib_entry_get_prefix (fib_entry_index);
843 rpaths = fib_entry_encode (fib_entry_index);
844 npaths = vec_len (rpaths);
845 src = fib_entry_get_best_source (fib_entry_index);
846 }
847 }
848
849 /* clang-format off */
850 REPLY_MACRO3_ZERO(VL_API_IP_ROUTE_LOOKUP_V2_REPLY,
851 npaths * sizeof (*fp),
852 ({
853 if (!rv)
854 {
855 ip_prefix_encode (pfx, &rmp->route.prefix);
856 rmp->route.table_id = mp->table_id;
857 rmp->route.n_paths = npaths;
858 rmp->route.src = src;
859 rmp->route.stats_index = fib_table_entry_get_stats_index (fib_index, pfx);
860 rmp->route.stats_index = htonl (rmp->route.stats_index);
861
862 fp = rmp->route.paths;
863 vec_foreach (rpath, rpaths)
864 {
865 fib_api_path_encode (rpath, fp);
866 fp++;
867 }
868 }
869 }));
870 /* clang-format on */
871 vec_free (rpaths);
872}
873
874void
Neale Ranns2297af02017-09-12 09:45:04 -0700875ip_table_create (fib_protocol_t fproto,
876 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -0700877{
878 u32 fib_index, mfib_index;
Steven Luongc3ed1c92020-07-27 10:06:58 -0700879 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns15002542017-09-10 04:39:11 -0700880
881 /*
882 * ignore action on the default table - this is always present
883 * and cannot be added nor deleted from the API
884 */
885 if (0 != table_id)
886 {
887 /*
888 * The API holds only one lock on the table.
889 * i.e. it can be added many times via the API but needs to be
890 * deleted only once.
891 * The FIB index for unicast and multicast is not necessarily the
892 * same, since internal VPP systesm (like LISP and SR) create
893 * their own unicast tables.
894 */
895 fib_index = fib_table_find (fproto, table_id);
896 mfib_index = mfib_table_find (fproto, table_id);
897
898 if (~0 == fib_index)
899 {
Neale Ranns2297af02017-09-12 09:45:04 -0700900 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
901 (is_api ?
902 FIB_SOURCE_API :
903 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -0700904 }
905 if (~0 == mfib_index)
906 {
Neale Ranns2297af02017-09-12 09:45:04 -0700907 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
908 (is_api ?
909 MFIB_SOURCE_API :
910 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -0700911 }
Steven Luongc3ed1c92020-07-27 10:06:58 -0700912
913 if ((~0 == fib_index) || (~0 == mfib_index))
914 call_elf_section_ip_table_callbacks (vnm, table_id, 1 /* is_add */ ,
915 vnm->ip_table_add_del_functions);
Neale Ranns15002542017-09-10 04:39:11 -0700916 }
917}
918
Neale Ranns097fa662018-05-01 05:17:55 -0700919static u32
Neale Ranns32e1c012016-11-22 17:07:28 +0000920mroute_add_del_handler (u8 is_add,
Neale Ranns097fa662018-05-01 05:17:55 -0700921 u8 is_multipath,
Neale Ranns32e1c012016-11-22 17:07:28 +0000922 u32 fib_index,
923 const mfib_prefix_t * prefix,
924 u32 entry_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700925 u32 rpf_id, fib_route_path_t * rpaths)
Neale Ranns32e1c012016-11-22 17:07:28 +0000926{
Neale Ranns097fa662018-05-01 05:17:55 -0700927 u32 mfib_entry_index = ~0;
Neale Ranns28c142e2018-09-07 09:37:07 -0700928
Neale Ranns097fa662018-05-01 05:17:55 -0700929 if (0 == vec_len (rpaths))
Neale Ranns32e1c012016-11-22 17:07:28 +0000930 {
Neale Ranns28c142e2018-09-07 09:37:07 -0700931 mfib_entry_index = mfib_table_entry_update (fib_index, prefix,
932 MFIB_SOURCE_API,
933 rpf_id, entry_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +0000934 }
935 else
936 {
Neale Ranns097fa662018-05-01 05:17:55 -0700937 if (is_add)
938 {
939 mfib_entry_index =
940 mfib_table_entry_paths_update (fib_index, prefix,
941 MFIB_SOURCE_API, rpaths);
942 }
943 else
944 {
945 mfib_table_entry_paths_remove (fib_index, prefix,
946 MFIB_SOURCE_API, rpaths);
947 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000948 }
949
Neale Ranns28c142e2018-09-07 09:37:07 -0700950 return (mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000951}
952
953static int
Neale Ranns28c142e2018-09-07 09:37:07 -0700954api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp,
955 u32 * stats_index)
Neale Ranns32e1c012016-11-22 17:07:28 +0000956{
Neale Ranns097fa662018-05-01 05:17:55 -0700957 fib_route_path_t *rpath, *rpaths = NULL;
Neale Ranns28c142e2018-09-07 09:37:07 -0700958 fib_node_index_t mfib_entry_index;
Neale Ranns990f6942020-10-20 07:20:17 +0000959 mfib_entry_flags_t eflags;
Neale Ranns097fa662018-05-01 05:17:55 -0700960 mfib_prefix_t pfx;
Neale Ranns32e1c012016-11-22 17:07:28 +0000961 u32 fib_index;
962 int rv;
Neale Ranns097fa662018-05-01 05:17:55 -0700963 u16 ii;
Neale Ranns32e1c012016-11-22 17:07:28 +0000964
Neale Ranns097fa662018-05-01 05:17:55 -0700965 ip_mprefix_decode (&mp->route.prefix, &pfx);
Neale Ranns32e1c012016-11-22 17:07:28 +0000966
Neale Ranns097fa662018-05-01 05:17:55 -0700967 rv = mfib_api_table_id_decode (pfx.fp_proto,
968 ntohl (mp->route.table_id), &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000969 if (0 != rv)
Neale Ranns097fa662018-05-01 05:17:55 -0700970 goto out;
Neale Ranns32e1c012016-11-22 17:07:28 +0000971
Neale Ranns097fa662018-05-01 05:17:55 -0700972 vec_validate (rpaths, mp->route.n_paths - 1);
Neale Ranns32e1c012016-11-22 17:07:28 +0000973
Neale Ranns097fa662018-05-01 05:17:55 -0700974 for (ii = 0; ii < mp->route.n_paths; ii++)
Neale Ranns32e1c012016-11-22 17:07:28 +0000975 {
Neale Ranns097fa662018-05-01 05:17:55 -0700976 rpath = &rpaths[ii];
977
978 rv = mfib_api_path_decode (&mp->route.paths[ii], rpath);
979
980 if (0 != rv)
981 goto out;
Neale Ranns32e1c012016-11-22 17:07:28 +0000982 }
983
Neale Ranns990f6942020-10-20 07:20:17 +0000984 eflags = mfib_api_path_entry_flags_decode (mp->route.entry_flags);
Neale Ranns28c142e2018-09-07 09:37:07 -0700985 mfib_entry_index = mroute_add_del_handler (mp->is_add,
Neale Ranns097fa662018-05-01 05:17:55 -0700986 mp->is_add,
Neale Ranns28c142e2018-09-07 09:37:07 -0700987 fib_index, &pfx,
Neale Ranns990f6942020-10-20 07:20:17 +0000988 eflags,
Neale Ranns097fa662018-05-01 05:17:55 -0700989 ntohl (mp->route.rpf_id),
990 rpaths);
Neale Ranns28c142e2018-09-07 09:37:07 -0700991
992 if (~0 != mfib_entry_index)
993 *stats_index = mfib_entry_get_stats_index (mfib_entry_index);
994
Neale Ranns097fa662018-05-01 05:17:55 -0700995out:
Neale Ranns28c142e2018-09-07 09:37:07 -0700996 return (rv);
Neale Ranns32e1c012016-11-22 17:07:28 +0000997}
998
999void
1000vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1001{
1002 vl_api_ip_mroute_add_del_reply_t *rmp;
Neale Ranns097fa662018-05-01 05:17:55 -07001003 u32 stats_index = ~0;
Neale Ranns32e1c012016-11-22 17:07:28 +00001004 int rv;
Neale Ranns32e1c012016-11-22 17:07:28 +00001005
Neale Ranns28c142e2018-09-07 09:37:07 -07001006 rv = api_mroute_add_del_t_handler (mp, &stats_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001007
Neale Ranns28c142e2018-09-07 09:37:07 -07001008 /* *INDENT-OFF* */
1009 REPLY_MACRO2 (VL_API_IP_MROUTE_ADD_DEL_REPLY,
1010 ({
1011 rmp->stats_index = htonl (stats_index);
1012 }));
1013 /* *INDENT-ON* */
Neale Ranns32e1c012016-11-22 17:07:28 +00001014}
1015
Dave Barachb5e8a772016-12-06 12:04:42 -05001016static void
1017send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001018 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1019 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001020{
1021 vl_api_ip_details_t *mp;
1022
1023 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001024 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001025 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -05001026
1027 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001028 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001029 mp->context = context;
1030
Florin Coras6c4dae22018-01-09 06:39:23 -08001031 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001032}
1033
1034static void
1035send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001036 vl_api_registration_t * reg,
Neale Ranns097fa662018-05-01 05:17:55 -07001037 const fib_prefix_t * pfx,
1038 u32 sw_if_index, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001039{
1040 vl_api_ip_address_details_t *mp;
1041
1042 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001043 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001044 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_ADDRESS_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -05001045
Neale Ranns097fa662018-05-01 05:17:55 -07001046 ip_prefix_encode (pfx, &mp->prefix);
Dave Barachb5e8a772016-12-06 12:04:42 -05001047 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001048 mp->sw_if_index = htonl (sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001049
Florin Coras6c4dae22018-01-09 06:39:23 -08001050 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001051}
1052
1053static void
1054vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1055{
1056 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001057 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001058 ip6_main_t *im6 = &ip6_main;
1059 ip4_main_t *im4 = &ip4_main;
1060 ip_lookup_main_t *lm6 = &im6->lookup_main;
1061 ip_lookup_main_t *lm4 = &im4->lookup_main;
1062 ip_interface_address_t *ia = 0;
1063 u32 sw_if_index = ~0;
1064 int rv __attribute__ ((unused)) = 0;
1065
1066 VALIDATE_SW_IF_INDEX (mp);
1067
1068 sw_if_index = ntohl (mp->sw_if_index);
1069
Florin Coras6c4dae22018-01-09 06:39:23 -08001070 reg = vl_api_client_index_to_registration (mp->client_index);
1071 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001072 return;
1073
Dave Barachb5e8a772016-12-06 12:04:42 -05001074 if (mp->is_ipv6)
1075 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001076 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001077 /* Do not send subnet details of the IP-interface for
1078 * unnumbered interfaces. otherwise listening clients
1079 * will be confused that the subnet is applied on more
1080 * than one interface */
1081 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001082 ({
Neale Ranns097fa662018-05-01 05:17:55 -07001083 fib_prefix_t pfx = {
1084 .fp_addr.ip6 = *(ip6_address_t *)ip_interface_address_get_address (lm6, ia),
1085 .fp_len = ia->address_length,
1086 .fp_proto = FIB_PROTOCOL_IP6,
1087 };
1088 send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001089 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001090 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001091 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001092 else
1093 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001094 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001095 foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001096 ({
Neale Ranns097fa662018-05-01 05:17:55 -07001097 fib_prefix_t pfx = {
1098 .fp_addr.ip4 = *(ip4_address_t *)ip_interface_address_get_address (lm4, ia),
1099 .fp_len = ia->address_length,
1100 .fp_proto = FIB_PROTOCOL_IP4,
1101 };
1102
1103 send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001104 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001105 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001106 }
Neale Ranns008dbe12018-09-07 09:32:36 -07001107
Dave Barachb5e8a772016-12-06 12:04:42 -05001108 BAD_SW_IF_INDEX_LABEL;
1109}
1110
1111static void
Neale Ranns9e2f9152018-05-18 02:27:10 -07001112send_ip_unnumbered_details (vpe_api_main_t * am,
1113 vl_api_registration_t * reg,
1114 u32 sw_if_index, u32 ip_sw_if_index, u32 context)
1115{
1116 vl_api_ip_unnumbered_details_t *mp;
1117
1118 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001119 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001120 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_UNNUMBERED_DETAILS);
Neale Ranns9e2f9152018-05-18 02:27:10 -07001121
1122 mp->context = context;
1123 mp->sw_if_index = htonl (sw_if_index);
1124 mp->ip_sw_if_index = htonl (ip_sw_if_index);
1125
1126 vl_api_send_msg (reg, (u8 *) mp);
1127}
1128
1129static void
1130vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
1131{
1132 vnet_main_t *vnm = vnet_get_main ();
1133 vnet_interface_main_t *im = &vnm->interface_main;
1134 int rv __attribute__ ((unused)) = 0;
1135 vpe_api_main_t *am = &vpe_api_main;
1136 vl_api_registration_t *reg;
1137 vnet_sw_interface_t *si;
1138 u32 sw_if_index;
1139
1140 sw_if_index = ntohl (mp->sw_if_index);
1141
1142 reg = vl_api_client_index_to_registration (mp->client_index);
1143 if (!reg)
1144 return;
1145
1146 if (~0 != sw_if_index)
1147 {
1148 VALIDATE_SW_IF_INDEX (mp);
1149
1150 si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
1151
Neale Rannsac3e72c2019-10-06 01:04:26 -07001152 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
Neale Ranns9e2f9152018-05-18 02:27:10 -07001153 {
1154 send_ip_unnumbered_details (am, reg,
1155 sw_if_index,
1156 si->unnumbered_sw_if_index,
1157 mp->context);
1158 }
1159 }
1160 else
1161 {
1162 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001163 pool_foreach (si, im->sw_interfaces)
1164 {
Neale Ranns9e2f9152018-05-18 02:27:10 -07001165 if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1166 {
1167 send_ip_unnumbered_details(am, reg,
1168 si->sw_if_index,
1169 si->unnumbered_sw_if_index,
1170 mp->context);
1171 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001172 }
Neale Ranns9e2f9152018-05-18 02:27:10 -07001173 /* *INDENT-ON* */
1174 }
1175
1176 BAD_SW_IF_INDEX_LABEL;
1177}
1178
1179static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001180vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1181{
1182 vpe_api_main_t *am = &vpe_api_main;
1183 vnet_main_t *vnm = vnet_get_main ();
Neale Rannscbe25aa2019-09-30 10:53:31 +00001184 //vlib_main_t *vm = vlib_get_main ();
Dave Barachb5e8a772016-12-06 12:04:42 -05001185 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001186 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001187 vnet_sw_interface_t *si, *sorted_sis;
1188 u32 sw_if_index = ~0;
1189
Florin Coras6c4dae22018-01-09 06:39:23 -08001190 reg = vl_api_client_index_to_registration (mp->client_index);
1191 if (!reg)
1192 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001193
1194 /* Gather interfaces. */
1195 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1196 _vec_len (sorted_sis) = 0;
1197 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001198 pool_foreach (si, im->sw_interfaces)
1199 {
Dave Barachb5e8a772016-12-06 12:04:42 -05001200 vec_add1 (sorted_sis, si[0]);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001201 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001202 /* *INDENT-ON* */
1203
1204 vec_foreach (si, sorted_sis)
1205 {
1206 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1207 {
Neale Rannscbe25aa2019-09-30 10:53:31 +00001208 /* if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index)) */
1209 /* { */
1210 /* continue; */
1211 /* } */
Dave Barachb5e8a772016-12-06 12:04:42 -05001212 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001213 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001214 }
1215 }
Matthew Smith6d5f6592019-09-18 13:51:46 -05001216
1217 vec_free (sorted_sis);
Dave Barachb5e8a772016-12-06 12:04:42 -05001218}
1219
1220static void
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001221vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t *mp)
Dave Barachb5e8a772016-12-06 12:04:42 -05001222{
1223 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001224 int rv;
1225 u32 table_id;
1226 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001227
Neale Ranns227038a2017-04-21 01:07:59 -07001228 table_id = ntohl (mp->vrf_id);
1229
1230#define _(a,b) if (mp->a) flow_hash_config |= b;
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001231 foreach_flow_hash_bit_v1;
Neale Ranns227038a2017-04-21 01:07:59 -07001232#undef _
1233
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001234 rv = ip_flow_hash_set ((mp->is_ipv6 ? AF_IP6 : AF_IP4), table_id,
1235 flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001236
1237 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1238}
1239
1240static void
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001241vl_api_set_ip_flow_hash_v2_t_handler (vl_api_set_ip_flow_hash_v2_t *mp)
Dave Barachb5e8a772016-12-06 12:04:42 -05001242{
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001243 vl_api_set_ip_flow_hash_v2_reply_t *rmp;
1244 ip_address_family_t af;
Dave Barachb5e8a772016-12-06 12:04:42 -05001245 int rv;
Dave Barachb5e8a772016-12-06 12:04:42 -05001246
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001247 rv = ip_address_family_decode (mp->af, &af);
Dave Barachb5e8a772016-12-06 12:04:42 -05001248
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001249 if (!rv)
1250 rv = ip_flow_hash_set (af, htonl (mp->table_id),
1251 htonl (mp->flow_hash_config));
Dave Barachb5e8a772016-12-06 12:04:42 -05001252
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001253 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_V2_REPLY);
Dave Barachb5e8a772016-12-06 12:04:42 -05001254}
1255
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001256static void
1257vl_api_set_ip_flow_hash_router_id_t_handler (
1258 vl_api_set_ip_flow_hash_router_id_t *mp)
1259{
1260 vl_api_set_ip_flow_hash_router_id_reply_t *rmp;
1261 int rv = 0;
1262
1263 ip_flow_hash_router_id_set (ntohl (mp->router_id));
1264
1265 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_ROUTER_ID_REPLY);
1266}
1267
Neale Ranns32e1c012016-11-22 17:07:28 +00001268void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001269vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001270 u32 context, const mfib_signal_t * mfs)
1271{
1272 vl_api_mfib_signal_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -08001273 const mfib_prefix_t *prefix;
Neale Ranns32e1c012016-11-22 17:07:28 +00001274 mfib_table_t *mfib;
1275 mfib_itf_t *mfi;
1276
1277 mp = vl_msg_api_alloc (sizeof (*mp));
1278
Dave Barachb7b92992018-10-17 10:38:51 -04001279 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001280 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_MFIB_SIGNAL_DETAILS);
Neale Ranns32e1c012016-11-22 17:07:28 +00001281 mp->context = context;
1282
1283 mfi = mfib_itf_get (mfs->mfs_itf);
Neale Ranns9e829a82018-12-17 05:50:32 -08001284 prefix = mfib_entry_get_prefix (mfs->mfs_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +00001285 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
Neale Ranns9e829a82018-12-17 05:50:32 -08001286 prefix->fp_proto);
Neale Ranns32e1c012016-11-22 17:07:28 +00001287 mp->table_id = ntohl (mfib->mft_table_id);
1288 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1289
Neale Ranns097fa662018-05-01 05:17:55 -07001290 ip_mprefix_encode (prefix, &mp->prefix);
Neale Ranns32e1c012016-11-22 17:07:28 +00001291
1292 if (0 != mfs->mfs_buffer_len)
1293 {
1294 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1295
1296 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1297 }
1298 else
1299 {
1300 mp->ip_packet_len = 0;
1301 }
1302
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001303 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001304}
1305
1306static void
1307vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1308{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001309 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001310
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001311 reg = vl_api_client_index_to_registration (mp->client_index);
1312 if (!reg)
1313 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001314
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001315 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001316 ;
1317}
Dave Barachb5e8a772016-12-06 12:04:42 -05001318
Florin Coras595992c2017-11-06 17:17:08 -08001319static void
1320 vl_api_ip_container_proxy_add_del_t_handler
1321 (vl_api_ip_container_proxy_add_del_t * mp)
1322{
1323 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1324 vnet_ip_container_proxy_args_t args;
1325 int rv = 0;
1326 clib_error_t *error;
1327
Dave Barachb7b92992018-10-17 10:38:51 -04001328 clib_memset (&args, 0, sizeof (args));
Neale Ranns37029302018-08-10 05:30:06 -07001329
1330 ip_prefix_decode (&mp->pfx, &args.prefix);
1331
Florin Coras595992c2017-11-06 17:17:08 -08001332 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1333 args.is_add = mp->is_add;
1334 if ((error = vnet_ip_container_proxy_add_del (&args)))
1335 {
1336 rv = clib_error_get_code (error);
1337 clib_error_report (error);
1338 }
1339
1340 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1341}
1342
Neale Ranns8f5fef22020-12-21 08:29:34 +00001343typedef struct ip_walk_ctx_t_
Matus Fabian75b9f452018-10-02 23:27:21 -07001344{
1345 vl_api_registration_t *reg;
1346 u32 context;
Neale Ranns8f5fef22020-12-21 08:29:34 +00001347} ip_walk_ctx_t;
Matus Fabian75b9f452018-10-02 23:27:21 -07001348
1349static int
1350ip_container_proxy_send_details (const fib_prefix_t * pfx, u32 sw_if_index,
1351 void *args)
1352{
1353 vl_api_ip_container_proxy_details_t *mp;
Neale Ranns8f5fef22020-12-21 08:29:34 +00001354 ip_walk_ctx_t *ctx = args;
Matus Fabian75b9f452018-10-02 23:27:21 -07001355
1356 mp = vl_msg_api_alloc (sizeof (*mp));
1357 if (!mp)
1358 return 1;
1359
Dave Barachb7b92992018-10-17 10:38:51 -04001360 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001361 mp->_vl_msg_id =
1362 ntohs (REPLY_MSG_ID_BASE + VL_API_IP_CONTAINER_PROXY_DETAILS);
Matus Fabian75b9f452018-10-02 23:27:21 -07001363 mp->context = ctx->context;
1364
1365 mp->sw_if_index = ntohl (sw_if_index);
1366 ip_prefix_encode (pfx, &mp->prefix);
1367
1368 vl_api_send_msg (ctx->reg, (u8 *) mp);
1369
1370 return 1;
1371}
1372
1373static void
1374vl_api_ip_container_proxy_dump_t_handler (vl_api_ip_container_proxy_dump_t *
1375 mp)
1376{
1377 vl_api_registration_t *reg;
1378
1379 reg = vl_api_client_index_to_registration (mp->client_index);
1380 if (!reg)
1381 return;
1382
Neale Ranns8f5fef22020-12-21 08:29:34 +00001383 ip_walk_ctx_t ctx = {
Matus Fabian75b9f452018-10-02 23:27:21 -07001384 .context = mp->context,
1385 .reg = reg,
1386 };
1387
1388 ip_container_proxy_walk (ip_container_proxy_send_details, &ctx);
1389}
1390
Neale Rannsb8d44812017-11-10 06:53:54 -08001391static void
1392vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1393{
1394 int rv = 0;
1395 vl_api_ioam_enable_reply_t *rmp;
1396 clib_error_t *error;
1397
1398 /* Ignoring the profile id as currently a single profile
1399 * is supported */
1400 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1401 mp->seqno, mp->analyse);
1402 if (error)
1403 {
1404 clib_error_report (error);
1405 rv = clib_error_get_code (error);
1406 }
1407
1408 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1409}
1410
1411static void
1412vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1413{
1414 int rv = 0;
1415 vl_api_ioam_disable_reply_t *rmp;
1416 clib_error_t *error;
1417
1418 error = clear_ioam_rewrite_fn ();
1419 if (error)
1420 {
1421 clib_error_report (error);
1422 rv = clib_error_get_code (error);
1423 }
1424
1425 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1426}
1427
1428static void
1429 vl_api_ip_source_and_port_range_check_add_del_t_handler
1430 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1431{
1432 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1433 int rv = 0;
1434
Neale Rannsb8d44812017-11-10 06:53:54 -08001435 u8 is_add = mp->is_add;
Neale Ranns37029302018-08-10 05:30:06 -07001436 fib_prefix_t pfx;
Neale Rannsb8d44812017-11-10 06:53:54 -08001437 u16 *low_ports = 0;
1438 u16 *high_ports = 0;
1439 u32 vrf_id;
1440 u16 tmp_low, tmp_high;
1441 u8 num_ranges;
1442 int i;
1443
Neale Ranns37029302018-08-10 05:30:06 -07001444 ip_prefix_decode (&mp->prefix, &pfx);
1445
Neale Rannsb8d44812017-11-10 06:53:54 -08001446 // Validate port range
1447 num_ranges = mp->number_of_ranges;
1448 if (num_ranges > 32)
1449 { // This is size of array in VPE.API
1450 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1451 goto reply;
1452 }
1453
1454 vec_reset_length (low_ports);
1455 vec_reset_length (high_ports);
1456
1457 for (i = 0; i < num_ranges; i++)
1458 {
1459 tmp_low = mp->low_ports[i];
1460 tmp_high = mp->high_ports[i];
1461 // If tmp_low <= tmp_high then only need to check tmp_low = 0
1462 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1463 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1464 {
1465 rv = VNET_API_ERROR_INVALID_VALUE;
1466 goto reply;
1467 }
1468 vec_add1 (low_ports, tmp_low);
1469 vec_add1 (high_ports, tmp_high + 1);
1470 }
1471
Neale Rannsb8d44812017-11-10 06:53:54 -08001472 vrf_id = ntohl (mp->vrf_id);
1473
1474 if (vrf_id < 1)
1475 {
1476 rv = VNET_API_ERROR_INVALID_VALUE;
1477 goto reply;
1478 }
1479
1480
Neale Ranns37029302018-08-10 05:30:06 -07001481 if (FIB_PROTOCOL_IP6 == pfx.fp_proto)
Neale Rannsb8d44812017-11-10 06:53:54 -08001482 {
Neale Ranns37029302018-08-10 05:30:06 -07001483 rv = ip6_source_and_port_range_check_add_del (&pfx.fp_addr.ip6,
1484 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08001485 vrf_id,
1486 low_ports,
1487 high_ports, is_add);
1488 }
1489 else
1490 {
Neale Ranns37029302018-08-10 05:30:06 -07001491 rv = ip4_source_and_port_range_check_add_del (&pfx.fp_addr.ip4,
1492 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08001493 vrf_id,
1494 low_ports,
1495 high_ports, is_add);
1496 }
1497
1498reply:
1499 vec_free (low_ports);
1500 vec_free (high_ports);
1501 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
1502}
1503
1504static void
1505 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
1506 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
1507{
1508 vlib_main_t *vm = vlib_get_main ();
1509 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
1510 ip4_main_t *im = &ip4_main;
1511 int rv;
1512 u32 sw_if_index;
1513 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1514 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1515 uword *p = 0;
1516 int i;
1517
1518 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
1519 ntohl (mp->tcp_out_vrf_id);
1520 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
1521 ntohl (mp->udp_out_vrf_id);
1522 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
1523 ntohl (mp->tcp_in_vrf_id);
1524 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
1525 ntohl (mp->udp_in_vrf_id);
1526
1527
1528 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
1529 {
1530 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
1531 {
1532 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
1533
1534 if (p == 0)
1535 {
1536 rv = VNET_API_ERROR_INVALID_VALUE;
1537 goto reply;
1538 }
1539
1540 fib_index[i] = p[0];
1541 }
1542 else
1543 fib_index[i] = ~0;
1544 }
1545 sw_if_index = ntohl (mp->sw_if_index);
1546
1547 VALIDATE_SW_IF_INDEX (mp);
1548
1549 rv =
1550 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
1551 mp->is_add);
1552
1553 BAD_SW_IF_INDEX_LABEL;
1554reply:
1555
1556 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
1557}
1558
Neale Rannscbe25aa2019-09-30 10:53:31 +00001559static void
1560 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1561 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1562{
1563 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1564 ip6_address_t ip;
1565 int rv;
1566
1567 VALIDATE_SW_IF_INDEX (mp);
1568
1569 ip6_address_decode (mp->ip, &ip);
1570
Neale Rannsec40a7d2020-04-23 07:36:12 +00001571 rv = ip6_link_set_local_address (ntohl (mp->sw_if_index), &ip);
Neale Rannscbe25aa2019-09-30 10:53:31 +00001572
1573 BAD_SW_IF_INDEX_LABEL;
1574 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1575}
1576
Neale Ranns9db6ada2019-11-08 12:42:31 +00001577static void
Benoît Ganne58a19152021-01-18 19:24:34 +01001578vl_api_sw_interface_ip6_get_link_local_address_t_handler (
1579 vl_api_sw_interface_ip6_get_link_local_address_t *mp)
1580{
1581 vl_api_sw_interface_ip6_get_link_local_address_reply_t *rmp;
1582 const ip6_address_t *ip = NULL;
1583 int rv = 0;
1584
1585 VALIDATE_SW_IF_INDEX (mp);
1586
1587 ip = ip6_get_link_local_address (ntohl (mp->sw_if_index));
1588 if (NULL == ip)
1589 rv = VNET_API_ERROR_IP6_NOT_ENABLED;
1590
1591 BAD_SW_IF_INDEX_LABEL;
1592 /* clang-format off */
1593 REPLY_MACRO2 (VL_API_SW_INTERFACE_IP6_GET_LINK_LOCAL_ADDRESS_REPLY,
1594 ({
1595 if (!rv)
1596 ip6_address_encode (ip, rmp->ip);
1597 }))
1598 /* clang-format on */
1599}
1600
1601static void
Neale Ranns9db6ada2019-11-08 12:42:31 +00001602vl_api_ip_table_replace_begin_t_handler (vl_api_ip_table_replace_begin_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -08001603{
Neale Ranns9db6ada2019-11-08 12:42:31 +00001604 vl_api_ip_table_replace_begin_reply_t *rmp;
1605 fib_protocol_t fproto;
1606 u32 fib_index;
1607 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001608
Neale Ranns9db6ada2019-11-08 12:42:31 +00001609 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1610 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
Neale Rannsb8d44812017-11-10 06:53:54 -08001611
Neale Ranns9db6ada2019-11-08 12:42:31 +00001612 if (INDEX_INVALID == fib_index)
1613 rv = VNET_API_ERROR_NO_SUCH_FIB;
1614 else
1615 {
1616 fib_table_mark (fib_index, fproto, FIB_SOURCE_API);
1617 mfib_table_mark (mfib_table_find (fproto, ntohl (mp->table.table_id)),
1618 fproto, MFIB_SOURCE_API);
Neale Rannsb8d44812017-11-10 06:53:54 -08001619 }
Neale Ranns9db6ada2019-11-08 12:42:31 +00001620 REPLY_MACRO (VL_API_IP_TABLE_REPLACE_BEGIN_REPLY);
Neale Rannsb8d44812017-11-10 06:53:54 -08001621}
1622
1623static void
Neale Ranns9db6ada2019-11-08 12:42:31 +00001624vl_api_ip_table_replace_end_t_handler (vl_api_ip_table_replace_end_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -08001625{
Neale Ranns9db6ada2019-11-08 12:42:31 +00001626 vl_api_ip_table_replace_end_reply_t *rmp;
1627 fib_protocol_t fproto;
1628 u32 fib_index;
1629 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001630
Neale Ranns9db6ada2019-11-08 12:42:31 +00001631 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1632 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1633
1634 if (INDEX_INVALID == fib_index)
1635 rv = VNET_API_ERROR_NO_SUCH_FIB;
Neale Rannsb8d44812017-11-10 06:53:54 -08001636 else
Neale Ranns9db6ada2019-11-08 12:42:31 +00001637 {
1638 fib_table_sweep (fib_index, fproto, FIB_SOURCE_API);
1639 mfib_table_sweep (mfib_table_find
1640 (fproto, ntohl (mp->table.table_id)), fproto,
1641 MFIB_SOURCE_API);
1642 }
1643 REPLY_MACRO (VL_API_IP_TABLE_REPLACE_END_REPLY);
1644}
Neale Rannsb8d44812017-11-10 06:53:54 -08001645
Neale Ranns9db6ada2019-11-08 12:42:31 +00001646static void
1647vl_api_ip_table_flush_t_handler (vl_api_ip_table_flush_t * mp)
1648{
1649 vl_api_ip_table_flush_reply_t *rmp;
1650 fib_protocol_t fproto;
1651 u32 fib_index;
1652 int rv = 0;
1653
1654 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1655 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1656
1657 if (INDEX_INVALID == fib_index)
1658 rv = VNET_API_ERROR_NO_SUCH_FIB;
1659 else
1660 {
1661 vnet_main_t *vnm = vnet_get_main ();
1662 vnet_interface_main_t *im = &vnm->interface_main;
1663 vnet_sw_interface_t *si;
1664
1665 /* Shut down interfaces in this FIB / clean out intfc routes */
1666 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001667 pool_foreach (si, im->sw_interfaces)
1668 {
Neale Ranns9db6ada2019-11-08 12:42:31 +00001669 if (fib_index == fib_table_get_index_for_sw_if_index (fproto,
1670 si->sw_if_index))
1671 {
1672 u32 flags = si->flags;
1673 flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP;
1674 vnet_sw_interface_set_flags (vnm, si->sw_if_index, flags);
1675 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001676 }
Neale Ranns9db6ada2019-11-08 12:42:31 +00001677 /* *INDENT-ON* */
1678
1679 fib_table_flush (fib_index, fproto, FIB_SOURCE_API);
1680 mfib_table_flush (mfib_table_find (fproto, ntohl (mp->table.table_id)),
1681 fproto, MFIB_SOURCE_API);
1682 }
1683
1684 REPLY_MACRO (VL_API_IP_TABLE_FLUSH_REPLY);
Neale Rannsb8d44812017-11-10 06:53:54 -08001685}
1686
Klement Sekera75e7d132017-09-20 08:26:30 +02001687void
1688vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
1689{
1690 vl_api_ip_reassembly_set_reply_t *rmp;
1691 int rv = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001692 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera75e7d132017-09-20 08:26:30 +02001693 {
Klement Sekerade34c352019-06-25 11:19:22 +00001694 case IP_REASS_TYPE_FULL:
1695 if (mp->is_ip6)
1696 {
1697 rv = ip6_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1698 clib_net_to_host_u32
1699 (mp->max_reassemblies),
1700 clib_net_to_host_u32
1701 (mp->max_reassembly_length),
1702 clib_net_to_host_u32
1703 (mp->expire_walk_interval_ms));
1704 }
1705 else
1706 {
1707 rv = ip4_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1708 clib_net_to_host_u32
1709 (mp->max_reassemblies),
1710 clib_net_to_host_u32
1711 (mp->max_reassembly_length),
1712 clib_net_to_host_u32
1713 (mp->expire_walk_interval_ms));
1714 }
1715 break;
1716 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1717 if (mp->is_ip6)
1718 {
1719 rv =
1720 ip6_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1721 clib_net_to_host_u32 (mp->max_reassemblies),
1722 clib_net_to_host_u32
1723 (mp->max_reassembly_length),
1724 clib_net_to_host_u32
1725 (mp->expire_walk_interval_ms));
1726 }
1727 else
1728 {
1729 rv = ip4_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1730 clib_net_to_host_u32 (mp->max_reassemblies),
1731 clib_net_to_host_u32
1732 (mp->max_reassembly_length),
1733 clib_net_to_host_u32
1734 (mp->expire_walk_interval_ms));
1735 }
1736 break;
Klement Sekera75e7d132017-09-20 08:26:30 +02001737 }
1738
1739 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
1740}
1741
1742void
1743vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
1744{
Ole Troan2e1c8962019-04-10 09:44:23 +02001745 vl_api_registration_t *rp;
Klement Sekera75e7d132017-09-20 08:26:30 +02001746
Ole Troan2e1c8962019-04-10 09:44:23 +02001747 rp = vl_api_client_index_to_registration (mp->client_index);
1748 if (rp == 0)
Klement Sekera75e7d132017-09-20 08:26:30 +02001749 return;
1750
1751 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001752 clib_memset (rmp, 0, sizeof (*rmp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001753 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_REASSEMBLY_GET_REPLY);
Klement Sekera75e7d132017-09-20 08:26:30 +02001754 rmp->context = mp->context;
1755 rmp->retval = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001756 u32 timeout_ms;
1757 u32 max_reassemblies;
1758 u32 max_reassembly_length;
1759 u32 expire_walk_interval_ms;
1760 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera75e7d132017-09-20 08:26:30 +02001761 {
Klement Sekerade34c352019-06-25 11:19:22 +00001762 case IP_REASS_TYPE_FULL:
1763 if (mp->is_ip6)
1764 {
1765 rmp->is_ip6 = 1;
1766 ip6_full_reass_get (&timeout_ms, &max_reassemblies,
1767 &max_reassembly_length,
1768 &expire_walk_interval_ms);
1769 }
1770 else
1771 {
1772 rmp->is_ip6 = 0;
1773 ip4_full_reass_get (&timeout_ms, &max_reassemblies,
1774 &max_reassembly_length,
1775 &expire_walk_interval_ms);
1776 }
1777 break;
1778 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1779 if (mp->is_ip6)
1780 {
1781 rmp->is_ip6 = 1;
1782 ip6_sv_reass_get (&timeout_ms, &max_reassemblies,
1783 &max_reassembly_length, &expire_walk_interval_ms);
1784 }
1785 else
1786 {
1787 rmp->is_ip6 = 0;
1788 ip4_sv_reass_get (&timeout_ms, &max_reassemblies,
1789 &max_reassembly_length, &expire_walk_interval_ms);
1790 }
1791 break;
Klement Sekera75e7d132017-09-20 08:26:30 +02001792 }
Matthew Smithb3174df2019-12-03 12:41:53 -06001793 rmp->timeout_ms = clib_host_to_net_u32 (timeout_ms);
1794 rmp->max_reassemblies = clib_host_to_net_u32 (max_reassemblies);
1795 rmp->max_reassembly_length = clib_host_to_net_u32 (max_reassembly_length);
Klement Sekera75e7d132017-09-20 08:26:30 +02001796 rmp->expire_walk_interval_ms =
Matthew Smithb3174df2019-12-03 12:41:53 -06001797 clib_host_to_net_u32 (expire_walk_interval_ms);
Ole Troan2e1c8962019-04-10 09:44:23 +02001798 vl_api_send_msg (rp, (u8 *) rmp);
Klement Sekera75e7d132017-09-20 08:26:30 +02001799}
1800
Klement Sekera4c533132018-02-22 11:41:12 +01001801void
1802 vl_api_ip_reassembly_enable_disable_t_handler
1803 (vl_api_ip_reassembly_enable_disable_t * mp)
1804{
1805 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
1806 int rv = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001807 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera4c533132018-02-22 11:41:12 +01001808 {
Klement Sekerade34c352019-06-25 11:19:22 +00001809 case IP_REASS_TYPE_FULL:
Klement Sekera896c8962019-06-24 11:52:49 +00001810 rv =
Klement Sekerade34c352019-06-25 11:19:22 +00001811 ip4_full_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1812 mp->enable_ip4);
1813 if (0 == rv)
1814 rv =
1815 ip6_full_reass_enable_disable (clib_net_to_host_u32
1816 (mp->sw_if_index), mp->enable_ip6);
1817 break;
1818 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1819 rv =
1820 ip4_sv_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1821 mp->enable_ip4);
1822 if (0 == rv)
1823 {
1824 rv =
1825 ip6_sv_reass_enable_disable (clib_net_to_host_u32
1826 (mp->sw_if_index), mp->enable_ip6);
1827 }
1828 break;
Klement Sekera4c533132018-02-22 11:41:12 +01001829 }
1830
Choree7f61d12018-11-28 10:27:58 +03301831 REPLY_MACRO (VL_API_IP_REASSEMBLY_ENABLE_DISABLE_REPLY);
Klement Sekera4c533132018-02-22 11:41:12 +01001832}
1833
Neale Ranns92207752019-06-03 13:21:40 +00001834static walk_rc_t
1835send_ip_punt_redirect_details (u32 rx_sw_if_index,
1836 const ip_punt_redirect_rx_t * ipr, void *arg)
1837{
Pavel Kotucek609e1212018-11-27 09:59:44 +01001838 vl_api_ip_punt_redirect_details_t *mp;
Neale Ranns097fa662018-05-01 05:17:55 -07001839 fib_path_encode_ctx_t path_ctx = {
1840 .rpaths = NULL,
1841 };
Neale Ranns8f5fef22020-12-21 08:29:34 +00001842 ip_walk_ctx_t *ctx = arg;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001843
1844 mp = vl_msg_api_alloc (sizeof (*mp));
1845 if (!mp)
Neale Ranns92207752019-06-03 13:21:40 +00001846 return (WALK_STOP);;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001847
1848 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar5ff59a12021-06-23 14:38:38 +00001849 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_IP_PUNT_REDIRECT_DETAILS);
Neale Ranns92207752019-06-03 13:21:40 +00001850 mp->context = ctx->context;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001851
Neale Ranns097fa662018-05-01 05:17:55 -07001852 fib_path_list_walk_w_ext (ipr->pl, NULL, fib_path_encode, &path_ctx);
Neale Ranns92207752019-06-03 13:21:40 +00001853
1854 mp->punt.rx_sw_if_index = htonl (rx_sw_if_index);
Neale Ranns097fa662018-05-01 05:17:55 -07001855 mp->punt.tx_sw_if_index = htonl (path_ctx.rpaths[0].frp_sw_if_index);
Neale Ranns92207752019-06-03 13:21:40 +00001856
Neale Ranns097fa662018-05-01 05:17:55 -07001857 ip_address_encode (&path_ctx.rpaths[0].frp_addr,
Neale Ranns92207752019-06-03 13:21:40 +00001858 fib_proto_to_ip46 (ipr->fproto), &mp->punt.nh);
1859
1860 vl_api_send_msg (ctx->reg, (u8 *) mp);
1861
Neale Ranns097fa662018-05-01 05:17:55 -07001862 vec_free (path_ctx.rpaths);
Neale Ranns92207752019-06-03 13:21:40 +00001863
1864 return (WALK_CONTINUE);
Pavel Kotucek609e1212018-11-27 09:59:44 +01001865}
1866
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02001867static walk_rc_t
1868send_ip_punt_redirect_v2_details (u32 rx_sw_if_index,
1869 const ip_punt_redirect_rx_t *ipr, void *arg)
1870{
1871 vl_api_ip_punt_redirect_v2_details_t *mp;
1872 fib_path_encode_ctx_t path_ctx = {
1873 .rpaths = NULL,
1874 };
1875 fib_route_path_t *rpath;
1876 ip_walk_ctx_t *ctx = arg;
1877 vl_api_fib_path_t *fp;
1878 int n_paths;
1879
1880 fib_path_list_walk_w_ext (ipr->pl, NULL, fib_path_encode, &path_ctx);
1881
1882 n_paths = vec_len (path_ctx.rpaths);
1883 mp = vl_msg_api_alloc (sizeof (*mp) + n_paths * sizeof (*fp));
1884 if (!mp)
1885 return (WALK_STOP);
1886
1887 clib_memset (mp, 0, sizeof (*mp));
1888 mp->_vl_msg_id =
1889 ntohs (REPLY_MSG_ID_BASE + VL_API_IP_PUNT_REDIRECT_V2_DETAILS);
1890 mp->context = ctx->context;
1891 mp->punt.rx_sw_if_index = htonl (rx_sw_if_index);
1892 mp->punt.n_paths = htonl (n_paths);
1893 fp = mp->punt.paths;
1894 vec_foreach (rpath, path_ctx.rpaths)
1895 {
1896 fib_api_path_encode (rpath, fp);
1897 fp++;
1898 }
1899 mp->punt.af = (ipr->fproto == FIB_PROTOCOL_IP6) ? ADDRESS_IP6 : ADDRESS_IP4;
1900
1901 vl_api_send_msg (ctx->reg, (u8 *) mp);
1902
1903 vec_free (path_ctx.rpaths);
1904
1905 return (WALK_CONTINUE);
1906}
1907
1908static void
1909vl_api_ip_punt_redirect_dump_common (ip_walk_ctx_t *ctx, fib_protocol_t fproto,
1910 u32 rx_sw_if_index,
1911 ip_punt_redirect_walk_cb_t cb)
1912{
1913
1914 if ((u32) ~0 != rx_sw_if_index)
1915 {
1916 index_t pri;
1917 pri = ip_punt_redirect_find (fproto, rx_sw_if_index);
1918
1919 if (INDEX_INVALID == pri)
1920 return;
1921
1922 cb (rx_sw_if_index, ip_punt_redirect_get (pri), ctx);
1923 }
1924 else
1925 ip_punt_redirect_walk (fproto, cb, ctx);
1926}
1927
Pavel Kotucek609e1212018-11-27 09:59:44 +01001928static void
1929vl_api_ip_punt_redirect_dump_t_handler (vl_api_ip_punt_redirect_dump_t * mp)
1930{
1931 vl_api_registration_t *reg;
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02001932 fib_protocol_t fproto;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001933
Pavel Kotucek609e1212018-11-27 09:59:44 +01001934 reg = vl_api_client_index_to_registration (mp->client_index);
1935 if (!reg)
1936 return;
1937
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02001938 fproto = (mp->is_ipv6 == 1) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001939
Neale Ranns8f5fef22020-12-21 08:29:34 +00001940 ip_walk_ctx_t ctx = {
Neale Ranns92207752019-06-03 13:21:40 +00001941 .reg = reg,
1942 .context = mp->context,
1943 };
1944
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02001945 vl_api_ip_punt_redirect_dump_common (&ctx, fproto, ntohl (mp->sw_if_index),
1946 send_ip_punt_redirect_details);
1947}
Neale Ranns92207752019-06-03 13:21:40 +00001948
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02001949static void
1950vl_api_ip_punt_redirect_v2_dump_t_handler (
1951 vl_api_ip_punt_redirect_v2_dump_t *mp)
1952{
1953 vl_api_registration_t *reg;
1954 ip_address_family_t af;
1955 fib_protocol_t fproto;
1956 int rv = 0;
Neale Ranns92207752019-06-03 13:21:40 +00001957
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02001958 reg = vl_api_client_index_to_registration (mp->client_index);
1959 if (!reg)
1960 return;
Neale Ranns92207752019-06-03 13:21:40 +00001961
Nathan Skrzypczak2a1783f2021-08-10 15:05:29 +02001962 rv = ip_address_family_decode (mp->af, &af);
1963 if (rv != 0)
1964 return;
1965
1966 fproto = (af == AF_IP6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
1967
1968 ip_walk_ctx_t ctx = {
1969 .reg = reg,
1970 .context = mp->context,
1971 };
1972
1973 vl_api_ip_punt_redirect_dump_common (&ctx, fproto, ntohl (mp->sw_if_index),
1974 send_ip_punt_redirect_v2_details);
Pavel Kotucek609e1212018-11-27 09:59:44 +01001975}
1976
Neale Ranns8f5fef22020-12-21 08:29:34 +00001977void
1978vl_api_ip_path_mtu_update_t_handler (vl_api_ip_path_mtu_update_t *mp)
1979{
1980 vl_api_ip_path_mtu_update_reply_t *rmp;
1981 ip_address_t nh;
1982 int rv = 0;
1983
1984 ip_address_decode2 (&mp->pmtu.nh, &nh);
1985
1986 rv = ip_path_mtu_update (&nh, ntohl (mp->pmtu.table_id),
1987 ntohs (mp->pmtu.path_mtu));
1988
1989 REPLY_MACRO (VL_API_IP_PATH_MTU_UPDATE_REPLY);
1990}
1991
1992void
1993vl_api_ip_path_mtu_replace_begin_t_handler (
1994 vl_api_ip_path_mtu_replace_begin_t *mp)
1995{
1996 vl_api_ip_path_mtu_replace_begin_reply_t *rmp;
1997 int rv;
1998
1999 rv = ip_path_mtu_replace_begin ();
2000
2001 REPLY_MACRO (VL_API_IP_PATH_MTU_REPLACE_BEGIN_REPLY);
2002}
2003
2004void
2005vl_api_ip_path_mtu_replace_end_t_handler (vl_api_ip_path_mtu_replace_end_t *mp)
2006{
2007 vl_api_ip_path_mtu_replace_end_reply_t *rmp;
2008 int rv;
2009
2010 rv = ip_path_mtu_replace_end ();
2011
2012 REPLY_MACRO (VL_API_IP_PATH_MTU_REPLACE_END_REPLY);
2013}
2014
2015static void
2016send_ip_path_mtu_details (index_t ipti, vl_api_registration_t *rp, u32 context)
2017{
2018 vl_api_ip_path_mtu_details_t *rmp;
2019 ip_address_t ip;
2020 ip_pmtu_t *ipt;
2021
2022 ipt = ip_path_mtu_get (ipti);
2023
2024 REPLY_MACRO_DETAILS4 (VL_API_IP_PATH_MTU_DETAILS, rp, context, ({
2025 ip_pmtu_get_ip (ipt, &ip);
2026 ip_address_encode2 (&ip, &rmp->pmtu.nh);
2027 rmp->pmtu.table_id =
2028 htonl (ip_pmtu_get_table_id (ipt));
2029 rmp->pmtu.path_mtu = htons (ipt->ipt_cfg_pmtu);
2030 }));
2031}
2032
2033static void
2034vl_api_ip_path_mtu_get_t_handler (vl_api_ip_path_mtu_get_t *mp)
2035{
2036 vl_api_ip_path_mtu_get_reply_t *rmp;
2037 i32 rv = 0;
2038
2039 REPLY_AND_DETAILS_MACRO (
2040 VL_API_IP_PATH_MTU_GET_REPLY, ip_pmtu_pool,
2041 ({ send_ip_path_mtu_details (cursor, rp, mp->context); }));
2042}
2043
Filip Tehlar5ff59a12021-06-23 14:38:38 +00002044#include <vnet/ip/ip.api.c>
Dave Barachb5e8a772016-12-06 12:04:42 -05002045
2046static clib_error_t *
2047ip_api_hookup (vlib_main_t * vm)
2048{
Dave Barach39d69112019-11-27 11:42:13 -05002049 api_main_t *am = vlibapi_get_main ();
Dave Barachb5e8a772016-12-06 12:04:42 -05002050
Dave Barachb5e8a772016-12-06 12:04:42 -05002051 /*
Neale Ranns6a231a12018-10-17 06:38:00 +00002052 * Mark the route add/del API as MP safe
2053 */
Neale Ranns097fa662018-05-01 05:17:55 -07002054 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL] = 1;
2055 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_REPLY] = 1;
Neale Ranns976b2592019-12-04 06:11:00 +00002056 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_V2] = 1;
2057 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_V2_REPLY] = 1;
Neale Ranns6a231a12018-10-17 06:38:00 +00002058
2059 /*
Dave Barachb5e8a772016-12-06 12:04:42 -05002060 * Set up the (msg_name, crc, message-id) table
2061 */
Filip Tehlar5ff59a12021-06-23 14:38:38 +00002062 REPLY_MSG_ID_BASE = setup_message_id_table ();
Dave Barachb5e8a772016-12-06 12:04:42 -05002063
Dave Barachb5e8a772016-12-06 12:04:42 -05002064 return 0;
2065}
2066
2067VLIB_API_INIT_FUNCTION (ip_api_hookup);
2068
2069/*
2070 * fd.io coding-style-patch-verification: ON
2071 *
2072 * Local Variables:
2073 * eval: (c-set-style "gnu")
2074 * End:
2075 */