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