blob: 91b12e5896a6d68071e5e705936eb30398131407 [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
51#include <vnet/vnet_msg_enum.h>
52
53#define vl_typedefs /* define message structures */
54#include <vnet/vnet_all_api_h.h>
55#undef vl_typedefs
56
57#define vl_endianfun /* define message structures */
58#include <vnet/vnet_all_api_h.h>
59#undef vl_endianfun
60
61/* instantiate all the print functions we know about */
62#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
63#define vl_printfun
64#include <vnet/vnet_all_api_h.h>
65#undef vl_printfun
66
67#include <vlibapi/api_helper_macros.h>
68
Ole Troan33a58172019-09-04 09:12:29 +020069#include <vnet/format_fns.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080070
Benoît Ganne58a19152021-01-18 19:24:34 +010071#define foreach_ip_api_msg \
72 _ (SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable) \
73 _ (IP_TABLE_DUMP, ip_table_dump) \
74 _ (IP_ROUTE_DUMP, ip_route_dump) \
Neale Ranns976b2592019-12-04 06:11:00 +000075 _ (IP_ROUTE_V2_DUMP, ip_route_v2_dump) \
Benoît Ganne58a19152021-01-18 19:24:34 +010076 _ (IP_MTABLE_DUMP, ip_mtable_dump) \
77 _ (IP_MROUTE_DUMP, ip_mroute_dump) \
78 _ (IP_MROUTE_ADD_DEL, ip_mroute_add_del) \
79 _ (MFIB_SIGNAL_DUMP, mfib_signal_dump) \
80 _ (IP_ADDRESS_DUMP, ip_address_dump) \
81 _ (IP_UNNUMBERED_DUMP, ip_unnumbered_dump) \
82 _ (IP_DUMP, ip_dump) \
83 _ (IP_TABLE_REPLACE_BEGIN, ip_table_replace_begin) \
84 _ (IP_TABLE_REPLACE_END, ip_table_replace_end) \
85 _ (IP_TABLE_FLUSH, ip_table_flush) \
86 _ (IP_ROUTE_ADD_DEL, ip_route_add_del) \
Neale Ranns976b2592019-12-04 06:11:00 +000087 _ (IP_ROUTE_ADD_DEL_V2, ip_route_add_del_v2) \
Benoît Ganne58a19152021-01-18 19:24:34 +010088 _ (IP_ROUTE_LOOKUP, ip_route_lookup) \
Neale Ranns976b2592019-12-04 06:11:00 +000089 _ (IP_ROUTE_LOOKUP_V2, ip_route_lookup_v2) \
Benoît Ganne58a19152021-01-18 19:24:34 +010090 _ (IP_TABLE_ADD_DEL, ip_table_add_del) \
91 _ (IP_PUNT_POLICE, ip_punt_police) \
92 _ (IP_PUNT_REDIRECT, ip_punt_redirect) \
93 _ (SET_IP_FLOW_HASH, set_ip_flow_hash) \
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +000094 _ (SET_IP_FLOW_HASH_V2, set_ip_flow_hash_v2) \
Neale Ranns3d5f08a2021-01-22 16:12:38 +000095 _ (SET_IP_FLOW_HASH_ROUTER_ID, set_ip_flow_hash_router_id) \
Benoît Ganne58a19152021-01-18 19:24:34 +010096 _ (IP_CONTAINER_PROXY_ADD_DEL, ip_container_proxy_add_del) \
97 _ (IP_CONTAINER_PROXY_DUMP, ip_container_proxy_dump) \
98 _ (IOAM_ENABLE, ioam_enable) \
99 _ (IOAM_DISABLE, ioam_disable) \
100 _ (IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL, \
101 ip_source_and_port_range_check_add_del) \
102 _ (IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL, \
103 ip_source_and_port_range_check_interface_add_del) \
104 _ (SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS, \
105 sw_interface_ip6_set_link_local_address) \
106 _ (SW_INTERFACE_IP6_GET_LINK_LOCAL_ADDRESS, \
107 sw_interface_ip6_get_link_local_address) \
108 _ (IP_REASSEMBLY_SET, ip_reassembly_set) \
109 _ (IP_REASSEMBLY_GET, ip_reassembly_get) \
110 _ (IP_REASSEMBLY_ENABLE_DISABLE, ip_reassembly_enable_disable) \
Neale Ranns8f5fef22020-12-21 08:29:34 +0000111 _ (IP_PUNT_REDIRECT_DUMP, ip_punt_redirect_dump) \
112 _ (IP_PATH_MTU_UPDATE, ip_path_mtu_update) \
113 _ (IP_PATH_MTU_REPLACE_BEGIN, ip_path_mtu_replace_begin) \
114 _ (IP_PATH_MTU_REPLACE_END, ip_path_mtu_replace_end) \
115 _ (IP_PATH_MTU_GET, ip_path_mtu_get)
Pavel Kotucek609e1212018-11-27 09:59:44 +0100116
Dave Barachb5e8a772016-12-06 12:04:42 -0500117static void
Neale Rannscbe25aa2019-09-30 10:53:31 +0000118 vl_api_sw_interface_ip6_enable_disable_t_handler
119 (vl_api_sw_interface_ip6_enable_disable_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500120{
Neale Rannscbe25aa2019-09-30 10:53:31 +0000121 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
122 int rv = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -0500123
Neale Rannscbe25aa2019-09-30 10:53:31 +0000124 VALIDATE_SW_IF_INDEX (mp);
Neale Ranns37029302018-08-10 05:30:06 -0700125
Neale Rannscbe25aa2019-09-30 10:53:31 +0000126 rv = ((mp->enable == 1) ?
Neale Rannsec40a7d2020-04-23 07:36:12 +0000127 ip6_link_enable (ntohl (mp->sw_if_index), NULL) :
Neale Rannscbe25aa2019-09-30 10:53:31 +0000128 ip6_link_disable (ntohl (mp->sw_if_index)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500129
Neale Rannscbe25aa2019-09-30 10:53:31 +0000130 BAD_SW_IF_INDEX_LABEL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500131
Neale Rannscbe25aa2019-09-30 10:53:31 +0000132 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
Dave Barachb5e8a772016-12-06 12:04:42 -0500133}
134
Dave Barachb5e8a772016-12-06 12:04:42 -0500135static void
Neale Ranns097fa662018-05-01 05:17:55 -0700136send_ip_table_details (vpe_api_main_t * am,
137 vl_api_registration_t * reg,
138 u32 context, const fib_table_t * table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500139{
Neale Ranns097fa662018-05-01 05:17:55 -0700140 vl_api_ip_table_details_t *mp;
Dave Barachb5e8a772016-12-06 12:04:42 -0500141
Neale Ranns097fa662018-05-01 05:17:55 -0700142 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500143 if (!mp)
144 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400145 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns097fa662018-05-01 05:17:55 -0700146 mp->_vl_msg_id = ntohs (VL_API_IP_TABLE_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -0500147 mp->context = context;
148
Neale Ranns097fa662018-05-01 05:17:55 -0700149 mp->table.is_ip6 = (table->ft_proto == FIB_PROTOCOL_IP6);
150 mp->table.table_id = htonl (table->ft_table_id);
151 memcpy (mp->table.name, table->ft_desc,
152 clib_min (vec_len (table->ft_desc), sizeof (mp->table.name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500153
Florin Coras6c4dae22018-01-09 06:39:23 -0800154 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500155}
156
Neale Ranns097fa662018-05-01 05:17:55 -0700157static void
158vl_api_ip_table_dump_t_handler (vl_api_ip_table_dump_t * mp)
159{
160 vpe_api_main_t *am = &vpe_api_main;
161 vl_api_registration_t *reg;
162 fib_table_t *fib_table;
163
164 reg = vl_api_client_index_to_registration (mp->client_index);
165 if (!reg)
166 return;
167
168 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100169 pool_foreach (fib_table, ip4_main.fibs)
170 {
Neale Ranns097fa662018-05-01 05:17:55 -0700171 send_ip_table_details(am, reg, mp->context, fib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100172 }
173 pool_foreach (fib_table, ip6_main.fibs)
174 {
Neale Ranns097fa662018-05-01 05:17:55 -0700175 /* don't send link locals */
176 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
177 continue;
178 send_ip_table_details(am, reg, mp->context, fib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100179 }
Neale Ranns097fa662018-05-01 05:17:55 -0700180 /* *INDENT-ON* */
181}
182
Neale Rannsa3af3372017-03-28 03:49:52 -0700183typedef struct vl_api_ip_fib_dump_walk_ctx_t_
184{
185 fib_node_index_t *feis;
186} vl_api_ip_fib_dump_walk_ctx_t;
187
Neale Ranns89541992017-04-06 04:41:02 -0700188static fib_table_walk_rc_t
Neale Rannsa3af3372017-03-28 03:49:52 -0700189vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
190{
191 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
192
193 vec_add1 (ctx->feis, fei);
194
Neale Ranns89541992017-04-06 04:41:02 -0700195 return (FIB_TABLE_WALK_CONTINUE);
Neale Rannsa3af3372017-03-28 03:49:52 -0700196}
197
Dave Barachb5e8a772016-12-06 12:04:42 -0500198static void
Neale Ranns097fa662018-05-01 05:17:55 -0700199send_ip_route_details (vpe_api_main_t * am,
200 vl_api_registration_t * reg,
201 u32 context, fib_node_index_t fib_entry_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500202{
Neale Ranns097fa662018-05-01 05:17:55 -0700203 fib_route_path_t *rpaths, *rpath;
204 vl_api_ip_route_details_t *mp;
Neale Rannsc5d43172018-07-30 08:04:40 -0700205 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500206 vl_api_fib_path_t *fp;
207 int path_count;
208
Neale Ranns097fa662018-05-01 05:17:55 -0700209 rpaths = NULL;
210 pfx = fib_entry_get_prefix (fib_entry_index);
211 rpaths = fib_entry_encode (fib_entry_index);
212
213 path_count = vec_len (rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500214 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
215 if (!mp)
216 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400217 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns097fa662018-05-01 05:17:55 -0700218 mp->_vl_msg_id = ntohs (VL_API_IP_ROUTE_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -0500219 mp->context = context;
220
Neale Ranns097fa662018-05-01 05:17:55 -0700221 ip_prefix_encode (pfx, &mp->route.prefix);
222 mp->route.table_id =
223 htonl (fib_table_get_table_id
224 (fib_entry_get_fib_index (fib_entry_index), pfx->fp_proto));
225 mp->route.n_paths = path_count;
226 mp->route.stats_index =
227 htonl (fib_table_entry_get_stats_index
228 (fib_entry_get_fib_index (fib_entry_index), pfx));
Dave Barachb5e8a772016-12-06 12:04:42 -0500229
Neale Ranns097fa662018-05-01 05:17:55 -0700230 fp = mp->route.paths;
231 vec_foreach (rpath, rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500232 {
Neale Ranns097fa662018-05-01 05:17:55 -0700233 fib_api_path_encode (rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500234 fp++;
235 }
236
Florin Coras6c4dae22018-01-09 06:39:23 -0800237 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns9db6ada2019-11-08 12:42:31 +0000238 vec_free (rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500239}
240
Neale Ranns976b2592019-12-04 06:11:00 +0000241static void
242send_ip_route_v2_details (vpe_api_main_t *am, vl_api_registration_t *reg,
243 u32 context, fib_node_index_t fib_entry_index)
244{
245 fib_route_path_t *rpaths, *rpath;
246 vl_api_ip_route_v2_details_t *mp;
247 const fib_prefix_t *pfx;
248 vl_api_fib_path_t *fp;
249 int path_count;
250
251 rpaths = NULL;
252 pfx = fib_entry_get_prefix (fib_entry_index);
253 rpaths = fib_entry_encode (fib_entry_index);
254
255 path_count = vec_len (rpaths);
256 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
257 if (!mp)
258 return;
259 clib_memset (mp, 0, sizeof (*mp));
260 mp->_vl_msg_id = ntohs (VL_API_IP_ROUTE_V2_DETAILS);
261 mp->context = context;
262
263 ip_prefix_encode (pfx, &mp->route.prefix);
264 mp->route.table_id = htonl (fib_table_get_table_id (
265 fib_entry_get_fib_index (fib_entry_index), pfx->fp_proto));
266 mp->route.n_paths = path_count;
267 mp->route.src = fib_entry_get_best_source (fib_entry_index);
268 mp->route.stats_index = htonl (fib_table_entry_get_stats_index (
269 fib_entry_get_fib_index (fib_entry_index), pfx));
270
271 fp = mp->route.paths;
272 vec_foreach (rpath, rpaths)
273 {
274 fib_api_path_encode (rpath, fp);
275 fp++;
276 }
277
278 vl_api_send_msg (reg, (u8 *) mp);
279 vec_free (rpaths);
280}
281
Dave Barachd7cb1b52016-12-09 09:52:16 -0500282typedef struct apt_ip6_fib_show_ctx_t_
283{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500284 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500285} api_ip6_fib_show_ctx_t;
286
Dave Barachb5e8a772016-12-06 12:04:42 -0500287static void
Neale Ranns097fa662018-05-01 05:17:55 -0700288vl_api_ip_route_dump_t_handler (vl_api_ip_route_dump_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500289{
290 vpe_api_main_t *am = &vpe_api_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500291 fib_node_index_t *fib_entry_index;
Florin Coras6c4dae22018-01-09 06:39:23 -0800292 vl_api_registration_t *reg;
Neale Ranns097fa662018-05-01 05:17:55 -0700293 fib_protocol_t fproto;
294 u32 fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -0500295
Florin Coras6c4dae22018-01-09 06:39:23 -0800296 reg = vl_api_client_index_to_registration (mp->client_index);
297 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500298 return;
299
Neale Ranns097fa662018-05-01 05:17:55 -0700300 vl_api_ip_fib_dump_walk_ctx_t ctx = {
301 .feis = NULL,
302 };
Neale Ranns81458422018-03-12 06:59:36 -0700303
Neale Ranns097fa662018-05-01 05:17:55 -0700304 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
305 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
306
307 if (INDEX_INVALID == fib_index)
308 return;
309
310 fib_table_walk (fib_index, fproto, vl_api_ip_fib_dump_walk, &ctx);
311
312 vec_foreach (fib_entry_index, ctx.feis)
313 {
314 send_ip_route_details (am, reg, mp->context, *fib_entry_index);
315 }
316
317 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500318}
319
320static void
Neale Ranns976b2592019-12-04 06:11:00 +0000321vl_api_ip_route_v2_dump_t_handler (vl_api_ip_route_v2_dump_t *mp)
322{
323 vpe_api_main_t *am = &vpe_api_main;
324 fib_node_index_t *fib_entry_index;
325 vl_api_registration_t *reg;
326 fib_protocol_t fproto;
327 fib_source_t src;
328 u32 fib_index;
329
330 reg = vl_api_client_index_to_registration (mp->client_index);
331 if (!reg)
332 return;
333
334 vl_api_ip_fib_dump_walk_ctx_t ctx = {
335 .feis = NULL,
336 };
337
338 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
339 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
340 src = mp->src;
341
342 if (INDEX_INVALID == fib_index)
343 return;
344
345 if (src)
346 fib_table_walk_w_src (fib_index, fproto, src, vl_api_ip_fib_dump_walk,
347 &ctx);
348 else
349 fib_table_walk (fib_index, fproto, vl_api_ip_fib_dump_walk, &ctx);
350
351 vec_foreach (fib_entry_index, ctx.feis)
352 {
353 send_ip_route_v2_details (am, reg, mp->context, *fib_entry_index);
354 }
355
356 vec_free (ctx.feis);
357}
358
359static void
Neale Ranns097fa662018-05-01 05:17:55 -0700360send_ip_mtable_details (vl_api_registration_t * reg,
361 u32 context, const mfib_table_t * mfib_table)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800362{
Neale Ranns097fa662018-05-01 05:17:55 -0700363 vl_api_ip_mtable_details_t *mp;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800364
Neale Ranns097fa662018-05-01 05:17:55 -0700365 mp = vl_msg_api_alloc (sizeof (*mp));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800366 if (!mp)
367 return;
Neale Ranns097fa662018-05-01 05:17:55 -0700368 memset (mp, 0, sizeof (*mp));
369 mp->_vl_msg_id = ntohs (VL_API_IP_MTABLE_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800370 mp->context = context;
371
Neale Ranns097fa662018-05-01 05:17:55 -0700372 mp->table.table_id = htonl (mfib_table->mft_table_id);
373 mp->table.is_ip6 = (FIB_PROTOCOL_IP6 == mfib_table->mft_proto);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800374
Florin Coras6c4dae22018-01-09 06:39:23 -0800375 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800376}
377
Neale Ranns5a8123b2017-01-26 01:18:23 -0800378static void
Neale Ranns097fa662018-05-01 05:17:55 -0700379vl_api_ip_mtable_dump_t_handler (vl_api_ip_mtable_dump_t * mp)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800380{
Florin Coras6c4dae22018-01-09 06:39:23 -0800381 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800382 mfib_table_t *mfib_table;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800383
Florin Coras6c4dae22018-01-09 06:39:23 -0800384 reg = vl_api_client_index_to_registration (mp->client_index);
385 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800386 return;
387
Neale Ranns5a8123b2017-01-26 01:18:23 -0800388 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100389 pool_foreach (mfib_table, ip4_main.mfibs)
390 {
Neale Ranns097fa662018-05-01 05:17:55 -0700391 send_ip_mtable_details (reg, mp->context, mfib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100392 }
393 pool_foreach (mfib_table, ip6_main.mfibs)
394 {
Neale Ranns097fa662018-05-01 05:17:55 -0700395 send_ip_mtable_details (reg, mp->context, mfib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100396 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800397 /* *INDENT-ON* */
Neale Ranns5a8123b2017-01-26 01:18:23 -0800398}
399
Neale Ranns097fa662018-05-01 05:17:55 -0700400typedef struct vl_api_ip_mfib_dump_ctx_t_
Neale Ranns5a8123b2017-01-26 01:18:23 -0800401{
402 fib_node_index_t *entries;
Neale Ranns097fa662018-05-01 05:17:55 -0700403} vl_api_ip_mfib_dump_ctx_t;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800404
Neale Ranns9db6ada2019-11-08 12:42:31 +0000405static walk_rc_t
Neale Ranns097fa662018-05-01 05:17:55 -0700406mfib_route_dump_walk (fib_node_index_t fei, void *arg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800407{
Neale Ranns097fa662018-05-01 05:17:55 -0700408 vl_api_ip_mfib_dump_ctx_t *ctx = arg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800409
410 vec_add1 (ctx->entries, fei);
411
Neale Ranns9db6ada2019-11-08 12:42:31 +0000412 return (WALK_CONTINUE);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800413}
414
415static void
Neale Ranns097fa662018-05-01 05:17:55 -0700416send_ip_mroute_details (vpe_api_main_t * am,
417 vl_api_registration_t * reg,
418 u32 context, fib_node_index_t mfib_entry_index)
419{
420 fib_route_path_t *rpaths, *rpath;
421 vl_api_ip_mroute_details_t *mp;
422 const mfib_prefix_t *pfx;
423 vl_api_mfib_path_t *fp;
Matthew Smitha2bbeb82020-09-17 16:33:10 -0500424 u8 path_count;
Neale Ranns097fa662018-05-01 05:17:55 -0700425
426 rpaths = NULL;
427 pfx = mfib_entry_get_prefix (mfib_entry_index);
428 rpaths = mfib_entry_encode (mfib_entry_index);
429
430 path_count = vec_len (rpaths);
431 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
432 if (!mp)
433 return;
434 clib_memset (mp, 0, sizeof (*mp));
435 mp->_vl_msg_id = ntohs (VL_API_IP_MROUTE_DETAILS);
436 mp->context = context;
437
438 ip_mprefix_encode (pfx, &mp->route.prefix);
439 mp->route.table_id =
440 htonl (mfib_table_get_table_id
441 (mfib_entry_get_fib_index (mfib_entry_index), pfx->fp_proto));
Matthew Smitha2bbeb82020-09-17 16:33:10 -0500442 mp->route.n_paths = path_count;
Neale Ranns097fa662018-05-01 05:17:55 -0700443 fp = mp->route.paths;
444 vec_foreach (rpath, rpaths)
445 {
446 mfib_api_path_encode (rpath, fp);
447 fp++;
448 }
449
450 vl_api_send_msg (reg, (u8 *) mp);
451 vec_free (rpaths);
452}
453
454static void
455vl_api_ip_mroute_dump_t_handler (vl_api_ip_mroute_dump_t * mp)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800456{
457 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800458 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800459 fib_node_index_t *mfeip;
Neale Ranns097fa662018-05-01 05:17:55 -0700460 fib_protocol_t fproto;
461 u32 fib_index;
462
463 vl_api_ip_mfib_dump_ctx_t ctx = {
Neale Ranns5a8123b2017-01-26 01:18:23 -0800464 .entries = NULL,
465 };
466
Florin Coras6c4dae22018-01-09 06:39:23 -0800467 reg = vl_api_client_index_to_registration (mp->client_index);
468 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800469 return;
470
Neale Ranns097fa662018-05-01 05:17:55 -0700471 fproto = fib_ip_proto (mp->table.is_ip6);
472 fib_index = mfib_table_find (fproto, ntohl (mp->table.table_id));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800473
Neale Ranns097fa662018-05-01 05:17:55 -0700474 if (INDEX_INVALID == fib_index)
475 return;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800476
Neale Ranns097fa662018-05-01 05:17:55 -0700477 mfib_table_walk (fib_index, fproto, mfib_route_dump_walk, &ctx);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800478
Neale Ranns097fa662018-05-01 05:17:55 -0700479 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800480
Neale Ranns097fa662018-05-01 05:17:55 -0700481 vec_foreach (mfeip, ctx.entries)
482 {
483 send_ip_mroute_details (am, reg, mp->context, *mfeip);
484 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800485
486 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800487}
488
489static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700490vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
491 vlib_main_t * vm)
492{
493 vl_api_ip_punt_police_reply_t *rmp;
494 int rv = 0;
495
496 if (mp->is_ip6)
497 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
498 else
499 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
500
501 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
502}
503
504static void
505vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
506 vlib_main_t * vm)
507{
508 vl_api_ip_punt_redirect_reply_t *rmp;
509 int rv = 0;
Pavel Kotucek609e1212018-11-27 09:59:44 +0100510 ip46_type_t ipv;
511 ip46_address_t nh;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700512
Pavel Kotucek609e1212018-11-27 09:59:44 +0100513 if (!vnet_sw_if_index_is_api_valid (ntohl (mp->punt.tx_sw_if_index)))
514 goto bad_sw_if_index;
515
516 ipv = ip_address_decode (&mp->punt.nh, &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700517 if (mp->is_add)
518 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100519 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700520 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100521 ip6_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
522 ntohl (mp->punt.tx_sw_if_index), &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700523 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100524 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700525 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100526 ip4_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
527 ntohl (mp->punt.tx_sw_if_index), &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700528 }
529 }
530 else
531 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100532 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700533 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100534 ip6_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700535 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100536 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700537 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100538 ip4_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700539 }
540 }
541
Pavel Kotucek609e1212018-11-27 09:59:44 +0100542 BAD_SW_IF_INDEX_LABEL;
543
Neale Rannsd91c1db2017-07-31 02:30:50 -0700544 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
545}
546
Steven Luongc3ed1c92020-07-27 10:06:58 -0700547static clib_error_t *
548call_elf_section_ip_table_callbacks (vnet_main_t * vnm, u32 table_id,
549 u32 flags,
550 _vnet_ip_table_function_list_elt_t **
551 elts)
552{
553 _vnet_ip_table_function_list_elt_t *elt;
554 vnet_ip_table_function_priority_t prio;
555 clib_error_t *error = 0;
556
557 for (prio = VNET_IP_TABLE_FUNC_PRIORITY_LOW;
558 prio <= VNET_IP_TABLE_FUNC_PRIORITY_HIGH; prio++)
559 {
560 elt = elts[prio];
561
562 while (elt)
563 {
564 error = elt->fp (vnm, table_id, flags);
565 if (error)
566 return error;
567 elt = elt->next_ip_table_function;
568 }
569 }
570 return error;
571}
572
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700573void
Neale Ranns15002542017-09-10 04:39:11 -0700574ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
575{
576 u32 fib_index, mfib_index;
Steven Luongc3ed1c92020-07-27 10:06:58 -0700577 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns15002542017-09-10 04:39:11 -0700578
579 /*
580 * ignore action on the default table - this is always present
581 * and cannot be added nor deleted from the API
582 */
583 if (0 != table_id)
584 {
585 /*
586 * The API holds only one lock on the table.
587 * i.e. it can be added many times via the API but needs to be
588 * deleted only once.
589 * The FIB index for unicast and multicast is not necessarily the
590 * same, since internal VPP systesm (like LISP and SR) create
591 * their own unicast tables.
592 */
593 fib_index = fib_table_find (fproto, table_id);
594 mfib_index = mfib_table_find (fproto, table_id);
595
Steven Luongc3ed1c92020-07-27 10:06:58 -0700596 if ((~0 != fib_index) || (~0 != mfib_index))
597 call_elf_section_ip_table_callbacks (vnm, table_id, 0 /* is_add */ ,
598 vnm->ip_table_add_del_functions);
599
Neale Ranns15002542017-09-10 04:39:11 -0700600 if (~0 != fib_index)
601 {
602 fib_table_unlock (fib_index, fproto,
603 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
604 }
605 if (~0 != mfib_index)
606 {
607 mfib_table_unlock (mfib_index, fproto,
608 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
609 }
610 }
611}
612
613void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700614vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
615{
616 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns097fa662018-05-01 05:17:55 -0700617 fib_protocol_t fproto = (mp->table.is_ip6 ?
618 FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
619 u32 table_id = ntohl (mp->table.table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700620 int rv = 0;
621
Neale Ranns15002542017-09-10 04:39:11 -0700622 if (mp->is_add)
623 {
Neale Ranns097fa662018-05-01 05:17:55 -0700624 ip_table_create (fproto, table_id, 1, mp->table.name);
Neale Ranns15002542017-09-10 04:39:11 -0700625 }
626 else
627 {
628 ip_table_delete (fproto, table_id, 1);
629 }
630
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700631 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
632}
633
Dave Barachb5e8a772016-12-06 12:04:42 -0500634static int
Neale Ranns097fa662018-05-01 05:17:55 -0700635ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp, u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500636{
Neale Ranns097fa662018-05-01 05:17:55 -0700637 fib_route_path_t *rpaths = NULL, *rpath;
638 fib_entry_flag_t entry_flags;
639 vl_api_fib_path_t *apath;
640 fib_prefix_t pfx;
641 u32 fib_index;
642 int rv, ii;
Dave Barachb5e8a772016-12-06 12:04:42 -0500643
Neale Ranns097fa662018-05-01 05:17:55 -0700644 entry_flags = FIB_ENTRY_FLAG_NONE;
645 ip_prefix_decode (&mp->route.prefix, &pfx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500646
Neale Ranns097fa662018-05-01 05:17:55 -0700647 rv = fib_api_table_id_decode (pfx.fp_proto,
648 ntohl (mp->route.table_id), &fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500649 if (0 != rv)
Neale Ranns097fa662018-05-01 05:17:55 -0700650 goto out;
Dave Barachb5e8a772016-12-06 12:04:42 -0500651
Neale Rannsc2ac2352019-07-02 14:33:29 +0000652 if (0 != mp->route.n_paths)
653 vec_validate (rpaths, mp->route.n_paths - 1);
Neale Ranns097fa662018-05-01 05:17:55 -0700654
655 for (ii = 0; ii < mp->route.n_paths; ii++)
656 {
657 apath = &mp->route.paths[ii];
658 rpath = &rpaths[ii];
659
660 rv = fib_api_path_decode (apath, rpath);
661
662 if ((rpath->frp_flags & FIB_ROUTE_PATH_LOCAL) &&
663 (~0 == rpath->frp_sw_if_index))
664 entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
665
666 if (0 != rv)
667 goto out;
668 }
669
Neale Ranns976b2592019-12-04 06:11:00 +0000670 rv = fib_api_route_add_del (mp->is_add, mp->is_multipath, fib_index, &pfx,
671 FIB_SOURCE_API, entry_flags, rpaths);
672
673 if (mp->is_add && 0 == rv)
674 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
675
676out:
677 vec_free (rpaths);
678
679 return (rv);
680}
681
682static int
683ip_route_add_del_v2_t_handler (vl_api_ip_route_add_del_v2_t *mp,
684 u32 *stats_index)
685{
686 fib_route_path_t *rpaths = NULL, *rpath;
687 fib_entry_flag_t entry_flags;
688 vl_api_fib_path_t *apath;
689 fib_source_t src;
690 fib_prefix_t pfx;
691 u32 fib_index;
692 int rv, ii;
693
694 entry_flags = FIB_ENTRY_FLAG_NONE;
695 ip_prefix_decode (&mp->route.prefix, &pfx);
696
697 rv = fib_api_table_id_decode (pfx.fp_proto, ntohl (mp->route.table_id),
698 &fib_index);
699 if (0 != rv)
700 goto out;
701
702 if (0 != mp->route.n_paths)
703 vec_validate (rpaths, mp->route.n_paths - 1);
704
705 for (ii = 0; ii < mp->route.n_paths; ii++)
706 {
707 apath = &mp->route.paths[ii];
708 rpath = &rpaths[ii];
709
710 rv = fib_api_path_decode (apath, rpath);
711
712 if ((rpath->frp_flags & FIB_ROUTE_PATH_LOCAL) &&
713 (~0 == rpath->frp_sw_if_index))
714 entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
715
716 if (0 != rv)
717 goto out;
718 }
719
720 src = (0 == mp->route.src ? FIB_SOURCE_API : mp->route.src);
721
722 rv = fib_api_route_add_del (mp->is_add, mp->is_multipath, fib_index, &pfx,
723 src, entry_flags, rpaths);
Neale Ranns008dbe12018-09-07 09:32:36 -0700724
725 if (mp->is_add && 0 == rv)
726 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
727
Neale Ranns097fa662018-05-01 05:17:55 -0700728out:
729 vec_free (rpaths);
Neale Ranns008dbe12018-09-07 09:32:36 -0700730
731 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -0500732}
733
734void
Neale Ranns097fa662018-05-01 05:17:55 -0700735vl_api_ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500736{
Neale Ranns097fa662018-05-01 05:17:55 -0700737 vl_api_ip_route_add_del_reply_t *rmp;
738 u32 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -0500739 int rv;
Dave Barachb5e8a772016-12-06 12:04:42 -0500740
Neale Ranns097fa662018-05-01 05:17:55 -0700741 rv = ip_route_add_del_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500742
Neale Ranns008dbe12018-09-07 09:32:36 -0700743 /* *INDENT-OFF* */
Neale Ranns097fa662018-05-01 05:17:55 -0700744 REPLY_MACRO2 (VL_API_IP_ROUTE_ADD_DEL_REPLY,
Neale Ranns008dbe12018-09-07 09:32:36 -0700745 ({
746 rmp->stats_index = htonl (stats_index);
747 }))
748 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500749}
750
Neale Ranns15002542017-09-10 04:39:11 -0700751void
Neale Ranns976b2592019-12-04 06:11:00 +0000752vl_api_ip_route_add_del_v2_t_handler (vl_api_ip_route_add_del_v2_t *mp)
753{
754 vl_api_ip_route_add_del_v2_reply_t *rmp;
755 u32 stats_index = ~0;
756 int rv;
757
758 rv = ip_route_add_del_v2_t_handler (mp, &stats_index);
759
760 /* clang-format off */
761 REPLY_MACRO2 (VL_API_IP_ROUTE_ADD_DEL_V2_REPLY,
762 ({
763 rmp->stats_index = htonl (stats_index);
764 }))
765 /* clang-format on */
766}
767
768void
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400769vl_api_ip_route_lookup_t_handler (vl_api_ip_route_lookup_t * mp)
770{
771 vl_api_ip_route_lookup_reply_t *rmp = NULL;
772 fib_route_path_t *rpaths = NULL, *rpath;
773 const fib_prefix_t *pfx = NULL;
774 fib_prefix_t lookup;
775 vl_api_fib_path_t *fp;
776 fib_node_index_t fib_entry_index;
777 u32 fib_index;
778 int npaths = 0;
779 int rv;
780
781 ip_prefix_decode (&mp->prefix, &lookup);
782 rv = fib_api_table_id_decode (lookup.fp_proto, ntohl (mp->table_id),
783 &fib_index);
784 if (PREDICT_TRUE (!rv))
785 {
786 if (mp->exact)
787 fib_entry_index = fib_table_lookup_exact_match (fib_index, &lookup);
788 else
789 fib_entry_index = fib_table_lookup (fib_index, &lookup);
790 if (fib_entry_index == FIB_NODE_INDEX_INVALID)
791 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
792 else
793 {
794 pfx = fib_entry_get_prefix (fib_entry_index);
795 rpaths = fib_entry_encode (fib_entry_index);
796 npaths = vec_len (rpaths);
797 }
798 }
799
800 /* *INDENT-OFF* */
801 REPLY_MACRO3_ZERO(VL_API_IP_ROUTE_LOOKUP_REPLY,
802 npaths * sizeof (*fp),
803 ({
804 if (!rv)
805 {
806 ip_prefix_encode (pfx, &rmp->route.prefix);
807 rmp->route.table_id = mp->table_id;
808 rmp->route.n_paths = npaths;
809 rmp->route.stats_index = fib_table_entry_get_stats_index (fib_index, pfx);
810 rmp->route.stats_index = htonl (rmp->route.stats_index);
811
812 fp = rmp->route.paths;
813 vec_foreach (rpath, rpaths)
814 {
815 fib_api_path_encode (rpath, fp);
816 fp++;
817 }
818 }
819 }));
820 /* *INDENT-ON* */
821 vec_free (rpaths);
822}
823
824void
Neale Ranns976b2592019-12-04 06:11:00 +0000825vl_api_ip_route_lookup_v2_t_handler (vl_api_ip_route_lookup_v2_t *mp)
826{
827 vl_api_ip_route_lookup_v2_reply_t *rmp = NULL;
828 fib_route_path_t *rpaths = NULL, *rpath;
829 const fib_prefix_t *pfx = NULL;
830 fib_prefix_t lookup;
831 vl_api_fib_path_t *fp;
832 fib_node_index_t fib_entry_index;
833 u32 fib_index;
834 int npaths = 0;
835 fib_source_t src = 0;
836 int rv;
837
838 ip_prefix_decode (&mp->prefix, &lookup);
839 rv = fib_api_table_id_decode (lookup.fp_proto, ntohl (mp->table_id),
840 &fib_index);
841 if (PREDICT_TRUE (!rv))
842 {
843 if (mp->exact)
844 fib_entry_index = fib_table_lookup_exact_match (fib_index, &lookup);
845 else
846 fib_entry_index = fib_table_lookup (fib_index, &lookup);
847 if (fib_entry_index == FIB_NODE_INDEX_INVALID)
848 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
849 else
850 {
851 pfx = fib_entry_get_prefix (fib_entry_index);
852 rpaths = fib_entry_encode (fib_entry_index);
853 npaths = vec_len (rpaths);
854 src = fib_entry_get_best_source (fib_entry_index);
855 }
856 }
857
858 /* clang-format off */
859 REPLY_MACRO3_ZERO(VL_API_IP_ROUTE_LOOKUP_V2_REPLY,
860 npaths * sizeof (*fp),
861 ({
862 if (!rv)
863 {
864 ip_prefix_encode (pfx, &rmp->route.prefix);
865 rmp->route.table_id = mp->table_id;
866 rmp->route.n_paths = npaths;
867 rmp->route.src = src;
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 }));
879 /* clang-format on */
880 vec_free (rpaths);
881}
882
883void
Neale Ranns2297af02017-09-12 09:45:04 -0700884ip_table_create (fib_protocol_t fproto,
885 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -0700886{
887 u32 fib_index, mfib_index;
Steven Luongc3ed1c92020-07-27 10:06:58 -0700888 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns15002542017-09-10 04:39:11 -0700889
890 /*
891 * ignore action on the default table - this is always present
892 * and cannot be added nor deleted from the API
893 */
894 if (0 != table_id)
895 {
896 /*
897 * The API holds only one lock on the table.
898 * i.e. it can be added many times via the API but needs to be
899 * deleted only once.
900 * The FIB index for unicast and multicast is not necessarily the
901 * same, since internal VPP systesm (like LISP and SR) create
902 * their own unicast tables.
903 */
904 fib_index = fib_table_find (fproto, table_id);
905 mfib_index = mfib_table_find (fproto, table_id);
906
907 if (~0 == fib_index)
908 {
Neale Ranns2297af02017-09-12 09:45:04 -0700909 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
910 (is_api ?
911 FIB_SOURCE_API :
912 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -0700913 }
914 if (~0 == mfib_index)
915 {
Neale Ranns2297af02017-09-12 09:45:04 -0700916 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
917 (is_api ?
918 MFIB_SOURCE_API :
919 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -0700920 }
Steven Luongc3ed1c92020-07-27 10:06:58 -0700921
922 if ((~0 == fib_index) || (~0 == mfib_index))
923 call_elf_section_ip_table_callbacks (vnm, table_id, 1 /* is_add */ ,
924 vnm->ip_table_add_del_functions);
Neale Ranns15002542017-09-10 04:39:11 -0700925 }
926}
927
Neale Ranns097fa662018-05-01 05:17:55 -0700928static u32
Neale Ranns32e1c012016-11-22 17:07:28 +0000929mroute_add_del_handler (u8 is_add,
Neale Ranns097fa662018-05-01 05:17:55 -0700930 u8 is_multipath,
Neale Ranns32e1c012016-11-22 17:07:28 +0000931 u32 fib_index,
932 const mfib_prefix_t * prefix,
933 u32 entry_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700934 u32 rpf_id, fib_route_path_t * rpaths)
Neale Ranns32e1c012016-11-22 17:07:28 +0000935{
Neale Ranns097fa662018-05-01 05:17:55 -0700936 u32 mfib_entry_index = ~0;
Neale Ranns28c142e2018-09-07 09:37:07 -0700937
Neale Ranns097fa662018-05-01 05:17:55 -0700938 if (0 == vec_len (rpaths))
Neale Ranns32e1c012016-11-22 17:07:28 +0000939 {
Neale Ranns28c142e2018-09-07 09:37:07 -0700940 mfib_entry_index = mfib_table_entry_update (fib_index, prefix,
941 MFIB_SOURCE_API,
942 rpf_id, entry_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +0000943 }
944 else
945 {
Neale Ranns097fa662018-05-01 05:17:55 -0700946 if (is_add)
947 {
948 mfib_entry_index =
949 mfib_table_entry_paths_update (fib_index, prefix,
950 MFIB_SOURCE_API, rpaths);
951 }
952 else
953 {
954 mfib_table_entry_paths_remove (fib_index, prefix,
955 MFIB_SOURCE_API, rpaths);
956 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000957 }
958
Neale Ranns28c142e2018-09-07 09:37:07 -0700959 return (mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000960}
961
962static int
Neale Ranns28c142e2018-09-07 09:37:07 -0700963api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp,
964 u32 * stats_index)
Neale Ranns32e1c012016-11-22 17:07:28 +0000965{
Neale Ranns097fa662018-05-01 05:17:55 -0700966 fib_route_path_t *rpath, *rpaths = NULL;
Neale Ranns28c142e2018-09-07 09:37:07 -0700967 fib_node_index_t mfib_entry_index;
Neale Ranns990f6942020-10-20 07:20:17 +0000968 mfib_entry_flags_t eflags;
Neale Ranns097fa662018-05-01 05:17:55 -0700969 mfib_prefix_t pfx;
Neale Ranns32e1c012016-11-22 17:07:28 +0000970 u32 fib_index;
971 int rv;
Neale Ranns097fa662018-05-01 05:17:55 -0700972 u16 ii;
Neale Ranns32e1c012016-11-22 17:07:28 +0000973
Neale Ranns097fa662018-05-01 05:17:55 -0700974 ip_mprefix_decode (&mp->route.prefix, &pfx);
Neale Ranns32e1c012016-11-22 17:07:28 +0000975
Neale Ranns097fa662018-05-01 05:17:55 -0700976 rv = mfib_api_table_id_decode (pfx.fp_proto,
977 ntohl (mp->route.table_id), &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000978 if (0 != rv)
Neale Ranns097fa662018-05-01 05:17:55 -0700979 goto out;
Neale Ranns32e1c012016-11-22 17:07:28 +0000980
Neale Ranns097fa662018-05-01 05:17:55 -0700981 vec_validate (rpaths, mp->route.n_paths - 1);
Neale Ranns32e1c012016-11-22 17:07:28 +0000982
Neale Ranns097fa662018-05-01 05:17:55 -0700983 for (ii = 0; ii < mp->route.n_paths; ii++)
Neale Ranns32e1c012016-11-22 17:07:28 +0000984 {
Neale Ranns097fa662018-05-01 05:17:55 -0700985 rpath = &rpaths[ii];
986
987 rv = mfib_api_path_decode (&mp->route.paths[ii], rpath);
988
989 if (0 != rv)
990 goto out;
Neale Ranns32e1c012016-11-22 17:07:28 +0000991 }
992
Neale Ranns990f6942020-10-20 07:20:17 +0000993 eflags = mfib_api_path_entry_flags_decode (mp->route.entry_flags);
Neale Ranns28c142e2018-09-07 09:37:07 -0700994 mfib_entry_index = mroute_add_del_handler (mp->is_add,
Neale Ranns097fa662018-05-01 05:17:55 -0700995 mp->is_add,
Neale Ranns28c142e2018-09-07 09:37:07 -0700996 fib_index, &pfx,
Neale Ranns990f6942020-10-20 07:20:17 +0000997 eflags,
Neale Ranns097fa662018-05-01 05:17:55 -0700998 ntohl (mp->route.rpf_id),
999 rpaths);
Neale Ranns28c142e2018-09-07 09:37:07 -07001000
1001 if (~0 != mfib_entry_index)
1002 *stats_index = mfib_entry_get_stats_index (mfib_entry_index);
1003
Neale Ranns097fa662018-05-01 05:17:55 -07001004out:
Neale Ranns28c142e2018-09-07 09:37:07 -07001005 return (rv);
Neale Ranns32e1c012016-11-22 17:07:28 +00001006}
1007
1008void
1009vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1010{
1011 vl_api_ip_mroute_add_del_reply_t *rmp;
Neale Ranns097fa662018-05-01 05:17:55 -07001012 u32 stats_index = ~0;
Neale Ranns32e1c012016-11-22 17:07:28 +00001013 int rv;
Neale Ranns32e1c012016-11-22 17:07:28 +00001014
Neale Ranns28c142e2018-09-07 09:37:07 -07001015 rv = api_mroute_add_del_t_handler (mp, &stats_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001016
Neale Ranns28c142e2018-09-07 09:37:07 -07001017 /* *INDENT-OFF* */
1018 REPLY_MACRO2 (VL_API_IP_MROUTE_ADD_DEL_REPLY,
1019 ({
1020 rmp->stats_index = htonl (stats_index);
1021 }));
1022 /* *INDENT-ON* */
Neale Ranns32e1c012016-11-22 17:07:28 +00001023}
1024
Dave Barachb5e8a772016-12-06 12:04:42 -05001025static void
1026send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001027 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1028 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001029{
1030 vl_api_ip_details_t *mp;
1031
1032 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001033 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -05001034 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1035
1036 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001037 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001038 mp->context = context;
1039
Florin Coras6c4dae22018-01-09 06:39:23 -08001040 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001041}
1042
1043static void
1044send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001045 vl_api_registration_t * reg,
Neale Ranns097fa662018-05-01 05:17:55 -07001046 const fib_prefix_t * pfx,
1047 u32 sw_if_index, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001048{
1049 vl_api_ip_address_details_t *mp;
1050
1051 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001052 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -05001053 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1054
Neale Ranns097fa662018-05-01 05:17:55 -07001055 ip_prefix_encode (pfx, &mp->prefix);
Dave Barachb5e8a772016-12-06 12:04:42 -05001056 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001057 mp->sw_if_index = htonl (sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001058
Florin Coras6c4dae22018-01-09 06:39:23 -08001059 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001060}
1061
1062static void
1063vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1064{
1065 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001066 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001067 ip6_main_t *im6 = &ip6_main;
1068 ip4_main_t *im4 = &ip4_main;
1069 ip_lookup_main_t *lm6 = &im6->lookup_main;
1070 ip_lookup_main_t *lm4 = &im4->lookup_main;
1071 ip_interface_address_t *ia = 0;
1072 u32 sw_if_index = ~0;
1073 int rv __attribute__ ((unused)) = 0;
1074
1075 VALIDATE_SW_IF_INDEX (mp);
1076
1077 sw_if_index = ntohl (mp->sw_if_index);
1078
Florin Coras6c4dae22018-01-09 06:39:23 -08001079 reg = vl_api_client_index_to_registration (mp->client_index);
1080 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001081 return;
1082
Dave Barachb5e8a772016-12-06 12:04:42 -05001083 if (mp->is_ipv6)
1084 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001085 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001086 /* Do not send subnet details of the IP-interface for
1087 * unnumbered interfaces. otherwise listening clients
1088 * will be confused that the subnet is applied on more
1089 * than one interface */
1090 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001091 ({
Neale Ranns097fa662018-05-01 05:17:55 -07001092 fib_prefix_t pfx = {
1093 .fp_addr.ip6 = *(ip6_address_t *)ip_interface_address_get_address (lm6, ia),
1094 .fp_len = ia->address_length,
1095 .fp_proto = FIB_PROTOCOL_IP6,
1096 };
1097 send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001098 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001099 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001100 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001101 else
1102 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001103 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001104 foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001105 ({
Neale Ranns097fa662018-05-01 05:17:55 -07001106 fib_prefix_t pfx = {
1107 .fp_addr.ip4 = *(ip4_address_t *)ip_interface_address_get_address (lm4, ia),
1108 .fp_len = ia->address_length,
1109 .fp_proto = FIB_PROTOCOL_IP4,
1110 };
1111
1112 send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001113 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001114 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001115 }
Neale Ranns008dbe12018-09-07 09:32:36 -07001116
Dave Barachb5e8a772016-12-06 12:04:42 -05001117 BAD_SW_IF_INDEX_LABEL;
1118}
1119
1120static void
Neale Ranns9e2f9152018-05-18 02:27:10 -07001121send_ip_unnumbered_details (vpe_api_main_t * am,
1122 vl_api_registration_t * reg,
1123 u32 sw_if_index, u32 ip_sw_if_index, u32 context)
1124{
1125 vl_api_ip_unnumbered_details_t *mp;
1126
1127 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001128 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns9e2f9152018-05-18 02:27:10 -07001129 mp->_vl_msg_id = ntohs (VL_API_IP_UNNUMBERED_DETAILS);
1130
1131 mp->context = context;
1132 mp->sw_if_index = htonl (sw_if_index);
1133 mp->ip_sw_if_index = htonl (ip_sw_if_index);
1134
1135 vl_api_send_msg (reg, (u8 *) mp);
1136}
1137
1138static void
1139vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
1140{
1141 vnet_main_t *vnm = vnet_get_main ();
1142 vnet_interface_main_t *im = &vnm->interface_main;
1143 int rv __attribute__ ((unused)) = 0;
1144 vpe_api_main_t *am = &vpe_api_main;
1145 vl_api_registration_t *reg;
1146 vnet_sw_interface_t *si;
1147 u32 sw_if_index;
1148
1149 sw_if_index = ntohl (mp->sw_if_index);
1150
1151 reg = vl_api_client_index_to_registration (mp->client_index);
1152 if (!reg)
1153 return;
1154
1155 if (~0 != sw_if_index)
1156 {
1157 VALIDATE_SW_IF_INDEX (mp);
1158
1159 si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
1160
Neale Rannsac3e72c2019-10-06 01:04:26 -07001161 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
Neale Ranns9e2f9152018-05-18 02:27:10 -07001162 {
1163 send_ip_unnumbered_details (am, reg,
1164 sw_if_index,
1165 si->unnumbered_sw_if_index,
1166 mp->context);
1167 }
1168 }
1169 else
1170 {
1171 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001172 pool_foreach (si, im->sw_interfaces)
1173 {
Neale Ranns9e2f9152018-05-18 02:27:10 -07001174 if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1175 {
1176 send_ip_unnumbered_details(am, reg,
1177 si->sw_if_index,
1178 si->unnumbered_sw_if_index,
1179 mp->context);
1180 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001181 }
Neale Ranns9e2f9152018-05-18 02:27:10 -07001182 /* *INDENT-ON* */
1183 }
1184
1185 BAD_SW_IF_INDEX_LABEL;
1186}
1187
1188static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001189vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1190{
1191 vpe_api_main_t *am = &vpe_api_main;
1192 vnet_main_t *vnm = vnet_get_main ();
Neale Rannscbe25aa2019-09-30 10:53:31 +00001193 //vlib_main_t *vm = vlib_get_main ();
Dave Barachb5e8a772016-12-06 12:04:42 -05001194 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001195 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001196 vnet_sw_interface_t *si, *sorted_sis;
1197 u32 sw_if_index = ~0;
1198
Florin Coras6c4dae22018-01-09 06:39:23 -08001199 reg = vl_api_client_index_to_registration (mp->client_index);
1200 if (!reg)
1201 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001202
1203 /* Gather interfaces. */
1204 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1205 _vec_len (sorted_sis) = 0;
1206 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001207 pool_foreach (si, im->sw_interfaces)
1208 {
Dave Barachb5e8a772016-12-06 12:04:42 -05001209 vec_add1 (sorted_sis, si[0]);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001210 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001211 /* *INDENT-ON* */
1212
1213 vec_foreach (si, sorted_sis)
1214 {
1215 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1216 {
Neale Rannscbe25aa2019-09-30 10:53:31 +00001217 /* if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index)) */
1218 /* { */
1219 /* continue; */
1220 /* } */
Dave Barachb5e8a772016-12-06 12:04:42 -05001221 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001222 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001223 }
1224 }
Matthew Smith6d5f6592019-09-18 13:51:46 -05001225
1226 vec_free (sorted_sis);
Dave Barachb5e8a772016-12-06 12:04:42 -05001227}
1228
1229static void
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001230vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t *mp)
Dave Barachb5e8a772016-12-06 12:04:42 -05001231{
1232 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001233 int rv;
1234 u32 table_id;
1235 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001236
Neale Ranns227038a2017-04-21 01:07:59 -07001237 table_id = ntohl (mp->vrf_id);
1238
1239#define _(a,b) if (mp->a) flow_hash_config |= b;
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001240 foreach_flow_hash_bit_v1;
Neale Ranns227038a2017-04-21 01:07:59 -07001241#undef _
1242
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001243 rv = ip_flow_hash_set ((mp->is_ipv6 ? AF_IP6 : AF_IP4), table_id,
1244 flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001245
1246 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1247}
1248
1249static void
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001250vl_api_set_ip_flow_hash_v2_t_handler (vl_api_set_ip_flow_hash_v2_t *mp)
Dave Barachb5e8a772016-12-06 12:04:42 -05001251{
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001252 vl_api_set_ip_flow_hash_v2_reply_t *rmp;
1253 ip_address_family_t af;
Dave Barachb5e8a772016-12-06 12:04:42 -05001254 int rv;
Dave Barachb5e8a772016-12-06 12:04:42 -05001255
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001256 rv = ip_address_family_decode (mp->af, &af);
Dave Barachb5e8a772016-12-06 12:04:42 -05001257
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001258 if (!rv)
1259 rv = ip_flow_hash_set (af, htonl (mp->table_id),
1260 htonl (mp->flow_hash_config));
Dave Barachb5e8a772016-12-06 12:04:42 -05001261
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001262 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_V2_REPLY);
Dave Barachb5e8a772016-12-06 12:04:42 -05001263}
1264
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001265static void
1266vl_api_set_ip_flow_hash_router_id_t_handler (
1267 vl_api_set_ip_flow_hash_router_id_t *mp)
1268{
1269 vl_api_set_ip_flow_hash_router_id_reply_t *rmp;
1270 int rv = 0;
1271
1272 ip_flow_hash_router_id_set (ntohl (mp->router_id));
1273
1274 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_ROUTER_ID_REPLY);
1275}
1276
Neale Ranns32e1c012016-11-22 17:07:28 +00001277void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001278vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001279 u32 context, const mfib_signal_t * mfs)
1280{
1281 vl_api_mfib_signal_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -08001282 const mfib_prefix_t *prefix;
Neale Ranns32e1c012016-11-22 17:07:28 +00001283 mfib_table_t *mfib;
1284 mfib_itf_t *mfi;
1285
1286 mp = vl_msg_api_alloc (sizeof (*mp));
1287
Dave Barachb7b92992018-10-17 10:38:51 -04001288 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns32e1c012016-11-22 17:07:28 +00001289 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1290 mp->context = context;
1291
1292 mfi = mfib_itf_get (mfs->mfs_itf);
Neale Ranns9e829a82018-12-17 05:50:32 -08001293 prefix = mfib_entry_get_prefix (mfs->mfs_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +00001294 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
Neale Ranns9e829a82018-12-17 05:50:32 -08001295 prefix->fp_proto);
Neale Ranns32e1c012016-11-22 17:07:28 +00001296 mp->table_id = ntohl (mfib->mft_table_id);
1297 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1298
Neale Ranns097fa662018-05-01 05:17:55 -07001299 ip_mprefix_encode (prefix, &mp->prefix);
Neale Ranns32e1c012016-11-22 17:07:28 +00001300
1301 if (0 != mfs->mfs_buffer_len)
1302 {
1303 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1304
1305 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1306 }
1307 else
1308 {
1309 mp->ip_packet_len = 0;
1310 }
1311
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001312 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001313}
1314
1315static void
1316vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1317{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001318 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001319
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001320 reg = vl_api_client_index_to_registration (mp->client_index);
1321 if (!reg)
1322 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001323
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001324 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001325 ;
1326}
Dave Barachb5e8a772016-12-06 12:04:42 -05001327
Florin Coras595992c2017-11-06 17:17:08 -08001328static void
1329 vl_api_ip_container_proxy_add_del_t_handler
1330 (vl_api_ip_container_proxy_add_del_t * mp)
1331{
1332 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1333 vnet_ip_container_proxy_args_t args;
1334 int rv = 0;
1335 clib_error_t *error;
1336
Dave Barachb7b92992018-10-17 10:38:51 -04001337 clib_memset (&args, 0, sizeof (args));
Neale Ranns37029302018-08-10 05:30:06 -07001338
1339 ip_prefix_decode (&mp->pfx, &args.prefix);
1340
Florin Coras595992c2017-11-06 17:17:08 -08001341 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1342 args.is_add = mp->is_add;
1343 if ((error = vnet_ip_container_proxy_add_del (&args)))
1344 {
1345 rv = clib_error_get_code (error);
1346 clib_error_report (error);
1347 }
1348
1349 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1350}
1351
Neale Ranns8f5fef22020-12-21 08:29:34 +00001352typedef struct ip_walk_ctx_t_
Matus Fabian75b9f452018-10-02 23:27:21 -07001353{
1354 vl_api_registration_t *reg;
1355 u32 context;
Neale Ranns8f5fef22020-12-21 08:29:34 +00001356} ip_walk_ctx_t;
Matus Fabian75b9f452018-10-02 23:27:21 -07001357
1358static int
1359ip_container_proxy_send_details (const fib_prefix_t * pfx, u32 sw_if_index,
1360 void *args)
1361{
1362 vl_api_ip_container_proxy_details_t *mp;
Neale Ranns8f5fef22020-12-21 08:29:34 +00001363 ip_walk_ctx_t *ctx = args;
Matus Fabian75b9f452018-10-02 23:27:21 -07001364
1365 mp = vl_msg_api_alloc (sizeof (*mp));
1366 if (!mp)
1367 return 1;
1368
Dave Barachb7b92992018-10-17 10:38:51 -04001369 clib_memset (mp, 0, sizeof (*mp));
Matus Fabian75b9f452018-10-02 23:27:21 -07001370 mp->_vl_msg_id = ntohs (VL_API_IP_CONTAINER_PROXY_DETAILS);
1371 mp->context = ctx->context;
1372
1373 mp->sw_if_index = ntohl (sw_if_index);
1374 ip_prefix_encode (pfx, &mp->prefix);
1375
1376 vl_api_send_msg (ctx->reg, (u8 *) mp);
1377
1378 return 1;
1379}
1380
1381static void
1382vl_api_ip_container_proxy_dump_t_handler (vl_api_ip_container_proxy_dump_t *
1383 mp)
1384{
1385 vl_api_registration_t *reg;
1386
1387 reg = vl_api_client_index_to_registration (mp->client_index);
1388 if (!reg)
1389 return;
1390
Neale Ranns8f5fef22020-12-21 08:29:34 +00001391 ip_walk_ctx_t ctx = {
Matus Fabian75b9f452018-10-02 23:27:21 -07001392 .context = mp->context,
1393 .reg = reg,
1394 };
1395
1396 ip_container_proxy_walk (ip_container_proxy_send_details, &ctx);
1397}
1398
Neale Rannsb8d44812017-11-10 06:53:54 -08001399static void
1400vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1401{
1402 int rv = 0;
1403 vl_api_ioam_enable_reply_t *rmp;
1404 clib_error_t *error;
1405
1406 /* Ignoring the profile id as currently a single profile
1407 * is supported */
1408 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1409 mp->seqno, mp->analyse);
1410 if (error)
1411 {
1412 clib_error_report (error);
1413 rv = clib_error_get_code (error);
1414 }
1415
1416 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1417}
1418
1419static void
1420vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1421{
1422 int rv = 0;
1423 vl_api_ioam_disable_reply_t *rmp;
1424 clib_error_t *error;
1425
1426 error = clear_ioam_rewrite_fn ();
1427 if (error)
1428 {
1429 clib_error_report (error);
1430 rv = clib_error_get_code (error);
1431 }
1432
1433 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1434}
1435
1436static void
1437 vl_api_ip_source_and_port_range_check_add_del_t_handler
1438 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1439{
1440 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1441 int rv = 0;
1442
Neale Rannsb8d44812017-11-10 06:53:54 -08001443 u8 is_add = mp->is_add;
Neale Ranns37029302018-08-10 05:30:06 -07001444 fib_prefix_t pfx;
Neale Rannsb8d44812017-11-10 06:53:54 -08001445 u16 *low_ports = 0;
1446 u16 *high_ports = 0;
1447 u32 vrf_id;
1448 u16 tmp_low, tmp_high;
1449 u8 num_ranges;
1450 int i;
1451
Neale Ranns37029302018-08-10 05:30:06 -07001452 ip_prefix_decode (&mp->prefix, &pfx);
1453
Neale Rannsb8d44812017-11-10 06:53:54 -08001454 // Validate port range
1455 num_ranges = mp->number_of_ranges;
1456 if (num_ranges > 32)
1457 { // This is size of array in VPE.API
1458 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1459 goto reply;
1460 }
1461
1462 vec_reset_length (low_ports);
1463 vec_reset_length (high_ports);
1464
1465 for (i = 0; i < num_ranges; i++)
1466 {
1467 tmp_low = mp->low_ports[i];
1468 tmp_high = mp->high_ports[i];
1469 // If tmp_low <= tmp_high then only need to check tmp_low = 0
1470 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1471 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1472 {
1473 rv = VNET_API_ERROR_INVALID_VALUE;
1474 goto reply;
1475 }
1476 vec_add1 (low_ports, tmp_low);
1477 vec_add1 (high_ports, tmp_high + 1);
1478 }
1479
Neale Rannsb8d44812017-11-10 06:53:54 -08001480 vrf_id = ntohl (mp->vrf_id);
1481
1482 if (vrf_id < 1)
1483 {
1484 rv = VNET_API_ERROR_INVALID_VALUE;
1485 goto reply;
1486 }
1487
1488
Neale Ranns37029302018-08-10 05:30:06 -07001489 if (FIB_PROTOCOL_IP6 == pfx.fp_proto)
Neale Rannsb8d44812017-11-10 06:53:54 -08001490 {
Neale Ranns37029302018-08-10 05:30:06 -07001491 rv = ip6_source_and_port_range_check_add_del (&pfx.fp_addr.ip6,
1492 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08001493 vrf_id,
1494 low_ports,
1495 high_ports, is_add);
1496 }
1497 else
1498 {
Neale Ranns37029302018-08-10 05:30:06 -07001499 rv = ip4_source_and_port_range_check_add_del (&pfx.fp_addr.ip4,
1500 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08001501 vrf_id,
1502 low_ports,
1503 high_ports, is_add);
1504 }
1505
1506reply:
1507 vec_free (low_ports);
1508 vec_free (high_ports);
1509 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
1510}
1511
1512static void
1513 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
1514 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
1515{
1516 vlib_main_t *vm = vlib_get_main ();
1517 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
1518 ip4_main_t *im = &ip4_main;
1519 int rv;
1520 u32 sw_if_index;
1521 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1522 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1523 uword *p = 0;
1524 int i;
1525
1526 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
1527 ntohl (mp->tcp_out_vrf_id);
1528 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
1529 ntohl (mp->udp_out_vrf_id);
1530 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
1531 ntohl (mp->tcp_in_vrf_id);
1532 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
1533 ntohl (mp->udp_in_vrf_id);
1534
1535
1536 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
1537 {
1538 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
1539 {
1540 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
1541
1542 if (p == 0)
1543 {
1544 rv = VNET_API_ERROR_INVALID_VALUE;
1545 goto reply;
1546 }
1547
1548 fib_index[i] = p[0];
1549 }
1550 else
1551 fib_index[i] = ~0;
1552 }
1553 sw_if_index = ntohl (mp->sw_if_index);
1554
1555 VALIDATE_SW_IF_INDEX (mp);
1556
1557 rv =
1558 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
1559 mp->is_add);
1560
1561 BAD_SW_IF_INDEX_LABEL;
1562reply:
1563
1564 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
1565}
1566
Neale Rannscbe25aa2019-09-30 10:53:31 +00001567static void
1568 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1569 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1570{
1571 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1572 ip6_address_t ip;
1573 int rv;
1574
1575 VALIDATE_SW_IF_INDEX (mp);
1576
1577 ip6_address_decode (mp->ip, &ip);
1578
Neale Rannsec40a7d2020-04-23 07:36:12 +00001579 rv = ip6_link_set_local_address (ntohl (mp->sw_if_index), &ip);
Neale Rannscbe25aa2019-09-30 10:53:31 +00001580
1581 BAD_SW_IF_INDEX_LABEL;
1582 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1583}
1584
Neale Ranns9db6ada2019-11-08 12:42:31 +00001585static void
Benoît Ganne58a19152021-01-18 19:24:34 +01001586vl_api_sw_interface_ip6_get_link_local_address_t_handler (
1587 vl_api_sw_interface_ip6_get_link_local_address_t *mp)
1588{
1589 vl_api_sw_interface_ip6_get_link_local_address_reply_t *rmp;
1590 const ip6_address_t *ip = NULL;
1591 int rv = 0;
1592
1593 VALIDATE_SW_IF_INDEX (mp);
1594
1595 ip = ip6_get_link_local_address (ntohl (mp->sw_if_index));
1596 if (NULL == ip)
1597 rv = VNET_API_ERROR_IP6_NOT_ENABLED;
1598
1599 BAD_SW_IF_INDEX_LABEL;
1600 /* clang-format off */
1601 REPLY_MACRO2 (VL_API_SW_INTERFACE_IP6_GET_LINK_LOCAL_ADDRESS_REPLY,
1602 ({
1603 if (!rv)
1604 ip6_address_encode (ip, rmp->ip);
1605 }))
1606 /* clang-format on */
1607}
1608
1609static void
Neale Ranns9db6ada2019-11-08 12:42:31 +00001610vl_api_ip_table_replace_begin_t_handler (vl_api_ip_table_replace_begin_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -08001611{
Neale Ranns9db6ada2019-11-08 12:42:31 +00001612 vl_api_ip_table_replace_begin_reply_t *rmp;
1613 fib_protocol_t fproto;
1614 u32 fib_index;
1615 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001616
Neale Ranns9db6ada2019-11-08 12:42:31 +00001617 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1618 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
Neale Rannsb8d44812017-11-10 06:53:54 -08001619
Neale Ranns9db6ada2019-11-08 12:42:31 +00001620 if (INDEX_INVALID == fib_index)
1621 rv = VNET_API_ERROR_NO_SUCH_FIB;
1622 else
1623 {
1624 fib_table_mark (fib_index, fproto, FIB_SOURCE_API);
1625 mfib_table_mark (mfib_table_find (fproto, ntohl (mp->table.table_id)),
1626 fproto, MFIB_SOURCE_API);
Neale Rannsb8d44812017-11-10 06:53:54 -08001627 }
Neale Ranns9db6ada2019-11-08 12:42:31 +00001628 REPLY_MACRO (VL_API_IP_TABLE_REPLACE_BEGIN_REPLY);
Neale Rannsb8d44812017-11-10 06:53:54 -08001629}
1630
1631static void
Neale Ranns9db6ada2019-11-08 12:42:31 +00001632vl_api_ip_table_replace_end_t_handler (vl_api_ip_table_replace_end_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -08001633{
Neale Ranns9db6ada2019-11-08 12:42:31 +00001634 vl_api_ip_table_replace_end_reply_t *rmp;
1635 fib_protocol_t fproto;
1636 u32 fib_index;
1637 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001638
Neale Ranns9db6ada2019-11-08 12:42:31 +00001639 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1640 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1641
1642 if (INDEX_INVALID == fib_index)
1643 rv = VNET_API_ERROR_NO_SUCH_FIB;
Neale Rannsb8d44812017-11-10 06:53:54 -08001644 else
Neale Ranns9db6ada2019-11-08 12:42:31 +00001645 {
1646 fib_table_sweep (fib_index, fproto, FIB_SOURCE_API);
1647 mfib_table_sweep (mfib_table_find
1648 (fproto, ntohl (mp->table.table_id)), fproto,
1649 MFIB_SOURCE_API);
1650 }
1651 REPLY_MACRO (VL_API_IP_TABLE_REPLACE_END_REPLY);
1652}
Neale Rannsb8d44812017-11-10 06:53:54 -08001653
Neale Ranns9db6ada2019-11-08 12:42:31 +00001654static void
1655vl_api_ip_table_flush_t_handler (vl_api_ip_table_flush_t * mp)
1656{
1657 vl_api_ip_table_flush_reply_t *rmp;
1658 fib_protocol_t fproto;
1659 u32 fib_index;
1660 int rv = 0;
1661
1662 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1663 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1664
1665 if (INDEX_INVALID == fib_index)
1666 rv = VNET_API_ERROR_NO_SUCH_FIB;
1667 else
1668 {
1669 vnet_main_t *vnm = vnet_get_main ();
1670 vnet_interface_main_t *im = &vnm->interface_main;
1671 vnet_sw_interface_t *si;
1672
1673 /* Shut down interfaces in this FIB / clean out intfc routes */
1674 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001675 pool_foreach (si, im->sw_interfaces)
1676 {
Neale Ranns9db6ada2019-11-08 12:42:31 +00001677 if (fib_index == fib_table_get_index_for_sw_if_index (fproto,
1678 si->sw_if_index))
1679 {
1680 u32 flags = si->flags;
1681 flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP;
1682 vnet_sw_interface_set_flags (vnm, si->sw_if_index, flags);
1683 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001684 }
Neale Ranns9db6ada2019-11-08 12:42:31 +00001685 /* *INDENT-ON* */
1686
1687 fib_table_flush (fib_index, fproto, FIB_SOURCE_API);
1688 mfib_table_flush (mfib_table_find (fproto, ntohl (mp->table.table_id)),
1689 fproto, MFIB_SOURCE_API);
1690 }
1691
1692 REPLY_MACRO (VL_API_IP_TABLE_FLUSH_REPLY);
Neale Rannsb8d44812017-11-10 06:53:54 -08001693}
1694
Klement Sekera75e7d132017-09-20 08:26:30 +02001695void
1696vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
1697{
1698 vl_api_ip_reassembly_set_reply_t *rmp;
1699 int rv = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001700 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera75e7d132017-09-20 08:26:30 +02001701 {
Klement Sekerade34c352019-06-25 11:19:22 +00001702 case IP_REASS_TYPE_FULL:
1703 if (mp->is_ip6)
1704 {
1705 rv = ip6_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1706 clib_net_to_host_u32
1707 (mp->max_reassemblies),
1708 clib_net_to_host_u32
1709 (mp->max_reassembly_length),
1710 clib_net_to_host_u32
1711 (mp->expire_walk_interval_ms));
1712 }
1713 else
1714 {
1715 rv = ip4_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1716 clib_net_to_host_u32
1717 (mp->max_reassemblies),
1718 clib_net_to_host_u32
1719 (mp->max_reassembly_length),
1720 clib_net_to_host_u32
1721 (mp->expire_walk_interval_ms));
1722 }
1723 break;
1724 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1725 if (mp->is_ip6)
1726 {
1727 rv =
1728 ip6_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1729 clib_net_to_host_u32 (mp->max_reassemblies),
1730 clib_net_to_host_u32
1731 (mp->max_reassembly_length),
1732 clib_net_to_host_u32
1733 (mp->expire_walk_interval_ms));
1734 }
1735 else
1736 {
1737 rv = ip4_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1738 clib_net_to_host_u32 (mp->max_reassemblies),
1739 clib_net_to_host_u32
1740 (mp->max_reassembly_length),
1741 clib_net_to_host_u32
1742 (mp->expire_walk_interval_ms));
1743 }
1744 break;
Klement Sekera75e7d132017-09-20 08:26:30 +02001745 }
1746
1747 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
1748}
1749
1750void
1751vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
1752{
Ole Troan2e1c8962019-04-10 09:44:23 +02001753 vl_api_registration_t *rp;
Klement Sekera75e7d132017-09-20 08:26:30 +02001754
Ole Troan2e1c8962019-04-10 09:44:23 +02001755 rp = vl_api_client_index_to_registration (mp->client_index);
1756 if (rp == 0)
Klement Sekera75e7d132017-09-20 08:26:30 +02001757 return;
1758
1759 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001760 clib_memset (rmp, 0, sizeof (*rmp));
Klement Sekera75e7d132017-09-20 08:26:30 +02001761 rmp->_vl_msg_id = ntohs (VL_API_IP_REASSEMBLY_GET_REPLY);
1762 rmp->context = mp->context;
1763 rmp->retval = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001764 u32 timeout_ms;
1765 u32 max_reassemblies;
1766 u32 max_reassembly_length;
1767 u32 expire_walk_interval_ms;
1768 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera75e7d132017-09-20 08:26:30 +02001769 {
Klement Sekerade34c352019-06-25 11:19:22 +00001770 case IP_REASS_TYPE_FULL:
1771 if (mp->is_ip6)
1772 {
1773 rmp->is_ip6 = 1;
1774 ip6_full_reass_get (&timeout_ms, &max_reassemblies,
1775 &max_reassembly_length,
1776 &expire_walk_interval_ms);
1777 }
1778 else
1779 {
1780 rmp->is_ip6 = 0;
1781 ip4_full_reass_get (&timeout_ms, &max_reassemblies,
1782 &max_reassembly_length,
1783 &expire_walk_interval_ms);
1784 }
1785 break;
1786 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1787 if (mp->is_ip6)
1788 {
1789 rmp->is_ip6 = 1;
1790 ip6_sv_reass_get (&timeout_ms, &max_reassemblies,
1791 &max_reassembly_length, &expire_walk_interval_ms);
1792 }
1793 else
1794 {
1795 rmp->is_ip6 = 0;
1796 ip4_sv_reass_get (&timeout_ms, &max_reassemblies,
1797 &max_reassembly_length, &expire_walk_interval_ms);
1798 }
1799 break;
Klement Sekera75e7d132017-09-20 08:26:30 +02001800 }
Matthew Smithb3174df2019-12-03 12:41:53 -06001801 rmp->timeout_ms = clib_host_to_net_u32 (timeout_ms);
1802 rmp->max_reassemblies = clib_host_to_net_u32 (max_reassemblies);
1803 rmp->max_reassembly_length = clib_host_to_net_u32 (max_reassembly_length);
Klement Sekera75e7d132017-09-20 08:26:30 +02001804 rmp->expire_walk_interval_ms =
Matthew Smithb3174df2019-12-03 12:41:53 -06001805 clib_host_to_net_u32 (expire_walk_interval_ms);
Ole Troan2e1c8962019-04-10 09:44:23 +02001806 vl_api_send_msg (rp, (u8 *) rmp);
Klement Sekera75e7d132017-09-20 08:26:30 +02001807}
1808
Klement Sekera4c533132018-02-22 11:41:12 +01001809void
1810 vl_api_ip_reassembly_enable_disable_t_handler
1811 (vl_api_ip_reassembly_enable_disable_t * mp)
1812{
1813 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
1814 int rv = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001815 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera4c533132018-02-22 11:41:12 +01001816 {
Klement Sekerade34c352019-06-25 11:19:22 +00001817 case IP_REASS_TYPE_FULL:
Klement Sekera896c8962019-06-24 11:52:49 +00001818 rv =
Klement Sekerade34c352019-06-25 11:19:22 +00001819 ip4_full_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1820 mp->enable_ip4);
1821 if (0 == rv)
1822 rv =
1823 ip6_full_reass_enable_disable (clib_net_to_host_u32
1824 (mp->sw_if_index), mp->enable_ip6);
1825 break;
1826 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1827 rv =
1828 ip4_sv_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1829 mp->enable_ip4);
1830 if (0 == rv)
1831 {
1832 rv =
1833 ip6_sv_reass_enable_disable (clib_net_to_host_u32
1834 (mp->sw_if_index), mp->enable_ip6);
1835 }
1836 break;
Klement Sekera4c533132018-02-22 11:41:12 +01001837 }
1838
Choree7f61d12018-11-28 10:27:58 +03301839 REPLY_MACRO (VL_API_IP_REASSEMBLY_ENABLE_DISABLE_REPLY);
Klement Sekera4c533132018-02-22 11:41:12 +01001840}
1841
Neale Ranns92207752019-06-03 13:21:40 +00001842static walk_rc_t
1843send_ip_punt_redirect_details (u32 rx_sw_if_index,
1844 const ip_punt_redirect_rx_t * ipr, void *arg)
1845{
Pavel Kotucek609e1212018-11-27 09:59:44 +01001846 vl_api_ip_punt_redirect_details_t *mp;
Neale Ranns097fa662018-05-01 05:17:55 -07001847 fib_path_encode_ctx_t path_ctx = {
1848 .rpaths = NULL,
1849 };
Neale Ranns8f5fef22020-12-21 08:29:34 +00001850 ip_walk_ctx_t *ctx = arg;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001851
1852 mp = vl_msg_api_alloc (sizeof (*mp));
1853 if (!mp)
Neale Ranns92207752019-06-03 13:21:40 +00001854 return (WALK_STOP);;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001855
1856 clib_memset (mp, 0, sizeof (*mp));
1857 mp->_vl_msg_id = ntohs (VL_API_IP_PUNT_REDIRECT_DETAILS);
Neale Ranns92207752019-06-03 13:21:40 +00001858 mp->context = ctx->context;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001859
Neale Ranns097fa662018-05-01 05:17:55 -07001860 fib_path_list_walk_w_ext (ipr->pl, NULL, fib_path_encode, &path_ctx);
Neale Ranns92207752019-06-03 13:21:40 +00001861
1862 mp->punt.rx_sw_if_index = htonl (rx_sw_if_index);
Neale Ranns097fa662018-05-01 05:17:55 -07001863 mp->punt.tx_sw_if_index = htonl (path_ctx.rpaths[0].frp_sw_if_index);
Neale Ranns92207752019-06-03 13:21:40 +00001864
Neale Ranns097fa662018-05-01 05:17:55 -07001865 ip_address_encode (&path_ctx.rpaths[0].frp_addr,
Neale Ranns92207752019-06-03 13:21:40 +00001866 fib_proto_to_ip46 (ipr->fproto), &mp->punt.nh);
1867
1868 vl_api_send_msg (ctx->reg, (u8 *) mp);
1869
Neale Ranns097fa662018-05-01 05:17:55 -07001870 vec_free (path_ctx.rpaths);
Neale Ranns92207752019-06-03 13:21:40 +00001871
1872 return (WALK_CONTINUE);
Pavel Kotucek609e1212018-11-27 09:59:44 +01001873}
1874
1875static void
1876vl_api_ip_punt_redirect_dump_t_handler (vl_api_ip_punt_redirect_dump_t * mp)
1877{
1878 vl_api_registration_t *reg;
Dave Barach77841402020-04-29 17:04:10 -04001879 fib_protocol_t fproto = FIB_PROTOCOL_IP4;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001880
Pavel Kotucek609e1212018-11-27 09:59:44 +01001881 reg = vl_api_client_index_to_registration (mp->client_index);
1882 if (!reg)
1883 return;
1884
Dave Barach77841402020-04-29 17:04:10 -04001885 if (mp->is_ipv6 == 1)
1886 fproto = FIB_PROTOCOL_IP6;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001887
Neale Ranns8f5fef22020-12-21 08:29:34 +00001888 ip_walk_ctx_t ctx = {
Neale Ranns92207752019-06-03 13:21:40 +00001889 .reg = reg,
1890 .context = mp->context,
1891 };
1892
1893 if (~0 != mp->sw_if_index)
Pavel Kotucek609e1212018-11-27 09:59:44 +01001894 {
Neale Ranns92207752019-06-03 13:21:40 +00001895 u32 rx_sw_if_index;
1896 index_t pri;
1897
1898 rx_sw_if_index = ntohl (mp->sw_if_index);
1899 pri = ip_punt_redirect_find (fproto, rx_sw_if_index);
1900
1901 if (INDEX_INVALID == pri)
1902 return;
1903
1904 send_ip_punt_redirect_details (rx_sw_if_index,
1905 ip_punt_redirect_get (pri), &ctx);
Pavel Kotucek609e1212018-11-27 09:59:44 +01001906 }
1907 else
Neale Ranns92207752019-06-03 13:21:40 +00001908 ip_punt_redirect_walk (fproto, send_ip_punt_redirect_details, &ctx);
Pavel Kotucek609e1212018-11-27 09:59:44 +01001909}
1910
Neale Ranns8f5fef22020-12-21 08:29:34 +00001911void
1912vl_api_ip_path_mtu_update_t_handler (vl_api_ip_path_mtu_update_t *mp)
1913{
1914 vl_api_ip_path_mtu_update_reply_t *rmp;
1915 ip_address_t nh;
1916 int rv = 0;
1917
1918 ip_address_decode2 (&mp->pmtu.nh, &nh);
1919
1920 rv = ip_path_mtu_update (&nh, ntohl (mp->pmtu.table_id),
1921 ntohs (mp->pmtu.path_mtu));
1922
1923 REPLY_MACRO (VL_API_IP_PATH_MTU_UPDATE_REPLY);
1924}
1925
1926void
1927vl_api_ip_path_mtu_replace_begin_t_handler (
1928 vl_api_ip_path_mtu_replace_begin_t *mp)
1929{
1930 vl_api_ip_path_mtu_replace_begin_reply_t *rmp;
1931 int rv;
1932
1933 rv = ip_path_mtu_replace_begin ();
1934
1935 REPLY_MACRO (VL_API_IP_PATH_MTU_REPLACE_BEGIN_REPLY);
1936}
1937
1938void
1939vl_api_ip_path_mtu_replace_end_t_handler (vl_api_ip_path_mtu_replace_end_t *mp)
1940{
1941 vl_api_ip_path_mtu_replace_end_reply_t *rmp;
1942 int rv;
1943
1944 rv = ip_path_mtu_replace_end ();
1945
1946 REPLY_MACRO (VL_API_IP_PATH_MTU_REPLACE_END_REPLY);
1947}
1948
1949static void
1950send_ip_path_mtu_details (index_t ipti, vl_api_registration_t *rp, u32 context)
1951{
1952 vl_api_ip_path_mtu_details_t *rmp;
1953 ip_address_t ip;
1954 ip_pmtu_t *ipt;
1955
1956 ipt = ip_path_mtu_get (ipti);
1957
1958 REPLY_MACRO_DETAILS4 (VL_API_IP_PATH_MTU_DETAILS, rp, context, ({
1959 ip_pmtu_get_ip (ipt, &ip);
1960 ip_address_encode2 (&ip, &rmp->pmtu.nh);
1961 rmp->pmtu.table_id =
1962 htonl (ip_pmtu_get_table_id (ipt));
1963 rmp->pmtu.path_mtu = htons (ipt->ipt_cfg_pmtu);
1964 }));
1965}
1966
1967static void
1968vl_api_ip_path_mtu_get_t_handler (vl_api_ip_path_mtu_get_t *mp)
1969{
1970 vl_api_ip_path_mtu_get_reply_t *rmp;
1971 i32 rv = 0;
1972
1973 REPLY_AND_DETAILS_MACRO (
1974 VL_API_IP_PATH_MTU_GET_REPLY, ip_pmtu_pool,
1975 ({ send_ip_path_mtu_details (cursor, rp, mp->context); }));
1976}
1977
Dave Barachb5e8a772016-12-06 12:04:42 -05001978#define vl_msg_name_crc_list
1979#include <vnet/ip/ip.api.h>
1980#undef vl_msg_name_crc_list
1981
1982static void
1983setup_message_id_table (api_main_t * am)
1984{
1985#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1986 foreach_vl_msg_name_crc_ip;
1987#undef _
1988}
1989
1990static clib_error_t *
1991ip_api_hookup (vlib_main_t * vm)
1992{
Dave Barach39d69112019-11-27 11:42:13 -05001993 api_main_t *am = vlibapi_get_main ();
Dave Barachb5e8a772016-12-06 12:04:42 -05001994
1995#define _(N,n) \
1996 vl_msg_api_set_handlers(VL_API_##N, #n, \
1997 vl_api_##n##_t_handler, \
1998 vl_noop_handler, \
1999 vl_api_##n##_t_endian, \
2000 vl_api_##n##_t_print, \
2001 sizeof(vl_api_##n##_t), 1);
2002 foreach_ip_api_msg;
2003#undef _
2004
2005 /*
Neale Ranns6a231a12018-10-17 06:38:00 +00002006 * Mark the route add/del API as MP safe
2007 */
Neale Ranns097fa662018-05-01 05:17:55 -07002008 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL] = 1;
2009 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_REPLY] = 1;
Neale Ranns976b2592019-12-04 06:11:00 +00002010 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_V2] = 1;
2011 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_V2_REPLY] = 1;
Neale Ranns6a231a12018-10-17 06:38:00 +00002012
2013 /*
Dave Barachb5e8a772016-12-06 12:04:42 -05002014 * Set up the (msg_name, crc, message-id) table
2015 */
2016 setup_message_id_table (am);
2017
Dave Barachb5e8a772016-12-06 12:04:42 -05002018 return 0;
2019}
2020
2021VLIB_API_INIT_FUNCTION (ip_api_hookup);
2022
2023/*
2024 * fd.io coding-style-patch-verification: ON
2025 *
2026 * Local Variables:
2027 * eval: (c-set-style "gnu")
2028 * End:
2029 */