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