API refactoring : policer
[vpp.git] / src / vpp / api / api.c
1 /*
2  *------------------------------------------------------------------
3  * api.c - message handler registration
4  *
5  * Copyright (c) 2010-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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/mman.h>
25 #include <sys/stat.h>
26 #include <netinet/in.h>
27 #include <signal.h>
28 #include <pthread.h>
29 #include <unistd.h>
30 #include <time.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <pwd.h>
34 #include <grp.h>
35
36 #include <vppinfra/clib.h>
37 #include <vppinfra/vec.h>
38 #include <vppinfra/hash.h>
39 #include <vppinfra/bitmap.h>
40 #include <vppinfra/fifo.h>
41 #include <vppinfra/time.h>
42 #include <vppinfra/mheap.h>
43 #include <vppinfra/heap.h>
44 #include <vppinfra/pool.h>
45 #include <vppinfra/format.h>
46 #include <vppinfra/error.h>
47
48 #include <vnet/api_errno.h>
49 #include <vnet/vnet.h>
50 #include <vnet/l2/l2_input.h>
51 #include <vnet/l2/l2_bd.h>
52 #include <vnet/ip/ip.h>
53 #include <vnet/ip/ip6.h>
54 #include <vnet/ip/ip6_neighbor.h>
55 #if WITH_LIBSSL > 0
56 #include <vnet/sr/sr.h>
57 #endif
58 #include <vlib/vlib.h>
59 #include <vlib/unix/unix.h>
60 #include <vlibapi/api.h>
61 #include <vlibmemory/api.h>
62 #include <vnet/classify/input_acl.h>
63 #include <vnet/l2/l2_classify.h>
64 #include <vnet/vxlan/vxlan.h>
65 #include <vnet/vxlan-gpe/vxlan_gpe.h>
66 #include <vnet/map/map.h>
67 #include <vnet/ip/ip6_hop_by_hop.h>
68 #include <vnet/ip/ip_source_and_port_range_check.h>
69 #include <vnet/ip/punt.h>
70 #include <vnet/feature/feature.h>
71
72 #undef BIHASH_TYPE
73 #undef __included_bihash_template_h__
74 #include <vnet/l2/l2_fib.h>
75
76 #include <vpp/stats/stats.h>
77 #include <vpp/oam/oam.h>
78
79 #include <vnet/ethernet/ethernet.h>
80 #include <vnet/ethernet/arp_packet.h>
81 #include <vnet/interface.h>
82 #include <vnet/l2/l2_fib.h>
83 #include <vnet/l2/l2_bd.h>
84 #include <vpp/api/vpe_msg_enum.h>
85 #include <vnet/span/span.h>
86 #include <vnet/fib/ip6_fib.h>
87 #include <vnet/fib/ip4_fib.h>
88 #include <vnet/fib/fib_api.h>
89 #include <vnet/dpo/drop_dpo.h>
90 #include <vnet/dpo/receive_dpo.h>
91 #include <vnet/dpo/lookup_dpo.h>
92 #include <vnet/dpo/classify_dpo.h>
93 #include <vnet/dpo/ip_null_dpo.h>
94 #define vl_typedefs             /* define message structures */
95 #include <vpp/api/vpe_all_api_h.h>
96 #undef vl_typedefs
97 #define vl_endianfun            /* define message structures */
98 #include <vpp/api/vpe_all_api_h.h>
99 #undef vl_endianfun
100 /* instantiate all the print functions we know about */
101 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
102 #define vl_printfun
103 #include <vpp/api/vpe_all_api_h.h>
104 #undef vl_printfun
105 #include <vlibapi/api_helper_macros.h>
106 #define foreach_vpe_api_msg                                             \
107 _(WANT_OAM_EVENTS, want_oam_events)                                     \
108 _(OAM_ADD_DEL, oam_add_del)                                             \
109 _(IS_ADDRESS_REACHABLE, is_address_reachable)                           \
110 _(SW_INTERFACE_SET_MPLS_ENABLE, sw_interface_set_mpls_enable)           \
111 _(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath)                       \
112 _(SW_INTERFACE_SET_VXLAN_BYPASS, sw_interface_set_vxlan_bypass)         \
113 _(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect)           \
114 _(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge)               \
115 _(CREATE_VLAN_SUBIF, create_vlan_subif)                                 \
116 _(CREATE_SUBIF, create_subif)                                           \
117 _(PROXY_ARP_ADD_DEL, proxy_arp_add_del)                                 \
118 _(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable)       \
119 _(VNET_GET_SUMMARY_STATS, vnet_get_summary_stats)                       \
120 _(RESET_FIB, reset_fib)                                                 \
121 _(CREATE_LOOPBACK, create_loopback)                                     \
122 _(CONTROL_PING, control_ping)                                           \
123 _(CLI_REQUEST, cli_request)                                             \
124 _(CLI_INBAND, cli_inband)                                               \
125 _(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit)                       \
126 _(L2_PATCH_ADD_DEL, l2_patch_add_del)                                   \
127 _(CLASSIFY_SET_INTERFACE_IP_TABLE, classify_set_interface_ip_table)     \
128 _(CLASSIFY_SET_INTERFACE_L2_TABLES, classify_set_interface_l2_tables)   \
129 _(GET_NODE_INDEX, get_node_index)                                       \
130 _(ADD_NODE_NEXT, add_node_next)                                         \
131 _(VXLAN_ADD_DEL_TUNNEL, vxlan_add_del_tunnel)                           \
132 _(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump)                                 \
133 _(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter)                     \
134 _(SHOW_VERSION, show_version)                                           \
135 _(VXLAN_GPE_ADD_DEL_TUNNEL, vxlan_gpe_add_del_tunnel)                   \
136 _(VXLAN_GPE_TUNNEL_DUMP, vxlan_gpe_tunnel_dump)                         \
137 _(INTERFACE_NAME_RENUMBER, interface_name_renumber)                     \
138 _(WANT_IP4_ARP_EVENTS, want_ip4_arp_events)                             \
139 _(WANT_IP6_ND_EVENTS, want_ip6_nd_events)                               \
140 _(INPUT_ACL_SET_INTERFACE, input_acl_set_interface)                     \
141 _(DELETE_LOOPBACK, delete_loopback)                                     \
142 _(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del)                                 \
143 _(GET_NODE_GRAPH, get_node_graph)                                       \
144 _(IOAM_ENABLE, ioam_enable)                                             \
145 _(IOAM_DISABLE, ioam_disable)                                           \
146 _(GET_NEXT_INDEX, get_next_index)                                       \
147 _(PG_CREATE_INTERFACE, pg_create_interface)                             \
148 _(PG_CAPTURE, pg_capture)                                               \
149 _(PG_ENABLE_DISABLE, pg_enable_disable)                                 \
150 _(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL,                               \
151   ip_source_and_port_range_check_add_del)                               \
152 _(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL,                     \
153   ip_source_and_port_range_check_interface_add_del)                     \
154 _(DELETE_SUBIF, delete_subif)                                           \
155 _(PUNT, punt)                                                           \
156 _(FEATURE_ENABLE_DISABLE, feature_enable_disable)
157
158 #define QUOTE_(x) #x
159 #define QUOTE(x) QUOTE_(x)
160 typedef enum
161 {
162   RESOLVE_IP4_ADD_DEL_ROUTE = 1,
163   RESOLVE_IP6_ADD_DEL_ROUTE,
164 } resolve_t;
165
166 static vlib_node_registration_t vpe_resolver_process_node;
167 vpe_api_main_t vpe_api_main;
168
169 static int arp_change_delete_callback (u32 pool_index, u8 * notused);
170 static int nd_change_delete_callback (u32 pool_index, u8 * notused);
171
172 /* Clean up all registrations belonging to the indicated client */
173 int
174 vl_api_memclnt_delete_callback (u32 client_index)
175 {
176   vpe_api_main_t *vam = &vpe_api_main;
177   vpe_client_registration_t *rp;
178   uword *p;
179   int stats_memclnt_delete_callback (u32 client_index);
180
181   stats_memclnt_delete_callback (client_index);
182
183 #define _(a)                                                    \
184     p = hash_get (vam->a##_registration_hash, client_index);    \
185     if (p) {                                                    \
186         rp = pool_elt_at_index (vam->a##_registrations, p[0]);  \
187         pool_put (vam->a##_registrations, rp);                  \
188         hash_unset (vam->a##_registration_hash, client_index);  \
189     }
190   foreach_registration_hash;
191 #undef _
192   return 0;
193 }
194
195 pub_sub_handler (oam_events, OAM_EVENTS);
196
197 #define RESOLUTION_EVENT 1
198 #define RESOLUTION_PENDING_EVENT 2
199 #define IP4_ARP_EVENT 3
200 #define IP6_ND_EVENT 4
201
202 int ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp);
203
204 int ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp);
205
206 void
207 handle_ip4_arp_event (u32 pool_index)
208 {
209   vpe_api_main_t *vam = &vpe_api_main;
210   vnet_main_t *vnm = vam->vnet_main;
211   vlib_main_t *vm = vam->vlib_main;
212   vl_api_ip4_arp_event_t *event;
213   vl_api_ip4_arp_event_t *mp;
214   unix_shared_memory_queue_t *q;
215
216   /* Client can cancel, die, etc. */
217   if (pool_is_free_index (vam->arp_events, pool_index))
218     return;
219
220   event = pool_elt_at_index (vam->arp_events, pool_index);
221
222   q = vl_api_client_index_to_input_queue (event->client_index);
223   if (!q)
224     {
225       (void) vnet_add_del_ip4_arp_change_event
226         (vnm, arp_change_delete_callback,
227          event->pid, &event->address,
228          vpe_resolver_process_node.index, IP4_ARP_EVENT,
229          ~0 /* pool index, notused */ , 0 /* is_add */ );
230       return;
231     }
232
233   if (q->cursize < q->maxsize)
234     {
235       mp = vl_msg_api_alloc (sizeof (*mp));
236       clib_memcpy (mp, event, sizeof (*mp));
237       vl_msg_api_send_shmem (q, (u8 *) & mp);
238     }
239   else
240     {
241       static f64 last_time;
242       /*
243        * Throttle syslog msgs.
244        * It's pretty tempting to just revoke the registration...
245        */
246       if (vlib_time_now (vm) > last_time + 10.0)
247         {
248           clib_warning ("arp event for %U to pid %d: queue stuffed!",
249                         format_ip4_address, &event->address, event->pid);
250           last_time = vlib_time_now (vm);
251         }
252     }
253 }
254
255 void
256 handle_ip6_nd_event (u32 pool_index)
257 {
258   vpe_api_main_t *vam = &vpe_api_main;
259   vnet_main_t *vnm = vam->vnet_main;
260   vlib_main_t *vm = vam->vlib_main;
261   vl_api_ip6_nd_event_t *event;
262   vl_api_ip6_nd_event_t *mp;
263   unix_shared_memory_queue_t *q;
264
265   /* Client can cancel, die, etc. */
266   if (pool_is_free_index (vam->nd_events, pool_index))
267     return;
268
269   event = pool_elt_at_index (vam->nd_events, pool_index);
270
271   q = vl_api_client_index_to_input_queue (event->client_index);
272   if (!q)
273     {
274       (void) vnet_add_del_ip6_nd_change_event
275         (vnm, nd_change_delete_callback,
276          event->pid, &event->address,
277          vpe_resolver_process_node.index, IP6_ND_EVENT,
278          ~0 /* pool index, notused */ , 0 /* is_add */ );
279       return;
280     }
281
282   if (q->cursize < q->maxsize)
283     {
284       mp = vl_msg_api_alloc (sizeof (*mp));
285       clib_memcpy (mp, event, sizeof (*mp));
286       vl_msg_api_send_shmem (q, (u8 *) & mp);
287     }
288   else
289     {
290       static f64 last_time;
291       /*
292        * Throttle syslog msgs.
293        * It's pretty tempting to just revoke the registration...
294        */
295       if (vlib_time_now (vm) > last_time + 10.0)
296         {
297           clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
298                         format_ip6_address, &event->address, event->pid);
299           last_time = vlib_time_now (vm);
300         }
301     }
302 }
303
304 static uword
305 resolver_process (vlib_main_t * vm,
306                   vlib_node_runtime_t * rt, vlib_frame_t * f)
307 {
308   uword event_type;
309   uword *event_data = 0;
310   f64 timeout = 100.0;
311   int i;
312
313   while (1)
314     {
315       vlib_process_wait_for_event_or_clock (vm, timeout);
316
317       event_type = vlib_process_get_events (vm, &event_data);
318
319       switch (event_type)
320         {
321         case RESOLUTION_PENDING_EVENT:
322           timeout = 1.0;
323           break;
324
325         case RESOLUTION_EVENT:
326           clib_warning ("resolver: BOGUS TYPE");
327           break;
328
329         case IP4_ARP_EVENT:
330           for (i = 0; i < vec_len (event_data); i++)
331             handle_ip4_arp_event (event_data[i]);
332           break;
333
334         case IP6_ND_EVENT:
335           for (i = 0; i < vec_len (event_data); i++)
336             handle_ip6_nd_event (event_data[i]);
337           break;
338
339         case ~0:                /* timeout */
340           break;
341         }
342
343       vec_reset_length (event_data);
344     }
345   return 0;                     /* or not */
346 }
347
348 /* *INDENT-OFF* */
349 VLIB_REGISTER_NODE (vpe_resolver_process_node,static) = {
350   .function = resolver_process,
351   .type = VLIB_NODE_TYPE_PROCESS,
352   .name = "vpe-route-resolver-process",
353 };
354 /* *INDENT-ON* */
355
356 static void
357 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
358 {
359   vl_api_sw_interface_set_vpath_reply_t *rmp;
360   int rv = 0;
361   u32 sw_if_index = ntohl (mp->sw_if_index);
362
363   VALIDATE_SW_IF_INDEX (mp);
364
365   l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
366   vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
367                                sw_if_index, mp->enable, 0, 0);
368   vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
369                                sw_if_index, mp->enable, 0, 0);
370   vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
371                                sw_if_index, mp->enable, 0, 0);
372   vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
373                                sw_if_index, mp->enable, 0, 0);
374
375   BAD_SW_IF_INDEX_LABEL;
376
377   REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
378 }
379
380 static void
381   vl_api_sw_interface_set_vxlan_bypass_t_handler
382   (vl_api_sw_interface_set_vxlan_bypass_t * mp)
383 {
384   vl_api_sw_interface_set_vxlan_bypass_reply_t *rmp;
385   int rv = 0;
386   u32 sw_if_index = ntohl (mp->sw_if_index);
387
388   VALIDATE_SW_IF_INDEX (mp);
389
390   if (mp->is_ipv6)
391     {
392       /* not yet implemented */
393     }
394   else
395     vnet_feature_enable_disable ("ip4-unicast", "ip4-vxlan-bypass",
396                                  sw_if_index, mp->enable, 0, 0);
397
398   BAD_SW_IF_INDEX_LABEL;
399
400   REPLY_MACRO (VL_API_SW_INTERFACE_SET_VXLAN_BYPASS_REPLY);
401 }
402
403 static void
404   vl_api_sw_interface_set_l2_xconnect_t_handler
405   (vl_api_sw_interface_set_l2_xconnect_t * mp)
406 {
407   vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
408   int rv = 0;
409   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
410   u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
411   vlib_main_t *vm = vlib_get_main ();
412   vnet_main_t *vnm = vnet_get_main ();
413
414   VALIDATE_RX_SW_IF_INDEX (mp);
415
416   if (mp->enable)
417     {
418       VALIDATE_TX_SW_IF_INDEX (mp);
419       rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
420                             rx_sw_if_index, 0, 0, 0, tx_sw_if_index);
421     }
422   else
423     {
424       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
425     }
426
427   BAD_RX_SW_IF_INDEX_LABEL;
428   BAD_TX_SW_IF_INDEX_LABEL;
429
430   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
431 }
432
433 static void
434   vl_api_sw_interface_set_l2_bridge_t_handler
435   (vl_api_sw_interface_set_l2_bridge_t * mp)
436 {
437   bd_main_t *bdm = &bd_main;
438   vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
439   int rv = 0;
440   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
441   u32 bd_id = ntohl (mp->bd_id);
442   u32 bd_index;
443   u32 bvi = mp->bvi;
444   u8 shg = mp->shg;
445   vlib_main_t *vm = vlib_get_main ();
446   vnet_main_t *vnm = vnet_get_main ();
447
448   VALIDATE_RX_SW_IF_INDEX (mp);
449
450   bd_index = bd_find_or_add_bd_index (bdm, bd_id);
451
452   if (mp->enable)
453     {
454       //VALIDATE_TX_SW_IF_INDEX(mp);
455       rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
456                             rx_sw_if_index, bd_index, bvi, shg, 0);
457     }
458   else
459     {
460       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
461     }
462
463   BAD_RX_SW_IF_INDEX_LABEL;
464
465   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
466 }
467
468 static void
469 vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
470 {
471   bd_main_t *bdm = &bd_main;
472   vl_api_bd_ip_mac_add_del_reply_t *rmp;
473   int rv = 0;
474   u32 bd_id = ntohl (mp->bd_id);
475   u32 bd_index;
476   uword *p;
477
478   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
479   if (p == 0)
480     {
481       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
482       goto out;
483     }
484
485   bd_index = p[0];
486   if (bd_add_del_ip_mac (bd_index, mp->ip_address,
487                          mp->mac_address, mp->is_ipv6, mp->is_add))
488     rv = VNET_API_ERROR_UNSPECIFIED;
489
490 out:
491   REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
492 }
493
494 static void
495 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
496 {
497   vl_api_create_vlan_subif_reply_t *rmp;
498   vnet_main_t *vnm = vnet_get_main ();
499   u32 hw_if_index, sw_if_index = (u32) ~ 0;
500   vnet_hw_interface_t *hi;
501   int rv = 0;
502   u32 id;
503   vnet_sw_interface_t template;
504   uword *p;
505   vnet_interface_main_t *im = &vnm->interface_main;
506   u64 sup_and_sub_key;
507   u64 *kp;
508   unix_shared_memory_queue_t *q;
509   clib_error_t *error;
510
511   VALIDATE_SW_IF_INDEX (mp);
512
513   hw_if_index = ntohl (mp->sw_if_index);
514   hi = vnet_get_hw_interface (vnm, hw_if_index);
515
516   id = ntohl (mp->vlan_id);
517   if (id == 0 || id > 4095)
518     {
519       rv = VNET_API_ERROR_INVALID_VLAN;
520       goto out;
521     }
522
523   sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
524
525   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
526   if (p)
527     {
528       rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
529       goto out;
530     }
531
532   kp = clib_mem_alloc (sizeof (*kp));
533   *kp = sup_and_sub_key;
534
535   memset (&template, 0, sizeof (template));
536   template.type = VNET_SW_INTERFACE_TYPE_SUB;
537   template.sup_sw_if_index = hi->sw_if_index;
538   template.sub.id = id;
539   template.sub.eth.raw_flags = 0;
540   template.sub.eth.flags.one_tag = 1;
541   template.sub.eth.outer_vlan_id = id;
542   template.sub.eth.flags.exact_match = 1;
543
544   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
545   if (error)
546     {
547       clib_error_report (error);
548       rv = VNET_API_ERROR_INVALID_REGISTRATION;
549       goto out;
550     }
551   hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
552   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
553
554   BAD_SW_IF_INDEX_LABEL;
555
556 out:
557   q = vl_api_client_index_to_input_queue (mp->client_index);
558   if (!q)
559     return;
560
561   rmp = vl_msg_api_alloc (sizeof (*rmp));
562   rmp->_vl_msg_id = ntohs (VL_API_CREATE_VLAN_SUBIF_REPLY);
563   rmp->context = mp->context;
564   rmp->retval = ntohl (rv);
565   rmp->sw_if_index = ntohl (sw_if_index);
566   vl_msg_api_send_shmem (q, (u8 *) & rmp);
567 }
568
569 static void
570 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
571 {
572   vl_api_create_subif_reply_t *rmp;
573   vnet_main_t *vnm = vnet_get_main ();
574   u32 sw_if_index = ~0;
575   int rv = 0;
576   u32 sub_id;
577   vnet_sw_interface_t *si;
578   vnet_hw_interface_t *hi;
579   vnet_sw_interface_t template;
580   uword *p;
581   vnet_interface_main_t *im = &vnm->interface_main;
582   u64 sup_and_sub_key;
583   u64 *kp;
584   clib_error_t *error;
585
586   VALIDATE_SW_IF_INDEX (mp);
587
588   si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
589   hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
590
591   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
592     {
593       rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
594       goto out;
595     }
596
597   sw_if_index = si->sw_if_index;
598   sub_id = ntohl (mp->sub_id);
599
600   sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
601
602   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
603   if (p)
604     {
605       if (CLIB_DEBUG > 0)
606         clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
607                       sw_if_index, sub_id);
608       rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
609       goto out;
610     }
611
612   kp = clib_mem_alloc (sizeof (*kp));
613   *kp = sup_and_sub_key;
614
615   memset (&template, 0, sizeof (template));
616   template.type = VNET_SW_INTERFACE_TYPE_SUB;
617   template.sup_sw_if_index = sw_if_index;
618   template.sub.id = sub_id;
619   template.sub.eth.flags.no_tags = mp->no_tags;
620   template.sub.eth.flags.one_tag = mp->one_tag;
621   template.sub.eth.flags.two_tags = mp->two_tags;
622   template.sub.eth.flags.dot1ad = mp->dot1ad;
623   template.sub.eth.flags.exact_match = mp->exact_match;
624   template.sub.eth.flags.default_sub = mp->default_sub;
625   template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
626   template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
627   template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
628   template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
629
630   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
631   if (error)
632     {
633       clib_error_report (error);
634       rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
635       goto out;
636     }
637
638   hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
639   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
640
641   BAD_SW_IF_INDEX_LABEL;
642
643 out:
644
645   /* *INDENT-OFF* */
646   REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
647   ({
648     rmp->sw_if_index = ntohl(sw_if_index);
649   }));
650   /* *INDENT-ON* */
651 }
652
653 static void
654 vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
655 {
656   vl_api_proxy_arp_add_del_reply_t *rmp;
657   u32 fib_index;
658   int rv;
659   ip4_main_t *im = &ip4_main;
660   stats_main_t *sm = &stats_main;
661   int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
662                               ip4_address_t * hi_addr,
663                               u32 fib_index, int is_del);
664   uword *p;
665
666   dslock (sm, 1 /* release hint */ , 6 /* tag */ );
667
668   p = hash_get (im->fib_index_by_table_id, ntohl (mp->vrf_id));
669
670   if (!p)
671     {
672       rv = VNET_API_ERROR_NO_SUCH_FIB;
673       goto out;
674     }
675
676   fib_index = p[0];
677
678   rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->low_address,
679                                (ip4_address_t *) mp->hi_address,
680                                fib_index, mp->is_add == 0);
681
682 out:
683   dsunlock (sm);
684   REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
685 }
686
687 static void
688   vl_api_proxy_arp_intfc_enable_disable_t_handler
689   (vl_api_proxy_arp_intfc_enable_disable_t * mp)
690 {
691   int rv = 0;
692   vnet_main_t *vnm = vnet_get_main ();
693   vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
694   vnet_sw_interface_t *si;
695   u32 sw_if_index;
696
697   VALIDATE_SW_IF_INDEX (mp);
698
699   sw_if_index = ntohl (mp->sw_if_index);
700
701   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
702     {
703       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
704       goto out;
705     }
706
707   si = vnet_get_sw_interface (vnm, sw_if_index);
708
709   ASSERT (si);
710
711   if (mp->enable_disable)
712     si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
713   else
714     si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
715
716   BAD_SW_IF_INDEX_LABEL;
717
718 out:
719   REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
720 }
721
722 static void
723 vl_api_is_address_reachable_t_handler (vl_api_is_address_reachable_t * mp)
724 {
725 #if 0
726   vpe_main_t *rm = &vpe_main;
727   ip4_main_t *im4 = &ip4_main;
728   ip6_main_t *im6 = &ip6_main;
729   ip_lookup_main_t *lm;
730   union
731   {
732     ip4_address_t ip4;
733     ip6_address_t ip6;
734   } addr;
735   u32 adj_index, sw_if_index;
736   vl_api_is_address_reachable_t *rmp;
737   ip_adjacency_t *adj;
738   unix_shared_memory_queue_t *q;
739
740   q = vl_api_client_index_to_input_queue (mp->client_index);
741   if (!q)
742     {
743       increment_missing_api_client_counter (rm->vlib_main);
744       return;
745     }
746
747   rmp = vl_msg_api_alloc (sizeof (*rmp));
748   clib_memcpy (rmp, mp, sizeof (*rmp));
749
750   sw_if_index = mp->next_hop_sw_if_index;
751   clib_memcpy (&addr, mp->address, sizeof (addr));
752   if (mp->is_ipv6)
753     {
754       lm = &im6->lookup_main;
755       adj_index = ip6_fib_lookup (im6, sw_if_index, &addr.ip6);
756     }
757   else
758     {
759       lm = &im4->lookup_main;
760       // FIXME NOT an ADJ
761       adj_index = ip4_fib_lookup (im4, sw_if_index, &addr.ip4);
762     }
763   if (adj_index == ~0)
764     {
765       rmp->is_error = 1;
766       goto send;
767     }
768   adj = ip_get_adjacency (lm, adj_index);
769
770   if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE
771       && adj->rewrite_header.sw_if_index == sw_if_index)
772     {
773       rmp->is_known = 1;
774     }
775   else
776     {
777       if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP
778           && adj->rewrite_header.sw_if_index == sw_if_index)
779         {
780           if (mp->is_ipv6)
781             ip6_probe_neighbor (rm->vlib_main, &addr.ip6, sw_if_index);
782           else
783             ip4_probe_neighbor (rm->vlib_main, &addr.ip4, sw_if_index);
784         }
785       else if (adj->lookup_next_index == IP_LOOKUP_NEXT_DROP)
786         {
787           rmp->is_known = 1;
788           goto send;
789         }
790       rmp->is_known = 0;
791     }
792
793 send:
794   vl_msg_api_send_shmem (q, (u8 *) & rmp);
795 #endif
796 }
797
798 static void
799   vl_api_sw_interface_set_mpls_enable_t_handler
800   (vl_api_sw_interface_set_mpls_enable_t * mp)
801 {
802   vl_api_sw_interface_set_mpls_enable_reply_t *rmp;
803   int rv = 0;
804
805   VALIDATE_SW_IF_INDEX (mp);
806
807   mpls_sw_interface_enable_disable (&mpls_main,
808                                     ntohl (mp->sw_if_index), mp->enable);
809
810   BAD_SW_IF_INDEX_LABEL;
811   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MPLS_ENABLE_REPLY);
812 }
813
814 void
815 send_oam_event (oam_target_t * t)
816 {
817   vpe_api_main_t *vam = &vpe_api_main;
818   unix_shared_memory_queue_t *q;
819   vpe_client_registration_t *reg;
820   vl_api_oam_event_t *mp;
821
822   /* *INDENT-OFF* */
823   pool_foreach(reg, vam->oam_events_registrations,
824   ({
825     q = vl_api_client_index_to_input_queue (reg->client_index);
826     if (q)
827       {
828         mp = vl_msg_api_alloc (sizeof (*mp));
829         mp->_vl_msg_id = ntohs (VL_API_OAM_EVENT);
830         clib_memcpy (mp->dst_address, &t->dst_address,
831                      sizeof (mp->dst_address));
832         mp->state = t->state;
833         vl_msg_api_send_shmem (q, (u8 *)&mp);
834       }
835   }));
836   /* *INDENT-ON* */
837 }
838
839 static void
840 vl_api_oam_add_del_t_handler (vl_api_oam_add_del_t * mp)
841 {
842   vl_api_oam_add_del_reply_t *rmp;
843   int rv;
844
845   rv = vpe_oam_add_del_target ((ip4_address_t *) mp->src_address,
846                                (ip4_address_t *) mp->dst_address,
847                                ntohl (mp->vrf_id), (int) (mp->is_add));
848
849   REPLY_MACRO (VL_API_OAM_ADD_DEL_REPLY);
850 }
851
852 static void
853 vl_api_vnet_get_summary_stats_t_handler (vl_api_vnet_get_summary_stats_t * mp)
854 {
855   stats_main_t *sm = &stats_main;
856   vnet_interface_main_t *im = sm->interface_main;
857   vl_api_vnet_summary_stats_reply_t *rmp;
858   vlib_combined_counter_main_t *cm;
859   vlib_counter_t v;
860   int i, which;
861   u64 total_pkts[VLIB_N_RX_TX];
862   u64 total_bytes[VLIB_N_RX_TX];
863
864   unix_shared_memory_queue_t *q =
865     vl_api_client_index_to_input_queue (mp->client_index);
866
867   if (!q)
868     return;
869
870   rmp = vl_msg_api_alloc (sizeof (*rmp));
871   rmp->_vl_msg_id = ntohs (VL_API_VNET_SUMMARY_STATS_REPLY);
872   rmp->context = mp->context;
873   rmp->retval = 0;
874
875   memset (total_pkts, 0, sizeof (total_pkts));
876   memset (total_bytes, 0, sizeof (total_bytes));
877
878   vnet_interface_counter_lock (im);
879
880   vec_foreach (cm, im->combined_sw_if_counters)
881   {
882     which = cm - im->combined_sw_if_counters;
883
884     for (i = 0; i < vec_len (cm->maxi); i++)
885       {
886         vlib_get_combined_counter (cm, i, &v);
887         total_pkts[which] += v.packets;
888         total_bytes[which] += v.bytes;
889       }
890   }
891   vnet_interface_counter_unlock (im);
892
893   rmp->total_pkts[VLIB_RX] = clib_host_to_net_u64 (total_pkts[VLIB_RX]);
894   rmp->total_bytes[VLIB_RX] = clib_host_to_net_u64 (total_bytes[VLIB_RX]);
895   rmp->total_pkts[VLIB_TX] = clib_host_to_net_u64 (total_pkts[VLIB_TX]);
896   rmp->total_bytes[VLIB_TX] = clib_host_to_net_u64 (total_bytes[VLIB_TX]);
897   rmp->vector_rate =
898     clib_host_to_net_u64 (vlib_last_vector_length_per_node (sm->vlib_main));
899
900   vl_msg_api_send_shmem (q, (u8 *) & rmp);
901 }
902
903 /* *INDENT-OFF* */
904 typedef CLIB_PACKED (struct {
905   ip4_address_t address;
906   u32 address_length: 6;
907   u32 index:26;
908 }) ip4_route_t;
909 /* *INDENT-ON* */
910
911 static int
912 ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
913 {
914   vnet_main_t *vnm = vnet_get_main ();
915   vnet_interface_main_t *im = &vnm->interface_main;
916   ip4_main_t *im4 = &ip4_main;
917   static u32 *sw_if_indices_to_shut;
918   stats_main_t *sm = &stats_main;
919   fib_table_t *fib_table;
920   ip4_fib_t *fib;
921   u32 sw_if_index;
922   int i;
923   int rv = VNET_API_ERROR_NO_SUCH_FIB;
924   u32 target_fib_id = ntohl (mp->vrf_id);
925
926   dslock (sm, 1 /* release hint */ , 8 /* tag */ );
927
928   /* *INDENT-OFF* */
929   pool_foreach (fib_table, im4->fibs,
930   ({
931     fib = &fib_table->v4;
932     vnet_sw_interface_t * si;
933
934     if (fib->table_id != target_fib_id)
935       continue;
936
937     /* remove any mpls encap/decap labels */
938     mpls_fib_reset_labels (fib->table_id);
939
940     /* remove any proxy arps in this fib */
941     vnet_proxy_arp_fib_reset (fib->table_id);
942
943     /* Set the flow hash for this fib to the default */
944     vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
945
946     vec_reset_length (sw_if_indices_to_shut);
947
948     /* Shut down interfaces in this FIB / clean out intfc routes */
949     pool_foreach (si, im->sw_interfaces,
950     ({
951       u32 sw_if_index = si->sw_if_index;
952
953       if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
954           && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
955               fib->index))
956         vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
957     }));
958
959     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
960       sw_if_index = sw_if_indices_to_shut[i];
961
962       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
963       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
964       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
965     }
966
967     fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
968
969     rv = 0;
970     break;
971     })); /* pool_foreach (fib) */
972     /* *INDENT-ON* */
973
974   dsunlock (sm);
975   return rv;
976 }
977
978 static int
979 ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
980 {
981   vnet_main_t *vnm = vnet_get_main ();
982   vnet_interface_main_t *im = &vnm->interface_main;
983   ip6_main_t *im6 = &ip6_main;
984   stats_main_t *sm = &stats_main;
985   static u32 *sw_if_indices_to_shut;
986   fib_table_t *fib_table;
987   ip6_fib_t *fib;
988   u32 sw_if_index;
989   int i;
990   int rv = VNET_API_ERROR_NO_SUCH_FIB;
991   u32 target_fib_id = ntohl (mp->vrf_id);
992
993   dslock (sm, 1 /* release hint */ , 9 /* tag */ );
994
995   /* *INDENT-OFF* */
996   pool_foreach (fib_table, im6->fibs,
997   ({
998     vnet_sw_interface_t * si;
999     fib = &(fib_table->v6);
1000
1001     if (fib->table_id != target_fib_id)
1002       continue;
1003
1004     vec_reset_length (sw_if_indices_to_shut);
1005
1006     /* Shut down interfaces in this FIB / clean out intfc routes */
1007     pool_foreach (si, im->sw_interfaces,
1008     ({
1009       if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
1010           fib->index)
1011         vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
1012     }));
1013
1014     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
1015       sw_if_index = sw_if_indices_to_shut[i];
1016
1017       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
1018       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
1019       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
1020     }
1021
1022     fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
1023
1024     rv = 0;
1025     break;
1026   })); /* pool_foreach (fib) */
1027   /* *INDENT-ON* */
1028
1029   dsunlock (sm);
1030   return rv;
1031 }
1032
1033 static void
1034 vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
1035 {
1036   int rv;
1037   vl_api_reset_fib_reply_t *rmp;
1038
1039   if (mp->is_ipv6)
1040     rv = ip6_reset_fib_t_handler (mp);
1041   else
1042     rv = ip4_reset_fib_t_handler (mp);
1043
1044   REPLY_MACRO (VL_API_RESET_FIB_REPLY);
1045 }
1046
1047 static void
1048 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
1049 {
1050   vl_api_create_loopback_reply_t *rmp;
1051   u32 sw_if_index;
1052   int rv;
1053
1054   rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address);
1055
1056   /* *INDENT-OFF* */
1057   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
1058   ({
1059     rmp->sw_if_index = ntohl (sw_if_index);
1060   }));
1061   /* *INDENT-ON* */
1062 }
1063
1064 static void
1065 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
1066 {
1067   vl_api_delete_loopback_reply_t *rmp;
1068   u32 sw_if_index;
1069   int rv;
1070
1071   sw_if_index = ntohl (mp->sw_if_index);
1072   rv = vnet_delete_loopback_interface (sw_if_index);
1073
1074   REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
1075 }
1076
1077 static void
1078 vl_api_control_ping_t_handler (vl_api_control_ping_t * mp)
1079 {
1080   vl_api_control_ping_reply_t *rmp;
1081   int rv = 0;
1082
1083   /* *INDENT-OFF* */
1084   REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
1085   ({
1086     rmp->vpe_pid = ntohl (getpid());
1087   }));
1088   /* *INDENT-ON* */
1089 }
1090
1091 static void
1092 shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
1093 {
1094   u8 **shmem_vecp = (u8 **) arg;
1095   u8 *shmem_vec;
1096   void *oldheap;
1097   api_main_t *am = &api_main;
1098   u32 offset;
1099
1100   shmem_vec = *shmem_vecp;
1101
1102   offset = vec_len (shmem_vec);
1103
1104   pthread_mutex_lock (&am->vlib_rp->mutex);
1105   oldheap = svm_push_data_heap (am->vlib_rp);
1106
1107   vec_validate (shmem_vec, offset + buffer_bytes - 1);
1108
1109   clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
1110
1111   svm_pop_heap (oldheap);
1112   pthread_mutex_unlock (&am->vlib_rp->mutex);
1113
1114   *shmem_vecp = shmem_vec;
1115 }
1116
1117
1118 static void
1119 vl_api_cli_request_t_handler (vl_api_cli_request_t * mp)
1120 {
1121   vl_api_cli_reply_t *rp;
1122   unix_shared_memory_queue_t *q;
1123   vlib_main_t *vm = vlib_get_main ();
1124   api_main_t *am = &api_main;
1125   unformat_input_t input;
1126   u8 *shmem_vec = 0;
1127   void *oldheap;
1128
1129   q = vl_api_client_index_to_input_queue (mp->client_index);
1130   if (!q)
1131     return;
1132
1133   rp = vl_msg_api_alloc (sizeof (*rp));
1134   rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY);
1135   rp->context = mp->context;
1136
1137   unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
1138
1139   vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
1140
1141   pthread_mutex_lock (&am->vlib_rp->mutex);
1142   oldheap = svm_push_data_heap (am->vlib_rp);
1143
1144   vec_add1 (shmem_vec, 0);
1145
1146   svm_pop_heap (oldheap);
1147   pthread_mutex_unlock (&am->vlib_rp->mutex);
1148
1149   rp->reply_in_shmem = (uword) shmem_vec;
1150
1151   vl_msg_api_send_shmem (q, (u8 *) & rp);
1152 }
1153
1154 static void
1155 inband_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
1156 {
1157   u8 **mem_vecp = (u8 **) arg;
1158   u8 *mem_vec = *mem_vecp;
1159   u32 offset = vec_len (mem_vec);
1160
1161   vec_validate (mem_vec, offset + buffer_bytes - 1);
1162   clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
1163   *mem_vecp = mem_vec;
1164 }
1165
1166 static void
1167 vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
1168 {
1169   vl_api_cli_inband_reply_t *rmp;
1170   int rv = 0;
1171   unix_shared_memory_queue_t *q;
1172   vlib_main_t *vm = vlib_get_main ();
1173   unformat_input_t input;
1174   u8 *out_vec = 0;
1175
1176   q = vl_api_client_index_to_input_queue (mp->client_index);
1177   if (!q)
1178     return;
1179
1180   unformat_init_string (&input, (char *) mp->cmd, ntohl (mp->length));
1181   vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
1182
1183   u32 len = vec_len (out_vec);
1184   /* *INDENT-OFF* */
1185   REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, len,
1186   ({
1187     rmp->length = htonl (len);
1188     clib_memcpy (rmp->reply, out_vec, len);
1189   }));
1190   /* *INDENT-ON* */
1191   vec_free (out_vec);
1192 }
1193
1194 static void
1195 vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
1196 {
1197   int rv;
1198   vl_api_set_arp_neighbor_limit_reply_t *rmp;
1199   vnet_main_t *vnm = vnet_get_main ();
1200   clib_error_t *error;
1201
1202   vnm->api_errno = 0;
1203
1204   if (mp->is_ipv6)
1205     error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
1206   else
1207     error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
1208
1209   if (error)
1210     {
1211       clib_error_report (error);
1212       rv = VNET_API_ERROR_UNSPECIFIED;
1213     }
1214   else
1215     {
1216       rv = vnm->api_errno;
1217     }
1218
1219   REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
1220 }
1221
1222 static void vl_api_classify_set_interface_ip_table_t_handler
1223   (vl_api_classify_set_interface_ip_table_t * mp)
1224 {
1225   vlib_main_t *vm = vlib_get_main ();
1226   vl_api_classify_set_interface_ip_table_reply_t *rmp;
1227   int rv;
1228   u32 table_index, sw_if_index;
1229
1230   table_index = ntohl (mp->table_index);
1231   sw_if_index = ntohl (mp->sw_if_index);
1232
1233   VALIDATE_SW_IF_INDEX (mp);
1234
1235   if (mp->is_ipv6)
1236     rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
1237   else
1238     rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
1239
1240   BAD_SW_IF_INDEX_LABEL;
1241
1242   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
1243 }
1244
1245 static void vl_api_classify_set_interface_l2_tables_t_handler
1246   (vl_api_classify_set_interface_l2_tables_t * mp)
1247 {
1248   vl_api_classify_set_interface_l2_tables_reply_t *rmp;
1249   int rv;
1250   u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
1251   int enable;
1252
1253   ip4_table_index = ntohl (mp->ip4_table_index);
1254   ip6_table_index = ntohl (mp->ip6_table_index);
1255   other_table_index = ntohl (mp->other_table_index);
1256   sw_if_index = ntohl (mp->sw_if_index);
1257
1258   VALIDATE_SW_IF_INDEX (mp);
1259
1260   if (mp->is_input)
1261     rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
1262                                             ip6_table_index,
1263                                             other_table_index);
1264   else
1265     rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
1266                                              ip6_table_index,
1267                                              other_table_index);
1268
1269   if (rv == 0)
1270     {
1271       if (ip4_table_index != ~0 || ip6_table_index != ~0
1272           || other_table_index != ~0)
1273         enable = 1;
1274       else
1275         enable = 0;
1276
1277       if (mp->is_input)
1278         vnet_l2_input_classify_enable_disable (sw_if_index, enable);
1279       else
1280         vnet_l2_output_classify_enable_disable (sw_if_index, enable);
1281     }
1282
1283   BAD_SW_IF_INDEX_LABEL;
1284
1285   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
1286 }
1287
1288 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
1289                                      u32 sw_if_index, u32 enable);
1290
1291 static void
1292 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
1293                                           mp)
1294 {
1295   int rv;
1296   vl_api_l2_interface_efp_filter_reply_t *rmp;
1297   vnet_main_t *vnm = vnet_get_main ();
1298
1299   // enable/disable the feature
1300   l2_efp_filter_configure (vnm, mp->sw_if_index, mp->enable_disable);
1301   rv = vnm->api_errno;
1302
1303   REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
1304 }
1305
1306 static void
1307 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
1308 {
1309   vl_api_show_version_reply_t *rmp;
1310   int rv = 0;
1311   char *vpe_api_get_build_directory (void);
1312   char *vpe_api_get_version (void);
1313   char *vpe_api_get_build_date (void);
1314
1315   unix_shared_memory_queue_t *q =
1316     vl_api_client_index_to_input_queue (mp->client_index);
1317
1318   if (!q)
1319     return;
1320
1321   /* *INDENT-OFF* */
1322   REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
1323   ({
1324     strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
1325     strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
1326              ARRAY_LEN(rmp->build_directory)-1);
1327     strncpy ((char *) rmp->version, vpe_api_get_version(),
1328              ARRAY_LEN(rmp->version)-1);
1329     strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
1330              ARRAY_LEN(rmp->build_date)-1);
1331   }));
1332   /* *INDENT-ON* */
1333 }
1334
1335 static void
1336 vl_api_get_node_index_t_handler (vl_api_get_node_index_t * mp)
1337 {
1338   vlib_main_t *vm = vlib_get_main ();
1339   vl_api_get_node_index_reply_t *rmp;
1340   vlib_node_t *n;
1341   int rv = 0;
1342   u32 node_index = ~0;
1343
1344   n = vlib_get_node_by_name (vm, mp->node_name);
1345
1346   if (n == 0)
1347     rv = VNET_API_ERROR_NO_SUCH_NODE;
1348   else
1349     node_index = n->index;
1350
1351   /* *INDENT-OFF* */
1352   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
1353   ({
1354     rmp->node_index = ntohl(node_index);
1355   }));
1356   /* *INDENT-ON* */
1357 }
1358
1359 static void
1360 vl_api_get_next_index_t_handler (vl_api_get_next_index_t * mp)
1361 {
1362   vlib_main_t *vm = vlib_get_main ();
1363   vl_api_get_next_index_reply_t *rmp;
1364   vlib_node_t *node, *next_node;
1365   int rv = 0;
1366   u32 next_node_index = ~0, next_index = ~0;
1367   uword *p;
1368
1369   node = vlib_get_node_by_name (vm, mp->node_name);
1370
1371   if (node == 0)
1372     {
1373       rv = VNET_API_ERROR_NO_SUCH_NODE;
1374       goto out;
1375     }
1376
1377   next_node = vlib_get_node_by_name (vm, mp->next_name);
1378
1379   if (next_node == 0)
1380     {
1381       rv = VNET_API_ERROR_NO_SUCH_NODE2;
1382       goto out;
1383     }
1384   else
1385     next_node_index = next_node->index;
1386
1387   p = hash_get (node->next_slot_by_node, next_node_index);
1388
1389   if (p == 0)
1390     {
1391       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1392       goto out;
1393     }
1394   else
1395     next_index = p[0];
1396
1397 out:
1398   /* *INDENT-OFF* */
1399   REPLY_MACRO2(VL_API_GET_NEXT_INDEX_REPLY,
1400   ({
1401     rmp->next_index = ntohl(next_index);
1402   }));
1403   /* *INDENT-ON* */
1404 }
1405
1406 static void
1407 vl_api_add_node_next_t_handler (vl_api_add_node_next_t * mp)
1408 {
1409   vlib_main_t *vm = vlib_get_main ();
1410   vl_api_add_node_next_reply_t *rmp;
1411   vlib_node_t *n, *next;
1412   int rv = 0;
1413   u32 next_index = ~0;
1414
1415   n = vlib_get_node_by_name (vm, mp->node_name);
1416
1417   if (n == 0)
1418     {
1419       rv = VNET_API_ERROR_NO_SUCH_NODE;
1420       goto out;
1421     }
1422
1423   next = vlib_get_node_by_name (vm, mp->next_name);
1424
1425   if (next == 0)
1426     rv = VNET_API_ERROR_NO_SUCH_NODE2;
1427   else
1428     next_index = vlib_node_add_next (vm, n->index, next->index);
1429
1430 out:
1431   /* *INDENT-OFF* */
1432   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
1433   ({
1434     rmp->next_index = ntohl(next_index);
1435   }));
1436   /* *INDENT-ON* */
1437 }
1438
1439 static void vl_api_vxlan_add_del_tunnel_t_handler
1440   (vl_api_vxlan_add_del_tunnel_t * mp)
1441 {
1442   vl_api_vxlan_add_del_tunnel_reply_t *rmp;
1443   int rv = 0;
1444   vnet_vxlan_add_del_tunnel_args_t _a, *a = &_a;
1445   u32 encap_fib_index;
1446   uword *p;
1447   ip4_main_t *im = &ip4_main;
1448   vnet_main_t *vnm = vnet_get_main ();
1449   u32 sw_if_index = ~0;
1450
1451   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
1452   if (!p)
1453     {
1454       rv = VNET_API_ERROR_NO_SUCH_FIB;
1455       goto out;
1456     }
1457   encap_fib_index = p[0];
1458   memset (a, 0, sizeof (*a));
1459
1460   a->is_add = mp->is_add;
1461   a->is_ip6 = mp->is_ipv6;
1462
1463   /* ip addresses sent in network byte order */
1464   ip46_from_addr_buf (mp->is_ipv6, mp->dst_address, &a->dst);
1465   ip46_from_addr_buf (mp->is_ipv6, mp->src_address, &a->src);
1466
1467   /* Check src & dst are different */
1468   if (ip46_address_cmp (&a->dst, &a->src) == 0)
1469     {
1470       rv = VNET_API_ERROR_SAME_SRC_DST;
1471       goto out;
1472     }
1473   a->mcast_sw_if_index = ntohl (mp->mcast_sw_if_index);
1474   if (ip46_address_is_multicast (&a->dst) &&
1475       pool_is_free_index (vnm->interface_main.sw_interfaces,
1476                           a->mcast_sw_if_index))
1477     {
1478       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1479       goto out;
1480     }
1481   a->encap_fib_index = encap_fib_index;
1482   a->decap_next_index = ntohl (mp->decap_next_index);
1483   a->vni = ntohl (mp->vni);
1484   rv = vnet_vxlan_add_del_tunnel (a, &sw_if_index);
1485
1486 out:
1487   /* *INDENT-OFF* */
1488   REPLY_MACRO2(VL_API_VXLAN_ADD_DEL_TUNNEL_REPLY,
1489   ({
1490     rmp->sw_if_index = ntohl (sw_if_index);
1491   }));
1492   /* *INDENT-ON* */
1493 }
1494
1495 static void send_vxlan_tunnel_details
1496   (vxlan_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
1497 {
1498   vl_api_vxlan_tunnel_details_t *rmp;
1499   ip4_main_t *im4 = &ip4_main;
1500   ip6_main_t *im6 = &ip6_main;
1501   u8 is_ipv6 = !ip46_address_is_ip4 (&t->dst);
1502
1503   rmp = vl_msg_api_alloc (sizeof (*rmp));
1504   memset (rmp, 0, sizeof (*rmp));
1505   rmp->_vl_msg_id = ntohs (VL_API_VXLAN_TUNNEL_DETAILS);
1506   if (is_ipv6)
1507     {
1508       memcpy (rmp->src_address, t->src.ip6.as_u8, 16);
1509       memcpy (rmp->dst_address, t->dst.ip6.as_u8, 16);
1510       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
1511     }
1512   else
1513     {
1514       memcpy (rmp->src_address, t->src.ip4.as_u8, 4);
1515       memcpy (rmp->dst_address, t->dst.ip4.as_u8, 4);
1516       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
1517     }
1518   rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
1519   rmp->vni = htonl (t->vni);
1520   rmp->decap_next_index = htonl (t->decap_next_index);
1521   rmp->sw_if_index = htonl (t->sw_if_index);
1522   rmp->is_ipv6 = is_ipv6;
1523   rmp->context = context;
1524
1525   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1526 }
1527
1528 static void vl_api_vxlan_tunnel_dump_t_handler
1529   (vl_api_vxlan_tunnel_dump_t * mp)
1530 {
1531   unix_shared_memory_queue_t *q;
1532   vxlan_main_t *vxm = &vxlan_main;
1533   vxlan_tunnel_t *t;
1534   u32 sw_if_index;
1535
1536   q = vl_api_client_index_to_input_queue (mp->client_index);
1537   if (q == 0)
1538     {
1539       return;
1540     }
1541
1542   sw_if_index = ntohl (mp->sw_if_index);
1543
1544   if (~0 == sw_if_index)
1545     {
1546       /* *INDENT-OFF* */
1547       pool_foreach (t, vxm->tunnels,
1548       ({
1549         send_vxlan_tunnel_details(t, q, mp->context);
1550       }));
1551       /* *INDENT-ON* */
1552     }
1553   else
1554     {
1555       if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) ||
1556           (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index]))
1557         {
1558           return;
1559         }
1560       t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
1561       send_vxlan_tunnel_details (t, q, mp->context);
1562     }
1563 }
1564
1565 static void
1566 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
1567 {
1568   extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1569                                     int is_add);
1570   vl_api_l2_patch_add_del_reply_t *rmp;
1571   int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1572                              int is_add);
1573   int rv = 0;
1574
1575   VALIDATE_RX_SW_IF_INDEX (mp);
1576   VALIDATE_TX_SW_IF_INDEX (mp);
1577
1578   rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
1579                               ntohl (mp->tx_sw_if_index),
1580                               (int) (mp->is_add != 0));
1581
1582   BAD_RX_SW_IF_INDEX_LABEL;
1583   BAD_TX_SW_IF_INDEX_LABEL;
1584
1585   REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
1586 }
1587
1588 static void
1589   vl_api_vxlan_gpe_add_del_tunnel_t_handler
1590   (vl_api_vxlan_gpe_add_del_tunnel_t * mp)
1591 {
1592   vl_api_vxlan_gpe_add_del_tunnel_reply_t *rmp;
1593   int rv = 0;
1594   vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
1595   u32 encap_fib_index, decap_fib_index;
1596   u8 protocol;
1597   uword *p;
1598   ip4_main_t *im = &ip4_main;
1599   u32 sw_if_index = ~0;
1600
1601
1602   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
1603   if (!p)
1604     {
1605       rv = VNET_API_ERROR_NO_SUCH_FIB;
1606       goto out;
1607     }
1608   encap_fib_index = p[0];
1609
1610   protocol = mp->protocol;
1611
1612   /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
1613   if (protocol == VXLAN_GPE_INPUT_NEXT_IP4_INPUT)
1614     {
1615       p = hash_get (im->fib_index_by_table_id, ntohl (mp->decap_vrf_id));
1616       if (!p)
1617         {
1618           rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
1619           goto out;
1620         }
1621       decap_fib_index = p[0];
1622     }
1623   else
1624     {
1625       decap_fib_index = ntohl (mp->decap_vrf_id);
1626     }
1627
1628   /* Check src & dst are different */
1629   if ((mp->is_ipv6 && memcmp (mp->local, mp->remote, 16) == 0) ||
1630       (!mp->is_ipv6 && memcmp (mp->local, mp->remote, 4) == 0))
1631     {
1632       rv = VNET_API_ERROR_SAME_SRC_DST;
1633       goto out;
1634     }
1635   memset (a, 0, sizeof (*a));
1636
1637   a->is_add = mp->is_add;
1638   a->is_ip6 = mp->is_ipv6;
1639   /* ip addresses sent in network byte order */
1640   if (a->is_ip6)
1641     {
1642       clib_memcpy (&(a->local.ip6), mp->local, 16);
1643       clib_memcpy (&(a->remote.ip6), mp->remote, 16);
1644     }
1645   else
1646     {
1647       clib_memcpy (&(a->local.ip4), mp->local, 4);
1648       clib_memcpy (&(a->remote.ip4), mp->remote, 4);
1649     }
1650   a->encap_fib_index = encap_fib_index;
1651   a->decap_fib_index = decap_fib_index;
1652   a->protocol = protocol;
1653   a->vni = ntohl (mp->vni);
1654   rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
1655
1656 out:
1657   /* *INDENT-OFF* */
1658   REPLY_MACRO2(VL_API_VXLAN_GPE_ADD_DEL_TUNNEL_REPLY,
1659   ({
1660     rmp->sw_if_index = ntohl (sw_if_index);
1661   }));
1662   /* *INDENT-ON* */
1663 }
1664
1665 static void send_vxlan_gpe_tunnel_details
1666   (vxlan_gpe_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
1667 {
1668   vl_api_vxlan_gpe_tunnel_details_t *rmp;
1669   ip4_main_t *im4 = &ip4_main;
1670   ip6_main_t *im6 = &ip6_main;
1671   u8 is_ipv6 = !(t->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
1672
1673   rmp = vl_msg_api_alloc (sizeof (*rmp));
1674   memset (rmp, 0, sizeof (*rmp));
1675   rmp->_vl_msg_id = ntohs (VL_API_VXLAN_GPE_TUNNEL_DETAILS);
1676   if (is_ipv6)
1677     {
1678       memcpy (rmp->local, &(t->local.ip6), 16);
1679       memcpy (rmp->remote, &(t->remote.ip6), 16);
1680       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
1681       rmp->decap_vrf_id = htonl (im6->fibs[t->decap_fib_index].ft_table_id);
1682     }
1683   else
1684     {
1685       memcpy (rmp->local, &(t->local.ip4), 4);
1686       memcpy (rmp->remote, &(t->remote.ip4), 4);
1687       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
1688       rmp->decap_vrf_id = htonl (im4->fibs[t->decap_fib_index].ft_table_id);
1689     }
1690   rmp->vni = htonl (t->vni);
1691   rmp->protocol = t->protocol;
1692   rmp->sw_if_index = htonl (t->sw_if_index);
1693   rmp->is_ipv6 = is_ipv6;
1694   rmp->context = context;
1695
1696   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1697 }
1698
1699 static void vl_api_vxlan_gpe_tunnel_dump_t_handler
1700   (vl_api_vxlan_gpe_tunnel_dump_t * mp)
1701 {
1702   unix_shared_memory_queue_t *q;
1703   vxlan_gpe_main_t *vgm = &vxlan_gpe_main;
1704   vxlan_gpe_tunnel_t *t;
1705   u32 sw_if_index;
1706
1707   q = vl_api_client_index_to_input_queue (mp->client_index);
1708   if (q == 0)
1709     {
1710       return;
1711     }
1712
1713   sw_if_index = ntohl (mp->sw_if_index);
1714
1715   if (~0 == sw_if_index)
1716     {
1717       /* *INDENT-OFF* */
1718       pool_foreach (t, vgm->tunnels,
1719       ({
1720         send_vxlan_gpe_tunnel_details(t, q, mp->context);
1721       }));
1722       /* *INDENT-ON* */
1723     }
1724   else
1725     {
1726       if ((sw_if_index >= vec_len (vgm->tunnel_index_by_sw_if_index)) ||
1727           (~0 == vgm->tunnel_index_by_sw_if_index[sw_if_index]))
1728         {
1729           return;
1730         }
1731       t = &vgm->tunnels[vgm->tunnel_index_by_sw_if_index[sw_if_index]];
1732       send_vxlan_gpe_tunnel_details (t, q, mp->context);
1733     }
1734 }
1735
1736 static void
1737 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
1738                                           mp)
1739 {
1740   vl_api_interface_name_renumber_reply_t *rmp;
1741   int rv = 0;
1742
1743   VALIDATE_SW_IF_INDEX (mp);
1744
1745   rv = vnet_interface_name_renumber
1746     (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
1747
1748   BAD_SW_IF_INDEX_LABEL;
1749
1750   REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
1751 }
1752
1753 static int
1754 arp_change_data_callback (u32 pool_index, u8 * new_mac,
1755                           u32 sw_if_index, u32 address)
1756 {
1757   vpe_api_main_t *am = &vpe_api_main;
1758   vlib_main_t *vm = am->vlib_main;
1759   vl_api_ip4_arp_event_t *event;
1760   static f64 arp_event_last_time;
1761   f64 now = vlib_time_now (vm);
1762
1763   if (pool_is_free_index (am->arp_events, pool_index))
1764     return 1;
1765
1766   event = pool_elt_at_index (am->arp_events, pool_index);
1767   /* *INDENT-OFF* */
1768   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
1769     {
1770       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
1771     }
1772   else
1773     {                           /* same mac */
1774       if (sw_if_index == event->sw_if_index &&
1775           (!event->mac_ip ||
1776            /* for BD case, also check IP address with 10 sec timeout */
1777            (address == event->address &&
1778             (now - arp_event_last_time) < 10.0)))
1779         return 1;
1780     }
1781   /* *INDENT-ON* */
1782
1783   arp_event_last_time = now;
1784   event->sw_if_index = sw_if_index;
1785   if (event->mac_ip)
1786     event->address = address;
1787   return 0;
1788 }
1789
1790 static int
1791 nd_change_data_callback (u32 pool_index, u8 * new_mac,
1792                          u32 sw_if_index, ip6_address_t * address)
1793 {
1794   vpe_api_main_t *am = &vpe_api_main;
1795   vlib_main_t *vm = am->vlib_main;
1796   vl_api_ip6_nd_event_t *event;
1797   static f64 nd_event_last_time;
1798   f64 now = vlib_time_now (vm);
1799
1800   if (pool_is_free_index (am->nd_events, pool_index))
1801     return 1;
1802
1803   event = pool_elt_at_index (am->nd_events, pool_index);
1804
1805   /* *INDENT-OFF* */
1806   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
1807     {
1808       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
1809     }
1810   else
1811     {                           /* same mac */
1812       if (sw_if_index == event->sw_if_index &&
1813           (!event->mac_ip ||
1814            /* for BD case, also check IP address with 10 sec timeout */
1815            (ip6_address_is_equal (address,
1816                                   (ip6_address_t *) event->address) &&
1817             (now - nd_event_last_time) < 10.0)))
1818         return 1;
1819     }
1820   /* *INDENT-ON* */
1821
1822   nd_event_last_time = now;
1823   event->sw_if_index = sw_if_index;
1824   if (event->mac_ip)
1825     clib_memcpy (event->address, address, sizeof (event->address));
1826   return 0;
1827 }
1828
1829 static int
1830 arp_change_delete_callback (u32 pool_index, u8 * notused)
1831 {
1832   vpe_api_main_t *am = &vpe_api_main;
1833
1834   if (pool_is_free_index (am->arp_events, pool_index))
1835     return 1;
1836
1837   pool_put_index (am->arp_events, pool_index);
1838   return 0;
1839 }
1840
1841 static int
1842 nd_change_delete_callback (u32 pool_index, u8 * notused)
1843 {
1844   vpe_api_main_t *am = &vpe_api_main;
1845
1846   if (pool_is_free_index (am->nd_events, pool_index))
1847     return 1;
1848
1849   pool_put_index (am->nd_events, pool_index);
1850   return 0;
1851 }
1852
1853 static void
1854 vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
1855 {
1856   vpe_api_main_t *am = &vpe_api_main;
1857   vnet_main_t *vnm = vnet_get_main ();
1858   vl_api_want_ip4_arp_events_reply_t *rmp;
1859   vl_api_ip4_arp_event_t *event;
1860   int rv;
1861
1862   if (mp->enable_disable)
1863     {
1864       pool_get (am->arp_events, event);
1865       memset (event, 0, sizeof (*event));
1866
1867       event->_vl_msg_id = ntohs (VL_API_IP4_ARP_EVENT);
1868       event->client_index = mp->client_index;
1869       event->context = mp->context;
1870       event->address = mp->address;
1871       event->pid = mp->pid;
1872       if (mp->address == 0)
1873         event->mac_ip = 1;
1874
1875       rv = vnet_add_del_ip4_arp_change_event
1876         (vnm, arp_change_data_callback,
1877          mp->pid, &mp->address /* addr, in net byte order */ ,
1878          vpe_resolver_process_node.index,
1879          IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
1880     }
1881   else
1882     {
1883       rv = vnet_add_del_ip4_arp_change_event
1884         (vnm, arp_change_delete_callback,
1885          mp->pid, &mp->address /* addr, in net byte order */ ,
1886          vpe_resolver_process_node.index,
1887          IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
1888     }
1889   REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
1890 }
1891
1892 static void
1893 vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
1894 {
1895   vpe_api_main_t *am = &vpe_api_main;
1896   vnet_main_t *vnm = vnet_get_main ();
1897   vl_api_want_ip6_nd_events_reply_t *rmp;
1898   vl_api_ip6_nd_event_t *event;
1899   int rv;
1900
1901   if (mp->enable_disable)
1902     {
1903       pool_get (am->nd_events, event);
1904       memset (event, 0, sizeof (*event));
1905
1906       event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
1907       event->client_index = mp->client_index;
1908       event->context = mp->context;
1909       clib_memcpy (event->address, mp->address, 16);
1910       event->pid = mp->pid;
1911       if (ip6_address_is_zero ((ip6_address_t *) mp->address))
1912         event->mac_ip = 1;
1913
1914       rv = vnet_add_del_ip6_nd_change_event
1915         (vnm, nd_change_data_callback,
1916          mp->pid, mp->address /* addr, in net byte order */ ,
1917          vpe_resolver_process_node.index,
1918          IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
1919     }
1920   else
1921     {
1922       rv = vnet_add_del_ip6_nd_change_event
1923         (vnm, nd_change_delete_callback,
1924          mp->pid, mp->address /* addr, in net byte order */ ,
1925          vpe_resolver_process_node.index,
1926          IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
1927     }
1928   REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
1929 }
1930
1931 static void vl_api_input_acl_set_interface_t_handler
1932   (vl_api_input_acl_set_interface_t * mp)
1933 {
1934   vlib_main_t *vm = vlib_get_main ();
1935   vl_api_input_acl_set_interface_reply_t *rmp;
1936   int rv;
1937   u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
1938
1939   ip4_table_index = ntohl (mp->ip4_table_index);
1940   ip6_table_index = ntohl (mp->ip6_table_index);
1941   l2_table_index = ntohl (mp->l2_table_index);
1942   sw_if_index = ntohl (mp->sw_if_index);
1943
1944   VALIDATE_SW_IF_INDEX (mp);
1945
1946   rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
1947                                  ip6_table_index, l2_table_index, mp->is_add);
1948
1949   BAD_SW_IF_INDEX_LABEL;
1950
1951   REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
1952 }
1953
1954 static void
1955 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
1956 {
1957   int rv = 0;
1958   u8 *vector = 0;
1959   api_main_t *am = &api_main;
1960   vlib_main_t *vm = vlib_get_main ();
1961   void *oldheap;
1962   vl_api_get_node_graph_reply_t *rmp;
1963
1964   pthread_mutex_lock (&am->vlib_rp->mutex);
1965   oldheap = svm_push_data_heap (am->vlib_rp);
1966
1967   /*
1968    * Keep the number of memcpy ops to a minimum (e.g. 1).
1969    */
1970   vec_validate (vector, 16384);
1971   vec_reset_length (vector);
1972
1973   /* $$$$ FIXME */
1974   vector = vlib_node_serialize (&vm->node_main, vector,
1975                                 (u32) ~ 0 /* all threads */ ,
1976                                 1 /* include nexts */ ,
1977                                 1 /* include stats */ );
1978
1979   svm_pop_heap (oldheap);
1980   pthread_mutex_unlock (&am->vlib_rp->mutex);
1981
1982   /* *INDENT-OFF* */
1983   REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
1984   ({
1985     rmp->reply_in_shmem = (uword) vector;
1986   }));
1987   /* *INDENT-ON* */
1988 }
1989
1990 static void
1991 vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1992 {
1993   int rv = 0;
1994   vl_api_ioam_enable_reply_t *rmp;
1995   clib_error_t *error;
1996
1997   /* Ignoring the profile id as currently a single profile
1998    * is supported */
1999   error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
2000                            mp->seqno, mp->analyse);
2001   if (error)
2002     {
2003       clib_error_report (error);
2004       rv = clib_error_get_code (error);
2005     }
2006
2007   REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
2008 }
2009
2010 static void
2011 vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
2012 {
2013   int rv = 0;
2014   vl_api_ioam_disable_reply_t *rmp;
2015   clib_error_t *error;
2016
2017   error = clear_ioam_rewrite_fn ();
2018   if (error)
2019     {
2020       clib_error_report (error);
2021       rv = clib_error_get_code (error);
2022     }
2023
2024   REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
2025 }
2026
2027 static void
2028 vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
2029 {
2030   vl_api_pg_create_interface_reply_t *rmp;
2031   int rv = 0;
2032
2033   pg_main_t *pg = &pg_main;
2034   u32 pg_if_id = pg_interface_add_or_get (pg, ntohl (mp->interface_id));
2035   pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
2036
2037   /* *INDENT-OFF* */
2038   REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
2039   ({
2040     rmp->sw_if_index = ntohl(pi->sw_if_index);
2041   }));
2042   /* *INDENT-ON* */
2043 }
2044
2045 static void
2046 vl_api_pg_capture_t_handler (vl_api_pg_capture_t * mp)
2047 {
2048   vl_api_pg_capture_reply_t *rmp;
2049   int rv = 0;
2050
2051   vnet_main_t *vnm = vnet_get_main ();
2052   vnet_interface_main_t *im = &vnm->interface_main;
2053   vnet_hw_interface_t *hi = 0;
2054
2055   u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
2056   u32 hw_if_index = ~0;
2057   uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
2058   if (p)
2059     hw_if_index = *p;
2060   vec_free (intf_name);
2061
2062   if (hw_if_index != ~0)
2063     {
2064       pg_capture_args_t _a, *a = &_a;
2065
2066       u32 len = ntohl (mp->pcap_name_length);
2067       u8 *pcap_file_name = vec_new (u8, len);
2068       clib_memcpy (pcap_file_name, mp->pcap_file_name, len);
2069
2070       hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
2071       a->hw_if_index = hw_if_index;
2072       a->dev_instance = hi->dev_instance;
2073       a->is_enabled = mp->is_enabled;
2074       a->pcap_file_name = pcap_file_name;
2075       a->count = ntohl (mp->count);
2076
2077       clib_error_t *e = pg_capture (a);
2078       if (e)
2079         {
2080           clib_error_report (e);
2081           rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
2082         }
2083
2084       vec_free (pcap_file_name);
2085     }
2086   REPLY_MACRO (VL_API_PG_CAPTURE_REPLY);
2087 }
2088
2089 static void
2090 vl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t * mp)
2091 {
2092   vl_api_pg_enable_disable_reply_t *rmp;
2093   int rv = 0;
2094
2095   pg_main_t *pg = &pg_main;
2096   u32 stream_index = ~0;
2097
2098   int is_enable = mp->is_enabled != 0;
2099   u32 len = ntohl (mp->stream_name_length) - 1;
2100
2101   if (len > 0)
2102     {
2103       u8 *stream_name = vec_new (u8, len);
2104       clib_memcpy (stream_name, mp->stream_name, len);
2105       uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
2106       if (p)
2107         stream_index = *p;
2108       vec_free (stream_name);
2109     }
2110
2111   pg_enable_disable (stream_index, is_enable);
2112
2113   REPLY_MACRO (VL_API_PG_ENABLE_DISABLE_REPLY);
2114 }
2115
2116 static void
2117   vl_api_ip_source_and_port_range_check_add_del_t_handler
2118   (vl_api_ip_source_and_port_range_check_add_del_t * mp)
2119 {
2120   vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
2121   int rv = 0;
2122
2123   u8 is_ipv6 = mp->is_ipv6;
2124   u8 is_add = mp->is_add;
2125   u8 mask_length = mp->mask_length;
2126   ip4_address_t ip4_addr;
2127   ip6_address_t ip6_addr;
2128   u16 *low_ports = 0;
2129   u16 *high_ports = 0;
2130   u32 vrf_id;
2131   u16 tmp_low, tmp_high;
2132   u8 num_ranges;
2133   int i;
2134
2135   // Validate port range
2136   num_ranges = mp->number_of_ranges;
2137   if (num_ranges > 32)
2138     {                           // This is size of array in VPE.API
2139       rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
2140       goto reply;
2141     }
2142
2143   vec_reset_length (low_ports);
2144   vec_reset_length (high_ports);
2145
2146   for (i = 0; i < num_ranges; i++)
2147     {
2148       tmp_low = mp->low_ports[i];
2149       tmp_high = mp->high_ports[i];
2150       // If tmp_low <= tmp_high then only need to check tmp_low = 0
2151       // If tmp_low <= tmp_high then only need to check tmp_high > 65535
2152       if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
2153         {
2154           rv = VNET_API_ERROR_INVALID_VALUE;
2155           goto reply;
2156         }
2157       vec_add1 (low_ports, tmp_low);
2158       vec_add1 (high_ports, tmp_high + 1);
2159     }
2160
2161   // Validate mask_length
2162   if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
2163     {
2164       rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
2165       goto reply;
2166     }
2167
2168   vrf_id = ntohl (mp->vrf_id);
2169
2170   if (vrf_id < 1)
2171     {
2172       rv = VNET_API_ERROR_INVALID_VALUE;
2173       goto reply;
2174     }
2175
2176
2177   if (is_ipv6)
2178     {
2179       clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
2180       rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
2181                                                     mask_length,
2182                                                     vrf_id,
2183                                                     low_ports,
2184                                                     high_ports, is_add);
2185     }
2186   else
2187     {
2188       clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
2189       rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
2190                                                     mask_length,
2191                                                     vrf_id,
2192                                                     low_ports,
2193                                                     high_ports, is_add);
2194     }
2195
2196 reply:
2197   vec_free (low_ports);
2198   vec_free (high_ports);
2199   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
2200 }
2201
2202 static void
2203   vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
2204   (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
2205 {
2206   vlib_main_t *vm = vlib_get_main ();
2207   vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
2208   ip4_main_t *im = &ip4_main;
2209   int rv;
2210   u32 sw_if_index;
2211   u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2212   u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2213   uword *p = 0;
2214   int i;
2215
2216   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
2217     ntohl (mp->tcp_out_vrf_id);
2218   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
2219     ntohl (mp->udp_out_vrf_id);
2220   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
2221     ntohl (mp->tcp_in_vrf_id);
2222   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
2223     ntohl (mp->udp_in_vrf_id);
2224
2225
2226   for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
2227     {
2228       if (vrf_id[i] != 0 && vrf_id[i] != ~0)
2229         {
2230           p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
2231
2232           if (p == 0)
2233             {
2234               rv = VNET_API_ERROR_INVALID_VALUE;
2235               goto reply;
2236             }
2237
2238           fib_index[i] = p[0];
2239         }
2240       else
2241         fib_index[i] = ~0;
2242     }
2243   sw_if_index = ntohl (mp->sw_if_index);
2244
2245   VALIDATE_SW_IF_INDEX (mp);
2246
2247   rv =
2248     set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
2249                                         mp->is_add);
2250
2251   BAD_SW_IF_INDEX_LABEL;
2252 reply:
2253
2254   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
2255 }
2256
2257 static void
2258 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
2259 {
2260   vl_api_delete_subif_reply_t *rmp;
2261   int rv;
2262
2263   rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
2264
2265   REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
2266 }
2267
2268 static void
2269 vl_api_punt_t_handler (vl_api_punt_t * mp)
2270 {
2271   vl_api_punt_reply_t *rmp;
2272   vlib_main_t *vm = vlib_get_main ();
2273   int rv = 0;
2274   clib_error_t *error;
2275
2276   error = vnet_punt_add_del (vm, mp->ipv, mp->l4_protocol,
2277                              ntohs (mp->l4_port), mp->is_add);
2278   if (error)
2279     {
2280       rv = -1;
2281       clib_error_report (error);
2282     }
2283
2284   REPLY_MACRO (VL_API_PUNT_REPLY);
2285 }
2286
2287 static void
2288 vl_api_feature_enable_disable_t_handler (vl_api_feature_enable_disable_t * mp)
2289 {
2290   vl_api_feature_enable_disable_reply_t *rmp;
2291   int rv = 0;
2292   u8 *arc_name, *feature_name;
2293
2294   VALIDATE_SW_IF_INDEX (mp);
2295
2296   arc_name = format (0, "%s%c", mp->arc_name, 0);
2297   feature_name = format (0, "%s%c", mp->feature_name, 0);
2298
2299   vnet_feature_registration_t *reg;
2300   reg =
2301     vnet_get_feature_reg ((const char *) arc_name,
2302                           (const char *) feature_name);
2303   if (reg == 0)
2304     rv = VNET_API_ERROR_INVALID_VALUE;
2305   else
2306     {
2307       u32 sw_if_index;
2308       clib_error_t *error = 0;
2309
2310       sw_if_index = ntohl (mp->sw_if_index);
2311       if (reg->enable_disable_cb)
2312         error = reg->enable_disable_cb (sw_if_index, mp->enable);
2313       if (!error)
2314         vnet_feature_enable_disable ((const char *) arc_name,
2315                                      (const char *) feature_name,
2316                                      sw_if_index, mp->enable, 0, 0);
2317       else
2318         {
2319           clib_error_report (error);
2320           rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
2321         }
2322     }
2323
2324   vec_free (feature_name);
2325   vec_free (arc_name);
2326
2327   BAD_SW_IF_INDEX_LABEL;
2328
2329   REPLY_MACRO (VL_API_FEATURE_ENABLE_DISABLE_REPLY);
2330 }
2331
2332 #define BOUNCE_HANDLER(nn)                                              \
2333 static void vl_api_##nn##_t_handler (                                   \
2334     vl_api_##nn##_t *mp)                                                \
2335 {                                                                       \
2336     vpe_client_registration_t *reg;                                     \
2337     vpe_api_main_t * vam = &vpe_api_main;                               \
2338     unix_shared_memory_queue_t * q;                                     \
2339                                                                         \
2340     /* One registration only... */                                      \
2341     pool_foreach(reg, vam->nn##_registrations,                          \
2342     ({                                                                  \
2343         q = vl_api_client_index_to_input_queue (reg->client_index);     \
2344         if (q) {                                                        \
2345             /*                                                          \
2346              * If the queue is stuffed, turf the msg and complain       \
2347              * It's unlikely that the intended recipient is             \
2348              * alive; avoid deadlock at all costs.                      \
2349              */                                                         \
2350             if (q->cursize == q->maxsize) {                             \
2351                 clib_warning ("ERROR: receiver queue full, drop msg");  \
2352                 vl_msg_api_free (mp);                                   \
2353                 return;                                                 \
2354             }                                                           \
2355             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
2356             return;                                                     \
2357         }                                                               \
2358     }));                                                                \
2359     vl_msg_api_free (mp);                                               \
2360 }
2361
2362 static void setup_message_id_table (api_main_t * am);
2363
2364 /*
2365  * vpe_api_hookup
2366  * Add vpe's API message handlers to the table.
2367  * vlib has alread mapped shared memory and
2368  * added the client registration handlers.
2369  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
2370  */
2371 static clib_error_t *
2372 vpe_api_hookup (vlib_main_t * vm)
2373 {
2374   api_main_t *am = &api_main;
2375
2376 #define _(N,n)                                                  \
2377     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
2378                            vl_api_##n##_t_handler,              \
2379                            vl_noop_handler,                     \
2380                            vl_api_##n##_t_endian,               \
2381                            vl_api_##n##_t_print,                \
2382                            sizeof(vl_api_##n##_t), 1);
2383   foreach_vpe_api_msg;
2384 #undef _
2385
2386   /*
2387    * Trace space for classifier mask+match
2388    */
2389   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
2390   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size
2391     += 5 * sizeof (u32x4);
2392   am->api_trace_cfg[VL_API_VXLAN_ADD_DEL_TUNNEL].size += 16 * sizeof (u32);
2393
2394   /*
2395    * Thread-safe API messages
2396    */
2397   am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
2398   am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
2399
2400   /*
2401    * Set up the (msg_name, crc, message-id) table
2402    */
2403   setup_message_id_table (am);
2404
2405   return 0;
2406 }
2407
2408 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
2409
2410 static clib_error_t *
2411 vpe_api_init (vlib_main_t * vm)
2412 {
2413   vpe_api_main_t *am = &vpe_api_main;
2414
2415   am->vlib_main = vm;
2416   am->vnet_main = vnet_get_main ();
2417   am->interface_events_registration_hash = hash_create (0, sizeof (uword));
2418   am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
2419   am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
2420   am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
2421   am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
2422   am->oam_events_registration_hash = hash_create (0, sizeof (uword));
2423   am->bfd_events_registration_hash = hash_create (0, sizeof (uword));
2424
2425   vl_api_init (vm);
2426   vl_set_memory_region_name ("/vpe-api");
2427   vl_enable_disable_memory_api (vm, 1 /* enable it */ );
2428
2429   return 0;
2430 }
2431
2432 VLIB_INIT_FUNCTION (vpe_api_init);
2433
2434
2435 static clib_error_t *
2436 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
2437 {
2438   u8 *chroot_path;
2439   u64 baseva, size, pvt_heap_size;
2440   int uid, gid, rv;
2441   const int max_buf_size = 4096;
2442   char *s, *buf;
2443   struct passwd _pw, *pw;
2444   struct group _grp, *grp;
2445   clib_error_t *e;
2446   buf = vec_new (char, 128);
2447   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2448     {
2449       if (unformat (input, "prefix %s", &chroot_path))
2450         {
2451           vec_add1 (chroot_path, 0);
2452           vl_set_memory_root_path ((char *) chroot_path);
2453         }
2454       else if (unformat (input, "uid %d", &uid))
2455         vl_set_memory_uid (uid);
2456       else if (unformat (input, "gid %d", &gid))
2457         vl_set_memory_gid (gid);
2458       else if (unformat (input, "baseva %llx", &baseva))
2459         vl_set_global_memory_baseva (baseva);
2460       else if (unformat (input, "global-size %lldM", &size))
2461         vl_set_global_memory_size (size * (1ULL << 20));
2462       else if (unformat (input, "global-size %lldG", &size))
2463         vl_set_global_memory_size (size * (1ULL << 30));
2464       else if (unformat (input, "global-size %lld", &size))
2465         vl_set_global_memory_size (size);
2466       else if (unformat (input, "global-pvt-heap-size %lldM", &pvt_heap_size))
2467         vl_set_global_pvt_heap_size (pvt_heap_size * (1ULL << 20));
2468       else if (unformat (input, "global-pvt-heap-size size %lld",
2469                          &pvt_heap_size))
2470         vl_set_global_pvt_heap_size (pvt_heap_size);
2471       else if (unformat (input, "api-pvt-heap-size %lldM", &pvt_heap_size))
2472         vl_set_api_pvt_heap_size (pvt_heap_size * (1ULL << 20));
2473       else if (unformat (input, "api-pvt-heap-size size %lld",
2474                          &pvt_heap_size))
2475         vl_set_api_pvt_heap_size (pvt_heap_size);
2476       else if (unformat (input, "api-size %lldM", &size))
2477         vl_set_api_memory_size (size * (1ULL << 20));
2478       else if (unformat (input, "api-size %lldG", &size))
2479         vl_set_api_memory_size (size * (1ULL << 30));
2480       else if (unformat (input, "api-size %lld", &size))
2481         vl_set_api_memory_size (size);
2482       else if (unformat (input, "uid %s", &s))
2483         {
2484           /* lookup the username */
2485           pw = NULL;
2486           while (((rv =
2487                    getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
2488                  && (vec_len (buf) <= max_buf_size))
2489             {
2490               vec_resize (buf, vec_len (buf) * 2);
2491             }
2492           if (rv < 0)
2493             {
2494               e = clib_error_return_code (0, rv,
2495                                           CLIB_ERROR_ERRNO_VALID |
2496                                           CLIB_ERROR_FATAL,
2497                                           "cannot fetch username %s", s);
2498               vec_free (s);
2499               vec_free (buf);
2500               return e;
2501             }
2502           if (pw == NULL)
2503             {
2504               e =
2505                 clib_error_return_fatal (0, "username %s does not exist", s);
2506               vec_free (s);
2507               vec_free (buf);
2508               return e;
2509             }
2510           vec_free (s);
2511           vl_set_memory_uid (pw->pw_uid);
2512         }
2513       else if (unformat (input, "gid %s", &s))
2514         {
2515           /* lookup the group name */
2516           grp = NULL;
2517           while (((rv =
2518                    getgrnam_r (s, &_grp, buf, vec_len (buf), &grp)) == ERANGE)
2519                  && (vec_len (buf) <= max_buf_size))
2520             {
2521               vec_resize (buf, vec_len (buf) * 2);
2522             }
2523           if (rv != 0)
2524             {
2525               e = clib_error_return_code (0, rv,
2526                                           CLIB_ERROR_ERRNO_VALID |
2527                                           CLIB_ERROR_FATAL,
2528                                           "cannot fetch group %s", s);
2529               vec_free (s);
2530               vec_free (buf);
2531               return e;
2532             }
2533           if (grp == NULL)
2534             {
2535               e = clib_error_return_fatal (0, "group %s does not exist", s);
2536               vec_free (s);
2537               vec_free (buf);
2538               return e;
2539             }
2540           vec_free (s);
2541           vec_free (buf);
2542           vl_set_memory_gid (grp->gr_gid);
2543         }
2544       else
2545         return clib_error_return (0, "unknown input `%U'",
2546                                   format_unformat_error, input);
2547     }
2548   return 0;
2549 }
2550
2551 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
2552
2553 void *
2554 get_unformat_vnet_sw_interface (void)
2555 {
2556   return (void *) &unformat_vnet_sw_interface;
2557 }
2558
2559 static u8 *
2560 format_arp_event (u8 * s, va_list * args)
2561 {
2562   vl_api_ip4_arp_event_t *event = va_arg (*args, vl_api_ip4_arp_event_t *);
2563
2564   s = format (s, "pid %d: ", event->pid);
2565   if (event->mac_ip)
2566     s = format (s, "bd mac/ip4 binding events");
2567   else
2568     s = format (s, "resolution for %U", format_ip4_address, &event->address);
2569   return s;
2570 }
2571
2572 static u8 *
2573 format_nd_event (u8 * s, va_list * args)
2574 {
2575   vl_api_ip6_nd_event_t *event = va_arg (*args, vl_api_ip6_nd_event_t *);
2576
2577   s = format (s, "pid %d: ", event->pid);
2578   if (event->mac_ip)
2579     s = format (s, "bd mac/ip6 binding events");
2580   else
2581     s = format (s, "resolution for %U", format_ip6_address, event->address);
2582   return s;
2583 }
2584
2585 static clib_error_t *
2586 show_ip_arp_nd_events_fn (vlib_main_t * vm,
2587                           unformat_input_t * input, vlib_cli_command_t * cmd)
2588 {
2589   vpe_api_main_t *am = &vpe_api_main;
2590   vl_api_ip4_arp_event_t *arp_event;
2591   vl_api_ip6_nd_event_t *nd_event;
2592
2593   if ((pool_elts (am->arp_events) == 0) && (pool_elts (am->nd_events) == 0))
2594     {
2595       vlib_cli_output (vm, "No active arp or nd event registrations");
2596       return 0;
2597     }
2598
2599   /* *INDENT-OFF* */
2600   pool_foreach (arp_event, am->arp_events,
2601   ({
2602     vlib_cli_output (vm, "%U", format_arp_event, arp_event);
2603   }));
2604
2605   pool_foreach (nd_event, am->nd_events,
2606   ({
2607     vlib_cli_output (vm, "%U", format_nd_event, nd_event);
2608   }));
2609   /* *INDENT-ON* */
2610
2611   return 0;
2612 }
2613
2614 /* *INDENT-OFF* */
2615 VLIB_CLI_COMMAND (show_ip_arp_nd_events, static) = {
2616   .path = "show arp-nd-event registrations",
2617   .function = show_ip_arp_nd_events_fn,
2618   .short_help = "Show ip4 arp and ip6 nd event registrations",
2619 };
2620 /* *INDENT-ON* */
2621
2622 #define vl_msg_name_crc_list
2623 #include <vpp/api/vpe_all_api_h.h>
2624 #undef vl_msg_name_crc_list
2625
2626 static void
2627 setup_message_id_table (api_main_t * am)
2628 {
2629 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
2630   foreach_vl_msg_name_crc_memclnt;
2631   foreach_vl_msg_name_crc_vpe;
2632 #undef _
2633 }
2634
2635
2636 /*
2637  * fd.io coding-style-patch-verification: ON
2638  *
2639  * Local Variables:
2640  * eval: (c-set-style "gnu")
2641  * End:
2642  */