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