blob: 599a66b367206ea47a551fee9e6c9384222a7d76 [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,
110 u8 * ip_address,
111 unix_shared_memory_queue_t * q, u32 context)
112{
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
125 vl_msg_api_send_shmem (q, (u8 *) & mp);
126}
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{
131 unix_shared_memory_queue_t *q;
132
133 q = vl_api_client_index_to_input_queue (mp->client_index);
134 if (q == 0)
135 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),
152 q, 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),
169 q, mp->context);
170 }
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,
205 unix_shared_memory_queue_t * q,
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
268 vl_msg_api_send_shmem (q, (u8 *) & mp);
269}
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
276static int
277vl_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
283 return (1);
284}
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;
290 unix_shared_memory_queue_t *q;
291 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
301 q = vl_api_client_index_to_input_queue (mp->client_index);
302 if (q == 0)
303 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);
Neale Ranns2297af02017-09-12 09:45:04 -0700324 send_ip_fib_details (am, q, 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,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500333 unix_shared_memory_queue_t * q,
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
396 vl_msg_api_send_shmem (q, (u8 *) & mp);
397}
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
Dave Barachd7cb1b52016-12-09 09:52:16 -0500417api_ip6_fib_table_get_all (unix_shared_memory_queue_t * q,
418 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);
Neale Rannsa161a6d2017-11-14 08:10:41 -0800442 send_ip6_fib_details (am, q, 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{
452 unix_shared_memory_queue_t *q;
453 ip6_main_t *im6 = &ip6_main;
454 fib_table_t *fib_table;
455
456 q = vl_api_client_index_to_input_queue (mp->client_index);
457 if (q == 0)
458 return;
459
460 /* *INDENT-OFF* */
461 pool_foreach (fib_table, im6->fibs,
462 ({
463 api_ip6_fib_table_get_all(q, mp, fib_table);
464 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500465 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500466}
467
468static void
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800469send_ip_mfib_details (unix_shared_memory_queue_t * q,
470 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
513 vl_msg_api_send_shmem (q, (u8 *) & mp);
514}
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{
Neale Ranns5a8123b2017-01-26 01:18:23 -0800534 unix_shared_memory_queue_t *q;
535 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
542 q = vl_api_client_index_to_input_queue (mp->client_index);
543 if (q == 0)
544 return;
545
546
547 /* *INDENT-OFF* */
548 pool_foreach (mfib_table, im->mfibs,
549 ({
550 ip4_mfib_table_walk(&mfib_table->v4,
551 vl_api_ip_mfib_table_dump_walk,
552 &ctx);
553
554 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
555
556 vec_foreach (mfeip, ctx.entries)
557 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800558 send_ip_mfib_details (q, mp->context,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800559 mfib_table->mft_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800560 *mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800561 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800562 vec_reset_length (ctx.entries);
563
564 }));
565 /* *INDENT-ON* */
566
567 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800568}
569
570static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800571send_ip6_mfib_details (vpe_api_main_t * am,
572 unix_shared_memory_queue_t * q,
573 u32 table_id,
574 mfib_prefix_t * pfx,
575 fib_route_path_encode_t * api_rpaths, u32 context)
576{
577 vl_api_ip6_mfib_details_t *mp;
578 fib_route_path_encode_t *api_rpath;
579 vl_api_fib_path_t *fp;
580 int path_count;
581
582 path_count = vec_len (api_rpaths);
583 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
584 if (!mp)
585 return;
586 memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700587 mp->_vl_msg_id = ntohs (VL_API_IP6_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800588 mp->context = context;
589
590 mp->table_id = htonl (table_id);
591 mp->address_length = pfx->fp_len;
592 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip6,
593 sizeof (pfx->fp_grp_addr.ip6));
594 memcpy (mp->src_address, &pfx->fp_src_addr.ip6,
595 sizeof (pfx->fp_src_addr.ip6));
596
597 mp->count = htonl (path_count);
598 fp = mp->path;
599 vec_foreach (api_rpath, api_rpaths)
600 {
601 memset (fp, 0, sizeof (*fp));
602
603 fp->weight = 0;
604 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
605 copy_fib_next_hop (api_rpath, fp);
606 fp++;
607 }
608
609 vl_msg_api_send_shmem (q, (u8 *) & mp);
610}
611
612typedef struct vl_api_ip6_mfib_dump_ctc_t_
613{
614 fib_node_index_t *entries;
615} vl_api_ip6_mfib_dump_ctc_t;
616
617static int
618vl_api_ip6_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
619{
620 vl_api_ip6_mfib_dump_ctc_t *ctx = arg;
621
622 vec_add1 (ctx->entries, fei);
623
624 return (0);
625}
626
627static void
628vl_api_ip6_mfib_dump_t_handler (vl_api_ip6_mfib_dump_t * mp)
629{
630 vpe_api_main_t *am = &vpe_api_main;
631 unix_shared_memory_queue_t *q;
632 ip6_main_t *im = &ip6_main;
633 mfib_table_t *mfib_table;
634 fib_node_index_t *mfeip;
635 mfib_prefix_t pfx;
636 fib_route_path_encode_t *api_rpaths = NULL;
637 vl_api_ip6_mfib_dump_ctc_t ctx = {
638 .entries = NULL,
639 };
640
641 q = vl_api_client_index_to_input_queue (mp->client_index);
642 if (q == 0)
643 return;
644
645
646 /* *INDENT-OFF* */
647 pool_foreach (mfib_table, im->mfibs,
648 ({
649 ip6_mfib_table_walk(&mfib_table->v6,
650 vl_api_ip6_mfib_table_dump_walk,
651 &ctx);
652
653 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
654
655 vec_foreach(mfeip, ctx.entries)
656 {
657 mfib_entry_get_prefix (*mfeip, &pfx);
658 mfib_entry_encode (*mfeip, &api_rpaths);
659 send_ip6_mfib_details (am, q,
660 mfib_table->mft_table_id,
661 &pfx, api_rpaths,
662 mp->context);
663 }
664 vec_reset_length (api_rpaths);
665 vec_reset_length (ctx.entries);
666
667 }));
668 /* *INDENT-ON* */
669
670 vec_free (ctx.entries);
671 vec_free (api_rpaths);
672}
673
674static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700675vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
676 vlib_main_t * vm)
677{
678 vl_api_ip_punt_police_reply_t *rmp;
679 int rv = 0;
680
681 if (mp->is_ip6)
682 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
683 else
684 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
685
686 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
687}
688
689static void
690vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
691 vlib_main_t * vm)
692{
693 vl_api_ip_punt_redirect_reply_t *rmp;
694 int rv = 0;
695
696 if (mp->is_add)
697 {
698 ip46_address_t nh;
699
700 memset (&nh, 0, sizeof (nh));
701
702 if (mp->is_ip6)
703 {
704 memcpy (&nh.ip6, mp->nh, sizeof (nh.ip6));
705
706 ip6_punt_redirect_add (ntohl (mp->rx_sw_if_index),
707 ntohl (mp->tx_sw_if_index), &nh);
708 }
709 else
710 {
711 memcpy (&nh.ip4, mp->nh, sizeof (nh.ip4));
712
713 ip4_punt_redirect_add (ntohl (mp->rx_sw_if_index),
714 ntohl (mp->tx_sw_if_index), &nh);
715 }
716 }
717 else
718 {
719 if (mp->is_ip6)
720 {
721 ip6_punt_redirect_del (ntohl (mp->rx_sw_if_index));
722 }
723 else
724 {
725 ip4_punt_redirect_del (ntohl (mp->rx_sw_if_index));
726 }
727 }
728
729 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
730}
731
732static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500733vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
734 vlib_main_t * vm)
735{
736 vl_api_ip_neighbor_add_del_reply_t *rmp;
737 vnet_main_t *vnm = vnet_get_main ();
738 int rv = 0;
739
740 VALIDATE_SW_IF_INDEX (mp);
741
742 stats_dslock_with_hint (1 /* release hint */ , 7 /* tag */ );
743
744 /*
745 * there's no validation here of the ND/ARP entry being added.
746 * The expectation is that the FIB will ensure that nothing bad
747 * will come of adding bogus entries.
748 */
749 if (mp->is_ipv6)
750 {
751 if (mp->is_add)
752 rv = vnet_set_ip6_ethernet_neighbor
753 (vm, ntohl (mp->sw_if_index),
754 (ip6_address_t *) (mp->dst_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800755 mp->mac_address, sizeof (mp->mac_address), mp->is_static,
756 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500757 else
758 rv = vnet_unset_ip6_ethernet_neighbor
759 (vm, ntohl (mp->sw_if_index),
760 (ip6_address_t *) (mp->dst_address),
761 mp->mac_address, sizeof (mp->mac_address));
762 }
763 else
764 {
765 ethernet_arp_ip4_over_ethernet_address_t a;
766
767 clib_memcpy (&a.ethernet, mp->mac_address, 6);
768 clib_memcpy (&a.ip4, mp->dst_address, 4);
769
770 if (mp->is_add)
771 rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800772 &a, mp->is_static,
773 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500774 else
775 rv =
776 vnet_arp_unset_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index), &a);
777 }
778
Dave Barachb5e8a772016-12-06 12:04:42 -0500779 stats_dsunlock ();
Neale Ranns8edad032017-10-09 05:26:13 -0700780
781 BAD_SW_IF_INDEX_LABEL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500782 REPLY_MACRO (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
783}
784
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700785void
Neale Ranns15002542017-09-10 04:39:11 -0700786ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
787{
788 u32 fib_index, mfib_index;
789
790 /*
791 * ignore action on the default table - this is always present
792 * and cannot be added nor deleted from the API
793 */
794 if (0 != table_id)
795 {
796 /*
797 * The API holds only one lock on the table.
798 * i.e. it can be added many times via the API but needs to be
799 * deleted only once.
800 * The FIB index for unicast and multicast is not necessarily the
801 * same, since internal VPP systesm (like LISP and SR) create
802 * their own unicast tables.
803 */
804 fib_index = fib_table_find (fproto, table_id);
805 mfib_index = mfib_table_find (fproto, table_id);
806
807 if (~0 != fib_index)
808 {
809 fib_table_unlock (fib_index, fproto,
810 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
811 }
812 if (~0 != mfib_index)
813 {
814 mfib_table_unlock (mfib_index, fproto,
815 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
816 }
817 }
818}
819
820void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700821vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
822{
823 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700824 fib_protocol_t fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
825 u32 table_id = ntohl (mp->table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700826 int rv = 0;
827
Neale Ranns15002542017-09-10 04:39:11 -0700828 if (mp->is_add)
829 {
Neale Ranns2297af02017-09-12 09:45:04 -0700830 ip_table_create (fproto, table_id, 1, mp->name);
Neale Ranns15002542017-09-10 04:39:11 -0700831 }
832 else
833 {
834 ip_table_delete (fproto, table_id, 1);
835 }
836
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700837 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
838}
839
Dave Barachb5e8a772016-12-06 12:04:42 -0500840int
841add_del_route_t_handler (u8 is_multipath,
842 u8 is_add,
843 u8 is_drop,
844 u8 is_unreach,
845 u8 is_prohibit,
846 u8 is_local,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800847 u8 is_multicast,
Dave Barachb5e8a772016-12-06 12:04:42 -0500848 u8 is_classify,
849 u32 classify_table_index,
850 u8 is_resolve_host,
851 u8 is_resolve_attached,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800852 u8 is_interface_rx,
853 u8 is_rpf_id,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800854 u8 is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -0700855 u8 is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800856 u8 is_udp_encap,
Dave Barachb5e8a772016-12-06 12:04:42 -0500857 u32 fib_index,
858 const fib_prefix_t * prefix,
Neale Rannsda78f952017-05-24 09:15:43 -0700859 dpo_proto_t next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500860 const ip46_address_t * next_hop,
Neale Ranns810086d2017-11-05 16:26:46 -0800861 u32 next_hop_id,
Dave Barachb5e8a772016-12-06 12:04:42 -0500862 u32 next_hop_sw_if_index,
863 u8 next_hop_fib_index,
Neale Ranns57b58602017-07-15 07:37:25 -0700864 u16 next_hop_weight,
865 u16 next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500866 mpls_label_t next_hop_via_label,
867 mpls_label_t * next_hop_out_label_stack)
868{
869 vnet_classify_main_t *cm = &vnet_classify_main;
870 fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
871 fib_route_path_t path = {
Neale Rannsda78f952017-05-24 09:15:43 -0700872 .frp_proto = next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500873 .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
874 .frp_sw_if_index = next_hop_sw_if_index,
875 .frp_fib_index = next_hop_fib_index,
876 .frp_weight = next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -0700877 .frp_preference = next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500878 .frp_label_stack = next_hop_out_label_stack,
879 };
880 fib_route_path_t *paths = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800881 fib_entry_flag_t entry_flags = FIB_ENTRY_FLAG_NONE;
Dave Barachb5e8a772016-12-06 12:04:42 -0500882
Neale Rannscaac3502017-09-18 18:04:10 -0700883 /*
884 * the special INVALID label meams we are not recursing via a
885 * label. Exp-null value is never a valid via-label so that
886 * also means it's not a via-label and means clients that set
887 * it to 0 by default get the expected behaviour
888 */
889 if ((MPLS_LABEL_INVALID != next_hop_via_label) && (0 != next_hop_via_label))
Dave Barachb5e8a772016-12-06 12:04:42 -0500890 {
Neale Rannsda78f952017-05-24 09:15:43 -0700891 path.frp_proto = DPO_PROTO_MPLS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500892 path.frp_local_label = next_hop_via_label;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800893 path.frp_eos = MPLS_NON_EOS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500894 }
Neale Rannsf068c3e2018-01-03 04:18:48 -0800895 if (is_dvr)
896 path_flags |= FIB_ROUTE_PATH_DVR;
Dave Barachb5e8a772016-12-06 12:04:42 -0500897 if (is_resolve_host)
898 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
899 if (is_resolve_attached)
900 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800901 if (is_interface_rx)
902 path_flags |= FIB_ROUTE_PATH_INTF_RX;
903 if (is_rpf_id)
904 path_flags |= FIB_ROUTE_PATH_RPF_ID;
Neale Ranns054c03a2017-10-13 05:15:07 -0700905 if (is_source_lookup)
906 path_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800907 if (is_multicast)
908 entry_flags |= FIB_ENTRY_FLAG_MULTICAST;
Neale Ranns810086d2017-11-05 16:26:46 -0800909 if (is_udp_encap)
910 {
911 path_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
912 path.frp_udp_encap_id = next_hop_id;
913 }
Florin Coras79ae2d32017-12-16 08:31:06 -0800914 if (path.frp_sw_if_index == ~0 && ip46_address_is_zero (&path.frp_addr)
915 && path.frp_fib_index != ~0)
916 {
917 path_flags |= FIB_ROUTE_PATH_DEAG;
918 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500919
920 path.frp_flags = path_flags;
921
922 if (is_multipath)
923 {
924 stats_dslock_with_hint (1 /* release hint */ , 10 /* tag */ );
925
926
927 vec_add1 (paths, path);
928
929 if (is_add)
930 fib_table_entry_path_add2 (fib_index,
931 prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800932 FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500933 else
934 fib_table_entry_path_remove2 (fib_index,
935 prefix, FIB_SOURCE_API, paths);
936
937 vec_free (paths);
938 stats_dsunlock ();
939 return 0;
940 }
941
942 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
943
944 if (is_drop || is_local || is_classify || is_unreach || is_prohibit)
945 {
946 /*
947 * special route types that link directly to the adj
948 */
949 if (is_add)
950 {
951 dpo_id_t dpo = DPO_INVALID;
952 dpo_proto_t dproto;
953
954 dproto = fib_proto_to_dpo (prefix->fp_proto);
955
956 if (is_drop)
957 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
958 else if (is_local)
959 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
960 else if (is_unreach)
961 ip_null_dpo_add_and_lock (dproto,
962 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
963 else if (is_prohibit)
964 ip_null_dpo_add_and_lock (dproto,
965 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
966 &dpo);
967 else if (is_classify)
968 {
969 if (pool_is_free_index (cm->tables,
970 ntohl (classify_table_index)))
971 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500972 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500973 return VNET_API_ERROR_NO_SUCH_TABLE;
974 }
975
976 dpo_set (&dpo, DPO_CLASSIFY, dproto,
977 classify_dpo_create (dproto,
978 ntohl (classify_table_index)));
979 }
980 else
981 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500982 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500983 return VNET_API_ERROR_NO_SUCH_TABLE;
984 }
985
986 fib_table_entry_special_dpo_update (fib_index,
987 prefix,
988 FIB_SOURCE_API,
989 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
990 dpo_reset (&dpo);
991 }
992 else
993 {
994 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
995 }
996 }
997 else
998 {
999 if (is_add)
1000 {
1001 vec_add1 (paths, path);
1002 fib_table_entry_update (fib_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001003 prefix, FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -05001004 vec_free (paths);
1005 }
1006 else
1007 {
1008 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
1009 }
1010 }
1011
Dave Barachd7cb1b52016-12-09 09:52:16 -05001012 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -05001013 return (0);
1014}
1015
1016int
1017add_del_route_check (fib_protocol_t table_proto,
1018 u32 table_id,
1019 u32 next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001020 dpo_proto_t next_hop_table_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -05001021 u32 next_hop_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001022 u8 is_rpf_id, u32 * fib_index, u32 * next_hop_fib_index)
Dave Barachb5e8a772016-12-06 12:04:42 -05001023{
1024 vnet_main_t *vnm = vnet_get_main ();
1025
Florin Corasd0a59722017-10-15 17:41:21 +00001026 /* Temporaray whilst I do the CSIT dance */
1027 u8 create_missing_tables = 1;
1028
Dave Barachb5e8a772016-12-06 12:04:42 -05001029 *fib_index = fib_table_find (table_proto, ntohl (table_id));
1030 if (~0 == *fib_index)
1031 {
Florin Corasd0a59722017-10-15 17:41:21 +00001032 if (create_missing_tables)
1033 {
1034 *fib_index = fib_table_find_or_create_and_lock (table_proto,
1035 ntohl (table_id),
1036 FIB_SOURCE_API);
1037 }
1038 else
1039 {
1040 /* No such VRF, and we weren't asked to create one */
1041 return VNET_API_ERROR_NO_SUCH_FIB;
1042 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001043 }
1044
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001045 if (!is_rpf_id && ~0 != ntohl (next_hop_sw_if_index))
Dave Barachb5e8a772016-12-06 12:04:42 -05001046 {
1047 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1048 ntohl (next_hop_sw_if_index)))
1049 {
1050 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1051 }
1052 }
1053 else
1054 {
Neale Rannsda78f952017-05-24 09:15:43 -07001055 fib_protocol_t fib_nh_proto;
1056
1057 if (next_hop_table_proto > DPO_PROTO_MPLS)
1058 return (0);
1059
1060 fib_nh_proto = dpo_proto_to_fib (next_hop_table_proto);
1061
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001062 if (is_rpf_id)
Neale Rannsda78f952017-05-24 09:15:43 -07001063 *next_hop_fib_index = mfib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001064 ntohl (next_hop_table_id));
1065 else
Neale Rannsda78f952017-05-24 09:15:43 -07001066 *next_hop_fib_index = fib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001067 ntohl (next_hop_table_id));
Dave Barachb5e8a772016-12-06 12:04:42 -05001068
1069 if (~0 == *next_hop_fib_index)
1070 {
Florin Corasd0a59722017-10-15 17:41:21 +00001071 if (create_missing_tables)
1072 {
1073 if (is_rpf_id)
1074 *next_hop_fib_index =
1075 mfib_table_find_or_create_and_lock (fib_nh_proto,
1076 ntohl
1077 (next_hop_table_id),
1078 MFIB_SOURCE_API);
1079 else
1080 *next_hop_fib_index =
1081 fib_table_find_or_create_and_lock (fib_nh_proto,
1082 ntohl
1083 (next_hop_table_id),
1084 FIB_SOURCE_API);
1085 }
1086 else
1087 {
1088 /* No such VRF, and we weren't asked to create one */
1089 return VNET_API_ERROR_NO_SUCH_FIB;
1090 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001091 }
1092 }
1093
1094 return (0);
1095}
1096
1097static int
1098ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1099{
1100 u32 fib_index, next_hop_fib_index;
1101 mpls_label_t *label_stack = NULL;
1102 int rv, ii, n_labels;;
1103
1104 rv = add_del_route_check (FIB_PROTOCOL_IP4,
1105 mp->table_id,
1106 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001107 DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -05001108 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001109 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001110
1111 if (0 != rv)
1112 return (rv);
1113
1114 fib_prefix_t pfx = {
1115 .fp_len = mp->dst_address_length,
1116 .fp_proto = FIB_PROTOCOL_IP4,
1117 };
1118 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
1119
1120 ip46_address_t nh;
1121 memset (&nh, 0, sizeof (nh));
1122 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
1123
1124 n_labels = mp->next_hop_n_out_labels;
1125 if (n_labels == 0)
1126 ;
1127 else if (1 == n_labels)
1128 vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
1129 else
1130 {
1131 vec_validate (label_stack, n_labels - 1);
1132 for (ii = 0; ii < n_labels; ii++)
1133 label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
1134 }
1135
1136 return (add_del_route_t_handler (mp->is_multipath,
1137 mp->is_add,
1138 mp->is_drop,
1139 mp->is_unreach,
1140 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001141 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001142 mp->is_classify,
1143 mp->classify_table_index,
1144 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001145 mp->is_resolve_attached, 0, 0,
Neale Rannsf068c3e2018-01-03 04:18:48 -08001146 mp->is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -07001147 mp->is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -08001148 mp->is_udp_encap,
Neale Rannsda78f952017-05-24 09:15:43 -07001149 fib_index, &pfx, DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -05001150 &nh,
Neale Ranns810086d2017-11-05 16:26:46 -08001151 ntohl (mp->next_hop_id),
Dave Barachb5e8a772016-12-06 12:04:42 -05001152 ntohl (mp->next_hop_sw_if_index),
1153 next_hop_fib_index,
1154 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001155 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001156 ntohl (mp->next_hop_via_label),
1157 label_stack));
1158}
1159
1160static int
1161ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1162{
1163 u32 fib_index, next_hop_fib_index;
1164 mpls_label_t *label_stack = NULL;
1165 int rv, ii, n_labels;;
1166
1167 rv = add_del_route_check (FIB_PROTOCOL_IP6,
1168 mp->table_id,
1169 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001170 DPO_PROTO_IP6,
Dave Barachb5e8a772016-12-06 12:04:42 -05001171 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001172 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001173
1174 if (0 != rv)
1175 return (rv);
1176
1177 fib_prefix_t pfx = {
1178 .fp_len = mp->dst_address_length,
1179 .fp_proto = FIB_PROTOCOL_IP6,
1180 };
1181 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1182
1183 ip46_address_t nh;
1184 memset (&nh, 0, sizeof (nh));
1185 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1186
1187 n_labels = mp->next_hop_n_out_labels;
1188 if (n_labels == 0)
1189 ;
1190 else if (1 == n_labels)
1191 vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
1192 else
1193 {
1194 vec_validate (label_stack, n_labels - 1);
1195 for (ii = 0; ii < n_labels; ii++)
1196 label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
1197 }
1198
1199 return (add_del_route_t_handler (mp->is_multipath,
1200 mp->is_add,
1201 mp->is_drop,
1202 mp->is_unreach,
1203 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001204 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001205 mp->is_classify,
1206 mp->classify_table_index,
1207 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001208 mp->is_resolve_attached, 0, 0,
Neale Rannsf068c3e2018-01-03 04:18:48 -08001209 mp->is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -07001210 mp->is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -08001211 mp->is_udp_encap,
Neale Rannsda78f952017-05-24 09:15:43 -07001212 fib_index, &pfx, DPO_PROTO_IP6,
Neale Ranns810086d2017-11-05 16:26:46 -08001213 &nh, ntohl (mp->next_hop_id),
1214 ntohl (mp->next_hop_sw_if_index),
Dave Barachb5e8a772016-12-06 12:04:42 -05001215 next_hop_fib_index,
1216 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001217 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001218 ntohl (mp->next_hop_via_label),
1219 label_stack));
1220}
1221
1222void
1223vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1224{
1225 vl_api_ip_add_del_route_reply_t *rmp;
1226 int rv;
1227 vnet_main_t *vnm = vnet_get_main ();
1228
1229 vnm->api_errno = 0;
1230
1231 if (mp->is_ipv6)
1232 rv = ip6_add_del_route_t_handler (mp);
1233 else
1234 rv = ip4_add_del_route_t_handler (mp);
1235
1236 rv = (rv == 0) ? vnm->api_errno : rv;
1237
1238 REPLY_MACRO (VL_API_IP_ADD_DEL_ROUTE_REPLY);
1239}
1240
Neale Ranns15002542017-09-10 04:39:11 -07001241void
Neale Ranns2297af02017-09-12 09:45:04 -07001242ip_table_create (fib_protocol_t fproto,
1243 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -07001244{
1245 u32 fib_index, mfib_index;
1246
1247 /*
1248 * ignore action on the default table - this is always present
1249 * and cannot be added nor deleted from the API
1250 */
1251 if (0 != table_id)
1252 {
1253 /*
1254 * The API holds only one lock on the table.
1255 * i.e. it can be added many times via the API but needs to be
1256 * deleted only once.
1257 * The FIB index for unicast and multicast is not necessarily the
1258 * same, since internal VPP systesm (like LISP and SR) create
1259 * their own unicast tables.
1260 */
1261 fib_index = fib_table_find (fproto, table_id);
1262 mfib_index = mfib_table_find (fproto, table_id);
1263
1264 if (~0 == fib_index)
1265 {
Neale Ranns2297af02017-09-12 09:45:04 -07001266 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
1267 (is_api ?
1268 FIB_SOURCE_API :
1269 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001270 }
1271 if (~0 == mfib_index)
1272 {
Neale Ranns2297af02017-09-12 09:45:04 -07001273 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
1274 (is_api ?
1275 MFIB_SOURCE_API :
1276 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001277 }
1278 }
1279}
1280
Neale Ranns32e1c012016-11-22 17:07:28 +00001281static int
1282add_del_mroute_check (fib_protocol_t table_proto,
1283 u32 table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001284 u32 next_hop_sw_if_index, u8 is_local, u32 * fib_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001285{
1286 vnet_main_t *vnm = vnet_get_main ();
1287
1288 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1289 if (~0 == *fib_index)
1290 {
Neale Ranns15002542017-09-10 04:39:11 -07001291 /* No such table */
1292 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Ranns32e1c012016-11-22 17:07:28 +00001293 }
1294
1295 if (~0 != ntohl (next_hop_sw_if_index))
1296 {
1297 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1298 ntohl (next_hop_sw_if_index)))
1299 {
1300 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1301 }
1302 }
1303
1304 return (0);
1305}
1306
1307static int
1308mroute_add_del_handler (u8 is_add,
1309 u8 is_local,
1310 u32 fib_index,
1311 const mfib_prefix_t * prefix,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001312 dpo_proto_t nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001313 u32 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001314 fib_rpf_id_t rpf_id,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001315 u32 next_hop_sw_if_index, u32 itf_flags, u32 bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +00001316{
1317 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1318
1319 fib_route_path_t path = {
1320 .frp_sw_if_index = next_hop_sw_if_index,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001321 .frp_proto = nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001322 };
1323
1324 if (is_local)
1325 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1326
Neale Rannsd792d9c2017-10-21 10:53:20 -07001327 if (DPO_PROTO_BIER == nh_proto)
1328 {
1329 path.frp_bier_imp = bier_imp;
1330 path.frp_flags = FIB_ROUTE_PATH_BIER_IMP;
1331 }
1332 else if (!is_local && ~0 == next_hop_sw_if_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001333 {
1334 mfib_table_entry_update (fib_index, prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001335 MFIB_SOURCE_API, rpf_id, entry_flags);
Neale Rannsd792d9c2017-10-21 10:53:20 -07001336 goto done;
1337 }
1338
1339 if (is_add)
1340 {
1341 mfib_table_entry_path_update (fib_index, prefix,
1342 MFIB_SOURCE_API, &path, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001343 }
1344 else
1345 {
Neale Rannsd792d9c2017-10-21 10:53:20 -07001346 mfib_table_entry_path_remove (fib_index, prefix,
1347 MFIB_SOURCE_API, &path);
Neale Ranns32e1c012016-11-22 17:07:28 +00001348 }
1349
Neale Rannsd792d9c2017-10-21 10:53:20 -07001350done:
Neale Ranns32e1c012016-11-22 17:07:28 +00001351 stats_dsunlock ();
1352 return (0);
1353}
1354
1355static int
1356api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1357{
1358 fib_protocol_t fproto;
Neale Rannsd792d9c2017-10-21 10:53:20 -07001359 dpo_proto_t nh_proto;
Neale Ranns32e1c012016-11-22 17:07:28 +00001360 u32 fib_index;
1361 int rv;
1362
Neale Rannsd792d9c2017-10-21 10:53:20 -07001363 nh_proto = mp->next_hop_afi;
Neale Ranns32e1c012016-11-22 17:07:28 +00001364 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1365 rv = add_del_mroute_check (fproto,
1366 mp->table_id,
1367 mp->next_hop_sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -07001368 mp->is_local, &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001369
1370 if (0 != rv)
1371 return (rv);
1372
1373 mfib_prefix_t pfx = {
1374 .fp_len = ntohs (mp->grp_address_length),
1375 .fp_proto = fproto,
1376 };
1377
1378 if (FIB_PROTOCOL_IP4 == fproto)
1379 {
1380 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1381 sizeof (pfx.fp_grp_addr.ip4));
1382 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1383 sizeof (pfx.fp_src_addr.ip4));
1384 }
1385 else
1386 {
1387 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1388 sizeof (pfx.fp_grp_addr.ip6));
1389 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1390 sizeof (pfx.fp_src_addr.ip6));
1391 }
1392
1393 return (mroute_add_del_handler (mp->is_add,
1394 mp->is_local,
1395 fib_index, &pfx,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001396 nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001397 ntohl (mp->entry_flags),
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001398 ntohl (mp->rpf_id),
Neale Ranns32e1c012016-11-22 17:07:28 +00001399 ntohl (mp->next_hop_sw_if_index),
Neale Rannsd792d9c2017-10-21 10:53:20 -07001400 ntohl (mp->itf_flags),
1401 ntohl (mp->bier_imp)));
Neale Ranns32e1c012016-11-22 17:07:28 +00001402}
1403
1404void
1405vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1406{
1407 vl_api_ip_mroute_add_del_reply_t *rmp;
1408 int rv;
1409 vnet_main_t *vnm = vnet_get_main ();
1410
1411 vnm->api_errno = 0;
1412
1413 rv = api_mroute_add_del_t_handler (mp);
1414
1415 rv = (rv == 0) ? vnm->api_errno : rv;
1416
1417 REPLY_MACRO (VL_API_IP_MROUTE_ADD_DEL_REPLY);
1418}
1419
Dave Barachb5e8a772016-12-06 12:04:42 -05001420static void
1421send_ip_details (vpe_api_main_t * am,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001422 unix_shared_memory_queue_t * q, u32 sw_if_index,
1423 u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001424{
1425 vl_api_ip_details_t *mp;
1426
1427 mp = vl_msg_api_alloc (sizeof (*mp));
1428 memset (mp, 0, sizeof (*mp));
1429 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1430
1431 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001432 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001433 mp->context = context;
1434
1435 vl_msg_api_send_shmem (q, (u8 *) & mp);
1436}
1437
1438static void
1439send_ip_address_details (vpe_api_main_t * am,
1440 unix_shared_memory_queue_t * q,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001441 u8 * ip, u16 prefix_length,
1442 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001443{
1444 vl_api_ip_address_details_t *mp;
1445
1446 mp = vl_msg_api_alloc (sizeof (*mp));
1447 memset (mp, 0, sizeof (*mp));
1448 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1449
1450 if (is_ipv6)
1451 {
1452 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1453 }
1454 else
1455 {
1456 u32 *tp = (u32 *) mp->ip;
1457 *tp = *(u32 *) ip;
1458 }
1459 mp->prefix_length = prefix_length;
1460 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001461 mp->sw_if_index = htonl (sw_if_index);
1462 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001463
1464 vl_msg_api_send_shmem (q, (u8 *) & mp);
1465}
1466
1467static void
1468vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1469{
1470 vpe_api_main_t *am = &vpe_api_main;
1471 unix_shared_memory_queue_t *q;
1472 ip6_address_t *r6;
1473 ip4_address_t *r4;
1474 ip6_main_t *im6 = &ip6_main;
1475 ip4_main_t *im4 = &ip4_main;
1476 ip_lookup_main_t *lm6 = &im6->lookup_main;
1477 ip_lookup_main_t *lm4 = &im4->lookup_main;
1478 ip_interface_address_t *ia = 0;
1479 u32 sw_if_index = ~0;
1480 int rv __attribute__ ((unused)) = 0;
1481
1482 VALIDATE_SW_IF_INDEX (mp);
1483
1484 sw_if_index = ntohl (mp->sw_if_index);
1485
1486 q = vl_api_client_index_to_input_queue (mp->client_index);
1487 if (q == 0)
1488 return;
1489
Dave Barachb5e8a772016-12-06 12:04:42 -05001490 if (mp->is_ipv6)
1491 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001492 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001493 foreach_ip_interface_address (lm6, ia, sw_if_index,
1494 1 /* honor unnumbered */,
1495 ({
1496 r6 = ip_interface_address_get_address (lm6, ia);
1497 u16 prefix_length = ia->address_length;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001498 send_ip_address_details(am, q, (u8*)r6, prefix_length,
1499 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001500 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001501 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001502 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001503 else
1504 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001505 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001506 foreach_ip_interface_address (lm4, ia, sw_if_index,
1507 1 /* honor unnumbered */,
1508 ({
1509 r4 = ip_interface_address_get_address (lm4, ia);
1510 u16 prefix_length = ia->address_length;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001511 send_ip_address_details(am, q, (u8*)r4, prefix_length,
1512 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001513 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001514 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001515 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001516 BAD_SW_IF_INDEX_LABEL;
1517}
1518
1519static void
1520vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1521{
1522 vpe_api_main_t *am = &vpe_api_main;
1523 vnet_main_t *vnm = vnet_get_main ();
1524 vlib_main_t *vm = vlib_get_main ();
1525 vnet_interface_main_t *im = &vnm->interface_main;
1526 unix_shared_memory_queue_t *q;
1527 vnet_sw_interface_t *si, *sorted_sis;
1528 u32 sw_if_index = ~0;
1529
1530 q = vl_api_client_index_to_input_queue (mp->client_index);
1531 if (q == 0)
1532 {
1533 return;
1534 }
1535
1536 /* Gather interfaces. */
1537 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1538 _vec_len (sorted_sis) = 0;
1539 /* *INDENT-OFF* */
1540 pool_foreach (si, im->sw_interfaces,
1541 ({
1542 vec_add1 (sorted_sis, si[0]);
1543 }));
1544 /* *INDENT-ON* */
1545
1546 vec_foreach (si, sorted_sis)
1547 {
1548 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1549 {
1550 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1551 {
1552 continue;
1553 }
1554 sw_if_index = si->sw_if_index;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001555 send_ip_details (am, q, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001556 }
1557 }
1558}
1559
1560static void
1561set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1562{
1563 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001564 int rv;
1565 u32 table_id;
1566 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001567
Neale Ranns227038a2017-04-21 01:07:59 -07001568 table_id = ntohl (mp->vrf_id);
1569
1570#define _(a,b) if (mp->a) flow_hash_config |= b;
1571 foreach_flow_hash_bit;
1572#undef _
1573
1574 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001575
1576 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1577}
1578
1579static void
1580set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1581{
1582 vl_api_set_ip_flow_hash_reply_t *rmp;
1583 int rv;
1584 u32 table_id;
1585 flow_hash_config_t flow_hash_config = 0;
1586
1587 table_id = ntohl (mp->vrf_id);
1588
1589#define _(a,b) if (mp->a) flow_hash_config |= b;
1590 foreach_flow_hash_bit;
1591#undef _
1592
1593 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1594
1595 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1596}
1597
1598
1599static void
1600vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1601{
1602 if (mp->is_ipv6 == 0)
1603 set_ip4_flow_hash (mp);
1604 else
1605 set_ip6_flow_hash (mp);
1606}
1607
1608static void
1609 vl_api_sw_interface_ip6nd_ra_config_t_handler
1610 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1611{
1612 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1613 vlib_main_t *vm = vlib_get_main ();
1614 int rv = 0;
1615 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1616 default_router;
1617
1618 is_no = mp->is_no == 1;
1619 suppress = mp->suppress == 1;
1620 managed = mp->managed == 1;
1621 other = mp->other == 1;
1622 ll_option = mp->ll_option == 1;
1623 send_unicast = mp->send_unicast == 1;
1624 cease = mp->cease == 1;
1625 default_router = mp->default_router == 1;
1626
1627 VALIDATE_SW_IF_INDEX (mp);
1628
1629 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1630 suppress, managed, other,
1631 ll_option, send_unicast, cease,
1632 default_router, ntohl (mp->lifetime),
1633 ntohl (mp->initial_count),
1634 ntohl (mp->initial_interval),
1635 ntohl (mp->max_interval),
1636 ntohl (mp->min_interval), is_no);
1637
1638 BAD_SW_IF_INDEX_LABEL;
1639
1640 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1641}
1642
1643static void
1644 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1645 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1646{
1647 vlib_main_t *vm = vlib_get_main ();
1648 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1649 int rv = 0;
1650 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1651
1652 VALIDATE_SW_IF_INDEX (mp);
1653
1654 is_no = mp->is_no == 1;
1655 use_default = mp->use_default == 1;
1656 no_advertise = mp->no_advertise == 1;
1657 off_link = mp->off_link == 1;
1658 no_autoconfig = mp->no_autoconfig == 1;
1659 no_onlink = mp->no_onlink == 1;
1660
1661 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1662 (ip6_address_t *) mp->address,
1663 mp->address_length, use_default,
1664 ntohl (mp->val_lifetime),
1665 ntohl (mp->pref_lifetime), no_advertise,
1666 off_link, no_autoconfig, no_onlink, is_no);
1667
1668 BAD_SW_IF_INDEX_LABEL;
1669 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1670}
1671
1672static void
Neale Ranns3f844d02017-02-18 00:03:54 -08001673send_ip6nd_proxy_details (unix_shared_memory_queue_t * q,
1674 u32 context,
1675 const ip46_address_t * addr, u32 sw_if_index)
1676{
1677 vl_api_ip6nd_proxy_details_t *mp;
1678
1679 mp = vl_msg_api_alloc (sizeof (*mp));
1680 memset (mp, 0, sizeof (*mp));
1681 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1682 mp->context = context;
1683 mp->sw_if_index = htonl (sw_if_index);
1684 memcpy (mp->address, addr, 16);
1685
1686 vl_msg_api_send_shmem (q, (u8 *) & mp);
1687}
1688
Neale Ranns3f844d02017-02-18 00:03:54 -08001689typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1690{
1691 u32 *indices;
1692} api_ip6nd_proxy_fib_table_walk_ctx_t;
1693
1694static int
1695api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1696{
1697 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1698
1699 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1700 {
1701 vec_add1 (ctx->indices, fei);
1702 }
1703
1704 return (1);
1705}
1706
1707static void
1708vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1709{
1710 ip6_main_t *im6 = &ip6_main;
1711 fib_table_t *fib_table;
1712 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1713 .indices = NULL,
1714 };
1715 fib_node_index_t *feip;
1716 fib_prefix_t pfx;
1717 unix_shared_memory_queue_t *q;
1718
1719 q = vl_api_client_index_to_input_queue (mp->client_index);
1720 if (q == 0)
1721 {
1722 return;
1723 }
1724
1725 /* *INDENT-OFF* */
1726 pool_foreach (fib_table, im6->fibs,
1727 ({
1728 fib_table_walk(fib_table->ft_index,
1729 FIB_PROTOCOL_IP6,
1730 api_ip6nd_proxy_fib_table_walk,
1731 &ctx);
1732 }));
1733 /* *INDENT-ON* */
1734
1735 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1736
1737 vec_foreach (feip, ctx.indices)
1738 {
1739 fib_entry_get_prefix (*feip, &pfx);
1740
1741 send_ip6nd_proxy_details (q,
1742 mp->context,
1743 &pfx.fp_addr,
1744 fib_entry_get_resolving_interface (*feip));
1745 }
1746
1747 vec_free (ctx.indices);
1748}
1749
1750static void
1751vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1752{
1753 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
1754 int rv = 0;
1755
1756 VALIDATE_SW_IF_INDEX (mp);
1757
1758 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index),
1759 (ip6_address_t *) mp->address, mp->is_del);
1760
1761 BAD_SW_IF_INDEX_LABEL;
1762 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1763}
1764
1765static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001766 vl_api_sw_interface_ip6_enable_disable_t_handler
1767 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1768{
1769 vlib_main_t *vm = vlib_get_main ();
1770 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1771 vnet_main_t *vnm = vnet_get_main ();
1772 int rv = 0;
1773 clib_error_t *error;
1774
1775 vnm->api_errno = 0;
1776
1777 VALIDATE_SW_IF_INDEX (mp);
1778
1779 error =
1780 (mp->enable == 1) ? enable_ip6_interface (vm,
1781 ntohl (mp->sw_if_index)) :
1782 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1783
1784 if (error)
1785 {
1786 clib_error_report (error);
1787 rv = VNET_API_ERROR_UNSPECIFIED;
1788 }
1789 else
1790 {
1791 rv = vnm->api_errno;
1792 }
1793
1794 BAD_SW_IF_INDEX_LABEL;
1795
1796 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1797}
1798
1799static void
1800 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1801 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1802{
1803 vlib_main_t *vm = vlib_get_main ();
1804 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1805 int rv = 0;
1806 clib_error_t *error;
1807 vnet_main_t *vnm = vnet_get_main ();
1808
1809 vnm->api_errno = 0;
1810
1811 VALIDATE_SW_IF_INDEX (mp);
1812
1813 error = set_ip6_link_local_address (vm,
1814 ntohl (mp->sw_if_index),
Neale Ranns75152282017-01-09 01:00:45 -08001815 (ip6_address_t *) mp->address);
Dave Barachb5e8a772016-12-06 12:04:42 -05001816 if (error)
1817 {
1818 clib_error_report (error);
1819 rv = VNET_API_ERROR_UNSPECIFIED;
1820 }
1821 else
1822 {
1823 rv = vnm->api_errno;
1824 }
1825
1826 BAD_SW_IF_INDEX_LABEL;
1827
1828 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1829}
1830
Neale Ranns32e1c012016-11-22 17:07:28 +00001831void
1832vl_mfib_signal_send_one (unix_shared_memory_queue_t * q,
1833 u32 context, const mfib_signal_t * mfs)
1834{
1835 vl_api_mfib_signal_details_t *mp;
1836 mfib_prefix_t prefix;
1837 mfib_table_t *mfib;
1838 mfib_itf_t *mfi;
1839
1840 mp = vl_msg_api_alloc (sizeof (*mp));
1841
1842 memset (mp, 0, sizeof (*mp));
1843 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1844 mp->context = context;
1845
1846 mfi = mfib_itf_get (mfs->mfs_itf);
1847 mfib_entry_get_prefix (mfs->mfs_entry, &prefix);
1848 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
1849 prefix.fp_proto);
1850 mp->table_id = ntohl (mfib->mft_table_id);
1851 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1852
1853 if (FIB_PROTOCOL_IP4 == prefix.fp_proto)
1854 {
1855 mp->grp_address_len = ntohs (prefix.fp_len);
1856
1857 memcpy (mp->grp_address, &prefix.fp_grp_addr.ip4, 4);
1858 if (prefix.fp_len > 32)
1859 {
1860 memcpy (mp->src_address, &prefix.fp_src_addr.ip4, 4);
1861 }
1862 }
1863 else
1864 {
1865 mp->grp_address_len = ntohs (prefix.fp_len);
1866
1867 ASSERT (0);
1868 }
1869
1870 if (0 != mfs->mfs_buffer_len)
1871 {
1872 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1873
1874 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1875 }
1876 else
1877 {
1878 mp->ip_packet_len = 0;
1879 }
1880
1881 vl_msg_api_send_shmem (q, (u8 *) & mp);
1882}
1883
1884static void
1885vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1886{
1887 unix_shared_memory_queue_t *q;
1888
1889 q = vl_api_client_index_to_input_queue (mp->client_index);
1890 if (q == 0)
1891 {
1892 return;
1893 }
1894
1895 while (q->cursize < q->maxsize && mfib_signal_send_one (q, mp->context))
1896 ;
1897}
Dave Barachb5e8a772016-12-06 12:04:42 -05001898
Florin Coras595992c2017-11-06 17:17:08 -08001899static void
1900 vl_api_ip_container_proxy_add_del_t_handler
1901 (vl_api_ip_container_proxy_add_del_t * mp)
1902{
1903 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1904 vnet_ip_container_proxy_args_t args;
1905 int rv = 0;
1906 clib_error_t *error;
1907
1908 memset (&args, 0, sizeof (args));
1909 ip_set (&args.prefix.fp_addr, mp->ip, mp->is_ip4);
1910 args.prefix.fp_len = mp->plen ? mp->plen : (mp->is_ip4 ? 32 : 128);
1911 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1912 args.is_add = mp->is_add;
1913 if ((error = vnet_ip_container_proxy_add_del (&args)))
1914 {
1915 rv = clib_error_get_code (error);
1916 clib_error_report (error);
1917 }
1918
1919 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1920}
1921
Neale Rannsb8d44812017-11-10 06:53:54 -08001922static void
1923vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1924{
1925 int rv = 0;
1926 vl_api_ioam_enable_reply_t *rmp;
1927 clib_error_t *error;
1928
1929 /* Ignoring the profile id as currently a single profile
1930 * is supported */
1931 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1932 mp->seqno, mp->analyse);
1933 if (error)
1934 {
1935 clib_error_report (error);
1936 rv = clib_error_get_code (error);
1937 }
1938
1939 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1940}
1941
1942static void
1943vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1944{
1945 int rv = 0;
1946 vl_api_ioam_disable_reply_t *rmp;
1947 clib_error_t *error;
1948
1949 error = clear_ioam_rewrite_fn ();
1950 if (error)
1951 {
1952 clib_error_report (error);
1953 rv = clib_error_get_code (error);
1954 }
1955
1956 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1957}
1958
1959static void
1960 vl_api_ip_source_and_port_range_check_add_del_t_handler
1961 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1962{
1963 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1964 int rv = 0;
1965
1966 u8 is_ipv6 = mp->is_ipv6;
1967 u8 is_add = mp->is_add;
1968 u8 mask_length = mp->mask_length;
1969 ip4_address_t ip4_addr;
1970 ip6_address_t ip6_addr;
1971 u16 *low_ports = 0;
1972 u16 *high_ports = 0;
1973 u32 vrf_id;
1974 u16 tmp_low, tmp_high;
1975 u8 num_ranges;
1976 int i;
1977
1978 // Validate port range
1979 num_ranges = mp->number_of_ranges;
1980 if (num_ranges > 32)
1981 { // This is size of array in VPE.API
1982 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1983 goto reply;
1984 }
1985
1986 vec_reset_length (low_ports);
1987 vec_reset_length (high_ports);
1988
1989 for (i = 0; i < num_ranges; i++)
1990 {
1991 tmp_low = mp->low_ports[i];
1992 tmp_high = mp->high_ports[i];
1993 // If tmp_low <= tmp_high then only need to check tmp_low = 0
1994 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1995 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1996 {
1997 rv = VNET_API_ERROR_INVALID_VALUE;
1998 goto reply;
1999 }
2000 vec_add1 (low_ports, tmp_low);
2001 vec_add1 (high_ports, tmp_high + 1);
2002 }
2003
2004 // Validate mask_length
2005 if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
2006 {
2007 rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
2008 goto reply;
2009 }
2010
2011 vrf_id = ntohl (mp->vrf_id);
2012
2013 if (vrf_id < 1)
2014 {
2015 rv = VNET_API_ERROR_INVALID_VALUE;
2016 goto reply;
2017 }
2018
2019
2020 if (is_ipv6)
2021 {
2022 clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
2023 rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
2024 mask_length,
2025 vrf_id,
2026 low_ports,
2027 high_ports, is_add);
2028 }
2029 else
2030 {
2031 clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
2032 rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
2033 mask_length,
2034 vrf_id,
2035 low_ports,
2036 high_ports, is_add);
2037 }
2038
2039reply:
2040 vec_free (low_ports);
2041 vec_free (high_ports);
2042 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
2043}
2044
2045static void
2046 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
2047 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
2048{
2049 vlib_main_t *vm = vlib_get_main ();
2050 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
2051 ip4_main_t *im = &ip4_main;
2052 int rv;
2053 u32 sw_if_index;
2054 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2055 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2056 uword *p = 0;
2057 int i;
2058
2059 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
2060 ntohl (mp->tcp_out_vrf_id);
2061 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
2062 ntohl (mp->udp_out_vrf_id);
2063 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
2064 ntohl (mp->tcp_in_vrf_id);
2065 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
2066 ntohl (mp->udp_in_vrf_id);
2067
2068
2069 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
2070 {
2071 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
2072 {
2073 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
2074
2075 if (p == 0)
2076 {
2077 rv = VNET_API_ERROR_INVALID_VALUE;
2078 goto reply;
2079 }
2080
2081 fib_index[i] = p[0];
2082 }
2083 else
2084 fib_index[i] = ~0;
2085 }
2086 sw_if_index = ntohl (mp->sw_if_index);
2087
2088 VALIDATE_SW_IF_INDEX (mp);
2089
2090 rv =
2091 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
2092 mp->is_add);
2093
2094 BAD_SW_IF_INDEX_LABEL;
2095reply:
2096
2097 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
2098}
2099
2100#define IP4_ARP_EVENT 3
2101#define IP6_ND_EVENT 4
2102
2103static int arp_change_delete_callback (u32 pool_index, u8 * notused);
2104static int nd_change_delete_callback (u32 pool_index, u8 * notused);
2105static vlib_node_registration_t ip_resolver_process_node;
2106
2107static void
2108handle_ip4_arp_event (u32 pool_index)
2109{
2110 vpe_api_main_t *vam = &vpe_api_main;
2111 vnet_main_t *vnm = vam->vnet_main;
2112 vlib_main_t *vm = vam->vlib_main;
2113 vl_api_ip4_arp_event_t *event;
2114 vl_api_ip4_arp_event_t *mp;
2115 unix_shared_memory_queue_t *q;
2116
2117 /* Client can cancel, die, etc. */
2118 if (pool_is_free_index (vam->arp_events, pool_index))
2119 return;
2120
2121 event = pool_elt_at_index (vam->arp_events, pool_index);
2122
2123 q = vl_api_client_index_to_input_queue (event->client_index);
2124 if (!q)
2125 {
2126 (void) vnet_add_del_ip4_arp_change_event
2127 (vnm, arp_change_delete_callback,
2128 event->pid, &event->address,
2129 ip_resolver_process_node.index, IP4_ARP_EVENT,
2130 ~0 /* pool index, notused */ , 0 /* is_add */ );
2131 return;
2132 }
2133
2134 if (q->cursize < q->maxsize)
2135 {
2136 mp = vl_msg_api_alloc (sizeof (*mp));
2137 clib_memcpy (mp, event, sizeof (*mp));
2138 vl_msg_api_send_shmem (q, (u8 *) & mp);
2139 }
2140 else
2141 {
2142 static f64 last_time;
2143 /*
2144 * Throttle syslog msgs.
2145 * It's pretty tempting to just revoke the registration...
2146 */
2147 if (vlib_time_now (vm) > last_time + 10.0)
2148 {
2149 clib_warning ("arp event for %U to pid %d: queue stuffed!",
2150 format_ip4_address, &event->address, event->pid);
2151 last_time = vlib_time_now (vm);
2152 }
2153 }
2154}
2155
2156void
2157handle_ip6_nd_event (u32 pool_index)
2158{
2159 vpe_api_main_t *vam = &vpe_api_main;
2160 vnet_main_t *vnm = vam->vnet_main;
2161 vlib_main_t *vm = vam->vlib_main;
2162 vl_api_ip6_nd_event_t *event;
2163 vl_api_ip6_nd_event_t *mp;
2164 unix_shared_memory_queue_t *q;
2165
2166 /* Client can cancel, die, etc. */
2167 if (pool_is_free_index (vam->nd_events, pool_index))
2168 return;
2169
2170 event = pool_elt_at_index (vam->nd_events, pool_index);
2171
2172 q = vl_api_client_index_to_input_queue (event->client_index);
2173 if (!q)
2174 {
2175 (void) vnet_add_del_ip6_nd_change_event
2176 (vnm, nd_change_delete_callback,
2177 event->pid, &event->address,
2178 ip_resolver_process_node.index, IP6_ND_EVENT,
2179 ~0 /* pool index, notused */ , 0 /* is_add */ );
2180 return;
2181 }
2182
2183 if (q->cursize < q->maxsize)
2184 {
2185 mp = vl_msg_api_alloc (sizeof (*mp));
2186 clib_memcpy (mp, event, sizeof (*mp));
2187 vl_msg_api_send_shmem (q, (u8 *) & mp);
2188 }
2189 else
2190 {
2191 static f64 last_time;
2192 /*
2193 * Throttle syslog msgs.
2194 * It's pretty tempting to just revoke the registration...
2195 */
2196 if (vlib_time_now (vm) > last_time + 10.0)
2197 {
2198 clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
2199 format_ip6_address, &event->address, event->pid);
2200 last_time = vlib_time_now (vm);
2201 }
2202 }
2203}
2204
2205static uword
2206resolver_process (vlib_main_t * vm,
2207 vlib_node_runtime_t * rt, vlib_frame_t * f)
2208{
2209 volatile f64 timeout = 100.0;
2210 volatile uword *event_data = 0;
2211
2212 while (1)
2213 {
2214 vlib_process_wait_for_event_or_clock (vm, timeout);
2215
2216 uword event_type =
2217 vlib_process_get_events (vm, (uword **) & event_data);
2218
2219 int i;
2220 switch (event_type)
2221 {
2222 case IP4_ARP_EVENT:
2223 for (i = 0; i < vec_len (event_data); i++)
2224 handle_ip4_arp_event (event_data[i]);
2225 break;
2226
2227 case IP6_ND_EVENT:
2228 for (i = 0; i < vec_len (event_data); i++)
2229 handle_ip6_nd_event (event_data[i]);
2230 break;
2231
2232 case ~0: /* timeout */
2233 break;
2234 }
2235
2236 vec_reset_length (event_data);
2237 }
2238 return 0; /* or not */
2239}
2240
2241/* *INDENT-OFF* */
2242VLIB_REGISTER_NODE (ip_resolver_process_node,static) = {
2243 .function = resolver_process,
2244 .type = VLIB_NODE_TYPE_PROCESS,
2245 .name = "ip-route-resolver-process",
2246};
2247/* *INDENT-ON* */
2248
2249static int
2250nd_change_data_callback (u32 pool_index, u8 * new_mac,
2251 u32 sw_if_index, ip6_address_t * address)
2252{
2253 vpe_api_main_t *am = &vpe_api_main;
2254 vl_api_ip6_nd_event_t *event;
2255
2256 if (pool_is_free_index (am->nd_events, pool_index))
2257 return 1;
2258
2259 event = pool_elt_at_index (am->nd_events, pool_index);
2260 if (eth_mac_equal (event->new_mac, new_mac) &&
2261 sw_if_index == ntohl (event->sw_if_index))
2262 {
2263 return 1;
2264 }
2265
2266 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2267 event->sw_if_index = htonl (sw_if_index);
2268 return 0;
2269}
2270
2271static int
2272arp_change_delete_callback (u32 pool_index, u8 * notused)
2273{
2274 vpe_api_main_t *am = &vpe_api_main;
2275
2276 if (pool_is_free_index (am->arp_events, pool_index))
2277 return 1;
2278
2279 pool_put_index (am->arp_events, pool_index);
2280 return 0;
2281}
2282
2283static int
2284nd_change_delete_callback (u32 pool_index, u8 * notused)
2285{
2286 vpe_api_main_t *am = &vpe_api_main;
2287
2288 if (pool_is_free_index (am->nd_events, pool_index))
2289 return 1;
2290
2291 pool_put_index (am->nd_events, pool_index);
2292 return 0;
2293}
2294
2295static vlib_node_registration_t wc_arp_process_node;
2296
2297enum
2298{ WC_ARP_REPORT, WC_ND_REPORT };
2299
2300static uword
2301wc_arp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
2302{
2303 /* These cross the longjmp boundry (vlib_process_wait_for_event)
2304 * and need to be volatile - to prevent them from being optimized into
2305 * a register - which could change during suspension */
2306
2307 volatile wc_arp_report_t arp_prev = { 0 };
2308 volatile wc_nd_report_t nd_prev = { 0 };
2309 volatile f64 last_arp = vlib_time_now (vm);
2310 volatile f64 last_nd = vlib_time_now (vm);
2311
2312 while (1)
2313 {
2314 vlib_process_wait_for_event (vm);
2315 uword event_type = WC_ARP_REPORT;
2316 void *event_data = vlib_process_get_event_data (vm, &event_type);
2317
2318 f64 now = vlib_time_now (vm);
2319 int i;
2320 if (event_type == WC_ARP_REPORT)
2321 {
2322 wc_arp_report_t *arp_events = event_data;
2323 for (i = 0; i < vec_len (arp_events); i++)
2324 {
2325 /* discard dup event */
2326 if (arp_prev.ip4 == arp_events[i].ip4 &&
2327 eth_mac_equal ((u8 *) arp_prev.mac, arp_events[i].mac) &&
2328 arp_prev.sw_if_index == arp_events[i].sw_if_index &&
2329 (now - last_arp) < 10.0)
2330 {
2331 continue;
2332 }
2333 arp_prev = arp_events[i];
2334 last_arp = now;
2335 vpe_client_registration_t *reg;
2336 /* *INDENT-OFF* */
2337 pool_foreach(reg, vpe_api_main.wc_ip4_arp_events_registrations,
2338 ({
2339 unix_shared_memory_queue_t *q;
2340 q = vl_api_client_index_to_input_queue (reg->client_index);
2341 if (q && q->cursize < q->maxsize)
2342 {
2343 vl_api_ip4_arp_event_t * event = vl_msg_api_alloc (sizeof *event);
2344 memset (event, 0, sizeof *event);
2345 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2346 event->client_index = reg->client_index;
2347 event->pid = reg->client_pid;
2348 event->mac_ip = 1;
2349 event->address = arp_events[i].ip4;
2350 event->sw_if_index = htonl(arp_events[i].sw_if_index);
2351 memcpy(event->new_mac, arp_events[i].mac, sizeof event->new_mac);
2352 vl_msg_api_send_shmem (q, (u8 *) &event);
2353 }
2354 }));
2355 /* *INDENT-ON* */
2356 }
2357 }
2358 else if (event_type == WC_ND_REPORT)
2359 {
2360 wc_nd_report_t *nd_events = event_data;
2361 for (i = 0; i < vec_len (nd_events); i++)
2362 {
2363 /* discard dup event */
2364 if (ip6_address_is_equal
2365 ((ip6_address_t *) & nd_prev.ip6, &nd_events[i].ip6)
2366 && eth_mac_equal ((u8 *) nd_prev.mac, nd_events[i].mac)
2367 && nd_prev.sw_if_index == nd_events[i].sw_if_index
2368 && (now - last_nd) < 10.0)
2369 {
2370 continue;
2371 }
2372 nd_prev = nd_events[i];
2373 last_nd = now;
2374 vpe_client_registration_t *reg;
2375 /* *INDENT-OFF* */
2376 pool_foreach(reg, vpe_api_main.wc_ip6_nd_events_registrations,
2377 ({
2378 unix_shared_memory_queue_t *q;
2379 q = vl_api_client_index_to_input_queue (reg->client_index);
2380 if (q && q->cursize < q->maxsize)
2381 {
2382 vl_api_ip6_nd_event_t * event = vl_msg_api_alloc (sizeof *event);
2383 memset (event, 0, sizeof *event);
2384 event->_vl_msg_id = htons (VL_API_IP6_ND_EVENT);
2385 event->client_index = reg->client_index;
2386 event->pid = reg->client_pid;
2387 event->mac_ip = 1;
2388 memcpy(event->address, nd_events[i].ip6.as_u8, sizeof event->address);
2389 event->sw_if_index = htonl(nd_events[i].sw_if_index);
2390 memcpy(event->new_mac, nd_events[i].mac, sizeof event->new_mac);
2391 vl_msg_api_send_shmem (q, (u8 *) &event);
2392 }
2393 }));
2394 /* *INDENT-ON* */
2395 }
2396 }
2397 vlib_process_put_event_data (vm, event_data);
2398 }
2399
2400 return 0;
2401}
2402
2403/* *INDENT-OFF* */
2404VLIB_REGISTER_NODE (wc_arp_process_node,static) = {
2405 .function = wc_arp_process,
2406 .type = VLIB_NODE_TYPE_PROCESS,
2407 .name = "wildcard-ip4-arp-publisher-process",
2408};
2409/* *INDENT-ON* */
2410
2411static int
2412arp_change_data_callback (u32 pool_index, u8 * new_mac,
2413 u32 sw_if_index, u32 address)
2414{
2415 vpe_api_main_t *am = &vpe_api_main;
2416 vl_api_ip4_arp_event_t *event;
2417
2418 if (pool_is_free_index (am->arp_events, pool_index))
2419 return 1;
2420
2421 event = pool_elt_at_index (am->arp_events, pool_index);
2422 if (eth_mac_equal (event->new_mac, new_mac) &&
2423 sw_if_index == ntohl (event->sw_if_index))
2424 {
2425 return 1;
2426 }
2427
2428 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2429 event->sw_if_index = htonl (sw_if_index);
2430 return 0;
2431}
2432
2433static void
2434vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
2435{
2436 vpe_api_main_t *am = &vpe_api_main;
2437 vnet_main_t *vnm = vnet_get_main ();
2438 vl_api_want_ip4_arp_events_reply_t *rmp;
2439 int rv = 0;
2440
2441 if (mp->address == 0)
2442 {
2443 uword *p =
2444 hash_get (am->wc_ip4_arp_events_registration_hash, mp->client_index);
2445 vpe_client_registration_t *rp;
2446 if (p)
2447 {
2448 if (mp->enable_disable)
2449 {
2450 clib_warning ("pid %d: already enabled...", mp->pid);
2451 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2452 goto reply;
2453 }
2454 else
2455 {
2456 rp =
2457 pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2458 pool_put (am->wc_ip4_arp_events_registrations, rp);
2459 hash_unset (am->wc_ip4_arp_events_registration_hash,
2460 mp->client_index);
2461 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
2462 wc_arp_set_publisher_node (~0, WC_ARP_REPORT);
2463 goto reply;
2464 }
2465 }
2466 if (mp->enable_disable == 0)
2467 {
2468 clib_warning ("pid %d: already disabled...", mp->pid);
2469 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2470 goto reply;
2471 }
2472 pool_get (am->wc_ip4_arp_events_registrations, rp);
2473 rp->client_index = mp->client_index;
2474 rp->client_pid = mp->pid;
2475 hash_set (am->wc_ip4_arp_events_registration_hash, rp->client_index,
2476 rp - am->wc_ip4_arp_events_registrations);
2477 wc_arp_set_publisher_node (wc_arp_process_node.index, WC_ARP_REPORT);
2478 goto reply;
2479 }
2480
2481 if (mp->enable_disable)
2482 {
2483 vl_api_ip4_arp_event_t *event;
2484 pool_get (am->arp_events, event);
2485 rv = vnet_add_del_ip4_arp_change_event
2486 (vnm, arp_change_data_callback,
2487 mp->pid, &mp->address /* addr, in net byte order */ ,
2488 ip_resolver_process_node.index,
2489 IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
2490
2491 if (rv)
2492 {
2493 pool_put (am->arp_events, event);
2494 goto reply;
2495 }
2496 memset (event, 0, sizeof (*event));
2497
2498 /* Python API expects events to have no context */
2499 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2500 event->client_index = mp->client_index;
2501 event->address = mp->address;
2502 event->pid = mp->pid;
2503 if (mp->address == 0)
2504 event->mac_ip = 1;
2505 }
2506 else
2507 {
2508 rv = vnet_add_del_ip4_arp_change_event
2509 (vnm, arp_change_delete_callback,
2510 mp->pid, &mp->address /* addr, in net byte order */ ,
2511 ip_resolver_process_node.index,
2512 IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2513 }
2514reply:
2515 REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
2516}
2517
2518static void
2519vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
2520{
2521 vpe_api_main_t *am = &vpe_api_main;
2522 vnet_main_t *vnm = vnet_get_main ();
2523 vl_api_want_ip6_nd_events_reply_t *rmp;
2524 int rv = 0;
2525
2526 if (ip6_address_is_zero ((ip6_address_t *) mp->address))
2527 {
2528 uword *p =
2529 hash_get (am->wc_ip6_nd_events_registration_hash, mp->client_index);
2530 vpe_client_registration_t *rp;
2531 if (p)
2532 {
2533 if (mp->enable_disable)
2534 {
2535 clib_warning ("pid %d: already enabled...", mp->pid);
2536 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2537 goto reply;
2538 }
2539 else
2540 {
2541 rp =
2542 pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2543 pool_put (am->wc_ip6_nd_events_registrations, rp);
2544 hash_unset (am->wc_ip6_nd_events_registration_hash,
2545 mp->client_index);
2546 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
2547 wc_nd_set_publisher_node (~0, 2);
2548 goto reply;
2549 }
2550 }
2551 if (mp->enable_disable == 0)
2552 {
2553 clib_warning ("pid %d: already disabled...", mp->pid);
2554 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2555 goto reply;
2556 }
2557 pool_get (am->wc_ip6_nd_events_registrations, rp);
2558 rp->client_index = mp->client_index;
2559 rp->client_pid = mp->pid;
2560 hash_set (am->wc_ip6_nd_events_registration_hash, rp->client_index,
2561 rp - am->wc_ip6_nd_events_registrations);
2562 wc_nd_set_publisher_node (wc_arp_process_node.index, WC_ND_REPORT);
2563 goto reply;
2564 }
2565
2566 if (mp->enable_disable)
2567 {
2568 vl_api_ip6_nd_event_t *event;
2569 pool_get (am->nd_events, event);
2570
2571 rv = vnet_add_del_ip6_nd_change_event
2572 (vnm, nd_change_data_callback,
2573 mp->pid, mp->address /* addr, in net byte order */ ,
2574 ip_resolver_process_node.index,
2575 IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
2576
2577 if (rv)
2578 {
2579 pool_put (am->nd_events, event);
2580 goto reply;
2581 }
2582 memset (event, 0, sizeof (*event));
2583
2584 event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
2585 event->client_index = mp->client_index;
2586 clib_memcpy (event->address, mp->address, sizeof event->address);
2587 event->pid = mp->pid;
2588 }
2589 else
2590 {
2591 rv = vnet_add_del_ip6_nd_change_event
2592 (vnm, nd_change_delete_callback,
2593 mp->pid, mp->address /* addr, in net byte order */ ,
2594 ip_resolver_process_node.index,
2595 IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2596 }
2597reply:
2598 REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
2599}
2600
2601static void
2602vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2603{
2604 vl_api_proxy_arp_add_del_reply_t *rmp;
2605 u32 fib_index;
2606 int rv;
2607 ip4_main_t *im = &ip4_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08002608 int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
2609 ip4_address_t * hi_addr,
2610 u32 fib_index, int is_del);
2611 uword *p;
2612
Ole Troan3288ed72017-12-06 17:00:05 +01002613 stats_dslock_with_hint (1 /* release hint */ , 6 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002614
2615 p = hash_get (im->fib_index_by_table_id, ntohl (mp->vrf_id));
2616
2617 if (!p)
2618 {
2619 rv = VNET_API_ERROR_NO_SUCH_FIB;
2620 goto out;
2621 }
2622
2623 fib_index = p[0];
2624
2625 rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->low_address,
2626 (ip4_address_t *) mp->hi_address,
2627 fib_index, mp->is_add == 0);
2628
2629out:
Ole Troan3288ed72017-12-06 17:00:05 +01002630 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002631 REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2632}
2633
2634static void
2635 vl_api_proxy_arp_intfc_enable_disable_t_handler
2636 (vl_api_proxy_arp_intfc_enable_disable_t * mp)
2637{
2638 int rv = 0;
2639 vnet_main_t *vnm = vnet_get_main ();
2640 vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
2641
2642 VALIDATE_SW_IF_INDEX (mp);
2643
2644 vnet_sw_interface_t *si =
2645 vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
2646
2647 ASSERT (si);
2648
2649 if (mp->enable_disable)
2650 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2651 else
2652 si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2653
2654 BAD_SW_IF_INDEX_LABEL;
2655
2656 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
2657}
2658
2659static int
2660ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2661{
2662 vnet_main_t *vnm = vnet_get_main ();
2663 vnet_interface_main_t *im = &vnm->interface_main;
2664 ip4_main_t *im4 = &ip4_main;
2665 static u32 *sw_if_indices_to_shut;
Neale Rannsb8d44812017-11-10 06:53:54 -08002666 fib_table_t *fib_table;
2667 ip4_fib_t *fib;
2668 u32 sw_if_index;
2669 int i;
2670 int rv = VNET_API_ERROR_NO_SUCH_FIB;
2671 u32 target_fib_id = ntohl (mp->vrf_id);
2672
Ole Troan3288ed72017-12-06 17:00:05 +01002673 stats_dslock_with_hint (1 /* release hint */ , 8 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002674
2675 /* *INDENT-OFF* */
2676 pool_foreach (fib_table, im4->fibs,
2677 ({
2678 vnet_sw_interface_t * si;
2679
2680 fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
2681
2682 if (fib->table_id != target_fib_id)
2683 continue;
2684
2685 /* remove any mpls encap/decap labels */
2686 mpls_fib_reset_labels (fib->table_id);
2687
2688 /* remove any proxy arps in this fib */
2689 vnet_proxy_arp_fib_reset (fib->table_id);
2690
2691 /* Set the flow hash for this fib to the default */
2692 vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2693
2694 vec_reset_length (sw_if_indices_to_shut);
2695
2696 /* Shut down interfaces in this FIB / clean out intfc routes */
2697 pool_foreach (si, im->sw_interfaces,
2698 ({
2699 u32 sw_if_index = si->sw_if_index;
2700
2701 if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
2702 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
2703 fib->index))
2704 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2705 }));
2706
2707 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2708 sw_if_index = sw_if_indices_to_shut[i];
2709
2710 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2711 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2712 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2713 }
2714
2715 fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
2716
2717 rv = 0;
2718 break;
2719 })); /* pool_foreach (fib) */
2720 /* *INDENT-ON* */
2721
Ole Troan3288ed72017-12-06 17:00:05 +01002722 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002723 return rv;
2724}
2725
2726static int
2727ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2728{
2729 vnet_main_t *vnm = vnet_get_main ();
2730 vnet_interface_main_t *im = &vnm->interface_main;
2731 ip6_main_t *im6 = &ip6_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08002732 static u32 *sw_if_indices_to_shut;
2733 fib_table_t *fib_table;
2734 ip6_fib_t *fib;
2735 u32 sw_if_index;
2736 int i;
2737 int rv = VNET_API_ERROR_NO_SUCH_FIB;
2738 u32 target_fib_id = ntohl (mp->vrf_id);
2739
Ole Troan3288ed72017-12-06 17:00:05 +01002740 stats_dslock_with_hint (1 /* release hint */ , 9 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002741
2742 /* *INDENT-OFF* */
2743 pool_foreach (fib_table, im6->fibs,
2744 ({
2745 vnet_sw_interface_t * si;
2746
2747 fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
2748
2749 if (fib->table_id != target_fib_id)
2750 continue;
2751
2752 vec_reset_length (sw_if_indices_to_shut);
2753
2754 /* Set the flow hash for this fib to the default */
2755 vnet_set_ip6_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2756
2757 /* Shut down interfaces in this FIB / clean out intfc routes */
2758 pool_foreach (si, im->sw_interfaces,
2759 ({
2760 if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
2761 fib->index)
2762 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2763 }));
2764
2765 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2766 sw_if_index = sw_if_indices_to_shut[i];
2767
2768 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2769 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2770 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2771 }
2772
2773 fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
2774
2775 rv = 0;
2776 break;
2777 })); /* pool_foreach (fib) */
2778 /* *INDENT-ON* */
2779
Ole Troan3288ed72017-12-06 17:00:05 +01002780 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002781 return rv;
2782}
2783
2784static void
2785vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2786{
2787 int rv;
2788 vl_api_reset_fib_reply_t *rmp;
2789
2790 if (mp->is_ipv6)
2791 rv = ip6_reset_fib_t_handler (mp);
2792 else
2793 rv = ip4_reset_fib_t_handler (mp);
2794
2795 REPLY_MACRO (VL_API_RESET_FIB_REPLY);
2796}
2797
2798static void
2799vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
2800{
2801 int rv;
2802 vl_api_set_arp_neighbor_limit_reply_t *rmp;
2803 vnet_main_t *vnm = vnet_get_main ();
2804 clib_error_t *error;
2805
2806 vnm->api_errno = 0;
2807
2808 if (mp->is_ipv6)
2809 error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
2810 else
2811 error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
2812
2813 if (error)
2814 {
2815 clib_error_report (error);
2816 rv = VNET_API_ERROR_UNSPECIFIED;
2817 }
2818 else
2819 {
2820 rv = vnm->api_errno;
2821 }
2822
2823 REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
2824}
2825
Dave Barachb5e8a772016-12-06 12:04:42 -05002826#define vl_msg_name_crc_list
2827#include <vnet/ip/ip.api.h>
2828#undef vl_msg_name_crc_list
2829
2830static void
2831setup_message_id_table (api_main_t * am)
2832{
2833#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
2834 foreach_vl_msg_name_crc_ip;
2835#undef _
2836}
2837
2838static clib_error_t *
2839ip_api_hookup (vlib_main_t * vm)
2840{
2841 api_main_t *am = &api_main;
2842
2843#define _(N,n) \
2844 vl_msg_api_set_handlers(VL_API_##N, #n, \
2845 vl_api_##n##_t_handler, \
2846 vl_noop_handler, \
2847 vl_api_##n##_t_endian, \
2848 vl_api_##n##_t_print, \
2849 sizeof(vl_api_##n##_t), 1);
2850 foreach_ip_api_msg;
2851#undef _
2852
2853 /*
2854 * Set up the (msg_name, crc, message-id) table
2855 */
2856 setup_message_id_table (am);
2857
2858 return 0;
2859}
2860
2861VLIB_API_INIT_FUNCTION (ip_api_hookup);
2862
2863/*
2864 * fd.io coding-style-patch-verification: ON
2865 *
2866 * Local Variables:
2867 * eval: (c-set-style "gnu")
2868 * End:
2869 */