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