1550dc89a979c92245229fb4e3751ddacb622038
[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 <pwd.h>
34 #include <grp.h>
35
36 #include <vppinfra/clib.h>
37 #include <vppinfra/vec.h>
38 #include <vppinfra/hash.h>
39 #include <vppinfra/bitmap.h>
40 #include <vppinfra/fifo.h>
41 #include <vppinfra/time.h>
42 #include <vppinfra/mheap.h>
43 #include <vppinfra/heap.h>
44 #include <vppinfra/pool.h>
45 #include <vppinfra/format.h>
46 #include <vppinfra/error.h>
47
48 #include <vnet/api_errno.h> // alagalah TODO : committers please pay note, is this ok?
49 #include <vnet/vnet.h>
50 #include <vnet/l2/l2_input.h>
51 #include <vnet/l2/l2_bd.h>
52 #include <vnet/l2tp/l2tp.h>
53 #include <vnet/ip/ip.h>
54 #include <vnet/unix/tuntap.h>
55 #include <vnet/unix/tapcli.h>
56 #include <vnet/mpls-gre/mpls.h>
57 #include <vnet/dhcp/proxy.h>
58 #include <vnet/dhcp/client.h>
59 #if IPV6SR > 0
60 #include <vnet/sr/sr.h>
61 #endif
62 #include <vnet/dhcpv6/proxy.h>
63 #include <vlib/vlib.h>
64 #include <vlib/unix/unix.h>
65 #include <vlibapi/api.h>
66 #include <vlibmemory/api.h>
67 #include <vnet/classify/vnet_classify.h>
68 #include <vnet/classify/input_acl.h>
69 #include <vnet/l2/l2_classify.h>
70 #include <vnet/vxlan/vxlan.h>
71 #include <vnet/gre/gre.h>
72 #include <vnet/l2/l2_vtr.h>
73 #include <vnet/vxlan-gpe/vxlan_gpe.h>
74 #include <vnet/lisp-gpe/lisp_gpe.h>
75 #include <vnet/lisp-cp/control.h>
76 #include <vnet/map/map.h>
77 #include <vnet/cop/cop.h>
78 #include <vnet/ip/ip6_hop_by_hop.h>
79 #include <vnet/devices/af_packet/af_packet.h>
80 #include <vnet/policer/policer.h>
81 #include <vnet/devices/netmap/netmap.h>
82
83 #undef BIHASH_TYPE
84 #undef __included_bihash_template_h__
85 #include <vnet/l2/l2_fib.h>
86
87 #if IPSEC > 0
88 #include <vnet/ipsec/ipsec.h>
89 #include <vnet/ipsec/ikev2.h>
90 #endif /* IPSEC */
91 #if DPDK > 0
92 #include <vnet/devices/virtio/vhost-user.h>
93 #endif
94
95 #include <stats/stats.h>
96 #include <oam/oam.h>
97
98 #include <vnet/ethernet/ethernet.h>
99 #include <vnet/ethernet/arp_packet.h>
100 #include <vnet/interface.h>
101
102 #include <vnet/l2/l2_fib.h>
103 #include <vnet/l2/l2_bd.h>
104 #include <api/vpe_msg_enum.h>
105
106 #define f64_endian(a)
107 #define f64_print(a,b)
108
109 #define vl_typedefs             /* define message structures */
110 #include <api/vpe_all_api_h.h>
111 #undef vl_typedefs
112
113 #define vl_endianfun             /* define message structures */
114 #include <api/vpe_all_api_h.h>
115 #undef vl_endianfun
116
117 /* instantiate all the print functions we know about */
118 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
119 #define vl_printfun
120 #include <api/vpe_all_api_h.h>
121 #undef vl_printfun
122
123 #define REPLY_MACRO(t)                                          \
124 do {                                                            \
125     unix_shared_memory_queue_t * q;                             \
126     rv = vl_msg_api_pd_handler (mp, rv);                        \
127     q = vl_api_client_index_to_input_queue (mp->client_index);  \
128     if (!q)                                                     \
129         return;                                                 \
130                                                                 \
131     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
132     rmp->_vl_msg_id = ntohs((t));                               \
133     rmp->context = mp->context;                                 \
134     rmp->retval = ntohl(rv);                                    \
135                                                                 \
136     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
137 } while(0);
138
139 #define REPLY_MACRO2(t, body)                                   \
140 do {                                                            \
141     unix_shared_memory_queue_t * q;                             \
142     rv = vl_msg_api_pd_handler (mp, rv);                        \
143     q = vl_api_client_index_to_input_queue (mp->client_index);  \
144     if (!q)                                                     \
145         return;                                                 \
146                                                                 \
147     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
148     rmp->_vl_msg_id = ntohs((t));                               \
149     rmp->context = mp->context;                                 \
150     rmp->retval = ntohl(rv);                                    \
151     do {body;} while (0);                                       \
152     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
153 } while(0);
154
155 #if (1 || CLIB_DEBUG > 0)       /* "trust, but verify" */
156
157 #define VALIDATE_SW_IF_INDEX(mp)                                \
158  do { u32 __sw_if_index = ntohl(mp->sw_if_index);               \
159     vnet_main_t *__vnm = vnet_get_main();                       \
160     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
161                            __sw_if_index)) {                    \
162         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
163         goto bad_sw_if_index;                                   \
164     }                                                           \
165 } while(0);
166
167 #define BAD_SW_IF_INDEX_LABEL                   \
168 do {                                            \
169 bad_sw_if_index:                                \
170     ;                                           \
171 } while (0);
172
173 #define VALIDATE_RX_SW_IF_INDEX(mp)                             \
174  do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index);         \
175     vnet_main_t *__vnm = vnet_get_main();                       \
176     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
177                            __rx_sw_if_index)) {                 \
178         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
179         goto bad_rx_sw_if_index;                                \
180     }                                                           \
181 } while(0);
182
183 #define BAD_RX_SW_IF_INDEX_LABEL                \
184 do {                                            \
185 bad_rx_sw_if_index:                             \
186     ;                                           \
187 } while (0);
188
189 #define VALIDATE_TX_SW_IF_INDEX(mp)                             \
190  do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index);         \
191     vnet_main_t *__vnm = vnet_get_main();                       \
192     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
193                            __tx_sw_if_index)) {                 \
194         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
195         goto bad_tx_sw_if_index;                                \
196     }                                                           \
197 } while(0);
198
199 #define BAD_TX_SW_IF_INDEX_LABEL                \
200 do {                                            \
201 bad_tx_sw_if_index:                             \
202     ;                                           \
203 } while (0);
204
205 #else
206
207 #define VALIDATE_SW_IF_INDEX(mp)
208 #define BAD_SW_IF_INDEX_LABEL
209 #define VALIDATE_RX_SW_IF_INDEX(mp)
210 #define BAD_RX_SW_IF_INDEX_LABEL
211 #define VALIDATE_TX_SW_IF_INDEX(mp)
212 #define BAD_TX_SW_IF_INDEX_LABEL
213
214 #endif  /* CLIB_DEBUG > 0 */
215
216 #define foreach_vpe_api_msg                                             \
217 _(WANT_INTERFACE_EVENTS, want_interface_events)                         \
218 _(WANT_OAM_EVENTS, want_oam_events)                                     \
219 _(OAM_ADD_DEL, oam_add_del)                                             \
220 _(SW_INTERFACE_DUMP, sw_interface_dump)                                 \
221 _(SW_INTERFACE_DETAILS, sw_interface_details)                           \
222 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags)                       \
223 _(IP_ADD_DEL_ROUTE, ip_add_del_route)                                   \
224 _(IS_ADDRESS_REACHABLE, is_address_reachable)                           \
225 _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address)           \
226 _(SW_INTERFACE_SET_TABLE, sw_interface_set_table)                       \
227 _(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath)                       \
228 _(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect)           \
229 _(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge)               \
230 _(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del)                         \
231 _(BRIDGE_DOMAIN_DUMP, bridge_domain_dump)                               \
232 _(BRIDGE_DOMAIN_DETAILS, bridge_domain_details)                         \
233 _(BRIDGE_DOMAIN_SW_IF_DETAILS, bridge_domain_sw_if_details)             \
234 _(L2FIB_ADD_DEL, l2fib_add_del)                                         \
235 _(L2_FLAGS, l2_flags)                                                   \
236 _(BRIDGE_FLAGS, bridge_flags)                                           \
237 _(TAP_CONNECT, tap_connect)                                             \
238 _(TAP_MODIFY, tap_modify)                                               \
239 _(TAP_DELETE, tap_delete)                                               \
240 _(SW_INTERFACE_TAP_DUMP, sw_interface_tap_dump)                         \
241 _(CREATE_VLAN_SUBIF, create_vlan_subif)                                 \
242 _(CREATE_SUBIF, create_subif)                                           \
243 _(MPLS_GRE_ADD_DEL_TUNNEL, mpls_gre_add_del_tunnel)                     \
244 _(MPLS_ETHERNET_ADD_DEL_TUNNEL, mpls_ethernet_add_del_tunnel)           \
245 _(MPLS_ETHERNET_ADD_DEL_TUNNEL_2, mpls_ethernet_add_del_tunnel_2)       \
246 _(MPLS_ADD_DEL_ENCAP, mpls_add_del_encap)                               \
247 _(MPLS_ADD_DEL_DECAP, mpls_add_del_decap)                               \
248 _(PROXY_ARP_ADD_DEL, proxy_arp_add_del)                                 \
249 _(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable)       \
250 _(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del)                             \
251 _(VNET_GET_SUMMARY_STATS, vnet_get_summary_stats)                       \
252 _(RESET_FIB, reset_fib)                                                 \
253 _(DHCP_PROXY_CONFIG,dhcp_proxy_config)                                  \
254 _(DHCP_PROXY_CONFIG_2,dhcp_proxy_config_2)                              \
255 _(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss)                                \
256 _(DHCP_CLIENT_CONFIG, dhcp_client_config)                               \
257 _(SET_IP_FLOW_HASH,set_ip_flow_hash)                                    \
258 _(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config)           \
259 _(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix)           \
260 _(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable )    \
261 _(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS,                              \
262   sw_interface_ip6_set_link_local_address)                              \
263 _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered)             \
264 _(CREATE_LOOPBACK, create_loopback)                                     \
265 _(CONTROL_PING, control_ping)                                           \
266 _(CLI_REQUEST, cli_request)                                             \
267 _(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit)                       \
268 _(L2_PATCH_ADD_DEL, l2_patch_add_del)                                   \
269 _(CLASSIFY_ADD_DEL_TABLE, classify_add_del_table)                       \
270 _(CLASSIFY_ADD_DEL_SESSION, classify_add_del_session)                   \
271 _(CLASSIFY_SET_INTERFACE_IP_TABLE, classify_set_interface_ip_table)     \
272 _(CLASSIFY_SET_INTERFACE_L2_TABLES, classify_set_interface_l2_tables)   \
273 _(GET_NODE_INDEX, get_node_index)                                       \
274 _(ADD_NODE_NEXT, add_node_next)                                         \
275 _(L2TPV3_CREATE_TUNNEL, l2tpv3_create_tunnel)                           \
276 _(L2TPV3_SET_TUNNEL_COOKIES, l2tpv3_set_tunnel_cookies)                 \
277 _(L2TPV3_INTERFACE_ENABLE_DISABLE, l2tpv3_interface_enable_disable)     \
278 _(L2TPV3_SET_LOOKUP_KEY, l2tpv3_set_lookup_key)                         \
279 _(SW_IF_L2TPV3_TUNNEL_DUMP, sw_if_l2tpv3_tunnel_dump)                   \
280 _(VXLAN_ADD_DEL_TUNNEL, vxlan_add_del_tunnel)                           \
281 _(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump)                                 \
282 _(GRE_ADD_DEL_TUNNEL, gre_add_del_tunnel)                               \
283 _(GRE_TUNNEL_DUMP, gre_tunnel_dump)                                     \
284 _(L2_FIB_CLEAR_TABLE, l2_fib_clear_table)                               \
285 _(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter)                     \
286 _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite)         \
287 _(CREATE_VHOST_USER_IF, create_vhost_user_if)                           \
288 _(MODIFY_VHOST_USER_IF, modify_vhost_user_if)                           \
289 _(DELETE_VHOST_USER_IF, delete_vhost_user_if)                           \
290 _(SW_INTERFACE_VHOST_USER_DUMP, sw_interface_vhost_user_dump)           \
291 _(IP_ADDRESS_DUMP, ip_address_dump)                                     \
292 _(IP_DUMP, ip_dump)                                                     \
293 _(SW_INTERFACE_VHOST_USER_DETAILS, sw_interface_vhost_user_details)     \
294 _(SHOW_VERSION, show_version)                                           \
295 _(L2_FIB_TABLE_DUMP, l2_fib_table_dump)                                 \
296 _(L2_FIB_TABLE_ENTRY, l2_fib_table_entry)                               \
297 _(VXLAN_GPE_ADD_DEL_TUNNEL, vxlan_gpe_add_del_tunnel)                   \
298 _(VXLAN_GPE_TUNNEL_DUMP, vxlan_gpe_tunnel_dump)                         \
299 _(INTERFACE_NAME_RENUMBER, interface_name_renumber)                     \
300 _(WANT_IP4_ARP_EVENTS, want_ip4_arp_events)                             \
301 _(INPUT_ACL_SET_INTERFACE, input_acl_set_interface)                     \
302 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del)                                 \
303 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd)             \
304 _(IPSEC_SPD_ADD_DEL_ENTRY, ipsec_spd_add_del_entry)                     \
305 _(IPSEC_SAD_ADD_DEL_ENTRY, ipsec_sad_add_del_entry)                     \
306 _(IPSEC_SA_SET_KEY, ipsec_sa_set_key)                                   \
307 _(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del)                         \
308 _(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth)                       \
309 _(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id)                           \
310 _(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts)                           \
311 _(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key)                             \
312 _(DELETE_LOOPBACK, delete_loopback)                                     \
313 _(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del)                                 \
314 _(MAP_ADD_DOMAIN, map_add_domain)                                       \
315 _(MAP_DEL_DOMAIN, map_del_domain)                                       \
316 _(MAP_ADD_DEL_RULE, map_add_del_rule)                                   \
317 _(MAP_DOMAIN_DUMP, map_domain_dump)                                     \
318 _(MAP_RULE_DUMP, map_rule_dump)                                         \
319 _(MAP_SUMMARY_STATS, map_summary_stats)                                 \
320 _(COP_INTERFACE_ENABLE_DISABLE, cop_interface_enable_disable)           \
321 _(COP_WHITELIST_ENABLE_DISABLE, cop_whitelist_enable_disable)           \
322 _(GET_NODE_GRAPH, get_node_graph)                                       \
323 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats)                   \
324 _(TRACE_PROFILE_ADD, trace_profile_add)                                 \
325 _(TRACE_PROFILE_APPLY, trace_profile_apply)                             \
326 _(TRACE_PROFILE_DEL, trace_profile_del)                                 \
327 _(LISP_ADD_DEL_LOCATOR_SET, lisp_add_del_locator_set)                   \
328 _(LISP_ADD_DEL_LOCATOR, lisp_add_del_locator)                           \
329 _(LISP_ADD_DEL_LOCAL_EID, lisp_add_del_local_eid)                       \
330 _(LISP_GPE_ADD_DEL_FWD_ENTRY, lisp_gpe_add_del_fwd_entry)               \
331 _(LISP_ADD_DEL_MAP_RESOLVER, lisp_add_del_map_resolver)                 \
332 _(LISP_GPE_ENABLE_DISABLE, lisp_gpe_enable_disable)                     \
333 _(LISP_ENABLE_DISABLE, lisp_enable_disable)                             \
334 _(LISP_GPE_ADD_DEL_IFACE, lisp_gpe_add_del_iface)                       \
335 _(LISP_ADD_DEL_REMOTE_MAPPING, lisp_add_del_remote_mapping)             \
336 _(LISP_PITR_SET_LOCATOR_SET, lisp_pitr_set_locator_set)                 \
337 _(LISP_EID_TABLE_ADD_DEL_MAP, lisp_eid_table_add_del_map)               \
338 _(LISP_LOCATOR_SET_DUMP, lisp_locator_set_dump)                         \
339 _(LISP_LOCAL_EID_TABLE_DUMP, lisp_local_eid_table_dump)                 \
340 _(LISP_GPE_TUNNEL_DUMP, lisp_gpe_tunnel_dump)                           \
341 _(LISP_MAP_RESOLVER_DUMP, lisp_map_resolver_dump)                       \
342 _(LISP_EID_TABLE_MAP_DUMP, lisp_eid_table_map_dump)                     \
343 _(LISP_ENABLE_DISABLE_STATUS_DUMP,                                      \
344   lisp_enable_disable_status_dump)                                      \
345 _(LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS,                                   \
346   lisp_add_del_map_request_itr_rlocs)                                   \
347 _(LISP_GET_MAP_REQUEST_ITR_RLOCS, lisp_get_map_request_itr_rlocs)       \
348 _(SR_MULTICAST_MAP_ADD_DEL, sr_multicast_map_add_del)                   \
349 _(AF_PACKET_CREATE, af_packet_create)                                   \
350 _(AF_PACKET_DELETE, af_packet_delete)                                   \
351 _(POLICER_ADD_DEL, policer_add_del)                                     \
352 _(POLICER_DUMP, policer_dump)                                           \
353 _(NETMAP_CREATE, netmap_create)                                         \
354 _(NETMAP_DELETE, netmap_delete)                                         \
355 _(MPLS_GRE_TUNNEL_DUMP, mpls_gre_tunnel_dump)                           \
356 _(MPLS_GRE_TUNNEL_DETAILS, mpls_gre_tunnel_details)                     \
357 _(MPLS_ETH_TUNNEL_DUMP, mpls_eth_tunnel_dump)                           \
358 _(MPLS_ETH_TUNNEL_DETAILS, mpls_eth_tunnel_details)                     \
359 _(MPLS_FIB_ENCAP_DUMP, mpls_fib_encap_dump)                             \
360 _(MPLS_FIB_ENCAP_DETAILS, mpls_fib_encap_details)                       \
361 _(MPLS_FIB_DECAP_DUMP, mpls_fib_decap_dump)                             \
362 _(MPLS_FIB_DECAP_DETAILS, mpls_fib_decap_details)
363
364 #define QUOTE_(x) #x
365 #define QUOTE(x) QUOTE_(x)
366
367 #define foreach_registration_hash               \
368 _(interface_events)                             \
369 _(to_netconf_server)                            \
370 _(from_netconf_server)                          \
371 _(to_netconf_client)                            \
372 _(from_netconf_client)                          \
373 _(oam_events)
374
375 typedef enum {
376     RESOLVE_IP4_ADD_DEL_ROUTE=1,
377     RESOLVE_IP6_ADD_DEL_ROUTE,
378     RESOLVE_MPLS_ETHERNET_ADD_DEL,
379 } resolve_t;
380
381 typedef struct {
382     u8 resolve_type;
383     union {
384         vl_api_ip_add_del_route_t r;
385         vl_api_mpls_ethernet_add_del_tunnel_2_t t;
386     };
387 } pending_route_t;
388
389 typedef struct {
390
391 #define _(a) uword *a##_registration_hash;              \
392     vpe_client_registration_t * a##_registrations;
393 foreach_registration_hash
394 #undef _
395
396     /* notifications happen really early in the game */
397     u8 link_state_process_up;
398
399     /* ip4 pending route adds */
400     pending_route_t * pending_routes;
401
402     /* ip4 arp event registration pool */
403     vl_api_ip4_arp_event_t * arp_events;
404
405     /* convenience */
406     vlib_main_t * vlib_main;
407     vnet_main_t * vnet_main;
408 } vpe_api_main_t;
409
410 static vlib_node_registration_t vpe_resolver_process_node;
411 static vpe_api_main_t vpe_api_main;
412
413 static void send_sw_interface_flags (vpe_api_main_t * am,
414                                      unix_shared_memory_queue_t *q,
415                                      vnet_sw_interface_t * swif);
416 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
417                                      unix_shared_memory_queue_t *q,
418                                      u32 sw_if_index);
419
420 static int arp_change_delete_callback (u32 pool_index, u8 * notused);
421
422
423 /* Clean up all registrations belonging to the indicated client */
424 int vl_api_memclnt_delete_callback (u32 client_index)
425 {
426     vpe_api_main_t * vam = &vpe_api_main;
427     vpe_client_registration_t *rp;
428     uword * p;
429     int stats_memclnt_delete_callback (u32 client_index);
430
431     stats_memclnt_delete_callback (client_index);
432
433 #define _(a)                                                    \
434     p = hash_get (vam->a##_registration_hash, client_index);    \
435     if (p) {                                                    \
436         rp = pool_elt_at_index (vam->a##_registrations, p[0]);  \
437         pool_put (vam->a##_registrations, rp);                  \
438         hash_unset (vam->a##_registration_hash, client_index);  \
439     }
440     foreach_registration_hash;
441 #undef _
442     return 0;
443 }
444
445 #define API_LINK_STATE_EVENT 1
446 #define API_ADMIN_UP_DOWN_EVENT 2
447
448 static int
449 event_data_cmp (void * a1, void * a2)
450 {
451   uword * e1 = a1;
452   uword * e2 = a2;
453
454   return (word) e1[0] - (word) e2[0];
455 }
456
457 static uword
458 link_state_process (vlib_main_t * vm,
459                     vlib_node_runtime_t * rt,
460                     vlib_frame_t * f)
461 {
462     vpe_api_main_t * vam = &vpe_api_main;
463     vnet_main_t * vnm = vam->vnet_main;
464     vnet_sw_interface_t * swif;
465     uword * event_data = 0;
466     vpe_client_registration_t *reg;
467     int i;
468     u32 prev_sw_if_index;
469     unix_shared_memory_queue_t * q;
470
471     vam->link_state_process_up = 1;
472
473     while (1) {
474         vlib_process_wait_for_event (vm);
475
476         /* Unified list of changed link or admin state sw_if_indices */
477         vlib_process_get_events_with_type
478             (vm, &event_data, API_LINK_STATE_EVENT);
479         vlib_process_get_events_with_type
480             (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
481
482         /* Sort, so we can eliminate duplicates */
483         vec_sort_with_function (event_data, event_data_cmp);
484
485         prev_sw_if_index = ~0;
486
487         for (i = 0; i < vec_len(event_data); i++) {
488             /* Only one message per swif */
489             if (prev_sw_if_index == event_data[i])
490                 continue;
491             prev_sw_if_index = event_data[i];
492
493             pool_foreach(reg, vam->interface_events_registrations,
494             ({
495                 q = vl_api_client_index_to_input_queue (reg->client_index);
496                 if (q) {
497                     // sw_interface may be deleted already
498                     if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
499                             event_data[i]))
500                     {
501                         swif = vnet_get_sw_interface (vnm, event_data[i]);
502                         send_sw_interface_flags (vam, q, swif);
503                     }
504                 }
505             }));
506         }
507         vec_reset_length (event_data);
508     }
509
510     return 0;
511 }
512
513 static clib_error_t *
514 link_up_down_function (vnet_main_t *vm, u32 hw_if_index, u32 flags);
515 static clib_error_t *
516 admin_up_down_function (vnet_main_t *vm, u32 hw_if_index, u32 flags);
517
518 VLIB_REGISTER_NODE (link_state_process_node,static) = {
519     .function = link_state_process,
520     .type = VLIB_NODE_TYPE_PROCESS,
521     .name = "vpe-link-state-process",
522 };
523
524 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
525 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
526
527 static clib_error_t *
528 link_up_down_function (vnet_main_t *vm, u32 hw_if_index, u32 flags)
529 {
530     vpe_api_main_t * vam = &vpe_api_main;
531     vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
532
533     if (vam->link_state_process_up)
534         vlib_process_signal_event (vam->vlib_main,
535                                    link_state_process_node.index,
536                                    API_LINK_STATE_EVENT,
537                                    hi->sw_if_index);
538     return 0;
539 }
540
541 static clib_error_t *
542 admin_up_down_function (vnet_main_t *vm, u32 sw_if_index, u32 flags)
543 {
544     vpe_api_main_t * vam = &vpe_api_main;
545
546     /*
547      * Note: it's perfectly fair to set a subif admin up / admin down.
548      * Note the subtle distinction between this routine and the previous
549      * routine.
550      */
551     if (vam->link_state_process_up)
552         vlib_process_signal_event (vam->vlib_main,
553                                    link_state_process_node.index,
554                                    API_ADMIN_UP_DOWN_EVENT,
555                                    sw_if_index);
556     return 0;
557 }
558
559 #define pub_sub_handler(lca,UCA)                                        \
560 static void vl_api_want_##lca##_t_handler (                             \
561     vl_api_want_##lca##_t *mp)                                          \
562 {                                                                       \
563     vpe_api_main_t *vam = &vpe_api_main;                                \
564     vpe_client_registration_t *rp;                                      \
565     vl_api_want_##lca##_reply_t *rmp;                                   \
566     uword *p;                                                           \
567     i32 rv = 0;                                                         \
568                                                                         \
569     p = hash_get (vam->lca##_registration_hash, mp->client_index);      \
570     if (p) {                                                            \
571         if (mp->enable_disable) {                                       \
572             clib_warning ("pid %d: already enabled...", mp->pid);       \
573             rv = VNET_API_ERROR_INVALID_REGISTRATION;                   \
574             goto reply;                                                 \
575         } else {                                                        \
576             rp = pool_elt_at_index (vam->lca##_registrations, p[0]);    \
577             pool_put (vam->lca##_registrations, rp);                    \
578             hash_unset (vam->lca##_registration_hash,                   \
579                 mp->client_index);                                      \
580             goto reply;                                                 \
581         }                                                               \
582     }                                                                   \
583     if (mp->enable_disable == 0) {                                      \
584         clib_warning ("pid %d: already disabled...", mp->pid);          \
585         rv = VNET_API_ERROR_INVALID_REGISTRATION;                       \
586         goto reply;                                                     \
587     }                                                                   \
588     pool_get (vam->lca##_registrations, rp);                            \
589     rp->client_index = mp->client_index;                                \
590     rp->client_pid = mp->pid;                                           \
591     hash_set (vam->lca##_registration_hash, rp->client_index,           \
592               rp - vam->lca##_registrations);                           \
593                                                                         \
594 reply:                                                                  \
595     REPLY_MACRO (VL_API_WANT_##UCA##_REPLY);                            \
596 }
597
598 pub_sub_handler (interface_events,INTERFACE_EVENTS)
599 pub_sub_handler (oam_events,OAM_EVENTS)
600
601 #define RESOLUTION_EVENT 1
602 #define RESOLUTION_PENDING_EVENT 2
603 #define IP4_ARP_EVENT 3
604
605 static int ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t *mp);
606 static int ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t *mp);
607 static int mpls_ethernet_add_del_tunnel_2_t_handler
608 (vl_api_mpls_ethernet_add_del_tunnel_2_t *mp);
609
610 void handle_ip4_arp_event (u32 pool_index)
611 {
612     vpe_api_main_t * vam = &vpe_api_main;
613     vnet_main_t * vnm = vam->vnet_main;
614     vlib_main_t * vm = vam->vlib_main;
615     vl_api_ip4_arp_event_t * event;
616     vl_api_ip4_arp_event_t * mp;
617     unix_shared_memory_queue_t * q;
618
619     /* Client can cancel, die, etc. */
620     if (pool_is_free_index (vam->arp_events, pool_index))
621         return;
622
623     event = pool_elt_at_index (vam->arp_events, pool_index);
624
625     q = vl_api_client_index_to_input_queue (event->client_index);
626     if (!q) {
627         (void) vnet_add_del_ip4_arp_change_event
628             (vnm, arp_change_delete_callback,
629              event->pid, &event->address,
630              vpe_resolver_process_node.index, IP4_ARP_EVENT,
631              ~0 /* pool index, notused */, 0 /* is_add */);
632         return;
633     }
634
635     if (q->cursize < q->maxsize) {
636         mp =  vl_msg_api_alloc (sizeof (*mp));
637         clib_memcpy (mp, event, sizeof (*mp));
638         vl_msg_api_send_shmem (q, (u8 *)&mp);
639     } else {
640         static f64 last_time;
641         /*
642          * Throttle syslog msgs.
643          * It's pretty tempting to just revoke the registration...
644          */
645         if (vlib_time_now (vm) > last_time + 10.0) {
646             clib_warning ("arp event for %U to pid %d: queue stuffed!",
647                           format_ip4_address, &event->address, event->pid);
648             last_time = vlib_time_now(vm);
649         }
650     }
651 }
652
653 static uword
654 resolver_process (vlib_main_t * vm,
655                     vlib_node_runtime_t * rt,
656                     vlib_frame_t * f)
657 {
658     uword event_type;
659     uword *event_data = 0;
660     f64 timeout = 100.0;
661     vpe_api_main_t * vam = &vpe_api_main;
662     pending_route_t * pr;
663     vl_api_ip_add_del_route_t * adr;
664     vl_api_mpls_ethernet_add_del_tunnel_2_t *pme;
665     u32 * resolution_failures = 0;
666     int i, rv;
667     clib_error_t * e;
668
669     while (1) {
670         vlib_process_wait_for_event_or_clock (vm, timeout);
671
672         event_type = vlib_process_get_events (vm, &event_data);
673
674         switch (event_type) {
675         case RESOLUTION_PENDING_EVENT:
676             timeout = 1.0;
677             break;
678
679         case RESOLUTION_EVENT:
680             for (i = 0; i < vec_len(event_data); i++) {
681                 /*
682                  * Resolution events can occur long after the
683                  * original request has timed out. $$$ add a cancel
684                  * mechanism..
685                  */
686                 if (pool_is_free_index (vam->pending_routes, event_data[i]))
687                     continue;
688
689                 pr = pool_elt_at_index (vam->pending_routes, event_data[i]);
690                 adr = &pr->r;
691                 pme = &pr->t;
692
693                 switch (pr->resolve_type) {
694                 case RESOLVE_IP4_ADD_DEL_ROUTE:
695                     rv = ip4_add_del_route_t_handler (adr);
696                     clib_warning ("resolver: add %U/%d via %U %s",
697                                   format_ip4_address,
698                                   (ip4_address_t *)&(adr->dst_address),
699                                   adr->dst_address_length,
700                                   format_ip4_address,
701                                   (ip4_address_t *)&(adr->next_hop_address),
702                                   (rv >= 0) ? "succeeded" : "failed");
703                     break;
704
705                 case RESOLVE_IP6_ADD_DEL_ROUTE:
706                     rv = ip6_add_del_route_t_handler (adr);
707                     clib_warning ("resolver: add %U/%d via %U %s",
708                                   format_ip6_address,
709                                   (ip6_address_t *)&(adr->dst_address),
710                                   adr->dst_address_length,
711                                   format_ip6_address,
712                                   (ip6_address_t *)&(adr->next_hop_address),
713                                   (rv >= 0) ? "succeeded" : "failed");
714                     break;
715
716                 case RESOLVE_MPLS_ETHERNET_ADD_DEL:
717                     rv = mpls_ethernet_add_del_tunnel_2_t_handler (pme);
718                     clib_warning ("resolver: add mpls-o-e via %U %s",
719                                   format_ip4_address,
720             (ip4_address_t *)&(pme->next_hop_ip4_address_in_outer_vrf),
721                                   (rv >= 0) ? "succeeded" : "failed");
722                     break;
723
724                 default:
725                     clib_warning ("resolver: BOGUS TYPE %d", pr->resolve_type);
726                 }
727                 pool_put (vam->pending_routes, pr);
728             }
729             break;
730
731         case IP4_ARP_EVENT:
732             for (i = 0; i < vec_len(event_data); i++)
733                 handle_ip4_arp_event (event_data[i]);
734             break;
735
736         case ~0:                /* timeout, retry pending resolutions */
737             pool_foreach (pr, vam->pending_routes,
738             ({
739                 int is_adr = 1;
740                 adr = &pr->r;
741                 pme = &pr->t;
742
743                 /* May fail, e.g. due to interface down */
744                 switch (pr->resolve_type) {
745                 case RESOLVE_IP4_ADD_DEL_ROUTE:
746                     e = ip4_probe_neighbor
747                         (vm, (ip4_address_t *)&(adr->next_hop_address),
748                          ntohl(adr->next_hop_sw_if_index));
749                     break;
750
751                 case RESOLVE_IP6_ADD_DEL_ROUTE:
752                     e = ip6_probe_neighbor
753                         (vm, (ip6_address_t *)&(adr->next_hop_address),
754                          ntohl(adr->next_hop_sw_if_index));
755                     break;
756
757                 case RESOLVE_MPLS_ETHERNET_ADD_DEL:
758                     is_adr = 0;
759                     e = ip4_probe_neighbor
760                         (vm,
761             (ip4_address_t *)&(pme->next_hop_ip4_address_in_outer_vrf),
762                          pme->resolve_opaque);
763                     break;
764
765                 default:
766                     e = clib_error_return (0, "resolver: BOGUS TYPE %d",
767                                            pr->resolve_type);
768                 }
769                 if (e) {
770                     clib_error_report (e);
771                     if (is_adr)
772                         adr->resolve_attempts = 1;
773                     else
774                         pme->resolve_attempts = 1;
775
776                 }
777                 if (is_adr) {
778                     adr->resolve_attempts -= 1;
779                     if (adr->resolve_attempts == 0)
780                         vec_add1 (resolution_failures,
781                                   pr - vam->pending_routes);
782                 } else {
783                     pme->resolve_attempts -= 1;
784                     if (pme->resolve_attempts == 0)
785                         vec_add1 (resolution_failures,
786                                   pr - vam->pending_routes);
787                 }
788
789             }));
790             for (i = 0; i < vec_len (resolution_failures); i++) {
791                 pr = pool_elt_at_index (vam->pending_routes,
792                                         resolution_failures[i]);
793                 adr = &pr->r;
794                 pme = &pr->t;
795
796                 switch (pr->resolve_type) {
797                 case RESOLVE_IP4_ADD_DEL_ROUTE:
798                     clib_warning ("resolver: add %U/%d via %U retry failure",
799                                   format_ip4_address,
800                                   (ip4_address_t *)&(adr->dst_address),
801                                   adr->dst_address_length,
802                                   format_ip4_address,
803                                   (ip4_address_t *)&(adr->next_hop_address));
804                     break;
805
806                 case RESOLVE_IP6_ADD_DEL_ROUTE:
807                     clib_warning ("resolver: add %U/%d via %U retry failure",
808                                   format_ip6_address,
809                                   (ip6_address_t *)&(adr->dst_address),
810                                   adr->dst_address_length,
811                                   format_ip6_address,
812                                   (ip6_address_t *)&(adr->next_hop_address));
813                     break;
814
815                 case RESOLVE_MPLS_ETHERNET_ADD_DEL:
816                     clib_warning ("resolver: add mpls-o-e via %U retry failure",
817                                   format_ip4_address,
818                    (ip4_address_t *)&(pme->next_hop_ip4_address_in_outer_vrf));
819                     break;
820
821                 default:
822                     clib_warning ("BUG");
823                 }
824                 pool_put(vam->pending_routes, pr);
825             }
826             vec_reset_length (resolution_failures);
827             break;
828         }
829         if (pool_elts (vam->pending_routes) == 0)
830             timeout = 100.0;
831         vec_reset_length (event_data);
832     }
833     return 0; /* or not */
834 }
835
836 VLIB_REGISTER_NODE (vpe_resolver_process_node,static) = {
837     .function = resolver_process,
838     .type = VLIB_NODE_TYPE_PROCESS,
839     .name = "vpe-route-resolver-process",
840 };
841
842 static int ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t *mp)
843 {
844     ip4_main_t * im = &ip4_main;
845     ip_lookup_main_t * lm = &im->lookup_main;
846     vnet_classify_main_t * cm = &vnet_classify_main;
847     stats_main_t * sm = &stats_main;
848     ip4_add_del_route_args_t a;
849     ip4_address_t next_hop_address;
850     u32 fib_index;
851     vpe_api_main_t * vam = &vpe_api_main;
852     vnet_main_t * vnm = vam->vnet_main;
853     vlib_main_t * vm = vlib_get_main();
854     pending_route_t * pr;
855     vl_api_ip_add_del_route_t * adr;
856     uword * p;
857     clib_error_t * e;
858     u32 ai;
859     ip_adjacency_t *nh_adj, *add_adj = 0;
860
861     p = hash_get (im->fib_index_by_table_id, ntohl(mp->vrf_id));
862     if (!p) {
863         if (mp->create_vrf_if_needed) {
864             ip4_fib_t * f;
865             f = find_ip4_fib_by_table_index_or_id (im, ntohl(mp->vrf_id),
866                                                    0 /* flags */);
867             fib_index = f->index;
868         } else {
869             /* No such VRF, and we weren't asked to create one */
870             return VNET_API_ERROR_NO_SUCH_FIB;
871         }
872     } else {
873         fib_index = p[0];
874     }
875
876     if (pool_is_free_index (vnm->interface_main.sw_interfaces,
877                             ntohl(mp->next_hop_sw_if_index)))
878         return VNET_API_ERROR_NO_MATCHING_INTERFACE;
879
880     clib_memcpy (next_hop_address.data, mp->next_hop_address,
881             sizeof (next_hop_address.data));
882
883     /* Arp for the next_hop if necessary */
884     if (mp->is_add && mp->resolve_if_needed) {
885         u32 lookup_result;
886         ip_adjacency_t * adj;
887
888         lookup_result = ip4_fib_lookup_with_table
889             (im, fib_index, &next_hop_address, 1 /* disable default route */);
890
891         adj = ip_get_adjacency (lm, lookup_result);
892
893         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP) {
894             pool_get (vam->pending_routes, pr);
895             pr->resolve_type = RESOLVE_IP4_ADD_DEL_ROUTE;
896             adr = &pr->r;
897             clib_memcpy (adr, mp, sizeof (*adr));
898             /* recursion block, "just in case" */
899             adr->resolve_if_needed = 0;
900             adr->resolve_attempts = ntohl(mp->resolve_attempts);
901             vnet_register_ip4_arp_resolution_event
902                 (vnm, &next_hop_address, vpe_resolver_process_node.index,
903                  RESOLUTION_EVENT, pr - vam->pending_routes);
904
905             vlib_process_signal_event
906                 (vm, vpe_resolver_process_node.index,
907                  RESOLUTION_PENDING_EVENT, 0 /* data */);
908
909             /* The interface may be down, etc. */
910             e = ip4_probe_neighbor
911                 (vm, (ip4_address_t *)&(mp->next_hop_address),
912                  ntohl(mp->next_hop_sw_if_index));
913
914             if (e)
915                 clib_error_report(e);
916
917             return VNET_API_ERROR_IN_PROGRESS;
918         }
919     }
920
921     if (mp->is_multipath) {
922         u32 flags;
923
924         dslock (sm, 1 /* release hint */, 10 /* tag */);
925
926         if (mp->is_add)
927            flags = IP4_ROUTE_FLAG_ADD;
928         else
929            flags = IP4_ROUTE_FLAG_DEL;
930
931         if (mp->not_last)
932             flags |= IP4_ROUTE_FLAG_NOT_LAST_IN_GROUP;
933
934         ip4_add_del_route_next_hop (im, flags,
935                                     (ip4_address_t *) mp->dst_address,
936                                     (u32) mp->dst_address_length,
937                                     (ip4_address_t *) mp->next_hop_address,
938                                     ntohl(mp->next_hop_sw_if_index),
939                                     (u32) mp->next_hop_weight,
940                                     ~0 /* adj_index */,
941                                     fib_index);
942         dsunlock(sm);
943         return 0;
944     }
945
946     memset (&a, 0, sizeof (a));
947     clib_memcpy (a.dst_address.data, mp->dst_address, sizeof (a.dst_address.data));
948
949     a.dst_address_length = mp->dst_address_length;
950
951     a.flags = (mp->is_add ? IP4_ROUTE_FLAG_ADD : IP4_ROUTE_FLAG_DEL);
952     a.flags |= IP4_ROUTE_FLAG_FIB_INDEX;
953     a.table_index_or_table_id = fib_index;
954     a.add_adj = 0;
955     a.n_add_adj = 0;
956
957     if (mp->not_last)
958         a.flags |= IP4_ROUTE_FLAG_NOT_LAST_IN_GROUP;
959
960     dslock (sm, 1 /* release hint */, 2 /* tag */);
961
962     if (mp->is_add) {
963         if (mp->is_drop)
964             ai = lm->drop_adj_index;
965         else if (mp->is_local)
966             ai = lm->local_adj_index;
967         else if (mp->is_classify) {
968             ip_adjacency_t cadj;
969             memset(&cadj, 0, sizeof(cadj));
970             cadj.lookup_next_index = IP_LOOKUP_NEXT_CLASSIFY;
971             cadj.classify.table_index = ntohl(mp->classify_table_index);
972             if (pool_is_free_index (cm->tables, cadj.classify.table_index)) {
973                 dsunlock(sm);
974                 return VNET_API_ERROR_NO_SUCH_TABLE;
975             }
976             vec_add1 (add_adj, cadj);
977             goto do_add_del;
978         }
979         else {
980             ai = ip4_fib_lookup_with_table
981                 (im, fib_index, &next_hop_address,
982                  1 /* disable default route */);
983             if (ai == lm->miss_adj_index) {
984                 dsunlock(sm);
985                 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
986             }
987         }
988
989         nh_adj = ip_get_adjacency (lm, ai);
990         if (nh_adj->lookup_next_index == IP_LOOKUP_NEXT_ARP &&
991             nh_adj->arp.next_hop.ip4.as_u32 == 0) {
992             /* the next-hop resovles via a glean adj. create and use
993              * a ARP adj for the next-hop */
994             a.adj_index = vnet_arp_glean_add(fib_index, &next_hop_address);
995             a.add_adj = NULL;
996             a.n_add_adj = 0;
997             ip4_add_del_route (im, &a);
998
999             goto done;
1000         }
1001         vec_add1 (add_adj, nh_adj[0]);
1002         if (mp->lookup_in_vrf) {
1003             p = hash_get (im->fib_index_by_table_id, ntohl(mp->lookup_in_vrf));
1004             if (p)
1005                 add_adj[0].explicit_fib_index = p[0];
1006             else {
1007                 vec_free (add_adj);
1008                 dsunlock(sm);
1009                 return VNET_API_ERROR_NO_SUCH_INNER_FIB;
1010             }
1011         }
1012     } else {
1013         ip_adjacency_t * adj;
1014         int disable_default_route = 1;
1015
1016         /* Trying to delete the default route? */
1017         if (a.dst_address.as_u32 == 0 &&
1018             a.dst_address_length == 0)
1019             disable_default_route = 0;
1020
1021         ai = ip4_fib_lookup_with_table
1022             (im, fib_index, &a.dst_address, disable_default_route);
1023         if (ai == lm->miss_adj_index) {
1024             dsunlock(sm);
1025             return VNET_API_ERROR_UNKNOWN_DESTINATION;
1026         }
1027
1028         adj = ip_get_adjacency (lm, ai);
1029         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP) {
1030             dsunlock(sm);
1031             return VNET_API_ERROR_ADDRESS_MATCHES_INTERFACE_ADDRESS;
1032         }
1033     }
1034
1035 do_add_del:
1036     a.adj_index = ~0;
1037     a.add_adj = add_adj;
1038     a.n_add_adj = vec_len(add_adj);
1039     ip4_add_del_route (im, &a);
1040
1041     vec_free (add_adj);
1042
1043 done:
1044     dsunlock (sm);
1045     return 0;
1046 }
1047
1048 static int ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t *mp)
1049 {
1050     ip6_main_t * im = &ip6_main;
1051     ip_lookup_main_t * lm = &im->lookup_main;
1052     vnet_main_t * vnm = vnet_get_main();
1053     vlib_main_t * vm = vlib_get_main();
1054     vpe_api_main_t * vam = &vpe_api_main;
1055     stats_main_t * sm = &stats_main;
1056     ip6_add_del_route_args_t a;
1057     ip6_address_t next_hop_address;
1058     pending_route_t * pr;
1059     vl_api_ip_add_del_route_t * adr;
1060
1061     u32 fib_index;
1062     uword * p;
1063     clib_error_t * e;
1064     ip_adjacency_t * nh_adj, * add_adj = 0;
1065     u32 ai;
1066
1067     p = hash_get (im->fib_index_by_table_id, ntohl(mp->vrf_id));
1068
1069     if (!p) {
1070         if (mp->create_vrf_if_needed) {
1071             ip6_fib_t * f;
1072             f = find_ip6_fib_by_table_index_or_id (im, ntohl(mp->vrf_id),
1073                                                    0 /* flags */);
1074             fib_index = f->index;
1075         } else {
1076             /* No such VRF, and we weren't asked to create one */
1077             return VNET_API_ERROR_NO_SUCH_FIB;
1078         }
1079     } else {
1080         fib_index = p[0];
1081     }
1082
1083     if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1084                             ntohl(mp->next_hop_sw_if_index)))
1085         return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1086
1087     clib_memcpy (next_hop_address.as_u8, mp->next_hop_address,
1088             sizeof (next_hop_address.as_u8));
1089
1090     /* Arp for the next_hop if necessary */
1091     if (mp->is_add && mp->resolve_if_needed) {
1092         u32 lookup_result;
1093         ip_adjacency_t * adj;
1094
1095         lookup_result = ip6_fib_lookup_with_table
1096             (im, fib_index, &next_hop_address);
1097
1098         adj = ip_get_adjacency (lm, lookup_result);
1099
1100         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP) {
1101             pool_get (vam->pending_routes, pr);
1102             adr = &pr->r;
1103             pr->resolve_type = RESOLVE_IP6_ADD_DEL_ROUTE;
1104             clib_memcpy (adr, mp, sizeof (*adr));
1105             /* recursion block, "just in case" */
1106             adr->resolve_if_needed = 0;
1107             adr->resolve_attempts = ntohl(mp->resolve_attempts);
1108             vnet_register_ip6_neighbor_resolution_event
1109                 (vnm, &next_hop_address, vpe_resolver_process_node.index,
1110                  RESOLUTION_EVENT, pr - vam->pending_routes);
1111
1112             vlib_process_signal_event
1113                 (vm, vpe_resolver_process_node.index,
1114                  RESOLUTION_PENDING_EVENT, 0 /* data */);
1115
1116             /* The interface may be down, etc. */
1117             e = ip6_probe_neighbor
1118                 (vm, (ip6_address_t *)&(mp->next_hop_address),
1119                  ntohl(mp->next_hop_sw_if_index));
1120
1121             if (e)
1122                 clib_error_report(e);
1123
1124             return VNET_API_ERROR_IN_PROGRESS;
1125         }
1126     }
1127
1128     if (mp->is_multipath) {
1129         u32 flags;
1130
1131         dslock (sm, 1 /* release hint */, 11 /* tag */);
1132
1133         if (mp->is_add)
1134             flags = IP6_ROUTE_FLAG_ADD;
1135         else
1136             flags = IP6_ROUTE_FLAG_DEL;
1137
1138         if (mp->not_last)
1139             flags |= IP6_ROUTE_FLAG_NOT_LAST_IN_GROUP;
1140
1141         ip6_add_del_route_next_hop (im, flags, (ip6_address_t *)mp->dst_address,
1142                                     (u32) mp->dst_address_length,
1143                                     (ip6_address_t *)mp->next_hop_address,
1144                                     ntohl(mp->next_hop_sw_if_index),
1145                                     (u32) mp->next_hop_weight,
1146                                     ~0 /* adj_index */,
1147                                     fib_index);
1148         dsunlock(sm);
1149         return 0;
1150     }
1151
1152     memset (&a, 0, sizeof (a));
1153     clib_memcpy (a.dst_address.as_u8, mp->dst_address, sizeof (a.dst_address.as_u8));
1154
1155     a.dst_address_length = mp->dst_address_length;
1156
1157     a.flags = (mp->is_add ? IP6_ROUTE_FLAG_ADD : IP6_ROUTE_FLAG_DEL);
1158     a.flags |= IP6_ROUTE_FLAG_FIB_INDEX;
1159     a.table_index_or_table_id = fib_index;
1160     a.add_adj = 0;
1161     a.n_add_adj = 0;
1162
1163     if (mp->not_last)
1164         a.flags |= IP6_ROUTE_FLAG_NOT_LAST_IN_GROUP;
1165
1166     dslock (sm, 1 /* release hint */, 3 /* tag */);
1167
1168     if (mp->is_add) {
1169         if (mp->is_drop)
1170             ai = lm->drop_adj_index;
1171         else if (mp->is_local)
1172             ai = lm->local_adj_index;
1173         else {
1174             ai = ip6_fib_lookup_with_table
1175                 (im, fib_index, &next_hop_address);
1176             if (ai == lm->miss_adj_index) {
1177                 dsunlock(sm);
1178                 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
1179             }
1180         }
1181
1182         nh_adj = ip_get_adjacency (lm, ai);
1183         vec_add1 (add_adj, nh_adj[0]);
1184         if (mp->lookup_in_vrf) {
1185             p = hash_get (im->fib_index_by_table_id, ntohl(mp->lookup_in_vrf));
1186             if (p)
1187                 add_adj[0].explicit_fib_index = p[0];
1188             else {
1189                 vec_free (add_adj);
1190                 dsunlock(sm);
1191                 return VNET_API_ERROR_NO_SUCH_INNER_FIB;
1192             }
1193         }
1194     } else {
1195         ip_adjacency_t * adj;
1196
1197         ai = ip6_fib_lookup_with_table
1198             (im, fib_index, &a.dst_address);
1199         if (ai == lm->miss_adj_index) {
1200             dsunlock(sm);
1201             return VNET_API_ERROR_UNKNOWN_DESTINATION;
1202         }
1203         adj = ip_get_adjacency (lm, ai);
1204         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP) {
1205             dsunlock(sm);
1206             return VNET_API_ERROR_ADDRESS_MATCHES_INTERFACE_ADDRESS;
1207         }
1208     }
1209
1210     a.adj_index = ~0;
1211     a.add_adj = add_adj;
1212     a.n_add_adj = vec_len(add_adj);
1213     ip6_add_del_route (im, &a);
1214
1215     vec_free (add_adj);
1216
1217     dsunlock (sm);
1218     return 0;
1219 }
1220
1221 void vl_api_ip_add_del_route_t_handler (
1222     vl_api_ip_add_del_route_t *mp)
1223 {
1224     vl_api_ip_add_del_route_reply_t * rmp;
1225     int rv;
1226     vnet_main_t * vnm = vnet_get_main();
1227
1228     vnm->api_errno = 0;
1229
1230     if (mp->is_ipv6)
1231         rv = ip6_add_del_route_t_handler (mp);
1232     else
1233         rv = ip4_add_del_route_t_handler (mp);
1234
1235     rv = (rv == 0) ? vnm->api_errno : rv;
1236
1237     REPLY_MACRO(VL_API_IP_ADD_DEL_ROUTE_REPLY);
1238 }
1239
1240 void api_config_default_ip_route (u8 is_ipv6, u8 is_add, u32 vrf_id,
1241                                   u32 sw_if_index, u8 *next_hop_addr)
1242 {
1243     vl_api_ip_add_del_route_t mp;
1244     int rv;
1245
1246     memset (&mp, 0, sizeof(vl_api_ip_add_del_route_t));
1247
1248     /*
1249      * Configure default IP route:
1250      *  - ip route add 0.0.0.0/1 via <GW IP>
1251      *  - ip route add 128.0.0.0/1 via <GW IP>
1252      */
1253     mp.next_hop_sw_if_index = ntohl(sw_if_index);
1254     mp.vrf_id = vrf_id;
1255     mp.resolve_attempts = ~0;
1256     mp.resolve_if_needed = 1;
1257     mp.is_add = is_add;
1258     mp.is_ipv6 = is_ipv6;
1259     mp.next_hop_weight = 1;
1260
1261     clib_memcpy (&mp.next_hop_address[0], next_hop_addr, 16);
1262
1263     if (is_ipv6)
1264         rv = ip6_add_del_route_t_handler (&mp);
1265     else
1266       {
1267         mp.dst_address_length = 1;
1268
1269         mp.dst_address[0] = 0;
1270         rv = ip4_add_del_route_t_handler (&mp);
1271
1272         mp.dst_address[0] = 128;
1273         rv |= ip4_add_del_route_t_handler (&mp);
1274       }
1275
1276     if (rv)
1277         clib_error_return (0, "failed to config default IP route");
1278
1279 }
1280
1281 static void
1282 vl_api_sw_interface_add_del_address_t_handler
1283 (vl_api_sw_interface_add_del_address_t *mp)
1284 {
1285     vlib_main_t *vm = vlib_get_main();
1286     vl_api_sw_interface_add_del_address_reply_t * rmp;
1287     int rv = 0;
1288     u32 is_del;
1289
1290     VALIDATE_SW_IF_INDEX(mp);
1291
1292     is_del = mp->is_add == 0;
1293
1294     if (mp->del_all)
1295         ip_del_all_interface_addresses (vm, ntohl(mp->sw_if_index));
1296     else if (mp->is_ipv6)
1297         ip6_add_del_interface_address (vm, ntohl(mp->sw_if_index),
1298                                        (void *)mp->address,
1299                                        mp->address_length, is_del);
1300     else
1301         ip4_add_del_interface_address (vm, ntohl(mp->sw_if_index),
1302                                        (void *) mp->address,
1303                                        mp->address_length, is_del);
1304
1305     BAD_SW_IF_INDEX_LABEL;
1306
1307     REPLY_MACRO(VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
1308 }
1309
1310 static void
1311 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t *mp)
1312 {
1313     int rv = 0;
1314     u32 table_id = ntohl(mp->vrf_id);
1315     u32 sw_if_index = ntohl(mp->sw_if_index);
1316     vl_api_sw_interface_set_table_reply_t * rmp;
1317     stats_main_t * sm = &stats_main;
1318
1319     VALIDATE_SW_IF_INDEX(mp);
1320
1321     dslock (sm, 1 /* release hint */, 4 /* tag */);
1322
1323     if (mp->is_ipv6) {
1324         ip6_main_t * im = &ip6_main;
1325         ip6_fib_t * fib =
1326             find_ip6_fib_by_table_index_or_id (im, table_id,
1327                                                IP6_ROUTE_FLAG_TABLE_ID);
1328         if (fib) {
1329             vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1330             im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
1331         } else {
1332             rv = VNET_API_ERROR_NO_SUCH_FIB;
1333         }
1334     } else {
1335         ip4_main_t * im = &ip4_main;
1336         ip4_fib_t * fib = find_ip4_fib_by_table_index_or_id
1337             (im, table_id, IP4_ROUTE_FLAG_TABLE_ID);
1338
1339         /* Truthfully this can't fail */
1340         if (fib) {
1341             vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1342             im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
1343         } else {
1344             rv = VNET_API_ERROR_NO_SUCH_FIB;
1345         }
1346     }
1347     dsunlock(sm);
1348
1349     BAD_SW_IF_INDEX_LABEL;
1350
1351     REPLY_MACRO(VL_API_SW_INTERFACE_SET_TABLE_REPLY);
1352 }
1353
1354 static void
1355 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t *mp)
1356 {
1357     vlib_main_t *vm = vlib_get_main();
1358     ip4_main_t * im4 = &ip4_main;
1359     ip6_main_t * im6 = &ip6_main;
1360     vl_api_sw_interface_set_vpath_reply_t * rmp;
1361     int rv = 0;
1362     u32 ci;
1363     u32 sw_if_index = ntohl(mp->sw_if_index);
1364     ip4_main_t   *ip4m = &ip4_main;
1365     ip6_main_t   *ip6m = &ip6_main;
1366     ip_lookup_main_t *ip4lm = &ip4m->lookup_main;
1367     ip_lookup_main_t *ip6lm = &ip6m->lookup_main;
1368     ip_config_main_t *rx_cm4u = &ip4lm->rx_config_mains[VNET_UNICAST];
1369     ip_config_main_t *rx_cm4m = &ip4lm->rx_config_mains[VNET_MULTICAST];
1370     ip_config_main_t *rx_cm6u = &ip6lm->rx_config_mains[VNET_UNICAST];
1371     ip_config_main_t *rx_cm6m = &ip6lm->rx_config_mains[VNET_MULTICAST];
1372
1373     VALIDATE_SW_IF_INDEX(mp);
1374
1375     l2input_intf_bitmap_enable(sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
1376     if (mp->enable) {
1377         ci = rx_cm4u->config_index_by_sw_if_index[sw_if_index]; //IP4 unicast
1378         ci = vnet_config_add_feature(vm, &rx_cm4u->config_main,
1379                                      ci, 
1380                                      im4->ip4_unicast_rx_feature_vpath,
1381                                      0, 0);
1382         rx_cm4u->config_index_by_sw_if_index[sw_if_index] = ci;
1383         ci = rx_cm4m->config_index_by_sw_if_index[sw_if_index]; //IP4 mcast
1384         ci = vnet_config_add_feature(vm, &rx_cm4m->config_main,
1385                                      ci, 
1386                                      im4->ip4_multicast_rx_feature_vpath,
1387                                      0, 0);
1388         rx_cm4m->config_index_by_sw_if_index[sw_if_index] = ci;
1389         ci = rx_cm6u->config_index_by_sw_if_index[sw_if_index]; //IP6 unicast
1390         ci = vnet_config_add_feature(vm, &rx_cm6u->config_main,
1391                                      ci, 
1392                                      im6->ip6_unicast_rx_feature_vpath,
1393                                      0, 0);
1394         rx_cm6u->config_index_by_sw_if_index[sw_if_index] = ci;
1395         ci = rx_cm6m->config_index_by_sw_if_index[sw_if_index]; //IP6 mcast
1396         ci = vnet_config_add_feature(vm, &rx_cm6m->config_main,
1397                                      ci, 
1398                                      im6->ip6_multicast_rx_feature_vpath,
1399                                      0, 0);
1400         rx_cm6m->config_index_by_sw_if_index[sw_if_index] = ci;
1401     } else {
1402         ci = rx_cm4u->config_index_by_sw_if_index[sw_if_index]; //IP4 unicast
1403         ci = vnet_config_del_feature(vm, &rx_cm4u->config_main,
1404                                      ci, 
1405                                      im4->ip4_unicast_rx_feature_vpath,
1406                                      0, 0);
1407         rx_cm4u->config_index_by_sw_if_index[sw_if_index] = ci;
1408         ci = rx_cm4m->config_index_by_sw_if_index[sw_if_index]; //IP4 mcast
1409         ci = vnet_config_del_feature(vm, &rx_cm4m->config_main,
1410                                      ci, 
1411                                      im4->ip4_multicast_rx_feature_vpath,
1412                                      0, 0);
1413         rx_cm4m->config_index_by_sw_if_index[sw_if_index] = ci;
1414         ci = rx_cm6u->config_index_by_sw_if_index[sw_if_index]; //IP6 unicast
1415         ci = vnet_config_del_feature(vm, &rx_cm6u->config_main,
1416                                      ci, 
1417                                      im6->ip6_unicast_rx_feature_vpath,
1418                                      0, 0);
1419         rx_cm6u->config_index_by_sw_if_index[sw_if_index] = ci;
1420         ci = rx_cm6m->config_index_by_sw_if_index[sw_if_index]; //IP6 mcast
1421         ci = vnet_config_del_feature(vm, &rx_cm6m->config_main,
1422                                      ci, 
1423                                      im6->ip6_multicast_rx_feature_vpath,
1424                                      0, 0);
1425         rx_cm6m->config_index_by_sw_if_index[sw_if_index] = ci;
1426     }
1427
1428     BAD_SW_IF_INDEX_LABEL;
1429
1430     REPLY_MACRO(VL_API_SW_INTERFACE_SET_VPATH_REPLY);
1431 }
1432
1433 static void
1434 vl_api_sw_interface_set_l2_xconnect_t_handler (
1435     vl_api_sw_interface_set_l2_xconnect_t *mp)
1436 {
1437     vl_api_sw_interface_set_l2_xconnect_reply_t * rmp;
1438     int rv = 0;
1439     u32 rx_sw_if_index = ntohl(mp->rx_sw_if_index);
1440     u32 tx_sw_if_index = ntohl(mp->tx_sw_if_index);
1441     vlib_main_t *vm  = vlib_get_main();
1442     vnet_main_t *vnm = vnet_get_main();
1443
1444     VALIDATE_RX_SW_IF_INDEX(mp);
1445
1446     if (mp->enable) {
1447         VALIDATE_TX_SW_IF_INDEX(mp);
1448         rv = set_int_l2_mode(vm, vnm, MODE_L2_XC,
1449                              rx_sw_if_index, 0, 0, 0, tx_sw_if_index);
1450     } else {
1451         rv = set_int_l2_mode(vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
1452     }
1453
1454     BAD_RX_SW_IF_INDEX_LABEL;
1455     BAD_TX_SW_IF_INDEX_LABEL;
1456
1457     REPLY_MACRO(VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
1458 }
1459
1460 static void
1461 vl_api_sw_interface_set_l2_bridge_t_handler (
1462     vl_api_sw_interface_set_l2_bridge_t *mp)
1463 {
1464     bd_main_t * bdm = &bd_main;
1465     vl_api_sw_interface_set_l2_bridge_reply_t * rmp;
1466     int rv = 0;
1467     u32 rx_sw_if_index = ntohl(mp->rx_sw_if_index);
1468     u32 bd_id = ntohl(mp->bd_id);
1469     u32 bd_index;
1470     u32 bvi = mp->bvi;
1471     u8 shg = mp->shg;
1472     vlib_main_t *vm  = vlib_get_main();
1473     vnet_main_t *vnm = vnet_get_main();
1474
1475     VALIDATE_RX_SW_IF_INDEX(mp);
1476
1477     bd_index = bd_find_or_add_bd_index (bdm, bd_id);
1478
1479     if (mp->enable) {
1480         //VALIDATE_TX_SW_IF_INDEX(mp);
1481         rv = set_int_l2_mode(vm, vnm, MODE_L2_BRIDGE,
1482                              rx_sw_if_index, bd_index, bvi, shg, 0);
1483     } else {
1484         rv = set_int_l2_mode(vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
1485     }
1486
1487     BAD_RX_SW_IF_INDEX_LABEL;
1488
1489     REPLY_MACRO(VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
1490 }
1491
1492 static void
1493 vl_api_bridge_domain_add_del_t_handler (
1494     vl_api_bridge_domain_add_del_t *mp)
1495 {
1496     vlib_main_t * vm = vlib_get_main ();
1497     bd_main_t * bdm = &bd_main;
1498     vl_api_bridge_domain_add_del_reply_t * rmp;
1499     int rv = 0;
1500     u32 enable_flags = 0, disable_flags = 0;
1501     u32 bd_id = ntohl(mp->bd_id);
1502     u32 bd_index;
1503
1504     if (mp->is_add) {
1505         bd_index = bd_find_or_add_bd_index (bdm, bd_id);
1506
1507         if (mp->flood)
1508             enable_flags |= L2_FLOOD;
1509         else
1510             disable_flags |= L2_FLOOD;
1511
1512         if (mp->uu_flood)
1513             enable_flags |= L2_UU_FLOOD;
1514         else
1515             disable_flags |= L2_UU_FLOOD;
1516
1517         if (mp->forward)
1518             enable_flags |= L2_FWD;
1519         else
1520             disable_flags |= L2_FWD;
1521
1522         if (mp->arp_term)
1523             enable_flags |= L2_ARP_TERM;
1524         else
1525             disable_flags |= L2_ARP_TERM;
1526
1527         if (mp->learn)
1528             enable_flags |= L2_LEARN;
1529         else
1530             disable_flags |= L2_LEARN;
1531
1532         if (enable_flags)
1533             bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */);
1534
1535         if (disable_flags)
1536             bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */);
1537
1538     } else
1539         rv = bd_delete_bd_index(bdm, bd_id);
1540
1541     REPLY_MACRO(VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
1542 }
1543
1544 static void vl_api_bridge_domain_details_t_handler (
1545     vl_api_bridge_domain_details_t * mp)
1546 {
1547     clib_warning ("BUG");
1548 }
1549
1550 static void vl_api_bridge_domain_sw_if_details_t_handler (
1551     vl_api_bridge_domain_sw_if_details_t * mp)
1552 {
1553     clib_warning ("BUG");
1554 }
1555
1556 static void send_bridge_domain_details (unix_shared_memory_queue_t *q,
1557                                         l2_bridge_domain_t * bd_config,
1558                                         u32 n_sw_ifs,
1559                                         u32 context)
1560 {
1561     vl_api_bridge_domain_details_t * mp;
1562
1563     mp = vl_msg_api_alloc (sizeof (*mp));
1564     memset (mp, 0, sizeof (*mp));
1565     mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
1566     mp->bd_id = ntohl (bd_config->bd_id);
1567     mp->flood = bd_feature_flood (bd_config);
1568     mp->uu_flood = bd_feature_uu_flood (bd_config);
1569     mp->forward = bd_feature_forward (bd_config);
1570     mp->learn = bd_feature_learn (bd_config);
1571     mp->arp_term = bd_feature_arp_term (bd_config);
1572     mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
1573     mp->n_sw_ifs = ntohl (n_sw_ifs);
1574     mp->context = context;
1575
1576     vl_msg_api_send_shmem (q, (u8 *)&mp);
1577 }
1578
1579 static void send_bd_sw_if_details (l2input_main_t * l2im,
1580                                    unix_shared_memory_queue_t *q,
1581                                    l2_flood_member_t * member, u32 bd_id,
1582                                    u32 context)
1583 {
1584     vl_api_bridge_domain_sw_if_details_t * mp;
1585     l2_input_config_t * input_cfg;
1586
1587     mp = vl_msg_api_alloc (sizeof (*mp));
1588     memset (mp, 0, sizeof (*mp));
1589     mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_SW_IF_DETAILS);
1590     mp->bd_id = ntohl (bd_id);
1591     mp->sw_if_index = ntohl (member->sw_if_index);
1592     input_cfg = vec_elt_at_index (l2im->configs, member->sw_if_index);
1593     mp->shg = input_cfg->shg;
1594     mp->context = context;
1595
1596     vl_msg_api_send_shmem (q, (u8 *)&mp);
1597 }
1598
1599 static void vl_api_bridge_domain_dump_t_handler (
1600     vl_api_bridge_domain_dump_t *mp)
1601 {
1602     bd_main_t * bdm = &bd_main;
1603     l2input_main_t * l2im = &l2input_main;
1604     unix_shared_memory_queue_t * q;
1605     l2_bridge_domain_t * bd_config;
1606     u32 bd_id, bd_index;
1607     u32 end;
1608
1609     q = vl_api_client_index_to_input_queue (mp->client_index);
1610
1611     if (q == 0)
1612         return;
1613
1614     bd_id = ntohl(mp->bd_id);
1615
1616     bd_index = (bd_id == ~0) ? 0 : bd_find_or_add_bd_index (bdm, bd_id);
1617     end = (bd_id == ~0) ? vec_len (l2im->bd_configs) : bd_index + 1;
1618     for (; bd_index < end; bd_index++) {
1619         bd_config = l2input_bd_config_from_index (l2im, bd_index);
1620         /* skip dummy bd_id 0 */
1621         if (bd_config && (bd_config->bd_id > 0)) {
1622             u32 n_sw_ifs;
1623             l2_flood_member_t * m;
1624
1625             n_sw_ifs = vec_len (bd_config->members);
1626             send_bridge_domain_details (q, bd_config, n_sw_ifs, mp->context);
1627
1628             vec_foreach (m, bd_config->members) {
1629                 send_bd_sw_if_details (l2im, q, m, bd_config->bd_id, mp->context);
1630             }
1631         }
1632     }
1633 }
1634
1635 static void
1636 vl_api_l2fib_add_del_t_handler (
1637     vl_api_l2fib_add_del_t *mp)
1638 {
1639     bd_main_t * bdm = &bd_main;
1640     l2input_main_t * l2im = &l2input_main;
1641     vl_api_l2fib_add_del_reply_t * rmp;
1642     int rv = 0;
1643     u64 mac = 0;
1644     u32 sw_if_index = ntohl(mp->sw_if_index);
1645     u32 bd_id = ntohl(mp->bd_id);
1646     u32 bd_index;
1647     u32 static_mac;
1648     u32 filter_mac;
1649     uword * p;
1650
1651     mac = mp->mac;
1652
1653     p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1654     if (!p) {
1655         rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1656         goto bad_sw_if_index;
1657     }
1658     bd_index = p[0];
1659
1660     if (mp->is_add) {
1661         VALIDATE_SW_IF_INDEX(mp);
1662         if (vec_len(l2im->configs) <= sw_if_index) {
1663             rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1664             goto bad_sw_if_index;
1665         } else {
1666             l2_input_config_t * config;
1667             config = vec_elt_at_index(l2im->configs, sw_if_index);
1668             if (config->bridge == 0) {
1669                 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1670                 goto bad_sw_if_index;
1671             }
1672         }
1673         static_mac = mp->static_mac ? 1 : 0;
1674         filter_mac = mp->filter_mac ? 1 : 0;
1675         l2fib_add_entry(mac, bd_index, sw_if_index, static_mac, filter_mac,
1676                         0 /* bvi_mac */);
1677     } else {
1678         l2fib_del_entry(mac, bd_index);
1679     }
1680
1681     BAD_SW_IF_INDEX_LABEL;
1682
1683     REPLY_MACRO(VL_API_L2FIB_ADD_DEL_REPLY);
1684 }
1685
1686 static void
1687 vl_api_l2_flags_t_handler (
1688     vl_api_l2_flags_t *mp)
1689 {
1690     vl_api_l2_flags_reply_t * rmp;
1691     int rv = 0;
1692     u32 sw_if_index = ntohl(mp->sw_if_index);
1693     u32 flags = ntohl(mp->feature_bitmap);
1694     u32 rbm = 0;
1695
1696     VALIDATE_SW_IF_INDEX(mp);
1697
1698 #define _(a,b) \
1699     if (flags & L2INPUT_FEAT_ ## a) \
1700         rbm = l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_ ## a, mp->is_set);
1701     foreach_l2input_feat;
1702 #undef _
1703
1704     BAD_SW_IF_INDEX_LABEL;
1705
1706     REPLY_MACRO2(VL_API_L2_FLAGS_REPLY, rmp->resulting_feature_bitmap = ntohl(rbm));
1707 }
1708
1709 static void
1710 vl_api_bridge_flags_t_handler (
1711     vl_api_bridge_flags_t *mp)
1712 {
1713     vlib_main_t *vm  = vlib_get_main();
1714     bd_main_t * bdm = &bd_main;
1715     vl_api_bridge_flags_reply_t * rmp;
1716     int rv = 0;
1717     u32 bd_id = ntohl(mp->bd_id);
1718     u32 bd_index;
1719     u32 flags = ntohl(mp->feature_bitmap);
1720     uword * p;
1721
1722     p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1723     if (p == 0) {
1724         rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1725         goto out;
1726     }
1727
1728     bd_index = p[0];
1729
1730     bd_set_flags(vm, bd_index, flags, mp->is_set);
1731
1732 out:
1733     REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
1734                  rmp->resulting_feature_bitmap = ntohl(flags));
1735 }
1736
1737 static void
1738 vl_api_bd_ip_mac_add_del_t_handler (
1739     vl_api_bd_ip_mac_add_del_t *mp)
1740 {
1741     bd_main_t * bdm = &bd_main;
1742     vl_api_bd_ip_mac_add_del_reply_t * rmp;
1743     int rv = 0;
1744     u32 bd_id = ntohl(mp->bd_id);
1745     u32 bd_index;
1746     uword * p;
1747
1748     p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1749     if (p == 0) {
1750         rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1751         goto out;
1752     }
1753
1754     bd_index = p[0];
1755     if (bd_add_del_ip_mac(bd_index,  mp->ip_address,
1756                           mp->mac_address, mp->is_ipv6, mp->is_add))
1757         rv = VNET_API_ERROR_UNSPECIFIED;
1758
1759 out:
1760     REPLY_MACRO(VL_API_BD_IP_MAC_ADD_DEL_REPLY);
1761 }
1762
1763 static void
1764 vl_api_tap_connect_t_handler (vl_api_tap_connect_t *mp, vlib_main_t *vm)
1765 {
1766     int rv;
1767     vl_api_tap_connect_reply_t * rmp;
1768     unix_shared_memory_queue_t * q;
1769     u32 sw_if_index = (u32)~0;
1770
1771     rv = vnet_tap_connect_renumber (vm, mp->tap_name,
1772                            mp->use_random_mac ? 0 : mp->mac_address,
1773                            &sw_if_index, mp->renumber,
1774                            ntohl(mp->custom_dev_instance));
1775
1776     q = vl_api_client_index_to_input_queue (mp->client_index);
1777     if (!q)
1778         return;
1779
1780     rmp = vl_msg_api_alloc (sizeof (*rmp));
1781     rmp->_vl_msg_id = ntohs(VL_API_TAP_CONNECT_REPLY);
1782     rmp->context = mp->context;
1783     rmp->retval = ntohl(rv);
1784     rmp->sw_if_index = ntohl(sw_if_index);
1785
1786     vl_msg_api_send_shmem (q, (u8 *)&rmp);
1787 }
1788
1789 static void
1790 vl_api_tap_modify_t_handler (vl_api_tap_modify_t *mp, vlib_main_t *vm)
1791 {
1792     int rv;
1793     vl_api_tap_modify_reply_t * rmp;
1794     unix_shared_memory_queue_t * q;
1795     u32 sw_if_index = (u32)~0;
1796
1797     rv = vnet_tap_modify (vm, ntohl(mp->sw_if_index), mp->tap_name,
1798                            mp->use_random_mac ? 0 : mp->mac_address,
1799                            &sw_if_index, mp->renumber,
1800                            ntohl(mp->custom_dev_instance));
1801
1802     q = vl_api_client_index_to_input_queue (mp->client_index);
1803     if (!q)
1804         return;
1805
1806     rmp = vl_msg_api_alloc (sizeof (*rmp));
1807     rmp->_vl_msg_id = ntohs(VL_API_TAP_MODIFY_REPLY);
1808     rmp->context = mp->context;
1809     rmp->retval = ntohl(rv);
1810     rmp->sw_if_index = ntohl(sw_if_index);
1811
1812     vl_msg_api_send_shmem (q, (u8 *)&rmp);
1813 }
1814
1815 static void
1816 vl_api_tap_delete_t_handler (vl_api_tap_delete_t *mp, vlib_main_t *vm)
1817 {
1818     int rv;
1819     vpe_api_main_t * vam = &vpe_api_main;
1820     vl_api_tap_delete_reply_t * rmp;
1821     unix_shared_memory_queue_t * q;
1822     u32 sw_if_index = ntohl(mp->sw_if_index);
1823
1824     rv = vnet_tap_delete (vm, sw_if_index);
1825
1826     q = vl_api_client_index_to_input_queue (mp->client_index);
1827     if (!q)
1828         return;
1829
1830     rmp = vl_msg_api_alloc (sizeof (*rmp));
1831     rmp->_vl_msg_id = ntohs(VL_API_TAP_DELETE_REPLY);
1832     rmp->context = mp->context;
1833     rmp->retval = ntohl(rv);
1834
1835     vl_msg_api_send_shmem (q, (u8 *)&rmp);
1836
1837     if (!rv)
1838         send_sw_interface_flags_deleted (vam, q, sw_if_index);
1839 }
1840
1841 static void
1842 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
1843 {
1844     vl_api_create_vlan_subif_reply_t * rmp;
1845     vnet_main_t * vnm = vnet_get_main();
1846     u32 hw_if_index, sw_if_index = (u32)~0;
1847     vnet_hw_interface_t * hi;
1848     int rv = 0;
1849     u32 id;
1850     vnet_sw_interface_t template;
1851     uword * p;
1852     vnet_interface_main_t * im = &vnm->interface_main;
1853     u64 sup_and_sub_key;
1854     u64 * kp;
1855     unix_shared_memory_queue_t * q;
1856     clib_error_t * error;
1857
1858     VALIDATE_SW_IF_INDEX(mp);
1859
1860     hw_if_index = ntohl(mp->sw_if_index);
1861     hi = vnet_get_hw_interface (vnm, hw_if_index);
1862
1863     id = ntohl(mp->vlan_id);
1864     if (id == 0 || id > 4095) {
1865         rv = VNET_API_ERROR_INVALID_VLAN;
1866         goto out;
1867     }
1868
1869     sup_and_sub_key = ((u64)(hi->sw_if_index) << 32) | (u64) id;
1870
1871     p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1872     if (p) {
1873         rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
1874         goto out;
1875     }
1876
1877     kp = clib_mem_alloc (sizeof (*kp));
1878     *kp = sup_and_sub_key;
1879
1880     memset (&template, 0, sizeof (template));
1881     template.type = VNET_SW_INTERFACE_TYPE_SUB;
1882     template.sup_sw_if_index = hi->sw_if_index;
1883     template.sub.id = id;
1884     template.sub.eth.raw_flags = 0;
1885     template.sub.eth.flags.one_tag = 1;
1886     template.sub.eth.outer_vlan_id = id;
1887     template.sub.eth.flags.exact_match = 1;
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_INVALID_REGISTRATION;
1893         goto out;
1894     }
1895     hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
1896     hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1897
1898     BAD_SW_IF_INDEX_LABEL;
1899
1900 out:
1901     q = vl_api_client_index_to_input_queue (mp->client_index);
1902     if (!q)
1903         return;
1904
1905     rmp = vl_msg_api_alloc (sizeof (*rmp));
1906     rmp->_vl_msg_id = ntohs(VL_API_CREATE_VLAN_SUBIF_REPLY);
1907     rmp->context = mp->context;
1908     rmp->retval = ntohl(rv);
1909     rmp->sw_if_index = ntohl(sw_if_index);
1910     vl_msg_api_send_shmem (q, (u8 *)&rmp);
1911 }
1912
1913 static void
1914 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
1915 {
1916     vl_api_create_subif_reply_t * rmp;
1917     vnet_main_t * vnm = vnet_get_main();
1918     u32 sw_if_index = ~0;
1919     int rv = 0;
1920     u32 sub_id;
1921     vnet_sw_interface_t *si;
1922     vnet_hw_interface_t *hi;
1923     vnet_sw_interface_t template;
1924     uword * p;
1925     vnet_interface_main_t * im = &vnm->interface_main;
1926     u64 sup_and_sub_key;
1927     u64 * kp;
1928     clib_error_t * error;
1929
1930     VALIDATE_SW_IF_INDEX(mp);
1931
1932     si = vnet_get_sup_sw_interface (vnm, ntohl(mp->sw_if_index));
1933     hi = vnet_get_sup_hw_interface (vnm, ntohl(mp->sw_if_index));
1934
1935     if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE) {
1936          rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1937         goto out;
1938     }
1939
1940     sw_if_index = si->sw_if_index;
1941     sub_id = ntohl(mp->sub_id);
1942
1943     sup_and_sub_key = ((u64)(sw_if_index) << 32) | (u64) sub_id;
1944
1945     p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1946     if (p) {
1947         if (CLIB_DEBUG > 0)
1948             clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
1949                           sw_if_index, sub_id);
1950         rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
1951         goto out;
1952     }
1953
1954     kp = clib_mem_alloc (sizeof (*kp));
1955     *kp = sup_and_sub_key;
1956
1957     memset (&template, 0, sizeof (template));
1958     template.type = VNET_SW_INTERFACE_TYPE_SUB;
1959     template.sup_sw_if_index = sw_if_index;
1960     template.sub.id = sub_id;
1961     template.sub.eth.flags.no_tags = mp->no_tags;
1962     template.sub.eth.flags.one_tag = mp->one_tag;
1963     template.sub.eth.flags.two_tags = mp->two_tags;
1964     template.sub.eth.flags.dot1ad = mp->dot1ad;
1965     template.sub.eth.flags.exact_match = mp->exact_match;
1966     template.sub.eth.flags.default_sub = mp->default_sub;
1967     template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
1968     template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
1969     template.sub.eth.outer_vlan_id = ntohs(mp->outer_vlan_id);
1970     template.sub.eth.inner_vlan_id = ntohs(mp->inner_vlan_id);
1971
1972     error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1973     if (error) {
1974         clib_error_report (error);
1975         rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
1976         goto out;
1977     }
1978
1979     hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
1980     hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1981
1982     BAD_SW_IF_INDEX_LABEL;
1983
1984 out:
1985
1986     REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
1987     ({
1988         rmp->sw_if_index = ntohl(sw_if_index);
1989     }));
1990 }
1991
1992 static void
1993 vl_api_mpls_gre_add_del_tunnel_t_handler (vl_api_mpls_gre_add_del_tunnel_t *mp)
1994 {
1995     vl_api_mpls_gre_add_del_tunnel_reply_t * rmp;
1996     int rv = 0;
1997     stats_main_t * sm = &stats_main;
1998     u32 tunnel_sw_if_index = ~0;
1999
2000     dslock (sm, 1 /* release hint */, 5 /* tag */);
2001
2002     rv = vnet_mpls_gre_add_del_tunnel ((ip4_address_t *)(mp->src_address),
2003                                        (ip4_address_t *)(mp->dst_address),
2004                                        (ip4_address_t *)(mp->intfc_address),
2005                                        (u32)(mp->intfc_address_length),
2006                                        ntohl(mp->inner_vrf_id),
2007                                        ntohl(mp->outer_vrf_id),
2008                                        &tunnel_sw_if_index,
2009                                        mp->l2_only,
2010                                        mp->is_add);
2011     dsunlock (sm);
2012
2013     REPLY_MACRO2(VL_API_MPLS_GRE_ADD_DEL_TUNNEL_REPLY,
2014     ({
2015         rmp->tunnel_sw_if_index = ntohl(tunnel_sw_if_index);
2016     }));
2017 }
2018
2019 static void
2020 vl_api_mpls_ethernet_add_del_tunnel_t_handler
2021 (vl_api_mpls_ethernet_add_del_tunnel_t *mp)
2022 {
2023     vl_api_mpls_ethernet_add_del_tunnel_reply_t * rmp;
2024     int rv = 0;
2025     stats_main_t * sm = &stats_main;
2026     u32 tunnel_sw_if_index;
2027
2028     dslock (sm, 1 /* release hint */, 5 /* tag */);
2029
2030     rv = vnet_mpls_ethernet_add_del_tunnel
2031         (mp->dst_mac_address, (ip4_address_t *)(mp->adj_address),
2032          (u32)(mp->adj_address_length), ntohl(mp->vrf_id),
2033          ntohl(mp->tx_sw_if_index),
2034          &tunnel_sw_if_index,
2035          mp->l2_only,
2036          mp->is_add);
2037
2038     dsunlock (sm);
2039
2040     REPLY_MACRO2(VL_API_MPLS_ETHERNET_ADD_DEL_TUNNEL_REPLY,
2041     ({
2042         rmp->tunnel_sw_if_index = ntohl(tunnel_sw_if_index);
2043     }));
2044 }
2045
2046 /*
2047  * This piece of misery brought to you because the control-plane
2048  * can't figure out the tx interface + dst-mac address all by itself
2049  */
2050 static int mpls_ethernet_add_del_tunnel_2_t_handler
2051 (vl_api_mpls_ethernet_add_del_tunnel_2_t *mp)
2052 {
2053     pending_route_t * pr;
2054     vl_api_mpls_ethernet_add_del_tunnel_2_t *pme;
2055     vnet_main_t * vnm = vnet_get_main();
2056     vlib_main_t * vm = vlib_get_main();
2057     stats_main_t * sm = &stats_main;
2058     vpe_api_main_t * vam = &vpe_api_main;
2059     u32 inner_fib_index, outer_fib_index;
2060     ip4_main_t * im = &ip4_main;
2061     ip_lookup_main_t * lm = &im->lookup_main;
2062     ip_adjacency_t * adj = 0;
2063     u32 lookup_result;
2064     u32 tx_sw_if_index;
2065     u8 * dst_mac_address;
2066     clib_error_t * e;
2067     uword * p;
2068     int rv;
2069     u32 tunnel_sw_if_index;
2070
2071     p = hash_get (im->fib_index_by_table_id, ntohl(mp->outer_vrf_id));
2072     if (!p)
2073         return VNET_API_ERROR_NO_SUCH_FIB;
2074     else
2075         outer_fib_index = p[0];
2076
2077
2078     p = hash_get (im->fib_index_by_table_id, ntohl(mp->inner_vrf_id));
2079     if (!p)
2080         return VNET_API_ERROR_NO_SUCH_INNER_FIB;
2081     else
2082         inner_fib_index = p[0];
2083
2084     if (inner_fib_index == outer_fib_index)
2085         return VNET_API_ERROR_INVALID_VALUE;
2086
2087     lookup_result = ip4_fib_lookup_with_table
2088         (im, outer_fib_index,
2089          (ip4_address_t *)mp->next_hop_ip4_address_in_outer_vrf,
2090          1 /* disable default route */);
2091
2092     adj = ip_get_adjacency (lm, lookup_result);
2093     tx_sw_if_index = adj->rewrite_header.sw_if_index;
2094
2095     if (mp->is_add && mp->resolve_if_needed) {
2096         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP) {
2097             pool_get (vam->pending_routes, pr);
2098             pr->resolve_type = RESOLVE_MPLS_ETHERNET_ADD_DEL;
2099             pme = &pr->t;
2100             clib_memcpy (pme, mp, sizeof (*pme));
2101             /* recursion block, "just in case" */
2102             pme->resolve_if_needed = 0;
2103             pme->resolve_attempts = ntohl(mp->resolve_attempts);
2104             pme->resolve_opaque = tx_sw_if_index;
2105             vnet_register_ip4_arp_resolution_event
2106                 (vnm,
2107                  (ip4_address_t *)&(pme->next_hop_ip4_address_in_outer_vrf),
2108                  vpe_resolver_process_node.index,
2109                  RESOLUTION_EVENT, pr - vam->pending_routes);
2110
2111             vlib_process_signal_event
2112                 (vm, vpe_resolver_process_node.index,
2113                  RESOLUTION_PENDING_EVENT, 0 /* data */);
2114
2115             /* The interface may be down, etc. */
2116             e = ip4_probe_neighbor
2117                 (vm, (ip4_address_t *)&(mp->next_hop_ip4_address_in_outer_vrf),
2118                  tx_sw_if_index);
2119
2120             if (e)
2121                 clib_error_report(e);
2122
2123             return VNET_API_ERROR_IN_PROGRESS;
2124         }
2125     }
2126
2127     if (adj->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
2128         return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
2129
2130     dst_mac_address =
2131         vnet_rewrite_get_data_internal
2132         (&adj->rewrite_header, sizeof (adj->rewrite_data));
2133
2134     dslock (sm, 1 /* release hint */, 10 /* tag */);
2135
2136     rv = vnet_mpls_ethernet_add_del_tunnel
2137         (dst_mac_address, (ip4_address_t *)(mp->adj_address),
2138          (u32)(mp->adj_address_length), ntohl(mp->inner_vrf_id),
2139          tx_sw_if_index, &tunnel_sw_if_index, mp->l2_only, mp->is_add);
2140
2141     dsunlock (sm);
2142
2143     return rv;
2144 }
2145
2146 static void
2147 vl_api_mpls_ethernet_add_del_tunnel_2_t_handler
2148 (vl_api_mpls_ethernet_add_del_tunnel_2_t *mp)
2149 {
2150     vl_api_mpls_ethernet_add_del_tunnel_reply_t * rmp;
2151     int rv = 0;
2152
2153     rv = mpls_ethernet_add_del_tunnel_2_t_handler (mp);
2154
2155     REPLY_MACRO(VL_API_MPLS_ETHERNET_ADD_DEL_TUNNEL_2_REPLY);
2156 }
2157
2158
2159 static void
2160 vl_api_mpls_add_del_encap_t_handler (vl_api_mpls_add_del_encap_t *mp)
2161 {
2162     vl_api_mpls_add_del_encap_reply_t * rmp;
2163     int rv;
2164     static u32 * labels;
2165     int i;
2166
2167     vec_reset_length (labels);
2168
2169     for (i = 0; i < mp->nlabels; i++)
2170         vec_add1 (labels, ntohl(mp->labels[i]));
2171
2172     /* $$$$ fixme */
2173     rv = vnet_mpls_add_del_encap ((ip4_address_t *)mp->dst_address,
2174                                   ntohl(mp->vrf_id), labels,
2175                                   ~0 /* policy_tunnel_index */,
2176                                   0 /* no_dst_hash */,
2177                                   0 /* indexp */,
2178                                   mp->is_add);
2179
2180     REPLY_MACRO(VL_API_MPLS_ADD_DEL_ENCAP_REPLY);
2181 }
2182
2183 static void
2184 vl_api_mpls_add_del_decap_t_handler
2185 (vl_api_mpls_add_del_decap_t *mp)
2186 {
2187     vl_api_mpls_add_del_decap_reply_t * rmp;
2188     int rv;
2189
2190     rv = vnet_mpls_add_del_decap (ntohl(mp->rx_vrf_id), ntohl(mp->tx_vrf_id),
2191                                   ntohl(mp->label), ntohl(mp->next_index),
2192                                   mp->s_bit, mp->is_add);
2193
2194     REPLY_MACRO(VL_API_MPLS_ADD_DEL_DECAP_REPLY);
2195 }
2196
2197 static void
2198 vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t *mp)
2199 {
2200     vl_api_proxy_arp_add_del_reply_t * rmp;
2201     u32 fib_index;
2202     int rv;
2203     ip4_main_t * im = &ip4_main;
2204     stats_main_t * sm = &stats_main;
2205     int vnet_proxy_arp_add_del (ip4_address_t *lo_addr,
2206                                 ip4_address_t *hi_addr,
2207                                 u32 fib_index, int is_del);
2208     uword * p;
2209
2210     dslock (sm, 1 /* release hint */, 6 /* tag */);
2211
2212     p = hash_get (im->fib_index_by_table_id, ntohl(mp->vrf_id));
2213
2214     if (! p) {
2215         rv = VNET_API_ERROR_NO_SUCH_FIB;
2216         goto out;
2217     }
2218
2219     fib_index = p[0];
2220
2221     rv = vnet_proxy_arp_add_del ((ip4_address_t *)mp->low_address,
2222                                  (ip4_address_t *)mp->hi_address,
2223                                  fib_index, mp->is_add == 0);
2224
2225 out:
2226     dsunlock (sm);
2227     REPLY_MACRO(VL_API_PROXY_ARP_ADD_DEL_REPLY);
2228 }
2229
2230 static void
2231 vl_api_proxy_arp_intfc_enable_disable_t_handler
2232 (vl_api_proxy_arp_intfc_enable_disable_t *mp)
2233 {
2234     int rv = 0;
2235     vnet_main_t * vnm = vnet_get_main();
2236     vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
2237     vnet_sw_interface_t * si;
2238     u32 sw_if_index;
2239
2240     VALIDATE_SW_IF_INDEX(mp);
2241
2242     sw_if_index = ntohl(mp->sw_if_index);
2243
2244     if (pool_is_free_index (vnm->interface_main.sw_interfaces,
2245                             sw_if_index)) {
2246         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2247         goto out;
2248     }
2249
2250     si = vnet_get_sw_interface (vnm, sw_if_index);
2251
2252     ASSERT(si);
2253
2254     if (mp->enable_disable)
2255         si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2256     else
2257         si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2258
2259     BAD_SW_IF_INDEX_LABEL;
2260
2261     out:
2262     REPLY_MACRO(VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
2263 }
2264
2265 static void
2266 vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t *mp, vlib_main_t * vm)
2267 {
2268     vl_api_ip_neighbor_add_del_reply_t * rmp;
2269     vnet_main_t * vnm = vnet_get_main();
2270     u32 fib_index;
2271     int rv=0;
2272     stats_main_t * sm = &stats_main;
2273
2274     VALIDATE_SW_IF_INDEX(mp);
2275
2276     dslock (sm, 1 /* release hint */, 7 /* tag */);
2277
2278     if (mp->is_ipv6) {
2279         if (mp->is_add)
2280             rv = vnet_set_ip6_ethernet_neighbor
2281                 (vm, ntohl(mp->sw_if_index),
2282                  (ip6_address_t *)(mp->dst_address),
2283                  mp->mac_address, sizeof (mp->mac_address), mp->is_static);
2284         else
2285             rv = vnet_unset_ip6_ethernet_neighbor
2286                 (vm, ntohl(mp->sw_if_index),
2287                  (ip6_address_t *)(mp->dst_address),
2288                  mp->mac_address, sizeof(mp->mac_address));
2289     } else {
2290         ip4_main_t * im = &ip4_main;
2291         ip_lookup_main_t * lm = &im->lookup_main;
2292         ethernet_arp_ip4_over_ethernet_address_t a;
2293         u32 ai;
2294         ip_adjacency_t *nh_adj;
2295
2296         uword * p = hash_get (im->fib_index_by_table_id, ntohl(mp->vrf_id));
2297         if (! p) {
2298             rv = VNET_API_ERROR_NO_SUCH_FIB;
2299             goto out;
2300         }
2301         fib_index = p[0];
2302
2303         /*
2304          * Unfortunately, folks have a penchant for
2305          * adding interface addresses to the ARP cache, and
2306          * wondering why the forwarder eventually ASSERTs...
2307          */
2308         ai = ip4_fib_lookup_with_table
2309             (im, fib_index, (ip4_address_t *)(mp->dst_address),
2310              1 /* disable default route */);
2311
2312         if (ai != 0) {
2313             nh_adj = ip_get_adjacency (lm, ai);
2314             /* Never allow manipulation of a local adj! */
2315             if (nh_adj->lookup_next_index == IP_LOOKUP_NEXT_LOCAL) {
2316                 clib_warning("%U matches local adj",
2317                              format_ip4_address,
2318                              (ip4_address_t *)(mp->dst_address));
2319                 rv = VNET_API_ERROR_ADDRESS_MATCHES_INTERFACE_ADDRESS;
2320                 goto out;
2321             }
2322         }
2323
2324         clib_memcpy (&a.ethernet, mp->mac_address, 6);
2325         clib_memcpy (&a.ip4, mp->dst_address, 4);
2326
2327         if (mp->is_add)
2328             rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl(mp->sw_if_index),
2329                                                  fib_index, &a, mp->is_static);
2330         else
2331             rv = vnet_arp_unset_ip4_over_ethernet (vnm, ntohl(mp->sw_if_index),
2332                                                    fib_index, &a);
2333     }
2334
2335     BAD_SW_IF_INDEX_LABEL;
2336     out:
2337     dsunlock (sm);
2338     REPLY_MACRO(VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
2339 }
2340
2341 static void
2342 vl_api_is_address_reachable_t_handler (vl_api_is_address_reachable_t *mp)
2343 {
2344 #if 0
2345     vpe_main_t *rm = &vpe_main;
2346     ip4_main_t *im4 = &ip4_main;
2347     ip6_main_t *im6 = &ip6_main;
2348     ip_lookup_main_t * lm;
2349     union {
2350         ip4_address_t ip4;
2351         ip6_address_t ip6;
2352     } addr;
2353     u32 adj_index, sw_if_index;
2354     vl_api_is_address_reachable_t *rmp;
2355     ip_adjacency_t * adj;
2356     unix_shared_memory_queue_t *q;
2357
2358     q = vl_api_client_index_to_input_queue (mp->client_index);
2359     if (!q) {
2360         increment_missing_api_client_counter (rm->vlib_main);
2361         return;
2362     }
2363
2364     rmp = vl_msg_api_alloc (sizeof (*rmp));
2365     clib_memcpy (rmp, mp, sizeof (*rmp));
2366
2367     sw_if_index = mp->next_hop_sw_if_index;
2368     clib_memcpy (&addr, mp->address, sizeof (addr));
2369     if (mp->is_ipv6) {
2370         lm = &im6->lookup_main;
2371         adj_index =
2372             ip6_fib_lookup (im6, sw_if_index, &addr.ip6);
2373     } else {
2374         lm = &im4->lookup_main;
2375         adj_index =
2376             ip4_fib_lookup (im4, sw_if_index, &addr.ip4);
2377     }
2378     if (adj_index == ~0) {
2379         rmp->is_error = 1;
2380         goto send;
2381     }
2382     adj = ip_get_adjacency (lm, adj_index);
2383
2384     if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE
2385         && adj->rewrite_header.sw_if_index == sw_if_index) {
2386         rmp->is_known = 1;
2387     } else {
2388         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP
2389             && adj->rewrite_header.sw_if_index == sw_if_index) {
2390             if (mp->is_ipv6)
2391                 ip6_probe_neighbor (rm->vlib_main, &addr.ip6, sw_if_index);
2392             else
2393                 ip4_probe_neighbor (rm->vlib_main, &addr.ip4, sw_if_index);
2394         } else if (adj->lookup_next_index == IP_LOOKUP_NEXT_DROP) {
2395             rmp->is_known = 1;
2396             goto send;
2397         }
2398         rmp->is_known = 0;
2399     }
2400
2401 send:
2402     vl_msg_api_send_shmem (q, (u8 *)&rmp);
2403 #endif
2404 }
2405
2406 static void vl_api_sw_interface_details_t_handler (
2407     vl_api_sw_interface_details_t * mp)
2408 {
2409     clib_warning ("BUG");
2410 }
2411
2412 static void vl_api_sw_interface_set_flags_t_handler (
2413     vl_api_sw_interface_set_flags_t * mp)
2414 {
2415    vl_api_sw_interface_set_flags_reply_t *rmp;
2416    vnet_main_t * vnm = vnet_get_main();
2417    int rv = 0;
2418    clib_error_t * error;
2419    u16 flags;
2420
2421    VALIDATE_SW_IF_INDEX(mp);
2422
2423    flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
2424
2425    error = vnet_sw_interface_set_flags (vnm,
2426                                         ntohl(mp->sw_if_index),
2427                                         flags);
2428    if (error) {
2429        rv = -1;
2430        clib_error_report (error);
2431    }
2432
2433    BAD_SW_IF_INDEX_LABEL;
2434    REPLY_MACRO(VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
2435 }
2436
2437 static void vl_api_sw_interface_clear_stats_t_handler (
2438     vl_api_sw_interface_clear_stats_t * mp)
2439 {
2440    vl_api_sw_interface_clear_stats_reply_t *rmp;
2441
2442    vnet_main_t * vnm = vnet_get_main();
2443    vnet_interface_main_t * im = &vnm->interface_main;
2444    vlib_simple_counter_main_t * sm;
2445    vlib_combined_counter_main_t * cm;
2446    static vnet_main_t ** my_vnet_mains;
2447    int i, j, n_counters;
2448
2449    int rv = 0;
2450
2451    vec_reset_length (my_vnet_mains);
2452
2453    for (i = 0; i < vec_len (vnet_mains); i++)
2454      {
2455        if (vnet_mains[i])
2456          vec_add1 (my_vnet_mains, vnet_mains[i]);
2457      }
2458
2459    if (vec_len (vnet_mains) == 0)
2460      vec_add1 (my_vnet_mains, vnm);
2461
2462    n_counters = vec_len (im->combined_sw_if_counters);
2463
2464    for (j = 0; j < n_counters; j++)
2465      {
2466        for (i = 0; i < vec_len(my_vnet_mains); i++)
2467          {
2468            im = &my_vnet_mains[i]->interface_main;
2469            cm = im->combined_sw_if_counters + j;
2470            if (mp->sw_if_index == (u32)~0)
2471              vlib_clear_combined_counters (cm);
2472            else
2473              vlib_zero_combined_counter (cm, ntohl(mp->sw_if_index));
2474          }
2475      }
2476
2477    n_counters = vec_len (im->sw_if_counters);
2478
2479    for (j = 0; j < n_counters; j++)
2480      {
2481        for (i = 0; i < vec_len(my_vnet_mains); i++)
2482          {
2483            im = &my_vnet_mains[i]->interface_main;
2484            sm = im->sw_if_counters + j;
2485            if (mp->sw_if_index == (u32)~0)
2486              vlib_clear_simple_counters (sm);
2487            else
2488              vlib_zero_simple_counter (sm, ntohl(mp->sw_if_index));
2489          }
2490      }
2491
2492    REPLY_MACRO(VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
2493 }
2494
2495 static void send_sw_interface_details (vpe_api_main_t * am,
2496                                        unix_shared_memory_queue_t *q,
2497                                        vnet_sw_interface_t * swif,
2498                                        u8 * interface_name,
2499                                        u32 context)
2500 {
2501     vl_api_sw_interface_details_t * mp;
2502     vnet_hw_interface_t * hi;
2503
2504     hi = vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
2505
2506     mp = vl_msg_api_alloc (sizeof (*mp));
2507     memset (mp, 0, sizeof (*mp));
2508     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_DETAILS);
2509     mp->sw_if_index = ntohl(swif->sw_if_index);
2510     mp->sup_sw_if_index = ntohl(swif->sup_sw_if_index);
2511     mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
2512         1 : 0;
2513     mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
2514         1 : 0;
2515     mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
2516                        VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
2517     mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
2518                       VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
2519     mp->link_mtu = ntohs(hi->max_packet_bytes);
2520     mp->context = context;
2521
2522     strncpy ((char *) mp->interface_name,
2523              (char *) interface_name, ARRAY_LEN(mp->interface_name)-1);
2524
2525     /* Send the L2 address for ethernet physical intfcs */
2526     if (swif->sup_sw_if_index == swif->sw_if_index
2527         && hi->hw_class_index == ethernet_hw_interface_class.index) {
2528         ethernet_main_t *em = ethernet_get_main (am->vlib_main);
2529         ethernet_interface_t *ei;
2530
2531         ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
2532         ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
2533         clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
2534         mp->l2_address_length = ntohl(sizeof (ei->address));
2535     } else if (swif->sup_sw_if_index != swif->sw_if_index) {
2536         vnet_sub_interface_t *sub = &swif->sub;
2537         mp->sub_id = ntohl(sub->id);
2538         mp->sub_dot1ad = sub->eth.flags.dot1ad;
2539         mp->sub_number_of_tags = sub->eth.flags.one_tag + sub->eth.flags.two_tags*2;
2540         mp->sub_outer_vlan_id = ntohs(sub->eth.outer_vlan_id);
2541         mp->sub_inner_vlan_id = ntohs(sub->eth.inner_vlan_id);
2542         mp->sub_exact_match = sub->eth.flags.exact_match;
2543         mp->sub_default = sub->eth.flags.default_sub;
2544         mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
2545         mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
2546
2547         /* vlan tag rewrite data */
2548         u32 vtr_op = L2_VTR_DISABLED;
2549         u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
2550
2551         if (l2vtr_get(am->vlib_main, am->vnet_main, swif->sw_if_index,
2552                       &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0) {
2553             // error - default to disabled
2554             mp->vtr_op = ntohl(L2_VTR_DISABLED);
2555             clib_warning("cannot get vlan tag rewrite for sw_if_index %d",
2556                     swif->sw_if_index);
2557         } else {
2558             mp->vtr_op = ntohl(vtr_op);
2559             mp->vtr_push_dot1q = ntohl(vtr_push_dot1q);
2560             mp->vtr_tag1 = ntohl(vtr_tag1);
2561             mp->vtr_tag2 = ntohl(vtr_tag2);
2562         }
2563     }
2564
2565     vl_msg_api_send_shmem (q, (u8 *)&mp);
2566 }
2567
2568 static void send_sw_interface_flags (vpe_api_main_t * am,
2569                                      unix_shared_memory_queue_t *q,
2570                                      vnet_sw_interface_t * swif)
2571 {
2572     vl_api_sw_interface_set_flags_t *mp;
2573     vnet_main_t * vnm = am->vnet_main;
2574
2575     vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
2576                                                          swif->sw_if_index);
2577     mp = vl_msg_api_alloc (sizeof (*mp));
2578     memset (mp, 0, sizeof (*mp));
2579     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_SET_FLAGS);
2580     mp->sw_if_index = ntohl(swif->sw_if_index);
2581
2582     mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
2583         1 : 0;
2584     mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
2585         1 : 0;
2586     vl_msg_api_send_shmem (q, (u8 *)&mp);
2587 }
2588
2589 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
2590                                      unix_shared_memory_queue_t *q,
2591                                              u32 sw_if_index)
2592     __attribute__((unused));
2593
2594 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
2595                                      unix_shared_memory_queue_t *q,
2596                                      u32 sw_if_index)
2597 {
2598     vl_api_sw_interface_set_flags_t *mp;
2599
2600     mp = vl_msg_api_alloc (sizeof (*mp));
2601     memset (mp, 0, sizeof (*mp));
2602     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_SET_FLAGS);
2603     mp->sw_if_index = ntohl(sw_if_index);
2604
2605     mp->admin_up_down = 0;
2606     mp->link_up_down = 0;
2607     mp->deleted = 1;
2608     vl_msg_api_send_shmem (q, (u8 *)&mp);
2609 }
2610
2611 static void vl_api_sw_interface_dump_t_handler (
2612     vl_api_sw_interface_dump_t *mp)
2613 {
2614     vpe_api_main_t * am = &vpe_api_main;
2615     vnet_sw_interface_t * swif;
2616     vnet_interface_main_t * im = &am->vnet_main->interface_main;
2617     u8 * filter_string = 0, * name_string = 0;
2618     unix_shared_memory_queue_t * q;
2619     char * strcasestr (char *, char *); /* lnx hdr file botch */
2620
2621     q = vl_api_client_index_to_input_queue (mp->client_index);
2622
2623     if (q == 0)
2624         return;
2625
2626     if (mp->name_filter_valid) {
2627         mp->name_filter [ARRAY_LEN(mp->name_filter)-1] = 0;
2628         filter_string = format (0, "%s%c", mp->name_filter, 0);
2629     }
2630
2631     pool_foreach (swif, im->sw_interfaces,
2632     ({
2633         name_string = format (name_string, "%U%c",
2634                               format_vnet_sw_interface_name,
2635                               am->vnet_main, swif, 0);
2636
2637         if (mp->name_filter_valid == 0 ||
2638             strcasestr((char *) name_string, (char *) filter_string)) {
2639
2640             send_sw_interface_details (am, q, swif, name_string, mp->context);
2641         }
2642         _vec_len (name_string) = 0;
2643     }));
2644
2645     vec_free (name_string);
2646     vec_free (filter_string);
2647 }
2648
2649 void send_oam_event (oam_target_t * t)
2650 {
2651     vpe_api_main_t * vam = &vpe_api_main;
2652     unix_shared_memory_queue_t * q;
2653     vpe_client_registration_t *reg;
2654     vl_api_oam_event_t * mp;
2655
2656     pool_foreach(reg, vam->oam_events_registrations,
2657     ({
2658         q = vl_api_client_index_to_input_queue (reg->client_index);
2659         if (q) {
2660             mp = vl_msg_api_alloc (sizeof (*mp));
2661             mp->_vl_msg_id = ntohs (VL_API_OAM_EVENT);
2662             clib_memcpy (mp->dst_address, &t->dst_address, sizeof (mp->dst_address));
2663             mp->state = t->state;
2664             vl_msg_api_send_shmem (q, (u8 *)&mp);
2665         }
2666     }));
2667 }
2668
2669 static void
2670 vl_api_oam_add_del_t_handler (vl_api_oam_add_del_t *mp)
2671 {
2672     vl_api_oam_add_del_reply_t * rmp;
2673     int rv;
2674
2675     rv = vpe_oam_add_del_target ((ip4_address_t *)mp->src_address,
2676                                  (ip4_address_t *)mp->dst_address,
2677                                  ntohl(mp->vrf_id),
2678                                  (int)(mp->is_add));
2679
2680     REPLY_MACRO(VL_API_OAM_ADD_DEL_REPLY);
2681 }
2682
2683 static void
2684 vl_api_vnet_get_summary_stats_t_handler (
2685     vl_api_vnet_get_summary_stats_t *mp)
2686 {
2687     stats_main_t * sm = &stats_main;
2688     vnet_interface_main_t * im = sm->interface_main;
2689     vl_api_vnet_summary_stats_reply_t *rmp;
2690     vlib_combined_counter_main_t * cm;
2691     vlib_counter_t v;
2692     int i, which;
2693     u64 total_pkts[VLIB_N_RX_TX];
2694     u64 total_bytes[VLIB_N_RX_TX];
2695
2696     unix_shared_memory_queue_t * q =
2697         vl_api_client_index_to_input_queue (mp->client_index);
2698
2699     if (!q)
2700         return;
2701
2702     rmp = vl_msg_api_alloc (sizeof (*rmp));
2703     rmp->_vl_msg_id = ntohs(VL_API_VNET_SUMMARY_STATS_REPLY);
2704     rmp->context = mp->context;
2705     rmp->retval = 0;
2706
2707     memset (total_pkts, 0, sizeof (total_pkts));
2708     memset (total_bytes, 0, sizeof (total_bytes));
2709
2710     vnet_interface_counter_lock (im);
2711
2712     vec_foreach (cm, im->combined_sw_if_counters) {
2713         which = cm - im->combined_sw_if_counters;
2714
2715         for (i = 0; i < vec_len (cm->maxi); i++) {
2716             vlib_get_combined_counter (cm, i, &v);
2717             total_pkts[which] += v.packets;
2718             total_bytes[which] += v.bytes;
2719         }
2720     }
2721     vnet_interface_counter_unlock (im);
2722
2723     /* Note: in HOST byte order! */
2724     rmp->total_pkts[VLIB_RX] = total_pkts[VLIB_RX];
2725     rmp->total_bytes[VLIB_RX] = total_bytes[VLIB_RX];
2726     rmp->total_pkts[VLIB_TX] = total_pkts[VLIB_TX];
2727     rmp->total_bytes[VLIB_TX] = total_bytes[VLIB_TX];
2728     rmp->vector_rate = vlib_last_vector_length_per_node (sm->vlib_main);
2729
2730     vl_msg_api_send_shmem (q, (u8 *)&rmp);
2731 }
2732
2733 typedef CLIB_PACKED (struct {
2734   ip4_address_t address;
2735
2736   u32 address_length : 6;
2737
2738   u32 index : 26;
2739 }) ip4_route_t;
2740
2741 static int ip4_reset_fib_t_handler (vl_api_reset_fib_t *mp)
2742 {
2743     vnet_main_t * vnm = vnet_get_main();
2744     vnet_interface_main_t * im = &vnm->interface_main;
2745     ip4_main_t * im4 = &ip4_main;
2746     static ip4_route_t * routes;
2747     static u32 * sw_if_indices_to_shut;
2748     stats_main_t * sm = &stats_main;
2749     ip4_route_t * r;
2750     ip4_fib_t * fib;
2751     u32 sw_if_index;
2752     int i;
2753     int rv = VNET_API_ERROR_NO_SUCH_FIB;
2754     u32 target_fib_id = ntohl(mp->vrf_id);
2755
2756     dslock (sm, 1 /* release hint */, 8 /* tag */);
2757
2758     vec_foreach (fib, im4->fibs) {
2759         vnet_sw_interface_t * si;
2760
2761         if (fib->table_id != target_fib_id)
2762             continue;
2763
2764         /* remove any mpls/gre tunnels in this fib */
2765         vnet_mpls_gre_delete_fib_tunnels (fib->table_id);
2766
2767         /* remove any mpls encap/decap labels */
2768         mpls_fib_reset_labels (fib->table_id);
2769
2770         /* remove any proxy arps in this fib */
2771         vnet_proxy_arp_fib_reset (fib->table_id);
2772
2773         /* Set the flow hash for this fib to the default */
2774         vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2775
2776         vec_reset_length (sw_if_indices_to_shut);
2777
2778         /* Shut down interfaces in this FIB / clean out intfc routes */
2779         pool_foreach (si, im->sw_interfaces,
2780         ({
2781             u32 sw_if_index = si->sw_if_index;
2782
2783             if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
2784                 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
2785                     fib - im4->fibs))
2786                 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2787         }));
2788
2789         for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2790             sw_if_index = sw_if_indices_to_shut[i];
2791             // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
2792
2793             u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2794             flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2795             vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2796         }
2797
2798         vec_reset_length (routes);
2799
2800         for (i = 0; i < ARRAY_LEN (fib->adj_index_by_dst_address); i++) {
2801             uword * hash = fib->adj_index_by_dst_address[i];
2802             hash_pair_t * p;
2803             ip4_route_t x;
2804
2805             x.address_length = i;
2806
2807             hash_foreach_pair (p, hash,
2808             ({
2809                 x.address.data_u32 = p->key;
2810                 vec_add1 (routes, x);
2811             }));
2812         }
2813
2814         vec_foreach (r, routes) {
2815             ip4_add_del_route_args_t a;
2816
2817             memset (&a, 0, sizeof (a));
2818             a.flags = IP4_ROUTE_FLAG_FIB_INDEX | IP4_ROUTE_FLAG_DEL;
2819             a.table_index_or_table_id = fib - im4->fibs;
2820             a.dst_address = r->address;
2821             a.dst_address_length = r->address_length;
2822             a.adj_index = ~0;
2823
2824             ip4_add_del_route (im4, &a);
2825             ip4_maybe_remap_adjacencies (im4, fib - im4->fibs,
2826                                          IP4_ROUTE_FLAG_FIB_INDEX);
2827         }
2828         rv = 0;
2829         break;
2830     } /* vec_foreach (fib) */
2831
2832     dsunlock(sm);
2833     return rv;
2834 }
2835
2836 typedef struct {
2837   ip6_address_t address;
2838   u32 address_length;
2839   u32 index;
2840 } ip6_route_t;
2841
2842 typedef struct {
2843   u32 fib_index;
2844   ip6_route_t ** routep;
2845 } add_routes_in_fib_arg_t;
2846
2847 static void add_routes_in_fib (clib_bihash_kv_24_8_t * kvp, void *arg)
2848 {
2849   add_routes_in_fib_arg_t * ap = arg;
2850
2851   if (kvp->key[2]>>32 == ap->fib_index)
2852     {
2853       ip6_address_t *addr;
2854       ip6_route_t * r;
2855       addr = (ip6_address_t *) kvp;
2856       vec_add2 (*ap->routep, r, 1);
2857       r->address = addr[0];
2858       r->address_length = kvp->key[2] & 0xFF;
2859       r->index = kvp->value;
2860     }
2861 }
2862
2863 static int ip6_reset_fib_t_handler (vl_api_reset_fib_t *mp)
2864 {
2865     vnet_main_t * vnm = vnet_get_main();
2866     vnet_interface_main_t * im = &vnm->interface_main;
2867     ip6_main_t * im6 = &ip6_main;
2868     stats_main_t * sm = &stats_main;
2869     static ip6_route_t * routes;
2870     static u32 * sw_if_indices_to_shut;
2871     ip6_route_t * r;
2872     ip6_fib_t * fib;
2873     u32 sw_if_index;
2874     int i;
2875     int rv = VNET_API_ERROR_NO_SUCH_FIB;
2876     u32 target_fib_id = ntohl(mp->vrf_id);
2877     add_routes_in_fib_arg_t _a, *a=&_a;
2878     clib_bihash_24_8_t * h = &im6->ip6_lookup_table;
2879
2880     dslock (sm, 1 /* release hint */, 9 /* tag */);
2881
2882     vec_foreach (fib, im6->fibs) {
2883         vnet_sw_interface_t * si;
2884
2885         if (fib->table_id != target_fib_id)
2886             continue;
2887
2888         vec_reset_length (sw_if_indices_to_shut);
2889
2890         /* Shut down interfaces in this FIB / clean out intfc routes */
2891         pool_foreach (si, im->sw_interfaces,
2892         ({
2893             if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
2894                 fib - im6->fibs)
2895                 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2896         }));
2897
2898         for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2899             sw_if_index = sw_if_indices_to_shut[i];
2900             // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
2901
2902             u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2903             flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2904             vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2905         }
2906
2907         vec_reset_length (routes);
2908
2909         a->fib_index = fib - im6->fibs;
2910         a->routep = &routes;
2911
2912         clib_bihash_foreach_key_value_pair_24_8 (h, add_routes_in_fib, a);
2913
2914         vec_foreach (r, routes) {
2915             ip6_add_del_route_args_t a;
2916
2917             memset (&a, 0, sizeof (a));
2918             a.flags = IP6_ROUTE_FLAG_FIB_INDEX | IP6_ROUTE_FLAG_DEL;
2919             a.table_index_or_table_id = fib - im6->fibs;
2920             a.dst_address = r->address;
2921             a.dst_address_length = r->address_length;
2922             a.adj_index = ~0;
2923
2924             ip6_add_del_route (im6, &a);
2925             ip6_maybe_remap_adjacencies (im6, fib - im6->fibs,
2926                                          IP6_ROUTE_FLAG_FIB_INDEX);
2927         }
2928         rv = 0;
2929         /* Reinstall the neighbor / router discovery routes */
2930         vnet_ip6_fib_init (im6, fib - im6->fibs);
2931         break;
2932     } /* vec_foreach (fib) */
2933
2934     dsunlock(sm);
2935     return rv;
2936 }
2937
2938 static void vl_api_reset_fib_t_handler (vl_api_reset_fib_t *mp)
2939 {
2940     int rv;
2941     vl_api_reset_fib_reply_t * rmp;
2942
2943     if (mp->is_ipv6)
2944         rv = ip6_reset_fib_t_handler (mp);
2945     else
2946         rv = ip4_reset_fib_t_handler (mp);
2947
2948     REPLY_MACRO(VL_API_RESET_FIB_REPLY);
2949 }
2950
2951
2952 static void
2953 dhcpv4_proxy_config (vl_api_dhcp_proxy_config_t *mp)
2954 {
2955     vl_api_dhcp_proxy_config_reply_t * rmp;
2956     int rv;
2957
2958     rv = dhcp_proxy_set_server ((ip4_address_t *)(&mp->dhcp_server),
2959                                 (ip4_address_t *)(&mp->dhcp_src_address),
2960                                 (u32) ntohl(mp->vrf_id),
2961                                 (int) mp->insert_circuit_id,
2962                                 (int) (mp->is_add == 0));
2963
2964     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_REPLY);
2965 }
2966
2967
2968 static void
2969 dhcpv6_proxy_config (vl_api_dhcp_proxy_config_t *mp)
2970 {
2971     vl_api_dhcp_proxy_config_reply_t * rmp;
2972     int rv = -1;
2973
2974     rv = dhcpv6_proxy_set_server ((ip6_address_t *)(&mp->dhcp_server),
2975                                 (ip6_address_t *)(&mp->dhcp_src_address),
2976                                 (u32) ntohl(mp->vrf_id),
2977                                 (int) mp->insert_circuit_id,
2978                                 (int) (mp->is_add == 0));
2979
2980     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_REPLY);
2981 }
2982
2983 static void
2984 dhcpv4_proxy_config_2 (vl_api_dhcp_proxy_config_2_t *mp)
2985 {
2986     vl_api_dhcp_proxy_config_reply_t * rmp;
2987     int rv;
2988
2989     rv = dhcp_proxy_set_server_2 ((ip4_address_t *)(&mp->dhcp_server),
2990                                 (ip4_address_t *)(&mp->dhcp_src_address),
2991                                 (u32) ntohl(mp->rx_vrf_id),
2992                                 (u32) ntohl(mp->server_vrf_id),
2993                                 (int) mp->insert_circuit_id,
2994                                 (int) (mp->is_add == 0));
2995
2996     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_2_REPLY);
2997 }
2998
2999
3000 static void
3001 dhcpv6_proxy_config_2 (vl_api_dhcp_proxy_config_2_t *mp)
3002 {
3003     vl_api_dhcp_proxy_config_reply_t * rmp;
3004     int rv = -1;
3005
3006 #if 0 // $$$$ FIXME
3007     rv = dhcpv6_proxy_set_server_2 ((ip6_address_t *)(&mp->dhcp_server),
3008                                 (ip6_address_t *)(&mp->dhcp_src_address),
3009                                 (u32) ntohl(mp->rx_vrf_id),
3010                                 (u32) ntohl(mp->server_vrf_id),
3011                                 (int) mp->insert_circuit_id,
3012                                 (int) (mp->is_add == 0));
3013 #else
3014     rv = VNET_API_ERROR_UNIMPLEMENTED;
3015 #endif
3016
3017     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_2_REPLY);
3018 }
3019
3020
3021 static void
3022 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t *mp)
3023 {
3024     vl_api_dhcp_proxy_set_vss_reply_t *rmp;
3025     int rv;
3026     if (!mp->is_ipv6)
3027         rv = dhcp_proxy_set_option82_vss(ntohl(mp->tbl_id),
3028                                          ntohl(mp->oui),
3029                                          ntohl(mp->fib_id),
3030                                          (int)mp->is_add == 0);
3031     else
3032          rv = dhcpv6_proxy_set_vss( ntohl(mp->tbl_id),
3033                                          ntohl(mp->oui),
3034                                          ntohl(mp->fib_id),
3035                                          (int)mp->is_add == 0);
3036
3037     REPLY_MACRO(VL_API_DHCP_PROXY_SET_VSS_REPLY);
3038 }
3039
3040
3041 static void vl_api_dhcp_proxy_config_t_handler
3042 (vl_api_dhcp_proxy_config_t *mp)
3043 {
3044     if (mp->is_ipv6 == 0)
3045         dhcpv4_proxy_config (mp);
3046     else
3047         dhcpv6_proxy_config (mp);
3048 }
3049
3050 static void vl_api_dhcp_proxy_config_2_t_handler
3051 (vl_api_dhcp_proxy_config_2_t *mp)
3052 {
3053     if (mp->is_ipv6 == 0)
3054         dhcpv4_proxy_config_2 (mp);
3055     else
3056         dhcpv6_proxy_config_2 (mp);
3057 }
3058
3059 void dhcp_compl_event_callback (u32 client_index, u32 pid, u8 * hostname,
3060        u8 is_ipv6, u8 * host_address, u8 * router_address, u8 * host_mac)
3061 {
3062     unix_shared_memory_queue_t * q;
3063     vl_api_dhcp_compl_event_t * mp;
3064
3065     q = vl_api_client_index_to_input_queue (client_index);
3066     if (!q)
3067         return;
3068
3069     mp = vl_msg_api_alloc (sizeof (*mp));
3070     mp->client_index = client_index;
3071     mp->pid = pid;
3072     mp->is_ipv6 = is_ipv6;
3073     clib_memcpy (&mp->hostname, hostname, vec_len(hostname));
3074     mp->hostname[vec_len(hostname) + 1] = '\n';
3075     clib_memcpy (&mp->host_address[0], host_address, 16);
3076     clib_memcpy (&mp->router_address[0], router_address, 16);
3077     clib_memcpy (&mp->host_mac[0], host_mac, 6);
3078
3079     mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
3080
3081     vl_msg_api_send_shmem (q, (u8 *)&mp);
3082 }
3083
3084 static void vl_api_dhcp_client_config_t_handler
3085 (vl_api_dhcp_client_config_t *mp)
3086 {
3087     vlib_main_t *vm = vlib_get_main();
3088     vl_api_dhcp_client_config_reply_t * rmp;
3089     int rv = 0;
3090
3091     VALIDATE_SW_IF_INDEX(mp);
3092
3093     rv = dhcp_client_config(vm, ntohl(mp->sw_if_index),
3094              mp->hostname, mp->is_add, mp->client_index,
3095              mp->want_dhcp_event ? dhcp_compl_event_callback : NULL,
3096              mp->pid);
3097
3098     BAD_SW_IF_INDEX_LABEL;
3099
3100     REPLY_MACRO(VL_API_DHCP_CLIENT_CONFIG_REPLY);
3101 }
3102
3103 static void
3104 vl_api_sw_interface_ip6nd_ra_config_t_handler
3105 (vl_api_sw_interface_ip6nd_ra_config_t *mp, vlib_main_t *vm)
3106 {
3107    vl_api_sw_interface_ip6nd_ra_config_reply_t * rmp;
3108     int rv = 0;
3109     u8  is_no,  surpress, managed, other, ll_option, send_unicast, cease, default_router;
3110
3111     is_no = mp->is_no == 1;
3112     surpress = mp->surpress == 1;
3113     managed = mp->managed == 1;
3114    other = mp->other == 1;
3115     ll_option = mp->ll_option == 1;
3116     send_unicast = mp->send_unicast == 1;
3117     cease = mp->cease == 1;
3118     default_router = mp->default_router  == 1;
3119
3120     VALIDATE_SW_IF_INDEX(mp);
3121
3122     rv = ip6_neighbor_ra_config(vm, ntohl(mp->sw_if_index),
3123                                 surpress,  managed,  other,
3124                                 ll_option,  send_unicast,  cease,
3125                                 default_router, ntohl (mp->lifetime),
3126                                 ntohl(mp->initial_count),  ntohl(mp->initial_interval),
3127                                 ntohl(mp->max_interval), ntohl( mp->min_interval),
3128                                 is_no);
3129
3130     BAD_SW_IF_INDEX_LABEL;
3131
3132     REPLY_MACRO(VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
3133 }
3134
3135 static void
3136 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
3137 (vl_api_sw_interface_ip6nd_ra_prefix_t *mp, vlib_main_t *vm)
3138 {
3139    vl_api_sw_interface_ip6nd_ra_prefix_reply_t * rmp;
3140     int rv = 0;
3141     u8  is_no,  use_default,  no_advertise, off_link, no_autoconfig, no_onlink;
3142
3143     VALIDATE_SW_IF_INDEX(mp);
3144
3145     is_no = mp->is_no == 1;
3146     use_default = mp->use_default == 1;
3147     no_advertise = mp->no_advertise == 1;
3148     off_link = mp->off_link == 1;
3149     no_autoconfig = mp->no_autoconfig == 1;
3150     no_onlink = mp->no_onlink == 1;
3151
3152     rv = ip6_neighbor_ra_prefix(vm,  ntohl(mp->sw_if_index),
3153                                 (ip6_address_t *)mp->address,  mp->address_length,
3154                                 use_default,  ntohl(mp->val_lifetime), ntohl(mp->pref_lifetime),
3155                                 no_advertise,  off_link, no_autoconfig, no_onlink,
3156                                 is_no);
3157
3158     BAD_SW_IF_INDEX_LABEL;
3159     REPLY_MACRO(VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
3160 }
3161
3162 static void
3163 vl_api_sw_interface_ip6_enable_disable_t_handler
3164 (vl_api_sw_interface_ip6_enable_disable_t *mp, vlib_main_t *vm)
3165 {
3166     vl_api_sw_interface_ip6_enable_disable_reply_t * rmp;
3167     vnet_main_t * vnm = vnet_get_main();
3168     int rv = 0;
3169     clib_error_t * error;
3170
3171     vnm->api_errno = 0;
3172
3173     VALIDATE_SW_IF_INDEX(mp);
3174
3175     error = ( mp->enable == 1) ? enable_ip6_interface(vm,ntohl(mp->sw_if_index)) :
3176         disable_ip6_interface(vm,ntohl(mp->sw_if_index));
3177
3178     if (error)  {
3179         clib_error_report(error);
3180         rv = VNET_API_ERROR_UNSPECIFIED;
3181     } else {
3182         rv = vnm->api_errno;
3183     }
3184
3185     BAD_SW_IF_INDEX_LABEL;
3186
3187     REPLY_MACRO(VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
3188 }
3189
3190 static void
3191 vl_api_sw_interface_ip6_set_link_local_address_t_handler
3192 (vl_api_sw_interface_ip6_set_link_local_address_t *mp, vlib_main_t *vm)
3193 {
3194     vl_api_sw_interface_ip6_set_link_local_address_reply_t * rmp;
3195     int rv = 0;
3196     clib_error_t * error;
3197     vnet_main_t * vnm = vnet_get_main();
3198
3199     vnm->api_errno = 0;
3200
3201     VALIDATE_SW_IF_INDEX(mp);
3202
3203     error = set_ip6_link_local_address(vm,
3204                                        ntohl(mp->sw_if_index),
3205                                        (ip6_address_t *)mp->address,
3206                                        mp->address_length);
3207     if (error)  {
3208       clib_error_report(error);
3209       rv = VNET_API_ERROR_UNSPECIFIED;
3210     } else {
3211         rv = vnm->api_errno;
3212     }
3213
3214     BAD_SW_IF_INDEX_LABEL;
3215
3216     REPLY_MACRO(VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
3217 }
3218
3219 static void set_ip6_flow_hash (vl_api_set_ip_flow_hash_t *mp)
3220 {
3221     vl_api_set_ip_flow_hash_reply_t *rmp;
3222     int rv = VNET_API_ERROR_UNIMPLEMENTED;
3223
3224     clib_warning ("unimplemented...");
3225
3226     REPLY_MACRO(VL_API_SET_IP_FLOW_HASH_REPLY);
3227 }
3228
3229 static void set_ip4_flow_hash (vl_api_set_ip_flow_hash_t *mp)
3230 {
3231     vl_api_set_ip_flow_hash_reply_t *rmp;
3232     int rv;
3233     u32 table_id;
3234     u32 flow_hash_config = 0;
3235
3236     table_id = ntohl(mp->vrf_id);
3237
3238 #define _(a,b) if (mp->a) flow_hash_config |= b;
3239     foreach_flow_hash_bit;
3240 #undef _
3241
3242     rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
3243
3244     REPLY_MACRO(VL_API_SET_IP_FLOW_HASH_REPLY);
3245 }
3246
3247
3248 static void vl_api_set_ip_flow_hash_t_handler
3249 (vl_api_set_ip_flow_hash_t *mp)
3250 {
3251     if (mp->is_ipv6 == 0)
3252         set_ip4_flow_hash (mp);
3253     else
3254         set_ip6_flow_hash (mp);
3255 }
3256
3257 static void vl_api_sw_interface_set_unnumbered_t_handler
3258 (vl_api_sw_interface_set_unnumbered_t *mp)
3259 {
3260     vl_api_sw_interface_set_unnumbered_reply_t * rmp;
3261     int rv = 0;
3262     vnet_sw_interface_t * si;
3263     vnet_main_t *vnm = vnet_get_main();
3264     u32 sw_if_index, unnumbered_sw_if_index;
3265
3266     sw_if_index = ntohl(mp->sw_if_index);
3267     unnumbered_sw_if_index = ntohl(mp->unnumbered_sw_if_index);
3268
3269     /*
3270      * The API message field names are backwards from
3271      * the underlying data structure names.
3272      * It's not worth changing them now.
3273      */
3274     if (pool_is_free_index (vnm->interface_main.sw_interfaces,
3275                             unnumbered_sw_if_index)) {
3276         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
3277         goto done;
3278     }
3279
3280     /* Only check the "use loop0" field when setting the binding */
3281     if (mp->is_add &&
3282         pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index)) {
3283         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
3284         goto done;
3285     }
3286
3287     si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
3288
3289     if (mp->is_add) {
3290         si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
3291         si->unnumbered_sw_if_index = sw_if_index;
3292     } else {
3293         si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
3294         si->unnumbered_sw_if_index = (u32)~0;
3295     }
3296
3297  done:
3298     REPLY_MACRO(VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
3299 }
3300
3301 static void vl_api_create_loopback_t_handler
3302 (vl_api_create_loopback_t *mp)
3303 {
3304     vl_api_create_loopback_reply_t * rmp;
3305     u32 sw_if_index;
3306     int rv;
3307
3308     rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address);
3309
3310     REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
3311     ({
3312         rmp->sw_if_index = ntohl (sw_if_index);
3313     }));
3314 }
3315
3316 static void vl_api_delete_loopback_t_handler
3317 (vl_api_delete_loopback_t *mp)
3318 {
3319     vl_api_delete_loopback_reply_t * rmp;
3320     u32 sw_if_index;
3321     int rv;
3322
3323     sw_if_index = ntohl (mp->sw_if_index);
3324     rv = vnet_delete_loopback_interface (sw_if_index);
3325
3326     REPLY_MACRO(VL_API_DELETE_LOOPBACK_REPLY);
3327 }
3328
3329 static void vl_api_control_ping_t_handler
3330 (vl_api_control_ping_t *mp)
3331 {
3332     vl_api_control_ping_reply_t * rmp;
3333     int rv = 0;
3334
3335     REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
3336     ({
3337         rmp->vpe_pid = ntohl (getpid());
3338     }));
3339 }
3340
3341 static void shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
3342 {
3343     u8 **shmem_vecp = (u8 **)arg;
3344     u8 *shmem_vec;
3345     void *oldheap;
3346     api_main_t * am = &api_main;
3347     u32 offset;
3348
3349     shmem_vec = *shmem_vecp;
3350
3351     offset = vec_len (shmem_vec);
3352
3353     pthread_mutex_lock (&am->vlib_rp->mutex);
3354     oldheap = svm_push_data_heap (am->vlib_rp);
3355
3356     vec_validate (shmem_vec, offset + buffer_bytes - 1);
3357
3358     clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
3359
3360     svm_pop_heap (oldheap);
3361     pthread_mutex_unlock (&am->vlib_rp->mutex);
3362
3363     *shmem_vecp = shmem_vec;
3364 }
3365
3366
3367 static void vl_api_cli_request_t_handler
3368 (vl_api_cli_request_t *mp)
3369 {
3370     vl_api_cli_reply_t *rp;
3371     unix_shared_memory_queue_t *q;
3372     vlib_main_t * vm = vlib_get_main();
3373     api_main_t * am = &api_main;
3374     unformat_input_t input;
3375     u8 *shmem_vec=0;
3376     void *oldheap;
3377
3378     q = vl_api_client_index_to_input_queue (mp->client_index);
3379     if (!q)
3380         return;
3381
3382     rp = vl_msg_api_alloc (sizeof (*rp));
3383     rp->_vl_msg_id = ntohs(VL_API_CLI_REPLY);
3384     rp->context = mp->context;
3385
3386     unformat_init_vector (&input, (u8 *)(uword)mp->cmd_in_shmem);
3387
3388     vlib_cli_input (vm, &input, shmem_cli_output,
3389                     (uword)&shmem_vec);
3390
3391     pthread_mutex_lock (&am->vlib_rp->mutex);
3392     oldheap = svm_push_data_heap (am->vlib_rp);
3393
3394     vec_add1(shmem_vec, 0);
3395
3396     svm_pop_heap (oldheap);
3397     pthread_mutex_unlock (&am->vlib_rp->mutex);
3398
3399     rp->reply_in_shmem = (uword)shmem_vec;
3400
3401     vl_msg_api_send_shmem (q, (u8 *)&rp);
3402 }
3403
3404 static void vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t *mp)
3405 {
3406     int rv;
3407     vl_api_set_arp_neighbor_limit_reply_t * rmp;
3408     vnet_main_t *vnm = vnet_get_main();
3409     clib_error_t * error;
3410
3411     vnm->api_errno = 0;
3412
3413     if (mp->is_ipv6)
3414         error = ip6_set_neighbor_limit (ntohl(mp->arp_neighbor_limit));
3415     else
3416         error = ip4_set_arp_limit (ntohl(mp->arp_neighbor_limit));
3417
3418     if (error)  {
3419         clib_error_report(error);
3420         rv = VNET_API_ERROR_UNSPECIFIED;
3421     } else {
3422         rv = vnm->api_errno;
3423     }
3424
3425     REPLY_MACRO(VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3426 }
3427
3428 static void vl_api_sr_tunnel_add_del_t_handler
3429 (vl_api_sr_tunnel_add_del_t *mp)
3430 {
3431 #if IPV6SR == 0
3432     clib_warning ("unimplemented");
3433 #else
3434     ip6_sr_add_del_tunnel_args_t _a, *a=&_a;
3435     int rv = 0;
3436     vl_api_sr_tunnel_add_del_reply_t * rmp;
3437     ip6_address_t * segments = 0, * seg;
3438     ip6_address_t * tags = 0, *tag;
3439     ip6_address_t * this_address;
3440     int i;
3441
3442     if (mp->n_segments == 0) {
3443         rv = -11;
3444         goto out;
3445     }
3446
3447     memset (a, 0, sizeof (*a));
3448     a->src_address = (ip6_address_t *)&mp->src_address;
3449     a->dst_address = (ip6_address_t *)&mp->dst_address;
3450     a->dst_mask_width = mp->dst_mask_width;
3451     a->flags_net_byte_order = mp->flags_net_byte_order;
3452     a->is_del = (mp->is_add == 0);
3453     a->rx_table_id = ntohl(mp->outer_vrf_id);
3454     a->tx_table_id = ntohl(mp->inner_vrf_id);
3455
3456     a->name = format(0, "%s", mp->name);
3457     if (!(vec_len(a->name)))
3458       a->name = 0;
3459
3460     a->policy_name = format(0, "%s", mp->policy_name);
3461     if (!(vec_len(a->policy_name)))
3462       a->policy_name = 0;
3463
3464     /* Yank segments and tags out of the API message */
3465     this_address = (ip6_address_t *)mp->segs_and_tags;
3466     for (i = 0; i < mp->n_segments; i++) {
3467         vec_add2 (segments, seg, 1);
3468         clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
3469         this_address++;
3470     }
3471     for (i = 0; i < mp->n_tags; i++) {
3472         vec_add2 (tags, tag, 1);
3473         clib_memcpy (tag->as_u8, this_address->as_u8, sizeof (*this_address));
3474         this_address++;
3475     }
3476
3477     a->segments = segments;
3478     a->tags = tags;
3479
3480     rv = ip6_sr_add_del_tunnel (a);
3481
3482 out:
3483
3484     REPLY_MACRO(VL_API_SR_TUNNEL_ADD_DEL_REPLY);
3485 #endif
3486 }
3487
3488 static void vl_api_sr_policy_add_del_t_handler
3489 (vl_api_sr_policy_add_del_t *mp)
3490 {
3491 #if IPV6SR == 0
3492     clib_warning ("unimplemented");
3493 #else
3494     ip6_sr_add_del_policy_args_t _a, *a=&_a;
3495     int rv = 0;
3496     vl_api_sr_policy_add_del_reply_t * rmp;
3497     int i;
3498
3499     memset (a, 0, sizeof (*a));
3500     a->is_del = (mp->is_add == 0);
3501
3502     a->name = format(0, "%s", mp->name);
3503     if (!(vec_len(a->name)))
3504       {
3505         rv = VNET_API_ERROR_NO_SUCH_NODE2;
3506         goto out;
3507       }
3508
3509     if (!(mp->tunnel_names[0]))
3510       {
3511         rv = VNET_API_ERROR_NO_SUCH_NODE2;
3512         goto out;
3513       }
3514
3515     // start deserializing tunnel_names
3516     int num_tunnels = mp->tunnel_names[0]; //number of tunnels
3517     u8 * deser_tun_names = mp->tunnel_names;
3518     deser_tun_names += 1; //moving along
3519
3520     u8 * tun_name = 0;
3521     int tun_name_len = 0;
3522
3523     for (i=0; i < num_tunnels; i++)
3524       {
3525         tun_name_len= *deser_tun_names;
3526         deser_tun_names += 1;
3527         vec_resize (tun_name, tun_name_len);
3528         memcpy(tun_name, deser_tun_names, tun_name_len);
3529         vec_add1 (a->tunnel_names, tun_name);
3530         deser_tun_names += tun_name_len;
3531         tun_name = 0;
3532       }
3533
3534     rv = ip6_sr_add_del_policy (a);
3535
3536 out:
3537
3538     REPLY_MACRO(VL_API_SR_POLICY_ADD_DEL_REPLY);
3539 #endif
3540 }
3541
3542 static void vl_api_sr_multicast_map_add_del_t_handler
3543 (vl_api_sr_multicast_map_add_del_t *mp)
3544 {
3545 #if IPV6SR == 0
3546     clib_warning ("unimplemented");
3547 #else
3548     ip6_sr_add_del_multicastmap_args_t _a, *a=&_a;
3549     int rv = 0;
3550     vl_api_sr_multicast_map_add_del_reply_t * rmp;
3551
3552     memset (a, 0, sizeof (*a));
3553     a->is_del = (mp->is_add == 0);
3554
3555     a->multicast_address = (ip6_address_t *)&mp->multicast_address;
3556     a->policy_name = format(0, "%s", mp->policy_name);
3557
3558     if (a->multicast_address == 0)
3559       {
3560         rv = -1 ;
3561         goto out;
3562       }
3563
3564     if (!(a->policy_name))
3565       {
3566         rv = -2 ;
3567         goto out;
3568       }
3569
3570 #if DPDK > 0 /* Cannot call replicate without DPDK */
3571     rv = ip6_sr_add_del_multicastmap (a);
3572 #else
3573     clib_warning ("multicast replication without DPDK not implemented");
3574     rv = VNET_API_ERROR_UNIMPLEMENTED;
3575 #endif /* DPDK */
3576
3577 out:
3578
3579     REPLY_MACRO(VL_API_SR_MULTICAST_MAP_ADD_DEL_REPLY);
3580 #endif
3581 }
3582
3583 #define foreach_classify_add_del_table_field    \
3584 _(table_index)                                  \
3585 _(nbuckets)                                     \
3586 _(memory_size)                                  \
3587 _(skip_n_vectors)                               \
3588 _(match_n_vectors)                              \
3589 _(next_table_index)                             \
3590 _(miss_next_index)
3591
3592 static void vl_api_classify_add_del_table_t_handler
3593 (vl_api_classify_add_del_table_t * mp)
3594 {
3595     vl_api_classify_add_del_table_reply_t * rmp;
3596     vnet_classify_main_t * cm = &vnet_classify_main;
3597     vnet_classify_table_t * t;
3598     int rv;
3599
3600 #define _(a) u32 a;
3601     foreach_classify_add_del_table_field;
3602 #undef _
3603
3604 #define _(a) a = ntohl(mp->a);
3605     foreach_classify_add_del_table_field;
3606 #undef _
3607
3608     /* The underlying API fails silently, on purpose, so check here */
3609     if (mp->is_add == 0)
3610         if (pool_is_free_index (cm->tables, table_index)) {
3611             rv = VNET_API_ERROR_NO_SUCH_TABLE;
3612             goto out;
3613         }
3614
3615     rv = vnet_classify_add_del_table
3616         (cm, mp->mask, nbuckets, memory_size,
3617          skip_n_vectors, match_n_vectors,
3618          next_table_index, miss_next_index,
3619          &table_index, mp->is_add);
3620
3621 out:
3622     REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
3623     ({
3624         if (rv == 0 && mp->is_add) {
3625             t = pool_elt_at_index (cm->tables, table_index);
3626             rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
3627             rmp->match_n_vectors = ntohl(t->match_n_vectors);
3628             rmp->new_table_index = ntohl(table_index);
3629         } else {
3630             rmp->skip_n_vectors = ~0;
3631             rmp->match_n_vectors = ~0;
3632             rmp->new_table_index = ~0;
3633         }
3634     }));
3635 }
3636
3637 static void vl_api_classify_add_del_session_t_handler
3638 (vl_api_classify_add_del_session_t * mp)
3639 {
3640     vnet_classify_main_t * cm = &vnet_classify_main;
3641     vl_api_classify_add_del_session_reply_t * rmp;
3642     int rv;
3643     u32 table_index, hit_next_index, opaque_index;
3644     i32 advance;
3645
3646     table_index = ntohl (mp->table_index);
3647     hit_next_index = ntohl (mp->hit_next_index);
3648     opaque_index = ntohl (mp->opaque_index);
3649     advance = ntohl (mp->advance);
3650
3651     rv = vnet_classify_add_del_session
3652         (cm, table_index, mp->match, hit_next_index, opaque_index,
3653          advance, mp->is_add);
3654
3655     REPLY_MACRO(VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
3656 }
3657
3658 static void vl_api_classify_set_interface_ip_table_t_handler
3659 (vl_api_classify_set_interface_ip_table_t * mp)
3660 {
3661     vlib_main_t * vm = vlib_get_main();
3662     vl_api_classify_set_interface_ip_table_reply_t * rmp;
3663     int rv;
3664     u32 table_index, sw_if_index;
3665
3666     table_index = ntohl (mp->table_index);
3667     sw_if_index = ntohl (mp->sw_if_index);
3668
3669     VALIDATE_SW_IF_INDEX(mp);
3670
3671     if (mp->is_ipv6)
3672         rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3673     else
3674         rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
3675
3676     BAD_SW_IF_INDEX_LABEL;
3677
3678     REPLY_MACRO(VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
3679 }
3680
3681 static void vl_api_classify_set_interface_l2_tables_t_handler
3682 (vl_api_classify_set_interface_l2_tables_t * mp)
3683 {
3684     vl_api_classify_set_interface_l2_tables_reply_t * rmp;
3685     int rv;
3686     u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
3687     int enable;
3688
3689     ip4_table_index = ntohl(mp->ip4_table_index);
3690     ip6_table_index = ntohl(mp->ip6_table_index);
3691     other_table_index = ntohl(mp->other_table_index);
3692     sw_if_index = ntohl(mp->sw_if_index);
3693
3694     VALIDATE_SW_IF_INDEX(mp);
3695
3696     rv = vnet_l2_classify_set_tables (sw_if_index, ip4_table_index,
3697                                       ip6_table_index, other_table_index);
3698
3699     if (rv == 0) {
3700         if (ip4_table_index != ~0 || ip6_table_index != ~0
3701             || other_table_index != ~0)
3702             enable = 1;
3703         else
3704             enable = 0;
3705
3706         vnet_l2_classify_enable_disable (sw_if_index, enable);
3707     }
3708
3709     BAD_SW_IF_INDEX_LABEL;
3710
3711     REPLY_MACRO(VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
3712 }
3713
3714 static void
3715 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t *mp)
3716 {
3717     int rv = 0;
3718     vl_api_l2_fib_clear_table_reply_t * rmp;
3719
3720     /* DAW-FIXME: This API should only clear non-static l2fib entries, but
3721      *            that is not currently implemented.  When that TODO is fixed
3722      *            this call should be changed to pass 1 instead of 0.
3723      */
3724     l2fib_clear_table (0);
3725
3726     REPLY_MACRO(VL_API_L2_FIB_CLEAR_TABLE_REPLY);
3727 }
3728
3729 extern void l2_efp_filter_configure(vnet_main_t * vnet_main,
3730                                     u32           sw_if_index,
3731                                     u32           enable);
3732
3733 static void
3734 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *mp)
3735 {
3736     int rv;
3737     vl_api_l2_interface_efp_filter_reply_t * rmp;
3738     vnet_main_t *vnm = vnet_get_main();
3739
3740     // enable/disable the feature
3741     l2_efp_filter_configure (vnm, mp->sw_if_index, mp->enable_disable);
3742     rv = vnm->api_errno;
3743
3744     REPLY_MACRO(VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
3745 }
3746
3747 static void
3748 vl_api_l2_interface_vlan_tag_rewrite_t_handler (vl_api_l2_interface_vlan_tag_rewrite_t *mp)
3749 {
3750     int rv = 0;
3751     vl_api_l2_interface_vlan_tag_rewrite_reply_t * rmp;
3752     vnet_main_t * vnm = vnet_get_main();
3753     vlib_main_t * vm = vlib_get_main();
3754     u32 vtr_op;
3755
3756     VALIDATE_SW_IF_INDEX(mp);
3757
3758     vtr_op = ntohl(mp->vtr_op);
3759
3760     /* The L2 code is unsuspicious */
3761     switch(vtr_op) {
3762     case L2_VTR_DISABLED:
3763     case L2_VTR_PUSH_1:
3764     case L2_VTR_PUSH_2:
3765     case L2_VTR_POP_1:
3766     case L2_VTR_POP_2:
3767     case L2_VTR_TRANSLATE_1_1:
3768     case L2_VTR_TRANSLATE_1_2:
3769     case L2_VTR_TRANSLATE_2_1:
3770     case L2_VTR_TRANSLATE_2_2:
3771         break;
3772
3773     default:
3774         rv = VNET_API_ERROR_INVALID_VALUE;
3775         goto bad_sw_if_index;
3776     }
3777
3778     rv = l2vtr_configure (vm, vnm, ntohl(mp->sw_if_index), vtr_op,
3779                           ntohl(mp->push_dot1q), ntohl(mp->tag1),
3780                           ntohl(mp->tag2));
3781
3782     BAD_SW_IF_INDEX_LABEL;
3783
3784     REPLY_MACRO(VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
3785 }
3786
3787 static void
3788 vl_api_create_vhost_user_if_t_handler (vl_api_create_vhost_user_if_t *mp)
3789 {
3790 #if DPDK > 0
3791     int rv = 0;
3792     vl_api_create_vhost_user_if_reply_t * rmp;
3793     u32 sw_if_index = (u32)~0;
3794
3795     vnet_main_t * vnm = vnet_get_main();
3796     vlib_main_t * vm = vlib_get_main();
3797
3798     rv = dpdk_vhost_user_create_if(vnm, vm, (char *)mp->sock_filename,
3799                               mp->is_server, &sw_if_index, (u64)~0,
3800                               mp->renumber, ntohl(mp->custom_dev_instance),
3801                               (mp->use_custom_mac)?mp->mac_address:NULL);
3802
3803     REPLY_MACRO2(VL_API_CREATE_VHOST_USER_IF_REPLY,
3804     ({
3805       rmp->sw_if_index = ntohl (sw_if_index);
3806     }));
3807 #endif
3808 }
3809
3810 static void
3811 vl_api_modify_vhost_user_if_t_handler (vl_api_modify_vhost_user_if_t *mp)
3812 {
3813 #if DPDK > 0
3814     int rv = 0;
3815     vl_api_modify_vhost_user_if_reply_t * rmp;
3816     u32 sw_if_index = ntohl(mp->sw_if_index);
3817
3818     vnet_main_t * vnm = vnet_get_main();
3819     vlib_main_t * vm = vlib_get_main();
3820
3821     rv = dpdk_vhost_user_modify_if(vnm, vm, (char *)mp->sock_filename,
3822                               mp->is_server, sw_if_index, (u64)~0,
3823                               mp->renumber, ntohl(mp->custom_dev_instance));
3824
3825     REPLY_MACRO(VL_API_MODIFY_VHOST_USER_IF_REPLY);
3826 #endif
3827 }
3828
3829 static void
3830 vl_api_delete_vhost_user_if_t_handler (vl_api_delete_vhost_user_if_t *mp)
3831 {
3832 #if DPDK > 0
3833     int rv = 0;
3834     vpe_api_main_t * vam = &vpe_api_main;
3835     vl_api_delete_vhost_user_if_reply_t * rmp;
3836     u32 sw_if_index = ntohl(mp->sw_if_index);
3837
3838     vnet_main_t * vnm = vnet_get_main();
3839     vlib_main_t * vm = vlib_get_main();
3840
3841     rv = dpdk_vhost_user_delete_if(vnm, vm, sw_if_index);
3842
3843     REPLY_MACRO(VL_API_DELETE_VHOST_USER_IF_REPLY);
3844     if (!rv) {
3845         unix_shared_memory_queue_t * q =
3846             vl_api_client_index_to_input_queue (mp->client_index);
3847         if (!q)
3848             return;
3849
3850         send_sw_interface_flags_deleted (vam, q, sw_if_index);
3851     }
3852 #endif
3853 }
3854
3855 static void vl_api_sw_interface_vhost_user_details_t_handler (
3856     vl_api_sw_interface_vhost_user_details_t * mp)
3857 {
3858     clib_warning ("BUG");
3859 }
3860
3861 #if DPDK > 0
3862 static void send_sw_interface_vhost_user_details (vpe_api_main_t * am,
3863                                        unix_shared_memory_queue_t *q,
3864                                        vhost_user_intf_details_t * vui,
3865                                        u32 context)
3866 {
3867     vl_api_sw_interface_vhost_user_details_t * mp;
3868
3869     mp = vl_msg_api_alloc (sizeof (*mp));
3870     memset (mp, 0, sizeof (*mp));
3871     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_VHOST_USER_DETAILS);
3872     mp->sw_if_index = ntohl(vui->sw_if_index);
3873     mp->virtio_net_hdr_sz = ntohl (vui->virtio_net_hdr_sz);
3874     mp->features = clib_net_to_host_u64 (vui->features);
3875     mp->is_server = vui->is_server;
3876     mp->num_regions = ntohl(vui->num_regions);
3877     mp->sock_errno = ntohl(vui->sock_errno);
3878     mp->context = context;
3879
3880     strncpy ((char *) mp->sock_filename,
3881              (char *) vui->sock_filename, ARRAY_LEN(mp->sock_filename)-1);
3882     strncpy ((char *) mp->interface_name,
3883              (char *) vui->if_name, ARRAY_LEN(mp->interface_name)-1);
3884
3885     vl_msg_api_send_shmem (q, (u8 *)&mp);
3886 }
3887 #endif
3888
3889 static void
3890 vl_api_sw_interface_vhost_user_dump_t_handler (
3891         vl_api_sw_interface_vhost_user_dump_t *mp)
3892 {
3893 #if DPDK > 0
3894     int rv = 0;
3895     vpe_api_main_t * am = &vpe_api_main;
3896     vnet_main_t * vnm = vnet_get_main();
3897     vlib_main_t * vm = vlib_get_main();
3898     vhost_user_intf_details_t *ifaces = NULL;
3899     vhost_user_intf_details_t *vuid = NULL;
3900     unix_shared_memory_queue_t * q;
3901
3902     q = vl_api_client_index_to_input_queue (mp->client_index);
3903     if (q == 0)
3904         return;
3905
3906     rv = dpdk_vhost_user_dump_ifs(vnm, vm, &ifaces);
3907     if (rv)
3908         return;
3909
3910     vec_foreach (vuid, ifaces) {
3911         send_sw_interface_vhost_user_details (am, q, vuid, mp->context);
3912     }
3913     vec_free(ifaces);
3914 #endif
3915 }
3916
3917 static void send_sw_if_l2tpv3_tunnel_details (vpe_api_main_t * am,
3918                                        unix_shared_memory_queue_t *q,
3919                                        l2t_session_t *s,
3920                                        l2t_main_t * lm,
3921                                        u32 context)
3922 {
3923     vl_api_sw_if_l2tpv3_tunnel_details_t * mp;
3924     u8 * if_name = NULL;
3925     vnet_sw_interface_t * si = NULL;
3926
3927     si = vnet_get_hw_sw_interface (lm->vnet_main, s->hw_if_index);
3928
3929     if_name = format(if_name, "%U",
3930                      format_vnet_sw_interface_name, lm->vnet_main, si);
3931
3932     mp = vl_msg_api_alloc (sizeof (*mp));
3933     memset (mp, 0, sizeof (*mp));
3934     mp->_vl_msg_id = ntohs(VL_API_SW_IF_L2TPV3_TUNNEL_DETAILS);
3935     strncpy((char *)mp->interface_name,
3936             (char *)if_name, ARRAY_LEN(mp->interface_name)-1);
3937     mp->sw_if_index = ntohl(si->sw_if_index);
3938     mp->local_session_id = s->local_session_id;
3939     mp->remote_session_id = s->remote_session_id;
3940     mp->local_cookie[0] = s->local_cookie[0];
3941     mp->local_cookie[1] = s->local_cookie[1];
3942     mp->remote_cookie = s->remote_cookie;
3943     clib_memcpy(mp->client_address, &s->client_address, sizeof(s->client_address));
3944     clib_memcpy(mp->our_address, &s->our_address, sizeof(s->our_address));
3945     mp->l2_sublayer_present = s->l2_sublayer_present;
3946     mp->context = context;
3947
3948     vl_msg_api_send_shmem (q, (u8 *)&mp);
3949 }
3950
3951 static void send_ip_address_details (vpe_api_main_t * am,
3952                                      unix_shared_memory_queue_t * q,
3953                                      u8 * ip,
3954                                      u16 prefix_length,
3955                                      u8 is_ipv6,
3956                                      u32 context)
3957 {
3958     vl_api_ip_address_details_t * mp;
3959
3960     mp = vl_msg_api_alloc (sizeof (*mp));
3961     memset (mp, 0, sizeof (*mp));
3962     mp->_vl_msg_id = ntohs(VL_API_IP_ADDRESS_DETAILS);
3963
3964     if (is_ipv6) {
3965         clib_memcpy(&mp->ip, ip, sizeof(mp->ip));
3966     } else {
3967         u32 * tp = (u32 *)mp->ip;
3968         *tp = ntohl(*(u32*)ip);
3969     }
3970     mp->prefix_length = prefix_length;
3971     mp->context = context;
3972
3973     vl_msg_api_send_shmem (q, (u8 *)&mp);
3974 }
3975
3976 static void
3977 vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t *mp)
3978 {
3979     vpe_api_main_t * am = &vpe_api_main;
3980     unix_shared_memory_queue_t * q;
3981     ip6_address_t * r6;
3982     ip4_address_t * r4;
3983     ip6_main_t * im6 = &ip6_main;
3984     ip4_main_t * im4 = &ip4_main;
3985     ip_lookup_main_t * lm6 = &im6->lookup_main;
3986     ip_lookup_main_t * lm4 = &im4->lookup_main;
3987     ip_interface_address_t * ia = 0;
3988     u32 sw_if_index = ~0;
3989     int rv __attribute__ ((unused)) = 0;
3990
3991     VALIDATE_SW_IF_INDEX(mp);
3992
3993     sw_if_index = ntohl(mp->sw_if_index);
3994
3995     q = vl_api_client_index_to_input_queue (mp->client_index);
3996     if (q == 0) {
3997         return;
3998     }
3999
4000     if (mp->is_ipv6) {
4001         foreach_ip_interface_address (lm6, ia, sw_if_index,
4002                                       1 /* honor unnumbered */,
4003         ({
4004             r6 = ip_interface_address_get_address (lm6, ia);
4005             u16 prefix_length = ia->address_length;
4006             send_ip_address_details(am, q, (u8*)r6, prefix_length, 1, mp->context);
4007         }));
4008     } else {
4009         foreach_ip_interface_address (lm4, ia, sw_if_index,
4010                                       1 /* honor unnumbered */,
4011         ({
4012             r4 = ip_interface_address_get_address (lm4, ia);
4013             u16 prefix_length = ia->address_length;
4014             send_ip_address_details(am, q, (u8*)r4, prefix_length, 0, mp->context);
4015         }));
4016     }
4017     BAD_SW_IF_INDEX_LABEL;
4018 }
4019
4020 static void send_ip_details (vpe_api_main_t * am,
4021                                unix_shared_memory_queue_t *q,
4022                                u32 sw_if_index,
4023                                u32 context)
4024 {
4025     vl_api_ip_details_t * mp;
4026
4027     mp = vl_msg_api_alloc (sizeof (*mp));
4028     memset (mp, 0, sizeof (*mp));
4029     mp->_vl_msg_id = ntohs(VL_API_IP_DETAILS);
4030
4031     mp->sw_if_index = ntohl(sw_if_index);
4032     mp->context = context;
4033
4034     vl_msg_api_send_shmem (q, (u8 *)&mp);
4035 }
4036
4037 static void
4038 vl_api_sw_if_l2tpv3_tunnel_dump_t_handler (
4039         vl_api_sw_if_l2tpv3_tunnel_dump_t *mp)
4040 {
4041     vpe_api_main_t * am = &vpe_api_main;
4042     l2t_main_t * lm = &l2t_main;
4043     unix_shared_memory_queue_t * q;
4044     l2t_session_t *session;
4045
4046     q = vl_api_client_index_to_input_queue (mp->client_index);
4047     if (q == 0)
4048         return;
4049
4050     pool_foreach (session, lm->sessions,
4051     ({
4052         send_sw_if_l2tpv3_tunnel_details (am, q, session, lm, mp->context);
4053     }));
4054 }
4055
4056
4057 static void send_sw_interface_tap_details (vpe_api_main_t * am,
4058                                        unix_shared_memory_queue_t *q,
4059                                        tapcli_interface_details_t *tap_if,
4060                                        u32 context)
4061 {
4062     vl_api_sw_interface_tap_details_t * mp;
4063     mp = vl_msg_api_alloc (sizeof (*mp));
4064     memset (mp, 0, sizeof (*mp));
4065     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_TAP_DETAILS);
4066     mp->sw_if_index = ntohl(tap_if->sw_if_index);
4067     strncpy((char *)mp->dev_name,
4068             (char *)tap_if->dev_name, ARRAY_LEN(mp->dev_name)-1);
4069     mp->context = context;
4070
4071     vl_msg_api_send_shmem (q, (u8 *)&mp);
4072 }
4073
4074 static void
4075 vl_api_sw_interface_tap_dump_t_handler (
4076         vl_api_sw_interface_tap_dump_t *mp)
4077 {
4078     int rv = 0;
4079     vpe_api_main_t * am = &vpe_api_main;
4080     unix_shared_memory_queue_t * q;
4081     tapcli_interface_details_t *tapifs = NULL;
4082     tapcli_interface_details_t *tap_if = NULL;
4083
4084     q = vl_api_client_index_to_input_queue (mp->client_index);
4085     if (q == 0)
4086         return;
4087
4088     rv = vnet_tap_dump_ifs(&tapifs);
4089     if (rv)
4090         return;
4091
4092     vec_foreach(tap_if, tapifs) {
4093         send_sw_interface_tap_details(am, q, tap_if, mp->context);
4094     }
4095
4096     vec_free(tapifs);
4097 }
4098
4099 static void
4100 vl_api_ip_dump_t_handler (vl_api_ip_dump_t *mp)
4101 {
4102     vpe_api_main_t * am = &vpe_api_main;
4103     vnet_main_t * vnm = vnet_get_main();
4104     vlib_main_t * vm = vlib_get_main();
4105     vnet_interface_main_t * im = &vnm->interface_main;
4106     unix_shared_memory_queue_t * q;
4107     vnet_sw_interface_t * si, * sorted_sis;
4108     u32 sw_if_index = ~0;
4109
4110     q = vl_api_client_index_to_input_queue (mp->client_index);
4111     if (q == 0) {
4112         return;
4113     }
4114
4115     /* Gather interfaces. */
4116     sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
4117     _vec_len (sorted_sis) = 0;
4118     pool_foreach (si, im->sw_interfaces, ({ vec_add1 (sorted_sis, si[0]); }));
4119
4120     vec_foreach (si, sorted_sis) {
4121         if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)) {
4122             if (mp->is_ipv6 && !ip6_interface_enabled(vm, si->sw_if_index)) {
4123                 continue;
4124             }
4125             sw_if_index = si->sw_if_index;
4126             send_ip_details(am, q, sw_if_index, mp->context);
4127         }
4128     }
4129 }
4130
4131 static void vl_api_l2_fib_table_entry_t_handler (
4132     vl_api_l2_fib_table_entry_t * mp)
4133 {
4134     clib_warning ("BUG");
4135 }
4136
4137 static void send_l2fib_table_entry (vpe_api_main_t * am,
4138                                     unix_shared_memory_queue_t *q,
4139                                     l2fib_entry_key_t * l2fe_key,
4140                                     l2fib_entry_result_t * l2fe_res,
4141                                     u32 context)
4142 {
4143     vl_api_l2_fib_table_entry_t * mp;
4144
4145     mp = vl_msg_api_alloc (sizeof (*mp));
4146     memset (mp, 0, sizeof (*mp));
4147     mp->_vl_msg_id = ntohs(VL_API_L2_FIB_TABLE_ENTRY);
4148
4149     mp->bd_id = ntohl(l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
4150
4151     mp->mac = l2fib_make_key (l2fe_key->fields.mac, 0);
4152     mp->sw_if_index = ntohl(l2fe_res->fields.sw_if_index);
4153     mp->static_mac = l2fe_res->fields.static_mac;
4154     mp->filter_mac = l2fe_res->fields.filter;
4155     mp->bvi_mac = l2fe_res->fields.bvi;
4156     mp->context = context;
4157
4158     vl_msg_api_send_shmem (q, (u8 *)&mp);
4159 }
4160
4161 static void
4162 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t *mp)
4163 {
4164     vpe_api_main_t * am = &vpe_api_main;
4165     bd_main_t * bdm = &bd_main;
4166     l2fib_entry_key_t *l2fe_key = NULL;
4167     l2fib_entry_result_t *l2fe_res = NULL;
4168     u32 ni, bd_id = ntohl (mp->bd_id);
4169     u32 bd_index;
4170     unix_shared_memory_queue_t * q;
4171     uword * p;
4172
4173     q = vl_api_client_index_to_input_queue (mp->client_index);
4174     if (q == 0)
4175         return;
4176
4177     /* see l2fib_table_dump: ~0 means "any" */
4178     if (bd_id == ~0)
4179         bd_index = ~0;
4180     else {
4181         p = hash_get (bdm->bd_index_by_bd_id, bd_id);
4182         if (p == 0)
4183             return;
4184
4185         bd_index = p[0];
4186     }
4187
4188     l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
4189
4190     vec_foreach_index (ni, l2fe_key) {
4191         send_l2fib_table_entry (am, q, vec_elt_at_index(l2fe_key, ni),
4192                                 vec_elt_at_index(l2fe_res, ni), mp->context);
4193     }
4194     vec_free(l2fe_key);
4195     vec_free(l2fe_res);
4196 }
4197
4198 static void
4199 vl_api_show_version_t_handler (vl_api_show_version_t *mp)
4200 {
4201     vl_api_show_version_reply_t *rmp;
4202     int rv = 0;
4203     char * vpe_api_get_build_directory(void);
4204     char * vpe_api_get_version(void);
4205     char * vpe_api_get_build_date(void);
4206
4207     unix_shared_memory_queue_t * q =
4208         vl_api_client_index_to_input_queue (mp->client_index);
4209
4210     if (!q)
4211         return;
4212
4213     REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
4214     ({
4215         strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
4216         strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
4217                  ARRAY_LEN(rmp->build_directory)-1);
4218         strncpy ((char *) rmp->version, vpe_api_get_version(),
4219                  ARRAY_LEN(rmp->version)-1);
4220         strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
4221                  ARRAY_LEN(rmp->build_date)-1);
4222     }));
4223 }
4224
4225 static void vl_api_get_node_index_t_handler
4226 (vl_api_get_node_index_t * mp)
4227 {
4228     vlib_main_t * vm = vlib_get_main();
4229     vl_api_get_node_index_reply_t * rmp;
4230     vlib_node_t * n;
4231     int rv = 0;
4232     u32 node_index = ~0;
4233
4234     n = vlib_get_node_by_name (vm, mp->node_name);
4235
4236     if (n == 0)
4237         rv = VNET_API_ERROR_NO_SUCH_NODE;
4238     else
4239         node_index = n->index;
4240
4241     REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
4242     ({
4243         rmp->node_index = ntohl(node_index);
4244     }))
4245 }
4246
4247 static void vl_api_add_node_next_t_handler
4248 (vl_api_add_node_next_t * mp)
4249 {
4250     vlib_main_t * vm = vlib_get_main();
4251     vl_api_add_node_next_reply_t * rmp;
4252     vlib_node_t * n, * next;
4253     int rv = 0;
4254     u32 next_index = ~0;
4255
4256     n = vlib_get_node_by_name (vm, mp->node_name);
4257
4258     if (n == 0) {
4259         rv = VNET_API_ERROR_NO_SUCH_NODE;
4260         goto out;
4261     }
4262
4263     next = vlib_get_node_by_name (vm, mp->next_name);
4264
4265     if (next == 0)
4266         rv = VNET_API_ERROR_NO_SUCH_NODE2;
4267     else
4268         next_index = vlib_node_add_next (vm, n->index, next->index);
4269
4270 out:
4271     REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
4272     ({
4273         rmp->next_index = ntohl(next_index);
4274     }))
4275 }
4276
4277 static void vl_api_l2tpv3_create_tunnel_t_handler
4278 (vl_api_l2tpv3_create_tunnel_t *mp)
4279 {
4280     vl_api_l2tpv3_create_tunnel_reply_t * rmp;
4281     l2t_main_t *lm = &l2t_main;
4282     u32 sw_if_index = (u32)~0;
4283     int rv;
4284
4285     if (mp->is_ipv6 != 1) {
4286         rv = VNET_API_ERROR_UNIMPLEMENTED;
4287         goto out;
4288     }
4289
4290     rv = create_l2tpv3_ipv6_tunnel (lm,
4291                                (ip6_address_t *) mp->client_address,
4292                                (ip6_address_t *) mp->our_address,
4293                                ntohl(mp->local_session_id),
4294                                ntohl(mp->remote_session_id),
4295                                clib_net_to_host_u64(mp->local_cookie),
4296                                clib_net_to_host_u64(mp->remote_cookie),
4297                                mp->l2_sublayer_present,
4298                                &sw_if_index);
4299
4300 out:
4301     REPLY_MACRO2(VL_API_L2TPV3_CREATE_TUNNEL_REPLY,
4302     ({
4303         rmp->sw_if_index = ntohl (sw_if_index);
4304     }))
4305 }
4306
4307 static void vl_api_l2tpv3_set_tunnel_cookies_t_handler
4308 (vl_api_l2tpv3_set_tunnel_cookies_t *mp)
4309 {
4310     vl_api_l2tpv3_set_tunnel_cookies_reply_t * rmp;
4311     l2t_main_t *lm = &l2t_main;
4312     int rv;
4313
4314     VALIDATE_SW_IF_INDEX(mp);
4315
4316     rv = l2tpv3_set_tunnel_cookies (lm, ntohl(mp->sw_if_index),
4317                                   clib_net_to_host_u64(mp->new_local_cookie),
4318                                   clib_net_to_host_u64(mp->new_remote_cookie));
4319
4320     BAD_SW_IF_INDEX_LABEL;
4321
4322     REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY);
4323 }
4324
4325 static void vl_api_l2tpv3_interface_enable_disable_t_handler
4326 (vl_api_l2tpv3_interface_enable_disable_t * mp)
4327 {
4328     int rv;
4329     vnet_main_t * vnm = vnet_get_main();
4330     vl_api_l2tpv3_interface_enable_disable_reply_t * rmp;
4331
4332     VALIDATE_SW_IF_INDEX(mp);
4333
4334     rv = l2tpv3_interface_enable_disable
4335         (vnm, ntohl(mp->sw_if_index), mp->enable_disable);
4336
4337     BAD_SW_IF_INDEX_LABEL;
4338
4339     REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY);
4340 }
4341
4342 static void vl_api_l2tpv3_set_lookup_key_t_handler
4343 (vl_api_l2tpv3_set_lookup_key_t * mp)
4344 {
4345     int rv = 0;
4346     l2t_main_t *lm = &l2t_main;
4347     vl_api_l2tpv3_set_lookup_key_reply_t * rmp;
4348
4349     if (mp->key > L2T_LOOKUP_SESSION_ID) {
4350         rv = VNET_API_ERROR_INVALID_VALUE;
4351         goto out;
4352     }
4353
4354     lm->lookup_type = mp->key;
4355
4356 out:
4357     REPLY_MACRO (VL_API_L2TPV3_SET_LOOKUP_KEY_REPLY);
4358 }
4359
4360 static void vl_api_vxlan_add_del_tunnel_t_handler
4361 (vl_api_vxlan_add_del_tunnel_t * mp)
4362 {
4363     vl_api_vxlan_add_del_tunnel_reply_t * rmp;
4364     int rv = 0;
4365     vnet_vxlan_add_del_tunnel_args_t _a, *a = &_a;
4366     u32 encap_fib_index;
4367     uword * p;
4368     ip4_main_t * im = &ip4_main;
4369     u32 sw_if_index = ~0;
4370
4371     p = hash_get (im->fib_index_by_table_id, ntohl(mp->encap_vrf_id));
4372     if (! p) {
4373         rv = VNET_API_ERROR_NO_SUCH_FIB;
4374         goto out;
4375     }
4376     encap_fib_index = p[0];
4377
4378     /* Check src & dst are different */
4379     if ((mp->is_ipv6 && memcmp(mp->src_address, mp->dst_address, 16) == 0) ||
4380        (!mp->is_ipv6 && memcmp(mp->src_address, mp->dst_address, 4) == 0)) {
4381         rv = VNET_API_ERROR_SAME_SRC_DST;
4382         goto out;
4383     }
4384     memset (a, 0, sizeof (*a));
4385
4386     a->is_add = mp->is_add;
4387     a->is_ip6 = mp->is_ipv6;
4388
4389     /* ip addresses sent in network byte order */
4390     if (a->is_ip6) {
4391         memcpy(&(a->src.ip6), mp->src_address, 16);
4392         memcpy(&(a->dst.ip6), mp->dst_address, 16);
4393     } else {
4394         memcpy(&(a->src.ip4), mp->src_address, 4);
4395         memcpy(&(a->dst.ip4), mp->dst_address, 4);
4396     }
4397
4398     a->encap_fib_index = encap_fib_index;
4399     a->decap_next_index = ntohl(mp->decap_next_index);
4400     a->vni = ntohl(mp->vni);
4401     rv = vnet_vxlan_add_del_tunnel (a, &sw_if_index);
4402
4403 out:
4404     REPLY_MACRO2(VL_API_VXLAN_ADD_DEL_TUNNEL_REPLY,
4405     ({
4406         rmp->sw_if_index = ntohl (sw_if_index);
4407     }));
4408 }
4409
4410 static void send_vxlan_tunnel_details
4411 (vxlan_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4412 {
4413     vl_api_vxlan_tunnel_details_t * rmp;
4414     ip4_main_t * im4 = &ip4_main;
4415     ip6_main_t * im6 = &ip6_main;
4416     u8 is_ipv6 = !(t->flags & VXLAN_TUNNEL_IS_IPV4);
4417
4418     rmp = vl_msg_api_alloc (sizeof (*rmp));
4419     memset (rmp, 0, sizeof (*rmp));
4420     rmp->_vl_msg_id = ntohs(VL_API_VXLAN_TUNNEL_DETAILS);
4421     if (is_ipv6) {
4422         memcpy(rmp->src_address, &(t->src.ip6), 16);
4423         memcpy(rmp->dst_address, &(t->dst.ip6), 16);
4424         rmp->encap_vrf_id = htonl(im6->fibs[t->encap_fib_index].table_id);
4425     } else {
4426         memcpy(rmp->src_address, &(t->src.ip4), 4);
4427         memcpy(rmp->dst_address, &(t->dst.ip4), 4);
4428         rmp->encap_vrf_id = htonl(im4->fibs[t->encap_fib_index].table_id);
4429     }
4430     rmp->vni = htonl(t->vni);
4431     rmp->decap_next_index = htonl(t->decap_next_index);
4432     rmp->sw_if_index = htonl(t->sw_if_index);
4433     rmp->is_ipv6 = is_ipv6;
4434     rmp->context = context;
4435
4436     vl_msg_api_send_shmem (q, (u8 *)&rmp);
4437 }
4438
4439 static void vl_api_vxlan_tunnel_dump_t_handler
4440 (vl_api_vxlan_tunnel_dump_t * mp)
4441 {
4442     unix_shared_memory_queue_t * q;
4443     vxlan_main_t * vxm = &vxlan_main;
4444     vxlan_tunnel_t * t;
4445     u32 sw_if_index;
4446
4447     q = vl_api_client_index_to_input_queue (mp->client_index);
4448     if (q == 0) {
4449         return;
4450     }
4451
4452     sw_if_index = ntohl(mp->sw_if_index);
4453
4454     if (~0 == sw_if_index) {
4455         pool_foreach (t, vxm->tunnels,
4456         ({
4457             send_vxlan_tunnel_details(t, q, mp->context);
4458         }));
4459     } else {
4460         if ((sw_if_index >= vec_len(vxm->tunnel_index_by_sw_if_index)) ||
4461                 (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index])) {
4462             return;
4463         }
4464         t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
4465         send_vxlan_tunnel_details(t, q, mp->context);
4466     }
4467 }
4468
4469 static void vl_api_gre_add_del_tunnel_t_handler
4470 (vl_api_gre_add_del_tunnel_t * mp)
4471 {
4472     vl_api_gre_add_del_tunnel_reply_t * rmp;
4473     int rv = 0;
4474     vnet_gre_add_del_tunnel_args_t _a, *a = &_a;
4475     u32 outer_table_id;
4476     uword * p;
4477     ip4_main_t * im = &ip4_main;
4478     u32 sw_if_index = ~0;
4479
4480     p = hash_get (im->fib_index_by_table_id, ntohl(mp->outer_table_id));
4481     if (! p) {
4482         rv = VNET_API_ERROR_NO_SUCH_FIB;
4483         goto out;
4484     }
4485     outer_table_id = p[0];
4486
4487     /* Check src & dst are different */
4488     if (memcmp(&mp->src_address, &mp->dst_address, 4) == 0) {
4489         rv = VNET_API_ERROR_SAME_SRC_DST;
4490         goto out;
4491     }
4492
4493     memset (a, 0, sizeof (*a));
4494
4495     a->is_add = mp->is_add;
4496
4497     /* ip addresses sent in network byte order */
4498     a->src.as_u32 = mp->src_address;
4499     a->dst.as_u32 = mp->dst_address;
4500
4501     a->outer_table_id = outer_table_id;
4502     rv = vnet_gre_add_del_tunnel (a, &sw_if_index);
4503
4504 out:
4505     REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY,
4506     ({
4507         rmp->sw_if_index = ntohl (sw_if_index);
4508     }));
4509 }
4510
4511 static void send_gre_tunnel_details
4512 (gre_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4513 {
4514     vl_api_gre_tunnel_details_t * rmp;
4515     ip4_main_t * im = &ip4_main;
4516
4517     rmp = vl_msg_api_alloc (sizeof (*rmp));
4518     memset (rmp, 0, sizeof (*rmp));
4519     rmp->_vl_msg_id = ntohs(VL_API_GRE_TUNNEL_DETAILS);
4520     rmp->src_address = t->tunnel_src.data_u32;
4521     rmp->dst_address = t->tunnel_dst.data_u32;
4522     rmp->outer_table_id = htonl(im->fibs[t->outer_fib_index].table_id);
4523     rmp->sw_if_index = htonl(t->sw_if_index);
4524     rmp->context = context;
4525
4526     vl_msg_api_send_shmem (q, (u8 *)&rmp);
4527 }
4528
4529 static void vl_api_gre_tunnel_dump_t_handler
4530 (vl_api_gre_tunnel_dump_t * mp)
4531 {
4532     unix_shared_memory_queue_t * q;
4533     gre_main_t * gm = &gre_main;
4534     gre_tunnel_t * t;
4535     u32 sw_if_index;
4536
4537     q = vl_api_client_index_to_input_queue (mp->client_index);
4538     if (q == 0) {
4539         return;
4540     }
4541
4542     sw_if_index = ntohl(mp->sw_if_index);
4543
4544     if (~0 == sw_if_index) {
4545         pool_foreach (t, gm->tunnels,
4546         ({
4547             send_gre_tunnel_details(t, q, mp->context);
4548         }));
4549     } else {
4550         if ((sw_if_index >= vec_len(gm->tunnel_index_by_sw_if_index)) ||
4551                 (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index])) {
4552             return;
4553         }
4554         t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
4555         send_gre_tunnel_details(t, q, mp->context);
4556     }
4557 }
4558
4559 static void
4560 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t *mp)
4561 {
4562     extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
4563                                       int is_add);
4564     vl_api_l2_patch_add_del_reply_t * rmp;
4565     int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
4566                                int is_add);
4567     int rv = 0;
4568
4569     VALIDATE_RX_SW_IF_INDEX(mp);
4570     VALIDATE_TX_SW_IF_INDEX(mp);
4571
4572     rv = vnet_l2_patch_add_del (ntohl(mp->rx_sw_if_index),
4573                                 ntohl(mp->tx_sw_if_index),
4574                                 (int)(mp->is_add != 0));
4575
4576     BAD_RX_SW_IF_INDEX_LABEL;
4577     BAD_TX_SW_IF_INDEX_LABEL;
4578
4579     REPLY_MACRO(VL_API_L2_PATCH_ADD_DEL_REPLY);
4580 }
4581
4582 static void
4583 vl_api_vxlan_gpe_add_del_tunnel_t_handler
4584 (vl_api_vxlan_gpe_add_del_tunnel_t * mp)
4585 {
4586     vl_api_vxlan_gpe_add_del_tunnel_reply_t * rmp;
4587     int rv = 0;
4588     vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
4589     u32 encap_fib_index, decap_fib_index;
4590     u8 protocol;
4591     uword * p;
4592     ip4_main_t * im = &ip4_main;
4593     u32 sw_if_index = ~0;
4594
4595
4596     p = hash_get (im->fib_index_by_table_id, ntohl(mp->encap_vrf_id));
4597     if (! p) {
4598         rv = VNET_API_ERROR_NO_SUCH_FIB;
4599         goto out;
4600     }
4601     encap_fib_index = p[0];
4602
4603     protocol = mp->protocol;
4604
4605     /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
4606     if (protocol == VXLAN_GPE_INPUT_NEXT_IP4_INPUT) {
4607         p = hash_get (im->fib_index_by_table_id, ntohl(mp->decap_vrf_id));
4608         if (! p) {
4609             rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
4610             goto out;
4611         }
4612         decap_fib_index = p[0];
4613     } else {
4614         decap_fib_index = ntohl(mp->decap_vrf_id);
4615     }
4616
4617     /* Check src & dst are different */
4618     if ((mp->is_ipv6 && memcmp(mp->local, mp->remote, 16) == 0) ||
4619        (!mp->is_ipv6 && memcmp(mp->local, mp->remote, 4) == 0)) {
4620         rv = VNET_API_ERROR_SAME_SRC_DST;
4621         goto out;
4622     }
4623     memset (a, 0, sizeof (*a));
4624
4625     a->is_add = mp->is_add;
4626     a->is_ip6 = mp->is_ipv6;
4627     /* ip addresses sent in network byte order */
4628     if (a->is_ip6) {
4629       clib_memcpy(&(a->local.ip6), mp->local, 16);
4630       clib_memcpy(&(a->remote.ip6), mp->remote, 16);
4631     } else {
4632       clib_memcpy(&(a->local.ip4), mp->local, 4);
4633       clib_memcpy(&(a->remote.ip4), mp->remote, 4);
4634     }
4635     a->encap_fib_index = encap_fib_index;
4636     a->decap_fib_index = decap_fib_index;
4637     a->protocol = protocol;
4638     a->vni = ntohl(mp->vni);
4639     rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
4640
4641 out:
4642     REPLY_MACRO2(VL_API_VXLAN_GPE_ADD_DEL_TUNNEL_REPLY,
4643     ({
4644         rmp->sw_if_index = ntohl (sw_if_index);
4645     }));
4646 }
4647
4648 static void send_vxlan_gpe_tunnel_details
4649 (vxlan_gpe_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4650 {
4651     vl_api_vxlan_gpe_tunnel_details_t * rmp;
4652     ip4_main_t * im4 = &ip4_main;
4653     ip6_main_t * im6 = &ip6_main;
4654     u8 is_ipv6 = !(t->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
4655
4656     rmp = vl_msg_api_alloc (sizeof (*rmp));
4657     memset (rmp, 0, sizeof (*rmp));
4658     rmp->_vl_msg_id = ntohs(VL_API_VXLAN_GPE_TUNNEL_DETAILS);
4659     if (is_ipv6) {
4660         memcpy(rmp->local, &(t->local.ip6), 16);
4661         memcpy(rmp->remote, &(t->remote.ip6), 16);
4662         rmp->encap_vrf_id = htonl(im6->fibs[t->encap_fib_index].table_id);
4663         rmp->decap_vrf_id = htonl(im6->fibs[t->decap_fib_index].table_id);
4664     } else {
4665         memcpy(rmp->local, &(t->local.ip4), 4);
4666         memcpy(rmp->remote, &(t->remote.ip4), 4);
4667         rmp->encap_vrf_id = htonl(im4->fibs[t->encap_fib_index].table_id);
4668         rmp->decap_vrf_id = htonl(im4->fibs[t->decap_fib_index].table_id);
4669     }
4670     rmp->vni = htonl(t->vni);
4671     rmp->protocol = t->protocol;
4672     rmp->sw_if_index = htonl(t->sw_if_index);
4673     rmp->is_ipv6 = is_ipv6;
4674     rmp->context = context;
4675
4676     vl_msg_api_send_shmem (q, (u8 *)&rmp);
4677 }
4678
4679 static void vl_api_vxlan_gpe_tunnel_dump_t_handler
4680 (vl_api_vxlan_gpe_tunnel_dump_t * mp)
4681 {
4682     unix_shared_memory_queue_t * q;
4683     vxlan_gpe_main_t * vgm = &vxlan_gpe_main;
4684     vxlan_gpe_tunnel_t * t;
4685     u32 sw_if_index;
4686
4687     q = vl_api_client_index_to_input_queue (mp->client_index);
4688     if (q == 0) {
4689         return;
4690     }
4691
4692     sw_if_index = ntohl(mp->sw_if_index);
4693
4694     if (~0 == sw_if_index) {
4695         pool_foreach (t, vgm->tunnels,
4696         ({
4697             send_vxlan_gpe_tunnel_details(t, q, mp->context);
4698         }));
4699     } else {
4700         if ((sw_if_index >= vec_len(vgm->tunnel_index_by_sw_if_index)) ||
4701                 (~0 == vgm->tunnel_index_by_sw_if_index[sw_if_index])) {
4702             return;
4703         }
4704         t = &vgm->tunnels[vgm->tunnel_index_by_sw_if_index[sw_if_index]];
4705         send_vxlan_gpe_tunnel_details(t, q, mp->context);
4706     }
4707 }
4708
4709 static void
4710 vl_api_lisp_add_del_locator_set_t_handler(vl_api_lisp_add_del_locator_set_t *mp)
4711 {
4712     vl_api_lisp_add_del_locator_set_reply_t *rmp;
4713     int rv = 0;
4714     vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
4715     u32 ls_index = ~0;
4716     u8 *locator_name = NULL;
4717
4718     memset(a, 0, sizeof(a[0]));
4719
4720     locator_name = format(0, "%s", mp->locator_set_name);
4721
4722     a->name = locator_name;
4723     a->locators = NULL;
4724     a->is_add = mp->is_add;
4725     a->local = 1;
4726
4727     rv = vnet_lisp_add_del_locator_set(a, &ls_index);
4728
4729     vec_free(locator_name);
4730
4731     REPLY_MACRO(VL_API_LISP_ADD_DEL_LOCATOR_SET_REPLY);
4732 }
4733
4734 static void
4735 vl_api_lisp_add_del_locator_t_handler(
4736     vl_api_lisp_add_del_locator_t *mp)
4737 {
4738     vl_api_lisp_add_del_locator_reply_t *rmp;
4739     int rv = 0;
4740     locator_t locator, *locators = NULL;
4741     vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
4742     u32 ls_index = ~0;
4743     u8 *locator_name = NULL;
4744
4745     memset(&locator, 0, sizeof(locator));
4746     memset(a, 0, sizeof(a[0]));
4747
4748     locator.sw_if_index = ntohl(mp->sw_if_index);
4749     locator.priority = mp->priority;
4750     locator.weight = mp->weight;
4751     locator.local = 1;
4752     vec_add1(locators, locator);
4753
4754     locator_name = format(0, "%s", mp->locator_set_name);
4755
4756     a->name = locator_name;
4757     a->locators = locators;
4758     a->is_add = mp->is_add;
4759     a->local = 1;
4760
4761     rv = vnet_lisp_add_del_locator(a, NULL, &ls_index);
4762
4763     vec_free(locators);
4764     vec_free(locator_name);
4765
4766     REPLY_MACRO(VL_API_LISP_ADD_DEL_LOCATOR_REPLY);
4767 }
4768
4769 static void
4770 vl_api_lisp_add_del_local_eid_t_handler(
4771     vl_api_lisp_add_del_local_eid_t *mp)
4772 {
4773     vl_api_lisp_add_del_local_eid_reply_t *rmp;
4774     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
4775     int rv = 0;
4776     ip_prefix_t  *prefp = NULL;
4777     ip_address_t *ip_eid = NULL;
4778     gid_address_t eid;
4779     uword * p = NULL;
4780     u32 locator_set_index = ~0, map_index = ~0;
4781     vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
4782     u8 *name = NULL;
4783     memset (a, 0, sizeof (a[0]));
4784
4785     prefp = &gid_address_ippref(&eid);
4786     ip_eid = &ip_prefix_addr(prefp);
4787     gid_address_type (&eid) = GID_ADDR_IP_PREFIX;
4788
4789     if (mp->is_ipv6) {
4790         clib_memcpy(&ip_addr_v6(ip_eid), mp->ip_address,
4791                sizeof(ip_addr_v6(ip_eid)));
4792         ip_addr_version(ip_eid) = IP6;
4793     } else {
4794         clib_memcpy(&ip_addr_v4(ip_eid), mp->ip_address,
4795                sizeof(ip_addr_v4(ip_eid)));
4796         ip_addr_version(ip_eid) = IP4;
4797     }
4798     ip_prefix_len(prefp) = mp->prefix_len;
4799
4800     name = format(0, "%s", mp->locator_set_name);
4801     p = hash_get_mem(lcm->locator_set_index_by_name, name);
4802     if (!p) {
4803         rv = VNET_API_ERROR_INVALID_VALUE;
4804         goto out;
4805     }
4806     locator_set_index = p[0];
4807
4808   /* XXX treat batch configuration */
4809     a->is_add = mp->is_add;
4810     gid_address_vni (&eid) = clib_net_to_host_u32 (mp->vni);
4811     a->deid = eid;
4812     a->locator_set_index = locator_set_index;
4813     a->local = 1;
4814     rv = vnet_lisp_add_del_local_mapping(a, &map_index);
4815
4816 out:
4817     vec_free(name);
4818     gid_address_free (&a->deid);
4819
4820     REPLY_MACRO(VL_API_LISP_ADD_DEL_LOCAL_EID_REPLY);
4821 }
4822
4823 static void
4824 vl_api_lisp_eid_table_add_del_map_t_handler(
4825     vl_api_lisp_eid_table_add_del_map_t *mp)
4826 {
4827     vl_api_lisp_eid_table_add_del_map_reply_t *rmp;
4828     int rv = 0;
4829     rv = vnet_lisp_eid_table_map (clib_net_to_host_u32 (mp->vni),
4830                                   clib_net_to_host_u32 (mp->vrf), mp->is_add);
4831     REPLY_MACRO(VL_API_LISP_EID_TABLE_ADD_DEL_MAP_REPLY)
4832 }
4833
4834 static void
4835 lisp_gpe_add_del_fwd_entry_set_address(
4836     vl_api_lisp_gpe_add_del_fwd_entry_t *mp,
4837     ip_address_t                        *slocator,
4838     ip_address_t                        *dlocator,
4839     gid_address_t                       *eid)
4840 {
4841     ip_address_t *ip_eid = NULL;
4842     ip_prefix_t *prefp = NULL;
4843
4844     prefp = &gid_address_ippref(eid);
4845     ip_eid = &ip_prefix_addr(prefp);
4846
4847     if (mp->eid_is_ipv6) {
4848         clib_memcpy(&ip_addr_v6(ip_eid), mp->eid_ip_address,
4849                sizeof(ip_addr_v6(ip_eid)));
4850         ip_addr_version(ip_eid) = IP6;
4851     } else {
4852         clib_memcpy(&ip_addr_v4(ip_eid), mp->eid_ip_address,
4853                sizeof(ip_addr_v4(ip_eid)));
4854         ip_addr_version(ip_eid) = IP4;
4855     }
4856     ip_prefix_len(prefp) = mp->eid_prefix_len;
4857
4858     if (mp->address_is_ipv6) {
4859         clib_memcpy(&ip_addr_v6(slocator), mp->source_ip_address,
4860                sizeof(ip_addr_v6(slocator)));
4861         ip_addr_version(slocator) = IP6;
4862         clib_memcpy(&ip_addr_v6(dlocator), mp->destination_ip_address,
4863                sizeof(ip_addr_v6(dlocator)));
4864         ip_addr_version(dlocator) = IP6;
4865     } else {
4866         clib_memcpy(&ip_addr_v4(slocator), mp->source_ip_address,
4867                sizeof(ip_addr_v4(slocator)));
4868         ip_addr_version(slocator) = IP4;
4869         clib_memcpy(&ip_addr_v4(dlocator), mp->destination_ip_address,
4870                sizeof(ip_addr_v4(dlocator)));
4871         ip_addr_version(dlocator) = IP4;
4872     }
4873 }
4874
4875 static void
4876 vl_api_lisp_gpe_add_del_fwd_entry_t_handler(
4877     vl_api_lisp_gpe_add_del_fwd_entry_t *mp)
4878 {
4879     vl_api_lisp_gpe_add_del_fwd_entry_reply_t *rmp;
4880     int rv = 0;
4881     ip_address_t slocator, dlocator;
4882     gid_address_t eid;
4883     vnet_lisp_gpe_add_del_fwd_entry_args_t a;
4884
4885     lisp_gpe_add_del_fwd_entry_set_address(mp, &slocator, &dlocator, &eid);
4886
4887     memset (&a, 0, sizeof(a));
4888
4889     a.is_add = mp->is_add;
4890     a.deid = eid;
4891     a.slocator = slocator;
4892     a.dlocator = dlocator;
4893     rv = vnet_lisp_gpe_add_del_fwd_entry (&a, 0);
4894
4895     REPLY_MACRO(VL_API_LISP_GPE_ADD_DEL_FWD_ENTRY_REPLY);
4896 }
4897
4898 static void
4899 vl_api_lisp_add_del_map_resolver_t_handler(
4900     vl_api_lisp_add_del_map_resolver_t *mp)
4901 {
4902     vl_api_lisp_add_del_map_resolver_reply_t *rmp;
4903     int rv = 0;
4904     ip_address_t *ip_addr = NULL;
4905     vnet_lisp_add_del_map_resolver_args_t _a, * a = &_a;
4906
4907     a->is_add = mp->is_add;
4908     ip_addr = &a->address;
4909
4910     if (mp->is_ipv6) {
4911         clib_memcpy(&ip_addr_v6(ip_addr), mp->ip_address,
4912                sizeof(ip_addr_v6(ip_addr)));
4913         ip_addr_version(ip_addr) = IP6;
4914     } else {
4915         clib_memcpy(&ip_addr_v4(ip_addr), mp->ip_address,
4916                sizeof(ip_addr_v4(ip_addr)));
4917         ip_addr_version(ip_addr) = IP4;
4918     }
4919
4920     rv = vnet_lisp_add_del_map_resolver (a);
4921
4922     REPLY_MACRO(VL_API_LISP_ADD_DEL_MAP_RESOLVER_REPLY);
4923 }
4924
4925 static void
4926 vl_api_lisp_gpe_enable_disable_t_handler(
4927     vl_api_lisp_gpe_enable_disable_t *mp)
4928 {
4929     vl_api_lisp_gpe_enable_disable_reply_t *rmp;
4930     int rv = 0;
4931     vnet_lisp_gpe_enable_disable_args_t _a, * a = &_a;
4932
4933     a->is_en = mp->is_en;
4934     vnet_lisp_gpe_enable_disable (a);
4935
4936     REPLY_MACRO(VL_API_LISP_GPE_ENABLE_DISABLE_REPLY);
4937 }
4938
4939 static void
4940 vl_api_lisp_enable_disable_t_handler(
4941     vl_api_lisp_enable_disable_t *mp)
4942 {
4943     vl_api_lisp_enable_disable_reply_t *rmp;
4944     int rv = 0;
4945
4946     vnet_lisp_enable_disable (mp->is_en);
4947     REPLY_MACRO(VL_API_LISP_ENABLE_DISABLE_REPLY);
4948 }
4949
4950 static void
4951 vl_api_lisp_gpe_add_del_iface_t_handler(
4952     vl_api_lisp_gpe_add_del_iface_t *mp)
4953 {
4954     vl_api_lisp_gpe_add_del_iface_reply_t *rmp;
4955     int rv = 0;
4956     vnet_lisp_gpe_add_del_iface_args_t _a, * a = &_a;
4957
4958     a->is_add = mp->is_add;
4959     a->table_id = mp->table_id;
4960     a->vni = mp->vni;
4961     rv = vnet_lisp_gpe_add_del_iface (a, 0);
4962
4963     REPLY_MACRO(VL_API_LISP_GPE_ADD_DEL_IFACE_REPLY);
4964 }
4965
4966 static void
4967 vl_api_lisp_pitr_set_locator_set_t_handler(
4968   vl_api_lisp_pitr_set_locator_set_t *mp)
4969 {
4970     vl_api_lisp_pitr_set_locator_set_reply_t *rmp;
4971     int rv = 0;
4972     u8 * ls_name = 0;
4973
4974     ls_name = format (0, "%s", mp->ls_name);
4975     rv = vnet_lisp_pitr_set_locator_set (ls_name, mp->is_add);
4976     vec_free (ls_name);
4977
4978     REPLY_MACRO(VL_API_LISP_PITR_SET_LOCATOR_SET_REPLY);
4979 }
4980
4981 static void
4982 vl_api_lisp_add_del_map_request_itr_rlocs_t_handler
4983 (vl_api_lisp_add_del_map_request_itr_rlocs_t *mp)
4984 {
4985     vl_api_lisp_add_del_map_request_itr_rlocs_reply_t *rmp;
4986     int rv = 0;
4987     u8 * locator_set_name = NULL;
4988     vnet_lisp_add_del_mreq_itr_rloc_args_t _a, * a = &_a;
4989
4990     locator_set_name = format (0, "%s", mp->locator_set_name);
4991
4992     a->is_add = mp->is_add;
4993     a->locator_set_name = locator_set_name;
4994
4995     rv = vnet_lisp_add_del_mreq_itr_rlocs(a);
4996
4997     vec_free(locator_set_name);
4998
4999     REPLY_MACRO(VL_API_LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS_REPLY);
5000 }
5001
5002 /** Used for transferring locators via VPP API */
5003 typedef CLIB_PACKED(struct
5004 {
5005   u8 is_ip4; /**< is locator an IPv4 address */
5006   u8 addr[16]; /**< IPv4/IPv6 address */
5007 }) rloc_t;
5008
5009 static void
5010 send_lisp_locator_set_details_set_address
5011 (vl_api_lisp_locator_set_details_t *rmp,
5012  gid_address_t *gid_address)
5013 {
5014     ip_prefix_t *ip_addr;
5015
5016     if (gid_address_type(gid_address) != GID_ADDR_IP_PREFIX) {
5017         return;
5018     }
5019
5020     ip_addr = &gid_address_ippref(gid_address);
5021     rmp->prefix_len = ip_prefix_len(ip_addr);
5022     rmp->is_ipv6 = ip_prefix_version(ip_addr);
5023     ip_address_copy_addr(rmp->ip_address, &ip_prefix_addr(ip_addr));
5024 }
5025
5026 static void
5027 vl_api_lisp_add_del_remote_mapping_t_handler (
5028     vl_api_lisp_add_del_remote_mapping_t *mp)
5029 {
5030     u32 i;
5031     ip_address_t rloc, * rlocs = 0;
5032     vl_api_lisp_add_del_remote_mapping_reply_t * rmp;
5033     int rv = 0;
5034     gid_address_t _seid, * seid = &_seid;
5035     gid_address_t _deid, * deid = &_deid;
5036     ip_prefix_t * seid_pref = &gid_address_ippref(seid);
5037     ip_prefix_t * deid_pref = &gid_address_ippref(deid);
5038
5039     gid_address_type(seid) = GID_ADDR_IP_PREFIX;
5040     gid_address_type(deid) = GID_ADDR_IP_PREFIX;
5041     ip_address_t * seid_addr = &ip_prefix_addr(seid_pref);
5042     ip_address_t * deid_addr = &ip_prefix_addr(deid_pref);
5043     ip_prefix_len(seid_pref) = mp->seid_len;
5044     ip_prefix_len(deid_pref) = mp->deid_len;
5045     gid_address_vni (seid) = ntohl (mp->vni);
5046     gid_address_vni (deid) = ntohl (mp->vni);
5047
5048     if (mp->eid_is_ip4) {
5049         ip_prefix_version(seid_pref) = IP4;
5050         ip_prefix_version(deid_pref) = IP4;
5051         clib_memcpy (&ip_addr_v4(seid_addr),
5052                      mp->seid, sizeof (ip_addr_v4(seid_addr)));
5053         clib_memcpy (&ip_addr_v4(deid_addr),
5054                      mp->deid, sizeof (ip_addr_v4(deid_addr)));
5055     } else {
5056         ip_prefix_version(seid_pref) = IP6;
5057         ip_prefix_version(deid_pref) = IP6;
5058         clib_memcpy (&ip_addr_v6(seid_addr),
5059                      mp->seid, sizeof (ip_addr_v6(seid_addr)));
5060         clib_memcpy (&ip_addr_v6(deid_addr),
5061                      mp->deid, sizeof (ip_addr_v6(deid_addr)));
5062     }
5063
5064     for (i = 0; i < mp->rloc_num; i++) {
5065         rloc_t * r = &((rloc_t *) mp->rlocs)[i];
5066         if (r->is_ip4) {
5067             clib_memcpy (&ip_addr_v4(&rloc), &r->addr, sizeof (rloc_t));
5068             ip_addr_version (&rloc) = IP4;
5069         } else {
5070             clib_memcpy (&ip_addr_v6(&rloc), &r->addr, sizeof (rloc_t));
5071             ip_addr_version (&rloc) = IP6;
5072         }
5073         vec_add1 (rlocs, rloc);
5074     }
5075
5076     rv = vnet_lisp_add_del_remote_mapping (deid, seid, rlocs, mp->action,
5077                                            mp->is_add, mp->del_all);
5078     vec_free (rlocs);
5079     REPLY_MACRO(VL_API_LISP_GPE_ADD_DEL_IFACE_REPLY);
5080 }
5081
5082 static void
5083 send_lisp_locator_set_details (lisp_cp_main_t *lcm,
5084                                locator_set_t *lsit,
5085                                unix_shared_memory_queue_t *q,
5086                                u32 context,
5087                                u32 index)
5088 {
5089     vl_api_lisp_locator_set_details_t *rmp;
5090     locator_t *loc = NULL;
5091     u32 * locit = NULL;
5092     u8 * str = NULL;
5093
5094     vec_foreach (locit, lsit->locator_indices) {
5095         loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
5096         rmp = vl_msg_api_alloc (sizeof (*rmp));
5097         memset (rmp, 0, sizeof (*rmp));
5098         rmp->_vl_msg_id = ntohs(VL_API_LISP_LOCATOR_SET_DETAILS);
5099         rmp->local = lsit->local;
5100         if (lsit->local) {
5101             ASSERT(lsit->name != NULL);
5102             strncpy((char *) rmp->locator_set_name,
5103                     (char *) lsit->name, ARRAY_LEN(rmp->locator_set_name) - 1);
5104             rmp->sw_if_index = htonl(loc->sw_if_index);
5105         } else {
5106             str = format(0, "remote-%d", index);
5107             strncpy((char *) rmp->locator_set_name, (char *) str,
5108                     ARRAY_LEN(rmp->locator_set_name) - 1);
5109             send_lisp_locator_set_details_set_address(rmp, &loc->address);
5110
5111             vec_free(str);
5112         }
5113         rmp->priority = loc->priority;
5114         rmp->weight = loc->weight;
5115         rmp->context = context;
5116
5117         vl_msg_api_send_shmem (q, (u8 *)&rmp);
5118     }
5119 }
5120
5121 static void
5122 vl_api_lisp_locator_set_dump_t_handler (vl_api_lisp_locator_set_dump_t *mp)
5123 {
5124     unix_shared_memory_queue_t * q = NULL;
5125     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5126     locator_set_t * lsit = NULL;
5127     u32 index;
5128
5129     q = vl_api_client_index_to_input_queue (mp->client_index);
5130     if (q == 0) {
5131         return;
5132     }
5133
5134     index = 0;
5135     pool_foreach (lsit, lcm->locator_set_pool,
5136         ({
5137             send_lisp_locator_set_details(lcm, lsit, q, mp->context, index++);
5138         }));
5139 }
5140
5141 static void
5142 send_lisp_local_eid_table_details (mapping_t *mapit,
5143                                    unix_shared_memory_queue_t *q,
5144                                    u32 context)
5145 {
5146     vl_api_lisp_local_eid_table_details_t *rmp = NULL;
5147     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5148     locator_set_t *ls = NULL;
5149     gid_address_t *gid = NULL;
5150     ip_prefix_t *ip_prefix = NULL;
5151     u8 * str = NULL;
5152     u8 type = ~0;
5153
5154     ls = pool_elt_at_index (lcm->locator_set_pool,
5155                             mapit->locator_set_index);
5156
5157     gid = &mapit->eid;
5158     type = gid_address_type(gid);
5159
5160     if (type != GID_ADDR_IP_PREFIX) {
5161         return;
5162     }
5163
5164     ip_prefix = &gid_address_ippref(gid);
5165
5166     rmp = vl_msg_api_alloc (sizeof (*rmp));
5167     memset (rmp, 0, sizeof (*rmp));
5168     rmp->_vl_msg_id = ntohs(VL_API_LISP_LOCAL_EID_TABLE_DETAILS);
5169     if (ls->local) {
5170         ASSERT(ls->name != NULL);
5171         strncpy((char *) rmp->locator_set_name,
5172                 (char *) ls->name, ARRAY_LEN(rmp->locator_set_name) - 1);
5173     } else {
5174             str = format(0, "remote-%d", mapit->locator_set_index);
5175             strncpy((char *) rmp->locator_set_name, (char *) str,
5176                     ARRAY_LEN(rmp->locator_set_name) - 1);
5177             vec_free(str);
5178     }
5179
5180     switch (ip_prefix_version(ip_prefix)) {
5181         case IP4:
5182             rmp->eid_is_ipv6 = 0;
5183             clib_memcpy(rmp->eid_ip_address, &ip_prefix_v4(ip_prefix),
5184                    sizeof(ip_prefix_v4(ip_prefix)));
5185             break;
5186
5187         case IP6:
5188             rmp->eid_is_ipv6 = 1;
5189             clib_memcpy(rmp->eid_ip_address, &ip_prefix_v6(ip_prefix),
5190                    sizeof(ip_prefix_v6(ip_prefix)));
5191             break;
5192
5193         default:
5194             ASSERT(0);
5195     }
5196     rmp->eid_prefix_len = ip_prefix_len(ip_prefix);
5197     rmp->context = context;
5198     rmp->vni = clib_host_to_net_u32 (gid_address_vni (gid));
5199     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5200 }
5201
5202 static void
5203 vl_api_lisp_local_eid_table_dump_t_handler (
5204     vl_api_lisp_local_eid_table_dump_t *mp)
5205 {
5206     unix_shared_memory_queue_t * q = NULL;
5207     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5208     mapping_t * mapit = NULL;
5209
5210     q = vl_api_client_index_to_input_queue (mp->client_index);
5211     if (q == 0) {
5212         return;
5213     }
5214
5215     pool_foreach (mapit, lcm->mapping_pool,
5216         ({
5217             send_lisp_local_eid_table_details(mapit, q, mp->context);
5218         }));
5219 }
5220
5221 static void
5222 send_lisp_gpe_tunnel_details (lisp_gpe_tunnel_t *tunnel,
5223                               unix_shared_memory_queue_t *q,
5224                               u32 context)
5225 {
5226     vl_api_lisp_gpe_tunnel_details_t *rmp;
5227     lisp_gpe_main_t * lgm = &lisp_gpe_main;
5228
5229     rmp = vl_msg_api_alloc (sizeof (*rmp));
5230     memset (rmp, 0, sizeof (*rmp));
5231     rmp->_vl_msg_id = ntohs(VL_API_LISP_GPE_TUNNEL_DETAILS);
5232
5233     rmp->tunnels = tunnel - lgm->tunnels;
5234
5235     rmp->is_ipv6 = ip_addr_version(&tunnel->src) == IP6 ? 1 : 0;
5236     ip_address_copy_addr(rmp->source_ip, &tunnel->src);
5237     ip_address_copy_addr(rmp->destination_ip, &tunnel->dst);
5238
5239     rmp->encap_fib_id = htonl(tunnel->encap_fib_index);
5240     rmp->decap_fib_id = htonl(tunnel->decap_fib_index);
5241     rmp->dcap_next = htonl(tunnel->decap_next_index);
5242     rmp->lisp_ver = tunnel->ver_res;
5243     rmp->next_protocol = tunnel->next_protocol;
5244     rmp->flags = tunnel->flags;
5245     rmp->ver_res = tunnel->ver_res;
5246     rmp->res = tunnel->res;
5247     rmp->iid = htonl(tunnel->vni);
5248     rmp->context = context;
5249
5250     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5251 }
5252
5253 static void
5254 vl_api_lisp_gpe_tunnel_dump_t_handler (
5255     vl_api_lisp_gpe_tunnel_dump_t *mp)
5256 {
5257     unix_shared_memory_queue_t * q = NULL;
5258     lisp_gpe_main_t * lgm = &lisp_gpe_main;
5259     lisp_gpe_tunnel_t * tunnel = NULL;
5260
5261     if (pool_elts(lgm->tunnels) == 0) {
5262         return;
5263     }
5264
5265     q = vl_api_client_index_to_input_queue (mp->client_index);
5266     if (q == 0) {
5267         return;
5268     }
5269
5270     pool_foreach(tunnel, lgm->tunnels,
5271                  ({
5272                      send_lisp_gpe_tunnel_details(tunnel, q, mp->context);
5273                 }));
5274 }
5275
5276 static void
5277 send_lisp_map_resolver_details (ip_address_t *ip,
5278                                 unix_shared_memory_queue_t *q,
5279                                 u32 context)
5280 {
5281     vl_api_lisp_map_resolver_details_t *rmp = NULL;
5282
5283     rmp = vl_msg_api_alloc (sizeof (*rmp));
5284     memset (rmp, 0, sizeof (*rmp));
5285     rmp->_vl_msg_id = ntohs(VL_API_LISP_MAP_RESOLVER_DETAILS);
5286
5287     switch (ip_addr_version(ip)) {
5288         case IP4:
5289             rmp->is_ipv6 = 0;
5290             clib_memcpy(rmp->ip_address, &ip_addr_v4(ip), sizeof(ip_addr_v4(ip)));
5291             break;
5292
5293         case IP6:
5294             rmp->is_ipv6 = 1;
5295             clib_memcpy(rmp->ip_address, &ip_addr_v6(ip), sizeof(ip_addr_v6(ip)));
5296             break;
5297
5298         default:
5299             ASSERT(0);
5300     }
5301     rmp->context = context;
5302
5303     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5304 }
5305
5306 static void
5307 vl_api_lisp_map_resolver_dump_t_handler (
5308     vl_api_lisp_map_resolver_dump_t *mp)
5309 {
5310     unix_shared_memory_queue_t * q = NULL;
5311     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5312     ip_address_t *ip = NULL;
5313
5314     q = vl_api_client_index_to_input_queue (mp->client_index);
5315     if (q == 0) {
5316         return;
5317     }
5318
5319     vec_foreach(ip, lcm->map_resolvers) {
5320         send_lisp_map_resolver_details(ip, q, mp->context);
5321     }
5322
5323 }
5324
5325 static void
5326 vl_api_lisp_eid_table_map_dump_t_handler (
5327     vl_api_lisp_eid_table_map_dump_t *mp)
5328 {
5329     unix_shared_memory_queue_t * q = NULL;
5330     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5331     hash_pair_t * p;
5332
5333     q = vl_api_client_index_to_input_queue (mp->client_index);
5334     if (q == 0) {
5335         return;
5336     }
5337     hash_foreach_pair (p, lcm->table_id_by_vni, {
5338         vl_api_lisp_eid_table_map_details_t * rmp = NULL;
5339         memset (rmp, 0, sizeof (*rmp));
5340         rmp->_vl_msg_id = ntohs(VL_API_LISP_EID_TABLE_MAP_DETAILS);
5341         rmp->vni = p->key;
5342         rmp->vrf = p->value[0];
5343         rmp->context = mp->context;
5344     });
5345 }
5346
5347 static void
5348 send_lisp_enable_disable_details (unix_shared_memory_queue_t *q,
5349                                       u32 context)
5350 {
5351     vl_api_lisp_enable_disable_status_details_t *rmp = NULL;
5352
5353     rmp = vl_msg_api_alloc (sizeof (*rmp));
5354     memset (rmp, 0, sizeof (*rmp));
5355     rmp->_vl_msg_id = ntohs(VL_API_LISP_ENABLE_DISABLE_STATUS_DETAILS);
5356
5357     rmp->gpe_status = vnet_lisp_gpe_enable_disable_status ();
5358     rmp->feature_status = vnet_lisp_enable_disable_status ();
5359     rmp->context = context;
5360
5361     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5362 }
5363
5364 static void
5365 vl_api_lisp_enable_disable_status_dump_t_handler
5366 (vl_api_lisp_enable_disable_status_dump_t *mp)
5367 {
5368     unix_shared_memory_queue_t * q = NULL;
5369
5370     q = vl_api_client_index_to_input_queue (mp->client_index);
5371     if (q == 0) {
5372         return;
5373     }
5374
5375     send_lisp_enable_disable_details(q, mp->context);
5376 }
5377
5378 static void
5379 vl_api_lisp_get_map_request_itr_rlocs_t_handler (
5380     vl_api_lisp_get_map_request_itr_rlocs_t *mp)
5381 {
5382     unix_shared_memory_queue_t * q = NULL;
5383     vl_api_lisp_get_map_request_itr_rlocs_reply_t *rmp = NULL;
5384     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5385     locator_set_t * loc_set = 0;
5386     u8 * tmp_str = 0;
5387     int rv = 0;
5388
5389     q = vl_api_client_index_to_input_queue (mp->client_index);
5390     if (q == 0) {
5391         return;
5392     }
5393
5394     if (~0 == lcm->mreq_itr_rlocs) {
5395       tmp_str = format(0, " ");
5396     } else {
5397       loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
5398       tmp_str = format(0, "%s", loc_set->name);
5399     }
5400
5401     REPLY_MACRO2(VL_API_LISP_GET_MAP_REQUEST_ITR_RLOCS_REPLY,
5402     ({
5403       strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
5404               ARRAY_LEN(rmp->locator_set_name) - 1);
5405     }));
5406
5407     vec_free(tmp_str);
5408 }
5409
5410 static void
5411 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *mp)
5412 {
5413     vl_api_interface_name_renumber_reply_t * rmp;
5414     int rv = 0;
5415
5416     VALIDATE_SW_IF_INDEX(mp);
5417
5418     rv = vnet_interface_name_renumber
5419         (ntohl(mp->sw_if_index), ntohl(mp->new_show_dev_instance));
5420
5421     BAD_SW_IF_INDEX_LABEL;
5422
5423     REPLY_MACRO(VL_API_INTERFACE_NAME_RENUMBER_REPLY);
5424 }
5425
5426 static int arp_change_data_callback (u32 pool_index, u8 * new_mac,
5427                                      u32 sw_if_index, u32 address)
5428 {
5429     vpe_api_main_t * am = &vpe_api_main;
5430     vlib_main_t * vm = am->vlib_main;
5431     vl_api_ip4_arp_event_t * event;
5432     static f64 arp_event_last_time;
5433     f64 now = vlib_time_now (vm);
5434
5435     if (pool_is_free_index (am->arp_events, pool_index))
5436         return 1;
5437
5438     event = pool_elt_at_index (am->arp_events, pool_index);
5439     if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac))) {
5440         clib_memcpy (event->new_mac, new_mac, sizeof(event->new_mac));
5441     } else { /* same mac */
5442         if ((sw_if_index == event->sw_if_index) &&
5443             ((address == 0) ||
5444              /* for BD case, also check IP address with 10 sec timeout */
5445              ((address == event->address) &&
5446               ((now - arp_event_last_time) < 10.0))))
5447             return 1;
5448     }
5449
5450     arp_event_last_time = now;
5451     event->sw_if_index = sw_if_index;
5452     if (address) event->address = address;
5453     return 0;
5454 }
5455
5456 static int arp_change_delete_callback (u32 pool_index, u8 * notused)
5457 {
5458     vpe_api_main_t * am = &vpe_api_main;
5459
5460     if (pool_is_free_index (am->arp_events, pool_index))
5461         return 1;
5462
5463     pool_put_index (am->arp_events, pool_index);
5464     return 0;
5465 }
5466
5467 static void
5468 vl_api_want_ip4_arp_events_t_handler
5469 (vl_api_want_ip4_arp_events_t * mp)
5470 {
5471     vpe_api_main_t * am = &vpe_api_main;
5472     vnet_main_t * vnm = vnet_get_main();
5473     vl_api_want_ip4_arp_events_reply_t *rmp;
5474     vl_api_ip4_arp_event_t * event;
5475     int rv;
5476
5477     if (mp->enable_disable) {
5478         pool_get (am->arp_events, event);
5479         memset (event, 0, sizeof (*event));
5480
5481         event->_vl_msg_id = ntohs(VL_API_IP4_ARP_EVENT);
5482         event->client_index = mp->client_index;
5483         event->context = mp->context;
5484         event->address = mp->address;
5485         event->pid = mp->pid;
5486
5487         rv = vnet_add_del_ip4_arp_change_event
5488             (vnm, arp_change_data_callback,
5489              mp->pid,
5490              &mp->address /* addr, in net byte order */,
5491              vpe_resolver_process_node.index,
5492              IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */);
5493     } else {
5494         rv = vnet_add_del_ip4_arp_change_event
5495             (vnm, arp_change_delete_callback,
5496              mp->pid,
5497              &mp->address /* addr, in net byte order */,
5498              vpe_resolver_process_node.index,
5499              IP4_ARP_EVENT, ~0 /* pool index */, 0 /* is_add */);
5500     }
5501     REPLY_MACRO(VL_API_WANT_IP4_ARP_EVENTS_REPLY);
5502 }
5503
5504 static void vl_api_input_acl_set_interface_t_handler
5505 (vl_api_input_acl_set_interface_t * mp)
5506 {
5507     vlib_main_t *vm = vlib_get_main();
5508     vl_api_input_acl_set_interface_reply_t * rmp;
5509     int rv;
5510     u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
5511
5512     ip4_table_index = ntohl(mp->ip4_table_index);
5513     ip6_table_index = ntohl(mp->ip6_table_index);
5514     l2_table_index = ntohl(mp->l2_table_index);
5515     sw_if_index = ntohl(mp->sw_if_index);
5516
5517     VALIDATE_SW_IF_INDEX(mp);
5518
5519     rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
5520                                    ip6_table_index, l2_table_index,
5521                                    mp->is_add);
5522
5523     BAD_SW_IF_INDEX_LABEL;
5524
5525     REPLY_MACRO(VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
5526 }
5527
5528 static void vl_api_ipsec_spd_add_del_t_handler
5529 (vl_api_ipsec_spd_add_del_t * mp)
5530 {
5531 #if IPSEC == 0
5532     clib_warning ("unimplemented");
5533 #else
5534
5535     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5536     vl_api_ipsec_spd_add_del_reply_t * rmp;
5537     int rv;
5538
5539 #if DPDK > 0
5540     rv = ipsec_add_del_spd (vm, ntohl(mp->spd_id), mp->is_add);
5541 #else
5542     rv = VNET_API_ERROR_UNIMPLEMENTED;
5543 #endif
5544
5545     REPLY_MACRO(VL_API_IPSEC_SPD_ADD_DEL_REPLY);
5546 #endif
5547 }
5548
5549 static void vl_api_ipsec_interface_add_del_spd_t_handler
5550 (vl_api_ipsec_interface_add_del_spd_t * mp)
5551 {
5552     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5553     vl_api_ipsec_interface_add_del_spd_reply_t * rmp;
5554     int rv;
5555     u32 sw_if_index __attribute__((unused));
5556     u32 spd_id __attribute__((unused));
5557
5558     sw_if_index = ntohl(mp->sw_if_index);
5559     spd_id = ntohl(mp->spd_id);
5560
5561     VALIDATE_SW_IF_INDEX(mp);
5562
5563 #if IPSEC > 0
5564     rv = ipsec_set_interface_spd(vm, sw_if_index, spd_id, mp->is_add);
5565 #else
5566     rv = VNET_API_ERROR_UNIMPLEMENTED;
5567 #endif
5568
5569     BAD_SW_IF_INDEX_LABEL;
5570
5571     REPLY_MACRO(VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
5572 }
5573
5574 static void vl_api_ipsec_spd_add_del_entry_t_handler
5575 (vl_api_ipsec_spd_add_del_entry_t * mp)
5576 {
5577     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5578     vl_api_ipsec_spd_add_del_entry_reply_t * rmp;
5579     int rv;
5580
5581 #if IPSEC > 0
5582     ipsec_policy_t p;
5583
5584     memset(&p, 0, sizeof(p));
5585
5586     p.id = ntohl(mp->spd_id);
5587     p.priority = ntohl(mp->priority);
5588     p.is_outbound = mp->is_outbound;
5589     p.is_ipv6 = mp->is_ipv6;
5590
5591     clib_memcpy(&p.raddr.start, mp->remote_address_start, 16);
5592     clib_memcpy(&p.raddr.stop, mp->remote_address_stop, 16);
5593     clib_memcpy(&p.laddr.start, mp->local_address_start, 16);
5594     clib_memcpy(&p.laddr.stop, mp->local_address_stop, 16);
5595
5596     p.protocol = mp->protocol;
5597     p.rport.start = ntohs(mp->remote_port_start);
5598     p.rport.stop  = ntohs(mp->remote_port_stop);
5599     p.lport.start = ntohs(mp->local_port_start);
5600     p.lport.stop  = ntohs(mp->local_port_stop);
5601     /* policy action resolve unsupported */
5602     if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE) {
5603         clib_warning("unsupported action: 'resolve'");
5604         rv = VNET_API_ERROR_UNIMPLEMENTED;
5605         goto out;
5606     }
5607     p.policy = mp->policy;
5608     p.sa_id = ntohl(mp->sa_id);
5609
5610     rv = ipsec_add_del_policy(vm, &p, mp->is_add);
5611     if (rv)
5612       goto out;
5613
5614     if (mp->is_ip_any) {
5615       p.is_ipv6 = 1;
5616       rv = ipsec_add_del_policy(vm, &p, mp->is_add);
5617     }
5618 #else
5619     rv = VNET_API_ERROR_UNIMPLEMENTED;
5620     goto out;
5621 #endif
5622
5623 out:
5624     REPLY_MACRO(VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
5625 }
5626
5627 static void vl_api_ipsec_sad_add_del_entry_t_handler
5628 (vl_api_ipsec_sad_add_del_entry_t * mp)
5629 {
5630     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5631     vl_api_ipsec_sad_add_del_entry_reply_t * rmp;
5632     int rv;
5633 #if IPSEC > 0
5634     ipsec_sa_t sa;
5635
5636     memset(&sa, 0, sizeof(sa));
5637
5638     sa.id = ntohl(mp->sad_id);
5639     sa.spi = ntohl(mp->spi);
5640     /* security protocol AH unsupported */
5641     if (mp->protocol == IPSEC_PROTOCOL_AH) {
5642         clib_warning("unsupported security protocol 'AH'");
5643         rv = VNET_API_ERROR_UNIMPLEMENTED;
5644         goto out;
5645     }
5646     sa.protocol = mp->protocol;
5647     /* check for unsupported crypto-alg */
5648     if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
5649         mp->crypto_algorithm > IPSEC_CRYPTO_ALG_AES_CBC_256) {
5650         clib_warning("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
5651                      mp->crypto_algorithm);
5652         rv = VNET_API_ERROR_UNIMPLEMENTED;
5653         goto out;
5654     }
5655     sa.crypto_alg = mp->crypto_algorithm;
5656     sa.crypto_key_len = mp->crypto_key_length;
5657     clib_memcpy(&sa.crypto_key, mp->crypto_key, sizeof(sa.crypto_key));
5658     /* check for unsupported integ-alg */
5659     if (mp->integrity_algorithm < IPSEC_INTEG_ALG_SHA1_96 ||
5660         mp->integrity_algorithm > IPSEC_INTEG_ALG_SHA_512_256) {
5661         clib_warning("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
5662                      mp->integrity_algorithm);
5663         rv = VNET_API_ERROR_UNIMPLEMENTED;
5664         goto out;
5665     }
5666     sa.integ_alg = mp->integrity_algorithm;
5667     sa.integ_key_len = mp->integrity_key_length;
5668     clib_memcpy(&sa.integ_key, mp->integrity_key, sizeof(sa.integ_key));
5669     sa.use_esn = mp->use_extended_sequence_number;
5670     sa.is_tunnel = mp->is_tunnel;
5671     sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
5672     clib_memcpy(&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
5673     clib_memcpy(&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
5674
5675     rv = ipsec_add_del_sa(vm, &sa, mp->is_add);
5676 #else
5677     rv = VNET_API_ERROR_UNIMPLEMENTED;
5678     goto out;
5679 #endif
5680
5681 out:
5682     REPLY_MACRO(VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
5683 }
5684
5685 static void
5686 vl_api_ikev2_profile_add_del_t_handler
5687 (vl_api_ikev2_profile_add_del_t * mp)
5688 {
5689     vl_api_ikev2_profile_add_del_reply_t * rmp;
5690     int rv = 0;
5691
5692 #if IPSEC > 0
5693     vlib_main_t * vm = vlib_get_main();
5694     clib_error_t * error;
5695     u8 * tmp = format(0, "%s", mp->name);
5696     error = ikev2_add_del_profile(vm, tmp, mp->is_add);
5697     vec_free (tmp);
5698     if (error)
5699       rv = VNET_API_ERROR_UNSPECIFIED;
5700 #else
5701     rv = VNET_API_ERROR_UNIMPLEMENTED;
5702 #endif
5703
5704    REPLY_MACRO(VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
5705 }
5706
5707 static void
5708 vl_api_ikev2_profile_set_auth_t_handler
5709 (vl_api_ikev2_profile_set_auth_t * mp)
5710 {
5711     vl_api_ikev2_profile_set_auth_reply_t * rmp;
5712     int rv = 0;
5713
5714 #if IPSEC > 0
5715     vlib_main_t * vm = vlib_get_main();
5716     clib_error_t * error;
5717     u8 * tmp = format(0, "%s", mp->name);
5718     u8 * data = vec_new (u8, mp->data_len);
5719     clib_memcpy(data, mp->data, mp->data_len);
5720     error = ikev2_set_profile_auth(vm, tmp, mp->auth_method, data, mp->is_hex);
5721     vec_free (tmp);
5722     vec_free (data);
5723     if (error)
5724       rv = VNET_API_ERROR_UNSPECIFIED;
5725 #else
5726     rv = VNET_API_ERROR_UNIMPLEMENTED;
5727 #endif
5728
5729    REPLY_MACRO(VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
5730 }
5731
5732 static void
5733 vl_api_ikev2_profile_set_id_t_handler
5734 (vl_api_ikev2_profile_set_id_t * mp)
5735 {
5736     vl_api_ikev2_profile_add_del_reply_t * rmp;
5737     int rv = 0;
5738
5739 #if IPSEC > 0
5740     vlib_main_t * vm = vlib_get_main();
5741     clib_error_t * error;
5742     u8 * tmp = format(0, "%s", mp->name);
5743     u8 * data = vec_new (u8, mp->data_len);
5744     clib_memcpy(data, mp->data, mp->data_len);
5745     error = ikev2_set_profile_id(vm, tmp, mp->id_type, data, mp->is_local);
5746     vec_free (tmp);
5747     vec_free (data);
5748     if (error)
5749       rv = VNET_API_ERROR_UNSPECIFIED;
5750 #else
5751     rv = VNET_API_ERROR_UNIMPLEMENTED;
5752 #endif
5753
5754    REPLY_MACRO(VL_API_IKEV2_PROFILE_SET_ID_REPLY);
5755 }
5756
5757 static void
5758 vl_api_ikev2_profile_set_ts_t_handler
5759 (vl_api_ikev2_profile_set_ts_t * mp)
5760 {
5761     vl_api_ikev2_profile_set_ts_reply_t * rmp;
5762     int rv = 0;
5763
5764 #if IPSEC > 0
5765     vlib_main_t * vm = vlib_get_main();
5766     clib_error_t * error;
5767     u8 * tmp = format(0, "%s", mp->name);
5768     error = ikev2_set_profile_ts(vm, tmp, mp->proto, mp->start_port,
5769                                  mp->end_port, (ip4_address_t) mp->start_addr,
5770                                  (ip4_address_t) mp->end_addr, mp->is_local);
5771     vec_free (tmp);
5772     if (error)
5773       rv = VNET_API_ERROR_UNSPECIFIED;
5774 #else
5775     rv = VNET_API_ERROR_UNIMPLEMENTED;
5776 #endif
5777
5778    REPLY_MACRO(VL_API_IKEV2_PROFILE_SET_TS_REPLY);
5779 }
5780
5781 static void
5782 vl_api_ikev2_set_local_key_t_handler
5783 (vl_api_ikev2_set_local_key_t * mp)
5784 {
5785     vl_api_ikev2_profile_set_ts_reply_t * rmp;
5786     int rv = 0;
5787
5788 #if IPSEC > 0
5789     vlib_main_t * vm = vlib_get_main();
5790     clib_error_t * error;
5791
5792     error = ikev2_set_local_key(vm, mp->key_file);
5793     if (error)
5794       rv = VNET_API_ERROR_UNSPECIFIED;
5795 #else
5796     rv = VNET_API_ERROR_UNIMPLEMENTED;
5797 #endif
5798
5799    REPLY_MACRO(VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
5800 }
5801
5802 static void
5803 vl_api_map_add_domain_t_handler
5804 (vl_api_map_add_domain_t * mp)
5805 {
5806   vl_api_map_add_domain_reply_t * rmp;
5807   int rv = 0;
5808   u32 index;
5809   u8 flags = mp->is_translation ? MAP_DOMAIN_TRANSLATION : 0;
5810   rv = map_create_domain((ip4_address_t *)&mp->ip4_prefix, mp->ip4_prefix_len,
5811                          (ip6_address_t *)&mp->ip6_prefix, mp->ip6_prefix_len,
5812                          (ip6_address_t *)&mp->ip6_src, mp->ip6_src_prefix_len,
5813                          mp->ea_bits_len, mp->psid_offset, mp->psid_length, &index, ntohs(mp->mtu), flags);
5814
5815   REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
5816                ({
5817                  rmp->index = ntohl(index);
5818                }));
5819 }
5820
5821 static void
5822 vl_api_map_del_domain_t_handler
5823 (vl_api_map_del_domain_t * mp)
5824 {
5825   vl_api_map_del_domain_reply_t * rmp;
5826   int rv = 0;
5827
5828   rv = map_delete_domain(ntohl(mp->index));
5829
5830   REPLY_MACRO(VL_API_MAP_DEL_DOMAIN_REPLY);
5831 }
5832
5833 static void
5834 vl_api_map_add_del_rule_t_handler
5835 (vl_api_map_add_del_rule_t * mp)
5836 {
5837   vl_api_map_del_domain_reply_t * rmp;
5838   int rv = 0;
5839
5840   rv = map_add_del_psid(ntohl(mp->index), ntohs(mp->psid), (ip6_address_t *)mp->ip6_dst, mp->is_add);
5841
5842   REPLY_MACRO(VL_API_MAP_ADD_DEL_RULE_REPLY);
5843 }
5844
5845 static void
5846 vl_api_map_domain_dump_t_handler
5847 (vl_api_map_domain_dump_t * mp)
5848 {
5849   vl_api_map_domain_details_t * rmp;
5850   map_main_t *mm = &map_main;
5851   map_domain_t *d;
5852   unix_shared_memory_queue_t * q;
5853
5854   if (pool_elts (mm->domains) == 0)
5855       return;
5856
5857   q = vl_api_client_index_to_input_queue (mp->client_index);
5858   if (q == 0) {
5859     return;
5860   }
5861
5862   pool_foreach(d, mm->domains, ({
5863     /* Make sure every field is initiated (or don't skip the memset()) */
5864     rmp = vl_msg_api_alloc (sizeof (*rmp));
5865     rmp->_vl_msg_id = ntohs(VL_API_MAP_DOMAIN_DETAILS);
5866     rmp->domain_index = htonl(d - mm->domains);
5867     rmp->ea_bits_len = d->ea_bits_len;
5868     rmp->psid_offset = d->psid_offset;
5869     rmp->psid_length = d->psid_length;
5870     clib_memcpy(rmp->ip4_prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix));
5871     rmp->ip4_prefix_len = d->ip4_prefix_len;
5872     clib_memcpy(rmp->ip6_prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix));
5873     rmp->ip6_prefix_len = d->ip6_prefix_len;
5874     clib_memcpy(rmp->ip6_src, &d->ip6_src, sizeof(rmp->ip6_src));
5875     rmp->ip6_src_len = d->ip6_src_len;
5876     rmp->mtu = htons(d->mtu);
5877     rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION);
5878     rmp->context = mp->context;
5879
5880     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5881   }));
5882 }
5883
5884 static void
5885 vl_api_map_rule_dump_t_handler
5886 (vl_api_map_rule_dump_t * mp)
5887 {
5888   unix_shared_memory_queue_t * q;
5889   u16 i;
5890   ip6_address_t dst;
5891   vl_api_map_rule_details_t * rmp;
5892   map_main_t *mm = &map_main;
5893   u32 domain_index = ntohl(mp->domain_index);
5894   map_domain_t *d;
5895
5896   if (pool_elts (mm->domains) == 0)
5897    return;
5898
5899   d = pool_elt_at_index(mm->domains, domain_index);
5900   if (!d || !d->rules) {
5901     return;
5902   }
5903
5904   q = vl_api_client_index_to_input_queue (mp->client_index);
5905   if (q == 0) {
5906     return;
5907   }
5908
5909   for (i = 0; i < (0x1 << d->psid_length); i++) {
5910     dst = d->rules[i];
5911     if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0) {
5912       continue;
5913     }
5914     rmp = vl_msg_api_alloc(sizeof(*rmp));
5915     memset(rmp, 0, sizeof(*rmp));
5916     rmp->_vl_msg_id = ntohs(VL_API_MAP_RULE_DETAILS);
5917     rmp->psid = htons(i);
5918     clib_memcpy(rmp->ip6_dst, &dst, sizeof(rmp->ip6_dst));
5919     rmp->context = mp->context;
5920     vl_msg_api_send_shmem(q, (u8 *)&rmp);
5921   }
5922 }
5923
5924 static void
5925 vl_api_map_summary_stats_t_handler (
5926     vl_api_map_summary_stats_t *mp)
5927 {
5928     vl_api_map_summary_stats_reply_t *rmp;
5929     vlib_combined_counter_main_t *cm;
5930     vlib_counter_t v;
5931     int i, which;
5932     u64 total_pkts[VLIB_N_RX_TX];
5933     u64 total_bytes[VLIB_N_RX_TX];
5934     map_main_t *mm = &map_main;
5935     unix_shared_memory_queue_t *q =
5936         vl_api_client_index_to_input_queue(mp->client_index);
5937
5938     if (!q)
5939         return;
5940
5941     rmp = vl_msg_api_alloc (sizeof (*rmp));
5942     rmp->_vl_msg_id = ntohs(VL_API_MAP_SUMMARY_STATS_REPLY);
5943     rmp->context = mp->context;
5944     rmp->retval = 0;
5945
5946     memset (total_pkts, 0, sizeof (total_pkts));
5947     memset (total_bytes, 0, sizeof (total_bytes));
5948
5949     map_domain_counter_lock (mm);
5950     vec_foreach(cm, mm->domain_counters) {
5951       which = cm - mm->domain_counters;
5952
5953       for (i = 0; i < vec_len(cm->maxi); i++) {
5954         vlib_get_combined_counter (cm, i, &v);
5955         total_pkts[which] += v.packets;
5956         total_bytes[which] += v.bytes;
5957       }
5958     }
5959
5960     map_domain_counter_unlock (mm);
5961
5962     /* Note: in network byte order! */
5963     rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] = clib_host_to_net_u64(total_pkts[MAP_DOMAIN_COUNTER_RX]);
5964     rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] = clib_host_to_net_u64(total_bytes[MAP_DOMAIN_COUNTER_RX]);
5965     rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] = clib_host_to_net_u64(total_pkts[MAP_DOMAIN_COUNTER_TX]);
5966     rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] = clib_host_to_net_u64(total_bytes[MAP_DOMAIN_COUNTER_TX]);
5967     rmp->total_bindings = clib_host_to_net_u64(pool_elts(mm->domains));
5968     rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
5969     rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] = clib_host_to_net_u64(map_error_counter_get(ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
5970     rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] = clib_host_to_net_u64(map_error_counter_get(ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
5971
5972     vl_msg_api_send_shmem(q, (u8 *)&rmp);
5973 }
5974
5975 static void vl_api_ipsec_sa_set_key_t_handler
5976 (vl_api_ipsec_sa_set_key_t * mp)
5977 {
5978     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5979     vl_api_ipsec_sa_set_key_reply_t *rmp;
5980     int rv;
5981 #if IPSEC > 0
5982     ipsec_sa_t sa;
5983     sa.id = ntohl(mp->sa_id);
5984     sa.crypto_key_len = mp->crypto_key_length;
5985     clib_memcpy(&sa.crypto_key, mp->crypto_key, sizeof(sa.crypto_key));
5986     sa.integ_key_len = mp->integrity_key_length;
5987     clib_memcpy(&sa.integ_key, mp->integrity_key, sizeof(sa.integ_key));
5988
5989     rv = ipsec_set_sa_key(vm, &sa);
5990 #else
5991     rv = VNET_API_ERROR_UNIMPLEMENTED;
5992 #endif
5993
5994     REPLY_MACRO(VL_API_IPSEC_SA_SET_KEY_REPLY);
5995 }
5996
5997 static void vl_api_cop_interface_enable_disable_t_handler
5998 (vl_api_cop_interface_enable_disable_t * mp)
5999 {
6000     vl_api_cop_interface_enable_disable_reply_t * rmp;
6001     int rv;
6002     u32 sw_if_index = ntohl(mp->sw_if_index);
6003     int enable_disable;
6004
6005     VALIDATE_SW_IF_INDEX(mp);
6006
6007     enable_disable = (int) mp->enable_disable;
6008
6009     rv = cop_interface_enable_disable (sw_if_index, enable_disable);
6010
6011     BAD_SW_IF_INDEX_LABEL;
6012
6013     REPLY_MACRO(VL_API_COP_INTERFACE_ENABLE_DISABLE_REPLY);
6014 }
6015
6016 static void vl_api_cop_whitelist_enable_disable_t_handler
6017 (vl_api_cop_whitelist_enable_disable_t * mp)
6018 {
6019     vl_api_cop_whitelist_enable_disable_reply_t * rmp;
6020     cop_whitelist_enable_disable_args_t _a, *a=&_a;
6021     u32 sw_if_index = ntohl(mp->sw_if_index);
6022     int rv;
6023
6024     VALIDATE_SW_IF_INDEX(mp);
6025
6026     a->sw_if_index = sw_if_index;
6027     a->ip4 = mp->ip4;
6028     a->ip6 = mp->ip6;
6029     a->default_cop = mp->default_cop;
6030     a->fib_id = ntohl(mp->fib_id);
6031
6032     rv = cop_whitelist_enable_disable (a);
6033
6034     BAD_SW_IF_INDEX_LABEL;
6035
6036     REPLY_MACRO(VL_API_COP_WHITELIST_ENABLE_DISABLE_REPLY);
6037 }
6038
6039 static void vl_api_get_node_graph_t_handler
6040 (vl_api_get_node_graph_t * mp)
6041 {
6042     int rv = 0;
6043     u8 * vector = 0;
6044     api_main_t * am = &api_main;
6045     vlib_main_t * vm = vlib_get_main();
6046     void * oldheap;
6047     vl_api_get_node_graph_reply_t * rmp;
6048
6049     pthread_mutex_lock (&am->vlib_rp->mutex);
6050     oldheap = svm_push_data_heap (am->vlib_rp);
6051
6052     /*
6053      * Keep the number of memcpy ops to a minimum (e.g. 1).
6054      */
6055     vec_validate (vector, 16384);
6056     vec_reset_length (vector);
6057
6058     /* $$$$ FIXME */
6059     vector = vlib_node_serialize (&vm->node_main, vector,
6060                                   (u32)~0 /* all threads */,
6061                                   1 /* include nexts */,
6062                                   1 /* include stats */);
6063
6064     svm_pop_heap (oldheap);
6065     pthread_mutex_unlock (&am->vlib_rp->mutex);
6066
6067     REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
6068                  rmp->reply_in_shmem = (uword) vector);
6069 }
6070
6071 static void vl_api_trace_profile_add_t_handler
6072 (vl_api_trace_profile_add_t *mp)
6073 {
6074     int rv = 0;
6075     vl_api_trace_profile_add_reply_t * rmp;
6076     clib_error_t *error;
6077
6078     /* Ignoring the profile id as currently a single profile
6079      * is supported */
6080     error = ip6_ioam_trace_profile_set(mp->trace_num_elt, mp->trace_type,
6081                                ntohl(mp->node_id), ntohl(mp->trace_app_data),
6082                                mp->pow_enable, mp->trace_tsp,
6083                                mp->trace_ppc);
6084     if (error) {
6085       clib_error_report(error);
6086       rv = clib_error_get_code(error);
6087     }
6088
6089     REPLY_MACRO(VL_API_TRACE_PROFILE_ADD_REPLY);
6090 }
6091
6092 static void vl_api_trace_profile_apply_t_handler
6093 (vl_api_trace_profile_apply_t *mp)
6094 {
6095     int rv = 0;
6096     vl_api_trace_profile_apply_reply_t * rmp;
6097
6098     if (mp->enable != 0) {
6099       rv = ip6_ioam_set_destination ((ip6_address_t *)(&mp->dest_ipv6),
6100                                 ntohl(mp->prefix_length),
6101                                 ntohl(mp->vrf_id),
6102                         mp->trace_op == IOAM_HBYH_ADD,
6103                         mp->trace_op == IOAM_HBYH_POP,
6104                         mp->trace_op == IOAM_HBYH_MOD);
6105     } else {
6106       //ip6_ioam_clear_destination(&ip6, mp->prefix_length, mp->vrf_id);
6107     }
6108     REPLY_MACRO(VL_API_TRACE_PROFILE_APPLY_REPLY);
6109 }
6110
6111 static void vl_api_trace_profile_del_t_handler
6112 (vl_api_trace_profile_del_t *mp)
6113 {
6114     int rv = 0;
6115     vl_api_trace_profile_del_reply_t * rmp;
6116     clib_error_t *error;
6117
6118     error = clear_ioam_rewrite_fn();
6119     if (error) {
6120       clib_error_report(error);
6121       rv = clib_error_get_code(error);
6122     }
6123
6124     REPLY_MACRO(VL_API_TRACE_PROFILE_DEL_REPLY);
6125 }
6126
6127 static void
6128 vl_api_af_packet_create_t_handler
6129 (vl_api_af_packet_create_t *mp)
6130 {
6131     vlib_main_t *vm = vlib_get_main();
6132     vl_api_af_packet_create_reply_t *rmp;
6133     int rv = 0;
6134     u8 *host_if_name = NULL;
6135     u32 sw_if_index;
6136
6137     host_if_name = format(0, "%s", mp->host_if_name);
6138     vec_add1 (host_if_name, 0);
6139
6140     rv = af_packet_create_if(vm, host_if_name,
6141                              mp->use_random_hw_addr ? 0 : mp->hw_addr, &sw_if_index);
6142
6143     vec_free(host_if_name);
6144
6145     REPLY_MACRO2(VL_API_AF_PACKET_CREATE_REPLY,
6146                  rmp->sw_if_index = clib_host_to_net_u32(sw_if_index));
6147 }
6148
6149 static void
6150 vl_api_af_packet_delete_t_handler
6151 (vl_api_af_packet_delete_t *mp)
6152 {
6153     vlib_main_t * vm = vlib_get_main();
6154     vl_api_af_packet_delete_reply_t *rmp;
6155     int rv = 0;
6156     u8 *host_if_name = NULL;
6157
6158     host_if_name = format(0, "%s", mp->host_if_name);
6159     vec_add1 (host_if_name, 0);
6160
6161     rv = af_packet_delete_if(vm, host_if_name);
6162
6163     vec_free(host_if_name);
6164
6165     REPLY_MACRO(VL_API_AF_PACKET_DELETE_REPLY);
6166 }
6167
6168 static void
6169 vl_api_policer_add_del_t_handler
6170 (vl_api_policer_add_del_t *mp)
6171 {
6172     vlib_main_t * vm = vlib_get_main();
6173     vl_api_policer_add_del_reply_t *rmp;
6174     int rv = 0;
6175     u8 *name = NULL;
6176     sse2_qos_pol_cfg_params_st cfg;
6177     clib_error_t * error;
6178
6179     name = format(0, "%s", mp->name);
6180
6181     memset (&cfg, 0, sizeof (cfg));
6182     cfg.rfc = mp->type;
6183     cfg.rnd_type = mp->round_type;
6184     cfg.rate_type = mp->rate_type;
6185     cfg.rb.kbps.cir_kbps = mp->cir;
6186     cfg.rb.kbps.eir_kbps = mp->eir;
6187     cfg.rb.kbps.cb_bytes = mp->cb;
6188     cfg.rb.kbps.eb_bytes = mp->eb;
6189
6190     error = policer_add_del(vm, name, &cfg, mp->is_add);
6191
6192     if (error)
6193       rv = VNET_API_ERROR_UNSPECIFIED;
6194
6195     REPLY_MACRO(VL_API_POLICER_ADD_DEL_REPLY);
6196 }
6197
6198 static void
6199 send_policer_details (u8 *name,
6200                       sse2_qos_pol_cfg_params_st *config,
6201                       policer_read_response_type_st *templ,
6202                       unix_shared_memory_queue_t *q,
6203                       u32 context)
6204 {
6205     vl_api_policer_details_t * mp;
6206
6207     mp = vl_msg_api_alloc (sizeof (*mp));
6208     memset (mp, 0, sizeof (*mp));
6209     mp->_vl_msg_id = ntohs (VL_API_POLICER_DETAILS);
6210     mp->context = context;
6211     mp->cir = htonl(config->rb.kbps.cir_kbps);
6212     mp->eir = htonl(config->rb.kbps.eir_kbps);
6213     mp->cb = htonl(config->rb.kbps.cb_bytes);
6214     mp->eb = htonl(config->rb.kbps.eb_bytes);
6215     mp->rate_type = config->rate_type;
6216     mp->round_type = config->rnd_type;
6217     mp->type = config->rfc;
6218     mp->single_rate = templ->single_rate ? 1 : 0;
6219     mp->color_aware = templ->color_aware ? 1 : 0;
6220     mp->scale = htonl(templ->scale);
6221     mp->cir_tokens_per_period = htonl(templ->cir_tokens_per_period);
6222     mp->pir_tokens_per_period = htonl(templ->pir_tokens_per_period);
6223     mp->current_limit = htonl(templ->current_limit);
6224     mp->current_bucket = htonl(templ->current_bucket);
6225     mp->extended_limit = htonl(templ->extended_limit);
6226     mp->extended_bucket = htonl(templ->extended_bucket);
6227     mp->last_update_time = clib_host_to_net_u64(templ->last_update_time);
6228
6229     strncpy ((char *) mp->name, (char *) name, ARRAY_LEN(mp->name) - 1);
6230
6231     vl_msg_api_send_shmem (q, (u8 *)&mp);
6232 }
6233
6234 static void
6235 vl_api_policer_dump_t_handler
6236 (vl_api_policer_dump_t *mp)
6237 {
6238     unix_shared_memory_queue_t * q;
6239     vnet_policer_main_t * pm = &vnet_policer_main;
6240     hash_pair_t * hp;
6241     uword * p;
6242     u32 pool_index;
6243     u8 * match_name = 0;
6244     u8 * name;
6245     sse2_qos_pol_cfg_params_st *config;
6246     policer_read_response_type_st *templ;
6247
6248     q = vl_api_client_index_to_input_queue (mp->client_index);
6249     if (q == 0)
6250         return;
6251
6252     if (mp->match_name_valid) {
6253         match_name = format(0, "%s%c", mp->match_name, 0);
6254     }
6255
6256     if (mp->match_name_valid) {
6257         p = hash_get_mem (pm->policer_config_by_name, match_name);
6258         if (p) {
6259             pool_index = p[0];
6260             config = pool_elt_at_index (pm->configs, pool_index);
6261             templ = pool_elt_at_index (pm->policer_templates, pool_index);
6262             send_policer_details(match_name, config, templ, q, mp->context);
6263         }
6264     } else {
6265         hash_foreach_pair (hp, pm->policer_config_by_name,
6266         ({
6267             name = (u8 *) hp->key;
6268             pool_index = hp->value[0];
6269             config = pool_elt_at_index (pm->configs, pool_index);
6270             templ = pool_elt_at_index (pm->policer_templates, pool_index);
6271             send_policer_details(name, config, templ, q, mp->context);
6272         }));
6273     }
6274 }
6275
6276 static void
6277 vl_api_netmap_create_t_handler
6278 (vl_api_netmap_create_t *mp)
6279 {
6280     vlib_main_t *vm = vlib_get_main();
6281     vl_api_netmap_create_reply_t *rmp;
6282     int rv = 0;
6283     u8 *if_name = NULL;
6284
6285     if_name = format(0, "%s", mp->netmap_if_name);
6286     vec_add1 (if_name, 0);
6287
6288     rv = netmap_create_if(vm, if_name, mp->use_random_hw_addr ? 0 : mp->hw_addr,
6289                           mp->is_pipe, mp->is_master, 0);
6290
6291     vec_free(if_name);
6292
6293     REPLY_MACRO(VL_API_NETMAP_CREATE_REPLY);
6294 }
6295
6296 static void
6297 vl_api_netmap_delete_t_handler
6298 (vl_api_netmap_delete_t *mp)
6299 {
6300     vlib_main_t * vm = vlib_get_main();
6301     vl_api_netmap_delete_reply_t *rmp;
6302     int rv = 0;
6303     u8 *if_name = NULL;
6304
6305     if_name = format(0, "%s", mp->netmap_if_name);
6306     vec_add1 (if_name, 0);
6307
6308     rv = netmap_delete_if(vm, if_name);
6309
6310     vec_free(if_name);
6311
6312     REPLY_MACRO(VL_API_NETMAP_DELETE_REPLY);
6313 }
6314
6315 static void vl_api_mpls_gre_tunnel_details_t_handler (
6316     vl_api_mpls_gre_tunnel_details_t * mp)
6317 {
6318     clib_warning ("BUG");
6319 }
6320
6321 static void send_mpls_gre_tunnel_entry (vpe_api_main_t * am,
6322                                     unix_shared_memory_queue_t *q,
6323                                     mpls_gre_tunnel_t * gt,
6324                                     u32 index,
6325                                     u32 context)
6326 {
6327     vl_api_mpls_gre_tunnel_details_t * mp;
6328
6329     mp = vl_msg_api_alloc (sizeof (*mp));
6330     memset (mp, 0, sizeof (*mp));
6331     mp->_vl_msg_id = ntohs(VL_API_MPLS_GRE_TUNNEL_DETAILS);
6332     mp->context = context;
6333
6334     if (gt != NULL) {
6335         mp->tunnel_index    = htonl(index);
6336         mp->tunnel_src      = gt->tunnel_src.as_u32;
6337         mp->tunnel_dst      = gt->tunnel_dst.as_u32;
6338         mp->intfc_address   = gt->intfc_address.as_u32;
6339         mp->mask_width      = htonl(gt->mask_width);
6340         mp->inner_fib_index = htonl(gt->inner_fib_index);
6341         mp->outer_fib_index = htonl(gt->outer_fib_index);
6342         mp->encap_index     = htonl(gt->encap_index);
6343         mp->hw_if_index     = htonl(gt->hw_if_index);
6344         mp->l2_only         = htonl(gt->l2_only);
6345     }
6346
6347     mpls_main_t * mm = &mpls_main;
6348     mpls_encap_t * e;
6349     int i;
6350     u32 len = 0;
6351
6352     e = pool_elt_at_index (mm->encaps, gt->encap_index);
6353     len = vec_len (e->labels);
6354     mp->nlabels = htonl(len);
6355
6356     for (i = 0; i < len; i++) {
6357         mp->labels[i] = htonl(vnet_mpls_uc_get_label(
6358                 clib_host_to_net_u32(e->labels[i].label_exp_s_ttl)));
6359     }
6360
6361     vl_msg_api_send_shmem (q, (u8 *)&mp);
6362 }
6363
6364 static void
6365 vl_api_mpls_gre_tunnel_dump_t_handler (vl_api_mpls_gre_tunnel_dump_t *mp)
6366 {
6367     vpe_api_main_t * am = &vpe_api_main;
6368     unix_shared_memory_queue_t * q;
6369     vlib_main_t * vm = &vlib_global_main;
6370     mpls_main_t * mm = &mpls_main;
6371     mpls_gre_tunnel_t * gt;
6372     u32 index = ntohl(mp->tunnel_index);
6373
6374     q = vl_api_client_index_to_input_queue (mp->client_index);
6375     if (q == 0)
6376         return;
6377
6378     if (pool_elts (mm->gre_tunnels)) {
6379         if(mp->tunnel_index >= 0) {
6380             vlib_cli_output (vm, "MPLS-GRE tunnel %u", index);
6381             gt = pool_elt_at_index (mm->gre_tunnels, index);
6382             send_mpls_gre_tunnel_entry (am, q, gt, gt - mm->gre_tunnels, mp->context);
6383         } else {
6384             vlib_cli_output (vm, "MPLS-GRE tunnels");
6385             pool_foreach (gt, mm->gre_tunnels,
6386             ({
6387               send_mpls_gre_tunnel_entry (am, q, gt, gt - mm->gre_tunnels, mp->context);
6388             }));
6389         }
6390     } else {
6391         vlib_cli_output (vm, "No MPLS-GRE tunnels");
6392     }
6393 }
6394
6395 static void vl_api_mpls_eth_tunnel_details_t_handler (
6396     vl_api_mpls_eth_tunnel_details_t * mp)
6397 {
6398     clib_warning ("BUG");
6399 }
6400
6401 static void send_mpls_eth_tunnel_entry (vpe_api_main_t * am,
6402                                     unix_shared_memory_queue_t *q,
6403                                     mpls_eth_tunnel_t * et,
6404                                     u32 index,
6405                                     u32 context)
6406 {
6407     vl_api_mpls_eth_tunnel_details_t * mp;
6408
6409     mp = vl_msg_api_alloc (sizeof (*mp));
6410     memset (mp, 0, sizeof (*mp));
6411     mp->_vl_msg_id = ntohs(VL_API_MPLS_ETH_TUNNEL_DETAILS);
6412     mp->context = context;
6413
6414     if (et != NULL) {
6415         mp->tunnel_index    = htonl(index);
6416         memcpy(mp->tunnel_dst_mac, et->tunnel_dst, 6);
6417         mp->intfc_address   = et->intfc_address.as_u32;
6418         mp->tx_sw_if_index  = htonl(et->tx_sw_if_index);
6419         mp->inner_fib_index = htonl(et->inner_fib_index);
6420         mp->mask_width      = htonl(et->mask_width);
6421         mp->encap_index     = htonl(et->encap_index);
6422         mp->hw_if_index     = htonl(et->hw_if_index);
6423         mp->l2_only         = htonl(et->l2_only);
6424     }
6425
6426     mpls_main_t * mm = &mpls_main;
6427     mpls_encap_t * e;
6428     int i;
6429     u32 len = 0;
6430
6431     e = pool_elt_at_index (mm->encaps, et->encap_index);
6432     len = vec_len (e->labels);
6433     mp->nlabels = htonl(len);
6434
6435     for (i = 0; i < len; i++) {
6436         mp->labels[i] = htonl(vnet_mpls_uc_get_label(
6437                 clib_host_to_net_u32(e->labels[i].label_exp_s_ttl)));
6438     }
6439
6440     vl_msg_api_send_shmem (q, (u8 *)&mp);
6441 }
6442
6443 static void
6444 vl_api_mpls_eth_tunnel_dump_t_handler (vl_api_mpls_eth_tunnel_dump_t *mp)
6445 {
6446     vpe_api_main_t * am = &vpe_api_main;
6447     unix_shared_memory_queue_t * q;
6448     vlib_main_t * vm = &vlib_global_main;
6449     mpls_main_t * mm = &mpls_main;
6450     mpls_eth_tunnel_t * et;
6451     u32 index = ntohl(mp->tunnel_index);
6452
6453     q = vl_api_client_index_to_input_queue (mp->client_index);
6454     if (q == 0)
6455         return;
6456
6457     clib_warning("Received mpls_eth_tunnel_dump");
6458     clib_warning("Received tunnel index: %u from client %u", index, mp->client_index);
6459
6460     if (pool_elts (mm->eth_tunnels)) {
6461         if(mp->tunnel_index >= 0) {
6462             vlib_cli_output (vm, "MPLS-Ethernet tunnel %u", index);
6463             et = pool_elt_at_index (mm->eth_tunnels, index);
6464             send_mpls_eth_tunnel_entry (am, q, et, et - mm->eth_tunnels, mp->context);
6465         } else {
6466             clib_warning("MPLS-Ethernet tunnels");
6467             pool_foreach (et, mm->eth_tunnels,
6468             ({
6469                 send_mpls_eth_tunnel_entry (am, q, et, et - mm->eth_tunnels, mp->context);
6470             }));
6471         }
6472     } else {
6473         clib_warning("No MPLS-Ethernet tunnels");
6474     }
6475 }
6476
6477 static void vl_api_mpls_fib_encap_details_t_handler (
6478     vl_api_mpls_fib_encap_details_t * mp)
6479 {
6480     clib_warning ("BUG");
6481 }
6482
6483 static void send_mpls_fib_encap_details (vpe_api_main_t * am,
6484                                     unix_shared_memory_queue_t *q,
6485                                     show_mpls_fib_t *s,
6486                                     u32 context)
6487 {
6488     vl_api_mpls_fib_encap_details_t * mp;
6489
6490     mp = vl_msg_api_alloc (sizeof (*mp));
6491     memset (mp, 0, sizeof (*mp));
6492     mp->_vl_msg_id = ntohs(VL_API_MPLS_FIB_ENCAP_DETAILS);
6493     mp->context = context;
6494
6495     mp->fib_index   = htonl(s->fib_index);
6496     mp->entry_index = htonl(s->entry_index);
6497     mp->dest        = s->dest;
6498     mp->s_bit       = htonl(s->s_bit);
6499
6500     mpls_main_t * mm = &mpls_main;
6501     mpls_encap_t * e;
6502     int i;
6503     u32 len = 0;
6504
6505     e = pool_elt_at_index (mm->encaps, s->entry_index);
6506     len = vec_len (e->labels);
6507     mp->nlabels = htonl(len);
6508
6509     for (i = 0; i < len; i++) {
6510         mp->labels[i] = htonl(vnet_mpls_uc_get_label(
6511                 clib_host_to_net_u32(e->labels[i].label_exp_s_ttl)));
6512     }
6513
6514     vl_msg_api_send_shmem (q, (u8 *)&mp);
6515 }
6516
6517 static void
6518 vl_api_mpls_fib_encap_dump_t_handler (vl_api_mpls_fib_encap_dump_t *mp)
6519 {
6520     vpe_api_main_t * am = &vpe_api_main;
6521     unix_shared_memory_queue_t * q;
6522     vlib_main_t * vm = &vlib_global_main;
6523     u64 key;
6524     u32 value;
6525     show_mpls_fib_t *records = 0;
6526     show_mpls_fib_t *s;
6527     mpls_main_t * mm = &mpls_main;
6528     ip4_main_t * im = &ip4_main;
6529     ip4_fib_t * rx_fib;
6530
6531     q = vl_api_client_index_to_input_queue (mp->client_index);
6532     if (q == 0)
6533         return;
6534
6535     hash_foreach (key, value, mm->mpls_encap_by_fib_and_dest,
6536     ({
6537         vec_add2 (records, s, 1);
6538         s->fib_index = (u32)(key>>32);
6539         s->dest = (u32)(key & 0xFFFFFFFF);
6540         s->entry_index = (u32) value;
6541     }));
6542
6543     if (0 == vec_len(records)) {
6544         vlib_cli_output(vm, "MPLS encap table empty");
6545         goto out;
6546     }
6547
6548     /* sort output by dst address within fib */
6549     vec_sort_with_function(records, mpls_dest_cmp);
6550     vec_sort_with_function(records, mpls_fib_index_cmp);
6551     vlib_cli_output(vm, "MPLS encap table");
6552     vlib_cli_output(vm, "%=6s%=16s%=16s", "Table", "Dest address", "Labels");
6553     vec_foreach (s, records)
6554     {
6555         rx_fib = vec_elt_at_index(im->fibs, s->fib_index);
6556         vlib_cli_output(vm, "%=6d%=16U%=16U", rx_fib->table_id,
6557                 format_ip4_address, &s->dest, format_mpls_encap_index, mm,
6558                 s->entry_index);
6559         send_mpls_fib_encap_details (am, q, s, mp->context);
6560     }
6561
6562 out:
6563     vec_free(records);
6564 }
6565
6566 static void vl_api_mpls_fib_decap_details_t_handler (
6567     vl_api_mpls_fib_decap_details_t * mp)
6568 {
6569     clib_warning ("BUG");
6570 }
6571
6572 static void send_mpls_fib_decap_details (vpe_api_main_t * am,
6573                                     unix_shared_memory_queue_t *q,
6574                                     show_mpls_fib_t *s,
6575                                     u32 rx_table_id,
6576                                     u32 tx_table_id,
6577                                     char *swif_tag,
6578                                     u32 context)
6579 {
6580     vl_api_mpls_fib_decap_details_t * mp;
6581
6582     mp = vl_msg_api_alloc (sizeof (*mp));
6583     memset (mp, 0, sizeof (*mp));
6584     mp->_vl_msg_id = ntohs(VL_API_MPLS_FIB_DECAP_DETAILS);
6585     mp->context = context;
6586
6587     mp->fib_index   = htonl(s->fib_index);
6588     mp->entry_index = htonl(s->entry_index);
6589     mp->dest        = s->dest;
6590     mp->s_bit       = htonl(s->s_bit);
6591     mp->label       = htonl(s->label);
6592     mp->rx_table_id = htonl(rx_table_id);
6593     mp->tx_table_id = htonl(tx_table_id);
6594     strncpy ((char *) mp->swif_tag,
6595              (char *) swif_tag, ARRAY_LEN(mp->swif_tag)-1);
6596
6597     vl_msg_api_send_shmem (q, (u8 *)&mp);
6598 }
6599
6600 static void
6601 vl_api_mpls_fib_decap_dump_t_handler (vl_api_mpls_fib_decap_dump_t *mp)
6602 {
6603     vpe_api_main_t * am = &vpe_api_main;
6604     unix_shared_memory_queue_t * q;
6605     vlib_main_t * vm = &vlib_global_main;
6606     u64 key;
6607     u32 value;
6608     show_mpls_fib_t *records = 0;
6609     show_mpls_fib_t *s;
6610     mpls_main_t * mm = &mpls_main;
6611     ip4_main_t * im = &ip4_main;
6612     ip4_fib_t * rx_fib;
6613     ip4_fib_t *tx_fib;
6614     u32 tx_table_id;
6615     char *swif_tag;
6616
6617     q = vl_api_client_index_to_input_queue (mp->client_index);
6618     if (q == 0)
6619         return;
6620
6621     hash_foreach (key, value, mm->mpls_decap_by_rx_fib_and_label,
6622     ({
6623         vec_add2 (records, s, 1);
6624         s->fib_index = (u32)(key>>32);
6625         s->entry_index = (u32) value;
6626         s->label = ((u32) key)>>12;
6627         s->s_bit = (key & (1<<8)) != 0;
6628     }));
6629
6630     if (!vec_len(records)) {
6631         vlib_cli_output(vm, "MPLS decap table empty");
6632         goto out;
6633     }
6634
6635     vec_sort_with_function(records, mpls_label_cmp);
6636     vlib_cli_output(vm, "MPLS decap table");
6637     vlib_cli_output(vm, "%=10s%=15s%=6s%=6s", "RX Table", "TX Table/Intfc",
6638             "Label", "S-bit");
6639     vec_foreach (s, records)
6640     {
6641         mpls_decap_t * d;
6642         d = pool_elt_at_index(mm->decaps, s->entry_index);
6643         if (d->next_index == MPLS_INPUT_NEXT_IP4_INPUT) {
6644             tx_fib = vec_elt_at_index(im->fibs, d->tx_fib_index);
6645             tx_table_id = tx_fib->table_id;
6646             swif_tag = "     ";
6647         } else {
6648             tx_table_id = d->tx_fib_index;
6649             swif_tag = "(i)  ";
6650         }
6651         rx_fib = vec_elt_at_index(im->fibs, s->fib_index);
6652
6653         vlib_cli_output(vm, "%=10d%=10d%=5s%=6d%=6d", rx_fib->table_id,
6654                 tx_table_id, swif_tag, s->label, s->s_bit);
6655
6656         send_mpls_fib_decap_details (am, q, s, rx_fib->table_id,
6657                 tx_table_id, swif_tag, mp->context);
6658     }
6659
6660 out:
6661     vec_free(records);
6662 }
6663
6664 #define BOUNCE_HANDLER(nn)                                              \
6665 static void vl_api_##nn##_t_handler (                                   \
6666     vl_api_##nn##_t *mp)                                                \
6667 {                                                                       \
6668     vpe_client_registration_t *reg;                                     \
6669     vpe_api_main_t * vam = &vpe_api_main;                               \
6670     unix_shared_memory_queue_t * q;                                     \
6671                                                                         \
6672     /* One registration only... */                                      \
6673     pool_foreach(reg, vam->nn##_registrations,                          \
6674     ({                                                                  \
6675         q = vl_api_client_index_to_input_queue (reg->client_index);     \
6676         if (q) {                                                        \
6677             /*                                                          \
6678              * If the queue is stuffed, turf the msg and complain       \
6679              * It's unlikely that the intended recipient is             \
6680              * alive; avoid deadlock at all costs.                      \
6681              */                                                         \
6682             if (q->cursize == q->maxsize) {                             \
6683                 clib_warning ("ERROR: receiver queue full, drop msg");  \
6684                 vl_msg_api_free (mp);                                   \
6685                 return;                                                 \
6686             }                                                           \
6687             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
6688             return;                                                     \
6689         }                                                               \
6690     }));                                                                \
6691     vl_msg_api_free (mp);                                               \
6692 }
6693
6694 /*
6695  * vpe_api_hookup
6696  * Add vpe's API message handlers to the table.
6697  * vlib has alread mapped shared memory and
6698  * added the client registration handlers.
6699  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
6700  */
6701
6702 static clib_error_t *
6703 vpe_api_hookup (vlib_main_t *vm)
6704 {
6705     api_main_t * am = &api_main;
6706
6707 #define _(N,n)                                                  \
6708     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
6709                            vl_api_##n##_t_handler,              \
6710                            vl_noop_handler,                     \
6711                            vl_api_##n##_t_endian,               \
6712                            vl_api_##n##_t_print,                \
6713                            sizeof(vl_api_##n##_t), 1);
6714     foreach_vpe_api_msg;
6715 #undef _
6716
6717     /*
6718      * Manually register the sr tunnel add del msg, so we trace
6719      * enough bytes to capture a typical segment list
6720      */
6721     vl_msg_api_set_handlers (VL_API_SR_TUNNEL_ADD_DEL,
6722                              "sr_tunnel_add_del",
6723                              vl_api_sr_tunnel_add_del_t_handler,
6724                              vl_noop_handler,
6725                              vl_api_sr_tunnel_add_del_t_endian,
6726                              vl_api_sr_tunnel_add_del_t_print,
6727                              256, 1);
6728
6729
6730     /*
6731      * Manually register the sr policy add del msg, so we trace
6732      * enough bytes to capture a typical tunnel name list
6733      */
6734     vl_msg_api_set_handlers (VL_API_SR_POLICY_ADD_DEL,
6735                              "sr_policy_add_del",
6736                              vl_api_sr_policy_add_del_t_handler,
6737                              vl_noop_handler,
6738                              vl_api_sr_policy_add_del_t_endian,
6739                              vl_api_sr_policy_add_del_t_print,
6740                              256, 1);
6741
6742     /*
6743      * Trace space for 8 MPLS encap labels, classifier mask+match
6744      */
6745     am->api_trace_cfg [VL_API_MPLS_ADD_DEL_ENCAP].size += 8 * sizeof(u32);
6746     am->api_trace_cfg [VL_API_CLASSIFY_ADD_DEL_TABLE].size
6747         += 5 * sizeof (u32x4);
6748     am->api_trace_cfg [VL_API_CLASSIFY_ADD_DEL_SESSION].size
6749         += 5 * sizeof (u32x4);
6750     am->api_trace_cfg [VL_API_VXLAN_ADD_DEL_TUNNEL].size
6751         += 16 * sizeof (u32);
6752
6753     /*
6754      * Thread-safe API messages
6755      */
6756     am->is_mp_safe [VL_API_IP_ADD_DEL_ROUTE] = 1;
6757     am->is_mp_safe [VL_API_GET_NODE_GRAPH] = 1;
6758
6759     return 0;
6760 }
6761
6762 VLIB_API_INIT_FUNCTION(vpe_api_hookup);
6763
6764 static clib_error_t *
6765 vpe_api_init (vlib_main_t *vm)
6766 {
6767     vpe_api_main_t *am = &vpe_api_main;
6768
6769     am->vlib_main = vm;
6770     am->vnet_main = vnet_get_main();
6771     am->interface_events_registration_hash = hash_create (0, sizeof (uword));
6772     am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
6773     am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
6774     am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
6775     am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
6776     am->oam_events_registration_hash = hash_create (0, sizeof (uword));
6777
6778     vl_api_init (vm);
6779     vl_set_memory_region_name ("/vpe-api");
6780     vl_enable_disable_memory_api (vm, 1 /* enable it */);
6781
6782     return 0;
6783 }
6784
6785 VLIB_INIT_FUNCTION(vpe_api_init);
6786
6787
6788 static clib_error_t *
6789 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
6790 {
6791   u8 * chroot_path;
6792   int uid, gid, rv;
6793   char *s, buf[128];
6794   struct passwd _pw, *pw;
6795   struct group _grp, *grp;
6796   clib_error_t *e;
6797
6798   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
6799     {
6800       if (unformat (input, "prefix %s", &chroot_path))
6801         {
6802           vec_add1 (chroot_path, 0);
6803           vl_set_memory_root_path ((char *)chroot_path);
6804         }
6805       else if (unformat (input, "uid %d", &uid))
6806         vl_set_memory_uid (uid);
6807       else if (unformat (input, "gid %d", &gid))
6808         vl_set_memory_gid (gid);
6809       else if (unformat (input, "uid %s", &s))
6810         {
6811           /* lookup the username */
6812           pw = NULL;
6813           rv = getpwnam_r(s, &_pw, buf, sizeof(buf), &pw);
6814           if (rv < 0)
6815             {
6816               e = clib_error_return_code(0, rv,
6817                        CLIB_ERROR_ERRNO_VALID | CLIB_ERROR_FATAL,
6818                        "cannot fetch username %s", s);
6819               vec_free (s);
6820               return e;
6821             }
6822           if (pw == NULL)
6823             {
6824               e = clib_error_return_fatal(0, "username %s does not exist", s);
6825               vec_free (s);
6826               return e;
6827             }
6828           vec_free (s);
6829           vl_set_memory_uid (pw->pw_uid);
6830         }
6831       else if (unformat (input, "gid %s", &s))
6832         {
6833           /* lookup the group name */
6834           grp = NULL;
6835           rv = getgrnam_r(s, &_grp, buf, sizeof(buf), &grp);
6836           if (rv != 0)
6837             {
6838               e = clib_error_return_code(0, rv,
6839                        CLIB_ERROR_ERRNO_VALID | CLIB_ERROR_FATAL,
6840                        "cannot fetch group %s", s);
6841               vec_free (s);
6842               return e;
6843             }
6844           if (grp == NULL)
6845             {
6846               e = clib_error_return_fatal(0, "group %s does not exist", s);
6847               vec_free (s);
6848               return e;
6849             }
6850           vec_free (s);
6851           vl_set_memory_gid (grp->gr_gid);
6852         }
6853       else
6854         return clib_error_return (0, "unknown input `%U'",
6855                                   format_unformat_error, input);
6856     }
6857   return 0;
6858 }
6859 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
6860
6861 void * get_unformat_vnet_sw_interface (void)
6862 {
6863     return (void *) &unformat_vnet_sw_interface;
6864 }
6865
6866 #undef vl_api_version
6867 #define vl_api_version(n,v) static u32 vpe_api_version = v;
6868 #include <api/vpe.api.h>
6869 #undef vl_api_version
6870
6871 int vl_msg_api_version_check (vl_api_memclnt_create_t * mp)
6872 {
6873     if (clib_host_to_net_u32(mp->api_versions[0]) != vpe_api_version) {
6874         clib_warning ("vpe API mismatch: 0x%08x instead of 0x%08x",
6875                       clib_host_to_net_u32 (mp->api_versions[0]),
6876                       vpe_api_version);
6877         return -1;
6878     }
6879     return 0;
6880 }
6881
6882 static u8 * format_arp_event (u8 * s, va_list * args)
6883 {
6884     vl_api_ip4_arp_event_t * event = va_arg (*args, vl_api_ip4_arp_event_t *);
6885
6886     s = format (s, "pid %d: %U", event->pid,
6887                 format_ip4_address, &event->address);
6888     return s;
6889 }
6890
6891 static clib_error_t *
6892 show_ip4_arp_events_fn (vlib_main_t * vm,
6893                         unformat_input_t * input,
6894                         vlib_cli_command_t * cmd)
6895 {
6896     vpe_api_main_t * am = &vpe_api_main;
6897     vl_api_ip4_arp_event_t * event;
6898
6899     if (pool_elts (am->arp_events) == 0) {
6900         vlib_cli_output (vm, "No active arp event registrations");
6901         return 0;
6902     }
6903
6904     pool_foreach (event, am->arp_events,
6905     ({
6906         vlib_cli_output (vm, "%U", format_arp_event, event);
6907     }));
6908
6909     return 0;
6910 }
6911
6912 VLIB_CLI_COMMAND (show_ip4_arp_events, static) = {
6913   .path = "show arp event registrations",
6914   .function = show_ip4_arp_events_fn,
6915   .short_help = "Show arp event registrations",
6916 };