blob: 20e192057568ab13444982f1522d2d122a35e88c [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, \
Florin Coras595992c2017-11-06 17:17:08 -080082 sw_interface_ip6_set_link_local_address) \
83_(IP_CONTAINER_PROXY_ADD_DEL, ip_container_proxy_add_del )
Dave Barachb5e8a772016-12-06 12:04:42 -050084
85extern void stats_dslock_with_hint (int hint, int tag);
86extern void stats_dsunlock (void);
87
88static void
89send_ip_neighbor_details (u8 is_ipv6,
90 u8 is_static,
91 u8 * mac_address,
92 u8 * ip_address,
93 unix_shared_memory_queue_t * q, u32 context)
94{
95 vl_api_ip_neighbor_details_t *mp;
96
97 mp = vl_msg_api_alloc (sizeof (*mp));
98 memset (mp, 0, sizeof (*mp));
99 mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS);
100 mp->context = context;
101 mp->is_ipv6 = is_ipv6;
102 mp->is_static = is_static;
103 memcpy (mp->mac_address, mac_address, 6);
104 memcpy (mp->ip_address, ip_address, (is_ipv6) ? 16 : 4);
105
106 vl_msg_api_send_shmem (q, (u8 *) & mp);
107}
108
109static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500110vl_api_ip_neighbor_dump_t_handler (vl_api_ip_neighbor_dump_t * mp)
111{
112 unix_shared_memory_queue_t *q;
113
114 q = vl_api_client_index_to_input_queue (mp->client_index);
115 if (q == 0)
116 return;
117
118 u32 sw_if_index = ntohl (mp->sw_if_index);
119
120 if (mp->is_ipv6)
121 {
122 ip6_neighbor_t *n, *ns;
123
124 ns = ip6_neighbors_entries (sw_if_index);
125 /* *INDENT-OFF* */
126 vec_foreach (n, ns)
127 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500128 send_ip_neighbor_details
129 (mp->is_ipv6, ((n->flags & IP6_NEIGHBOR_FLAG_STATIC) ? 1 : 0),
130 (u8 *) n->link_layer_address,
131 (u8 *) & (n->key.ip6_address.as_u8),
132 q, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500133 }
134 /* *INDENT-ON* */
135 vec_free (ns);
136 }
137 else
138 {
139 ethernet_arp_ip4_entry_t *n, *ns;
140
141 ns = ip4_neighbor_entries (sw_if_index);
142 /* *INDENT-OFF* */
143 vec_foreach (n, ns)
144 {
145 send_ip_neighbor_details (mp->is_ipv6,
146 ((n->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC) ? 1 : 0),
147 (u8*) n->ethernet_address,
148 (u8*) & (n->ip4_address.as_u8),
149 q, mp->context);
150 }
151 /* *INDENT-ON* */
152 vec_free (ns);
153 }
154}
155
156
157void
158copy_fib_next_hop (fib_route_path_encode_t * api_rpath, void *fp_arg)
159{
160 int is_ip4;
161 vl_api_fib_path_t *fp = (vl_api_fib_path_t *) fp_arg;
162
Neale Rannsda78f952017-05-24 09:15:43 -0700163 if (api_rpath->rpath.frp_proto == DPO_PROTO_IP4)
Dave Barachb5e8a772016-12-06 12:04:42 -0500164 fp->afi = IP46_TYPE_IP4;
Neale Rannsda78f952017-05-24 09:15:43 -0700165 else if (api_rpath->rpath.frp_proto == DPO_PROTO_IP6)
Dave Barachb5e8a772016-12-06 12:04:42 -0500166 fp->afi = IP46_TYPE_IP6;
167 else
168 {
169 is_ip4 = ip46_address_is_ip4 (&api_rpath->rpath.frp_addr);
170 if (is_ip4)
171 fp->afi = IP46_TYPE_IP4;
172 else
173 fp->afi = IP46_TYPE_IP6;
174 }
175 if (fp->afi == IP46_TYPE_IP4)
176 memcpy (fp->next_hop, &api_rpath->rpath.frp_addr.ip4,
177 sizeof (api_rpath->rpath.frp_addr.ip4));
178 else
179 memcpy (fp->next_hop, &api_rpath->rpath.frp_addr.ip6,
180 sizeof (api_rpath->rpath.frp_addr.ip6));
181}
182
183static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500184send_ip_fib_details (vpe_api_main_t * am,
185 unix_shared_memory_queue_t * q,
Neale Ranns2297af02017-09-12 09:45:04 -0700186 const fib_table_t * table,
187 const fib_prefix_t * pfx,
Dave Barachb5e8a772016-12-06 12:04:42 -0500188 fib_route_path_encode_t * api_rpaths, u32 context)
189{
190 vl_api_ip_fib_details_t *mp;
191 fib_route_path_encode_t *api_rpath;
192 vl_api_fib_path_t *fp;
193 int path_count;
194
195 path_count = vec_len (api_rpaths);
196 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
197 if (!mp)
198 return;
199 memset (mp, 0, sizeof (*mp));
200 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
201 mp->context = context;
202
Neale Ranns2297af02017-09-12 09:45:04 -0700203 mp->table_id = htonl (table->ft_table_id);
204 memcpy (mp->table_name, table->ft_desc,
205 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500206 mp->address_length = pfx->fp_len;
207 memcpy (mp->address, &pfx->fp_addr.ip4, sizeof (pfx->fp_addr.ip4));
208
209 mp->count = htonl (path_count);
210 fp = mp->path;
211 vec_foreach (api_rpath, api_rpaths)
212 {
213 memset (fp, 0, sizeof (*fp));
214 switch (api_rpath->dpo.dpoi_type)
215 {
216 case DPO_RECEIVE:
217 fp->is_local = true;
218 break;
219 case DPO_DROP:
220 fp->is_drop = true;
221 break;
222 case DPO_IP_NULL:
223 switch (api_rpath->dpo.dpoi_index)
224 {
225 case IP_NULL_ACTION_NONE:
226 fp->is_drop = true;
227 break;
228 case IP_NULL_ACTION_SEND_ICMP_UNREACH:
229 fp->is_unreach = true;
230 break;
231 case IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
232 fp->is_prohibit = true;
233 break;
234 default:
235 break;
236 }
237 break;
238 default:
239 break;
240 }
Neale Rannsa0a908f2017-08-01 11:40:03 -0700241 fp->weight = api_rpath->rpath.frp_weight;
242 fp->preference = api_rpath->rpath.frp_preference;
Dave Barachb5e8a772016-12-06 12:04:42 -0500243 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
244 copy_fib_next_hop (api_rpath, fp);
245 fp++;
246 }
247
248 vl_msg_api_send_shmem (q, (u8 *) & mp);
249}
250
Neale Rannsa3af3372017-03-28 03:49:52 -0700251typedef struct vl_api_ip_fib_dump_walk_ctx_t_
252{
253 fib_node_index_t *feis;
254} vl_api_ip_fib_dump_walk_ctx_t;
255
256static int
257vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
258{
259 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
260
261 vec_add1 (ctx->feis, fei);
262
263 return (1);
264}
265
Dave Barachb5e8a772016-12-06 12:04:42 -0500266static void
267vl_api_ip_fib_dump_t_handler (vl_api_ip_fib_dump_t * mp)
268{
269 vpe_api_main_t *am = &vpe_api_main;
270 unix_shared_memory_queue_t *q;
271 ip4_main_t *im = &ip4_main;
272 fib_table_t *fib_table;
Neale Rannsa3af3372017-03-28 03:49:52 -0700273 fib_node_index_t *lfeip;
Dave Barachb5e8a772016-12-06 12:04:42 -0500274 fib_prefix_t pfx;
275 u32 fib_index;
276 fib_route_path_encode_t *api_rpaths;
Neale Rannsa3af3372017-03-28 03:49:52 -0700277 vl_api_ip_fib_dump_walk_ctx_t ctx = {
278 .feis = NULL,
279 };
Dave Barachb5e8a772016-12-06 12:04:42 -0500280
281 q = vl_api_client_index_to_input_queue (mp->client_index);
282 if (q == 0)
283 return;
284
285 /* *INDENT-OFF* */
286 pool_foreach (fib_table, im->fibs,
287 ({
Neale Rannsa3af3372017-03-28 03:49:52 -0700288 fib_table_walk(fib_table->ft_index,
289 FIB_PROTOCOL_IP4,
290 vl_api_ip_fib_dump_walk,
291 &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500292 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500293 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500294
Neale Rannsa3af3372017-03-28 03:49:52 -0700295 vec_sort_with_function (ctx.feis, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500296
Neale Rannsa3af3372017-03-28 03:49:52 -0700297 vec_foreach (lfeip, ctx.feis)
Dave Barachb5e8a772016-12-06 12:04:42 -0500298 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500299 fib_entry_get_prefix (*lfeip, &pfx);
300 fib_index = fib_entry_get_fib_index (*lfeip);
301 fib_table = fib_table_get (fib_index, pfx.fp_proto);
Dave Barachb5e8a772016-12-06 12:04:42 -0500302 api_rpaths = NULL;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500303 fib_entry_encode (*lfeip, &api_rpaths);
Neale Ranns2297af02017-09-12 09:45:04 -0700304 send_ip_fib_details (am, q, fib_table, &pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500305 vec_free (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500306 }
307
Neale Rannsa3af3372017-03-28 03:49:52 -0700308 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500309}
310
311static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500312send_ip6_fib_details (vpe_api_main_t * am,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500313 unix_shared_memory_queue_t * q,
314 u32 table_id, fib_prefix_t * pfx,
315 fib_route_path_encode_t * api_rpaths, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500316{
317 vl_api_ip6_fib_details_t *mp;
318 fib_route_path_encode_t *api_rpath;
319 vl_api_fib_path_t *fp;
320 int path_count;
321
Dave Barachd7cb1b52016-12-09 09:52:16 -0500322 path_count = vec_len (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500323 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
324 if (!mp)
325 return;
326 memset (mp, 0, sizeof (*mp));
327 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
328 mp->context = context;
329
330 mp->table_id = htonl (table_id);
331 mp->address_length = pfx->fp_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500332 memcpy (mp->address, &pfx->fp_addr.ip6, sizeof (pfx->fp_addr.ip6));
Dave Barachb5e8a772016-12-06 12:04:42 -0500333
334 mp->count = htonl (path_count);
335 fp = mp->path;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500336 vec_foreach (api_rpath, api_rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500337 {
338 memset (fp, 0, sizeof (*fp));
339 switch (api_rpath->dpo.dpoi_type)
340 {
341 case DPO_RECEIVE:
342 fp->is_local = true;
343 break;
344 case DPO_DROP:
345 fp->is_drop = true;
346 break;
347 case DPO_IP_NULL:
348 switch (api_rpath->dpo.dpoi_index)
349 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500350 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_NONE:
Dave Barachb5e8a772016-12-06 12:04:42 -0500351 fp->is_drop = true;
352 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500353 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_SEND_ICMP_UNREACH:
Dave Barachb5e8a772016-12-06 12:04:42 -0500354 fp->is_unreach = true;
355 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500356 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
Dave Barachb5e8a772016-12-06 12:04:42 -0500357 fp->is_prohibit = true;
358 break;
359 default:
360 break;
361 }
362 break;
363 default:
364 break;
365 }
Neale Rannsa0a908f2017-08-01 11:40:03 -0700366 fp->weight = api_rpath->rpath.frp_weight;
367 fp->preference = api_rpath->rpath.frp_preference;
Dave Barach00916112017-10-18 10:54:12 -0400368 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500369 copy_fib_next_hop (api_rpath, fp);
370 fp++;
371 }
372
373 vl_msg_api_send_shmem (q, (u8 *) & mp);
374}
375
Dave Barachd7cb1b52016-12-09 09:52:16 -0500376typedef struct apt_ip6_fib_show_ctx_t_
377{
378 u32 fib_index;
379 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500380} api_ip6_fib_show_ctx_t;
381
382static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500383api_ip6_fib_table_put_entries (clib_bihash_kv_24_8_t * kvp, void *arg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500384{
385 api_ip6_fib_show_ctx_t *ctx = arg;
386
387 if ((kvp->key[2] >> 32) == ctx->fib_index)
388 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500389 vec_add1 (ctx->entries, kvp->value);
Dave Barachb5e8a772016-12-06 12:04:42 -0500390 }
391}
392
393static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500394api_ip6_fib_table_get_all (unix_shared_memory_queue_t * q,
395 vl_api_ip6_fib_dump_t * mp,
396 fib_table_t * fib_table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500397{
398 vpe_api_main_t *am = &vpe_api_main;
399 ip6_main_t *im6 = &ip6_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500400 fib_node_index_t *fib_entry_index;
401 api_ip6_fib_show_ctx_t ctx = {
Neale Rannsa3af3372017-03-28 03:49:52 -0700402 .fib_index = fib_table->ft_index,
403 .entries = NULL,
Dave Barachb5e8a772016-12-06 12:04:42 -0500404 };
405 fib_route_path_encode_t *api_rpaths;
406 fib_prefix_t pfx;
407
Dave Barachd7cb1b52016-12-09 09:52:16 -0500408 BV (clib_bihash_foreach_key_value_pair)
409 ((BVT (clib_bihash) *) & im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].
410 ip6_hash, api_ip6_fib_table_put_entries, &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500411
Dave Barachd7cb1b52016-12-09 09:52:16 -0500412 vec_sort_with_function (ctx.entries, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500413
Dave Barachd7cb1b52016-12-09 09:52:16 -0500414 vec_foreach (fib_entry_index, ctx.entries)
415 {
416 fib_entry_get_prefix (*fib_entry_index, &pfx);
417 api_rpaths = NULL;
418 fib_entry_encode (*fib_entry_index, &api_rpaths);
419 send_ip6_fib_details (am, q,
420 fib_table->ft_table_id,
421 &pfx, api_rpaths, mp->context);
422 vec_free (api_rpaths);
423 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500424
Dave Barachd7cb1b52016-12-09 09:52:16 -0500425 vec_free (ctx.entries);
Dave Barachb5e8a772016-12-06 12:04:42 -0500426}
427
428static void
429vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
430{
431 unix_shared_memory_queue_t *q;
432 ip6_main_t *im6 = &ip6_main;
433 fib_table_t *fib_table;
434
435 q = vl_api_client_index_to_input_queue (mp->client_index);
436 if (q == 0)
437 return;
438
439 /* *INDENT-OFF* */
440 pool_foreach (fib_table, im6->fibs,
441 ({
442 api_ip6_fib_table_get_all(q, mp, fib_table);
443 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500444 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500445}
446
447static void
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800448send_ip_mfib_details (unix_shared_memory_queue_t * q,
449 u32 context, u32 table_id, fib_node_index_t mfei)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800450{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800451 fib_route_path_encode_t *api_rpath, *api_rpaths = NULL;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800452 vl_api_ip_mfib_details_t *mp;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800453 mfib_entry_t *mfib_entry;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800454 vl_api_fib_path_t *fp;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800455 mfib_prefix_t pfx;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800456 int path_count;
457
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800458 mfib_entry = mfib_entry_get (mfei);
459 mfib_entry_get_prefix (mfei, &pfx);
460 mfib_entry_encode (mfei, &api_rpaths);
461
Neale Ranns5a8123b2017-01-26 01:18:23 -0800462 path_count = vec_len (api_rpaths);
463 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
464 if (!mp)
465 return;
466 memset (mp, 0, sizeof (*mp));
467 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
468 mp->context = context;
469
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800470 mp->rpf_id = mfib_entry->mfe_rpf_id;
471 mp->entry_flags = mfib_entry->mfe_flags;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800472 mp->table_id = htonl (table_id);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800473 mp->address_length = pfx.fp_len;
474 memcpy (mp->grp_address, &pfx.fp_grp_addr.ip4,
475 sizeof (pfx.fp_grp_addr.ip4));
476 memcpy (mp->src_address, &pfx.fp_src_addr.ip4,
477 sizeof (pfx.fp_src_addr.ip4));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800478
479 mp->count = htonl (path_count);
480 fp = mp->path;
481 vec_foreach (api_rpath, api_rpaths)
482 {
483 memset (fp, 0, sizeof (*fp));
484
485 fp->weight = 0;
486 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
487 copy_fib_next_hop (api_rpath, fp);
488 fp++;
489 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800490 vec_free (api_rpaths);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800491
492 vl_msg_api_send_shmem (q, (u8 *) & mp);
493}
494
495typedef struct vl_api_ip_mfib_dump_ctc_t_
496{
497 fib_node_index_t *entries;
498} vl_api_ip_mfib_dump_ctc_t;
499
500static int
501vl_api_ip_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
502{
503 vl_api_ip_mfib_dump_ctc_t *ctx = arg;
504
505 vec_add1 (ctx->entries, fei);
506
507 return (0);
508}
509
510static void
511vl_api_ip_mfib_dump_t_handler (vl_api_ip_mfib_dump_t * mp)
512{
Neale Ranns5a8123b2017-01-26 01:18:23 -0800513 unix_shared_memory_queue_t *q;
514 ip4_main_t *im = &ip4_main;
515 mfib_table_t *mfib_table;
516 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800517 vl_api_ip_mfib_dump_ctc_t ctx = {
518 .entries = NULL,
519 };
520
521 q = vl_api_client_index_to_input_queue (mp->client_index);
522 if (q == 0)
523 return;
524
525
526 /* *INDENT-OFF* */
527 pool_foreach (mfib_table, im->mfibs,
528 ({
529 ip4_mfib_table_walk(&mfib_table->v4,
530 vl_api_ip_mfib_table_dump_walk,
531 &ctx);
532
533 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
534
535 vec_foreach (mfeip, ctx.entries)
536 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800537 send_ip_mfib_details (q, mp->context,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800538 mfib_table->mft_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800539 *mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800540 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800541 vec_reset_length (ctx.entries);
542
543 }));
544 /* *INDENT-ON* */
545
546 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800547}
548
549static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800550send_ip6_mfib_details (vpe_api_main_t * am,
551 unix_shared_memory_queue_t * q,
552 u32 table_id,
553 mfib_prefix_t * pfx,
554 fib_route_path_encode_t * api_rpaths, u32 context)
555{
556 vl_api_ip6_mfib_details_t *mp;
557 fib_route_path_encode_t *api_rpath;
558 vl_api_fib_path_t *fp;
559 int path_count;
560
561 path_count = vec_len (api_rpaths);
562 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
563 if (!mp)
564 return;
565 memset (mp, 0, sizeof (*mp));
566 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
567 mp->context = context;
568
569 mp->table_id = htonl (table_id);
570 mp->address_length = pfx->fp_len;
571 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip6,
572 sizeof (pfx->fp_grp_addr.ip6));
573 memcpy (mp->src_address, &pfx->fp_src_addr.ip6,
574 sizeof (pfx->fp_src_addr.ip6));
575
576 mp->count = htonl (path_count);
577 fp = mp->path;
578 vec_foreach (api_rpath, api_rpaths)
579 {
580 memset (fp, 0, sizeof (*fp));
581
582 fp->weight = 0;
583 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
584 copy_fib_next_hop (api_rpath, fp);
585 fp++;
586 }
587
588 vl_msg_api_send_shmem (q, (u8 *) & mp);
589}
590
591typedef struct vl_api_ip6_mfib_dump_ctc_t_
592{
593 fib_node_index_t *entries;
594} vl_api_ip6_mfib_dump_ctc_t;
595
596static int
597vl_api_ip6_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
598{
599 vl_api_ip6_mfib_dump_ctc_t *ctx = arg;
600
601 vec_add1 (ctx->entries, fei);
602
603 return (0);
604}
605
606static void
607vl_api_ip6_mfib_dump_t_handler (vl_api_ip6_mfib_dump_t * mp)
608{
609 vpe_api_main_t *am = &vpe_api_main;
610 unix_shared_memory_queue_t *q;
611 ip6_main_t *im = &ip6_main;
612 mfib_table_t *mfib_table;
613 fib_node_index_t *mfeip;
614 mfib_prefix_t pfx;
615 fib_route_path_encode_t *api_rpaths = NULL;
616 vl_api_ip6_mfib_dump_ctc_t ctx = {
617 .entries = NULL,
618 };
619
620 q = vl_api_client_index_to_input_queue (mp->client_index);
621 if (q == 0)
622 return;
623
624
625 /* *INDENT-OFF* */
626 pool_foreach (mfib_table, im->mfibs,
627 ({
628 ip6_mfib_table_walk(&mfib_table->v6,
629 vl_api_ip6_mfib_table_dump_walk,
630 &ctx);
631
632 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
633
634 vec_foreach(mfeip, ctx.entries)
635 {
636 mfib_entry_get_prefix (*mfeip, &pfx);
637 mfib_entry_encode (*mfeip, &api_rpaths);
638 send_ip6_mfib_details (am, q,
639 mfib_table->mft_table_id,
640 &pfx, api_rpaths,
641 mp->context);
642 }
643 vec_reset_length (api_rpaths);
644 vec_reset_length (ctx.entries);
645
646 }));
647 /* *INDENT-ON* */
648
649 vec_free (ctx.entries);
650 vec_free (api_rpaths);
651}
652
653static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700654vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
655 vlib_main_t * vm)
656{
657 vl_api_ip_punt_police_reply_t *rmp;
658 int rv = 0;
659
660 if (mp->is_ip6)
661 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
662 else
663 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
664
665 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
666}
667
668static void
669vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
670 vlib_main_t * vm)
671{
672 vl_api_ip_punt_redirect_reply_t *rmp;
673 int rv = 0;
674
675 if (mp->is_add)
676 {
677 ip46_address_t nh;
678
679 memset (&nh, 0, sizeof (nh));
680
681 if (mp->is_ip6)
682 {
683 memcpy (&nh.ip6, mp->nh, sizeof (nh.ip6));
684
685 ip6_punt_redirect_add (ntohl (mp->rx_sw_if_index),
686 ntohl (mp->tx_sw_if_index), &nh);
687 }
688 else
689 {
690 memcpy (&nh.ip4, mp->nh, sizeof (nh.ip4));
691
692 ip4_punt_redirect_add (ntohl (mp->rx_sw_if_index),
693 ntohl (mp->tx_sw_if_index), &nh);
694 }
695 }
696 else
697 {
698 if (mp->is_ip6)
699 {
700 ip6_punt_redirect_del (ntohl (mp->rx_sw_if_index));
701 }
702 else
703 {
704 ip4_punt_redirect_del (ntohl (mp->rx_sw_if_index));
705 }
706 }
707
708 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
709}
710
711static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500712vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
713 vlib_main_t * vm)
714{
715 vl_api_ip_neighbor_add_del_reply_t *rmp;
716 vnet_main_t *vnm = vnet_get_main ();
717 int rv = 0;
718
719 VALIDATE_SW_IF_INDEX (mp);
720
721 stats_dslock_with_hint (1 /* release hint */ , 7 /* tag */ );
722
723 /*
724 * there's no validation here of the ND/ARP entry being added.
725 * The expectation is that the FIB will ensure that nothing bad
726 * will come of adding bogus entries.
727 */
728 if (mp->is_ipv6)
729 {
730 if (mp->is_add)
731 rv = vnet_set_ip6_ethernet_neighbor
732 (vm, ntohl (mp->sw_if_index),
733 (ip6_address_t *) (mp->dst_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800734 mp->mac_address, sizeof (mp->mac_address), mp->is_static,
735 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500736 else
737 rv = vnet_unset_ip6_ethernet_neighbor
738 (vm, ntohl (mp->sw_if_index),
739 (ip6_address_t *) (mp->dst_address),
740 mp->mac_address, sizeof (mp->mac_address));
741 }
742 else
743 {
744 ethernet_arp_ip4_over_ethernet_address_t a;
745
746 clib_memcpy (&a.ethernet, mp->mac_address, 6);
747 clib_memcpy (&a.ip4, mp->dst_address, 4);
748
749 if (mp->is_add)
750 rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800751 &a, mp->is_static,
752 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500753 else
754 rv =
755 vnet_arp_unset_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index), &a);
756 }
757
Dave Barachb5e8a772016-12-06 12:04:42 -0500758 stats_dsunlock ();
Neale Ranns8edad032017-10-09 05:26:13 -0700759
760 BAD_SW_IF_INDEX_LABEL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500761 REPLY_MACRO (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
762}
763
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700764void
Neale Ranns15002542017-09-10 04:39:11 -0700765ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
766{
767 u32 fib_index, mfib_index;
768
769 /*
770 * ignore action on the default table - this is always present
771 * and cannot be added nor deleted from the API
772 */
773 if (0 != table_id)
774 {
775 /*
776 * The API holds only one lock on the table.
777 * i.e. it can be added many times via the API but needs to be
778 * deleted only once.
779 * The FIB index for unicast and multicast is not necessarily the
780 * same, since internal VPP systesm (like LISP and SR) create
781 * their own unicast tables.
782 */
783 fib_index = fib_table_find (fproto, table_id);
784 mfib_index = mfib_table_find (fproto, table_id);
785
786 if (~0 != fib_index)
787 {
788 fib_table_unlock (fib_index, fproto,
789 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
790 }
791 if (~0 != mfib_index)
792 {
793 mfib_table_unlock (mfib_index, fproto,
794 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
795 }
796 }
797}
798
799void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700800vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
801{
802 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700803 fib_protocol_t fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
804 u32 table_id = ntohl (mp->table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700805 int rv = 0;
806
Neale Ranns15002542017-09-10 04:39:11 -0700807 if (mp->is_add)
808 {
Neale Ranns2297af02017-09-12 09:45:04 -0700809 ip_table_create (fproto, table_id, 1, mp->name);
Neale Ranns15002542017-09-10 04:39:11 -0700810 }
811 else
812 {
813 ip_table_delete (fproto, table_id, 1);
814 }
815
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700816 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
817}
818
Dave Barachb5e8a772016-12-06 12:04:42 -0500819int
820add_del_route_t_handler (u8 is_multipath,
821 u8 is_add,
822 u8 is_drop,
823 u8 is_unreach,
824 u8 is_prohibit,
825 u8 is_local,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800826 u8 is_multicast,
Dave Barachb5e8a772016-12-06 12:04:42 -0500827 u8 is_classify,
828 u32 classify_table_index,
829 u8 is_resolve_host,
830 u8 is_resolve_attached,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800831 u8 is_interface_rx,
832 u8 is_rpf_id,
Neale Ranns6f631152017-10-03 08:20:21 -0700833 u8 is_l2_bridged,
Neale Ranns054c03a2017-10-13 05:15:07 -0700834 u8 is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800835 u8 is_udp_encap,
Dave Barachb5e8a772016-12-06 12:04:42 -0500836 u32 fib_index,
837 const fib_prefix_t * prefix,
Neale Rannsda78f952017-05-24 09:15:43 -0700838 dpo_proto_t next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500839 const ip46_address_t * next_hop,
Neale Ranns810086d2017-11-05 16:26:46 -0800840 u32 next_hop_id,
Dave Barachb5e8a772016-12-06 12:04:42 -0500841 u32 next_hop_sw_if_index,
842 u8 next_hop_fib_index,
Neale Ranns57b58602017-07-15 07:37:25 -0700843 u16 next_hop_weight,
844 u16 next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500845 mpls_label_t next_hop_via_label,
846 mpls_label_t * next_hop_out_label_stack)
847{
848 vnet_classify_main_t *cm = &vnet_classify_main;
849 fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
850 fib_route_path_t path = {
Neale Rannsda78f952017-05-24 09:15:43 -0700851 .frp_proto = next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500852 .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
853 .frp_sw_if_index = next_hop_sw_if_index,
854 .frp_fib_index = next_hop_fib_index,
855 .frp_weight = next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -0700856 .frp_preference = next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500857 .frp_label_stack = next_hop_out_label_stack,
858 };
859 fib_route_path_t *paths = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800860 fib_entry_flag_t entry_flags = FIB_ENTRY_FLAG_NONE;
Dave Barachb5e8a772016-12-06 12:04:42 -0500861
Neale Rannscaac3502017-09-18 18:04:10 -0700862 /*
863 * the special INVALID label meams we are not recursing via a
864 * label. Exp-null value is never a valid via-label so that
865 * also means it's not a via-label and means clients that set
866 * it to 0 by default get the expected behaviour
867 */
868 if ((MPLS_LABEL_INVALID != next_hop_via_label) && (0 != next_hop_via_label))
Dave Barachb5e8a772016-12-06 12:04:42 -0500869 {
Neale Rannsda78f952017-05-24 09:15:43 -0700870 path.frp_proto = DPO_PROTO_MPLS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500871 path.frp_local_label = next_hop_via_label;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800872 path.frp_eos = MPLS_NON_EOS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500873 }
Neale Ranns6f631152017-10-03 08:20:21 -0700874 if (is_l2_bridged)
875 path.frp_proto = DPO_PROTO_ETHERNET;
Dave Barachb5e8a772016-12-06 12:04:42 -0500876 if (is_resolve_host)
877 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
878 if (is_resolve_attached)
879 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800880 if (is_interface_rx)
881 path_flags |= FIB_ROUTE_PATH_INTF_RX;
882 if (is_rpf_id)
883 path_flags |= FIB_ROUTE_PATH_RPF_ID;
Neale Ranns054c03a2017-10-13 05:15:07 -0700884 if (is_source_lookup)
885 path_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800886 if (is_multicast)
887 entry_flags |= FIB_ENTRY_FLAG_MULTICAST;
Neale Ranns810086d2017-11-05 16:26:46 -0800888 if (is_udp_encap)
889 {
890 path_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
891 path.frp_udp_encap_id = next_hop_id;
892 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500893
894 path.frp_flags = path_flags;
895
896 if (is_multipath)
897 {
898 stats_dslock_with_hint (1 /* release hint */ , 10 /* tag */ );
899
900
901 vec_add1 (paths, path);
902
903 if (is_add)
904 fib_table_entry_path_add2 (fib_index,
905 prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800906 FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500907 else
908 fib_table_entry_path_remove2 (fib_index,
909 prefix, FIB_SOURCE_API, paths);
910
911 vec_free (paths);
912 stats_dsunlock ();
913 return 0;
914 }
915
916 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
917
918 if (is_drop || is_local || is_classify || is_unreach || is_prohibit)
919 {
920 /*
921 * special route types that link directly to the adj
922 */
923 if (is_add)
924 {
925 dpo_id_t dpo = DPO_INVALID;
926 dpo_proto_t dproto;
927
928 dproto = fib_proto_to_dpo (prefix->fp_proto);
929
930 if (is_drop)
931 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
932 else if (is_local)
933 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
934 else if (is_unreach)
935 ip_null_dpo_add_and_lock (dproto,
936 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
937 else if (is_prohibit)
938 ip_null_dpo_add_and_lock (dproto,
939 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
940 &dpo);
941 else if (is_classify)
942 {
943 if (pool_is_free_index (cm->tables,
944 ntohl (classify_table_index)))
945 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500946 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500947 return VNET_API_ERROR_NO_SUCH_TABLE;
948 }
949
950 dpo_set (&dpo, DPO_CLASSIFY, dproto,
951 classify_dpo_create (dproto,
952 ntohl (classify_table_index)));
953 }
954 else
955 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500956 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500957 return VNET_API_ERROR_NO_SUCH_TABLE;
958 }
959
960 fib_table_entry_special_dpo_update (fib_index,
961 prefix,
962 FIB_SOURCE_API,
963 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
964 dpo_reset (&dpo);
965 }
966 else
967 {
968 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
969 }
970 }
971 else
972 {
973 if (is_add)
974 {
975 vec_add1 (paths, path);
976 fib_table_entry_update (fib_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800977 prefix, FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500978 vec_free (paths);
979 }
980 else
981 {
982 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
983 }
984 }
985
Dave Barachd7cb1b52016-12-09 09:52:16 -0500986 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500987 return (0);
988}
989
990int
991add_del_route_check (fib_protocol_t table_proto,
992 u32 table_id,
993 u32 next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700994 dpo_proto_t next_hop_table_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500995 u32 next_hop_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800996 u8 is_rpf_id, u32 * fib_index, u32 * next_hop_fib_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500997{
998 vnet_main_t *vnm = vnet_get_main ();
999
Florin Corasd0a59722017-10-15 17:41:21 +00001000 /* Temporaray whilst I do the CSIT dance */
1001 u8 create_missing_tables = 1;
1002
Dave Barachb5e8a772016-12-06 12:04:42 -05001003 *fib_index = fib_table_find (table_proto, ntohl (table_id));
1004 if (~0 == *fib_index)
1005 {
Florin Corasd0a59722017-10-15 17:41:21 +00001006 if (create_missing_tables)
1007 {
1008 *fib_index = fib_table_find_or_create_and_lock (table_proto,
1009 ntohl (table_id),
1010 FIB_SOURCE_API);
1011 }
1012 else
1013 {
1014 /* No such VRF, and we weren't asked to create one */
1015 return VNET_API_ERROR_NO_SUCH_FIB;
1016 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001017 }
1018
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001019 if (!is_rpf_id && ~0 != ntohl (next_hop_sw_if_index))
Dave Barachb5e8a772016-12-06 12:04:42 -05001020 {
1021 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1022 ntohl (next_hop_sw_if_index)))
1023 {
1024 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1025 }
1026 }
1027 else
1028 {
Neale Rannsda78f952017-05-24 09:15:43 -07001029 fib_protocol_t fib_nh_proto;
1030
1031 if (next_hop_table_proto > DPO_PROTO_MPLS)
1032 return (0);
1033
1034 fib_nh_proto = dpo_proto_to_fib (next_hop_table_proto);
1035
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001036 if (is_rpf_id)
Neale Rannsda78f952017-05-24 09:15:43 -07001037 *next_hop_fib_index = mfib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001038 ntohl (next_hop_table_id));
1039 else
Neale Rannsda78f952017-05-24 09:15:43 -07001040 *next_hop_fib_index = fib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001041 ntohl (next_hop_table_id));
Dave Barachb5e8a772016-12-06 12:04:42 -05001042
1043 if (~0 == *next_hop_fib_index)
1044 {
Florin Corasd0a59722017-10-15 17:41:21 +00001045 if (create_missing_tables)
1046 {
1047 if (is_rpf_id)
1048 *next_hop_fib_index =
1049 mfib_table_find_or_create_and_lock (fib_nh_proto,
1050 ntohl
1051 (next_hop_table_id),
1052 MFIB_SOURCE_API);
1053 else
1054 *next_hop_fib_index =
1055 fib_table_find_or_create_and_lock (fib_nh_proto,
1056 ntohl
1057 (next_hop_table_id),
1058 FIB_SOURCE_API);
1059 }
1060 else
1061 {
1062 /* No such VRF, and we weren't asked to create one */
1063 return VNET_API_ERROR_NO_SUCH_FIB;
1064 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001065 }
1066 }
1067
1068 return (0);
1069}
1070
1071static int
1072ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1073{
1074 u32 fib_index, next_hop_fib_index;
1075 mpls_label_t *label_stack = NULL;
1076 int rv, ii, n_labels;;
1077
1078 rv = add_del_route_check (FIB_PROTOCOL_IP4,
1079 mp->table_id,
1080 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001081 DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -05001082 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001083 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001084
1085 if (0 != rv)
1086 return (rv);
1087
1088 fib_prefix_t pfx = {
1089 .fp_len = mp->dst_address_length,
1090 .fp_proto = FIB_PROTOCOL_IP4,
1091 };
1092 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
1093
1094 ip46_address_t nh;
1095 memset (&nh, 0, sizeof (nh));
1096 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
1097
1098 n_labels = mp->next_hop_n_out_labels;
1099 if (n_labels == 0)
1100 ;
1101 else if (1 == n_labels)
1102 vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
1103 else
1104 {
1105 vec_validate (label_stack, n_labels - 1);
1106 for (ii = 0; ii < n_labels; ii++)
1107 label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
1108 }
1109
1110 return (add_del_route_t_handler (mp->is_multipath,
1111 mp->is_add,
1112 mp->is_drop,
1113 mp->is_unreach,
1114 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001115 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001116 mp->is_classify,
1117 mp->classify_table_index,
1118 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001119 mp->is_resolve_attached, 0, 0,
Neale Ranns6f631152017-10-03 08:20:21 -07001120 mp->is_l2_bridged,
Neale Ranns054c03a2017-10-13 05:15:07 -07001121 mp->is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -08001122 mp->is_udp_encap,
Neale Rannsda78f952017-05-24 09:15:43 -07001123 fib_index, &pfx, DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -05001124 &nh,
Neale Ranns810086d2017-11-05 16:26:46 -08001125 ntohl (mp->next_hop_id),
Dave Barachb5e8a772016-12-06 12:04:42 -05001126 ntohl (mp->next_hop_sw_if_index),
1127 next_hop_fib_index,
1128 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001129 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001130 ntohl (mp->next_hop_via_label),
1131 label_stack));
1132}
1133
1134static int
1135ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1136{
1137 u32 fib_index, next_hop_fib_index;
1138 mpls_label_t *label_stack = NULL;
1139 int rv, ii, n_labels;;
1140
1141 rv = add_del_route_check (FIB_PROTOCOL_IP6,
1142 mp->table_id,
1143 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001144 DPO_PROTO_IP6,
Dave Barachb5e8a772016-12-06 12:04:42 -05001145 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001146 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001147
1148 if (0 != rv)
1149 return (rv);
1150
1151 fib_prefix_t pfx = {
1152 .fp_len = mp->dst_address_length,
1153 .fp_proto = FIB_PROTOCOL_IP6,
1154 };
1155 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1156
1157 ip46_address_t nh;
1158 memset (&nh, 0, sizeof (nh));
1159 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1160
1161 n_labels = mp->next_hop_n_out_labels;
1162 if (n_labels == 0)
1163 ;
1164 else if (1 == n_labels)
1165 vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
1166 else
1167 {
1168 vec_validate (label_stack, n_labels - 1);
1169 for (ii = 0; ii < n_labels; ii++)
1170 label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
1171 }
1172
1173 return (add_del_route_t_handler (mp->is_multipath,
1174 mp->is_add,
1175 mp->is_drop,
1176 mp->is_unreach,
1177 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001178 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001179 mp->is_classify,
1180 mp->classify_table_index,
1181 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001182 mp->is_resolve_attached, 0, 0,
Neale Ranns6f631152017-10-03 08:20:21 -07001183 mp->is_l2_bridged,
Neale Ranns054c03a2017-10-13 05:15:07 -07001184 mp->is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -08001185 mp->is_udp_encap,
Neale Rannsda78f952017-05-24 09:15:43 -07001186 fib_index, &pfx, DPO_PROTO_IP6,
Neale Ranns810086d2017-11-05 16:26:46 -08001187 &nh, ntohl (mp->next_hop_id),
1188 ntohl (mp->next_hop_sw_if_index),
Dave Barachb5e8a772016-12-06 12:04:42 -05001189 next_hop_fib_index,
1190 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001191 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001192 ntohl (mp->next_hop_via_label),
1193 label_stack));
1194}
1195
1196void
1197vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1198{
1199 vl_api_ip_add_del_route_reply_t *rmp;
1200 int rv;
1201 vnet_main_t *vnm = vnet_get_main ();
1202
1203 vnm->api_errno = 0;
1204
1205 if (mp->is_ipv6)
1206 rv = ip6_add_del_route_t_handler (mp);
1207 else
1208 rv = ip4_add_del_route_t_handler (mp);
1209
1210 rv = (rv == 0) ? vnm->api_errno : rv;
1211
1212 REPLY_MACRO (VL_API_IP_ADD_DEL_ROUTE_REPLY);
1213}
1214
Neale Ranns15002542017-09-10 04:39:11 -07001215void
Neale Ranns2297af02017-09-12 09:45:04 -07001216ip_table_create (fib_protocol_t fproto,
1217 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -07001218{
1219 u32 fib_index, mfib_index;
1220
1221 /*
1222 * ignore action on the default table - this is always present
1223 * and cannot be added nor deleted from the API
1224 */
1225 if (0 != table_id)
1226 {
1227 /*
1228 * The API holds only one lock on the table.
1229 * i.e. it can be added many times via the API but needs to be
1230 * deleted only once.
1231 * The FIB index for unicast and multicast is not necessarily the
1232 * same, since internal VPP systesm (like LISP and SR) create
1233 * their own unicast tables.
1234 */
1235 fib_index = fib_table_find (fproto, table_id);
1236 mfib_index = mfib_table_find (fproto, table_id);
1237
1238 if (~0 == fib_index)
1239 {
Neale Ranns2297af02017-09-12 09:45:04 -07001240 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
1241 (is_api ?
1242 FIB_SOURCE_API :
1243 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001244 }
1245 if (~0 == mfib_index)
1246 {
Neale Ranns2297af02017-09-12 09:45:04 -07001247 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
1248 (is_api ?
1249 MFIB_SOURCE_API :
1250 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001251 }
1252 }
1253}
1254
Neale Ranns32e1c012016-11-22 17:07:28 +00001255static int
1256add_del_mroute_check (fib_protocol_t table_proto,
1257 u32 table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001258 u32 next_hop_sw_if_index, u8 is_local, u32 * fib_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001259{
1260 vnet_main_t *vnm = vnet_get_main ();
1261
1262 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1263 if (~0 == *fib_index)
1264 {
Neale Ranns15002542017-09-10 04:39:11 -07001265 /* No such table */
1266 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Ranns32e1c012016-11-22 17:07:28 +00001267 }
1268
1269 if (~0 != ntohl (next_hop_sw_if_index))
1270 {
1271 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1272 ntohl (next_hop_sw_if_index)))
1273 {
1274 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1275 }
1276 }
1277
1278 return (0);
1279}
1280
1281static int
1282mroute_add_del_handler (u8 is_add,
1283 u8 is_local,
1284 u32 fib_index,
1285 const mfib_prefix_t * prefix,
1286 u32 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001287 fib_rpf_id_t rpf_id,
Neale Ranns32e1c012016-11-22 17:07:28 +00001288 u32 next_hop_sw_if_index, u32 itf_flags)
1289{
1290 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1291
1292 fib_route_path_t path = {
1293 .frp_sw_if_index = next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001294 .frp_proto = fib_proto_to_dpo (prefix->fp_proto),
Neale Ranns32e1c012016-11-22 17:07:28 +00001295 };
1296
1297 if (is_local)
1298 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1299
1300
1301 if (!is_local && ~0 == next_hop_sw_if_index)
1302 {
1303 mfib_table_entry_update (fib_index, prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001304 MFIB_SOURCE_API, rpf_id, entry_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001305 }
1306 else
1307 {
1308 if (is_add)
1309 {
1310 mfib_table_entry_path_update (fib_index, prefix,
1311 MFIB_SOURCE_API, &path, itf_flags);
1312 }
1313 else
1314 {
1315 mfib_table_entry_path_remove (fib_index, prefix,
1316 MFIB_SOURCE_API, &path);
1317 }
1318 }
1319
1320 stats_dsunlock ();
1321 return (0);
1322}
1323
1324static int
1325api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1326{
1327 fib_protocol_t fproto;
1328 u32 fib_index;
1329 int rv;
1330
1331 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1332 rv = add_del_mroute_check (fproto,
1333 mp->table_id,
1334 mp->next_hop_sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -07001335 mp->is_local, &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001336
1337 if (0 != rv)
1338 return (rv);
1339
1340 mfib_prefix_t pfx = {
1341 .fp_len = ntohs (mp->grp_address_length),
1342 .fp_proto = fproto,
1343 };
1344
1345 if (FIB_PROTOCOL_IP4 == fproto)
1346 {
1347 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1348 sizeof (pfx.fp_grp_addr.ip4));
1349 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1350 sizeof (pfx.fp_src_addr.ip4));
1351 }
1352 else
1353 {
1354 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1355 sizeof (pfx.fp_grp_addr.ip6));
1356 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1357 sizeof (pfx.fp_src_addr.ip6));
1358 }
1359
1360 return (mroute_add_del_handler (mp->is_add,
1361 mp->is_local,
1362 fib_index, &pfx,
1363 ntohl (mp->entry_flags),
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001364 ntohl (mp->rpf_id),
Neale Ranns32e1c012016-11-22 17:07:28 +00001365 ntohl (mp->next_hop_sw_if_index),
1366 ntohl (mp->itf_flags)));
1367}
1368
1369void
1370vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1371{
1372 vl_api_ip_mroute_add_del_reply_t *rmp;
1373 int rv;
1374 vnet_main_t *vnm = vnet_get_main ();
1375
1376 vnm->api_errno = 0;
1377
1378 rv = api_mroute_add_del_t_handler (mp);
1379
1380 rv = (rv == 0) ? vnm->api_errno : rv;
1381
1382 REPLY_MACRO (VL_API_IP_MROUTE_ADD_DEL_REPLY);
1383}
1384
Dave Barachb5e8a772016-12-06 12:04:42 -05001385static void
1386send_ip_details (vpe_api_main_t * am,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001387 unix_shared_memory_queue_t * q, u32 sw_if_index,
1388 u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001389{
1390 vl_api_ip_details_t *mp;
1391
1392 mp = vl_msg_api_alloc (sizeof (*mp));
1393 memset (mp, 0, sizeof (*mp));
1394 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1395
1396 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001397 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001398 mp->context = context;
1399
1400 vl_msg_api_send_shmem (q, (u8 *) & mp);
1401}
1402
1403static void
1404send_ip_address_details (vpe_api_main_t * am,
1405 unix_shared_memory_queue_t * q,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001406 u8 * ip, u16 prefix_length,
1407 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001408{
1409 vl_api_ip_address_details_t *mp;
1410
1411 mp = vl_msg_api_alloc (sizeof (*mp));
1412 memset (mp, 0, sizeof (*mp));
1413 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1414
1415 if (is_ipv6)
1416 {
1417 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1418 }
1419 else
1420 {
1421 u32 *tp = (u32 *) mp->ip;
1422 *tp = *(u32 *) ip;
1423 }
1424 mp->prefix_length = prefix_length;
1425 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001426 mp->sw_if_index = htonl (sw_if_index);
1427 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001428
1429 vl_msg_api_send_shmem (q, (u8 *) & mp);
1430}
1431
1432static void
1433vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1434{
1435 vpe_api_main_t *am = &vpe_api_main;
1436 unix_shared_memory_queue_t *q;
1437 ip6_address_t *r6;
1438 ip4_address_t *r4;
1439 ip6_main_t *im6 = &ip6_main;
1440 ip4_main_t *im4 = &ip4_main;
1441 ip_lookup_main_t *lm6 = &im6->lookup_main;
1442 ip_lookup_main_t *lm4 = &im4->lookup_main;
1443 ip_interface_address_t *ia = 0;
1444 u32 sw_if_index = ~0;
1445 int rv __attribute__ ((unused)) = 0;
1446
1447 VALIDATE_SW_IF_INDEX (mp);
1448
1449 sw_if_index = ntohl (mp->sw_if_index);
1450
1451 q = vl_api_client_index_to_input_queue (mp->client_index);
1452 if (q == 0)
1453 return;
1454
Dave Barachb5e8a772016-12-06 12:04:42 -05001455 if (mp->is_ipv6)
1456 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001457 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001458 foreach_ip_interface_address (lm6, ia, sw_if_index,
1459 1 /* honor unnumbered */,
1460 ({
1461 r6 = ip_interface_address_get_address (lm6, ia);
1462 u16 prefix_length = ia->address_length;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001463 send_ip_address_details(am, q, (u8*)r6, prefix_length,
1464 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001465 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001466 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001467 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001468 else
1469 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001470 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001471 foreach_ip_interface_address (lm4, ia, sw_if_index,
1472 1 /* honor unnumbered */,
1473 ({
1474 r4 = ip_interface_address_get_address (lm4, ia);
1475 u16 prefix_length = ia->address_length;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001476 send_ip_address_details(am, q, (u8*)r4, prefix_length,
1477 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001478 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001479 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001480 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001481 BAD_SW_IF_INDEX_LABEL;
1482}
1483
1484static void
1485vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1486{
1487 vpe_api_main_t *am = &vpe_api_main;
1488 vnet_main_t *vnm = vnet_get_main ();
1489 vlib_main_t *vm = vlib_get_main ();
1490 vnet_interface_main_t *im = &vnm->interface_main;
1491 unix_shared_memory_queue_t *q;
1492 vnet_sw_interface_t *si, *sorted_sis;
1493 u32 sw_if_index = ~0;
1494
1495 q = vl_api_client_index_to_input_queue (mp->client_index);
1496 if (q == 0)
1497 {
1498 return;
1499 }
1500
1501 /* Gather interfaces. */
1502 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1503 _vec_len (sorted_sis) = 0;
1504 /* *INDENT-OFF* */
1505 pool_foreach (si, im->sw_interfaces,
1506 ({
1507 vec_add1 (sorted_sis, si[0]);
1508 }));
1509 /* *INDENT-ON* */
1510
1511 vec_foreach (si, sorted_sis)
1512 {
1513 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1514 {
1515 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1516 {
1517 continue;
1518 }
1519 sw_if_index = si->sw_if_index;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001520 send_ip_details (am, q, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001521 }
1522 }
1523}
1524
1525static void
1526set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1527{
1528 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001529 int rv;
1530 u32 table_id;
1531 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001532
Neale Ranns227038a2017-04-21 01:07:59 -07001533 table_id = ntohl (mp->vrf_id);
1534
1535#define _(a,b) if (mp->a) flow_hash_config |= b;
1536 foreach_flow_hash_bit;
1537#undef _
1538
1539 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001540
1541 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1542}
1543
1544static void
1545set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1546{
1547 vl_api_set_ip_flow_hash_reply_t *rmp;
1548 int rv;
1549 u32 table_id;
1550 flow_hash_config_t flow_hash_config = 0;
1551
1552 table_id = ntohl (mp->vrf_id);
1553
1554#define _(a,b) if (mp->a) flow_hash_config |= b;
1555 foreach_flow_hash_bit;
1556#undef _
1557
1558 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1559
1560 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1561}
1562
1563
1564static void
1565vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1566{
1567 if (mp->is_ipv6 == 0)
1568 set_ip4_flow_hash (mp);
1569 else
1570 set_ip6_flow_hash (mp);
1571}
1572
1573static void
1574 vl_api_sw_interface_ip6nd_ra_config_t_handler
1575 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1576{
1577 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1578 vlib_main_t *vm = vlib_get_main ();
1579 int rv = 0;
1580 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1581 default_router;
1582
1583 is_no = mp->is_no == 1;
1584 suppress = mp->suppress == 1;
1585 managed = mp->managed == 1;
1586 other = mp->other == 1;
1587 ll_option = mp->ll_option == 1;
1588 send_unicast = mp->send_unicast == 1;
1589 cease = mp->cease == 1;
1590 default_router = mp->default_router == 1;
1591
1592 VALIDATE_SW_IF_INDEX (mp);
1593
1594 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1595 suppress, managed, other,
1596 ll_option, send_unicast, cease,
1597 default_router, ntohl (mp->lifetime),
1598 ntohl (mp->initial_count),
1599 ntohl (mp->initial_interval),
1600 ntohl (mp->max_interval),
1601 ntohl (mp->min_interval), is_no);
1602
1603 BAD_SW_IF_INDEX_LABEL;
1604
1605 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1606}
1607
1608static void
1609 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1610 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1611{
1612 vlib_main_t *vm = vlib_get_main ();
1613 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1614 int rv = 0;
1615 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1616
1617 VALIDATE_SW_IF_INDEX (mp);
1618
1619 is_no = mp->is_no == 1;
1620 use_default = mp->use_default == 1;
1621 no_advertise = mp->no_advertise == 1;
1622 off_link = mp->off_link == 1;
1623 no_autoconfig = mp->no_autoconfig == 1;
1624 no_onlink = mp->no_onlink == 1;
1625
1626 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1627 (ip6_address_t *) mp->address,
1628 mp->address_length, use_default,
1629 ntohl (mp->val_lifetime),
1630 ntohl (mp->pref_lifetime), no_advertise,
1631 off_link, no_autoconfig, no_onlink, is_no);
1632
1633 BAD_SW_IF_INDEX_LABEL;
1634 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1635}
1636
1637static void
Neale Ranns3f844d02017-02-18 00:03:54 -08001638send_ip6nd_proxy_details (unix_shared_memory_queue_t * q,
1639 u32 context,
1640 const ip46_address_t * addr, u32 sw_if_index)
1641{
1642 vl_api_ip6nd_proxy_details_t *mp;
1643
1644 mp = vl_msg_api_alloc (sizeof (*mp));
1645 memset (mp, 0, sizeof (*mp));
1646 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1647 mp->context = context;
1648 mp->sw_if_index = htonl (sw_if_index);
1649 memcpy (mp->address, addr, 16);
1650
1651 vl_msg_api_send_shmem (q, (u8 *) & mp);
1652}
1653
Neale Ranns3f844d02017-02-18 00:03:54 -08001654typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1655{
1656 u32 *indices;
1657} api_ip6nd_proxy_fib_table_walk_ctx_t;
1658
1659static int
1660api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1661{
1662 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1663
1664 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1665 {
1666 vec_add1 (ctx->indices, fei);
1667 }
1668
1669 return (1);
1670}
1671
1672static void
1673vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1674{
1675 ip6_main_t *im6 = &ip6_main;
1676 fib_table_t *fib_table;
1677 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1678 .indices = NULL,
1679 };
1680 fib_node_index_t *feip;
1681 fib_prefix_t pfx;
1682 unix_shared_memory_queue_t *q;
1683
1684 q = vl_api_client_index_to_input_queue (mp->client_index);
1685 if (q == 0)
1686 {
1687 return;
1688 }
1689
1690 /* *INDENT-OFF* */
1691 pool_foreach (fib_table, im6->fibs,
1692 ({
1693 fib_table_walk(fib_table->ft_index,
1694 FIB_PROTOCOL_IP6,
1695 api_ip6nd_proxy_fib_table_walk,
1696 &ctx);
1697 }));
1698 /* *INDENT-ON* */
1699
1700 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1701
1702 vec_foreach (feip, ctx.indices)
1703 {
1704 fib_entry_get_prefix (*feip, &pfx);
1705
1706 send_ip6nd_proxy_details (q,
1707 mp->context,
1708 &pfx.fp_addr,
1709 fib_entry_get_resolving_interface (*feip));
1710 }
1711
1712 vec_free (ctx.indices);
1713}
1714
1715static void
1716vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1717{
1718 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
1719 int rv = 0;
1720
1721 VALIDATE_SW_IF_INDEX (mp);
1722
1723 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index),
1724 (ip6_address_t *) mp->address, mp->is_del);
1725
1726 BAD_SW_IF_INDEX_LABEL;
1727 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1728}
1729
1730static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001731 vl_api_sw_interface_ip6_enable_disable_t_handler
1732 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1733{
1734 vlib_main_t *vm = vlib_get_main ();
1735 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1736 vnet_main_t *vnm = vnet_get_main ();
1737 int rv = 0;
1738 clib_error_t *error;
1739
1740 vnm->api_errno = 0;
1741
1742 VALIDATE_SW_IF_INDEX (mp);
1743
1744 error =
1745 (mp->enable == 1) ? enable_ip6_interface (vm,
1746 ntohl (mp->sw_if_index)) :
1747 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1748
1749 if (error)
1750 {
1751 clib_error_report (error);
1752 rv = VNET_API_ERROR_UNSPECIFIED;
1753 }
1754 else
1755 {
1756 rv = vnm->api_errno;
1757 }
1758
1759 BAD_SW_IF_INDEX_LABEL;
1760
1761 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1762}
1763
1764static void
1765 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1766 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1767{
1768 vlib_main_t *vm = vlib_get_main ();
1769 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1770 int rv = 0;
1771 clib_error_t *error;
1772 vnet_main_t *vnm = vnet_get_main ();
1773
1774 vnm->api_errno = 0;
1775
1776 VALIDATE_SW_IF_INDEX (mp);
1777
1778 error = set_ip6_link_local_address (vm,
1779 ntohl (mp->sw_if_index),
Neale Ranns75152282017-01-09 01:00:45 -08001780 (ip6_address_t *) mp->address);
Dave Barachb5e8a772016-12-06 12:04:42 -05001781 if (error)
1782 {
1783 clib_error_report (error);
1784 rv = VNET_API_ERROR_UNSPECIFIED;
1785 }
1786 else
1787 {
1788 rv = vnm->api_errno;
1789 }
1790
1791 BAD_SW_IF_INDEX_LABEL;
1792
1793 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1794}
1795
Neale Ranns32e1c012016-11-22 17:07:28 +00001796void
1797vl_mfib_signal_send_one (unix_shared_memory_queue_t * q,
1798 u32 context, const mfib_signal_t * mfs)
1799{
1800 vl_api_mfib_signal_details_t *mp;
1801 mfib_prefix_t prefix;
1802 mfib_table_t *mfib;
1803 mfib_itf_t *mfi;
1804
1805 mp = vl_msg_api_alloc (sizeof (*mp));
1806
1807 memset (mp, 0, sizeof (*mp));
1808 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1809 mp->context = context;
1810
1811 mfi = mfib_itf_get (mfs->mfs_itf);
1812 mfib_entry_get_prefix (mfs->mfs_entry, &prefix);
1813 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
1814 prefix.fp_proto);
1815 mp->table_id = ntohl (mfib->mft_table_id);
1816 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1817
1818 if (FIB_PROTOCOL_IP4 == prefix.fp_proto)
1819 {
1820 mp->grp_address_len = ntohs (prefix.fp_len);
1821
1822 memcpy (mp->grp_address, &prefix.fp_grp_addr.ip4, 4);
1823 if (prefix.fp_len > 32)
1824 {
1825 memcpy (mp->src_address, &prefix.fp_src_addr.ip4, 4);
1826 }
1827 }
1828 else
1829 {
1830 mp->grp_address_len = ntohs (prefix.fp_len);
1831
1832 ASSERT (0);
1833 }
1834
1835 if (0 != mfs->mfs_buffer_len)
1836 {
1837 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1838
1839 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1840 }
1841 else
1842 {
1843 mp->ip_packet_len = 0;
1844 }
1845
1846 vl_msg_api_send_shmem (q, (u8 *) & mp);
1847}
1848
1849static void
1850vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1851{
1852 unix_shared_memory_queue_t *q;
1853
1854 q = vl_api_client_index_to_input_queue (mp->client_index);
1855 if (q == 0)
1856 {
1857 return;
1858 }
1859
1860 while (q->cursize < q->maxsize && mfib_signal_send_one (q, mp->context))
1861 ;
1862}
Dave Barachb5e8a772016-12-06 12:04:42 -05001863
Florin Coras595992c2017-11-06 17:17:08 -08001864static void
1865 vl_api_ip_container_proxy_add_del_t_handler
1866 (vl_api_ip_container_proxy_add_del_t * mp)
1867{
1868 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1869 vnet_ip_container_proxy_args_t args;
1870 int rv = 0;
1871 clib_error_t *error;
1872
1873 memset (&args, 0, sizeof (args));
1874 ip_set (&args.prefix.fp_addr, mp->ip, mp->is_ip4);
1875 args.prefix.fp_len = mp->plen ? mp->plen : (mp->is_ip4 ? 32 : 128);
1876 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1877 args.is_add = mp->is_add;
1878 if ((error = vnet_ip_container_proxy_add_del (&args)))
1879 {
1880 rv = clib_error_get_code (error);
1881 clib_error_report (error);
1882 }
1883
1884 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1885}
1886
Dave Barachb5e8a772016-12-06 12:04:42 -05001887#define vl_msg_name_crc_list
1888#include <vnet/ip/ip.api.h>
1889#undef vl_msg_name_crc_list
1890
1891static void
1892setup_message_id_table (api_main_t * am)
1893{
1894#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1895 foreach_vl_msg_name_crc_ip;
1896#undef _
1897}
1898
1899static clib_error_t *
1900ip_api_hookup (vlib_main_t * vm)
1901{
1902 api_main_t *am = &api_main;
1903
1904#define _(N,n) \
1905 vl_msg_api_set_handlers(VL_API_##N, #n, \
1906 vl_api_##n##_t_handler, \
1907 vl_noop_handler, \
1908 vl_api_##n##_t_endian, \
1909 vl_api_##n##_t_print, \
1910 sizeof(vl_api_##n##_t), 1);
1911 foreach_ip_api_msg;
1912#undef _
1913
1914 /*
1915 * Set up the (msg_name, crc, message-id) table
1916 */
1917 setup_message_id_table (am);
1918
1919 return 0;
1920}
1921
1922VLIB_API_INIT_FUNCTION (ip_api_hookup);
1923
1924/*
1925 * fd.io coding-style-patch-verification: ON
1926 *
1927 * Local Variables:
1928 * eval: (c-set-style "gnu")
1929 * End:
1930 */