blob: 0fdb167312ee587a0339bdc880ac211f60647d31 [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));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700467 mp->_vl_msg_id = ntohs (VL_API_IP_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800468 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));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700566 mp->_vl_msg_id = ntohs (VL_API_IP6_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800567 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,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001286 dpo_proto_t nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001287 u32 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001288 fib_rpf_id_t rpf_id,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001289 u32 next_hop_sw_if_index, u32 itf_flags, u32 bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +00001290{
1291 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1292
1293 fib_route_path_t path = {
1294 .frp_sw_if_index = next_hop_sw_if_index,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001295 .frp_proto = nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001296 };
1297
1298 if (is_local)
1299 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1300
Neale Rannsd792d9c2017-10-21 10:53:20 -07001301 if (DPO_PROTO_BIER == nh_proto)
1302 {
1303 path.frp_bier_imp = bier_imp;
1304 path.frp_flags = FIB_ROUTE_PATH_BIER_IMP;
1305 }
1306 else if (!is_local && ~0 == next_hop_sw_if_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001307 {
1308 mfib_table_entry_update (fib_index, prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001309 MFIB_SOURCE_API, rpf_id, entry_flags);
Neale Rannsd792d9c2017-10-21 10:53:20 -07001310 goto done;
1311 }
1312
1313 if (is_add)
1314 {
1315 mfib_table_entry_path_update (fib_index, prefix,
1316 MFIB_SOURCE_API, &path, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001317 }
1318 else
1319 {
Neale Rannsd792d9c2017-10-21 10:53:20 -07001320 mfib_table_entry_path_remove (fib_index, prefix,
1321 MFIB_SOURCE_API, &path);
Neale Ranns32e1c012016-11-22 17:07:28 +00001322 }
1323
Neale Rannsd792d9c2017-10-21 10:53:20 -07001324done:
Neale Ranns32e1c012016-11-22 17:07:28 +00001325 stats_dsunlock ();
1326 return (0);
1327}
1328
1329static int
1330api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1331{
1332 fib_protocol_t fproto;
Neale Rannsd792d9c2017-10-21 10:53:20 -07001333 dpo_proto_t nh_proto;
Neale Ranns32e1c012016-11-22 17:07:28 +00001334 u32 fib_index;
1335 int rv;
1336
Neale Rannsd792d9c2017-10-21 10:53:20 -07001337 nh_proto = mp->next_hop_afi;
Neale Ranns32e1c012016-11-22 17:07:28 +00001338 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1339 rv = add_del_mroute_check (fproto,
1340 mp->table_id,
1341 mp->next_hop_sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -07001342 mp->is_local, &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001343
1344 if (0 != rv)
1345 return (rv);
1346
1347 mfib_prefix_t pfx = {
1348 .fp_len = ntohs (mp->grp_address_length),
1349 .fp_proto = fproto,
1350 };
1351
1352 if (FIB_PROTOCOL_IP4 == fproto)
1353 {
1354 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1355 sizeof (pfx.fp_grp_addr.ip4));
1356 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1357 sizeof (pfx.fp_src_addr.ip4));
1358 }
1359 else
1360 {
1361 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1362 sizeof (pfx.fp_grp_addr.ip6));
1363 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1364 sizeof (pfx.fp_src_addr.ip6));
1365 }
1366
1367 return (mroute_add_del_handler (mp->is_add,
1368 mp->is_local,
1369 fib_index, &pfx,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001370 nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001371 ntohl (mp->entry_flags),
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001372 ntohl (mp->rpf_id),
Neale Ranns32e1c012016-11-22 17:07:28 +00001373 ntohl (mp->next_hop_sw_if_index),
Neale Rannsd792d9c2017-10-21 10:53:20 -07001374 ntohl (mp->itf_flags),
1375 ntohl (mp->bier_imp)));
Neale Ranns32e1c012016-11-22 17:07:28 +00001376}
1377
1378void
1379vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1380{
1381 vl_api_ip_mroute_add_del_reply_t *rmp;
1382 int rv;
1383 vnet_main_t *vnm = vnet_get_main ();
1384
1385 vnm->api_errno = 0;
1386
1387 rv = api_mroute_add_del_t_handler (mp);
1388
1389 rv = (rv == 0) ? vnm->api_errno : rv;
1390
1391 REPLY_MACRO (VL_API_IP_MROUTE_ADD_DEL_REPLY);
1392}
1393
Dave Barachb5e8a772016-12-06 12:04:42 -05001394static void
1395send_ip_details (vpe_api_main_t * am,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001396 unix_shared_memory_queue_t * q, u32 sw_if_index,
1397 u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001398{
1399 vl_api_ip_details_t *mp;
1400
1401 mp = vl_msg_api_alloc (sizeof (*mp));
1402 memset (mp, 0, sizeof (*mp));
1403 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1404
1405 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001406 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001407 mp->context = context;
1408
1409 vl_msg_api_send_shmem (q, (u8 *) & mp);
1410}
1411
1412static void
1413send_ip_address_details (vpe_api_main_t * am,
1414 unix_shared_memory_queue_t * q,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001415 u8 * ip, u16 prefix_length,
1416 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001417{
1418 vl_api_ip_address_details_t *mp;
1419
1420 mp = vl_msg_api_alloc (sizeof (*mp));
1421 memset (mp, 0, sizeof (*mp));
1422 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1423
1424 if (is_ipv6)
1425 {
1426 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1427 }
1428 else
1429 {
1430 u32 *tp = (u32 *) mp->ip;
1431 *tp = *(u32 *) ip;
1432 }
1433 mp->prefix_length = prefix_length;
1434 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001435 mp->sw_if_index = htonl (sw_if_index);
1436 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001437
1438 vl_msg_api_send_shmem (q, (u8 *) & mp);
1439}
1440
1441static void
1442vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1443{
1444 vpe_api_main_t *am = &vpe_api_main;
1445 unix_shared_memory_queue_t *q;
1446 ip6_address_t *r6;
1447 ip4_address_t *r4;
1448 ip6_main_t *im6 = &ip6_main;
1449 ip4_main_t *im4 = &ip4_main;
1450 ip_lookup_main_t *lm6 = &im6->lookup_main;
1451 ip_lookup_main_t *lm4 = &im4->lookup_main;
1452 ip_interface_address_t *ia = 0;
1453 u32 sw_if_index = ~0;
1454 int rv __attribute__ ((unused)) = 0;
1455
1456 VALIDATE_SW_IF_INDEX (mp);
1457
1458 sw_if_index = ntohl (mp->sw_if_index);
1459
1460 q = vl_api_client_index_to_input_queue (mp->client_index);
1461 if (q == 0)
1462 return;
1463
Dave Barachb5e8a772016-12-06 12:04:42 -05001464 if (mp->is_ipv6)
1465 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001466 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001467 foreach_ip_interface_address (lm6, ia, sw_if_index,
1468 1 /* honor unnumbered */,
1469 ({
1470 r6 = ip_interface_address_get_address (lm6, ia);
1471 u16 prefix_length = ia->address_length;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001472 send_ip_address_details(am, q, (u8*)r6, prefix_length,
1473 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001474 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001475 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001476 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001477 else
1478 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001479 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001480 foreach_ip_interface_address (lm4, ia, sw_if_index,
1481 1 /* honor unnumbered */,
1482 ({
1483 r4 = ip_interface_address_get_address (lm4, ia);
1484 u16 prefix_length = ia->address_length;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001485 send_ip_address_details(am, q, (u8*)r4, prefix_length,
1486 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001487 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001488 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001489 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001490 BAD_SW_IF_INDEX_LABEL;
1491}
1492
1493static void
1494vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1495{
1496 vpe_api_main_t *am = &vpe_api_main;
1497 vnet_main_t *vnm = vnet_get_main ();
1498 vlib_main_t *vm = vlib_get_main ();
1499 vnet_interface_main_t *im = &vnm->interface_main;
1500 unix_shared_memory_queue_t *q;
1501 vnet_sw_interface_t *si, *sorted_sis;
1502 u32 sw_if_index = ~0;
1503
1504 q = vl_api_client_index_to_input_queue (mp->client_index);
1505 if (q == 0)
1506 {
1507 return;
1508 }
1509
1510 /* Gather interfaces. */
1511 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1512 _vec_len (sorted_sis) = 0;
1513 /* *INDENT-OFF* */
1514 pool_foreach (si, im->sw_interfaces,
1515 ({
1516 vec_add1 (sorted_sis, si[0]);
1517 }));
1518 /* *INDENT-ON* */
1519
1520 vec_foreach (si, sorted_sis)
1521 {
1522 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1523 {
1524 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1525 {
1526 continue;
1527 }
1528 sw_if_index = si->sw_if_index;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001529 send_ip_details (am, q, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001530 }
1531 }
1532}
1533
1534static void
1535set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1536{
1537 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001538 int rv;
1539 u32 table_id;
1540 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001541
Neale Ranns227038a2017-04-21 01:07:59 -07001542 table_id = ntohl (mp->vrf_id);
1543
1544#define _(a,b) if (mp->a) flow_hash_config |= b;
1545 foreach_flow_hash_bit;
1546#undef _
1547
1548 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001549
1550 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1551}
1552
1553static void
1554set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1555{
1556 vl_api_set_ip_flow_hash_reply_t *rmp;
1557 int rv;
1558 u32 table_id;
1559 flow_hash_config_t flow_hash_config = 0;
1560
1561 table_id = ntohl (mp->vrf_id);
1562
1563#define _(a,b) if (mp->a) flow_hash_config |= b;
1564 foreach_flow_hash_bit;
1565#undef _
1566
1567 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1568
1569 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1570}
1571
1572
1573static void
1574vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1575{
1576 if (mp->is_ipv6 == 0)
1577 set_ip4_flow_hash (mp);
1578 else
1579 set_ip6_flow_hash (mp);
1580}
1581
1582static void
1583 vl_api_sw_interface_ip6nd_ra_config_t_handler
1584 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1585{
1586 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1587 vlib_main_t *vm = vlib_get_main ();
1588 int rv = 0;
1589 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1590 default_router;
1591
1592 is_no = mp->is_no == 1;
1593 suppress = mp->suppress == 1;
1594 managed = mp->managed == 1;
1595 other = mp->other == 1;
1596 ll_option = mp->ll_option == 1;
1597 send_unicast = mp->send_unicast == 1;
1598 cease = mp->cease == 1;
1599 default_router = mp->default_router == 1;
1600
1601 VALIDATE_SW_IF_INDEX (mp);
1602
1603 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1604 suppress, managed, other,
1605 ll_option, send_unicast, cease,
1606 default_router, ntohl (mp->lifetime),
1607 ntohl (mp->initial_count),
1608 ntohl (mp->initial_interval),
1609 ntohl (mp->max_interval),
1610 ntohl (mp->min_interval), is_no);
1611
1612 BAD_SW_IF_INDEX_LABEL;
1613
1614 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1615}
1616
1617static void
1618 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1619 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1620{
1621 vlib_main_t *vm = vlib_get_main ();
1622 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1623 int rv = 0;
1624 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1625
1626 VALIDATE_SW_IF_INDEX (mp);
1627
1628 is_no = mp->is_no == 1;
1629 use_default = mp->use_default == 1;
1630 no_advertise = mp->no_advertise == 1;
1631 off_link = mp->off_link == 1;
1632 no_autoconfig = mp->no_autoconfig == 1;
1633 no_onlink = mp->no_onlink == 1;
1634
1635 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1636 (ip6_address_t *) mp->address,
1637 mp->address_length, use_default,
1638 ntohl (mp->val_lifetime),
1639 ntohl (mp->pref_lifetime), no_advertise,
1640 off_link, no_autoconfig, no_onlink, is_no);
1641
1642 BAD_SW_IF_INDEX_LABEL;
1643 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1644}
1645
1646static void
Neale Ranns3f844d02017-02-18 00:03:54 -08001647send_ip6nd_proxy_details (unix_shared_memory_queue_t * q,
1648 u32 context,
1649 const ip46_address_t * addr, u32 sw_if_index)
1650{
1651 vl_api_ip6nd_proxy_details_t *mp;
1652
1653 mp = vl_msg_api_alloc (sizeof (*mp));
1654 memset (mp, 0, sizeof (*mp));
1655 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1656 mp->context = context;
1657 mp->sw_if_index = htonl (sw_if_index);
1658 memcpy (mp->address, addr, 16);
1659
1660 vl_msg_api_send_shmem (q, (u8 *) & mp);
1661}
1662
Neale Ranns3f844d02017-02-18 00:03:54 -08001663typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1664{
1665 u32 *indices;
1666} api_ip6nd_proxy_fib_table_walk_ctx_t;
1667
1668static int
1669api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1670{
1671 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1672
1673 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1674 {
1675 vec_add1 (ctx->indices, fei);
1676 }
1677
1678 return (1);
1679}
1680
1681static void
1682vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1683{
1684 ip6_main_t *im6 = &ip6_main;
1685 fib_table_t *fib_table;
1686 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1687 .indices = NULL,
1688 };
1689 fib_node_index_t *feip;
1690 fib_prefix_t pfx;
1691 unix_shared_memory_queue_t *q;
1692
1693 q = vl_api_client_index_to_input_queue (mp->client_index);
1694 if (q == 0)
1695 {
1696 return;
1697 }
1698
1699 /* *INDENT-OFF* */
1700 pool_foreach (fib_table, im6->fibs,
1701 ({
1702 fib_table_walk(fib_table->ft_index,
1703 FIB_PROTOCOL_IP6,
1704 api_ip6nd_proxy_fib_table_walk,
1705 &ctx);
1706 }));
1707 /* *INDENT-ON* */
1708
1709 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1710
1711 vec_foreach (feip, ctx.indices)
1712 {
1713 fib_entry_get_prefix (*feip, &pfx);
1714
1715 send_ip6nd_proxy_details (q,
1716 mp->context,
1717 &pfx.fp_addr,
1718 fib_entry_get_resolving_interface (*feip));
1719 }
1720
1721 vec_free (ctx.indices);
1722}
1723
1724static void
1725vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1726{
1727 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
1728 int rv = 0;
1729
1730 VALIDATE_SW_IF_INDEX (mp);
1731
1732 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index),
1733 (ip6_address_t *) mp->address, mp->is_del);
1734
1735 BAD_SW_IF_INDEX_LABEL;
1736 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1737}
1738
1739static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001740 vl_api_sw_interface_ip6_enable_disable_t_handler
1741 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1742{
1743 vlib_main_t *vm = vlib_get_main ();
1744 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1745 vnet_main_t *vnm = vnet_get_main ();
1746 int rv = 0;
1747 clib_error_t *error;
1748
1749 vnm->api_errno = 0;
1750
1751 VALIDATE_SW_IF_INDEX (mp);
1752
1753 error =
1754 (mp->enable == 1) ? enable_ip6_interface (vm,
1755 ntohl (mp->sw_if_index)) :
1756 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1757
1758 if (error)
1759 {
1760 clib_error_report (error);
1761 rv = VNET_API_ERROR_UNSPECIFIED;
1762 }
1763 else
1764 {
1765 rv = vnm->api_errno;
1766 }
1767
1768 BAD_SW_IF_INDEX_LABEL;
1769
1770 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1771}
1772
1773static void
1774 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1775 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1776{
1777 vlib_main_t *vm = vlib_get_main ();
1778 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1779 int rv = 0;
1780 clib_error_t *error;
1781 vnet_main_t *vnm = vnet_get_main ();
1782
1783 vnm->api_errno = 0;
1784
1785 VALIDATE_SW_IF_INDEX (mp);
1786
1787 error = set_ip6_link_local_address (vm,
1788 ntohl (mp->sw_if_index),
Neale Ranns75152282017-01-09 01:00:45 -08001789 (ip6_address_t *) mp->address);
Dave Barachb5e8a772016-12-06 12:04:42 -05001790 if (error)
1791 {
1792 clib_error_report (error);
1793 rv = VNET_API_ERROR_UNSPECIFIED;
1794 }
1795 else
1796 {
1797 rv = vnm->api_errno;
1798 }
1799
1800 BAD_SW_IF_INDEX_LABEL;
1801
1802 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1803}
1804
Neale Ranns32e1c012016-11-22 17:07:28 +00001805void
1806vl_mfib_signal_send_one (unix_shared_memory_queue_t * q,
1807 u32 context, const mfib_signal_t * mfs)
1808{
1809 vl_api_mfib_signal_details_t *mp;
1810 mfib_prefix_t prefix;
1811 mfib_table_t *mfib;
1812 mfib_itf_t *mfi;
1813
1814 mp = vl_msg_api_alloc (sizeof (*mp));
1815
1816 memset (mp, 0, sizeof (*mp));
1817 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1818 mp->context = context;
1819
1820 mfi = mfib_itf_get (mfs->mfs_itf);
1821 mfib_entry_get_prefix (mfs->mfs_entry, &prefix);
1822 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
1823 prefix.fp_proto);
1824 mp->table_id = ntohl (mfib->mft_table_id);
1825 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1826
1827 if (FIB_PROTOCOL_IP4 == prefix.fp_proto)
1828 {
1829 mp->grp_address_len = ntohs (prefix.fp_len);
1830
1831 memcpy (mp->grp_address, &prefix.fp_grp_addr.ip4, 4);
1832 if (prefix.fp_len > 32)
1833 {
1834 memcpy (mp->src_address, &prefix.fp_src_addr.ip4, 4);
1835 }
1836 }
1837 else
1838 {
1839 mp->grp_address_len = ntohs (prefix.fp_len);
1840
1841 ASSERT (0);
1842 }
1843
1844 if (0 != mfs->mfs_buffer_len)
1845 {
1846 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1847
1848 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1849 }
1850 else
1851 {
1852 mp->ip_packet_len = 0;
1853 }
1854
1855 vl_msg_api_send_shmem (q, (u8 *) & mp);
1856}
1857
1858static void
1859vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1860{
1861 unix_shared_memory_queue_t *q;
1862
1863 q = vl_api_client_index_to_input_queue (mp->client_index);
1864 if (q == 0)
1865 {
1866 return;
1867 }
1868
1869 while (q->cursize < q->maxsize && mfib_signal_send_one (q, mp->context))
1870 ;
1871}
Dave Barachb5e8a772016-12-06 12:04:42 -05001872
Florin Coras595992c2017-11-06 17:17:08 -08001873static void
1874 vl_api_ip_container_proxy_add_del_t_handler
1875 (vl_api_ip_container_proxy_add_del_t * mp)
1876{
1877 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1878 vnet_ip_container_proxy_args_t args;
1879 int rv = 0;
1880 clib_error_t *error;
1881
1882 memset (&args, 0, sizeof (args));
1883 ip_set (&args.prefix.fp_addr, mp->ip, mp->is_ip4);
1884 args.prefix.fp_len = mp->plen ? mp->plen : (mp->is_ip4 ? 32 : 128);
1885 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1886 args.is_add = mp->is_add;
1887 if ((error = vnet_ip_container_proxy_add_del (&args)))
1888 {
1889 rv = clib_error_get_code (error);
1890 clib_error_report (error);
1891 }
1892
1893 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1894}
1895
Dave Barachb5e8a772016-12-06 12:04:42 -05001896#define vl_msg_name_crc_list
1897#include <vnet/ip/ip.api.h>
1898#undef vl_msg_name_crc_list
1899
1900static void
1901setup_message_id_table (api_main_t * am)
1902{
1903#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1904 foreach_vl_msg_name_crc_ip;
1905#undef _
1906}
1907
1908static clib_error_t *
1909ip_api_hookup (vlib_main_t * vm)
1910{
1911 api_main_t *am = &api_main;
1912
1913#define _(N,n) \
1914 vl_msg_api_set_handlers(VL_API_##N, #n, \
1915 vl_api_##n##_t_handler, \
1916 vl_noop_handler, \
1917 vl_api_##n##_t_endian, \
1918 vl_api_##n##_t_print, \
1919 sizeof(vl_api_##n##_t), 1);
1920 foreach_ip_api_msg;
1921#undef _
1922
1923 /*
1924 * Set up the (msg_name, crc, message-id) table
1925 */
1926 setup_message_id_table (am);
1927
1928 return 0;
1929}
1930
1931VLIB_API_INIT_FUNCTION (ip_api_hookup);
1932
1933/*
1934 * fd.io coding-style-patch-verification: ON
1935 *
1936 * Local Variables:
1937 * eval: (c-set-style "gnu")
1938 * End:
1939 */