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