Move classify_table_index under the union
[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     sw_if_index = si->sw_if_index;
1863     sub_id = ntohl(mp->sub_id);
1864     
1865     sup_and_sub_key = ((u64)(sw_if_index) << 32) | (u64) sub_id;
1866
1867     p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1868     if (p) {
1869         if (CLIB_DEBUG > 0)
1870             clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
1871                           sw_if_index, sub_id);
1872         rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
1873         goto out;
1874     }
1875
1876     kp = clib_mem_alloc (sizeof (*kp));
1877     *kp = sup_and_sub_key;
1878     
1879     memset (&template, 0, sizeof (template));
1880     template.type = VNET_SW_INTERFACE_TYPE_SUB;
1881     template.sup_sw_if_index = sw_if_index;
1882     template.sub.id = sub_id;
1883     template.sub.eth.flags.no_tags = mp->no_tags;
1884     template.sub.eth.flags.one_tag = mp->one_tag;
1885     template.sub.eth.flags.two_tags = mp->two_tags;
1886     template.sub.eth.flags.dot1ad = mp->dot1ad;
1887     template.sub.eth.flags.exact_match = mp->exact_match;
1888     template.sub.eth.flags.default_sub = mp->default_sub;
1889     template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
1890     template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
1891     template.sub.eth.outer_vlan_id = ntohs(mp->outer_vlan_id);
1892     template.sub.eth.inner_vlan_id = ntohs(mp->inner_vlan_id);
1893     
1894     error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1895     if (error) {
1896         clib_error_report (error);
1897         rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
1898         goto out;
1899     }
1900         
1901     hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
1902     hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1903     
1904     BAD_SW_IF_INDEX_LABEL;
1905     
1906 out:
1907     
1908     REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY, 
1909     ({
1910         rmp->sw_if_index = ntohl(sw_if_index);
1911     }));
1912 }
1913
1914 static void
1915 vl_api_mpls_gre_add_del_tunnel_t_handler (vl_api_mpls_gre_add_del_tunnel_t *mp)
1916 {
1917     vl_api_mpls_gre_add_del_tunnel_reply_t * rmp;
1918     int rv = 0;
1919     stats_main_t * sm = &stats_main;
1920     u32 tunnel_sw_if_index = ~0;
1921
1922     dslock (sm, 1 /* release hint */, 5 /* tag */);
1923
1924     rv = vnet_mpls_gre_add_del_tunnel ((ip4_address_t *)(mp->src_address),
1925                                        (ip4_address_t *)(mp->dst_address),
1926                                        (ip4_address_t *)(mp->intfc_address),
1927                                        (u32)(mp->intfc_address_length),
1928                                        ntohl(mp->inner_vrf_id),
1929                                        ntohl(mp->outer_vrf_id),
1930                                        &tunnel_sw_if_index,
1931                                        mp->l2_only,
1932                                        mp->is_add);
1933     dsunlock (sm);
1934     
1935     REPLY_MACRO2(VL_API_MPLS_GRE_ADD_DEL_TUNNEL_REPLY,
1936     ({
1937         rmp->tunnel_sw_if_index = ntohl(tunnel_sw_if_index);
1938     }));
1939 }
1940
1941 static void
1942 vl_api_mpls_ethernet_add_del_tunnel_t_handler 
1943 (vl_api_mpls_ethernet_add_del_tunnel_t *mp)
1944 {
1945     vl_api_mpls_ethernet_add_del_tunnel_reply_t * rmp;
1946     int rv = 0;
1947     stats_main_t * sm = &stats_main;
1948     u32 tunnel_sw_if_index;
1949
1950     dslock (sm, 1 /* release hint */, 5 /* tag */);
1951
1952     rv = vnet_mpls_ethernet_add_del_tunnel 
1953         (mp->dst_mac_address, (ip4_address_t *)(mp->adj_address),
1954          (u32)(mp->adj_address_length), ntohl(mp->vrf_id), 
1955          ntohl(mp->tx_sw_if_index),
1956          &tunnel_sw_if_index,
1957          mp->l2_only,
1958          mp->is_add);
1959
1960     dsunlock (sm);
1961     
1962     REPLY_MACRO2(VL_API_MPLS_ETHERNET_ADD_DEL_TUNNEL_REPLY,
1963     ({
1964         rmp->tunnel_sw_if_index = ntohl(tunnel_sw_if_index);
1965     }));
1966 }
1967
1968 /* 
1969  * This piece of misery brought to you because the control-plane
1970  * can't figure out the tx interface + dst-mac address all by itself
1971  */
1972 static int mpls_ethernet_add_del_tunnel_2_t_handler 
1973 (vl_api_mpls_ethernet_add_del_tunnel_2_t *mp)
1974 {
1975     pending_route_t * pr;
1976     vl_api_mpls_ethernet_add_del_tunnel_2_t *pme;
1977     vnet_main_t * vnm = vnet_get_main();
1978     vlib_main_t * vm = vlib_get_main();
1979     stats_main_t * sm = &stats_main;
1980     vpe_api_main_t * vam = &vpe_api_main;
1981     u32 inner_fib_index, outer_fib_index;
1982     ip4_main_t * im = &ip4_main;
1983     ip_lookup_main_t * lm = &im->lookup_main;
1984     ip_adjacency_t * adj = 0;
1985     u32 lookup_result;
1986     u32 tx_sw_if_index;
1987     u8 * dst_mac_address;
1988     clib_error_t * e;
1989     uword * p;
1990     int rv;
1991     u32 tunnel_sw_if_index;
1992     
1993     p = hash_get (im->fib_index_by_table_id, ntohl(mp->outer_vrf_id));
1994     if (!p) 
1995         return VNET_API_ERROR_NO_SUCH_FIB;
1996     else 
1997         outer_fib_index = p[0];
1998     
1999     
2000     p = hash_get (im->fib_index_by_table_id, ntohl(mp->inner_vrf_id));
2001     if (!p) 
2002         return VNET_API_ERROR_NO_SUCH_INNER_FIB;
2003     else 
2004         inner_fib_index = p[0];
2005     
2006     if (inner_fib_index == outer_fib_index)
2007         return VNET_API_ERROR_INVALID_VALUE;
2008
2009     lookup_result = ip4_fib_lookup_with_table 
2010         (im, outer_fib_index, 
2011          (ip4_address_t *)mp->next_hop_ip4_address_in_outer_vrf, 
2012          1 /* disable default route */);
2013     
2014     adj = ip_get_adjacency (lm, lookup_result);
2015     tx_sw_if_index = adj->rewrite_header.sw_if_index;
2016
2017     if (mp->is_add && mp->resolve_if_needed) {
2018         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP) {
2019             pool_get (vam->pending_routes, pr);
2020             pr->resolve_type = RESOLVE_MPLS_ETHERNET_ADD_DEL;
2021             pme = &pr->t;
2022             memcpy (pme, mp, sizeof (*pme));
2023             /* recursion block, "just in case" */
2024             pme->resolve_if_needed = 0;
2025             pme->resolve_attempts = ntohl(mp->resolve_attempts);
2026             pme->resolve_opaque = tx_sw_if_index;
2027             vnet_register_ip4_arp_resolution_event 
2028                 (vnm, 
2029                  (ip4_address_t *)&(pme->next_hop_ip4_address_in_outer_vrf),
2030                  vpe_resolver_process_node.index,
2031                  RESOLUTION_EVENT, pr - vam->pending_routes);
2032
2033             vlib_process_signal_event 
2034                 (vm, vpe_resolver_process_node.index,
2035                  RESOLUTION_PENDING_EVENT, 0 /* data */);
2036
2037             /* The interface may be down, etc. */
2038             e = ip4_probe_neighbor 
2039                 (vm, (ip4_address_t *)&(mp->next_hop_ip4_address_in_outer_vrf),
2040                  tx_sw_if_index);
2041
2042             if (e)
2043                 clib_error_report(e);
2044             
2045             return VNET_API_ERROR_IN_PROGRESS;
2046         }
2047     }
2048         
2049     if (adj->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
2050         return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
2051     
2052     dst_mac_address = 
2053         vnet_rewrite_get_data_internal
2054         (&adj->rewrite_header, sizeof (adj->rewrite_data));
2055     
2056     dslock (sm, 1 /* release hint */, 10 /* tag */);
2057
2058     rv = vnet_mpls_ethernet_add_del_tunnel 
2059         (dst_mac_address, (ip4_address_t *)(mp->adj_address),
2060          (u32)(mp->adj_address_length), ntohl(mp->inner_vrf_id), 
2061          tx_sw_if_index, &tunnel_sw_if_index, mp->l2_only, mp->is_add);
2062
2063     dsunlock (sm);
2064
2065     return rv;
2066 }
2067
2068 static void
2069 vl_api_mpls_ethernet_add_del_tunnel_2_t_handler 
2070 (vl_api_mpls_ethernet_add_del_tunnel_2_t *mp)
2071 {
2072     vl_api_mpls_ethernet_add_del_tunnel_reply_t * rmp;
2073     int rv = 0;
2074
2075     rv = mpls_ethernet_add_del_tunnel_2_t_handler (mp);
2076     
2077     REPLY_MACRO(VL_API_MPLS_ETHERNET_ADD_DEL_TUNNEL_2_REPLY);
2078 }
2079
2080
2081 static void
2082 vl_api_mpls_add_del_encap_t_handler (vl_api_mpls_add_del_encap_t *mp)
2083 {
2084     vl_api_mpls_add_del_encap_reply_t * rmp;
2085     int rv;
2086     static u32 * labels;
2087     int i;
2088
2089     vec_reset_length (labels);
2090
2091     for (i = 0; i < mp->nlabels; i++)
2092         vec_add1 (labels, ntohl(mp->labels[i]));
2093
2094     /* $$$$ fixme */
2095     rv = vnet_mpls_add_del_encap ((ip4_address_t *)mp->dst_address,
2096                                   ntohl(mp->vrf_id), labels, 
2097                                   ~0 /* policy_tunnel_index */,
2098                                   0 /* no_dst_hash */, 
2099                                   0 /* indexp */, 
2100                                   mp->is_add);
2101     
2102     REPLY_MACRO(VL_API_MPLS_ADD_DEL_ENCAP_REPLY);
2103 }
2104
2105 static void
2106 vl_api_mpls_add_del_decap_t_handler 
2107 (vl_api_mpls_add_del_decap_t *mp)
2108 {
2109     vl_api_mpls_add_del_decap_reply_t * rmp;
2110     int rv;
2111
2112     rv = vnet_mpls_add_del_decap (ntohl(mp->rx_vrf_id), ntohl(mp->tx_vrf_id),
2113                                   ntohl(mp->label), ntohl(mp->next_index),
2114                                   mp->s_bit, mp->is_add);
2115     
2116     REPLY_MACRO(VL_API_MPLS_ADD_DEL_DECAP_REPLY);
2117 }
2118
2119 static void
2120 vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t *mp)
2121 {
2122     vl_api_proxy_arp_add_del_reply_t * rmp;
2123     u32 fib_index;
2124     int rv;
2125     ip4_main_t * im = &ip4_main;
2126     stats_main_t * sm = &stats_main;
2127     int vnet_proxy_arp_add_del (ip4_address_t *lo_addr,
2128                                 ip4_address_t *hi_addr,
2129                                 u32 fib_index, int is_del);
2130     uword * p;
2131
2132     dslock (sm, 1 /* release hint */, 6 /* tag */);
2133
2134     p = hash_get (im->fib_index_by_table_id, ntohl(mp->vrf_id));
2135
2136     if (! p) {
2137         rv = VNET_API_ERROR_NO_SUCH_FIB;
2138         goto out;
2139     }
2140
2141     fib_index = p[0];
2142
2143     rv = vnet_proxy_arp_add_del ((ip4_address_t *)mp->low_address,
2144                                  (ip4_address_t *)mp->hi_address,
2145                                  fib_index, mp->is_add == 0);
2146     
2147 out:
2148     dsunlock (sm);
2149     REPLY_MACRO(VL_API_PROXY_ARP_ADD_DEL_REPLY);
2150 }
2151
2152 static void
2153 vl_api_proxy_arp_intfc_enable_disable_t_handler 
2154 (vl_api_proxy_arp_intfc_enable_disable_t *mp)
2155 {
2156     int rv = 0;
2157     vnet_main_t * vnm = vnet_get_main();
2158     vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
2159     vnet_sw_interface_t * si;
2160     u32 sw_if_index;
2161     
2162     VALIDATE_SW_IF_INDEX(mp);
2163
2164     sw_if_index = ntohl(mp->sw_if_index);
2165
2166     if (pool_is_free_index (vnm->interface_main.sw_interfaces, 
2167                             sw_if_index)) {
2168         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2169         goto out;
2170     }
2171
2172     si = vnet_get_sw_interface (vnm, sw_if_index);
2173
2174     ASSERT(si);
2175
2176     if (mp->enable_disable)
2177         si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2178     else 
2179         si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2180
2181     BAD_SW_IF_INDEX_LABEL;
2182
2183     out:    
2184     REPLY_MACRO(VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
2185 }
2186
2187 static void
2188 vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t *mp, vlib_main_t * vm)
2189 {
2190     vl_api_ip_neighbor_add_del_reply_t * rmp;
2191     vnet_main_t * vnm = vnet_get_main();
2192     u32 fib_index;
2193     int rv=0;
2194     stats_main_t * sm = &stats_main;
2195
2196     VALIDATE_SW_IF_INDEX(mp);
2197
2198     dslock (sm, 1 /* release hint */, 7 /* tag */);
2199
2200     if (mp->is_ipv6) {
2201         if (mp->is_add)
2202             rv = vnet_set_ip6_ethernet_neighbor 
2203                 (vm, ntohl(mp->sw_if_index),
2204                  (ip6_address_t *)(mp->dst_address), 
2205                  mp->mac_address, sizeof (mp->mac_address));
2206         else
2207             rv = vnet_unset_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         ip4_main_t * im = &ip4_main;
2213         ip_lookup_main_t * lm = &im->lookup_main;
2214         ethernet_arp_ip4_over_ethernet_address_t a;
2215         u32 ai;
2216         ip_adjacency_t *nh_adj;
2217
2218         uword * p = hash_get (im->fib_index_by_table_id, ntohl(mp->vrf_id));
2219         if (! p) {
2220             rv = VNET_API_ERROR_NO_SUCH_FIB;
2221             goto out;
2222         }
2223         fib_index = p[0];
2224
2225         /* 
2226          * Unfortunately, folks have a penchant for
2227          * adding interface addresses to the ARP cache, and
2228          * wondering why the forwarder eventually ASSERTs...
2229          */
2230         ai = ip4_fib_lookup_with_table 
2231             (im, fib_index, (ip4_address_t *)(mp->dst_address), 
2232              1 /* disable default route */);
2233         
2234         if (ai != 0) {
2235             nh_adj = ip_get_adjacency (lm, ai);
2236             /* Never allow manipulation of a local adj! */
2237             if (nh_adj->lookup_next_index == IP_LOOKUP_NEXT_LOCAL) {
2238                 clib_warning("%U matches local adj",
2239                              format_ip4_address, 
2240                              (ip4_address_t *)(mp->dst_address));
2241                 rv = VNET_API_ERROR_ADDRESS_MATCHES_INTERFACE_ADDRESS;
2242                 goto out;
2243             }
2244         }
2245
2246         memcpy (&a.ethernet, mp->mac_address, 6);
2247         memcpy (&a.ip4, mp->dst_address, 4);
2248
2249         if (mp->is_add) 
2250             rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl(mp->sw_if_index), 
2251                                                  fib_index, &a, mp->is_static);
2252         else
2253             rv = vnet_arp_unset_ip4_over_ethernet (vnm, ntohl(mp->sw_if_index), 
2254                                                    fib_index, &a);
2255     }
2256             
2257     BAD_SW_IF_INDEX_LABEL;
2258     out:
2259     dsunlock (sm);
2260     REPLY_MACRO(VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
2261 }
2262
2263 static void 
2264 vl_api_is_address_reachable_t_handler (vl_api_is_address_reachable_t *mp)
2265 {
2266 #if 0
2267     vpe_main_t *rm = &vpe_main;
2268     ip4_main_t *im4 = &ip4_main;
2269     ip6_main_t *im6 = &ip6_main;
2270     ip_lookup_main_t * lm;
2271     union {
2272         ip4_address_t ip4;
2273         ip6_address_t ip6;
2274     } addr;
2275     u32 adj_index, sw_if_index;
2276     vl_api_is_address_reachable_t *rmp;
2277     ip_adjacency_t * adj;
2278     unix_shared_memory_queue_t *q;
2279     
2280     q = vl_api_client_index_to_input_queue (mp->client_index);
2281     if (!q) {
2282         increment_missing_api_client_counter (rm->vlib_main);
2283         return;
2284     }
2285
2286     rmp = vl_msg_api_alloc (sizeof (*rmp));
2287     memcpy (rmp, mp, sizeof (*rmp));
2288
2289     sw_if_index = mp->next_hop_sw_if_index;
2290     memcpy (&addr, mp->address, sizeof (addr));
2291     if (mp->is_ipv6) {
2292         lm = &im6->lookup_main;
2293         adj_index = 
2294             ip6_fib_lookup (im6, sw_if_index, &addr.ip6);
2295     } else {
2296         lm = &im4->lookup_main;
2297         adj_index = 
2298             ip4_fib_lookup (im4, sw_if_index, &addr.ip4);
2299     }
2300     if (adj_index == ~0) {
2301         rmp->is_error = 1;
2302         goto send;
2303     }
2304     adj = ip_get_adjacency (lm, adj_index);
2305
2306     if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE
2307         && adj->rewrite_header.sw_if_index == sw_if_index) {
2308         rmp->is_known = 1;
2309     } else {
2310         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP
2311             && adj->rewrite_header.sw_if_index == sw_if_index) {
2312             if (mp->is_ipv6)
2313                 ip6_probe_neighbor (rm->vlib_main, &addr.ip6, sw_if_index);
2314             else
2315                 ip4_probe_neighbor (rm->vlib_main, &addr.ip4, sw_if_index);
2316         } else if (adj->lookup_next_index == IP_LOOKUP_NEXT_DROP) {
2317             rmp->is_known = 1;
2318             goto send;
2319         }
2320         rmp->is_known = 0;
2321     }
2322
2323 send:
2324     vl_msg_api_send_shmem (q, (u8 *)&rmp);
2325 #endif
2326 }
2327
2328 static void vl_api_sw_interface_details_t_handler (
2329     vl_api_sw_interface_details_t * mp)
2330 {
2331     clib_warning ("BUG");
2332 }
2333
2334 static void vl_api_sw_interface_set_flags_t_handler (
2335     vl_api_sw_interface_set_flags_t * mp)
2336 {
2337    vl_api_sw_interface_set_flags_reply_t *rmp;
2338    vnet_main_t * vnm = vnet_get_main();
2339    int rv = 0;
2340    clib_error_t * error;
2341    u16 flags;
2342
2343    VALIDATE_SW_IF_INDEX(mp);
2344
2345    flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
2346
2347    error = vnet_sw_interface_set_flags (vnm, 
2348                                         ntohl(mp->sw_if_index),
2349                                         flags);        
2350    if (error) {
2351        rv = -1;
2352        clib_error_report (error);
2353    }
2354
2355    BAD_SW_IF_INDEX_LABEL;
2356    REPLY_MACRO(VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
2357 }
2358
2359 static void vl_api_sw_interface_clear_stats_t_handler (
2360     vl_api_sw_interface_clear_stats_t * mp)
2361 {
2362    vl_api_sw_interface_clear_stats_reply_t *rmp;
2363
2364    vnet_main_t * vnm = vnet_get_main();
2365    vnet_interface_main_t * im = &vnm->interface_main;
2366    vlib_simple_counter_main_t * sm;
2367    vlib_combined_counter_main_t * cm;
2368    static vnet_main_t ** my_vnet_mains;
2369    int i, j, n_counters;
2370
2371    int rv = 0;
2372
2373    vec_reset_length (my_vnet_mains);
2374
2375    for (i = 0; i < vec_len (vnet_mains); i++)
2376      {
2377        if (vnet_mains[i])
2378          vec_add1 (my_vnet_mains, vnet_mains[i]);
2379      }
2380
2381    if (vec_len (vnet_mains) == 0)
2382      vec_add1 (my_vnet_mains, vnm);
2383
2384    n_counters = vec_len (im->combined_sw_if_counters);
2385
2386    for (j = 0; j < n_counters; j++)
2387      {
2388        for (i = 0; i < vec_len(my_vnet_mains); i++)
2389          {
2390            im = &my_vnet_mains[i]->interface_main;
2391            cm = im->combined_sw_if_counters + j;
2392            if (mp->sw_if_index == (u32)~0)
2393              vlib_clear_combined_counters (cm);
2394            else
2395              vlib_zero_combined_counter (cm, ntohl(mp->sw_if_index));
2396          }
2397      }
2398
2399    n_counters = vec_len (im->sw_if_counters);
2400
2401    for (j = 0; j < n_counters; j++)
2402      {
2403        for (i = 0; i < vec_len(my_vnet_mains); i++)
2404          {
2405            im = &my_vnet_mains[i]->interface_main;
2406            sm = im->sw_if_counters + j;
2407            if (mp->sw_if_index == (u32)~0)
2408              vlib_clear_simple_counters (sm);
2409            else
2410              vlib_zero_simple_counter (sm, ntohl(mp->sw_if_index));
2411          }
2412      }
2413
2414    REPLY_MACRO(VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
2415 }
2416
2417 static void send_sw_interface_details (vpe_api_main_t * am,
2418                                        unix_shared_memory_queue_t *q,
2419                                        vnet_sw_interface_t * swif,
2420                                        u8 * interface_name)
2421 {
2422     vl_api_sw_interface_details_t * mp;
2423     vnet_hw_interface_t * hi;
2424
2425     hi = vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
2426
2427     mp = vl_msg_api_alloc (sizeof (*mp));
2428     memset (mp, 0, sizeof (*mp));
2429     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_DETAILS);
2430     mp->sw_if_index = ntohl(swif->sw_if_index);
2431     mp->sup_sw_if_index = ntohl(swif->sup_sw_if_index);
2432     mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
2433         1 : 0;
2434     mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 
2435         1 : 0;
2436     mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
2437                        VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
2438     mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
2439                       VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
2440     mp->link_mtu = ntohs(hi->max_packet_bytes);
2441
2442     strncpy ((char *) mp->interface_name, 
2443              (char *) interface_name, ARRAY_LEN(mp->interface_name)-1);
2444     
2445     /* Send the L2 address for ethernet physical intfcs */
2446     if (swif->sup_sw_if_index == swif->sw_if_index
2447         && hi->hw_class_index == ethernet_hw_interface_class.index) {
2448         ethernet_main_t *em = ethernet_get_main (am->vlib_main);
2449         ethernet_interface_t *ei;
2450         
2451         ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
2452         ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
2453         memcpy (mp->l2_address, ei->address, sizeof (ei->address));
2454         mp->l2_address_length = ntohl(sizeof (ei->address));
2455     } else if (swif->sup_sw_if_index != swif->sw_if_index) {
2456         vnet_sub_interface_t *sub = &swif->sub;
2457         mp->sub_id = ntohl(sub->id);
2458         mp->sub_dot1ad = sub->eth.flags.dot1ad;
2459         mp->sub_number_of_tags = sub->eth.flags.one_tag + sub->eth.flags.two_tags*2;
2460         mp->sub_outer_vlan_id = ntohs(sub->eth.outer_vlan_id);
2461         mp->sub_inner_vlan_id = ntohs(sub->eth.inner_vlan_id);
2462         mp->sub_exact_match = sub->eth.flags.exact_match;
2463         mp->sub_default = sub->eth.flags.default_sub;
2464         mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
2465         mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
2466
2467         /* vlan tag rewrite data */
2468         u32 vtr_op = L2_VTR_DISABLED;
2469         u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
2470
2471         if (l2vtr_get(am->vlib_main, am->vnet_main, swif->sw_if_index,
2472                       &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0) {
2473             // error - default to disabled
2474             mp->vtr_op = ntohl(L2_VTR_DISABLED);
2475             clib_warning("cannot get vlan tag rewrite for sw_if_index %d", 
2476                     swif->sw_if_index);
2477         } else {
2478             mp->vtr_op = ntohl(vtr_op);
2479             mp->vtr_push_dot1q = ntohl(vtr_push_dot1q);
2480             mp->vtr_tag1 = ntohl(vtr_tag1);
2481             mp->vtr_tag2 = ntohl(vtr_tag2);
2482         }
2483     }
2484
2485     vl_msg_api_send_shmem (q, (u8 *)&mp);
2486 }
2487
2488 static void send_sw_interface_flags (vpe_api_main_t * am,
2489                                      unix_shared_memory_queue_t *q,
2490                                      vnet_sw_interface_t * swif)
2491 {
2492     vl_api_sw_interface_set_flags_t *mp;
2493     vnet_main_t * vnm = am->vnet_main;
2494
2495     vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, 
2496                                                          swif->sw_if_index);
2497     mp = vl_msg_api_alloc (sizeof (*mp));
2498     memset (mp, 0, sizeof (*mp));
2499     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_SET_FLAGS);
2500     mp->sw_if_index = ntohl(swif->sw_if_index);
2501
2502     mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
2503         1 : 0;
2504     mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 
2505         1 : 0;
2506     vl_msg_api_send_shmem (q, (u8 *)&mp);
2507 }
2508
2509 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
2510                                      unix_shared_memory_queue_t *q,
2511                                              u32 sw_if_index) 
2512     __attribute__((unused));
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 {
2518     vl_api_sw_interface_set_flags_t *mp;
2519
2520     mp = vl_msg_api_alloc (sizeof (*mp));
2521     memset (mp, 0, sizeof (*mp));
2522     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_SET_FLAGS);
2523     mp->sw_if_index = ntohl(sw_if_index);
2524
2525     mp->admin_up_down = 0;
2526     mp->link_up_down = 0;
2527     mp->deleted = 1;
2528     vl_msg_api_send_shmem (q, (u8 *)&mp);
2529 }
2530
2531 static void vl_api_sw_interface_dump_t_handler (
2532     vl_api_sw_interface_dump_t *mp)
2533 {
2534     vpe_api_main_t * am = &vpe_api_main;
2535     vnet_sw_interface_t * swif;
2536     vnet_interface_main_t * im = &am->vnet_main->interface_main;
2537     u8 * filter_string = 0, * name_string = 0;
2538     unix_shared_memory_queue_t * q;
2539     char * strcasestr (char *, char *); /* lnx hdr file botch */
2540
2541     q = vl_api_client_index_to_input_queue (mp->client_index);
2542
2543     if (q == 0)
2544         return;
2545     
2546     if (mp->name_filter_valid) {
2547         mp->name_filter [ARRAY_LEN(mp->name_filter)-1] = 0;
2548         filter_string = format (0, "%s%c", mp->name_filter, 0);
2549     }
2550
2551     pool_foreach (swif, im->sw_interfaces, 
2552     ({
2553         name_string = format (name_string, "%U%c", 
2554                               format_vnet_sw_interface_name,
2555                               am->vnet_main, swif, 0);
2556
2557         if (mp->name_filter_valid == 0 ||
2558             strcasestr((char *) name_string, (char *) filter_string)) {
2559
2560             send_sw_interface_details (am, q, swif, name_string);
2561             send_sw_interface_flags (am, q, swif);
2562         }
2563         _vec_len (name_string) = 0;
2564     }));
2565
2566     vec_free (name_string);
2567     vec_free (filter_string);
2568 }
2569
2570 void send_oam_event (oam_target_t * t)
2571 {
2572     vpe_api_main_t * vam = &vpe_api_main;
2573     unix_shared_memory_queue_t * q;
2574     vpe_client_registration_t *reg;
2575     vl_api_oam_event_t * mp;
2576
2577     pool_foreach(reg, vam->oam_events_registrations, 
2578     ({
2579         q = vl_api_client_index_to_input_queue (reg->client_index);
2580         if (q) {
2581             mp = vl_msg_api_alloc (sizeof (*mp));
2582             mp->_vl_msg_id = ntohs (VL_API_OAM_EVENT);
2583             memcpy (mp->dst_address, &t->dst_address, sizeof (mp->dst_address));
2584             mp->state = t->state;
2585             vl_msg_api_send_shmem (q, (u8 *)&mp);
2586         }
2587     }));
2588 }
2589
2590 static void
2591 vl_api_oam_add_del_t_handler (vl_api_oam_add_del_t *mp)
2592 {
2593     vl_api_oam_add_del_reply_t * rmp;
2594     int rv;
2595
2596     rv = vpe_oam_add_del_target ((ip4_address_t *)mp->src_address,
2597                                  (ip4_address_t *)mp->dst_address,
2598                                  ntohl(mp->vrf_id),
2599                                  (int)(mp->is_add));
2600
2601     REPLY_MACRO(VL_API_OAM_ADD_DEL_REPLY);
2602 }
2603
2604 static void
2605 vl_api_vnet_get_summary_stats_t_handler (
2606     vl_api_vnet_get_summary_stats_t *mp)
2607 {
2608     stats_main_t * sm = &stats_main;
2609     vnet_interface_main_t * im = sm->interface_main;
2610     vl_api_vnet_summary_stats_reply_t *rmp;
2611     vlib_combined_counter_main_t * cm;
2612     vlib_counter_t v;
2613     int i, which;
2614     u64 total_pkts[VLIB_N_RX_TX];
2615     u64 total_bytes[VLIB_N_RX_TX];
2616
2617     unix_shared_memory_queue_t * q =
2618         vl_api_client_index_to_input_queue (mp->client_index);
2619     
2620     if (!q)
2621         return;
2622     
2623     rmp = vl_msg_api_alloc (sizeof (*rmp));
2624     rmp->_vl_msg_id = ntohs(VL_API_VNET_SUMMARY_STATS_REPLY);
2625     rmp->context = mp->context;
2626     rmp->retval = 0;
2627
2628     memset (total_pkts, 0, sizeof (total_pkts));
2629     memset (total_bytes, 0, sizeof (total_bytes));
2630
2631     vnet_interface_counter_lock (im);
2632
2633     vec_foreach (cm, im->combined_sw_if_counters) {
2634         which = cm - im->combined_sw_if_counters;
2635
2636         for (i = 0; i < vec_len (cm->maxi); i++) {
2637             vlib_get_combined_counter (cm, i, &v);
2638             total_pkts[which] += v.packets;
2639             total_bytes[which] += v.bytes;
2640         }
2641     }
2642     vnet_interface_counter_unlock (im);
2643
2644     /* Note: in HOST byte order! */
2645     rmp->total_pkts[VLIB_RX] = total_pkts[VLIB_RX];
2646     rmp->total_bytes[VLIB_RX] = total_bytes[VLIB_RX];
2647     rmp->total_pkts[VLIB_TX] = total_pkts[VLIB_TX];
2648     rmp->total_bytes[VLIB_TX] = total_bytes[VLIB_TX];
2649     rmp->vector_rate = vlib_last_vector_length_per_node (sm->vlib_main);
2650
2651     vl_msg_api_send_shmem (q, (u8 *)&rmp);
2652 }
2653
2654 typedef CLIB_PACKED (struct {
2655   ip4_address_t address;
2656
2657   u32 address_length : 6;
2658
2659   u32 index : 26;
2660 }) ip4_route_t;
2661
2662 static int ip4_reset_fib_t_handler (vl_api_reset_fib_t *mp)
2663 {
2664     vnet_main_t * vnm = vnet_get_main();
2665     vnet_interface_main_t * im = &vnm->interface_main;
2666     ip4_main_t * im4 = &ip4_main;
2667     static ip4_route_t * routes;
2668     static u32 * sw_if_indices_to_shut;
2669     stats_main_t * sm = &stats_main;
2670     ip4_route_t * r;
2671     ip4_fib_t * fib;
2672     u32 sw_if_index;
2673     int i;
2674     int rv = VNET_API_ERROR_NO_SUCH_FIB;
2675     u32 target_fib_id = ntohl(mp->vrf_id);
2676
2677     dslock (sm, 1 /* release hint */, 8 /* tag */);
2678
2679     vec_foreach (fib, im4->fibs) {
2680         vnet_sw_interface_t * si;
2681
2682         if (fib->table_id != target_fib_id)
2683             continue;
2684         
2685         /* remove any mpls/gre tunnels in this fib */
2686         vnet_mpls_gre_delete_fib_tunnels (fib->table_id);
2687
2688         /* remove any mpls encap/decap labels */
2689         mpls_fib_reset_labels (fib->table_id);
2690
2691         /* remove any proxy arps in this fib */
2692         vnet_proxy_arp_fib_reset (fib->table_id);
2693
2694         /* Set the flow hash for this fib to the default */
2695         vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2696
2697         vec_reset_length (sw_if_indices_to_shut);
2698
2699         /* Shut down interfaces in this FIB / clean out intfc routes */
2700         pool_foreach (si, im->sw_interfaces,
2701         ({
2702             u32 sw_if_index = si->sw_if_index;
2703
2704             if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
2705                 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
2706                     fib - im4->fibs))
2707                 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2708         }));
2709                       
2710         for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2711             sw_if_index = sw_if_indices_to_shut[i];
2712             // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
2713
2714             u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2715             flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2716             vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2717         }
2718
2719         vec_reset_length (routes);
2720
2721         for (i = 0; i < ARRAY_LEN (fib->adj_index_by_dst_address); i++) {
2722             uword * hash = fib->adj_index_by_dst_address[i];
2723             hash_pair_t * p;
2724             ip4_route_t x;
2725
2726             x.address_length = i;
2727
2728             hash_foreach_pair (p, hash, 
2729             ({
2730                 x.address.data_u32 = p->key;
2731                 vec_add1 (routes, x);
2732             }));
2733         }
2734       
2735         vec_foreach (r, routes) {
2736             ip4_add_del_route_args_t a;
2737
2738             memset (&a, 0, sizeof (a));
2739             a.flags = IP4_ROUTE_FLAG_FIB_INDEX | IP4_ROUTE_FLAG_DEL;
2740             a.table_index_or_table_id = fib - im4->fibs;
2741             a.dst_address = r->address;
2742             a.dst_address_length = r->address_length;
2743             a.adj_index = ~0;
2744
2745             ip4_add_del_route (im4, &a);
2746             ip4_maybe_remap_adjacencies (im4, fib - im4->fibs, 
2747                                          IP4_ROUTE_FLAG_FIB_INDEX);
2748         } 
2749         rv = 0;
2750         break;
2751     } /* vec_foreach (fib) */
2752
2753     dsunlock(sm);
2754     return rv;
2755 }
2756
2757 typedef struct {
2758   ip6_address_t address;
2759   u32 address_length;
2760   u32 index;
2761 } ip6_route_t;
2762
2763 typedef struct {
2764   u32 fib_index;
2765   ip6_route_t ** routep;
2766 } add_routes_in_fib_arg_t;
2767
2768 static void add_routes_in_fib (clib_bihash_kv_24_8_t * kvp, void *arg)
2769 {
2770   add_routes_in_fib_arg_t * ap = arg;
2771
2772   if (kvp->key[2]>>32 == ap->fib_index)
2773     {
2774       ip6_address_t *addr;
2775       ip6_route_t * r;
2776       addr = (ip6_address_t *) kvp;
2777       vec_add2 (*ap->routep, r, 1);
2778       r->address = addr[0];
2779       r->address_length = kvp->key[2] & 0xFF;
2780       r->index = kvp->value;
2781     }
2782 }
2783
2784 static int ip6_reset_fib_t_handler (vl_api_reset_fib_t *mp)
2785 {
2786     vnet_main_t * vnm = vnet_get_main();
2787     vnet_interface_main_t * im = &vnm->interface_main;
2788     ip6_main_t * im6 = &ip6_main;
2789     stats_main_t * sm = &stats_main;
2790     static ip6_route_t * routes;
2791     static u32 * sw_if_indices_to_shut;
2792     ip6_route_t * r;
2793     ip6_fib_t * fib;
2794     u32 sw_if_index;
2795     int i;
2796     int rv = VNET_API_ERROR_NO_SUCH_FIB;
2797     u32 target_fib_id = ntohl(mp->vrf_id);
2798     add_routes_in_fib_arg_t _a, *a=&_a;
2799     clib_bihash_24_8_t * h = &im6->ip6_lookup_table;
2800
2801     dslock (sm, 1 /* release hint */, 9 /* tag */);
2802
2803     vec_foreach (fib, im6->fibs) {
2804         vnet_sw_interface_t * si;
2805
2806         if (fib->table_id != target_fib_id)
2807             continue;
2808
2809         vec_reset_length (sw_if_indices_to_shut);
2810
2811         /* Shut down interfaces in this FIB / clean out intfc routes */
2812         pool_foreach (si, im->sw_interfaces,
2813         ({
2814             if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
2815                 fib - im6->fibs)
2816                 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2817         }));
2818                       
2819         for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2820             sw_if_index = sw_if_indices_to_shut[i];
2821             // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
2822
2823             u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2824             flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2825             vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2826         }
2827
2828         vec_reset_length (routes);
2829
2830         a->fib_index = fib - im6->fibs;
2831         a->routep = &routes;
2832
2833         clib_bihash_foreach_key_value_pair_24_8 (h, add_routes_in_fib, a);
2834
2835         vec_foreach (r, routes) {
2836             ip6_add_del_route_args_t a;
2837
2838             memset (&a, 0, sizeof (a));
2839             a.flags = IP6_ROUTE_FLAG_FIB_INDEX | IP6_ROUTE_FLAG_DEL;
2840             a.table_index_or_table_id = fib - im6->fibs;
2841             a.dst_address = r->address;
2842             a.dst_address_length = r->address_length;
2843             a.adj_index = ~0;
2844
2845             ip6_add_del_route (im6, &a);
2846             ip6_maybe_remap_adjacencies (im6, fib - im6->fibs, 
2847                                          IP6_ROUTE_FLAG_FIB_INDEX);
2848         } 
2849         rv = 0;
2850         /* Reinstall the neighbor / router discovery routes */
2851         vnet_ip6_fib_init (im6, fib - im6->fibs);
2852         break;
2853     } /* vec_foreach (fib) */
2854
2855     dsunlock(sm);
2856     return rv;
2857 }
2858
2859 static void vl_api_reset_fib_t_handler (vl_api_reset_fib_t *mp)
2860 {
2861     int rv;
2862     vl_api_reset_fib_reply_t * rmp;
2863
2864     if (mp->is_ipv6)
2865         rv = ip6_reset_fib_t_handler (mp);
2866     else
2867         rv = ip4_reset_fib_t_handler (mp);
2868     
2869     REPLY_MACRO(VL_API_RESET_FIB_REPLY);
2870 }
2871
2872
2873 static void
2874 dhcpv4_proxy_config (vl_api_dhcp_proxy_config_t *mp)
2875 {
2876     vl_api_dhcp_proxy_config_reply_t * rmp;
2877     int rv;
2878
2879     rv = dhcp_proxy_set_server ((ip4_address_t *)(&mp->dhcp_server),
2880                                 (ip4_address_t *)(&mp->dhcp_src_address),
2881                                 (u32) ntohl(mp->vrf_id),
2882                                 (int) mp->insert_circuit_id,
2883                                 (int) (mp->is_add == 0));
2884     
2885     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_REPLY);
2886 }
2887
2888
2889 static void
2890 dhcpv6_proxy_config (vl_api_dhcp_proxy_config_t *mp)
2891 {
2892     vl_api_dhcp_proxy_config_reply_t * rmp;
2893     int rv = -1;
2894
2895     rv = dhcpv6_proxy_set_server ((ip6_address_t *)(&mp->dhcp_server),
2896                                 (ip6_address_t *)(&mp->dhcp_src_address),
2897                                 (u32) ntohl(mp->vrf_id),
2898                                 (int) mp->insert_circuit_id,
2899                                 (int) (mp->is_add == 0));
2900     
2901     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_REPLY);
2902 }
2903
2904 static void
2905 dhcpv4_proxy_config_2 (vl_api_dhcp_proxy_config_2_t *mp)
2906 {
2907     vl_api_dhcp_proxy_config_reply_t * rmp;
2908     int rv;
2909
2910     rv = dhcp_proxy_set_server_2 ((ip4_address_t *)(&mp->dhcp_server),
2911                                 (ip4_address_t *)(&mp->dhcp_src_address),
2912                                 (u32) ntohl(mp->rx_vrf_id),
2913                                 (u32) ntohl(mp->server_vrf_id),
2914                                 (int) mp->insert_circuit_id,
2915                                 (int) (mp->is_add == 0));
2916     
2917     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_2_REPLY);
2918 }
2919
2920
2921 static void
2922 dhcpv6_proxy_config_2 (vl_api_dhcp_proxy_config_2_t *mp)
2923 {
2924     vl_api_dhcp_proxy_config_reply_t * rmp;
2925     int rv = -1;
2926
2927 #if 0 // $$$$ FIXME
2928     rv = dhcpv6_proxy_set_server_2 ((ip6_address_t *)(&mp->dhcp_server),
2929                                 (ip6_address_t *)(&mp->dhcp_src_address),
2930                                 (u32) ntohl(mp->rx_vrf_id),
2931                                 (u32) ntohl(mp->server_vrf_id),
2932                                 (int) mp->insert_circuit_id,
2933                                 (int) (mp->is_add == 0));
2934 #else
2935     rv = VNET_API_ERROR_UNIMPLEMENTED;
2936 #endif
2937     
2938     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_2_REPLY);
2939 }
2940
2941
2942 static void
2943 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t *mp)
2944 {
2945     vl_api_dhcp_proxy_set_vss_reply_t *rmp;
2946     int rv;
2947     if (!mp->is_ipv6) 
2948         rv = dhcp_proxy_set_option82_vss(ntohl(mp->tbl_id),
2949                                          ntohl(mp->oui),
2950                                          ntohl(mp->fib_id),
2951                                          (int)mp->is_add == 0);
2952     else
2953          rv = dhcpv6_proxy_set_vss( ntohl(mp->tbl_id),
2954                                          ntohl(mp->oui),
2955                                          ntohl(mp->fib_id),
2956                                          (int)mp->is_add == 0);
2957     
2958     REPLY_MACRO(VL_API_DHCP_PROXY_SET_VSS_REPLY);
2959 }
2960
2961
2962 static void vl_api_dhcp_proxy_config_t_handler 
2963 (vl_api_dhcp_proxy_config_t *mp)
2964 {
2965     if (mp->is_ipv6 == 0)
2966         dhcpv4_proxy_config (mp);
2967     else
2968         dhcpv6_proxy_config (mp);
2969 }
2970
2971 static void vl_api_dhcp_proxy_config_2_t_handler 
2972 (vl_api_dhcp_proxy_config_2_t *mp)
2973 {
2974     if (mp->is_ipv6 == 0)
2975         dhcpv4_proxy_config_2 (mp);
2976     else
2977         dhcpv6_proxy_config_2 (mp);
2978 }
2979
2980 void dhcp_compl_event_callback (u32 client_index, u32 pid, u8 * hostname,
2981        u8 is_ipv6, u8 * host_address, u8 * router_address, u8 * host_mac)
2982 {
2983     unix_shared_memory_queue_t * q;
2984     vl_api_dhcp_compl_event_t * mp;
2985
2986     q = vl_api_client_index_to_input_queue (client_index);
2987     if (!q)
2988         return;
2989
2990     mp = vl_msg_api_alloc (sizeof (*mp));
2991     mp->client_index = client_index;
2992     mp->pid = pid;
2993     mp->is_ipv6 = is_ipv6;
2994     memcpy (&mp->hostname, hostname, vec_len(hostname));
2995     mp->hostname[vec_len(hostname) + 1] = '\n';
2996     memcpy (&mp->host_address[0], host_address, 16);
2997     memcpy (&mp->router_address[0], router_address, 16);
2998     memcpy (&mp->host_mac[0], host_mac, 6);
2999
3000     mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
3001
3002     vl_msg_api_send_shmem (q, (u8 *)&mp);
3003 }
3004
3005 static void vl_api_dhcp_client_config_t_handler 
3006 (vl_api_dhcp_client_config_t *mp)
3007 {
3008     vlib_main_t *vm = vlib_get_main();
3009     vl_api_dhcp_client_config_reply_t * rmp;
3010     int rv = 0;
3011
3012     VALIDATE_SW_IF_INDEX(mp);
3013
3014     rv = dhcp_client_config(vm, ntohl(mp->sw_if_index), 
3015              mp->hostname, mp->is_add, mp->client_index,
3016              mp->want_dhcp_event ? dhcp_compl_event_callback : NULL,
3017              mp->pid);
3018
3019     BAD_SW_IF_INDEX_LABEL;
3020
3021     REPLY_MACRO(VL_API_DHCP_CLIENT_CONFIG_REPLY);
3022 }
3023
3024 static void
3025 vl_api_sw_interface_ip6nd_ra_config_t_handler 
3026 (vl_api_sw_interface_ip6nd_ra_config_t *mp, vlib_main_t *vm)
3027 {
3028    vl_api_sw_interface_ip6nd_ra_config_reply_t * rmp;
3029     int rv = 0;
3030     u8  is_no,  surpress, managed, other, ll_option, send_unicast, cease, default_router;
3031  
3032     is_no = mp->is_no == 1;
3033     surpress = mp->surpress == 1;
3034     managed = mp->managed == 1;
3035    other = mp->other == 1;
3036     ll_option = mp->ll_option == 1;
3037     send_unicast = mp->send_unicast == 1;
3038     cease = mp->cease == 1;
3039     default_router = mp->default_router  == 1;
3040
3041     VALIDATE_SW_IF_INDEX(mp);
3042
3043     rv = ip6_neighbor_ra_config(vm, ntohl(mp->sw_if_index), 
3044                                 surpress,  managed,  other,
3045                                 ll_option,  send_unicast,  cease, 
3046                                 default_router, ntohl (mp->lifetime),
3047                                 ntohl(mp->initial_count),  ntohl(mp->initial_interval),  
3048                                 ntohl(mp->max_interval), ntohl( mp->min_interval),
3049                                 is_no);
3050
3051     BAD_SW_IF_INDEX_LABEL;
3052
3053     REPLY_MACRO(VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
3054 }
3055
3056 static void
3057 vl_api_sw_interface_ip6nd_ra_prefix_t_handler 
3058 (vl_api_sw_interface_ip6nd_ra_prefix_t *mp, vlib_main_t *vm)
3059 {
3060    vl_api_sw_interface_ip6nd_ra_prefix_reply_t * rmp;
3061     int rv = 0;
3062     u8  is_no,  use_default,  no_advertise, off_link, no_autoconfig, no_onlink;
3063  
3064     VALIDATE_SW_IF_INDEX(mp);
3065
3066     is_no = mp->is_no == 1;
3067     use_default = mp->use_default == 1;
3068     no_advertise = mp->no_advertise == 1;
3069     off_link = mp->off_link == 1;
3070     no_autoconfig = mp->no_autoconfig == 1;
3071     no_onlink = mp->no_onlink == 1;
3072  
3073     rv = ip6_neighbor_ra_prefix(vm,  ntohl(mp->sw_if_index),  
3074                                 (ip6_address_t *)mp->address,  mp->address_length,
3075                                 use_default,  ntohl(mp->val_lifetime), ntohl(mp->pref_lifetime),
3076                                 no_advertise,  off_link, no_autoconfig, no_onlink,
3077                                 is_no);
3078
3079     BAD_SW_IF_INDEX_LABEL;
3080     REPLY_MACRO(VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
3081 }
3082
3083 static void
3084 vl_api_sw_interface_ip6_enable_disable_t_handler 
3085 (vl_api_sw_interface_ip6_enable_disable_t *mp, vlib_main_t *vm)
3086 {
3087     vl_api_sw_interface_ip6_enable_disable_reply_t * rmp;
3088     vnet_main_t * vnm = vnet_get_main();
3089     int rv = 0;
3090     clib_error_t * error;
3091
3092     vnm->api_errno = 0;
3093
3094     VALIDATE_SW_IF_INDEX(mp);
3095
3096     error = ( mp->enable == 1) ? enable_ip6_interface(vm,ntohl(mp->sw_if_index)) :
3097         disable_ip6_interface(vm,ntohl(mp->sw_if_index));
3098     
3099     if (error)  {
3100         clib_error_report(error);
3101         rv = VNET_API_ERROR_UNSPECIFIED;
3102     } else {
3103         rv = vnm->api_errno;
3104     }
3105     
3106     BAD_SW_IF_INDEX_LABEL;
3107
3108     REPLY_MACRO(VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
3109 }
3110
3111 static void
3112 vl_api_sw_interface_ip6_set_link_local_address_t_handler 
3113 (vl_api_sw_interface_ip6_set_link_local_address_t *mp, vlib_main_t *vm)
3114 {
3115     vl_api_sw_interface_ip6_set_link_local_address_reply_t * rmp;
3116     int rv = 0;
3117     clib_error_t * error;
3118     vnet_main_t * vnm = vnet_get_main();
3119
3120     vnm->api_errno = 0;
3121
3122     VALIDATE_SW_IF_INDEX(mp);
3123
3124     error = set_ip6_link_local_address(vm,
3125                                        ntohl(mp->sw_if_index),
3126                                        (ip6_address_t *)mp->address,
3127                                        mp->address_length);
3128     if (error)  {
3129       clib_error_report(error);
3130       rv = VNET_API_ERROR_UNSPECIFIED;
3131     } else {
3132         rv = vnm->api_errno;
3133     }
3134
3135     BAD_SW_IF_INDEX_LABEL;
3136     
3137     REPLY_MACRO(VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
3138 }
3139
3140 static void set_ip6_flow_hash (vl_api_set_ip_flow_hash_t *mp)
3141 {
3142     vl_api_set_ip_flow_hash_reply_t *rmp;
3143     int rv = VNET_API_ERROR_UNIMPLEMENTED;
3144
3145     clib_warning ("unimplemented...");
3146     
3147     REPLY_MACRO(VL_API_SET_IP_FLOW_HASH_REPLY);
3148 }
3149
3150 static void set_ip4_flow_hash (vl_api_set_ip_flow_hash_t *mp)
3151 {
3152     vl_api_set_ip_flow_hash_reply_t *rmp;
3153     int rv;
3154     u32 table_id;
3155     u32 flow_hash_config = 0;
3156
3157     table_id = ntohl(mp->vrf_id);
3158
3159 #define _(a,b) if (mp->a) flow_hash_config |= b;
3160     foreach_flow_hash_bit;
3161 #undef _
3162
3163     rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
3164
3165     REPLY_MACRO(VL_API_SET_IP_FLOW_HASH_REPLY);
3166 }
3167
3168
3169 static void vl_api_set_ip_flow_hash_t_handler
3170 (vl_api_set_ip_flow_hash_t *mp)
3171 {
3172     if (mp->is_ipv6 == 0)
3173         set_ip4_flow_hash (mp);
3174     else
3175         set_ip6_flow_hash (mp);
3176 }
3177
3178 static void vl_api_sw_interface_set_unnumbered_t_handler 
3179 (vl_api_sw_interface_set_unnumbered_t *mp)
3180 {
3181     vl_api_sw_interface_set_unnumbered_reply_t * rmp;
3182     int rv = 0;
3183     vnet_sw_interface_t * si;
3184     vnet_main_t *vnm = vnet_get_main();
3185     u32 sw_if_index, unnumbered_sw_if_index;
3186
3187     sw_if_index = ntohl(mp->sw_if_index);
3188     unnumbered_sw_if_index = ntohl(mp->unnumbered_sw_if_index);
3189
3190     /*
3191      * The API message field names are backwards from 
3192      * the underlying data structure names.
3193      * It's not worth changing them now.
3194      */
3195     if (pool_is_free_index (vnm->interface_main.sw_interfaces, 
3196                             unnumbered_sw_if_index)) {
3197         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
3198         goto done;
3199     }
3200     
3201     /* Only check the "use loop0" field when setting the binding */
3202     if (mp->is_add && 
3203         pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index)) {
3204         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
3205         goto done;
3206     }
3207
3208     si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
3209
3210     if (mp->is_add) {
3211         si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
3212         si->unnumbered_sw_if_index = sw_if_index;
3213     } else {
3214         si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
3215         si->unnumbered_sw_if_index = (u32)~0;
3216     }
3217     
3218  done:
3219     REPLY_MACRO(VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
3220 }
3221
3222 static void vl_api_create_loopback_t_handler
3223 (vl_api_create_loopback_t *mp)
3224 {
3225     vl_api_create_loopback_reply_t * rmp;
3226     u32 sw_if_index;
3227     int rv;
3228     
3229     rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address);
3230
3231     REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
3232     ({
3233         rmp->sw_if_index = ntohl (sw_if_index);
3234     }));
3235 }
3236
3237 static void vl_api_delete_loopback_t_handler
3238 (vl_api_delete_loopback_t *mp)
3239 {
3240     vl_api_delete_loopback_reply_t * rmp;
3241     u32 sw_if_index;
3242     int rv;
3243
3244     sw_if_index = ntohl (mp->sw_if_index);
3245     rv = vnet_delete_loopback_interface (sw_if_index);
3246
3247     REPLY_MACRO(VL_API_DELETE_LOOPBACK_REPLY);
3248 }
3249
3250 static void vl_api_control_ping_t_handler
3251 (vl_api_control_ping_t *mp)
3252 {
3253     vl_api_control_ping_reply_t * rmp;
3254     int rv = 0;
3255
3256     REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
3257     ({
3258         rmp->vpe_pid = ntohl (getpid());
3259     }));
3260 }
3261
3262 static void shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
3263 {
3264     u8 **shmem_vecp = (u8 **)arg;
3265     u8 *shmem_vec;
3266     void *oldheap;
3267     api_main_t * am = &api_main;
3268     u32 offset;
3269
3270     shmem_vec = *shmem_vecp;
3271
3272     offset = vec_len (shmem_vec);
3273
3274     pthread_mutex_lock (&am->vlib_rp->mutex);
3275     oldheap = svm_push_data_heap (am->vlib_rp);
3276     
3277     vec_validate (shmem_vec, offset + buffer_bytes - 1);
3278
3279     memcpy (shmem_vec + offset, buffer, buffer_bytes);
3280
3281     svm_pop_heap (oldheap);
3282     pthread_mutex_unlock (&am->vlib_rp->mutex);
3283
3284     *shmem_vecp = shmem_vec;
3285 }
3286
3287
3288 static void vl_api_cli_request_t_handler 
3289 (vl_api_cli_request_t *mp)
3290 {
3291     vl_api_cli_reply_t *rp;
3292     unix_shared_memory_queue_t *q;
3293     vlib_main_t * vm = vlib_get_main();
3294     api_main_t * am = &api_main;
3295     unformat_input_t input;
3296     u8 *shmem_vec=0;
3297     void *oldheap;
3298
3299     q = vl_api_client_index_to_input_queue (mp->client_index);
3300     if (!q)
3301         return;
3302
3303     rp = vl_msg_api_alloc (sizeof (*rp));
3304     rp->_vl_msg_id = ntohs(VL_API_CLI_REPLY);
3305     rp->context = mp->context;
3306
3307     unformat_init_vector (&input, (u8 *)mp->cmd_in_shmem);
3308
3309     vlib_cli_input (vm, &input, shmem_cli_output, 
3310                     (uword)&shmem_vec);
3311
3312     pthread_mutex_lock (&am->vlib_rp->mutex);
3313     oldheap = svm_push_data_heap (am->vlib_rp);
3314     
3315     vec_add1(shmem_vec, 0);
3316
3317     svm_pop_heap (oldheap);
3318     pthread_mutex_unlock (&am->vlib_rp->mutex);
3319
3320     rp->reply_in_shmem = (uword)shmem_vec;
3321
3322     vl_msg_api_send_shmem (q, (u8 *)&rp);
3323 }
3324
3325 static void vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t *mp)
3326 {
3327     int rv;
3328     vl_api_set_arp_neighbor_limit_reply_t * rmp;
3329     vnet_main_t *vnm = vnet_get_main();
3330     clib_error_t * error;
3331
3332     vnm->api_errno = 0;
3333
3334     if (mp->is_ipv6)
3335         error = ip6_set_neighbor_limit (ntohl(mp->arp_neighbor_limit));
3336     else
3337         error = ip4_set_arp_limit (ntohl(mp->arp_neighbor_limit));
3338     
3339     if (error)  {
3340         clib_error_report(error);
3341         rv = VNET_API_ERROR_UNSPECIFIED;
3342     } else {
3343         rv = vnm->api_errno;
3344     }
3345
3346     REPLY_MACRO(VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3347 }
3348
3349 static void vl_api_sr_tunnel_add_del_t_handler
3350 (vl_api_sr_tunnel_add_del_t *mp)
3351 {
3352 #if IPV6SR == 0
3353     clib_warning ("unimplemented");
3354 #else
3355     ip6_sr_add_del_tunnel_args_t _a, *a=&_a;
3356     int rv = 0;
3357     vl_api_sr_tunnel_add_del_reply_t * rmp;
3358     ip6_address_t * segments = 0, * seg;
3359     ip6_address_t * tags = 0, *tag;
3360     ip6_address_t * this_address;
3361     int i;
3362
3363     if (mp->n_segments == 0) {
3364         rv = -11;
3365         goto out;
3366     }
3367
3368     memset (a, 0, sizeof (*a));
3369     a->src_address = (ip6_address_t *)&mp->src_address;
3370     a->dst_address = (ip6_address_t *)&mp->dst_address;
3371     a->dst_mask_width = mp->dst_mask_width;
3372     a->flags_net_byte_order = mp->flags_net_byte_order;
3373     a->is_del = (mp->is_add == 0);
3374     a->rx_table_id = ntohl(mp->outer_vrf_id);
3375     a->tx_table_id = ntohl(mp->inner_vrf_id);
3376
3377     /* Yank segments and tags out of the API message */
3378     this_address = (ip6_address_t *)mp->segs_and_tags;
3379     for (i = 0; i < mp->n_segments; i++) {
3380         vec_add2 (segments, seg, 1);
3381         memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
3382         this_address++;
3383     }
3384     for (i = 0; i < mp->n_tags; i++) {
3385         vec_add2 (tags, tag, 1);
3386         memcpy (tag->as_u8, this_address->as_u8, sizeof (*this_address));
3387         this_address++;
3388     }
3389
3390     a->segments = segments;
3391     a->tags = tags;
3392
3393     rv = ip6_sr_add_del_tunnel (a);
3394
3395 out:
3396
3397     REPLY_MACRO(VL_API_SR_TUNNEL_ADD_DEL_REPLY);
3398 #endif
3399 }
3400
3401 #define foreach_classify_add_del_table_field    \
3402 _(table_index)                                  \
3403 _(nbuckets)                                     \
3404 _(memory_size)                                  \
3405 _(skip_n_vectors)                               \
3406 _(match_n_vectors)                              \
3407 _(next_table_index)                             \
3408 _(miss_next_index)
3409
3410 static void vl_api_classify_add_del_table_t_handler
3411 (vl_api_classify_add_del_table_t * mp)
3412 {
3413     vl_api_classify_add_del_table_reply_t * rmp;
3414     vnet_classify_main_t * cm = &vnet_classify_main;
3415     vnet_classify_table_t * t;
3416     int rv;
3417
3418 #define _(a) u32 a;
3419     foreach_classify_add_del_table_field;
3420 #undef _
3421
3422 #define _(a) a = ntohl(mp->a);    
3423     foreach_classify_add_del_table_field;
3424 #undef _
3425
3426     /* The underlying API fails silently, on purpose, so check here */
3427     if (mp->is_add == 0) 
3428         if (pool_is_free_index (cm->tables, table_index)) {
3429             rv = VNET_API_ERROR_NO_SUCH_TABLE;
3430             goto out;
3431         }
3432
3433     rv = vnet_classify_add_del_table 
3434         (cm, mp->mask, nbuckets, memory_size, 
3435          skip_n_vectors, match_n_vectors, 
3436          next_table_index, miss_next_index,
3437          &table_index, mp->is_add);
3438     
3439 out:
3440     REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
3441     ({
3442         if (rv == 0 && mp->is_add) {
3443             t = pool_elt_at_index (cm->tables, table_index);
3444             rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
3445             rmp->match_n_vectors = ntohl(t->match_n_vectors);
3446             rmp->new_table_index = ntohl(table_index);
3447         } else {
3448             rmp->skip_n_vectors = ~0;
3449             rmp->match_n_vectors = ~0;
3450             rmp->new_table_index = ~0;
3451         }
3452     }));
3453 }
3454
3455 static void vl_api_classify_add_del_session_t_handler
3456 (vl_api_classify_add_del_session_t * mp)
3457 {
3458     vnet_classify_main_t * cm = &vnet_classify_main;
3459     vl_api_classify_add_del_session_reply_t * rmp;
3460     int rv;
3461     u32 table_index, hit_next_index, opaque_index;
3462     i32 advance;
3463
3464     table_index = ntohl (mp->table_index);
3465     hit_next_index = ntohl (mp->hit_next_index);
3466     opaque_index = ntohl (mp->opaque_index);
3467     advance = ntohl (mp->advance);
3468     
3469     rv = vnet_classify_add_del_session 
3470         (cm, table_index, mp->match, hit_next_index, opaque_index,
3471          advance, mp->is_add);
3472
3473     REPLY_MACRO(VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
3474 }
3475
3476 static void vl_api_classify_set_interface_ip_table_t_handler 
3477 (vl_api_classify_set_interface_ip_table_t * mp)
3478 {
3479     vlib_main_t * vm = vlib_get_main();
3480     vl_api_classify_set_interface_ip_table_reply_t * rmp;
3481     int rv;
3482     u32 table_index, sw_if_index;
3483
3484     table_index = ntohl (mp->table_index);
3485     sw_if_index = ntohl (mp->sw_if_index);
3486     
3487     VALIDATE_SW_IF_INDEX(mp);
3488
3489     if (mp->is_ipv6)
3490         rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3491     else
3492         rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
3493
3494     BAD_SW_IF_INDEX_LABEL;
3495
3496     REPLY_MACRO(VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
3497 }
3498
3499 static void vl_api_classify_set_interface_l2_tables_t_handler 
3500 (vl_api_classify_set_interface_l2_tables_t * mp)
3501 {
3502     vl_api_classify_set_interface_l2_tables_reply_t * rmp;
3503     int rv;
3504     u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
3505     int enable;
3506
3507     ip4_table_index = ntohl(mp->ip4_table_index);
3508     ip6_table_index = ntohl(mp->ip6_table_index);
3509     other_table_index = ntohl(mp->other_table_index);
3510     sw_if_index = ntohl(mp->sw_if_index);
3511
3512     VALIDATE_SW_IF_INDEX(mp);
3513
3514     rv = vnet_l2_classify_set_tables (sw_if_index, ip4_table_index, 
3515                                       ip6_table_index, other_table_index);
3516
3517     if (rv == 0) {
3518         if (ip4_table_index != ~0 || ip6_table_index != ~0 
3519             || other_table_index != ~0)
3520             enable = 1;
3521         else
3522             enable = 0;
3523             
3524         vnet_l2_classify_enable_disable (sw_if_index, enable);
3525     }
3526
3527     BAD_SW_IF_INDEX_LABEL;
3528
3529     REPLY_MACRO(VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
3530 }
3531
3532 static void 
3533 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t *mp)
3534 {
3535     int rv = 0;
3536     vl_api_l2_fib_clear_table_reply_t * rmp;
3537
3538     /* DAW-FIXME: This API should only clear non-static l2fib entries, but
3539      *            that is not currently implemented.  When that TODO is fixed
3540      *            this call should be changed to pass 1 instead of 0.
3541      */
3542     l2fib_clear_table (0);
3543
3544     REPLY_MACRO(VL_API_L2_FIB_CLEAR_TABLE_REPLY);
3545 }
3546
3547 extern void l2_efp_filter_configure(vnet_main_t * vnet_main,
3548                                     u32           sw_if_index,
3549                                     u32           enable);
3550
3551 static void 
3552 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *mp)
3553 {
3554     int rv;
3555     vl_api_l2_interface_efp_filter_reply_t * rmp;
3556     vnet_main_t *vnm = vnet_get_main();
3557
3558     // enable/disable the feature
3559     l2_efp_filter_configure (vnm, mp->sw_if_index, mp->enable_disable);
3560     rv = vnm->api_errno;
3561
3562     REPLY_MACRO(VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
3563 }
3564
3565 static void 
3566 vl_api_l2_interface_vlan_tag_rewrite_t_handler (vl_api_l2_interface_vlan_tag_rewrite_t *mp)
3567 {
3568     int rv = 0;
3569     vl_api_l2_interface_vlan_tag_rewrite_reply_t * rmp;
3570     vnet_main_t * vnm = vnet_get_main();
3571     vlib_main_t * vm = vlib_get_main();
3572     u32 vtr_op;
3573
3574     VALIDATE_SW_IF_INDEX(mp);
3575
3576     vtr_op = ntohl(mp->vtr_op);
3577
3578     /* The L2 code is unsuspicious */
3579     switch(vtr_op) {
3580     case L2_VTR_DISABLED:
3581     case L2_VTR_PUSH_1:
3582     case L2_VTR_PUSH_2:
3583     case L2_VTR_POP_1:
3584     case L2_VTR_POP_2:
3585     case L2_VTR_TRANSLATE_1_1:
3586     case L2_VTR_TRANSLATE_1_2:
3587     case L2_VTR_TRANSLATE_2_1:
3588     case L2_VTR_TRANSLATE_2_2:
3589         break;
3590
3591     default:
3592         rv = VNET_API_ERROR_INVALID_VALUE;
3593         goto bad_sw_if_index;
3594     }
3595
3596     rv = l2vtr_configure (vm, vnm, ntohl(mp->sw_if_index), vtr_op, 
3597                           ntohl(mp->push_dot1q), ntohl(mp->tag1), 
3598                           ntohl(mp->tag2));
3599     
3600     BAD_SW_IF_INDEX_LABEL;
3601
3602     REPLY_MACRO(VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
3603 }
3604
3605 static void
3606 vl_api_create_vhost_user_if_t_handler (vl_api_create_vhost_user_if_t *mp)
3607 {
3608 #if DPDK > 0
3609     int rv = 0;
3610     vl_api_create_vhost_user_if_reply_t * rmp;
3611     u32 sw_if_index = (u32)~0;
3612
3613     vnet_main_t * vnm = vnet_get_main();
3614     vlib_main_t * vm = vlib_get_main();
3615
3616     rv = dpdk_vhost_user_create_if(vnm, vm, (char *)mp->sock_filename,
3617                               mp->is_server, &sw_if_index, (u64)~0,
3618                               mp->renumber, ntohl(mp->custom_dev_instance),
3619                               (mp->use_custom_mac)?mp->mac_address:NULL);
3620
3621     REPLY_MACRO2(VL_API_CREATE_VHOST_USER_IF_REPLY,
3622     ({
3623       rmp->sw_if_index = ntohl (sw_if_index);
3624     }));
3625 #endif
3626 }
3627
3628 static void
3629 vl_api_modify_vhost_user_if_t_handler (vl_api_modify_vhost_user_if_t *mp)
3630 {
3631 #if DPDK > 0
3632     int rv = 0;
3633     vl_api_modify_vhost_user_if_reply_t * rmp;
3634     u32 sw_if_index = ntohl(mp->sw_if_index);
3635
3636     vnet_main_t * vnm = vnet_get_main();
3637     vlib_main_t * vm = vlib_get_main();
3638
3639     rv = dpdk_vhost_user_modify_if(vnm, vm, (char *)mp->sock_filename,
3640                               mp->is_server, sw_if_index, (u64)~0,
3641                               mp->renumber, ntohl(mp->custom_dev_instance));
3642
3643     REPLY_MACRO(VL_API_MODIFY_VHOST_USER_IF_REPLY);
3644 #endif
3645 }
3646
3647 static void
3648 vl_api_delete_vhost_user_if_t_handler (vl_api_delete_vhost_user_if_t *mp)
3649 {
3650 #if DPDK > 0
3651     int rv = 0;
3652     vpe_api_main_t * vam = &vpe_api_main;
3653     vl_api_delete_vhost_user_if_reply_t * rmp;
3654     u32 sw_if_index = ntohl(mp->sw_if_index);
3655
3656     vnet_main_t * vnm = vnet_get_main();
3657     vlib_main_t * vm = vlib_get_main();
3658
3659     rv = dpdk_vhost_user_delete_if(vnm, vm, sw_if_index);
3660
3661     REPLY_MACRO(VL_API_DELETE_VHOST_USER_IF_REPLY);
3662     if (!rv) {
3663         unix_shared_memory_queue_t * q =
3664             vl_api_client_index_to_input_queue (mp->client_index);
3665         if (!q)
3666             return;
3667
3668         send_sw_interface_flags_deleted (vam, q, sw_if_index);
3669     }
3670 #endif
3671 }
3672
3673 static void vl_api_sw_interface_vhost_user_details_t_handler (
3674     vl_api_sw_interface_vhost_user_details_t * mp)
3675 {
3676     clib_warning ("BUG");
3677 }
3678
3679 #if DPDK > 0
3680 static void send_sw_interface_vhost_user_details (vpe_api_main_t * am,
3681                                        unix_shared_memory_queue_t *q,
3682                                        vhost_user_intf_details_t * vui)
3683 {
3684     vl_api_sw_interface_vhost_user_details_t * mp;
3685
3686     mp = vl_msg_api_alloc (sizeof (*mp));
3687     memset (mp, 0, sizeof (*mp));
3688     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_VHOST_USER_DETAILS);
3689     mp->sw_if_index = ntohl(vui->sw_if_index);
3690     mp->virtio_net_hdr_sz = ntohl (vui->virtio_net_hdr_sz);
3691     mp->features = clib_net_to_host_u64 (vui->features);
3692     mp->is_server = vui->is_server;
3693     mp->num_regions = ntohl(vui->num_regions);
3694     mp->sock_errno = ntohl(vui->sock_errno);
3695
3696     strncpy ((char *) mp->sock_filename,
3697              (char *) vui->sock_filename, ARRAY_LEN(mp->sock_filename)-1);
3698     strncpy ((char *) mp->interface_name,
3699              (char *) vui->if_name, ARRAY_LEN(mp->interface_name)-1);
3700
3701     vl_msg_api_send_shmem (q, (u8 *)&mp);
3702 }
3703 #endif
3704
3705 static void
3706 vl_api_sw_interface_vhost_user_dump_t_handler (
3707         vl_api_sw_interface_vhost_user_dump_t *mp)
3708 {
3709 #if DPDK > 0
3710     int rv = 0;
3711     vpe_api_main_t * am = &vpe_api_main;
3712     vnet_main_t * vnm = vnet_get_main();
3713     vlib_main_t * vm = vlib_get_main();
3714     vhost_user_intf_details_t *ifaces = NULL;
3715     vhost_user_intf_details_t *vuid = NULL;
3716     unix_shared_memory_queue_t * q;
3717
3718     q = vl_api_client_index_to_input_queue (mp->client_index);
3719     if (q == 0)
3720         return;
3721
3722     rv = dpdk_vhost_user_dump_ifs(vnm, vm, &ifaces);
3723     if (rv)
3724         return;
3725
3726     vec_foreach (vuid, ifaces) {
3727         send_sw_interface_vhost_user_details (am, q, vuid);
3728     }
3729     vec_free(ifaces);
3730 #endif
3731 }
3732
3733 static void send_sw_if_l2tpv3_tunnel_details (vpe_api_main_t * am,
3734                                        unix_shared_memory_queue_t *q,
3735                                        l2t_session_t *s,
3736                                        l2t_main_t * lm)
3737 {
3738     vl_api_sw_if_l2tpv3_tunnel_details_t * mp;
3739     u8 * if_name = NULL;
3740     vnet_sw_interface_t * si = NULL;
3741
3742     si = vnet_get_hw_sw_interface (lm->vnet_main, s->hw_if_index);
3743
3744     if_name = format(if_name, "%U",
3745                      format_vnet_sw_interface_name, lm->vnet_main, si);
3746
3747     mp = vl_msg_api_alloc (sizeof (*mp));
3748     memset (mp, 0, sizeof (*mp));
3749     mp->_vl_msg_id = ntohs(VL_API_SW_IF_L2TPV3_TUNNEL_DETAILS);
3750     strncpy((char *)mp->interface_name,
3751             (char *)if_name, ARRAY_LEN(mp->interface_name)-1);
3752     mp->sw_if_index = ntohl(si->sw_if_index);
3753     mp->local_session_id = s->local_session_id;
3754     mp->remote_session_id = s->remote_session_id;
3755     mp->local_cookie[0] = s->local_cookie[0];
3756     mp->local_cookie[1] = s->local_cookie[1];
3757     mp->remote_cookie = s->remote_cookie;
3758     memcpy(mp->client_address, &s->client_address, sizeof(s->client_address));
3759     memcpy(mp->our_address, &s->our_address, sizeof(s->our_address));
3760     mp->l2_sublayer_present = s->l2_sublayer_present;
3761
3762     vl_msg_api_send_shmem (q, (u8 *)&mp);
3763 }
3764
3765 static void send_ip_address_details (vpe_api_main_t * am,
3766                                      unix_shared_memory_queue_t * q,
3767                                      u8 * ip,
3768                                      u16 prefix_length,
3769                                      u8 is_ipv6)
3770 {
3771     vl_api_ip_address_details_t * mp;
3772
3773     mp = vl_msg_api_alloc (sizeof (*mp));
3774     memset (mp, 0, sizeof (*mp));
3775     mp->_vl_msg_id = ntohs(VL_API_IP_ADDRESS_DETAILS);
3776
3777     if (is_ipv6) {
3778         memcpy(&mp->ip, ip, sizeof(mp->ip));
3779     } else {
3780         u32 * tp = (u32 *)mp->ip;
3781         *tp = ntohl(*(u32*)ip);
3782     }
3783     mp->prefix_length = prefix_length;
3784
3785     vl_msg_api_send_shmem (q, (u8 *)&mp);
3786 }
3787
3788 static void
3789 vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t *mp)
3790 {
3791     vpe_api_main_t * am = &vpe_api_main;
3792     unix_shared_memory_queue_t * q;
3793     ip6_address_t * r6;
3794     ip4_address_t * r4;
3795     ip6_main_t * im6 = &ip6_main;
3796     ip4_main_t * im4 = &ip4_main;
3797     ip_lookup_main_t * lm6 = &im6->lookup_main;
3798     ip_lookup_main_t * lm4 = &im4->lookup_main;
3799     ip_interface_address_t * ia = 0;
3800     u32 sw_if_index = ~0;
3801
3802     sw_if_index = ntohl(mp->sw_if_index);
3803
3804     q = vl_api_client_index_to_input_queue (mp->client_index);
3805     if (q == 0) {
3806         return;
3807     }
3808
3809     if (mp->is_ipv6) {
3810         foreach_ip_interface_address (lm6, ia, sw_if_index,
3811                                       1 /* honor unnumbered */,
3812         ({
3813             r6 = ip_interface_address_get_address (lm6, ia);
3814             u16 prefix_length = ia->address_length;
3815             send_ip_address_details(am, q, (u8*)r6, prefix_length, 1);
3816         }));
3817     } else {
3818         foreach_ip_interface_address (lm4, ia, sw_if_index,
3819                                       1 /* honor unnumbered */,
3820         ({
3821             r4 = ip_interface_address_get_address (lm4, ia);
3822             u16 prefix_length = ia->address_length;
3823             send_ip_address_details(am, q, (u8*)r4, prefix_length, 0);
3824         }));
3825     }
3826 }
3827
3828 static void send_ip_details (vpe_api_main_t * am,
3829                                unix_shared_memory_queue_t *q,
3830                                u32 sw_if_index)
3831 {
3832     vl_api_ip_details_t * mp;
3833
3834     mp = vl_msg_api_alloc (sizeof (*mp));
3835     memset (mp, 0, sizeof (*mp));
3836     mp->_vl_msg_id = ntohs(VL_API_IP_DETAILS);
3837
3838     mp->sw_if_index = ntohl(sw_if_index);
3839
3840     vl_msg_api_send_shmem (q, (u8 *)&mp);
3841 }
3842
3843 static void
3844 vl_api_sw_if_l2tpv3_tunnel_dump_t_handler (
3845         vl_api_sw_if_l2tpv3_tunnel_dump_t *mp)
3846 {
3847     vpe_api_main_t * am = &vpe_api_main;
3848     l2t_main_t * lm = &l2t_main;
3849     unix_shared_memory_queue_t * q;
3850     l2t_session_t *session;
3851
3852     q = vl_api_client_index_to_input_queue (mp->client_index);
3853     if (q == 0)
3854         return;
3855
3856     pool_foreach (session, lm->sessions, 
3857     ({
3858         send_sw_if_l2tpv3_tunnel_details (am, q, session, lm);
3859     }));
3860 }
3861
3862
3863 static void send_sw_interface_tap_details (vpe_api_main_t * am,
3864                                        unix_shared_memory_queue_t *q,
3865                                        tapcli_interface_details_t *tap_if)
3866 {
3867     vl_api_sw_interface_tap_details_t * mp;
3868     mp = vl_msg_api_alloc (sizeof (*mp));
3869     memset (mp, 0, sizeof (*mp));
3870     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_TAP_DETAILS);
3871     mp->sw_if_index = ntohl(tap_if->sw_if_index);
3872     strncpy((char *)mp->dev_name,
3873             (char *)tap_if->dev_name, ARRAY_LEN(mp->dev_name)-1);
3874
3875     vl_msg_api_send_shmem (q, (u8 *)&mp);
3876 }
3877
3878 static void
3879 vl_api_sw_interface_tap_dump_t_handler (
3880         vl_api_sw_interface_tap_dump_t *mp)
3881 {
3882     int rv = 0;
3883     vpe_api_main_t * am = &vpe_api_main;
3884     unix_shared_memory_queue_t * q;
3885     tapcli_interface_details_t *tapifs = NULL;
3886     tapcli_interface_details_t *tap_if = NULL;
3887
3888     q = vl_api_client_index_to_input_queue (mp->client_index);
3889     if (q == 0)
3890         return;
3891
3892     rv = vnet_tap_dump_ifs(&tapifs);
3893     if (rv)
3894         return;
3895
3896     vec_foreach(tap_if, tapifs) {
3897         send_sw_interface_tap_details(am, q, tap_if);
3898     }
3899
3900     vec_free(tapifs);
3901 }
3902
3903 static void
3904 vl_api_ip_dump_t_handler (vl_api_ip_dump_t *mp)
3905 {
3906     vpe_api_main_t * am = &vpe_api_main;
3907     vnet_main_t * vnm = vnet_get_main();
3908     vlib_main_t * vm = vlib_get_main();
3909     vnet_interface_main_t * im = &vnm->interface_main;
3910     unix_shared_memory_queue_t * q;
3911     vnet_sw_interface_t * si, * sorted_sis;
3912     u32 sw_if_index = ~0;
3913
3914     q = vl_api_client_index_to_input_queue (mp->client_index);
3915     if (q == 0) {
3916         return;
3917     }
3918
3919     /* Gather interfaces. */
3920     sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
3921     _vec_len (sorted_sis) = 0;
3922     pool_foreach (si, im->sw_interfaces, ({ vec_add1 (sorted_sis, si[0]); }));
3923
3924     vec_foreach (si, sorted_sis) {
3925         if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)) {
3926             if (mp->is_ipv6 && !ip6_interface_enabled(vm, si->sw_if_index)) {
3927                 continue;
3928             }
3929             sw_if_index = si->sw_if_index;
3930             send_ip_details(am, q, sw_if_index);
3931         }
3932     }
3933 }
3934
3935 static void vl_api_l2_fib_table_entry_t_handler (
3936     vl_api_l2_fib_table_entry_t * mp)
3937 {
3938     clib_warning ("BUG");
3939 }
3940
3941 static void send_l2fib_table_entry (vpe_api_main_t * am,
3942                                     unix_shared_memory_queue_t *q,
3943                                     l2fib_entry_key_t * l2fe_key,
3944                                     l2fib_entry_result_t * l2fe_res)
3945 {
3946     vl_api_l2_fib_table_entry_t * mp;
3947
3948     mp = vl_msg_api_alloc (sizeof (*mp));
3949     memset (mp, 0, sizeof (*mp));
3950     mp->_vl_msg_id = ntohs(VL_API_L2_FIB_TABLE_ENTRY);
3951
3952     mp->bd_id = ntohl(l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
3953
3954     mp->mac = l2fib_make_key (l2fe_key->fields.mac, 0);
3955     mp->sw_if_index = ntohl(l2fe_res->fields.sw_if_index);
3956     mp->static_mac = l2fe_res->fields.static_mac;
3957     mp->filter_mac = l2fe_res->fields.filter;
3958     mp->bvi_mac = l2fe_res->fields.bvi;
3959
3960     vl_msg_api_send_shmem (q, (u8 *)&mp);
3961 }
3962
3963 static void
3964 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t *mp)
3965 {
3966     vpe_api_main_t * am = &vpe_api_main;
3967     bd_main_t * bdm = &bd_main;
3968     l2fib_entry_key_t *l2fe_key = NULL;
3969     l2fib_entry_result_t *l2fe_res = NULL;
3970     u32 ni, bd_id = ntohl (mp->bd_id);
3971     u32 bd_index;
3972     unix_shared_memory_queue_t * q;
3973     uword * p;
3974
3975     q = vl_api_client_index_to_input_queue (mp->client_index);
3976     if (q == 0)
3977         return;
3978
3979     /* see l2fib_table_dump: ~0 means "any" */
3980     if (bd_id == ~0)
3981         bd_index = ~0;
3982     else {
3983         p = hash_get (bdm->bd_index_by_bd_id, bd_id);
3984         if (p == 0)
3985             return;
3986         
3987         bd_index = p[0];
3988     }
3989
3990     l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
3991
3992     vec_foreach_index (ni, l2fe_key) {
3993         send_l2fib_table_entry (am, q, vec_elt_at_index(l2fe_key, ni),
3994                                 vec_elt_at_index(l2fe_res, ni));
3995     }
3996     vec_free(l2fe_key);
3997     vec_free(l2fe_res);
3998 }
3999
4000 static void
4001 vl_api_show_version_t_handler (vl_api_show_version_t *mp)
4002 {
4003     vl_api_show_version_reply_t *rmp;
4004     int rv = 0;
4005     char * vpe_api_get_build_directory(void);
4006     char * vpe_api_get_version(void);
4007     char * vpe_api_get_build_date(void);
4008
4009     unix_shared_memory_queue_t * q =
4010         vl_api_client_index_to_input_queue (mp->client_index);
4011     
4012     if (!q)
4013         return;
4014     
4015     REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
4016     ({
4017         strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
4018         strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
4019                  ARRAY_LEN(rmp->build_directory)-1);
4020         strncpy ((char *) rmp->version, vpe_api_get_version(),
4021                  ARRAY_LEN(rmp->version)-1);
4022         strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
4023                  ARRAY_LEN(rmp->build_date)-1);
4024     }));
4025 }
4026
4027 static void vl_api_get_node_index_t_handler
4028 (vl_api_get_node_index_t * mp)
4029 {
4030     vlib_main_t * vm = vlib_get_main();
4031     vl_api_get_node_index_reply_t * rmp;
4032     vlib_node_t * n;
4033     int rv = 0;
4034     u32 node_index = ~0;
4035
4036     n = vlib_get_node_by_name (vm, mp->node_name);
4037
4038     if (n == 0)
4039         rv = VNET_API_ERROR_NO_SUCH_NODE;
4040     else
4041         node_index = n->index;
4042
4043     REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY, 
4044     ({
4045         rmp->node_index = ntohl(node_index);
4046     }))
4047 }
4048
4049 static void vl_api_add_node_next_t_handler
4050 (vl_api_add_node_next_t * mp)
4051 {
4052     vlib_main_t * vm = vlib_get_main();
4053     vl_api_add_node_next_reply_t * rmp;
4054     vlib_node_t * n, * next;
4055     int rv = 0;
4056     u32 next_index = ~0;
4057
4058     n = vlib_get_node_by_name (vm, mp->node_name);
4059
4060     if (n == 0) {
4061         rv = VNET_API_ERROR_NO_SUCH_NODE;
4062         goto out;
4063     }
4064
4065     next = vlib_get_node_by_name (vm, mp->next_name);
4066
4067     if (next == 0) 
4068         rv = VNET_API_ERROR_NO_SUCH_NODE2;
4069     else 
4070         next_index = vlib_node_add_next (vm, n->index, next->index);
4071
4072 out:
4073     REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY, 
4074     ({
4075         rmp->next_index = ntohl(next_index);
4076     }))
4077 }
4078
4079 static void vl_api_l2tpv3_create_tunnel_t_handler 
4080 (vl_api_l2tpv3_create_tunnel_t *mp)
4081 {
4082     vl_api_l2tpv3_create_tunnel_reply_t * rmp;
4083     l2t_main_t *lm = &l2t_main;
4084     u32 sw_if_index = (u32)~0;
4085     int rv;
4086     
4087     if (mp->is_ipv6 != 1) {
4088         rv = VNET_API_ERROR_UNIMPLEMENTED;
4089         goto out;
4090     }
4091
4092     rv = create_l2tpv3_ipv6_tunnel (lm,
4093                                (ip6_address_t *) mp->client_address,
4094                                (ip6_address_t *) mp->our_address,
4095                                ntohl(mp->local_session_id),
4096                                ntohl(mp->remote_session_id),
4097                                clib_net_to_host_u64(mp->local_cookie),
4098                                clib_net_to_host_u64(mp->remote_cookie),
4099                                mp->l2_sublayer_present, 
4100                                &sw_if_index);
4101
4102 out:
4103     REPLY_MACRO2(VL_API_L2TPV3_CREATE_TUNNEL_REPLY,
4104     ({
4105         rmp->sw_if_index = ntohl (sw_if_index);
4106     }))
4107 }
4108
4109 static void vl_api_l2tpv3_set_tunnel_cookies_t_handler 
4110 (vl_api_l2tpv3_set_tunnel_cookies_t *mp)
4111 {
4112     vl_api_l2tpv3_set_tunnel_cookies_reply_t * rmp;
4113     l2t_main_t *lm = &l2t_main;
4114     int rv;
4115
4116     VALIDATE_SW_IF_INDEX(mp);
4117
4118     rv = l2tpv3_set_tunnel_cookies (lm, ntohl(mp->sw_if_index),
4119                                   clib_net_to_host_u64(mp->new_local_cookie),
4120                                   clib_net_to_host_u64(mp->new_remote_cookie));
4121
4122     BAD_SW_IF_INDEX_LABEL;
4123     
4124     REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY);
4125 }
4126
4127 static void vl_api_l2tpv3_interface_enable_disable_t_handler 
4128 (vl_api_l2tpv3_interface_enable_disable_t * mp)
4129 {
4130     int rv;
4131     vnet_main_t * vnm = vnet_get_main();
4132     vl_api_l2tpv3_interface_enable_disable_reply_t * rmp;
4133
4134     VALIDATE_SW_IF_INDEX(mp);
4135
4136     rv = l2tpv3_interface_enable_disable 
4137         (vnm, ntohl(mp->sw_if_index), mp->enable_disable);
4138
4139     BAD_SW_IF_INDEX_LABEL;
4140
4141     REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY);
4142 }
4143
4144 static void vl_api_l2tpv3_set_lookup_key_t_handler 
4145 (vl_api_l2tpv3_set_lookup_key_t * mp)
4146 {
4147     int rv = 0;
4148     l2t_main_t *lm = &l2t_main;
4149     vl_api_l2tpv3_set_lookup_key_reply_t * rmp;
4150
4151     if (mp->key > L2T_LOOKUP_SESSION_ID) {
4152         rv = VNET_API_ERROR_INVALID_VALUE;
4153         goto out;
4154     }
4155     
4156     lm->lookup_type = mp->key;
4157
4158 out:
4159     REPLY_MACRO (VL_API_L2TPV3_SET_LOOKUP_KEY_REPLY);
4160 }
4161
4162 static void vl_api_vxlan_add_del_tunnel_t_handler 
4163 (vl_api_vxlan_add_del_tunnel_t * mp)
4164 {
4165     vl_api_vxlan_add_del_tunnel_reply_t * rmp;
4166     int rv = 0;
4167     vnet_vxlan_add_del_tunnel_args_t _a, *a = &_a;
4168     u32 encap_fib_index;
4169     uword * p;
4170     ip4_main_t * im = &ip4_main;
4171     u32 sw_if_index = ~0;
4172
4173     p = hash_get (im->fib_index_by_table_id, ntohl(mp->encap_vrf_id));
4174     if (! p) {
4175         rv = VNET_API_ERROR_NO_SUCH_FIB;
4176         goto out;
4177     }
4178     encap_fib_index = p[0];
4179
4180     memset (a, 0, sizeof (*a));
4181
4182     a->is_add = mp->is_add;
4183
4184     /* ip addresses sent in network byte order */
4185     a->src.as_u32 = mp->src_address;
4186     a->dst.as_u32 = mp->dst_address;
4187
4188     a->encap_fib_index = encap_fib_index;
4189     a->decap_next_index = ntohl(mp->decap_next_index);
4190     a->vni = ntohl(mp->vni);
4191     rv = vnet_vxlan_add_del_tunnel (a, &sw_if_index);
4192     
4193 out:
4194     REPLY_MACRO2(VL_API_VXLAN_ADD_DEL_TUNNEL_REPLY,
4195     ({
4196         rmp->sw_if_index = ntohl (sw_if_index);
4197     }));
4198 }
4199
4200 static void send_vxlan_tunnel_details
4201 (vxlan_tunnel_t * t, unix_shared_memory_queue_t * q)
4202 {
4203     vl_api_vxlan_tunnel_details_t * rmp;
4204     ip4_main_t * im = &ip4_main;
4205
4206     rmp = vl_msg_api_alloc (sizeof (*rmp));
4207     memset (rmp, 0, sizeof (*rmp));
4208     rmp->_vl_msg_id = ntohs(VL_API_VXLAN_TUNNEL_DETAILS);
4209     rmp->src_address = t->src.data_u32;
4210     rmp->dst_address = t->dst.data_u32;
4211     rmp->encap_vrf_id = htonl(im->fibs[t->encap_fib_index].table_id);
4212     rmp->vni = htonl(t->vni);
4213     rmp->decap_next_index = htonl(t->decap_next_index);
4214     rmp->sw_if_index = htonl(t->sw_if_index);
4215
4216     vl_msg_api_send_shmem (q, (u8 *)&rmp);
4217 }
4218
4219 static void vl_api_vxlan_tunnel_dump_t_handler
4220 (vl_api_vxlan_tunnel_dump_t * mp)
4221 {
4222     unix_shared_memory_queue_t * q;
4223     vxlan_main_t * vxm = &vxlan_main;
4224     vxlan_tunnel_t * t;
4225     u32 sw_if_index;
4226
4227     q = vl_api_client_index_to_input_queue (mp->client_index);
4228     if (q == 0) {
4229         return;
4230     }
4231
4232     sw_if_index = ntohl(mp->sw_if_index);
4233
4234     if (~0 == sw_if_index) {
4235         pool_foreach (t, vxm->tunnels,
4236         ({
4237             send_vxlan_tunnel_details(t, q);
4238         }));
4239     } else {
4240         if ((sw_if_index >= vec_len(vxm->tunnel_index_by_sw_if_index)) ||
4241                 (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index])) {
4242             return;
4243         }
4244         t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
4245         send_vxlan_tunnel_details(t, q);
4246     }
4247 }
4248
4249 static void 
4250 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t *mp)
4251 {
4252     extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
4253                                       int is_add);
4254     vl_api_l2_patch_add_del_reply_t * rmp;
4255     int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index, 
4256                                int is_add);
4257     int rv = 0;
4258
4259     VALIDATE_RX_SW_IF_INDEX(mp);
4260     VALIDATE_TX_SW_IF_INDEX(mp);
4261
4262     rv = vnet_l2_patch_add_del (ntohl(mp->rx_sw_if_index), 
4263                                 ntohl(mp->tx_sw_if_index), 
4264                                 (int)(mp->is_add != 0));
4265     
4266     BAD_RX_SW_IF_INDEX_LABEL;
4267     BAD_TX_SW_IF_INDEX_LABEL;
4268
4269     REPLY_MACRO(VL_API_L2_PATCH_ADD_DEL_REPLY);
4270 }
4271
4272 static void
4273 vl_api_nsh_gre_add_del_tunnel_t_handler 
4274 (vl_api_nsh_gre_add_del_tunnel_t * mp)
4275 {
4276     vl_api_nsh_gre_add_del_tunnel_reply_t * rmp;
4277     int rv = 0;
4278     vnet_nsh_gre_add_del_tunnel_args_t _a, *a = &_a;
4279     u32 encap_fib_index, decap_fib_index;
4280     u32 decap_next_index;
4281     uword * p;
4282     ip4_main_t * im = &ip4_main;
4283     u32 * tlvs = 0;
4284     u32 sw_if_index = ~0;
4285     int i;
4286
4287     p = hash_get (im->fib_index_by_table_id, ntohl(mp->encap_vrf_id));
4288     if (! p) {
4289         rv = VNET_API_ERROR_NO_SUCH_FIB;
4290         goto out;
4291     }
4292     encap_fib_index = p[0];
4293
4294     decap_next_index = ntohl(mp->decap_next_index);
4295
4296     /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
4297     if (decap_next_index == NSH_INPUT_NEXT_IP4_INPUT) {
4298         p = hash_get (im->fib_index_by_table_id, ntohl(mp->decap_vrf_id));
4299         if (! p) {
4300             rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
4301             goto out;
4302         }
4303         decap_fib_index = p[0];
4304     } else {
4305         decap_fib_index = ntohl(mp->decap_vrf_id);
4306     }
4307
4308     memset (a, 0, sizeof (*a));
4309
4310     a->is_add = mp->is_add;
4311     /* ip addresses sent in network byte order */
4312     a->src.as_u32 = ntohl(mp->src);
4313     a->dst.as_u32 = ntohl(mp->dst);
4314     a->encap_fib_index = encap_fib_index;
4315     a->decap_fib_index = decap_fib_index;
4316     a->decap_next_index = decap_next_index;
4317     a->ver_o_c = mp->ver_o_c;
4318     a->length = mp->length;
4319     a->md_type = mp->md_type;
4320     a->next_protocol = mp->next_protocol;
4321     a->spi_si = ntohl(mp->spi_si);
4322     a->c1 = ntohl(mp->c1);
4323     a->c2 = ntohl(mp->c2);
4324     a->c3 = ntohl(mp->c3);
4325     a->c4 = ntohl(mp->c4);
4326
4327     for (i = 0; i < mp->tlv_len_in_words; i++)
4328         vec_add1 (tlvs, ntohl(mp->tlvs[i]));
4329
4330     a->tlvs = tlvs;
4331
4332     rv = vnet_nsh_gre_add_del_tunnel (a, &sw_if_index);
4333     
4334 out:
4335     REPLY_MACRO2(VL_API_NSH_GRE_ADD_DEL_TUNNEL_REPLY,
4336     ({
4337         rmp->sw_if_index = ntohl (sw_if_index);
4338     }));
4339 }
4340
4341 static void
4342 vl_api_nsh_vxlan_gpe_add_del_tunnel_t_handler 
4343 (vl_api_nsh_vxlan_gpe_add_del_tunnel_t * mp)
4344 {
4345     vl_api_nsh_vxlan_gpe_add_del_tunnel_reply_t * rmp;
4346     int rv = 0;
4347     vnet_nsh_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
4348     u32 encap_fib_index, decap_fib_index;
4349     u32 decap_next_index;
4350     uword * p;
4351     ip4_main_t * im = &ip4_main;
4352     u32 * tlvs = 0;
4353     u32 sw_if_index = ~0;
4354     int i;
4355
4356     p = hash_get (im->fib_index_by_table_id, ntohl(mp->encap_vrf_id));
4357     if (! p) {
4358         rv = VNET_API_ERROR_NO_SUCH_FIB;
4359         goto out;
4360     }
4361     encap_fib_index = p[0];
4362
4363     decap_next_index = ntohl(mp->decap_next_index);
4364
4365     /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
4366     if (decap_next_index == NSH_INPUT_NEXT_IP4_INPUT) {
4367         p = hash_get (im->fib_index_by_table_id, ntohl(mp->decap_vrf_id));
4368         if (! p) {
4369             rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
4370             goto out;
4371         }
4372         decap_fib_index = p[0];
4373     } else {
4374         decap_fib_index = ntohl(mp->decap_vrf_id);
4375     }
4376
4377     memset (a, 0, sizeof (*a));
4378
4379     a->is_add = mp->is_add;
4380     /* ip addresses sent in network byte order */
4381     a->src.as_u32 = ntohl(mp->src);
4382     a->dst.as_u32 = ntohl(mp->dst);
4383     a->encap_fib_index = encap_fib_index;
4384     a->decap_fib_index = decap_fib_index;
4385     a->decap_next_index = decap_next_index;
4386     a->vni = ntohl(mp->vni);
4387     a->ver_o_c = mp->ver_o_c;
4388     a->length = mp->length;
4389     a->md_type = mp->md_type;
4390     a->next_protocol = mp->next_protocol;
4391     a->spi_si = ntohl(mp->spi_si);
4392     a->c1 = ntohl(mp->c1);
4393     a->c2 = ntohl(mp->c2);
4394     a->c3 = ntohl(mp->c3);
4395     a->c4 = ntohl(mp->c4);
4396
4397     for (i = 0; i < mp->tlv_len_in_words; i++)
4398         vec_add1 (tlvs, ntohl(mp->tlvs[i]));
4399
4400     a->tlvs = tlvs;
4401
4402     rv = vnet_nsh_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
4403     
4404 out:
4405     REPLY_MACRO2(VL_API_NSH_VXLAN_GPE_ADD_DEL_TUNNEL_REPLY,
4406     ({
4407         rmp->sw_if_index = ntohl (sw_if_index);
4408     }));
4409 }
4410
4411 static void
4412 vl_api_lisp_gpe_add_del_tunnel_t_handler 
4413 (vl_api_lisp_gpe_add_del_tunnel_t * mp)
4414 {
4415     vl_api_lisp_gpe_add_del_tunnel_reply_t * rmp;
4416     int rv = 0;
4417     vnet_lisp_gpe_add_del_tunnel_args_t _a, *a = &_a;
4418     u32 encap_fib_index, decap_fib_index;
4419     u32 decap_next_index;
4420     uword * p;
4421     ip4_main_t * im = &ip4_main;
4422     u32 sw_if_index = ~0;
4423
4424     p = hash_get (im->fib_index_by_table_id, ntohl(mp->encap_vrf_id));
4425     if (! p) {
4426         rv = VNET_API_ERROR_NO_SUCH_FIB;
4427         goto out;
4428     }
4429     encap_fib_index = p[0];
4430
4431     decap_next_index = ntohl(mp->decap_next_index);
4432
4433     /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
4434     if (decap_next_index == NSH_INPUT_NEXT_IP4_INPUT) {
4435         p = hash_get (im->fib_index_by_table_id, ntohl(mp->decap_vrf_id));
4436         if (! p) {
4437             rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
4438             goto out;
4439         }
4440         decap_fib_index = p[0];
4441     } else {
4442         decap_fib_index = ntohl(mp->decap_vrf_id);
4443     }
4444
4445     memset (a, 0, sizeof (*a));
4446
4447     a->is_add = mp->is_add;
4448     /* ip addresses sent in network byte order */
4449     a->src.as_u32 = mp->src;
4450     a->dst.as_u32 = mp->dst;
4451     a->encap_fib_index = encap_fib_index;
4452     a->decap_fib_index = decap_fib_index;
4453     a->decap_next_index = decap_next_index;
4454     a->flags = mp->flags;
4455     a->ver_res = mp->ver_res;
4456     a->res = mp->res;
4457     a->next_protocol = mp->next_protocol;
4458     a->iid = clib_net_to_host_u32 (mp->iid);
4459
4460     rv = vnet_lisp_gpe_add_del_tunnel (a, &sw_if_index);
4461     
4462 out:
4463     REPLY_MACRO2(VL_API_LISP_GPE_ADD_DEL_TUNNEL_REPLY,
4464     ({
4465         rmp->sw_if_index = ntohl (sw_if_index);
4466     }));
4467 }
4468
4469 static void 
4470 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *mp)
4471 {
4472     vl_api_interface_name_renumber_reply_t * rmp;
4473     int rv = 0;
4474
4475     VALIDATE_SW_IF_INDEX(mp);
4476
4477     rv = vnet_interface_name_renumber 
4478         (ntohl(mp->sw_if_index), ntohl(mp->new_show_dev_instance));
4479     
4480     BAD_SW_IF_INDEX_LABEL;
4481
4482     REPLY_MACRO(VL_API_INTERFACE_NAME_RENUMBER_REPLY);
4483 }
4484
4485 static int arp_change_data_callback (u32 pool_index, u8 * new_mac, 
4486                                      u32 sw_if_index, u32 address)
4487 {
4488     vpe_api_main_t * am = &vpe_api_main;
4489     vlib_main_t * vm = am->vlib_main;
4490     vl_api_ip4_arp_event_t * event;
4491     static f64 arp_event_last_time;
4492     f64 now = vlib_time_now (vm);
4493
4494     if (pool_is_free_index (am->arp_events, pool_index))
4495         return 1;
4496
4497     event = pool_elt_at_index (am->arp_events, pool_index);
4498     if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac))) {
4499         memcpy (event->new_mac, new_mac, sizeof(event->new_mac));
4500     } else { /* same mac */
4501         if ((sw_if_index == event->sw_if_index) && 
4502             ((address == 0) || 
4503              /* for BD case, also check IP address with 10 sec timeout */
4504              ((address == event->address) && 
4505               ((now - arp_event_last_time) < 10.0))))
4506             return 1;
4507     }
4508     
4509     arp_event_last_time = now;
4510     event->sw_if_index = sw_if_index;
4511     if (address) event->address = address;
4512     return 0;
4513 }
4514
4515 static int arp_change_delete_callback (u32 pool_index, u8 * notused)
4516 {
4517     vpe_api_main_t * am = &vpe_api_main;
4518     
4519     if (pool_is_free_index (am->arp_events, pool_index))
4520         return 1;
4521
4522     pool_put_index (am->arp_events, pool_index);
4523     return 0;
4524 }
4525
4526 static void
4527 vl_api_want_ip4_arp_events_t_handler 
4528 (vl_api_want_ip4_arp_events_t * mp)
4529 {
4530     vpe_api_main_t * am = &vpe_api_main;
4531     vnet_main_t * vnm = vnet_get_main();
4532     vl_api_want_ip4_arp_events_reply_t *rmp;
4533     vl_api_ip4_arp_event_t * event;
4534     int rv;
4535     
4536     if (mp->enable_disable) {
4537         pool_get (am->arp_events, event);
4538         memset (event, 0, sizeof (*event));
4539
4540         event->_vl_msg_id = ntohs(VL_API_IP4_ARP_EVENT);
4541         event->client_index = mp->client_index;
4542         event->context = mp->context;
4543         event->address = mp->address;
4544         event->pid = mp->pid;
4545
4546         rv = vnet_add_del_ip4_arp_change_event 
4547             (vnm, arp_change_data_callback,
4548              mp->pid,
4549              &mp->address /* addr, in net byte order */, 
4550              vpe_resolver_process_node.index,
4551              IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */);
4552     } else {
4553         rv = vnet_add_del_ip4_arp_change_event 
4554             (vnm, arp_change_delete_callback,
4555              mp->pid,
4556              &mp->address /* addr, in net byte order */, 
4557              vpe_resolver_process_node.index,
4558              IP4_ARP_EVENT, ~0 /* pool index */, 0 /* is_add */);
4559     }
4560     REPLY_MACRO(VL_API_WANT_IP4_ARP_EVENTS_REPLY);
4561 }
4562
4563 static void vl_api_input_acl_set_interface_t_handler 
4564 (vl_api_input_acl_set_interface_t * mp)
4565 {
4566     vlib_main_t *vm = vlib_get_main();
4567     vl_api_input_acl_set_interface_reply_t * rmp;
4568     int rv;
4569     u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
4570
4571     ip4_table_index = ntohl(mp->ip4_table_index);
4572     ip6_table_index = ntohl(mp->ip6_table_index);
4573     l2_table_index = ntohl(mp->l2_table_index);
4574     sw_if_index = ntohl(mp->sw_if_index);
4575
4576     VALIDATE_SW_IF_INDEX(mp);
4577
4578     rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index, 
4579                                    ip6_table_index, l2_table_index,
4580                                    mp->is_add);
4581
4582     BAD_SW_IF_INDEX_LABEL;
4583
4584     REPLY_MACRO(VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
4585 }
4586
4587 static void vl_api_ipsec_spd_add_del_t_handler
4588 (vl_api_ipsec_spd_add_del_t * mp)
4589 {
4590 #if IPSEC == 0
4591     clib_warning ("unimplemented");
4592 #else
4593
4594     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
4595     vl_api_ipsec_spd_add_del_reply_t * rmp;
4596     int rv;
4597
4598 #if DPDK > 0
4599     rv = ipsec_add_del_spd (vm, ntohl(mp->spd_id), mp->is_add);
4600 #else
4601     rv = VNET_API_ERROR_UNIMPLEMENTED;
4602 #endif
4603
4604     REPLY_MACRO(VL_API_IPSEC_SPD_ADD_DEL_REPLY);
4605 #endif
4606 }
4607
4608 static void vl_api_ipsec_interface_add_del_spd_t_handler
4609 (vl_api_ipsec_interface_add_del_spd_t * mp)
4610 {
4611     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
4612     vl_api_ipsec_interface_add_del_spd_reply_t * rmp;
4613     int rv;
4614     u32 sw_if_index __attribute__((unused));
4615     u32 spd_id __attribute__((unused));
4616
4617     sw_if_index = ntohl(mp->sw_if_index);
4618     spd_id = ntohl(mp->spd_id);
4619
4620     VALIDATE_SW_IF_INDEX(mp);
4621
4622 #if IPSEC > 0 
4623     rv = ipsec_set_interface_spd(vm, sw_if_index, spd_id, mp->is_add);
4624 #else
4625     rv = VNET_API_ERROR_UNIMPLEMENTED;
4626 #endif
4627
4628     BAD_SW_IF_INDEX_LABEL;
4629
4630     REPLY_MACRO(VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
4631 }
4632
4633 static void vl_api_ipsec_spd_add_del_entry_t_handler
4634 (vl_api_ipsec_spd_add_del_entry_t * mp)
4635 {
4636     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
4637     vl_api_ipsec_spd_add_del_entry_reply_t * rmp;
4638     int rv;
4639
4640 #if IPSEC > 0 
4641     ipsec_policy_t p;
4642
4643     p.id = ntohl(mp->spd_id);
4644     p.priority = ntohl(mp->priority);
4645     p.is_outbound = mp->is_outbound;
4646     p.is_ipv6 = mp->is_ipv6;
4647
4648     memcpy(&p.raddr.start, mp->remote_address_start, 16);
4649     memcpy(&p.raddr.stop, mp->remote_address_stop, 16);
4650     memcpy(&p.laddr.start, mp->local_address_start, 16);
4651     memcpy(&p.laddr.stop, mp->local_address_stop, 16);
4652
4653     p.protocol = mp->protocol;
4654     p.rport.start = ntohs(mp->remote_port_start);
4655     p.rport.stop  = ntohs(mp->remote_port_stop);
4656     p.lport.start = ntohs(mp->local_port_start);
4657     p.lport.stop  = ntohs(mp->local_port_stop);
4658     /* policy action resolve unsupported */
4659     if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE) {
4660         clib_warning("unsupported action: 'resolve'");
4661         rv = VNET_API_ERROR_UNIMPLEMENTED;
4662         goto out;
4663     }
4664     p.policy = mp->policy;
4665     p.sa_id = ntohl(mp->sa_id);
4666
4667     rv = ipsec_add_del_policy(vm, &p, mp->is_add);
4668     if (rv)
4669       goto out;
4670
4671     if (mp->is_ip_any) {
4672       p.is_ipv6 = 1;
4673       rv = ipsec_add_del_policy(vm, &p, mp->is_add);
4674     }
4675 #else
4676     rv = VNET_API_ERROR_UNIMPLEMENTED;
4677     goto out;
4678 #endif
4679
4680 out:
4681     REPLY_MACRO(VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
4682 }
4683
4684 static void vl_api_ipsec_sad_add_del_entry_t_handler
4685 (vl_api_ipsec_sad_add_del_entry_t * mp)
4686 {
4687     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
4688     vl_api_ipsec_sad_add_del_entry_reply_t * rmp;
4689     int rv;
4690 #if IPSEC > 0
4691     ipsec_sa_t sa;
4692
4693     sa.id = ntohl(mp->sad_id);
4694     sa.spi = ntohl(mp->spi);
4695     /* security protocol AH unsupported */
4696     if (mp->protocol == IPSEC_PROTOCOL_AH) {
4697         clib_warning("unsupported security protocol 'AH'");
4698         rv = VNET_API_ERROR_UNIMPLEMENTED;
4699         goto out;
4700     }
4701     sa.protocol = mp->protocol;
4702     /* check for unsupported crypto-alg */
4703     if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
4704         mp->crypto_algorithm > IPSEC_CRYPTO_ALG_AES_CBC_256) {
4705         clib_warning("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg, 
4706                      mp->crypto_algorithm);
4707         rv = VNET_API_ERROR_UNIMPLEMENTED;
4708         goto out;
4709     }
4710     sa.crypto_alg = mp->crypto_algorithm;
4711     sa.crypto_key_len = mp->crypto_key_length;
4712     memcpy(&sa.crypto_key, mp->crypto_key, sizeof(sa.crypto_key));
4713     /* check for unsupported integ-alg */
4714     if (mp->integrity_algorithm < IPSEC_INTEG_ALG_SHA1_96 ||
4715         mp->integrity_algorithm > IPSEC_INTEG_ALG_SHA_512_256) {
4716         clib_warning("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
4717                      mp->integrity_algorithm);
4718         rv = VNET_API_ERROR_UNIMPLEMENTED;
4719         goto out;
4720     }
4721     sa.integ_alg = mp->integrity_algorithm;
4722     sa.integ_key_len = mp->integrity_key_length;
4723     memcpy(&sa.integ_key, mp->integrity_key, sizeof(sa.integ_key));
4724     sa.use_esn = mp->use_extended_sequence_number;
4725     sa.is_tunnel = mp->is_tunnel;
4726     sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
4727     memcpy(&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
4728     memcpy(&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
4729
4730     rv = ipsec_add_del_sa(vm, &sa, mp->is_add);
4731 #else
4732     rv = VNET_API_ERROR_UNIMPLEMENTED;
4733     goto out;
4734 #endif
4735
4736 out:
4737     REPLY_MACRO(VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
4738 }
4739 static void
4740 vl_api_map_add_domain_t_handler
4741 (vl_api_map_add_domain_t * mp)
4742 {
4743   vl_api_map_add_domain_reply_t * rmp;
4744   int rv = 0;
4745   u32 index;
4746   u8 flags = mp->is_translation ? MAP_DOMAIN_TRANSLATION : 0;
4747   rv = map_create_domain((ip4_address_t *)&mp->ip4_prefix, mp->ip4_prefix_len,
4748                          (ip6_address_t *)&mp->ip6_prefix, mp->ip6_prefix_len,
4749                          (ip6_address_t *)&mp->ip6_src, mp->ip6_src_prefix_len,
4750                          mp->ea_bits_len, mp->psid_offset, mp->psid_length, &index, ntohs(mp->mtu), flags);
4751
4752   REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
4753                ({
4754                  rmp->index = ntohl(index);
4755                }));
4756 }
4757
4758 static void
4759 vl_api_map_del_domain_t_handler
4760 (vl_api_map_del_domain_t * mp)
4761 {
4762   vl_api_map_del_domain_reply_t * rmp;
4763   int rv = 0;
4764
4765   rv = map_delete_domain(ntohl(mp->index));
4766
4767   REPLY_MACRO(VL_API_MAP_DEL_DOMAIN_REPLY);
4768 }
4769
4770 static void
4771 vl_api_map_add_del_rule_t_handler
4772 (vl_api_map_add_del_rule_t * mp)
4773 {
4774   vl_api_map_del_domain_reply_t * rmp;
4775   int rv = 0;
4776
4777   rv = map_add_del_psid(ntohl(mp->index), ntohs(mp->psid), (ip6_address_t *)mp->ip6_dst, mp->is_add);
4778
4779   REPLY_MACRO(VL_API_MAP_ADD_DEL_RULE_REPLY);
4780 }
4781
4782 static void
4783 vl_api_map_domain_dump_t_handler
4784 (vl_api_map_domain_dump_t * mp)
4785 {
4786   vl_api_map_domain_details_t * rmp;
4787   map_main_t *mm = &map_main;
4788   map_domain_t *d;
4789   unix_shared_memory_queue_t * q;
4790
4791   if (pool_elts (mm->domains) == 0)
4792       return;
4793
4794   q = vl_api_client_index_to_input_queue (mp->client_index);
4795   if (q == 0) {
4796     return;
4797   }
4798
4799   pool_foreach(d, mm->domains, ({
4800     rmp = vl_msg_api_alloc (sizeof (*rmp));
4801     memset (rmp, 0, sizeof (*rmp));
4802     rmp->_vl_msg_id = ntohs(VL_API_MAP_DOMAIN_DETAILS);
4803     rmp->domain_index = htonl(d - mm->domains);
4804     rmp->ea_bits_len = d->ea_bits_len;
4805     rmp->psid_offset = d->psid_offset;
4806     rmp->psid_length = d->psid_length;
4807     memcpy(rmp->ip4_prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix));
4808     rmp->ip4_prefix_len = d->ip4_prefix_len;
4809     memcpy(rmp->ip6_prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix));
4810     rmp->ip6_prefix_len = d->ip6_prefix_len;
4811     memcpy(rmp->ip6_src, &d->ip6_src, sizeof(rmp->ip6_src));
4812     rmp->ip6_src_len = d->ip6_src_len;
4813     rmp->mtu = htons(d->mtu);
4814     rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION);
4815
4816     vl_msg_api_send_shmem (q, (u8 *)&rmp);
4817   }));
4818 }
4819
4820 static void
4821 vl_api_map_rule_dump_t_handler
4822 (vl_api_map_rule_dump_t * mp)
4823 {
4824   unix_shared_memory_queue_t * q;
4825   u16 i;
4826   ip6_address_t dst;
4827   vl_api_map_rule_details_t * rmp;
4828   map_main_t *mm = &map_main;
4829   u32 domain_index = ntohl(mp->domain_index);
4830   map_domain_t *d;
4831
4832   if (pool_elts (mm->domains) == 0)
4833    return;
4834
4835   d = pool_elt_at_index(mm->domains, domain_index);
4836   if (!d || !d->rules) {
4837     return;
4838   }
4839
4840   q = vl_api_client_index_to_input_queue (mp->client_index);
4841   if (q == 0) {
4842     return;
4843   }
4844
4845   for (i = 0; i < (0x1 << d->psid_length); i++) {
4846     dst = d->rules[i];
4847     if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0) {
4848       continue;
4849     }
4850     rmp = vl_msg_api_alloc(sizeof(*rmp));
4851     memset(rmp, 0, sizeof(*rmp));
4852     rmp->_vl_msg_id = ntohs(VL_API_MAP_RULE_DETAILS);
4853     rmp->psid = htons(i);
4854     memcpy(rmp->ip6_dst, &dst, sizeof(rmp->ip6_dst));
4855     vl_msg_api_send_shmem(q, (u8 *)&rmp);
4856   }
4857 }
4858
4859 static void
4860 vl_api_map_summary_stats_t_handler (
4861     vl_api_map_summary_stats_t *mp)
4862 {
4863     vl_api_map_summary_stats_reply_t *rmp;
4864     vlib_combined_counter_main_t *cm;
4865     vlib_counter_t v;
4866     int i, which;
4867     u64 total_pkts[VLIB_N_RX_TX];
4868     u64 total_bytes[VLIB_N_RX_TX];
4869     map_main_t *mm = &map_main;
4870     unix_shared_memory_queue_t *q =
4871         vl_api_client_index_to_input_queue(mp->client_index);
4872
4873     if (!q)
4874         return;
4875     
4876     rmp = vl_msg_api_alloc (sizeof (*rmp));
4877     rmp->_vl_msg_id = ntohs(VL_API_MAP_SUMMARY_STATS_REPLY);
4878     rmp->context = mp->context;
4879     rmp->retval = 0;
4880
4881     memset (total_pkts, 0, sizeof (total_pkts));
4882     memset (total_bytes, 0, sizeof (total_bytes));
4883
4884     map_domain_counter_lock (mm);
4885     vec_foreach(cm, mm->domain_counters) {
4886       which = cm - mm->domain_counters;
4887
4888       for (i = 0; i < vec_len(cm->maxi); i++) {
4889         vlib_get_combined_counter (cm, i, &v);
4890         total_pkts[which] += v.packets;
4891         total_bytes[which] += v.bytes;
4892       }
4893     }
4894
4895     map_domain_counter_unlock (mm);
4896
4897     /* Note: in HOST byte order! */
4898     rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] = total_pkts[MAP_DOMAIN_COUNTER_RX];
4899     rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] = total_bytes[MAP_DOMAIN_COUNTER_RX];
4900     rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] = total_pkts[MAP_DOMAIN_COUNTER_TX];
4901     rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] = total_bytes[MAP_DOMAIN_COUNTER_TX];
4902     rmp->total_bindings = pool_elts(mm->domains);
4903     rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
4904     rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] = map_error_counter_get(ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK);
4905     rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] = map_error_counter_get(ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK);
4906
4907     vl_msg_api_send_shmem(q, (u8 *)&rmp);
4908 }
4909
4910 static void vl_api_ipsec_sa_set_key_t_handler
4911 (vl_api_ipsec_sa_set_key_t * mp)
4912 {
4913     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
4914     vl_api_ipsec_sa_set_key_reply_t *rmp;
4915     int rv;
4916 #if IPSEC > 0
4917     ipsec_sa_t sa;
4918     sa.id = ntohl(mp->sa_id);
4919     sa.crypto_key_len = mp->crypto_key_length;
4920     memcpy(&sa.crypto_key, mp->crypto_key, sizeof(sa.crypto_key));
4921     sa.integ_key_len = mp->integrity_key_length;
4922     memcpy(&sa.integ_key, mp->integrity_key, sizeof(sa.integ_key));
4923
4924     rv = ipsec_set_sa_key(vm, &sa);
4925 #else
4926     rv = VNET_API_ERROR_UNIMPLEMENTED;
4927 #endif
4928
4929     REPLY_MACRO(VL_API_IPSEC_SA_SET_KEY_REPLY);
4930 }
4931
4932 static void vl_api_cop_interface_enable_disable_t_handler
4933 (vl_api_cop_interface_enable_disable_t * mp)
4934 {
4935     vl_api_cop_interface_enable_disable_reply_t * rmp;
4936     int rv;
4937     u32 sw_if_index = ntohl(mp->sw_if_index);
4938     int enable_disable;
4939
4940     VALIDATE_SW_IF_INDEX(mp);
4941
4942     enable_disable = (int) mp->enable_disable;
4943
4944     rv = cop_interface_enable_disable (sw_if_index, enable_disable);
4945
4946     BAD_SW_IF_INDEX_LABEL;
4947
4948     REPLY_MACRO(VL_API_COP_INTERFACE_ENABLE_DISABLE_REPLY);
4949 }
4950
4951 static void vl_api_cop_whitelist_enable_disable_t_handler
4952 (vl_api_cop_whitelist_enable_disable_t * mp)
4953 {
4954     vl_api_cop_whitelist_enable_disable_reply_t * rmp;
4955     cop_whitelist_enable_disable_args_t _a, *a=&_a;
4956     u32 sw_if_index = ntohl(mp->sw_if_index);
4957     int rv;
4958
4959     VALIDATE_SW_IF_INDEX(mp);
4960
4961     a->sw_if_index = sw_if_index;
4962     a->ip4 = mp->ip4;
4963     a->ip6 = mp->ip6;
4964     a->default_cop = mp->default_cop;
4965     a->fib_id = ntohl(mp->fib_id);
4966
4967     rv = cop_whitelist_enable_disable (a);
4968
4969     BAD_SW_IF_INDEX_LABEL;
4970
4971     REPLY_MACRO(VL_API_COP_WHITELIST_ENABLE_DISABLE_REPLY);
4972 }
4973
4974 static void vl_api_get_node_graph_t_handler
4975 (vl_api_get_node_graph_t * mp)
4976 {
4977     int rv = 0;
4978     u8 * vector = 0;
4979     api_main_t * am = &api_main;
4980     vlib_main_t * vm = vlib_get_main();
4981     void * oldheap;
4982     vl_api_get_node_graph_reply_t * rmp;
4983     
4984     pthread_mutex_lock (&am->vlib_rp->mutex);
4985     oldheap = svm_push_data_heap (am->vlib_rp);
4986     
4987     /* 
4988      * Keep the number of memcpy ops to a minimum (e.g. 1).
4989      * The current size of the serialized vector is
4990      * slightly under 4K.
4991      */
4992     vec_validate (vector, 4095);
4993     vec_reset_length (vector);
4994
4995     vector = vlib_node_serialize (&vm->node_main, vector);
4996     
4997     svm_pop_heap (oldheap);
4998     pthread_mutex_unlock (&am->vlib_rp->mutex);
4999
5000     REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
5001                  rmp->reply_in_shmem = (uword) vector);
5002 }
5003
5004 static void vl_api_trace_profile_add_t_handler
5005 (vl_api_trace_profile_add_t *mp)
5006 {
5007     int rv = 0;
5008     vl_api_trace_profile_add_reply_t * rmp;
5009     clib_error_t *error;
5010
5011     /* Ignoring the profile id as currently a single profile 
5012      * is supported */
5013     error = ip6_ioam_trace_profile_set(mp->trace_num_elt, mp->trace_type, 
5014                                ntohl(mp->node_id), ntohl(mp->trace_app_data), 
5015                                mp->pow_enable, mp->trace_tsp, 
5016                                mp->trace_ppc);
5017     if (error) {
5018       clib_error_report(error);
5019       rv = clib_error_get_code(error);
5020     }
5021     
5022     REPLY_MACRO(VL_API_TRACE_PROFILE_ADD_REPLY);
5023 }
5024
5025 static void vl_api_trace_profile_apply_t_handler
5026 (vl_api_trace_profile_apply_t *mp)
5027 {
5028     int rv = 0;
5029     vl_api_trace_profile_apply_reply_t * rmp;
5030  
5031     if (mp->enable != 0) {
5032       rv = ip6_ioam_set_destination ((ip6_address_t *)(&mp->dest_ipv6),
5033                                 ntohl(mp->prefix_length),
5034                                 ntohl(mp->vrf_id), 
5035                         mp->trace_op == IOAM_HBYH_ADD,
5036                         mp->trace_op == IOAM_HBYH_POP,
5037                         mp->trace_op == IOAM_HBYH_MOD);
5038     } else {
5039       //ip6_ioam_clear_destination(&ip6, mp->prefix_length, mp->vrf_id);
5040     }
5041     REPLY_MACRO(VL_API_TRACE_PROFILE_APPLY_REPLY);
5042 }
5043
5044 static void vl_api_trace_profile_del_t_handler
5045 (vl_api_trace_profile_del_t *mp)
5046 {
5047     int rv = 0;
5048     vl_api_trace_profile_del_reply_t * rmp;
5049     clib_error_t *error;
5050
5051     error = clear_ioam_rewrite_fn();
5052     if (error) {
5053       clib_error_report(error);
5054       rv = clib_error_get_code(error);
5055     }
5056     
5057     REPLY_MACRO(VL_API_TRACE_PROFILE_DEL_REPLY);
5058 }
5059
5060
5061 #define BOUNCE_HANDLER(nn)                                              \
5062 static void vl_api_##nn##_t_handler (                                   \
5063     vl_api_##nn##_t *mp)                                                \
5064 {                                                                       \
5065     vpe_client_registration_t *reg;                                     \
5066     vpe_api_main_t * vam = &vpe_api_main;                               \
5067     unix_shared_memory_queue_t * q;                                     \
5068                                                                         \
5069     /* One registration only... */                                      \
5070     pool_foreach(reg, vam->nn##_registrations,                          \
5071     ({                                                                  \
5072         q = vl_api_client_index_to_input_queue (reg->client_index);     \
5073         if (q) {                                                        \
5074             /*                                                          \
5075              * If the queue is stuffed, turf the msg and complain       \
5076              * It's unlikely that the intended recipient is             \
5077              * alive; avoid deadlock at all costs.                      \
5078              */                                                         \
5079             if (q->cursize == q->maxsize) {                             \
5080                 clib_warning ("ERROR: receiver queue full, drop msg");  \
5081                 vl_msg_api_free (mp);                                   \
5082                 return;                                                 \
5083             }                                                           \
5084             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
5085             return;                                                     \
5086         }                                                               \
5087     }));                                                                \
5088     vl_msg_api_free (mp);                                               \
5089 }
5090
5091 BOUNCE_HANDLER(to_netconf_server);
5092 BOUNCE_HANDLER(from_netconf_server);
5093 BOUNCE_HANDLER(to_netconf_client);
5094 BOUNCE_HANDLER(from_netconf_client);
5095
5096 /*
5097  * vpe_api_hookup
5098  * Add vpe's API message handlers to the table.
5099  * vlib has alread mapped shared memory and
5100  * added the client registration handlers. 
5101  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
5102  */
5103
5104 static clib_error_t *
5105 vpe_api_hookup (vlib_main_t *vm)
5106 {
5107     api_main_t * am = &api_main;
5108
5109 #define _(N,n)                                                  \
5110     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
5111                            vl_api_##n##_t_handler,              \
5112                            vl_noop_handler,                     \
5113                            vl_api_##n##_t_endian,               \
5114                            vl_api_##n##_t_print,                \
5115                            sizeof(vl_api_##n##_t), 1); 
5116     foreach_vpe_api_msg;
5117 #undef _
5118
5119     /* 
5120      * Manually register the sr tunnel add del msg, so we trace
5121      * enough bytes to capture a typical segment list
5122      */
5123     vl_msg_api_set_handlers (VL_API_SR_TUNNEL_ADD_DEL, 
5124                              "sr_tunnel_add_del",
5125                              vl_api_sr_tunnel_add_del_t_handler,
5126                              vl_noop_handler,
5127                              vl_api_sr_tunnel_add_del_t_endian,
5128                              vl_api_sr_tunnel_add_del_t_print,
5129                              256, 1);
5130
5131     am->message_bounce [VL_API_FROM_NETCONF_SERVER] = 1;
5132     am->message_bounce [VL_API_TO_NETCONF_SERVER] = 1;
5133     am->message_bounce [VL_API_FROM_NETCONF_CLIENT] = 1;
5134     am->message_bounce [VL_API_TO_NETCONF_CLIENT] = 1;
5135
5136     /* 
5137      * Trace space for 8 MPLS encap labels, classifier mask+match
5138      */
5139     am->api_trace_cfg [VL_API_MPLS_ADD_DEL_ENCAP].size += 8 * sizeof(u32);
5140     am->api_trace_cfg [VL_API_CLASSIFY_ADD_DEL_TABLE].size
5141         += 5 * sizeof (u32x4);
5142     am->api_trace_cfg [VL_API_CLASSIFY_ADD_DEL_SESSION].size
5143         += 5 * sizeof (u32x4);
5144     am->api_trace_cfg [VL_API_VXLAN_ADD_DEL_TUNNEL].size
5145         += 16 * sizeof (u32);
5146     
5147     /*
5148      * trace space for 4 nsh-gre variable TLV words
5149      */
5150     am->api_trace_cfg [VL_API_NSH_GRE_ADD_DEL_TUNNEL].size
5151         += 4 * sizeof (u32);
5152
5153     /* 
5154      * Thread-safe API messages
5155      */
5156     am->is_mp_safe [VL_API_IP_ADD_DEL_ROUTE] = 1;
5157
5158     return 0;
5159 }
5160
5161 VLIB_API_INIT_FUNCTION(vpe_api_hookup);
5162
5163 static clib_error_t *
5164 vpe_api_init (vlib_main_t *vm)
5165 {
5166     vpe_api_main_t *am = &vpe_api_main;
5167
5168     am->vlib_main = vm;
5169     am->vnet_main = vnet_get_main();
5170     am->interface_events_registration_hash = hash_create (0, sizeof (uword));
5171     am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
5172     am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
5173     am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
5174     am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
5175     am->oam_events_registration_hash = hash_create (0, sizeof (uword));
5176
5177     vl_api_init (vm);
5178     vl_set_memory_region_name ("/vpe-api");
5179     vl_enable_disable_memory_api (vm, 1 /* enable it */);
5180
5181     return 0;
5182 }
5183
5184 VLIB_INIT_FUNCTION(vpe_api_init);
5185
5186 static clib_error_t *
5187 chroot_config (vlib_main_t * vm, unformat_input_t * input)
5188 {
5189   u8 * chroot_path;
5190
5191   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
5192     {
5193       if (unformat (input, "prefix %s", &chroot_path))
5194         {
5195           vec_add1 (chroot_path, 0);
5196           vl_set_memory_root_path ((char *)chroot_path);
5197         }
5198       else
5199         return clib_error_return (0, "unknown input `%U'",
5200                                   format_unformat_error, input);
5201     }
5202   return 0;
5203 }
5204 VLIB_EARLY_CONFIG_FUNCTION (chroot_config, "chroot");
5205
5206 void * get_unformat_vnet_sw_interface (void)
5207 {
5208     return (void *) &unformat_vnet_sw_interface;
5209 }
5210
5211 #undef vl_api_version
5212 #define vl_api_version(n,v) static u32 vpe_api_version = v;
5213 #include <api/vpe.api.h>
5214 #undef vl_api_version
5215
5216 int vl_msg_api_version_check (vl_api_memclnt_create_t * mp)
5217 {
5218     if (clib_host_to_net_u32(mp->api_versions[0]) != vpe_api_version) {
5219         clib_warning ("vpe API mismatch: 0x%08x instead of 0x%08x",
5220                       clib_host_to_net_u32 (mp->api_versions[0]),
5221                       vpe_api_version);
5222         return -1;
5223     }
5224     return 0;
5225 }
5226
5227 static u8 * format_arp_event (u8 * s, va_list * args)
5228 {
5229     vl_api_ip4_arp_event_t * event = va_arg (*args, vl_api_ip4_arp_event_t *);
5230
5231     s = format (s, "pid %d: %U", event->pid,
5232                 format_ip4_address, &event->address);
5233     return s;
5234 }
5235
5236 static clib_error_t * 
5237 show_ip4_arp_events_fn (vlib_main_t * vm,
5238                         unformat_input_t * input, 
5239                         vlib_cli_command_t * cmd)
5240 {
5241     vpe_api_main_t * am = &vpe_api_main;
5242     vl_api_ip4_arp_event_t * event;
5243
5244     if (pool_elts (am->arp_events) == 0) {
5245         vlib_cli_output (vm, "No active arp event registrations");
5246         return 0;
5247     }
5248
5249     pool_foreach (event, am->arp_events, 
5250     ({
5251         vlib_cli_output (vm, "%U", format_arp_event, event);
5252     }));
5253
5254     return 0;
5255 }
5256
5257 VLIB_CLI_COMMAND (show_ip4_arp_events, static) = {
5258   .path = "show arp event registrations",
5259   .function = show_ip4_arp_events_fn,
5260   .short_help = "Show arp event registrations",
5261 };