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