blob: 0ec69e48e54386b639234362c590dfe3b359b602 [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>
Dave Barachb5e8a772016-12-06 12:04:42 -050045
46#include <vnet/vnet_msg_enum.h>
47
48#define vl_typedefs /* define message structures */
49#include <vnet/vnet_all_api_h.h>
50#undef vl_typedefs
51
52#define vl_endianfun /* define message structures */
53#include <vnet/vnet_all_api_h.h>
54#undef vl_endianfun
55
56/* instantiate all the print functions we know about */
57#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
58#define vl_printfun
59#include <vnet/vnet_all_api_h.h>
60#undef vl_printfun
61
62#include <vlibapi/api_helper_macros.h>
63
Neale Ranns5a8123b2017-01-26 01:18:23 -080064
Dave Barachb5e8a772016-12-06 12:04:42 -050065#define foreach_ip_api_msg \
66_(IP_FIB_DUMP, ip_fib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050067_(IP6_FIB_DUMP, ip6_fib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080068_(IP_MFIB_DUMP, ip_mfib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080069_(IP6_MFIB_DUMP, ip6_mfib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050070_(IP_NEIGHBOR_DUMP, ip_neighbor_dump) \
Neale Ranns32e1c012016-11-22 17:07:28 +000071_(IP_MROUTE_ADD_DEL, ip_mroute_add_del) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080072_(MFIB_SIGNAL_DUMP, mfib_signal_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050073_(IP_ADDRESS_DUMP, ip_address_dump) \
74_(IP_DUMP, ip_dump) \
75_(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del) \
Neale Rannsb8d44812017-11-10 06:53:54 -080076_(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit) \
77_(WANT_IP4_ARP_EVENTS, want_ip4_arp_events) \
78_(WANT_IP6_ND_EVENTS, want_ip6_nd_events) \
79_(PROXY_ARP_ADD_DEL, proxy_arp_add_del) \
80_(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable) \
81_(RESET_FIB, reset_fib) \
Dave Barachb5e8a772016-12-06 12:04:42 -050082_(IP_ADD_DEL_ROUTE, ip_add_del_route) \
Neale Ranns28ab9cc2017-08-14 07:18:42 -070083_(IP_TABLE_ADD_DEL, ip_table_add_del) \
Neale Rannsd91c1db2017-07-31 02:30:50 -070084_(IP_PUNT_POLICE, ip_punt_police) \
85_(IP_PUNT_REDIRECT, ip_punt_redirect) \
Dave Barachb5e8a772016-12-06 12:04:42 -050086_(SET_IP_FLOW_HASH,set_ip_flow_hash) \
87_(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config) \
88_(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix) \
Neale Ranns3f844d02017-02-18 00:03:54 -080089_(IP6ND_PROXY_ADD_DEL, ip6nd_proxy_add_del) \
90_(IP6ND_PROXY_DUMP, ip6nd_proxy_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050091_(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable ) \
92_(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS, \
Florin Coras595992c2017-11-06 17:17:08 -080093 sw_interface_ip6_set_link_local_address) \
Neale Rannsb8d44812017-11-10 06:53:54 -080094_(IP_CONTAINER_PROXY_ADD_DEL, ip_container_proxy_add_del) \
95_(IOAM_ENABLE, ioam_enable) \
96_(IOAM_DISABLE, ioam_disable) \
97_(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL, \
98 ip_source_and_port_range_check_add_del) \
99_(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL, \
100 ip_source_and_port_range_check_interface_add_del)
Dave Barachb5e8a772016-12-06 12:04:42 -0500101
102extern void stats_dslock_with_hint (int hint, int tag);
103extern void stats_dsunlock (void);
104
105static void
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600106send_ip_neighbor_details (u32 sw_if_index,
107 u8 is_ipv6,
Dave Barachb5e8a772016-12-06 12:04:42 -0500108 u8 is_static,
109 u8 * mac_address,
Florin Coras6c4dae22018-01-09 06:39:23 -0800110 u8 * ip_address, vl_api_registration_t * reg,
111 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500112{
113 vl_api_ip_neighbor_details_t *mp;
114
115 mp = vl_msg_api_alloc (sizeof (*mp));
116 memset (mp, 0, sizeof (*mp));
117 mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS);
118 mp->context = context;
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600119 mp->sw_if_index = htonl (sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500120 mp->is_ipv6 = is_ipv6;
121 mp->is_static = is_static;
122 memcpy (mp->mac_address, mac_address, 6);
123 memcpy (mp->ip_address, ip_address, (is_ipv6) ? 16 : 4);
124
Florin Coras6c4dae22018-01-09 06:39:23 -0800125 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500126}
127
128static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500129vl_api_ip_neighbor_dump_t_handler (vl_api_ip_neighbor_dump_t * mp)
130{
Florin Coras6c4dae22018-01-09 06:39:23 -0800131 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500132
Florin Coras6c4dae22018-01-09 06:39:23 -0800133 reg = vl_api_client_index_to_registration (mp->client_index);
134 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500135 return;
136
137 u32 sw_if_index = ntohl (mp->sw_if_index);
138
139 if (mp->is_ipv6)
140 {
141 ip6_neighbor_t *n, *ns;
142
143 ns = ip6_neighbors_entries (sw_if_index);
144 /* *INDENT-OFF* */
145 vec_foreach (n, ns)
146 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500147 send_ip_neighbor_details
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600148 (sw_if_index, mp->is_ipv6,
149 ((n->flags & IP6_NEIGHBOR_FLAG_STATIC) ? 1 : 0),
Dave Barachd7cb1b52016-12-09 09:52:16 -0500150 (u8 *) n->link_layer_address,
151 (u8 *) & (n->key.ip6_address.as_u8),
Florin Coras6c4dae22018-01-09 06:39:23 -0800152 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500153 }
154 /* *INDENT-ON* */
155 vec_free (ns);
156 }
157 else
158 {
159 ethernet_arp_ip4_entry_t *n, *ns;
160
161 ns = ip4_neighbor_entries (sw_if_index);
162 /* *INDENT-OFF* */
163 vec_foreach (n, ns)
164 {
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600165 send_ip_neighbor_details (sw_if_index, mp->is_ipv6,
Dave Barachb5e8a772016-12-06 12:04:42 -0500166 ((n->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC) ? 1 : 0),
167 (u8*) n->ethernet_address,
168 (u8*) & (n->ip4_address.as_u8),
Florin Coras6c4dae22018-01-09 06:39:23 -0800169 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500170 }
171 /* *INDENT-ON* */
172 vec_free (ns);
173 }
174}
175
176
177void
178copy_fib_next_hop (fib_route_path_encode_t * api_rpath, void *fp_arg)
179{
180 int is_ip4;
181 vl_api_fib_path_t *fp = (vl_api_fib_path_t *) fp_arg;
182
Neale Rannsda78f952017-05-24 09:15:43 -0700183 if (api_rpath->rpath.frp_proto == DPO_PROTO_IP4)
Dave Barachb5e8a772016-12-06 12:04:42 -0500184 fp->afi = IP46_TYPE_IP4;
Neale Rannsda78f952017-05-24 09:15:43 -0700185 else if (api_rpath->rpath.frp_proto == DPO_PROTO_IP6)
Dave Barachb5e8a772016-12-06 12:04:42 -0500186 fp->afi = IP46_TYPE_IP6;
187 else
188 {
189 is_ip4 = ip46_address_is_ip4 (&api_rpath->rpath.frp_addr);
190 if (is_ip4)
191 fp->afi = IP46_TYPE_IP4;
192 else
193 fp->afi = IP46_TYPE_IP6;
194 }
195 if (fp->afi == IP46_TYPE_IP4)
196 memcpy (fp->next_hop, &api_rpath->rpath.frp_addr.ip4,
197 sizeof (api_rpath->rpath.frp_addr.ip4));
198 else
199 memcpy (fp->next_hop, &api_rpath->rpath.frp_addr.ip6,
200 sizeof (api_rpath->rpath.frp_addr.ip6));
201}
202
203static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500204send_ip_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800205 vl_api_registration_t * reg,
Neale Ranns2297af02017-09-12 09:45:04 -0700206 const fib_table_t * table,
207 const fib_prefix_t * pfx,
Dave Barachb5e8a772016-12-06 12:04:42 -0500208 fib_route_path_encode_t * api_rpaths, u32 context)
209{
210 vl_api_ip_fib_details_t *mp;
211 fib_route_path_encode_t *api_rpath;
212 vl_api_fib_path_t *fp;
213 int path_count;
214
215 path_count = vec_len (api_rpaths);
216 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
217 if (!mp)
218 return;
219 memset (mp, 0, sizeof (*mp));
220 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
221 mp->context = context;
222
Neale Ranns2297af02017-09-12 09:45:04 -0700223 mp->table_id = htonl (table->ft_table_id);
224 memcpy (mp->table_name, table->ft_desc,
225 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500226 mp->address_length = pfx->fp_len;
227 memcpy (mp->address, &pfx->fp_addr.ip4, sizeof (pfx->fp_addr.ip4));
228
229 mp->count = htonl (path_count);
230 fp = mp->path;
231 vec_foreach (api_rpath, api_rpaths)
232 {
233 memset (fp, 0, sizeof (*fp));
234 switch (api_rpath->dpo.dpoi_type)
235 {
236 case DPO_RECEIVE:
237 fp->is_local = true;
238 break;
239 case DPO_DROP:
240 fp->is_drop = true;
241 break;
242 case DPO_IP_NULL:
243 switch (api_rpath->dpo.dpoi_index)
244 {
245 case IP_NULL_ACTION_NONE:
246 fp->is_drop = true;
247 break;
248 case IP_NULL_ACTION_SEND_ICMP_UNREACH:
249 fp->is_unreach = true;
250 break;
251 case IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
252 fp->is_prohibit = true;
253 break;
254 default:
255 break;
256 }
257 break;
258 default:
259 break;
260 }
Neale Rannsa0a908f2017-08-01 11:40:03 -0700261 fp->weight = api_rpath->rpath.frp_weight;
262 fp->preference = api_rpath->rpath.frp_preference;
Dave Barachb5e8a772016-12-06 12:04:42 -0500263 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
264 copy_fib_next_hop (api_rpath, fp);
265 fp++;
266 }
267
Florin Coras6c4dae22018-01-09 06:39:23 -0800268 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500269}
270
Neale Rannsa3af3372017-03-28 03:49:52 -0700271typedef struct vl_api_ip_fib_dump_walk_ctx_t_
272{
273 fib_node_index_t *feis;
274} vl_api_ip_fib_dump_walk_ctx_t;
275
Neale Ranns89541992017-04-06 04:41:02 -0700276static fib_table_walk_rc_t
Neale Rannsa3af3372017-03-28 03:49:52 -0700277vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
278{
279 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
280
281 vec_add1 (ctx->feis, fei);
282
Neale Ranns89541992017-04-06 04:41:02 -0700283 return (FIB_TABLE_WALK_CONTINUE);
Neale Rannsa3af3372017-03-28 03:49:52 -0700284}
285
Dave Barachb5e8a772016-12-06 12:04:42 -0500286static void
287vl_api_ip_fib_dump_t_handler (vl_api_ip_fib_dump_t * mp)
288{
289 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800290 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500291 ip4_main_t *im = &ip4_main;
292 fib_table_t *fib_table;
Neale Rannsa3af3372017-03-28 03:49:52 -0700293 fib_node_index_t *lfeip;
Dave Barachb5e8a772016-12-06 12:04:42 -0500294 fib_prefix_t pfx;
295 u32 fib_index;
296 fib_route_path_encode_t *api_rpaths;
Neale Rannsa3af3372017-03-28 03:49:52 -0700297 vl_api_ip_fib_dump_walk_ctx_t ctx = {
298 .feis = NULL,
299 };
Dave Barachb5e8a772016-12-06 12:04:42 -0500300
Florin Coras6c4dae22018-01-09 06:39:23 -0800301 reg = vl_api_client_index_to_registration (mp->client_index);
302 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500303 return;
304
305 /* *INDENT-OFF* */
306 pool_foreach (fib_table, im->fibs,
307 ({
Neale Rannsa3af3372017-03-28 03:49:52 -0700308 fib_table_walk(fib_table->ft_index,
309 FIB_PROTOCOL_IP4,
310 vl_api_ip_fib_dump_walk,
311 &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500312 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500313 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500314
Neale Rannsa3af3372017-03-28 03:49:52 -0700315 vec_sort_with_function (ctx.feis, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500316
Neale Rannsa3af3372017-03-28 03:49:52 -0700317 vec_foreach (lfeip, ctx.feis)
Dave Barachb5e8a772016-12-06 12:04:42 -0500318 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500319 fib_entry_get_prefix (*lfeip, &pfx);
320 fib_index = fib_entry_get_fib_index (*lfeip);
321 fib_table = fib_table_get (fib_index, pfx.fp_proto);
Dave Barachb5e8a772016-12-06 12:04:42 -0500322 api_rpaths = NULL;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500323 fib_entry_encode (*lfeip, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800324 send_ip_fib_details (am, reg, fib_table, &pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500325 vec_free (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500326 }
327
Neale Rannsa3af3372017-03-28 03:49:52 -0700328 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500329}
330
331static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500332send_ip6_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800333 vl_api_registration_t * reg,
Neale Rannsa161a6d2017-11-14 08:10:41 -0800334 const fib_table_t * table,
335 const fib_prefix_t * pfx,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500336 fib_route_path_encode_t * api_rpaths, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500337{
338 vl_api_ip6_fib_details_t *mp;
339 fib_route_path_encode_t *api_rpath;
340 vl_api_fib_path_t *fp;
341 int path_count;
342
Dave Barachd7cb1b52016-12-09 09:52:16 -0500343 path_count = vec_len (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500344 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
345 if (!mp)
346 return;
347 memset (mp, 0, sizeof (*mp));
348 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
349 mp->context = context;
350
Neale Rannsa161a6d2017-11-14 08:10:41 -0800351 mp->table_id = htonl (table->ft_table_id);
Dave Barachb5e8a772016-12-06 12:04:42 -0500352 mp->address_length = pfx->fp_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500353 memcpy (mp->address, &pfx->fp_addr.ip6, sizeof (pfx->fp_addr.ip6));
Neale Rannsa161a6d2017-11-14 08:10:41 -0800354 memcpy (mp->table_name, table->ft_desc,
355 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500356
357 mp->count = htonl (path_count);
358 fp = mp->path;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500359 vec_foreach (api_rpath, api_rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500360 {
361 memset (fp, 0, sizeof (*fp));
362 switch (api_rpath->dpo.dpoi_type)
363 {
364 case DPO_RECEIVE:
365 fp->is_local = true;
366 break;
367 case DPO_DROP:
368 fp->is_drop = true;
369 break;
370 case DPO_IP_NULL:
371 switch (api_rpath->dpo.dpoi_index)
372 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500373 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_NONE:
Dave Barachb5e8a772016-12-06 12:04:42 -0500374 fp->is_drop = true;
375 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500376 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_SEND_ICMP_UNREACH:
Dave Barachb5e8a772016-12-06 12:04:42 -0500377 fp->is_unreach = true;
378 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500379 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
Dave Barachb5e8a772016-12-06 12:04:42 -0500380 fp->is_prohibit = true;
381 break;
382 default:
383 break;
384 }
385 break;
386 default:
387 break;
388 }
Neale Rannsa0a908f2017-08-01 11:40:03 -0700389 fp->weight = api_rpath->rpath.frp_weight;
390 fp->preference = api_rpath->rpath.frp_preference;
Dave Barach00916112017-10-18 10:54:12 -0400391 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500392 copy_fib_next_hop (api_rpath, fp);
393 fp++;
394 }
395
Florin Coras6c4dae22018-01-09 06:39:23 -0800396 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500397}
398
Dave Barachd7cb1b52016-12-09 09:52:16 -0500399typedef struct apt_ip6_fib_show_ctx_t_
400{
401 u32 fib_index;
402 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500403} api_ip6_fib_show_ctx_t;
404
405static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500406api_ip6_fib_table_put_entries (clib_bihash_kv_24_8_t * kvp, void *arg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500407{
408 api_ip6_fib_show_ctx_t *ctx = arg;
409
410 if ((kvp->key[2] >> 32) == ctx->fib_index)
411 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500412 vec_add1 (ctx->entries, kvp->value);
Dave Barachb5e8a772016-12-06 12:04:42 -0500413 }
414}
415
416static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800417api_ip6_fib_table_get_all (vl_api_registration_t * reg,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500418 vl_api_ip6_fib_dump_t * mp,
419 fib_table_t * fib_table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500420{
421 vpe_api_main_t *am = &vpe_api_main;
422 ip6_main_t *im6 = &ip6_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500423 fib_node_index_t *fib_entry_index;
424 api_ip6_fib_show_ctx_t ctx = {
Neale Rannsa3af3372017-03-28 03:49:52 -0700425 .fib_index = fib_table->ft_index,
426 .entries = NULL,
Dave Barachb5e8a772016-12-06 12:04:42 -0500427 };
428 fib_route_path_encode_t *api_rpaths;
429 fib_prefix_t pfx;
430
Dave Barachd7cb1b52016-12-09 09:52:16 -0500431 BV (clib_bihash_foreach_key_value_pair)
432 ((BVT (clib_bihash) *) & im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].
433 ip6_hash, api_ip6_fib_table_put_entries, &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500434
Dave Barachd7cb1b52016-12-09 09:52:16 -0500435 vec_sort_with_function (ctx.entries, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500436
Dave Barachd7cb1b52016-12-09 09:52:16 -0500437 vec_foreach (fib_entry_index, ctx.entries)
438 {
439 fib_entry_get_prefix (*fib_entry_index, &pfx);
440 api_rpaths = NULL;
441 fib_entry_encode (*fib_entry_index, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800442 send_ip6_fib_details (am, reg, fib_table, &pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500443 vec_free (api_rpaths);
444 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500445
Dave Barachd7cb1b52016-12-09 09:52:16 -0500446 vec_free (ctx.entries);
Dave Barachb5e8a772016-12-06 12:04:42 -0500447}
448
449static void
450vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
451{
Florin Coras6c4dae22018-01-09 06:39:23 -0800452 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500453 ip6_main_t *im6 = &ip6_main;
454 fib_table_t *fib_table;
455
Florin Coras6c4dae22018-01-09 06:39:23 -0800456 reg = vl_api_client_index_to_registration (mp->client_index);
457 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500458 return;
459
460 /* *INDENT-OFF* */
461 pool_foreach (fib_table, im6->fibs,
462 ({
Florin Coras6c4dae22018-01-09 06:39:23 -0800463 api_ip6_fib_table_get_all(reg, mp, fib_table);
Dave Barachb5e8a772016-12-06 12:04:42 -0500464 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500465 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500466}
467
468static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800469send_ip_mfib_details (vl_api_registration_t * reg,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800470 u32 context, u32 table_id, fib_node_index_t mfei)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800471{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800472 fib_route_path_encode_t *api_rpath, *api_rpaths = NULL;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800473 vl_api_ip_mfib_details_t *mp;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800474 mfib_entry_t *mfib_entry;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800475 vl_api_fib_path_t *fp;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800476 mfib_prefix_t pfx;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800477 int path_count;
478
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800479 mfib_entry = mfib_entry_get (mfei);
480 mfib_entry_get_prefix (mfei, &pfx);
481 mfib_entry_encode (mfei, &api_rpaths);
482
Neale Ranns5a8123b2017-01-26 01:18:23 -0800483 path_count = vec_len (api_rpaths);
484 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
485 if (!mp)
486 return;
487 memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700488 mp->_vl_msg_id = ntohs (VL_API_IP_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800489 mp->context = context;
490
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800491 mp->rpf_id = mfib_entry->mfe_rpf_id;
492 mp->entry_flags = mfib_entry->mfe_flags;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800493 mp->table_id = htonl (table_id);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800494 mp->address_length = pfx.fp_len;
495 memcpy (mp->grp_address, &pfx.fp_grp_addr.ip4,
496 sizeof (pfx.fp_grp_addr.ip4));
497 memcpy (mp->src_address, &pfx.fp_src_addr.ip4,
498 sizeof (pfx.fp_src_addr.ip4));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800499
500 mp->count = htonl (path_count);
501 fp = mp->path;
502 vec_foreach (api_rpath, api_rpaths)
503 {
504 memset (fp, 0, sizeof (*fp));
505
506 fp->weight = 0;
507 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
508 copy_fib_next_hop (api_rpath, fp);
509 fp++;
510 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800511 vec_free (api_rpaths);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800512
Florin Coras6c4dae22018-01-09 06:39:23 -0800513 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800514}
515
516typedef struct vl_api_ip_mfib_dump_ctc_t_
517{
518 fib_node_index_t *entries;
519} vl_api_ip_mfib_dump_ctc_t;
520
521static int
522vl_api_ip_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
523{
524 vl_api_ip_mfib_dump_ctc_t *ctx = arg;
525
526 vec_add1 (ctx->entries, fei);
527
528 return (0);
529}
530
531static void
532vl_api_ip_mfib_dump_t_handler (vl_api_ip_mfib_dump_t * mp)
533{
Florin Coras6c4dae22018-01-09 06:39:23 -0800534 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800535 ip4_main_t *im = &ip4_main;
536 mfib_table_t *mfib_table;
537 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800538 vl_api_ip_mfib_dump_ctc_t ctx = {
539 .entries = NULL,
540 };
541
Florin Coras6c4dae22018-01-09 06:39:23 -0800542 reg = vl_api_client_index_to_registration (mp->client_index);
543 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800544 return;
545
Neale Ranns5a8123b2017-01-26 01:18:23 -0800546 /* *INDENT-OFF* */
547 pool_foreach (mfib_table, im->mfibs,
548 ({
549 ip4_mfib_table_walk(&mfib_table->v4,
550 vl_api_ip_mfib_table_dump_walk,
551 &ctx);
552
553 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
554
555 vec_foreach (mfeip, ctx.entries)
556 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800557 send_ip_mfib_details (reg, mp->context,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800558 mfib_table->mft_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800559 *mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800560 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800561 vec_reset_length (ctx.entries);
562
563 }));
564 /* *INDENT-ON* */
565
566 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800567}
568
569static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800570send_ip6_mfib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800571 vl_api_registration_t * reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800572 u32 table_id,
573 mfib_prefix_t * pfx,
574 fib_route_path_encode_t * api_rpaths, u32 context)
575{
576 vl_api_ip6_mfib_details_t *mp;
577 fib_route_path_encode_t *api_rpath;
578 vl_api_fib_path_t *fp;
579 int path_count;
580
581 path_count = vec_len (api_rpaths);
582 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
583 if (!mp)
584 return;
585 memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700586 mp->_vl_msg_id = ntohs (VL_API_IP6_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800587 mp->context = context;
588
589 mp->table_id = htonl (table_id);
590 mp->address_length = pfx->fp_len;
591 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip6,
592 sizeof (pfx->fp_grp_addr.ip6));
593 memcpy (mp->src_address, &pfx->fp_src_addr.ip6,
594 sizeof (pfx->fp_src_addr.ip6));
595
596 mp->count = htonl (path_count);
597 fp = mp->path;
598 vec_foreach (api_rpath, api_rpaths)
599 {
600 memset (fp, 0, sizeof (*fp));
601
602 fp->weight = 0;
603 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
604 copy_fib_next_hop (api_rpath, fp);
605 fp++;
606 }
607
Florin Coras6c4dae22018-01-09 06:39:23 -0800608 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800609}
610
611typedef struct vl_api_ip6_mfib_dump_ctc_t_
612{
613 fib_node_index_t *entries;
614} vl_api_ip6_mfib_dump_ctc_t;
615
616static int
617vl_api_ip6_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
618{
619 vl_api_ip6_mfib_dump_ctc_t *ctx = arg;
620
621 vec_add1 (ctx->entries, fei);
622
623 return (0);
624}
625
626static void
627vl_api_ip6_mfib_dump_t_handler (vl_api_ip6_mfib_dump_t * mp)
628{
629 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800630 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800631 ip6_main_t *im = &ip6_main;
632 mfib_table_t *mfib_table;
633 fib_node_index_t *mfeip;
634 mfib_prefix_t pfx;
635 fib_route_path_encode_t *api_rpaths = NULL;
636 vl_api_ip6_mfib_dump_ctc_t ctx = {
637 .entries = NULL,
638 };
639
Florin Coras6c4dae22018-01-09 06:39:23 -0800640 reg = vl_api_client_index_to_registration (mp->client_index);
641 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800642 return;
643
644
645 /* *INDENT-OFF* */
646 pool_foreach (mfib_table, im->mfibs,
647 ({
648 ip6_mfib_table_walk(&mfib_table->v6,
649 vl_api_ip6_mfib_table_dump_walk,
650 &ctx);
651
652 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
653
654 vec_foreach(mfeip, ctx.entries)
655 {
656 mfib_entry_get_prefix (*mfeip, &pfx);
657 mfib_entry_encode (*mfeip, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800658 send_ip6_mfib_details (am, reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800659 mfib_table->mft_table_id,
660 &pfx, api_rpaths,
661 mp->context);
662 }
663 vec_reset_length (api_rpaths);
664 vec_reset_length (ctx.entries);
665
666 }));
667 /* *INDENT-ON* */
668
669 vec_free (ctx.entries);
670 vec_free (api_rpaths);
671}
672
673static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700674vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
675 vlib_main_t * vm)
676{
677 vl_api_ip_punt_police_reply_t *rmp;
678 int rv = 0;
679
680 if (mp->is_ip6)
681 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
682 else
683 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
684
685 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
686}
687
688static void
689vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
690 vlib_main_t * vm)
691{
692 vl_api_ip_punt_redirect_reply_t *rmp;
693 int rv = 0;
694
695 if (mp->is_add)
696 {
697 ip46_address_t nh;
698
699 memset (&nh, 0, sizeof (nh));
700
701 if (mp->is_ip6)
702 {
703 memcpy (&nh.ip6, mp->nh, sizeof (nh.ip6));
704
705 ip6_punt_redirect_add (ntohl (mp->rx_sw_if_index),
706 ntohl (mp->tx_sw_if_index), &nh);
707 }
708 else
709 {
710 memcpy (&nh.ip4, mp->nh, sizeof (nh.ip4));
711
712 ip4_punt_redirect_add (ntohl (mp->rx_sw_if_index),
713 ntohl (mp->tx_sw_if_index), &nh);
714 }
715 }
716 else
717 {
718 if (mp->is_ip6)
719 {
720 ip6_punt_redirect_del (ntohl (mp->rx_sw_if_index));
721 }
722 else
723 {
724 ip4_punt_redirect_del (ntohl (mp->rx_sw_if_index));
725 }
726 }
727
728 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
729}
730
731static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500732vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
733 vlib_main_t * vm)
734{
735 vl_api_ip_neighbor_add_del_reply_t *rmp;
736 vnet_main_t *vnm = vnet_get_main ();
737 int rv = 0;
738
739 VALIDATE_SW_IF_INDEX (mp);
740
741 stats_dslock_with_hint (1 /* release hint */ , 7 /* tag */ );
742
743 /*
744 * there's no validation here of the ND/ARP entry being added.
745 * The expectation is that the FIB will ensure that nothing bad
746 * will come of adding bogus entries.
747 */
748 if (mp->is_ipv6)
749 {
750 if (mp->is_add)
751 rv = vnet_set_ip6_ethernet_neighbor
752 (vm, ntohl (mp->sw_if_index),
753 (ip6_address_t *) (mp->dst_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800754 mp->mac_address, sizeof (mp->mac_address), mp->is_static,
755 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500756 else
757 rv = vnet_unset_ip6_ethernet_neighbor
758 (vm, ntohl (mp->sw_if_index),
759 (ip6_address_t *) (mp->dst_address),
760 mp->mac_address, sizeof (mp->mac_address));
761 }
762 else
763 {
764 ethernet_arp_ip4_over_ethernet_address_t a;
765
766 clib_memcpy (&a.ethernet, mp->mac_address, 6);
767 clib_memcpy (&a.ip4, mp->dst_address, 4);
768
769 if (mp->is_add)
770 rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800771 &a, mp->is_static,
772 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500773 else
774 rv =
775 vnet_arp_unset_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index), &a);
776 }
777
Dave Barachb5e8a772016-12-06 12:04:42 -0500778 stats_dsunlock ();
Neale Ranns8edad032017-10-09 05:26:13 -0700779
780 BAD_SW_IF_INDEX_LABEL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500781 REPLY_MACRO (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
782}
783
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700784void
Neale Ranns15002542017-09-10 04:39:11 -0700785ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
786{
787 u32 fib_index, mfib_index;
788
789 /*
790 * ignore action on the default table - this is always present
791 * and cannot be added nor deleted from the API
792 */
793 if (0 != table_id)
794 {
795 /*
796 * The API holds only one lock on the table.
797 * i.e. it can be added many times via the API but needs to be
798 * deleted only once.
799 * The FIB index for unicast and multicast is not necessarily the
800 * same, since internal VPP systesm (like LISP and SR) create
801 * their own unicast tables.
802 */
803 fib_index = fib_table_find (fproto, table_id);
804 mfib_index = mfib_table_find (fproto, table_id);
805
806 if (~0 != fib_index)
807 {
808 fib_table_unlock (fib_index, fproto,
809 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
810 }
811 if (~0 != mfib_index)
812 {
813 mfib_table_unlock (mfib_index, fproto,
814 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
815 }
816 }
817}
818
819void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700820vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
821{
822 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700823 fib_protocol_t fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
824 u32 table_id = ntohl (mp->table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700825 int rv = 0;
826
Neale Ranns15002542017-09-10 04:39:11 -0700827 if (mp->is_add)
828 {
Neale Ranns2297af02017-09-12 09:45:04 -0700829 ip_table_create (fproto, table_id, 1, mp->name);
Neale Ranns15002542017-09-10 04:39:11 -0700830 }
831 else
832 {
833 ip_table_delete (fproto, table_id, 1);
834 }
835
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700836 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
837}
838
Dave Barachb5e8a772016-12-06 12:04:42 -0500839int
840add_del_route_t_handler (u8 is_multipath,
841 u8 is_add,
842 u8 is_drop,
843 u8 is_unreach,
844 u8 is_prohibit,
845 u8 is_local,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800846 u8 is_multicast,
Dave Barachb5e8a772016-12-06 12:04:42 -0500847 u8 is_classify,
848 u32 classify_table_index,
849 u8 is_resolve_host,
850 u8 is_resolve_attached,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800851 u8 is_interface_rx,
852 u8 is_rpf_id,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800853 u8 is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -0700854 u8 is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800855 u8 is_udp_encap,
Dave Barachb5e8a772016-12-06 12:04:42 -0500856 u32 fib_index,
857 const fib_prefix_t * prefix,
Neale Rannsda78f952017-05-24 09:15:43 -0700858 dpo_proto_t next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500859 const ip46_address_t * next_hop,
Neale Ranns810086d2017-11-05 16:26:46 -0800860 u32 next_hop_id,
Dave Barachb5e8a772016-12-06 12:04:42 -0500861 u32 next_hop_sw_if_index,
862 u8 next_hop_fib_index,
Neale Ranns57b58602017-07-15 07:37:25 -0700863 u16 next_hop_weight,
864 u16 next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500865 mpls_label_t next_hop_via_label,
866 mpls_label_t * next_hop_out_label_stack)
867{
868 vnet_classify_main_t *cm = &vnet_classify_main;
869 fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
870 fib_route_path_t path = {
Neale Rannsda78f952017-05-24 09:15:43 -0700871 .frp_proto = next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500872 .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
873 .frp_sw_if_index = next_hop_sw_if_index,
874 .frp_fib_index = next_hop_fib_index,
875 .frp_weight = next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -0700876 .frp_preference = next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500877 .frp_label_stack = next_hop_out_label_stack,
878 };
879 fib_route_path_t *paths = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800880 fib_entry_flag_t entry_flags = FIB_ENTRY_FLAG_NONE;
Dave Barachb5e8a772016-12-06 12:04:42 -0500881
Neale Rannscaac3502017-09-18 18:04:10 -0700882 /*
883 * the special INVALID label meams we are not recursing via a
884 * label. Exp-null value is never a valid via-label so that
885 * also means it's not a via-label and means clients that set
886 * it to 0 by default get the expected behaviour
887 */
888 if ((MPLS_LABEL_INVALID != next_hop_via_label) && (0 != next_hop_via_label))
Dave Barachb5e8a772016-12-06 12:04:42 -0500889 {
Neale Rannsda78f952017-05-24 09:15:43 -0700890 path.frp_proto = DPO_PROTO_MPLS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500891 path.frp_local_label = next_hop_via_label;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800892 path.frp_eos = MPLS_NON_EOS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500893 }
Neale Rannsf068c3e2018-01-03 04:18:48 -0800894 if (is_dvr)
895 path_flags |= FIB_ROUTE_PATH_DVR;
Dave Barachb5e8a772016-12-06 12:04:42 -0500896 if (is_resolve_host)
897 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
898 if (is_resolve_attached)
899 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800900 if (is_interface_rx)
901 path_flags |= FIB_ROUTE_PATH_INTF_RX;
902 if (is_rpf_id)
903 path_flags |= FIB_ROUTE_PATH_RPF_ID;
Neale Ranns054c03a2017-10-13 05:15:07 -0700904 if (is_source_lookup)
905 path_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800906 if (is_multicast)
907 entry_flags |= FIB_ENTRY_FLAG_MULTICAST;
Neale Ranns810086d2017-11-05 16:26:46 -0800908 if (is_udp_encap)
909 {
910 path_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
911 path.frp_udp_encap_id = next_hop_id;
912 }
Florin Coras79ae2d32017-12-16 08:31:06 -0800913 if (path.frp_sw_if_index == ~0 && ip46_address_is_zero (&path.frp_addr)
914 && path.frp_fib_index != ~0)
915 {
916 path_flags |= FIB_ROUTE_PATH_DEAG;
917 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500918
919 path.frp_flags = path_flags;
920
921 if (is_multipath)
922 {
923 stats_dslock_with_hint (1 /* release hint */ , 10 /* tag */ );
924
925
926 vec_add1 (paths, path);
927
928 if (is_add)
929 fib_table_entry_path_add2 (fib_index,
930 prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800931 FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500932 else
933 fib_table_entry_path_remove2 (fib_index,
934 prefix, FIB_SOURCE_API, paths);
935
936 vec_free (paths);
937 stats_dsunlock ();
938 return 0;
939 }
940
941 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
942
943 if (is_drop || is_local || is_classify || is_unreach || is_prohibit)
944 {
945 /*
946 * special route types that link directly to the adj
947 */
948 if (is_add)
949 {
950 dpo_id_t dpo = DPO_INVALID;
951 dpo_proto_t dproto;
952
953 dproto = fib_proto_to_dpo (prefix->fp_proto);
954
955 if (is_drop)
956 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
957 else if (is_local)
958 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
959 else if (is_unreach)
960 ip_null_dpo_add_and_lock (dproto,
961 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
962 else if (is_prohibit)
963 ip_null_dpo_add_and_lock (dproto,
964 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
965 &dpo);
966 else if (is_classify)
967 {
968 if (pool_is_free_index (cm->tables,
969 ntohl (classify_table_index)))
970 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500971 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500972 return VNET_API_ERROR_NO_SUCH_TABLE;
973 }
974
975 dpo_set (&dpo, DPO_CLASSIFY, dproto,
976 classify_dpo_create (dproto,
977 ntohl (classify_table_index)));
978 }
979 else
980 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500981 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500982 return VNET_API_ERROR_NO_SUCH_TABLE;
983 }
984
985 fib_table_entry_special_dpo_update (fib_index,
986 prefix,
987 FIB_SOURCE_API,
988 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
989 dpo_reset (&dpo);
990 }
991 else
992 {
993 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
994 }
995 }
996 else
997 {
998 if (is_add)
999 {
1000 vec_add1 (paths, path);
1001 fib_table_entry_update (fib_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001002 prefix, FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -05001003 vec_free (paths);
1004 }
1005 else
1006 {
1007 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
1008 }
1009 }
1010
Dave Barachd7cb1b52016-12-09 09:52:16 -05001011 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -05001012 return (0);
1013}
1014
1015int
1016add_del_route_check (fib_protocol_t table_proto,
1017 u32 table_id,
1018 u32 next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001019 dpo_proto_t next_hop_table_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -05001020 u32 next_hop_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001021 u8 is_rpf_id, u32 * fib_index, u32 * next_hop_fib_index)
Dave Barachb5e8a772016-12-06 12:04:42 -05001022{
1023 vnet_main_t *vnm = vnet_get_main ();
1024
Florin Corasd0a59722017-10-15 17:41:21 +00001025 /* Temporaray whilst I do the CSIT dance */
1026 u8 create_missing_tables = 1;
1027
Dave Barachb5e8a772016-12-06 12:04:42 -05001028 *fib_index = fib_table_find (table_proto, ntohl (table_id));
1029 if (~0 == *fib_index)
1030 {
Florin Corasd0a59722017-10-15 17:41:21 +00001031 if (create_missing_tables)
1032 {
1033 *fib_index = fib_table_find_or_create_and_lock (table_proto,
1034 ntohl (table_id),
1035 FIB_SOURCE_API);
1036 }
1037 else
1038 {
1039 /* No such VRF, and we weren't asked to create one */
1040 return VNET_API_ERROR_NO_SUCH_FIB;
1041 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001042 }
1043
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001044 if (!is_rpf_id && ~0 != ntohl (next_hop_sw_if_index))
Dave Barachb5e8a772016-12-06 12:04:42 -05001045 {
1046 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1047 ntohl (next_hop_sw_if_index)))
1048 {
1049 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1050 }
1051 }
1052 else
1053 {
Neale Rannsda78f952017-05-24 09:15:43 -07001054 fib_protocol_t fib_nh_proto;
1055
1056 if (next_hop_table_proto > DPO_PROTO_MPLS)
1057 return (0);
1058
1059 fib_nh_proto = dpo_proto_to_fib (next_hop_table_proto);
1060
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001061 if (is_rpf_id)
Neale Rannsda78f952017-05-24 09:15:43 -07001062 *next_hop_fib_index = mfib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001063 ntohl (next_hop_table_id));
1064 else
Neale Rannsda78f952017-05-24 09:15:43 -07001065 *next_hop_fib_index = fib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001066 ntohl (next_hop_table_id));
Dave Barachb5e8a772016-12-06 12:04:42 -05001067
1068 if (~0 == *next_hop_fib_index)
1069 {
Florin Corasd0a59722017-10-15 17:41:21 +00001070 if (create_missing_tables)
1071 {
1072 if (is_rpf_id)
1073 *next_hop_fib_index =
1074 mfib_table_find_or_create_and_lock (fib_nh_proto,
1075 ntohl
1076 (next_hop_table_id),
1077 MFIB_SOURCE_API);
1078 else
1079 *next_hop_fib_index =
1080 fib_table_find_or_create_and_lock (fib_nh_proto,
1081 ntohl
1082 (next_hop_table_id),
1083 FIB_SOURCE_API);
1084 }
1085 else
1086 {
1087 /* No such VRF, and we weren't asked to create one */
1088 return VNET_API_ERROR_NO_SUCH_FIB;
1089 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001090 }
1091 }
1092
1093 return (0);
1094}
1095
1096static int
1097ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1098{
1099 u32 fib_index, next_hop_fib_index;
1100 mpls_label_t *label_stack = NULL;
1101 int rv, ii, n_labels;;
1102
1103 rv = add_del_route_check (FIB_PROTOCOL_IP4,
1104 mp->table_id,
1105 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001106 DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -05001107 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001108 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001109
1110 if (0 != rv)
1111 return (rv);
1112
1113 fib_prefix_t pfx = {
1114 .fp_len = mp->dst_address_length,
1115 .fp_proto = FIB_PROTOCOL_IP4,
1116 };
1117 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
1118
1119 ip46_address_t nh;
1120 memset (&nh, 0, sizeof (nh));
1121 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
1122
1123 n_labels = mp->next_hop_n_out_labels;
1124 if (n_labels == 0)
1125 ;
1126 else if (1 == n_labels)
1127 vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
1128 else
1129 {
1130 vec_validate (label_stack, n_labels - 1);
1131 for (ii = 0; ii < n_labels; ii++)
1132 label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
1133 }
1134
1135 return (add_del_route_t_handler (mp->is_multipath,
1136 mp->is_add,
1137 mp->is_drop,
1138 mp->is_unreach,
1139 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001140 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001141 mp->is_classify,
1142 mp->classify_table_index,
1143 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001144 mp->is_resolve_attached, 0, 0,
Neale Rannsf068c3e2018-01-03 04:18:48 -08001145 mp->is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -07001146 mp->is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -08001147 mp->is_udp_encap,
Neale Rannsda78f952017-05-24 09:15:43 -07001148 fib_index, &pfx, DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -05001149 &nh,
Neale Ranns810086d2017-11-05 16:26:46 -08001150 ntohl (mp->next_hop_id),
Dave Barachb5e8a772016-12-06 12:04:42 -05001151 ntohl (mp->next_hop_sw_if_index),
1152 next_hop_fib_index,
1153 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001154 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001155 ntohl (mp->next_hop_via_label),
1156 label_stack));
1157}
1158
1159static int
1160ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1161{
1162 u32 fib_index, next_hop_fib_index;
1163 mpls_label_t *label_stack = NULL;
1164 int rv, ii, n_labels;;
1165
1166 rv = add_del_route_check (FIB_PROTOCOL_IP6,
1167 mp->table_id,
1168 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001169 DPO_PROTO_IP6,
Dave Barachb5e8a772016-12-06 12:04:42 -05001170 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001171 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001172
1173 if (0 != rv)
1174 return (rv);
1175
1176 fib_prefix_t pfx = {
1177 .fp_len = mp->dst_address_length,
1178 .fp_proto = FIB_PROTOCOL_IP6,
1179 };
1180 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1181
1182 ip46_address_t nh;
1183 memset (&nh, 0, sizeof (nh));
1184 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1185
1186 n_labels = mp->next_hop_n_out_labels;
1187 if (n_labels == 0)
1188 ;
1189 else if (1 == n_labels)
1190 vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
1191 else
1192 {
1193 vec_validate (label_stack, n_labels - 1);
1194 for (ii = 0; ii < n_labels; ii++)
1195 label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
1196 }
1197
1198 return (add_del_route_t_handler (mp->is_multipath,
1199 mp->is_add,
1200 mp->is_drop,
1201 mp->is_unreach,
1202 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001203 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001204 mp->is_classify,
1205 mp->classify_table_index,
1206 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001207 mp->is_resolve_attached, 0, 0,
Neale Rannsf068c3e2018-01-03 04:18:48 -08001208 mp->is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -07001209 mp->is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -08001210 mp->is_udp_encap,
Neale Rannsda78f952017-05-24 09:15:43 -07001211 fib_index, &pfx, DPO_PROTO_IP6,
Neale Ranns810086d2017-11-05 16:26:46 -08001212 &nh, ntohl (mp->next_hop_id),
1213 ntohl (mp->next_hop_sw_if_index),
Dave Barachb5e8a772016-12-06 12:04:42 -05001214 next_hop_fib_index,
1215 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001216 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001217 ntohl (mp->next_hop_via_label),
1218 label_stack));
1219}
1220
1221void
1222vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1223{
1224 vl_api_ip_add_del_route_reply_t *rmp;
1225 int rv;
1226 vnet_main_t *vnm = vnet_get_main ();
1227
1228 vnm->api_errno = 0;
1229
1230 if (mp->is_ipv6)
1231 rv = ip6_add_del_route_t_handler (mp);
1232 else
1233 rv = ip4_add_del_route_t_handler (mp);
1234
1235 rv = (rv == 0) ? vnm->api_errno : rv;
1236
1237 REPLY_MACRO (VL_API_IP_ADD_DEL_ROUTE_REPLY);
1238}
1239
Neale Ranns15002542017-09-10 04:39:11 -07001240void
Neale Ranns2297af02017-09-12 09:45:04 -07001241ip_table_create (fib_protocol_t fproto,
1242 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -07001243{
1244 u32 fib_index, mfib_index;
1245
1246 /*
1247 * ignore action on the default table - this is always present
1248 * and cannot be added nor deleted from the API
1249 */
1250 if (0 != table_id)
1251 {
1252 /*
1253 * The API holds only one lock on the table.
1254 * i.e. it can be added many times via the API but needs to be
1255 * deleted only once.
1256 * The FIB index for unicast and multicast is not necessarily the
1257 * same, since internal VPP systesm (like LISP and SR) create
1258 * their own unicast tables.
1259 */
1260 fib_index = fib_table_find (fproto, table_id);
1261 mfib_index = mfib_table_find (fproto, table_id);
1262
1263 if (~0 == fib_index)
1264 {
Neale Ranns2297af02017-09-12 09:45:04 -07001265 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
1266 (is_api ?
1267 FIB_SOURCE_API :
1268 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001269 }
1270 if (~0 == mfib_index)
1271 {
Neale Ranns2297af02017-09-12 09:45:04 -07001272 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
1273 (is_api ?
1274 MFIB_SOURCE_API :
1275 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001276 }
1277 }
1278}
1279
Neale Ranns32e1c012016-11-22 17:07:28 +00001280static int
1281add_del_mroute_check (fib_protocol_t table_proto,
1282 u32 table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001283 u32 next_hop_sw_if_index, u8 is_local, u32 * fib_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001284{
1285 vnet_main_t *vnm = vnet_get_main ();
1286
1287 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1288 if (~0 == *fib_index)
1289 {
Neale Ranns15002542017-09-10 04:39:11 -07001290 /* No such table */
1291 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Ranns32e1c012016-11-22 17:07:28 +00001292 }
1293
1294 if (~0 != ntohl (next_hop_sw_if_index))
1295 {
1296 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1297 ntohl (next_hop_sw_if_index)))
1298 {
1299 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1300 }
1301 }
1302
1303 return (0);
1304}
1305
1306static int
1307mroute_add_del_handler (u8 is_add,
1308 u8 is_local,
1309 u32 fib_index,
1310 const mfib_prefix_t * prefix,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001311 dpo_proto_t nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001312 u32 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001313 fib_rpf_id_t rpf_id,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001314 u32 next_hop_sw_if_index, u32 itf_flags, u32 bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +00001315{
1316 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1317
1318 fib_route_path_t path = {
1319 .frp_sw_if_index = next_hop_sw_if_index,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001320 .frp_proto = nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001321 };
1322
1323 if (is_local)
1324 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1325
Neale Rannsd792d9c2017-10-21 10:53:20 -07001326 if (DPO_PROTO_BIER == nh_proto)
1327 {
1328 path.frp_bier_imp = bier_imp;
1329 path.frp_flags = FIB_ROUTE_PATH_BIER_IMP;
1330 }
1331 else if (!is_local && ~0 == next_hop_sw_if_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001332 {
1333 mfib_table_entry_update (fib_index, prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001334 MFIB_SOURCE_API, rpf_id, entry_flags);
Neale Rannsd792d9c2017-10-21 10:53:20 -07001335 goto done;
1336 }
1337
1338 if (is_add)
1339 {
1340 mfib_table_entry_path_update (fib_index, prefix,
1341 MFIB_SOURCE_API, &path, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001342 }
1343 else
1344 {
Neale Rannsd792d9c2017-10-21 10:53:20 -07001345 mfib_table_entry_path_remove (fib_index, prefix,
1346 MFIB_SOURCE_API, &path);
Neale Ranns32e1c012016-11-22 17:07:28 +00001347 }
1348
Neale Rannsd792d9c2017-10-21 10:53:20 -07001349done:
Neale Ranns32e1c012016-11-22 17:07:28 +00001350 stats_dsunlock ();
1351 return (0);
1352}
1353
1354static int
1355api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1356{
1357 fib_protocol_t fproto;
Neale Rannsd792d9c2017-10-21 10:53:20 -07001358 dpo_proto_t nh_proto;
Neale Ranns32e1c012016-11-22 17:07:28 +00001359 u32 fib_index;
1360 int rv;
1361
Neale Rannsd792d9c2017-10-21 10:53:20 -07001362 nh_proto = mp->next_hop_afi;
Neale Ranns32e1c012016-11-22 17:07:28 +00001363 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1364 rv = add_del_mroute_check (fproto,
1365 mp->table_id,
1366 mp->next_hop_sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -07001367 mp->is_local, &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001368
1369 if (0 != rv)
1370 return (rv);
1371
1372 mfib_prefix_t pfx = {
1373 .fp_len = ntohs (mp->grp_address_length),
1374 .fp_proto = fproto,
1375 };
1376
1377 if (FIB_PROTOCOL_IP4 == fproto)
1378 {
1379 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1380 sizeof (pfx.fp_grp_addr.ip4));
1381 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1382 sizeof (pfx.fp_src_addr.ip4));
1383 }
1384 else
1385 {
1386 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1387 sizeof (pfx.fp_grp_addr.ip6));
1388 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1389 sizeof (pfx.fp_src_addr.ip6));
1390 }
1391
1392 return (mroute_add_del_handler (mp->is_add,
1393 mp->is_local,
1394 fib_index, &pfx,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001395 nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001396 ntohl (mp->entry_flags),
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001397 ntohl (mp->rpf_id),
Neale Ranns32e1c012016-11-22 17:07:28 +00001398 ntohl (mp->next_hop_sw_if_index),
Neale Rannsd792d9c2017-10-21 10:53:20 -07001399 ntohl (mp->itf_flags),
1400 ntohl (mp->bier_imp)));
Neale Ranns32e1c012016-11-22 17:07:28 +00001401}
1402
1403void
1404vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1405{
1406 vl_api_ip_mroute_add_del_reply_t *rmp;
1407 int rv;
1408 vnet_main_t *vnm = vnet_get_main ();
1409
1410 vnm->api_errno = 0;
1411
1412 rv = api_mroute_add_del_t_handler (mp);
1413
1414 rv = (rv == 0) ? vnm->api_errno : rv;
1415
1416 REPLY_MACRO (VL_API_IP_MROUTE_ADD_DEL_REPLY);
1417}
1418
Dave Barachb5e8a772016-12-06 12:04:42 -05001419static void
1420send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001421 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1422 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001423{
1424 vl_api_ip_details_t *mp;
1425
1426 mp = vl_msg_api_alloc (sizeof (*mp));
1427 memset (mp, 0, sizeof (*mp));
1428 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1429
1430 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001431 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001432 mp->context = context;
1433
Florin Coras6c4dae22018-01-09 06:39:23 -08001434 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001435}
1436
1437static void
1438send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001439 vl_api_registration_t * reg,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001440 u8 * ip, u16 prefix_length,
1441 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001442{
1443 vl_api_ip_address_details_t *mp;
1444
1445 mp = vl_msg_api_alloc (sizeof (*mp));
1446 memset (mp, 0, sizeof (*mp));
1447 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1448
1449 if (is_ipv6)
1450 {
1451 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1452 }
1453 else
1454 {
1455 u32 *tp = (u32 *) mp->ip;
1456 *tp = *(u32 *) ip;
1457 }
1458 mp->prefix_length = prefix_length;
1459 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001460 mp->sw_if_index = htonl (sw_if_index);
1461 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001462
Florin Coras6c4dae22018-01-09 06:39:23 -08001463 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001464}
1465
1466static void
1467vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1468{
1469 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001470 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001471 ip6_address_t *r6;
1472 ip4_address_t *r4;
1473 ip6_main_t *im6 = &ip6_main;
1474 ip4_main_t *im4 = &ip4_main;
1475 ip_lookup_main_t *lm6 = &im6->lookup_main;
1476 ip_lookup_main_t *lm4 = &im4->lookup_main;
1477 ip_interface_address_t *ia = 0;
1478 u32 sw_if_index = ~0;
1479 int rv __attribute__ ((unused)) = 0;
1480
1481 VALIDATE_SW_IF_INDEX (mp);
1482
1483 sw_if_index = ntohl (mp->sw_if_index);
1484
Florin Coras6c4dae22018-01-09 06:39:23 -08001485 reg = vl_api_client_index_to_registration (mp->client_index);
1486 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001487 return;
1488
Dave Barachb5e8a772016-12-06 12:04:42 -05001489 if (mp->is_ipv6)
1490 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001491 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001492 foreach_ip_interface_address (lm6, ia, sw_if_index,
1493 1 /* honor unnumbered */,
1494 ({
1495 r6 = ip_interface_address_get_address (lm6, ia);
1496 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001497 send_ip_address_details(am, reg, (u8*)r6, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001498 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001499 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001500 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001501 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001502 else
1503 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001504 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001505 foreach_ip_interface_address (lm4, ia, sw_if_index,
1506 1 /* honor unnumbered */,
1507 ({
1508 r4 = ip_interface_address_get_address (lm4, ia);
1509 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001510 send_ip_address_details(am, reg, (u8*)r4, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001511 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001512 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001513 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001514 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001515 BAD_SW_IF_INDEX_LABEL;
1516}
1517
1518static void
1519vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1520{
1521 vpe_api_main_t *am = &vpe_api_main;
1522 vnet_main_t *vnm = vnet_get_main ();
1523 vlib_main_t *vm = vlib_get_main ();
1524 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001525 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001526 vnet_sw_interface_t *si, *sorted_sis;
1527 u32 sw_if_index = ~0;
1528
Florin Coras6c4dae22018-01-09 06:39:23 -08001529 reg = vl_api_client_index_to_registration (mp->client_index);
1530 if (!reg)
1531 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001532
1533 /* Gather interfaces. */
1534 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1535 _vec_len (sorted_sis) = 0;
1536 /* *INDENT-OFF* */
1537 pool_foreach (si, im->sw_interfaces,
1538 ({
1539 vec_add1 (sorted_sis, si[0]);
1540 }));
1541 /* *INDENT-ON* */
1542
1543 vec_foreach (si, sorted_sis)
1544 {
1545 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1546 {
1547 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1548 {
1549 continue;
1550 }
1551 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001552 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001553 }
1554 }
1555}
1556
1557static void
1558set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1559{
1560 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001561 int rv;
1562 u32 table_id;
1563 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001564
Neale Ranns227038a2017-04-21 01:07:59 -07001565 table_id = ntohl (mp->vrf_id);
1566
1567#define _(a,b) if (mp->a) flow_hash_config |= b;
1568 foreach_flow_hash_bit;
1569#undef _
1570
1571 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001572
1573 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1574}
1575
1576static void
1577set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1578{
1579 vl_api_set_ip_flow_hash_reply_t *rmp;
1580 int rv;
1581 u32 table_id;
1582 flow_hash_config_t flow_hash_config = 0;
1583
1584 table_id = ntohl (mp->vrf_id);
1585
1586#define _(a,b) if (mp->a) flow_hash_config |= b;
1587 foreach_flow_hash_bit;
1588#undef _
1589
1590 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1591
1592 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1593}
1594
1595
1596static void
1597vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1598{
1599 if (mp->is_ipv6 == 0)
1600 set_ip4_flow_hash (mp);
1601 else
1602 set_ip6_flow_hash (mp);
1603}
1604
1605static void
1606 vl_api_sw_interface_ip6nd_ra_config_t_handler
1607 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1608{
1609 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1610 vlib_main_t *vm = vlib_get_main ();
1611 int rv = 0;
1612 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1613 default_router;
1614
1615 is_no = mp->is_no == 1;
1616 suppress = mp->suppress == 1;
1617 managed = mp->managed == 1;
1618 other = mp->other == 1;
1619 ll_option = mp->ll_option == 1;
1620 send_unicast = mp->send_unicast == 1;
1621 cease = mp->cease == 1;
1622 default_router = mp->default_router == 1;
1623
1624 VALIDATE_SW_IF_INDEX (mp);
1625
1626 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1627 suppress, managed, other,
1628 ll_option, send_unicast, cease,
1629 default_router, ntohl (mp->lifetime),
1630 ntohl (mp->initial_count),
1631 ntohl (mp->initial_interval),
1632 ntohl (mp->max_interval),
1633 ntohl (mp->min_interval), is_no);
1634
1635 BAD_SW_IF_INDEX_LABEL;
1636
1637 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1638}
1639
1640static void
1641 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1642 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1643{
1644 vlib_main_t *vm = vlib_get_main ();
1645 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1646 int rv = 0;
1647 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1648
1649 VALIDATE_SW_IF_INDEX (mp);
1650
1651 is_no = mp->is_no == 1;
1652 use_default = mp->use_default == 1;
1653 no_advertise = mp->no_advertise == 1;
1654 off_link = mp->off_link == 1;
1655 no_autoconfig = mp->no_autoconfig == 1;
1656 no_onlink = mp->no_onlink == 1;
1657
1658 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1659 (ip6_address_t *) mp->address,
1660 mp->address_length, use_default,
1661 ntohl (mp->val_lifetime),
1662 ntohl (mp->pref_lifetime), no_advertise,
1663 off_link, no_autoconfig, no_onlink, is_no);
1664
1665 BAD_SW_IF_INDEX_LABEL;
1666 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1667}
1668
1669static void
Florin Coras6c4dae22018-01-09 06:39:23 -08001670send_ip6nd_proxy_details (vl_api_registration_t * reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001671 u32 context,
1672 const ip46_address_t * addr, u32 sw_if_index)
1673{
1674 vl_api_ip6nd_proxy_details_t *mp;
1675
1676 mp = vl_msg_api_alloc (sizeof (*mp));
1677 memset (mp, 0, sizeof (*mp));
1678 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1679 mp->context = context;
1680 mp->sw_if_index = htonl (sw_if_index);
1681 memcpy (mp->address, addr, 16);
1682
Florin Coras6c4dae22018-01-09 06:39:23 -08001683 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns3f844d02017-02-18 00:03:54 -08001684}
1685
Neale Ranns3f844d02017-02-18 00:03:54 -08001686typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1687{
1688 u32 *indices;
1689} api_ip6nd_proxy_fib_table_walk_ctx_t;
1690
Neale Ranns89541992017-04-06 04:41:02 -07001691static fib_table_walk_rc_t
Neale Ranns3f844d02017-02-18 00:03:54 -08001692api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1693{
1694 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1695
1696 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1697 {
1698 vec_add1 (ctx->indices, fei);
1699 }
1700
Neale Ranns89541992017-04-06 04:41:02 -07001701 return (FIB_TABLE_WALK_CONTINUE);
Neale Ranns3f844d02017-02-18 00:03:54 -08001702}
1703
1704static void
1705vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1706{
1707 ip6_main_t *im6 = &ip6_main;
1708 fib_table_t *fib_table;
1709 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1710 .indices = NULL,
1711 };
1712 fib_node_index_t *feip;
1713 fib_prefix_t pfx;
Florin Coras6c4dae22018-01-09 06:39:23 -08001714 vl_api_registration_t *reg;
Neale Ranns3f844d02017-02-18 00:03:54 -08001715
Florin Coras6c4dae22018-01-09 06:39:23 -08001716 reg = vl_api_client_index_to_registration (mp->client_index);
1717 if (!reg)
1718 return;
Neale Ranns3f844d02017-02-18 00:03:54 -08001719
1720 /* *INDENT-OFF* */
1721 pool_foreach (fib_table, im6->fibs,
1722 ({
1723 fib_table_walk(fib_table->ft_index,
1724 FIB_PROTOCOL_IP6,
1725 api_ip6nd_proxy_fib_table_walk,
1726 &ctx);
1727 }));
1728 /* *INDENT-ON* */
1729
1730 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1731
1732 vec_foreach (feip, ctx.indices)
1733 {
1734 fib_entry_get_prefix (*feip, &pfx);
1735
Florin Coras6c4dae22018-01-09 06:39:23 -08001736 send_ip6nd_proxy_details (reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001737 mp->context,
1738 &pfx.fp_addr,
1739 fib_entry_get_resolving_interface (*feip));
1740 }
1741
1742 vec_free (ctx.indices);
1743}
1744
1745static void
1746vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1747{
1748 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
1749 int rv = 0;
1750
1751 VALIDATE_SW_IF_INDEX (mp);
1752
1753 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index),
1754 (ip6_address_t *) mp->address, mp->is_del);
1755
1756 BAD_SW_IF_INDEX_LABEL;
1757 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1758}
1759
1760static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001761 vl_api_sw_interface_ip6_enable_disable_t_handler
1762 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1763{
1764 vlib_main_t *vm = vlib_get_main ();
1765 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1766 vnet_main_t *vnm = vnet_get_main ();
1767 int rv = 0;
1768 clib_error_t *error;
1769
1770 vnm->api_errno = 0;
1771
1772 VALIDATE_SW_IF_INDEX (mp);
1773
1774 error =
1775 (mp->enable == 1) ? enable_ip6_interface (vm,
1776 ntohl (mp->sw_if_index)) :
1777 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1778
1779 if (error)
1780 {
1781 clib_error_report (error);
1782 rv = VNET_API_ERROR_UNSPECIFIED;
1783 }
1784 else
1785 {
1786 rv = vnm->api_errno;
1787 }
1788
1789 BAD_SW_IF_INDEX_LABEL;
1790
1791 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1792}
1793
1794static void
1795 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1796 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1797{
1798 vlib_main_t *vm = vlib_get_main ();
1799 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1800 int rv = 0;
1801 clib_error_t *error;
1802 vnet_main_t *vnm = vnet_get_main ();
1803
1804 vnm->api_errno = 0;
1805
1806 VALIDATE_SW_IF_INDEX (mp);
1807
1808 error = set_ip6_link_local_address (vm,
1809 ntohl (mp->sw_if_index),
Neale Ranns75152282017-01-09 01:00:45 -08001810 (ip6_address_t *) mp->address);
Dave Barachb5e8a772016-12-06 12:04:42 -05001811 if (error)
1812 {
1813 clib_error_report (error);
1814 rv = VNET_API_ERROR_UNSPECIFIED;
1815 }
1816 else
1817 {
1818 rv = vnm->api_errno;
1819 }
1820
1821 BAD_SW_IF_INDEX_LABEL;
1822
1823 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1824}
1825
Neale Ranns32e1c012016-11-22 17:07:28 +00001826void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001827vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001828 u32 context, const mfib_signal_t * mfs)
1829{
1830 vl_api_mfib_signal_details_t *mp;
1831 mfib_prefix_t prefix;
1832 mfib_table_t *mfib;
1833 mfib_itf_t *mfi;
1834
1835 mp = vl_msg_api_alloc (sizeof (*mp));
1836
1837 memset (mp, 0, sizeof (*mp));
1838 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1839 mp->context = context;
1840
1841 mfi = mfib_itf_get (mfs->mfs_itf);
1842 mfib_entry_get_prefix (mfs->mfs_entry, &prefix);
1843 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
1844 prefix.fp_proto);
1845 mp->table_id = ntohl (mfib->mft_table_id);
1846 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1847
1848 if (FIB_PROTOCOL_IP4 == prefix.fp_proto)
1849 {
1850 mp->grp_address_len = ntohs (prefix.fp_len);
1851
1852 memcpy (mp->grp_address, &prefix.fp_grp_addr.ip4, 4);
1853 if (prefix.fp_len > 32)
1854 {
1855 memcpy (mp->src_address, &prefix.fp_src_addr.ip4, 4);
1856 }
1857 }
1858 else
1859 {
1860 mp->grp_address_len = ntohs (prefix.fp_len);
1861
1862 ASSERT (0);
1863 }
1864
1865 if (0 != mfs->mfs_buffer_len)
1866 {
1867 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1868
1869 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1870 }
1871 else
1872 {
1873 mp->ip_packet_len = 0;
1874 }
1875
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001876 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001877}
1878
1879static void
1880vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1881{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001882 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001883
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001884 reg = vl_api_client_index_to_registration (mp->client_index);
1885 if (!reg)
1886 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001887
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001888 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001889 ;
1890}
Dave Barachb5e8a772016-12-06 12:04:42 -05001891
Florin Coras595992c2017-11-06 17:17:08 -08001892static void
1893 vl_api_ip_container_proxy_add_del_t_handler
1894 (vl_api_ip_container_proxy_add_del_t * mp)
1895{
1896 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1897 vnet_ip_container_proxy_args_t args;
1898 int rv = 0;
1899 clib_error_t *error;
1900
1901 memset (&args, 0, sizeof (args));
1902 ip_set (&args.prefix.fp_addr, mp->ip, mp->is_ip4);
1903 args.prefix.fp_len = mp->plen ? mp->plen : (mp->is_ip4 ? 32 : 128);
1904 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1905 args.is_add = mp->is_add;
1906 if ((error = vnet_ip_container_proxy_add_del (&args)))
1907 {
1908 rv = clib_error_get_code (error);
1909 clib_error_report (error);
1910 }
1911
1912 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1913}
1914
Neale Rannsb8d44812017-11-10 06:53:54 -08001915static void
1916vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1917{
1918 int rv = 0;
1919 vl_api_ioam_enable_reply_t *rmp;
1920 clib_error_t *error;
1921
1922 /* Ignoring the profile id as currently a single profile
1923 * is supported */
1924 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1925 mp->seqno, mp->analyse);
1926 if (error)
1927 {
1928 clib_error_report (error);
1929 rv = clib_error_get_code (error);
1930 }
1931
1932 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1933}
1934
1935static void
1936vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1937{
1938 int rv = 0;
1939 vl_api_ioam_disable_reply_t *rmp;
1940 clib_error_t *error;
1941
1942 error = clear_ioam_rewrite_fn ();
1943 if (error)
1944 {
1945 clib_error_report (error);
1946 rv = clib_error_get_code (error);
1947 }
1948
1949 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1950}
1951
1952static void
1953 vl_api_ip_source_and_port_range_check_add_del_t_handler
1954 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1955{
1956 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1957 int rv = 0;
1958
1959 u8 is_ipv6 = mp->is_ipv6;
1960 u8 is_add = mp->is_add;
1961 u8 mask_length = mp->mask_length;
1962 ip4_address_t ip4_addr;
1963 ip6_address_t ip6_addr;
1964 u16 *low_ports = 0;
1965 u16 *high_ports = 0;
1966 u32 vrf_id;
1967 u16 tmp_low, tmp_high;
1968 u8 num_ranges;
1969 int i;
1970
1971 // Validate port range
1972 num_ranges = mp->number_of_ranges;
1973 if (num_ranges > 32)
1974 { // This is size of array in VPE.API
1975 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1976 goto reply;
1977 }
1978
1979 vec_reset_length (low_ports);
1980 vec_reset_length (high_ports);
1981
1982 for (i = 0; i < num_ranges; i++)
1983 {
1984 tmp_low = mp->low_ports[i];
1985 tmp_high = mp->high_ports[i];
1986 // If tmp_low <= tmp_high then only need to check tmp_low = 0
1987 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1988 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1989 {
1990 rv = VNET_API_ERROR_INVALID_VALUE;
1991 goto reply;
1992 }
1993 vec_add1 (low_ports, tmp_low);
1994 vec_add1 (high_ports, tmp_high + 1);
1995 }
1996
1997 // Validate mask_length
1998 if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
1999 {
2000 rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
2001 goto reply;
2002 }
2003
2004 vrf_id = ntohl (mp->vrf_id);
2005
2006 if (vrf_id < 1)
2007 {
2008 rv = VNET_API_ERROR_INVALID_VALUE;
2009 goto reply;
2010 }
2011
2012
2013 if (is_ipv6)
2014 {
2015 clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
2016 rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
2017 mask_length,
2018 vrf_id,
2019 low_ports,
2020 high_ports, is_add);
2021 }
2022 else
2023 {
2024 clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
2025 rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
2026 mask_length,
2027 vrf_id,
2028 low_ports,
2029 high_ports, is_add);
2030 }
2031
2032reply:
2033 vec_free (low_ports);
2034 vec_free (high_ports);
2035 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
2036}
2037
2038static void
2039 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
2040 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
2041{
2042 vlib_main_t *vm = vlib_get_main ();
2043 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
2044 ip4_main_t *im = &ip4_main;
2045 int rv;
2046 u32 sw_if_index;
2047 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2048 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2049 uword *p = 0;
2050 int i;
2051
2052 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
2053 ntohl (mp->tcp_out_vrf_id);
2054 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
2055 ntohl (mp->udp_out_vrf_id);
2056 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
2057 ntohl (mp->tcp_in_vrf_id);
2058 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
2059 ntohl (mp->udp_in_vrf_id);
2060
2061
2062 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
2063 {
2064 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
2065 {
2066 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
2067
2068 if (p == 0)
2069 {
2070 rv = VNET_API_ERROR_INVALID_VALUE;
2071 goto reply;
2072 }
2073
2074 fib_index[i] = p[0];
2075 }
2076 else
2077 fib_index[i] = ~0;
2078 }
2079 sw_if_index = ntohl (mp->sw_if_index);
2080
2081 VALIDATE_SW_IF_INDEX (mp);
2082
2083 rv =
2084 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
2085 mp->is_add);
2086
2087 BAD_SW_IF_INDEX_LABEL;
2088reply:
2089
2090 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
2091}
2092
2093#define IP4_ARP_EVENT 3
2094#define IP6_ND_EVENT 4
2095
2096static int arp_change_delete_callback (u32 pool_index, u8 * notused);
2097static int nd_change_delete_callback (u32 pool_index, u8 * notused);
2098static vlib_node_registration_t ip_resolver_process_node;
2099
2100static void
2101handle_ip4_arp_event (u32 pool_index)
2102{
2103 vpe_api_main_t *vam = &vpe_api_main;
2104 vnet_main_t *vnm = vam->vnet_main;
2105 vlib_main_t *vm = vam->vlib_main;
2106 vl_api_ip4_arp_event_t *event;
2107 vl_api_ip4_arp_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002108 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002109
2110 /* Client can cancel, die, etc. */
2111 if (pool_is_free_index (vam->arp_events, pool_index))
2112 return;
2113
2114 event = pool_elt_at_index (vam->arp_events, pool_index);
2115
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002116 reg = vl_api_client_index_to_registration (event->client_index);
2117 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002118 {
2119 (void) vnet_add_del_ip4_arp_change_event
2120 (vnm, arp_change_delete_callback,
2121 event->pid, &event->address,
2122 ip_resolver_process_node.index, IP4_ARP_EVENT,
2123 ~0 /* pool index, notused */ , 0 /* is_add */ );
2124 return;
2125 }
2126
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002127 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002128 {
2129 mp = vl_msg_api_alloc (sizeof (*mp));
2130 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002131 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002132 }
2133 else
2134 {
2135 static f64 last_time;
2136 /*
2137 * Throttle syslog msgs.
2138 * It's pretty tempting to just revoke the registration...
2139 */
2140 if (vlib_time_now (vm) > last_time + 10.0)
2141 {
2142 clib_warning ("arp event for %U to pid %d: queue stuffed!",
2143 format_ip4_address, &event->address, event->pid);
2144 last_time = vlib_time_now (vm);
2145 }
2146 }
2147}
2148
2149void
2150handle_ip6_nd_event (u32 pool_index)
2151{
2152 vpe_api_main_t *vam = &vpe_api_main;
2153 vnet_main_t *vnm = vam->vnet_main;
2154 vlib_main_t *vm = vam->vlib_main;
2155 vl_api_ip6_nd_event_t *event;
2156 vl_api_ip6_nd_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002157 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002158
2159 /* Client can cancel, die, etc. */
2160 if (pool_is_free_index (vam->nd_events, pool_index))
2161 return;
2162
2163 event = pool_elt_at_index (vam->nd_events, pool_index);
2164
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002165 reg = vl_api_client_index_to_registration (event->client_index);
2166 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002167 {
2168 (void) vnet_add_del_ip6_nd_change_event
2169 (vnm, nd_change_delete_callback,
2170 event->pid, &event->address,
2171 ip_resolver_process_node.index, IP6_ND_EVENT,
2172 ~0 /* pool index, notused */ , 0 /* is_add */ );
2173 return;
2174 }
2175
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002176 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002177 {
2178 mp = vl_msg_api_alloc (sizeof (*mp));
2179 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002180 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002181 }
2182 else
2183 {
2184 static f64 last_time;
2185 /*
2186 * Throttle syslog msgs.
2187 * It's pretty tempting to just revoke the registration...
2188 */
2189 if (vlib_time_now (vm) > last_time + 10.0)
2190 {
2191 clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
2192 format_ip6_address, &event->address, event->pid);
2193 last_time = vlib_time_now (vm);
2194 }
2195 }
2196}
2197
2198static uword
2199resolver_process (vlib_main_t * vm,
2200 vlib_node_runtime_t * rt, vlib_frame_t * f)
2201{
2202 volatile f64 timeout = 100.0;
2203 volatile uword *event_data = 0;
2204
2205 while (1)
2206 {
2207 vlib_process_wait_for_event_or_clock (vm, timeout);
2208
2209 uword event_type =
2210 vlib_process_get_events (vm, (uword **) & event_data);
2211
2212 int i;
2213 switch (event_type)
2214 {
2215 case IP4_ARP_EVENT:
2216 for (i = 0; i < vec_len (event_data); i++)
2217 handle_ip4_arp_event (event_data[i]);
2218 break;
2219
2220 case IP6_ND_EVENT:
2221 for (i = 0; i < vec_len (event_data); i++)
2222 handle_ip6_nd_event (event_data[i]);
2223 break;
2224
2225 case ~0: /* timeout */
2226 break;
2227 }
2228
2229 vec_reset_length (event_data);
2230 }
2231 return 0; /* or not */
2232}
2233
2234/* *INDENT-OFF* */
2235VLIB_REGISTER_NODE (ip_resolver_process_node,static) = {
2236 .function = resolver_process,
2237 .type = VLIB_NODE_TYPE_PROCESS,
2238 .name = "ip-route-resolver-process",
2239};
2240/* *INDENT-ON* */
2241
2242static int
2243nd_change_data_callback (u32 pool_index, u8 * new_mac,
2244 u32 sw_if_index, ip6_address_t * address)
2245{
2246 vpe_api_main_t *am = &vpe_api_main;
2247 vl_api_ip6_nd_event_t *event;
2248
2249 if (pool_is_free_index (am->nd_events, pool_index))
2250 return 1;
2251
2252 event = pool_elt_at_index (am->nd_events, pool_index);
2253 if (eth_mac_equal (event->new_mac, new_mac) &&
2254 sw_if_index == ntohl (event->sw_if_index))
2255 {
2256 return 1;
2257 }
2258
2259 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2260 event->sw_if_index = htonl (sw_if_index);
2261 return 0;
2262}
2263
2264static int
2265arp_change_delete_callback (u32 pool_index, u8 * notused)
2266{
2267 vpe_api_main_t *am = &vpe_api_main;
2268
2269 if (pool_is_free_index (am->arp_events, pool_index))
2270 return 1;
2271
2272 pool_put_index (am->arp_events, pool_index);
2273 return 0;
2274}
2275
2276static int
2277nd_change_delete_callback (u32 pool_index, u8 * notused)
2278{
2279 vpe_api_main_t *am = &vpe_api_main;
2280
2281 if (pool_is_free_index (am->nd_events, pool_index))
2282 return 1;
2283
2284 pool_put_index (am->nd_events, pool_index);
2285 return 0;
2286}
2287
2288static vlib_node_registration_t wc_arp_process_node;
2289
2290enum
2291{ WC_ARP_REPORT, WC_ND_REPORT };
2292
2293static uword
2294wc_arp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
2295{
2296 /* These cross the longjmp boundry (vlib_process_wait_for_event)
2297 * and need to be volatile - to prevent them from being optimized into
2298 * a register - which could change during suspension */
2299
2300 volatile wc_arp_report_t arp_prev = { 0 };
2301 volatile wc_nd_report_t nd_prev = { 0 };
2302 volatile f64 last_arp = vlib_time_now (vm);
2303 volatile f64 last_nd = vlib_time_now (vm);
2304
2305 while (1)
2306 {
2307 vlib_process_wait_for_event (vm);
2308 uword event_type = WC_ARP_REPORT;
2309 void *event_data = vlib_process_get_event_data (vm, &event_type);
2310
2311 f64 now = vlib_time_now (vm);
2312 int i;
2313 if (event_type == WC_ARP_REPORT)
2314 {
2315 wc_arp_report_t *arp_events = event_data;
2316 for (i = 0; i < vec_len (arp_events); i++)
2317 {
2318 /* discard dup event */
2319 if (arp_prev.ip4 == arp_events[i].ip4 &&
2320 eth_mac_equal ((u8 *) arp_prev.mac, arp_events[i].mac) &&
2321 arp_prev.sw_if_index == arp_events[i].sw_if_index &&
2322 (now - last_arp) < 10.0)
2323 {
2324 continue;
2325 }
2326 arp_prev = arp_events[i];
2327 last_arp = now;
2328 vpe_client_registration_t *reg;
2329 /* *INDENT-OFF* */
2330 pool_foreach(reg, vpe_api_main.wc_ip4_arp_events_registrations,
2331 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002332 vl_api_registration_t *vl_reg;
2333 vl_reg = vl_api_client_index_to_registration (reg->client_index);
2334 if (reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002335 {
2336 vl_api_ip4_arp_event_t * event = vl_msg_api_alloc (sizeof *event);
2337 memset (event, 0, sizeof *event);
2338 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2339 event->client_index = reg->client_index;
2340 event->pid = reg->client_pid;
2341 event->mac_ip = 1;
2342 event->address = arp_events[i].ip4;
2343 event->sw_if_index = htonl(arp_events[i].sw_if_index);
2344 memcpy(event->new_mac, arp_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002345 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002346 }
2347 }));
2348 /* *INDENT-ON* */
2349 }
2350 }
2351 else if (event_type == WC_ND_REPORT)
2352 {
2353 wc_nd_report_t *nd_events = event_data;
2354 for (i = 0; i < vec_len (nd_events); i++)
2355 {
2356 /* discard dup event */
2357 if (ip6_address_is_equal
2358 ((ip6_address_t *) & nd_prev.ip6, &nd_events[i].ip6)
2359 && eth_mac_equal ((u8 *) nd_prev.mac, nd_events[i].mac)
2360 && nd_prev.sw_if_index == nd_events[i].sw_if_index
2361 && (now - last_nd) < 10.0)
2362 {
2363 continue;
2364 }
2365 nd_prev = nd_events[i];
2366 last_nd = now;
2367 vpe_client_registration_t *reg;
2368 /* *INDENT-OFF* */
2369 pool_foreach(reg, vpe_api_main.wc_ip6_nd_events_registrations,
2370 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002371 vl_api_registration_t *vl_reg;
2372 vl_reg = vl_api_client_index_to_registration (reg->client_index);
2373 if (vl_reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002374 {
2375 vl_api_ip6_nd_event_t * event = vl_msg_api_alloc (sizeof *event);
2376 memset (event, 0, sizeof *event);
2377 event->_vl_msg_id = htons (VL_API_IP6_ND_EVENT);
2378 event->client_index = reg->client_index;
2379 event->pid = reg->client_pid;
2380 event->mac_ip = 1;
2381 memcpy(event->address, nd_events[i].ip6.as_u8, sizeof event->address);
2382 event->sw_if_index = htonl(nd_events[i].sw_if_index);
2383 memcpy(event->new_mac, nd_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002384 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002385 }
2386 }));
2387 /* *INDENT-ON* */
2388 }
2389 }
2390 vlib_process_put_event_data (vm, event_data);
2391 }
2392
2393 return 0;
2394}
2395
2396/* *INDENT-OFF* */
2397VLIB_REGISTER_NODE (wc_arp_process_node,static) = {
2398 .function = wc_arp_process,
2399 .type = VLIB_NODE_TYPE_PROCESS,
2400 .name = "wildcard-ip4-arp-publisher-process",
2401};
2402/* *INDENT-ON* */
2403
2404static int
2405arp_change_data_callback (u32 pool_index, u8 * new_mac,
2406 u32 sw_if_index, u32 address)
2407{
2408 vpe_api_main_t *am = &vpe_api_main;
2409 vl_api_ip4_arp_event_t *event;
2410
2411 if (pool_is_free_index (am->arp_events, pool_index))
2412 return 1;
2413
2414 event = pool_elt_at_index (am->arp_events, pool_index);
2415 if (eth_mac_equal (event->new_mac, new_mac) &&
2416 sw_if_index == ntohl (event->sw_if_index))
2417 {
2418 return 1;
2419 }
2420
2421 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2422 event->sw_if_index = htonl (sw_if_index);
2423 return 0;
2424}
2425
2426static void
2427vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
2428{
2429 vpe_api_main_t *am = &vpe_api_main;
2430 vnet_main_t *vnm = vnet_get_main ();
2431 vl_api_want_ip4_arp_events_reply_t *rmp;
2432 int rv = 0;
2433
2434 if (mp->address == 0)
2435 {
2436 uword *p =
2437 hash_get (am->wc_ip4_arp_events_registration_hash, mp->client_index);
2438 vpe_client_registration_t *rp;
2439 if (p)
2440 {
2441 if (mp->enable_disable)
2442 {
2443 clib_warning ("pid %d: already enabled...", mp->pid);
2444 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2445 goto reply;
2446 }
2447 else
2448 {
2449 rp =
2450 pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2451 pool_put (am->wc_ip4_arp_events_registrations, rp);
2452 hash_unset (am->wc_ip4_arp_events_registration_hash,
2453 mp->client_index);
2454 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
2455 wc_arp_set_publisher_node (~0, WC_ARP_REPORT);
2456 goto reply;
2457 }
2458 }
2459 if (mp->enable_disable == 0)
2460 {
2461 clib_warning ("pid %d: already disabled...", mp->pid);
2462 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2463 goto reply;
2464 }
2465 pool_get (am->wc_ip4_arp_events_registrations, rp);
2466 rp->client_index = mp->client_index;
2467 rp->client_pid = mp->pid;
2468 hash_set (am->wc_ip4_arp_events_registration_hash, rp->client_index,
2469 rp - am->wc_ip4_arp_events_registrations);
2470 wc_arp_set_publisher_node (wc_arp_process_node.index, WC_ARP_REPORT);
2471 goto reply;
2472 }
2473
2474 if (mp->enable_disable)
2475 {
2476 vl_api_ip4_arp_event_t *event;
2477 pool_get (am->arp_events, event);
2478 rv = vnet_add_del_ip4_arp_change_event
2479 (vnm, arp_change_data_callback,
2480 mp->pid, &mp->address /* addr, in net byte order */ ,
2481 ip_resolver_process_node.index,
2482 IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
2483
2484 if (rv)
2485 {
2486 pool_put (am->arp_events, event);
2487 goto reply;
2488 }
2489 memset (event, 0, sizeof (*event));
2490
2491 /* Python API expects events to have no context */
2492 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2493 event->client_index = mp->client_index;
2494 event->address = mp->address;
2495 event->pid = mp->pid;
2496 if (mp->address == 0)
2497 event->mac_ip = 1;
2498 }
2499 else
2500 {
2501 rv = vnet_add_del_ip4_arp_change_event
2502 (vnm, arp_change_delete_callback,
2503 mp->pid, &mp->address /* addr, in net byte order */ ,
2504 ip_resolver_process_node.index,
2505 IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2506 }
2507reply:
2508 REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
2509}
2510
2511static void
2512vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
2513{
2514 vpe_api_main_t *am = &vpe_api_main;
2515 vnet_main_t *vnm = vnet_get_main ();
2516 vl_api_want_ip6_nd_events_reply_t *rmp;
2517 int rv = 0;
2518
2519 if (ip6_address_is_zero ((ip6_address_t *) mp->address))
2520 {
2521 uword *p =
2522 hash_get (am->wc_ip6_nd_events_registration_hash, mp->client_index);
2523 vpe_client_registration_t *rp;
2524 if (p)
2525 {
2526 if (mp->enable_disable)
2527 {
2528 clib_warning ("pid %d: already enabled...", mp->pid);
2529 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2530 goto reply;
2531 }
2532 else
2533 {
2534 rp =
2535 pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2536 pool_put (am->wc_ip6_nd_events_registrations, rp);
2537 hash_unset (am->wc_ip6_nd_events_registration_hash,
2538 mp->client_index);
2539 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
2540 wc_nd_set_publisher_node (~0, 2);
2541 goto reply;
2542 }
2543 }
2544 if (mp->enable_disable == 0)
2545 {
2546 clib_warning ("pid %d: already disabled...", mp->pid);
2547 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2548 goto reply;
2549 }
2550 pool_get (am->wc_ip6_nd_events_registrations, rp);
2551 rp->client_index = mp->client_index;
2552 rp->client_pid = mp->pid;
2553 hash_set (am->wc_ip6_nd_events_registration_hash, rp->client_index,
2554 rp - am->wc_ip6_nd_events_registrations);
2555 wc_nd_set_publisher_node (wc_arp_process_node.index, WC_ND_REPORT);
2556 goto reply;
2557 }
2558
2559 if (mp->enable_disable)
2560 {
2561 vl_api_ip6_nd_event_t *event;
2562 pool_get (am->nd_events, event);
2563
2564 rv = vnet_add_del_ip6_nd_change_event
2565 (vnm, nd_change_data_callback,
2566 mp->pid, mp->address /* addr, in net byte order */ ,
2567 ip_resolver_process_node.index,
2568 IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
2569
2570 if (rv)
2571 {
2572 pool_put (am->nd_events, event);
2573 goto reply;
2574 }
2575 memset (event, 0, sizeof (*event));
2576
2577 event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
2578 event->client_index = mp->client_index;
2579 clib_memcpy (event->address, mp->address, sizeof event->address);
2580 event->pid = mp->pid;
2581 }
2582 else
2583 {
2584 rv = vnet_add_del_ip6_nd_change_event
2585 (vnm, nd_change_delete_callback,
2586 mp->pid, mp->address /* addr, in net byte order */ ,
2587 ip_resolver_process_node.index,
2588 IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2589 }
2590reply:
2591 REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
2592}
2593
2594static void
2595vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2596{
2597 vl_api_proxy_arp_add_del_reply_t *rmp;
2598 u32 fib_index;
2599 int rv;
2600 ip4_main_t *im = &ip4_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08002601 int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
2602 ip4_address_t * hi_addr,
2603 u32 fib_index, int is_del);
2604 uword *p;
2605
Ole Troan3288ed72017-12-06 17:00:05 +01002606 stats_dslock_with_hint (1 /* release hint */ , 6 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002607
2608 p = hash_get (im->fib_index_by_table_id, ntohl (mp->vrf_id));
2609
2610 if (!p)
2611 {
2612 rv = VNET_API_ERROR_NO_SUCH_FIB;
2613 goto out;
2614 }
2615
2616 fib_index = p[0];
2617
2618 rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->low_address,
2619 (ip4_address_t *) mp->hi_address,
2620 fib_index, mp->is_add == 0);
2621
2622out:
Ole Troan3288ed72017-12-06 17:00:05 +01002623 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002624 REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2625}
2626
2627static void
2628 vl_api_proxy_arp_intfc_enable_disable_t_handler
2629 (vl_api_proxy_arp_intfc_enable_disable_t * mp)
2630{
2631 int rv = 0;
2632 vnet_main_t *vnm = vnet_get_main ();
2633 vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
2634
2635 VALIDATE_SW_IF_INDEX (mp);
2636
2637 vnet_sw_interface_t *si =
2638 vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
2639
2640 ASSERT (si);
2641
2642 if (mp->enable_disable)
2643 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2644 else
2645 si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2646
2647 BAD_SW_IF_INDEX_LABEL;
2648
2649 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
2650}
2651
2652static int
2653ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2654{
2655 vnet_main_t *vnm = vnet_get_main ();
2656 vnet_interface_main_t *im = &vnm->interface_main;
2657 ip4_main_t *im4 = &ip4_main;
2658 static u32 *sw_if_indices_to_shut;
Neale Rannsb8d44812017-11-10 06:53:54 -08002659 fib_table_t *fib_table;
2660 ip4_fib_t *fib;
2661 u32 sw_if_index;
2662 int i;
2663 int rv = VNET_API_ERROR_NO_SUCH_FIB;
2664 u32 target_fib_id = ntohl (mp->vrf_id);
2665
Ole Troan3288ed72017-12-06 17:00:05 +01002666 stats_dslock_with_hint (1 /* release hint */ , 8 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002667
2668 /* *INDENT-OFF* */
2669 pool_foreach (fib_table, im4->fibs,
2670 ({
2671 vnet_sw_interface_t * si;
2672
2673 fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
2674
2675 if (fib->table_id != target_fib_id)
2676 continue;
2677
2678 /* remove any mpls encap/decap labels */
2679 mpls_fib_reset_labels (fib->table_id);
2680
2681 /* remove any proxy arps in this fib */
2682 vnet_proxy_arp_fib_reset (fib->table_id);
2683
2684 /* Set the flow hash for this fib to the default */
2685 vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2686
2687 vec_reset_length (sw_if_indices_to_shut);
2688
2689 /* Shut down interfaces in this FIB / clean out intfc routes */
2690 pool_foreach (si, im->sw_interfaces,
2691 ({
2692 u32 sw_if_index = si->sw_if_index;
2693
2694 if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
2695 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
2696 fib->index))
2697 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2698 }));
2699
2700 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2701 sw_if_index = sw_if_indices_to_shut[i];
2702
2703 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2704 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2705 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2706 }
2707
2708 fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
2709
2710 rv = 0;
2711 break;
2712 })); /* pool_foreach (fib) */
2713 /* *INDENT-ON* */
2714
Ole Troan3288ed72017-12-06 17:00:05 +01002715 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002716 return rv;
2717}
2718
2719static int
2720ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2721{
2722 vnet_main_t *vnm = vnet_get_main ();
2723 vnet_interface_main_t *im = &vnm->interface_main;
2724 ip6_main_t *im6 = &ip6_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08002725 static u32 *sw_if_indices_to_shut;
2726 fib_table_t *fib_table;
2727 ip6_fib_t *fib;
2728 u32 sw_if_index;
2729 int i;
2730 int rv = VNET_API_ERROR_NO_SUCH_FIB;
2731 u32 target_fib_id = ntohl (mp->vrf_id);
2732
Ole Troan3288ed72017-12-06 17:00:05 +01002733 stats_dslock_with_hint (1 /* release hint */ , 9 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002734
2735 /* *INDENT-OFF* */
2736 pool_foreach (fib_table, im6->fibs,
2737 ({
2738 vnet_sw_interface_t * si;
2739
2740 fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
2741
2742 if (fib->table_id != target_fib_id)
2743 continue;
2744
2745 vec_reset_length (sw_if_indices_to_shut);
2746
2747 /* Set the flow hash for this fib to the default */
2748 vnet_set_ip6_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2749
2750 /* Shut down interfaces in this FIB / clean out intfc routes */
2751 pool_foreach (si, im->sw_interfaces,
2752 ({
2753 if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
2754 fib->index)
2755 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2756 }));
2757
2758 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2759 sw_if_index = sw_if_indices_to_shut[i];
2760
2761 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2762 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2763 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2764 }
2765
2766 fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
2767
2768 rv = 0;
2769 break;
2770 })); /* pool_foreach (fib) */
2771 /* *INDENT-ON* */
2772
Ole Troan3288ed72017-12-06 17:00:05 +01002773 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002774 return rv;
2775}
2776
2777static void
2778vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2779{
2780 int rv;
2781 vl_api_reset_fib_reply_t *rmp;
2782
2783 if (mp->is_ipv6)
2784 rv = ip6_reset_fib_t_handler (mp);
2785 else
2786 rv = ip4_reset_fib_t_handler (mp);
2787
2788 REPLY_MACRO (VL_API_RESET_FIB_REPLY);
2789}
2790
2791static void
2792vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
2793{
2794 int rv;
2795 vl_api_set_arp_neighbor_limit_reply_t *rmp;
2796 vnet_main_t *vnm = vnet_get_main ();
2797 clib_error_t *error;
2798
2799 vnm->api_errno = 0;
2800
2801 if (mp->is_ipv6)
2802 error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
2803 else
2804 error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
2805
2806 if (error)
2807 {
2808 clib_error_report (error);
2809 rv = VNET_API_ERROR_UNSPECIFIED;
2810 }
2811 else
2812 {
2813 rv = vnm->api_errno;
2814 }
2815
2816 REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
2817}
2818
Dave Barachb5e8a772016-12-06 12:04:42 -05002819#define vl_msg_name_crc_list
2820#include <vnet/ip/ip.api.h>
2821#undef vl_msg_name_crc_list
2822
2823static void
2824setup_message_id_table (api_main_t * am)
2825{
2826#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
2827 foreach_vl_msg_name_crc_ip;
2828#undef _
2829}
2830
2831static clib_error_t *
2832ip_api_hookup (vlib_main_t * vm)
2833{
2834 api_main_t *am = &api_main;
2835
2836#define _(N,n) \
2837 vl_msg_api_set_handlers(VL_API_##N, #n, \
2838 vl_api_##n##_t_handler, \
2839 vl_noop_handler, \
2840 vl_api_##n##_t_endian, \
2841 vl_api_##n##_t_print, \
2842 sizeof(vl_api_##n##_t), 1);
2843 foreach_ip_api_msg;
2844#undef _
2845
2846 /*
2847 * Set up the (msg_name, crc, message-id) table
2848 */
2849 setup_message_id_table (am);
2850
2851 return 0;
2852}
2853
2854VLIB_API_INIT_FUNCTION (ip_api_hookup);
2855
2856/*
2857 * fd.io coding-style-patch-verification: ON
2858 *
2859 * Local Variables:
2860 * eval: (c-set-style "gnu")
2861 * End:
2862 */