Add LISP RTR support
[vpp.git] / vpp / api / api.c
1 /*
2  *------------------------------------------------------------------
3  * api.c - message handler registration
4  * 
5  * Copyright (c) 2010 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 <vppinfra/clib.h>
34 #include <vppinfra/vec.h>
35 #include <vppinfra/hash.h>
36 #include <vppinfra/bitmap.h>
37 #include <vppinfra/fifo.h>
38 #include <vppinfra/time.h>
39 #include <vppinfra/mheap.h>
40 #include <vppinfra/heap.h>
41 #include <vppinfra/pool.h>
42 #include <vppinfra/format.h>
43 #include <vppinfra/error.h>
44
45 #include <vnet/api_errno.h> // alagalah TODO : committers please pay note, is this ok?
46 #include <vnet/vnet.h>
47 #include <vnet/l2/l2_input.h>
48 #include <vnet/l2/l2_bd.h>
49 #include <vnet/l2tp/l2tp.h>
50 #include <vnet/ip/ip.h>
51 #include <vnet/unix/tuntap.h>
52 #include <vnet/unix/tapcli.h>
53 #include <vnet/mpls-gre/mpls.h>
54 #include <vnet/dhcp/proxy.h>
55 #include <vnet/dhcp/client.h>
56 #if IPV6SR > 0
57 #include <vnet/sr/sr.h>
58 #endif
59 #include <vnet/dhcpv6/proxy.h>
60 #include <vlib/vlib.h>
61 #include <vlib/unix/unix.h>
62 #include <vlibapi/api.h>
63 #include <vlibmemory/api.h>
64 #include <vnet/classify/vnet_classify.h>
65 #include <vnet/classify/input_acl.h>
66 #include <vnet/l2/l2_classify.h>
67 #include <vnet/vxlan/vxlan.h>
68 #include <vnet/gre/gre.h>
69 #include <vnet/l2/l2_vtr.h>
70 #include <vnet/vxlan-gpe/vxlan_gpe.h>
71 #include <vnet/lisp-gpe/lisp_gpe.h>
72 #include <vnet/lisp-cp/control.h>
73 #include <vnet/map/map.h>
74 #include <vnet/cop/cop.h>
75 #include <vnet/ip/ip6_hop_by_hop.h>
76 #include <vnet/devices/af_packet/af_packet.h>
77 #include <vnet/policer/policer.h>
78 #include <vnet/devices/netmap/netmap.h>
79
80 #undef BIHASH_TYPE
81 #undef __included_bihash_template_h__
82 #include <vnet/l2/l2_fib.h>
83
84 #if IPSEC > 0
85 #include <vnet/ipsec/ipsec.h>
86 #include <vnet/ipsec/ikev2.h>
87 #endif /* IPSEC */
88 #if DPDK > 0
89 #include <vnet/devices/virtio/vhost-user.h>
90 #endif
91
92 #include <stats/stats.h>
93 #include <oam/oam.h>
94
95 #include <vnet/ethernet/ethernet.h>
96 #include <vnet/ethernet/arp_packet.h>
97 #include <vnet/interface.h>
98
99 #include <vnet/l2/l2_fib.h>
100 #include <vnet/l2/l2_bd.h>
101 #include <api/vpe_msg_enum.h>
102
103 #define f64_endian(a)
104 #define f64_print(a,b)
105
106 #define vl_typedefs             /* define message structures */
107 #include <api/vpe_all_api_h.h> 
108 #undef vl_typedefs
109
110 #define vl_endianfun             /* define message structures */
111 #include <api/vpe_all_api_h.h> 
112 #undef vl_endianfun
113
114 /* instantiate all the print functions we know about */
115 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
116 #define vl_printfun
117 #include <api/vpe_all_api_h.h>
118 #undef vl_printfun
119
120 #define REPLY_MACRO(t)                                          \
121 do {                                                            \
122     unix_shared_memory_queue_t * q;                             \
123     rv = vl_msg_api_pd_handler (mp, rv);                        \
124     q = vl_api_client_index_to_input_queue (mp->client_index);  \
125     if (!q)                                                     \
126         return;                                                 \
127                                                                 \
128     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
129     rmp->_vl_msg_id = ntohs((t));                               \
130     rmp->context = mp->context;                                 \
131     rmp->retval = ntohl(rv);                                    \
132                                                                 \
133     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
134 } while(0);
135
136 #define REPLY_MACRO2(t, body)                                   \
137 do {                                                            \
138     unix_shared_memory_queue_t * q;                             \
139     rv = vl_msg_api_pd_handler (mp, rv);                        \
140     q = vl_api_client_index_to_input_queue (mp->client_index);  \
141     if (!q)                                                     \
142         return;                                                 \
143                                                                 \
144     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
145     rmp->_vl_msg_id = ntohs((t));                               \
146     rmp->context = mp->context;                                 \
147     rmp->retval = ntohl(rv);                                    \
148     do {body;} while (0);                                       \
149     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
150 } while(0);
151
152 #if (1 || CLIB_DEBUG > 0)       /* "trust, but verify" */
153
154 #define VALIDATE_SW_IF_INDEX(mp)                                \
155  do { u32 __sw_if_index = ntohl(mp->sw_if_index);               \
156     vnet_main_t *__vnm = vnet_get_main();                       \
157     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
158                            __sw_if_index)) {                    \
159         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
160         goto bad_sw_if_index;                                   \
161     }                                                           \
162 } while(0);
163
164 #define BAD_SW_IF_INDEX_LABEL                   \
165 do {                                            \
166 bad_sw_if_index:                                \
167     ;                                           \
168 } while (0);
169
170 #define VALIDATE_RX_SW_IF_INDEX(mp)                             \
171  do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index);         \
172     vnet_main_t *__vnm = vnet_get_main();                       \
173     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
174                            __rx_sw_if_index)) {                 \
175         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
176         goto bad_rx_sw_if_index;                                \
177     }                                                           \
178 } while(0);
179
180 #define BAD_RX_SW_IF_INDEX_LABEL                \
181 do {                                            \
182 bad_rx_sw_if_index:                             \
183     ;                                           \
184 } while (0);
185
186 #define VALIDATE_TX_SW_IF_INDEX(mp)                             \
187  do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index);         \
188     vnet_main_t *__vnm = vnet_get_main();                       \
189     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
190                            __tx_sw_if_index)) {                 \
191         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
192         goto bad_tx_sw_if_index;                                \
193     }                                                           \
194 } while(0);
195
196 #define BAD_TX_SW_IF_INDEX_LABEL                \
197 do {                                            \
198 bad_tx_sw_if_index:                             \
199     ;                                           \
200 } while (0);
201
202 #else
203
204 #define VALIDATE_SW_IF_INDEX(mp)
205 #define BAD_SW_IF_INDEX_LABEL
206 #define VALIDATE_RX_SW_IF_INDEX(mp)
207 #define BAD_RX_SW_IF_INDEX_LABEL
208 #define VALIDATE_TX_SW_IF_INDEX(mp)
209 #define BAD_TX_SW_IF_INDEX_LABEL
210
211 #endif  /* CLIB_DEBUG > 0 */
212
213 #define foreach_vpe_api_msg                                             \
214 _(WANT_INTERFACE_EVENTS, want_interface_events)                         \
215 _(WANT_OAM_EVENTS, want_oam_events)                                     \
216 _(OAM_ADD_DEL, oam_add_del)                                             \
217 _(SW_INTERFACE_DUMP, sw_interface_dump)                                 \
218 _(SW_INTERFACE_DETAILS, sw_interface_details)                           \
219 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags)                       \
220 _(IP_ADD_DEL_ROUTE, ip_add_del_route)                                   \
221 _(IS_ADDRESS_REACHABLE, is_address_reachable)                           \
222 _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address)           \
223 _(SW_INTERFACE_SET_TABLE, sw_interface_set_table)                       \
224 _(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath)                       \
225 _(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect)           \
226 _(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge)               \
227 _(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del)                         \
228 _(BRIDGE_DOMAIN_DUMP, bridge_domain_dump)                               \
229 _(BRIDGE_DOMAIN_DETAILS, bridge_domain_details)                         \
230 _(BRIDGE_DOMAIN_SW_IF_DETAILS, bridge_domain_sw_if_details)             \
231 _(L2FIB_ADD_DEL, l2fib_add_del)                                         \
232 _(L2_FLAGS, l2_flags)                                                   \
233 _(BRIDGE_FLAGS, bridge_flags)                                           \
234 _(TAP_CONNECT, tap_connect)                                             \
235 _(TAP_MODIFY, tap_modify)                                               \
236 _(TAP_DELETE, tap_delete)                                               \
237 _(SW_INTERFACE_TAP_DUMP, sw_interface_tap_dump)                         \
238 _(CREATE_VLAN_SUBIF, create_vlan_subif)                                 \
239 _(CREATE_SUBIF, create_subif)                                           \
240 _(MPLS_GRE_ADD_DEL_TUNNEL, mpls_gre_add_del_tunnel)                     \
241 _(MPLS_ETHERNET_ADD_DEL_TUNNEL, mpls_ethernet_add_del_tunnel)           \
242 _(MPLS_ETHERNET_ADD_DEL_TUNNEL_2, mpls_ethernet_add_del_tunnel_2)       \
243 _(MPLS_ADD_DEL_ENCAP, mpls_add_del_encap)                               \
244 _(MPLS_ADD_DEL_DECAP, mpls_add_del_decap)                               \
245 _(PROXY_ARP_ADD_DEL, proxy_arp_add_del)                                 \
246 _(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable)       \
247 _(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del)                             \
248 _(VNET_GET_SUMMARY_STATS, vnet_get_summary_stats)                       \
249 _(RESET_FIB, reset_fib)                                                 \
250 _(DHCP_PROXY_CONFIG,dhcp_proxy_config)                                  \
251 _(DHCP_PROXY_CONFIG_2,dhcp_proxy_config_2)                              \
252 _(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss)                                \
253 _(DHCP_CLIENT_CONFIG, dhcp_client_config)                               \
254 _(SET_IP_FLOW_HASH,set_ip_flow_hash)                                    \
255 _(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config)           \
256 _(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix)           \
257 _(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable )    \
258 _(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS,                              \
259   sw_interface_ip6_set_link_local_address)                              \
260 _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered)             \
261 _(CREATE_LOOPBACK, create_loopback)                                     \
262 _(CONTROL_PING, control_ping)                                           \
263 _(CLI_REQUEST, cli_request)                                             \
264 _(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit)                       \
265 _(L2_PATCH_ADD_DEL, l2_patch_add_del)                                   \
266 _(CLASSIFY_ADD_DEL_TABLE, classify_add_del_table)                       \
267 _(CLASSIFY_ADD_DEL_SESSION, classify_add_del_session)                   \
268 _(CLASSIFY_SET_INTERFACE_IP_TABLE, classify_set_interface_ip_table)     \
269 _(CLASSIFY_SET_INTERFACE_L2_TABLES, classify_set_interface_l2_tables)   \
270 _(GET_NODE_INDEX, get_node_index)                                       \
271 _(ADD_NODE_NEXT, add_node_next)                                         \
272 _(L2TPV3_CREATE_TUNNEL, l2tpv3_create_tunnel)                           \
273 _(L2TPV3_SET_TUNNEL_COOKIES, l2tpv3_set_tunnel_cookies)                 \
274 _(L2TPV3_INTERFACE_ENABLE_DISABLE, l2tpv3_interface_enable_disable)     \
275 _(L2TPV3_SET_LOOKUP_KEY, l2tpv3_set_lookup_key)                         \
276 _(SW_IF_L2TPV3_TUNNEL_DUMP, sw_if_l2tpv3_tunnel_dump)                   \
277 _(VXLAN_ADD_DEL_TUNNEL, vxlan_add_del_tunnel)                           \
278 _(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump)                                 \
279 _(GRE_ADD_DEL_TUNNEL, gre_add_del_tunnel)                               \
280 _(GRE_TUNNEL_DUMP, gre_tunnel_dump)                                     \
281 _(L2_FIB_CLEAR_TABLE, l2_fib_clear_table)                               \
282 _(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter)                     \
283 _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite)         \
284 _(CREATE_VHOST_USER_IF, create_vhost_user_if)                           \
285 _(MODIFY_VHOST_USER_IF, modify_vhost_user_if)                           \
286 _(DELETE_VHOST_USER_IF, delete_vhost_user_if)                           \
287 _(SW_INTERFACE_VHOST_USER_DUMP, sw_interface_vhost_user_dump)           \
288 _(IP_ADDRESS_DUMP, ip_address_dump)                                     \
289 _(IP_DUMP, ip_dump)                                                     \
290 _(SW_INTERFACE_VHOST_USER_DETAILS, sw_interface_vhost_user_details)     \
291 _(SHOW_VERSION, show_version)                                           \
292 _(L2_FIB_TABLE_DUMP, l2_fib_table_dump)                                 \
293 _(L2_FIB_TABLE_ENTRY, l2_fib_table_entry)                               \
294 _(VXLAN_GPE_ADD_DEL_TUNNEL, vxlan_gpe_add_del_tunnel)                   \
295 _(INTERFACE_NAME_RENUMBER, interface_name_renumber)                     \
296 _(WANT_IP4_ARP_EVENTS, want_ip4_arp_events)                             \
297 _(INPUT_ACL_SET_INTERFACE, input_acl_set_interface)                     \
298 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del)                                 \
299 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd)             \
300 _(IPSEC_SPD_ADD_DEL_ENTRY, ipsec_spd_add_del_entry)                     \
301 _(IPSEC_SAD_ADD_DEL_ENTRY, ipsec_sad_add_del_entry)                     \
302 _(IPSEC_SA_SET_KEY, ipsec_sa_set_key)                                   \
303 _(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del)                         \
304 _(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth)                       \
305 _(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id)                           \
306 _(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts)                           \
307 _(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key)                             \
308 _(DELETE_LOOPBACK, delete_loopback)                                     \
309 _(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del)                                 \
310 _(MAP_ADD_DOMAIN, map_add_domain)                                       \
311 _(MAP_DEL_DOMAIN, map_del_domain)                                       \
312 _(MAP_ADD_DEL_RULE, map_add_del_rule)                                   \
313 _(MAP_DOMAIN_DUMP, map_domain_dump)                                     \
314 _(MAP_RULE_DUMP, map_rule_dump)                                         \
315 _(MAP_SUMMARY_STATS, map_summary_stats)                                 \
316 _(COP_INTERFACE_ENABLE_DISABLE, cop_interface_enable_disable)           \
317 _(COP_WHITELIST_ENABLE_DISABLE, cop_whitelist_enable_disable)           \
318 _(GET_NODE_GRAPH, get_node_graph)                                       \
319 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats)                   \
320 _(TRACE_PROFILE_ADD, trace_profile_add)                                 \
321 _(TRACE_PROFILE_APPLY, trace_profile_apply)                             \
322 _(TRACE_PROFILE_DEL, trace_profile_del)                                 \
323 _(LISP_ADD_DEL_LOCATOR_SET, lisp_add_del_locator_set)                   \
324 _(LISP_ADD_DEL_LOCATOR, lisp_add_del_locator)                           \
325 _(LISP_ADD_DEL_LOCAL_EID, lisp_add_del_local_eid)                       \
326 _(LISP_GPE_ADD_DEL_FWD_ENTRY, lisp_gpe_add_del_fwd_entry)               \
327 _(LISP_ADD_DEL_MAP_RESOLVER, lisp_add_del_map_resolver)                 \
328 _(LISP_GPE_ENABLE_DISABLE, lisp_gpe_enable_disable)                     \
329 _(LISP_ENABLE_DISABLE, lisp_enable_disable)                             \
330 _(LISP_GPE_ADD_DEL_IFACE, lisp_gpe_add_del_iface)                       \
331 _(LISP_ADD_DEL_REMOTE_MAPPING, lisp_add_del_remote_mapping)             \
332 _(LISP_PITR_SET_LOCATOR_SET, lisp_pitr_set_locator_set)                 \
333 _(LISP_LOCATOR_SET_DUMP, lisp_locator_set_dump)                         \
334 _(LISP_LOCAL_EID_TABLE_DUMP, lisp_local_eid_table_dump)                 \
335 _(LISP_GPE_TUNNEL_DUMP, lisp_gpe_tunnel_dump)                           \
336 _(LISP_MAP_RESOLVER_DUMP, lisp_map_resolver_dump)                       \
337 _(LISP_ENABLE_DISABLE_STATUS_DUMP,                                      \
338   lisp_enable_disable_status_dump)                                      \
339 _(SR_MULTICAST_MAP_ADD_DEL, sr_multicast_map_add_del)                   \
340 _(AF_PACKET_CREATE, af_packet_create)                                   \
341 _(AF_PACKET_DELETE, af_packet_delete)                                   \
342 _(POLICER_ADD_DEL, policer_add_del)                                     \
343 _(POLICER_DUMP, policer_dump)                                           \
344 _(NETMAP_CREATE, netmap_create)                                         \
345 _(NETMAP_DELETE, netmap_delete)
346
347 #define QUOTE_(x) #x
348 #define QUOTE(x) QUOTE_(x)
349
350 #define foreach_registration_hash               \
351 _(interface_events)                             \
352 _(to_netconf_server)                            \
353 _(from_netconf_server)                          \
354 _(to_netconf_client)                            \
355 _(from_netconf_client)                          \
356 _(oam_events)
357
358 typedef enum {
359     RESOLVE_IP4_ADD_DEL_ROUTE=1,
360     RESOLVE_IP6_ADD_DEL_ROUTE,
361     RESOLVE_MPLS_ETHERNET_ADD_DEL,
362 } resolve_t;
363
364 typedef struct {
365     u8 resolve_type;
366     union {
367         vl_api_ip_add_del_route_t r;
368         vl_api_mpls_ethernet_add_del_tunnel_2_t t;
369     };
370 } pending_route_t;
371
372 typedef struct {
373
374 #define _(a) uword *a##_registration_hash;              \
375     vpe_client_registration_t * a##_registrations;
376 foreach_registration_hash
377 #undef _
378
379     /* notifications happen really early in the game */
380     u8 link_state_process_up;
381
382     /* ip4 pending route adds */
383     pending_route_t * pending_routes;
384
385     /* ip4 arp event registration pool */
386     vl_api_ip4_arp_event_t * arp_events;
387
388     /* convenience */
389     vlib_main_t * vlib_main;
390     vnet_main_t * vnet_main;
391 } vpe_api_main_t;
392
393 static vlib_node_registration_t vpe_resolver_process_node;
394 static vpe_api_main_t vpe_api_main;
395
396 static void send_sw_interface_flags (vpe_api_main_t * am,
397                                      unix_shared_memory_queue_t *q,
398                                      vnet_sw_interface_t * swif);
399 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
400                                      unix_shared_memory_queue_t *q,
401                                      u32 sw_if_index);
402
403 static int arp_change_delete_callback (u32 pool_index, u8 * notused);
404
405
406 /* Clean up all registrations belonging to the indicated client */
407 int vl_api_memclnt_delete_callback (u32 client_index) 
408 {
409     vpe_api_main_t * vam = &vpe_api_main;
410     vpe_client_registration_t *rp;
411     uword * p;
412     int stats_memclnt_delete_callback (u32 client_index);
413
414     stats_memclnt_delete_callback (client_index);
415
416 #define _(a)                                                    \
417     p = hash_get (vam->a##_registration_hash, client_index);    \
418     if (p) {                                                    \
419         rp = pool_elt_at_index (vam->a##_registrations, p[0]);  \
420         pool_put (vam->a##_registrations, rp);                  \
421         hash_unset (vam->a##_registration_hash, client_index);  \
422     }
423     foreach_registration_hash;
424 #undef _
425     return 0;
426 }
427
428 #define API_LINK_STATE_EVENT 1
429 #define API_ADMIN_UP_DOWN_EVENT 2
430
431 static int
432 event_data_cmp (void * a1, void * a2)
433 {
434   uword * e1 = a1;
435   uword * e2 = a2;
436
437   return (word) e1[0] - (word) e2[0];
438 }
439
440 static uword
441 link_state_process (vlib_main_t * vm,
442                     vlib_node_runtime_t * rt,
443                     vlib_frame_t * f)
444 {
445     vpe_api_main_t * vam = &vpe_api_main;
446     vnet_main_t * vnm = vam->vnet_main;
447     vnet_sw_interface_t * swif;
448     uword * event_data = 0;
449     vpe_client_registration_t *reg;
450     int i;
451     u32 prev_sw_if_index;
452     unix_shared_memory_queue_t * q;
453
454     vam->link_state_process_up = 1;
455
456     while (1) {
457         vlib_process_wait_for_event (vm);
458
459         /* Unified list of changed link or admin state sw_if_indices */
460         vlib_process_get_events_with_type 
461             (vm, &event_data, API_LINK_STATE_EVENT);
462         vlib_process_get_events_with_type 
463             (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
464
465         /* Sort, so we can eliminate duplicates */
466         vec_sort_with_function (event_data, event_data_cmp);
467
468         prev_sw_if_index = ~0;
469
470         for (i = 0; i < vec_len(event_data); i++) {
471             /* Only one message per swif */
472             if (prev_sw_if_index == event_data[i])
473                 continue;
474             prev_sw_if_index = event_data[i];
475
476             pool_foreach(reg, vam->interface_events_registrations, 
477             ({
478                 q = vl_api_client_index_to_input_queue (reg->client_index);
479                 if (q) {
480                     // sw_interface may be deleted already
481                     if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
482                             event_data[i]))
483                     {
484                         swif = vnet_get_sw_interface (vnm, event_data[i]);
485                         send_sw_interface_flags (vam, q, swif);
486                     }
487                 }
488             }));
489         }
490         vec_reset_length (event_data);
491     }
492
493     return 0;
494 }
495
496 static clib_error_t *
497 link_up_down_function (vnet_main_t *vm, u32 hw_if_index, u32 flags);
498 static clib_error_t *
499 admin_up_down_function (vnet_main_t *vm, u32 hw_if_index, u32 flags);
500
501 VLIB_REGISTER_NODE (link_state_process_node,static) = {
502     .function = link_state_process,
503     .type = VLIB_NODE_TYPE_PROCESS,
504     .name = "vpe-link-state-process",
505 };
506
507 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
508 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
509
510 static clib_error_t *
511 link_up_down_function (vnet_main_t *vm, u32 hw_if_index, u32 flags)
512 {
513     vpe_api_main_t * vam = &vpe_api_main;
514     vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
515     
516     if (vam->link_state_process_up)
517         vlib_process_signal_event (vam->vlib_main, 
518                                    link_state_process_node.index,
519                                    API_LINK_STATE_EVENT, 
520                                    hi->sw_if_index);
521     return 0;
522 }
523
524 static clib_error_t *
525 admin_up_down_function (vnet_main_t *vm, u32 sw_if_index, u32 flags)
526 {
527     vpe_api_main_t * vam = &vpe_api_main;
528
529     /* 
530      * Note: it's perfectly fair to set a subif admin up / admin down.
531      * Note the subtle distinction between this routine and the previous
532      * routine.
533      */
534     if (vam->link_state_process_up)
535         vlib_process_signal_event (vam->vlib_main, 
536                                    link_state_process_node.index,
537                                    API_ADMIN_UP_DOWN_EVENT, 
538                                    sw_if_index);
539     return 0;
540 }
541
542 #define pub_sub_handler(lca,UCA)                                        \
543 static void vl_api_want_##lca##_t_handler (                             \
544     vl_api_want_##lca##_t *mp)                                          \
545 {                                                                       \
546     vpe_api_main_t *vam = &vpe_api_main;                                \
547     vpe_client_registration_t *rp;                                      \
548     vl_api_want_##lca##_reply_t *rmp;                                   \
549     uword *p;                                                           \
550     i32 rv = 0;                                                         \
551                                                                         \
552     p = hash_get (vam->lca##_registration_hash, mp->client_index);      \
553     if (p) {                                                            \
554         if (mp->enable_disable) {                                       \
555             clib_warning ("pid %d: already enabled...", mp->pid);       \
556             rv = VNET_API_ERROR_INVALID_REGISTRATION;                   \
557             goto reply;                                                 \
558         } else {                                                        \
559             rp = pool_elt_at_index (vam->lca##_registrations, p[0]);    \
560             pool_put (vam->lca##_registrations, rp);                    \
561             hash_unset (vam->lca##_registration_hash,                   \
562                 mp->client_index);                                      \
563             goto reply;                                                 \
564         }                                                               \
565     }                                                                   \
566     if (mp->enable_disable == 0) {                                      \
567         clib_warning ("pid %d: already disabled...", mp->pid);          \
568         rv = VNET_API_ERROR_INVALID_REGISTRATION;                       \
569         goto reply;                                                     \
570     }                                                                   \
571     pool_get (vam->lca##_registrations, rp);                            \
572     rp->client_index = mp->client_index;                                \
573     rp->client_pid = mp->pid;                                           \
574     hash_set (vam->lca##_registration_hash, rp->client_index,           \
575               rp - vam->lca##_registrations);                           \
576                                                                         \
577 reply:                                                                  \
578     REPLY_MACRO (VL_API_WANT_##UCA##_REPLY);                            \
579 }
580
581 pub_sub_handler (interface_events,INTERFACE_EVENTS)
582 pub_sub_handler (oam_events,OAM_EVENTS)
583
584 #define RESOLUTION_EVENT 1
585 #define RESOLUTION_PENDING_EVENT 2
586 #define IP4_ARP_EVENT 3
587
588 static int ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t *mp);
589 static int ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t *mp);
590 static int mpls_ethernet_add_del_tunnel_2_t_handler 
591 (vl_api_mpls_ethernet_add_del_tunnel_2_t *mp);
592
593 void handle_ip4_arp_event (u32 pool_index)
594 {
595     vpe_api_main_t * vam = &vpe_api_main;
596     vnet_main_t * vnm = vam->vnet_main;
597     vlib_main_t * vm = vam->vlib_main;
598     vl_api_ip4_arp_event_t * event;
599     vl_api_ip4_arp_event_t * mp;
600     unix_shared_memory_queue_t * q;
601
602     /* Client can cancel, die, etc. */
603     if (pool_is_free_index (vam->arp_events, pool_index))
604         return;
605                 
606     event = pool_elt_at_index (vam->arp_events, pool_index);
607     
608     q = vl_api_client_index_to_input_queue (event->client_index);
609     if (!q) {
610         (void) vnet_add_del_ip4_arp_change_event 
611             (vnm, arp_change_delete_callback,
612              event->pid, &event->address,
613              vpe_resolver_process_node.index, IP4_ARP_EVENT,
614              ~0 /* pool index, notused */, 0 /* is_add */);
615         return;
616     }
617     
618     if (q->cursize < q->maxsize) {
619         mp =  vl_msg_api_alloc (sizeof (*mp));
620         clib_memcpy (mp, event, sizeof (*mp));
621         vl_msg_api_send_shmem (q, (u8 *)&mp);
622     } else {
623         static f64 last_time;
624         /* 
625          * Throttle syslog msgs. 
626          * It's pretty tempting to just revoke the registration...
627          */
628         if (vlib_time_now (vm) > last_time + 10.0) {
629             clib_warning ("arp event for %U to pid %d: queue stuffed!",
630                           format_ip4_address, &event->address, event->pid);
631             last_time = vlib_time_now(vm);
632         }
633     }
634 }
635
636 static uword
637 resolver_process (vlib_main_t * vm,
638                     vlib_node_runtime_t * rt,
639                     vlib_frame_t * f)
640 {
641     uword event_type;
642     uword *event_data = 0;
643     f64 timeout = 100.0;
644     vpe_api_main_t * vam = &vpe_api_main;
645     pending_route_t * pr;
646     vl_api_ip_add_del_route_t * adr;
647     vl_api_mpls_ethernet_add_del_tunnel_2_t *pme;
648     u32 * resolution_failures = 0;
649     int i, rv;
650     clib_error_t * e;
651
652     while (1) {
653         vlib_process_wait_for_event_or_clock (vm, timeout);
654
655         event_type = vlib_process_get_events (vm, &event_data);
656
657         switch (event_type) {
658         case RESOLUTION_PENDING_EVENT:
659             timeout = 1.0;
660             break;
661
662         case RESOLUTION_EVENT:
663             for (i = 0; i < vec_len(event_data); i++) {
664                 /*
665                  * Resolution events can occur long after the
666                  * original request has timed out. $$$ add a cancel
667                  * mechanism..
668                  */
669                 if (pool_is_free_index (vam->pending_routes, event_data[i]))
670                     continue;
671
672                 pr = pool_elt_at_index (vam->pending_routes, event_data[i]);
673                 adr = &pr->r;
674                 pme = &pr->t;
675
676                 switch (pr->resolve_type) {                    
677                 case RESOLVE_IP4_ADD_DEL_ROUTE:
678                     rv = ip4_add_del_route_t_handler (adr);
679                     clib_warning ("resolver: add %U/%d via %U %s",
680                                   format_ip4_address,
681                                   (ip4_address_t *)&(adr->dst_address),
682                                   adr->dst_address_length,
683                                   format_ip4_address,
684                                   (ip4_address_t *)&(adr->next_hop_address),
685                                   (rv >= 0) ? "succeeded" : "failed");
686                     break;
687
688                 case RESOLVE_IP6_ADD_DEL_ROUTE:
689                     rv = ip6_add_del_route_t_handler (adr);
690                     clib_warning ("resolver: add %U/%d via %U %s",
691                                   format_ip6_address,
692                                   (ip6_address_t *)&(adr->dst_address),
693                                   adr->dst_address_length,
694                                   format_ip6_address,
695                                   (ip6_address_t *)&(adr->next_hop_address),
696                                   (rv >= 0) ? "succeeded" : "failed");
697                     break;
698
699                 case RESOLVE_MPLS_ETHERNET_ADD_DEL:
700                     rv = mpls_ethernet_add_del_tunnel_2_t_handler (pme);
701                     clib_warning ("resolver: add mpls-o-e via %U %s",
702                                   format_ip4_address,
703             (ip4_address_t *)&(pme->next_hop_ip4_address_in_outer_vrf),
704                                   (rv >= 0) ? "succeeded" : "failed");
705                     break;
706
707                 default:
708                     clib_warning ("resolver: BOGUS TYPE %d", pr->resolve_type);
709                 }
710                 pool_put (vam->pending_routes, pr);
711             }
712             break;
713
714         case IP4_ARP_EVENT:
715             for (i = 0; i < vec_len(event_data); i++)
716                 handle_ip4_arp_event (event_data[i]);
717             break;
718
719         case ~0:                /* timeout, retry pending resolutions */
720             pool_foreach (pr, vam->pending_routes, 
721             ({
722                 int is_adr = 1;
723                 adr = &pr->r;
724                 pme = &pr->t;
725
726                 /* May fail, e.g. due to interface down */
727                 switch (pr->resolve_type) {
728                 case RESOLVE_IP4_ADD_DEL_ROUTE:
729                     e = ip4_probe_neighbor 
730                         (vm, (ip4_address_t *)&(adr->next_hop_address),
731                          ntohl(adr->next_hop_sw_if_index));
732                     break;
733
734                 case RESOLVE_IP6_ADD_DEL_ROUTE:
735                     e = ip6_probe_neighbor 
736                         (vm, (ip6_address_t *)&(adr->next_hop_address),
737                          ntohl(adr->next_hop_sw_if_index));
738                     break;
739
740                 case RESOLVE_MPLS_ETHERNET_ADD_DEL:
741                     is_adr = 0;
742                     e = ip4_probe_neighbor 
743                         (vm, 
744             (ip4_address_t *)&(pme->next_hop_ip4_address_in_outer_vrf),
745                          pme->resolve_opaque);
746                     break;
747                     
748                 default:
749                     e = clib_error_return (0, "resolver: BOGUS TYPE %d",
750                                            pr->resolve_type);
751                 }
752                 if (e) {
753                     clib_error_report (e);
754                     if (is_adr)
755                         adr->resolve_attempts = 1;
756                     else
757                         pme->resolve_attempts = 1;
758                     
759                 }
760                 if (is_adr) {
761                     adr->resolve_attempts -= 1;
762                     if (adr->resolve_attempts == 0)
763                         vec_add1 (resolution_failures, 
764                                   pr - vam->pending_routes);
765                 } else {
766                     pme->resolve_attempts -= 1;
767                     if (pme->resolve_attempts == 0)
768                         vec_add1 (resolution_failures, 
769                                   pr - vam->pending_routes);
770                 }
771                     
772             }));
773             for (i = 0; i < vec_len (resolution_failures); i++) {
774                 pr = pool_elt_at_index (vam->pending_routes, 
775                                         resolution_failures[i]);
776                 adr = &pr->r;
777                 pme = &pr->t;
778
779                 switch (pr->resolve_type) {
780                 case RESOLVE_IP4_ADD_DEL_ROUTE:
781                     clib_warning ("resolver: add %U/%d via %U retry failure",
782                                   format_ip4_address,
783                                   (ip4_address_t *)&(adr->dst_address),
784                                   adr->dst_address_length,
785                                   format_ip4_address,
786                                   (ip4_address_t *)&(adr->next_hop_address));
787                     break;
788
789                 case RESOLVE_IP6_ADD_DEL_ROUTE:
790                     clib_warning ("resolver: add %U/%d via %U retry failure",
791                                   format_ip6_address,
792                                   (ip6_address_t *)&(adr->dst_address),
793                                   adr->dst_address_length,
794                                   format_ip6_address,
795                                   (ip6_address_t *)&(adr->next_hop_address));
796                     break;
797
798                 case RESOLVE_MPLS_ETHERNET_ADD_DEL:
799                     clib_warning ("resolver: add mpls-o-e via %U retry failure",
800                                   format_ip4_address,
801                    (ip4_address_t *)&(pme->next_hop_ip4_address_in_outer_vrf));
802                     break;
803
804                 default:
805                     clib_warning ("BUG");
806                 }
807                 pool_put(vam->pending_routes, pr);
808             }
809             vec_reset_length (resolution_failures);
810             break;
811         }
812         if (pool_elts (vam->pending_routes) == 0)
813             timeout = 100.0;
814         vec_reset_length (event_data);
815     }
816     return 0; /* or not */
817 }
818
819 VLIB_REGISTER_NODE (vpe_resolver_process_node,static) = {
820     .function = resolver_process,
821     .type = VLIB_NODE_TYPE_PROCESS,
822     .name = "vpe-route-resolver-process",
823 };
824
825 static int ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t *mp)
826 {
827     ip4_main_t * im = &ip4_main;
828     ip_lookup_main_t * lm = &im->lookup_main;
829     vnet_classify_main_t * cm = &vnet_classify_main;
830     stats_main_t * sm = &stats_main;
831     ip4_add_del_route_args_t a;
832     ip4_address_t next_hop_address;
833     u32 fib_index;
834     vpe_api_main_t * vam = &vpe_api_main;
835     vnet_main_t * vnm = vam->vnet_main;
836     vlib_main_t * vm = vlib_get_main();
837     pending_route_t * pr;
838     vl_api_ip_add_del_route_t * adr;
839     uword * p;
840     clib_error_t * e;
841     u32 ai;
842     ip_adjacency_t *nh_adj, *add_adj = 0;
843
844     p = hash_get (im->fib_index_by_table_id, ntohl(mp->vrf_id));
845     if (!p) {
846         if (mp->create_vrf_if_needed) {
847             ip4_fib_t * f;
848             f = find_ip4_fib_by_table_index_or_id (im, ntohl(mp->vrf_id), 
849                                                    0 /* flags */);
850             fib_index = f->index;
851         } else {
852             /* No such VRF, and we weren't asked to create one */
853             return VNET_API_ERROR_NO_SUCH_FIB;
854         }
855     } else {
856         fib_index = p[0];
857     }
858
859     if (pool_is_free_index (vnm->interface_main.sw_interfaces, 
860                             ntohl(mp->next_hop_sw_if_index)))
861         return VNET_API_ERROR_NO_MATCHING_INTERFACE;
862     
863     clib_memcpy (next_hop_address.data, mp->next_hop_address, 
864             sizeof (next_hop_address.data));
865
866     /* Arp for the next_hop if necessary */
867     if (mp->is_add && mp->resolve_if_needed) {
868         u32 lookup_result;
869         ip_adjacency_t * adj;
870
871         lookup_result = ip4_fib_lookup_with_table 
872             (im, fib_index, &next_hop_address, 1 /* disable default route */);
873
874         adj = ip_get_adjacency (lm, lookup_result);
875
876         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP) {
877             pool_get (vam->pending_routes, pr);
878             pr->resolve_type = RESOLVE_IP4_ADD_DEL_ROUTE;
879             adr = &pr->r;
880             clib_memcpy (adr, mp, sizeof (*adr));
881             /* recursion block, "just in case" */
882             adr->resolve_if_needed = 0;
883             adr->resolve_attempts = ntohl(mp->resolve_attempts);
884             vnet_register_ip4_arp_resolution_event 
885                 (vnm, &next_hop_address, vpe_resolver_process_node.index,
886                  RESOLUTION_EVENT, pr - vam->pending_routes);
887
888             vlib_process_signal_event 
889                 (vm, vpe_resolver_process_node.index,
890                  RESOLUTION_PENDING_EVENT, 0 /* data */);
891
892             /* The interface may be down, etc. */
893             e = ip4_probe_neighbor 
894                 (vm, (ip4_address_t *)&(mp->next_hop_address),
895                  ntohl(mp->next_hop_sw_if_index));
896
897             if (e)
898                 clib_error_report(e);
899
900             return VNET_API_ERROR_IN_PROGRESS;
901         }
902     }
903
904     if (mp->is_multipath) {
905         u32 flags;
906
907         dslock (sm, 1 /* release hint */, 10 /* tag */);
908
909         if (mp->is_add)
910            flags = IP4_ROUTE_FLAG_ADD;
911         else
912            flags = IP4_ROUTE_FLAG_DEL;
913
914         if (mp->not_last)
915             flags |= IP4_ROUTE_FLAG_NOT_LAST_IN_GROUP;
916
917         ip4_add_del_route_next_hop (im, flags, 
918                                     (ip4_address_t *) mp->dst_address,
919                                     (u32) mp->dst_address_length,
920                                     (ip4_address_t *) mp->next_hop_address,
921                                     ntohl(mp->next_hop_sw_if_index),
922                                     (u32) mp->next_hop_weight, 
923                                     ~0 /* adj_index */,
924                                     fib_index);
925         dsunlock(sm);
926         return 0;
927     }
928
929     memset (&a, 0, sizeof (a));
930     clib_memcpy (a.dst_address.data, mp->dst_address, sizeof (a.dst_address.data));
931
932     a.dst_address_length = mp->dst_address_length;
933
934     a.flags = (mp->is_add ? IP4_ROUTE_FLAG_ADD : IP4_ROUTE_FLAG_DEL);
935     a.flags |= IP4_ROUTE_FLAG_FIB_INDEX;
936     a.table_index_or_table_id = fib_index;
937     a.add_adj = 0;
938     a.n_add_adj = 0;
939
940     if (mp->not_last)
941         a.flags |= IP4_ROUTE_FLAG_NOT_LAST_IN_GROUP;
942
943     dslock (sm, 1 /* release hint */, 2 /* tag */);
944
945     if (mp->is_add) {
946         if (mp->is_drop)
947             ai = lm->drop_adj_index;
948         else if (mp->is_local)
949             ai = lm->local_adj_index;
950         else if (mp->is_classify) {
951             ip_adjacency_t cadj;
952             memset(&cadj, 0, sizeof(cadj));
953             cadj.lookup_next_index = IP_LOOKUP_NEXT_CLASSIFY;
954             cadj.classify.table_index = ntohl(mp->classify_table_index);
955             if (pool_is_free_index (cm->tables, cadj.classify.table_index)) {
956                 dsunlock(sm);
957                 return VNET_API_ERROR_NO_SUCH_TABLE;
958             }
959             vec_add1 (add_adj, cadj);
960             goto do_add_del;
961         }
962         else {
963             ai = ip4_fib_lookup_with_table 
964                 (im, fib_index, &next_hop_address, 
965                  1 /* disable default route */);
966             if (ai == lm->miss_adj_index) {
967                 dsunlock(sm);
968                 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
969             }
970         }
971         
972         nh_adj = ip_get_adjacency (lm, ai);
973         vec_add1 (add_adj, nh_adj[0]);
974         if (mp->lookup_in_vrf) {
975             p = hash_get (im->fib_index_by_table_id, ntohl(mp->lookup_in_vrf));
976             if (p)
977                 add_adj[0].explicit_fib_index = p[0];
978             else {
979                 vec_free (add_adj);
980                 dsunlock(sm);
981                 return VNET_API_ERROR_NO_SUCH_INNER_FIB;
982             }
983         }
984     } else {
985         ip_adjacency_t * adj;
986         int disable_default_route = 1;
987         
988         /* Trying to delete the default route? */
989         if (a.dst_address.as_u32 == 0 &&
990             a.dst_address_length == 0)
991             disable_default_route = 0;
992         
993         ai = ip4_fib_lookup_with_table 
994             (im, fib_index, &a.dst_address, disable_default_route);
995         if (ai == lm->miss_adj_index) {
996             dsunlock(sm);
997             return VNET_API_ERROR_UNKNOWN_DESTINATION;
998         }
999
1000         adj = ip_get_adjacency (lm, ai);
1001         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP) {
1002             dsunlock(sm);
1003             return VNET_API_ERROR_ADDRESS_MATCHES_INTERFACE_ADDRESS;
1004         }
1005     }
1006     
1007 do_add_del:
1008     a.adj_index = ~0;
1009     a.add_adj = add_adj;
1010     a.n_add_adj = vec_len(add_adj);
1011     ip4_add_del_route (im, &a);
1012     
1013     vec_free (add_adj);
1014
1015     dsunlock (sm);
1016     return 0;
1017 }
1018
1019 static int ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t *mp)
1020 {
1021     ip6_main_t * im = &ip6_main;
1022     ip_lookup_main_t * lm = &im->lookup_main;
1023     vnet_main_t * vnm = vnet_get_main();
1024     vlib_main_t * vm = vlib_get_main();
1025     vpe_api_main_t * vam = &vpe_api_main;
1026     stats_main_t * sm = &stats_main;
1027     ip6_add_del_route_args_t a;
1028     ip6_address_t next_hop_address;
1029     pending_route_t * pr;
1030     vl_api_ip_add_del_route_t * adr;
1031
1032     u32 fib_index;
1033     uword * p;
1034     clib_error_t * e;
1035     ip_adjacency_t * nh_adj, * add_adj = 0;
1036     u32 ai;
1037
1038     p = hash_get (im->fib_index_by_table_id, ntohl(mp->vrf_id));
1039
1040     if (!p) {
1041         if (mp->create_vrf_if_needed) {
1042             ip6_fib_t * f;
1043             f = find_ip6_fib_by_table_index_or_id (im, ntohl(mp->vrf_id), 
1044                                                    0 /* flags */);
1045             fib_index = f->index;
1046         } else {
1047             /* No such VRF, and we weren't asked to create one */
1048             return VNET_API_ERROR_NO_SUCH_FIB;
1049         }
1050     } else {
1051         fib_index = p[0];
1052     }
1053
1054     if (pool_is_free_index (vnm->interface_main.sw_interfaces, 
1055                             ntohl(mp->next_hop_sw_if_index)))
1056         return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1057
1058     clib_memcpy (next_hop_address.as_u8, mp->next_hop_address, 
1059             sizeof (next_hop_address.as_u8));
1060
1061     /* Arp for the next_hop if necessary */
1062     if (mp->is_add && mp->resolve_if_needed) {
1063         u32 lookup_result;
1064         ip_adjacency_t * adj;
1065
1066         lookup_result = ip6_fib_lookup_with_table 
1067             (im, fib_index, &next_hop_address);
1068
1069         adj = ip_get_adjacency (lm, lookup_result);
1070
1071         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP) {
1072             pool_get (vam->pending_routes, pr);
1073             adr = &pr->r;
1074             pr->resolve_type = RESOLVE_IP6_ADD_DEL_ROUTE;
1075             clib_memcpy (adr, mp, sizeof (*adr));
1076             /* recursion block, "just in case" */
1077             adr->resolve_if_needed = 0;
1078             adr->resolve_attempts = ntohl(mp->resolve_attempts);
1079             vnet_register_ip6_neighbor_resolution_event 
1080                 (vnm, &next_hop_address, vpe_resolver_process_node.index,
1081                  RESOLUTION_EVENT, pr - vam->pending_routes);
1082
1083             vlib_process_signal_event 
1084                 (vm, vpe_resolver_process_node.index,
1085                  RESOLUTION_PENDING_EVENT, 0 /* data */);
1086
1087             /* The interface may be down, etc. */
1088             e = ip6_probe_neighbor 
1089                 (vm, (ip6_address_t *)&(mp->next_hop_address),
1090                  ntohl(mp->next_hop_sw_if_index));
1091
1092             if (e)
1093                 clib_error_report(e);
1094
1095             return VNET_API_ERROR_IN_PROGRESS;
1096         }
1097     }
1098
1099     if (mp->is_multipath) {
1100         u32 flags;
1101
1102         dslock (sm, 1 /* release hint */, 11 /* tag */);
1103
1104         if (mp->is_add)
1105             flags = IP6_ROUTE_FLAG_ADD;
1106         else
1107             flags = IP6_ROUTE_FLAG_DEL;
1108
1109         if (mp->not_last)
1110             flags |= IP6_ROUTE_FLAG_NOT_LAST_IN_GROUP;
1111
1112         ip6_add_del_route_next_hop (im, flags, (ip6_address_t *)mp->dst_address,
1113                                     (u32) mp->dst_address_length,
1114                                     (ip6_address_t *)mp->next_hop_address,
1115                                     ntohl(mp->next_hop_sw_if_index),
1116                                     (u32) mp->next_hop_weight, 
1117                                     ~0 /* adj_index */,
1118                                     fib_index);
1119         dsunlock(sm);
1120         return 0;
1121     }
1122
1123     memset (&a, 0, sizeof (a));
1124     clib_memcpy (a.dst_address.as_u8, mp->dst_address, sizeof (a.dst_address.as_u8));
1125
1126     a.dst_address_length = mp->dst_address_length;
1127     
1128     a.flags = (mp->is_add ? IP6_ROUTE_FLAG_ADD : IP6_ROUTE_FLAG_DEL);
1129     a.flags |= IP6_ROUTE_FLAG_FIB_INDEX;
1130     a.table_index_or_table_id = fib_index;
1131     a.add_adj = 0;
1132     a.n_add_adj = 0;
1133     
1134     if (mp->not_last)
1135         a.flags |= IP6_ROUTE_FLAG_NOT_LAST_IN_GROUP;
1136     
1137     dslock (sm, 1 /* release hint */, 3 /* tag */);
1138
1139     if (mp->is_add) {
1140         if (mp->is_drop)
1141             ai = lm->drop_adj_index;
1142         else if (mp->is_local)
1143             ai = lm->local_adj_index;
1144         else {
1145             ai = ip6_fib_lookup_with_table 
1146                 (im, fib_index, &next_hop_address);
1147             if (ai == lm->miss_adj_index) {
1148                 dsunlock(sm);
1149                 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
1150             }
1151         }
1152         
1153         nh_adj = ip_get_adjacency (lm, ai);
1154         vec_add1 (add_adj, nh_adj[0]);
1155         if (mp->lookup_in_vrf) {
1156             p = hash_get (im->fib_index_by_table_id, ntohl(mp->lookup_in_vrf));
1157             if (p)
1158                 add_adj[0].explicit_fib_index = p[0];
1159             else {
1160                 vec_free (add_adj);
1161                 dsunlock(sm);
1162                 return VNET_API_ERROR_NO_SUCH_INNER_FIB;
1163             }
1164         }
1165     } else {
1166         ip_adjacency_t * adj;
1167         
1168         ai = ip6_fib_lookup_with_table 
1169             (im, fib_index, &a.dst_address);
1170         if (ai == lm->miss_adj_index) {
1171             dsunlock(sm);
1172             return VNET_API_ERROR_UNKNOWN_DESTINATION;
1173         }
1174         adj = ip_get_adjacency (lm, ai);
1175         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP) {
1176             dsunlock(sm);
1177             return VNET_API_ERROR_ADDRESS_MATCHES_INTERFACE_ADDRESS;
1178         }
1179     }
1180     
1181     a.adj_index = ~0;
1182     a.add_adj = add_adj;
1183     a.n_add_adj = vec_len(add_adj);
1184     ip6_add_del_route (im, &a);
1185     
1186     vec_free (add_adj);
1187
1188     dsunlock (sm);
1189     return 0;
1190 }
1191
1192 void vl_api_ip_add_del_route_t_handler (
1193     vl_api_ip_add_del_route_t *mp)
1194 {
1195     vl_api_ip_add_del_route_reply_t * rmp;
1196     int rv;
1197     vnet_main_t * vnm = vnet_get_main();
1198
1199     vnm->api_errno = 0;
1200
1201     if (mp->is_ipv6)
1202         rv = ip6_add_del_route_t_handler (mp);
1203     else
1204         rv = ip4_add_del_route_t_handler (mp);
1205
1206     rv = (rv == 0) ? vnm->api_errno : rv;
1207
1208     REPLY_MACRO(VL_API_IP_ADD_DEL_ROUTE_REPLY);
1209 }
1210
1211 void api_config_default_ip_route (u8 is_ipv6, u8 is_add, u32 vrf_id,
1212                                   u32 sw_if_index, u8 *next_hop_addr)
1213 {
1214     vl_api_ip_add_del_route_t mp;
1215     int rv;
1216
1217     memset (&mp, 0, sizeof(vl_api_ip_add_del_route_t));
1218
1219     /*
1220      * Configure default IP route:
1221      *  - ip route add 0.0.0.0/1 via <GW IP>
1222      *  - ip route add 128.0.0.0/1 via <GW IP>
1223      */
1224     mp.next_hop_sw_if_index = ntohl(sw_if_index);
1225     mp.vrf_id = vrf_id;
1226     mp.resolve_attempts = ~0;
1227     mp.resolve_if_needed = 1;
1228     mp.is_add = is_add;
1229     mp.is_ipv6 = is_ipv6;
1230     mp.next_hop_weight = 1;
1231
1232     clib_memcpy (&mp.next_hop_address[0], next_hop_addr, 16);
1233
1234     if (is_ipv6)
1235         rv = ip6_add_del_route_t_handler (&mp);
1236     else
1237       {
1238         mp.dst_address_length = 1;
1239
1240         mp.dst_address[0] = 0;
1241         rv = ip4_add_del_route_t_handler (&mp);
1242
1243         mp.dst_address[0] = 128;
1244         rv |= ip4_add_del_route_t_handler (&mp);
1245       }
1246
1247     if (rv)
1248         clib_error_return (0, "failed to config default IP route");
1249
1250 }
1251
1252 static void 
1253 vl_api_sw_interface_add_del_address_t_handler 
1254 (vl_api_sw_interface_add_del_address_t *mp)
1255 {
1256     vlib_main_t *vm = vlib_get_main();
1257     vl_api_sw_interface_add_del_address_reply_t * rmp;
1258     int rv = 0;
1259     u32 is_del;
1260
1261     VALIDATE_SW_IF_INDEX(mp);
1262
1263     is_del = mp->is_add == 0;
1264
1265     if (mp->del_all)
1266         ip_del_all_interface_addresses (vm, ntohl(mp->sw_if_index));
1267     else if (mp->is_ipv6)
1268         ip6_add_del_interface_address (vm, ntohl(mp->sw_if_index),
1269                                        (void *)mp->address, 
1270                                        mp->address_length, is_del);
1271     else 
1272         ip4_add_del_interface_address (vm, ntohl(mp->sw_if_index),
1273                                        (void *) mp->address, 
1274                                        mp->address_length, is_del);
1275     
1276     BAD_SW_IF_INDEX_LABEL;
1277
1278     REPLY_MACRO(VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
1279 }
1280
1281 static void
1282 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t *mp)
1283 {
1284     int rv = 0;
1285     u32 table_id = ntohl(mp->vrf_id);
1286     u32 sw_if_index = ntohl(mp->sw_if_index);
1287     vl_api_sw_interface_set_table_reply_t * rmp;
1288     stats_main_t * sm = &stats_main;
1289
1290     VALIDATE_SW_IF_INDEX(mp);
1291
1292     dslock (sm, 1 /* release hint */, 4 /* tag */);
1293
1294     if (mp->is_ipv6) {
1295         ip6_main_t * im = &ip6_main;
1296         ip6_fib_t * fib = 
1297             find_ip6_fib_by_table_index_or_id (im, table_id, 
1298                                                IP6_ROUTE_FLAG_TABLE_ID);
1299         if (fib) {
1300             vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1301             im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
1302         } else {
1303             rv = VNET_API_ERROR_NO_SUCH_FIB;
1304         }
1305     } else {
1306         ip4_main_t * im = &ip4_main;
1307         ip4_fib_t * fib = find_ip4_fib_by_table_index_or_id 
1308             (im, table_id, IP4_ROUTE_FLAG_TABLE_ID);
1309         
1310         /* Truthfully this can't fail */
1311         if (fib) {
1312             vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1313             im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
1314         } else {
1315             rv = VNET_API_ERROR_NO_SUCH_FIB;
1316         }
1317     }
1318     dsunlock(sm);
1319     
1320     BAD_SW_IF_INDEX_LABEL;
1321
1322     REPLY_MACRO(VL_API_SW_INTERFACE_SET_TABLE_REPLY);
1323 }
1324
1325 static void 
1326 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t *mp)
1327 {
1328     vlib_main_t *vm = vlib_get_main();
1329     vl_api_sw_interface_set_vpath_reply_t * rmp;
1330     int rv = 0;
1331     u32 ci;
1332     u32 sw_if_index = ntohl(mp->sw_if_index);
1333     ip4_main_t   *ip4m = &ip4_main;
1334     ip6_main_t   *ip6m = &ip6_main;
1335     ip_lookup_main_t *ip4lm = &ip4m->lookup_main;
1336     ip_lookup_main_t *ip6lm = &ip6m->lookup_main;
1337     ip_config_main_t *rx_cm4u = &ip4lm->rx_config_mains[VNET_UNICAST];
1338     ip_config_main_t *rx_cm4m = &ip4lm->rx_config_mains[VNET_MULTICAST];
1339     ip_config_main_t *rx_cm6u = &ip6lm->rx_config_mains[VNET_UNICAST]; 
1340     ip_config_main_t *rx_cm6m = &ip6lm->rx_config_mains[VNET_MULTICAST];
1341
1342     VALIDATE_SW_IF_INDEX(mp);
1343
1344     l2input_intf_bitmap_enable(sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
1345     if (mp->enable) {
1346         ci = rx_cm4u->config_index_by_sw_if_index[sw_if_index]; //IP4 unicast
1347         ci = vnet_config_add_feature(vm, &rx_cm4u->config_main, 
1348                                      ci, IP4_RX_FEATURE_VPATH, 0, 0);
1349         rx_cm4u->config_index_by_sw_if_index[sw_if_index] = ci;
1350         ci = rx_cm4m->config_index_by_sw_if_index[sw_if_index]; //IP4 mcast
1351         ci = vnet_config_add_feature(vm, &rx_cm4m->config_main, 
1352                                      ci, IP4_RX_FEATURE_VPATH, 0, 0);
1353         rx_cm4m->config_index_by_sw_if_index[sw_if_index] = ci;
1354         ci = rx_cm6u->config_index_by_sw_if_index[sw_if_index]; //IP6 unicast
1355         ci = vnet_config_add_feature(vm, &rx_cm6u->config_main, 
1356                                      ci, IP6_RX_FEATURE_VPATH, 0, 0);
1357         rx_cm6u->config_index_by_sw_if_index[sw_if_index] = ci;
1358         ci = rx_cm6m->config_index_by_sw_if_index[sw_if_index]; //IP6 mcast
1359         ci = vnet_config_add_feature(vm, &rx_cm6m->config_main, 
1360                                      ci, IP6_RX_FEATURE_VPATH, 0, 0);
1361         rx_cm6m->config_index_by_sw_if_index[sw_if_index] = ci;
1362     } else {
1363         ci = rx_cm4u->config_index_by_sw_if_index[sw_if_index]; //IP4 unicast
1364         ci = vnet_config_del_feature(vm, &rx_cm4u->config_main, 
1365                                      ci, IP4_RX_FEATURE_VPATH, 0, 0);
1366         rx_cm4u->config_index_by_sw_if_index[sw_if_index] = ci;
1367         ci = rx_cm4m->config_index_by_sw_if_index[sw_if_index]; //IP4 mcast
1368         ci = vnet_config_del_feature(vm, &rx_cm4m->config_main, 
1369                                      ci, IP4_RX_FEATURE_VPATH, 0, 0);
1370         rx_cm4m->config_index_by_sw_if_index[sw_if_index] = ci;
1371         ci = rx_cm6u->config_index_by_sw_if_index[sw_if_index]; //IP6 unicast
1372         ci = vnet_config_del_feature(vm, &rx_cm6u->config_main, 
1373                                      ci, IP6_RX_FEATURE_VPATH, 0, 0);
1374         rx_cm6u->config_index_by_sw_if_index[sw_if_index] = ci;
1375         ci = rx_cm6m->config_index_by_sw_if_index[sw_if_index]; //IP6 mcast
1376         ci = vnet_config_del_feature(vm, &rx_cm6m->config_main, 
1377                                      ci, IP6_RX_FEATURE_VPATH, 0, 0);
1378         rx_cm6m->config_index_by_sw_if_index[sw_if_index] = ci;
1379     }
1380
1381     BAD_SW_IF_INDEX_LABEL;
1382
1383     REPLY_MACRO(VL_API_SW_INTERFACE_SET_VPATH_REPLY);
1384 }
1385
1386 static void 
1387 vl_api_sw_interface_set_l2_xconnect_t_handler (
1388     vl_api_sw_interface_set_l2_xconnect_t *mp)
1389 {
1390     vl_api_sw_interface_set_l2_xconnect_reply_t * rmp;
1391     int rv = 0;
1392     u32 rx_sw_if_index = ntohl(mp->rx_sw_if_index);
1393     u32 tx_sw_if_index = ntohl(mp->tx_sw_if_index);
1394     vlib_main_t *vm  = vlib_get_main();
1395     vnet_main_t *vnm = vnet_get_main();
1396
1397     VALIDATE_RX_SW_IF_INDEX(mp);
1398
1399     if (mp->enable) {
1400         VALIDATE_TX_SW_IF_INDEX(mp);
1401         rv = set_int_l2_mode(vm, vnm, MODE_L2_XC, 
1402                              rx_sw_if_index, 0, 0, 0, tx_sw_if_index);
1403     } else {
1404         rv = set_int_l2_mode(vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
1405     }
1406     
1407     BAD_RX_SW_IF_INDEX_LABEL;
1408     BAD_TX_SW_IF_INDEX_LABEL;
1409
1410     REPLY_MACRO(VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
1411 }
1412
1413 static void 
1414 vl_api_sw_interface_set_l2_bridge_t_handler (
1415     vl_api_sw_interface_set_l2_bridge_t *mp)
1416 {
1417     bd_main_t * bdm = &bd_main;
1418     vl_api_sw_interface_set_l2_bridge_reply_t * rmp;
1419     int rv = 0;
1420     u32 rx_sw_if_index = ntohl(mp->rx_sw_if_index);
1421     u32 bd_id = ntohl(mp->bd_id);
1422     u32 bd_index;
1423     u32 bvi = mp->bvi;
1424     u8 shg = mp->shg;
1425     vlib_main_t *vm  = vlib_get_main();
1426     vnet_main_t *vnm = vnet_get_main();
1427
1428     VALIDATE_RX_SW_IF_INDEX(mp);
1429
1430     bd_index = bd_find_or_add_bd_index (bdm, bd_id);
1431
1432     if (mp->enable) {
1433         //VALIDATE_TX_SW_IF_INDEX(mp);
1434         rv = set_int_l2_mode(vm, vnm, MODE_L2_BRIDGE, 
1435                              rx_sw_if_index, bd_index, bvi, shg, 0);
1436     } else {
1437         rv = set_int_l2_mode(vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
1438     }
1439     
1440     BAD_RX_SW_IF_INDEX_LABEL;
1441
1442     REPLY_MACRO(VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
1443 }
1444
1445 static void 
1446 vl_api_bridge_domain_add_del_t_handler (
1447     vl_api_bridge_domain_add_del_t *mp)
1448 {
1449     vlib_main_t * vm = vlib_get_main ();
1450     bd_main_t * bdm = &bd_main;
1451     vl_api_bridge_domain_add_del_reply_t * rmp;
1452     int rv = 0;
1453     u32 enable_flags = 0, disable_flags = 0;
1454     u32 bd_id = ntohl(mp->bd_id);
1455     u32 bd_index;
1456
1457     if (mp->is_add) {
1458         bd_index = bd_find_or_add_bd_index (bdm, bd_id);
1459
1460         if (mp->flood) 
1461             enable_flags |= L2_FLOOD;
1462         else
1463             disable_flags |= L2_FLOOD;
1464
1465         if (mp->uu_flood) 
1466             enable_flags |= L2_UU_FLOOD;
1467         else
1468             disable_flags |= L2_UU_FLOOD;
1469
1470         if (mp->forward)
1471             enable_flags |= L2_FWD;
1472         else
1473             disable_flags |= L2_FWD;
1474
1475         if (mp->arp_term) 
1476             enable_flags |= L2_ARP_TERM;
1477         else
1478             disable_flags |= L2_ARP_TERM;
1479
1480         if (mp->learn)
1481             enable_flags |= L2_LEARN;
1482         else
1483             disable_flags |= L2_LEARN;
1484
1485         if (enable_flags)
1486             bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */);
1487
1488         if (disable_flags)
1489             bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */);
1490
1491     } else
1492         rv = bd_delete_bd_index(bdm, bd_id);
1493
1494     REPLY_MACRO(VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
1495 }
1496
1497 static void vl_api_bridge_domain_details_t_handler (
1498     vl_api_bridge_domain_details_t * mp)
1499 {
1500     clib_warning ("BUG");
1501 }
1502
1503 static void vl_api_bridge_domain_sw_if_details_t_handler (
1504     vl_api_bridge_domain_sw_if_details_t * mp)
1505 {
1506     clib_warning ("BUG");
1507 }
1508
1509 static void send_bridge_domain_details (unix_shared_memory_queue_t *q,
1510                                         l2_bridge_domain_t * bd_config,
1511                                         u32 n_sw_ifs,
1512                                         u32 context)
1513 {
1514     vl_api_bridge_domain_details_t * mp;
1515
1516     mp = vl_msg_api_alloc (sizeof (*mp));
1517     memset (mp, 0, sizeof (*mp));
1518     mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
1519     mp->bd_id = ntohl (bd_config->bd_id);
1520     mp->flood = bd_feature_flood (bd_config);
1521     mp->uu_flood = bd_feature_uu_flood (bd_config);
1522     mp->forward = bd_feature_forward (bd_config);
1523     mp->learn = bd_feature_learn (bd_config);
1524     mp->arp_term = bd_feature_arp_term (bd_config);
1525     mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
1526     mp->n_sw_ifs = ntohl (n_sw_ifs);
1527     mp->context = context;
1528
1529     vl_msg_api_send_shmem (q, (u8 *)&mp);
1530 }
1531
1532 static void send_bd_sw_if_details (l2input_main_t * l2im,
1533                                    unix_shared_memory_queue_t *q,
1534                                    l2_flood_member_t * member, u32 bd_id,
1535                                    u32 context)
1536 {
1537     vl_api_bridge_domain_sw_if_details_t * mp;
1538     l2_input_config_t * input_cfg;
1539
1540     mp = vl_msg_api_alloc (sizeof (*mp));
1541     memset (mp, 0, sizeof (*mp));
1542     mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_SW_IF_DETAILS);
1543     mp->bd_id = ntohl (bd_id);
1544     mp->sw_if_index = ntohl (member->sw_if_index);
1545     input_cfg = vec_elt_at_index (l2im->configs, member->sw_if_index);
1546     mp->shg = input_cfg->shg;
1547     mp->context = context;
1548
1549     vl_msg_api_send_shmem (q, (u8 *)&mp);
1550 }
1551
1552 static void vl_api_bridge_domain_dump_t_handler (
1553     vl_api_bridge_domain_dump_t *mp)
1554 {
1555     bd_main_t * bdm = &bd_main;
1556     l2input_main_t * l2im = &l2input_main;
1557     unix_shared_memory_queue_t * q;
1558     l2_bridge_domain_t * bd_config;
1559     u32 bd_id, bd_index;
1560     u32 end;
1561
1562     q = vl_api_client_index_to_input_queue (mp->client_index);
1563
1564     if (q == 0)
1565         return;
1566     
1567     bd_id = ntohl(mp->bd_id);
1568
1569     bd_index = (bd_id == ~0) ? 0 : bd_find_or_add_bd_index (bdm, bd_id);
1570     end = (bd_id == ~0) ? vec_len (l2im->bd_configs) : bd_index + 1;
1571     for (; bd_index < end; bd_index++) {
1572         bd_config = l2input_bd_config_from_index (l2im, bd_index);
1573         /* skip dummy bd_id 0 */
1574         if (bd_config && (bd_config->bd_id > 0)) {
1575             u32 n_sw_ifs;
1576             l2_flood_member_t * m;
1577             
1578             n_sw_ifs = vec_len (bd_config->members);
1579             send_bridge_domain_details (q, bd_config, n_sw_ifs, mp->context);
1580             
1581             vec_foreach (m, bd_config->members) {
1582                 send_bd_sw_if_details (l2im, q, m, bd_config->bd_id, mp->context);
1583             }
1584         }
1585     }
1586 }
1587
1588 static void 
1589 vl_api_l2fib_add_del_t_handler (
1590     vl_api_l2fib_add_del_t *mp)
1591 {
1592     bd_main_t * bdm = &bd_main;
1593     l2input_main_t * l2im = &l2input_main;
1594     vl_api_l2fib_add_del_reply_t * rmp;
1595     int rv = 0;
1596     u64 mac = 0;
1597     u32 sw_if_index = ntohl(mp->sw_if_index);
1598     u32 bd_id = ntohl(mp->bd_id);
1599     u32 bd_index;
1600     u32 static_mac;
1601     u32 filter_mac;
1602     uword * p;
1603
1604     mac = mp->mac;
1605
1606     p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1607     if (!p) {
1608         rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1609         goto bad_sw_if_index;
1610     }
1611     bd_index = p[0];
1612
1613     if (mp->is_add) {
1614         VALIDATE_SW_IF_INDEX(mp);
1615         if (vec_len(l2im->configs) <= sw_if_index) {
1616             rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1617             goto bad_sw_if_index;
1618         } else {
1619             l2_input_config_t * config;
1620             config = vec_elt_at_index(l2im->configs, sw_if_index);
1621             if (config->bridge == 0) {
1622                 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1623                 goto bad_sw_if_index;
1624             }
1625         }
1626         static_mac = mp->static_mac ? 1 : 0;
1627         filter_mac = mp->filter_mac ? 1 : 0;
1628         l2fib_add_entry(mac, bd_index, sw_if_index, static_mac, filter_mac,
1629                         0 /* bvi_mac */);
1630     } else {
1631         l2fib_del_entry(mac, bd_index);
1632     }
1633
1634     BAD_SW_IF_INDEX_LABEL;
1635
1636     REPLY_MACRO(VL_API_L2FIB_ADD_DEL_REPLY);
1637 }
1638
1639 static void 
1640 vl_api_l2_flags_t_handler (
1641     vl_api_l2_flags_t *mp)
1642 {
1643     vl_api_l2_flags_reply_t * rmp;
1644     int rv = 0;
1645     u32 sw_if_index = ntohl(mp->sw_if_index);
1646     u32 flags = ntohl(mp->feature_bitmap);
1647     u32 rbm = 0;
1648
1649     VALIDATE_SW_IF_INDEX(mp);
1650
1651 #define _(a,b) \
1652     if (flags & L2INPUT_FEAT_ ## a) \
1653         rbm = l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_ ## a, mp->is_set);
1654     foreach_l2input_feat;
1655 #undef _
1656
1657     BAD_SW_IF_INDEX_LABEL;
1658
1659     REPLY_MACRO2(VL_API_L2_FLAGS_REPLY, rmp->resulting_feature_bitmap = ntohl(rbm));
1660 }
1661
1662 static void 
1663 vl_api_bridge_flags_t_handler (
1664     vl_api_bridge_flags_t *mp)
1665 {
1666     vlib_main_t *vm  = vlib_get_main();
1667     bd_main_t * bdm = &bd_main;
1668     vl_api_bridge_flags_reply_t * rmp;
1669     int rv = 0;
1670     u32 bd_id = ntohl(mp->bd_id);
1671     u32 bd_index; 
1672     u32 flags = ntohl(mp->feature_bitmap);
1673     uword * p;
1674
1675     p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1676     if (p == 0) {
1677         rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1678         goto out;
1679     }
1680  
1681     bd_index = p[0];
1682
1683     bd_set_flags(vm, bd_index, flags, mp->is_set);
1684
1685 out:
1686     REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
1687                  rmp->resulting_feature_bitmap = ntohl(flags));
1688 }
1689
1690 static void 
1691 vl_api_bd_ip_mac_add_del_t_handler (
1692     vl_api_bd_ip_mac_add_del_t *mp)
1693 {
1694     bd_main_t * bdm = &bd_main;
1695     vl_api_bd_ip_mac_add_del_reply_t * rmp;
1696     int rv = 0;
1697     u32 bd_id = ntohl(mp->bd_id);
1698     u32 bd_index; 
1699     uword * p;
1700
1701     p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1702     if (p == 0) {
1703         rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1704         goto out;
1705     }
1706  
1707     bd_index = p[0];
1708     if (bd_add_del_ip_mac(bd_index,  mp->ip_address, 
1709                           mp->mac_address, mp->is_ipv6, mp->is_add))
1710         rv = VNET_API_ERROR_UNSPECIFIED;
1711
1712 out:
1713     REPLY_MACRO(VL_API_BD_IP_MAC_ADD_DEL_REPLY);
1714 }
1715
1716 static void
1717 vl_api_tap_connect_t_handler (vl_api_tap_connect_t *mp, vlib_main_t *vm)
1718 {
1719     int rv;
1720     vl_api_tap_connect_reply_t * rmp;
1721     unix_shared_memory_queue_t * q;
1722     u32 sw_if_index = (u32)~0;
1723
1724     rv = vnet_tap_connect_renumber (vm, mp->tap_name, 
1725                            mp->use_random_mac ? 0 : mp->mac_address,
1726                            &sw_if_index, mp->renumber,
1727                            ntohl(mp->custom_dev_instance));
1728     
1729     q = vl_api_client_index_to_input_queue (mp->client_index);
1730     if (!q)
1731         return;
1732
1733     rmp = vl_msg_api_alloc (sizeof (*rmp));
1734     rmp->_vl_msg_id = ntohs(VL_API_TAP_CONNECT_REPLY);
1735     rmp->context = mp->context;
1736     rmp->retval = ntohl(rv);
1737     rmp->sw_if_index = ntohl(sw_if_index);
1738
1739     vl_msg_api_send_shmem (q, (u8 *)&rmp);
1740 }
1741
1742 static void
1743 vl_api_tap_modify_t_handler (vl_api_tap_modify_t *mp, vlib_main_t *vm)
1744 {
1745     int rv;
1746     vl_api_tap_modify_reply_t * rmp;
1747     unix_shared_memory_queue_t * q;
1748     u32 sw_if_index = (u32)~0;
1749
1750     rv = vnet_tap_modify (vm, ntohl(mp->sw_if_index), mp->tap_name,
1751                            mp->use_random_mac ? 0 : mp->mac_address,
1752                            &sw_if_index, mp->renumber,
1753                            ntohl(mp->custom_dev_instance));
1754     
1755     q = vl_api_client_index_to_input_queue (mp->client_index);
1756     if (!q)
1757         return;
1758
1759     rmp = vl_msg_api_alloc (sizeof (*rmp));
1760     rmp->_vl_msg_id = ntohs(VL_API_TAP_MODIFY_REPLY);
1761     rmp->context = mp->context;
1762     rmp->retval = ntohl(rv);
1763     rmp->sw_if_index = ntohl(sw_if_index);
1764
1765     vl_msg_api_send_shmem (q, (u8 *)&rmp);
1766 }
1767
1768 static void
1769 vl_api_tap_delete_t_handler (vl_api_tap_delete_t *mp, vlib_main_t *vm)
1770 {
1771     int rv;
1772     vpe_api_main_t * vam = &vpe_api_main;
1773     vl_api_tap_delete_reply_t * rmp;
1774     unix_shared_memory_queue_t * q;
1775     u32 sw_if_index = ntohl(mp->sw_if_index);
1776
1777     rv = vnet_tap_delete (vm, sw_if_index);
1778
1779     q = vl_api_client_index_to_input_queue (mp->client_index);
1780     if (!q)
1781         return;
1782
1783     rmp = vl_msg_api_alloc (sizeof (*rmp));
1784     rmp->_vl_msg_id = ntohs(VL_API_TAP_DELETE_REPLY);
1785     rmp->context = mp->context;
1786     rmp->retval = ntohl(rv);
1787
1788     vl_msg_api_send_shmem (q, (u8 *)&rmp);
1789
1790     if (!rv)
1791         send_sw_interface_flags_deleted (vam, q, sw_if_index);
1792 }
1793
1794 static void
1795 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
1796 {
1797     vl_api_create_vlan_subif_reply_t * rmp;
1798     vnet_main_t * vnm = vnet_get_main();
1799     u32 hw_if_index, sw_if_index = (u32)~0;
1800     vnet_hw_interface_t * hi;
1801     int rv = 0;
1802     u32 id;
1803     vnet_sw_interface_t template;
1804     uword * p;
1805     vnet_interface_main_t * im = &vnm->interface_main;
1806     u64 sup_and_sub_key;
1807     u64 * kp;
1808     unix_shared_memory_queue_t * q;
1809     clib_error_t * error;
1810
1811     VALIDATE_SW_IF_INDEX(mp);
1812
1813     hw_if_index = ntohl(mp->sw_if_index);
1814     hi = vnet_get_hw_interface (vnm, hw_if_index);
1815
1816     id = ntohl(mp->vlan_id);
1817     if (id == 0 || id > 4095) {
1818         rv = VNET_API_ERROR_INVALID_VLAN;
1819         goto out;
1820     }
1821       
1822     sup_and_sub_key = ((u64)(hi->sw_if_index) << 32) | (u64) id;
1823   
1824     p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1825     if (p) {
1826         rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
1827         goto out;
1828     }
1829   
1830     kp = clib_mem_alloc (sizeof (*kp));
1831     *kp = sup_and_sub_key;
1832     
1833     memset (&template, 0, sizeof (template));
1834     template.type = VNET_SW_INTERFACE_TYPE_SUB;
1835     template.sup_sw_if_index = hi->sw_if_index;
1836     template.sub.id = id;
1837     template.sub.eth.raw_flags = 0;
1838     template.sub.eth.flags.one_tag = 1;
1839     template.sub.eth.outer_vlan_id = id;
1840     template.sub.eth.flags.exact_match = 1;
1841
1842     error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1843     if (error) {
1844         clib_error_report(error);
1845         rv = VNET_API_ERROR_INVALID_REGISTRATION;
1846         goto out;
1847     }
1848     hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
1849     hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1850   
1851     BAD_SW_IF_INDEX_LABEL;
1852
1853 out:
1854     q = vl_api_client_index_to_input_queue (mp->client_index);
1855     if (!q)
1856         return;
1857     
1858     rmp = vl_msg_api_alloc (sizeof (*rmp));
1859     rmp->_vl_msg_id = ntohs(VL_API_CREATE_VLAN_SUBIF_REPLY);
1860     rmp->context = mp->context;
1861     rmp->retval = ntohl(rv);
1862     rmp->sw_if_index = ntohl(sw_if_index);
1863     vl_msg_api_send_shmem (q, (u8 *)&rmp);
1864 }
1865
1866 static void
1867 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
1868 {
1869     vl_api_create_subif_reply_t * rmp;
1870     vnet_main_t * vnm = vnet_get_main();
1871     u32 sw_if_index = ~0;
1872     int rv = 0;
1873     u32 sub_id;
1874     vnet_sw_interface_t *si;
1875     vnet_hw_interface_t *hi;
1876     vnet_sw_interface_t template;
1877     uword * p;
1878     vnet_interface_main_t * im = &vnm->interface_main;
1879     u64 sup_and_sub_key;
1880     u64 * kp;
1881     clib_error_t * error;
1882
1883     VALIDATE_SW_IF_INDEX(mp);
1884
1885     si = vnet_get_sup_sw_interface (vnm, ntohl(mp->sw_if_index));
1886     hi = vnet_get_sup_hw_interface (vnm, ntohl(mp->sw_if_index));
1887
1888     if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE) {
1889          rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1890         goto out;
1891     }
1892
1893     sw_if_index = si->sw_if_index;
1894     sub_id = ntohl(mp->sub_id);
1895     
1896     sup_and_sub_key = ((u64)(sw_if_index) << 32) | (u64) sub_id;
1897
1898     p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1899     if (p) {
1900         if (CLIB_DEBUG > 0)
1901             clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
1902                           sw_if_index, sub_id);
1903         rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
1904         goto out;
1905     }
1906
1907     kp = clib_mem_alloc (sizeof (*kp));
1908     *kp = sup_and_sub_key;
1909     
1910     memset (&template, 0, sizeof (template));
1911     template.type = VNET_SW_INTERFACE_TYPE_SUB;
1912     template.sup_sw_if_index = sw_if_index;
1913     template.sub.id = sub_id;
1914     template.sub.eth.flags.no_tags = mp->no_tags;
1915     template.sub.eth.flags.one_tag = mp->one_tag;
1916     template.sub.eth.flags.two_tags = mp->two_tags;
1917     template.sub.eth.flags.dot1ad = mp->dot1ad;
1918     template.sub.eth.flags.exact_match = mp->exact_match;
1919     template.sub.eth.flags.default_sub = mp->default_sub;
1920     template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
1921     template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
1922     template.sub.eth.outer_vlan_id = ntohs(mp->outer_vlan_id);
1923     template.sub.eth.inner_vlan_id = ntohs(mp->inner_vlan_id);
1924     
1925     error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1926     if (error) {
1927         clib_error_report (error);
1928         rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
1929         goto out;
1930     }
1931         
1932     hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
1933     hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1934     
1935     BAD_SW_IF_INDEX_LABEL;
1936     
1937 out:
1938     
1939     REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY, 
1940     ({
1941         rmp->sw_if_index = ntohl(sw_if_index);
1942     }));
1943 }
1944
1945 static void
1946 vl_api_mpls_gre_add_del_tunnel_t_handler (vl_api_mpls_gre_add_del_tunnel_t *mp)
1947 {
1948     vl_api_mpls_gre_add_del_tunnel_reply_t * rmp;
1949     int rv = 0;
1950     stats_main_t * sm = &stats_main;
1951     u32 tunnel_sw_if_index = ~0;
1952
1953     dslock (sm, 1 /* release hint */, 5 /* tag */);
1954
1955     rv = vnet_mpls_gre_add_del_tunnel ((ip4_address_t *)(mp->src_address),
1956                                        (ip4_address_t *)(mp->dst_address),
1957                                        (ip4_address_t *)(mp->intfc_address),
1958                                        (u32)(mp->intfc_address_length),
1959                                        ntohl(mp->inner_vrf_id),
1960                                        ntohl(mp->outer_vrf_id),
1961                                        &tunnel_sw_if_index,
1962                                        mp->l2_only,
1963                                        mp->is_add);
1964     dsunlock (sm);
1965     
1966     REPLY_MACRO2(VL_API_MPLS_GRE_ADD_DEL_TUNNEL_REPLY,
1967     ({
1968         rmp->tunnel_sw_if_index = ntohl(tunnel_sw_if_index);
1969     }));
1970 }
1971
1972 static void
1973 vl_api_mpls_ethernet_add_del_tunnel_t_handler 
1974 (vl_api_mpls_ethernet_add_del_tunnel_t *mp)
1975 {
1976     vl_api_mpls_ethernet_add_del_tunnel_reply_t * rmp;
1977     int rv = 0;
1978     stats_main_t * sm = &stats_main;
1979     u32 tunnel_sw_if_index;
1980
1981     dslock (sm, 1 /* release hint */, 5 /* tag */);
1982
1983     rv = vnet_mpls_ethernet_add_del_tunnel 
1984         (mp->dst_mac_address, (ip4_address_t *)(mp->adj_address),
1985          (u32)(mp->adj_address_length), ntohl(mp->vrf_id), 
1986          ntohl(mp->tx_sw_if_index),
1987          &tunnel_sw_if_index,
1988          mp->l2_only,
1989          mp->is_add);
1990
1991     dsunlock (sm);
1992     
1993     REPLY_MACRO2(VL_API_MPLS_ETHERNET_ADD_DEL_TUNNEL_REPLY,
1994     ({
1995         rmp->tunnel_sw_if_index = ntohl(tunnel_sw_if_index);
1996     }));
1997 }
1998
1999 /* 
2000  * This piece of misery brought to you because the control-plane
2001  * can't figure out the tx interface + dst-mac address all by itself
2002  */
2003 static int mpls_ethernet_add_del_tunnel_2_t_handler 
2004 (vl_api_mpls_ethernet_add_del_tunnel_2_t *mp)
2005 {
2006     pending_route_t * pr;
2007     vl_api_mpls_ethernet_add_del_tunnel_2_t *pme;
2008     vnet_main_t * vnm = vnet_get_main();
2009     vlib_main_t * vm = vlib_get_main();
2010     stats_main_t * sm = &stats_main;
2011     vpe_api_main_t * vam = &vpe_api_main;
2012     u32 inner_fib_index, outer_fib_index;
2013     ip4_main_t * im = &ip4_main;
2014     ip_lookup_main_t * lm = &im->lookup_main;
2015     ip_adjacency_t * adj = 0;
2016     u32 lookup_result;
2017     u32 tx_sw_if_index;
2018     u8 * dst_mac_address;
2019     clib_error_t * e;
2020     uword * p;
2021     int rv;
2022     u32 tunnel_sw_if_index;
2023     
2024     p = hash_get (im->fib_index_by_table_id, ntohl(mp->outer_vrf_id));
2025     if (!p) 
2026         return VNET_API_ERROR_NO_SUCH_FIB;
2027     else 
2028         outer_fib_index = p[0];
2029     
2030     
2031     p = hash_get (im->fib_index_by_table_id, ntohl(mp->inner_vrf_id));
2032     if (!p) 
2033         return VNET_API_ERROR_NO_SUCH_INNER_FIB;
2034     else 
2035         inner_fib_index = p[0];
2036     
2037     if (inner_fib_index == outer_fib_index)
2038         return VNET_API_ERROR_INVALID_VALUE;
2039
2040     lookup_result = ip4_fib_lookup_with_table 
2041         (im, outer_fib_index, 
2042          (ip4_address_t *)mp->next_hop_ip4_address_in_outer_vrf, 
2043          1 /* disable default route */);
2044     
2045     adj = ip_get_adjacency (lm, lookup_result);
2046     tx_sw_if_index = adj->rewrite_header.sw_if_index;
2047
2048     if (mp->is_add && mp->resolve_if_needed) {
2049         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP) {
2050             pool_get (vam->pending_routes, pr);
2051             pr->resolve_type = RESOLVE_MPLS_ETHERNET_ADD_DEL;
2052             pme = &pr->t;
2053             clib_memcpy (pme, mp, sizeof (*pme));
2054             /* recursion block, "just in case" */
2055             pme->resolve_if_needed = 0;
2056             pme->resolve_attempts = ntohl(mp->resolve_attempts);
2057             pme->resolve_opaque = tx_sw_if_index;
2058             vnet_register_ip4_arp_resolution_event 
2059                 (vnm, 
2060                  (ip4_address_t *)&(pme->next_hop_ip4_address_in_outer_vrf),
2061                  vpe_resolver_process_node.index,
2062                  RESOLUTION_EVENT, pr - vam->pending_routes);
2063
2064             vlib_process_signal_event 
2065                 (vm, vpe_resolver_process_node.index,
2066                  RESOLUTION_PENDING_EVENT, 0 /* data */);
2067
2068             /* The interface may be down, etc. */
2069             e = ip4_probe_neighbor 
2070                 (vm, (ip4_address_t *)&(mp->next_hop_ip4_address_in_outer_vrf),
2071                  tx_sw_if_index);
2072
2073             if (e)
2074                 clib_error_report(e);
2075             
2076             return VNET_API_ERROR_IN_PROGRESS;
2077         }
2078     }
2079         
2080     if (adj->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
2081         return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
2082     
2083     dst_mac_address = 
2084         vnet_rewrite_get_data_internal
2085         (&adj->rewrite_header, sizeof (adj->rewrite_data));
2086     
2087     dslock (sm, 1 /* release hint */, 10 /* tag */);
2088
2089     rv = vnet_mpls_ethernet_add_del_tunnel 
2090         (dst_mac_address, (ip4_address_t *)(mp->adj_address),
2091          (u32)(mp->adj_address_length), ntohl(mp->inner_vrf_id), 
2092          tx_sw_if_index, &tunnel_sw_if_index, mp->l2_only, mp->is_add);
2093
2094     dsunlock (sm);
2095
2096     return rv;
2097 }
2098
2099 static void
2100 vl_api_mpls_ethernet_add_del_tunnel_2_t_handler 
2101 (vl_api_mpls_ethernet_add_del_tunnel_2_t *mp)
2102 {
2103     vl_api_mpls_ethernet_add_del_tunnel_reply_t * rmp;
2104     int rv = 0;
2105
2106     rv = mpls_ethernet_add_del_tunnel_2_t_handler (mp);
2107     
2108     REPLY_MACRO(VL_API_MPLS_ETHERNET_ADD_DEL_TUNNEL_2_REPLY);
2109 }
2110
2111
2112 static void
2113 vl_api_mpls_add_del_encap_t_handler (vl_api_mpls_add_del_encap_t *mp)
2114 {
2115     vl_api_mpls_add_del_encap_reply_t * rmp;
2116     int rv;
2117     static u32 * labels;
2118     int i;
2119
2120     vec_reset_length (labels);
2121
2122     for (i = 0; i < mp->nlabels; i++)
2123         vec_add1 (labels, ntohl(mp->labels[i]));
2124
2125     /* $$$$ fixme */
2126     rv = vnet_mpls_add_del_encap ((ip4_address_t *)mp->dst_address,
2127                                   ntohl(mp->vrf_id), labels, 
2128                                   ~0 /* policy_tunnel_index */,
2129                                   0 /* no_dst_hash */, 
2130                                   0 /* indexp */, 
2131                                   mp->is_add);
2132     
2133     REPLY_MACRO(VL_API_MPLS_ADD_DEL_ENCAP_REPLY);
2134 }
2135
2136 static void
2137 vl_api_mpls_add_del_decap_t_handler 
2138 (vl_api_mpls_add_del_decap_t *mp)
2139 {
2140     vl_api_mpls_add_del_decap_reply_t * rmp;
2141     int rv;
2142
2143     rv = vnet_mpls_add_del_decap (ntohl(mp->rx_vrf_id), ntohl(mp->tx_vrf_id),
2144                                   ntohl(mp->label), ntohl(mp->next_index),
2145                                   mp->s_bit, mp->is_add);
2146     
2147     REPLY_MACRO(VL_API_MPLS_ADD_DEL_DECAP_REPLY);
2148 }
2149
2150 static void
2151 vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t *mp)
2152 {
2153     vl_api_proxy_arp_add_del_reply_t * rmp;
2154     u32 fib_index;
2155     int rv;
2156     ip4_main_t * im = &ip4_main;
2157     stats_main_t * sm = &stats_main;
2158     int vnet_proxy_arp_add_del (ip4_address_t *lo_addr,
2159                                 ip4_address_t *hi_addr,
2160                                 u32 fib_index, int is_del);
2161     uword * p;
2162
2163     dslock (sm, 1 /* release hint */, 6 /* tag */);
2164
2165     p = hash_get (im->fib_index_by_table_id, ntohl(mp->vrf_id));
2166
2167     if (! p) {
2168         rv = VNET_API_ERROR_NO_SUCH_FIB;
2169         goto out;
2170     }
2171
2172     fib_index = p[0];
2173
2174     rv = vnet_proxy_arp_add_del ((ip4_address_t *)mp->low_address,
2175                                  (ip4_address_t *)mp->hi_address,
2176                                  fib_index, mp->is_add == 0);
2177     
2178 out:
2179     dsunlock (sm);
2180     REPLY_MACRO(VL_API_PROXY_ARP_ADD_DEL_REPLY);
2181 }
2182
2183 static void
2184 vl_api_proxy_arp_intfc_enable_disable_t_handler 
2185 (vl_api_proxy_arp_intfc_enable_disable_t *mp)
2186 {
2187     int rv = 0;
2188     vnet_main_t * vnm = vnet_get_main();
2189     vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
2190     vnet_sw_interface_t * si;
2191     u32 sw_if_index;
2192     
2193     VALIDATE_SW_IF_INDEX(mp);
2194
2195     sw_if_index = ntohl(mp->sw_if_index);
2196
2197     if (pool_is_free_index (vnm->interface_main.sw_interfaces, 
2198                             sw_if_index)) {
2199         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2200         goto out;
2201     }
2202
2203     si = vnet_get_sw_interface (vnm, sw_if_index);
2204
2205     ASSERT(si);
2206
2207     if (mp->enable_disable)
2208         si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2209     else 
2210         si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2211
2212     BAD_SW_IF_INDEX_LABEL;
2213
2214     out:    
2215     REPLY_MACRO(VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
2216 }
2217
2218 static void
2219 vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t *mp, vlib_main_t * vm)
2220 {
2221     vl_api_ip_neighbor_add_del_reply_t * rmp;
2222     vnet_main_t * vnm = vnet_get_main();
2223     u32 fib_index;
2224     int rv=0;
2225     stats_main_t * sm = &stats_main;
2226
2227     VALIDATE_SW_IF_INDEX(mp);
2228
2229     dslock (sm, 1 /* release hint */, 7 /* tag */);
2230
2231     if (mp->is_ipv6) {
2232         if (mp->is_add)
2233             rv = vnet_set_ip6_ethernet_neighbor 
2234                 (vm, ntohl(mp->sw_if_index),
2235                  (ip6_address_t *)(mp->dst_address), 
2236                  mp->mac_address, sizeof (mp->mac_address), mp->is_static);
2237         else
2238             rv = vnet_unset_ip6_ethernet_neighbor 
2239                 (vm, ntohl(mp->sw_if_index),
2240                  (ip6_address_t *)(mp->dst_address), 
2241                  mp->mac_address, sizeof(mp->mac_address));
2242     } else {
2243         ip4_main_t * im = &ip4_main;
2244         ip_lookup_main_t * lm = &im->lookup_main;
2245         ethernet_arp_ip4_over_ethernet_address_t a;
2246         u32 ai;
2247         ip_adjacency_t *nh_adj;
2248
2249         uword * p = hash_get (im->fib_index_by_table_id, ntohl(mp->vrf_id));
2250         if (! p) {
2251             rv = VNET_API_ERROR_NO_SUCH_FIB;
2252             goto out;
2253         }
2254         fib_index = p[0];
2255
2256         /* 
2257          * Unfortunately, folks have a penchant for
2258          * adding interface addresses to the ARP cache, and
2259          * wondering why the forwarder eventually ASSERTs...
2260          */
2261         ai = ip4_fib_lookup_with_table 
2262             (im, fib_index, (ip4_address_t *)(mp->dst_address), 
2263              1 /* disable default route */);
2264         
2265         if (ai != 0) {
2266             nh_adj = ip_get_adjacency (lm, ai);
2267             /* Never allow manipulation of a local adj! */
2268             if (nh_adj->lookup_next_index == IP_LOOKUP_NEXT_LOCAL) {
2269                 clib_warning("%U matches local adj",
2270                              format_ip4_address, 
2271                              (ip4_address_t *)(mp->dst_address));
2272                 rv = VNET_API_ERROR_ADDRESS_MATCHES_INTERFACE_ADDRESS;
2273                 goto out;
2274             }
2275         }
2276
2277         clib_memcpy (&a.ethernet, mp->mac_address, 6);
2278         clib_memcpy (&a.ip4, mp->dst_address, 4);
2279
2280         if (mp->is_add) 
2281             rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl(mp->sw_if_index), 
2282                                                  fib_index, &a, mp->is_static);
2283         else
2284             rv = vnet_arp_unset_ip4_over_ethernet (vnm, ntohl(mp->sw_if_index), 
2285                                                    fib_index, &a);
2286     }
2287             
2288     BAD_SW_IF_INDEX_LABEL;
2289     out:
2290     dsunlock (sm);
2291     REPLY_MACRO(VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
2292 }
2293
2294 static void 
2295 vl_api_is_address_reachable_t_handler (vl_api_is_address_reachable_t *mp)
2296 {
2297 #if 0
2298     vpe_main_t *rm = &vpe_main;
2299     ip4_main_t *im4 = &ip4_main;
2300     ip6_main_t *im6 = &ip6_main;
2301     ip_lookup_main_t * lm;
2302     union {
2303         ip4_address_t ip4;
2304         ip6_address_t ip6;
2305     } addr;
2306     u32 adj_index, sw_if_index;
2307     vl_api_is_address_reachable_t *rmp;
2308     ip_adjacency_t * adj;
2309     unix_shared_memory_queue_t *q;
2310     
2311     q = vl_api_client_index_to_input_queue (mp->client_index);
2312     if (!q) {
2313         increment_missing_api_client_counter (rm->vlib_main);
2314         return;
2315     }
2316
2317     rmp = vl_msg_api_alloc (sizeof (*rmp));
2318     clib_memcpy (rmp, mp, sizeof (*rmp));
2319
2320     sw_if_index = mp->next_hop_sw_if_index;
2321     clib_memcpy (&addr, mp->address, sizeof (addr));
2322     if (mp->is_ipv6) {
2323         lm = &im6->lookup_main;
2324         adj_index = 
2325             ip6_fib_lookup (im6, sw_if_index, &addr.ip6);
2326     } else {
2327         lm = &im4->lookup_main;
2328         adj_index = 
2329             ip4_fib_lookup (im4, sw_if_index, &addr.ip4);
2330     }
2331     if (adj_index == ~0) {
2332         rmp->is_error = 1;
2333         goto send;
2334     }
2335     adj = ip_get_adjacency (lm, adj_index);
2336
2337     if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE
2338         && adj->rewrite_header.sw_if_index == sw_if_index) {
2339         rmp->is_known = 1;
2340     } else {
2341         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP
2342             && adj->rewrite_header.sw_if_index == sw_if_index) {
2343             if (mp->is_ipv6)
2344                 ip6_probe_neighbor (rm->vlib_main, &addr.ip6, sw_if_index);
2345             else
2346                 ip4_probe_neighbor (rm->vlib_main, &addr.ip4, sw_if_index);
2347         } else if (adj->lookup_next_index == IP_LOOKUP_NEXT_DROP) {
2348             rmp->is_known = 1;
2349             goto send;
2350         }
2351         rmp->is_known = 0;
2352     }
2353
2354 send:
2355     vl_msg_api_send_shmem (q, (u8 *)&rmp);
2356 #endif
2357 }
2358
2359 static void vl_api_sw_interface_details_t_handler (
2360     vl_api_sw_interface_details_t * mp)
2361 {
2362     clib_warning ("BUG");
2363 }
2364
2365 static void vl_api_sw_interface_set_flags_t_handler (
2366     vl_api_sw_interface_set_flags_t * mp)
2367 {
2368    vl_api_sw_interface_set_flags_reply_t *rmp;
2369    vnet_main_t * vnm = vnet_get_main();
2370    int rv = 0;
2371    clib_error_t * error;
2372    u16 flags;
2373
2374    VALIDATE_SW_IF_INDEX(mp);
2375
2376    flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
2377
2378    error = vnet_sw_interface_set_flags (vnm, 
2379                                         ntohl(mp->sw_if_index),
2380                                         flags);        
2381    if (error) {
2382        rv = -1;
2383        clib_error_report (error);
2384    }
2385
2386    BAD_SW_IF_INDEX_LABEL;
2387    REPLY_MACRO(VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
2388 }
2389
2390 static void vl_api_sw_interface_clear_stats_t_handler (
2391     vl_api_sw_interface_clear_stats_t * mp)
2392 {
2393    vl_api_sw_interface_clear_stats_reply_t *rmp;
2394
2395    vnet_main_t * vnm = vnet_get_main();
2396    vnet_interface_main_t * im = &vnm->interface_main;
2397    vlib_simple_counter_main_t * sm;
2398    vlib_combined_counter_main_t * cm;
2399    static vnet_main_t ** my_vnet_mains;
2400    int i, j, n_counters;
2401
2402    int rv = 0;
2403
2404    vec_reset_length (my_vnet_mains);
2405
2406    for (i = 0; i < vec_len (vnet_mains); i++)
2407      {
2408        if (vnet_mains[i])
2409          vec_add1 (my_vnet_mains, vnet_mains[i]);
2410      }
2411
2412    if (vec_len (vnet_mains) == 0)
2413      vec_add1 (my_vnet_mains, vnm);
2414
2415    n_counters = vec_len (im->combined_sw_if_counters);
2416
2417    for (j = 0; j < n_counters; j++)
2418      {
2419        for (i = 0; i < vec_len(my_vnet_mains); i++)
2420          {
2421            im = &my_vnet_mains[i]->interface_main;
2422            cm = im->combined_sw_if_counters + j;
2423            if (mp->sw_if_index == (u32)~0)
2424              vlib_clear_combined_counters (cm);
2425            else
2426              vlib_zero_combined_counter (cm, ntohl(mp->sw_if_index));
2427          }
2428      }
2429
2430    n_counters = vec_len (im->sw_if_counters);
2431
2432    for (j = 0; j < n_counters; j++)
2433      {
2434        for (i = 0; i < vec_len(my_vnet_mains); i++)
2435          {
2436            im = &my_vnet_mains[i]->interface_main;
2437            sm = im->sw_if_counters + j;
2438            if (mp->sw_if_index == (u32)~0)
2439              vlib_clear_simple_counters (sm);
2440            else
2441              vlib_zero_simple_counter (sm, ntohl(mp->sw_if_index));
2442          }
2443      }
2444
2445    REPLY_MACRO(VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
2446 }
2447
2448 static void send_sw_interface_details (vpe_api_main_t * am,
2449                                        unix_shared_memory_queue_t *q,
2450                                        vnet_sw_interface_t * swif,
2451                                        u8 * interface_name,
2452                                        u32 context)
2453 {
2454     vl_api_sw_interface_details_t * mp;
2455     vnet_hw_interface_t * hi;
2456
2457     hi = vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
2458
2459     mp = vl_msg_api_alloc (sizeof (*mp));
2460     memset (mp, 0, sizeof (*mp));
2461     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_DETAILS);
2462     mp->sw_if_index = ntohl(swif->sw_if_index);
2463     mp->sup_sw_if_index = ntohl(swif->sup_sw_if_index);
2464     mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
2465         1 : 0;
2466     mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 
2467         1 : 0;
2468     mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
2469                        VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
2470     mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
2471                       VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
2472     mp->link_mtu = ntohs(hi->max_packet_bytes);
2473     mp->context = context;
2474
2475     strncpy ((char *) mp->interface_name, 
2476              (char *) interface_name, ARRAY_LEN(mp->interface_name)-1);
2477     
2478     /* Send the L2 address for ethernet physical intfcs */
2479     if (swif->sup_sw_if_index == swif->sw_if_index
2480         && hi->hw_class_index == ethernet_hw_interface_class.index) {
2481         ethernet_main_t *em = ethernet_get_main (am->vlib_main);
2482         ethernet_interface_t *ei;
2483         
2484         ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
2485         ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
2486         clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
2487         mp->l2_address_length = ntohl(sizeof (ei->address));
2488     } else if (swif->sup_sw_if_index != swif->sw_if_index) {
2489         vnet_sub_interface_t *sub = &swif->sub;
2490         mp->sub_id = ntohl(sub->id);
2491         mp->sub_dot1ad = sub->eth.flags.dot1ad;
2492         mp->sub_number_of_tags = sub->eth.flags.one_tag + sub->eth.flags.two_tags*2;
2493         mp->sub_outer_vlan_id = ntohs(sub->eth.outer_vlan_id);
2494         mp->sub_inner_vlan_id = ntohs(sub->eth.inner_vlan_id);
2495         mp->sub_exact_match = sub->eth.flags.exact_match;
2496         mp->sub_default = sub->eth.flags.default_sub;
2497         mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
2498         mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
2499
2500         /* vlan tag rewrite data */
2501         u32 vtr_op = L2_VTR_DISABLED;
2502         u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
2503
2504         if (l2vtr_get(am->vlib_main, am->vnet_main, swif->sw_if_index,
2505                       &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0) {
2506             // error - default to disabled
2507             mp->vtr_op = ntohl(L2_VTR_DISABLED);
2508             clib_warning("cannot get vlan tag rewrite for sw_if_index %d", 
2509                     swif->sw_if_index);
2510         } else {
2511             mp->vtr_op = ntohl(vtr_op);
2512             mp->vtr_push_dot1q = ntohl(vtr_push_dot1q);
2513             mp->vtr_tag1 = ntohl(vtr_tag1);
2514             mp->vtr_tag2 = ntohl(vtr_tag2);
2515         }
2516     }
2517
2518     vl_msg_api_send_shmem (q, (u8 *)&mp);
2519 }
2520
2521 static void send_sw_interface_flags (vpe_api_main_t * am,
2522                                      unix_shared_memory_queue_t *q,
2523                                      vnet_sw_interface_t * swif)
2524 {
2525     vl_api_sw_interface_set_flags_t *mp;
2526     vnet_main_t * vnm = am->vnet_main;
2527
2528     vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, 
2529                                                          swif->sw_if_index);
2530     mp = vl_msg_api_alloc (sizeof (*mp));
2531     memset (mp, 0, sizeof (*mp));
2532     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_SET_FLAGS);
2533     mp->sw_if_index = ntohl(swif->sw_if_index);
2534
2535     mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
2536         1 : 0;
2537     mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 
2538         1 : 0;
2539     vl_msg_api_send_shmem (q, (u8 *)&mp);
2540 }
2541
2542 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
2543                                      unix_shared_memory_queue_t *q,
2544                                              u32 sw_if_index) 
2545     __attribute__((unused));
2546
2547 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
2548                                      unix_shared_memory_queue_t *q,
2549                                      u32 sw_if_index)
2550 {
2551     vl_api_sw_interface_set_flags_t *mp;
2552
2553     mp = vl_msg_api_alloc (sizeof (*mp));
2554     memset (mp, 0, sizeof (*mp));
2555     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_SET_FLAGS);
2556     mp->sw_if_index = ntohl(sw_if_index);
2557
2558     mp->admin_up_down = 0;
2559     mp->link_up_down = 0;
2560     mp->deleted = 1;
2561     vl_msg_api_send_shmem (q, (u8 *)&mp);
2562 }
2563
2564 static void vl_api_sw_interface_dump_t_handler (
2565     vl_api_sw_interface_dump_t *mp)
2566 {
2567     vpe_api_main_t * am = &vpe_api_main;
2568     vnet_sw_interface_t * swif;
2569     vnet_interface_main_t * im = &am->vnet_main->interface_main;
2570     u8 * filter_string = 0, * name_string = 0;
2571     unix_shared_memory_queue_t * q;
2572     char * strcasestr (char *, char *); /* lnx hdr file botch */
2573
2574     q = vl_api_client_index_to_input_queue (mp->client_index);
2575
2576     if (q == 0)
2577         return;
2578     
2579     if (mp->name_filter_valid) {
2580         mp->name_filter [ARRAY_LEN(mp->name_filter)-1] = 0;
2581         filter_string = format (0, "%s%c", mp->name_filter, 0);
2582     }
2583
2584     pool_foreach (swif, im->sw_interfaces, 
2585     ({
2586         name_string = format (name_string, "%U%c", 
2587                               format_vnet_sw_interface_name,
2588                               am->vnet_main, swif, 0);
2589
2590         if (mp->name_filter_valid == 0 ||
2591             strcasestr((char *) name_string, (char *) filter_string)) {
2592
2593             send_sw_interface_details (am, q, swif, name_string, mp->context);
2594             send_sw_interface_flags (am, q, swif);
2595         }
2596         _vec_len (name_string) = 0;
2597     }));
2598
2599     vec_free (name_string);
2600     vec_free (filter_string);
2601 }
2602
2603 void send_oam_event (oam_target_t * t)
2604 {
2605     vpe_api_main_t * vam = &vpe_api_main;
2606     unix_shared_memory_queue_t * q;
2607     vpe_client_registration_t *reg;
2608     vl_api_oam_event_t * mp;
2609
2610     pool_foreach(reg, vam->oam_events_registrations, 
2611     ({
2612         q = vl_api_client_index_to_input_queue (reg->client_index);
2613         if (q) {
2614             mp = vl_msg_api_alloc (sizeof (*mp));
2615             mp->_vl_msg_id = ntohs (VL_API_OAM_EVENT);
2616             clib_memcpy (mp->dst_address, &t->dst_address, sizeof (mp->dst_address));
2617             mp->state = t->state;
2618             vl_msg_api_send_shmem (q, (u8 *)&mp);
2619         }
2620     }));
2621 }
2622
2623 static void
2624 vl_api_oam_add_del_t_handler (vl_api_oam_add_del_t *mp)
2625 {
2626     vl_api_oam_add_del_reply_t * rmp;
2627     int rv;
2628
2629     rv = vpe_oam_add_del_target ((ip4_address_t *)mp->src_address,
2630                                  (ip4_address_t *)mp->dst_address,
2631                                  ntohl(mp->vrf_id),
2632                                  (int)(mp->is_add));
2633
2634     REPLY_MACRO(VL_API_OAM_ADD_DEL_REPLY);
2635 }
2636
2637 static void
2638 vl_api_vnet_get_summary_stats_t_handler (
2639     vl_api_vnet_get_summary_stats_t *mp)
2640 {
2641     stats_main_t * sm = &stats_main;
2642     vnet_interface_main_t * im = sm->interface_main;
2643     vl_api_vnet_summary_stats_reply_t *rmp;
2644     vlib_combined_counter_main_t * cm;
2645     vlib_counter_t v;
2646     int i, which;
2647     u64 total_pkts[VLIB_N_RX_TX];
2648     u64 total_bytes[VLIB_N_RX_TX];
2649
2650     unix_shared_memory_queue_t * q =
2651         vl_api_client_index_to_input_queue (mp->client_index);
2652     
2653     if (!q)
2654         return;
2655     
2656     rmp = vl_msg_api_alloc (sizeof (*rmp));
2657     rmp->_vl_msg_id = ntohs(VL_API_VNET_SUMMARY_STATS_REPLY);
2658     rmp->context = mp->context;
2659     rmp->retval = 0;
2660
2661     memset (total_pkts, 0, sizeof (total_pkts));
2662     memset (total_bytes, 0, sizeof (total_bytes));
2663
2664     vnet_interface_counter_lock (im);
2665
2666     vec_foreach (cm, im->combined_sw_if_counters) {
2667         which = cm - im->combined_sw_if_counters;
2668
2669         for (i = 0; i < vec_len (cm->maxi); i++) {
2670             vlib_get_combined_counter (cm, i, &v);
2671             total_pkts[which] += v.packets;
2672             total_bytes[which] += v.bytes;
2673         }
2674     }
2675     vnet_interface_counter_unlock (im);
2676
2677     /* Note: in HOST byte order! */
2678     rmp->total_pkts[VLIB_RX] = total_pkts[VLIB_RX];
2679     rmp->total_bytes[VLIB_RX] = total_bytes[VLIB_RX];
2680     rmp->total_pkts[VLIB_TX] = total_pkts[VLIB_TX];
2681     rmp->total_bytes[VLIB_TX] = total_bytes[VLIB_TX];
2682     rmp->vector_rate = vlib_last_vector_length_per_node (sm->vlib_main);
2683
2684     vl_msg_api_send_shmem (q, (u8 *)&rmp);
2685 }
2686
2687 typedef CLIB_PACKED (struct {
2688   ip4_address_t address;
2689
2690   u32 address_length : 6;
2691
2692   u32 index : 26;
2693 }) ip4_route_t;
2694
2695 static int ip4_reset_fib_t_handler (vl_api_reset_fib_t *mp)
2696 {
2697     vnet_main_t * vnm = vnet_get_main();
2698     vnet_interface_main_t * im = &vnm->interface_main;
2699     ip4_main_t * im4 = &ip4_main;
2700     static ip4_route_t * routes;
2701     static u32 * sw_if_indices_to_shut;
2702     stats_main_t * sm = &stats_main;
2703     ip4_route_t * r;
2704     ip4_fib_t * fib;
2705     u32 sw_if_index;
2706     int i;
2707     int rv = VNET_API_ERROR_NO_SUCH_FIB;
2708     u32 target_fib_id = ntohl(mp->vrf_id);
2709
2710     dslock (sm, 1 /* release hint */, 8 /* tag */);
2711
2712     vec_foreach (fib, im4->fibs) {
2713         vnet_sw_interface_t * si;
2714
2715         if (fib->table_id != target_fib_id)
2716             continue;
2717         
2718         /* remove any mpls/gre tunnels in this fib */
2719         vnet_mpls_gre_delete_fib_tunnels (fib->table_id);
2720
2721         /* remove any mpls encap/decap labels */
2722         mpls_fib_reset_labels (fib->table_id);
2723
2724         /* remove any proxy arps in this fib */
2725         vnet_proxy_arp_fib_reset (fib->table_id);
2726
2727         /* Set the flow hash for this fib to the default */
2728         vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2729
2730         vec_reset_length (sw_if_indices_to_shut);
2731
2732         /* Shut down interfaces in this FIB / clean out intfc routes */
2733         pool_foreach (si, im->sw_interfaces,
2734         ({
2735             u32 sw_if_index = si->sw_if_index;
2736
2737             if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
2738                 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
2739                     fib - im4->fibs))
2740                 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2741         }));
2742                       
2743         for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2744             sw_if_index = sw_if_indices_to_shut[i];
2745             // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
2746
2747             u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2748             flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2749             vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2750         }
2751
2752         vec_reset_length (routes);
2753
2754         for (i = 0; i < ARRAY_LEN (fib->adj_index_by_dst_address); i++) {
2755             uword * hash = fib->adj_index_by_dst_address[i];
2756             hash_pair_t * p;
2757             ip4_route_t x;
2758
2759             x.address_length = i;
2760
2761             hash_foreach_pair (p, hash, 
2762             ({
2763                 x.address.data_u32 = p->key;
2764                 vec_add1 (routes, x);
2765             }));
2766         }
2767       
2768         vec_foreach (r, routes) {
2769             ip4_add_del_route_args_t a;
2770
2771             memset (&a, 0, sizeof (a));
2772             a.flags = IP4_ROUTE_FLAG_FIB_INDEX | IP4_ROUTE_FLAG_DEL;
2773             a.table_index_or_table_id = fib - im4->fibs;
2774             a.dst_address = r->address;
2775             a.dst_address_length = r->address_length;
2776             a.adj_index = ~0;
2777
2778             ip4_add_del_route (im4, &a);
2779             ip4_maybe_remap_adjacencies (im4, fib - im4->fibs, 
2780                                          IP4_ROUTE_FLAG_FIB_INDEX);
2781         } 
2782         rv = 0;
2783         break;
2784     } /* vec_foreach (fib) */
2785
2786     dsunlock(sm);
2787     return rv;
2788 }
2789
2790 typedef struct {
2791   ip6_address_t address;
2792   u32 address_length;
2793   u32 index;
2794 } ip6_route_t;
2795
2796 typedef struct {
2797   u32 fib_index;
2798   ip6_route_t ** routep;
2799 } add_routes_in_fib_arg_t;
2800
2801 static void add_routes_in_fib (clib_bihash_kv_24_8_t * kvp, void *arg)
2802 {
2803   add_routes_in_fib_arg_t * ap = arg;
2804
2805   if (kvp->key[2]>>32 == ap->fib_index)
2806     {
2807       ip6_address_t *addr;
2808       ip6_route_t * r;
2809       addr = (ip6_address_t *) kvp;
2810       vec_add2 (*ap->routep, r, 1);
2811       r->address = addr[0];
2812       r->address_length = kvp->key[2] & 0xFF;
2813       r->index = kvp->value;
2814     }
2815 }
2816
2817 static int ip6_reset_fib_t_handler (vl_api_reset_fib_t *mp)
2818 {
2819     vnet_main_t * vnm = vnet_get_main();
2820     vnet_interface_main_t * im = &vnm->interface_main;
2821     ip6_main_t * im6 = &ip6_main;
2822     stats_main_t * sm = &stats_main;
2823     static ip6_route_t * routes;
2824     static u32 * sw_if_indices_to_shut;
2825     ip6_route_t * r;
2826     ip6_fib_t * fib;
2827     u32 sw_if_index;
2828     int i;
2829     int rv = VNET_API_ERROR_NO_SUCH_FIB;
2830     u32 target_fib_id = ntohl(mp->vrf_id);
2831     add_routes_in_fib_arg_t _a, *a=&_a;
2832     clib_bihash_24_8_t * h = &im6->ip6_lookup_table;
2833
2834     dslock (sm, 1 /* release hint */, 9 /* tag */);
2835
2836     vec_foreach (fib, im6->fibs) {
2837         vnet_sw_interface_t * si;
2838
2839         if (fib->table_id != target_fib_id)
2840             continue;
2841
2842         vec_reset_length (sw_if_indices_to_shut);
2843
2844         /* Shut down interfaces in this FIB / clean out intfc routes */
2845         pool_foreach (si, im->sw_interfaces,
2846         ({
2847             if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
2848                 fib - im6->fibs)
2849                 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2850         }));
2851                       
2852         for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2853             sw_if_index = sw_if_indices_to_shut[i];
2854             // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
2855
2856             u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2857             flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2858             vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2859         }
2860
2861         vec_reset_length (routes);
2862
2863         a->fib_index = fib - im6->fibs;
2864         a->routep = &routes;
2865
2866         clib_bihash_foreach_key_value_pair_24_8 (h, add_routes_in_fib, a);
2867
2868         vec_foreach (r, routes) {
2869             ip6_add_del_route_args_t a;
2870
2871             memset (&a, 0, sizeof (a));
2872             a.flags = IP6_ROUTE_FLAG_FIB_INDEX | IP6_ROUTE_FLAG_DEL;
2873             a.table_index_or_table_id = fib - im6->fibs;
2874             a.dst_address = r->address;
2875             a.dst_address_length = r->address_length;
2876             a.adj_index = ~0;
2877
2878             ip6_add_del_route (im6, &a);
2879             ip6_maybe_remap_adjacencies (im6, fib - im6->fibs, 
2880                                          IP6_ROUTE_FLAG_FIB_INDEX);
2881         } 
2882         rv = 0;
2883         /* Reinstall the neighbor / router discovery routes */
2884         vnet_ip6_fib_init (im6, fib - im6->fibs);
2885         break;
2886     } /* vec_foreach (fib) */
2887
2888     dsunlock(sm);
2889     return rv;
2890 }
2891
2892 static void vl_api_reset_fib_t_handler (vl_api_reset_fib_t *mp)
2893 {
2894     int rv;
2895     vl_api_reset_fib_reply_t * rmp;
2896
2897     if (mp->is_ipv6)
2898         rv = ip6_reset_fib_t_handler (mp);
2899     else
2900         rv = ip4_reset_fib_t_handler (mp);
2901     
2902     REPLY_MACRO(VL_API_RESET_FIB_REPLY);
2903 }
2904
2905
2906 static void
2907 dhcpv4_proxy_config (vl_api_dhcp_proxy_config_t *mp)
2908 {
2909     vl_api_dhcp_proxy_config_reply_t * rmp;
2910     int rv;
2911
2912     rv = dhcp_proxy_set_server ((ip4_address_t *)(&mp->dhcp_server),
2913                                 (ip4_address_t *)(&mp->dhcp_src_address),
2914                                 (u32) ntohl(mp->vrf_id),
2915                                 (int) mp->insert_circuit_id,
2916                                 (int) (mp->is_add == 0));
2917     
2918     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_REPLY);
2919 }
2920
2921
2922 static void
2923 dhcpv6_proxy_config (vl_api_dhcp_proxy_config_t *mp)
2924 {
2925     vl_api_dhcp_proxy_config_reply_t * rmp;
2926     int rv = -1;
2927
2928     rv = dhcpv6_proxy_set_server ((ip6_address_t *)(&mp->dhcp_server),
2929                                 (ip6_address_t *)(&mp->dhcp_src_address),
2930                                 (u32) ntohl(mp->vrf_id),
2931                                 (int) mp->insert_circuit_id,
2932                                 (int) (mp->is_add == 0));
2933     
2934     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_REPLY);
2935 }
2936
2937 static void
2938 dhcpv4_proxy_config_2 (vl_api_dhcp_proxy_config_2_t *mp)
2939 {
2940     vl_api_dhcp_proxy_config_reply_t * rmp;
2941     int rv;
2942
2943     rv = dhcp_proxy_set_server_2 ((ip4_address_t *)(&mp->dhcp_server),
2944                                 (ip4_address_t *)(&mp->dhcp_src_address),
2945                                 (u32) ntohl(mp->rx_vrf_id),
2946                                 (u32) ntohl(mp->server_vrf_id),
2947                                 (int) mp->insert_circuit_id,
2948                                 (int) (mp->is_add == 0));
2949     
2950     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_2_REPLY);
2951 }
2952
2953
2954 static void
2955 dhcpv6_proxy_config_2 (vl_api_dhcp_proxy_config_2_t *mp)
2956 {
2957     vl_api_dhcp_proxy_config_reply_t * rmp;
2958     int rv = -1;
2959
2960 #if 0 // $$$$ FIXME
2961     rv = dhcpv6_proxy_set_server_2 ((ip6_address_t *)(&mp->dhcp_server),
2962                                 (ip6_address_t *)(&mp->dhcp_src_address),
2963                                 (u32) ntohl(mp->rx_vrf_id),
2964                                 (u32) ntohl(mp->server_vrf_id),
2965                                 (int) mp->insert_circuit_id,
2966                                 (int) (mp->is_add == 0));
2967 #else
2968     rv = VNET_API_ERROR_UNIMPLEMENTED;
2969 #endif
2970     
2971     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_2_REPLY);
2972 }
2973
2974
2975 static void
2976 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t *mp)
2977 {
2978     vl_api_dhcp_proxy_set_vss_reply_t *rmp;
2979     int rv;
2980     if (!mp->is_ipv6) 
2981         rv = dhcp_proxy_set_option82_vss(ntohl(mp->tbl_id),
2982                                          ntohl(mp->oui),
2983                                          ntohl(mp->fib_id),
2984                                          (int)mp->is_add == 0);
2985     else
2986          rv = dhcpv6_proxy_set_vss( ntohl(mp->tbl_id),
2987                                          ntohl(mp->oui),
2988                                          ntohl(mp->fib_id),
2989                                          (int)mp->is_add == 0);
2990     
2991     REPLY_MACRO(VL_API_DHCP_PROXY_SET_VSS_REPLY);
2992 }
2993
2994
2995 static void vl_api_dhcp_proxy_config_t_handler 
2996 (vl_api_dhcp_proxy_config_t *mp)
2997 {
2998     if (mp->is_ipv6 == 0)
2999         dhcpv4_proxy_config (mp);
3000     else
3001         dhcpv6_proxy_config (mp);
3002 }
3003
3004 static void vl_api_dhcp_proxy_config_2_t_handler 
3005 (vl_api_dhcp_proxy_config_2_t *mp)
3006 {
3007     if (mp->is_ipv6 == 0)
3008         dhcpv4_proxy_config_2 (mp);
3009     else
3010         dhcpv6_proxy_config_2 (mp);
3011 }
3012
3013 void dhcp_compl_event_callback (u32 client_index, u32 pid, u8 * hostname,
3014        u8 is_ipv6, u8 * host_address, u8 * router_address, u8 * host_mac)
3015 {
3016     unix_shared_memory_queue_t * q;
3017     vl_api_dhcp_compl_event_t * mp;
3018
3019     q = vl_api_client_index_to_input_queue (client_index);
3020     if (!q)
3021         return;
3022
3023     mp = vl_msg_api_alloc (sizeof (*mp));
3024     mp->client_index = client_index;
3025     mp->pid = pid;
3026     mp->is_ipv6 = is_ipv6;
3027     clib_memcpy (&mp->hostname, hostname, vec_len(hostname));
3028     mp->hostname[vec_len(hostname) + 1] = '\n';
3029     clib_memcpy (&mp->host_address[0], host_address, 16);
3030     clib_memcpy (&mp->router_address[0], router_address, 16);
3031     clib_memcpy (&mp->host_mac[0], host_mac, 6);
3032
3033     mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
3034
3035     vl_msg_api_send_shmem (q, (u8 *)&mp);
3036 }
3037
3038 static void vl_api_dhcp_client_config_t_handler 
3039 (vl_api_dhcp_client_config_t *mp)
3040 {
3041     vlib_main_t *vm = vlib_get_main();
3042     vl_api_dhcp_client_config_reply_t * rmp;
3043     int rv = 0;
3044
3045     VALIDATE_SW_IF_INDEX(mp);
3046
3047     rv = dhcp_client_config(vm, ntohl(mp->sw_if_index), 
3048              mp->hostname, mp->is_add, mp->client_index,
3049              mp->want_dhcp_event ? dhcp_compl_event_callback : NULL,
3050              mp->pid);
3051
3052     BAD_SW_IF_INDEX_LABEL;
3053
3054     REPLY_MACRO(VL_API_DHCP_CLIENT_CONFIG_REPLY);
3055 }
3056
3057 static void
3058 vl_api_sw_interface_ip6nd_ra_config_t_handler 
3059 (vl_api_sw_interface_ip6nd_ra_config_t *mp, vlib_main_t *vm)
3060 {
3061    vl_api_sw_interface_ip6nd_ra_config_reply_t * rmp;
3062     int rv = 0;
3063     u8  is_no,  surpress, managed, other, ll_option, send_unicast, cease, default_router;
3064  
3065     is_no = mp->is_no == 1;
3066     surpress = mp->surpress == 1;
3067     managed = mp->managed == 1;
3068    other = mp->other == 1;
3069     ll_option = mp->ll_option == 1;
3070     send_unicast = mp->send_unicast == 1;
3071     cease = mp->cease == 1;
3072     default_router = mp->default_router  == 1;
3073
3074     VALIDATE_SW_IF_INDEX(mp);
3075
3076     rv = ip6_neighbor_ra_config(vm, ntohl(mp->sw_if_index), 
3077                                 surpress,  managed,  other,
3078                                 ll_option,  send_unicast,  cease, 
3079                                 default_router, ntohl (mp->lifetime),
3080                                 ntohl(mp->initial_count),  ntohl(mp->initial_interval),  
3081                                 ntohl(mp->max_interval), ntohl( mp->min_interval),
3082                                 is_no);
3083
3084     BAD_SW_IF_INDEX_LABEL;
3085
3086     REPLY_MACRO(VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
3087 }
3088
3089 static void
3090 vl_api_sw_interface_ip6nd_ra_prefix_t_handler 
3091 (vl_api_sw_interface_ip6nd_ra_prefix_t *mp, vlib_main_t *vm)
3092 {
3093    vl_api_sw_interface_ip6nd_ra_prefix_reply_t * rmp;
3094     int rv = 0;
3095     u8  is_no,  use_default,  no_advertise, off_link, no_autoconfig, no_onlink;
3096  
3097     VALIDATE_SW_IF_INDEX(mp);
3098
3099     is_no = mp->is_no == 1;
3100     use_default = mp->use_default == 1;
3101     no_advertise = mp->no_advertise == 1;
3102     off_link = mp->off_link == 1;
3103     no_autoconfig = mp->no_autoconfig == 1;
3104     no_onlink = mp->no_onlink == 1;
3105  
3106     rv = ip6_neighbor_ra_prefix(vm,  ntohl(mp->sw_if_index),  
3107                                 (ip6_address_t *)mp->address,  mp->address_length,
3108                                 use_default,  ntohl(mp->val_lifetime), ntohl(mp->pref_lifetime),
3109                                 no_advertise,  off_link, no_autoconfig, no_onlink,
3110                                 is_no);
3111
3112     BAD_SW_IF_INDEX_LABEL;
3113     REPLY_MACRO(VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
3114 }
3115
3116 static void
3117 vl_api_sw_interface_ip6_enable_disable_t_handler 
3118 (vl_api_sw_interface_ip6_enable_disable_t *mp, vlib_main_t *vm)
3119 {
3120     vl_api_sw_interface_ip6_enable_disable_reply_t * rmp;
3121     vnet_main_t * vnm = vnet_get_main();
3122     int rv = 0;
3123     clib_error_t * error;
3124
3125     vnm->api_errno = 0;
3126
3127     VALIDATE_SW_IF_INDEX(mp);
3128
3129     error = ( mp->enable == 1) ? enable_ip6_interface(vm,ntohl(mp->sw_if_index)) :
3130         disable_ip6_interface(vm,ntohl(mp->sw_if_index));
3131     
3132     if (error)  {
3133         clib_error_report(error);
3134         rv = VNET_API_ERROR_UNSPECIFIED;
3135     } else {
3136         rv = vnm->api_errno;
3137     }
3138     
3139     BAD_SW_IF_INDEX_LABEL;
3140
3141     REPLY_MACRO(VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
3142 }
3143
3144 static void
3145 vl_api_sw_interface_ip6_set_link_local_address_t_handler 
3146 (vl_api_sw_interface_ip6_set_link_local_address_t *mp, vlib_main_t *vm)
3147 {
3148     vl_api_sw_interface_ip6_set_link_local_address_reply_t * rmp;
3149     int rv = 0;
3150     clib_error_t * error;
3151     vnet_main_t * vnm = vnet_get_main();
3152
3153     vnm->api_errno = 0;
3154
3155     VALIDATE_SW_IF_INDEX(mp);
3156
3157     error = set_ip6_link_local_address(vm,
3158                                        ntohl(mp->sw_if_index),
3159                                        (ip6_address_t *)mp->address,
3160                                        mp->address_length);
3161     if (error)  {
3162       clib_error_report(error);
3163       rv = VNET_API_ERROR_UNSPECIFIED;
3164     } else {
3165         rv = vnm->api_errno;
3166     }
3167
3168     BAD_SW_IF_INDEX_LABEL;
3169     
3170     REPLY_MACRO(VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
3171 }
3172
3173 static void set_ip6_flow_hash (vl_api_set_ip_flow_hash_t *mp)
3174 {
3175     vl_api_set_ip_flow_hash_reply_t *rmp;
3176     int rv = VNET_API_ERROR_UNIMPLEMENTED;
3177
3178     clib_warning ("unimplemented...");
3179     
3180     REPLY_MACRO(VL_API_SET_IP_FLOW_HASH_REPLY);
3181 }
3182
3183 static void set_ip4_flow_hash (vl_api_set_ip_flow_hash_t *mp)
3184 {
3185     vl_api_set_ip_flow_hash_reply_t *rmp;
3186     int rv;
3187     u32 table_id;
3188     u32 flow_hash_config = 0;
3189
3190     table_id = ntohl(mp->vrf_id);
3191
3192 #define _(a,b) if (mp->a) flow_hash_config |= b;
3193     foreach_flow_hash_bit;
3194 #undef _
3195
3196     rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
3197
3198     REPLY_MACRO(VL_API_SET_IP_FLOW_HASH_REPLY);
3199 }
3200
3201
3202 static void vl_api_set_ip_flow_hash_t_handler
3203 (vl_api_set_ip_flow_hash_t *mp)
3204 {
3205     if (mp->is_ipv6 == 0)
3206         set_ip4_flow_hash (mp);
3207     else
3208         set_ip6_flow_hash (mp);
3209 }
3210
3211 static void vl_api_sw_interface_set_unnumbered_t_handler 
3212 (vl_api_sw_interface_set_unnumbered_t *mp)
3213 {
3214     vl_api_sw_interface_set_unnumbered_reply_t * rmp;
3215     int rv = 0;
3216     vnet_sw_interface_t * si;
3217     vnet_main_t *vnm = vnet_get_main();
3218     u32 sw_if_index, unnumbered_sw_if_index;
3219
3220     sw_if_index = ntohl(mp->sw_if_index);
3221     unnumbered_sw_if_index = ntohl(mp->unnumbered_sw_if_index);
3222
3223     /*
3224      * The API message field names are backwards from 
3225      * the underlying data structure names.
3226      * It's not worth changing them now.
3227      */
3228     if (pool_is_free_index (vnm->interface_main.sw_interfaces, 
3229                             unnumbered_sw_if_index)) {
3230         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
3231         goto done;
3232     }
3233     
3234     /* Only check the "use loop0" field when setting the binding */
3235     if (mp->is_add && 
3236         pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index)) {
3237         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
3238         goto done;
3239     }
3240
3241     si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
3242
3243     if (mp->is_add) {
3244         si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
3245         si->unnumbered_sw_if_index = sw_if_index;
3246     } else {
3247         si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
3248         si->unnumbered_sw_if_index = (u32)~0;
3249     }
3250     
3251  done:
3252     REPLY_MACRO(VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
3253 }
3254
3255 static void vl_api_create_loopback_t_handler
3256 (vl_api_create_loopback_t *mp)
3257 {
3258     vl_api_create_loopback_reply_t * rmp;
3259     u32 sw_if_index;
3260     int rv;
3261     
3262     rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address);
3263
3264     REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
3265     ({
3266         rmp->sw_if_index = ntohl (sw_if_index);
3267     }));
3268 }
3269
3270 static void vl_api_delete_loopback_t_handler
3271 (vl_api_delete_loopback_t *mp)
3272 {
3273     vl_api_delete_loopback_reply_t * rmp;
3274     u32 sw_if_index;
3275     int rv;
3276
3277     sw_if_index = ntohl (mp->sw_if_index);
3278     rv = vnet_delete_loopback_interface (sw_if_index);
3279
3280     REPLY_MACRO(VL_API_DELETE_LOOPBACK_REPLY);
3281 }
3282
3283 static void vl_api_control_ping_t_handler
3284 (vl_api_control_ping_t *mp)
3285 {
3286     vl_api_control_ping_reply_t * rmp;
3287     int rv = 0;
3288
3289     REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
3290     ({
3291         rmp->vpe_pid = ntohl (getpid());
3292     }));
3293 }
3294
3295 static void shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
3296 {
3297     u8 **shmem_vecp = (u8 **)arg;
3298     u8 *shmem_vec;
3299     void *oldheap;
3300     api_main_t * am = &api_main;
3301     u32 offset;
3302
3303     shmem_vec = *shmem_vecp;
3304
3305     offset = vec_len (shmem_vec);
3306
3307     pthread_mutex_lock (&am->vlib_rp->mutex);
3308     oldheap = svm_push_data_heap (am->vlib_rp);
3309     
3310     vec_validate (shmem_vec, offset + buffer_bytes - 1);
3311
3312     clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
3313
3314     svm_pop_heap (oldheap);
3315     pthread_mutex_unlock (&am->vlib_rp->mutex);
3316
3317     *shmem_vecp = shmem_vec;
3318 }
3319
3320
3321 static void vl_api_cli_request_t_handler 
3322 (vl_api_cli_request_t *mp)
3323 {
3324     vl_api_cli_reply_t *rp;
3325     unix_shared_memory_queue_t *q;
3326     vlib_main_t * vm = vlib_get_main();
3327     api_main_t * am = &api_main;
3328     unformat_input_t input;
3329     u8 *shmem_vec=0;
3330     void *oldheap;
3331
3332     q = vl_api_client_index_to_input_queue (mp->client_index);
3333     if (!q)
3334         return;
3335
3336     rp = vl_msg_api_alloc (sizeof (*rp));
3337     rp->_vl_msg_id = ntohs(VL_API_CLI_REPLY);
3338     rp->context = mp->context;
3339
3340     unformat_init_vector (&input, (u8 *)(uword)mp->cmd_in_shmem);
3341
3342     vlib_cli_input (vm, &input, shmem_cli_output, 
3343                     (uword)&shmem_vec);
3344
3345     pthread_mutex_lock (&am->vlib_rp->mutex);
3346     oldheap = svm_push_data_heap (am->vlib_rp);
3347     
3348     vec_add1(shmem_vec, 0);
3349
3350     svm_pop_heap (oldheap);
3351     pthread_mutex_unlock (&am->vlib_rp->mutex);
3352
3353     rp->reply_in_shmem = (uword)shmem_vec;
3354
3355     vl_msg_api_send_shmem (q, (u8 *)&rp);
3356 }
3357
3358 static void vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t *mp)
3359 {
3360     int rv;
3361     vl_api_set_arp_neighbor_limit_reply_t * rmp;
3362     vnet_main_t *vnm = vnet_get_main();
3363     clib_error_t * error;
3364
3365     vnm->api_errno = 0;
3366
3367     if (mp->is_ipv6)
3368         error = ip6_set_neighbor_limit (ntohl(mp->arp_neighbor_limit));
3369     else
3370         error = ip4_set_arp_limit (ntohl(mp->arp_neighbor_limit));
3371     
3372     if (error)  {
3373         clib_error_report(error);
3374         rv = VNET_API_ERROR_UNSPECIFIED;
3375     } else {
3376         rv = vnm->api_errno;
3377     }
3378
3379     REPLY_MACRO(VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3380 }
3381
3382 static void vl_api_sr_tunnel_add_del_t_handler
3383 (vl_api_sr_tunnel_add_del_t *mp)
3384 {
3385 #if IPV6SR == 0
3386     clib_warning ("unimplemented");
3387 #else
3388     ip6_sr_add_del_tunnel_args_t _a, *a=&_a;
3389     int rv = 0;
3390     vl_api_sr_tunnel_add_del_reply_t * rmp;
3391     ip6_address_t * segments = 0, * seg;
3392     ip6_address_t * tags = 0, *tag;
3393     ip6_address_t * this_address;
3394     int i;
3395
3396     if (mp->n_segments == 0) {
3397         rv = -11;
3398         goto out;
3399     }
3400
3401     memset (a, 0, sizeof (*a));
3402     a->src_address = (ip6_address_t *)&mp->src_address;
3403     a->dst_address = (ip6_address_t *)&mp->dst_address;
3404     a->dst_mask_width = mp->dst_mask_width;
3405     a->flags_net_byte_order = mp->flags_net_byte_order;
3406     a->is_del = (mp->is_add == 0);
3407     a->rx_table_id = ntohl(mp->outer_vrf_id);
3408     a->tx_table_id = ntohl(mp->inner_vrf_id);
3409     
3410     a->name = format(0, "%s", mp->name);
3411     if (!(vec_len(a->name)))
3412       a->name = 0;
3413     
3414     a->policy_name = format(0, "%s", mp->policy_name);
3415     if (!(vec_len(a->policy_name)))
3416       a->policy_name = 0;
3417     
3418     /* Yank segments and tags out of the API message */
3419     this_address = (ip6_address_t *)mp->segs_and_tags;
3420     for (i = 0; i < mp->n_segments; i++) {
3421         vec_add2 (segments, seg, 1);
3422         clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
3423         this_address++;
3424     }
3425     for (i = 0; i < mp->n_tags; i++) {
3426         vec_add2 (tags, tag, 1);
3427         clib_memcpy (tag->as_u8, this_address->as_u8, sizeof (*this_address));
3428         this_address++;
3429     }
3430
3431     a->segments = segments;
3432     a->tags = tags;
3433
3434     rv = ip6_sr_add_del_tunnel (a);
3435
3436 out:
3437
3438     REPLY_MACRO(VL_API_SR_TUNNEL_ADD_DEL_REPLY);
3439 #endif
3440 }
3441
3442 static void vl_api_sr_policy_add_del_t_handler
3443 (vl_api_sr_policy_add_del_t *mp)
3444 {
3445 #if IPV6SR == 0
3446     clib_warning ("unimplemented");
3447 #else
3448     ip6_sr_add_del_policy_args_t _a, *a=&_a;
3449     int rv = 0;
3450     vl_api_sr_policy_add_del_reply_t * rmp;
3451     int i;
3452
3453     memset (a, 0, sizeof (*a));
3454     a->is_del = (mp->is_add == 0);
3455     
3456     a->name = format(0, "%s", mp->name);
3457     if (!(vec_len(a->name)))
3458       {
3459         rv = VNET_API_ERROR_NO_SUCH_NODE2;
3460         goto out;
3461       }      
3462
3463     if (!(mp->tunnel_names[0]))
3464       {
3465         rv = VNET_API_ERROR_NO_SUCH_NODE2;
3466         goto out;
3467       }
3468
3469     // start deserializing tunnel_names
3470     int num_tunnels = mp->tunnel_names[0]; //number of tunnels
3471     u8 * deser_tun_names = mp->tunnel_names;
3472     deser_tun_names += 1; //moving along
3473
3474     u8 * tun_name = 0;
3475     int tun_name_len = 0;
3476
3477     for (i=0; i < num_tunnels; i++)
3478       {
3479         tun_name_len= *deser_tun_names;
3480         deser_tun_names += 1;
3481         vec_resize (tun_name, tun_name_len);
3482         memcpy(tun_name, deser_tun_names, tun_name_len);
3483         vec_add1 (a->tunnel_names, tun_name);
3484         deser_tun_names += tun_name_len;
3485         tun_name = 0;
3486       }
3487     
3488     rv = ip6_sr_add_del_policy (a);
3489
3490 out:
3491
3492     REPLY_MACRO(VL_API_SR_POLICY_ADD_DEL_REPLY);
3493 #endif
3494 }
3495
3496 static void vl_api_sr_multicast_map_add_del_t_handler
3497 (vl_api_sr_multicast_map_add_del_t *mp)
3498 {
3499 #if IPV6SR == 0
3500     clib_warning ("unimplemented");
3501 #else
3502     ip6_sr_add_del_multicastmap_args_t _a, *a=&_a;
3503     int rv = 0;
3504     vl_api_sr_multicast_map_add_del_reply_t * rmp;
3505
3506     memset (a, 0, sizeof (*a));
3507     a->is_del = (mp->is_add == 0);
3508     
3509     a->multicast_address = (ip6_address_t *)&mp->multicast_address;
3510     a->policy_name = format(0, "%s", mp->policy_name);
3511
3512     if (a->multicast_address == 0)
3513       {
3514         rv = -1 ; 
3515         goto out;
3516       }      
3517
3518     if (!(a->policy_name))
3519       {
3520         rv = -2 ; 
3521         goto out;
3522       }
3523
3524 #if DPDK > 0 /* Cannot call replicate without DPDK */
3525     rv = ip6_sr_add_del_multicastmap (a);
3526 #else
3527     clib_warning ("multicast replication without DPDK not implemented");
3528     rv = VNET_API_ERROR_UNIMPLEMENTED;
3529 #endif /* DPDK */
3530
3531 out:
3532
3533     REPLY_MACRO(VL_API_SR_MULTICAST_MAP_ADD_DEL_REPLY);
3534 #endif
3535 }
3536
3537 #define foreach_classify_add_del_table_field    \
3538 _(table_index)                                  \
3539 _(nbuckets)                                     \
3540 _(memory_size)                                  \
3541 _(skip_n_vectors)                               \
3542 _(match_n_vectors)                              \
3543 _(next_table_index)                             \
3544 _(miss_next_index)
3545
3546 static void vl_api_classify_add_del_table_t_handler
3547 (vl_api_classify_add_del_table_t * mp)
3548 {
3549     vl_api_classify_add_del_table_reply_t * rmp;
3550     vnet_classify_main_t * cm = &vnet_classify_main;
3551     vnet_classify_table_t * t;
3552     int rv;
3553
3554 #define _(a) u32 a;
3555     foreach_classify_add_del_table_field;
3556 #undef _
3557
3558 #define _(a) a = ntohl(mp->a);    
3559     foreach_classify_add_del_table_field;
3560 #undef _
3561
3562     /* The underlying API fails silently, on purpose, so check here */
3563     if (mp->is_add == 0) 
3564         if (pool_is_free_index (cm->tables, table_index)) {
3565             rv = VNET_API_ERROR_NO_SUCH_TABLE;
3566             goto out;
3567         }
3568
3569     rv = vnet_classify_add_del_table 
3570         (cm, mp->mask, nbuckets, memory_size, 
3571          skip_n_vectors, match_n_vectors, 
3572          next_table_index, miss_next_index,
3573          &table_index, mp->is_add);
3574     
3575 out:
3576     REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
3577     ({
3578         if (rv == 0 && mp->is_add) {
3579             t = pool_elt_at_index (cm->tables, table_index);
3580             rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
3581             rmp->match_n_vectors = ntohl(t->match_n_vectors);
3582             rmp->new_table_index = ntohl(table_index);
3583         } else {
3584             rmp->skip_n_vectors = ~0;
3585             rmp->match_n_vectors = ~0;
3586             rmp->new_table_index = ~0;
3587         }
3588     }));
3589 }
3590
3591 static void vl_api_classify_add_del_session_t_handler
3592 (vl_api_classify_add_del_session_t * mp)
3593 {
3594     vnet_classify_main_t * cm = &vnet_classify_main;
3595     vl_api_classify_add_del_session_reply_t * rmp;
3596     int rv;
3597     u32 table_index, hit_next_index, opaque_index;
3598     i32 advance;
3599
3600     table_index = ntohl (mp->table_index);
3601     hit_next_index = ntohl (mp->hit_next_index);
3602     opaque_index = ntohl (mp->opaque_index);
3603     advance = ntohl (mp->advance);
3604     
3605     rv = vnet_classify_add_del_session 
3606         (cm, table_index, mp->match, hit_next_index, opaque_index,
3607          advance, mp->is_add);
3608
3609     REPLY_MACRO(VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
3610 }
3611
3612 static void vl_api_classify_set_interface_ip_table_t_handler 
3613 (vl_api_classify_set_interface_ip_table_t * mp)
3614 {
3615     vlib_main_t * vm = vlib_get_main();
3616     vl_api_classify_set_interface_ip_table_reply_t * rmp;
3617     int rv;
3618     u32 table_index, sw_if_index;
3619
3620     table_index = ntohl (mp->table_index);
3621     sw_if_index = ntohl (mp->sw_if_index);
3622     
3623     VALIDATE_SW_IF_INDEX(mp);
3624
3625     if (mp->is_ipv6)
3626         rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3627     else
3628         rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
3629
3630     BAD_SW_IF_INDEX_LABEL;
3631
3632     REPLY_MACRO(VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
3633 }
3634
3635 static void vl_api_classify_set_interface_l2_tables_t_handler 
3636 (vl_api_classify_set_interface_l2_tables_t * mp)
3637 {
3638     vl_api_classify_set_interface_l2_tables_reply_t * rmp;
3639     int rv;
3640     u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
3641     int enable;
3642
3643     ip4_table_index = ntohl(mp->ip4_table_index);
3644     ip6_table_index = ntohl(mp->ip6_table_index);
3645     other_table_index = ntohl(mp->other_table_index);
3646     sw_if_index = ntohl(mp->sw_if_index);
3647
3648     VALIDATE_SW_IF_INDEX(mp);
3649
3650     rv = vnet_l2_classify_set_tables (sw_if_index, ip4_table_index, 
3651                                       ip6_table_index, other_table_index);
3652
3653     if (rv == 0) {
3654         if (ip4_table_index != ~0 || ip6_table_index != ~0 
3655             || other_table_index != ~0)
3656             enable = 1;
3657         else
3658             enable = 0;
3659             
3660         vnet_l2_classify_enable_disable (sw_if_index, enable);
3661     }
3662
3663     BAD_SW_IF_INDEX_LABEL;
3664
3665     REPLY_MACRO(VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
3666 }
3667
3668 static void 
3669 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t *mp)
3670 {
3671     int rv = 0;
3672     vl_api_l2_fib_clear_table_reply_t * rmp;
3673
3674     /* DAW-FIXME: This API should only clear non-static l2fib entries, but
3675      *            that is not currently implemented.  When that TODO is fixed
3676      *            this call should be changed to pass 1 instead of 0.
3677      */
3678     l2fib_clear_table (0);
3679
3680     REPLY_MACRO(VL_API_L2_FIB_CLEAR_TABLE_REPLY);
3681 }
3682
3683 extern void l2_efp_filter_configure(vnet_main_t * vnet_main,
3684                                     u32           sw_if_index,
3685                                     u32           enable);
3686
3687 static void 
3688 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *mp)
3689 {
3690     int rv;
3691     vl_api_l2_interface_efp_filter_reply_t * rmp;
3692     vnet_main_t *vnm = vnet_get_main();
3693
3694     // enable/disable the feature
3695     l2_efp_filter_configure (vnm, mp->sw_if_index, mp->enable_disable);
3696     rv = vnm->api_errno;
3697
3698     REPLY_MACRO(VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
3699 }
3700
3701 static void 
3702 vl_api_l2_interface_vlan_tag_rewrite_t_handler (vl_api_l2_interface_vlan_tag_rewrite_t *mp)
3703 {
3704     int rv = 0;
3705     vl_api_l2_interface_vlan_tag_rewrite_reply_t * rmp;
3706     vnet_main_t * vnm = vnet_get_main();
3707     vlib_main_t * vm = vlib_get_main();
3708     u32 vtr_op;
3709
3710     VALIDATE_SW_IF_INDEX(mp);
3711
3712     vtr_op = ntohl(mp->vtr_op);
3713
3714     /* The L2 code is unsuspicious */
3715     switch(vtr_op) {
3716     case L2_VTR_DISABLED:
3717     case L2_VTR_PUSH_1:
3718     case L2_VTR_PUSH_2:
3719     case L2_VTR_POP_1:
3720     case L2_VTR_POP_2:
3721     case L2_VTR_TRANSLATE_1_1:
3722     case L2_VTR_TRANSLATE_1_2:
3723     case L2_VTR_TRANSLATE_2_1:
3724     case L2_VTR_TRANSLATE_2_2:
3725         break;
3726
3727     default:
3728         rv = VNET_API_ERROR_INVALID_VALUE;
3729         goto bad_sw_if_index;
3730     }
3731
3732     rv = l2vtr_configure (vm, vnm, ntohl(mp->sw_if_index), vtr_op, 
3733                           ntohl(mp->push_dot1q), ntohl(mp->tag1), 
3734                           ntohl(mp->tag2));
3735     
3736     BAD_SW_IF_INDEX_LABEL;
3737
3738     REPLY_MACRO(VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
3739 }
3740
3741 static void
3742 vl_api_create_vhost_user_if_t_handler (vl_api_create_vhost_user_if_t *mp)
3743 {
3744 #if DPDK > 0
3745     int rv = 0;
3746     vl_api_create_vhost_user_if_reply_t * rmp;
3747     u32 sw_if_index = (u32)~0;
3748
3749     vnet_main_t * vnm = vnet_get_main();
3750     vlib_main_t * vm = vlib_get_main();
3751
3752     rv = dpdk_vhost_user_create_if(vnm, vm, (char *)mp->sock_filename,
3753                               mp->is_server, &sw_if_index, (u64)~0,
3754                               mp->renumber, ntohl(mp->custom_dev_instance),
3755                               (mp->use_custom_mac)?mp->mac_address:NULL);
3756
3757     REPLY_MACRO2(VL_API_CREATE_VHOST_USER_IF_REPLY,
3758     ({
3759       rmp->sw_if_index = ntohl (sw_if_index);
3760     }));
3761 #endif
3762 }
3763
3764 static void
3765 vl_api_modify_vhost_user_if_t_handler (vl_api_modify_vhost_user_if_t *mp)
3766 {
3767 #if DPDK > 0
3768     int rv = 0;
3769     vl_api_modify_vhost_user_if_reply_t * rmp;
3770     u32 sw_if_index = ntohl(mp->sw_if_index);
3771
3772     vnet_main_t * vnm = vnet_get_main();
3773     vlib_main_t * vm = vlib_get_main();
3774
3775     rv = dpdk_vhost_user_modify_if(vnm, vm, (char *)mp->sock_filename,
3776                               mp->is_server, sw_if_index, (u64)~0,
3777                               mp->renumber, ntohl(mp->custom_dev_instance));
3778
3779     REPLY_MACRO(VL_API_MODIFY_VHOST_USER_IF_REPLY);
3780 #endif
3781 }
3782
3783 static void
3784 vl_api_delete_vhost_user_if_t_handler (vl_api_delete_vhost_user_if_t *mp)
3785 {
3786 #if DPDK > 0
3787     int rv = 0;
3788     vpe_api_main_t * vam = &vpe_api_main;
3789     vl_api_delete_vhost_user_if_reply_t * rmp;
3790     u32 sw_if_index = ntohl(mp->sw_if_index);
3791
3792     vnet_main_t * vnm = vnet_get_main();
3793     vlib_main_t * vm = vlib_get_main();
3794
3795     rv = dpdk_vhost_user_delete_if(vnm, vm, sw_if_index);
3796
3797     REPLY_MACRO(VL_API_DELETE_VHOST_USER_IF_REPLY);
3798     if (!rv) {
3799         unix_shared_memory_queue_t * q =
3800             vl_api_client_index_to_input_queue (mp->client_index);
3801         if (!q)
3802             return;
3803
3804         send_sw_interface_flags_deleted (vam, q, sw_if_index);
3805     }
3806 #endif
3807 }
3808
3809 static void vl_api_sw_interface_vhost_user_details_t_handler (
3810     vl_api_sw_interface_vhost_user_details_t * mp)
3811 {
3812     clib_warning ("BUG");
3813 }
3814
3815 #if DPDK > 0
3816 static void send_sw_interface_vhost_user_details (vpe_api_main_t * am,
3817                                        unix_shared_memory_queue_t *q,
3818                                        vhost_user_intf_details_t * vui,
3819                                        u32 context)
3820 {
3821     vl_api_sw_interface_vhost_user_details_t * mp;
3822
3823     mp = vl_msg_api_alloc (sizeof (*mp));
3824     memset (mp, 0, sizeof (*mp));
3825     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_VHOST_USER_DETAILS);
3826     mp->sw_if_index = ntohl(vui->sw_if_index);
3827     mp->virtio_net_hdr_sz = ntohl (vui->virtio_net_hdr_sz);
3828     mp->features = clib_net_to_host_u64 (vui->features);
3829     mp->is_server = vui->is_server;
3830     mp->num_regions = ntohl(vui->num_regions);
3831     mp->sock_errno = ntohl(vui->sock_errno);
3832     mp->context = context;
3833
3834     strncpy ((char *) mp->sock_filename,
3835              (char *) vui->sock_filename, ARRAY_LEN(mp->sock_filename)-1);
3836     strncpy ((char *) mp->interface_name,
3837              (char *) vui->if_name, ARRAY_LEN(mp->interface_name)-1);
3838
3839     vl_msg_api_send_shmem (q, (u8 *)&mp);
3840 }
3841 #endif
3842
3843 static void
3844 vl_api_sw_interface_vhost_user_dump_t_handler (
3845         vl_api_sw_interface_vhost_user_dump_t *mp)
3846 {
3847 #if DPDK > 0
3848     int rv = 0;
3849     vpe_api_main_t * am = &vpe_api_main;
3850     vnet_main_t * vnm = vnet_get_main();
3851     vlib_main_t * vm = vlib_get_main();
3852     vhost_user_intf_details_t *ifaces = NULL;
3853     vhost_user_intf_details_t *vuid = NULL;
3854     unix_shared_memory_queue_t * q;
3855
3856     q = vl_api_client_index_to_input_queue (mp->client_index);
3857     if (q == 0)
3858         return;
3859
3860     rv = dpdk_vhost_user_dump_ifs(vnm, vm, &ifaces);
3861     if (rv)
3862         return;
3863
3864     vec_foreach (vuid, ifaces) {
3865         send_sw_interface_vhost_user_details (am, q, vuid, mp->context);
3866     }
3867     vec_free(ifaces);
3868 #endif
3869 }
3870
3871 static void send_sw_if_l2tpv3_tunnel_details (vpe_api_main_t * am,
3872                                        unix_shared_memory_queue_t *q,
3873                                        l2t_session_t *s,
3874                                        l2t_main_t * lm,
3875                                        u32 context)
3876 {
3877     vl_api_sw_if_l2tpv3_tunnel_details_t * mp;
3878     u8 * if_name = NULL;
3879     vnet_sw_interface_t * si = NULL;
3880
3881     si = vnet_get_hw_sw_interface (lm->vnet_main, s->hw_if_index);
3882
3883     if_name = format(if_name, "%U",
3884                      format_vnet_sw_interface_name, lm->vnet_main, si);
3885
3886     mp = vl_msg_api_alloc (sizeof (*mp));
3887     memset (mp, 0, sizeof (*mp));
3888     mp->_vl_msg_id = ntohs(VL_API_SW_IF_L2TPV3_TUNNEL_DETAILS);
3889     strncpy((char *)mp->interface_name,
3890             (char *)if_name, ARRAY_LEN(mp->interface_name)-1);
3891     mp->sw_if_index = ntohl(si->sw_if_index);
3892     mp->local_session_id = s->local_session_id;
3893     mp->remote_session_id = s->remote_session_id;
3894     mp->local_cookie[0] = s->local_cookie[0];
3895     mp->local_cookie[1] = s->local_cookie[1];
3896     mp->remote_cookie = s->remote_cookie;
3897     clib_memcpy(mp->client_address, &s->client_address, sizeof(s->client_address));
3898     clib_memcpy(mp->our_address, &s->our_address, sizeof(s->our_address));
3899     mp->l2_sublayer_present = s->l2_sublayer_present;
3900     mp->context = context;
3901
3902     vl_msg_api_send_shmem (q, (u8 *)&mp);
3903 }
3904
3905 static void send_ip_address_details (vpe_api_main_t * am,
3906                                      unix_shared_memory_queue_t * q,
3907                                      u8 * ip,
3908                                      u16 prefix_length,
3909                                      u8 is_ipv6,
3910                                      u32 context)
3911 {
3912     vl_api_ip_address_details_t * mp;
3913
3914     mp = vl_msg_api_alloc (sizeof (*mp));
3915     memset (mp, 0, sizeof (*mp));
3916     mp->_vl_msg_id = ntohs(VL_API_IP_ADDRESS_DETAILS);
3917
3918     if (is_ipv6) {
3919         clib_memcpy(&mp->ip, ip, sizeof(mp->ip));
3920     } else {
3921         u32 * tp = (u32 *)mp->ip;
3922         *tp = ntohl(*(u32*)ip);
3923     }
3924     mp->prefix_length = prefix_length;
3925     mp->context = context;
3926
3927     vl_msg_api_send_shmem (q, (u8 *)&mp);
3928 }
3929
3930 static void
3931 vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t *mp)
3932 {
3933     vpe_api_main_t * am = &vpe_api_main;
3934     unix_shared_memory_queue_t * q;
3935     ip6_address_t * r6;
3936     ip4_address_t * r4;
3937     ip6_main_t * im6 = &ip6_main;
3938     ip4_main_t * im4 = &ip4_main;
3939     ip_lookup_main_t * lm6 = &im6->lookup_main;
3940     ip_lookup_main_t * lm4 = &im4->lookup_main;
3941     ip_interface_address_t * ia = 0;
3942     u32 sw_if_index = ~0;
3943     int rv __attribute__ ((unused)) = 0;
3944
3945     VALIDATE_SW_IF_INDEX(mp);
3946
3947     sw_if_index = ntohl(mp->sw_if_index);
3948
3949     q = vl_api_client_index_to_input_queue (mp->client_index);
3950     if (q == 0) {
3951         return;
3952     }
3953
3954     if (mp->is_ipv6) {
3955         foreach_ip_interface_address (lm6, ia, sw_if_index,
3956                                       1 /* honor unnumbered */,
3957         ({
3958             r6 = ip_interface_address_get_address (lm6, ia);
3959             u16 prefix_length = ia->address_length;
3960             send_ip_address_details(am, q, (u8*)r6, prefix_length, 1, mp->context);
3961         }));
3962     } else {
3963         foreach_ip_interface_address (lm4, ia, sw_if_index,
3964                                       1 /* honor unnumbered */,
3965         ({
3966             r4 = ip_interface_address_get_address (lm4, ia);
3967             u16 prefix_length = ia->address_length;
3968             send_ip_address_details(am, q, (u8*)r4, prefix_length, 0, mp->context);
3969         }));
3970     }
3971     BAD_SW_IF_INDEX_LABEL;
3972 }
3973
3974 static void send_ip_details (vpe_api_main_t * am,
3975                                unix_shared_memory_queue_t *q,
3976                                u32 sw_if_index,
3977                                u32 context)
3978 {
3979     vl_api_ip_details_t * mp;
3980
3981     mp = vl_msg_api_alloc (sizeof (*mp));
3982     memset (mp, 0, sizeof (*mp));
3983     mp->_vl_msg_id = ntohs(VL_API_IP_DETAILS);
3984
3985     mp->sw_if_index = ntohl(sw_if_index);
3986     mp->context = context;
3987
3988     vl_msg_api_send_shmem (q, (u8 *)&mp);
3989 }
3990
3991 static void
3992 vl_api_sw_if_l2tpv3_tunnel_dump_t_handler (
3993         vl_api_sw_if_l2tpv3_tunnel_dump_t *mp)
3994 {
3995     vpe_api_main_t * am = &vpe_api_main;
3996     l2t_main_t * lm = &l2t_main;
3997     unix_shared_memory_queue_t * q;
3998     l2t_session_t *session;
3999
4000     q = vl_api_client_index_to_input_queue (mp->client_index);
4001     if (q == 0)
4002         return;
4003
4004     pool_foreach (session, lm->sessions, 
4005     ({
4006         send_sw_if_l2tpv3_tunnel_details (am, q, session, lm, mp->context);
4007     }));
4008 }
4009
4010
4011 static void send_sw_interface_tap_details (vpe_api_main_t * am,
4012                                        unix_shared_memory_queue_t *q,
4013                                        tapcli_interface_details_t *tap_if,
4014                                        u32 context)
4015 {
4016     vl_api_sw_interface_tap_details_t * mp;
4017     mp = vl_msg_api_alloc (sizeof (*mp));
4018     memset (mp, 0, sizeof (*mp));
4019     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_TAP_DETAILS);
4020     mp->sw_if_index = ntohl(tap_if->sw_if_index);
4021     strncpy((char *)mp->dev_name,
4022             (char *)tap_if->dev_name, ARRAY_LEN(mp->dev_name)-1);
4023     mp->context = context;
4024
4025     vl_msg_api_send_shmem (q, (u8 *)&mp);
4026 }
4027
4028 static void
4029 vl_api_sw_interface_tap_dump_t_handler (
4030         vl_api_sw_interface_tap_dump_t *mp)
4031 {
4032     int rv = 0;
4033     vpe_api_main_t * am = &vpe_api_main;
4034     unix_shared_memory_queue_t * q;
4035     tapcli_interface_details_t *tapifs = NULL;
4036     tapcli_interface_details_t *tap_if = NULL;
4037
4038     q = vl_api_client_index_to_input_queue (mp->client_index);
4039     if (q == 0)
4040         return;
4041
4042     rv = vnet_tap_dump_ifs(&tapifs);
4043     if (rv)
4044         return;
4045
4046     vec_foreach(tap_if, tapifs) {
4047         send_sw_interface_tap_details(am, q, tap_if, mp->context);
4048     }
4049
4050     vec_free(tapifs);
4051 }
4052
4053 static void
4054 vl_api_ip_dump_t_handler (vl_api_ip_dump_t *mp)
4055 {
4056     vpe_api_main_t * am = &vpe_api_main;
4057     vnet_main_t * vnm = vnet_get_main();
4058     vlib_main_t * vm = vlib_get_main();
4059     vnet_interface_main_t * im = &vnm->interface_main;
4060     unix_shared_memory_queue_t * q;
4061     vnet_sw_interface_t * si, * sorted_sis;
4062     u32 sw_if_index = ~0;
4063
4064     q = vl_api_client_index_to_input_queue (mp->client_index);
4065     if (q == 0) {
4066         return;
4067     }
4068
4069     /* Gather interfaces. */
4070     sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
4071     _vec_len (sorted_sis) = 0;
4072     pool_foreach (si, im->sw_interfaces, ({ vec_add1 (sorted_sis, si[0]); }));
4073
4074     vec_foreach (si, sorted_sis) {
4075         if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)) {
4076             if (mp->is_ipv6 && !ip6_interface_enabled(vm, si->sw_if_index)) {
4077                 continue;
4078             }
4079             sw_if_index = si->sw_if_index;
4080             send_ip_details(am, q, sw_if_index, mp->context);
4081         }
4082     }
4083 }
4084
4085 static void vl_api_l2_fib_table_entry_t_handler (
4086     vl_api_l2_fib_table_entry_t * mp)
4087 {
4088     clib_warning ("BUG");
4089 }
4090
4091 static void send_l2fib_table_entry (vpe_api_main_t * am,
4092                                     unix_shared_memory_queue_t *q,
4093                                     l2fib_entry_key_t * l2fe_key,
4094                                     l2fib_entry_result_t * l2fe_res,
4095                                     u32 context)
4096 {
4097     vl_api_l2_fib_table_entry_t * mp;
4098
4099     mp = vl_msg_api_alloc (sizeof (*mp));
4100     memset (mp, 0, sizeof (*mp));
4101     mp->_vl_msg_id = ntohs(VL_API_L2_FIB_TABLE_ENTRY);
4102
4103     mp->bd_id = ntohl(l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
4104
4105     mp->mac = l2fib_make_key (l2fe_key->fields.mac, 0);
4106     mp->sw_if_index = ntohl(l2fe_res->fields.sw_if_index);
4107     mp->static_mac = l2fe_res->fields.static_mac;
4108     mp->filter_mac = l2fe_res->fields.filter;
4109     mp->bvi_mac = l2fe_res->fields.bvi;
4110     mp->context = context;
4111
4112     vl_msg_api_send_shmem (q, (u8 *)&mp);
4113 }
4114
4115 static void
4116 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t *mp)
4117 {
4118     vpe_api_main_t * am = &vpe_api_main;
4119     bd_main_t * bdm = &bd_main;
4120     l2fib_entry_key_t *l2fe_key = NULL;
4121     l2fib_entry_result_t *l2fe_res = NULL;
4122     u32 ni, bd_id = ntohl (mp->bd_id);
4123     u32 bd_index;
4124     unix_shared_memory_queue_t * q;
4125     uword * p;
4126
4127     q = vl_api_client_index_to_input_queue (mp->client_index);
4128     if (q == 0)
4129         return;
4130
4131     /* see l2fib_table_dump: ~0 means "any" */
4132     if (bd_id == ~0)
4133         bd_index = ~0;
4134     else {
4135         p = hash_get (bdm->bd_index_by_bd_id, bd_id);
4136         if (p == 0)
4137             return;
4138         
4139         bd_index = p[0];
4140     }
4141
4142     l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
4143
4144     vec_foreach_index (ni, l2fe_key) {
4145         send_l2fib_table_entry (am, q, vec_elt_at_index(l2fe_key, ni),
4146                                 vec_elt_at_index(l2fe_res, ni), mp->context);
4147     }
4148     vec_free(l2fe_key);
4149     vec_free(l2fe_res);
4150 }
4151
4152 static void
4153 vl_api_show_version_t_handler (vl_api_show_version_t *mp)
4154 {
4155     vl_api_show_version_reply_t *rmp;
4156     int rv = 0;
4157     char * vpe_api_get_build_directory(void);
4158     char * vpe_api_get_version(void);
4159     char * vpe_api_get_build_date(void);
4160
4161     unix_shared_memory_queue_t * q =
4162         vl_api_client_index_to_input_queue (mp->client_index);
4163     
4164     if (!q)
4165         return;
4166     
4167     REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
4168     ({
4169         strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
4170         strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
4171                  ARRAY_LEN(rmp->build_directory)-1);
4172         strncpy ((char *) rmp->version, vpe_api_get_version(),
4173                  ARRAY_LEN(rmp->version)-1);
4174         strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
4175                  ARRAY_LEN(rmp->build_date)-1);
4176     }));
4177 }
4178
4179 static void vl_api_get_node_index_t_handler
4180 (vl_api_get_node_index_t * mp)
4181 {
4182     vlib_main_t * vm = vlib_get_main();
4183     vl_api_get_node_index_reply_t * rmp;
4184     vlib_node_t * n;
4185     int rv = 0;
4186     u32 node_index = ~0;
4187
4188     n = vlib_get_node_by_name (vm, mp->node_name);
4189
4190     if (n == 0)
4191         rv = VNET_API_ERROR_NO_SUCH_NODE;
4192     else
4193         node_index = n->index;
4194
4195     REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY, 
4196     ({
4197         rmp->node_index = ntohl(node_index);
4198     }))
4199 }
4200
4201 static void vl_api_add_node_next_t_handler
4202 (vl_api_add_node_next_t * mp)
4203 {
4204     vlib_main_t * vm = vlib_get_main();
4205     vl_api_add_node_next_reply_t * rmp;
4206     vlib_node_t * n, * next;
4207     int rv = 0;
4208     u32 next_index = ~0;
4209
4210     n = vlib_get_node_by_name (vm, mp->node_name);
4211
4212     if (n == 0) {
4213         rv = VNET_API_ERROR_NO_SUCH_NODE;
4214         goto out;
4215     }
4216
4217     next = vlib_get_node_by_name (vm, mp->next_name);
4218
4219     if (next == 0) 
4220         rv = VNET_API_ERROR_NO_SUCH_NODE2;
4221     else 
4222         next_index = vlib_node_add_next (vm, n->index, next->index);
4223
4224 out:
4225     REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY, 
4226     ({
4227         rmp->next_index = ntohl(next_index);
4228     }))
4229 }
4230
4231 static void vl_api_l2tpv3_create_tunnel_t_handler 
4232 (vl_api_l2tpv3_create_tunnel_t *mp)
4233 {
4234     vl_api_l2tpv3_create_tunnel_reply_t * rmp;
4235     l2t_main_t *lm = &l2t_main;
4236     u32 sw_if_index = (u32)~0;
4237     int rv;
4238     
4239     if (mp->is_ipv6 != 1) {
4240         rv = VNET_API_ERROR_UNIMPLEMENTED;
4241         goto out;
4242     }
4243
4244     rv = create_l2tpv3_ipv6_tunnel (lm,
4245                                (ip6_address_t *) mp->client_address,
4246                                (ip6_address_t *) mp->our_address,
4247                                ntohl(mp->local_session_id),
4248                                ntohl(mp->remote_session_id),
4249                                clib_net_to_host_u64(mp->local_cookie),
4250                                clib_net_to_host_u64(mp->remote_cookie),
4251                                mp->l2_sublayer_present, 
4252                                &sw_if_index);
4253
4254 out:
4255     REPLY_MACRO2(VL_API_L2TPV3_CREATE_TUNNEL_REPLY,
4256     ({
4257         rmp->sw_if_index = ntohl (sw_if_index);
4258     }))
4259 }
4260
4261 static void vl_api_l2tpv3_set_tunnel_cookies_t_handler 
4262 (vl_api_l2tpv3_set_tunnel_cookies_t *mp)
4263 {
4264     vl_api_l2tpv3_set_tunnel_cookies_reply_t * rmp;
4265     l2t_main_t *lm = &l2t_main;
4266     int rv;
4267
4268     VALIDATE_SW_IF_INDEX(mp);
4269
4270     rv = l2tpv3_set_tunnel_cookies (lm, ntohl(mp->sw_if_index),
4271                                   clib_net_to_host_u64(mp->new_local_cookie),
4272                                   clib_net_to_host_u64(mp->new_remote_cookie));
4273
4274     BAD_SW_IF_INDEX_LABEL;
4275     
4276     REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY);
4277 }
4278
4279 static void vl_api_l2tpv3_interface_enable_disable_t_handler 
4280 (vl_api_l2tpv3_interface_enable_disable_t * mp)
4281 {
4282     int rv;
4283     vnet_main_t * vnm = vnet_get_main();
4284     vl_api_l2tpv3_interface_enable_disable_reply_t * rmp;
4285
4286     VALIDATE_SW_IF_INDEX(mp);
4287
4288     rv = l2tpv3_interface_enable_disable 
4289         (vnm, ntohl(mp->sw_if_index), mp->enable_disable);
4290
4291     BAD_SW_IF_INDEX_LABEL;
4292
4293     REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY);
4294 }
4295
4296 static void vl_api_l2tpv3_set_lookup_key_t_handler 
4297 (vl_api_l2tpv3_set_lookup_key_t * mp)
4298 {
4299     int rv = 0;
4300     l2t_main_t *lm = &l2t_main;
4301     vl_api_l2tpv3_set_lookup_key_reply_t * rmp;
4302
4303     if (mp->key > L2T_LOOKUP_SESSION_ID) {
4304         rv = VNET_API_ERROR_INVALID_VALUE;
4305         goto out;
4306     }
4307     
4308     lm->lookup_type = mp->key;
4309
4310 out:
4311     REPLY_MACRO (VL_API_L2TPV3_SET_LOOKUP_KEY_REPLY);
4312 }
4313
4314 static void vl_api_vxlan_add_del_tunnel_t_handler 
4315 (vl_api_vxlan_add_del_tunnel_t * mp)
4316 {
4317     vl_api_vxlan_add_del_tunnel_reply_t * rmp;
4318     int rv = 0;
4319     vnet_vxlan_add_del_tunnel_args_t _a, *a = &_a;
4320     u32 encap_fib_index;
4321     uword * p;
4322     ip4_main_t * im = &ip4_main;
4323     u32 sw_if_index = ~0;
4324
4325     p = hash_get (im->fib_index_by_table_id, ntohl(mp->encap_vrf_id));
4326     if (! p) {
4327         rv = VNET_API_ERROR_NO_SUCH_FIB;
4328         goto out;
4329     }
4330     encap_fib_index = p[0];
4331
4332     /* Check src & dst are different */
4333     if ((a->is_ip6 && memcmp(mp->src_address, mp->dst_address, 16) == 0) ||
4334        (!a->is_ip6 && memcmp(mp->src_address, mp->dst_address, 4) == 0)) {
4335         rv = VNET_API_ERROR_SAME_SRC_DST;
4336         goto out;
4337     }
4338     memset (a, 0, sizeof (*a));
4339
4340     a->is_add = mp->is_add;
4341     a->is_ip6 = mp->is_ipv6;
4342
4343     /* ip addresses sent in network byte order */
4344     if (a->is_ip6) {
4345         memcpy(&(a->src.ip6), mp->src_address, 16);
4346         memcpy(&(a->dst.ip6), mp->dst_address, 16);
4347     } else {
4348         memcpy(&(a->src.ip4), mp->src_address, 4);
4349         memcpy(&(a->dst.ip4), mp->dst_address, 4);
4350     }
4351
4352     a->encap_fib_index = encap_fib_index;
4353     a->decap_next_index = ntohl(mp->decap_next_index);
4354     a->vni = ntohl(mp->vni);
4355     rv = vnet_vxlan_add_del_tunnel (a, &sw_if_index);
4356     
4357 out:
4358     REPLY_MACRO2(VL_API_VXLAN_ADD_DEL_TUNNEL_REPLY,
4359     ({
4360         rmp->sw_if_index = ntohl (sw_if_index);
4361     }));
4362 }
4363
4364 static void send_vxlan_tunnel_details
4365 (vxlan_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4366 {
4367     vl_api_vxlan_tunnel_details_t * rmp;
4368     ip4_main_t * im4 = &ip4_main;
4369     ip6_main_t * im6 = &ip6_main;
4370     u8 is_ipv6 = !(t->flags & VXLAN_TUNNEL_IS_IPV4);
4371
4372     rmp = vl_msg_api_alloc (sizeof (*rmp));
4373     memset (rmp, 0, sizeof (*rmp));
4374     rmp->_vl_msg_id = ntohs(VL_API_VXLAN_TUNNEL_DETAILS);
4375     if (is_ipv6) {
4376         memcpy(rmp->src_address, &(t->src.ip6), 16);
4377         memcpy(rmp->dst_address, &(t->dst.ip6), 16);
4378         rmp->encap_vrf_id = htonl(im6->fibs[t->encap_fib_index].table_id);
4379     } else {
4380         memcpy(rmp->src_address, &(t->src.ip4), 4);
4381         memcpy(rmp->dst_address, &(t->dst.ip4), 4);
4382         rmp->encap_vrf_id = htonl(im4->fibs[t->encap_fib_index].table_id);
4383     }
4384     rmp->vni = htonl(t->vni);
4385     rmp->decap_next_index = htonl(t->decap_next_index);
4386     rmp->sw_if_index = htonl(t->sw_if_index);
4387     rmp->is_ipv6 = is_ipv6;
4388     rmp->context = context;
4389
4390     vl_msg_api_send_shmem (q, (u8 *)&rmp);
4391 }
4392
4393 static void vl_api_vxlan_tunnel_dump_t_handler
4394 (vl_api_vxlan_tunnel_dump_t * mp)
4395 {
4396     unix_shared_memory_queue_t * q;
4397     vxlan_main_t * vxm = &vxlan_main;
4398     vxlan_tunnel_t * t;
4399     u32 sw_if_index;
4400
4401     q = vl_api_client_index_to_input_queue (mp->client_index);
4402     if (q == 0) {
4403         return;
4404     }
4405
4406     sw_if_index = ntohl(mp->sw_if_index);
4407
4408     if (~0 == sw_if_index) {
4409         pool_foreach (t, vxm->tunnels,
4410         ({
4411             send_vxlan_tunnel_details(t, q, mp->context);
4412         }));
4413     } else {
4414         if ((sw_if_index >= vec_len(vxm->tunnel_index_by_sw_if_index)) ||
4415                 (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index])) {
4416             return;
4417         }
4418         t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
4419         send_vxlan_tunnel_details(t, q, mp->context);
4420     }
4421 }
4422
4423 static void vl_api_gre_add_del_tunnel_t_handler
4424 (vl_api_gre_add_del_tunnel_t * mp)
4425 {
4426     vl_api_gre_add_del_tunnel_reply_t * rmp;
4427     int rv = 0;
4428     vnet_gre_add_del_tunnel_args_t _a, *a = &_a;
4429     u32 outer_table_id;
4430     uword * p;
4431     ip4_main_t * im = &ip4_main;
4432     u32 sw_if_index = ~0;
4433
4434     p = hash_get (im->fib_index_by_table_id, ntohl(mp->outer_table_id));
4435     if (! p) {
4436         rv = VNET_API_ERROR_NO_SUCH_FIB;
4437         goto out;
4438     }
4439     outer_table_id = p[0];
4440
4441     /* Check src & dst are different */
4442     if (memcmp(&mp->src_address, &mp->dst_address, 4) == 0) {
4443         rv = VNET_API_ERROR_SAME_SRC_DST;
4444         goto out;
4445     }
4446
4447     memset (a, 0, sizeof (*a));
4448
4449     a->is_add = mp->is_add;
4450
4451     /* ip addresses sent in network byte order */
4452     a->src.as_u32 = mp->src_address;
4453     a->dst.as_u32 = mp->dst_address;
4454
4455     a->outer_table_id = outer_table_id;
4456     rv = vnet_gre_add_del_tunnel (a, &sw_if_index);
4457
4458 out:
4459     REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY,
4460     ({
4461         rmp->sw_if_index = ntohl (sw_if_index);
4462     }));
4463 }
4464
4465 static void send_gre_tunnel_details
4466 (gre_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4467 {
4468     vl_api_gre_tunnel_details_t * rmp;
4469     ip4_main_t * im = &ip4_main;
4470
4471     rmp = vl_msg_api_alloc (sizeof (*rmp));
4472     memset (rmp, 0, sizeof (*rmp));
4473     rmp->_vl_msg_id = ntohs(VL_API_GRE_TUNNEL_DETAILS);
4474     rmp->src_address = t->tunnel_src.data_u32;
4475     rmp->dst_address = t->tunnel_dst.data_u32;
4476     rmp->outer_table_id = htonl(im->fibs[t->outer_fib_index].table_id);
4477     rmp->sw_if_index = htonl(t->sw_if_index);
4478     rmp->context = context;
4479
4480     vl_msg_api_send_shmem (q, (u8 *)&rmp);
4481 }
4482
4483 static void vl_api_gre_tunnel_dump_t_handler
4484 (vl_api_gre_tunnel_dump_t * mp)
4485 {
4486     unix_shared_memory_queue_t * q;
4487     gre_main_t * gm = &gre_main;
4488     gre_tunnel_t * t;
4489     u32 sw_if_index;
4490
4491     q = vl_api_client_index_to_input_queue (mp->client_index);
4492     if (q == 0) {
4493         return;
4494     }
4495
4496     sw_if_index = ntohl(mp->sw_if_index);
4497
4498     if (~0 == sw_if_index) {
4499         pool_foreach (t, gm->tunnels,
4500         ({
4501             send_gre_tunnel_details(t, q, mp->context);
4502         }));
4503     } else {
4504         if ((sw_if_index >= vec_len(gm->tunnel_index_by_sw_if_index)) ||
4505                 (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index])) {
4506             return;
4507         }
4508         t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
4509         send_gre_tunnel_details(t, q, mp->context);
4510     }
4511 }
4512
4513 static void 
4514 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t *mp)
4515 {
4516     extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
4517                                       int is_add);
4518     vl_api_l2_patch_add_del_reply_t * rmp;
4519     int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index, 
4520                                int is_add);
4521     int rv = 0;
4522
4523     VALIDATE_RX_SW_IF_INDEX(mp);
4524     VALIDATE_TX_SW_IF_INDEX(mp);
4525
4526     rv = vnet_l2_patch_add_del (ntohl(mp->rx_sw_if_index), 
4527                                 ntohl(mp->tx_sw_if_index), 
4528                                 (int)(mp->is_add != 0));
4529     
4530     BAD_RX_SW_IF_INDEX_LABEL;
4531     BAD_TX_SW_IF_INDEX_LABEL;
4532
4533     REPLY_MACRO(VL_API_L2_PATCH_ADD_DEL_REPLY);
4534 }
4535
4536 static void
4537 vl_api_vxlan_gpe_add_del_tunnel_t_handler 
4538 (vl_api_vxlan_gpe_add_del_tunnel_t * mp)
4539 {
4540     vl_api_vxlan_gpe_add_del_tunnel_reply_t * rmp;
4541     int rv = 0;
4542     vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
4543     u32 encap_fib_index, decap_fib_index;
4544     u8 protocol;
4545     uword * p;
4546     ip4_main_t * im = &ip4_main;
4547     u32 sw_if_index = ~0;
4548
4549
4550     p = hash_get (im->fib_index_by_table_id, ntohl(mp->encap_vrf_id));
4551     if (! p) {
4552         rv = VNET_API_ERROR_NO_SUCH_FIB;
4553         goto out;
4554     }
4555     encap_fib_index = p[0];
4556
4557     protocol = ntohl(mp->protocol);
4558
4559     /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
4560     if (protocol == VXLAN_GPE_INPUT_NEXT_IP4_INPUT) {
4561         p = hash_get (im->fib_index_by_table_id, ntohl(mp->decap_vrf_id));
4562         if (! p) {
4563             rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
4564             goto out;
4565         }
4566         decap_fib_index = p[0];
4567     } else {
4568         decap_fib_index = ntohl(mp->decap_vrf_id);
4569     }
4570
4571     memset (a, 0, sizeof (*a));
4572
4573     a->is_add = mp->is_add;
4574     /* ip addresses sent in network byte order */
4575     a->local.as_u32 = ntohl(mp->local);
4576     a->remote.as_u32 = ntohl(mp->remote);
4577     a->encap_fib_index = encap_fib_index;
4578     a->decap_fib_index = decap_fib_index;
4579     a->protocol = protocol;
4580     a->vni = ntohl(mp->vni);
4581     rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
4582     
4583 out:
4584     REPLY_MACRO2(VL_API_VXLAN_GPE_ADD_DEL_TUNNEL_REPLY,
4585     ({
4586         rmp->sw_if_index = ntohl (sw_if_index);
4587     }));
4588 }
4589
4590 static void
4591 vl_api_lisp_add_del_locator_set_t_handler(vl_api_lisp_add_del_locator_set_t *mp)
4592 {
4593     vl_api_lisp_add_del_locator_set_reply_t *rmp;
4594     int rv = 0;
4595     vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
4596     u32 ls_index = ~0;
4597     u8 *locator_name = NULL;
4598
4599     memset(a, 0, sizeof(a[0]));
4600
4601     locator_name = format(0, "%s", mp->locator_set_name);
4602
4603     a->name = locator_name;
4604     a->locators = NULL;
4605     a->is_add = mp->is_add;
4606     a->local = 1;
4607
4608     rv = vnet_lisp_add_del_locator_set_name(a, &ls_index);
4609
4610     vec_free(locator_name);
4611
4612     REPLY_MACRO(VL_API_LISP_ADD_DEL_LOCATOR_SET_REPLY);
4613 }
4614
4615 static void
4616 vl_api_lisp_add_del_locator_t_handler(
4617     vl_api_lisp_add_del_locator_t *mp)
4618 {
4619     vl_api_lisp_add_del_locator_reply_t *rmp;
4620     int rv = 0;
4621     locator_t locator, *locators = NULL;
4622     vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
4623     u32 ls_index = ~0;
4624     u8 *locator_name = NULL;
4625
4626     memset(&locator, 0, sizeof(locator));
4627     memset(a, 0, sizeof(a[0]));
4628
4629     locator.sw_if_index = ntohl(mp->sw_if_index);
4630     locator.priority = mp->priority;
4631     locator.weight = mp->weight;
4632     locator.local = 1;
4633     vec_add1(locators, locator);
4634
4635     locator_name = format(0, "%s", mp->locator_set_name);
4636
4637     a->name = locator_name;
4638     a->locators = locators;
4639     a->is_add = mp->is_add;
4640     a->local = 1;
4641
4642     rv = vnet_lisp_add_del_locator(a, &ls_index);
4643
4644     vec_free(locators);
4645     vec_free(locator_name);
4646
4647     REPLY_MACRO(VL_API_LISP_ADD_DEL_LOCATOR_REPLY);
4648 }
4649
4650 static void
4651 vl_api_lisp_add_del_local_eid_t_handler(
4652     vl_api_lisp_add_del_local_eid_t *mp)
4653 {
4654     vl_api_lisp_add_del_local_eid_reply_t *rmp;
4655     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
4656     int rv = 0;
4657     ip_prefix_t  *prefp = NULL;
4658     ip_address_t *ip_eid = NULL;
4659     gid_address_t eid;
4660     uword * p = NULL;
4661     u32 locator_set_index = ~0, map_index = ~0;
4662     vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
4663     u8 *name = NULL;
4664
4665     prefp = &gid_address_ippref(&eid);
4666     ip_eid = &ip_prefix_addr(prefp);
4667     gid_address_type (&eid) = GID_ADDR_IP_PREFIX;
4668
4669     if (mp->is_ipv6) {
4670         clib_memcpy(&ip_addr_v6(ip_eid), mp->ip_address,
4671                sizeof(ip_addr_v6(ip_eid)));
4672         ip_addr_version(ip_eid) = IP6;
4673     } else {
4674         clib_memcpy(&ip_addr_v4(ip_eid), mp->ip_address,
4675                sizeof(ip_addr_v4(ip_eid)));
4676         ip_addr_version(ip_eid) = IP4;
4677     }
4678     ip_prefix_len(prefp) = mp->prefix_len;
4679
4680     name = format(0, "%s", mp->locator_set_name);
4681     p = hash_get_mem(lcm->locator_set_index_by_name, name);
4682     if (!p) {
4683         rv = VNET_API_ERROR_INVALID_VALUE;
4684         goto out;
4685     }
4686     locator_set_index = p[0];
4687
4688   /* XXX treat batch configuration */
4689     a->is_add = mp->is_add;
4690     a->deid = eid;
4691     a->locator_set_index = locator_set_index;
4692     a->local = 1;
4693
4694     rv = vnet_lisp_add_del_local_mapping(a, &map_index);
4695
4696 out:
4697     vec_free(name);
4698
4699     REPLY_MACRO(VL_API_LISP_ADD_DEL_LOCAL_EID_REPLY);
4700 }
4701
4702 static void
4703 lisp_gpe_add_del_fwd_entry_set_address(
4704     vl_api_lisp_gpe_add_del_fwd_entry_t *mp,
4705     ip_address_t                        *slocator,
4706     ip_address_t                        *dlocator,
4707     gid_address_t                       *eid)
4708 {
4709     ip_address_t *ip_eid = NULL;
4710     ip_prefix_t *prefp = NULL;
4711
4712     prefp = &gid_address_ippref(eid);
4713     ip_eid = &ip_prefix_addr(prefp);
4714
4715     if (mp->eid_is_ipv6) {
4716         clib_memcpy(&ip_addr_v6(ip_eid), mp->eid_ip_address,
4717                sizeof(ip_addr_v6(ip_eid)));
4718         ip_addr_version(ip_eid) = IP6;
4719     } else {
4720         clib_memcpy(&ip_addr_v4(ip_eid), mp->eid_ip_address,
4721                sizeof(ip_addr_v4(ip_eid)));
4722         ip_addr_version(ip_eid) = IP4;
4723     }
4724     ip_prefix_len(prefp) = mp->eid_prefix_len;
4725
4726     if (mp->address_is_ipv6) {
4727         clib_memcpy(&ip_addr_v6(slocator), mp->source_ip_address,
4728                sizeof(ip_addr_v6(slocator)));
4729         ip_addr_version(slocator) = IP6;
4730         clib_memcpy(&ip_addr_v6(dlocator), mp->destination_ip_address,
4731                sizeof(ip_addr_v6(dlocator)));
4732         ip_addr_version(dlocator) = IP6;
4733     } else {
4734         clib_memcpy(&ip_addr_v4(slocator), mp->source_ip_address,
4735                sizeof(ip_addr_v4(slocator)));
4736         ip_addr_version(slocator) = IP4;
4737         clib_memcpy(&ip_addr_v4(dlocator), mp->destination_ip_address,
4738                sizeof(ip_addr_v4(dlocator)));
4739         ip_addr_version(dlocator) = IP4;
4740     }
4741 }
4742
4743 static void
4744 vl_api_lisp_gpe_add_del_fwd_entry_t_handler(
4745     vl_api_lisp_gpe_add_del_fwd_entry_t *mp)
4746 {
4747     vl_api_lisp_gpe_add_del_fwd_entry_reply_t *rmp;
4748     int rv = 0;
4749     ip_address_t slocator, dlocator;
4750     gid_address_t eid;
4751     vnet_lisp_gpe_add_del_fwd_entry_args_t a;
4752
4753     lisp_gpe_add_del_fwd_entry_set_address(mp, &slocator, &dlocator, &eid);
4754
4755     memset (&a, 0, sizeof(a));
4756
4757     a.is_add = mp->is_add;
4758     a.deid = eid;
4759     a.slocator = slocator;
4760     a.dlocator = dlocator;
4761     rv = vnet_lisp_gpe_add_del_fwd_entry (&a, 0);
4762
4763     REPLY_MACRO(VL_API_LISP_GPE_ADD_DEL_FWD_ENTRY_REPLY);
4764 }
4765
4766 static void
4767 vl_api_lisp_add_del_map_resolver_t_handler(
4768     vl_api_lisp_add_del_map_resolver_t *mp)
4769 {
4770     vl_api_lisp_add_del_map_resolver_reply_t *rmp;
4771     int rv = 0;
4772     ip_address_t *ip_addr = NULL;
4773     vnet_lisp_add_del_map_resolver_args_t _a, * a = &_a;
4774
4775     a->is_add = mp->is_add;
4776     ip_addr = &a->address;
4777
4778     if (mp->is_ipv6) {
4779         clib_memcpy(&ip_addr_v6(ip_addr), mp->ip_address,
4780                sizeof(ip_addr_v6(ip_addr)));
4781         ip_addr_version(ip_addr) = IP6;
4782     } else {
4783         clib_memcpy(&ip_addr_v4(ip_addr), mp->ip_address,
4784                sizeof(ip_addr_v4(ip_addr)));
4785         ip_addr_version(ip_addr) = IP4;
4786     }
4787
4788     rv = vnet_lisp_add_del_map_resolver (a);
4789
4790     REPLY_MACRO(VL_API_LISP_ADD_DEL_MAP_RESOLVER_REPLY);
4791 }
4792
4793 static void
4794 vl_api_lisp_gpe_enable_disable_t_handler(
4795     vl_api_lisp_gpe_enable_disable_t *mp)
4796 {
4797     vl_api_lisp_gpe_enable_disable_reply_t *rmp;
4798     int rv = 0;
4799     vnet_lisp_gpe_enable_disable_args_t _a, * a = &_a;
4800
4801     a->is_en = mp->is_en;
4802     vnet_lisp_gpe_enable_disable (a);
4803
4804     REPLY_MACRO(VL_API_LISP_GPE_ENABLE_DISABLE_REPLY);
4805 }
4806
4807 static void
4808 vl_api_lisp_enable_disable_t_handler(
4809     vl_api_lisp_enable_disable_t *mp)
4810 {
4811     vl_api_lisp_enable_disable_reply_t *rmp;
4812     int rv = 0;
4813
4814     vnet_lisp_enable_disable (mp->is_en);
4815     REPLY_MACRO(VL_API_LISP_ENABLE_DISABLE_REPLY);
4816 }
4817
4818 static void
4819 vl_api_lisp_gpe_add_del_iface_t_handler(
4820     vl_api_lisp_gpe_add_del_iface_t *mp)
4821 {
4822     vl_api_lisp_gpe_add_del_iface_reply_t *rmp;
4823     int rv = 0;
4824     vnet_lisp_gpe_add_del_iface_args_t _a, * a = &_a;
4825
4826     a->is_add = mp->is_add;
4827     a->table_id = mp->table_id;
4828     a->vni = mp->vni;
4829     vnet_lisp_gpe_add_del_iface (a, 0);
4830
4831     REPLY_MACRO(VL_API_LISP_GPE_ADD_DEL_IFACE_REPLY);
4832 }
4833
4834 static void
4835 vl_api_lisp_pitr_set_locator_set_t_handler(
4836   vl_api_lisp_pitr_set_locator_set_t *mp)
4837 {
4838     vl_api_lisp_pitr_set_locator_set_reply_t *rmp;
4839     int rv = 0;
4840     u8 * ls_name = 0;
4841
4842     ls_name = format (0, "%s", mp->ls_name);
4843     rv = vnet_lisp_pitr_set_locator_set (ls_name, mp->is_add);
4844     vec_free (ls_name);
4845
4846     REPLY_MACRO(VL_API_LISP_PITR_SET_LOCATOR_SET_REPLY);
4847 }
4848
4849 /** Used for transferring locators via VPP API */
4850 typedef CLIB_PACKED(struct
4851 {
4852   u8 is_ip4; /**< is locator an IPv4 address */
4853   u8 addr[16]; /**< IPv4/IPv6 address */
4854 }) rloc_t;
4855
4856 static void
4857 send_lisp_locator_set_details_set_address
4858 (vl_api_lisp_locator_set_details_t *rmp,
4859  gid_address_t *gid_address)
4860 {
4861     ip_prefix_t *ip_addr;
4862
4863     if (gid_address_type(gid_address) != GID_ADDR_IP_PREFIX) {
4864         return;
4865     }
4866
4867     ip_addr = &gid_address_ippref(gid_address);
4868     rmp->prefix_len = ip_prefix_len(ip_addr);
4869     rmp->is_ipv6 = ip_prefix_version(ip_addr);
4870     ip_address_copy_addr(rmp->ip_address, &ip_prefix_addr(ip_addr));
4871 }
4872
4873 static void
4874 vl_api_lisp_add_del_remote_mapping_t_handler (
4875     vl_api_lisp_add_del_remote_mapping_t *mp)
4876 {
4877     u32 i;
4878     ip_address_t rloc, * rlocs = 0;
4879     vl_api_lisp_add_del_remote_mapping_reply_t * rmp;
4880     int rv = 0;
4881     gid_address_t _seid, * seid = &_seid;
4882     gid_address_t _deid, * deid = &_deid;
4883     ip_prefix_t * seid_pref = &gid_address_ippref(seid);
4884     ip_prefix_t * deid_pref = &gid_address_ippref(deid);
4885
4886     gid_address_type(seid) = GID_ADDR_IP_PREFIX;
4887     gid_address_type(deid) = GID_ADDR_IP_PREFIX;
4888     ip_address_t * seid_addr = &ip_prefix_addr(seid_pref);
4889     ip_address_t * deid_addr = &ip_prefix_addr(deid_pref);
4890     ip_prefix_len(seid_pref) = mp->seid_len;
4891     ip_prefix_len(deid_pref) = mp->deid_len;
4892     gid_address_set_vni (seid, ntohl (mp->vni));
4893     gid_address_set_vni (deid, ntohl (mp->vni));
4894
4895     if (mp->eid_is_ip4) {
4896         ip_prefix_version(seid_pref) = IP4;
4897         ip_prefix_version(deid_pref) = IP4;
4898         clib_memcpy (&ip_addr_v4(seid_addr),
4899                      mp->seid, sizeof (ip_addr_v4(seid_addr)));
4900         clib_memcpy (&ip_addr_v4(deid_addr),
4901                      mp->deid, sizeof (ip_addr_v4(deid_addr)));
4902     } else {
4903         ip_prefix_version(seid_pref) = IP6;
4904         ip_prefix_version(deid_pref) = IP6;
4905         clib_memcpy (&ip_addr_v6(seid_addr),
4906                      mp->seid, sizeof (ip_addr_v6(seid_addr)));
4907         clib_memcpy (&ip_addr_v6(deid_addr),
4908                      mp->deid, sizeof (ip_addr_v6(deid_addr)));
4909     }
4910
4911     for (i = 0; i < mp->rloc_num; i++) {
4912         rloc_t * r = &((rloc_t *) mp->rlocs)[i];
4913         if (r->is_ip4) {
4914             clib_memcpy (&ip_addr_v4(&rloc), &r->addr, sizeof (rloc_t));
4915             ip_addr_version (&rloc) = IP4;
4916         } else {
4917             clib_memcpy (&ip_addr_v6(&rloc), &r->addr, sizeof (rloc_t));
4918             ip_addr_version (&rloc) = IP6;
4919         }
4920         vec_add1 (rlocs, rloc);
4921     }
4922
4923     rv = vnet_lisp_add_del_remote_mapping (deid, seid, rlocs,
4924                                            mp->action, mp->is_add);
4925     vec_free (rlocs);
4926     REPLY_MACRO(VL_API_LISP_GPE_ADD_DEL_IFACE_REPLY);
4927 }
4928
4929 static void
4930 send_lisp_locator_set_details (lisp_cp_main_t *lcm,
4931                                locator_set_t *lsit,
4932                                unix_shared_memory_queue_t *q,
4933                                u32 context,
4934                                u32 index)
4935 {
4936     vl_api_lisp_locator_set_details_t *rmp;
4937     locator_t *loc = NULL;
4938     u32 * locit = NULL;
4939     u8 * str = NULL;
4940
4941     vec_foreach (locit, lsit->locator_indices) {
4942         loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
4943         rmp = vl_msg_api_alloc (sizeof (*rmp));
4944         memset (rmp, 0, sizeof (*rmp));
4945         rmp->_vl_msg_id = ntohs(VL_API_LISP_LOCATOR_SET_DETAILS);
4946         rmp->local = lsit->local;
4947         if (lsit->local) {
4948             ASSERT(lsit->name != NULL);
4949             strncpy((char *) rmp->locator_set_name,
4950                     (char *) lsit->name, ARRAY_LEN(rmp->locator_set_name) - 1);
4951             rmp->sw_if_index = htonl(loc->sw_if_index);
4952         } else {
4953             str = format(0, "remote-%d", index);
4954             strncpy((char *) rmp->locator_set_name, (char *) str,
4955                     ARRAY_LEN(rmp->locator_set_name) - 1);
4956             send_lisp_locator_set_details_set_address(rmp, &loc->address);
4957
4958             vec_free(str);
4959         }
4960         rmp->priority = loc->priority;
4961         rmp->weight = loc->weight;
4962         rmp->context = context;
4963
4964         vl_msg_api_send_shmem (q, (u8 *)&rmp);
4965     }
4966 }
4967
4968 static void
4969 vl_api_lisp_locator_set_dump_t_handler (vl_api_lisp_locator_set_dump_t *mp)
4970 {
4971     unix_shared_memory_queue_t * q = NULL;
4972     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
4973     locator_set_t * lsit = NULL;
4974     u32 index;
4975
4976     q = vl_api_client_index_to_input_queue (mp->client_index);
4977     if (q == 0) {
4978         return;
4979     }
4980
4981     index = 0;
4982     pool_foreach (lsit, lcm->locator_set_pool,
4983         ({
4984             send_lisp_locator_set_details(lcm, lsit, q, mp->context, index++);
4985         }));
4986 }
4987
4988 static void
4989 send_lisp_local_eid_table_details (mapping_t *mapit,
4990                                    unix_shared_memory_queue_t *q,
4991                                    u32 context)
4992 {
4993     vl_api_lisp_local_eid_table_details_t *rmp = NULL;
4994     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
4995     locator_set_t *ls = NULL;
4996     gid_address_t *gid = NULL;
4997     ip_prefix_t *ip_prefix = NULL;
4998     u8 * str = NULL;
4999     u8 type = ~0;
5000
5001     ls = pool_elt_at_index (lcm->locator_set_pool,
5002                             mapit->locator_set_index);
5003
5004     gid = &mapit->eid;
5005     type = gid_address_type(gid);
5006
5007     if (type != GID_ADDR_IP_PREFIX) {
5008         return;
5009     }
5010
5011     ip_prefix = &gid_address_ippref(gid);
5012
5013     rmp = vl_msg_api_alloc (sizeof (*rmp));
5014     memset (rmp, 0, sizeof (*rmp));
5015     rmp->_vl_msg_id = ntohs(VL_API_LISP_LOCAL_EID_TABLE_DETAILS);
5016     if (ls->local) {
5017         ASSERT(ls->name != NULL);
5018         strncpy((char *) rmp->locator_set_name,
5019                 (char *) ls->name, ARRAY_LEN(rmp->locator_set_name) - 1);
5020     } else {
5021             str = format(0, "remote-%d", mapit->locator_set_index);
5022             strncpy((char *) rmp->locator_set_name, (char *) str,
5023                     ARRAY_LEN(rmp->locator_set_name) - 1);
5024             vec_free(str);
5025     }
5026
5027     switch (ip_prefix_version(ip_prefix)) {
5028         case IP4:
5029             rmp->eid_is_ipv6 = 0;
5030             clib_memcpy(rmp->eid_ip_address, &ip_prefix_v4(ip_prefix),
5031                    sizeof(ip_prefix_v4(ip_prefix)));
5032             break;
5033
5034         case IP6:
5035             rmp->eid_is_ipv6 = 1;
5036             clib_memcpy(rmp->eid_ip_address, &ip_prefix_v6(ip_prefix),
5037                    sizeof(ip_prefix_v6(ip_prefix)));
5038             break;
5039
5040         default:
5041             ASSERT(0);
5042     }
5043     rmp->eid_prefix_len = ip_prefix_len(ip_prefix);
5044     rmp->context = context;
5045
5046     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5047 }
5048
5049 static void
5050 vl_api_lisp_local_eid_table_dump_t_handler (
5051     vl_api_lisp_local_eid_table_dump_t *mp)
5052 {
5053     unix_shared_memory_queue_t * q = NULL;
5054     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5055     mapping_t * mapit = NULL;
5056
5057     q = vl_api_client_index_to_input_queue (mp->client_index);
5058     if (q == 0) {
5059         return;
5060     }
5061
5062     pool_foreach (mapit, lcm->mapping_pool,
5063         ({
5064             send_lisp_local_eid_table_details(mapit, q, mp->context);
5065         }));
5066 }
5067
5068 static void
5069 send_lisp_gpe_tunnel_details (lisp_gpe_tunnel_t *tunnel,
5070                               unix_shared_memory_queue_t *q,
5071                               u32 context)
5072 {
5073     vl_api_lisp_gpe_tunnel_details_t *rmp;
5074     lisp_gpe_main_t * lgm = &lisp_gpe_main;
5075
5076     rmp = vl_msg_api_alloc (sizeof (*rmp));
5077     memset (rmp, 0, sizeof (*rmp));
5078     rmp->_vl_msg_id = ntohs(VL_API_LISP_GPE_TUNNEL_DETAILS);
5079
5080     rmp->tunnels = tunnel - lgm->tunnels;
5081
5082     rmp->is_ipv6 = ip_addr_version(&tunnel->src) == IP6 ? 1 : 0;
5083     ip_address_copy_addr(rmp->source_ip, &tunnel->src);
5084     ip_address_copy_addr(rmp->destination_ip, &tunnel->dst);
5085
5086     rmp->encap_fib_id = htonl(tunnel->encap_fib_index);
5087     rmp->decap_fib_id = htonl(tunnel->decap_fib_index);
5088     rmp->dcap_next = htonl(tunnel->decap_next_index);
5089     rmp->lisp_ver = tunnel->ver_res;
5090     rmp->next_protocol = tunnel->next_protocol;
5091     rmp->flags = tunnel->flags;
5092     rmp->ver_res = tunnel->ver_res;
5093     rmp->res = tunnel->res;
5094     rmp->iid = htonl(tunnel->vni);
5095     rmp->context = context;
5096
5097     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5098 }
5099
5100 static void
5101 vl_api_lisp_gpe_tunnel_dump_t_handler (
5102     vl_api_lisp_local_eid_table_dump_t *mp)
5103 {
5104     unix_shared_memory_queue_t * q = NULL;
5105     lisp_gpe_main_t * lgm = &lisp_gpe_main;
5106     lisp_gpe_tunnel_t * tunnel = NULL;
5107
5108     if (pool_elts(lgm->tunnels) == 0) {
5109         return;
5110     }
5111
5112     q = vl_api_client_index_to_input_queue (mp->client_index);
5113     if (q == 0) {
5114         return;
5115     }
5116
5117     pool_foreach(tunnel, lgm->tunnels,
5118                  ({
5119                      send_lisp_gpe_tunnel_details(tunnel, q, mp->context);
5120                 }));
5121 }
5122
5123 static void
5124 send_lisp_map_resolver_details (ip_address_t *ip,
5125                                 unix_shared_memory_queue_t *q,
5126                                 u32 context)
5127 {
5128     vl_api_lisp_map_resolver_details_t *rmp = NULL;
5129
5130     rmp = vl_msg_api_alloc (sizeof (*rmp));
5131     memset (rmp, 0, sizeof (*rmp));
5132     rmp->_vl_msg_id = ntohs(VL_API_LISP_MAP_RESOLVER_DETAILS);
5133
5134     switch (ip_addr_version(ip)) {
5135         case IP4:
5136             rmp->is_ipv6 = 0;
5137             clib_memcpy(rmp->ip_address, &ip_addr_v4(ip), sizeof(ip_addr_v4(ip)));
5138             break;
5139
5140         case IP6:
5141             rmp->is_ipv6 = 1;
5142             clib_memcpy(rmp->ip_address, &ip_addr_v6(ip), sizeof(ip_addr_v6(ip)));
5143             break;
5144
5145         default:
5146             ASSERT(0);
5147     }
5148     rmp->context = context;
5149
5150     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5151 }
5152
5153 static void
5154 vl_api_lisp_map_resolver_dump_t_handler (
5155     vl_api_lisp_local_eid_table_dump_t *mp)
5156 {
5157     unix_shared_memory_queue_t * q = NULL;
5158     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5159     ip_address_t *ip = NULL;
5160
5161     q = vl_api_client_index_to_input_queue (mp->client_index);
5162     if (q == 0) {
5163         return;
5164     }
5165
5166     vec_foreach(ip, lcm->map_resolvers) {
5167         send_lisp_map_resolver_details(ip, q, mp->context);
5168     }
5169
5170 }
5171
5172 static void
5173 send_lisp_enable_disable_details (unix_shared_memory_queue_t *q,
5174                                       u32 context)
5175 {
5176     vl_api_lisp_enable_disable_status_details_t *rmp = NULL;
5177
5178     rmp = vl_msg_api_alloc (sizeof (*rmp));
5179     memset (rmp, 0, sizeof (*rmp));
5180     rmp->_vl_msg_id = ntohs(VL_API_LISP_ENABLE_DISABLE_STATUS_DETAILS);
5181
5182     rmp->gpe_status = vnet_lisp_gpe_enable_disable_status ();
5183     rmp->feature_status = vnet_lisp_enable_disable_status ();
5184     rmp->context = context;
5185
5186     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5187 }
5188
5189 static void
5190 vl_api_lisp_enable_disable_status_dump_t_handler
5191 (vl_api_lisp_enable_disable_status_dump_t *mp)
5192 {
5193     unix_shared_memory_queue_t * q = NULL;
5194
5195     q = vl_api_client_index_to_input_queue (mp->client_index);
5196     if (q == 0) {
5197         return;
5198     }
5199
5200     send_lisp_enable_disable_details(q, mp->context);
5201 }
5202
5203 static void 
5204 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *mp)
5205 {
5206     vl_api_interface_name_renumber_reply_t * rmp;
5207     int rv = 0;
5208
5209     VALIDATE_SW_IF_INDEX(mp);
5210
5211     rv = vnet_interface_name_renumber 
5212         (ntohl(mp->sw_if_index), ntohl(mp->new_show_dev_instance));
5213     
5214     BAD_SW_IF_INDEX_LABEL;
5215
5216     REPLY_MACRO(VL_API_INTERFACE_NAME_RENUMBER_REPLY);
5217 }
5218
5219 static int arp_change_data_callback (u32 pool_index, u8 * new_mac, 
5220                                      u32 sw_if_index, u32 address)
5221 {
5222     vpe_api_main_t * am = &vpe_api_main;
5223     vlib_main_t * vm = am->vlib_main;
5224     vl_api_ip4_arp_event_t * event;
5225     static f64 arp_event_last_time;
5226     f64 now = vlib_time_now (vm);
5227
5228     if (pool_is_free_index (am->arp_events, pool_index))
5229         return 1;
5230
5231     event = pool_elt_at_index (am->arp_events, pool_index);
5232     if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac))) {
5233         clib_memcpy (event->new_mac, new_mac, sizeof(event->new_mac));
5234     } else { /* same mac */
5235         if ((sw_if_index == event->sw_if_index) && 
5236             ((address == 0) || 
5237              /* for BD case, also check IP address with 10 sec timeout */
5238              ((address == event->address) && 
5239               ((now - arp_event_last_time) < 10.0))))
5240             return 1;
5241     }
5242     
5243     arp_event_last_time = now;
5244     event->sw_if_index = sw_if_index;
5245     if (address) event->address = address;
5246     return 0;
5247 }
5248
5249 static int arp_change_delete_callback (u32 pool_index, u8 * notused)
5250 {
5251     vpe_api_main_t * am = &vpe_api_main;
5252     
5253     if (pool_is_free_index (am->arp_events, pool_index))
5254         return 1;
5255
5256     pool_put_index (am->arp_events, pool_index);
5257     return 0;
5258 }
5259
5260 static void
5261 vl_api_want_ip4_arp_events_t_handler 
5262 (vl_api_want_ip4_arp_events_t * mp)
5263 {
5264     vpe_api_main_t * am = &vpe_api_main;
5265     vnet_main_t * vnm = vnet_get_main();
5266     vl_api_want_ip4_arp_events_reply_t *rmp;
5267     vl_api_ip4_arp_event_t * event;
5268     int rv;
5269     
5270     if (mp->enable_disable) {
5271         pool_get (am->arp_events, event);
5272         memset (event, 0, sizeof (*event));
5273
5274         event->_vl_msg_id = ntohs(VL_API_IP4_ARP_EVENT);
5275         event->client_index = mp->client_index;
5276         event->context = mp->context;
5277         event->address = mp->address;
5278         event->pid = mp->pid;
5279
5280         rv = vnet_add_del_ip4_arp_change_event 
5281             (vnm, arp_change_data_callback,
5282              mp->pid,
5283              &mp->address /* addr, in net byte order */, 
5284              vpe_resolver_process_node.index,
5285              IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */);
5286     } else {
5287         rv = vnet_add_del_ip4_arp_change_event 
5288             (vnm, arp_change_delete_callback,
5289              mp->pid,
5290              &mp->address /* addr, in net byte order */, 
5291              vpe_resolver_process_node.index,
5292              IP4_ARP_EVENT, ~0 /* pool index */, 0 /* is_add */);
5293     }
5294     REPLY_MACRO(VL_API_WANT_IP4_ARP_EVENTS_REPLY);
5295 }
5296
5297 static void vl_api_input_acl_set_interface_t_handler 
5298 (vl_api_input_acl_set_interface_t * mp)
5299 {
5300     vlib_main_t *vm = vlib_get_main();
5301     vl_api_input_acl_set_interface_reply_t * rmp;
5302     int rv;
5303     u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
5304
5305     ip4_table_index = ntohl(mp->ip4_table_index);
5306     ip6_table_index = ntohl(mp->ip6_table_index);
5307     l2_table_index = ntohl(mp->l2_table_index);
5308     sw_if_index = ntohl(mp->sw_if_index);
5309
5310     VALIDATE_SW_IF_INDEX(mp);
5311
5312     rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index, 
5313                                    ip6_table_index, l2_table_index,
5314                                    mp->is_add);
5315
5316     BAD_SW_IF_INDEX_LABEL;
5317
5318     REPLY_MACRO(VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
5319 }
5320
5321 static void vl_api_ipsec_spd_add_del_t_handler
5322 (vl_api_ipsec_spd_add_del_t * mp)
5323 {
5324 #if IPSEC == 0
5325     clib_warning ("unimplemented");
5326 #else
5327
5328     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5329     vl_api_ipsec_spd_add_del_reply_t * rmp;
5330     int rv;
5331
5332 #if DPDK > 0
5333     rv = ipsec_add_del_spd (vm, ntohl(mp->spd_id), mp->is_add);
5334 #else
5335     rv = VNET_API_ERROR_UNIMPLEMENTED;
5336 #endif
5337
5338     REPLY_MACRO(VL_API_IPSEC_SPD_ADD_DEL_REPLY);
5339 #endif
5340 }
5341
5342 static void vl_api_ipsec_interface_add_del_spd_t_handler
5343 (vl_api_ipsec_interface_add_del_spd_t * mp)
5344 {
5345     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5346     vl_api_ipsec_interface_add_del_spd_reply_t * rmp;
5347     int rv;
5348     u32 sw_if_index __attribute__((unused));
5349     u32 spd_id __attribute__((unused));
5350
5351     sw_if_index = ntohl(mp->sw_if_index);
5352     spd_id = ntohl(mp->spd_id);
5353
5354     VALIDATE_SW_IF_INDEX(mp);
5355
5356 #if IPSEC > 0
5357     rv = ipsec_set_interface_spd(vm, sw_if_index, spd_id, mp->is_add);
5358 #else
5359     rv = VNET_API_ERROR_UNIMPLEMENTED;
5360 #endif
5361
5362     BAD_SW_IF_INDEX_LABEL;
5363
5364     REPLY_MACRO(VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
5365 }
5366
5367 static void vl_api_ipsec_spd_add_del_entry_t_handler
5368 (vl_api_ipsec_spd_add_del_entry_t * mp)
5369 {
5370     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5371     vl_api_ipsec_spd_add_del_entry_reply_t * rmp;
5372     int rv;
5373
5374 #if IPSEC > 0
5375     ipsec_policy_t p;
5376
5377     memset(&p, 0, sizeof(p));
5378
5379     p.id = ntohl(mp->spd_id);
5380     p.priority = ntohl(mp->priority);
5381     p.is_outbound = mp->is_outbound;
5382     p.is_ipv6 = mp->is_ipv6;
5383
5384     clib_memcpy(&p.raddr.start, mp->remote_address_start, 16);
5385     clib_memcpy(&p.raddr.stop, mp->remote_address_stop, 16);
5386     clib_memcpy(&p.laddr.start, mp->local_address_start, 16);
5387     clib_memcpy(&p.laddr.stop, mp->local_address_stop, 16);
5388
5389     p.protocol = mp->protocol;
5390     p.rport.start = ntohs(mp->remote_port_start);
5391     p.rport.stop  = ntohs(mp->remote_port_stop);
5392     p.lport.start = ntohs(mp->local_port_start);
5393     p.lport.stop  = ntohs(mp->local_port_stop);
5394     /* policy action resolve unsupported */
5395     if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE) {
5396         clib_warning("unsupported action: 'resolve'");
5397         rv = VNET_API_ERROR_UNIMPLEMENTED;
5398         goto out;
5399     }
5400     p.policy = mp->policy;
5401     p.sa_id = ntohl(mp->sa_id);
5402
5403     rv = ipsec_add_del_policy(vm, &p, mp->is_add);
5404     if (rv)
5405       goto out;
5406
5407     if (mp->is_ip_any) {
5408       p.is_ipv6 = 1;
5409       rv = ipsec_add_del_policy(vm, &p, mp->is_add);
5410     }
5411 #else
5412     rv = VNET_API_ERROR_UNIMPLEMENTED;
5413     goto out;
5414 #endif
5415
5416 out:
5417     REPLY_MACRO(VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
5418 }
5419
5420 static void vl_api_ipsec_sad_add_del_entry_t_handler
5421 (vl_api_ipsec_sad_add_del_entry_t * mp)
5422 {
5423     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5424     vl_api_ipsec_sad_add_del_entry_reply_t * rmp;
5425     int rv;
5426 #if IPSEC > 0
5427     ipsec_sa_t sa;
5428
5429     memset(&sa, 0, sizeof(sa));
5430
5431     sa.id = ntohl(mp->sad_id);
5432     sa.spi = ntohl(mp->spi);
5433     /* security protocol AH unsupported */
5434     if (mp->protocol == IPSEC_PROTOCOL_AH) {
5435         clib_warning("unsupported security protocol 'AH'");
5436         rv = VNET_API_ERROR_UNIMPLEMENTED;
5437         goto out;
5438     }
5439     sa.protocol = mp->protocol;
5440     /* check for unsupported crypto-alg */
5441     if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
5442         mp->crypto_algorithm > IPSEC_CRYPTO_ALG_AES_CBC_256) {
5443         clib_warning("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
5444                      mp->crypto_algorithm);
5445         rv = VNET_API_ERROR_UNIMPLEMENTED;
5446         goto out;
5447     }
5448     sa.crypto_alg = mp->crypto_algorithm;
5449     sa.crypto_key_len = mp->crypto_key_length;
5450     clib_memcpy(&sa.crypto_key, mp->crypto_key, sizeof(sa.crypto_key));
5451     /* check for unsupported integ-alg */
5452     if (mp->integrity_algorithm < IPSEC_INTEG_ALG_SHA1_96 ||
5453         mp->integrity_algorithm > IPSEC_INTEG_ALG_SHA_512_256) {
5454         clib_warning("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
5455                      mp->integrity_algorithm);
5456         rv = VNET_API_ERROR_UNIMPLEMENTED;
5457         goto out;
5458     }
5459     sa.integ_alg = mp->integrity_algorithm;
5460     sa.integ_key_len = mp->integrity_key_length;
5461     clib_memcpy(&sa.integ_key, mp->integrity_key, sizeof(sa.integ_key));
5462     sa.use_esn = mp->use_extended_sequence_number;
5463     sa.is_tunnel = mp->is_tunnel;
5464     sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
5465     clib_memcpy(&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
5466     clib_memcpy(&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
5467
5468     rv = ipsec_add_del_sa(vm, &sa, mp->is_add);
5469 #else
5470     rv = VNET_API_ERROR_UNIMPLEMENTED;
5471     goto out;
5472 #endif
5473
5474 out:
5475     REPLY_MACRO(VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
5476 }
5477
5478 static void
5479 vl_api_ikev2_profile_add_del_t_handler
5480 (vl_api_ikev2_profile_add_del_t * mp)
5481 {
5482     vl_api_ikev2_profile_add_del_reply_t * rmp;
5483     int rv = 0;
5484
5485 #if IPSEC > 0
5486     vlib_main_t * vm = vlib_get_main();
5487     clib_error_t * error;
5488     u8 * tmp = format(0, "%s", mp->name);
5489     error = ikev2_add_del_profile(vm, tmp, mp->is_add);
5490     vec_free (tmp);
5491     if (error)
5492       rv = VNET_API_ERROR_UNSPECIFIED;
5493 #else
5494     rv = VNET_API_ERROR_UNIMPLEMENTED;
5495 #endif
5496
5497    REPLY_MACRO(VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
5498 }
5499
5500 static void
5501 vl_api_ikev2_profile_set_auth_t_handler
5502 (vl_api_ikev2_profile_set_auth_t * mp)
5503 {
5504     vl_api_ikev2_profile_set_auth_reply_t * rmp;
5505     int rv = 0;
5506
5507 #if IPSEC > 0
5508     vlib_main_t * vm = vlib_get_main();
5509     clib_error_t * error;
5510     u8 * tmp = format(0, "%s", mp->name);
5511     u8 * data = vec_new (u8, mp->data_len);
5512     clib_memcpy(data, mp->data, mp->data_len);
5513     error = ikev2_set_profile_auth(vm, tmp, mp->auth_method, data, mp->is_hex);
5514     vec_free (tmp);
5515     vec_free (data);
5516     if (error)
5517       rv = VNET_API_ERROR_UNSPECIFIED;
5518 #else
5519     rv = VNET_API_ERROR_UNIMPLEMENTED;
5520 #endif
5521
5522    REPLY_MACRO(VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
5523 }
5524
5525 static void
5526 vl_api_ikev2_profile_set_id_t_handler
5527 (vl_api_ikev2_profile_set_id_t * mp)
5528 {
5529     vl_api_ikev2_profile_add_del_reply_t * rmp;
5530     int rv = 0;
5531
5532 #if IPSEC > 0
5533     vlib_main_t * vm = vlib_get_main();
5534     clib_error_t * error;
5535     u8 * tmp = format(0, "%s", mp->name);
5536     u8 * data = vec_new (u8, mp->data_len);
5537     clib_memcpy(data, mp->data, mp->data_len);
5538     error = ikev2_set_profile_id(vm, tmp, mp->id_type, data, mp->is_local);
5539     vec_free (tmp);
5540     vec_free (data);
5541     if (error)
5542       rv = VNET_API_ERROR_UNSPECIFIED;
5543 #else
5544     rv = VNET_API_ERROR_UNIMPLEMENTED;
5545 #endif
5546
5547    REPLY_MACRO(VL_API_IKEV2_PROFILE_SET_ID_REPLY);
5548 }
5549
5550 static void
5551 vl_api_ikev2_profile_set_ts_t_handler
5552 (vl_api_ikev2_profile_set_ts_t * mp)
5553 {
5554     vl_api_ikev2_profile_set_ts_reply_t * rmp;
5555     int rv = 0;
5556
5557 #if IPSEC > 0
5558     vlib_main_t * vm = vlib_get_main();
5559     clib_error_t * error;
5560     u8 * tmp = format(0, "%s", mp->name);
5561     error = ikev2_set_profile_ts(vm, tmp, mp->proto, mp->start_port,
5562                                  mp->end_port, (ip4_address_t) mp->start_addr,
5563                                  (ip4_address_t) mp->end_addr, mp->is_local);
5564     vec_free (tmp);
5565     if (error)
5566       rv = VNET_API_ERROR_UNSPECIFIED;
5567 #else
5568     rv = VNET_API_ERROR_UNIMPLEMENTED;
5569 #endif
5570
5571    REPLY_MACRO(VL_API_IKEV2_PROFILE_SET_TS_REPLY);
5572 }
5573
5574 static void
5575 vl_api_ikev2_set_local_key_t_handler
5576 (vl_api_ikev2_set_local_key_t * mp)
5577 {
5578     vl_api_ikev2_profile_set_ts_reply_t * rmp;
5579     int rv = 0;
5580
5581 #if IPSEC > 0
5582     vlib_main_t * vm = vlib_get_main();
5583     clib_error_t * error;
5584
5585     error = ikev2_set_local_key(vm, mp->key_file);
5586     if (error)
5587       rv = VNET_API_ERROR_UNSPECIFIED;
5588 #else
5589     rv = VNET_API_ERROR_UNIMPLEMENTED;
5590 #endif
5591
5592    REPLY_MACRO(VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
5593 }
5594
5595 static void
5596 vl_api_map_add_domain_t_handler
5597 (vl_api_map_add_domain_t * mp)
5598 {
5599   vl_api_map_add_domain_reply_t * rmp;
5600   int rv = 0;
5601   u32 index;
5602   u8 flags = mp->is_translation ? MAP_DOMAIN_TRANSLATION : 0;
5603   rv = map_create_domain((ip4_address_t *)&mp->ip4_prefix, mp->ip4_prefix_len,
5604                          (ip6_address_t *)&mp->ip6_prefix, mp->ip6_prefix_len,
5605                          (ip6_address_t *)&mp->ip6_src, mp->ip6_src_prefix_len,
5606                          mp->ea_bits_len, mp->psid_offset, mp->psid_length, &index, ntohs(mp->mtu), flags);
5607
5608   REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
5609                ({
5610                  rmp->index = ntohl(index);
5611                }));
5612 }
5613
5614 static void
5615 vl_api_map_del_domain_t_handler
5616 (vl_api_map_del_domain_t * mp)
5617 {
5618   vl_api_map_del_domain_reply_t * rmp;
5619   int rv = 0;
5620
5621   rv = map_delete_domain(ntohl(mp->index));
5622
5623   REPLY_MACRO(VL_API_MAP_DEL_DOMAIN_REPLY);
5624 }
5625
5626 static void
5627 vl_api_map_add_del_rule_t_handler
5628 (vl_api_map_add_del_rule_t * mp)
5629 {
5630   vl_api_map_del_domain_reply_t * rmp;
5631   int rv = 0;
5632
5633   rv = map_add_del_psid(ntohl(mp->index), ntohs(mp->psid), (ip6_address_t *)mp->ip6_dst, mp->is_add);
5634
5635   REPLY_MACRO(VL_API_MAP_ADD_DEL_RULE_REPLY);
5636 }
5637
5638 static void
5639 vl_api_map_domain_dump_t_handler
5640 (vl_api_map_domain_dump_t * mp)
5641 {
5642   vl_api_map_domain_details_t * rmp;
5643   map_main_t *mm = &map_main;
5644   map_domain_t *d;
5645   unix_shared_memory_queue_t * q;
5646
5647   if (pool_elts (mm->domains) == 0)
5648       return;
5649
5650   q = vl_api_client_index_to_input_queue (mp->client_index);
5651   if (q == 0) {
5652     return;
5653   }
5654
5655   pool_foreach(d, mm->domains, ({
5656     /* Make sure every field is initiated (or don't skip the memset()) */
5657     rmp = vl_msg_api_alloc (sizeof (*rmp));
5658     rmp->_vl_msg_id = ntohs(VL_API_MAP_DOMAIN_DETAILS);
5659     rmp->domain_index = htonl(d - mm->domains);
5660     rmp->ea_bits_len = d->ea_bits_len;
5661     rmp->psid_offset = d->psid_offset;
5662     rmp->psid_length = d->psid_length;
5663     clib_memcpy(rmp->ip4_prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix));
5664     rmp->ip4_prefix_len = d->ip4_prefix_len;
5665     clib_memcpy(rmp->ip6_prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix));
5666     rmp->ip6_prefix_len = d->ip6_prefix_len;
5667     clib_memcpy(rmp->ip6_src, &d->ip6_src, sizeof(rmp->ip6_src));
5668     rmp->ip6_src_len = d->ip6_src_len;
5669     rmp->mtu = htons(d->mtu);
5670     rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION);
5671     rmp->context = mp->context;
5672
5673     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5674   }));
5675 }
5676
5677 static void
5678 vl_api_map_rule_dump_t_handler
5679 (vl_api_map_rule_dump_t * mp)
5680 {
5681   unix_shared_memory_queue_t * q;
5682   u16 i;
5683   ip6_address_t dst;
5684   vl_api_map_rule_details_t * rmp;
5685   map_main_t *mm = &map_main;
5686   u32 domain_index = ntohl(mp->domain_index);
5687   map_domain_t *d;
5688
5689   if (pool_elts (mm->domains) == 0)
5690    return;
5691
5692   d = pool_elt_at_index(mm->domains, domain_index);
5693   if (!d || !d->rules) {
5694     return;
5695   }
5696
5697   q = vl_api_client_index_to_input_queue (mp->client_index);
5698   if (q == 0) {
5699     return;
5700   }
5701
5702   for (i = 0; i < (0x1 << d->psid_length); i++) {
5703     dst = d->rules[i];
5704     if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0) {
5705       continue;
5706     }
5707     rmp = vl_msg_api_alloc(sizeof(*rmp));
5708     memset(rmp, 0, sizeof(*rmp));
5709     rmp->_vl_msg_id = ntohs(VL_API_MAP_RULE_DETAILS);
5710     rmp->psid = htons(i);
5711     clib_memcpy(rmp->ip6_dst, &dst, sizeof(rmp->ip6_dst));
5712     rmp->context = mp->context;
5713     vl_msg_api_send_shmem(q, (u8 *)&rmp);
5714   }
5715 }
5716
5717 static void
5718 vl_api_map_summary_stats_t_handler (
5719     vl_api_map_summary_stats_t *mp)
5720 {
5721     vl_api_map_summary_stats_reply_t *rmp;
5722     vlib_combined_counter_main_t *cm;
5723     vlib_counter_t v;
5724     int i, which;
5725     u64 total_pkts[VLIB_N_RX_TX];
5726     u64 total_bytes[VLIB_N_RX_TX];
5727     map_main_t *mm = &map_main;
5728     unix_shared_memory_queue_t *q =
5729         vl_api_client_index_to_input_queue(mp->client_index);
5730
5731     if (!q)
5732         return;
5733     
5734     rmp = vl_msg_api_alloc (sizeof (*rmp));
5735     rmp->_vl_msg_id = ntohs(VL_API_MAP_SUMMARY_STATS_REPLY);
5736     rmp->context = mp->context;
5737     rmp->retval = 0;
5738
5739     memset (total_pkts, 0, sizeof (total_pkts));
5740     memset (total_bytes, 0, sizeof (total_bytes));
5741
5742     map_domain_counter_lock (mm);
5743     vec_foreach(cm, mm->domain_counters) {
5744       which = cm - mm->domain_counters;
5745
5746       for (i = 0; i < vec_len(cm->maxi); i++) {
5747         vlib_get_combined_counter (cm, i, &v);
5748         total_pkts[which] += v.packets;
5749         total_bytes[which] += v.bytes;
5750       }
5751     }
5752
5753     map_domain_counter_unlock (mm);
5754
5755     /* Note: in network byte order! */
5756     rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] = clib_host_to_net_u64(total_pkts[MAP_DOMAIN_COUNTER_RX]);
5757     rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] = clib_host_to_net_u64(total_bytes[MAP_DOMAIN_COUNTER_RX]);
5758     rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] = clib_host_to_net_u64(total_pkts[MAP_DOMAIN_COUNTER_TX]);
5759     rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] = clib_host_to_net_u64(total_bytes[MAP_DOMAIN_COUNTER_TX]);
5760     rmp->total_bindings = clib_host_to_net_u64(pool_elts(mm->domains));
5761     rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
5762     rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] = clib_host_to_net_u64(map_error_counter_get(ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
5763     rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] = clib_host_to_net_u64(map_error_counter_get(ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
5764
5765     vl_msg_api_send_shmem(q, (u8 *)&rmp);
5766 }
5767
5768 static void vl_api_ipsec_sa_set_key_t_handler
5769 (vl_api_ipsec_sa_set_key_t * mp)
5770 {
5771     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5772     vl_api_ipsec_sa_set_key_reply_t *rmp;
5773     int rv;
5774 #if IPSEC > 0
5775     ipsec_sa_t sa;
5776     sa.id = ntohl(mp->sa_id);
5777     sa.crypto_key_len = mp->crypto_key_length;
5778     clib_memcpy(&sa.crypto_key, mp->crypto_key, sizeof(sa.crypto_key));
5779     sa.integ_key_len = mp->integrity_key_length;
5780     clib_memcpy(&sa.integ_key, mp->integrity_key, sizeof(sa.integ_key));
5781
5782     rv = ipsec_set_sa_key(vm, &sa);
5783 #else
5784     rv = VNET_API_ERROR_UNIMPLEMENTED;
5785 #endif
5786
5787     REPLY_MACRO(VL_API_IPSEC_SA_SET_KEY_REPLY);
5788 }
5789
5790 static void vl_api_cop_interface_enable_disable_t_handler
5791 (vl_api_cop_interface_enable_disable_t * mp)
5792 {
5793     vl_api_cop_interface_enable_disable_reply_t * rmp;
5794     int rv;
5795     u32 sw_if_index = ntohl(mp->sw_if_index);
5796     int enable_disable;
5797
5798     VALIDATE_SW_IF_INDEX(mp);
5799
5800     enable_disable = (int) mp->enable_disable;
5801
5802     rv = cop_interface_enable_disable (sw_if_index, enable_disable);
5803
5804     BAD_SW_IF_INDEX_LABEL;
5805
5806     REPLY_MACRO(VL_API_COP_INTERFACE_ENABLE_DISABLE_REPLY);
5807 }
5808
5809 static void vl_api_cop_whitelist_enable_disable_t_handler
5810 (vl_api_cop_whitelist_enable_disable_t * mp)
5811 {
5812     vl_api_cop_whitelist_enable_disable_reply_t * rmp;
5813     cop_whitelist_enable_disable_args_t _a, *a=&_a;
5814     u32 sw_if_index = ntohl(mp->sw_if_index);
5815     int rv;
5816
5817     VALIDATE_SW_IF_INDEX(mp);
5818
5819     a->sw_if_index = sw_if_index;
5820     a->ip4 = mp->ip4;
5821     a->ip6 = mp->ip6;
5822     a->default_cop = mp->default_cop;
5823     a->fib_id = ntohl(mp->fib_id);
5824
5825     rv = cop_whitelist_enable_disable (a);
5826
5827     BAD_SW_IF_INDEX_LABEL;
5828
5829     REPLY_MACRO(VL_API_COP_WHITELIST_ENABLE_DISABLE_REPLY);
5830 }
5831
5832 static void vl_api_get_node_graph_t_handler
5833 (vl_api_get_node_graph_t * mp)
5834 {
5835     int rv = 0;
5836     u8 * vector = 0;
5837     api_main_t * am = &api_main;
5838     vlib_main_t * vm = vlib_get_main();
5839     void * oldheap;
5840     vl_api_get_node_graph_reply_t * rmp;
5841     
5842     pthread_mutex_lock (&am->vlib_rp->mutex);
5843     oldheap = svm_push_data_heap (am->vlib_rp);
5844     
5845     /* 
5846      * Keep the number of memcpy ops to a minimum (e.g. 1).
5847      */
5848     vec_validate (vector, 16384);
5849     vec_reset_length (vector);
5850
5851     /* $$$$ FIXME */
5852     vector = vlib_node_serialize (&vm->node_main, vector, 
5853                                   (u32)~0 /* all threads */,
5854                                   1 /* include nexts */,
5855                                   1 /* include stats */);
5856     
5857     svm_pop_heap (oldheap);
5858     pthread_mutex_unlock (&am->vlib_rp->mutex);
5859
5860     REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
5861                  rmp->reply_in_shmem = (uword) vector);
5862 }
5863
5864 static void vl_api_trace_profile_add_t_handler
5865 (vl_api_trace_profile_add_t *mp)
5866 {
5867     int rv = 0;
5868     vl_api_trace_profile_add_reply_t * rmp;
5869     clib_error_t *error;
5870
5871     /* Ignoring the profile id as currently a single profile 
5872      * is supported */
5873     error = ip6_ioam_trace_profile_set(mp->trace_num_elt, mp->trace_type, 
5874                                ntohl(mp->node_id), ntohl(mp->trace_app_data), 
5875                                mp->pow_enable, mp->trace_tsp, 
5876                                mp->trace_ppc);
5877     if (error) {
5878       clib_error_report(error);
5879       rv = clib_error_get_code(error);
5880     }
5881     
5882     REPLY_MACRO(VL_API_TRACE_PROFILE_ADD_REPLY);
5883 }
5884
5885 static void vl_api_trace_profile_apply_t_handler
5886 (vl_api_trace_profile_apply_t *mp)
5887 {
5888     int rv = 0;
5889     vl_api_trace_profile_apply_reply_t * rmp;
5890  
5891     if (mp->enable != 0) {
5892       rv = ip6_ioam_set_destination ((ip6_address_t *)(&mp->dest_ipv6),
5893                                 ntohl(mp->prefix_length),
5894                                 ntohl(mp->vrf_id), 
5895                         mp->trace_op == IOAM_HBYH_ADD,
5896                         mp->trace_op == IOAM_HBYH_POP,
5897                         mp->trace_op == IOAM_HBYH_MOD);
5898     } else {
5899       //ip6_ioam_clear_destination(&ip6, mp->prefix_length, mp->vrf_id);
5900     }
5901     REPLY_MACRO(VL_API_TRACE_PROFILE_APPLY_REPLY);
5902 }
5903
5904 static void vl_api_trace_profile_del_t_handler
5905 (vl_api_trace_profile_del_t *mp)
5906 {
5907     int rv = 0;
5908     vl_api_trace_profile_del_reply_t * rmp;
5909     clib_error_t *error;
5910
5911     error = clear_ioam_rewrite_fn();
5912     if (error) {
5913       clib_error_report(error);
5914       rv = clib_error_get_code(error);
5915     }
5916     
5917     REPLY_MACRO(VL_API_TRACE_PROFILE_DEL_REPLY);
5918 }
5919
5920 static void
5921 vl_api_af_packet_create_t_handler
5922 (vl_api_af_packet_create_t *mp)
5923 {
5924     vlib_main_t *vm = vlib_get_main();
5925     vl_api_af_packet_create_reply_t *rmp;
5926     int rv = 0;
5927     u8 *host_if_name = NULL;
5928
5929     host_if_name = format(0, "%s", mp->host_if_name);
5930     vec_add1 (host_if_name, 0);
5931
5932     rv = af_packet_create_if(vm, host_if_name,
5933                              mp->use_random_hw_addr ? 0 : mp->hw_addr);
5934
5935     vec_free(host_if_name);
5936
5937     REPLY_MACRO(VL_API_AF_PACKET_CREATE_REPLY);
5938 }
5939
5940 static void
5941 vl_api_af_packet_delete_t_handler
5942 (vl_api_af_packet_delete_t *mp)
5943 {
5944     vlib_main_t * vm = vlib_get_main();
5945     vl_api_af_packet_delete_reply_t *rmp;
5946     int rv = 0;
5947     u8 *host_if_name = NULL;
5948
5949     host_if_name = format(0, "%s", mp->host_if_name);
5950     vec_add1 (host_if_name, 0);
5951
5952     rv = af_packet_delete_if(vm, host_if_name);
5953
5954     vec_free(host_if_name);
5955
5956     REPLY_MACRO(VL_API_AF_PACKET_DELETE_REPLY);
5957 }
5958
5959 static void
5960 vl_api_policer_add_del_t_handler
5961 (vl_api_policer_add_del_t *mp)
5962 {
5963     vlib_main_t * vm = vlib_get_main();
5964     vl_api_policer_add_del_reply_t *rmp;
5965     int rv = 0;
5966     u8 *name = NULL;
5967     sse2_qos_pol_cfg_params_st cfg;
5968     clib_error_t * error;
5969
5970     name = format(0, "%s", mp->name);
5971
5972     memset (&cfg, 0, sizeof (cfg));
5973     cfg.rfc = mp->type;
5974     cfg.rnd_type = mp->round_type;
5975     cfg.rate_type = mp->rate_type;
5976     cfg.rb.kbps.cir_kbps = mp->cir;
5977     cfg.rb.kbps.eir_kbps = mp->eir;
5978     cfg.rb.kbps.cb_bytes = mp->cb;
5979     cfg.rb.kbps.eb_bytes = mp->eb;
5980
5981     error = policer_add_del(vm, name, &cfg, mp->is_add);
5982
5983     if (error)
5984       rv = VNET_API_ERROR_UNSPECIFIED;
5985
5986     REPLY_MACRO(VL_API_POLICER_ADD_DEL_REPLY);
5987 }
5988
5989 static void
5990 send_policer_details (u8 *name,
5991                       sse2_qos_pol_cfg_params_st *config,
5992                       policer_read_response_type_st *templ,
5993                       unix_shared_memory_queue_t *q,
5994                       u32 context)
5995 {
5996     vl_api_policer_details_t * mp;
5997
5998     mp = vl_msg_api_alloc (sizeof (*mp));
5999     memset (mp, 0, sizeof (*mp));
6000     mp->_vl_msg_id = ntohs (VL_API_POLICER_DETAILS);
6001     mp->context = context;
6002     mp->cir = htonl(config->rb.kbps.cir_kbps);
6003     mp->eir = htonl(config->rb.kbps.eir_kbps);
6004     mp->cb = htonl(config->rb.kbps.cb_bytes);
6005     mp->eb = htonl(config->rb.kbps.eb_bytes);
6006     mp->rate_type = config->rate_type;
6007     mp->round_type = config->rnd_type;
6008     mp->type = config->rfc;
6009     mp->single_rate = templ->single_rate ? 1 : 0;
6010     mp->color_aware = templ->color_aware ? 1 : 0;
6011     mp->scale = htonl(templ->scale);
6012     mp->cir_tokens_per_period = htonl(templ->cir_tokens_per_period);
6013     mp->pir_tokens_per_period = htonl(templ->pir_tokens_per_period);
6014     mp->current_limit = htonl(templ->current_limit);
6015     mp->current_bucket = htonl(templ->current_bucket);
6016     mp->extended_limit = htonl(templ->extended_limit);
6017     mp->extended_bucket = htonl(templ->extended_bucket);
6018     mp->last_update_time = clib_host_to_net_u64(templ->last_update_time);
6019
6020     strncpy ((char *) mp->name, (char *) name, ARRAY_LEN(mp->name) - 1);
6021
6022     vl_msg_api_send_shmem (q, (u8 *)&mp);
6023 }
6024
6025 static void
6026 vl_api_policer_dump_t_handler
6027 (vl_api_policer_dump_t *mp)
6028 {
6029     unix_shared_memory_queue_t * q;
6030     vnet_policer_main_t * pm = &vnet_policer_main;
6031     hash_pair_t * hp;
6032     uword * p;
6033     u32 pool_index;
6034     u8 * match_name = 0;
6035     u8 * name;
6036     sse2_qos_pol_cfg_params_st *config;
6037     policer_read_response_type_st *templ;
6038
6039     q = vl_api_client_index_to_input_queue (mp->client_index);
6040     if (q == 0)
6041         return;
6042
6043     if (mp->match_name_valid) {
6044         match_name = format(0, "%s%c", mp->match_name, 0);
6045     }
6046
6047     if (mp->match_name_valid) {
6048         p = hash_get_mem (pm->policer_config_by_name, match_name);
6049         if (p) {
6050             pool_index = p[0];
6051             config = pool_elt_at_index (pm->configs, pool_index);
6052             templ = pool_elt_at_index (pm->policer_templates, pool_index);
6053             send_policer_details(match_name, config, templ, q, mp->context);
6054         }
6055     } else {
6056         hash_foreach_pair (hp, pm->policer_config_by_name,
6057         ({
6058             name = (u8 *) hp->key;
6059             pool_index = hp->value[0];
6060             config = pool_elt_at_index (pm->configs, pool_index);
6061             templ = pool_elt_at_index (pm->policer_templates, pool_index);
6062             send_policer_details(name, config, templ, q, mp->context);
6063         }));
6064     }
6065 }
6066
6067 static void
6068 vl_api_netmap_create_t_handler
6069 (vl_api_netmap_create_t *mp)
6070 {
6071     vlib_main_t *vm = vlib_get_main();
6072     vl_api_netmap_create_reply_t *rmp;
6073     int rv = 0;
6074     u8 *if_name = NULL;
6075
6076     if_name = format(0, "%s", mp->netmap_if_name);
6077     vec_add1 (if_name, 0);
6078
6079     rv = netmap_create_if(vm, if_name, mp->use_random_hw_addr ? 0 : mp->hw_addr,
6080                           mp->is_pipe, mp->is_master);
6081
6082     vec_free(if_name);
6083
6084     REPLY_MACRO(VL_API_NETMAP_CREATE_REPLY);
6085 }
6086
6087 static void
6088 vl_api_netmap_delete_t_handler
6089 (vl_api_netmap_delete_t *mp)
6090 {
6091     vlib_main_t * vm = vlib_get_main();
6092     vl_api_netmap_delete_reply_t *rmp;
6093     int rv = 0;
6094     u8 *if_name = NULL;
6095
6096     if_name = format(0, "%s", mp->netmap_if_name);
6097     vec_add1 (if_name, 0);
6098
6099     rv = netmap_delete_if(vm, if_name);
6100
6101     vec_free(if_name);
6102
6103     REPLY_MACRO(VL_API_NETMAP_DELETE_REPLY);
6104 }
6105
6106 #define BOUNCE_HANDLER(nn)                                              \
6107 static void vl_api_##nn##_t_handler (                                   \
6108     vl_api_##nn##_t *mp)                                                \
6109 {                                                                       \
6110     vpe_client_registration_t *reg;                                     \
6111     vpe_api_main_t * vam = &vpe_api_main;                               \
6112     unix_shared_memory_queue_t * q;                                     \
6113                                                                         \
6114     /* One registration only... */                                      \
6115     pool_foreach(reg, vam->nn##_registrations,                          \
6116     ({                                                                  \
6117         q = vl_api_client_index_to_input_queue (reg->client_index);     \
6118         if (q) {                                                        \
6119             /*                                                          \
6120              * If the queue is stuffed, turf the msg and complain       \
6121              * It's unlikely that the intended recipient is             \
6122              * alive; avoid deadlock at all costs.                      \
6123              */                                                         \
6124             if (q->cursize == q->maxsize) {                             \
6125                 clib_warning ("ERROR: receiver queue full, drop msg");  \
6126                 vl_msg_api_free (mp);                                   \
6127                 return;                                                 \
6128             }                                                           \
6129             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
6130             return;                                                     \
6131         }                                                               \
6132     }));                                                                \
6133     vl_msg_api_free (mp);                                               \
6134 }
6135
6136 /*
6137  * vpe_api_hookup
6138  * Add vpe's API message handlers to the table.
6139  * vlib has alread mapped shared memory and
6140  * added the client registration handlers. 
6141  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
6142  */
6143
6144 static clib_error_t *
6145 vpe_api_hookup (vlib_main_t *vm)
6146 {
6147     api_main_t * am = &api_main;
6148
6149 #define _(N,n)                                                  \
6150     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
6151                            vl_api_##n##_t_handler,              \
6152                            vl_noop_handler,                     \
6153                            vl_api_##n##_t_endian,               \
6154                            vl_api_##n##_t_print,                \
6155                            sizeof(vl_api_##n##_t), 1); 
6156     foreach_vpe_api_msg;
6157 #undef _
6158
6159     /* 
6160      * Manually register the sr tunnel add del msg, so we trace
6161      * enough bytes to capture a typical segment list
6162      */
6163     vl_msg_api_set_handlers (VL_API_SR_TUNNEL_ADD_DEL, 
6164                              "sr_tunnel_add_del",
6165                              vl_api_sr_tunnel_add_del_t_handler,
6166                              vl_noop_handler,
6167                              vl_api_sr_tunnel_add_del_t_endian,
6168                              vl_api_sr_tunnel_add_del_t_print,
6169                              256, 1);
6170
6171
6172     /* 
6173      * Manually register the sr policy add del msg, so we trace
6174      * enough bytes to capture a typical tunnel name list
6175      */
6176     vl_msg_api_set_handlers (VL_API_SR_POLICY_ADD_DEL, 
6177                              "sr_policy_add_del",
6178                              vl_api_sr_policy_add_del_t_handler,
6179                              vl_noop_handler,
6180                              vl_api_sr_policy_add_del_t_endian,
6181                              vl_api_sr_policy_add_del_t_print,
6182                              256, 1);
6183
6184     /* 
6185      * Trace space for 8 MPLS encap labels, classifier mask+match
6186      */
6187     am->api_trace_cfg [VL_API_MPLS_ADD_DEL_ENCAP].size += 8 * sizeof(u32);
6188     am->api_trace_cfg [VL_API_CLASSIFY_ADD_DEL_TABLE].size
6189         += 5 * sizeof (u32x4);
6190     am->api_trace_cfg [VL_API_CLASSIFY_ADD_DEL_SESSION].size
6191         += 5 * sizeof (u32x4);
6192     am->api_trace_cfg [VL_API_VXLAN_ADD_DEL_TUNNEL].size
6193         += 16 * sizeof (u32);
6194     
6195     /* 
6196      * Thread-safe API messages
6197      */
6198     am->is_mp_safe [VL_API_IP_ADD_DEL_ROUTE] = 1;
6199     am->is_mp_safe [VL_API_GET_NODE_GRAPH] = 1;
6200
6201     return 0;
6202 }
6203
6204 VLIB_API_INIT_FUNCTION(vpe_api_hookup);
6205
6206 static clib_error_t *
6207 vpe_api_init (vlib_main_t *vm)
6208 {
6209     vpe_api_main_t *am = &vpe_api_main;
6210
6211     am->vlib_main = vm;
6212     am->vnet_main = vnet_get_main();
6213     am->interface_events_registration_hash = hash_create (0, sizeof (uword));
6214     am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
6215     am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
6216     am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
6217     am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
6218     am->oam_events_registration_hash = hash_create (0, sizeof (uword));
6219
6220     vl_api_init (vm);
6221     vl_set_memory_region_name ("/vpe-api");
6222     vl_enable_disable_memory_api (vm, 1 /* enable it */);
6223
6224     return 0;
6225 }
6226
6227 VLIB_INIT_FUNCTION(vpe_api_init);
6228
6229 static clib_error_t *
6230 chroot_config (vlib_main_t * vm, unformat_input_t * input)
6231 {
6232   u8 * chroot_path;
6233
6234   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
6235     {
6236       if (unformat (input, "prefix %s", &chroot_path))
6237         {
6238           vec_add1 (chroot_path, 0);
6239           vl_set_memory_root_path ((char *)chroot_path);
6240         }
6241       else
6242         return clib_error_return (0, "unknown input `%U'",
6243                                   format_unformat_error, input);
6244     }
6245   return 0;
6246 }
6247 VLIB_EARLY_CONFIG_FUNCTION (chroot_config, "chroot");
6248
6249 void * get_unformat_vnet_sw_interface (void)
6250 {
6251     return (void *) &unformat_vnet_sw_interface;
6252 }
6253
6254 #undef vl_api_version
6255 #define vl_api_version(n,v) static u32 vpe_api_version = v;
6256 #include <api/vpe.api.h>
6257 #undef vl_api_version
6258
6259 int vl_msg_api_version_check (vl_api_memclnt_create_t * mp)
6260 {
6261     if (clib_host_to_net_u32(mp->api_versions[0]) != vpe_api_version) {
6262         clib_warning ("vpe API mismatch: 0x%08x instead of 0x%08x",
6263                       clib_host_to_net_u32 (mp->api_versions[0]),
6264                       vpe_api_version);
6265         return -1;
6266     }
6267     return 0;
6268 }
6269
6270 static u8 * format_arp_event (u8 * s, va_list * args)
6271 {
6272     vl_api_ip4_arp_event_t * event = va_arg (*args, vl_api_ip4_arp_event_t *);
6273
6274     s = format (s, "pid %d: %U", event->pid,
6275                 format_ip4_address, &event->address);
6276     return s;
6277 }
6278
6279 static clib_error_t * 
6280 show_ip4_arp_events_fn (vlib_main_t * vm,
6281                         unformat_input_t * input, 
6282                         vlib_cli_command_t * cmd)
6283 {
6284     vpe_api_main_t * am = &vpe_api_main;
6285     vl_api_ip4_arp_event_t * event;
6286
6287     if (pool_elts (am->arp_events) == 0) {
6288         vlib_cli_output (vm, "No active arp event registrations");
6289         return 0;
6290     }
6291
6292     pool_foreach (event, am->arp_events, 
6293     ({
6294         vlib_cli_output (vm, "%U", format_arp_event, event);
6295     }));
6296
6297     return 0;
6298 }
6299
6300 VLIB_CLI_COMMAND (show_ip4_arp_events, static) = {
6301   .path = "show arp event registrations",
6302   .function = show_ip4_arp_events_fn,
6303   .short_help = "Show arp event registrations",
6304 };