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