blob: b9f1782b342f1d8bb6e770b412a66cb913b9e459 [file] [log] [blame]
Dave Barachb5e8a772016-12-06 12:04:42 -05001/*
2 *------------------------------------------------------------------
3 * ip_api.c - vnet ip api
4 *
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <vnet/vnet.h>
21#include <vlibmemory/api.h>
22
23#include <vnet/interface.h>
24#include <vnet/api_errno.h>
25#include <vnet/ethernet/ethernet.h>
26#include <vnet/ip/ip.h>
27#include <vnet/ip/ip6_neighbor.h>
28#include <vnet/fib/fib_table.h>
29#include <vnet/fib/fib_api.h>
30#include <vnet/dpo/drop_dpo.h>
31#include <vnet/dpo/receive_dpo.h>
32#include <vnet/dpo/lookup_dpo.h>
33#include <vnet/dpo/classify_dpo.h>
34#include <vnet/dpo/ip_null_dpo.h>
35#include <vnet/ethernet/arp_packet.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080036#include <vnet/mfib/ip6_mfib.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000037#include <vnet/mfib/ip4_mfib.h>
38#include <vnet/mfib/mfib_signal.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080039#include <vnet/mfib/mfib_entry.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050040
41#include <vnet/vnet_msg_enum.h>
42
43#define vl_typedefs /* define message structures */
44#include <vnet/vnet_all_api_h.h>
45#undef vl_typedefs
46
47#define vl_endianfun /* define message structures */
48#include <vnet/vnet_all_api_h.h>
49#undef vl_endianfun
50
51/* instantiate all the print functions we know about */
52#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
53#define vl_printfun
54#include <vnet/vnet_all_api_h.h>
55#undef vl_printfun
56
57#include <vlibapi/api_helper_macros.h>
58
Neale Ranns5a8123b2017-01-26 01:18:23 -080059
Dave Barachb5e8a772016-12-06 12:04:42 -050060#define foreach_ip_api_msg \
61_(IP_FIB_DUMP, ip_fib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050062_(IP6_FIB_DUMP, ip6_fib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080063_(IP_MFIB_DUMP, ip_mfib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080064_(IP6_MFIB_DUMP, ip6_mfib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050065_(IP_NEIGHBOR_DUMP, ip_neighbor_dump) \
Neale Ranns32e1c012016-11-22 17:07:28 +000066_(IP_MROUTE_ADD_DEL, ip_mroute_add_del) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080067_(MFIB_SIGNAL_DUMP, mfib_signal_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050068_(IP_ADDRESS_DUMP, ip_address_dump) \
69_(IP_DUMP, ip_dump) \
70_(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del) \
71_(IP_ADD_DEL_ROUTE, ip_add_del_route) \
72_(SET_IP_FLOW_HASH,set_ip_flow_hash) \
73_(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config) \
74_(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix) \
Neale Ranns3f844d02017-02-18 00:03:54 -080075_(IP6ND_PROXY_ADD_DEL, ip6nd_proxy_add_del) \
76_(IP6ND_PROXY_DUMP, ip6nd_proxy_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050077_(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable ) \
78_(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS, \
79 sw_interface_ip6_set_link_local_address)
80
81extern void stats_dslock_with_hint (int hint, int tag);
82extern void stats_dsunlock (void);
83
84static void
85send_ip_neighbor_details (u8 is_ipv6,
86 u8 is_static,
87 u8 * mac_address,
88 u8 * ip_address,
89 unix_shared_memory_queue_t * q, u32 context)
90{
91 vl_api_ip_neighbor_details_t *mp;
92
93 mp = vl_msg_api_alloc (sizeof (*mp));
94 memset (mp, 0, sizeof (*mp));
95 mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS);
96 mp->context = context;
97 mp->is_ipv6 = is_ipv6;
98 mp->is_static = is_static;
99 memcpy (mp->mac_address, mac_address, 6);
100 memcpy (mp->ip_address, ip_address, (is_ipv6) ? 16 : 4);
101
102 vl_msg_api_send_shmem (q, (u8 *) & mp);
103}
104
105static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500106vl_api_ip_neighbor_dump_t_handler (vl_api_ip_neighbor_dump_t * mp)
107{
108 unix_shared_memory_queue_t *q;
109
110 q = vl_api_client_index_to_input_queue (mp->client_index);
111 if (q == 0)
112 return;
113
114 u32 sw_if_index = ntohl (mp->sw_if_index);
115
116 if (mp->is_ipv6)
117 {
118 ip6_neighbor_t *n, *ns;
119
120 ns = ip6_neighbors_entries (sw_if_index);
121 /* *INDENT-OFF* */
122 vec_foreach (n, ns)
123 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500124 send_ip_neighbor_details
125 (mp->is_ipv6, ((n->flags & IP6_NEIGHBOR_FLAG_STATIC) ? 1 : 0),
126 (u8 *) n->link_layer_address,
127 (u8 *) & (n->key.ip6_address.as_u8),
128 q, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500129 }
130 /* *INDENT-ON* */
131 vec_free (ns);
132 }
133 else
134 {
135 ethernet_arp_ip4_entry_t *n, *ns;
136
137 ns = ip4_neighbor_entries (sw_if_index);
138 /* *INDENT-OFF* */
139 vec_foreach (n, ns)
140 {
141 send_ip_neighbor_details (mp->is_ipv6,
142 ((n->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC) ? 1 : 0),
143 (u8*) n->ethernet_address,
144 (u8*) & (n->ip4_address.as_u8),
145 q, mp->context);
146 }
147 /* *INDENT-ON* */
148 vec_free (ns);
149 }
150}
151
152
153void
154copy_fib_next_hop (fib_route_path_encode_t * api_rpath, void *fp_arg)
155{
156 int is_ip4;
157 vl_api_fib_path_t *fp = (vl_api_fib_path_t *) fp_arg;
158
159 if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP4)
160 fp->afi = IP46_TYPE_IP4;
161 else if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP6)
162 fp->afi = IP46_TYPE_IP6;
163 else
164 {
165 is_ip4 = ip46_address_is_ip4 (&api_rpath->rpath.frp_addr);
166 if (is_ip4)
167 fp->afi = IP46_TYPE_IP4;
168 else
169 fp->afi = IP46_TYPE_IP6;
170 }
171 if (fp->afi == IP46_TYPE_IP4)
172 memcpy (fp->next_hop, &api_rpath->rpath.frp_addr.ip4,
173 sizeof (api_rpath->rpath.frp_addr.ip4));
174 else
175 memcpy (fp->next_hop, &api_rpath->rpath.frp_addr.ip6,
176 sizeof (api_rpath->rpath.frp_addr.ip6));
177}
178
179static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500180send_ip_fib_details (vpe_api_main_t * am,
181 unix_shared_memory_queue_t * q,
182 u32 table_id, fib_prefix_t * pfx,
183 fib_route_path_encode_t * api_rpaths, u32 context)
184{
185 vl_api_ip_fib_details_t *mp;
186 fib_route_path_encode_t *api_rpath;
187 vl_api_fib_path_t *fp;
188 int path_count;
189
190 path_count = vec_len (api_rpaths);
191 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
192 if (!mp)
193 return;
194 memset (mp, 0, sizeof (*mp));
195 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
196 mp->context = context;
197
198 mp->table_id = htonl (table_id);
199 mp->address_length = pfx->fp_len;
200 memcpy (mp->address, &pfx->fp_addr.ip4, sizeof (pfx->fp_addr.ip4));
201
202 mp->count = htonl (path_count);
203 fp = mp->path;
204 vec_foreach (api_rpath, api_rpaths)
205 {
206 memset (fp, 0, sizeof (*fp));
207 switch (api_rpath->dpo.dpoi_type)
208 {
209 case DPO_RECEIVE:
210 fp->is_local = true;
211 break;
212 case DPO_DROP:
213 fp->is_drop = true;
214 break;
215 case DPO_IP_NULL:
216 switch (api_rpath->dpo.dpoi_index)
217 {
218 case IP_NULL_ACTION_NONE:
219 fp->is_drop = true;
220 break;
221 case IP_NULL_ACTION_SEND_ICMP_UNREACH:
222 fp->is_unreach = true;
223 break;
224 case IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
225 fp->is_prohibit = true;
226 break;
227 default:
228 break;
229 }
230 break;
231 default:
232 break;
233 }
234 fp->weight = htonl (api_rpath->rpath.frp_weight);
235 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
236 copy_fib_next_hop (api_rpath, fp);
237 fp++;
238 }
239
240 vl_msg_api_send_shmem (q, (u8 *) & mp);
241}
242
Neale Rannsa3af3372017-03-28 03:49:52 -0700243typedef struct vl_api_ip_fib_dump_walk_ctx_t_
244{
245 fib_node_index_t *feis;
246} vl_api_ip_fib_dump_walk_ctx_t;
247
248static int
249vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
250{
251 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
252
253 vec_add1 (ctx->feis, fei);
254
255 return (1);
256}
257
Dave Barachb5e8a772016-12-06 12:04:42 -0500258static void
259vl_api_ip_fib_dump_t_handler (vl_api_ip_fib_dump_t * mp)
260{
261 vpe_api_main_t *am = &vpe_api_main;
262 unix_shared_memory_queue_t *q;
263 ip4_main_t *im = &ip4_main;
264 fib_table_t *fib_table;
Neale Rannsa3af3372017-03-28 03:49:52 -0700265 fib_node_index_t *lfeip;
Dave Barachb5e8a772016-12-06 12:04:42 -0500266 fib_prefix_t pfx;
267 u32 fib_index;
268 fib_route_path_encode_t *api_rpaths;
Neale Rannsa3af3372017-03-28 03:49:52 -0700269 vl_api_ip_fib_dump_walk_ctx_t ctx = {
270 .feis = NULL,
271 };
Dave Barachb5e8a772016-12-06 12:04:42 -0500272
273 q = vl_api_client_index_to_input_queue (mp->client_index);
274 if (q == 0)
275 return;
276
277 /* *INDENT-OFF* */
278 pool_foreach (fib_table, im->fibs,
279 ({
Neale Rannsa3af3372017-03-28 03:49:52 -0700280 fib_table_walk(fib_table->ft_index,
281 FIB_PROTOCOL_IP4,
282 vl_api_ip_fib_dump_walk,
283 &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500284 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500285 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500286
Neale Rannsa3af3372017-03-28 03:49:52 -0700287 vec_sort_with_function (ctx.feis, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500288
Neale Rannsa3af3372017-03-28 03:49:52 -0700289 vec_foreach (lfeip, ctx.feis)
Dave Barachb5e8a772016-12-06 12:04:42 -0500290 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500291 fib_entry_get_prefix (*lfeip, &pfx);
292 fib_index = fib_entry_get_fib_index (*lfeip);
293 fib_table = fib_table_get (fib_index, pfx.fp_proto);
Dave Barachb5e8a772016-12-06 12:04:42 -0500294 api_rpaths = NULL;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500295 fib_entry_encode (*lfeip, &api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500296 send_ip_fib_details (am, q,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500297 fib_table->ft_table_id, &pfx, api_rpaths,
298 mp->context);
299 vec_free (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500300 }
301
Neale Rannsa3af3372017-03-28 03:49:52 -0700302 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500303}
304
305static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500306send_ip6_fib_details (vpe_api_main_t * am,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500307 unix_shared_memory_queue_t * q,
308 u32 table_id, fib_prefix_t * pfx,
309 fib_route_path_encode_t * api_rpaths, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500310{
311 vl_api_ip6_fib_details_t *mp;
312 fib_route_path_encode_t *api_rpath;
313 vl_api_fib_path_t *fp;
314 int path_count;
315
Dave Barachd7cb1b52016-12-09 09:52:16 -0500316 path_count = vec_len (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500317 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
318 if (!mp)
319 return;
320 memset (mp, 0, sizeof (*mp));
321 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
322 mp->context = context;
323
324 mp->table_id = htonl (table_id);
325 mp->address_length = pfx->fp_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500326 memcpy (mp->address, &pfx->fp_addr.ip6, sizeof (pfx->fp_addr.ip6));
Dave Barachb5e8a772016-12-06 12:04:42 -0500327
328 mp->count = htonl (path_count);
329 fp = mp->path;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500330 vec_foreach (api_rpath, api_rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500331 {
332 memset (fp, 0, sizeof (*fp));
333 switch (api_rpath->dpo.dpoi_type)
334 {
335 case DPO_RECEIVE:
336 fp->is_local = true;
337 break;
338 case DPO_DROP:
339 fp->is_drop = true;
340 break;
341 case DPO_IP_NULL:
342 switch (api_rpath->dpo.dpoi_index)
343 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500344 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_NONE:
Dave Barachb5e8a772016-12-06 12:04:42 -0500345 fp->is_drop = true;
346 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500347 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_SEND_ICMP_UNREACH:
Dave Barachb5e8a772016-12-06 12:04:42 -0500348 fp->is_unreach = true;
349 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500350 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
Dave Barachb5e8a772016-12-06 12:04:42 -0500351 fp->is_prohibit = true;
352 break;
353 default:
354 break;
355 }
356 break;
357 default:
358 break;
359 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500360 fp->weight = htonl (api_rpath->rpath.frp_weight);
361 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500362 copy_fib_next_hop (api_rpath, fp);
363 fp++;
364 }
365
366 vl_msg_api_send_shmem (q, (u8 *) & mp);
367}
368
Dave Barachd7cb1b52016-12-09 09:52:16 -0500369typedef struct apt_ip6_fib_show_ctx_t_
370{
371 u32 fib_index;
372 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500373} api_ip6_fib_show_ctx_t;
374
375static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500376api_ip6_fib_table_put_entries (clib_bihash_kv_24_8_t * kvp, void *arg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500377{
378 api_ip6_fib_show_ctx_t *ctx = arg;
379
380 if ((kvp->key[2] >> 32) == ctx->fib_index)
381 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500382 vec_add1 (ctx->entries, kvp->value);
Dave Barachb5e8a772016-12-06 12:04:42 -0500383 }
384}
385
386static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500387api_ip6_fib_table_get_all (unix_shared_memory_queue_t * q,
388 vl_api_ip6_fib_dump_t * mp,
389 fib_table_t * fib_table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500390{
391 vpe_api_main_t *am = &vpe_api_main;
392 ip6_main_t *im6 = &ip6_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500393 fib_node_index_t *fib_entry_index;
394 api_ip6_fib_show_ctx_t ctx = {
Neale Rannsa3af3372017-03-28 03:49:52 -0700395 .fib_index = fib_table->ft_index,
396 .entries = NULL,
Dave Barachb5e8a772016-12-06 12:04:42 -0500397 };
398 fib_route_path_encode_t *api_rpaths;
399 fib_prefix_t pfx;
400
Dave Barachd7cb1b52016-12-09 09:52:16 -0500401 BV (clib_bihash_foreach_key_value_pair)
402 ((BVT (clib_bihash) *) & im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].
403 ip6_hash, api_ip6_fib_table_put_entries, &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500404
Dave Barachd7cb1b52016-12-09 09:52:16 -0500405 vec_sort_with_function (ctx.entries, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500406
Dave Barachd7cb1b52016-12-09 09:52:16 -0500407 vec_foreach (fib_entry_index, ctx.entries)
408 {
409 fib_entry_get_prefix (*fib_entry_index, &pfx);
410 api_rpaths = NULL;
411 fib_entry_encode (*fib_entry_index, &api_rpaths);
412 send_ip6_fib_details (am, q,
413 fib_table->ft_table_id,
414 &pfx, api_rpaths, mp->context);
415 vec_free (api_rpaths);
416 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500417
Dave Barachd7cb1b52016-12-09 09:52:16 -0500418 vec_free (ctx.entries);
Dave Barachb5e8a772016-12-06 12:04:42 -0500419}
420
421static void
422vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
423{
424 unix_shared_memory_queue_t *q;
425 ip6_main_t *im6 = &ip6_main;
426 fib_table_t *fib_table;
427
428 q = vl_api_client_index_to_input_queue (mp->client_index);
429 if (q == 0)
430 return;
431
432 /* *INDENT-OFF* */
433 pool_foreach (fib_table, im6->fibs,
434 ({
435 api_ip6_fib_table_get_all(q, mp, fib_table);
436 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500437 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500438}
439
440static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800441send_ip_mfib_details (vpe_api_main_t * am,
442 unix_shared_memory_queue_t * q,
443 u32 table_id,
444 mfib_prefix_t * pfx,
445 fib_route_path_encode_t * api_rpaths, u32 context)
446{
447 vl_api_ip_mfib_details_t *mp;
448 fib_route_path_encode_t *api_rpath;
449 vl_api_fib_path_t *fp;
450 int path_count;
451
452 path_count = vec_len (api_rpaths);
453 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
454 if (!mp)
455 return;
456 memset (mp, 0, sizeof (*mp));
457 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
458 mp->context = context;
459
460 mp->table_id = htonl (table_id);
461 mp->address_length = pfx->fp_len;
462 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip4,
463 sizeof (pfx->fp_grp_addr.ip4));
464 memcpy (mp->src_address, &pfx->fp_src_addr.ip4,
465 sizeof (pfx->fp_src_addr.ip4));
466
467 mp->count = htonl (path_count);
468 fp = mp->path;
469 vec_foreach (api_rpath, api_rpaths)
470 {
471 memset (fp, 0, sizeof (*fp));
472
473 fp->weight = 0;
474 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
475 copy_fib_next_hop (api_rpath, fp);
476 fp++;
477 }
478
479 vl_msg_api_send_shmem (q, (u8 *) & mp);
480}
481
482typedef struct vl_api_ip_mfib_dump_ctc_t_
483{
484 fib_node_index_t *entries;
485} vl_api_ip_mfib_dump_ctc_t;
486
487static int
488vl_api_ip_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
489{
490 vl_api_ip_mfib_dump_ctc_t *ctx = arg;
491
492 vec_add1 (ctx->entries, fei);
493
494 return (0);
495}
496
497static void
498vl_api_ip_mfib_dump_t_handler (vl_api_ip_mfib_dump_t * mp)
499{
500 vpe_api_main_t *am = &vpe_api_main;
501 unix_shared_memory_queue_t *q;
502 ip4_main_t *im = &ip4_main;
503 mfib_table_t *mfib_table;
504 fib_node_index_t *mfeip;
505 mfib_prefix_t pfx;
506 fib_route_path_encode_t *api_rpaths = NULL;
507 vl_api_ip_mfib_dump_ctc_t ctx = {
508 .entries = NULL,
509 };
510
511 q = vl_api_client_index_to_input_queue (mp->client_index);
512 if (q == 0)
513 return;
514
515
516 /* *INDENT-OFF* */
517 pool_foreach (mfib_table, im->mfibs,
518 ({
519 ip4_mfib_table_walk(&mfib_table->v4,
520 vl_api_ip_mfib_table_dump_walk,
521 &ctx);
522
523 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
524
525 vec_foreach (mfeip, ctx.entries)
526 {
527 mfib_entry_get_prefix (*mfeip, &pfx);
528 mfib_entry_encode (*mfeip, &api_rpaths);
529 send_ip_mfib_details (am, q,
530 mfib_table->mft_table_id,
531 &pfx, api_rpaths,
532 mp->context);
533 }
534 vec_reset_length (api_rpaths);
535 vec_reset_length (ctx.entries);
536
537 }));
538 /* *INDENT-ON* */
539
540 vec_free (ctx.entries);
541 vec_free (api_rpaths);
542}
543
544static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800545send_ip6_mfib_details (vpe_api_main_t * am,
546 unix_shared_memory_queue_t * q,
547 u32 table_id,
548 mfib_prefix_t * pfx,
549 fib_route_path_encode_t * api_rpaths, u32 context)
550{
551 vl_api_ip6_mfib_details_t *mp;
552 fib_route_path_encode_t *api_rpath;
553 vl_api_fib_path_t *fp;
554 int path_count;
555
556 path_count = vec_len (api_rpaths);
557 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
558 if (!mp)
559 return;
560 memset (mp, 0, sizeof (*mp));
561 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
562 mp->context = context;
563
564 mp->table_id = htonl (table_id);
565 mp->address_length = pfx->fp_len;
566 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip6,
567 sizeof (pfx->fp_grp_addr.ip6));
568 memcpy (mp->src_address, &pfx->fp_src_addr.ip6,
569 sizeof (pfx->fp_src_addr.ip6));
570
571 mp->count = htonl (path_count);
572 fp = mp->path;
573 vec_foreach (api_rpath, api_rpaths)
574 {
575 memset (fp, 0, sizeof (*fp));
576
577 fp->weight = 0;
578 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
579 copy_fib_next_hop (api_rpath, fp);
580 fp++;
581 }
582
583 vl_msg_api_send_shmem (q, (u8 *) & mp);
584}
585
586typedef struct vl_api_ip6_mfib_dump_ctc_t_
587{
588 fib_node_index_t *entries;
589} vl_api_ip6_mfib_dump_ctc_t;
590
591static int
592vl_api_ip6_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
593{
594 vl_api_ip6_mfib_dump_ctc_t *ctx = arg;
595
596 vec_add1 (ctx->entries, fei);
597
598 return (0);
599}
600
601static void
602vl_api_ip6_mfib_dump_t_handler (vl_api_ip6_mfib_dump_t * mp)
603{
604 vpe_api_main_t *am = &vpe_api_main;
605 unix_shared_memory_queue_t *q;
606 ip6_main_t *im = &ip6_main;
607 mfib_table_t *mfib_table;
608 fib_node_index_t *mfeip;
609 mfib_prefix_t pfx;
610 fib_route_path_encode_t *api_rpaths = NULL;
611 vl_api_ip6_mfib_dump_ctc_t ctx = {
612 .entries = NULL,
613 };
614
615 q = vl_api_client_index_to_input_queue (mp->client_index);
616 if (q == 0)
617 return;
618
619
620 /* *INDENT-OFF* */
621 pool_foreach (mfib_table, im->mfibs,
622 ({
623 ip6_mfib_table_walk(&mfib_table->v6,
624 vl_api_ip6_mfib_table_dump_walk,
625 &ctx);
626
627 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
628
629 vec_foreach(mfeip, ctx.entries)
630 {
631 mfib_entry_get_prefix (*mfeip, &pfx);
632 mfib_entry_encode (*mfeip, &api_rpaths);
633 send_ip6_mfib_details (am, q,
634 mfib_table->mft_table_id,
635 &pfx, api_rpaths,
636 mp->context);
637 }
638 vec_reset_length (api_rpaths);
639 vec_reset_length (ctx.entries);
640
641 }));
642 /* *INDENT-ON* */
643
644 vec_free (ctx.entries);
645 vec_free (api_rpaths);
646}
647
648static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500649vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
650 vlib_main_t * vm)
651{
652 vl_api_ip_neighbor_add_del_reply_t *rmp;
653 vnet_main_t *vnm = vnet_get_main ();
654 int rv = 0;
655
656 VALIDATE_SW_IF_INDEX (mp);
657
658 stats_dslock_with_hint (1 /* release hint */ , 7 /* tag */ );
659
660 /*
661 * there's no validation here of the ND/ARP entry being added.
662 * The expectation is that the FIB will ensure that nothing bad
663 * will come of adding bogus entries.
664 */
665 if (mp->is_ipv6)
666 {
667 if (mp->is_add)
668 rv = vnet_set_ip6_ethernet_neighbor
669 (vm, ntohl (mp->sw_if_index),
670 (ip6_address_t *) (mp->dst_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800671 mp->mac_address, sizeof (mp->mac_address), mp->is_static,
672 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500673 else
674 rv = vnet_unset_ip6_ethernet_neighbor
675 (vm, ntohl (mp->sw_if_index),
676 (ip6_address_t *) (mp->dst_address),
677 mp->mac_address, sizeof (mp->mac_address));
678 }
679 else
680 {
681 ethernet_arp_ip4_over_ethernet_address_t a;
682
683 clib_memcpy (&a.ethernet, mp->mac_address, 6);
684 clib_memcpy (&a.ip4, mp->dst_address, 4);
685
686 if (mp->is_add)
687 rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800688 &a, mp->is_static,
689 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500690 else
691 rv =
692 vnet_arp_unset_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index), &a);
693 }
694
695 BAD_SW_IF_INDEX_LABEL;
696
697 stats_dsunlock ();
698 REPLY_MACRO (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
699}
700
701int
702add_del_route_t_handler (u8 is_multipath,
703 u8 is_add,
704 u8 is_drop,
705 u8 is_unreach,
706 u8 is_prohibit,
707 u8 is_local,
708 u8 is_classify,
709 u32 classify_table_index,
710 u8 is_resolve_host,
711 u8 is_resolve_attached,
712 u32 fib_index,
713 const fib_prefix_t * prefix,
714 u8 next_hop_proto_is_ip4,
715 const ip46_address_t * next_hop,
716 u32 next_hop_sw_if_index,
717 u8 next_hop_fib_index,
718 u32 next_hop_weight,
719 mpls_label_t next_hop_via_label,
720 mpls_label_t * next_hop_out_label_stack)
721{
722 vnet_classify_main_t *cm = &vnet_classify_main;
723 fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
724 fib_route_path_t path = {
725 .frp_proto = (next_hop_proto_is_ip4 ?
726 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6),
727 .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
728 .frp_sw_if_index = next_hop_sw_if_index,
729 .frp_fib_index = next_hop_fib_index,
730 .frp_weight = next_hop_weight,
731 .frp_label_stack = next_hop_out_label_stack,
732 };
733 fib_route_path_t *paths = NULL;
734
735 if (MPLS_LABEL_INVALID != next_hop_via_label)
736 {
737 path.frp_proto = FIB_PROTOCOL_MPLS;
738 path.frp_local_label = next_hop_via_label;
739 }
740 if (is_resolve_host)
741 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
742 if (is_resolve_attached)
743 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
744
745 path.frp_flags = path_flags;
746
747 if (is_multipath)
748 {
749 stats_dslock_with_hint (1 /* release hint */ , 10 /* tag */ );
750
751
752 vec_add1 (paths, path);
753
754 if (is_add)
755 fib_table_entry_path_add2 (fib_index,
756 prefix,
757 FIB_SOURCE_API,
758 FIB_ENTRY_FLAG_NONE, paths);
759 else
760 fib_table_entry_path_remove2 (fib_index,
761 prefix, FIB_SOURCE_API, paths);
762
763 vec_free (paths);
764 stats_dsunlock ();
765 return 0;
766 }
767
768 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
769
770 if (is_drop || is_local || is_classify || is_unreach || is_prohibit)
771 {
772 /*
773 * special route types that link directly to the adj
774 */
775 if (is_add)
776 {
777 dpo_id_t dpo = DPO_INVALID;
778 dpo_proto_t dproto;
779
780 dproto = fib_proto_to_dpo (prefix->fp_proto);
781
782 if (is_drop)
783 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
784 else if (is_local)
785 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
786 else if (is_unreach)
787 ip_null_dpo_add_and_lock (dproto,
788 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
789 else if (is_prohibit)
790 ip_null_dpo_add_and_lock (dproto,
791 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
792 &dpo);
793 else if (is_classify)
794 {
795 if (pool_is_free_index (cm->tables,
796 ntohl (classify_table_index)))
797 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500798 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500799 return VNET_API_ERROR_NO_SUCH_TABLE;
800 }
801
802 dpo_set (&dpo, DPO_CLASSIFY, dproto,
803 classify_dpo_create (dproto,
804 ntohl (classify_table_index)));
805 }
806 else
807 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500808 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500809 return VNET_API_ERROR_NO_SUCH_TABLE;
810 }
811
812 fib_table_entry_special_dpo_update (fib_index,
813 prefix,
814 FIB_SOURCE_API,
815 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
816 dpo_reset (&dpo);
817 }
818 else
819 {
820 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
821 }
822 }
823 else
824 {
825 if (is_add)
826 {
827 vec_add1 (paths, path);
828 fib_table_entry_update (fib_index,
829 prefix,
830 FIB_SOURCE_API, FIB_ENTRY_FLAG_NONE, paths);
831 vec_free (paths);
832 }
833 else
834 {
835 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
836 }
837 }
838
Dave Barachd7cb1b52016-12-09 09:52:16 -0500839 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500840 return (0);
841}
842
843int
844add_del_route_check (fib_protocol_t table_proto,
845 u32 table_id,
846 u32 next_hop_sw_if_index,
847 fib_protocol_t next_hop_table_proto,
848 u32 next_hop_table_id,
849 u8 create_missing_tables,
850 u32 * fib_index, u32 * next_hop_fib_index)
851{
852 vnet_main_t *vnm = vnet_get_main ();
853
854 *fib_index = fib_table_find (table_proto, ntohl (table_id));
855 if (~0 == *fib_index)
856 {
857 if (create_missing_tables)
858 {
859 *fib_index = fib_table_find_or_create_and_lock (table_proto,
860 ntohl (table_id));
861 }
862 else
863 {
864 /* No such VRF, and we weren't asked to create one */
865 return VNET_API_ERROR_NO_SUCH_FIB;
866 }
867 }
868
869 if (~0 != ntohl (next_hop_sw_if_index))
870 {
871 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
872 ntohl (next_hop_sw_if_index)))
873 {
874 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
875 }
876 }
877 else
878 {
879 *next_hop_fib_index = fib_table_find (next_hop_table_proto,
880 ntohl (next_hop_table_id));
881
882 if (~0 == *next_hop_fib_index)
883 {
884 if (create_missing_tables)
885 {
886 *next_hop_fib_index =
887 fib_table_find_or_create_and_lock (next_hop_table_proto,
888 ntohl (next_hop_table_id));
889 }
890 else
891 {
892 /* No such VRF, and we weren't asked to create one */
893 return VNET_API_ERROR_NO_SUCH_FIB;
894 }
895 }
896 }
897
898 return (0);
899}
900
901static int
902ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
903{
904 u32 fib_index, next_hop_fib_index;
905 mpls_label_t *label_stack = NULL;
906 int rv, ii, n_labels;;
907
908 rv = add_del_route_check (FIB_PROTOCOL_IP4,
909 mp->table_id,
910 mp->next_hop_sw_if_index,
911 FIB_PROTOCOL_IP4,
912 mp->next_hop_table_id,
913 mp->create_vrf_if_needed,
914 &fib_index, &next_hop_fib_index);
915
916 if (0 != rv)
917 return (rv);
918
919 fib_prefix_t pfx = {
920 .fp_len = mp->dst_address_length,
921 .fp_proto = FIB_PROTOCOL_IP4,
922 };
923 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
924
925 ip46_address_t nh;
926 memset (&nh, 0, sizeof (nh));
927 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
928
929 n_labels = mp->next_hop_n_out_labels;
930 if (n_labels == 0)
931 ;
932 else if (1 == n_labels)
933 vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
934 else
935 {
936 vec_validate (label_stack, n_labels - 1);
937 for (ii = 0; ii < n_labels; ii++)
938 label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
939 }
940
941 return (add_del_route_t_handler (mp->is_multipath,
942 mp->is_add,
943 mp->is_drop,
944 mp->is_unreach,
945 mp->is_prohibit,
946 mp->is_local,
947 mp->is_classify,
948 mp->classify_table_index,
949 mp->is_resolve_host,
950 mp->is_resolve_attached,
951 fib_index, &pfx, 1,
952 &nh,
953 ntohl (mp->next_hop_sw_if_index),
954 next_hop_fib_index,
955 mp->next_hop_weight,
956 ntohl (mp->next_hop_via_label),
957 label_stack));
958}
959
960static int
961ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
962{
963 u32 fib_index, next_hop_fib_index;
964 mpls_label_t *label_stack = NULL;
965 int rv, ii, n_labels;;
966
967 rv = add_del_route_check (FIB_PROTOCOL_IP6,
968 mp->table_id,
969 mp->next_hop_sw_if_index,
970 FIB_PROTOCOL_IP6,
971 mp->next_hop_table_id,
972 mp->create_vrf_if_needed,
973 &fib_index, &next_hop_fib_index);
974
975 if (0 != rv)
976 return (rv);
977
978 fib_prefix_t pfx = {
979 .fp_len = mp->dst_address_length,
980 .fp_proto = FIB_PROTOCOL_IP6,
981 };
982 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
983
984 ip46_address_t nh;
985 memset (&nh, 0, sizeof (nh));
986 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
987
988 n_labels = mp->next_hop_n_out_labels;
989 if (n_labels == 0)
990 ;
991 else if (1 == n_labels)
992 vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
993 else
994 {
995 vec_validate (label_stack, n_labels - 1);
996 for (ii = 0; ii < n_labels; ii++)
997 label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
998 }
999
1000 return (add_del_route_t_handler (mp->is_multipath,
1001 mp->is_add,
1002 mp->is_drop,
1003 mp->is_unreach,
1004 mp->is_prohibit,
1005 mp->is_local,
1006 mp->is_classify,
1007 mp->classify_table_index,
1008 mp->is_resolve_host,
1009 mp->is_resolve_attached,
1010 fib_index, &pfx, 0,
1011 &nh, ntohl (mp->next_hop_sw_if_index),
1012 next_hop_fib_index,
1013 mp->next_hop_weight,
1014 ntohl (mp->next_hop_via_label),
1015 label_stack));
1016}
1017
1018void
1019vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1020{
1021 vl_api_ip_add_del_route_reply_t *rmp;
1022 int rv;
1023 vnet_main_t *vnm = vnet_get_main ();
1024
1025 vnm->api_errno = 0;
1026
1027 if (mp->is_ipv6)
1028 rv = ip6_add_del_route_t_handler (mp);
1029 else
1030 rv = ip4_add_del_route_t_handler (mp);
1031
1032 rv = (rv == 0) ? vnm->api_errno : rv;
1033
1034 REPLY_MACRO (VL_API_IP_ADD_DEL_ROUTE_REPLY);
1035}
1036
Neale Ranns32e1c012016-11-22 17:07:28 +00001037static int
1038add_del_mroute_check (fib_protocol_t table_proto,
1039 u32 table_id,
1040 u32 next_hop_sw_if_index,
1041 u8 is_local, u8 create_missing_tables, u32 * fib_index)
1042{
1043 vnet_main_t *vnm = vnet_get_main ();
1044
1045 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1046 if (~0 == *fib_index)
1047 {
1048 if (create_missing_tables)
1049 {
1050 *fib_index = mfib_table_find_or_create_and_lock (table_proto,
1051 ntohl (table_id));
1052 }
1053 else
1054 {
1055 /* No such VRF, and we weren't asked to create one */
1056 return VNET_API_ERROR_NO_SUCH_FIB;
1057 }
1058 }
1059
1060 if (~0 != ntohl (next_hop_sw_if_index))
1061 {
1062 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1063 ntohl (next_hop_sw_if_index)))
1064 {
1065 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1066 }
1067 }
1068
1069 return (0);
1070}
1071
1072static int
1073mroute_add_del_handler (u8 is_add,
1074 u8 is_local,
1075 u32 fib_index,
1076 const mfib_prefix_t * prefix,
1077 u32 entry_flags,
1078 u32 next_hop_sw_if_index, u32 itf_flags)
1079{
1080 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1081
1082 fib_route_path_t path = {
1083 .frp_sw_if_index = next_hop_sw_if_index,
1084 .frp_proto = prefix->fp_proto,
1085 };
1086
1087 if (is_local)
1088 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1089
1090
1091 if (!is_local && ~0 == next_hop_sw_if_index)
1092 {
1093 mfib_table_entry_update (fib_index, prefix,
1094 MFIB_SOURCE_API, entry_flags);
1095 }
1096 else
1097 {
1098 if (is_add)
1099 {
1100 mfib_table_entry_path_update (fib_index, prefix,
1101 MFIB_SOURCE_API, &path, itf_flags);
1102 }
1103 else
1104 {
1105 mfib_table_entry_path_remove (fib_index, prefix,
1106 MFIB_SOURCE_API, &path);
1107 }
1108 }
1109
1110 stats_dsunlock ();
1111 return (0);
1112}
1113
1114static int
1115api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1116{
1117 fib_protocol_t fproto;
1118 u32 fib_index;
1119 int rv;
1120
1121 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1122 rv = add_del_mroute_check (fproto,
1123 mp->table_id,
1124 mp->next_hop_sw_if_index,
1125 mp->is_local,
1126 mp->create_vrf_if_needed, &fib_index);
1127
1128 if (0 != rv)
1129 return (rv);
1130
1131 mfib_prefix_t pfx = {
1132 .fp_len = ntohs (mp->grp_address_length),
1133 .fp_proto = fproto,
1134 };
1135
1136 if (FIB_PROTOCOL_IP4 == fproto)
1137 {
1138 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1139 sizeof (pfx.fp_grp_addr.ip4));
1140 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1141 sizeof (pfx.fp_src_addr.ip4));
1142 }
1143 else
1144 {
1145 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1146 sizeof (pfx.fp_grp_addr.ip6));
1147 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1148 sizeof (pfx.fp_src_addr.ip6));
1149 }
1150
1151 return (mroute_add_del_handler (mp->is_add,
1152 mp->is_local,
1153 fib_index, &pfx,
1154 ntohl (mp->entry_flags),
1155 ntohl (mp->next_hop_sw_if_index),
1156 ntohl (mp->itf_flags)));
1157}
1158
1159void
1160vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1161{
1162 vl_api_ip_mroute_add_del_reply_t *rmp;
1163 int rv;
1164 vnet_main_t *vnm = vnet_get_main ();
1165
1166 vnm->api_errno = 0;
1167
1168 rv = api_mroute_add_del_t_handler (mp);
1169
1170 rv = (rv == 0) ? vnm->api_errno : rv;
1171
1172 REPLY_MACRO (VL_API_IP_MROUTE_ADD_DEL_REPLY);
1173}
1174
Dave Barachb5e8a772016-12-06 12:04:42 -05001175static void
1176send_ip_details (vpe_api_main_t * am,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001177 unix_shared_memory_queue_t * q, u32 sw_if_index,
1178 u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001179{
1180 vl_api_ip_details_t *mp;
1181
1182 mp = vl_msg_api_alloc (sizeof (*mp));
1183 memset (mp, 0, sizeof (*mp));
1184 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1185
1186 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001187 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001188 mp->context = context;
1189
1190 vl_msg_api_send_shmem (q, (u8 *) & mp);
1191}
1192
1193static void
1194send_ip_address_details (vpe_api_main_t * am,
1195 unix_shared_memory_queue_t * q,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001196 u8 * ip, u16 prefix_length,
1197 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001198{
1199 vl_api_ip_address_details_t *mp;
1200
1201 mp = vl_msg_api_alloc (sizeof (*mp));
1202 memset (mp, 0, sizeof (*mp));
1203 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1204
1205 if (is_ipv6)
1206 {
1207 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1208 }
1209 else
1210 {
1211 u32 *tp = (u32 *) mp->ip;
1212 *tp = *(u32 *) ip;
1213 }
1214 mp->prefix_length = prefix_length;
1215 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001216 mp->sw_if_index = htonl (sw_if_index);
1217 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001218
1219 vl_msg_api_send_shmem (q, (u8 *) & mp);
1220}
1221
1222static void
1223vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1224{
1225 vpe_api_main_t *am = &vpe_api_main;
1226 unix_shared_memory_queue_t *q;
1227 ip6_address_t *r6;
1228 ip4_address_t *r4;
1229 ip6_main_t *im6 = &ip6_main;
1230 ip4_main_t *im4 = &ip4_main;
1231 ip_lookup_main_t *lm6 = &im6->lookup_main;
1232 ip_lookup_main_t *lm4 = &im4->lookup_main;
1233 ip_interface_address_t *ia = 0;
1234 u32 sw_if_index = ~0;
1235 int rv __attribute__ ((unused)) = 0;
1236
1237 VALIDATE_SW_IF_INDEX (mp);
1238
1239 sw_if_index = ntohl (mp->sw_if_index);
1240
1241 q = vl_api_client_index_to_input_queue (mp->client_index);
1242 if (q == 0)
1243 return;
1244
Dave Barachb5e8a772016-12-06 12:04:42 -05001245 if (mp->is_ipv6)
1246 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001247 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001248 foreach_ip_interface_address (lm6, ia, sw_if_index,
1249 1 /* honor unnumbered */,
1250 ({
1251 r6 = ip_interface_address_get_address (lm6, ia);
1252 u16 prefix_length = ia->address_length;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001253 send_ip_address_details(am, q, (u8*)r6, prefix_length,
1254 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001255 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001256 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001257 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001258 else
1259 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001260 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001261 foreach_ip_interface_address (lm4, ia, sw_if_index,
1262 1 /* honor unnumbered */,
1263 ({
1264 r4 = ip_interface_address_get_address (lm4, ia);
1265 u16 prefix_length = ia->address_length;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001266 send_ip_address_details(am, q, (u8*)r4, prefix_length,
1267 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001268 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001269 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001270 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001271 BAD_SW_IF_INDEX_LABEL;
1272}
1273
1274static void
1275vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1276{
1277 vpe_api_main_t *am = &vpe_api_main;
1278 vnet_main_t *vnm = vnet_get_main ();
1279 vlib_main_t *vm = vlib_get_main ();
1280 vnet_interface_main_t *im = &vnm->interface_main;
1281 unix_shared_memory_queue_t *q;
1282 vnet_sw_interface_t *si, *sorted_sis;
1283 u32 sw_if_index = ~0;
1284
1285 q = vl_api_client_index_to_input_queue (mp->client_index);
1286 if (q == 0)
1287 {
1288 return;
1289 }
1290
1291 /* Gather interfaces. */
1292 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1293 _vec_len (sorted_sis) = 0;
1294 /* *INDENT-OFF* */
1295 pool_foreach (si, im->sw_interfaces,
1296 ({
1297 vec_add1 (sorted_sis, si[0]);
1298 }));
1299 /* *INDENT-ON* */
1300
1301 vec_foreach (si, sorted_sis)
1302 {
1303 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1304 {
1305 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1306 {
1307 continue;
1308 }
1309 sw_if_index = si->sw_if_index;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001310 send_ip_details (am, q, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001311 }
1312 }
1313}
1314
1315static void
1316set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1317{
1318 vl_api_set_ip_flow_hash_reply_t *rmp;
1319 int rv = VNET_API_ERROR_UNIMPLEMENTED;
1320
1321 clib_warning ("unimplemented...");
1322
1323 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1324}
1325
1326static void
1327set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1328{
1329 vl_api_set_ip_flow_hash_reply_t *rmp;
1330 int rv;
1331 u32 table_id;
1332 flow_hash_config_t flow_hash_config = 0;
1333
1334 table_id = ntohl (mp->vrf_id);
1335
1336#define _(a,b) if (mp->a) flow_hash_config |= b;
1337 foreach_flow_hash_bit;
1338#undef _
1339
1340 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1341
1342 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1343}
1344
1345
1346static void
1347vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1348{
1349 if (mp->is_ipv6 == 0)
1350 set_ip4_flow_hash (mp);
1351 else
1352 set_ip6_flow_hash (mp);
1353}
1354
1355static void
1356 vl_api_sw_interface_ip6nd_ra_config_t_handler
1357 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1358{
1359 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1360 vlib_main_t *vm = vlib_get_main ();
1361 int rv = 0;
1362 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1363 default_router;
1364
1365 is_no = mp->is_no == 1;
1366 suppress = mp->suppress == 1;
1367 managed = mp->managed == 1;
1368 other = mp->other == 1;
1369 ll_option = mp->ll_option == 1;
1370 send_unicast = mp->send_unicast == 1;
1371 cease = mp->cease == 1;
1372 default_router = mp->default_router == 1;
1373
1374 VALIDATE_SW_IF_INDEX (mp);
1375
1376 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1377 suppress, managed, other,
1378 ll_option, send_unicast, cease,
1379 default_router, ntohl (mp->lifetime),
1380 ntohl (mp->initial_count),
1381 ntohl (mp->initial_interval),
1382 ntohl (mp->max_interval),
1383 ntohl (mp->min_interval), is_no);
1384
1385 BAD_SW_IF_INDEX_LABEL;
1386
1387 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1388}
1389
1390static void
1391 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1392 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1393{
1394 vlib_main_t *vm = vlib_get_main ();
1395 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1396 int rv = 0;
1397 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1398
1399 VALIDATE_SW_IF_INDEX (mp);
1400
1401 is_no = mp->is_no == 1;
1402 use_default = mp->use_default == 1;
1403 no_advertise = mp->no_advertise == 1;
1404 off_link = mp->off_link == 1;
1405 no_autoconfig = mp->no_autoconfig == 1;
1406 no_onlink = mp->no_onlink == 1;
1407
1408 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1409 (ip6_address_t *) mp->address,
1410 mp->address_length, use_default,
1411 ntohl (mp->val_lifetime),
1412 ntohl (mp->pref_lifetime), no_advertise,
1413 off_link, no_autoconfig, no_onlink, is_no);
1414
1415 BAD_SW_IF_INDEX_LABEL;
1416 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1417}
1418
1419static void
Neale Ranns3f844d02017-02-18 00:03:54 -08001420send_ip6nd_proxy_details (unix_shared_memory_queue_t * q,
1421 u32 context,
1422 const ip46_address_t * addr, u32 sw_if_index)
1423{
1424 vl_api_ip6nd_proxy_details_t *mp;
1425
1426 mp = vl_msg_api_alloc (sizeof (*mp));
1427 memset (mp, 0, sizeof (*mp));
1428 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1429 mp->context = context;
1430 mp->sw_if_index = htonl (sw_if_index);
1431 memcpy (mp->address, addr, 16);
1432
1433 vl_msg_api_send_shmem (q, (u8 *) & mp);
1434}
1435
Neale Ranns3f844d02017-02-18 00:03:54 -08001436typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1437{
1438 u32 *indices;
1439} api_ip6nd_proxy_fib_table_walk_ctx_t;
1440
1441static int
1442api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1443{
1444 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1445
1446 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1447 {
1448 vec_add1 (ctx->indices, fei);
1449 }
1450
1451 return (1);
1452}
1453
1454static void
1455vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1456{
1457 ip6_main_t *im6 = &ip6_main;
1458 fib_table_t *fib_table;
1459 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1460 .indices = NULL,
1461 };
1462 fib_node_index_t *feip;
1463 fib_prefix_t pfx;
1464 unix_shared_memory_queue_t *q;
1465
1466 q = vl_api_client_index_to_input_queue (mp->client_index);
1467 if (q == 0)
1468 {
1469 return;
1470 }
1471
1472 /* *INDENT-OFF* */
1473 pool_foreach (fib_table, im6->fibs,
1474 ({
1475 fib_table_walk(fib_table->ft_index,
1476 FIB_PROTOCOL_IP6,
1477 api_ip6nd_proxy_fib_table_walk,
1478 &ctx);
1479 }));
1480 /* *INDENT-ON* */
1481
1482 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1483
1484 vec_foreach (feip, ctx.indices)
1485 {
1486 fib_entry_get_prefix (*feip, &pfx);
1487
1488 send_ip6nd_proxy_details (q,
1489 mp->context,
1490 &pfx.fp_addr,
1491 fib_entry_get_resolving_interface (*feip));
1492 }
1493
1494 vec_free (ctx.indices);
1495}
1496
1497static void
1498vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1499{
1500 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
1501 int rv = 0;
1502
1503 VALIDATE_SW_IF_INDEX (mp);
1504
1505 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index),
1506 (ip6_address_t *) mp->address, mp->is_del);
1507
1508 BAD_SW_IF_INDEX_LABEL;
1509 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1510}
1511
1512static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001513 vl_api_sw_interface_ip6_enable_disable_t_handler
1514 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1515{
1516 vlib_main_t *vm = vlib_get_main ();
1517 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1518 vnet_main_t *vnm = vnet_get_main ();
1519 int rv = 0;
1520 clib_error_t *error;
1521
1522 vnm->api_errno = 0;
1523
1524 VALIDATE_SW_IF_INDEX (mp);
1525
1526 error =
1527 (mp->enable == 1) ? enable_ip6_interface (vm,
1528 ntohl (mp->sw_if_index)) :
1529 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1530
1531 if (error)
1532 {
1533 clib_error_report (error);
1534 rv = VNET_API_ERROR_UNSPECIFIED;
1535 }
1536 else
1537 {
1538 rv = vnm->api_errno;
1539 }
1540
1541 BAD_SW_IF_INDEX_LABEL;
1542
1543 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1544}
1545
1546static void
1547 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1548 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1549{
1550 vlib_main_t *vm = vlib_get_main ();
1551 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1552 int rv = 0;
1553 clib_error_t *error;
1554 vnet_main_t *vnm = vnet_get_main ();
1555
1556 vnm->api_errno = 0;
1557
1558 VALIDATE_SW_IF_INDEX (mp);
1559
1560 error = set_ip6_link_local_address (vm,
1561 ntohl (mp->sw_if_index),
Neale Ranns75152282017-01-09 01:00:45 -08001562 (ip6_address_t *) mp->address);
Dave Barachb5e8a772016-12-06 12:04:42 -05001563 if (error)
1564 {
1565 clib_error_report (error);
1566 rv = VNET_API_ERROR_UNSPECIFIED;
1567 }
1568 else
1569 {
1570 rv = vnm->api_errno;
1571 }
1572
1573 BAD_SW_IF_INDEX_LABEL;
1574
1575 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1576}
1577
Neale Ranns32e1c012016-11-22 17:07:28 +00001578void
1579vl_mfib_signal_send_one (unix_shared_memory_queue_t * q,
1580 u32 context, const mfib_signal_t * mfs)
1581{
1582 vl_api_mfib_signal_details_t *mp;
1583 mfib_prefix_t prefix;
1584 mfib_table_t *mfib;
1585 mfib_itf_t *mfi;
1586
1587 mp = vl_msg_api_alloc (sizeof (*mp));
1588
1589 memset (mp, 0, sizeof (*mp));
1590 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1591 mp->context = context;
1592
1593 mfi = mfib_itf_get (mfs->mfs_itf);
1594 mfib_entry_get_prefix (mfs->mfs_entry, &prefix);
1595 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
1596 prefix.fp_proto);
1597 mp->table_id = ntohl (mfib->mft_table_id);
1598 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1599
1600 if (FIB_PROTOCOL_IP4 == prefix.fp_proto)
1601 {
1602 mp->grp_address_len = ntohs (prefix.fp_len);
1603
1604 memcpy (mp->grp_address, &prefix.fp_grp_addr.ip4, 4);
1605 if (prefix.fp_len > 32)
1606 {
1607 memcpy (mp->src_address, &prefix.fp_src_addr.ip4, 4);
1608 }
1609 }
1610 else
1611 {
1612 mp->grp_address_len = ntohs (prefix.fp_len);
1613
1614 ASSERT (0);
1615 }
1616
1617 if (0 != mfs->mfs_buffer_len)
1618 {
1619 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1620
1621 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1622 }
1623 else
1624 {
1625 mp->ip_packet_len = 0;
1626 }
1627
1628 vl_msg_api_send_shmem (q, (u8 *) & mp);
1629}
1630
1631static void
1632vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1633{
1634 unix_shared_memory_queue_t *q;
1635
1636 q = vl_api_client_index_to_input_queue (mp->client_index);
1637 if (q == 0)
1638 {
1639 return;
1640 }
1641
1642 while (q->cursize < q->maxsize && mfib_signal_send_one (q, mp->context))
1643 ;
1644}
Dave Barachb5e8a772016-12-06 12:04:42 -05001645
1646#define vl_msg_name_crc_list
1647#include <vnet/ip/ip.api.h>
1648#undef vl_msg_name_crc_list
1649
1650static void
1651setup_message_id_table (api_main_t * am)
1652{
1653#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1654 foreach_vl_msg_name_crc_ip;
1655#undef _
1656}
1657
1658static clib_error_t *
1659ip_api_hookup (vlib_main_t * vm)
1660{
1661 api_main_t *am = &api_main;
1662
1663#define _(N,n) \
1664 vl_msg_api_set_handlers(VL_API_##N, #n, \
1665 vl_api_##n##_t_handler, \
1666 vl_noop_handler, \
1667 vl_api_##n##_t_endian, \
1668 vl_api_##n##_t_print, \
1669 sizeof(vl_api_##n##_t), 1);
1670 foreach_ip_api_msg;
1671#undef _
1672
1673 /*
1674 * Set up the (msg_name, crc, message-id) table
1675 */
1676 setup_message_id_table (am);
1677
1678 return 0;
1679}
1680
1681VLIB_API_INIT_FUNCTION (ip_api_hookup);
1682
1683/*
1684 * fd.io coding-style-patch-verification: ON
1685 *
1686 * Local Variables:
1687 * eval: (c-set-style "gnu")
1688 * End:
1689 */