blob: ab164a5f4673f1b656db7e88496b5cc6794c007c [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) \
75_(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable ) \
76_(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS, \
77 sw_interface_ip6_set_link_local_address)
78
79extern void stats_dslock_with_hint (int hint, int tag);
80extern void stats_dsunlock (void);
81
82static void
83send_ip_neighbor_details (u8 is_ipv6,
84 u8 is_static,
85 u8 * mac_address,
86 u8 * ip_address,
87 unix_shared_memory_queue_t * q, u32 context)
88{
89 vl_api_ip_neighbor_details_t *mp;
90
91 mp = vl_msg_api_alloc (sizeof (*mp));
92 memset (mp, 0, sizeof (*mp));
93 mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS);
94 mp->context = context;
95 mp->is_ipv6 = is_ipv6;
96 mp->is_static = is_static;
97 memcpy (mp->mac_address, mac_address, 6);
98 memcpy (mp->ip_address, ip_address, (is_ipv6) ? 16 : 4);
99
100 vl_msg_api_send_shmem (q, (u8 *) & mp);
101}
102
103static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500104vl_api_ip_neighbor_dump_t_handler (vl_api_ip_neighbor_dump_t * mp)
105{
106 unix_shared_memory_queue_t *q;
107
108 q = vl_api_client_index_to_input_queue (mp->client_index);
109 if (q == 0)
110 return;
111
112 u32 sw_if_index = ntohl (mp->sw_if_index);
113
114 if (mp->is_ipv6)
115 {
116 ip6_neighbor_t *n, *ns;
117
118 ns = ip6_neighbors_entries (sw_if_index);
119 /* *INDENT-OFF* */
120 vec_foreach (n, ns)
121 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500122 send_ip_neighbor_details
123 (mp->is_ipv6, ((n->flags & IP6_NEIGHBOR_FLAG_STATIC) ? 1 : 0),
124 (u8 *) n->link_layer_address,
125 (u8 *) & (n->key.ip6_address.as_u8),
126 q, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500127 }
128 /* *INDENT-ON* */
129 vec_free (ns);
130 }
131 else
132 {
133 ethernet_arp_ip4_entry_t *n, *ns;
134
135 ns = ip4_neighbor_entries (sw_if_index);
136 /* *INDENT-OFF* */
137 vec_foreach (n, ns)
138 {
139 send_ip_neighbor_details (mp->is_ipv6,
140 ((n->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC) ? 1 : 0),
141 (u8*) n->ethernet_address,
142 (u8*) & (n->ip4_address.as_u8),
143 q, mp->context);
144 }
145 /* *INDENT-ON* */
146 vec_free (ns);
147 }
148}
149
150
151void
152copy_fib_next_hop (fib_route_path_encode_t * api_rpath, void *fp_arg)
153{
154 int is_ip4;
155 vl_api_fib_path_t *fp = (vl_api_fib_path_t *) fp_arg;
156
157 if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP4)
158 fp->afi = IP46_TYPE_IP4;
159 else if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP6)
160 fp->afi = IP46_TYPE_IP6;
161 else
162 {
163 is_ip4 = ip46_address_is_ip4 (&api_rpath->rpath.frp_addr);
164 if (is_ip4)
165 fp->afi = IP46_TYPE_IP4;
166 else
167 fp->afi = IP46_TYPE_IP6;
168 }
169 if (fp->afi == IP46_TYPE_IP4)
170 memcpy (fp->next_hop, &api_rpath->rpath.frp_addr.ip4,
171 sizeof (api_rpath->rpath.frp_addr.ip4));
172 else
173 memcpy (fp->next_hop, &api_rpath->rpath.frp_addr.ip6,
174 sizeof (api_rpath->rpath.frp_addr.ip6));
175}
176
177static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500178send_ip_fib_details (vpe_api_main_t * am,
179 unix_shared_memory_queue_t * q,
180 u32 table_id, fib_prefix_t * pfx,
181 fib_route_path_encode_t * api_rpaths, u32 context)
182{
183 vl_api_ip_fib_details_t *mp;
184 fib_route_path_encode_t *api_rpath;
185 vl_api_fib_path_t *fp;
186 int path_count;
187
188 path_count = vec_len (api_rpaths);
189 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
190 if (!mp)
191 return;
192 memset (mp, 0, sizeof (*mp));
193 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
194 mp->context = context;
195
196 mp->table_id = htonl (table_id);
197 mp->address_length = pfx->fp_len;
198 memcpy (mp->address, &pfx->fp_addr.ip4, sizeof (pfx->fp_addr.ip4));
199
200 mp->count = htonl (path_count);
201 fp = mp->path;
202 vec_foreach (api_rpath, api_rpaths)
203 {
204 memset (fp, 0, sizeof (*fp));
205 switch (api_rpath->dpo.dpoi_type)
206 {
207 case DPO_RECEIVE:
208 fp->is_local = true;
209 break;
210 case DPO_DROP:
211 fp->is_drop = true;
212 break;
213 case DPO_IP_NULL:
214 switch (api_rpath->dpo.dpoi_index)
215 {
216 case IP_NULL_ACTION_NONE:
217 fp->is_drop = true;
218 break;
219 case IP_NULL_ACTION_SEND_ICMP_UNREACH:
220 fp->is_unreach = true;
221 break;
222 case IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
223 fp->is_prohibit = true;
224 break;
225 default:
226 break;
227 }
228 break;
229 default:
230 break;
231 }
232 fp->weight = htonl (api_rpath->rpath.frp_weight);
233 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
234 copy_fib_next_hop (api_rpath, fp);
235 fp++;
236 }
237
238 vl_msg_api_send_shmem (q, (u8 *) & mp);
239}
240
241static void
242vl_api_ip_fib_dump_t_handler (vl_api_ip_fib_dump_t * mp)
243{
244 vpe_api_main_t *am = &vpe_api_main;
245 unix_shared_memory_queue_t *q;
246 ip4_main_t *im = &ip4_main;
247 fib_table_t *fib_table;
248 fib_node_index_t lfei, *lfeip, *lfeis = NULL;
249 mpls_label_t key;
250 fib_prefix_t pfx;
251 u32 fib_index;
252 fib_route_path_encode_t *api_rpaths;
253 int i;
254
255 q = vl_api_client_index_to_input_queue (mp->client_index);
256 if (q == 0)
257 return;
258
259 /* *INDENT-OFF* */
260 pool_foreach (fib_table, im->fibs,
261 ({
262 for (i = 0; i < ARRAY_LEN (fib_table->v4.fib_entry_by_dst_address); i++)
263 {
264 hash_foreach(key, lfei, fib_table->v4.fib_entry_by_dst_address[i],
265 ({
266 vec_add1(lfeis, lfei);
267 }));
268 }
269 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500270 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500271
Dave Barachd7cb1b52016-12-09 09:52:16 -0500272 vec_sort_with_function (lfeis, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500273
Dave Barachd7cb1b52016-12-09 09:52:16 -0500274 vec_foreach (lfeip, lfeis)
Dave Barachb5e8a772016-12-06 12:04:42 -0500275 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500276 fib_entry_get_prefix (*lfeip, &pfx);
277 fib_index = fib_entry_get_fib_index (*lfeip);
278 fib_table = fib_table_get (fib_index, pfx.fp_proto);
Dave Barachb5e8a772016-12-06 12:04:42 -0500279 api_rpaths = NULL;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500280 fib_entry_encode (*lfeip, &api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500281 send_ip_fib_details (am, q,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500282 fib_table->ft_table_id, &pfx, api_rpaths,
283 mp->context);
284 vec_free (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500285 }
286
287 vec_free (lfeis);
288}
289
290static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500291send_ip6_fib_details (vpe_api_main_t * am,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500292 unix_shared_memory_queue_t * q,
293 u32 table_id, fib_prefix_t * pfx,
294 fib_route_path_encode_t * api_rpaths, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500295{
296 vl_api_ip6_fib_details_t *mp;
297 fib_route_path_encode_t *api_rpath;
298 vl_api_fib_path_t *fp;
299 int path_count;
300
Dave Barachd7cb1b52016-12-09 09:52:16 -0500301 path_count = vec_len (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500302 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
303 if (!mp)
304 return;
305 memset (mp, 0, sizeof (*mp));
306 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
307 mp->context = context;
308
309 mp->table_id = htonl (table_id);
310 mp->address_length = pfx->fp_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500311 memcpy (mp->address, &pfx->fp_addr.ip6, sizeof (pfx->fp_addr.ip6));
Dave Barachb5e8a772016-12-06 12:04:42 -0500312
313 mp->count = htonl (path_count);
314 fp = mp->path;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500315 vec_foreach (api_rpath, api_rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500316 {
317 memset (fp, 0, sizeof (*fp));
318 switch (api_rpath->dpo.dpoi_type)
319 {
320 case DPO_RECEIVE:
321 fp->is_local = true;
322 break;
323 case DPO_DROP:
324 fp->is_drop = true;
325 break;
326 case DPO_IP_NULL:
327 switch (api_rpath->dpo.dpoi_index)
328 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500329 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_NONE:
Dave Barachb5e8a772016-12-06 12:04:42 -0500330 fp->is_drop = true;
331 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500332 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_SEND_ICMP_UNREACH:
Dave Barachb5e8a772016-12-06 12:04:42 -0500333 fp->is_unreach = true;
334 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500335 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
Dave Barachb5e8a772016-12-06 12:04:42 -0500336 fp->is_prohibit = true;
337 break;
338 default:
339 break;
340 }
341 break;
342 default:
343 break;
344 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500345 fp->weight = htonl (api_rpath->rpath.frp_weight);
346 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500347 copy_fib_next_hop (api_rpath, fp);
348 fp++;
349 }
350
351 vl_msg_api_send_shmem (q, (u8 *) & mp);
352}
353
Dave Barachd7cb1b52016-12-09 09:52:16 -0500354typedef struct apt_ip6_fib_show_ctx_t_
355{
356 u32 fib_index;
357 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500358} api_ip6_fib_show_ctx_t;
359
360static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500361api_ip6_fib_table_put_entries (clib_bihash_kv_24_8_t * kvp, void *arg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500362{
363 api_ip6_fib_show_ctx_t *ctx = arg;
364
365 if ((kvp->key[2] >> 32) == ctx->fib_index)
366 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500367 vec_add1 (ctx->entries, kvp->value);
Dave Barachb5e8a772016-12-06 12:04:42 -0500368 }
369}
370
371static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500372api_ip6_fib_table_get_all (unix_shared_memory_queue_t * q,
373 vl_api_ip6_fib_dump_t * mp,
374 fib_table_t * fib_table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500375{
376 vpe_api_main_t *am = &vpe_api_main;
377 ip6_main_t *im6 = &ip6_main;
378 ip6_fib_t *fib = &fib_table->v6;
379 fib_node_index_t *fib_entry_index;
380 api_ip6_fib_show_ctx_t ctx = {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500381 .fib_index = fib->index,.entries = NULL,
Dave Barachb5e8a772016-12-06 12:04:42 -0500382 };
383 fib_route_path_encode_t *api_rpaths;
384 fib_prefix_t pfx;
385
Dave Barachd7cb1b52016-12-09 09:52:16 -0500386 BV (clib_bihash_foreach_key_value_pair)
387 ((BVT (clib_bihash) *) & im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].
388 ip6_hash, api_ip6_fib_table_put_entries, &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500389
Dave Barachd7cb1b52016-12-09 09:52:16 -0500390 vec_sort_with_function (ctx.entries, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500391
Dave Barachd7cb1b52016-12-09 09:52:16 -0500392 vec_foreach (fib_entry_index, ctx.entries)
393 {
394 fib_entry_get_prefix (*fib_entry_index, &pfx);
395 api_rpaths = NULL;
396 fib_entry_encode (*fib_entry_index, &api_rpaths);
397 send_ip6_fib_details (am, q,
398 fib_table->ft_table_id,
399 &pfx, api_rpaths, mp->context);
400 vec_free (api_rpaths);
401 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500402
Dave Barachd7cb1b52016-12-09 09:52:16 -0500403 vec_free (ctx.entries);
Dave Barachb5e8a772016-12-06 12:04:42 -0500404}
405
406static void
407vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
408{
409 unix_shared_memory_queue_t *q;
410 ip6_main_t *im6 = &ip6_main;
411 fib_table_t *fib_table;
412
413 q = vl_api_client_index_to_input_queue (mp->client_index);
414 if (q == 0)
415 return;
416
417 /* *INDENT-OFF* */
418 pool_foreach (fib_table, im6->fibs,
419 ({
420 api_ip6_fib_table_get_all(q, mp, fib_table);
421 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500422 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500423}
424
425static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800426send_ip_mfib_details (vpe_api_main_t * am,
427 unix_shared_memory_queue_t * q,
428 u32 table_id,
429 mfib_prefix_t * pfx,
430 fib_route_path_encode_t * api_rpaths, u32 context)
431{
432 vl_api_ip_mfib_details_t *mp;
433 fib_route_path_encode_t *api_rpath;
434 vl_api_fib_path_t *fp;
435 int path_count;
436
437 path_count = vec_len (api_rpaths);
438 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
439 if (!mp)
440 return;
441 memset (mp, 0, sizeof (*mp));
442 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
443 mp->context = context;
444
445 mp->table_id = htonl (table_id);
446 mp->address_length = pfx->fp_len;
447 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip4,
448 sizeof (pfx->fp_grp_addr.ip4));
449 memcpy (mp->src_address, &pfx->fp_src_addr.ip4,
450 sizeof (pfx->fp_src_addr.ip4));
451
452 mp->count = htonl (path_count);
453 fp = mp->path;
454 vec_foreach (api_rpath, api_rpaths)
455 {
456 memset (fp, 0, sizeof (*fp));
457
458 fp->weight = 0;
459 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
460 copy_fib_next_hop (api_rpath, fp);
461 fp++;
462 }
463
464 vl_msg_api_send_shmem (q, (u8 *) & mp);
465}
466
467typedef struct vl_api_ip_mfib_dump_ctc_t_
468{
469 fib_node_index_t *entries;
470} vl_api_ip_mfib_dump_ctc_t;
471
472static int
473vl_api_ip_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
474{
475 vl_api_ip_mfib_dump_ctc_t *ctx = arg;
476
477 vec_add1 (ctx->entries, fei);
478
479 return (0);
480}
481
482static void
483vl_api_ip_mfib_dump_t_handler (vl_api_ip_mfib_dump_t * mp)
484{
485 vpe_api_main_t *am = &vpe_api_main;
486 unix_shared_memory_queue_t *q;
487 ip4_main_t *im = &ip4_main;
488 mfib_table_t *mfib_table;
489 fib_node_index_t *mfeip;
490 mfib_prefix_t pfx;
491 fib_route_path_encode_t *api_rpaths = NULL;
492 vl_api_ip_mfib_dump_ctc_t ctx = {
493 .entries = NULL,
494 };
495
496 q = vl_api_client_index_to_input_queue (mp->client_index);
497 if (q == 0)
498 return;
499
500
501 /* *INDENT-OFF* */
502 pool_foreach (mfib_table, im->mfibs,
503 ({
504 ip4_mfib_table_walk(&mfib_table->v4,
505 vl_api_ip_mfib_table_dump_walk,
506 &ctx);
507
508 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
509
510 vec_foreach (mfeip, ctx.entries)
511 {
512 mfib_entry_get_prefix (*mfeip, &pfx);
513 mfib_entry_encode (*mfeip, &api_rpaths);
514 send_ip_mfib_details (am, q,
515 mfib_table->mft_table_id,
516 &pfx, api_rpaths,
517 mp->context);
518 }
519 vec_reset_length (api_rpaths);
520 vec_reset_length (ctx.entries);
521
522 }));
523 /* *INDENT-ON* */
524
525 vec_free (ctx.entries);
526 vec_free (api_rpaths);
527}
528
529static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800530send_ip6_mfib_details (vpe_api_main_t * am,
531 unix_shared_memory_queue_t * q,
532 u32 table_id,
533 mfib_prefix_t * pfx,
534 fib_route_path_encode_t * api_rpaths, u32 context)
535{
536 vl_api_ip6_mfib_details_t *mp;
537 fib_route_path_encode_t *api_rpath;
538 vl_api_fib_path_t *fp;
539 int path_count;
540
541 path_count = vec_len (api_rpaths);
542 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
543 if (!mp)
544 return;
545 memset (mp, 0, sizeof (*mp));
546 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
547 mp->context = context;
548
549 mp->table_id = htonl (table_id);
550 mp->address_length = pfx->fp_len;
551 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip6,
552 sizeof (pfx->fp_grp_addr.ip6));
553 memcpy (mp->src_address, &pfx->fp_src_addr.ip6,
554 sizeof (pfx->fp_src_addr.ip6));
555
556 mp->count = htonl (path_count);
557 fp = mp->path;
558 vec_foreach (api_rpath, api_rpaths)
559 {
560 memset (fp, 0, sizeof (*fp));
561
562 fp->weight = 0;
563 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
564 copy_fib_next_hop (api_rpath, fp);
565 fp++;
566 }
567
568 vl_msg_api_send_shmem (q, (u8 *) & mp);
569}
570
571typedef struct vl_api_ip6_mfib_dump_ctc_t_
572{
573 fib_node_index_t *entries;
574} vl_api_ip6_mfib_dump_ctc_t;
575
576static int
577vl_api_ip6_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
578{
579 vl_api_ip6_mfib_dump_ctc_t *ctx = arg;
580
581 vec_add1 (ctx->entries, fei);
582
583 return (0);
584}
585
586static void
587vl_api_ip6_mfib_dump_t_handler (vl_api_ip6_mfib_dump_t * mp)
588{
589 vpe_api_main_t *am = &vpe_api_main;
590 unix_shared_memory_queue_t *q;
591 ip6_main_t *im = &ip6_main;
592 mfib_table_t *mfib_table;
593 fib_node_index_t *mfeip;
594 mfib_prefix_t pfx;
595 fib_route_path_encode_t *api_rpaths = NULL;
596 vl_api_ip6_mfib_dump_ctc_t ctx = {
597 .entries = NULL,
598 };
599
600 q = vl_api_client_index_to_input_queue (mp->client_index);
601 if (q == 0)
602 return;
603
604
605 /* *INDENT-OFF* */
606 pool_foreach (mfib_table, im->mfibs,
607 ({
608 ip6_mfib_table_walk(&mfib_table->v6,
609 vl_api_ip6_mfib_table_dump_walk,
610 &ctx);
611
612 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
613
614 vec_foreach(mfeip, ctx.entries)
615 {
616 mfib_entry_get_prefix (*mfeip, &pfx);
617 mfib_entry_encode (*mfeip, &api_rpaths);
618 send_ip6_mfib_details (am, q,
619 mfib_table->mft_table_id,
620 &pfx, api_rpaths,
621 mp->context);
622 }
623 vec_reset_length (api_rpaths);
624 vec_reset_length (ctx.entries);
625
626 }));
627 /* *INDENT-ON* */
628
629 vec_free (ctx.entries);
630 vec_free (api_rpaths);
631}
632
633static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500634vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
635 vlib_main_t * vm)
636{
637 vl_api_ip_neighbor_add_del_reply_t *rmp;
638 vnet_main_t *vnm = vnet_get_main ();
639 int rv = 0;
640
641 VALIDATE_SW_IF_INDEX (mp);
642
643 stats_dslock_with_hint (1 /* release hint */ , 7 /* tag */ );
644
645 /*
646 * there's no validation here of the ND/ARP entry being added.
647 * The expectation is that the FIB will ensure that nothing bad
648 * will come of adding bogus entries.
649 */
650 if (mp->is_ipv6)
651 {
652 if (mp->is_add)
653 rv = vnet_set_ip6_ethernet_neighbor
654 (vm, ntohl (mp->sw_if_index),
655 (ip6_address_t *) (mp->dst_address),
656 mp->mac_address, sizeof (mp->mac_address), mp->is_static);
657 else
658 rv = vnet_unset_ip6_ethernet_neighbor
659 (vm, ntohl (mp->sw_if_index),
660 (ip6_address_t *) (mp->dst_address),
661 mp->mac_address, sizeof (mp->mac_address));
662 }
663 else
664 {
665 ethernet_arp_ip4_over_ethernet_address_t a;
666
667 clib_memcpy (&a.ethernet, mp->mac_address, 6);
668 clib_memcpy (&a.ip4, mp->dst_address, 4);
669
670 if (mp->is_add)
671 rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index),
672 &a, mp->is_static);
673 else
674 rv =
675 vnet_arp_unset_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index), &a);
676 }
677
678 BAD_SW_IF_INDEX_LABEL;
679
680 stats_dsunlock ();
681 REPLY_MACRO (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
682}
683
684int
685add_del_route_t_handler (u8 is_multipath,
686 u8 is_add,
687 u8 is_drop,
688 u8 is_unreach,
689 u8 is_prohibit,
690 u8 is_local,
691 u8 is_classify,
692 u32 classify_table_index,
693 u8 is_resolve_host,
694 u8 is_resolve_attached,
695 u32 fib_index,
696 const fib_prefix_t * prefix,
697 u8 next_hop_proto_is_ip4,
698 const ip46_address_t * next_hop,
699 u32 next_hop_sw_if_index,
700 u8 next_hop_fib_index,
701 u32 next_hop_weight,
702 mpls_label_t next_hop_via_label,
703 mpls_label_t * next_hop_out_label_stack)
704{
705 vnet_classify_main_t *cm = &vnet_classify_main;
706 fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
707 fib_route_path_t path = {
708 .frp_proto = (next_hop_proto_is_ip4 ?
709 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6),
710 .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
711 .frp_sw_if_index = next_hop_sw_if_index,
712 .frp_fib_index = next_hop_fib_index,
713 .frp_weight = next_hop_weight,
714 .frp_label_stack = next_hop_out_label_stack,
715 };
716 fib_route_path_t *paths = NULL;
717
718 if (MPLS_LABEL_INVALID != next_hop_via_label)
719 {
720 path.frp_proto = FIB_PROTOCOL_MPLS;
721 path.frp_local_label = next_hop_via_label;
722 }
723 if (is_resolve_host)
724 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
725 if (is_resolve_attached)
726 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
727
728 path.frp_flags = path_flags;
729
730 if (is_multipath)
731 {
732 stats_dslock_with_hint (1 /* release hint */ , 10 /* tag */ );
733
734
735 vec_add1 (paths, path);
736
737 if (is_add)
738 fib_table_entry_path_add2 (fib_index,
739 prefix,
740 FIB_SOURCE_API,
741 FIB_ENTRY_FLAG_NONE, paths);
742 else
743 fib_table_entry_path_remove2 (fib_index,
744 prefix, FIB_SOURCE_API, paths);
745
746 vec_free (paths);
747 stats_dsunlock ();
748 return 0;
749 }
750
751 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
752
753 if (is_drop || is_local || is_classify || is_unreach || is_prohibit)
754 {
755 /*
756 * special route types that link directly to the adj
757 */
758 if (is_add)
759 {
760 dpo_id_t dpo = DPO_INVALID;
761 dpo_proto_t dproto;
762
763 dproto = fib_proto_to_dpo (prefix->fp_proto);
764
765 if (is_drop)
766 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
767 else if (is_local)
768 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
769 else if (is_unreach)
770 ip_null_dpo_add_and_lock (dproto,
771 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
772 else if (is_prohibit)
773 ip_null_dpo_add_and_lock (dproto,
774 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
775 &dpo);
776 else if (is_classify)
777 {
778 if (pool_is_free_index (cm->tables,
779 ntohl (classify_table_index)))
780 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500781 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500782 return VNET_API_ERROR_NO_SUCH_TABLE;
783 }
784
785 dpo_set (&dpo, DPO_CLASSIFY, dproto,
786 classify_dpo_create (dproto,
787 ntohl (classify_table_index)));
788 }
789 else
790 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500791 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500792 return VNET_API_ERROR_NO_SUCH_TABLE;
793 }
794
795 fib_table_entry_special_dpo_update (fib_index,
796 prefix,
797 FIB_SOURCE_API,
798 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
799 dpo_reset (&dpo);
800 }
801 else
802 {
803 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
804 }
805 }
806 else
807 {
808 if (is_add)
809 {
810 vec_add1 (paths, path);
811 fib_table_entry_update (fib_index,
812 prefix,
813 FIB_SOURCE_API, FIB_ENTRY_FLAG_NONE, paths);
814 vec_free (paths);
815 }
816 else
817 {
818 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
819 }
820 }
821
Dave Barachd7cb1b52016-12-09 09:52:16 -0500822 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500823 return (0);
824}
825
826int
827add_del_route_check (fib_protocol_t table_proto,
828 u32 table_id,
829 u32 next_hop_sw_if_index,
830 fib_protocol_t next_hop_table_proto,
831 u32 next_hop_table_id,
832 u8 create_missing_tables,
833 u32 * fib_index, u32 * next_hop_fib_index)
834{
835 vnet_main_t *vnm = vnet_get_main ();
836
837 *fib_index = fib_table_find (table_proto, ntohl (table_id));
838 if (~0 == *fib_index)
839 {
840 if (create_missing_tables)
841 {
842 *fib_index = fib_table_find_or_create_and_lock (table_proto,
843 ntohl (table_id));
844 }
845 else
846 {
847 /* No such VRF, and we weren't asked to create one */
848 return VNET_API_ERROR_NO_SUCH_FIB;
849 }
850 }
851
852 if (~0 != ntohl (next_hop_sw_if_index))
853 {
854 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
855 ntohl (next_hop_sw_if_index)))
856 {
857 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
858 }
859 }
860 else
861 {
862 *next_hop_fib_index = fib_table_find (next_hop_table_proto,
863 ntohl (next_hop_table_id));
864
865 if (~0 == *next_hop_fib_index)
866 {
867 if (create_missing_tables)
868 {
869 *next_hop_fib_index =
870 fib_table_find_or_create_and_lock (next_hop_table_proto,
871 ntohl (next_hop_table_id));
872 }
873 else
874 {
875 /* No such VRF, and we weren't asked to create one */
876 return VNET_API_ERROR_NO_SUCH_FIB;
877 }
878 }
879 }
880
881 return (0);
882}
883
884static int
885ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
886{
887 u32 fib_index, next_hop_fib_index;
888 mpls_label_t *label_stack = NULL;
889 int rv, ii, n_labels;;
890
891 rv = add_del_route_check (FIB_PROTOCOL_IP4,
892 mp->table_id,
893 mp->next_hop_sw_if_index,
894 FIB_PROTOCOL_IP4,
895 mp->next_hop_table_id,
896 mp->create_vrf_if_needed,
897 &fib_index, &next_hop_fib_index);
898
899 if (0 != rv)
900 return (rv);
901
902 fib_prefix_t pfx = {
903 .fp_len = mp->dst_address_length,
904 .fp_proto = FIB_PROTOCOL_IP4,
905 };
906 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
907
908 ip46_address_t nh;
909 memset (&nh, 0, sizeof (nh));
910 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
911
912 n_labels = mp->next_hop_n_out_labels;
913 if (n_labels == 0)
914 ;
915 else if (1 == n_labels)
916 vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
917 else
918 {
919 vec_validate (label_stack, n_labels - 1);
920 for (ii = 0; ii < n_labels; ii++)
921 label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
922 }
923
924 return (add_del_route_t_handler (mp->is_multipath,
925 mp->is_add,
926 mp->is_drop,
927 mp->is_unreach,
928 mp->is_prohibit,
929 mp->is_local,
930 mp->is_classify,
931 mp->classify_table_index,
932 mp->is_resolve_host,
933 mp->is_resolve_attached,
934 fib_index, &pfx, 1,
935 &nh,
936 ntohl (mp->next_hop_sw_if_index),
937 next_hop_fib_index,
938 mp->next_hop_weight,
939 ntohl (mp->next_hop_via_label),
940 label_stack));
941}
942
943static int
944ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
945{
946 u32 fib_index, next_hop_fib_index;
947 mpls_label_t *label_stack = NULL;
948 int rv, ii, n_labels;;
949
950 rv = add_del_route_check (FIB_PROTOCOL_IP6,
951 mp->table_id,
952 mp->next_hop_sw_if_index,
953 FIB_PROTOCOL_IP6,
954 mp->next_hop_table_id,
955 mp->create_vrf_if_needed,
956 &fib_index, &next_hop_fib_index);
957
958 if (0 != rv)
959 return (rv);
960
961 fib_prefix_t pfx = {
962 .fp_len = mp->dst_address_length,
963 .fp_proto = FIB_PROTOCOL_IP6,
964 };
965 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
966
967 ip46_address_t nh;
968 memset (&nh, 0, sizeof (nh));
969 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
970
971 n_labels = mp->next_hop_n_out_labels;
972 if (n_labels == 0)
973 ;
974 else if (1 == n_labels)
975 vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
976 else
977 {
978 vec_validate (label_stack, n_labels - 1);
979 for (ii = 0; ii < n_labels; ii++)
980 label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
981 }
982
983 return (add_del_route_t_handler (mp->is_multipath,
984 mp->is_add,
985 mp->is_drop,
986 mp->is_unreach,
987 mp->is_prohibit,
988 mp->is_local,
989 mp->is_classify,
990 mp->classify_table_index,
991 mp->is_resolve_host,
992 mp->is_resolve_attached,
993 fib_index, &pfx, 0,
994 &nh, ntohl (mp->next_hop_sw_if_index),
995 next_hop_fib_index,
996 mp->next_hop_weight,
997 ntohl (mp->next_hop_via_label),
998 label_stack));
999}
1000
1001void
1002vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1003{
1004 vl_api_ip_add_del_route_reply_t *rmp;
1005 int rv;
1006 vnet_main_t *vnm = vnet_get_main ();
1007
1008 vnm->api_errno = 0;
1009
1010 if (mp->is_ipv6)
1011 rv = ip6_add_del_route_t_handler (mp);
1012 else
1013 rv = ip4_add_del_route_t_handler (mp);
1014
1015 rv = (rv == 0) ? vnm->api_errno : rv;
1016
1017 REPLY_MACRO (VL_API_IP_ADD_DEL_ROUTE_REPLY);
1018}
1019
Neale Ranns32e1c012016-11-22 17:07:28 +00001020static int
1021add_del_mroute_check (fib_protocol_t table_proto,
1022 u32 table_id,
1023 u32 next_hop_sw_if_index,
1024 u8 is_local, u8 create_missing_tables, u32 * fib_index)
1025{
1026 vnet_main_t *vnm = vnet_get_main ();
1027
1028 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1029 if (~0 == *fib_index)
1030 {
1031 if (create_missing_tables)
1032 {
1033 *fib_index = mfib_table_find_or_create_and_lock (table_proto,
1034 ntohl (table_id));
1035 }
1036 else
1037 {
1038 /* No such VRF, and we weren't asked to create one */
1039 return VNET_API_ERROR_NO_SUCH_FIB;
1040 }
1041 }
1042
1043 if (~0 != ntohl (next_hop_sw_if_index))
1044 {
1045 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1046 ntohl (next_hop_sw_if_index)))
1047 {
1048 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1049 }
1050 }
1051
1052 return (0);
1053}
1054
1055static int
1056mroute_add_del_handler (u8 is_add,
1057 u8 is_local,
1058 u32 fib_index,
1059 const mfib_prefix_t * prefix,
1060 u32 entry_flags,
1061 u32 next_hop_sw_if_index, u32 itf_flags)
1062{
1063 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1064
1065 fib_route_path_t path = {
1066 .frp_sw_if_index = next_hop_sw_if_index,
1067 .frp_proto = prefix->fp_proto,
1068 };
1069
1070 if (is_local)
1071 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1072
1073
1074 if (!is_local && ~0 == next_hop_sw_if_index)
1075 {
1076 mfib_table_entry_update (fib_index, prefix,
1077 MFIB_SOURCE_API, entry_flags);
1078 }
1079 else
1080 {
1081 if (is_add)
1082 {
1083 mfib_table_entry_path_update (fib_index, prefix,
1084 MFIB_SOURCE_API, &path, itf_flags);
1085 }
1086 else
1087 {
1088 mfib_table_entry_path_remove (fib_index, prefix,
1089 MFIB_SOURCE_API, &path);
1090 }
1091 }
1092
1093 stats_dsunlock ();
1094 return (0);
1095}
1096
1097static int
1098api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1099{
1100 fib_protocol_t fproto;
1101 u32 fib_index;
1102 int rv;
1103
1104 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1105 rv = add_del_mroute_check (fproto,
1106 mp->table_id,
1107 mp->next_hop_sw_if_index,
1108 mp->is_local,
1109 mp->create_vrf_if_needed, &fib_index);
1110
1111 if (0 != rv)
1112 return (rv);
1113
1114 mfib_prefix_t pfx = {
1115 .fp_len = ntohs (mp->grp_address_length),
1116 .fp_proto = fproto,
1117 };
1118
1119 if (FIB_PROTOCOL_IP4 == fproto)
1120 {
1121 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1122 sizeof (pfx.fp_grp_addr.ip4));
1123 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1124 sizeof (pfx.fp_src_addr.ip4));
1125 }
1126 else
1127 {
1128 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1129 sizeof (pfx.fp_grp_addr.ip6));
1130 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1131 sizeof (pfx.fp_src_addr.ip6));
1132 }
1133
1134 return (mroute_add_del_handler (mp->is_add,
1135 mp->is_local,
1136 fib_index, &pfx,
1137 ntohl (mp->entry_flags),
1138 ntohl (mp->next_hop_sw_if_index),
1139 ntohl (mp->itf_flags)));
1140}
1141
1142void
1143vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1144{
1145 vl_api_ip_mroute_add_del_reply_t *rmp;
1146 int rv;
1147 vnet_main_t *vnm = vnet_get_main ();
1148
1149 vnm->api_errno = 0;
1150
1151 rv = api_mroute_add_del_t_handler (mp);
1152
1153 rv = (rv == 0) ? vnm->api_errno : rv;
1154
1155 REPLY_MACRO (VL_API_IP_MROUTE_ADD_DEL_REPLY);
1156}
1157
Dave Barachb5e8a772016-12-06 12:04:42 -05001158static void
1159send_ip_details (vpe_api_main_t * am,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001160 unix_shared_memory_queue_t * q, u32 sw_if_index,
1161 u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001162{
1163 vl_api_ip_details_t *mp;
1164
1165 mp = vl_msg_api_alloc (sizeof (*mp));
1166 memset (mp, 0, sizeof (*mp));
1167 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1168
1169 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001170 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001171 mp->context = context;
1172
1173 vl_msg_api_send_shmem (q, (u8 *) & mp);
1174}
1175
1176static void
1177send_ip_address_details (vpe_api_main_t * am,
1178 unix_shared_memory_queue_t * q,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001179 u8 * ip, u16 prefix_length,
1180 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001181{
1182 vl_api_ip_address_details_t *mp;
1183
1184 mp = vl_msg_api_alloc (sizeof (*mp));
1185 memset (mp, 0, sizeof (*mp));
1186 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1187
1188 if (is_ipv6)
1189 {
1190 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1191 }
1192 else
1193 {
1194 u32 *tp = (u32 *) mp->ip;
1195 *tp = *(u32 *) ip;
1196 }
1197 mp->prefix_length = prefix_length;
1198 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001199 mp->sw_if_index = htonl (sw_if_index);
1200 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001201
1202 vl_msg_api_send_shmem (q, (u8 *) & mp);
1203}
1204
1205static void
1206vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1207{
1208 vpe_api_main_t *am = &vpe_api_main;
1209 unix_shared_memory_queue_t *q;
1210 ip6_address_t *r6;
1211 ip4_address_t *r4;
1212 ip6_main_t *im6 = &ip6_main;
1213 ip4_main_t *im4 = &ip4_main;
1214 ip_lookup_main_t *lm6 = &im6->lookup_main;
1215 ip_lookup_main_t *lm4 = &im4->lookup_main;
1216 ip_interface_address_t *ia = 0;
1217 u32 sw_if_index = ~0;
1218 int rv __attribute__ ((unused)) = 0;
1219
1220 VALIDATE_SW_IF_INDEX (mp);
1221
1222 sw_if_index = ntohl (mp->sw_if_index);
1223
1224 q = vl_api_client_index_to_input_queue (mp->client_index);
1225 if (q == 0)
1226 return;
1227
Dave Barachb5e8a772016-12-06 12:04:42 -05001228 if (mp->is_ipv6)
1229 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001230 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001231 foreach_ip_interface_address (lm6, ia, sw_if_index,
1232 1 /* honor unnumbered */,
1233 ({
1234 r6 = ip_interface_address_get_address (lm6, ia);
1235 u16 prefix_length = ia->address_length;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001236 send_ip_address_details(am, q, (u8*)r6, prefix_length,
1237 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001238 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001239 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001240 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001241 else
1242 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001243 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001244 foreach_ip_interface_address (lm4, ia, sw_if_index,
1245 1 /* honor unnumbered */,
1246 ({
1247 r4 = ip_interface_address_get_address (lm4, ia);
1248 u16 prefix_length = ia->address_length;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001249 send_ip_address_details(am, q, (u8*)r4, prefix_length,
1250 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001251 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001252 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001253 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001254 BAD_SW_IF_INDEX_LABEL;
1255}
1256
1257static void
1258vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1259{
1260 vpe_api_main_t *am = &vpe_api_main;
1261 vnet_main_t *vnm = vnet_get_main ();
1262 vlib_main_t *vm = vlib_get_main ();
1263 vnet_interface_main_t *im = &vnm->interface_main;
1264 unix_shared_memory_queue_t *q;
1265 vnet_sw_interface_t *si, *sorted_sis;
1266 u32 sw_if_index = ~0;
1267
1268 q = vl_api_client_index_to_input_queue (mp->client_index);
1269 if (q == 0)
1270 {
1271 return;
1272 }
1273
1274 /* Gather interfaces. */
1275 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1276 _vec_len (sorted_sis) = 0;
1277 /* *INDENT-OFF* */
1278 pool_foreach (si, im->sw_interfaces,
1279 ({
1280 vec_add1 (sorted_sis, si[0]);
1281 }));
1282 /* *INDENT-ON* */
1283
1284 vec_foreach (si, sorted_sis)
1285 {
1286 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1287 {
1288 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1289 {
1290 continue;
1291 }
1292 sw_if_index = si->sw_if_index;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001293 send_ip_details (am, q, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001294 }
1295 }
1296}
1297
1298static void
1299set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1300{
1301 vl_api_set_ip_flow_hash_reply_t *rmp;
1302 int rv = VNET_API_ERROR_UNIMPLEMENTED;
1303
1304 clib_warning ("unimplemented...");
1305
1306 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1307}
1308
1309static void
1310set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1311{
1312 vl_api_set_ip_flow_hash_reply_t *rmp;
1313 int rv;
1314 u32 table_id;
1315 flow_hash_config_t flow_hash_config = 0;
1316
1317 table_id = ntohl (mp->vrf_id);
1318
1319#define _(a,b) if (mp->a) flow_hash_config |= b;
1320 foreach_flow_hash_bit;
1321#undef _
1322
1323 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1324
1325 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1326}
1327
1328
1329static void
1330vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1331{
1332 if (mp->is_ipv6 == 0)
1333 set_ip4_flow_hash (mp);
1334 else
1335 set_ip6_flow_hash (mp);
1336}
1337
1338static void
1339 vl_api_sw_interface_ip6nd_ra_config_t_handler
1340 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1341{
1342 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1343 vlib_main_t *vm = vlib_get_main ();
1344 int rv = 0;
1345 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1346 default_router;
1347
1348 is_no = mp->is_no == 1;
1349 suppress = mp->suppress == 1;
1350 managed = mp->managed == 1;
1351 other = mp->other == 1;
1352 ll_option = mp->ll_option == 1;
1353 send_unicast = mp->send_unicast == 1;
1354 cease = mp->cease == 1;
1355 default_router = mp->default_router == 1;
1356
1357 VALIDATE_SW_IF_INDEX (mp);
1358
1359 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1360 suppress, managed, other,
1361 ll_option, send_unicast, cease,
1362 default_router, ntohl (mp->lifetime),
1363 ntohl (mp->initial_count),
1364 ntohl (mp->initial_interval),
1365 ntohl (mp->max_interval),
1366 ntohl (mp->min_interval), is_no);
1367
1368 BAD_SW_IF_INDEX_LABEL;
1369
1370 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1371}
1372
1373static void
1374 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1375 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1376{
1377 vlib_main_t *vm = vlib_get_main ();
1378 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1379 int rv = 0;
1380 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1381
1382 VALIDATE_SW_IF_INDEX (mp);
1383
1384 is_no = mp->is_no == 1;
1385 use_default = mp->use_default == 1;
1386 no_advertise = mp->no_advertise == 1;
1387 off_link = mp->off_link == 1;
1388 no_autoconfig = mp->no_autoconfig == 1;
1389 no_onlink = mp->no_onlink == 1;
1390
1391 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1392 (ip6_address_t *) mp->address,
1393 mp->address_length, use_default,
1394 ntohl (mp->val_lifetime),
1395 ntohl (mp->pref_lifetime), no_advertise,
1396 off_link, no_autoconfig, no_onlink, is_no);
1397
1398 BAD_SW_IF_INDEX_LABEL;
1399 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1400}
1401
1402static void
1403 vl_api_sw_interface_ip6_enable_disable_t_handler
1404 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1405{
1406 vlib_main_t *vm = vlib_get_main ();
1407 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1408 vnet_main_t *vnm = vnet_get_main ();
1409 int rv = 0;
1410 clib_error_t *error;
1411
1412 vnm->api_errno = 0;
1413
1414 VALIDATE_SW_IF_INDEX (mp);
1415
1416 error =
1417 (mp->enable == 1) ? enable_ip6_interface (vm,
1418 ntohl (mp->sw_if_index)) :
1419 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1420
1421 if (error)
1422 {
1423 clib_error_report (error);
1424 rv = VNET_API_ERROR_UNSPECIFIED;
1425 }
1426 else
1427 {
1428 rv = vnm->api_errno;
1429 }
1430
1431 BAD_SW_IF_INDEX_LABEL;
1432
1433 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1434}
1435
1436static void
1437 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1438 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1439{
1440 vlib_main_t *vm = vlib_get_main ();
1441 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1442 int rv = 0;
1443 clib_error_t *error;
1444 vnet_main_t *vnm = vnet_get_main ();
1445
1446 vnm->api_errno = 0;
1447
1448 VALIDATE_SW_IF_INDEX (mp);
1449
1450 error = set_ip6_link_local_address (vm,
1451 ntohl (mp->sw_if_index),
Neale Ranns75152282017-01-09 01:00:45 -08001452 (ip6_address_t *) mp->address);
Dave Barachb5e8a772016-12-06 12:04:42 -05001453 if (error)
1454 {
1455 clib_error_report (error);
1456 rv = VNET_API_ERROR_UNSPECIFIED;
1457 }
1458 else
1459 {
1460 rv = vnm->api_errno;
1461 }
1462
1463 BAD_SW_IF_INDEX_LABEL;
1464
1465 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1466}
1467
Neale Ranns32e1c012016-11-22 17:07:28 +00001468void
1469vl_mfib_signal_send_one (unix_shared_memory_queue_t * q,
1470 u32 context, const mfib_signal_t * mfs)
1471{
1472 vl_api_mfib_signal_details_t *mp;
1473 mfib_prefix_t prefix;
1474 mfib_table_t *mfib;
1475 mfib_itf_t *mfi;
1476
1477 mp = vl_msg_api_alloc (sizeof (*mp));
1478
1479 memset (mp, 0, sizeof (*mp));
1480 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1481 mp->context = context;
1482
1483 mfi = mfib_itf_get (mfs->mfs_itf);
1484 mfib_entry_get_prefix (mfs->mfs_entry, &prefix);
1485 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
1486 prefix.fp_proto);
1487 mp->table_id = ntohl (mfib->mft_table_id);
1488 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1489
1490 if (FIB_PROTOCOL_IP4 == prefix.fp_proto)
1491 {
1492 mp->grp_address_len = ntohs (prefix.fp_len);
1493
1494 memcpy (mp->grp_address, &prefix.fp_grp_addr.ip4, 4);
1495 if (prefix.fp_len > 32)
1496 {
1497 memcpy (mp->src_address, &prefix.fp_src_addr.ip4, 4);
1498 }
1499 }
1500 else
1501 {
1502 mp->grp_address_len = ntohs (prefix.fp_len);
1503
1504 ASSERT (0);
1505 }
1506
1507 if (0 != mfs->mfs_buffer_len)
1508 {
1509 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1510
1511 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1512 }
1513 else
1514 {
1515 mp->ip_packet_len = 0;
1516 }
1517
1518 vl_msg_api_send_shmem (q, (u8 *) & mp);
1519}
1520
1521static void
1522vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1523{
1524 unix_shared_memory_queue_t *q;
1525
1526 q = vl_api_client_index_to_input_queue (mp->client_index);
1527 if (q == 0)
1528 {
1529 return;
1530 }
1531
1532 while (q->cursize < q->maxsize && mfib_signal_send_one (q, mp->context))
1533 ;
1534}
Dave Barachb5e8a772016-12-06 12:04:42 -05001535
1536#define vl_msg_name_crc_list
1537#include <vnet/ip/ip.api.h>
1538#undef vl_msg_name_crc_list
1539
1540static void
1541setup_message_id_table (api_main_t * am)
1542{
1543#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1544 foreach_vl_msg_name_crc_ip;
1545#undef _
1546}
1547
1548static clib_error_t *
1549ip_api_hookup (vlib_main_t * vm)
1550{
1551 api_main_t *am = &api_main;
1552
1553#define _(N,n) \
1554 vl_msg_api_set_handlers(VL_API_##N, #n, \
1555 vl_api_##n##_t_handler, \
1556 vl_noop_handler, \
1557 vl_api_##n##_t_endian, \
1558 vl_api_##n##_t_print, \
1559 sizeof(vl_api_##n##_t), 1);
1560 foreach_ip_api_msg;
1561#undef _
1562
1563 /*
1564 * Set up the (msg_name, crc, message-id) table
1565 */
1566 setup_message_id_table (am);
1567
1568 return 0;
1569}
1570
1571VLIB_API_INIT_FUNCTION (ip_api_hookup);
1572
1573/*
1574 * fd.io coding-style-patch-verification: ON
1575 *
1576 * Local Variables:
1577 * eval: (c-set-style "gnu")
1578 * End:
1579 */