blob: d9ec4b479980fd902844b3683b755e93603fd28d [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) \
Neale Ranns28ab9cc2017-08-14 07:18:42 -070072_(IP_TABLE_ADD_DEL, ip_table_add_del) \
Neale Rannsd91c1db2017-07-31 02:30:50 -070073_(IP_PUNT_POLICE, ip_punt_police) \
74_(IP_PUNT_REDIRECT, ip_punt_redirect) \
Dave Barachb5e8a772016-12-06 12:04:42 -050075_(SET_IP_FLOW_HASH,set_ip_flow_hash) \
76_(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config) \
77_(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix) \
Neale Ranns3f844d02017-02-18 00:03:54 -080078_(IP6ND_PROXY_ADD_DEL, ip6nd_proxy_add_del) \
79_(IP6ND_PROXY_DUMP, ip6nd_proxy_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050080_(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable ) \
81_(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS, \
82 sw_interface_ip6_set_link_local_address)
83
84extern void stats_dslock_with_hint (int hint, int tag);
85extern void stats_dsunlock (void);
86
87static void
88send_ip_neighbor_details (u8 is_ipv6,
89 u8 is_static,
90 u8 * mac_address,
91 u8 * ip_address,
92 unix_shared_memory_queue_t * q, u32 context)
93{
94 vl_api_ip_neighbor_details_t *mp;
95
96 mp = vl_msg_api_alloc (sizeof (*mp));
97 memset (mp, 0, sizeof (*mp));
98 mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS);
99 mp->context = context;
100 mp->is_ipv6 = is_ipv6;
101 mp->is_static = is_static;
102 memcpy (mp->mac_address, mac_address, 6);
103 memcpy (mp->ip_address, ip_address, (is_ipv6) ? 16 : 4);
104
105 vl_msg_api_send_shmem (q, (u8 *) & mp);
106}
107
108static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500109vl_api_ip_neighbor_dump_t_handler (vl_api_ip_neighbor_dump_t * mp)
110{
111 unix_shared_memory_queue_t *q;
112
113 q = vl_api_client_index_to_input_queue (mp->client_index);
114 if (q == 0)
115 return;
116
117 u32 sw_if_index = ntohl (mp->sw_if_index);
118
119 if (mp->is_ipv6)
120 {
121 ip6_neighbor_t *n, *ns;
122
123 ns = ip6_neighbors_entries (sw_if_index);
124 /* *INDENT-OFF* */
125 vec_foreach (n, ns)
126 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500127 send_ip_neighbor_details
128 (mp->is_ipv6, ((n->flags & IP6_NEIGHBOR_FLAG_STATIC) ? 1 : 0),
129 (u8 *) n->link_layer_address,
130 (u8 *) & (n->key.ip6_address.as_u8),
131 q, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500132 }
133 /* *INDENT-ON* */
134 vec_free (ns);
135 }
136 else
137 {
138 ethernet_arp_ip4_entry_t *n, *ns;
139
140 ns = ip4_neighbor_entries (sw_if_index);
141 /* *INDENT-OFF* */
142 vec_foreach (n, ns)
143 {
144 send_ip_neighbor_details (mp->is_ipv6,
145 ((n->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC) ? 1 : 0),
146 (u8*) n->ethernet_address,
147 (u8*) & (n->ip4_address.as_u8),
148 q, mp->context);
149 }
150 /* *INDENT-ON* */
151 vec_free (ns);
152 }
153}
154
155
156void
157copy_fib_next_hop (fib_route_path_encode_t * api_rpath, void *fp_arg)
158{
159 int is_ip4;
160 vl_api_fib_path_t *fp = (vl_api_fib_path_t *) fp_arg;
161
Neale Rannsda78f952017-05-24 09:15:43 -0700162 if (api_rpath->rpath.frp_proto == DPO_PROTO_IP4)
Dave Barachb5e8a772016-12-06 12:04:42 -0500163 fp->afi = IP46_TYPE_IP4;
Neale Rannsda78f952017-05-24 09:15:43 -0700164 else if (api_rpath->rpath.frp_proto == DPO_PROTO_IP6)
Dave Barachb5e8a772016-12-06 12:04:42 -0500165 fp->afi = IP46_TYPE_IP6;
166 else
167 {
168 is_ip4 = ip46_address_is_ip4 (&api_rpath->rpath.frp_addr);
169 if (is_ip4)
170 fp->afi = IP46_TYPE_IP4;
171 else
172 fp->afi = IP46_TYPE_IP6;
173 }
174 if (fp->afi == IP46_TYPE_IP4)
175 memcpy (fp->next_hop, &api_rpath->rpath.frp_addr.ip4,
176 sizeof (api_rpath->rpath.frp_addr.ip4));
177 else
178 memcpy (fp->next_hop, &api_rpath->rpath.frp_addr.ip6,
179 sizeof (api_rpath->rpath.frp_addr.ip6));
180}
181
182static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500183send_ip_fib_details (vpe_api_main_t * am,
184 unix_shared_memory_queue_t * q,
Neale Ranns2297af02017-09-12 09:45:04 -0700185 const fib_table_t * table,
186 const fib_prefix_t * pfx,
Dave Barachb5e8a772016-12-06 12:04:42 -0500187 fib_route_path_encode_t * api_rpaths, u32 context)
188{
189 vl_api_ip_fib_details_t *mp;
190 fib_route_path_encode_t *api_rpath;
191 vl_api_fib_path_t *fp;
192 int path_count;
193
194 path_count = vec_len (api_rpaths);
195 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
196 if (!mp)
197 return;
198 memset (mp, 0, sizeof (*mp));
199 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
200 mp->context = context;
201
Neale Ranns2297af02017-09-12 09:45:04 -0700202 mp->table_id = htonl (table->ft_table_id);
203 memcpy (mp->table_name, table->ft_desc,
204 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500205 mp->address_length = pfx->fp_len;
206 memcpy (mp->address, &pfx->fp_addr.ip4, sizeof (pfx->fp_addr.ip4));
207
208 mp->count = htonl (path_count);
209 fp = mp->path;
210 vec_foreach (api_rpath, api_rpaths)
211 {
212 memset (fp, 0, sizeof (*fp));
213 switch (api_rpath->dpo.dpoi_type)
214 {
215 case DPO_RECEIVE:
216 fp->is_local = true;
217 break;
218 case DPO_DROP:
219 fp->is_drop = true;
220 break;
221 case DPO_IP_NULL:
222 switch (api_rpath->dpo.dpoi_index)
223 {
224 case IP_NULL_ACTION_NONE:
225 fp->is_drop = true;
226 break;
227 case IP_NULL_ACTION_SEND_ICMP_UNREACH:
228 fp->is_unreach = true;
229 break;
230 case IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
231 fp->is_prohibit = true;
232 break;
233 default:
234 break;
235 }
236 break;
237 default:
238 break;
239 }
Neale Rannsa0a908f2017-08-01 11:40:03 -0700240 fp->weight = api_rpath->rpath.frp_weight;
241 fp->preference = api_rpath->rpath.frp_preference;
Dave Barachb5e8a772016-12-06 12:04:42 -0500242 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
243 copy_fib_next_hop (api_rpath, fp);
244 fp++;
245 }
246
247 vl_msg_api_send_shmem (q, (u8 *) & mp);
248}
249
Neale Rannsa3af3372017-03-28 03:49:52 -0700250typedef struct vl_api_ip_fib_dump_walk_ctx_t_
251{
252 fib_node_index_t *feis;
253} vl_api_ip_fib_dump_walk_ctx_t;
254
255static int
256vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
257{
258 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
259
260 vec_add1 (ctx->feis, fei);
261
262 return (1);
263}
264
Dave Barachb5e8a772016-12-06 12:04:42 -0500265static void
266vl_api_ip_fib_dump_t_handler (vl_api_ip_fib_dump_t * mp)
267{
268 vpe_api_main_t *am = &vpe_api_main;
269 unix_shared_memory_queue_t *q;
270 ip4_main_t *im = &ip4_main;
271 fib_table_t *fib_table;
Neale Rannsa3af3372017-03-28 03:49:52 -0700272 fib_node_index_t *lfeip;
Dave Barachb5e8a772016-12-06 12:04:42 -0500273 fib_prefix_t pfx;
274 u32 fib_index;
275 fib_route_path_encode_t *api_rpaths;
Neale Rannsa3af3372017-03-28 03:49:52 -0700276 vl_api_ip_fib_dump_walk_ctx_t ctx = {
277 .feis = NULL,
278 };
Dave Barachb5e8a772016-12-06 12:04:42 -0500279
280 q = vl_api_client_index_to_input_queue (mp->client_index);
281 if (q == 0)
282 return;
283
284 /* *INDENT-OFF* */
285 pool_foreach (fib_table, im->fibs,
286 ({
Neale Rannsa3af3372017-03-28 03:49:52 -0700287 fib_table_walk(fib_table->ft_index,
288 FIB_PROTOCOL_IP4,
289 vl_api_ip_fib_dump_walk,
290 &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500291 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500292 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500293
Neale Rannsa3af3372017-03-28 03:49:52 -0700294 vec_sort_with_function (ctx.feis, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500295
Neale Rannsa3af3372017-03-28 03:49:52 -0700296 vec_foreach (lfeip, ctx.feis)
Dave Barachb5e8a772016-12-06 12:04:42 -0500297 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500298 fib_entry_get_prefix (*lfeip, &pfx);
299 fib_index = fib_entry_get_fib_index (*lfeip);
300 fib_table = fib_table_get (fib_index, pfx.fp_proto);
Dave Barachb5e8a772016-12-06 12:04:42 -0500301 api_rpaths = NULL;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500302 fib_entry_encode (*lfeip, &api_rpaths);
Neale Ranns2297af02017-09-12 09:45:04 -0700303 send_ip_fib_details (am, q, fib_table, &pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500304 vec_free (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500305 }
306
Neale Rannsa3af3372017-03-28 03:49:52 -0700307 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500308}
309
310static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500311send_ip6_fib_details (vpe_api_main_t * am,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500312 unix_shared_memory_queue_t * q,
313 u32 table_id, fib_prefix_t * pfx,
314 fib_route_path_encode_t * api_rpaths, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500315{
316 vl_api_ip6_fib_details_t *mp;
317 fib_route_path_encode_t *api_rpath;
318 vl_api_fib_path_t *fp;
319 int path_count;
320
Dave Barachd7cb1b52016-12-09 09:52:16 -0500321 path_count = vec_len (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500322 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
323 if (!mp)
324 return;
325 memset (mp, 0, sizeof (*mp));
326 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
327 mp->context = context;
328
329 mp->table_id = htonl (table_id);
330 mp->address_length = pfx->fp_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500331 memcpy (mp->address, &pfx->fp_addr.ip6, sizeof (pfx->fp_addr.ip6));
Dave Barachb5e8a772016-12-06 12:04:42 -0500332
333 mp->count = htonl (path_count);
334 fp = mp->path;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500335 vec_foreach (api_rpath, api_rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500336 {
337 memset (fp, 0, sizeof (*fp));
338 switch (api_rpath->dpo.dpoi_type)
339 {
340 case DPO_RECEIVE:
341 fp->is_local = true;
342 break;
343 case DPO_DROP:
344 fp->is_drop = true;
345 break;
346 case DPO_IP_NULL:
347 switch (api_rpath->dpo.dpoi_index)
348 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500349 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_NONE:
Dave Barachb5e8a772016-12-06 12:04:42 -0500350 fp->is_drop = true;
351 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500352 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_SEND_ICMP_UNREACH:
Dave Barachb5e8a772016-12-06 12:04:42 -0500353 fp->is_unreach = true;
354 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500355 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
Dave Barachb5e8a772016-12-06 12:04:42 -0500356 fp->is_prohibit = true;
357 break;
358 default:
359 break;
360 }
361 break;
362 default:
363 break;
364 }
Neale Rannsa0a908f2017-08-01 11:40:03 -0700365 fp->weight = api_rpath->rpath.frp_weight;
366 fp->preference = api_rpath->rpath.frp_preference;
367 fp->sw_if_index = api_rpath->rpath.frp_sw_if_index;
Dave Barachb5e8a772016-12-06 12:04:42 -0500368 copy_fib_next_hop (api_rpath, fp);
369 fp++;
370 }
371
372 vl_msg_api_send_shmem (q, (u8 *) & mp);
373}
374
Dave Barachd7cb1b52016-12-09 09:52:16 -0500375typedef struct apt_ip6_fib_show_ctx_t_
376{
377 u32 fib_index;
378 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500379} api_ip6_fib_show_ctx_t;
380
381static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500382api_ip6_fib_table_put_entries (clib_bihash_kv_24_8_t * kvp, void *arg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500383{
384 api_ip6_fib_show_ctx_t *ctx = arg;
385
386 if ((kvp->key[2] >> 32) == ctx->fib_index)
387 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500388 vec_add1 (ctx->entries, kvp->value);
Dave Barachb5e8a772016-12-06 12:04:42 -0500389 }
390}
391
392static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500393api_ip6_fib_table_get_all (unix_shared_memory_queue_t * q,
394 vl_api_ip6_fib_dump_t * mp,
395 fib_table_t * fib_table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500396{
397 vpe_api_main_t *am = &vpe_api_main;
398 ip6_main_t *im6 = &ip6_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500399 fib_node_index_t *fib_entry_index;
400 api_ip6_fib_show_ctx_t ctx = {
Neale Rannsa3af3372017-03-28 03:49:52 -0700401 .fib_index = fib_table->ft_index,
402 .entries = NULL,
Dave Barachb5e8a772016-12-06 12:04:42 -0500403 };
404 fib_route_path_encode_t *api_rpaths;
405 fib_prefix_t pfx;
406
Dave Barachd7cb1b52016-12-09 09:52:16 -0500407 BV (clib_bihash_foreach_key_value_pair)
408 ((BVT (clib_bihash) *) & im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].
409 ip6_hash, api_ip6_fib_table_put_entries, &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500410
Dave Barachd7cb1b52016-12-09 09:52:16 -0500411 vec_sort_with_function (ctx.entries, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500412
Dave Barachd7cb1b52016-12-09 09:52:16 -0500413 vec_foreach (fib_entry_index, ctx.entries)
414 {
415 fib_entry_get_prefix (*fib_entry_index, &pfx);
416 api_rpaths = NULL;
417 fib_entry_encode (*fib_entry_index, &api_rpaths);
418 send_ip6_fib_details (am, q,
419 fib_table->ft_table_id,
420 &pfx, api_rpaths, mp->context);
421 vec_free (api_rpaths);
422 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500423
Dave Barachd7cb1b52016-12-09 09:52:16 -0500424 vec_free (ctx.entries);
Dave Barachb5e8a772016-12-06 12:04:42 -0500425}
426
427static void
428vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
429{
430 unix_shared_memory_queue_t *q;
431 ip6_main_t *im6 = &ip6_main;
432 fib_table_t *fib_table;
433
434 q = vl_api_client_index_to_input_queue (mp->client_index);
435 if (q == 0)
436 return;
437
438 /* *INDENT-OFF* */
439 pool_foreach (fib_table, im6->fibs,
440 ({
441 api_ip6_fib_table_get_all(q, mp, fib_table);
442 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500443 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500444}
445
446static void
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800447send_ip_mfib_details (unix_shared_memory_queue_t * q,
448 u32 context, u32 table_id, fib_node_index_t mfei)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800449{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800450 fib_route_path_encode_t *api_rpath, *api_rpaths = NULL;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800451 vl_api_ip_mfib_details_t *mp;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800452 mfib_entry_t *mfib_entry;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800453 vl_api_fib_path_t *fp;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800454 mfib_prefix_t pfx;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800455 int path_count;
456
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800457 mfib_entry = mfib_entry_get (mfei);
458 mfib_entry_get_prefix (mfei, &pfx);
459 mfib_entry_encode (mfei, &api_rpaths);
460
Neale Ranns5a8123b2017-01-26 01:18:23 -0800461 path_count = vec_len (api_rpaths);
462 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
463 if (!mp)
464 return;
465 memset (mp, 0, sizeof (*mp));
466 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
467 mp->context = context;
468
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800469 mp->rpf_id = mfib_entry->mfe_rpf_id;
470 mp->entry_flags = mfib_entry->mfe_flags;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800471 mp->table_id = htonl (table_id);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800472 mp->address_length = pfx.fp_len;
473 memcpy (mp->grp_address, &pfx.fp_grp_addr.ip4,
474 sizeof (pfx.fp_grp_addr.ip4));
475 memcpy (mp->src_address, &pfx.fp_src_addr.ip4,
476 sizeof (pfx.fp_src_addr.ip4));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800477
478 mp->count = htonl (path_count);
479 fp = mp->path;
480 vec_foreach (api_rpath, api_rpaths)
481 {
482 memset (fp, 0, sizeof (*fp));
483
484 fp->weight = 0;
485 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
486 copy_fib_next_hop (api_rpath, fp);
487 fp++;
488 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800489 vec_free (api_rpaths);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800490
491 vl_msg_api_send_shmem (q, (u8 *) & mp);
492}
493
494typedef struct vl_api_ip_mfib_dump_ctc_t_
495{
496 fib_node_index_t *entries;
497} vl_api_ip_mfib_dump_ctc_t;
498
499static int
500vl_api_ip_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
501{
502 vl_api_ip_mfib_dump_ctc_t *ctx = arg;
503
504 vec_add1 (ctx->entries, fei);
505
506 return (0);
507}
508
509static void
510vl_api_ip_mfib_dump_t_handler (vl_api_ip_mfib_dump_t * mp)
511{
Neale Ranns5a8123b2017-01-26 01:18:23 -0800512 unix_shared_memory_queue_t *q;
513 ip4_main_t *im = &ip4_main;
514 mfib_table_t *mfib_table;
515 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800516 vl_api_ip_mfib_dump_ctc_t ctx = {
517 .entries = NULL,
518 };
519
520 q = vl_api_client_index_to_input_queue (mp->client_index);
521 if (q == 0)
522 return;
523
524
525 /* *INDENT-OFF* */
526 pool_foreach (mfib_table, im->mfibs,
527 ({
528 ip4_mfib_table_walk(&mfib_table->v4,
529 vl_api_ip_mfib_table_dump_walk,
530 &ctx);
531
532 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
533
534 vec_foreach (mfeip, ctx.entries)
535 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800536 send_ip_mfib_details (q, mp->context,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800537 mfib_table->mft_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800538 *mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800539 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800540 vec_reset_length (ctx.entries);
541
542 }));
543 /* *INDENT-ON* */
544
545 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800546}
547
548static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800549send_ip6_mfib_details (vpe_api_main_t * am,
550 unix_shared_memory_queue_t * q,
551 u32 table_id,
552 mfib_prefix_t * pfx,
553 fib_route_path_encode_t * api_rpaths, u32 context)
554{
555 vl_api_ip6_mfib_details_t *mp;
556 fib_route_path_encode_t *api_rpath;
557 vl_api_fib_path_t *fp;
558 int path_count;
559
560 path_count = vec_len (api_rpaths);
561 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
562 if (!mp)
563 return;
564 memset (mp, 0, sizeof (*mp));
565 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
566 mp->context = context;
567
568 mp->table_id = htonl (table_id);
569 mp->address_length = pfx->fp_len;
570 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip6,
571 sizeof (pfx->fp_grp_addr.ip6));
572 memcpy (mp->src_address, &pfx->fp_src_addr.ip6,
573 sizeof (pfx->fp_src_addr.ip6));
574
575 mp->count = htonl (path_count);
576 fp = mp->path;
577 vec_foreach (api_rpath, api_rpaths)
578 {
579 memset (fp, 0, sizeof (*fp));
580
581 fp->weight = 0;
582 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
583 copy_fib_next_hop (api_rpath, fp);
584 fp++;
585 }
586
587 vl_msg_api_send_shmem (q, (u8 *) & mp);
588}
589
590typedef struct vl_api_ip6_mfib_dump_ctc_t_
591{
592 fib_node_index_t *entries;
593} vl_api_ip6_mfib_dump_ctc_t;
594
595static int
596vl_api_ip6_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
597{
598 vl_api_ip6_mfib_dump_ctc_t *ctx = arg;
599
600 vec_add1 (ctx->entries, fei);
601
602 return (0);
603}
604
605static void
606vl_api_ip6_mfib_dump_t_handler (vl_api_ip6_mfib_dump_t * mp)
607{
608 vpe_api_main_t *am = &vpe_api_main;
609 unix_shared_memory_queue_t *q;
610 ip6_main_t *im = &ip6_main;
611 mfib_table_t *mfib_table;
612 fib_node_index_t *mfeip;
613 mfib_prefix_t pfx;
614 fib_route_path_encode_t *api_rpaths = NULL;
615 vl_api_ip6_mfib_dump_ctc_t ctx = {
616 .entries = NULL,
617 };
618
619 q = vl_api_client_index_to_input_queue (mp->client_index);
620 if (q == 0)
621 return;
622
623
624 /* *INDENT-OFF* */
625 pool_foreach (mfib_table, im->mfibs,
626 ({
627 ip6_mfib_table_walk(&mfib_table->v6,
628 vl_api_ip6_mfib_table_dump_walk,
629 &ctx);
630
631 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
632
633 vec_foreach(mfeip, ctx.entries)
634 {
635 mfib_entry_get_prefix (*mfeip, &pfx);
636 mfib_entry_encode (*mfeip, &api_rpaths);
637 send_ip6_mfib_details (am, q,
638 mfib_table->mft_table_id,
639 &pfx, api_rpaths,
640 mp->context);
641 }
642 vec_reset_length (api_rpaths);
643 vec_reset_length (ctx.entries);
644
645 }));
646 /* *INDENT-ON* */
647
648 vec_free (ctx.entries);
649 vec_free (api_rpaths);
650}
651
652static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700653vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
654 vlib_main_t * vm)
655{
656 vl_api_ip_punt_police_reply_t *rmp;
657 int rv = 0;
658
659 if (mp->is_ip6)
660 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
661 else
662 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
663
664 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
665}
666
667static void
668vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
669 vlib_main_t * vm)
670{
671 vl_api_ip_punt_redirect_reply_t *rmp;
672 int rv = 0;
673
674 if (mp->is_add)
675 {
676 ip46_address_t nh;
677
678 memset (&nh, 0, sizeof (nh));
679
680 if (mp->is_ip6)
681 {
682 memcpy (&nh.ip6, mp->nh, sizeof (nh.ip6));
683
684 ip6_punt_redirect_add (ntohl (mp->rx_sw_if_index),
685 ntohl (mp->tx_sw_if_index), &nh);
686 }
687 else
688 {
689 memcpy (&nh.ip4, mp->nh, sizeof (nh.ip4));
690
691 ip4_punt_redirect_add (ntohl (mp->rx_sw_if_index),
692 ntohl (mp->tx_sw_if_index), &nh);
693 }
694 }
695 else
696 {
697 if (mp->is_ip6)
698 {
699 ip6_punt_redirect_del (ntohl (mp->rx_sw_if_index));
700 }
701 else
702 {
703 ip4_punt_redirect_del (ntohl (mp->rx_sw_if_index));
704 }
705 }
706
707 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
708}
709
710static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500711vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
712 vlib_main_t * vm)
713{
714 vl_api_ip_neighbor_add_del_reply_t *rmp;
715 vnet_main_t *vnm = vnet_get_main ();
716 int rv = 0;
717
718 VALIDATE_SW_IF_INDEX (mp);
719
720 stats_dslock_with_hint (1 /* release hint */ , 7 /* tag */ );
721
722 /*
723 * there's no validation here of the ND/ARP entry being added.
724 * The expectation is that the FIB will ensure that nothing bad
725 * will come of adding bogus entries.
726 */
727 if (mp->is_ipv6)
728 {
729 if (mp->is_add)
730 rv = vnet_set_ip6_ethernet_neighbor
731 (vm, ntohl (mp->sw_if_index),
732 (ip6_address_t *) (mp->dst_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800733 mp->mac_address, sizeof (mp->mac_address), mp->is_static,
734 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500735 else
736 rv = vnet_unset_ip6_ethernet_neighbor
737 (vm, ntohl (mp->sw_if_index),
738 (ip6_address_t *) (mp->dst_address),
739 mp->mac_address, sizeof (mp->mac_address));
740 }
741 else
742 {
743 ethernet_arp_ip4_over_ethernet_address_t a;
744
745 clib_memcpy (&a.ethernet, mp->mac_address, 6);
746 clib_memcpy (&a.ip4, mp->dst_address, 4);
747
748 if (mp->is_add)
749 rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800750 &a, mp->is_static,
751 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500752 else
753 rv =
754 vnet_arp_unset_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index), &a);
755 }
756
Dave Barachb5e8a772016-12-06 12:04:42 -0500757 stats_dsunlock ();
Neale Ranns8edad032017-10-09 05:26:13 -0700758
759 BAD_SW_IF_INDEX_LABEL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500760 REPLY_MACRO (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
761}
762
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700763void
Neale Ranns15002542017-09-10 04:39:11 -0700764ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
765{
766 u32 fib_index, mfib_index;
767
768 /*
769 * ignore action on the default table - this is always present
770 * and cannot be added nor deleted from the API
771 */
772 if (0 != table_id)
773 {
774 /*
775 * The API holds only one lock on the table.
776 * i.e. it can be added many times via the API but needs to be
777 * deleted only once.
778 * The FIB index for unicast and multicast is not necessarily the
779 * same, since internal VPP systesm (like LISP and SR) create
780 * their own unicast tables.
781 */
782 fib_index = fib_table_find (fproto, table_id);
783 mfib_index = mfib_table_find (fproto, table_id);
784
785 if (~0 != fib_index)
786 {
787 fib_table_unlock (fib_index, fproto,
788 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
789 }
790 if (~0 != mfib_index)
791 {
792 mfib_table_unlock (mfib_index, fproto,
793 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
794 }
795 }
796}
797
798void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700799vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
800{
801 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700802 fib_protocol_t fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
803 u32 table_id = ntohl (mp->table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700804 int rv = 0;
805
Neale Ranns15002542017-09-10 04:39:11 -0700806 if (mp->is_add)
807 {
Neale Ranns2297af02017-09-12 09:45:04 -0700808 ip_table_create (fproto, table_id, 1, mp->name);
Neale Ranns15002542017-09-10 04:39:11 -0700809 }
810 else
811 {
812 ip_table_delete (fproto, table_id, 1);
813 }
814
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700815 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
816}
817
Dave Barachb5e8a772016-12-06 12:04:42 -0500818int
819add_del_route_t_handler (u8 is_multipath,
820 u8 is_add,
821 u8 is_drop,
822 u8 is_unreach,
823 u8 is_prohibit,
824 u8 is_local,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800825 u8 is_multicast,
Dave Barachb5e8a772016-12-06 12:04:42 -0500826 u8 is_classify,
827 u32 classify_table_index,
828 u8 is_resolve_host,
829 u8 is_resolve_attached,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800830 u8 is_interface_rx,
831 u8 is_rpf_id,
Neale Ranns6f631152017-10-03 08:20:21 -0700832 u8 is_l2_bridged,
Dave Barachb5e8a772016-12-06 12:04:42 -0500833 u32 fib_index,
834 const fib_prefix_t * prefix,
Neale Rannsda78f952017-05-24 09:15:43 -0700835 dpo_proto_t next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500836 const ip46_address_t * next_hop,
837 u32 next_hop_sw_if_index,
838 u8 next_hop_fib_index,
Neale Ranns57b58602017-07-15 07:37:25 -0700839 u16 next_hop_weight,
840 u16 next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500841 mpls_label_t next_hop_via_label,
842 mpls_label_t * next_hop_out_label_stack)
843{
844 vnet_classify_main_t *cm = &vnet_classify_main;
845 fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
846 fib_route_path_t path = {
Neale Rannsda78f952017-05-24 09:15:43 -0700847 .frp_proto = next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500848 .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
849 .frp_sw_if_index = next_hop_sw_if_index,
850 .frp_fib_index = next_hop_fib_index,
851 .frp_weight = next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -0700852 .frp_preference = next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500853 .frp_label_stack = next_hop_out_label_stack,
854 };
855 fib_route_path_t *paths = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800856 fib_entry_flag_t entry_flags = FIB_ENTRY_FLAG_NONE;
Dave Barachb5e8a772016-12-06 12:04:42 -0500857
Neale Rannscaac3502017-09-18 18:04:10 -0700858 /*
859 * the special INVALID label meams we are not recursing via a
860 * label. Exp-null value is never a valid via-label so that
861 * also means it's not a via-label and means clients that set
862 * it to 0 by default get the expected behaviour
863 */
864 if ((MPLS_LABEL_INVALID != next_hop_via_label) && (0 != next_hop_via_label))
Dave Barachb5e8a772016-12-06 12:04:42 -0500865 {
Neale Rannsda78f952017-05-24 09:15:43 -0700866 path.frp_proto = DPO_PROTO_MPLS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500867 path.frp_local_label = next_hop_via_label;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800868 path.frp_eos = MPLS_NON_EOS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500869 }
Neale Ranns6f631152017-10-03 08:20:21 -0700870 if (is_l2_bridged)
871 path.frp_proto = DPO_PROTO_ETHERNET;
Dave Barachb5e8a772016-12-06 12:04:42 -0500872 if (is_resolve_host)
873 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
874 if (is_resolve_attached)
875 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800876 if (is_interface_rx)
877 path_flags |= FIB_ROUTE_PATH_INTF_RX;
878 if (is_rpf_id)
879 path_flags |= FIB_ROUTE_PATH_RPF_ID;
880 if (is_multicast)
881 entry_flags |= FIB_ENTRY_FLAG_MULTICAST;
Dave Barachb5e8a772016-12-06 12:04:42 -0500882
883 path.frp_flags = path_flags;
884
885 if (is_multipath)
886 {
887 stats_dslock_with_hint (1 /* release hint */ , 10 /* tag */ );
888
889
890 vec_add1 (paths, path);
891
892 if (is_add)
893 fib_table_entry_path_add2 (fib_index,
894 prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800895 FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500896 else
897 fib_table_entry_path_remove2 (fib_index,
898 prefix, FIB_SOURCE_API, paths);
899
900 vec_free (paths);
901 stats_dsunlock ();
902 return 0;
903 }
904
905 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
906
907 if (is_drop || is_local || is_classify || is_unreach || is_prohibit)
908 {
909 /*
910 * special route types that link directly to the adj
911 */
912 if (is_add)
913 {
914 dpo_id_t dpo = DPO_INVALID;
915 dpo_proto_t dproto;
916
917 dproto = fib_proto_to_dpo (prefix->fp_proto);
918
919 if (is_drop)
920 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
921 else if (is_local)
922 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
923 else if (is_unreach)
924 ip_null_dpo_add_and_lock (dproto,
925 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
926 else if (is_prohibit)
927 ip_null_dpo_add_and_lock (dproto,
928 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
929 &dpo);
930 else if (is_classify)
931 {
932 if (pool_is_free_index (cm->tables,
933 ntohl (classify_table_index)))
934 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500935 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500936 return VNET_API_ERROR_NO_SUCH_TABLE;
937 }
938
939 dpo_set (&dpo, DPO_CLASSIFY, dproto,
940 classify_dpo_create (dproto,
941 ntohl (classify_table_index)));
942 }
943 else
944 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500945 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500946 return VNET_API_ERROR_NO_SUCH_TABLE;
947 }
948
949 fib_table_entry_special_dpo_update (fib_index,
950 prefix,
951 FIB_SOURCE_API,
952 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
953 dpo_reset (&dpo);
954 }
955 else
956 {
957 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
958 }
959 }
960 else
961 {
962 if (is_add)
963 {
964 vec_add1 (paths, path);
965 fib_table_entry_update (fib_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800966 prefix, FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500967 vec_free (paths);
968 }
969 else
970 {
971 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
972 }
973 }
974
Dave Barachd7cb1b52016-12-09 09:52:16 -0500975 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500976 return (0);
977}
978
979int
980add_del_route_check (fib_protocol_t table_proto,
981 u32 table_id,
982 u32 next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700983 dpo_proto_t next_hop_table_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500984 u32 next_hop_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800985 u8 is_rpf_id, u32 * fib_index, u32 * next_hop_fib_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500986{
987 vnet_main_t *vnm = vnet_get_main ();
988
Neale Ranns15002542017-09-10 04:39:11 -0700989 /* Temporaray whilst I do the CSIT dance */
990 u8 create_missing_tables = 1;
991
Dave Barachb5e8a772016-12-06 12:04:42 -0500992 *fib_index = fib_table_find (table_proto, ntohl (table_id));
993 if (~0 == *fib_index)
994 {
995 if (create_missing_tables)
996 {
997 *fib_index = fib_table_find_or_create_and_lock (table_proto,
Neale Ranns15002542017-09-10 04:39:11 -0700998 ntohl (table_id),
999 FIB_SOURCE_API);
Dave Barachb5e8a772016-12-06 12:04:42 -05001000 }
1001 else
1002 {
1003 /* No such VRF, and we weren't asked to create one */
1004 return VNET_API_ERROR_NO_SUCH_FIB;
1005 }
1006 }
1007
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001008 if (!is_rpf_id && ~0 != ntohl (next_hop_sw_if_index))
Dave Barachb5e8a772016-12-06 12:04:42 -05001009 {
1010 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1011 ntohl (next_hop_sw_if_index)))
1012 {
1013 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1014 }
1015 }
1016 else
1017 {
Neale Rannsda78f952017-05-24 09:15:43 -07001018 fib_protocol_t fib_nh_proto;
1019
1020 if (next_hop_table_proto > DPO_PROTO_MPLS)
1021 return (0);
1022
1023 fib_nh_proto = dpo_proto_to_fib (next_hop_table_proto);
1024
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001025 if (is_rpf_id)
Neale Rannsda78f952017-05-24 09:15:43 -07001026 *next_hop_fib_index = mfib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001027 ntohl (next_hop_table_id));
1028 else
Neale Rannsda78f952017-05-24 09:15:43 -07001029 *next_hop_fib_index = fib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001030 ntohl (next_hop_table_id));
Dave Barachb5e8a772016-12-06 12:04:42 -05001031
1032 if (~0 == *next_hop_fib_index)
1033 {
1034 if (create_missing_tables)
1035 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001036 if (is_rpf_id)
1037 *next_hop_fib_index =
Neale Rannsda78f952017-05-24 09:15:43 -07001038 mfib_table_find_or_create_and_lock (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001039 ntohl
Neale Ranns15002542017-09-10 04:39:11 -07001040 (next_hop_table_id),
1041 MFIB_SOURCE_API);
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001042 else
1043 *next_hop_fib_index =
Neale Rannsda78f952017-05-24 09:15:43 -07001044 fib_table_find_or_create_and_lock (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001045 ntohl
Neale Ranns15002542017-09-10 04:39:11 -07001046 (next_hop_table_id),
1047 FIB_SOURCE_API);
Dave Barachb5e8a772016-12-06 12:04:42 -05001048 }
1049 else
1050 {
1051 /* No such VRF, and we weren't asked to create one */
1052 return VNET_API_ERROR_NO_SUCH_FIB;
1053 }
1054 }
1055 }
1056
1057 return (0);
1058}
1059
1060static int
1061ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1062{
1063 u32 fib_index, next_hop_fib_index;
1064 mpls_label_t *label_stack = NULL;
1065 int rv, ii, n_labels;;
1066
1067 rv = add_del_route_check (FIB_PROTOCOL_IP4,
1068 mp->table_id,
1069 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001070 DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -05001071 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001072 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001073
1074 if (0 != rv)
1075 return (rv);
1076
1077 fib_prefix_t pfx = {
1078 .fp_len = mp->dst_address_length,
1079 .fp_proto = FIB_PROTOCOL_IP4,
1080 };
1081 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
1082
1083 ip46_address_t nh;
1084 memset (&nh, 0, sizeof (nh));
1085 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
1086
1087 n_labels = mp->next_hop_n_out_labels;
1088 if (n_labels == 0)
1089 ;
1090 else if (1 == n_labels)
1091 vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
1092 else
1093 {
1094 vec_validate (label_stack, n_labels - 1);
1095 for (ii = 0; ii < n_labels; ii++)
1096 label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
1097 }
1098
1099 return (add_del_route_t_handler (mp->is_multipath,
1100 mp->is_add,
1101 mp->is_drop,
1102 mp->is_unreach,
1103 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001104 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001105 mp->is_classify,
1106 mp->classify_table_index,
1107 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001108 mp->is_resolve_attached, 0, 0,
Neale Ranns6f631152017-10-03 08:20:21 -07001109 mp->is_l2_bridged,
Neale Rannsda78f952017-05-24 09:15:43 -07001110 fib_index, &pfx, DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -05001111 &nh,
1112 ntohl (mp->next_hop_sw_if_index),
1113 next_hop_fib_index,
1114 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001115 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001116 ntohl (mp->next_hop_via_label),
1117 label_stack));
1118}
1119
1120static int
1121ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1122{
1123 u32 fib_index, next_hop_fib_index;
1124 mpls_label_t *label_stack = NULL;
1125 int rv, ii, n_labels;;
1126
1127 rv = add_del_route_check (FIB_PROTOCOL_IP6,
1128 mp->table_id,
1129 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001130 DPO_PROTO_IP6,
Dave Barachb5e8a772016-12-06 12:04:42 -05001131 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001132 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001133
1134 if (0 != rv)
1135 return (rv);
1136
1137 fib_prefix_t pfx = {
1138 .fp_len = mp->dst_address_length,
1139 .fp_proto = FIB_PROTOCOL_IP6,
1140 };
1141 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1142
1143 ip46_address_t nh;
1144 memset (&nh, 0, sizeof (nh));
1145 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1146
1147 n_labels = mp->next_hop_n_out_labels;
1148 if (n_labels == 0)
1149 ;
1150 else if (1 == n_labels)
1151 vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
1152 else
1153 {
1154 vec_validate (label_stack, n_labels - 1);
1155 for (ii = 0; ii < n_labels; ii++)
1156 label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
1157 }
1158
1159 return (add_del_route_t_handler (mp->is_multipath,
1160 mp->is_add,
1161 mp->is_drop,
1162 mp->is_unreach,
1163 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001164 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001165 mp->is_classify,
1166 mp->classify_table_index,
1167 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001168 mp->is_resolve_attached, 0, 0,
Neale Ranns6f631152017-10-03 08:20:21 -07001169 mp->is_l2_bridged,
Neale Rannsda78f952017-05-24 09:15:43 -07001170 fib_index, &pfx, DPO_PROTO_IP6,
Dave Barachb5e8a772016-12-06 12:04:42 -05001171 &nh, ntohl (mp->next_hop_sw_if_index),
1172 next_hop_fib_index,
1173 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001174 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001175 ntohl (mp->next_hop_via_label),
1176 label_stack));
1177}
1178
1179void
1180vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1181{
1182 vl_api_ip_add_del_route_reply_t *rmp;
1183 int rv;
1184 vnet_main_t *vnm = vnet_get_main ();
1185
1186 vnm->api_errno = 0;
1187
1188 if (mp->is_ipv6)
1189 rv = ip6_add_del_route_t_handler (mp);
1190 else
1191 rv = ip4_add_del_route_t_handler (mp);
1192
1193 rv = (rv == 0) ? vnm->api_errno : rv;
1194
1195 REPLY_MACRO (VL_API_IP_ADD_DEL_ROUTE_REPLY);
1196}
1197
Neale Ranns15002542017-09-10 04:39:11 -07001198void
Neale Ranns2297af02017-09-12 09:45:04 -07001199ip_table_create (fib_protocol_t fproto,
1200 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -07001201{
1202 u32 fib_index, mfib_index;
1203
1204 /*
1205 * ignore action on the default table - this is always present
1206 * and cannot be added nor deleted from the API
1207 */
1208 if (0 != table_id)
1209 {
1210 /*
1211 * The API holds only one lock on the table.
1212 * i.e. it can be added many times via the API but needs to be
1213 * deleted only once.
1214 * The FIB index for unicast and multicast is not necessarily the
1215 * same, since internal VPP systesm (like LISP and SR) create
1216 * their own unicast tables.
1217 */
1218 fib_index = fib_table_find (fproto, table_id);
1219 mfib_index = mfib_table_find (fproto, table_id);
1220
1221 if (~0 == fib_index)
1222 {
Neale Ranns2297af02017-09-12 09:45:04 -07001223 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
1224 (is_api ?
1225 FIB_SOURCE_API :
1226 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001227 }
1228 if (~0 == mfib_index)
1229 {
Neale Ranns2297af02017-09-12 09:45:04 -07001230 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
1231 (is_api ?
1232 MFIB_SOURCE_API :
1233 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001234 }
1235 }
1236}
1237
Neale Ranns32e1c012016-11-22 17:07:28 +00001238static int
1239add_del_mroute_check (fib_protocol_t table_proto,
1240 u32 table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001241 u32 next_hop_sw_if_index, u8 is_local, u32 * fib_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001242{
1243 vnet_main_t *vnm = vnet_get_main ();
1244
1245 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1246 if (~0 == *fib_index)
1247 {
Neale Ranns15002542017-09-10 04:39:11 -07001248 /* No such table */
1249 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Ranns32e1c012016-11-22 17:07:28 +00001250 }
1251
1252 if (~0 != ntohl (next_hop_sw_if_index))
1253 {
1254 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1255 ntohl (next_hop_sw_if_index)))
1256 {
1257 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1258 }
1259 }
1260
1261 return (0);
1262}
1263
1264static int
1265mroute_add_del_handler (u8 is_add,
1266 u8 is_local,
1267 u32 fib_index,
1268 const mfib_prefix_t * prefix,
1269 u32 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001270 fib_rpf_id_t rpf_id,
Neale Ranns32e1c012016-11-22 17:07:28 +00001271 u32 next_hop_sw_if_index, u32 itf_flags)
1272{
1273 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1274
1275 fib_route_path_t path = {
1276 .frp_sw_if_index = next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001277 .frp_proto = fib_proto_to_dpo (prefix->fp_proto),
Neale Ranns32e1c012016-11-22 17:07:28 +00001278 };
1279
1280 if (is_local)
1281 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1282
1283
1284 if (!is_local && ~0 == next_hop_sw_if_index)
1285 {
1286 mfib_table_entry_update (fib_index, prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001287 MFIB_SOURCE_API, rpf_id, entry_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001288 }
1289 else
1290 {
1291 if (is_add)
1292 {
1293 mfib_table_entry_path_update (fib_index, prefix,
1294 MFIB_SOURCE_API, &path, itf_flags);
1295 }
1296 else
1297 {
1298 mfib_table_entry_path_remove (fib_index, prefix,
1299 MFIB_SOURCE_API, &path);
1300 }
1301 }
1302
1303 stats_dsunlock ();
1304 return (0);
1305}
1306
1307static int
1308api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1309{
1310 fib_protocol_t fproto;
1311 u32 fib_index;
1312 int rv;
1313
1314 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1315 rv = add_del_mroute_check (fproto,
1316 mp->table_id,
1317 mp->next_hop_sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -07001318 mp->is_local, &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001319
1320 if (0 != rv)
1321 return (rv);
1322
1323 mfib_prefix_t pfx = {
1324 .fp_len = ntohs (mp->grp_address_length),
1325 .fp_proto = fproto,
1326 };
1327
1328 if (FIB_PROTOCOL_IP4 == fproto)
1329 {
1330 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1331 sizeof (pfx.fp_grp_addr.ip4));
1332 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1333 sizeof (pfx.fp_src_addr.ip4));
1334 }
1335 else
1336 {
1337 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1338 sizeof (pfx.fp_grp_addr.ip6));
1339 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1340 sizeof (pfx.fp_src_addr.ip6));
1341 }
1342
1343 return (mroute_add_del_handler (mp->is_add,
1344 mp->is_local,
1345 fib_index, &pfx,
1346 ntohl (mp->entry_flags),
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001347 ntohl (mp->rpf_id),
Neale Ranns32e1c012016-11-22 17:07:28 +00001348 ntohl (mp->next_hop_sw_if_index),
1349 ntohl (mp->itf_flags)));
1350}
1351
1352void
1353vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1354{
1355 vl_api_ip_mroute_add_del_reply_t *rmp;
1356 int rv;
1357 vnet_main_t *vnm = vnet_get_main ();
1358
1359 vnm->api_errno = 0;
1360
1361 rv = api_mroute_add_del_t_handler (mp);
1362
1363 rv = (rv == 0) ? vnm->api_errno : rv;
1364
1365 REPLY_MACRO (VL_API_IP_MROUTE_ADD_DEL_REPLY);
1366}
1367
Dave Barachb5e8a772016-12-06 12:04:42 -05001368static void
1369send_ip_details (vpe_api_main_t * am,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001370 unix_shared_memory_queue_t * q, u32 sw_if_index,
1371 u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001372{
1373 vl_api_ip_details_t *mp;
1374
1375 mp = vl_msg_api_alloc (sizeof (*mp));
1376 memset (mp, 0, sizeof (*mp));
1377 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1378
1379 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001380 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001381 mp->context = context;
1382
1383 vl_msg_api_send_shmem (q, (u8 *) & mp);
1384}
1385
1386static void
1387send_ip_address_details (vpe_api_main_t * am,
1388 unix_shared_memory_queue_t * q,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001389 u8 * ip, u16 prefix_length,
1390 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001391{
1392 vl_api_ip_address_details_t *mp;
1393
1394 mp = vl_msg_api_alloc (sizeof (*mp));
1395 memset (mp, 0, sizeof (*mp));
1396 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1397
1398 if (is_ipv6)
1399 {
1400 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1401 }
1402 else
1403 {
1404 u32 *tp = (u32 *) mp->ip;
1405 *tp = *(u32 *) ip;
1406 }
1407 mp->prefix_length = prefix_length;
1408 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001409 mp->sw_if_index = htonl (sw_if_index);
1410 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001411
1412 vl_msg_api_send_shmem (q, (u8 *) & mp);
1413}
1414
1415static void
1416vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1417{
1418 vpe_api_main_t *am = &vpe_api_main;
1419 unix_shared_memory_queue_t *q;
1420 ip6_address_t *r6;
1421 ip4_address_t *r4;
1422 ip6_main_t *im6 = &ip6_main;
1423 ip4_main_t *im4 = &ip4_main;
1424 ip_lookup_main_t *lm6 = &im6->lookup_main;
1425 ip_lookup_main_t *lm4 = &im4->lookup_main;
1426 ip_interface_address_t *ia = 0;
1427 u32 sw_if_index = ~0;
1428 int rv __attribute__ ((unused)) = 0;
1429
1430 VALIDATE_SW_IF_INDEX (mp);
1431
1432 sw_if_index = ntohl (mp->sw_if_index);
1433
1434 q = vl_api_client_index_to_input_queue (mp->client_index);
1435 if (q == 0)
1436 return;
1437
Dave Barachb5e8a772016-12-06 12:04:42 -05001438 if (mp->is_ipv6)
1439 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001440 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001441 foreach_ip_interface_address (lm6, ia, sw_if_index,
1442 1 /* honor unnumbered */,
1443 ({
1444 r6 = ip_interface_address_get_address (lm6, ia);
1445 u16 prefix_length = ia->address_length;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001446 send_ip_address_details(am, q, (u8*)r6, prefix_length,
1447 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001448 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001449 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001450 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001451 else
1452 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001453 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001454 foreach_ip_interface_address (lm4, ia, sw_if_index,
1455 1 /* honor unnumbered */,
1456 ({
1457 r4 = ip_interface_address_get_address (lm4, ia);
1458 u16 prefix_length = ia->address_length;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001459 send_ip_address_details(am, q, (u8*)r4, prefix_length,
1460 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001461 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001462 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001463 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001464 BAD_SW_IF_INDEX_LABEL;
1465}
1466
1467static void
1468vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1469{
1470 vpe_api_main_t *am = &vpe_api_main;
1471 vnet_main_t *vnm = vnet_get_main ();
1472 vlib_main_t *vm = vlib_get_main ();
1473 vnet_interface_main_t *im = &vnm->interface_main;
1474 unix_shared_memory_queue_t *q;
1475 vnet_sw_interface_t *si, *sorted_sis;
1476 u32 sw_if_index = ~0;
1477
1478 q = vl_api_client_index_to_input_queue (mp->client_index);
1479 if (q == 0)
1480 {
1481 return;
1482 }
1483
1484 /* Gather interfaces. */
1485 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1486 _vec_len (sorted_sis) = 0;
1487 /* *INDENT-OFF* */
1488 pool_foreach (si, im->sw_interfaces,
1489 ({
1490 vec_add1 (sorted_sis, si[0]);
1491 }));
1492 /* *INDENT-ON* */
1493
1494 vec_foreach (si, sorted_sis)
1495 {
1496 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1497 {
1498 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1499 {
1500 continue;
1501 }
1502 sw_if_index = si->sw_if_index;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001503 send_ip_details (am, q, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001504 }
1505 }
1506}
1507
1508static void
1509set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1510{
1511 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001512 int rv;
1513 u32 table_id;
1514 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001515
Neale Ranns227038a2017-04-21 01:07:59 -07001516 table_id = ntohl (mp->vrf_id);
1517
1518#define _(a,b) if (mp->a) flow_hash_config |= b;
1519 foreach_flow_hash_bit;
1520#undef _
1521
1522 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001523
1524 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1525}
1526
1527static void
1528set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1529{
1530 vl_api_set_ip_flow_hash_reply_t *rmp;
1531 int rv;
1532 u32 table_id;
1533 flow_hash_config_t flow_hash_config = 0;
1534
1535 table_id = ntohl (mp->vrf_id);
1536
1537#define _(a,b) if (mp->a) flow_hash_config |= b;
1538 foreach_flow_hash_bit;
1539#undef _
1540
1541 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1542
1543 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1544}
1545
1546
1547static void
1548vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1549{
1550 if (mp->is_ipv6 == 0)
1551 set_ip4_flow_hash (mp);
1552 else
1553 set_ip6_flow_hash (mp);
1554}
1555
1556static void
1557 vl_api_sw_interface_ip6nd_ra_config_t_handler
1558 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1559{
1560 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1561 vlib_main_t *vm = vlib_get_main ();
1562 int rv = 0;
1563 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1564 default_router;
1565
1566 is_no = mp->is_no == 1;
1567 suppress = mp->suppress == 1;
1568 managed = mp->managed == 1;
1569 other = mp->other == 1;
1570 ll_option = mp->ll_option == 1;
1571 send_unicast = mp->send_unicast == 1;
1572 cease = mp->cease == 1;
1573 default_router = mp->default_router == 1;
1574
1575 VALIDATE_SW_IF_INDEX (mp);
1576
1577 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1578 suppress, managed, other,
1579 ll_option, send_unicast, cease,
1580 default_router, ntohl (mp->lifetime),
1581 ntohl (mp->initial_count),
1582 ntohl (mp->initial_interval),
1583 ntohl (mp->max_interval),
1584 ntohl (mp->min_interval), is_no);
1585
1586 BAD_SW_IF_INDEX_LABEL;
1587
1588 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1589}
1590
1591static void
1592 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1593 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1594{
1595 vlib_main_t *vm = vlib_get_main ();
1596 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1597 int rv = 0;
1598 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1599
1600 VALIDATE_SW_IF_INDEX (mp);
1601
1602 is_no = mp->is_no == 1;
1603 use_default = mp->use_default == 1;
1604 no_advertise = mp->no_advertise == 1;
1605 off_link = mp->off_link == 1;
1606 no_autoconfig = mp->no_autoconfig == 1;
1607 no_onlink = mp->no_onlink == 1;
1608
1609 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1610 (ip6_address_t *) mp->address,
1611 mp->address_length, use_default,
1612 ntohl (mp->val_lifetime),
1613 ntohl (mp->pref_lifetime), no_advertise,
1614 off_link, no_autoconfig, no_onlink, is_no);
1615
1616 BAD_SW_IF_INDEX_LABEL;
1617 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1618}
1619
1620static void
Neale Ranns3f844d02017-02-18 00:03:54 -08001621send_ip6nd_proxy_details (unix_shared_memory_queue_t * q,
1622 u32 context,
1623 const ip46_address_t * addr, u32 sw_if_index)
1624{
1625 vl_api_ip6nd_proxy_details_t *mp;
1626
1627 mp = vl_msg_api_alloc (sizeof (*mp));
1628 memset (mp, 0, sizeof (*mp));
1629 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1630 mp->context = context;
1631 mp->sw_if_index = htonl (sw_if_index);
1632 memcpy (mp->address, addr, 16);
1633
1634 vl_msg_api_send_shmem (q, (u8 *) & mp);
1635}
1636
Neale Ranns3f844d02017-02-18 00:03:54 -08001637typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1638{
1639 u32 *indices;
1640} api_ip6nd_proxy_fib_table_walk_ctx_t;
1641
1642static int
1643api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1644{
1645 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1646
1647 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1648 {
1649 vec_add1 (ctx->indices, fei);
1650 }
1651
1652 return (1);
1653}
1654
1655static void
1656vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1657{
1658 ip6_main_t *im6 = &ip6_main;
1659 fib_table_t *fib_table;
1660 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1661 .indices = NULL,
1662 };
1663 fib_node_index_t *feip;
1664 fib_prefix_t pfx;
1665 unix_shared_memory_queue_t *q;
1666
1667 q = vl_api_client_index_to_input_queue (mp->client_index);
1668 if (q == 0)
1669 {
1670 return;
1671 }
1672
1673 /* *INDENT-OFF* */
1674 pool_foreach (fib_table, im6->fibs,
1675 ({
1676 fib_table_walk(fib_table->ft_index,
1677 FIB_PROTOCOL_IP6,
1678 api_ip6nd_proxy_fib_table_walk,
1679 &ctx);
1680 }));
1681 /* *INDENT-ON* */
1682
1683 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1684
1685 vec_foreach (feip, ctx.indices)
1686 {
1687 fib_entry_get_prefix (*feip, &pfx);
1688
1689 send_ip6nd_proxy_details (q,
1690 mp->context,
1691 &pfx.fp_addr,
1692 fib_entry_get_resolving_interface (*feip));
1693 }
1694
1695 vec_free (ctx.indices);
1696}
1697
1698static void
1699vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1700{
1701 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
1702 int rv = 0;
1703
1704 VALIDATE_SW_IF_INDEX (mp);
1705
1706 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index),
1707 (ip6_address_t *) mp->address, mp->is_del);
1708
1709 BAD_SW_IF_INDEX_LABEL;
1710 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1711}
1712
1713static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001714 vl_api_sw_interface_ip6_enable_disable_t_handler
1715 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1716{
1717 vlib_main_t *vm = vlib_get_main ();
1718 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1719 vnet_main_t *vnm = vnet_get_main ();
1720 int rv = 0;
1721 clib_error_t *error;
1722
1723 vnm->api_errno = 0;
1724
1725 VALIDATE_SW_IF_INDEX (mp);
1726
1727 error =
1728 (mp->enable == 1) ? enable_ip6_interface (vm,
1729 ntohl (mp->sw_if_index)) :
1730 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1731
1732 if (error)
1733 {
1734 clib_error_report (error);
1735 rv = VNET_API_ERROR_UNSPECIFIED;
1736 }
1737 else
1738 {
1739 rv = vnm->api_errno;
1740 }
1741
1742 BAD_SW_IF_INDEX_LABEL;
1743
1744 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1745}
1746
1747static void
1748 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1749 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1750{
1751 vlib_main_t *vm = vlib_get_main ();
1752 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1753 int rv = 0;
1754 clib_error_t *error;
1755 vnet_main_t *vnm = vnet_get_main ();
1756
1757 vnm->api_errno = 0;
1758
1759 VALIDATE_SW_IF_INDEX (mp);
1760
1761 error = set_ip6_link_local_address (vm,
1762 ntohl (mp->sw_if_index),
Neale Ranns75152282017-01-09 01:00:45 -08001763 (ip6_address_t *) mp->address);
Dave Barachb5e8a772016-12-06 12:04:42 -05001764 if (error)
1765 {
1766 clib_error_report (error);
1767 rv = VNET_API_ERROR_UNSPECIFIED;
1768 }
1769 else
1770 {
1771 rv = vnm->api_errno;
1772 }
1773
1774 BAD_SW_IF_INDEX_LABEL;
1775
1776 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1777}
1778
Neale Ranns32e1c012016-11-22 17:07:28 +00001779void
1780vl_mfib_signal_send_one (unix_shared_memory_queue_t * q,
1781 u32 context, const mfib_signal_t * mfs)
1782{
1783 vl_api_mfib_signal_details_t *mp;
1784 mfib_prefix_t prefix;
1785 mfib_table_t *mfib;
1786 mfib_itf_t *mfi;
1787
1788 mp = vl_msg_api_alloc (sizeof (*mp));
1789
1790 memset (mp, 0, sizeof (*mp));
1791 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1792 mp->context = context;
1793
1794 mfi = mfib_itf_get (mfs->mfs_itf);
1795 mfib_entry_get_prefix (mfs->mfs_entry, &prefix);
1796 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
1797 prefix.fp_proto);
1798 mp->table_id = ntohl (mfib->mft_table_id);
1799 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1800
1801 if (FIB_PROTOCOL_IP4 == prefix.fp_proto)
1802 {
1803 mp->grp_address_len = ntohs (prefix.fp_len);
1804
1805 memcpy (mp->grp_address, &prefix.fp_grp_addr.ip4, 4);
1806 if (prefix.fp_len > 32)
1807 {
1808 memcpy (mp->src_address, &prefix.fp_src_addr.ip4, 4);
1809 }
1810 }
1811 else
1812 {
1813 mp->grp_address_len = ntohs (prefix.fp_len);
1814
1815 ASSERT (0);
1816 }
1817
1818 if (0 != mfs->mfs_buffer_len)
1819 {
1820 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1821
1822 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1823 }
1824 else
1825 {
1826 mp->ip_packet_len = 0;
1827 }
1828
1829 vl_msg_api_send_shmem (q, (u8 *) & mp);
1830}
1831
1832static void
1833vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1834{
1835 unix_shared_memory_queue_t *q;
1836
1837 q = vl_api_client_index_to_input_queue (mp->client_index);
1838 if (q == 0)
1839 {
1840 return;
1841 }
1842
1843 while (q->cursize < q->maxsize && mfib_signal_send_one (q, mp->context))
1844 ;
1845}
Dave Barachb5e8a772016-12-06 12:04:42 -05001846
1847#define vl_msg_name_crc_list
1848#include <vnet/ip/ip.api.h>
1849#undef vl_msg_name_crc_list
1850
1851static void
1852setup_message_id_table (api_main_t * am)
1853{
1854#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1855 foreach_vl_msg_name_crc_ip;
1856#undef _
1857}
1858
1859static clib_error_t *
1860ip_api_hookup (vlib_main_t * vm)
1861{
1862 api_main_t *am = &api_main;
1863
1864#define _(N,n) \
1865 vl_msg_api_set_handlers(VL_API_##N, #n, \
1866 vl_api_##n##_t_handler, \
1867 vl_noop_handler, \
1868 vl_api_##n##_t_endian, \
1869 vl_api_##n##_t_print, \
1870 sizeof(vl_api_##n##_t), 1);
1871 foreach_ip_api_msg;
1872#undef _
1873
1874 /*
1875 * Set up the (msg_name, crc, message-id) table
1876 */
1877 setup_message_id_table (am);
1878
1879 return 0;
1880}
1881
1882VLIB_API_INIT_FUNCTION (ip_api_hookup);
1883
1884/*
1885 * fd.io coding-style-patch-verification: ON
1886 *
1887 * Local Variables:
1888 * eval: (c-set-style "gnu")
1889 * End:
1890 */