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