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