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