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