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