blob: 60fa2faf9263d4fda3747f8613051736f122ab2f [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
Neale Rannsb8d44812017-11-10 06:53:54 -080023#include <vpp/stats/stats.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050024#include <vnet/interface.h>
25#include <vnet/api_errno.h>
26#include <vnet/ethernet/ethernet.h>
27#include <vnet/ip/ip.h>
28#include <vnet/ip/ip6_neighbor.h>
29#include <vnet/fib/fib_table.h>
30#include <vnet/fib/fib_api.h>
31#include <vnet/dpo/drop_dpo.h>
32#include <vnet/dpo/receive_dpo.h>
33#include <vnet/dpo/lookup_dpo.h>
34#include <vnet/dpo/classify_dpo.h>
35#include <vnet/dpo/ip_null_dpo.h>
36#include <vnet/ethernet/arp_packet.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080037#include <vnet/mfib/ip6_mfib.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000038#include <vnet/mfib/ip4_mfib.h>
39#include <vnet/mfib/mfib_signal.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080040#include <vnet/mfib/mfib_entry.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080041#include <vnet/ip/ip_source_and_port_range_check.h>
42#include <vnet/fib/ip4_fib.h>
43#include <vnet/fib/ip6_fib.h>
44#include <vnet/ip/ip6_hop_by_hop.h>
Klement Sekera75e7d132017-09-20 08:26:30 +020045#include <vnet/ip/ip4_reassembly.h>
46#include <vnet/ip/ip6_reassembly.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050047
48#include <vnet/vnet_msg_enum.h>
49
50#define vl_typedefs /* define message structures */
51#include <vnet/vnet_all_api_h.h>
52#undef vl_typedefs
53
54#define vl_endianfun /* define message structures */
55#include <vnet/vnet_all_api_h.h>
56#undef vl_endianfun
57
58/* instantiate all the print functions we know about */
59#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
60#define vl_printfun
61#include <vnet/vnet_all_api_h.h>
62#undef vl_printfun
63
64#include <vlibapi/api_helper_macros.h>
65
Neale Ranns5a8123b2017-01-26 01:18:23 -080066
Dave Barachb5e8a772016-12-06 12:04:42 -050067#define foreach_ip_api_msg \
68_(IP_FIB_DUMP, ip_fib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050069_(IP6_FIB_DUMP, ip6_fib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080070_(IP_MFIB_DUMP, ip_mfib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080071_(IP6_MFIB_DUMP, ip6_mfib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050072_(IP_NEIGHBOR_DUMP, ip_neighbor_dump) \
Neale Ranns32e1c012016-11-22 17:07:28 +000073_(IP_MROUTE_ADD_DEL, ip_mroute_add_del) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080074_(MFIB_SIGNAL_DUMP, mfib_signal_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050075_(IP_ADDRESS_DUMP, ip_address_dump) \
76_(IP_DUMP, ip_dump) \
77_(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del) \
Neale Rannsb8d44812017-11-10 06:53:54 -080078_(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit) \
79_(WANT_IP4_ARP_EVENTS, want_ip4_arp_events) \
80_(WANT_IP6_ND_EVENTS, want_ip6_nd_events) \
81_(PROXY_ARP_ADD_DEL, proxy_arp_add_del) \
82_(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable) \
83_(RESET_FIB, reset_fib) \
Dave Barachb5e8a772016-12-06 12:04:42 -050084_(IP_ADD_DEL_ROUTE, ip_add_del_route) \
Neale Ranns28ab9cc2017-08-14 07:18:42 -070085_(IP_TABLE_ADD_DEL, ip_table_add_del) \
Neale Rannsd91c1db2017-07-31 02:30:50 -070086_(IP_PUNT_POLICE, ip_punt_police) \
87_(IP_PUNT_REDIRECT, ip_punt_redirect) \
Dave Barachb5e8a772016-12-06 12:04:42 -050088_(SET_IP_FLOW_HASH,set_ip_flow_hash) \
89_(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config) \
90_(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix) \
Neale Ranns3f844d02017-02-18 00:03:54 -080091_(IP6ND_PROXY_ADD_DEL, ip6nd_proxy_add_del) \
92_(IP6ND_PROXY_DUMP, ip6nd_proxy_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050093_(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable ) \
94_(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS, \
Florin Coras595992c2017-11-06 17:17:08 -080095 sw_interface_ip6_set_link_local_address) \
Neale Rannsb8d44812017-11-10 06:53:54 -080096_(IP_CONTAINER_PROXY_ADD_DEL, ip_container_proxy_add_del) \
97_(IOAM_ENABLE, ioam_enable) \
98_(IOAM_DISABLE, ioam_disable) \
99_(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL, \
100 ip_source_and_port_range_check_add_del) \
101_(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL, \
Klement Sekera75e7d132017-09-20 08:26:30 +0200102 ip_source_and_port_range_check_interface_add_del) \
103_(IP_REASSEMBLY_SET, ip_reassembly_set) \
104_(IP_REASSEMBLY_GET, ip_reassembly_get)
Dave Barachb5e8a772016-12-06 12:04:42 -0500105
106extern void stats_dslock_with_hint (int hint, int tag);
107extern void stats_dsunlock (void);
108
109static void
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600110send_ip_neighbor_details (u32 sw_if_index,
111 u8 is_ipv6,
Dave Barachb5e8a772016-12-06 12:04:42 -0500112 u8 is_static,
113 u8 * mac_address,
Florin Coras6c4dae22018-01-09 06:39:23 -0800114 u8 * ip_address, vl_api_registration_t * reg,
115 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500116{
117 vl_api_ip_neighbor_details_t *mp;
118
119 mp = vl_msg_api_alloc (sizeof (*mp));
120 memset (mp, 0, sizeof (*mp));
121 mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS);
122 mp->context = context;
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600123 mp->sw_if_index = htonl (sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500124 mp->is_ipv6 = is_ipv6;
125 mp->is_static = is_static;
126 memcpy (mp->mac_address, mac_address, 6);
127 memcpy (mp->ip_address, ip_address, (is_ipv6) ? 16 : 4);
128
Florin Coras6c4dae22018-01-09 06:39:23 -0800129 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500130}
131
132static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500133vl_api_ip_neighbor_dump_t_handler (vl_api_ip_neighbor_dump_t * mp)
134{
Florin Coras6c4dae22018-01-09 06:39:23 -0800135 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500136
Florin Coras6c4dae22018-01-09 06:39:23 -0800137 reg = vl_api_client_index_to_registration (mp->client_index);
138 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500139 return;
140
141 u32 sw_if_index = ntohl (mp->sw_if_index);
142
143 if (mp->is_ipv6)
144 {
145 ip6_neighbor_t *n, *ns;
146
147 ns = ip6_neighbors_entries (sw_if_index);
148 /* *INDENT-OFF* */
149 vec_foreach (n, ns)
150 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500151 send_ip_neighbor_details
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600152 (sw_if_index, mp->is_ipv6,
153 ((n->flags & IP6_NEIGHBOR_FLAG_STATIC) ? 1 : 0),
Dave Barachd7cb1b52016-12-09 09:52:16 -0500154 (u8 *) n->link_layer_address,
155 (u8 *) & (n->key.ip6_address.as_u8),
Florin Coras6c4dae22018-01-09 06:39:23 -0800156 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500157 }
158 /* *INDENT-ON* */
159 vec_free (ns);
160 }
161 else
162 {
163 ethernet_arp_ip4_entry_t *n, *ns;
164
165 ns = ip4_neighbor_entries (sw_if_index);
166 /* *INDENT-OFF* */
167 vec_foreach (n, ns)
168 {
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600169 send_ip_neighbor_details (sw_if_index, mp->is_ipv6,
Dave Barachb5e8a772016-12-06 12:04:42 -0500170 ((n->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC) ? 1 : 0),
171 (u8*) n->ethernet_address,
172 (u8*) & (n->ip4_address.as_u8),
Florin Coras6c4dae22018-01-09 06:39:23 -0800173 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500174 }
175 /* *INDENT-ON* */
176 vec_free (ns);
177 }
178}
179
180
181void
182copy_fib_next_hop (fib_route_path_encode_t * api_rpath, void *fp_arg)
183{
184 int is_ip4;
185 vl_api_fib_path_t *fp = (vl_api_fib_path_t *) fp_arg;
186
Neale Rannsda78f952017-05-24 09:15:43 -0700187 if (api_rpath->rpath.frp_proto == DPO_PROTO_IP4)
Dave Barachb5e8a772016-12-06 12:04:42 -0500188 fp->afi = IP46_TYPE_IP4;
Neale Rannsda78f952017-05-24 09:15:43 -0700189 else if (api_rpath->rpath.frp_proto == DPO_PROTO_IP6)
Dave Barachb5e8a772016-12-06 12:04:42 -0500190 fp->afi = IP46_TYPE_IP6;
191 else
192 {
193 is_ip4 = ip46_address_is_ip4 (&api_rpath->rpath.frp_addr);
194 if (is_ip4)
195 fp->afi = IP46_TYPE_IP4;
196 else
197 fp->afi = IP46_TYPE_IP6;
198 }
199 if (fp->afi == IP46_TYPE_IP4)
200 memcpy (fp->next_hop, &api_rpath->rpath.frp_addr.ip4,
201 sizeof (api_rpath->rpath.frp_addr.ip4));
202 else
203 memcpy (fp->next_hop, &api_rpath->rpath.frp_addr.ip6,
204 sizeof (api_rpath->rpath.frp_addr.ip6));
205}
206
207static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500208send_ip_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800209 vl_api_registration_t * reg,
Neale Ranns2297af02017-09-12 09:45:04 -0700210 const fib_table_t * table,
211 const fib_prefix_t * pfx,
Dave Barachb5e8a772016-12-06 12:04:42 -0500212 fib_route_path_encode_t * api_rpaths, u32 context)
213{
214 vl_api_ip_fib_details_t *mp;
215 fib_route_path_encode_t *api_rpath;
216 vl_api_fib_path_t *fp;
217 int path_count;
218
219 path_count = vec_len (api_rpaths);
220 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
221 if (!mp)
222 return;
223 memset (mp, 0, sizeof (*mp));
224 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
225 mp->context = context;
226
Neale Ranns2297af02017-09-12 09:45:04 -0700227 mp->table_id = htonl (table->ft_table_id);
228 memcpy (mp->table_name, table->ft_desc,
229 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500230 mp->address_length = pfx->fp_len;
231 memcpy (mp->address, &pfx->fp_addr.ip4, sizeof (pfx->fp_addr.ip4));
232
233 mp->count = htonl (path_count);
234 fp = mp->path;
235 vec_foreach (api_rpath, api_rpaths)
236 {
237 memset (fp, 0, sizeof (*fp));
238 switch (api_rpath->dpo.dpoi_type)
239 {
240 case DPO_RECEIVE:
241 fp->is_local = true;
242 break;
243 case DPO_DROP:
244 fp->is_drop = true;
245 break;
246 case DPO_IP_NULL:
247 switch (api_rpath->dpo.dpoi_index)
248 {
249 case IP_NULL_ACTION_NONE:
250 fp->is_drop = true;
251 break;
252 case IP_NULL_ACTION_SEND_ICMP_UNREACH:
253 fp->is_unreach = true;
254 break;
255 case IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
256 fp->is_prohibit = true;
257 break;
258 default:
259 break;
260 }
261 break;
262 default:
263 break;
264 }
Neale Rannsa0a908f2017-08-01 11:40:03 -0700265 fp->weight = api_rpath->rpath.frp_weight;
266 fp->preference = api_rpath->rpath.frp_preference;
Dave Barachb5e8a772016-12-06 12:04:42 -0500267 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
268 copy_fib_next_hop (api_rpath, fp);
269 fp++;
270 }
271
Florin Coras6c4dae22018-01-09 06:39:23 -0800272 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500273}
274
Neale Rannsa3af3372017-03-28 03:49:52 -0700275typedef struct vl_api_ip_fib_dump_walk_ctx_t_
276{
277 fib_node_index_t *feis;
278} vl_api_ip_fib_dump_walk_ctx_t;
279
Neale Ranns89541992017-04-06 04:41:02 -0700280static fib_table_walk_rc_t
Neale Rannsa3af3372017-03-28 03:49:52 -0700281vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
282{
283 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
284
285 vec_add1 (ctx->feis, fei);
286
Neale Ranns89541992017-04-06 04:41:02 -0700287 return (FIB_TABLE_WALK_CONTINUE);
Neale Rannsa3af3372017-03-28 03:49:52 -0700288}
289
Dave Barachb5e8a772016-12-06 12:04:42 -0500290static void
291vl_api_ip_fib_dump_t_handler (vl_api_ip_fib_dump_t * mp)
292{
293 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800294 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500295 ip4_main_t *im = &ip4_main;
296 fib_table_t *fib_table;
Neale Rannsa3af3372017-03-28 03:49:52 -0700297 fib_node_index_t *lfeip;
Dave Barachb5e8a772016-12-06 12:04:42 -0500298 fib_prefix_t pfx;
299 u32 fib_index;
300 fib_route_path_encode_t *api_rpaths;
Neale Rannsa3af3372017-03-28 03:49:52 -0700301 vl_api_ip_fib_dump_walk_ctx_t ctx = {
302 .feis = NULL,
303 };
Dave Barachb5e8a772016-12-06 12:04:42 -0500304
Florin Coras6c4dae22018-01-09 06:39:23 -0800305 reg = vl_api_client_index_to_registration (mp->client_index);
306 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500307 return;
308
309 /* *INDENT-OFF* */
310 pool_foreach (fib_table, im->fibs,
311 ({
Neale Rannsa3af3372017-03-28 03:49:52 -0700312 fib_table_walk(fib_table->ft_index,
313 FIB_PROTOCOL_IP4,
314 vl_api_ip_fib_dump_walk,
315 &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500316 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500317 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500318
Neale Rannsa3af3372017-03-28 03:49:52 -0700319 vec_sort_with_function (ctx.feis, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500320
Neale Rannsa3af3372017-03-28 03:49:52 -0700321 vec_foreach (lfeip, ctx.feis)
Dave Barachb5e8a772016-12-06 12:04:42 -0500322 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500323 fib_entry_get_prefix (*lfeip, &pfx);
324 fib_index = fib_entry_get_fib_index (*lfeip);
325 fib_table = fib_table_get (fib_index, pfx.fp_proto);
Dave Barachb5e8a772016-12-06 12:04:42 -0500326 api_rpaths = NULL;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500327 fib_entry_encode (*lfeip, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800328 send_ip_fib_details (am, reg, fib_table, &pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500329 vec_free (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500330 }
331
Neale Rannsa3af3372017-03-28 03:49:52 -0700332 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500333}
334
335static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500336send_ip6_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800337 vl_api_registration_t * reg,
Neale Rannsa161a6d2017-11-14 08:10:41 -0800338 const fib_table_t * table,
339 const fib_prefix_t * pfx,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500340 fib_route_path_encode_t * api_rpaths, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500341{
342 vl_api_ip6_fib_details_t *mp;
343 fib_route_path_encode_t *api_rpath;
344 vl_api_fib_path_t *fp;
345 int path_count;
346
Dave Barachd7cb1b52016-12-09 09:52:16 -0500347 path_count = vec_len (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500348 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
349 if (!mp)
350 return;
351 memset (mp, 0, sizeof (*mp));
352 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
353 mp->context = context;
354
Neale Rannsa161a6d2017-11-14 08:10:41 -0800355 mp->table_id = htonl (table->ft_table_id);
Dave Barachb5e8a772016-12-06 12:04:42 -0500356 mp->address_length = pfx->fp_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500357 memcpy (mp->address, &pfx->fp_addr.ip6, sizeof (pfx->fp_addr.ip6));
Neale Rannsa161a6d2017-11-14 08:10:41 -0800358 memcpy (mp->table_name, table->ft_desc,
359 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500360
361 mp->count = htonl (path_count);
362 fp = mp->path;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500363 vec_foreach (api_rpath, api_rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500364 {
365 memset (fp, 0, sizeof (*fp));
366 switch (api_rpath->dpo.dpoi_type)
367 {
368 case DPO_RECEIVE:
369 fp->is_local = true;
370 break;
371 case DPO_DROP:
372 fp->is_drop = true;
373 break;
374 case DPO_IP_NULL:
375 switch (api_rpath->dpo.dpoi_index)
376 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500377 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_NONE:
Dave Barachb5e8a772016-12-06 12:04:42 -0500378 fp->is_drop = true;
379 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500380 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_SEND_ICMP_UNREACH:
Dave Barachb5e8a772016-12-06 12:04:42 -0500381 fp->is_unreach = true;
382 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500383 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
Dave Barachb5e8a772016-12-06 12:04:42 -0500384 fp->is_prohibit = true;
385 break;
386 default:
387 break;
388 }
389 break;
390 default:
391 break;
392 }
Neale Rannsa0a908f2017-08-01 11:40:03 -0700393 fp->weight = api_rpath->rpath.frp_weight;
394 fp->preference = api_rpath->rpath.frp_preference;
Dave Barach00916112017-10-18 10:54:12 -0400395 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500396 copy_fib_next_hop (api_rpath, fp);
397 fp++;
398 }
399
Florin Coras6c4dae22018-01-09 06:39:23 -0800400 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500401}
402
Dave Barachd7cb1b52016-12-09 09:52:16 -0500403typedef struct apt_ip6_fib_show_ctx_t_
404{
405 u32 fib_index;
406 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500407} api_ip6_fib_show_ctx_t;
408
409static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500410api_ip6_fib_table_put_entries (clib_bihash_kv_24_8_t * kvp, void *arg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500411{
412 api_ip6_fib_show_ctx_t *ctx = arg;
413
414 if ((kvp->key[2] >> 32) == ctx->fib_index)
415 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500416 vec_add1 (ctx->entries, kvp->value);
Dave Barachb5e8a772016-12-06 12:04:42 -0500417 }
418}
419
420static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800421api_ip6_fib_table_get_all (vl_api_registration_t * reg,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500422 vl_api_ip6_fib_dump_t * mp,
423 fib_table_t * fib_table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500424{
425 vpe_api_main_t *am = &vpe_api_main;
426 ip6_main_t *im6 = &ip6_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500427 fib_node_index_t *fib_entry_index;
428 api_ip6_fib_show_ctx_t ctx = {
Neale Rannsa3af3372017-03-28 03:49:52 -0700429 .fib_index = fib_table->ft_index,
430 .entries = NULL,
Dave Barachb5e8a772016-12-06 12:04:42 -0500431 };
432 fib_route_path_encode_t *api_rpaths;
433 fib_prefix_t pfx;
434
Dave Barachd7cb1b52016-12-09 09:52:16 -0500435 BV (clib_bihash_foreach_key_value_pair)
436 ((BVT (clib_bihash) *) & im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].
437 ip6_hash, api_ip6_fib_table_put_entries, &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500438
Dave Barachd7cb1b52016-12-09 09:52:16 -0500439 vec_sort_with_function (ctx.entries, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500440
Dave Barachd7cb1b52016-12-09 09:52:16 -0500441 vec_foreach (fib_entry_index, ctx.entries)
442 {
443 fib_entry_get_prefix (*fib_entry_index, &pfx);
444 api_rpaths = NULL;
445 fib_entry_encode (*fib_entry_index, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800446 send_ip6_fib_details (am, reg, fib_table, &pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500447 vec_free (api_rpaths);
448 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500449
Dave Barachd7cb1b52016-12-09 09:52:16 -0500450 vec_free (ctx.entries);
Dave Barachb5e8a772016-12-06 12:04:42 -0500451}
452
453static void
454vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
455{
Florin Coras6c4dae22018-01-09 06:39:23 -0800456 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500457 ip6_main_t *im6 = &ip6_main;
458 fib_table_t *fib_table;
459
Florin Coras6c4dae22018-01-09 06:39:23 -0800460 reg = vl_api_client_index_to_registration (mp->client_index);
461 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500462 return;
463
464 /* *INDENT-OFF* */
465 pool_foreach (fib_table, im6->fibs,
466 ({
Florin Coras6c4dae22018-01-09 06:39:23 -0800467 api_ip6_fib_table_get_all(reg, mp, fib_table);
Dave Barachb5e8a772016-12-06 12:04:42 -0500468 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500469 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500470}
471
472static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800473send_ip_mfib_details (vl_api_registration_t * reg,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800474 u32 context, u32 table_id, fib_node_index_t mfei)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800475{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800476 fib_route_path_encode_t *api_rpath, *api_rpaths = NULL;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800477 vl_api_ip_mfib_details_t *mp;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800478 mfib_entry_t *mfib_entry;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800479 vl_api_fib_path_t *fp;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800480 mfib_prefix_t pfx;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800481 int path_count;
482
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800483 mfib_entry = mfib_entry_get (mfei);
484 mfib_entry_get_prefix (mfei, &pfx);
485 mfib_entry_encode (mfei, &api_rpaths);
486
Neale Ranns5a8123b2017-01-26 01:18:23 -0800487 path_count = vec_len (api_rpaths);
488 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
489 if (!mp)
490 return;
491 memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700492 mp->_vl_msg_id = ntohs (VL_API_IP_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800493 mp->context = context;
494
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800495 mp->rpf_id = mfib_entry->mfe_rpf_id;
496 mp->entry_flags = mfib_entry->mfe_flags;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800497 mp->table_id = htonl (table_id);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800498 mp->address_length = pfx.fp_len;
499 memcpy (mp->grp_address, &pfx.fp_grp_addr.ip4,
500 sizeof (pfx.fp_grp_addr.ip4));
501 memcpy (mp->src_address, &pfx.fp_src_addr.ip4,
502 sizeof (pfx.fp_src_addr.ip4));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800503
504 mp->count = htonl (path_count);
505 fp = mp->path;
506 vec_foreach (api_rpath, api_rpaths)
507 {
508 memset (fp, 0, sizeof (*fp));
509
510 fp->weight = 0;
511 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
512 copy_fib_next_hop (api_rpath, fp);
513 fp++;
514 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800515 vec_free (api_rpaths);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800516
Florin Coras6c4dae22018-01-09 06:39:23 -0800517 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800518}
519
520typedef struct vl_api_ip_mfib_dump_ctc_t_
521{
522 fib_node_index_t *entries;
523} vl_api_ip_mfib_dump_ctc_t;
524
525static int
526vl_api_ip_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
527{
528 vl_api_ip_mfib_dump_ctc_t *ctx = arg;
529
530 vec_add1 (ctx->entries, fei);
531
532 return (0);
533}
534
535static void
536vl_api_ip_mfib_dump_t_handler (vl_api_ip_mfib_dump_t * mp)
537{
Florin Coras6c4dae22018-01-09 06:39:23 -0800538 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800539 ip4_main_t *im = &ip4_main;
540 mfib_table_t *mfib_table;
541 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800542 vl_api_ip_mfib_dump_ctc_t ctx = {
543 .entries = NULL,
544 };
545
Florin Coras6c4dae22018-01-09 06:39:23 -0800546 reg = vl_api_client_index_to_registration (mp->client_index);
547 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800548 return;
549
Neale Ranns5a8123b2017-01-26 01:18:23 -0800550 /* *INDENT-OFF* */
551 pool_foreach (mfib_table, im->mfibs,
552 ({
553 ip4_mfib_table_walk(&mfib_table->v4,
554 vl_api_ip_mfib_table_dump_walk,
555 &ctx);
556
557 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
558
559 vec_foreach (mfeip, ctx.entries)
560 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800561 send_ip_mfib_details (reg, mp->context,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800562 mfib_table->mft_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800563 *mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800564 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800565 vec_reset_length (ctx.entries);
566
567 }));
568 /* *INDENT-ON* */
569
570 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800571}
572
573static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800574send_ip6_mfib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800575 vl_api_registration_t * reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800576 u32 table_id,
577 mfib_prefix_t * pfx,
578 fib_route_path_encode_t * api_rpaths, u32 context)
579{
580 vl_api_ip6_mfib_details_t *mp;
581 fib_route_path_encode_t *api_rpath;
582 vl_api_fib_path_t *fp;
583 int path_count;
584
585 path_count = vec_len (api_rpaths);
586 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
587 if (!mp)
588 return;
589 memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700590 mp->_vl_msg_id = ntohs (VL_API_IP6_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800591 mp->context = context;
592
593 mp->table_id = htonl (table_id);
594 mp->address_length = pfx->fp_len;
595 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip6,
596 sizeof (pfx->fp_grp_addr.ip6));
597 memcpy (mp->src_address, &pfx->fp_src_addr.ip6,
598 sizeof (pfx->fp_src_addr.ip6));
599
600 mp->count = htonl (path_count);
601 fp = mp->path;
602 vec_foreach (api_rpath, api_rpaths)
603 {
604 memset (fp, 0, sizeof (*fp));
605
606 fp->weight = 0;
607 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
608 copy_fib_next_hop (api_rpath, fp);
609 fp++;
610 }
611
Florin Coras6c4dae22018-01-09 06:39:23 -0800612 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800613}
614
615typedef struct vl_api_ip6_mfib_dump_ctc_t_
616{
617 fib_node_index_t *entries;
618} vl_api_ip6_mfib_dump_ctc_t;
619
620static int
621vl_api_ip6_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
622{
623 vl_api_ip6_mfib_dump_ctc_t *ctx = arg;
624
625 vec_add1 (ctx->entries, fei);
626
627 return (0);
628}
629
630static void
631vl_api_ip6_mfib_dump_t_handler (vl_api_ip6_mfib_dump_t * mp)
632{
633 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800634 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800635 ip6_main_t *im = &ip6_main;
636 mfib_table_t *mfib_table;
637 fib_node_index_t *mfeip;
638 mfib_prefix_t pfx;
639 fib_route_path_encode_t *api_rpaths = NULL;
640 vl_api_ip6_mfib_dump_ctc_t ctx = {
641 .entries = NULL,
642 };
643
Florin Coras6c4dae22018-01-09 06:39:23 -0800644 reg = vl_api_client_index_to_registration (mp->client_index);
645 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800646 return;
647
648
649 /* *INDENT-OFF* */
650 pool_foreach (mfib_table, im->mfibs,
651 ({
652 ip6_mfib_table_walk(&mfib_table->v6,
653 vl_api_ip6_mfib_table_dump_walk,
654 &ctx);
655
656 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
657
658 vec_foreach(mfeip, ctx.entries)
659 {
660 mfib_entry_get_prefix (*mfeip, &pfx);
661 mfib_entry_encode (*mfeip, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800662 send_ip6_mfib_details (am, reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800663 mfib_table->mft_table_id,
664 &pfx, api_rpaths,
665 mp->context);
666 }
667 vec_reset_length (api_rpaths);
668 vec_reset_length (ctx.entries);
669
670 }));
671 /* *INDENT-ON* */
672
673 vec_free (ctx.entries);
674 vec_free (api_rpaths);
675}
676
677static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700678vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
679 vlib_main_t * vm)
680{
681 vl_api_ip_punt_police_reply_t *rmp;
682 int rv = 0;
683
684 if (mp->is_ip6)
685 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
686 else
687 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
688
689 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
690}
691
692static void
693vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
694 vlib_main_t * vm)
695{
696 vl_api_ip_punt_redirect_reply_t *rmp;
697 int rv = 0;
698
699 if (mp->is_add)
700 {
701 ip46_address_t nh;
702
703 memset (&nh, 0, sizeof (nh));
704
705 if (mp->is_ip6)
706 {
707 memcpy (&nh.ip6, mp->nh, sizeof (nh.ip6));
708
709 ip6_punt_redirect_add (ntohl (mp->rx_sw_if_index),
710 ntohl (mp->tx_sw_if_index), &nh);
711 }
712 else
713 {
714 memcpy (&nh.ip4, mp->nh, sizeof (nh.ip4));
715
716 ip4_punt_redirect_add (ntohl (mp->rx_sw_if_index),
717 ntohl (mp->tx_sw_if_index), &nh);
718 }
719 }
720 else
721 {
722 if (mp->is_ip6)
723 {
724 ip6_punt_redirect_del (ntohl (mp->rx_sw_if_index));
725 }
726 else
727 {
728 ip4_punt_redirect_del (ntohl (mp->rx_sw_if_index));
729 }
730 }
731
732 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
733}
734
735static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500736vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
737 vlib_main_t * vm)
738{
739 vl_api_ip_neighbor_add_del_reply_t *rmp;
740 vnet_main_t *vnm = vnet_get_main ();
741 int rv = 0;
742
743 VALIDATE_SW_IF_INDEX (mp);
744
745 stats_dslock_with_hint (1 /* release hint */ , 7 /* tag */ );
746
747 /*
748 * there's no validation here of the ND/ARP entry being added.
749 * The expectation is that the FIB will ensure that nothing bad
750 * will come of adding bogus entries.
751 */
752 if (mp->is_ipv6)
753 {
754 if (mp->is_add)
755 rv = vnet_set_ip6_ethernet_neighbor
756 (vm, ntohl (mp->sw_if_index),
757 (ip6_address_t *) (mp->dst_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800758 mp->mac_address, sizeof (mp->mac_address), mp->is_static,
759 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500760 else
761 rv = vnet_unset_ip6_ethernet_neighbor
762 (vm, ntohl (mp->sw_if_index),
763 (ip6_address_t *) (mp->dst_address),
764 mp->mac_address, sizeof (mp->mac_address));
765 }
766 else
767 {
768 ethernet_arp_ip4_over_ethernet_address_t a;
769
770 clib_memcpy (&a.ethernet, mp->mac_address, 6);
771 clib_memcpy (&a.ip4, mp->dst_address, 4);
772
773 if (mp->is_add)
774 rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800775 &a, mp->is_static,
776 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500777 else
778 rv =
779 vnet_arp_unset_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index), &a);
780 }
781
Dave Barachb5e8a772016-12-06 12:04:42 -0500782 stats_dsunlock ();
Neale Ranns8edad032017-10-09 05:26:13 -0700783
784 BAD_SW_IF_INDEX_LABEL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500785 REPLY_MACRO (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
786}
787
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700788void
Neale Ranns15002542017-09-10 04:39:11 -0700789ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
790{
791 u32 fib_index, mfib_index;
792
793 /*
794 * ignore action on the default table - this is always present
795 * and cannot be added nor deleted from the API
796 */
797 if (0 != table_id)
798 {
799 /*
800 * The API holds only one lock on the table.
801 * i.e. it can be added many times via the API but needs to be
802 * deleted only once.
803 * The FIB index for unicast and multicast is not necessarily the
804 * same, since internal VPP systesm (like LISP and SR) create
805 * their own unicast tables.
806 */
807 fib_index = fib_table_find (fproto, table_id);
808 mfib_index = mfib_table_find (fproto, table_id);
809
810 if (~0 != fib_index)
811 {
812 fib_table_unlock (fib_index, fproto,
813 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
814 }
815 if (~0 != mfib_index)
816 {
817 mfib_table_unlock (mfib_index, fproto,
818 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
819 }
820 }
821}
822
823void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700824vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
825{
826 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700827 fib_protocol_t fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
828 u32 table_id = ntohl (mp->table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700829 int rv = 0;
830
Neale Ranns15002542017-09-10 04:39:11 -0700831 if (mp->is_add)
832 {
Neale Ranns2297af02017-09-12 09:45:04 -0700833 ip_table_create (fproto, table_id, 1, mp->name);
Neale Ranns15002542017-09-10 04:39:11 -0700834 }
835 else
836 {
837 ip_table_delete (fproto, table_id, 1);
838 }
839
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700840 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
841}
842
Dave Barachb5e8a772016-12-06 12:04:42 -0500843int
844add_del_route_t_handler (u8 is_multipath,
845 u8 is_add,
846 u8 is_drop,
847 u8 is_unreach,
848 u8 is_prohibit,
849 u8 is_local,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800850 u8 is_multicast,
Dave Barachb5e8a772016-12-06 12:04:42 -0500851 u8 is_classify,
852 u32 classify_table_index,
853 u8 is_resolve_host,
854 u8 is_resolve_attached,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800855 u8 is_interface_rx,
856 u8 is_rpf_id,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800857 u8 is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -0700858 u8 is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800859 u8 is_udp_encap,
Dave Barachb5e8a772016-12-06 12:04:42 -0500860 u32 fib_index,
861 const fib_prefix_t * prefix,
Neale Rannsda78f952017-05-24 09:15:43 -0700862 dpo_proto_t next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500863 const ip46_address_t * next_hop,
Neale Ranns810086d2017-11-05 16:26:46 -0800864 u32 next_hop_id,
Dave Barachb5e8a772016-12-06 12:04:42 -0500865 u32 next_hop_sw_if_index,
866 u8 next_hop_fib_index,
Neale Ranns57b58602017-07-15 07:37:25 -0700867 u16 next_hop_weight,
868 u16 next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500869 mpls_label_t next_hop_via_label,
870 mpls_label_t * next_hop_out_label_stack)
871{
872 vnet_classify_main_t *cm = &vnet_classify_main;
873 fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
874 fib_route_path_t path = {
Neale Rannsda78f952017-05-24 09:15:43 -0700875 .frp_proto = next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500876 .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
877 .frp_sw_if_index = next_hop_sw_if_index,
878 .frp_fib_index = next_hop_fib_index,
879 .frp_weight = next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -0700880 .frp_preference = next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500881 .frp_label_stack = next_hop_out_label_stack,
882 };
883 fib_route_path_t *paths = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800884 fib_entry_flag_t entry_flags = FIB_ENTRY_FLAG_NONE;
Dave Barachb5e8a772016-12-06 12:04:42 -0500885
Neale Rannscaac3502017-09-18 18:04:10 -0700886 /*
887 * the special INVALID label meams we are not recursing via a
888 * label. Exp-null value is never a valid via-label so that
889 * also means it's not a via-label and means clients that set
890 * it to 0 by default get the expected behaviour
891 */
892 if ((MPLS_LABEL_INVALID != next_hop_via_label) && (0 != next_hop_via_label))
Dave Barachb5e8a772016-12-06 12:04:42 -0500893 {
Neale Rannsda78f952017-05-24 09:15:43 -0700894 path.frp_proto = DPO_PROTO_MPLS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500895 path.frp_local_label = next_hop_via_label;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800896 path.frp_eos = MPLS_NON_EOS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500897 }
Neale Rannsf068c3e2018-01-03 04:18:48 -0800898 if (is_dvr)
899 path_flags |= FIB_ROUTE_PATH_DVR;
Dave Barachb5e8a772016-12-06 12:04:42 -0500900 if (is_resolve_host)
901 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
902 if (is_resolve_attached)
903 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800904 if (is_interface_rx)
905 path_flags |= FIB_ROUTE_PATH_INTF_RX;
906 if (is_rpf_id)
907 path_flags |= FIB_ROUTE_PATH_RPF_ID;
Neale Ranns054c03a2017-10-13 05:15:07 -0700908 if (is_source_lookup)
909 path_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800910 if (is_multicast)
911 entry_flags |= FIB_ENTRY_FLAG_MULTICAST;
Neale Ranns810086d2017-11-05 16:26:46 -0800912 if (is_udp_encap)
913 {
914 path_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
915 path.frp_udp_encap_id = next_hop_id;
916 }
Florin Coras79ae2d32017-12-16 08:31:06 -0800917 if (path.frp_sw_if_index == ~0 && ip46_address_is_zero (&path.frp_addr)
918 && path.frp_fib_index != ~0)
919 {
920 path_flags |= FIB_ROUTE_PATH_DEAG;
921 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500922
923 path.frp_flags = path_flags;
924
925 if (is_multipath)
926 {
927 stats_dslock_with_hint (1 /* release hint */ , 10 /* tag */ );
928
929
930 vec_add1 (paths, path);
931
932 if (is_add)
933 fib_table_entry_path_add2 (fib_index,
934 prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800935 FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500936 else
937 fib_table_entry_path_remove2 (fib_index,
938 prefix, FIB_SOURCE_API, paths);
939
940 vec_free (paths);
941 stats_dsunlock ();
942 return 0;
943 }
944
945 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
946
947 if (is_drop || is_local || is_classify || is_unreach || is_prohibit)
948 {
949 /*
950 * special route types that link directly to the adj
951 */
952 if (is_add)
953 {
954 dpo_id_t dpo = DPO_INVALID;
955 dpo_proto_t dproto;
956
957 dproto = fib_proto_to_dpo (prefix->fp_proto);
958
959 if (is_drop)
960 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
961 else if (is_local)
962 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
963 else if (is_unreach)
964 ip_null_dpo_add_and_lock (dproto,
965 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
966 else if (is_prohibit)
967 ip_null_dpo_add_and_lock (dproto,
968 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
969 &dpo);
970 else if (is_classify)
971 {
972 if (pool_is_free_index (cm->tables,
973 ntohl (classify_table_index)))
974 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500975 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500976 return VNET_API_ERROR_NO_SUCH_TABLE;
977 }
978
979 dpo_set (&dpo, DPO_CLASSIFY, dproto,
980 classify_dpo_create (dproto,
981 ntohl (classify_table_index)));
982 }
983 else
984 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500985 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500986 return VNET_API_ERROR_NO_SUCH_TABLE;
987 }
988
989 fib_table_entry_special_dpo_update (fib_index,
990 prefix,
991 FIB_SOURCE_API,
992 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
993 dpo_reset (&dpo);
994 }
995 else
996 {
997 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
998 }
999 }
1000 else
1001 {
1002 if (is_add)
1003 {
1004 vec_add1 (paths, path);
1005 fib_table_entry_update (fib_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001006 prefix, FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -05001007 vec_free (paths);
1008 }
1009 else
1010 {
1011 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
1012 }
1013 }
1014
Dave Barachd7cb1b52016-12-09 09:52:16 -05001015 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -05001016 return (0);
1017}
1018
1019int
1020add_del_route_check (fib_protocol_t table_proto,
1021 u32 table_id,
1022 u32 next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001023 dpo_proto_t next_hop_table_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -05001024 u32 next_hop_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001025 u8 is_rpf_id, u32 * fib_index, u32 * next_hop_fib_index)
Dave Barachb5e8a772016-12-06 12:04:42 -05001026{
1027 vnet_main_t *vnm = vnet_get_main ();
1028
1029 *fib_index = fib_table_find (table_proto, ntohl (table_id));
1030 if (~0 == *fib_index)
1031 {
Neale Rannsb93078d2018-01-25 07:48:12 -08001032 /* No such VRF, and we weren't asked to create one */
1033 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -05001034 }
1035
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001036 if (!is_rpf_id && ~0 != ntohl (next_hop_sw_if_index))
Dave Barachb5e8a772016-12-06 12:04:42 -05001037 {
1038 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1039 ntohl (next_hop_sw_if_index)))
1040 {
1041 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1042 }
1043 }
1044 else
1045 {
Neale Rannsda78f952017-05-24 09:15:43 -07001046 fib_protocol_t fib_nh_proto;
1047
1048 if (next_hop_table_proto > DPO_PROTO_MPLS)
1049 return (0);
1050
1051 fib_nh_proto = dpo_proto_to_fib (next_hop_table_proto);
1052
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001053 if (is_rpf_id)
Neale Rannsda78f952017-05-24 09:15:43 -07001054 *next_hop_fib_index = mfib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001055 ntohl (next_hop_table_id));
1056 else
Neale Rannsda78f952017-05-24 09:15:43 -07001057 *next_hop_fib_index = fib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001058 ntohl (next_hop_table_id));
Dave Barachb5e8a772016-12-06 12:04:42 -05001059
1060 if (~0 == *next_hop_fib_index)
1061 {
Neale Rannsb93078d2018-01-25 07:48:12 -08001062 /* No such VRF, and we weren't asked to create one */
1063 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -05001064 }
1065 }
1066
1067 return (0);
1068}
1069
1070static int
1071ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1072{
1073 u32 fib_index, next_hop_fib_index;
1074 mpls_label_t *label_stack = NULL;
1075 int rv, ii, n_labels;;
1076
1077 rv = add_del_route_check (FIB_PROTOCOL_IP4,
1078 mp->table_id,
1079 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001080 DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -05001081 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001082 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001083
1084 if (0 != rv)
1085 return (rv);
1086
1087 fib_prefix_t pfx = {
1088 .fp_len = mp->dst_address_length,
1089 .fp_proto = FIB_PROTOCOL_IP4,
1090 };
1091 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
1092
1093 ip46_address_t nh;
1094 memset (&nh, 0, sizeof (nh));
1095 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
1096
1097 n_labels = mp->next_hop_n_out_labels;
1098 if (n_labels == 0)
1099 ;
1100 else if (1 == n_labels)
1101 vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
1102 else
1103 {
1104 vec_validate (label_stack, n_labels - 1);
1105 for (ii = 0; ii < n_labels; ii++)
1106 label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
1107 }
1108
1109 return (add_del_route_t_handler (mp->is_multipath,
1110 mp->is_add,
1111 mp->is_drop,
1112 mp->is_unreach,
1113 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001114 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001115 mp->is_classify,
1116 mp->classify_table_index,
1117 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001118 mp->is_resolve_attached, 0, 0,
Neale Rannsf068c3e2018-01-03 04:18:48 -08001119 mp->is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -07001120 mp->is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -08001121 mp->is_udp_encap,
Neale Rannsda78f952017-05-24 09:15:43 -07001122 fib_index, &pfx, DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -05001123 &nh,
Neale Ranns810086d2017-11-05 16:26:46 -08001124 ntohl (mp->next_hop_id),
Dave Barachb5e8a772016-12-06 12:04:42 -05001125 ntohl (mp->next_hop_sw_if_index),
1126 next_hop_fib_index,
1127 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001128 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001129 ntohl (mp->next_hop_via_label),
1130 label_stack));
1131}
1132
1133static int
1134ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1135{
1136 u32 fib_index, next_hop_fib_index;
1137 mpls_label_t *label_stack = NULL;
1138 int rv, ii, n_labels;;
1139
1140 rv = add_del_route_check (FIB_PROTOCOL_IP6,
1141 mp->table_id,
1142 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001143 DPO_PROTO_IP6,
Dave Barachb5e8a772016-12-06 12:04:42 -05001144 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001145 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001146
1147 if (0 != rv)
1148 return (rv);
1149
1150 fib_prefix_t pfx = {
1151 .fp_len = mp->dst_address_length,
1152 .fp_proto = FIB_PROTOCOL_IP6,
1153 };
1154 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1155
1156 ip46_address_t nh;
1157 memset (&nh, 0, sizeof (nh));
1158 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1159
1160 n_labels = mp->next_hop_n_out_labels;
1161 if (n_labels == 0)
1162 ;
1163 else if (1 == n_labels)
1164 vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
1165 else
1166 {
1167 vec_validate (label_stack, n_labels - 1);
1168 for (ii = 0; ii < n_labels; ii++)
1169 label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
1170 }
1171
1172 return (add_del_route_t_handler (mp->is_multipath,
1173 mp->is_add,
1174 mp->is_drop,
1175 mp->is_unreach,
1176 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001177 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001178 mp->is_classify,
1179 mp->classify_table_index,
1180 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001181 mp->is_resolve_attached, 0, 0,
Neale Rannsf068c3e2018-01-03 04:18:48 -08001182 mp->is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -07001183 mp->is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -08001184 mp->is_udp_encap,
Neale Rannsda78f952017-05-24 09:15:43 -07001185 fib_index, &pfx, DPO_PROTO_IP6,
Neale Ranns810086d2017-11-05 16:26:46 -08001186 &nh, ntohl (mp->next_hop_id),
1187 ntohl (mp->next_hop_sw_if_index),
Dave Barachb5e8a772016-12-06 12:04:42 -05001188 next_hop_fib_index,
1189 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001190 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001191 ntohl (mp->next_hop_via_label),
1192 label_stack));
1193}
1194
1195void
1196vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1197{
1198 vl_api_ip_add_del_route_reply_t *rmp;
1199 int rv;
1200 vnet_main_t *vnm = vnet_get_main ();
1201
1202 vnm->api_errno = 0;
1203
1204 if (mp->is_ipv6)
1205 rv = ip6_add_del_route_t_handler (mp);
1206 else
1207 rv = ip4_add_del_route_t_handler (mp);
1208
1209 rv = (rv == 0) ? vnm->api_errno : rv;
1210
1211 REPLY_MACRO (VL_API_IP_ADD_DEL_ROUTE_REPLY);
1212}
1213
Neale Ranns15002542017-09-10 04:39:11 -07001214void
Neale Ranns2297af02017-09-12 09:45:04 -07001215ip_table_create (fib_protocol_t fproto,
1216 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -07001217{
1218 u32 fib_index, mfib_index;
1219
1220 /*
1221 * ignore action on the default table - this is always present
1222 * and cannot be added nor deleted from the API
1223 */
1224 if (0 != table_id)
1225 {
1226 /*
1227 * The API holds only one lock on the table.
1228 * i.e. it can be added many times via the API but needs to be
1229 * deleted only once.
1230 * The FIB index for unicast and multicast is not necessarily the
1231 * same, since internal VPP systesm (like LISP and SR) create
1232 * their own unicast tables.
1233 */
1234 fib_index = fib_table_find (fproto, table_id);
1235 mfib_index = mfib_table_find (fproto, table_id);
1236
1237 if (~0 == fib_index)
1238 {
Neale Ranns2297af02017-09-12 09:45:04 -07001239 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
1240 (is_api ?
1241 FIB_SOURCE_API :
1242 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001243 }
1244 if (~0 == mfib_index)
1245 {
Neale Ranns2297af02017-09-12 09:45:04 -07001246 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
1247 (is_api ?
1248 MFIB_SOURCE_API :
1249 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001250 }
1251 }
1252}
1253
Neale Ranns32e1c012016-11-22 17:07:28 +00001254static int
1255add_del_mroute_check (fib_protocol_t table_proto,
1256 u32 table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001257 u32 next_hop_sw_if_index, u8 is_local, u32 * fib_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001258{
1259 vnet_main_t *vnm = vnet_get_main ();
1260
1261 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1262 if (~0 == *fib_index)
1263 {
Neale Ranns15002542017-09-10 04:39:11 -07001264 /* No such table */
1265 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Ranns32e1c012016-11-22 17:07:28 +00001266 }
1267
1268 if (~0 != ntohl (next_hop_sw_if_index))
1269 {
1270 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1271 ntohl (next_hop_sw_if_index)))
1272 {
1273 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1274 }
1275 }
1276
1277 return (0);
1278}
1279
1280static int
1281mroute_add_del_handler (u8 is_add,
1282 u8 is_local,
1283 u32 fib_index,
1284 const mfib_prefix_t * prefix,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001285 dpo_proto_t nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001286 u32 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001287 fib_rpf_id_t rpf_id,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001288 u32 next_hop_sw_if_index, u32 itf_flags, u32 bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +00001289{
1290 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1291
1292 fib_route_path_t path = {
1293 .frp_sw_if_index = next_hop_sw_if_index,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001294 .frp_proto = nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001295 };
1296
1297 if (is_local)
1298 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1299
Neale Rannsd792d9c2017-10-21 10:53:20 -07001300 if (DPO_PROTO_BIER == nh_proto)
1301 {
1302 path.frp_bier_imp = bier_imp;
1303 path.frp_flags = FIB_ROUTE_PATH_BIER_IMP;
1304 }
1305 else if (!is_local && ~0 == next_hop_sw_if_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001306 {
1307 mfib_table_entry_update (fib_index, prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001308 MFIB_SOURCE_API, rpf_id, entry_flags);
Neale Rannsd792d9c2017-10-21 10:53:20 -07001309 goto done;
1310 }
1311
1312 if (is_add)
1313 {
1314 mfib_table_entry_path_update (fib_index, prefix,
1315 MFIB_SOURCE_API, &path, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001316 }
1317 else
1318 {
Neale Rannsd792d9c2017-10-21 10:53:20 -07001319 mfib_table_entry_path_remove (fib_index, prefix,
1320 MFIB_SOURCE_API, &path);
Neale Ranns32e1c012016-11-22 17:07:28 +00001321 }
1322
Neale Rannsd792d9c2017-10-21 10:53:20 -07001323done:
Neale Ranns32e1c012016-11-22 17:07:28 +00001324 stats_dsunlock ();
1325 return (0);
1326}
1327
1328static int
1329api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1330{
1331 fib_protocol_t fproto;
Neale Rannsd792d9c2017-10-21 10:53:20 -07001332 dpo_proto_t nh_proto;
Neale Ranns32e1c012016-11-22 17:07:28 +00001333 u32 fib_index;
1334 int rv;
1335
Neale Rannsd792d9c2017-10-21 10:53:20 -07001336 nh_proto = mp->next_hop_afi;
Neale Ranns32e1c012016-11-22 17:07:28 +00001337 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1338 rv = add_del_mroute_check (fproto,
1339 mp->table_id,
1340 mp->next_hop_sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -07001341 mp->is_local, &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001342
1343 if (0 != rv)
1344 return (rv);
1345
1346 mfib_prefix_t pfx = {
1347 .fp_len = ntohs (mp->grp_address_length),
1348 .fp_proto = fproto,
1349 };
1350
1351 if (FIB_PROTOCOL_IP4 == fproto)
1352 {
1353 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1354 sizeof (pfx.fp_grp_addr.ip4));
1355 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1356 sizeof (pfx.fp_src_addr.ip4));
1357 }
1358 else
1359 {
1360 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1361 sizeof (pfx.fp_grp_addr.ip6));
1362 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1363 sizeof (pfx.fp_src_addr.ip6));
1364 }
1365
1366 return (mroute_add_del_handler (mp->is_add,
1367 mp->is_local,
1368 fib_index, &pfx,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001369 nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001370 ntohl (mp->entry_flags),
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001371 ntohl (mp->rpf_id),
Neale Ranns32e1c012016-11-22 17:07:28 +00001372 ntohl (mp->next_hop_sw_if_index),
Neale Rannsd792d9c2017-10-21 10:53:20 -07001373 ntohl (mp->itf_flags),
1374 ntohl (mp->bier_imp)));
Neale Ranns32e1c012016-11-22 17:07:28 +00001375}
1376
1377void
1378vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1379{
1380 vl_api_ip_mroute_add_del_reply_t *rmp;
1381 int rv;
1382 vnet_main_t *vnm = vnet_get_main ();
1383
1384 vnm->api_errno = 0;
1385
1386 rv = api_mroute_add_del_t_handler (mp);
1387
1388 rv = (rv == 0) ? vnm->api_errno : rv;
1389
1390 REPLY_MACRO (VL_API_IP_MROUTE_ADD_DEL_REPLY);
1391}
1392
Dave Barachb5e8a772016-12-06 12:04:42 -05001393static void
1394send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001395 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1396 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001397{
1398 vl_api_ip_details_t *mp;
1399
1400 mp = vl_msg_api_alloc (sizeof (*mp));
1401 memset (mp, 0, sizeof (*mp));
1402 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1403
1404 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001405 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001406 mp->context = context;
1407
Florin Coras6c4dae22018-01-09 06:39:23 -08001408 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001409}
1410
1411static void
1412send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001413 vl_api_registration_t * reg,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001414 u8 * ip, u16 prefix_length,
1415 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001416{
1417 vl_api_ip_address_details_t *mp;
1418
1419 mp = vl_msg_api_alloc (sizeof (*mp));
1420 memset (mp, 0, sizeof (*mp));
1421 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1422
1423 if (is_ipv6)
1424 {
1425 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1426 }
1427 else
1428 {
1429 u32 *tp = (u32 *) mp->ip;
1430 *tp = *(u32 *) ip;
1431 }
1432 mp->prefix_length = prefix_length;
1433 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001434 mp->sw_if_index = htonl (sw_if_index);
1435 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001436
Florin Coras6c4dae22018-01-09 06:39:23 -08001437 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001438}
1439
1440static void
1441vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1442{
1443 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001444 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001445 ip6_address_t *r6;
1446 ip4_address_t *r4;
1447 ip6_main_t *im6 = &ip6_main;
1448 ip4_main_t *im4 = &ip4_main;
1449 ip_lookup_main_t *lm6 = &im6->lookup_main;
1450 ip_lookup_main_t *lm4 = &im4->lookup_main;
1451 ip_interface_address_t *ia = 0;
1452 u32 sw_if_index = ~0;
1453 int rv __attribute__ ((unused)) = 0;
1454
1455 VALIDATE_SW_IF_INDEX (mp);
1456
1457 sw_if_index = ntohl (mp->sw_if_index);
1458
Florin Coras6c4dae22018-01-09 06:39:23 -08001459 reg = vl_api_client_index_to_registration (mp->client_index);
1460 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001461 return;
1462
Dave Barachb5e8a772016-12-06 12:04:42 -05001463 if (mp->is_ipv6)
1464 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001465 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001466 foreach_ip_interface_address (lm6, ia, sw_if_index,
1467 1 /* honor unnumbered */,
1468 ({
1469 r6 = ip_interface_address_get_address (lm6, ia);
1470 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001471 send_ip_address_details(am, reg, (u8*)r6, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001472 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001473 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001474 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001475 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001476 else
1477 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001478 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001479 foreach_ip_interface_address (lm4, ia, sw_if_index,
1480 1 /* honor unnumbered */,
1481 ({
1482 r4 = ip_interface_address_get_address (lm4, ia);
1483 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001484 send_ip_address_details(am, reg, (u8*)r4, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001485 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001486 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001487 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001488 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001489 BAD_SW_IF_INDEX_LABEL;
1490}
1491
1492static void
1493vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1494{
1495 vpe_api_main_t *am = &vpe_api_main;
1496 vnet_main_t *vnm = vnet_get_main ();
1497 vlib_main_t *vm = vlib_get_main ();
1498 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001499 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001500 vnet_sw_interface_t *si, *sorted_sis;
1501 u32 sw_if_index = ~0;
1502
Florin Coras6c4dae22018-01-09 06:39:23 -08001503 reg = vl_api_client_index_to_registration (mp->client_index);
1504 if (!reg)
1505 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001506
1507 /* Gather interfaces. */
1508 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1509 _vec_len (sorted_sis) = 0;
1510 /* *INDENT-OFF* */
1511 pool_foreach (si, im->sw_interfaces,
1512 ({
1513 vec_add1 (sorted_sis, si[0]);
1514 }));
1515 /* *INDENT-ON* */
1516
1517 vec_foreach (si, sorted_sis)
1518 {
1519 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1520 {
1521 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1522 {
1523 continue;
1524 }
1525 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001526 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001527 }
1528 }
1529}
1530
1531static void
1532set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1533{
1534 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001535 int rv;
1536 u32 table_id;
1537 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001538
Neale Ranns227038a2017-04-21 01:07:59 -07001539 table_id = ntohl (mp->vrf_id);
1540
1541#define _(a,b) if (mp->a) flow_hash_config |= b;
1542 foreach_flow_hash_bit;
1543#undef _
1544
1545 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001546
1547 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1548}
1549
1550static void
1551set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1552{
1553 vl_api_set_ip_flow_hash_reply_t *rmp;
1554 int rv;
1555 u32 table_id;
1556 flow_hash_config_t flow_hash_config = 0;
1557
1558 table_id = ntohl (mp->vrf_id);
1559
1560#define _(a,b) if (mp->a) flow_hash_config |= b;
1561 foreach_flow_hash_bit;
1562#undef _
1563
1564 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1565
1566 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1567}
1568
1569
1570static void
1571vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1572{
1573 if (mp->is_ipv6 == 0)
1574 set_ip4_flow_hash (mp);
1575 else
1576 set_ip6_flow_hash (mp);
1577}
1578
1579static void
1580 vl_api_sw_interface_ip6nd_ra_config_t_handler
1581 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1582{
1583 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1584 vlib_main_t *vm = vlib_get_main ();
1585 int rv = 0;
1586 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1587 default_router;
1588
1589 is_no = mp->is_no == 1;
1590 suppress = mp->suppress == 1;
1591 managed = mp->managed == 1;
1592 other = mp->other == 1;
1593 ll_option = mp->ll_option == 1;
1594 send_unicast = mp->send_unicast == 1;
1595 cease = mp->cease == 1;
1596 default_router = mp->default_router == 1;
1597
1598 VALIDATE_SW_IF_INDEX (mp);
1599
1600 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1601 suppress, managed, other,
1602 ll_option, send_unicast, cease,
1603 default_router, ntohl (mp->lifetime),
1604 ntohl (mp->initial_count),
1605 ntohl (mp->initial_interval),
1606 ntohl (mp->max_interval),
1607 ntohl (mp->min_interval), is_no);
1608
1609 BAD_SW_IF_INDEX_LABEL;
1610
1611 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1612}
1613
1614static void
1615 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1616 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1617{
1618 vlib_main_t *vm = vlib_get_main ();
1619 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1620 int rv = 0;
1621 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1622
1623 VALIDATE_SW_IF_INDEX (mp);
1624
1625 is_no = mp->is_no == 1;
1626 use_default = mp->use_default == 1;
1627 no_advertise = mp->no_advertise == 1;
1628 off_link = mp->off_link == 1;
1629 no_autoconfig = mp->no_autoconfig == 1;
1630 no_onlink = mp->no_onlink == 1;
1631
1632 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1633 (ip6_address_t *) mp->address,
1634 mp->address_length, use_default,
1635 ntohl (mp->val_lifetime),
1636 ntohl (mp->pref_lifetime), no_advertise,
1637 off_link, no_autoconfig, no_onlink, is_no);
1638
1639 BAD_SW_IF_INDEX_LABEL;
1640 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1641}
1642
1643static void
Florin Coras6c4dae22018-01-09 06:39:23 -08001644send_ip6nd_proxy_details (vl_api_registration_t * reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001645 u32 context,
1646 const ip46_address_t * addr, u32 sw_if_index)
1647{
1648 vl_api_ip6nd_proxy_details_t *mp;
1649
1650 mp = vl_msg_api_alloc (sizeof (*mp));
1651 memset (mp, 0, sizeof (*mp));
1652 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1653 mp->context = context;
1654 mp->sw_if_index = htonl (sw_if_index);
1655 memcpy (mp->address, addr, 16);
1656
Florin Coras6c4dae22018-01-09 06:39:23 -08001657 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns3f844d02017-02-18 00:03:54 -08001658}
1659
Neale Ranns3f844d02017-02-18 00:03:54 -08001660typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1661{
1662 u32 *indices;
1663} api_ip6nd_proxy_fib_table_walk_ctx_t;
1664
Neale Ranns89541992017-04-06 04:41:02 -07001665static fib_table_walk_rc_t
Neale Ranns3f844d02017-02-18 00:03:54 -08001666api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1667{
1668 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1669
1670 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1671 {
1672 vec_add1 (ctx->indices, fei);
1673 }
1674
Neale Ranns89541992017-04-06 04:41:02 -07001675 return (FIB_TABLE_WALK_CONTINUE);
Neale Ranns3f844d02017-02-18 00:03:54 -08001676}
1677
1678static void
1679vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1680{
1681 ip6_main_t *im6 = &ip6_main;
1682 fib_table_t *fib_table;
1683 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1684 .indices = NULL,
1685 };
1686 fib_node_index_t *feip;
1687 fib_prefix_t pfx;
Florin Coras6c4dae22018-01-09 06:39:23 -08001688 vl_api_registration_t *reg;
Neale Ranns3f844d02017-02-18 00:03:54 -08001689
Florin Coras6c4dae22018-01-09 06:39:23 -08001690 reg = vl_api_client_index_to_registration (mp->client_index);
1691 if (!reg)
1692 return;
Neale Ranns3f844d02017-02-18 00:03:54 -08001693
1694 /* *INDENT-OFF* */
1695 pool_foreach (fib_table, im6->fibs,
1696 ({
1697 fib_table_walk(fib_table->ft_index,
1698 FIB_PROTOCOL_IP6,
1699 api_ip6nd_proxy_fib_table_walk,
1700 &ctx);
1701 }));
1702 /* *INDENT-ON* */
1703
1704 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1705
1706 vec_foreach (feip, ctx.indices)
1707 {
1708 fib_entry_get_prefix (*feip, &pfx);
1709
Florin Coras6c4dae22018-01-09 06:39:23 -08001710 send_ip6nd_proxy_details (reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001711 mp->context,
1712 &pfx.fp_addr,
1713 fib_entry_get_resolving_interface (*feip));
1714 }
1715
1716 vec_free (ctx.indices);
1717}
1718
1719static void
1720vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1721{
1722 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
1723 int rv = 0;
1724
1725 VALIDATE_SW_IF_INDEX (mp);
1726
1727 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index),
1728 (ip6_address_t *) mp->address, mp->is_del);
1729
1730 BAD_SW_IF_INDEX_LABEL;
1731 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1732}
1733
1734static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001735 vl_api_sw_interface_ip6_enable_disable_t_handler
1736 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1737{
1738 vlib_main_t *vm = vlib_get_main ();
1739 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1740 vnet_main_t *vnm = vnet_get_main ();
1741 int rv = 0;
1742 clib_error_t *error;
1743
1744 vnm->api_errno = 0;
1745
1746 VALIDATE_SW_IF_INDEX (mp);
1747
1748 error =
1749 (mp->enable == 1) ? enable_ip6_interface (vm,
1750 ntohl (mp->sw_if_index)) :
1751 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1752
1753 if (error)
1754 {
1755 clib_error_report (error);
1756 rv = VNET_API_ERROR_UNSPECIFIED;
1757 }
1758 else
1759 {
1760 rv = vnm->api_errno;
1761 }
1762
1763 BAD_SW_IF_INDEX_LABEL;
1764
1765 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1766}
1767
1768static void
1769 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1770 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1771{
1772 vlib_main_t *vm = vlib_get_main ();
1773 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1774 int rv = 0;
1775 clib_error_t *error;
1776 vnet_main_t *vnm = vnet_get_main ();
1777
1778 vnm->api_errno = 0;
1779
1780 VALIDATE_SW_IF_INDEX (mp);
1781
1782 error = set_ip6_link_local_address (vm,
1783 ntohl (mp->sw_if_index),
Neale Ranns75152282017-01-09 01:00:45 -08001784 (ip6_address_t *) mp->address);
Dave Barachb5e8a772016-12-06 12:04:42 -05001785 if (error)
1786 {
1787 clib_error_report (error);
1788 rv = VNET_API_ERROR_UNSPECIFIED;
1789 }
1790 else
1791 {
1792 rv = vnm->api_errno;
1793 }
1794
1795 BAD_SW_IF_INDEX_LABEL;
1796
1797 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1798}
1799
Neale Ranns32e1c012016-11-22 17:07:28 +00001800void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001801vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001802 u32 context, const mfib_signal_t * mfs)
1803{
1804 vl_api_mfib_signal_details_t *mp;
1805 mfib_prefix_t prefix;
1806 mfib_table_t *mfib;
1807 mfib_itf_t *mfi;
1808
1809 mp = vl_msg_api_alloc (sizeof (*mp));
1810
1811 memset (mp, 0, sizeof (*mp));
1812 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1813 mp->context = context;
1814
1815 mfi = mfib_itf_get (mfs->mfs_itf);
1816 mfib_entry_get_prefix (mfs->mfs_entry, &prefix);
1817 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
1818 prefix.fp_proto);
1819 mp->table_id = ntohl (mfib->mft_table_id);
1820 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1821
1822 if (FIB_PROTOCOL_IP4 == prefix.fp_proto)
1823 {
1824 mp->grp_address_len = ntohs (prefix.fp_len);
1825
1826 memcpy (mp->grp_address, &prefix.fp_grp_addr.ip4, 4);
1827 if (prefix.fp_len > 32)
1828 {
1829 memcpy (mp->src_address, &prefix.fp_src_addr.ip4, 4);
1830 }
1831 }
1832 else
1833 {
1834 mp->grp_address_len = ntohs (prefix.fp_len);
1835
1836 ASSERT (0);
1837 }
1838
1839 if (0 != mfs->mfs_buffer_len)
1840 {
1841 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1842
1843 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1844 }
1845 else
1846 {
1847 mp->ip_packet_len = 0;
1848 }
1849
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001850 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001851}
1852
1853static void
1854vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1855{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001856 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001857
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001858 reg = vl_api_client_index_to_registration (mp->client_index);
1859 if (!reg)
1860 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001861
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001862 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001863 ;
1864}
Dave Barachb5e8a772016-12-06 12:04:42 -05001865
Florin Coras595992c2017-11-06 17:17:08 -08001866static void
1867 vl_api_ip_container_proxy_add_del_t_handler
1868 (vl_api_ip_container_proxy_add_del_t * mp)
1869{
1870 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1871 vnet_ip_container_proxy_args_t args;
1872 int rv = 0;
1873 clib_error_t *error;
1874
1875 memset (&args, 0, sizeof (args));
1876 ip_set (&args.prefix.fp_addr, mp->ip, mp->is_ip4);
1877 args.prefix.fp_len = mp->plen ? mp->plen : (mp->is_ip4 ? 32 : 128);
1878 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1879 args.is_add = mp->is_add;
1880 if ((error = vnet_ip_container_proxy_add_del (&args)))
1881 {
1882 rv = clib_error_get_code (error);
1883 clib_error_report (error);
1884 }
1885
1886 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1887}
1888
Neale Rannsb8d44812017-11-10 06:53:54 -08001889static void
1890vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1891{
1892 int rv = 0;
1893 vl_api_ioam_enable_reply_t *rmp;
1894 clib_error_t *error;
1895
1896 /* Ignoring the profile id as currently a single profile
1897 * is supported */
1898 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1899 mp->seqno, mp->analyse);
1900 if (error)
1901 {
1902 clib_error_report (error);
1903 rv = clib_error_get_code (error);
1904 }
1905
1906 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1907}
1908
1909static void
1910vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1911{
1912 int rv = 0;
1913 vl_api_ioam_disable_reply_t *rmp;
1914 clib_error_t *error;
1915
1916 error = clear_ioam_rewrite_fn ();
1917 if (error)
1918 {
1919 clib_error_report (error);
1920 rv = clib_error_get_code (error);
1921 }
1922
1923 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1924}
1925
1926static void
1927 vl_api_ip_source_and_port_range_check_add_del_t_handler
1928 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1929{
1930 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1931 int rv = 0;
1932
1933 u8 is_ipv6 = mp->is_ipv6;
1934 u8 is_add = mp->is_add;
1935 u8 mask_length = mp->mask_length;
1936 ip4_address_t ip4_addr;
1937 ip6_address_t ip6_addr;
1938 u16 *low_ports = 0;
1939 u16 *high_ports = 0;
1940 u32 vrf_id;
1941 u16 tmp_low, tmp_high;
1942 u8 num_ranges;
1943 int i;
1944
1945 // Validate port range
1946 num_ranges = mp->number_of_ranges;
1947 if (num_ranges > 32)
1948 { // This is size of array in VPE.API
1949 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1950 goto reply;
1951 }
1952
1953 vec_reset_length (low_ports);
1954 vec_reset_length (high_ports);
1955
1956 for (i = 0; i < num_ranges; i++)
1957 {
1958 tmp_low = mp->low_ports[i];
1959 tmp_high = mp->high_ports[i];
1960 // If tmp_low <= tmp_high then only need to check tmp_low = 0
1961 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1962 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1963 {
1964 rv = VNET_API_ERROR_INVALID_VALUE;
1965 goto reply;
1966 }
1967 vec_add1 (low_ports, tmp_low);
1968 vec_add1 (high_ports, tmp_high + 1);
1969 }
1970
1971 // Validate mask_length
1972 if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
1973 {
1974 rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
1975 goto reply;
1976 }
1977
1978 vrf_id = ntohl (mp->vrf_id);
1979
1980 if (vrf_id < 1)
1981 {
1982 rv = VNET_API_ERROR_INVALID_VALUE;
1983 goto reply;
1984 }
1985
1986
1987 if (is_ipv6)
1988 {
1989 clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
1990 rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
1991 mask_length,
1992 vrf_id,
1993 low_ports,
1994 high_ports, is_add);
1995 }
1996 else
1997 {
1998 clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
1999 rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
2000 mask_length,
2001 vrf_id,
2002 low_ports,
2003 high_ports, is_add);
2004 }
2005
2006reply:
2007 vec_free (low_ports);
2008 vec_free (high_ports);
2009 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
2010}
2011
2012static void
2013 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
2014 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
2015{
2016 vlib_main_t *vm = vlib_get_main ();
2017 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
2018 ip4_main_t *im = &ip4_main;
2019 int rv;
2020 u32 sw_if_index;
2021 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2022 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2023 uword *p = 0;
2024 int i;
2025
2026 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
2027 ntohl (mp->tcp_out_vrf_id);
2028 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
2029 ntohl (mp->udp_out_vrf_id);
2030 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
2031 ntohl (mp->tcp_in_vrf_id);
2032 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
2033 ntohl (mp->udp_in_vrf_id);
2034
2035
2036 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
2037 {
2038 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
2039 {
2040 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
2041
2042 if (p == 0)
2043 {
2044 rv = VNET_API_ERROR_INVALID_VALUE;
2045 goto reply;
2046 }
2047
2048 fib_index[i] = p[0];
2049 }
2050 else
2051 fib_index[i] = ~0;
2052 }
2053 sw_if_index = ntohl (mp->sw_if_index);
2054
2055 VALIDATE_SW_IF_INDEX (mp);
2056
2057 rv =
2058 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
2059 mp->is_add);
2060
2061 BAD_SW_IF_INDEX_LABEL;
2062reply:
2063
2064 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
2065}
2066
2067#define IP4_ARP_EVENT 3
2068#define IP6_ND_EVENT 4
2069
2070static int arp_change_delete_callback (u32 pool_index, u8 * notused);
2071static int nd_change_delete_callback (u32 pool_index, u8 * notused);
2072static vlib_node_registration_t ip_resolver_process_node;
2073
2074static void
2075handle_ip4_arp_event (u32 pool_index)
2076{
2077 vpe_api_main_t *vam = &vpe_api_main;
2078 vnet_main_t *vnm = vam->vnet_main;
2079 vlib_main_t *vm = vam->vlib_main;
2080 vl_api_ip4_arp_event_t *event;
2081 vl_api_ip4_arp_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002082 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002083
2084 /* Client can cancel, die, etc. */
2085 if (pool_is_free_index (vam->arp_events, pool_index))
2086 return;
2087
2088 event = pool_elt_at_index (vam->arp_events, pool_index);
2089
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002090 reg = vl_api_client_index_to_registration (event->client_index);
2091 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002092 {
2093 (void) vnet_add_del_ip4_arp_change_event
2094 (vnm, arp_change_delete_callback,
2095 event->pid, &event->address,
2096 ip_resolver_process_node.index, IP4_ARP_EVENT,
2097 ~0 /* pool index, notused */ , 0 /* is_add */ );
2098 return;
2099 }
2100
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002101 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002102 {
2103 mp = vl_msg_api_alloc (sizeof (*mp));
2104 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002105 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002106 }
2107 else
2108 {
2109 static f64 last_time;
2110 /*
2111 * Throttle syslog msgs.
2112 * It's pretty tempting to just revoke the registration...
2113 */
2114 if (vlib_time_now (vm) > last_time + 10.0)
2115 {
2116 clib_warning ("arp event for %U to pid %d: queue stuffed!",
2117 format_ip4_address, &event->address, event->pid);
2118 last_time = vlib_time_now (vm);
2119 }
2120 }
2121}
2122
2123void
2124handle_ip6_nd_event (u32 pool_index)
2125{
2126 vpe_api_main_t *vam = &vpe_api_main;
2127 vnet_main_t *vnm = vam->vnet_main;
2128 vlib_main_t *vm = vam->vlib_main;
2129 vl_api_ip6_nd_event_t *event;
2130 vl_api_ip6_nd_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002131 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002132
2133 /* Client can cancel, die, etc. */
2134 if (pool_is_free_index (vam->nd_events, pool_index))
2135 return;
2136
2137 event = pool_elt_at_index (vam->nd_events, pool_index);
2138
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002139 reg = vl_api_client_index_to_registration (event->client_index);
2140 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002141 {
2142 (void) vnet_add_del_ip6_nd_change_event
2143 (vnm, nd_change_delete_callback,
2144 event->pid, &event->address,
2145 ip_resolver_process_node.index, IP6_ND_EVENT,
2146 ~0 /* pool index, notused */ , 0 /* is_add */ );
2147 return;
2148 }
2149
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002150 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002151 {
2152 mp = vl_msg_api_alloc (sizeof (*mp));
2153 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002154 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002155 }
2156 else
2157 {
2158 static f64 last_time;
2159 /*
2160 * Throttle syslog msgs.
2161 * It's pretty tempting to just revoke the registration...
2162 */
2163 if (vlib_time_now (vm) > last_time + 10.0)
2164 {
2165 clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
2166 format_ip6_address, &event->address, event->pid);
2167 last_time = vlib_time_now (vm);
2168 }
2169 }
2170}
2171
2172static uword
2173resolver_process (vlib_main_t * vm,
2174 vlib_node_runtime_t * rt, vlib_frame_t * f)
2175{
2176 volatile f64 timeout = 100.0;
2177 volatile uword *event_data = 0;
2178
2179 while (1)
2180 {
2181 vlib_process_wait_for_event_or_clock (vm, timeout);
2182
2183 uword event_type =
2184 vlib_process_get_events (vm, (uword **) & event_data);
2185
2186 int i;
2187 switch (event_type)
2188 {
2189 case IP4_ARP_EVENT:
2190 for (i = 0; i < vec_len (event_data); i++)
2191 handle_ip4_arp_event (event_data[i]);
2192 break;
2193
2194 case IP6_ND_EVENT:
2195 for (i = 0; i < vec_len (event_data); i++)
2196 handle_ip6_nd_event (event_data[i]);
2197 break;
2198
2199 case ~0: /* timeout */
2200 break;
2201 }
2202
2203 vec_reset_length (event_data);
2204 }
2205 return 0; /* or not */
2206}
2207
2208/* *INDENT-OFF* */
2209VLIB_REGISTER_NODE (ip_resolver_process_node,static) = {
2210 .function = resolver_process,
2211 .type = VLIB_NODE_TYPE_PROCESS,
2212 .name = "ip-route-resolver-process",
2213};
2214/* *INDENT-ON* */
2215
2216static int
2217nd_change_data_callback (u32 pool_index, u8 * new_mac,
2218 u32 sw_if_index, ip6_address_t * address)
2219{
2220 vpe_api_main_t *am = &vpe_api_main;
2221 vl_api_ip6_nd_event_t *event;
2222
2223 if (pool_is_free_index (am->nd_events, pool_index))
2224 return 1;
2225
2226 event = pool_elt_at_index (am->nd_events, pool_index);
2227 if (eth_mac_equal (event->new_mac, new_mac) &&
2228 sw_if_index == ntohl (event->sw_if_index))
2229 {
2230 return 1;
2231 }
2232
2233 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2234 event->sw_if_index = htonl (sw_if_index);
2235 return 0;
2236}
2237
2238static int
2239arp_change_delete_callback (u32 pool_index, u8 * notused)
2240{
2241 vpe_api_main_t *am = &vpe_api_main;
2242
2243 if (pool_is_free_index (am->arp_events, pool_index))
2244 return 1;
2245
2246 pool_put_index (am->arp_events, pool_index);
2247 return 0;
2248}
2249
2250static int
2251nd_change_delete_callback (u32 pool_index, u8 * notused)
2252{
2253 vpe_api_main_t *am = &vpe_api_main;
2254
2255 if (pool_is_free_index (am->nd_events, pool_index))
2256 return 1;
2257
2258 pool_put_index (am->nd_events, pool_index);
2259 return 0;
2260}
2261
2262static vlib_node_registration_t wc_arp_process_node;
2263
2264enum
2265{ WC_ARP_REPORT, WC_ND_REPORT };
2266
2267static uword
2268wc_arp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
2269{
2270 /* These cross the longjmp boundry (vlib_process_wait_for_event)
2271 * and need to be volatile - to prevent them from being optimized into
2272 * a register - which could change during suspension */
2273
2274 volatile wc_arp_report_t arp_prev = { 0 };
2275 volatile wc_nd_report_t nd_prev = { 0 };
2276 volatile f64 last_arp = vlib_time_now (vm);
2277 volatile f64 last_nd = vlib_time_now (vm);
2278
2279 while (1)
2280 {
2281 vlib_process_wait_for_event (vm);
2282 uword event_type = WC_ARP_REPORT;
2283 void *event_data = vlib_process_get_event_data (vm, &event_type);
2284
2285 f64 now = vlib_time_now (vm);
2286 int i;
2287 if (event_type == WC_ARP_REPORT)
2288 {
2289 wc_arp_report_t *arp_events = event_data;
2290 for (i = 0; i < vec_len (arp_events); i++)
2291 {
2292 /* discard dup event */
2293 if (arp_prev.ip4 == arp_events[i].ip4 &&
2294 eth_mac_equal ((u8 *) arp_prev.mac, arp_events[i].mac) &&
2295 arp_prev.sw_if_index == arp_events[i].sw_if_index &&
2296 (now - last_arp) < 10.0)
2297 {
2298 continue;
2299 }
2300 arp_prev = arp_events[i];
2301 last_arp = now;
2302 vpe_client_registration_t *reg;
2303 /* *INDENT-OFF* */
2304 pool_foreach(reg, vpe_api_main.wc_ip4_arp_events_registrations,
2305 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002306 vl_api_registration_t *vl_reg;
2307 vl_reg = vl_api_client_index_to_registration (reg->client_index);
2308 if (reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002309 {
2310 vl_api_ip4_arp_event_t * event = vl_msg_api_alloc (sizeof *event);
2311 memset (event, 0, sizeof *event);
2312 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2313 event->client_index = reg->client_index;
2314 event->pid = reg->client_pid;
2315 event->mac_ip = 1;
2316 event->address = arp_events[i].ip4;
2317 event->sw_if_index = htonl(arp_events[i].sw_if_index);
2318 memcpy(event->new_mac, arp_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002319 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002320 }
2321 }));
2322 /* *INDENT-ON* */
2323 }
2324 }
2325 else if (event_type == WC_ND_REPORT)
2326 {
2327 wc_nd_report_t *nd_events = event_data;
2328 for (i = 0; i < vec_len (nd_events); i++)
2329 {
2330 /* discard dup event */
2331 if (ip6_address_is_equal
2332 ((ip6_address_t *) & nd_prev.ip6, &nd_events[i].ip6)
2333 && eth_mac_equal ((u8 *) nd_prev.mac, nd_events[i].mac)
2334 && nd_prev.sw_if_index == nd_events[i].sw_if_index
2335 && (now - last_nd) < 10.0)
2336 {
2337 continue;
2338 }
2339 nd_prev = nd_events[i];
2340 last_nd = now;
2341 vpe_client_registration_t *reg;
2342 /* *INDENT-OFF* */
2343 pool_foreach(reg, vpe_api_main.wc_ip6_nd_events_registrations,
2344 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002345 vl_api_registration_t *vl_reg;
2346 vl_reg = vl_api_client_index_to_registration (reg->client_index);
2347 if (vl_reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002348 {
2349 vl_api_ip6_nd_event_t * event = vl_msg_api_alloc (sizeof *event);
2350 memset (event, 0, sizeof *event);
2351 event->_vl_msg_id = htons (VL_API_IP6_ND_EVENT);
2352 event->client_index = reg->client_index;
2353 event->pid = reg->client_pid;
2354 event->mac_ip = 1;
2355 memcpy(event->address, nd_events[i].ip6.as_u8, sizeof event->address);
2356 event->sw_if_index = htonl(nd_events[i].sw_if_index);
2357 memcpy(event->new_mac, nd_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002358 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002359 }
2360 }));
2361 /* *INDENT-ON* */
2362 }
2363 }
2364 vlib_process_put_event_data (vm, event_data);
2365 }
2366
2367 return 0;
2368}
2369
2370/* *INDENT-OFF* */
2371VLIB_REGISTER_NODE (wc_arp_process_node,static) = {
2372 .function = wc_arp_process,
2373 .type = VLIB_NODE_TYPE_PROCESS,
2374 .name = "wildcard-ip4-arp-publisher-process",
2375};
2376/* *INDENT-ON* */
2377
2378static int
2379arp_change_data_callback (u32 pool_index, u8 * new_mac,
2380 u32 sw_if_index, u32 address)
2381{
2382 vpe_api_main_t *am = &vpe_api_main;
2383 vl_api_ip4_arp_event_t *event;
2384
2385 if (pool_is_free_index (am->arp_events, pool_index))
2386 return 1;
2387
2388 event = pool_elt_at_index (am->arp_events, pool_index);
2389 if (eth_mac_equal (event->new_mac, new_mac) &&
2390 sw_if_index == ntohl (event->sw_if_index))
2391 {
2392 return 1;
2393 }
2394
2395 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2396 event->sw_if_index = htonl (sw_if_index);
2397 return 0;
2398}
2399
2400static void
2401vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
2402{
2403 vpe_api_main_t *am = &vpe_api_main;
2404 vnet_main_t *vnm = vnet_get_main ();
2405 vl_api_want_ip4_arp_events_reply_t *rmp;
2406 int rv = 0;
2407
2408 if (mp->address == 0)
2409 {
2410 uword *p =
2411 hash_get (am->wc_ip4_arp_events_registration_hash, mp->client_index);
2412 vpe_client_registration_t *rp;
2413 if (p)
2414 {
2415 if (mp->enable_disable)
2416 {
2417 clib_warning ("pid %d: already enabled...", mp->pid);
2418 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2419 goto reply;
2420 }
2421 else
2422 {
2423 rp =
2424 pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2425 pool_put (am->wc_ip4_arp_events_registrations, rp);
2426 hash_unset (am->wc_ip4_arp_events_registration_hash,
2427 mp->client_index);
2428 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
2429 wc_arp_set_publisher_node (~0, WC_ARP_REPORT);
2430 goto reply;
2431 }
2432 }
2433 if (mp->enable_disable == 0)
2434 {
2435 clib_warning ("pid %d: already disabled...", mp->pid);
2436 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2437 goto reply;
2438 }
2439 pool_get (am->wc_ip4_arp_events_registrations, rp);
2440 rp->client_index = mp->client_index;
2441 rp->client_pid = mp->pid;
2442 hash_set (am->wc_ip4_arp_events_registration_hash, rp->client_index,
2443 rp - am->wc_ip4_arp_events_registrations);
2444 wc_arp_set_publisher_node (wc_arp_process_node.index, WC_ARP_REPORT);
2445 goto reply;
2446 }
2447
2448 if (mp->enable_disable)
2449 {
2450 vl_api_ip4_arp_event_t *event;
2451 pool_get (am->arp_events, event);
2452 rv = vnet_add_del_ip4_arp_change_event
2453 (vnm, arp_change_data_callback,
2454 mp->pid, &mp->address /* addr, in net byte order */ ,
2455 ip_resolver_process_node.index,
2456 IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
2457
2458 if (rv)
2459 {
2460 pool_put (am->arp_events, event);
2461 goto reply;
2462 }
2463 memset (event, 0, sizeof (*event));
2464
2465 /* Python API expects events to have no context */
2466 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2467 event->client_index = mp->client_index;
2468 event->address = mp->address;
2469 event->pid = mp->pid;
2470 if (mp->address == 0)
2471 event->mac_ip = 1;
2472 }
2473 else
2474 {
2475 rv = vnet_add_del_ip4_arp_change_event
2476 (vnm, arp_change_delete_callback,
2477 mp->pid, &mp->address /* addr, in net byte order */ ,
2478 ip_resolver_process_node.index,
2479 IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2480 }
2481reply:
2482 REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
2483}
2484
2485static void
2486vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
2487{
2488 vpe_api_main_t *am = &vpe_api_main;
2489 vnet_main_t *vnm = vnet_get_main ();
2490 vl_api_want_ip6_nd_events_reply_t *rmp;
2491 int rv = 0;
2492
2493 if (ip6_address_is_zero ((ip6_address_t *) mp->address))
2494 {
2495 uword *p =
2496 hash_get (am->wc_ip6_nd_events_registration_hash, mp->client_index);
2497 vpe_client_registration_t *rp;
2498 if (p)
2499 {
2500 if (mp->enable_disable)
2501 {
2502 clib_warning ("pid %d: already enabled...", mp->pid);
2503 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2504 goto reply;
2505 }
2506 else
2507 {
2508 rp =
2509 pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2510 pool_put (am->wc_ip6_nd_events_registrations, rp);
2511 hash_unset (am->wc_ip6_nd_events_registration_hash,
2512 mp->client_index);
2513 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
2514 wc_nd_set_publisher_node (~0, 2);
2515 goto reply;
2516 }
2517 }
2518 if (mp->enable_disable == 0)
2519 {
2520 clib_warning ("pid %d: already disabled...", mp->pid);
2521 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2522 goto reply;
2523 }
2524 pool_get (am->wc_ip6_nd_events_registrations, rp);
2525 rp->client_index = mp->client_index;
2526 rp->client_pid = mp->pid;
2527 hash_set (am->wc_ip6_nd_events_registration_hash, rp->client_index,
2528 rp - am->wc_ip6_nd_events_registrations);
2529 wc_nd_set_publisher_node (wc_arp_process_node.index, WC_ND_REPORT);
2530 goto reply;
2531 }
2532
2533 if (mp->enable_disable)
2534 {
2535 vl_api_ip6_nd_event_t *event;
2536 pool_get (am->nd_events, event);
2537
2538 rv = vnet_add_del_ip6_nd_change_event
2539 (vnm, nd_change_data_callback,
2540 mp->pid, mp->address /* addr, in net byte order */ ,
2541 ip_resolver_process_node.index,
2542 IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
2543
2544 if (rv)
2545 {
2546 pool_put (am->nd_events, event);
2547 goto reply;
2548 }
2549 memset (event, 0, sizeof (*event));
2550
2551 event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
2552 event->client_index = mp->client_index;
2553 clib_memcpy (event->address, mp->address, sizeof event->address);
2554 event->pid = mp->pid;
2555 }
2556 else
2557 {
2558 rv = vnet_add_del_ip6_nd_change_event
2559 (vnm, nd_change_delete_callback,
2560 mp->pid, mp->address /* addr, in net byte order */ ,
2561 ip_resolver_process_node.index,
2562 IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2563 }
2564reply:
2565 REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
2566}
2567
2568static void
2569vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2570{
2571 vl_api_proxy_arp_add_del_reply_t *rmp;
2572 u32 fib_index;
2573 int rv;
2574 ip4_main_t *im = &ip4_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08002575 int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
2576 ip4_address_t * hi_addr,
2577 u32 fib_index, int is_del);
2578 uword *p;
2579
Ole Troan3288ed72017-12-06 17:00:05 +01002580 stats_dslock_with_hint (1 /* release hint */ , 6 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002581
2582 p = hash_get (im->fib_index_by_table_id, ntohl (mp->vrf_id));
2583
2584 if (!p)
2585 {
2586 rv = VNET_API_ERROR_NO_SUCH_FIB;
2587 goto out;
2588 }
2589
2590 fib_index = p[0];
2591
2592 rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->low_address,
2593 (ip4_address_t *) mp->hi_address,
2594 fib_index, mp->is_add == 0);
2595
2596out:
Ole Troan3288ed72017-12-06 17:00:05 +01002597 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002598 REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2599}
2600
2601static void
2602 vl_api_proxy_arp_intfc_enable_disable_t_handler
2603 (vl_api_proxy_arp_intfc_enable_disable_t * mp)
2604{
2605 int rv = 0;
2606 vnet_main_t *vnm = vnet_get_main ();
2607 vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
2608
2609 VALIDATE_SW_IF_INDEX (mp);
2610
2611 vnet_sw_interface_t *si =
2612 vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
2613
2614 ASSERT (si);
2615
2616 if (mp->enable_disable)
2617 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2618 else
2619 si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2620
2621 BAD_SW_IF_INDEX_LABEL;
2622
2623 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
2624}
2625
2626static int
2627ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2628{
2629 vnet_main_t *vnm = vnet_get_main ();
2630 vnet_interface_main_t *im = &vnm->interface_main;
2631 ip4_main_t *im4 = &ip4_main;
2632 static u32 *sw_if_indices_to_shut;
Neale Rannsb8d44812017-11-10 06:53:54 -08002633 fib_table_t *fib_table;
2634 ip4_fib_t *fib;
2635 u32 sw_if_index;
2636 int i;
2637 int rv = VNET_API_ERROR_NO_SUCH_FIB;
2638 u32 target_fib_id = ntohl (mp->vrf_id);
2639
Ole Troan3288ed72017-12-06 17:00:05 +01002640 stats_dslock_with_hint (1 /* release hint */ , 8 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002641
2642 /* *INDENT-OFF* */
2643 pool_foreach (fib_table, im4->fibs,
2644 ({
2645 vnet_sw_interface_t * si;
2646
2647 fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
2648
2649 if (fib->table_id != target_fib_id)
2650 continue;
2651
2652 /* remove any mpls encap/decap labels */
2653 mpls_fib_reset_labels (fib->table_id);
2654
2655 /* remove any proxy arps in this fib */
2656 vnet_proxy_arp_fib_reset (fib->table_id);
2657
2658 /* Set the flow hash for this fib to the default */
2659 vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2660
2661 vec_reset_length (sw_if_indices_to_shut);
2662
2663 /* Shut down interfaces in this FIB / clean out intfc routes */
2664 pool_foreach (si, im->sw_interfaces,
2665 ({
2666 u32 sw_if_index = si->sw_if_index;
2667
2668 if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
2669 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
2670 fib->index))
2671 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2672 }));
2673
2674 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2675 sw_if_index = sw_if_indices_to_shut[i];
2676
2677 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2678 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2679 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2680 }
2681
2682 fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
2683
2684 rv = 0;
2685 break;
2686 })); /* pool_foreach (fib) */
2687 /* *INDENT-ON* */
2688
Ole Troan3288ed72017-12-06 17:00:05 +01002689 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002690 return rv;
2691}
2692
2693static int
2694ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2695{
2696 vnet_main_t *vnm = vnet_get_main ();
2697 vnet_interface_main_t *im = &vnm->interface_main;
2698 ip6_main_t *im6 = &ip6_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08002699 static u32 *sw_if_indices_to_shut;
2700 fib_table_t *fib_table;
2701 ip6_fib_t *fib;
2702 u32 sw_if_index;
2703 int i;
2704 int rv = VNET_API_ERROR_NO_SUCH_FIB;
2705 u32 target_fib_id = ntohl (mp->vrf_id);
2706
Ole Troan3288ed72017-12-06 17:00:05 +01002707 stats_dslock_with_hint (1 /* release hint */ , 9 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002708
2709 /* *INDENT-OFF* */
2710 pool_foreach (fib_table, im6->fibs,
2711 ({
2712 vnet_sw_interface_t * si;
2713
2714 fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
2715
2716 if (fib->table_id != target_fib_id)
2717 continue;
2718
2719 vec_reset_length (sw_if_indices_to_shut);
2720
2721 /* Set the flow hash for this fib to the default */
2722 vnet_set_ip6_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2723
2724 /* Shut down interfaces in this FIB / clean out intfc routes */
2725 pool_foreach (si, im->sw_interfaces,
2726 ({
2727 if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
2728 fib->index)
2729 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2730 }));
2731
2732 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2733 sw_if_index = sw_if_indices_to_shut[i];
2734
2735 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2736 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2737 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2738 }
2739
2740 fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
2741
2742 rv = 0;
2743 break;
2744 })); /* pool_foreach (fib) */
2745 /* *INDENT-ON* */
2746
Ole Troan3288ed72017-12-06 17:00:05 +01002747 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002748 return rv;
2749}
2750
2751static void
2752vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2753{
2754 int rv;
2755 vl_api_reset_fib_reply_t *rmp;
2756
2757 if (mp->is_ipv6)
2758 rv = ip6_reset_fib_t_handler (mp);
2759 else
2760 rv = ip4_reset_fib_t_handler (mp);
2761
2762 REPLY_MACRO (VL_API_RESET_FIB_REPLY);
2763}
2764
2765static void
2766vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
2767{
2768 int rv;
2769 vl_api_set_arp_neighbor_limit_reply_t *rmp;
2770 vnet_main_t *vnm = vnet_get_main ();
2771 clib_error_t *error;
2772
2773 vnm->api_errno = 0;
2774
2775 if (mp->is_ipv6)
2776 error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
2777 else
2778 error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
2779
2780 if (error)
2781 {
2782 clib_error_report (error);
2783 rv = VNET_API_ERROR_UNSPECIFIED;
2784 }
2785 else
2786 {
2787 rv = vnm->api_errno;
2788 }
2789
2790 REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
2791}
2792
Klement Sekera75e7d132017-09-20 08:26:30 +02002793void
2794vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
2795{
2796 vl_api_ip_reassembly_set_reply_t *rmp;
2797 int rv = 0;
2798 if (mp->is_ip6)
2799 {
2800 rv = ip6_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
2801 clib_net_to_host_u32 (mp->max_reassemblies),
2802 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
2803 }
2804 else
2805 {
2806 rv = ip4_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
2807 clib_net_to_host_u32 (mp->max_reassemblies),
2808 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
2809 }
2810
2811 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
2812}
2813
2814void
2815vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
2816{
2817 unix_shared_memory_queue_t *q;
2818
2819 q = vl_api_client_index_to_input_queue (mp->client_index);
2820
2821 if (q == 0)
2822 return;
2823
2824 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
2825 memset (rmp, 0, sizeof (*rmp));
2826 rmp->_vl_msg_id = ntohs (VL_API_IP_REASSEMBLY_GET_REPLY);
2827 rmp->context = mp->context;
2828 rmp->retval = 0;
2829 if (mp->is_ip6)
2830 {
2831 rmp->is_ip6 = 1;
2832 ip6_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
2833 &rmp->expire_walk_interval_ms);
2834 }
2835 else
2836 {
2837 rmp->is_ip6 = 0;
2838 ip4_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
2839 &rmp->expire_walk_interval_ms);
2840 }
2841 rmp->timeout_ms = clib_host_to_net_u32 (rmp->timeout_ms);
2842 rmp->max_reassemblies = clib_host_to_net_u32 (rmp->max_reassemblies);
2843 rmp->expire_walk_interval_ms =
2844 clib_host_to_net_u32 (rmp->expire_walk_interval_ms);
2845 vl_msg_api_send_shmem (q, (u8 *) & rmp);
2846}
2847
Dave Barachb5e8a772016-12-06 12:04:42 -05002848#define vl_msg_name_crc_list
2849#include <vnet/ip/ip.api.h>
2850#undef vl_msg_name_crc_list
2851
2852static void
2853setup_message_id_table (api_main_t * am)
2854{
2855#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
2856 foreach_vl_msg_name_crc_ip;
2857#undef _
2858}
2859
2860static clib_error_t *
2861ip_api_hookup (vlib_main_t * vm)
2862{
2863 api_main_t *am = &api_main;
2864
2865#define _(N,n) \
2866 vl_msg_api_set_handlers(VL_API_##N, #n, \
2867 vl_api_##n##_t_handler, \
2868 vl_noop_handler, \
2869 vl_api_##n##_t_endian, \
2870 vl_api_##n##_t_print, \
2871 sizeof(vl_api_##n##_t), 1);
2872 foreach_ip_api_msg;
2873#undef _
2874
2875 /*
2876 * Set up the (msg_name, crc, message-id) table
2877 */
2878 setup_message_id_table (am);
2879
2880 return 0;
2881}
2882
2883VLIB_API_INIT_FUNCTION (ip_api_hookup);
2884
2885/*
2886 * fd.io coding-style-patch-verification: ON
2887 *
2888 * Local Variables:
2889 * eval: (c-set-style "gnu")
2890 * End:
2891 */