Variable-message-length tracing support, VPP-370
[vpp.git] / vpp / vpp-api / api.c
1 /*
2  *------------------------------------------------------------------
3  * api.c - message handler registration
4  *
5  * Copyright (c) 2010-2016 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>
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/ip/ip6.h>
55 #include <vnet/unix/tuntap.h>
56 #include <vnet/unix/tapcli.h>
57 #include <vnet/mpls/mpls.h>
58 #include <vnet/mpls/mpls_tunnel.h>
59 #include <vnet/dhcp/proxy.h>
60 #include <vnet/dhcp/client.h>
61 #if IPV6SR > 0
62 #include <vnet/sr/sr.h>
63 #endif
64 #include <vnet/dhcpv6/proxy.h>
65 #include <vlib/vlib.h>
66 #include <vlib/unix/unix.h>
67 #include <vlibapi/api.h>
68 #include <vlibmemory/api.h>
69 #include <vnet/classify/vnet_classify.h>
70 #include <vnet/classify/input_acl.h>
71 #include <vnet/classify/policer_classify.h>
72 #include <vnet/classify/flow_classify.h>
73 #include <vnet/l2/l2_classify.h>
74 #include <vnet/vxlan/vxlan.h>
75 #include <vnet/gre/gre.h>
76 #include <vnet/l2/l2_vtr.h>
77 #include <vnet/vxlan-gpe/vxlan_gpe.h>
78 #include <vnet/lisp-gpe/lisp_gpe.h>
79 #include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
80 #include <vnet/lisp-gpe/lisp_gpe_tenant.h>
81 #include <vnet/lisp-cp/control.h>
82 #include <vnet/map/map.h>
83 #include <vnet/cop/cop.h>
84 #include <vnet/ip/ip6_hop_by_hop.h>
85 #include <vnet/ip/ip_source_and_port_range_check.h>
86 #include <vnet/devices/af_packet/af_packet.h>
87 #include <vnet/policer/policer.h>
88 #include <vnet/devices/netmap/netmap.h>
89 #include <vnet/flow/flow_report.h>
90 #include <vnet/ipsec-gre/ipsec_gre.h>
91 #include <vnet/flow/flow_report_classify.h>
92 #include <vnet/ip/punt.h>
93 #include <vnet/feature/feature.h>
94
95 #undef BIHASH_TYPE
96 #undef __included_bihash_template_h__
97 #include <vnet/l2/l2_fib.h>
98
99 #if DPDK > 0
100 #include <vnet/devices/dpdk/dpdk.h>
101 #endif
102
103 #if IPSEC > 0
104 #include <vnet/ipsec/ipsec.h>
105 #include <vnet/ipsec/ikev2.h>
106 #endif /* IPSEC */
107 #include <vnet/devices/virtio/vhost-user.h>
108
109 #include <stats/stats.h>
110 #include <oam/oam.h>
111
112 #include <vnet/ethernet/ethernet.h>
113 #include <vnet/ethernet/arp_packet.h>
114 #include <vnet/interface.h>
115
116 #include <vnet/l2/l2_fib.h>
117 #include <vnet/l2/l2_bd.h>
118 #include <vpp-api/vpe_msg_enum.h>
119 #include <vnet/span/span.h>
120
121 #include <vnet/fib/ip6_fib.h>
122 #include <vnet/fib/ip4_fib.h>
123 #include <vnet/dpo/drop_dpo.h>
124 #include <vnet/dpo/receive_dpo.h>
125 #include <vnet/dpo/lookup_dpo.h>
126 #include <vnet/dpo/classify_dpo.h>
127 #include <vnet/dpo/ip_null_dpo.h>
128
129 #define vl_typedefs             /* define message structures */
130 #include <vpp-api/vpe_all_api_h.h>
131 #undef vl_typedefs
132
133 #define vl_endianfun            /* define message structures */
134 #include <vpp-api/vpe_all_api_h.h>
135 #undef vl_endianfun
136
137 /* instantiate all the print functions we know about */
138 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
139 #define vl_printfun
140 #include <vpp-api/vpe_all_api_h.h>
141 #undef vl_printfun
142
143 #include <vlibapi/api_helper_macros.h>
144
145 #define foreach_vpe_api_msg                                             \
146 _(WANT_INTERFACE_EVENTS, want_interface_events)                         \
147 _(WANT_OAM_EVENTS, want_oam_events)                                     \
148 _(OAM_ADD_DEL, oam_add_del)                                             \
149 _(SW_INTERFACE_DUMP, sw_interface_dump)                                 \
150 _(SW_INTERFACE_DETAILS, sw_interface_details)                           \
151 _(IP_ADD_DEL_ROUTE, ip_add_del_route)                                   \
152 _(MPLS_ROUTE_ADD_DEL, mpls_route_add_del)                               \
153 _(MPLS_IP_BIND_UNBIND, mpls_ip_bind_unbind)                             \
154 _(IS_ADDRESS_REACHABLE, is_address_reachable)                           \
155 _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address)           \
156 _(SW_INTERFACE_SET_TABLE, sw_interface_set_table)                       \
157 _(SW_INTERFACE_SET_MPLS_ENABLE, sw_interface_set_mpls_enable)           \
158 _(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath)                       \
159 _(SW_INTERFACE_SET_VXLAN_BYPASS, sw_interface_set_vxlan_bypass)         \
160 _(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect)           \
161 _(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge)               \
162 _(SW_INTERFACE_SET_DPDK_HQOS_PIPE, sw_interface_set_dpdk_hqos_pipe)     \
163 _(SW_INTERFACE_SET_DPDK_HQOS_SUBPORT, sw_interface_set_dpdk_hqos_subport) \
164 _(SW_INTERFACE_SET_DPDK_HQOS_TCTBL, sw_interface_set_dpdk_hqos_tctbl)   \
165 _(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del)                         \
166 _(BRIDGE_DOMAIN_DUMP, bridge_domain_dump)                               \
167 _(BRIDGE_DOMAIN_DETAILS, bridge_domain_details)                         \
168 _(BRIDGE_DOMAIN_SW_IF_DETAILS, bridge_domain_sw_if_details)             \
169 _(L2FIB_ADD_DEL, l2fib_add_del)                                         \
170 _(L2_FLAGS, l2_flags)                                                   \
171 _(BRIDGE_FLAGS, bridge_flags)                                           \
172 _(TAP_CONNECT, tap_connect)                                             \
173 _(TAP_MODIFY, tap_modify)                                               \
174 _(TAP_DELETE, tap_delete)                                               \
175 _(SW_INTERFACE_TAP_DUMP, sw_interface_tap_dump)                         \
176 _(CREATE_VLAN_SUBIF, create_vlan_subif)                                 \
177 _(CREATE_SUBIF, create_subif)                                           \
178 _(MPLS_TUNNEL_ADD_DEL, mpls_tunnel_add_del)                             \
179 _(PROXY_ARP_ADD_DEL, proxy_arp_add_del)                                 \
180 _(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable)       \
181 _(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del)                             \
182 _(VNET_GET_SUMMARY_STATS, vnet_get_summary_stats)                       \
183 _(RESET_FIB, reset_fib)                                                 \
184 _(DHCP_PROXY_CONFIG,dhcp_proxy_config)                                  \
185 _(DHCP_PROXY_CONFIG_2,dhcp_proxy_config_2)                              \
186 _(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss)                                \
187 _(DHCP_CLIENT_CONFIG, dhcp_client_config)                               \
188 _(SET_IP_FLOW_HASH,set_ip_flow_hash)                                    \
189 _(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config)           \
190 _(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix)           \
191 _(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable )    \
192 _(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS,                              \
193   sw_interface_ip6_set_link_local_address)                              \
194 _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered)             \
195 _(CREATE_LOOPBACK, create_loopback)                                     \
196 _(CONTROL_PING, control_ping)                                           \
197 _(CLI_REQUEST, cli_request)                                             \
198 _(CLI_INBAND, cli_inband)                                               \
199 _(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit)                       \
200 _(L2_PATCH_ADD_DEL, l2_patch_add_del)                                   \
201 _(CLASSIFY_ADD_DEL_TABLE, classify_add_del_table)                       \
202 _(CLASSIFY_ADD_DEL_SESSION, classify_add_del_session)                   \
203 _(CLASSIFY_SET_INTERFACE_IP_TABLE, classify_set_interface_ip_table)     \
204 _(CLASSIFY_SET_INTERFACE_L2_TABLES, classify_set_interface_l2_tables)   \
205 _(GET_NODE_INDEX, get_node_index)                                       \
206 _(ADD_NODE_NEXT, add_node_next)                                         \
207 _(L2TPV3_CREATE_TUNNEL, l2tpv3_create_tunnel)                           \
208 _(L2TPV3_SET_TUNNEL_COOKIES, l2tpv3_set_tunnel_cookies)                 \
209 _(L2TPV3_INTERFACE_ENABLE_DISABLE, l2tpv3_interface_enable_disable)     \
210 _(L2TPV3_SET_LOOKUP_KEY, l2tpv3_set_lookup_key)                         \
211 _(SW_IF_L2TPV3_TUNNEL_DUMP, sw_if_l2tpv3_tunnel_dump)                   \
212 _(VXLAN_ADD_DEL_TUNNEL, vxlan_add_del_tunnel)                           \
213 _(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump)                                 \
214 _(GRE_ADD_DEL_TUNNEL, gre_add_del_tunnel)                               \
215 _(GRE_TUNNEL_DUMP, gre_tunnel_dump)                                     \
216 _(L2_FIB_CLEAR_TABLE, l2_fib_clear_table)                               \
217 _(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter)                     \
218 _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite)         \
219 _(CREATE_VHOST_USER_IF, create_vhost_user_if)                           \
220 _(MODIFY_VHOST_USER_IF, modify_vhost_user_if)                           \
221 _(DELETE_VHOST_USER_IF, delete_vhost_user_if)                           \
222 _(SW_INTERFACE_VHOST_USER_DUMP, sw_interface_vhost_user_dump)           \
223 _(IP_ADDRESS_DUMP, ip_address_dump)                                     \
224 _(IP_DUMP, ip_dump)                                                     \
225 _(SW_INTERFACE_VHOST_USER_DETAILS, sw_interface_vhost_user_details)     \
226 _(SHOW_VERSION, show_version)                                           \
227 _(L2_FIB_TABLE_DUMP, l2_fib_table_dump)                                 \
228 _(L2_FIB_TABLE_ENTRY, l2_fib_table_entry)                               \
229 _(VXLAN_GPE_ADD_DEL_TUNNEL, vxlan_gpe_add_del_tunnel)                   \
230 _(VXLAN_GPE_TUNNEL_DUMP, vxlan_gpe_tunnel_dump)                         \
231 _(INTERFACE_NAME_RENUMBER, interface_name_renumber)                     \
232 _(WANT_IP4_ARP_EVENTS, want_ip4_arp_events)                             \
233 _(WANT_IP6_ND_EVENTS, want_ip6_nd_events)                               \
234 _(INPUT_ACL_SET_INTERFACE, input_acl_set_interface)                     \
235 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del)                                 \
236 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd)             \
237 _(IPSEC_SPD_ADD_DEL_ENTRY, ipsec_spd_add_del_entry)                     \
238 _(IPSEC_SAD_ADD_DEL_ENTRY, ipsec_sad_add_del_entry)                     \
239 _(IPSEC_SA_SET_KEY, ipsec_sa_set_key)                                   \
240 _(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del)                         \
241 _(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth)                       \
242 _(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id)                           \
243 _(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts)                           \
244 _(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key)                             \
245 _(DELETE_LOOPBACK, delete_loopback)                                     \
246 _(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del)                                 \
247 _(MAP_ADD_DOMAIN, map_add_domain)                                       \
248 _(MAP_DEL_DOMAIN, map_del_domain)                                       \
249 _(MAP_ADD_DEL_RULE, map_add_del_rule)                                   \
250 _(MAP_DOMAIN_DUMP, map_domain_dump)                                     \
251 _(MAP_RULE_DUMP, map_rule_dump)                                         \
252 _(MAP_SUMMARY_STATS, map_summary_stats)                                 \
253 _(COP_INTERFACE_ENABLE_DISABLE, cop_interface_enable_disable)           \
254 _(COP_WHITELIST_ENABLE_DISABLE, cop_whitelist_enable_disable)           \
255 _(GET_NODE_GRAPH, get_node_graph)                                       \
256 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats)                   \
257 _(IOAM_ENABLE, ioam_enable)                                             \
258 _(IOAM_DISABLE, ioam_disable)                                           \
259 _(LISP_ADD_DEL_LOCATOR_SET, lisp_add_del_locator_set)                   \
260 _(LISP_ADD_DEL_LOCATOR, lisp_add_del_locator)                           \
261 _(LISP_ADD_DEL_LOCAL_EID, lisp_add_del_local_eid)                       \
262 _(LISP_GPE_ADD_DEL_FWD_ENTRY, lisp_gpe_add_del_fwd_entry)               \
263 _(LISP_ADD_DEL_MAP_RESOLVER, lisp_add_del_map_resolver)                 \
264 _(LISP_GPE_ENABLE_DISABLE, lisp_gpe_enable_disable)                     \
265 _(LISP_ENABLE_DISABLE, lisp_enable_disable)                             \
266 _(LISP_GPE_ADD_DEL_IFACE, lisp_gpe_add_del_iface)                       \
267 _(LISP_ADD_DEL_REMOTE_MAPPING, lisp_add_del_remote_mapping)             \
268 _(LISP_ADD_DEL_ADJACENCY, lisp_add_del_adjacency)                       \
269 _(LISP_PITR_SET_LOCATOR_SET, lisp_pitr_set_locator_set)                 \
270 _(LISP_MAP_REQUEST_MODE, lisp_map_request_mode)                         \
271 _(LISP_EID_TABLE_ADD_DEL_MAP, lisp_eid_table_add_del_map)               \
272 _(LISP_LOCATOR_SET_DUMP, lisp_locator_set_dump)                         \
273 _(LISP_LOCATOR_DUMP, lisp_locator_dump)                                 \
274 _(LISP_EID_TABLE_DUMP, lisp_eid_table_dump)                             \
275 _(LISP_GPE_TUNNEL_DUMP, lisp_gpe_tunnel_dump)                           \
276 _(LISP_MAP_RESOLVER_DUMP, lisp_map_resolver_dump)                       \
277 _(LISP_EID_TABLE_MAP_DUMP, lisp_eid_table_map_dump)                     \
278 _(LISP_EID_TABLE_VNI_DUMP, lisp_eid_table_vni_dump)                     \
279 _(LISP_ADJACENCIES_GET, lisp_adjacencies_get)                           \
280 _(SHOW_LISP_STATUS, show_lisp_status)                                   \
281 _(LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS,                                   \
282   lisp_add_del_map_request_itr_rlocs)                                   \
283 _(LISP_GET_MAP_REQUEST_ITR_RLOCS, lisp_get_map_request_itr_rlocs)       \
284 _(SHOW_LISP_PITR, show_lisp_pitr)                                       \
285 _(SHOW_LISP_MAP_REQUEST_MODE, show_lisp_map_request_mode)               \
286 _(SR_MULTICAST_MAP_ADD_DEL, sr_multicast_map_add_del)                   \
287 _(AF_PACKET_CREATE, af_packet_create)                                   \
288 _(AF_PACKET_DELETE, af_packet_delete)                                   \
289 _(POLICER_ADD_DEL, policer_add_del)                                     \
290 _(POLICER_DUMP, policer_dump)                                           \
291 _(POLICER_CLASSIFY_SET_INTERFACE, policer_classify_set_interface)       \
292 _(POLICER_CLASSIFY_DUMP, policer_classify_dump)                         \
293 _(NETMAP_CREATE, netmap_create)                                         \
294 _(NETMAP_DELETE, netmap_delete)                                         \
295 _(MPLS_TUNNEL_DUMP, mpls_tunnel_dump)                                   \
296 _(MPLS_TUNNEL_DETAILS, mpls_tunnel_details)                             \
297 _(MPLS_FIB_DUMP, mpls_fib_dump)                                         \
298 _(MPLS_FIB_DETAILS, mpls_fib_details)                                   \
299 _(CLASSIFY_TABLE_IDS,classify_table_ids)                                \
300 _(CLASSIFY_TABLE_BY_INTERFACE, classify_table_by_interface)             \
301 _(CLASSIFY_TABLE_INFO,classify_table_info)                              \
302 _(CLASSIFY_SESSION_DUMP,classify_session_dump)                          \
303 _(CLASSIFY_SESSION_DETAILS,classify_session_details)                    \
304 _(SET_IPFIX_EXPORTER, set_ipfix_exporter)                               \
305 _(IPFIX_EXPORTER_DUMP, ipfix_exporter_dump)                             \
306 _(SET_IPFIX_CLASSIFY_STREAM, set_ipfix_classify_stream)                 \
307 _(IPFIX_CLASSIFY_STREAM_DUMP, ipfix_classify_stream_dump)               \
308 _(IPFIX_CLASSIFY_TABLE_ADD_DEL, ipfix_classify_table_add_del)           \
309 _(IPFIX_CLASSIFY_TABLE_DUMP, ipfix_classify_table_dump)                 \
310 _(SW_INTERFACE_SPAN_ENABLE_DISABLE, sw_interface_span_enable_disable)   \
311 _(SW_INTERFACE_SPAN_DUMP, sw_interface_span_dump)                       \
312 _(GET_NEXT_INDEX, get_next_index)                                       \
313 _(PG_CREATE_INTERFACE, pg_create_interface)                             \
314 _(PG_CAPTURE, pg_capture)                                               \
315 _(PG_ENABLE_DISABLE, pg_enable_disable)                                 \
316 _(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL,                               \
317   ip_source_and_port_range_check_add_del)                               \
318 _(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL,                     \
319   ip_source_and_port_range_check_interface_add_del)                     \
320 _(IPSEC_GRE_ADD_DEL_TUNNEL, ipsec_gre_add_del_tunnel)                   \
321 _(IPSEC_GRE_TUNNEL_DUMP, ipsec_gre_tunnel_dump)                         \
322 _(DELETE_SUBIF, delete_subif)                                           \
323 _(L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite)           \
324 _(PUNT, punt)                                                           \
325 _(FLOW_CLASSIFY_SET_INTERFACE, flow_classify_set_interface)             \
326 _(FLOW_CLASSIFY_DUMP, flow_classify_dump)                               \
327 _(IPSEC_SPD_DUMP, ipsec_spd_dump)                                       \
328 _(IP_FIB_DUMP, ip_fib_dump)                                             \
329 _(IP_FIB_DETAILS, ip_fib_details)                                       \
330 _(IP6_FIB_DUMP, ip6_fib_dump)                                           \
331 _(IP6_FIB_DETAILS, ip6_fib_details)                                     \
332 _(FEATURE_ENABLE_DISABLE, feature_enable_disable)                       \
333 _(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del)
334
335 #define QUOTE_(x) #x
336 #define QUOTE(x) QUOTE_(x)
337
338 #define foreach_registration_hash               \
339 _(interface_events)                             \
340 _(to_netconf_server)                            \
341 _(from_netconf_server)                          \
342 _(to_netconf_client)                            \
343 _(from_netconf_client)                          \
344 _(oam_events)
345
346 typedef enum
347 {
348   RESOLVE_IP4_ADD_DEL_ROUTE = 1,
349   RESOLVE_IP6_ADD_DEL_ROUTE,
350 } resolve_t;
351
352 typedef struct
353 {
354
355 #define _(a) uword *a##_registration_hash;              \
356     vpe_client_registration_t * a##_registrations;
357   foreach_registration_hash
358 #undef _
359     /* notifications happen really early in the game */
360   u8 link_state_process_up;
361
362   /* ip4 arp event registration pool */
363   vl_api_ip4_arp_event_t *arp_events;
364
365   /* ip6 nd event registration pool */
366   vl_api_ip6_nd_event_t *nd_events;
367
368   /* convenience */
369   vlib_main_t *vlib_main;
370   vnet_main_t *vnet_main;
371 } vpe_api_main_t;
372
373 static vlib_node_registration_t vpe_resolver_process_node;
374 static vpe_api_main_t vpe_api_main;
375
376 static void send_sw_interface_flags (vpe_api_main_t * am,
377                                      unix_shared_memory_queue_t * q,
378                                      vnet_sw_interface_t * swif);
379 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
380                                              unix_shared_memory_queue_t * q,
381                                              u32 sw_if_index);
382
383 static int arp_change_delete_callback (u32 pool_index, u8 * notused);
384 static int nd_change_delete_callback (u32 pool_index, u8 * notused);
385
386
387 /* Clean up all registrations belonging to the indicated client */
388 int
389 vl_api_memclnt_delete_callback (u32 client_index)
390 {
391   vpe_api_main_t *vam = &vpe_api_main;
392   vpe_client_registration_t *rp;
393   uword *p;
394   int stats_memclnt_delete_callback (u32 client_index);
395
396   stats_memclnt_delete_callback (client_index);
397
398 #define _(a)                                                    \
399     p = hash_get (vam->a##_registration_hash, client_index);    \
400     if (p) {                                                    \
401         rp = pool_elt_at_index (vam->a##_registrations, p[0]);  \
402         pool_put (vam->a##_registrations, rp);                  \
403         hash_unset (vam->a##_registration_hash, client_index);  \
404     }
405   foreach_registration_hash;
406 #undef _
407   return 0;
408 }
409
410 #define API_LINK_STATE_EVENT 1
411 #define API_ADMIN_UP_DOWN_EVENT 2
412
413 static int
414 event_data_cmp (void *a1, void *a2)
415 {
416   uword *e1 = a1;
417   uword *e2 = a2;
418
419   return (word) e1[0] - (word) e2[0];
420 }
421
422 static uword
423 link_state_process (vlib_main_t * vm,
424                     vlib_node_runtime_t * rt, vlib_frame_t * f)
425 {
426   vpe_api_main_t *vam = &vpe_api_main;
427   vnet_main_t *vnm = vam->vnet_main;
428   vnet_sw_interface_t *swif;
429   uword *event_data = 0;
430   vpe_client_registration_t *reg;
431   int i;
432   u32 prev_sw_if_index;
433   unix_shared_memory_queue_t *q;
434
435   vam->link_state_process_up = 1;
436
437   while (1)
438     {
439       vlib_process_wait_for_event (vm);
440
441       /* Unified list of changed link or admin state sw_if_indices */
442       vlib_process_get_events_with_type
443         (vm, &event_data, API_LINK_STATE_EVENT);
444       vlib_process_get_events_with_type
445         (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
446
447       /* Sort, so we can eliminate duplicates */
448       vec_sort_with_function (event_data, event_data_cmp);
449
450       prev_sw_if_index = ~0;
451
452       for (i = 0; i < vec_len (event_data); i++)
453         {
454           /* Only one message per swif */
455           if (prev_sw_if_index == event_data[i])
456             continue;
457           prev_sw_if_index = event_data[i];
458
459           /* *INDENT-OFF* */
460           pool_foreach(reg, vam->interface_events_registrations,
461           ({
462             q = vl_api_client_index_to_input_queue (reg->client_index);
463             if (q)
464               {
465                 /* sw_interface may be deleted already */
466                 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
467                                          event_data[i]))
468                   {
469                     swif = vnet_get_sw_interface (vnm, event_data[i]);
470                     send_sw_interface_flags (vam, q, swif);
471                   }
472               }
473           }));
474           /* *INDENT-ON* */
475         }
476       vec_reset_length (event_data);
477     }
478
479   return 0;
480 }
481
482 static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
483                                             u32 flags);
484 static clib_error_t *admin_up_down_function (vnet_main_t * vm,
485                                              u32 hw_if_index, u32 flags);
486
487 /* *INDENT-OFF* */
488 VLIB_REGISTER_NODE (link_state_process_node,static) = {
489   .function = link_state_process,
490   .type = VLIB_NODE_TYPE_PROCESS,
491   .name = "vpe-link-state-process",
492 };
493 /* *INDENT-ON* */
494
495 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
496 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
497
498 static clib_error_t *
499 link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
500 {
501   vpe_api_main_t *vam = &vpe_api_main;
502   vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
503
504   if (vam->link_state_process_up)
505     vlib_process_signal_event (vam->vlib_main,
506                                link_state_process_node.index,
507                                API_LINK_STATE_EVENT, hi->sw_if_index);
508   return 0;
509 }
510
511 static clib_error_t *
512 admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
513 {
514   vpe_api_main_t *vam = &vpe_api_main;
515
516   /*
517    * Note: it's perfectly fair to set a subif admin up / admin down.
518    * Note the subtle distinction between this routine and the previous
519    * routine.
520    */
521   if (vam->link_state_process_up)
522     vlib_process_signal_event (vam->vlib_main,
523                                link_state_process_node.index,
524                                API_ADMIN_UP_DOWN_EVENT, sw_if_index);
525   return 0;
526 }
527
528 #define pub_sub_handler(lca,UCA)                                        \
529 static void vl_api_want_##lca##_t_handler (                             \
530     vl_api_want_##lca##_t *mp)                                          \
531 {                                                                       \
532     vpe_api_main_t *vam = &vpe_api_main;                                \
533     vpe_client_registration_t *rp;                                      \
534     vl_api_want_##lca##_reply_t *rmp;                                   \
535     uword *p;                                                           \
536     i32 rv = 0;                                                         \
537                                                                         \
538     p = hash_get (vam->lca##_registration_hash, mp->client_index);      \
539     if (p) {                                                            \
540         if (mp->enable_disable) {                                       \
541             clib_warning ("pid %d: already enabled...", mp->pid);       \
542             rv = VNET_API_ERROR_INVALID_REGISTRATION;                   \
543             goto reply;                                                 \
544         } else {                                                        \
545             rp = pool_elt_at_index (vam->lca##_registrations, p[0]);    \
546             pool_put (vam->lca##_registrations, rp);                    \
547             hash_unset (vam->lca##_registration_hash,                   \
548                 mp->client_index);                                      \
549             goto reply;                                                 \
550         }                                                               \
551     }                                                                   \
552     if (mp->enable_disable == 0) {                                      \
553         clib_warning ("pid %d: already disabled...", mp->pid);          \
554         rv = VNET_API_ERROR_INVALID_REGISTRATION;                       \
555         goto reply;                                                     \
556     }                                                                   \
557     pool_get (vam->lca##_registrations, rp);                            \
558     rp->client_index = mp->client_index;                                \
559     rp->client_pid = mp->pid;                                           \
560     hash_set (vam->lca##_registration_hash, rp->client_index,           \
561               rp - vam->lca##_registrations);                           \
562                                                                         \
563 reply:                                                                  \
564     REPLY_MACRO (VL_API_WANT_##UCA##_REPLY);                            \
565 }
566
567 /* *INDENT-OFF* */
568 pub_sub_handler (interface_events, INTERFACE_EVENTS)
569 pub_sub_handler (oam_events, OAM_EVENTS)
570 /* *INDENT-ON* */
571
572 #define RESOLUTION_EVENT 1
573 #define RESOLUTION_PENDING_EVENT 2
574 #define IP4_ARP_EVENT 3
575 #define IP6_ND_EVENT 4
576
577 static int ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp);
578
579 static int ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp);
580
581 void
582 handle_ip4_arp_event (u32 pool_index)
583 {
584   vpe_api_main_t *vam = &vpe_api_main;
585   vnet_main_t *vnm = vam->vnet_main;
586   vlib_main_t *vm = vam->vlib_main;
587   vl_api_ip4_arp_event_t *event;
588   vl_api_ip4_arp_event_t *mp;
589   unix_shared_memory_queue_t *q;
590
591   /* Client can cancel, die, etc. */
592   if (pool_is_free_index (vam->arp_events, pool_index))
593     return;
594
595   event = pool_elt_at_index (vam->arp_events, pool_index);
596
597   q = vl_api_client_index_to_input_queue (event->client_index);
598   if (!q)
599     {
600       (void) vnet_add_del_ip4_arp_change_event
601         (vnm, arp_change_delete_callback,
602          event->pid, &event->address,
603          vpe_resolver_process_node.index, IP4_ARP_EVENT,
604          ~0 /* pool index, notused */ , 0 /* is_add */ );
605       return;
606     }
607
608   if (q->cursize < q->maxsize)
609     {
610       mp = vl_msg_api_alloc (sizeof (*mp));
611       clib_memcpy (mp, event, sizeof (*mp));
612       vl_msg_api_send_shmem (q, (u8 *) & mp);
613     }
614   else
615     {
616       static f64 last_time;
617       /*
618        * Throttle syslog msgs.
619        * It's pretty tempting to just revoke the registration...
620        */
621       if (vlib_time_now (vm) > last_time + 10.0)
622         {
623           clib_warning ("arp event for %U to pid %d: queue stuffed!",
624                         format_ip4_address, &event->address, event->pid);
625           last_time = vlib_time_now (vm);
626         }
627     }
628 }
629
630 void
631 handle_ip6_nd_event (u32 pool_index)
632 {
633   vpe_api_main_t *vam = &vpe_api_main;
634   vnet_main_t *vnm = vam->vnet_main;
635   vlib_main_t *vm = vam->vlib_main;
636   vl_api_ip6_nd_event_t *event;
637   vl_api_ip6_nd_event_t *mp;
638   unix_shared_memory_queue_t *q;
639
640   /* Client can cancel, die, etc. */
641   if (pool_is_free_index (vam->nd_events, pool_index))
642     return;
643
644   event = pool_elt_at_index (vam->nd_events, pool_index);
645
646   q = vl_api_client_index_to_input_queue (event->client_index);
647   if (!q)
648     {
649       (void) vnet_add_del_ip6_nd_change_event
650         (vnm, nd_change_delete_callback,
651          event->pid, &event->address,
652          vpe_resolver_process_node.index, IP6_ND_EVENT,
653          ~0 /* pool index, notused */ , 0 /* is_add */ );
654       return;
655     }
656
657   if (q->cursize < q->maxsize)
658     {
659       mp = vl_msg_api_alloc (sizeof (*mp));
660       clib_memcpy (mp, event, sizeof (*mp));
661       vl_msg_api_send_shmem (q, (u8 *) & mp);
662     }
663   else
664     {
665       static f64 last_time;
666       /*
667        * Throttle syslog msgs.
668        * It's pretty tempting to just revoke the registration...
669        */
670       if (vlib_time_now (vm) > last_time + 10.0)
671         {
672           clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
673                         format_ip6_address, &event->address, event->pid);
674           last_time = vlib_time_now (vm);
675         }
676     }
677 }
678
679 static uword
680 resolver_process (vlib_main_t * vm,
681                   vlib_node_runtime_t * rt, vlib_frame_t * f)
682 {
683   uword event_type;
684   uword *event_data = 0;
685   f64 timeout = 100.0;
686   int i;
687
688   while (1)
689     {
690       vlib_process_wait_for_event_or_clock (vm, timeout);
691
692       event_type = vlib_process_get_events (vm, &event_data);
693
694       switch (event_type)
695         {
696         case RESOLUTION_PENDING_EVENT:
697           timeout = 1.0;
698           break;
699
700         case RESOLUTION_EVENT:
701           clib_warning ("resolver: BOGUS TYPE");
702           break;
703
704         case IP4_ARP_EVENT:
705           for (i = 0; i < vec_len (event_data); i++)
706             handle_ip4_arp_event (event_data[i]);
707           break;
708
709         case IP6_ND_EVENT:
710           for (i = 0; i < vec_len (event_data); i++)
711             handle_ip6_nd_event (event_data[i]);
712           break;
713
714         case ~0:                /* timeout */
715           break;
716         }
717
718       vec_reset_length (event_data);
719     }
720   return 0;                     /* or not */
721 }
722
723 /* *INDENT-OFF* */
724 VLIB_REGISTER_NODE (vpe_resolver_process_node,static) = {
725   .function = resolver_process,
726   .type = VLIB_NODE_TYPE_PROCESS,
727   .name = "vpe-route-resolver-process",
728 };
729 /* *INDENT-ON* */
730
731 static int
732 add_del_route_t_handler (u8 is_multipath,
733                          u8 is_add,
734                          u8 is_drop,
735                          u8 is_unreach,
736                          u8 is_prohibit,
737                          u8 is_local,
738                          u8 is_classify,
739                          u32 classify_table_index,
740                          u8 is_resolve_host,
741                          u8 is_resolve_attached,
742                          u32 fib_index,
743                          const fib_prefix_t * prefix,
744                          u8 next_hop_proto_is_ip4,
745                          const ip46_address_t * next_hop,
746                          u32 next_hop_sw_if_index,
747                          u8 next_hop_fib_index,
748                          u32 next_hop_weight,
749                          mpls_label_t next_hop_via_label,
750                          mpls_label_t * next_hop_out_label_stack)
751 {
752   vnet_classify_main_t *cm = &vnet_classify_main;
753   stats_main_t *sm = &stats_main;
754   fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
755   fib_route_path_t path = {
756     .frp_proto = (next_hop_proto_is_ip4 ?
757                   FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6),
758     .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
759     .frp_sw_if_index = next_hop_sw_if_index,
760     .frp_fib_index = next_hop_fib_index,
761     .frp_weight = next_hop_weight,
762     .frp_label_stack = next_hop_out_label_stack,
763   };
764   fib_route_path_t *paths = NULL;
765
766   if (MPLS_LABEL_INVALID != next_hop_via_label)
767     {
768       path.frp_proto = FIB_PROTOCOL_MPLS;
769       path.frp_local_label = next_hop_via_label;
770     }
771   if (is_resolve_host)
772     path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
773   if (is_resolve_attached)
774     path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
775
776   path.frp_flags = path_flags;
777
778   if (is_multipath)
779     {
780       dslock (sm, 1 /* release hint */ , 10 /* tag */ );
781
782
783       vec_add1 (paths, path);
784
785       if (is_add)
786         fib_table_entry_path_add2 (fib_index,
787                                    prefix,
788                                    FIB_SOURCE_API,
789                                    FIB_ENTRY_FLAG_NONE, paths);
790       else
791         fib_table_entry_path_remove2 (fib_index,
792                                       prefix, FIB_SOURCE_API, paths);
793
794       vec_free (paths);
795       dsunlock (sm);
796       return 0;
797     }
798
799   dslock (sm, 1 /* release hint */ , 2 /* tag */ );
800
801   if (is_drop || is_local || is_classify || is_unreach || is_prohibit)
802     {
803       /*
804        * special route types that link directly to the adj
805        */
806       if (is_add)
807         {
808           dpo_id_t dpo = DPO_INVALID;
809           dpo_proto_t dproto;
810
811           dproto = fib_proto_to_dpo (prefix->fp_proto);
812
813           if (is_drop)
814             ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
815           else if (is_local)
816             receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
817           else if (is_unreach)
818             ip_null_dpo_add_and_lock (dproto,
819                                       IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
820           else if (is_prohibit)
821             ip_null_dpo_add_and_lock (dproto,
822                                       IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
823                                       &dpo);
824           else if (is_classify)
825             {
826               if (pool_is_free_index (cm->tables,
827                                       ntohl (classify_table_index)))
828                 {
829                   dsunlock (sm);
830                   return VNET_API_ERROR_NO_SUCH_TABLE;
831                 }
832
833               dpo_set (&dpo, DPO_CLASSIFY, dproto,
834                        classify_dpo_create (dproto,
835                                             ntohl (classify_table_index)));
836             }
837           else
838             {
839               dsunlock (sm);
840               return VNET_API_ERROR_NO_SUCH_TABLE;
841             }
842
843           fib_table_entry_special_dpo_update (fib_index,
844                                               prefix,
845                                               FIB_SOURCE_API,
846                                               FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
847           dpo_reset (&dpo);
848         }
849       else
850         {
851           fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
852         }
853     }
854   else
855     {
856       if (is_add)
857         {
858           vec_add1 (paths, path);
859           fib_table_entry_update (fib_index,
860                                   prefix,
861                                   FIB_SOURCE_API, FIB_ENTRY_FLAG_NONE, paths);
862           vec_free (paths);
863         }
864       else
865         {
866           fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
867         }
868     }
869
870   dsunlock (sm);
871   return (0);
872 }
873
874 static int
875 add_del_route_check (fib_protocol_t table_proto,
876                      u32 table_id,
877                      u32 next_hop_sw_if_index,
878                      fib_protocol_t next_hop_table_proto,
879                      u32 next_hop_table_id,
880                      u8 create_missing_tables,
881                      u32 * fib_index, u32 * next_hop_fib_index)
882 {
883   vnet_main_t *vnm = vnet_get_main ();
884
885   *fib_index = fib_table_find (table_proto, ntohl (table_id));
886   if (~0 == *fib_index)
887     {
888       if (create_missing_tables)
889         {
890           *fib_index = fib_table_find_or_create_and_lock (table_proto,
891                                                           ntohl (table_id));
892         }
893       else
894         {
895           /* No such VRF, and we weren't asked to create one */
896           return VNET_API_ERROR_NO_SUCH_FIB;
897         }
898     }
899
900   if (~0 != ntohl (next_hop_sw_if_index))
901     {
902       if (pool_is_free_index (vnm->interface_main.sw_interfaces,
903                               ntohl (next_hop_sw_if_index)))
904         {
905           return VNET_API_ERROR_NO_MATCHING_INTERFACE;
906         }
907     }
908   else
909     {
910       *next_hop_fib_index = fib_table_find (next_hop_table_proto,
911                                             ntohl (next_hop_table_id));
912
913       if (~0 == *next_hop_fib_index)
914         {
915           if (create_missing_tables)
916             {
917               *next_hop_fib_index =
918                 fib_table_find_or_create_and_lock (next_hop_table_proto,
919                                                    ntohl (next_hop_table_id));
920             }
921           else
922             {
923               /* No such VRF, and we weren't asked to create one */
924               return VNET_API_ERROR_NO_SUCH_FIB;
925             }
926         }
927     }
928
929   return (0);
930 }
931
932 static int
933 ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
934 {
935   u32 fib_index, next_hop_fib_index;
936   mpls_label_t *label_stack = NULL;
937   int rv, ii, n_labels;;
938
939   rv = add_del_route_check (FIB_PROTOCOL_IP4,
940                             mp->table_id,
941                             mp->next_hop_sw_if_index,
942                             FIB_PROTOCOL_IP4,
943                             mp->next_hop_table_id,
944                             mp->create_vrf_if_needed,
945                             &fib_index, &next_hop_fib_index);
946
947   if (0 != rv)
948     return (rv);
949
950   fib_prefix_t pfx = {
951     .fp_len = mp->dst_address_length,
952     .fp_proto = FIB_PROTOCOL_IP4,
953   };
954   clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
955
956   ip46_address_t nh;
957   memset (&nh, 0, sizeof (nh));
958   memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
959
960   n_labels = mp->next_hop_n_out_labels;
961   if (n_labels == 0)
962     ;
963   else if (1 == n_labels)
964     vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
965   else
966     {
967       vec_validate (label_stack, n_labels - 1);
968       for (ii = 0; ii < n_labels; ii++)
969         label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
970     }
971
972   return (add_del_route_t_handler (mp->is_multipath,
973                                    mp->is_add,
974                                    mp->is_drop,
975                                    mp->is_unreach,
976                                    mp->is_prohibit,
977                                    mp->is_local,
978                                    mp->is_classify,
979                                    mp->classify_table_index,
980                                    mp->is_resolve_host,
981                                    mp->is_resolve_attached,
982                                    fib_index, &pfx, 1,
983                                    &nh,
984                                    ntohl (mp->next_hop_sw_if_index),
985                                    next_hop_fib_index,
986                                    mp->next_hop_weight,
987                                    ntohl (mp->next_hop_via_label),
988                                    label_stack));
989 }
990
991 static int
992 ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
993 {
994   u32 fib_index, next_hop_fib_index;
995   mpls_label_t *label_stack = NULL;
996   int rv, ii, n_labels;;
997
998   rv = add_del_route_check (FIB_PROTOCOL_IP6,
999                             mp->table_id,
1000                             mp->next_hop_sw_if_index,
1001                             FIB_PROTOCOL_IP6,
1002                             mp->next_hop_table_id,
1003                             mp->create_vrf_if_needed,
1004                             &fib_index, &next_hop_fib_index);
1005
1006   if (0 != rv)
1007     return (rv);
1008
1009   fib_prefix_t pfx = {
1010     .fp_len = mp->dst_address_length,
1011     .fp_proto = FIB_PROTOCOL_IP6,
1012   };
1013   clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1014
1015   ip46_address_t nh;
1016   memset (&nh, 0, sizeof (nh));
1017   memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1018
1019   n_labels = mp->next_hop_n_out_labels;
1020   if (n_labels == 0)
1021     ;
1022   else if (1 == n_labels)
1023     vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
1024   else
1025     {
1026       vec_validate (label_stack, n_labels - 1);
1027       for (ii = 0; ii < n_labels; ii++)
1028         label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
1029     }
1030
1031   return (add_del_route_t_handler (mp->is_multipath,
1032                                    mp->is_add,
1033                                    mp->is_drop,
1034                                    mp->is_unreach,
1035                                    mp->is_prohibit,
1036                                    mp->is_local,
1037                                    mp->is_classify,
1038                                    mp->classify_table_index,
1039                                    mp->is_resolve_host,
1040                                    mp->is_resolve_attached,
1041                                    fib_index, &pfx, 0,
1042                                    &nh, ntohl (mp->next_hop_sw_if_index),
1043                                    next_hop_fib_index,
1044                                    mp->next_hop_weight,
1045                                    ntohl (mp->next_hop_via_label),
1046                                    label_stack));
1047 }
1048
1049 static int
1050 mpls_route_add_del_t_handler (vnet_main_t * vnm,
1051                               vl_api_mpls_route_add_del_t * mp)
1052 {
1053   u32 fib_index, next_hop_fib_index;
1054   mpls_label_t *label_stack = NULL;
1055   int rv, ii, n_labels;;
1056
1057   fib_prefix_t pfx = {
1058     .fp_len = 21,
1059     .fp_proto = FIB_PROTOCOL_MPLS,
1060     .fp_eos = mp->mr_eos,
1061     .fp_label = ntohl (mp->mr_label),
1062   };
1063   if (pfx.fp_eos)
1064     {
1065       if (mp->mr_next_hop_proto_is_ip4)
1066         {
1067           pfx.fp_payload_proto = DPO_PROTO_IP4;
1068         }
1069       else
1070         {
1071           pfx.fp_payload_proto = DPO_PROTO_IP6;
1072         }
1073     }
1074   else
1075     {
1076       pfx.fp_payload_proto = DPO_PROTO_MPLS;
1077     }
1078
1079   rv = add_del_route_check (FIB_PROTOCOL_MPLS,
1080                             mp->mr_table_id,
1081                             mp->mr_next_hop_sw_if_index,
1082                             dpo_proto_to_fib (pfx.fp_payload_proto),
1083                             mp->mr_next_hop_table_id,
1084                             mp->mr_create_table_if_needed,
1085                             &fib_index, &next_hop_fib_index);
1086
1087   if (0 != rv)
1088     return (rv);
1089
1090   ip46_address_t nh;
1091   memset (&nh, 0, sizeof (nh));
1092
1093   if (mp->mr_next_hop_proto_is_ip4)
1094     memcpy (&nh.ip4, mp->mr_next_hop, sizeof (nh.ip4));
1095   else
1096     memcpy (&nh.ip6, mp->mr_next_hop, sizeof (nh.ip6));
1097
1098   n_labels = mp->mr_next_hop_n_out_labels;
1099   if (n_labels == 0)
1100     ;
1101   else if (1 == n_labels)
1102     vec_add1 (label_stack, ntohl (mp->mr_next_hop_out_label_stack[0]));
1103   else
1104     {
1105       vec_validate (label_stack, n_labels - 1);
1106       for (ii = 0; ii < n_labels; ii++)
1107         label_stack[ii] = ntohl (mp->mr_next_hop_out_label_stack[ii]);
1108     }
1109
1110   return (add_del_route_t_handler (mp->mr_is_multipath, mp->mr_is_add, 0,       // mp->is_drop,
1111                                    0,   // mp->is_unreach,
1112                                    0,   // mp->is_prohibit,
1113                                    0,   // mp->is_local,
1114                                    mp->mr_is_classify,
1115                                    mp->mr_classify_table_index,
1116                                    mp->mr_is_resolve_host,
1117                                    mp->mr_is_resolve_attached,
1118                                    fib_index, &pfx,
1119                                    mp->mr_next_hop_proto_is_ip4,
1120                                    &nh, ntohl (mp->mr_next_hop_sw_if_index),
1121                                    next_hop_fib_index,
1122                                    mp->mr_next_hop_weight,
1123                                    ntohl (mp->mr_next_hop_via_label),
1124                                    label_stack));
1125 }
1126
1127 void
1128 vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1129 {
1130   vl_api_ip_add_del_route_reply_t *rmp;
1131   int rv;
1132   vnet_main_t *vnm = vnet_get_main ();
1133
1134   vnm->api_errno = 0;
1135
1136   if (mp->is_ipv6)
1137     rv = ip6_add_del_route_t_handler (mp);
1138   else
1139     rv = ip4_add_del_route_t_handler (mp);
1140
1141   rv = (rv == 0) ? vnm->api_errno : rv;
1142
1143   REPLY_MACRO (VL_API_IP_ADD_DEL_ROUTE_REPLY);
1144 }
1145
1146 void
1147 vl_api_mpls_route_add_del_t_handler (vl_api_mpls_route_add_del_t * mp)
1148 {
1149   vl_api_mpls_route_add_del_reply_t *rmp;
1150   vnet_main_t *vnm;
1151   int rv;
1152
1153   vnm = vnet_get_main ();
1154   vnm->api_errno = 0;
1155
1156   rv = mpls_route_add_del_t_handler (vnm, mp);
1157
1158   rv = (rv == 0) ? vnm->api_errno : rv;
1159
1160   REPLY_MACRO (VL_API_MPLS_ROUTE_ADD_DEL_REPLY);
1161 }
1162
1163 static int
1164 mpls_ip_bind_unbind_handler (vnet_main_t * vnm,
1165                              vl_api_mpls_ip_bind_unbind_t * mp)
1166 {
1167   u32 mpls_fib_index, ip_fib_index;
1168
1169   mpls_fib_index =
1170     fib_table_find (FIB_PROTOCOL_MPLS, ntohl (mp->mb_mpls_table_id));
1171
1172   if (~0 == mpls_fib_index)
1173     {
1174       if (mp->mb_create_table_if_needed)
1175         {
1176           mpls_fib_index =
1177             fib_table_find_or_create_and_lock (FIB_PROTOCOL_MPLS,
1178                                                ntohl (mp->mb_mpls_table_id));
1179         }
1180       else
1181         return VNET_API_ERROR_NO_SUCH_FIB;
1182     }
1183
1184   ip_fib_index = fib_table_find ((mp->mb_is_ip4 ?
1185                                   FIB_PROTOCOL_IP4 :
1186                                   FIB_PROTOCOL_IP6),
1187                                  ntohl (mp->mb_ip_table_id));
1188   if (~0 == ip_fib_index)
1189     return VNET_API_ERROR_NO_SUCH_FIB;
1190
1191   fib_prefix_t pfx = {
1192     .fp_len = mp->mb_address_length,
1193   };
1194
1195   if (mp->mb_is_ip4)
1196     {
1197       pfx.fp_proto = FIB_PROTOCOL_IP4;
1198       clib_memcpy (&pfx.fp_addr.ip4, mp->mb_address,
1199                    sizeof (pfx.fp_addr.ip4));
1200     }
1201   else
1202     {
1203       pfx.fp_proto = FIB_PROTOCOL_IP6;
1204       clib_memcpy (&pfx.fp_addr.ip6, mp->mb_address,
1205                    sizeof (pfx.fp_addr.ip6));
1206     }
1207
1208   if (mp->mb_is_bind)
1209     fib_table_entry_local_label_add (ip_fib_index, &pfx,
1210                                      ntohl (mp->mb_label));
1211   else
1212     fib_table_entry_local_label_remove (ip_fib_index, &pfx,
1213                                         ntohl (mp->mb_label));
1214
1215   return (0);
1216 }
1217
1218 void
1219 vl_api_mpls_ip_bind_unbind_t_handler (vl_api_mpls_ip_bind_unbind_t * mp)
1220 {
1221   vl_api_mpls_route_add_del_reply_t *rmp;
1222   vnet_main_t *vnm;
1223   int rv;
1224
1225   vnm = vnet_get_main ();
1226   vnm->api_errno = 0;
1227
1228   rv = mpls_ip_bind_unbind_handler (vnm, mp);
1229
1230   rv = (rv == 0) ? vnm->api_errno : rv;
1231
1232   REPLY_MACRO (VL_API_MPLS_ROUTE_ADD_DEL_REPLY);
1233 }
1234
1235 static void
1236   vl_api_sw_interface_add_del_address_t_handler
1237   (vl_api_sw_interface_add_del_address_t * mp)
1238 {
1239   vlib_main_t *vm = vlib_get_main ();
1240   vl_api_sw_interface_add_del_address_reply_t *rmp;
1241   int rv = 0;
1242   u32 is_del;
1243
1244   VALIDATE_SW_IF_INDEX (mp);
1245
1246   is_del = mp->is_add == 0;
1247
1248   if (mp->del_all)
1249     ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
1250   else if (mp->is_ipv6)
1251     ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
1252                                    (void *) mp->address,
1253                                    mp->address_length, is_del);
1254   else
1255     ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
1256                                    (void *) mp->address,
1257                                    mp->address_length, is_del);
1258
1259   BAD_SW_IF_INDEX_LABEL;
1260
1261   REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
1262 }
1263
1264 static void
1265 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
1266 {
1267   int rv = 0;
1268   u32 table_id = ntohl (mp->vrf_id);
1269   u32 sw_if_index = ntohl (mp->sw_if_index);
1270   vl_api_sw_interface_set_table_reply_t *rmp;
1271   stats_main_t *sm = &stats_main;
1272   u32 fib_index;
1273
1274   VALIDATE_SW_IF_INDEX (mp);
1275
1276   dslock (sm, 1 /* release hint */ , 4 /* tag */ );
1277
1278   if (mp->is_ipv6)
1279     {
1280       fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
1281                                                      table_id);
1282
1283       vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
1284       ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
1285     }
1286   else
1287     {
1288
1289       fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1290                                                      table_id);
1291
1292       vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
1293       ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
1294     }
1295   dsunlock (sm);
1296
1297   BAD_SW_IF_INDEX_LABEL;
1298
1299   REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
1300 }
1301
1302 static void
1303 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
1304 {
1305   vl_api_sw_interface_set_vpath_reply_t *rmp;
1306   int rv = 0;
1307   u32 sw_if_index = ntohl (mp->sw_if_index);
1308
1309   VALIDATE_SW_IF_INDEX (mp);
1310
1311   l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
1312   vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
1313                                sw_if_index, mp->enable, 0, 0);
1314   vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
1315                                sw_if_index, mp->enable, 0, 0);
1316   vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
1317                                sw_if_index, mp->enable, 0, 0);
1318   vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
1319                                sw_if_index, mp->enable, 0, 0);
1320
1321   BAD_SW_IF_INDEX_LABEL;
1322
1323   REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
1324 }
1325
1326 static void
1327   vl_api_sw_interface_set_vxlan_bypass_t_handler
1328   (vl_api_sw_interface_set_vxlan_bypass_t * mp)
1329 {
1330   vl_api_sw_interface_set_vxlan_bypass_reply_t *rmp;
1331   int rv = 0;
1332   u32 sw_if_index = ntohl (mp->sw_if_index);
1333
1334   VALIDATE_SW_IF_INDEX (mp);
1335
1336   if (mp->is_ipv6)
1337     {
1338       /* not yet implemented */
1339     }
1340   else
1341     vnet_feature_enable_disable ("ip4-unicast", "ip4-vxlan-bypass",
1342                                  sw_if_index, mp->enable, 0, 0);
1343
1344   BAD_SW_IF_INDEX_LABEL;
1345
1346   REPLY_MACRO (VL_API_SW_INTERFACE_SET_VXLAN_BYPASS_REPLY);
1347 }
1348
1349 static void
1350   vl_api_sw_interface_set_l2_xconnect_t_handler
1351   (vl_api_sw_interface_set_l2_xconnect_t * mp)
1352 {
1353   vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
1354   int rv = 0;
1355   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
1356   u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
1357   vlib_main_t *vm = vlib_get_main ();
1358   vnet_main_t *vnm = vnet_get_main ();
1359
1360   VALIDATE_RX_SW_IF_INDEX (mp);
1361
1362   if (mp->enable)
1363     {
1364       VALIDATE_TX_SW_IF_INDEX (mp);
1365       rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
1366                             rx_sw_if_index, 0, 0, 0, tx_sw_if_index);
1367     }
1368   else
1369     {
1370       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
1371     }
1372
1373   BAD_RX_SW_IF_INDEX_LABEL;
1374   BAD_TX_SW_IF_INDEX_LABEL;
1375
1376   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
1377 }
1378
1379 static void
1380   vl_api_sw_interface_set_l2_bridge_t_handler
1381   (vl_api_sw_interface_set_l2_bridge_t * mp)
1382 {
1383   bd_main_t *bdm = &bd_main;
1384   vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
1385   int rv = 0;
1386   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
1387   u32 bd_id = ntohl (mp->bd_id);
1388   u32 bd_index;
1389   u32 bvi = mp->bvi;
1390   u8 shg = mp->shg;
1391   vlib_main_t *vm = vlib_get_main ();
1392   vnet_main_t *vnm = vnet_get_main ();
1393
1394   VALIDATE_RX_SW_IF_INDEX (mp);
1395
1396   bd_index = bd_find_or_add_bd_index (bdm, bd_id);
1397
1398   if (mp->enable)
1399     {
1400       //VALIDATE_TX_SW_IF_INDEX(mp);
1401       rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
1402                             rx_sw_if_index, bd_index, bvi, shg, 0);
1403     }
1404   else
1405     {
1406       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
1407     }
1408
1409   BAD_RX_SW_IF_INDEX_LABEL;
1410
1411   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
1412 }
1413
1414 static void
1415   vl_api_sw_interface_set_dpdk_hqos_pipe_t_handler
1416   (vl_api_sw_interface_set_dpdk_hqos_pipe_t * mp)
1417 {
1418   vl_api_sw_interface_set_dpdk_hqos_pipe_reply_t *rmp;
1419   int rv = 0;
1420
1421 #if DPDK > 0
1422   dpdk_main_t *dm = &dpdk_main;
1423   dpdk_device_t *xd;
1424
1425   u32 sw_if_index = ntohl (mp->sw_if_index);
1426   u32 subport = ntohl (mp->subport);
1427   u32 pipe = ntohl (mp->pipe);
1428   u32 profile = ntohl (mp->profile);
1429   vnet_hw_interface_t *hw;
1430
1431   VALIDATE_SW_IF_INDEX (mp);
1432
1433   /* hw_if & dpdk device */
1434   hw = vnet_get_sup_hw_interface (dm->vnet_main, sw_if_index);
1435
1436   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
1437
1438   rv = rte_sched_pipe_config (xd->hqos_ht->hqos, subport, pipe, profile);
1439
1440   BAD_SW_IF_INDEX_LABEL;
1441 #else
1442   clib_warning ("setting HQoS pipe parameters without DPDK not implemented");
1443   rv = VNET_API_ERROR_UNIMPLEMENTED;
1444 #endif /* DPDK */
1445
1446   REPLY_MACRO (VL_API_SW_INTERFACE_SET_DPDK_HQOS_PIPE_REPLY);
1447 }
1448
1449 static void
1450   vl_api_sw_interface_set_dpdk_hqos_subport_t_handler
1451   (vl_api_sw_interface_set_dpdk_hqos_subport_t * mp)
1452 {
1453   vl_api_sw_interface_set_dpdk_hqos_subport_reply_t *rmp;
1454   int rv = 0;
1455
1456 #if DPDK > 0
1457   dpdk_main_t *dm = &dpdk_main;
1458   dpdk_device_t *xd;
1459   struct rte_sched_subport_params p;
1460
1461   u32 sw_if_index = ntohl (mp->sw_if_index);
1462   u32 subport = ntohl (mp->subport);
1463   p.tb_rate = ntohl (mp->tb_rate);
1464   p.tb_size = ntohl (mp->tb_size);
1465   p.tc_rate[0] = ntohl (mp->tc_rate[0]);
1466   p.tc_rate[1] = ntohl (mp->tc_rate[1]);
1467   p.tc_rate[2] = ntohl (mp->tc_rate[2]);
1468   p.tc_rate[3] = ntohl (mp->tc_rate[3]);
1469   p.tc_period = ntohl (mp->tc_period);
1470
1471   vnet_hw_interface_t *hw;
1472
1473   VALIDATE_SW_IF_INDEX (mp);
1474
1475   /* hw_if & dpdk device */
1476   hw = vnet_get_sup_hw_interface (dm->vnet_main, sw_if_index);
1477
1478   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
1479
1480   rv = rte_sched_subport_config (xd->hqos_ht->hqos, subport, &p);
1481
1482   BAD_SW_IF_INDEX_LABEL;
1483 #else
1484   clib_warning
1485     ("setting HQoS subport parameters without DPDK not implemented");
1486   rv = VNET_API_ERROR_UNIMPLEMENTED;
1487 #endif /* DPDK */
1488
1489   REPLY_MACRO (VL_API_SW_INTERFACE_SET_DPDK_HQOS_SUBPORT_REPLY);
1490 }
1491
1492 static void
1493   vl_api_sw_interface_set_dpdk_hqos_tctbl_t_handler
1494   (vl_api_sw_interface_set_dpdk_hqos_tctbl_t * mp)
1495 {
1496   vl_api_sw_interface_set_dpdk_hqos_tctbl_reply_t *rmp;
1497   int rv = 0;
1498
1499 #if DPDK > 0
1500   dpdk_main_t *dm = &dpdk_main;
1501   vlib_thread_main_t *tm = vlib_get_thread_main ();
1502   dpdk_device_t *xd;
1503
1504   u32 sw_if_index = ntohl (mp->sw_if_index);
1505   u32 entry = ntohl (mp->entry);
1506   u32 tc = ntohl (mp->tc);
1507   u32 queue = ntohl (mp->queue);
1508   u32 val, i;
1509
1510   vnet_hw_interface_t *hw;
1511
1512   VALIDATE_SW_IF_INDEX (mp);
1513
1514   /* hw_if & dpdk device */
1515   hw = vnet_get_sup_hw_interface (dm->vnet_main, sw_if_index);
1516
1517   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
1518
1519   if (tc >= RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE)
1520     {
1521       clib_warning ("invalid traffic class !!");
1522       rv = VNET_API_ERROR_INVALID_VALUE;
1523       goto done;
1524     }
1525   if (queue >= RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS)
1526     {
1527       clib_warning ("invalid queue !!");
1528       rv = VNET_API_ERROR_INVALID_VALUE;
1529       goto done;
1530     }
1531
1532   /* Detect the set of worker threads */
1533   uword *p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1534
1535   if (p == 0)
1536     {
1537       clib_warning ("worker thread registration AWOL !!");
1538       rv = VNET_API_ERROR_INVALID_VALUE_2;
1539       goto done;
1540     }
1541
1542   vlib_thread_registration_t *tr = (vlib_thread_registration_t *) p[0];
1543   int worker_thread_first = tr->first_index;
1544   int worker_thread_count = tr->count;
1545
1546   val = tc * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + queue;
1547   for (i = 0; i < worker_thread_count; i++)
1548     xd->hqos_wt[worker_thread_first + i].hqos_tc_table[entry] = val;
1549
1550   BAD_SW_IF_INDEX_LABEL;
1551 done:
1552 #else
1553   clib_warning ("setting HQoS DSCP table entry without DPDK not implemented");
1554   rv = VNET_API_ERROR_UNIMPLEMENTED;
1555 #endif /* DPDK */
1556
1557   REPLY_MACRO (VL_API_SW_INTERFACE_SET_DPDK_HQOS_TCTBL_REPLY);
1558 }
1559
1560 static void
1561 vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
1562 {
1563   vlib_main_t *vm = vlib_get_main ();
1564   bd_main_t *bdm = &bd_main;
1565   vl_api_bridge_domain_add_del_reply_t *rmp;
1566   int rv = 0;
1567   u32 enable_flags = 0, disable_flags = 0;
1568   u32 bd_id = ntohl (mp->bd_id);
1569   u32 bd_index;
1570
1571   if (mp->is_add)
1572     {
1573       bd_index = bd_find_or_add_bd_index (bdm, bd_id);
1574
1575       if (mp->flood)
1576         enable_flags |= L2_FLOOD;
1577       else
1578         disable_flags |= L2_FLOOD;
1579
1580       if (mp->uu_flood)
1581         enable_flags |= L2_UU_FLOOD;
1582       else
1583         disable_flags |= L2_UU_FLOOD;
1584
1585       if (mp->forward)
1586         enable_flags |= L2_FWD;
1587       else
1588         disable_flags |= L2_FWD;
1589
1590       if (mp->arp_term)
1591         enable_flags |= L2_ARP_TERM;
1592       else
1593         disable_flags |= L2_ARP_TERM;
1594
1595       if (mp->learn)
1596         enable_flags |= L2_LEARN;
1597       else
1598         disable_flags |= L2_LEARN;
1599
1600       if (enable_flags)
1601         bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
1602
1603       if (disable_flags)
1604         bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
1605
1606     }
1607   else
1608     rv = bd_delete_bd_index (bdm, bd_id);
1609
1610   REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
1611 }
1612
1613 static void
1614 vl_api_bridge_domain_details_t_handler (vl_api_bridge_domain_details_t * mp)
1615 {
1616   clib_warning ("BUG");
1617 }
1618
1619 static void
1620   vl_api_bridge_domain_sw_if_details_t_handler
1621   (vl_api_bridge_domain_sw_if_details_t * mp)
1622 {
1623   clib_warning ("BUG");
1624 }
1625
1626 static void
1627 send_bridge_domain_details (unix_shared_memory_queue_t * q,
1628                             l2_bridge_domain_t * bd_config,
1629                             u32 n_sw_ifs, u32 context)
1630 {
1631   vl_api_bridge_domain_details_t *mp;
1632
1633   mp = vl_msg_api_alloc (sizeof (*mp));
1634   memset (mp, 0, sizeof (*mp));
1635   mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
1636   mp->bd_id = ntohl (bd_config->bd_id);
1637   mp->flood = bd_feature_flood (bd_config);
1638   mp->uu_flood = bd_feature_uu_flood (bd_config);
1639   mp->forward = bd_feature_forward (bd_config);
1640   mp->learn = bd_feature_learn (bd_config);
1641   mp->arp_term = bd_feature_arp_term (bd_config);
1642   mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
1643   mp->n_sw_ifs = ntohl (n_sw_ifs);
1644   mp->context = context;
1645
1646   vl_msg_api_send_shmem (q, (u8 *) & mp);
1647 }
1648
1649 static void
1650 send_bd_sw_if_details (l2input_main_t * l2im,
1651                        unix_shared_memory_queue_t * q,
1652                        l2_flood_member_t * member, u32 bd_id, u32 context)
1653 {
1654   vl_api_bridge_domain_sw_if_details_t *mp;
1655   l2_input_config_t *input_cfg;
1656
1657   mp = vl_msg_api_alloc (sizeof (*mp));
1658   memset (mp, 0, sizeof (*mp));
1659   mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_SW_IF_DETAILS);
1660   mp->bd_id = ntohl (bd_id);
1661   mp->sw_if_index = ntohl (member->sw_if_index);
1662   input_cfg = vec_elt_at_index (l2im->configs, member->sw_if_index);
1663   mp->shg = input_cfg->shg;
1664   mp->context = context;
1665
1666   vl_msg_api_send_shmem (q, (u8 *) & mp);
1667 }
1668
1669 static void
1670 vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
1671 {
1672   bd_main_t *bdm = &bd_main;
1673   l2input_main_t *l2im = &l2input_main;
1674   unix_shared_memory_queue_t *q;
1675   l2_bridge_domain_t *bd_config;
1676   u32 bd_id, bd_index;
1677   u32 end;
1678
1679   q = vl_api_client_index_to_input_queue (mp->client_index);
1680
1681   if (q == 0)
1682     return;
1683
1684   bd_id = ntohl (mp->bd_id);
1685
1686   bd_index = (bd_id == ~0) ? 0 : bd_find_or_add_bd_index (bdm, bd_id);
1687   end = (bd_id == ~0) ? vec_len (l2im->bd_configs) : bd_index + 1;
1688   for (; bd_index < end; bd_index++)
1689     {
1690       bd_config = l2input_bd_config_from_index (l2im, bd_index);
1691       /* skip dummy bd_id 0 */
1692       if (bd_config && (bd_config->bd_id > 0))
1693         {
1694           u32 n_sw_ifs;
1695           l2_flood_member_t *m;
1696
1697           n_sw_ifs = vec_len (bd_config->members);
1698           send_bridge_domain_details (q, bd_config, n_sw_ifs, mp->context);
1699
1700           vec_foreach (m, bd_config->members)
1701           {
1702             send_bd_sw_if_details (l2im, q, m, bd_config->bd_id, mp->context);
1703           }
1704         }
1705     }
1706 }
1707
1708 static void
1709 vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
1710 {
1711   bd_main_t *bdm = &bd_main;
1712   l2input_main_t *l2im = &l2input_main;
1713   vl_api_l2fib_add_del_reply_t *rmp;
1714   int rv = 0;
1715   u64 mac = 0;
1716   u32 sw_if_index = ntohl (mp->sw_if_index);
1717   u32 bd_id = ntohl (mp->bd_id);
1718   u32 bd_index;
1719   u32 static_mac;
1720   u32 filter_mac;
1721   u32 bvi_mac;
1722   uword *p;
1723
1724   mac = mp->mac;
1725
1726   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1727   if (!p)
1728     {
1729       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1730       goto bad_sw_if_index;
1731     }
1732   bd_index = p[0];
1733
1734   if (mp->is_add)
1735     {
1736       filter_mac = mp->filter_mac ? 1 : 0;
1737       if (filter_mac == 0)
1738         {
1739           VALIDATE_SW_IF_INDEX (mp);
1740           if (vec_len (l2im->configs) <= sw_if_index)
1741             {
1742               rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1743               goto bad_sw_if_index;
1744             }
1745           else
1746             {
1747               l2_input_config_t *config;
1748               config = vec_elt_at_index (l2im->configs, sw_if_index);
1749               if (config->bridge == 0)
1750                 {
1751                   rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1752                   goto bad_sw_if_index;
1753                 }
1754             }
1755         }
1756       static_mac = mp->static_mac ? 1 : 0;
1757       bvi_mac = mp->bvi_mac ? 1 : 0;
1758       l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac,
1759                        bvi_mac);
1760     }
1761   else
1762     {
1763       l2fib_del_entry (mac, bd_index);
1764     }
1765
1766   BAD_SW_IF_INDEX_LABEL;
1767
1768   REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
1769 }
1770
1771 static void
1772 vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
1773 {
1774   vl_api_l2_flags_reply_t *rmp;
1775   int rv = 0;
1776   u32 sw_if_index = ntohl (mp->sw_if_index);
1777   u32 flags = ntohl (mp->feature_bitmap);
1778   u32 rbm = 0;
1779
1780   VALIDATE_SW_IF_INDEX (mp);
1781
1782 #define _(a,b) \
1783     if (flags & L2INPUT_FEAT_ ## a) \
1784         rbm = l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_ ## a, mp->is_set);
1785   foreach_l2input_feat;
1786 #undef _
1787
1788   BAD_SW_IF_INDEX_LABEL;
1789
1790   /* *INDENT-OFF* */
1791   REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
1792   ({
1793     rmp->resulting_feature_bitmap = ntohl(rbm);
1794   }));
1795   /* *INDENT-ON* */
1796 }
1797
1798 static void
1799 vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
1800 {
1801   vlib_main_t *vm = vlib_get_main ();
1802   bd_main_t *bdm = &bd_main;
1803   vl_api_bridge_flags_reply_t *rmp;
1804   int rv = 0;
1805   u32 bd_id = ntohl (mp->bd_id);
1806   u32 bd_index;
1807   u32 flags = ntohl (mp->feature_bitmap);
1808   uword *p;
1809
1810   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1811   if (p == 0)
1812     {
1813       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1814       goto out;
1815     }
1816
1817   bd_index = p[0];
1818
1819   bd_set_flags (vm, bd_index, flags, mp->is_set);
1820
1821 out:
1822   /* *INDENT-OFF* */
1823   REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
1824   ({
1825     rmp->resulting_feature_bitmap = ntohl(flags);
1826   }));
1827   /* *INDENT-ON* */
1828 }
1829
1830 static void
1831 vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
1832 {
1833   bd_main_t *bdm = &bd_main;
1834   vl_api_bd_ip_mac_add_del_reply_t *rmp;
1835   int rv = 0;
1836   u32 bd_id = ntohl (mp->bd_id);
1837   u32 bd_index;
1838   uword *p;
1839
1840   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1841   if (p == 0)
1842     {
1843       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1844       goto out;
1845     }
1846
1847   bd_index = p[0];
1848   if (bd_add_del_ip_mac (bd_index, mp->ip_address,
1849                          mp->mac_address, mp->is_ipv6, mp->is_add))
1850     rv = VNET_API_ERROR_UNSPECIFIED;
1851
1852 out:
1853   REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
1854 }
1855
1856 static void
1857 vl_api_tap_connect_t_handler (vl_api_tap_connect_t * mp)
1858 {
1859   vlib_main_t *vm = vlib_get_main ();
1860   int rv;
1861   vl_api_tap_connect_reply_t *rmp;
1862   vnet_main_t *vnm = vnet_get_main ();
1863   unix_shared_memory_queue_t *q;
1864   u32 sw_if_index = (u32) ~ 0;
1865   u8 *tag;
1866
1867   rv = vnet_tap_connect_renumber (vm, mp->tap_name,
1868                                   mp->use_random_mac ? 0 : mp->mac_address,
1869                                   &sw_if_index, mp->renumber,
1870                                   ntohl (mp->custom_dev_instance));
1871
1872   /* Add tag if supplied */
1873   if (rv == 0 && mp->tag[0])
1874     {
1875       mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
1876       tag = format (0, "%s%c", mp->tag, 0);
1877       vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
1878     }
1879
1880   q = vl_api_client_index_to_input_queue (mp->client_index);
1881   if (!q)
1882     return;
1883
1884   rmp = vl_msg_api_alloc (sizeof (*rmp));
1885   rmp->_vl_msg_id = ntohs (VL_API_TAP_CONNECT_REPLY);
1886   rmp->context = mp->context;
1887   rmp->retval = ntohl (rv);
1888   rmp->sw_if_index = ntohl (sw_if_index);
1889
1890   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1891 }
1892
1893 static void
1894 vl_api_tap_modify_t_handler (vl_api_tap_modify_t * mp)
1895 {
1896   int rv;
1897   vl_api_tap_modify_reply_t *rmp;
1898   unix_shared_memory_queue_t *q;
1899   u32 sw_if_index = (u32) ~ 0;
1900   vlib_main_t *vm = vlib_get_main ();
1901
1902   rv = vnet_tap_modify (vm, ntohl (mp->sw_if_index), mp->tap_name,
1903                         mp->use_random_mac ? 0 : mp->mac_address,
1904                         &sw_if_index, mp->renumber,
1905                         ntohl (mp->custom_dev_instance));
1906
1907   q = vl_api_client_index_to_input_queue (mp->client_index);
1908   if (!q)
1909     return;
1910
1911   rmp = vl_msg_api_alloc (sizeof (*rmp));
1912   rmp->_vl_msg_id = ntohs (VL_API_TAP_MODIFY_REPLY);
1913   rmp->context = mp->context;
1914   rmp->retval = ntohl (rv);
1915   rmp->sw_if_index = ntohl (sw_if_index);
1916
1917   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1918 }
1919
1920 static void
1921 vl_api_tap_delete_t_handler (vl_api_tap_delete_t * mp)
1922 {
1923   vlib_main_t *vm = vlib_get_main ();
1924   int rv;
1925   vpe_api_main_t *vam = &vpe_api_main;
1926   vl_api_tap_delete_reply_t *rmp;
1927   unix_shared_memory_queue_t *q;
1928   u32 sw_if_index = ntohl (mp->sw_if_index);
1929
1930   rv = vnet_tap_delete (vm, sw_if_index);
1931   if (!rv)
1932     {
1933       vnet_main_t *vnm = vnet_get_main ();
1934       vnet_clear_sw_interface_tag (vnm, sw_if_index);
1935     }
1936
1937   q = vl_api_client_index_to_input_queue (mp->client_index);
1938   if (!q)
1939     return;
1940
1941   rmp = vl_msg_api_alloc (sizeof (*rmp));
1942   rmp->_vl_msg_id = ntohs (VL_API_TAP_DELETE_REPLY);
1943   rmp->context = mp->context;
1944   rmp->retval = ntohl (rv);
1945
1946   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1947
1948   if (!rv)
1949     send_sw_interface_flags_deleted (vam, q, sw_if_index);
1950 }
1951
1952 static void
1953 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
1954 {
1955   vl_api_create_vlan_subif_reply_t *rmp;
1956   vnet_main_t *vnm = vnet_get_main ();
1957   u32 hw_if_index, sw_if_index = (u32) ~ 0;
1958   vnet_hw_interface_t *hi;
1959   int rv = 0;
1960   u32 id;
1961   vnet_sw_interface_t template;
1962   uword *p;
1963   vnet_interface_main_t *im = &vnm->interface_main;
1964   u64 sup_and_sub_key;
1965   u64 *kp;
1966   unix_shared_memory_queue_t *q;
1967   clib_error_t *error;
1968
1969   VALIDATE_SW_IF_INDEX (mp);
1970
1971   hw_if_index = ntohl (mp->sw_if_index);
1972   hi = vnet_get_hw_interface (vnm, hw_if_index);
1973
1974   id = ntohl (mp->vlan_id);
1975   if (id == 0 || id > 4095)
1976     {
1977       rv = VNET_API_ERROR_INVALID_VLAN;
1978       goto out;
1979     }
1980
1981   sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
1982
1983   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1984   if (p)
1985     {
1986       rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
1987       goto out;
1988     }
1989
1990   kp = clib_mem_alloc (sizeof (*kp));
1991   *kp = sup_and_sub_key;
1992
1993   memset (&template, 0, sizeof (template));
1994   template.type = VNET_SW_INTERFACE_TYPE_SUB;
1995   template.sup_sw_if_index = hi->sw_if_index;
1996   template.sub.id = id;
1997   template.sub.eth.raw_flags = 0;
1998   template.sub.eth.flags.one_tag = 1;
1999   template.sub.eth.outer_vlan_id = id;
2000   template.sub.eth.flags.exact_match = 1;
2001
2002   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
2003   if (error)
2004     {
2005       clib_error_report (error);
2006       rv = VNET_API_ERROR_INVALID_REGISTRATION;
2007       goto out;
2008     }
2009   hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
2010   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
2011
2012   BAD_SW_IF_INDEX_LABEL;
2013
2014 out:
2015   q = vl_api_client_index_to_input_queue (mp->client_index);
2016   if (!q)
2017     return;
2018
2019   rmp = vl_msg_api_alloc (sizeof (*rmp));
2020   rmp->_vl_msg_id = ntohs (VL_API_CREATE_VLAN_SUBIF_REPLY);
2021   rmp->context = mp->context;
2022   rmp->retval = ntohl (rv);
2023   rmp->sw_if_index = ntohl (sw_if_index);
2024   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2025 }
2026
2027 static void
2028 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
2029 {
2030   vl_api_create_subif_reply_t *rmp;
2031   vnet_main_t *vnm = vnet_get_main ();
2032   u32 sw_if_index = ~0;
2033   int rv = 0;
2034   u32 sub_id;
2035   vnet_sw_interface_t *si;
2036   vnet_hw_interface_t *hi;
2037   vnet_sw_interface_t template;
2038   uword *p;
2039   vnet_interface_main_t *im = &vnm->interface_main;
2040   u64 sup_and_sub_key;
2041   u64 *kp;
2042   clib_error_t *error;
2043
2044   VALIDATE_SW_IF_INDEX (mp);
2045
2046   si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
2047   hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
2048
2049   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
2050     {
2051       rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
2052       goto out;
2053     }
2054
2055   sw_if_index = si->sw_if_index;
2056   sub_id = ntohl (mp->sub_id);
2057
2058   sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
2059
2060   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
2061   if (p)
2062     {
2063       if (CLIB_DEBUG > 0)
2064         clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
2065                       sw_if_index, sub_id);
2066       rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
2067       goto out;
2068     }
2069
2070   kp = clib_mem_alloc (sizeof (*kp));
2071   *kp = sup_and_sub_key;
2072
2073   memset (&template, 0, sizeof (template));
2074   template.type = VNET_SW_INTERFACE_TYPE_SUB;
2075   template.sup_sw_if_index = sw_if_index;
2076   template.sub.id = sub_id;
2077   template.sub.eth.flags.no_tags = mp->no_tags;
2078   template.sub.eth.flags.one_tag = mp->one_tag;
2079   template.sub.eth.flags.two_tags = mp->two_tags;
2080   template.sub.eth.flags.dot1ad = mp->dot1ad;
2081   template.sub.eth.flags.exact_match = mp->exact_match;
2082   template.sub.eth.flags.default_sub = mp->default_sub;
2083   template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
2084   template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
2085   template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
2086   template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
2087
2088   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
2089   if (error)
2090     {
2091       clib_error_report (error);
2092       rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
2093       goto out;
2094     }
2095
2096   hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
2097   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
2098
2099   BAD_SW_IF_INDEX_LABEL;
2100
2101 out:
2102
2103   /* *INDENT-OFF* */
2104   REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
2105   ({
2106     rmp->sw_if_index = ntohl(sw_if_index);
2107   }));
2108   /* *INDENT-ON* */
2109 }
2110
2111 static void
2112 vl_api_mpls_tunnel_add_del_t_handler (vl_api_mpls_tunnel_add_del_t * mp)
2113 {
2114   vl_api_mpls_tunnel_add_del_reply_t *rmp;
2115   int rv = 0;
2116   stats_main_t *sm = &stats_main;
2117   u32 tunnel_sw_if_index;
2118   int ii;
2119
2120   dslock (sm, 1 /* release hint */ , 5 /* tag */ );
2121
2122   if (mp->mt_is_add)
2123     {
2124       fib_route_path_t rpath, *rpaths = NULL;
2125       mpls_label_t *label_stack = NULL;
2126
2127       memset (&rpath, 0, sizeof (rpath));
2128
2129       if (mp->mt_next_hop_proto_is_ip4)
2130         {
2131           rpath.frp_proto = FIB_PROTOCOL_IP4;
2132           clib_memcpy (&rpath.frp_addr.ip4,
2133                        mp->mt_next_hop, sizeof (rpath.frp_addr.ip4));
2134         }
2135       else
2136         {
2137           rpath.frp_proto = FIB_PROTOCOL_IP6;
2138           clib_memcpy (&rpath.frp_addr.ip6,
2139                        mp->mt_next_hop, sizeof (rpath.frp_addr.ip6));
2140         }
2141       rpath.frp_sw_if_index = ntohl (mp->mt_next_hop_sw_if_index);
2142
2143       for (ii = 0; ii < mp->mt_next_hop_n_out_labels; ii++)
2144         vec_add1 (label_stack, ntohl (mp->mt_next_hop_out_label_stack[ii]));
2145
2146       vec_add1 (rpaths, rpath);
2147
2148       vnet_mpls_tunnel_add (rpaths, label_stack,
2149                             mp->mt_l2_only, &tunnel_sw_if_index);
2150       vec_free (rpaths);
2151       vec_free (label_stack);
2152     }
2153   else
2154     {
2155       tunnel_sw_if_index = ntohl (mp->mt_sw_if_index);
2156       vnet_mpls_tunnel_del (tunnel_sw_if_index);
2157     }
2158
2159   dsunlock (sm);
2160
2161   /* *INDENT-OFF* */
2162   REPLY_MACRO2(VL_API_MPLS_TUNNEL_ADD_DEL_REPLY,
2163   ({
2164     rmp->sw_if_index = ntohl(tunnel_sw_if_index);
2165   }));
2166   /* *INDENT-ON* */
2167 }
2168
2169 static void
2170 vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2171 {
2172   vl_api_proxy_arp_add_del_reply_t *rmp;
2173   u32 fib_index;
2174   int rv;
2175   ip4_main_t *im = &ip4_main;
2176   stats_main_t *sm = &stats_main;
2177   int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
2178                               ip4_address_t * hi_addr,
2179                               u32 fib_index, int is_del);
2180   uword *p;
2181
2182   dslock (sm, 1 /* release hint */ , 6 /* tag */ );
2183
2184   p = hash_get (im->fib_index_by_table_id, ntohl (mp->vrf_id));
2185
2186   if (!p)
2187     {
2188       rv = VNET_API_ERROR_NO_SUCH_FIB;
2189       goto out;
2190     }
2191
2192   fib_index = p[0];
2193
2194   rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->low_address,
2195                                (ip4_address_t *) mp->hi_address,
2196                                fib_index, mp->is_add == 0);
2197
2198 out:
2199   dsunlock (sm);
2200   REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2201 }
2202
2203 static void
2204   vl_api_proxy_arp_intfc_enable_disable_t_handler
2205   (vl_api_proxy_arp_intfc_enable_disable_t * mp)
2206 {
2207   int rv = 0;
2208   vnet_main_t *vnm = vnet_get_main ();
2209   vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
2210   vnet_sw_interface_t *si;
2211   u32 sw_if_index;
2212
2213   VALIDATE_SW_IF_INDEX (mp);
2214
2215   sw_if_index = ntohl (mp->sw_if_index);
2216
2217   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
2218     {
2219       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2220       goto out;
2221     }
2222
2223   si = vnet_get_sw_interface (vnm, sw_if_index);
2224
2225   ASSERT (si);
2226
2227   if (mp->enable_disable)
2228     si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2229   else
2230     si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2231
2232   BAD_SW_IF_INDEX_LABEL;
2233
2234 out:
2235   REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
2236 }
2237
2238 static void
2239 vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
2240                                       vlib_main_t * vm)
2241 {
2242   vl_api_ip_neighbor_add_del_reply_t *rmp;
2243   vnet_main_t *vnm = vnet_get_main ();
2244   stats_main_t *sm = &stats_main;
2245   int rv = 0;
2246
2247   VALIDATE_SW_IF_INDEX (mp);
2248
2249   dslock (sm, 1 /* release hint */ , 7 /* tag */ );
2250
2251   /*
2252    * there's no validation here of the ND/ARP entry being added.
2253    * The expectation is that the FIB will ensure that nothing bad
2254    * will come of adding bogus entries.
2255    */
2256   if (mp->is_ipv6)
2257     {
2258       if (mp->is_add)
2259         rv = vnet_set_ip6_ethernet_neighbor
2260           (vm, ntohl (mp->sw_if_index),
2261            (ip6_address_t *) (mp->dst_address),
2262            mp->mac_address, sizeof (mp->mac_address), mp->is_static);
2263       else
2264         rv = vnet_unset_ip6_ethernet_neighbor
2265           (vm, ntohl (mp->sw_if_index),
2266            (ip6_address_t *) (mp->dst_address),
2267            mp->mac_address, sizeof (mp->mac_address));
2268     }
2269   else
2270     {
2271       ethernet_arp_ip4_over_ethernet_address_t a;
2272
2273       clib_memcpy (&a.ethernet, mp->mac_address, 6);
2274       clib_memcpy (&a.ip4, mp->dst_address, 4);
2275
2276       if (mp->is_add)
2277         rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index),
2278                                              &a, mp->is_static);
2279       else
2280         rv =
2281           vnet_arp_unset_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index), &a);
2282     }
2283
2284   BAD_SW_IF_INDEX_LABEL;
2285
2286   dsunlock (sm);
2287   REPLY_MACRO (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
2288 }
2289
2290 static void
2291 vl_api_is_address_reachable_t_handler (vl_api_is_address_reachable_t * mp)
2292 {
2293 #if 0
2294   vpe_main_t *rm = &vpe_main;
2295   ip4_main_t *im4 = &ip4_main;
2296   ip6_main_t *im6 = &ip6_main;
2297   ip_lookup_main_t *lm;
2298   union
2299   {
2300     ip4_address_t ip4;
2301     ip6_address_t ip6;
2302   } addr;
2303   u32 adj_index, sw_if_index;
2304   vl_api_is_address_reachable_t *rmp;
2305   ip_adjacency_t *adj;
2306   unix_shared_memory_queue_t *q;
2307
2308   q = vl_api_client_index_to_input_queue (mp->client_index);
2309   if (!q)
2310     {
2311       increment_missing_api_client_counter (rm->vlib_main);
2312       return;
2313     }
2314
2315   rmp = vl_msg_api_alloc (sizeof (*rmp));
2316   clib_memcpy (rmp, mp, sizeof (*rmp));
2317
2318   sw_if_index = mp->next_hop_sw_if_index;
2319   clib_memcpy (&addr, mp->address, sizeof (addr));
2320   if (mp->is_ipv6)
2321     {
2322       lm = &im6->lookup_main;
2323       adj_index = ip6_fib_lookup (im6, sw_if_index, &addr.ip6);
2324     }
2325   else
2326     {
2327       lm = &im4->lookup_main;
2328       // FIXME NOT an ADJ
2329       adj_index = ip4_fib_lookup (im4, sw_if_index, &addr.ip4);
2330     }
2331   if (adj_index == ~0)
2332     {
2333       rmp->is_error = 1;
2334       goto send;
2335     }
2336   adj = ip_get_adjacency (lm, adj_index);
2337
2338   if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE
2339       && adj->rewrite_header.sw_if_index == sw_if_index)
2340     {
2341       rmp->is_known = 1;
2342     }
2343   else
2344     {
2345       if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP
2346           && adj->rewrite_header.sw_if_index == sw_if_index)
2347         {
2348           if (mp->is_ipv6)
2349             ip6_probe_neighbor (rm->vlib_main, &addr.ip6, sw_if_index);
2350           else
2351             ip4_probe_neighbor (rm->vlib_main, &addr.ip4, sw_if_index);
2352         }
2353       else if (adj->lookup_next_index == IP_LOOKUP_NEXT_DROP)
2354         {
2355           rmp->is_known = 1;
2356           goto send;
2357         }
2358       rmp->is_known = 0;
2359     }
2360
2361 send:
2362   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2363 #endif
2364 }
2365
2366 static void
2367 vl_api_sw_interface_details_t_handler (vl_api_sw_interface_details_t * mp)
2368 {
2369   clib_warning ("BUG");
2370 }
2371
2372 static void
2373   vl_api_sw_interface_set_mpls_enable_t_handler
2374   (vl_api_sw_interface_set_mpls_enable_t * mp)
2375 {
2376   vl_api_sw_interface_set_mpls_enable_reply_t *rmp;
2377   int rv = 0;
2378
2379   VALIDATE_SW_IF_INDEX (mp);
2380
2381   mpls_sw_interface_enable_disable (&mpls_main,
2382                                     ntohl (mp->sw_if_index), mp->enable);
2383
2384   BAD_SW_IF_INDEX_LABEL;
2385   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MPLS_ENABLE_REPLY);
2386 }
2387
2388 static void
2389 vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
2390                                            mp)
2391 {
2392   vl_api_sw_interface_clear_stats_reply_t *rmp;
2393
2394   vnet_main_t *vnm = vnet_get_main ();
2395   vnet_interface_main_t *im = &vnm->interface_main;
2396   vlib_simple_counter_main_t *sm;
2397   vlib_combined_counter_main_t *cm;
2398   static vnet_main_t **my_vnet_mains;
2399   int i, j, n_counters;
2400   int rv = 0;
2401
2402   if (mp->sw_if_index != ~0)
2403     VALIDATE_SW_IF_INDEX (mp);
2404
2405   vec_reset_length (my_vnet_mains);
2406
2407   for (i = 0; i < vec_len (vnet_mains); i++)
2408     {
2409       if (vnet_mains[i])
2410         vec_add1 (my_vnet_mains, vnet_mains[i]);
2411     }
2412
2413   if (vec_len (vnet_mains) == 0)
2414     vec_add1 (my_vnet_mains, vnm);
2415
2416   n_counters = vec_len (im->combined_sw_if_counters);
2417
2418   for (j = 0; j < n_counters; j++)
2419     {
2420       for (i = 0; i < vec_len (my_vnet_mains); i++)
2421         {
2422           im = &my_vnet_mains[i]->interface_main;
2423           cm = im->combined_sw_if_counters + j;
2424           if (mp->sw_if_index == (u32) ~ 0)
2425             vlib_clear_combined_counters (cm);
2426           else
2427             vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
2428         }
2429     }
2430
2431   n_counters = vec_len (im->sw_if_counters);
2432
2433   for (j = 0; j < n_counters; j++)
2434     {
2435       for (i = 0; i < vec_len (my_vnet_mains); i++)
2436         {
2437           im = &my_vnet_mains[i]->interface_main;
2438           sm = im->sw_if_counters + j;
2439           if (mp->sw_if_index == (u32) ~ 0)
2440             vlib_clear_simple_counters (sm);
2441           else
2442             vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
2443         }
2444     }
2445
2446   BAD_SW_IF_INDEX_LABEL;
2447
2448   REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
2449 }
2450
2451 static void
2452 send_sw_interface_details (vpe_api_main_t * am,
2453                            unix_shared_memory_queue_t * q,
2454                            vnet_sw_interface_t * swif,
2455                            u8 * interface_name, u32 context)
2456 {
2457   vl_api_sw_interface_details_t *mp;
2458   vnet_main_t *vnm = vnet_get_main ();
2459   vnet_hw_interface_t *hi;
2460   u8 *tag;
2461
2462   hi = vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
2463
2464   mp = vl_msg_api_alloc (sizeof (*mp));
2465   memset (mp, 0, sizeof (*mp));
2466   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
2467   mp->sw_if_index = ntohl (swif->sw_if_index);
2468   mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
2469   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
2470   mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
2471   mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
2472                      VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
2473   mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
2474                     VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
2475   mp->link_mtu = ntohs (hi->max_packet_bytes);
2476   mp->context = context;
2477
2478   strncpy ((char *) mp->interface_name,
2479            (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
2480
2481   /* Send the L2 address for ethernet physical intfcs */
2482   if (swif->sup_sw_if_index == swif->sw_if_index
2483       && hi->hw_class_index == ethernet_hw_interface_class.index)
2484     {
2485       ethernet_main_t *em = ethernet_get_main (am->vlib_main);
2486       ethernet_interface_t *ei;
2487
2488       ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
2489       ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
2490       clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
2491       mp->l2_address_length = ntohl (sizeof (ei->address));
2492     }
2493   else if (swif->sup_sw_if_index != swif->sw_if_index)
2494     {
2495       vnet_sub_interface_t *sub = &swif->sub;
2496       mp->sub_id = ntohl (sub->id);
2497       mp->sub_dot1ad = sub->eth.flags.dot1ad;
2498       mp->sub_number_of_tags =
2499         sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
2500       mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
2501       mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
2502       mp->sub_exact_match = sub->eth.flags.exact_match;
2503       mp->sub_default = sub->eth.flags.default_sub;
2504       mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
2505       mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
2506
2507       /* vlan tag rewrite data */
2508       u32 vtr_op = L2_VTR_DISABLED;
2509       u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
2510
2511       if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
2512                      &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
2513         {
2514           // error - default to disabled
2515           mp->vtr_op = ntohl (L2_VTR_DISABLED);
2516           clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
2517                         swif->sw_if_index);
2518         }
2519       else
2520         {
2521           mp->vtr_op = ntohl (vtr_op);
2522           mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
2523           mp->vtr_tag1 = ntohl (vtr_tag1);
2524           mp->vtr_tag2 = ntohl (vtr_tag2);
2525         }
2526     }
2527
2528   tag = vnet_get_sw_interface_tag (vnm, swif->sw_if_index);
2529   if (tag)
2530     strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
2531
2532   vl_msg_api_send_shmem (q, (u8 *) & mp);
2533 }
2534
2535 static void
2536 send_sw_interface_flags (vpe_api_main_t * am,
2537                          unix_shared_memory_queue_t * q,
2538                          vnet_sw_interface_t * swif)
2539 {
2540   vl_api_sw_interface_set_flags_t *mp;
2541   vnet_main_t *vnm = am->vnet_main;
2542
2543   vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
2544                                                        swif->sw_if_index);
2545   mp = vl_msg_api_alloc (sizeof (*mp));
2546   memset (mp, 0, sizeof (*mp));
2547   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SET_FLAGS);
2548   mp->sw_if_index = ntohl (swif->sw_if_index);
2549
2550   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
2551   mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
2552   vl_msg_api_send_shmem (q, (u8 *) & mp);
2553 }
2554
2555 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
2556                                              unix_shared_memory_queue_t * q,
2557                                              u32 sw_if_index)
2558   __attribute__ ((unused));
2559
2560 static void
2561 send_sw_interface_flags_deleted (vpe_api_main_t * am,
2562                                  unix_shared_memory_queue_t * q,
2563                                  u32 sw_if_index)
2564 {
2565   vl_api_sw_interface_set_flags_t *mp;
2566
2567   mp = vl_msg_api_alloc (sizeof (*mp));
2568   memset (mp, 0, sizeof (*mp));
2569   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SET_FLAGS);
2570   mp->sw_if_index = ntohl (sw_if_index);
2571
2572   mp->admin_up_down = 0;
2573   mp->link_up_down = 0;
2574   mp->deleted = 1;
2575   vl_msg_api_send_shmem (q, (u8 *) & mp);
2576 }
2577
2578 static void
2579 vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
2580 {
2581   vpe_api_main_t *am = &vpe_api_main;
2582   vnet_sw_interface_t *swif;
2583   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2584   u8 *filter_string = 0, *name_string = 0;
2585   unix_shared_memory_queue_t *q;
2586   char *strcasestr (char *, char *);    /* lnx hdr file botch */
2587
2588   q = vl_api_client_index_to_input_queue (mp->client_index);
2589
2590   if (q == 0)
2591     return;
2592
2593   if (mp->name_filter_valid)
2594     {
2595       mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
2596       filter_string = format (0, "%s%c", mp->name_filter, 0);
2597     }
2598
2599   /* *INDENT-OFF* */
2600   pool_foreach (swif, im->sw_interfaces,
2601   ({
2602     name_string = format (name_string, "%U%c",
2603                           format_vnet_sw_interface_name,
2604                           am->vnet_main, swif, 0);
2605
2606     if (mp->name_filter_valid == 0 ||
2607         strcasestr((char *) name_string, (char *) filter_string)) {
2608
2609       send_sw_interface_details (am, q, swif, name_string, mp->context);
2610     }
2611     _vec_len (name_string) = 0;
2612   }));
2613   /* *INDENT-ON* */
2614
2615   vec_free (name_string);
2616   vec_free (filter_string);
2617 }
2618
2619 void
2620 send_oam_event (oam_target_t * t)
2621 {
2622   vpe_api_main_t *vam = &vpe_api_main;
2623   unix_shared_memory_queue_t *q;
2624   vpe_client_registration_t *reg;
2625   vl_api_oam_event_t *mp;
2626
2627   /* *INDENT-OFF* */
2628   pool_foreach(reg, vam->oam_events_registrations,
2629   ({
2630     q = vl_api_client_index_to_input_queue (reg->client_index);
2631     if (q)
2632       {
2633         mp = vl_msg_api_alloc (sizeof (*mp));
2634         mp->_vl_msg_id = ntohs (VL_API_OAM_EVENT);
2635         clib_memcpy (mp->dst_address, &t->dst_address,
2636                      sizeof (mp->dst_address));
2637         mp->state = t->state;
2638         vl_msg_api_send_shmem (q, (u8 *)&mp);
2639       }
2640   }));
2641   /* *INDENT-ON* */
2642 }
2643
2644 static void
2645 vl_api_oam_add_del_t_handler (vl_api_oam_add_del_t * mp)
2646 {
2647   vl_api_oam_add_del_reply_t *rmp;
2648   int rv;
2649
2650   rv = vpe_oam_add_del_target ((ip4_address_t *) mp->src_address,
2651                                (ip4_address_t *) mp->dst_address,
2652                                ntohl (mp->vrf_id), (int) (mp->is_add));
2653
2654   REPLY_MACRO (VL_API_OAM_ADD_DEL_REPLY);
2655 }
2656
2657 static void
2658 vl_api_vnet_get_summary_stats_t_handler (vl_api_vnet_get_summary_stats_t * mp)
2659 {
2660   stats_main_t *sm = &stats_main;
2661   vnet_interface_main_t *im = sm->interface_main;
2662   vl_api_vnet_summary_stats_reply_t *rmp;
2663   vlib_combined_counter_main_t *cm;
2664   vlib_counter_t v;
2665   int i, which;
2666   u64 total_pkts[VLIB_N_RX_TX];
2667   u64 total_bytes[VLIB_N_RX_TX];
2668
2669   unix_shared_memory_queue_t *q =
2670     vl_api_client_index_to_input_queue (mp->client_index);
2671
2672   if (!q)
2673     return;
2674
2675   rmp = vl_msg_api_alloc (sizeof (*rmp));
2676   rmp->_vl_msg_id = ntohs (VL_API_VNET_SUMMARY_STATS_REPLY);
2677   rmp->context = mp->context;
2678   rmp->retval = 0;
2679
2680   memset (total_pkts, 0, sizeof (total_pkts));
2681   memset (total_bytes, 0, sizeof (total_bytes));
2682
2683   vnet_interface_counter_lock (im);
2684
2685   vec_foreach (cm, im->combined_sw_if_counters)
2686   {
2687     which = cm - im->combined_sw_if_counters;
2688
2689     for (i = 0; i < vec_len (cm->maxi); i++)
2690       {
2691         vlib_get_combined_counter (cm, i, &v);
2692         total_pkts[which] += v.packets;
2693         total_bytes[which] += v.bytes;
2694       }
2695   }
2696   vnet_interface_counter_unlock (im);
2697
2698   rmp->total_pkts[VLIB_RX] = clib_host_to_net_u64 (total_pkts[VLIB_RX]);
2699   rmp->total_bytes[VLIB_RX] = clib_host_to_net_u64 (total_bytes[VLIB_RX]);
2700   rmp->total_pkts[VLIB_TX] = clib_host_to_net_u64 (total_pkts[VLIB_TX]);
2701   rmp->total_bytes[VLIB_TX] = clib_host_to_net_u64 (total_bytes[VLIB_TX]);
2702   rmp->vector_rate =
2703     clib_host_to_net_u64 (vlib_last_vector_length_per_node (sm->vlib_main));
2704
2705   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2706 }
2707
2708 /* *INDENT-OFF* */
2709 typedef CLIB_PACKED (struct {
2710   ip4_address_t address;
2711   u32 address_length: 6;
2712   u32 index:26;
2713 }) ip4_route_t;
2714 /* *INDENT-ON* */
2715
2716 static int
2717 ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2718 {
2719   vnet_main_t *vnm = vnet_get_main ();
2720   vnet_interface_main_t *im = &vnm->interface_main;
2721   ip4_main_t *im4 = &ip4_main;
2722   static u32 *sw_if_indices_to_shut;
2723   stats_main_t *sm = &stats_main;
2724   fib_table_t *fib_table;
2725   ip4_fib_t *fib;
2726   u32 sw_if_index;
2727   int i;
2728   int rv = VNET_API_ERROR_NO_SUCH_FIB;
2729   u32 target_fib_id = ntohl (mp->vrf_id);
2730
2731   dslock (sm, 1 /* release hint */ , 8 /* tag */ );
2732
2733   /* *INDENT-OFF* */
2734   pool_foreach (fib_table, im4->fibs,
2735   ({
2736     fib = &fib_table->v4;
2737     vnet_sw_interface_t * si;
2738
2739     if (fib->table_id != target_fib_id)
2740       continue;
2741
2742     /* remove any mpls encap/decap labels */
2743     mpls_fib_reset_labels (fib->table_id);
2744
2745     /* remove any proxy arps in this fib */
2746     vnet_proxy_arp_fib_reset (fib->table_id);
2747
2748     /* Set the flow hash for this fib to the default */
2749     vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2750
2751     vec_reset_length (sw_if_indices_to_shut);
2752
2753     /* Shut down interfaces in this FIB / clean out intfc routes */
2754     pool_foreach (si, im->sw_interfaces,
2755     ({
2756       u32 sw_if_index = si->sw_if_index;
2757
2758       if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
2759           && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
2760               fib->index))
2761         vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2762     }));
2763
2764     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2765       sw_if_index = sw_if_indices_to_shut[i];
2766       // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
2767
2768       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2769       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2770       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2771     }
2772
2773     fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
2774     fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_INTERFACE);
2775
2776     rv = 0;
2777     break;
2778     })); /* pool_foreach (fib) */
2779     /* *INDENT-ON* */
2780
2781   dsunlock (sm);
2782   return rv;
2783 }
2784
2785 static int
2786 ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2787 {
2788   vnet_main_t *vnm = vnet_get_main ();
2789   vnet_interface_main_t *im = &vnm->interface_main;
2790   ip6_main_t *im6 = &ip6_main;
2791   stats_main_t *sm = &stats_main;
2792   static u32 *sw_if_indices_to_shut;
2793   fib_table_t *fib_table;
2794   ip6_fib_t *fib;
2795   u32 sw_if_index;
2796   int i;
2797   int rv = VNET_API_ERROR_NO_SUCH_FIB;
2798   u32 target_fib_id = ntohl (mp->vrf_id);
2799
2800   dslock (sm, 1 /* release hint */ , 9 /* tag */ );
2801
2802   /* *INDENT-OFF* */
2803   pool_foreach (fib_table, im6->fibs,
2804   ({
2805     vnet_sw_interface_t * si;
2806     fib = &(fib_table->v6);
2807
2808     if (fib->table_id != target_fib_id)
2809       continue;
2810
2811     vec_reset_length (sw_if_indices_to_shut);
2812
2813     /* Shut down interfaces in this FIB / clean out intfc routes */
2814     pool_foreach (si, im->sw_interfaces,
2815                   ({
2816                     if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
2817                         fib->index)
2818                       vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2819                   }));
2820
2821     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2822       sw_if_index = sw_if_indices_to_shut[i];
2823       // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
2824
2825       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2826       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2827       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2828     }
2829
2830     fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
2831     fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_INTERFACE);
2832
2833     rv = 0;
2834     break;
2835   })); /* pool_foreach (fib) */
2836   /* *INDENT-ON* */
2837
2838   dsunlock (sm);
2839   return rv;
2840 }
2841
2842 static void
2843 vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2844 {
2845   int rv;
2846   vl_api_reset_fib_reply_t *rmp;
2847
2848   if (mp->is_ipv6)
2849     rv = ip6_reset_fib_t_handler (mp);
2850   else
2851     rv = ip4_reset_fib_t_handler (mp);
2852
2853   REPLY_MACRO (VL_API_RESET_FIB_REPLY);
2854 }
2855
2856
2857 static void
2858 dhcpv4_proxy_config (vl_api_dhcp_proxy_config_t * mp)
2859 {
2860   vl_api_dhcp_proxy_config_reply_t *rmp;
2861   int rv;
2862
2863   rv = dhcp_proxy_set_server ((ip4_address_t *) (&mp->dhcp_server),
2864                               (ip4_address_t *) (&mp->dhcp_src_address),
2865                               (u32) ntohl (mp->vrf_id),
2866                               (int) mp->insert_circuit_id,
2867                               (int) (mp->is_add == 0));
2868
2869   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
2870 }
2871
2872
2873 static void
2874 dhcpv6_proxy_config (vl_api_dhcp_proxy_config_t * mp)
2875 {
2876   vl_api_dhcp_proxy_config_reply_t *rmp;
2877   int rv = -1;
2878
2879   rv = dhcpv6_proxy_set_server ((ip6_address_t *) (&mp->dhcp_server),
2880                                 (ip6_address_t *) (&mp->dhcp_src_address),
2881                                 (u32) ntohl (mp->vrf_id),
2882                                 (int) mp->insert_circuit_id,
2883                                 (int) (mp->is_add == 0));
2884
2885   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
2886 }
2887
2888 static void
2889 dhcpv4_proxy_config_2 (vl_api_dhcp_proxy_config_2_t * mp)
2890 {
2891   vl_api_dhcp_proxy_config_reply_t *rmp;
2892   int rv;
2893
2894   rv = dhcp_proxy_set_server_2 ((ip4_address_t *) (&mp->dhcp_server),
2895                                 (ip4_address_t *) (&mp->dhcp_src_address),
2896                                 (u32) ntohl (mp->rx_vrf_id),
2897                                 (u32) ntohl (mp->server_vrf_id),
2898                                 (int) mp->insert_circuit_id,
2899                                 (int) (mp->is_add == 0));
2900
2901   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_2_REPLY);
2902 }
2903
2904
2905 static void
2906 dhcpv6_proxy_config_2 (vl_api_dhcp_proxy_config_2_t * mp)
2907 {
2908   vl_api_dhcp_proxy_config_reply_t *rmp;
2909   int rv = -1;
2910
2911   rv = dhcpv6_proxy_set_server_2 ((ip6_address_t *) (&mp->dhcp_server),
2912                                   (ip6_address_t *) (&mp->dhcp_src_address),
2913                                   (u32) ntohl (mp->rx_vrf_id),
2914                                   (u32) ntohl (mp->server_vrf_id),
2915                                   (int) mp->insert_circuit_id,
2916                                   (int) (mp->is_add == 0));
2917
2918   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_2_REPLY);
2919 }
2920
2921
2922 static void
2923 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t * mp)
2924 {
2925   vl_api_dhcp_proxy_set_vss_reply_t *rmp;
2926   int rv;
2927   if (!mp->is_ipv6)
2928     rv = dhcp_proxy_set_option82_vss (ntohl (mp->tbl_id),
2929                                       ntohl (mp->oui),
2930                                       ntohl (mp->fib_id),
2931                                       (int) mp->is_add == 0);
2932   else
2933     rv = dhcpv6_proxy_set_vss (ntohl (mp->tbl_id),
2934                                ntohl (mp->oui),
2935                                ntohl (mp->fib_id), (int) mp->is_add == 0);
2936
2937   REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
2938 }
2939
2940
2941 static void vl_api_dhcp_proxy_config_t_handler
2942   (vl_api_dhcp_proxy_config_t * mp)
2943 {
2944   if (mp->is_ipv6 == 0)
2945     dhcpv4_proxy_config (mp);
2946   else
2947     dhcpv6_proxy_config (mp);
2948 }
2949
2950 static void vl_api_dhcp_proxy_config_2_t_handler
2951   (vl_api_dhcp_proxy_config_2_t * mp)
2952 {
2953   if (mp->is_ipv6 == 0)
2954     dhcpv4_proxy_config_2 (mp);
2955   else
2956     dhcpv6_proxy_config_2 (mp);
2957 }
2958
2959 void
2960 dhcp_compl_event_callback (u32 client_index, u32 pid, u8 * hostname,
2961                            u8 is_ipv6, u8 * host_address, u8 * router_address,
2962                            u8 * host_mac)
2963 {
2964   unix_shared_memory_queue_t *q;
2965   vl_api_dhcp_compl_event_t *mp;
2966
2967   q = vl_api_client_index_to_input_queue (client_index);
2968   if (!q)
2969     return;
2970
2971   mp = vl_msg_api_alloc (sizeof (*mp));
2972   mp->client_index = client_index;
2973   mp->pid = pid;
2974   mp->is_ipv6 = is_ipv6;
2975   clib_memcpy (&mp->hostname, hostname, vec_len (hostname));
2976   mp->hostname[vec_len (hostname) + 1] = '\n';
2977   clib_memcpy (&mp->host_address[0], host_address, 16);
2978   clib_memcpy (&mp->router_address[0], router_address, 16);
2979
2980   if (NULL != host_mac)
2981     clib_memcpy (&mp->host_mac[0], host_mac, 6);
2982
2983   mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
2984
2985   vl_msg_api_send_shmem (q, (u8 *) & mp);
2986 }
2987
2988 static void vl_api_dhcp_client_config_t_handler
2989   (vl_api_dhcp_client_config_t * mp)
2990 {
2991   vlib_main_t *vm = vlib_get_main ();
2992   vl_api_dhcp_client_config_reply_t *rmp;
2993   int rv = 0;
2994
2995   VALIDATE_SW_IF_INDEX (mp);
2996
2997   rv = dhcp_client_config (vm, ntohl (mp->sw_if_index),
2998                            mp->hostname, mp->is_add, mp->client_index,
2999                            mp->want_dhcp_event ? dhcp_compl_event_callback :
3000                            NULL, mp->pid);
3001
3002   BAD_SW_IF_INDEX_LABEL;
3003
3004   REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
3005 }
3006
3007 static void
3008   vl_api_sw_interface_ip6nd_ra_config_t_handler
3009   (vl_api_sw_interface_ip6nd_ra_config_t * mp)
3010 {
3011   vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
3012   vlib_main_t *vm = vlib_get_main ();
3013   int rv = 0;
3014   u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
3015     default_router;
3016
3017   is_no = mp->is_no == 1;
3018   suppress = mp->suppress == 1;
3019   managed = mp->managed == 1;
3020   other = mp->other == 1;
3021   ll_option = mp->ll_option == 1;
3022   send_unicast = mp->send_unicast == 1;
3023   cease = mp->cease == 1;
3024   default_router = mp->default_router == 1;
3025
3026   VALIDATE_SW_IF_INDEX (mp);
3027
3028   rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
3029                                suppress, managed, other,
3030                                ll_option, send_unicast, cease,
3031                                default_router, ntohl (mp->lifetime),
3032                                ntohl (mp->initial_count),
3033                                ntohl (mp->initial_interval),
3034                                ntohl (mp->max_interval),
3035                                ntohl (mp->min_interval), is_no);
3036
3037   BAD_SW_IF_INDEX_LABEL;
3038
3039   REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
3040 }
3041
3042 static void
3043   vl_api_sw_interface_ip6nd_ra_prefix_t_handler
3044   (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
3045 {
3046   vlib_main_t *vm = vlib_get_main ();
3047   vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
3048   int rv = 0;
3049   u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
3050
3051   VALIDATE_SW_IF_INDEX (mp);
3052
3053   is_no = mp->is_no == 1;
3054   use_default = mp->use_default == 1;
3055   no_advertise = mp->no_advertise == 1;
3056   off_link = mp->off_link == 1;
3057   no_autoconfig = mp->no_autoconfig == 1;
3058   no_onlink = mp->no_onlink == 1;
3059
3060   rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
3061                                (ip6_address_t *) mp->address,
3062                                mp->address_length, use_default,
3063                                ntohl (mp->val_lifetime),
3064                                ntohl (mp->pref_lifetime), no_advertise,
3065                                off_link, no_autoconfig, no_onlink, is_no);
3066
3067   BAD_SW_IF_INDEX_LABEL;
3068   REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
3069 }
3070
3071 static void
3072   vl_api_sw_interface_ip6_enable_disable_t_handler
3073   (vl_api_sw_interface_ip6_enable_disable_t * mp)
3074 {
3075   vlib_main_t *vm = vlib_get_main ();
3076   vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
3077   vnet_main_t *vnm = vnet_get_main ();
3078   int rv = 0;
3079   clib_error_t *error;
3080
3081   vnm->api_errno = 0;
3082
3083   VALIDATE_SW_IF_INDEX (mp);
3084
3085   error =
3086     (mp->enable == 1) ? enable_ip6_interface (vm,
3087                                               ntohl (mp->sw_if_index)) :
3088     disable_ip6_interface (vm, ntohl (mp->sw_if_index));
3089
3090   if (error)
3091     {
3092       clib_error_report (error);
3093       rv = VNET_API_ERROR_UNSPECIFIED;
3094     }
3095   else
3096     {
3097       rv = vnm->api_errno;
3098     }
3099
3100   BAD_SW_IF_INDEX_LABEL;
3101
3102   REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
3103 }
3104
3105 static void
3106   vl_api_sw_interface_ip6_set_link_local_address_t_handler
3107   (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
3108 {
3109   vlib_main_t *vm = vlib_get_main ();
3110   vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
3111   int rv = 0;
3112   clib_error_t *error;
3113   vnet_main_t *vnm = vnet_get_main ();
3114
3115   vnm->api_errno = 0;
3116
3117   VALIDATE_SW_IF_INDEX (mp);
3118
3119   error = set_ip6_link_local_address (vm,
3120                                       ntohl (mp->sw_if_index),
3121                                       (ip6_address_t *) mp->address,
3122                                       mp->address_length);
3123   if (error)
3124     {
3125       clib_error_report (error);
3126       rv = VNET_API_ERROR_UNSPECIFIED;
3127     }
3128   else
3129     {
3130       rv = vnm->api_errno;
3131     }
3132
3133   BAD_SW_IF_INDEX_LABEL;
3134
3135   REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
3136 }
3137
3138 static void
3139 set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
3140 {
3141   vl_api_set_ip_flow_hash_reply_t *rmp;
3142   int rv = VNET_API_ERROR_UNIMPLEMENTED;
3143
3144   clib_warning ("unimplemented...");
3145
3146   REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
3147 }
3148
3149 static void
3150 set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
3151 {
3152   vl_api_set_ip_flow_hash_reply_t *rmp;
3153   int rv;
3154   u32 table_id;
3155   flow_hash_config_t flow_hash_config = 0;
3156
3157   table_id = ntohl (mp->vrf_id);
3158
3159 #define _(a,b) if (mp->a) flow_hash_config |= b;
3160   foreach_flow_hash_bit;
3161 #undef _
3162
3163   rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
3164
3165   REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
3166 }
3167
3168
3169 static void
3170 vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
3171 {
3172   if (mp->is_ipv6 == 0)
3173     set_ip4_flow_hash (mp);
3174   else
3175     set_ip6_flow_hash (mp);
3176 }
3177
3178 static void vl_api_sw_interface_set_unnumbered_t_handler
3179   (vl_api_sw_interface_set_unnumbered_t * mp)
3180 {
3181   vl_api_sw_interface_set_unnumbered_reply_t *rmp;
3182   int rv = 0;
3183   vnet_sw_interface_t *si;
3184   vnet_main_t *vnm = vnet_get_main ();
3185   u32 sw_if_index, unnumbered_sw_if_index;
3186
3187   sw_if_index = ntohl (mp->sw_if_index);
3188   unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
3189
3190   /*
3191    * The API message field names are backwards from
3192    * the underlying data structure names.
3193    * It's not worth changing them now.
3194    */
3195   if (pool_is_free_index (vnm->interface_main.sw_interfaces,
3196                           unnumbered_sw_if_index))
3197     {
3198       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
3199       goto done;
3200     }
3201
3202   /* Only check the "use loop0" field when setting the binding */
3203   if (mp->is_add &&
3204       pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
3205     {
3206       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
3207       goto done;
3208     }
3209
3210   si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
3211
3212   if (mp->is_add)
3213     {
3214       si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
3215       si->unnumbered_sw_if_index = sw_if_index;
3216       ip4_sw_interface_enable_disable (unnumbered_sw_if_index, 1);
3217       ip6_sw_interface_enable_disable (unnumbered_sw_if_index, 1);
3218     }
3219   else
3220     {
3221       si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
3222       si->unnumbered_sw_if_index = (u32) ~ 0;
3223       ip4_sw_interface_enable_disable (unnumbered_sw_if_index, 0);
3224       ip6_sw_interface_enable_disable (unnumbered_sw_if_index, 0);
3225     }
3226
3227 done:
3228   REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
3229 }
3230
3231 static void
3232 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
3233 {
3234   vl_api_create_loopback_reply_t *rmp;
3235   u32 sw_if_index;
3236   int rv;
3237
3238   rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address);
3239
3240   /* *INDENT-OFF* */
3241   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
3242   ({
3243     rmp->sw_if_index = ntohl (sw_if_index);
3244   }));
3245   /* *INDENT-ON* */
3246 }
3247
3248 static void
3249 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
3250 {
3251   vl_api_delete_loopback_reply_t *rmp;
3252   u32 sw_if_index;
3253   int rv;
3254
3255   sw_if_index = ntohl (mp->sw_if_index);
3256   rv = vnet_delete_loopback_interface (sw_if_index);
3257
3258   REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
3259 }
3260
3261 static void
3262 vl_api_control_ping_t_handler (vl_api_control_ping_t * mp)
3263 {
3264   vl_api_control_ping_reply_t *rmp;
3265   int rv = 0;
3266
3267   /* *INDENT-OFF* */
3268   REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
3269   ({
3270     rmp->vpe_pid = ntohl (getpid());
3271   }));
3272   /* *INDENT-ON* */
3273 }
3274
3275 static void
3276 shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
3277 {
3278   u8 **shmem_vecp = (u8 **) arg;
3279   u8 *shmem_vec;
3280   void *oldheap;
3281   api_main_t *am = &api_main;
3282   u32 offset;
3283
3284   shmem_vec = *shmem_vecp;
3285
3286   offset = vec_len (shmem_vec);
3287
3288   pthread_mutex_lock (&am->vlib_rp->mutex);
3289   oldheap = svm_push_data_heap (am->vlib_rp);
3290
3291   vec_validate (shmem_vec, offset + buffer_bytes - 1);
3292
3293   clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
3294
3295   svm_pop_heap (oldheap);
3296   pthread_mutex_unlock (&am->vlib_rp->mutex);
3297
3298   *shmem_vecp = shmem_vec;
3299 }
3300
3301
3302 static void
3303 vl_api_cli_request_t_handler (vl_api_cli_request_t * mp)
3304 {
3305   vl_api_cli_reply_t *rp;
3306   unix_shared_memory_queue_t *q;
3307   vlib_main_t *vm = vlib_get_main ();
3308   api_main_t *am = &api_main;
3309   unformat_input_t input;
3310   u8 *shmem_vec = 0;
3311   void *oldheap;
3312
3313   q = vl_api_client_index_to_input_queue (mp->client_index);
3314   if (!q)
3315     return;
3316
3317   rp = vl_msg_api_alloc (sizeof (*rp));
3318   rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY);
3319   rp->context = mp->context;
3320
3321   unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
3322
3323   vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
3324
3325   pthread_mutex_lock (&am->vlib_rp->mutex);
3326   oldheap = svm_push_data_heap (am->vlib_rp);
3327
3328   vec_add1 (shmem_vec, 0);
3329
3330   svm_pop_heap (oldheap);
3331   pthread_mutex_unlock (&am->vlib_rp->mutex);
3332
3333   rp->reply_in_shmem = (uword) shmem_vec;
3334
3335   vl_msg_api_send_shmem (q, (u8 *) & rp);
3336 }
3337
3338 static void
3339 inband_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
3340 {
3341   u8 **mem_vecp = (u8 **) arg;
3342   u8 *mem_vec = *mem_vecp;
3343   u32 offset = vec_len (mem_vec);
3344
3345   vec_validate (mem_vec, offset + buffer_bytes - 1);
3346   clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
3347   *mem_vecp = mem_vec;
3348 }
3349
3350 static void
3351 vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
3352 {
3353   vl_api_cli_inband_reply_t *rmp;
3354   int rv = 0;
3355   unix_shared_memory_queue_t *q;
3356   vlib_main_t *vm = vlib_get_main ();
3357   unformat_input_t input;
3358   u8 *out_vec = 0;
3359
3360   q = vl_api_client_index_to_input_queue (mp->client_index);
3361   if (!q)
3362     return;
3363
3364   unformat_init_string (&input, (char *) mp->cmd, ntohl (mp->length));
3365   vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
3366
3367   u32 len = vec_len (out_vec);
3368   /* *INDENT-OFF* */
3369   REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, len,
3370   ({
3371     rmp->length = htonl (len);
3372     clib_memcpy (rmp->reply, out_vec, len);
3373   }));
3374   /* *INDENT-ON* */
3375   vec_free (out_vec);
3376 }
3377
3378 static void
3379 vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
3380 {
3381   int rv;
3382   vl_api_set_arp_neighbor_limit_reply_t *rmp;
3383   vnet_main_t *vnm = vnet_get_main ();
3384   clib_error_t *error;
3385
3386   vnm->api_errno = 0;
3387
3388   if (mp->is_ipv6)
3389     error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
3390   else
3391     error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
3392
3393   if (error)
3394     {
3395       clib_error_report (error);
3396       rv = VNET_API_ERROR_UNSPECIFIED;
3397     }
3398   else
3399     {
3400       rv = vnm->api_errno;
3401     }
3402
3403   REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3404 }
3405
3406 static void vl_api_sr_tunnel_add_del_t_handler
3407   (vl_api_sr_tunnel_add_del_t * mp)
3408 {
3409 #if IP6SR == 0
3410   clib_warning ("unimplemented");
3411 #else
3412   ip6_sr_add_del_tunnel_args_t _a, *a = &_a;
3413   int rv = 0;
3414   vl_api_sr_tunnel_add_del_reply_t *rmp;
3415   ip6_address_t *segments = 0, *seg;
3416   ip6_address_t *tags = 0, *tag;
3417   ip6_address_t *this_address;
3418   int i;
3419
3420   if (mp->n_segments == 0)
3421     {
3422       rv = -11;
3423       goto out;
3424     }
3425
3426   memset (a, 0, sizeof (*a));
3427   a->src_address = (ip6_address_t *) & mp->src_address;
3428   a->dst_address = (ip6_address_t *) & mp->dst_address;
3429   a->dst_mask_width = mp->dst_mask_width;
3430   a->flags_net_byte_order = mp->flags_net_byte_order;
3431   a->is_del = (mp->is_add == 0);
3432   a->rx_table_id = ntohl (mp->outer_vrf_id);
3433   a->tx_table_id = ntohl (mp->inner_vrf_id);
3434
3435   a->name = format (0, "%s", mp->name);
3436   if (!(vec_len (a->name)))
3437     a->name = 0;
3438
3439   a->policy_name = format (0, "%s", mp->policy_name);
3440   if (!(vec_len (a->policy_name)))
3441     a->policy_name = 0;
3442
3443   /* Yank segments and tags out of the API message */
3444   this_address = (ip6_address_t *) mp->segs_and_tags;
3445   for (i = 0; i < mp->n_segments; i++)
3446     {
3447       vec_add2 (segments, seg, 1);
3448       clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
3449       this_address++;
3450     }
3451   for (i = 0; i < mp->n_tags; i++)
3452     {
3453       vec_add2 (tags, tag, 1);
3454       clib_memcpy (tag->as_u8, this_address->as_u8, sizeof (*this_address));
3455       this_address++;
3456     }
3457
3458   a->segments = segments;
3459   a->tags = tags;
3460
3461   rv = ip6_sr_add_del_tunnel (a);
3462
3463 out:
3464
3465   REPLY_MACRO (VL_API_SR_TUNNEL_ADD_DEL_REPLY);
3466 #endif
3467 }
3468
3469 static void vl_api_sr_policy_add_del_t_handler
3470   (vl_api_sr_policy_add_del_t * mp)
3471 {
3472 #if IP6SR == 0
3473   clib_warning ("unimplemented");
3474 #else
3475   ip6_sr_add_del_policy_args_t _a, *a = &_a;
3476   int rv = 0;
3477   vl_api_sr_policy_add_del_reply_t *rmp;
3478   int i;
3479
3480   memset (a, 0, sizeof (*a));
3481   a->is_del = (mp->is_add == 0);
3482
3483   a->name = format (0, "%s", mp->name);
3484   if (!(vec_len (a->name)))
3485     {
3486       rv = VNET_API_ERROR_NO_SUCH_NODE2;
3487       goto out;
3488     }
3489
3490   if (!(mp->tunnel_names[0]))
3491     {
3492       rv = VNET_API_ERROR_NO_SUCH_NODE2;
3493       goto out;
3494     }
3495
3496   // start deserializing tunnel_names
3497   int num_tunnels = mp->tunnel_names[0];        //number of tunnels
3498   u8 *deser_tun_names = mp->tunnel_names;
3499   deser_tun_names += 1;         //moving along
3500
3501   u8 *tun_name = 0;
3502   int tun_name_len = 0;
3503
3504   for (i = 0; i < num_tunnels; i++)
3505     {
3506       tun_name_len = *deser_tun_names;
3507       deser_tun_names += 1;
3508       vec_resize (tun_name, tun_name_len);
3509       memcpy (tun_name, deser_tun_names, tun_name_len);
3510       vec_add1 (a->tunnel_names, tun_name);
3511       deser_tun_names += tun_name_len;
3512       tun_name = 0;
3513     }
3514
3515   rv = ip6_sr_add_del_policy (a);
3516
3517 out:
3518
3519   REPLY_MACRO (VL_API_SR_POLICY_ADD_DEL_REPLY);
3520 #endif
3521 }
3522
3523 static void vl_api_sr_multicast_map_add_del_t_handler
3524   (vl_api_sr_multicast_map_add_del_t * mp)
3525 {
3526 #if IP6SR == 0
3527   clib_warning ("unimplemented");
3528 #else
3529   ip6_sr_add_del_multicastmap_args_t _a, *a = &_a;
3530   int rv = 0;
3531   vl_api_sr_multicast_map_add_del_reply_t *rmp;
3532
3533   memset (a, 0, sizeof (*a));
3534   a->is_del = (mp->is_add == 0);
3535
3536   a->multicast_address = (ip6_address_t *) & mp->multicast_address;
3537   a->policy_name = format (0, "%s", mp->policy_name);
3538
3539   if (a->multicast_address == 0)
3540     {
3541       rv = -1;
3542       goto out;
3543     }
3544
3545   if (!(a->policy_name))
3546     {
3547       rv = -2;
3548       goto out;
3549     }
3550
3551 #if DPDK > 0                    /* Cannot call replicate without DPDK */
3552   rv = ip6_sr_add_del_multicastmap (a);
3553 #else
3554   clib_warning ("multicast replication without DPDK not implemented");
3555   rv = VNET_API_ERROR_UNIMPLEMENTED;
3556 #endif /* DPDK */
3557
3558 out:
3559
3560   REPLY_MACRO (VL_API_SR_MULTICAST_MAP_ADD_DEL_REPLY);
3561 #endif
3562 }
3563
3564 #define foreach_classify_add_del_table_field    \
3565 _(table_index)                                  \
3566 _(nbuckets)                                     \
3567 _(memory_size)                                  \
3568 _(skip_n_vectors)                               \
3569 _(match_n_vectors)                              \
3570 _(next_table_index)                             \
3571 _(miss_next_index)                              \
3572 _(current_data_flag)                            \
3573 _(current_data_offset)
3574
3575 static void vl_api_classify_add_del_table_t_handler
3576   (vl_api_classify_add_del_table_t * mp)
3577 {
3578   vl_api_classify_add_del_table_reply_t *rmp;
3579   vnet_classify_main_t *cm = &vnet_classify_main;
3580   vnet_classify_table_t *t;
3581   int rv;
3582
3583 #define _(a) u32 a;
3584   foreach_classify_add_del_table_field;
3585 #undef _
3586
3587 #define _(a) a = ntohl(mp->a);
3588   foreach_classify_add_del_table_field;
3589 #undef _
3590
3591   /* The underlying API fails silently, on purpose, so check here */
3592   if (mp->is_add == 0)          /* delete */
3593     {
3594       if (pool_is_free_index (cm->tables, table_index))
3595         {
3596           rv = VNET_API_ERROR_NO_SUCH_TABLE;
3597           goto out;
3598         }
3599     }
3600   else                          /* add or update */
3601     {
3602       if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3603         table_index = ~0;
3604     }
3605
3606   rv = vnet_classify_add_del_table
3607     (cm, mp->mask, nbuckets, memory_size,
3608      skip_n_vectors, match_n_vectors,
3609      next_table_index, miss_next_index, &table_index,
3610      current_data_flag, current_data_offset, mp->is_add);
3611
3612 out:
3613   /* *INDENT-OFF* */
3614   REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
3615   ({
3616     if (rv == 0 && mp->is_add)
3617       {
3618         t = pool_elt_at_index (cm->tables, table_index);
3619         rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
3620         rmp->match_n_vectors = ntohl(t->match_n_vectors);
3621         rmp->new_table_index = ntohl(table_index);
3622       }
3623     else
3624       {
3625         rmp->skip_n_vectors = ~0;
3626         rmp->match_n_vectors = ~0;
3627         rmp->new_table_index = ~0;
3628       }
3629   }));
3630   /* *INDENT-ON* */
3631 }
3632
3633 static void vl_api_classify_add_del_session_t_handler
3634   (vl_api_classify_add_del_session_t * mp)
3635 {
3636   vnet_classify_main_t *cm = &vnet_classify_main;
3637   vl_api_classify_add_del_session_reply_t *rmp;
3638   int rv;
3639   u32 table_index, hit_next_index, opaque_index, metadata;
3640   i32 advance;
3641   u8 action;
3642
3643   table_index = ntohl (mp->table_index);
3644   hit_next_index = ntohl (mp->hit_next_index);
3645   opaque_index = ntohl (mp->opaque_index);
3646   advance = ntohl (mp->advance);
3647   action = mp->action;
3648   metadata = ntohl (mp->metadata);
3649
3650   rv = vnet_classify_add_del_session
3651     (cm, table_index, mp->match, hit_next_index, opaque_index,
3652      advance, action, metadata, mp->is_add);
3653
3654   REPLY_MACRO (VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
3655 }
3656
3657 static void vl_api_classify_set_interface_ip_table_t_handler
3658   (vl_api_classify_set_interface_ip_table_t * mp)
3659 {
3660   vlib_main_t *vm = vlib_get_main ();
3661   vl_api_classify_set_interface_ip_table_reply_t *rmp;
3662   int rv;
3663   u32 table_index, sw_if_index;
3664
3665   table_index = ntohl (mp->table_index);
3666   sw_if_index = ntohl (mp->sw_if_index);
3667
3668   VALIDATE_SW_IF_INDEX (mp);
3669
3670   if (mp->is_ipv6)
3671     rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3672   else
3673     rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
3674
3675   BAD_SW_IF_INDEX_LABEL;
3676
3677   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
3678 }
3679
3680 static void vl_api_classify_set_interface_l2_tables_t_handler
3681   (vl_api_classify_set_interface_l2_tables_t * mp)
3682 {
3683   vl_api_classify_set_interface_l2_tables_reply_t *rmp;
3684   int rv;
3685   u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
3686   int enable;
3687
3688   ip4_table_index = ntohl (mp->ip4_table_index);
3689   ip6_table_index = ntohl (mp->ip6_table_index);
3690   other_table_index = ntohl (mp->other_table_index);
3691   sw_if_index = ntohl (mp->sw_if_index);
3692
3693   VALIDATE_SW_IF_INDEX (mp);
3694
3695   if (mp->is_input)
3696     rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
3697                                             ip6_table_index,
3698                                             other_table_index);
3699   else
3700     rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
3701                                              ip6_table_index,
3702                                              other_table_index);
3703
3704   if (rv == 0)
3705     {
3706       if (ip4_table_index != ~0 || ip6_table_index != ~0
3707           || other_table_index != ~0)
3708         enable = 1;
3709       else
3710         enable = 0;
3711
3712       if (mp->is_input)
3713         vnet_l2_input_classify_enable_disable (sw_if_index, enable);
3714       else
3715         vnet_l2_output_classify_enable_disable (sw_if_index, enable);
3716     }
3717
3718   BAD_SW_IF_INDEX_LABEL;
3719
3720   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
3721 }
3722
3723 static void
3724 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
3725 {
3726   int rv = 0;
3727   vl_api_l2_fib_clear_table_reply_t *rmp;
3728
3729   /* DAW-FIXME: This API should only clear non-static l2fib entries, but
3730    *            that is not currently implemented.  When that TODO is fixed
3731    *            this call should be changed to pass 1 instead of 0.
3732    */
3733   l2fib_clear_table (0);
3734
3735   REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
3736 }
3737
3738 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
3739                                      u32 sw_if_index, u32 enable);
3740
3741 static void
3742 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
3743                                           mp)
3744 {
3745   int rv;
3746   vl_api_l2_interface_efp_filter_reply_t *rmp;
3747   vnet_main_t *vnm = vnet_get_main ();
3748
3749   // enable/disable the feature
3750   l2_efp_filter_configure (vnm, mp->sw_if_index, mp->enable_disable);
3751   rv = vnm->api_errno;
3752
3753   REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
3754 }
3755
3756 static void
3757   vl_api_l2_interface_vlan_tag_rewrite_t_handler
3758   (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
3759 {
3760   int rv = 0;
3761   vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
3762   vnet_main_t *vnm = vnet_get_main ();
3763   vlib_main_t *vm = vlib_get_main ();
3764   u32 vtr_op;
3765
3766   VALIDATE_SW_IF_INDEX (mp);
3767
3768   vtr_op = ntohl (mp->vtr_op);
3769
3770   /* The L2 code is unsuspicious */
3771   switch (vtr_op)
3772     {
3773     case L2_VTR_DISABLED:
3774     case L2_VTR_PUSH_1:
3775     case L2_VTR_PUSH_2:
3776     case L2_VTR_POP_1:
3777     case L2_VTR_POP_2:
3778     case L2_VTR_TRANSLATE_1_1:
3779     case L2_VTR_TRANSLATE_1_2:
3780     case L2_VTR_TRANSLATE_2_1:
3781     case L2_VTR_TRANSLATE_2_2:
3782       break;
3783
3784     default:
3785       rv = VNET_API_ERROR_INVALID_VALUE;
3786       goto bad_sw_if_index;
3787     }
3788
3789   rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
3790                         ntohl (mp->push_dot1q), ntohl (mp->tag1),
3791                         ntohl (mp->tag2));
3792
3793   BAD_SW_IF_INDEX_LABEL;
3794
3795   REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
3796 }
3797
3798 static void
3799 vl_api_create_vhost_user_if_t_handler (vl_api_create_vhost_user_if_t * mp)
3800 {
3801   int rv = 0;
3802   vl_api_create_vhost_user_if_reply_t *rmp;
3803   u32 sw_if_index = (u32) ~ 0;
3804   vnet_main_t *vnm = vnet_get_main ();
3805   vlib_main_t *vm = vlib_get_main ();
3806
3807   rv = vhost_user_create_if (vnm, vm, (char *) mp->sock_filename,
3808                              mp->is_server, &sw_if_index, (u64) ~ 0,
3809                              mp->renumber, ntohl (mp->custom_dev_instance),
3810                              (mp->use_custom_mac) ? mp->mac_address : NULL);
3811
3812   /* Remember an interface tag for the new interface */
3813   if (rv == 0)
3814     {
3815       /* If a tag was supplied... */
3816       if (mp->tag[0])
3817         {
3818           /* Make sure it's a proper C-string */
3819           mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
3820           u8 *tag = format (0, "%s%c", mp->tag, 0);
3821           vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
3822         }
3823     }
3824
3825   /* *INDENT-OFF* */
3826   REPLY_MACRO2(VL_API_CREATE_VHOST_USER_IF_REPLY,
3827   ({
3828     rmp->sw_if_index = ntohl (sw_if_index);
3829   }));
3830   /* *INDENT-ON* */
3831 }
3832
3833 static void
3834 vl_api_modify_vhost_user_if_t_handler (vl_api_modify_vhost_user_if_t * mp)
3835 {
3836   int rv = 0;
3837   vl_api_modify_vhost_user_if_reply_t *rmp;
3838   u32 sw_if_index = ntohl (mp->sw_if_index);
3839
3840   vnet_main_t *vnm = vnet_get_main ();
3841   vlib_main_t *vm = vlib_get_main ();
3842
3843   rv = vhost_user_modify_if (vnm, vm, (char *) mp->sock_filename,
3844                              mp->is_server, sw_if_index, (u64) ~ 0,
3845                              mp->renumber, ntohl (mp->custom_dev_instance));
3846
3847   REPLY_MACRO (VL_API_MODIFY_VHOST_USER_IF_REPLY);
3848 }
3849
3850 static void
3851 vl_api_delete_vhost_user_if_t_handler (vl_api_delete_vhost_user_if_t * mp)
3852 {
3853   int rv = 0;
3854   vl_api_delete_vhost_user_if_reply_t *rmp;
3855   vpe_api_main_t *vam = &vpe_api_main;
3856   u32 sw_if_index = ntohl (mp->sw_if_index);
3857
3858   vnet_main_t *vnm = vnet_get_main ();
3859   vlib_main_t *vm = vlib_get_main ();
3860
3861   rv = vhost_user_delete_if (vnm, vm, sw_if_index);
3862
3863   REPLY_MACRO (VL_API_DELETE_VHOST_USER_IF_REPLY);
3864   if (!rv)
3865     {
3866       unix_shared_memory_queue_t *q =
3867         vl_api_client_index_to_input_queue (mp->client_index);
3868       if (!q)
3869         return;
3870
3871       vnet_clear_sw_interface_tag (vnm, sw_if_index);
3872       send_sw_interface_flags_deleted (vam, q, sw_if_index);
3873     }
3874 }
3875
3876 static void
3877   vl_api_sw_interface_vhost_user_details_t_handler
3878   (vl_api_sw_interface_vhost_user_details_t * mp)
3879 {
3880   clib_warning ("BUG");
3881 }
3882
3883 static void
3884 send_sw_interface_vhost_user_details (vpe_api_main_t * am,
3885                                       unix_shared_memory_queue_t * q,
3886                                       vhost_user_intf_details_t * vui,
3887                                       u32 context)
3888 {
3889   vl_api_sw_interface_vhost_user_details_t *mp;
3890
3891   mp = vl_msg_api_alloc (sizeof (*mp));
3892   memset (mp, 0, sizeof (*mp));
3893   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_VHOST_USER_DETAILS);
3894   mp->sw_if_index = ntohl (vui->sw_if_index);
3895   mp->virtio_net_hdr_sz = ntohl (vui->virtio_net_hdr_sz);
3896   mp->features = clib_net_to_host_u64 (vui->features);
3897   mp->is_server = vui->is_server;
3898   mp->num_regions = ntohl (vui->num_regions);
3899   mp->sock_errno = ntohl (vui->sock_errno);
3900   mp->context = context;
3901
3902   strncpy ((char *) mp->sock_filename,
3903            (char *) vui->sock_filename, ARRAY_LEN (mp->sock_filename) - 1);
3904   strncpy ((char *) mp->interface_name,
3905            (char *) vui->if_name, ARRAY_LEN (mp->interface_name) - 1);
3906
3907   vl_msg_api_send_shmem (q, (u8 *) & mp);
3908 }
3909
3910 static void
3911   vl_api_sw_interface_vhost_user_dump_t_handler
3912   (vl_api_sw_interface_vhost_user_dump_t * mp)
3913 {
3914   int rv = 0;
3915   vpe_api_main_t *am = &vpe_api_main;
3916   vnet_main_t *vnm = vnet_get_main ();
3917   vlib_main_t *vm = vlib_get_main ();
3918   vhost_user_intf_details_t *ifaces = NULL;
3919   vhost_user_intf_details_t *vuid = NULL;
3920   unix_shared_memory_queue_t *q;
3921
3922   q = vl_api_client_index_to_input_queue (mp->client_index);
3923   if (q == 0)
3924     return;
3925
3926   rv = vhost_user_dump_ifs (vnm, vm, &ifaces);
3927   if (rv)
3928     return;
3929
3930   vec_foreach (vuid, ifaces)
3931   {
3932     send_sw_interface_vhost_user_details (am, q, vuid, mp->context);
3933   }
3934   vec_free (ifaces);
3935 }
3936
3937 static void
3938 send_sw_if_l2tpv3_tunnel_details (vpe_api_main_t * am,
3939                                   unix_shared_memory_queue_t * q,
3940                                   l2t_session_t * s,
3941                                   l2t_main_t * lm, u32 context)
3942 {
3943   vl_api_sw_if_l2tpv3_tunnel_details_t *mp;
3944   u8 *if_name = NULL;
3945   vnet_sw_interface_t *si = NULL;
3946
3947   si = vnet_get_hw_sw_interface (lm->vnet_main, s->hw_if_index);
3948
3949   if_name = format (if_name, "%U",
3950                     format_vnet_sw_interface_name, lm->vnet_main, si);
3951
3952   mp = vl_msg_api_alloc (sizeof (*mp));
3953   memset (mp, 0, sizeof (*mp));
3954   mp->_vl_msg_id = ntohs (VL_API_SW_IF_L2TPV3_TUNNEL_DETAILS);
3955   strncpy ((char *) mp->interface_name,
3956            (char *) if_name, ARRAY_LEN (mp->interface_name) - 1);
3957   mp->sw_if_index = ntohl (si->sw_if_index);
3958   mp->local_session_id = s->local_session_id;
3959   mp->remote_session_id = s->remote_session_id;
3960   mp->local_cookie[0] = s->local_cookie[0];
3961   mp->local_cookie[1] = s->local_cookie[1];
3962   mp->remote_cookie = s->remote_cookie;
3963   clib_memcpy (mp->client_address, &s->client_address,
3964                sizeof (s->client_address));
3965   clib_memcpy (mp->our_address, &s->our_address, sizeof (s->our_address));
3966   mp->l2_sublayer_present = s->l2_sublayer_present;
3967   mp->context = context;
3968
3969   vl_msg_api_send_shmem (q, (u8 *) & mp);
3970 }
3971
3972 static void
3973 send_ip_address_details (vpe_api_main_t * am,
3974                          unix_shared_memory_queue_t * q,
3975                          u8 * ip, u16 prefix_length, u8 is_ipv6, u32 context)
3976 {
3977   vl_api_ip_address_details_t *mp;
3978
3979   mp = vl_msg_api_alloc (sizeof (*mp));
3980   memset (mp, 0, sizeof (*mp));
3981   mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
3982
3983   if (is_ipv6)
3984     {
3985       clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
3986     }
3987   else
3988     {
3989       u32 *tp = (u32 *) mp->ip;
3990       *tp = *(u32 *) ip;
3991     }
3992   mp->prefix_length = prefix_length;
3993   mp->context = context;
3994
3995   vl_msg_api_send_shmem (q, (u8 *) & mp);
3996 }
3997
3998 static void
3999 vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
4000 {
4001   vpe_api_main_t *am = &vpe_api_main;
4002   unix_shared_memory_queue_t *q;
4003   ip6_address_t *r6;
4004   ip4_address_t *r4;
4005   ip6_main_t *im6 = &ip6_main;
4006   ip4_main_t *im4 = &ip4_main;
4007   ip_lookup_main_t *lm6 = &im6->lookup_main;
4008   ip_lookup_main_t *lm4 = &im4->lookup_main;
4009   ip_interface_address_t *ia = 0;
4010   u32 sw_if_index = ~0;
4011   int rv __attribute__ ((unused)) = 0;
4012
4013   VALIDATE_SW_IF_INDEX (mp);
4014
4015   sw_if_index = ntohl (mp->sw_if_index);
4016
4017   q = vl_api_client_index_to_input_queue (mp->client_index);
4018   if (q == 0)
4019     {
4020       return;
4021     }
4022
4023   if (mp->is_ipv6)
4024     {
4025       /* *INDENT-OFF* */
4026       foreach_ip_interface_address (lm6, ia, sw_if_index,
4027                                     1 /* honor unnumbered */,
4028       ({
4029         r6 = ip_interface_address_get_address (lm6, ia);
4030         u16 prefix_length = ia->address_length;
4031         send_ip_address_details(am, q, (u8*)r6, prefix_length, 1, mp->context);
4032       }));
4033       /* *INDENT-ON* */
4034     }
4035   else
4036     {
4037       /* *INDENT-OFF* */
4038       foreach_ip_interface_address (lm4, ia, sw_if_index,
4039                                     1 /* honor unnumbered */,
4040       ({
4041         r4 = ip_interface_address_get_address (lm4, ia);
4042         u16 prefix_length = ia->address_length;
4043         send_ip_address_details(am, q, (u8*)r4, prefix_length, 0, mp->context);
4044       }));
4045       /* *INDENT-ON* */
4046     }
4047   BAD_SW_IF_INDEX_LABEL;
4048 }
4049
4050 static void
4051 send_ip_details (vpe_api_main_t * am,
4052                  unix_shared_memory_queue_t * q, u32 sw_if_index, u32 context)
4053 {
4054   vl_api_ip_details_t *mp;
4055
4056   mp = vl_msg_api_alloc (sizeof (*mp));
4057   memset (mp, 0, sizeof (*mp));
4058   mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
4059
4060   mp->sw_if_index = ntohl (sw_if_index);
4061   mp->context = context;
4062
4063   vl_msg_api_send_shmem (q, (u8 *) & mp);
4064 }
4065
4066 static void
4067 vl_api_sw_if_l2tpv3_tunnel_dump_t_handler (vl_api_sw_if_l2tpv3_tunnel_dump_t *
4068                                            mp)
4069 {
4070   vpe_api_main_t *am = &vpe_api_main;
4071   l2t_main_t *lm = &l2t_main;
4072   unix_shared_memory_queue_t *q;
4073   l2t_session_t *session;
4074
4075   q = vl_api_client_index_to_input_queue (mp->client_index);
4076   if (q == 0)
4077     return;
4078
4079   /* *INDENT-OFF* */
4080   pool_foreach (session, lm->sessions,
4081   ({
4082     send_sw_if_l2tpv3_tunnel_details (am, q, session, lm, mp->context);
4083   }));
4084   /* *INDENT-ON* */
4085 }
4086
4087
4088 static void
4089 send_sw_interface_tap_details (vpe_api_main_t * am,
4090                                unix_shared_memory_queue_t * q,
4091                                tapcli_interface_details_t * tap_if,
4092                                u32 context)
4093 {
4094   vl_api_sw_interface_tap_details_t *mp;
4095   mp = vl_msg_api_alloc (sizeof (*mp));
4096   memset (mp, 0, sizeof (*mp));
4097   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_TAP_DETAILS);
4098   mp->sw_if_index = ntohl (tap_if->sw_if_index);
4099   strncpy ((char *) mp->dev_name,
4100            (char *) tap_if->dev_name, ARRAY_LEN (mp->dev_name) - 1);
4101   mp->context = context;
4102
4103   vl_msg_api_send_shmem (q, (u8 *) & mp);
4104 }
4105
4106 static void
4107 vl_api_sw_interface_tap_dump_t_handler (vl_api_sw_interface_tap_dump_t * mp)
4108 {
4109   int rv = 0;
4110   vpe_api_main_t *am = &vpe_api_main;
4111   unix_shared_memory_queue_t *q;
4112   tapcli_interface_details_t *tapifs = NULL;
4113   tapcli_interface_details_t *tap_if = NULL;
4114
4115   q = vl_api_client_index_to_input_queue (mp->client_index);
4116   if (q == 0)
4117     return;
4118
4119   rv = vnet_tap_dump_ifs (&tapifs);
4120   if (rv)
4121     return;
4122
4123   vec_foreach (tap_if, tapifs)
4124   {
4125     send_sw_interface_tap_details (am, q, tap_if, mp->context);
4126   }
4127
4128   vec_free (tapifs);
4129 }
4130
4131 static void
4132 vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
4133 {
4134   vpe_api_main_t *am = &vpe_api_main;
4135   vnet_main_t *vnm = vnet_get_main ();
4136   vlib_main_t *vm = vlib_get_main ();
4137   vnet_interface_main_t *im = &vnm->interface_main;
4138   unix_shared_memory_queue_t *q;
4139   vnet_sw_interface_t *si, *sorted_sis;
4140   u32 sw_if_index = ~0;
4141
4142   q = vl_api_client_index_to_input_queue (mp->client_index);
4143   if (q == 0)
4144     {
4145       return;
4146     }
4147
4148   /* Gather interfaces. */
4149   sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
4150   _vec_len (sorted_sis) = 0;
4151   /* *INDENT-OFF* */
4152   pool_foreach (si, im->sw_interfaces,
4153   ({
4154     vec_add1 (sorted_sis, si[0]);
4155   }));
4156   /* *INDENT-ON* */
4157
4158   vec_foreach (si, sorted_sis)
4159   {
4160     if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
4161       {
4162         if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
4163           {
4164             continue;
4165           }
4166         sw_if_index = si->sw_if_index;
4167         send_ip_details (am, q, sw_if_index, mp->context);
4168       }
4169   }
4170 }
4171
4172 static void
4173 vl_api_l2_fib_table_entry_t_handler (vl_api_l2_fib_table_entry_t * mp)
4174 {
4175   clib_warning ("BUG");
4176 }
4177
4178 static void
4179 send_l2fib_table_entry (vpe_api_main_t * am,
4180                         unix_shared_memory_queue_t * q,
4181                         l2fib_entry_key_t * l2fe_key,
4182                         l2fib_entry_result_t * l2fe_res, u32 context)
4183 {
4184   vl_api_l2_fib_table_entry_t *mp;
4185
4186   mp = vl_msg_api_alloc (sizeof (*mp));
4187   memset (mp, 0, sizeof (*mp));
4188   mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_ENTRY);
4189
4190   mp->bd_id =
4191     ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
4192
4193   mp->mac = l2fib_make_key (l2fe_key->fields.mac, 0);
4194   mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
4195   mp->static_mac = l2fe_res->fields.static_mac;
4196   mp->filter_mac = l2fe_res->fields.filter;
4197   mp->bvi_mac = l2fe_res->fields.bvi;
4198   mp->context = context;
4199
4200   vl_msg_api_send_shmem (q, (u8 *) & mp);
4201 }
4202
4203 static void
4204 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
4205 {
4206   vpe_api_main_t *am = &vpe_api_main;
4207   bd_main_t *bdm = &bd_main;
4208   l2fib_entry_key_t *l2fe_key = NULL;
4209   l2fib_entry_result_t *l2fe_res = NULL;
4210   u32 ni, bd_id = ntohl (mp->bd_id);
4211   u32 bd_index;
4212   unix_shared_memory_queue_t *q;
4213   uword *p;
4214
4215   q = vl_api_client_index_to_input_queue (mp->client_index);
4216   if (q == 0)
4217     return;
4218
4219   /* see l2fib_table_dump: ~0 means "any" */
4220   if (bd_id == ~0)
4221     bd_index = ~0;
4222   else
4223     {
4224       p = hash_get (bdm->bd_index_by_bd_id, bd_id);
4225       if (p == 0)
4226         return;
4227
4228       bd_index = p[0];
4229     }
4230
4231   l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
4232
4233   vec_foreach_index (ni, l2fe_key)
4234   {
4235     send_l2fib_table_entry (am, q, vec_elt_at_index (l2fe_key, ni),
4236                             vec_elt_at_index (l2fe_res, ni), mp->context);
4237   }
4238   vec_free (l2fe_key);
4239   vec_free (l2fe_res);
4240 }
4241
4242 static void
4243 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
4244 {
4245   vl_api_show_version_reply_t *rmp;
4246   int rv = 0;
4247   char *vpe_api_get_build_directory (void);
4248   char *vpe_api_get_version (void);
4249   char *vpe_api_get_build_date (void);
4250
4251   unix_shared_memory_queue_t *q =
4252     vl_api_client_index_to_input_queue (mp->client_index);
4253
4254   if (!q)
4255     return;
4256
4257   /* *INDENT-OFF* */
4258   REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
4259   ({
4260     strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
4261     strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
4262              ARRAY_LEN(rmp->build_directory)-1);
4263     strncpy ((char *) rmp->version, vpe_api_get_version(),
4264              ARRAY_LEN(rmp->version)-1);
4265     strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
4266              ARRAY_LEN(rmp->build_date)-1);
4267   }));
4268   /* *INDENT-ON* */
4269 }
4270
4271 static void
4272 vl_api_get_node_index_t_handler (vl_api_get_node_index_t * mp)
4273 {
4274   vlib_main_t *vm = vlib_get_main ();
4275   vl_api_get_node_index_reply_t *rmp;
4276   vlib_node_t *n;
4277   int rv = 0;
4278   u32 node_index = ~0;
4279
4280   n = vlib_get_node_by_name (vm, mp->node_name);
4281
4282   if (n == 0)
4283     rv = VNET_API_ERROR_NO_SUCH_NODE;
4284   else
4285     node_index = n->index;
4286
4287   /* *INDENT-OFF* */
4288   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
4289   ({
4290     rmp->node_index = ntohl(node_index);
4291   }));
4292   /* *INDENT-ON* */
4293 }
4294
4295 static void
4296 vl_api_get_next_index_t_handler (vl_api_get_next_index_t * mp)
4297 {
4298   vlib_main_t *vm = vlib_get_main ();
4299   vl_api_get_next_index_reply_t *rmp;
4300   vlib_node_t *node, *next_node;
4301   int rv = 0;
4302   u32 next_node_index = ~0, next_index = ~0;
4303   uword *p;
4304
4305   node = vlib_get_node_by_name (vm, mp->node_name);
4306
4307   if (node == 0)
4308     {
4309       rv = VNET_API_ERROR_NO_SUCH_NODE;
4310       goto out;
4311     }
4312
4313   next_node = vlib_get_node_by_name (vm, mp->next_name);
4314
4315   if (next_node == 0)
4316     {
4317       rv = VNET_API_ERROR_NO_SUCH_NODE2;
4318       goto out;
4319     }
4320   else
4321     next_node_index = next_node->index;
4322
4323   p = hash_get (node->next_slot_by_node, next_node_index);
4324
4325   if (p == 0)
4326     {
4327       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
4328       goto out;
4329     }
4330   else
4331     next_index = p[0];
4332
4333 out:
4334   /* *INDENT-OFF* */
4335   REPLY_MACRO2(VL_API_GET_NEXT_INDEX_REPLY,
4336   ({
4337     rmp->next_index = ntohl(next_index);
4338   }));
4339   /* *INDENT-ON* */
4340 }
4341
4342 static void
4343 vl_api_add_node_next_t_handler (vl_api_add_node_next_t * mp)
4344 {
4345   vlib_main_t *vm = vlib_get_main ();
4346   vl_api_add_node_next_reply_t *rmp;
4347   vlib_node_t *n, *next;
4348   int rv = 0;
4349   u32 next_index = ~0;
4350
4351   n = vlib_get_node_by_name (vm, mp->node_name);
4352
4353   if (n == 0)
4354     {
4355       rv = VNET_API_ERROR_NO_SUCH_NODE;
4356       goto out;
4357     }
4358
4359   next = vlib_get_node_by_name (vm, mp->next_name);
4360
4361   if (next == 0)
4362     rv = VNET_API_ERROR_NO_SUCH_NODE2;
4363   else
4364     next_index = vlib_node_add_next (vm, n->index, next->index);
4365
4366 out:
4367   /* *INDENT-OFF* */
4368   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
4369   ({
4370     rmp->next_index = ntohl(next_index);
4371   }));
4372   /* *INDENT-ON* */
4373 }
4374
4375 static void vl_api_l2tpv3_create_tunnel_t_handler
4376   (vl_api_l2tpv3_create_tunnel_t * mp)
4377 {
4378   vl_api_l2tpv3_create_tunnel_reply_t *rmp;
4379   l2t_main_t *lm = &l2t_main;
4380   u32 sw_if_index = (u32) ~ 0;
4381   int rv;
4382
4383   if (mp->is_ipv6 != 1)
4384     {
4385       rv = VNET_API_ERROR_UNIMPLEMENTED;
4386       goto out;
4387     }
4388
4389   u32 encap_fib_index;
4390
4391   if (mp->encap_vrf_id != ~0)
4392     {
4393       uword *p;
4394       ip6_main_t *im = &ip6_main;
4395       if (!
4396           (p =
4397            hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id))))
4398         {
4399           rv = VNET_API_ERROR_NO_SUCH_FIB;
4400           goto out;
4401         }
4402       encap_fib_index = p[0];
4403     }
4404   else
4405     {
4406       encap_fib_index = ~0;
4407     }
4408
4409   rv = create_l2tpv3_ipv6_tunnel (lm,
4410                                   (ip6_address_t *) mp->client_address,
4411                                   (ip6_address_t *) mp->our_address,
4412                                   ntohl (mp->local_session_id),
4413                                   ntohl (mp->remote_session_id),
4414                                   clib_net_to_host_u64 (mp->local_cookie),
4415                                   clib_net_to_host_u64 (mp->remote_cookie),
4416                                   mp->l2_sublayer_present,
4417                                   encap_fib_index, &sw_if_index);
4418
4419 out:
4420   /* *INDENT-OFF* */
4421   REPLY_MACRO2(VL_API_L2TPV3_CREATE_TUNNEL_REPLY,
4422   ({
4423     rmp->sw_if_index = ntohl (sw_if_index);
4424   }));
4425   /* *INDENT-ON* */
4426 }
4427
4428 static void vl_api_l2tpv3_set_tunnel_cookies_t_handler
4429   (vl_api_l2tpv3_set_tunnel_cookies_t * mp)
4430 {
4431   vl_api_l2tpv3_set_tunnel_cookies_reply_t *rmp;
4432   l2t_main_t *lm = &l2t_main;
4433   int rv;
4434
4435   VALIDATE_SW_IF_INDEX (mp);
4436
4437   rv = l2tpv3_set_tunnel_cookies (lm, ntohl (mp->sw_if_index),
4438                                   clib_net_to_host_u64 (mp->new_local_cookie),
4439                                   clib_net_to_host_u64
4440                                   (mp->new_remote_cookie));
4441
4442   BAD_SW_IF_INDEX_LABEL;
4443
4444   REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY);
4445 }
4446
4447 static void vl_api_l2tpv3_interface_enable_disable_t_handler
4448   (vl_api_l2tpv3_interface_enable_disable_t * mp)
4449 {
4450   int rv;
4451   vnet_main_t *vnm = vnet_get_main ();
4452   vl_api_l2tpv3_interface_enable_disable_reply_t *rmp;
4453
4454   VALIDATE_SW_IF_INDEX (mp);
4455
4456   rv = l2tpv3_interface_enable_disable
4457     (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
4458
4459   BAD_SW_IF_INDEX_LABEL;
4460
4461   REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY);
4462 }
4463
4464 static void vl_api_l2tpv3_set_lookup_key_t_handler
4465   (vl_api_l2tpv3_set_lookup_key_t * mp)
4466 {
4467   int rv = 0;
4468   l2t_main_t *lm = &l2t_main;
4469   vl_api_l2tpv3_set_lookup_key_reply_t *rmp;
4470
4471   if (mp->key > L2T_LOOKUP_SESSION_ID)
4472     {
4473       rv = VNET_API_ERROR_INVALID_VALUE;
4474       goto out;
4475     }
4476
4477   lm->lookup_type = mp->key;
4478
4479 out:
4480   REPLY_MACRO (VL_API_L2TPV3_SET_LOOKUP_KEY_REPLY);
4481 }
4482
4483 static void vl_api_vxlan_add_del_tunnel_t_handler
4484   (vl_api_vxlan_add_del_tunnel_t * mp)
4485 {
4486   vl_api_vxlan_add_del_tunnel_reply_t *rmp;
4487   int rv = 0;
4488   vnet_vxlan_add_del_tunnel_args_t _a, *a = &_a;
4489   u32 encap_fib_index;
4490   uword *p;
4491   ip4_main_t *im = &ip4_main;
4492   u32 sw_if_index = ~0;
4493
4494   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
4495   if (!p)
4496     {
4497       rv = VNET_API_ERROR_NO_SUCH_FIB;
4498       goto out;
4499     }
4500   encap_fib_index = p[0];
4501   memset (a, 0, sizeof (*a));
4502
4503   a->is_add = mp->is_add;
4504   a->is_ip6 = mp->is_ipv6;
4505
4506   /* ip addresses sent in network byte order */
4507   ip46_from_addr_buf (mp->is_ipv6, mp->dst_address, &a->dst);
4508   ip46_from_addr_buf (mp->is_ipv6, mp->src_address, &a->src);
4509
4510   /* Check src & dst are different */
4511   if (ip46_address_cmp (&a->dst, &a->src) == 0)
4512     {
4513       rv = VNET_API_ERROR_SAME_SRC_DST;
4514       goto out;
4515     }
4516   a->mcast_sw_if_index = ntohl (mp->mcast_sw_if_index);
4517   a->encap_fib_index = encap_fib_index;
4518   a->decap_next_index = ntohl (mp->decap_next_index);
4519   a->vni = ntohl (mp->vni);
4520   rv = vnet_vxlan_add_del_tunnel (a, &sw_if_index);
4521
4522 out:
4523   /* *INDENT-OFF* */
4524   REPLY_MACRO2(VL_API_VXLAN_ADD_DEL_TUNNEL_REPLY,
4525   ({
4526     rmp->sw_if_index = ntohl (sw_if_index);
4527   }));
4528   /* *INDENT-ON* */
4529 }
4530
4531 static void send_vxlan_tunnel_details
4532   (vxlan_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4533 {
4534   vl_api_vxlan_tunnel_details_t *rmp;
4535   ip4_main_t *im4 = &ip4_main;
4536   ip6_main_t *im6 = &ip6_main;
4537   u8 is_ipv6 = !ip46_address_is_ip4 (&t->dst);
4538
4539   rmp = vl_msg_api_alloc (sizeof (*rmp));
4540   memset (rmp, 0, sizeof (*rmp));
4541   rmp->_vl_msg_id = ntohs (VL_API_VXLAN_TUNNEL_DETAILS);
4542   if (is_ipv6)
4543     {
4544       memcpy (rmp->src_address, t->src.ip6.as_u8, 16);
4545       memcpy (rmp->dst_address, t->dst.ip6.as_u8, 16);
4546       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
4547     }
4548   else
4549     {
4550       memcpy (rmp->src_address, t->src.ip4.as_u8, 4);
4551       memcpy (rmp->dst_address, t->dst.ip4.as_u8, 4);
4552       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
4553     }
4554   rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
4555   rmp->vni = htonl (t->vni);
4556   rmp->decap_next_index = htonl (t->decap_next_index);
4557   rmp->sw_if_index = htonl (t->sw_if_index);
4558   rmp->is_ipv6 = is_ipv6;
4559   rmp->context = context;
4560
4561   vl_msg_api_send_shmem (q, (u8 *) & rmp);
4562 }
4563
4564 static void vl_api_vxlan_tunnel_dump_t_handler
4565   (vl_api_vxlan_tunnel_dump_t * mp)
4566 {
4567   unix_shared_memory_queue_t *q;
4568   vxlan_main_t *vxm = &vxlan_main;
4569   vxlan_tunnel_t *t;
4570   u32 sw_if_index;
4571
4572   q = vl_api_client_index_to_input_queue (mp->client_index);
4573   if (q == 0)
4574     {
4575       return;
4576     }
4577
4578   sw_if_index = ntohl (mp->sw_if_index);
4579
4580   if (~0 == sw_if_index)
4581     {
4582       /* *INDENT-OFF* */
4583       pool_foreach (t, vxm->tunnels,
4584       ({
4585         send_vxlan_tunnel_details(t, q, mp->context);
4586       }));
4587       /* *INDENT-ON* */
4588     }
4589   else
4590     {
4591       if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) ||
4592           (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index]))
4593         {
4594           return;
4595         }
4596       t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
4597       send_vxlan_tunnel_details (t, q, mp->context);
4598     }
4599 }
4600
4601 static void vl_api_gre_add_del_tunnel_t_handler
4602   (vl_api_gre_add_del_tunnel_t * mp)
4603 {
4604   vl_api_gre_add_del_tunnel_reply_t *rmp;
4605   int rv = 0;
4606   vnet_gre_add_del_tunnel_args_t _a, *a = &_a;
4607   u32 outer_fib_id;
4608   uword *p;
4609   ip4_main_t *im = &ip4_main;
4610   u32 sw_if_index = ~0;
4611
4612   p = hash_get (im->fib_index_by_table_id, ntohl (mp->outer_fib_id));
4613   if (!p)
4614     {
4615       rv = VNET_API_ERROR_NO_SUCH_FIB;
4616       goto out;
4617     }
4618   outer_fib_id = p[0];
4619
4620   /* Check src & dst are different */
4621   if ((mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 16) == 0) ||
4622       (!mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 4) == 0))
4623     {
4624       rv = VNET_API_ERROR_SAME_SRC_DST;
4625       goto out;
4626     }
4627   memset (a, 0, sizeof (*a));
4628
4629   a->is_add = mp->is_add;
4630   a->teb = mp->teb;
4631
4632   /* ip addresses sent in network byte order */
4633   clib_memcpy (&(a->src), mp->src_address, 4);
4634   clib_memcpy (&(a->dst), mp->dst_address, 4);
4635
4636   a->outer_fib_id = outer_fib_id;
4637   rv = vnet_gre_add_del_tunnel (a, &sw_if_index);
4638
4639 out:
4640   /* *INDENT-OFF* */
4641   REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY,
4642   ({
4643     rmp->sw_if_index = ntohl (sw_if_index);
4644   }));
4645   /* *INDENT-ON* */
4646 }
4647
4648 static void send_gre_tunnel_details
4649   (gre_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4650 {
4651   vl_api_gre_tunnel_details_t *rmp;
4652   ip4_main_t *im = &ip4_main;
4653
4654   rmp = vl_msg_api_alloc (sizeof (*rmp));
4655   memset (rmp, 0, sizeof (*rmp));
4656   rmp->_vl_msg_id = ntohs (VL_API_GRE_TUNNEL_DETAILS);
4657   clib_memcpy (rmp->src_address, &(t->tunnel_src), 4);
4658   clib_memcpy (rmp->dst_address, &(t->tunnel_dst), 4);
4659   rmp->outer_fib_id = htonl (im->fibs[t->outer_fib_index].ft_table_id);
4660   rmp->teb = (GRE_TUNNEL_TYPE_TEB == t->type);
4661   rmp->sw_if_index = htonl (t->sw_if_index);
4662   rmp->context = context;
4663
4664   vl_msg_api_send_shmem (q, (u8 *) & rmp);
4665 }
4666
4667 static void
4668 vl_api_gre_tunnel_dump_t_handler (vl_api_gre_tunnel_dump_t * mp)
4669 {
4670   unix_shared_memory_queue_t *q;
4671   gre_main_t *gm = &gre_main;
4672   gre_tunnel_t *t;
4673   u32 sw_if_index;
4674
4675   q = vl_api_client_index_to_input_queue (mp->client_index);
4676   if (q == 0)
4677     {
4678       return;
4679     }
4680
4681   sw_if_index = ntohl (mp->sw_if_index);
4682
4683   if (~0 == sw_if_index)
4684     {
4685       /* *INDENT-OFF* */
4686       pool_foreach (t, gm->tunnels,
4687       ({
4688         send_gre_tunnel_details(t, q, mp->context);
4689       }));
4690       /* *INDENT-ON* */
4691     }
4692   else
4693     {
4694       if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) ||
4695           (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
4696         {
4697           return;
4698         }
4699       t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
4700       send_gre_tunnel_details (t, q, mp->context);
4701     }
4702 }
4703
4704 static void
4705 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
4706 {
4707   extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
4708                                     int is_add);
4709   vl_api_l2_patch_add_del_reply_t *rmp;
4710   int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
4711                              int is_add);
4712   int rv = 0;
4713
4714   VALIDATE_RX_SW_IF_INDEX (mp);
4715   VALIDATE_TX_SW_IF_INDEX (mp);
4716
4717   rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
4718                               ntohl (mp->tx_sw_if_index),
4719                               (int) (mp->is_add != 0));
4720
4721   BAD_RX_SW_IF_INDEX_LABEL;
4722   BAD_TX_SW_IF_INDEX_LABEL;
4723
4724   REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
4725 }
4726
4727 static void
4728   vl_api_vxlan_gpe_add_del_tunnel_t_handler
4729   (vl_api_vxlan_gpe_add_del_tunnel_t * mp)
4730 {
4731   vl_api_vxlan_gpe_add_del_tunnel_reply_t *rmp;
4732   int rv = 0;
4733   vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
4734   u32 encap_fib_index, decap_fib_index;
4735   u8 protocol;
4736   uword *p;
4737   ip4_main_t *im = &ip4_main;
4738   u32 sw_if_index = ~0;
4739
4740
4741   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
4742   if (!p)
4743     {
4744       rv = VNET_API_ERROR_NO_SUCH_FIB;
4745       goto out;
4746     }
4747   encap_fib_index = p[0];
4748
4749   protocol = mp->protocol;
4750
4751   /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
4752   if (protocol == VXLAN_GPE_INPUT_NEXT_IP4_INPUT)
4753     {
4754       p = hash_get (im->fib_index_by_table_id, ntohl (mp->decap_vrf_id));
4755       if (!p)
4756         {
4757           rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
4758           goto out;
4759         }
4760       decap_fib_index = p[0];
4761     }
4762   else
4763     {
4764       decap_fib_index = ntohl (mp->decap_vrf_id);
4765     }
4766
4767   /* Check src & dst are different */
4768   if ((mp->is_ipv6 && memcmp (mp->local, mp->remote, 16) == 0) ||
4769       (!mp->is_ipv6 && memcmp (mp->local, mp->remote, 4) == 0))
4770     {
4771       rv = VNET_API_ERROR_SAME_SRC_DST;
4772       goto out;
4773     }
4774   memset (a, 0, sizeof (*a));
4775
4776   a->is_add = mp->is_add;
4777   a->is_ip6 = mp->is_ipv6;
4778   /* ip addresses sent in network byte order */
4779   if (a->is_ip6)
4780     {
4781       clib_memcpy (&(a->local.ip6), mp->local, 16);
4782       clib_memcpy (&(a->remote.ip6), mp->remote, 16);
4783     }
4784   else
4785     {
4786       clib_memcpy (&(a->local.ip4), mp->local, 4);
4787       clib_memcpy (&(a->remote.ip4), mp->remote, 4);
4788     }
4789   a->encap_fib_index = encap_fib_index;
4790   a->decap_fib_index = decap_fib_index;
4791   a->protocol = protocol;
4792   a->vni = ntohl (mp->vni);
4793   rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
4794
4795 out:
4796   /* *INDENT-OFF* */
4797   REPLY_MACRO2(VL_API_VXLAN_GPE_ADD_DEL_TUNNEL_REPLY,
4798   ({
4799     rmp->sw_if_index = ntohl (sw_if_index);
4800   }));
4801   /* *INDENT-ON* */
4802 }
4803
4804 static void send_vxlan_gpe_tunnel_details
4805   (vxlan_gpe_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4806 {
4807   vl_api_vxlan_gpe_tunnel_details_t *rmp;
4808   ip4_main_t *im4 = &ip4_main;
4809   ip6_main_t *im6 = &ip6_main;
4810   u8 is_ipv6 = !(t->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
4811
4812   rmp = vl_msg_api_alloc (sizeof (*rmp));
4813   memset (rmp, 0, sizeof (*rmp));
4814   rmp->_vl_msg_id = ntohs (VL_API_VXLAN_GPE_TUNNEL_DETAILS);
4815   if (is_ipv6)
4816     {
4817       memcpy (rmp->local, &(t->local.ip6), 16);
4818       memcpy (rmp->remote, &(t->remote.ip6), 16);
4819       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
4820       rmp->decap_vrf_id = htonl (im6->fibs[t->decap_fib_index].ft_table_id);
4821     }
4822   else
4823     {
4824       memcpy (rmp->local, &(t->local.ip4), 4);
4825       memcpy (rmp->remote, &(t->remote.ip4), 4);
4826       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
4827       rmp->decap_vrf_id = htonl (im4->fibs[t->decap_fib_index].ft_table_id);
4828     }
4829   rmp->vni = htonl (t->vni);
4830   rmp->protocol = t->protocol;
4831   rmp->sw_if_index = htonl (t->sw_if_index);
4832   rmp->is_ipv6 = is_ipv6;
4833   rmp->context = context;
4834
4835   vl_msg_api_send_shmem (q, (u8 *) & rmp);
4836 }
4837
4838 static void vl_api_vxlan_gpe_tunnel_dump_t_handler
4839   (vl_api_vxlan_gpe_tunnel_dump_t * mp)
4840 {
4841   unix_shared_memory_queue_t *q;
4842   vxlan_gpe_main_t *vgm = &vxlan_gpe_main;
4843   vxlan_gpe_tunnel_t *t;
4844   u32 sw_if_index;
4845
4846   q = vl_api_client_index_to_input_queue (mp->client_index);
4847   if (q == 0)
4848     {
4849       return;
4850     }
4851
4852   sw_if_index = ntohl (mp->sw_if_index);
4853
4854   if (~0 == sw_if_index)
4855     {
4856       /* *INDENT-OFF* */
4857       pool_foreach (t, vgm->tunnels,
4858       ({
4859         send_vxlan_gpe_tunnel_details(t, q, mp->context);
4860       }));
4861       /* *INDENT-ON* */
4862     }
4863   else
4864     {
4865       if ((sw_if_index >= vec_len (vgm->tunnel_index_by_sw_if_index)) ||
4866           (~0 == vgm->tunnel_index_by_sw_if_index[sw_if_index]))
4867         {
4868           return;
4869         }
4870       t = &vgm->tunnels[vgm->tunnel_index_by_sw_if_index[sw_if_index]];
4871       send_vxlan_gpe_tunnel_details (t, q, mp->context);
4872     }
4873 }
4874
4875 /** Used for transferring locators via VPP API */
4876 /* *INDENT-OFF* */
4877 typedef CLIB_PACKED (struct {
4878   u32 sw_if_index; /**< locator sw_if_index */
4879   u8 priority; /**< locator priority */
4880   u8 weight; /**< locator weight */
4881 }) ls_locator_t;
4882 /* *INDENT-ON* */
4883
4884 static void
4885 vl_api_lisp_add_del_locator_set_t_handler (vl_api_lisp_add_del_locator_set_t *
4886                                            mp)
4887 {
4888   vl_api_lisp_add_del_locator_set_reply_t *rmp;
4889   int rv = 0;
4890   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
4891   locator_t locator;
4892   ls_locator_t *ls_loc;
4893   u32 ls_index = ~0, locator_num;
4894   u8 *locator_name = NULL;
4895   int i;
4896
4897   memset (a, 0, sizeof (a[0]));
4898
4899   locator_name = format (0, "%s", mp->locator_set_name);
4900
4901   a->name = locator_name;
4902   a->is_add = mp->is_add;
4903   a->local = 1;
4904   locator_num = clib_net_to_host_u32 (mp->locator_num);
4905
4906   memset (&locator, 0, sizeof (locator));
4907   for (i = 0; i < locator_num; i++)
4908     {
4909       ls_loc = &((ls_locator_t *) mp->locators)[i];
4910       VALIDATE_SW_IF_INDEX (ls_loc);
4911
4912       locator.sw_if_index = htonl (ls_loc->sw_if_index);
4913       locator.priority = ls_loc->priority;
4914       locator.weight = ls_loc->weight;
4915       locator.local = 1;
4916       vec_add1 (a->locators, locator);
4917     }
4918
4919   rv = vnet_lisp_add_del_locator_set (a, &ls_index);
4920
4921   BAD_SW_IF_INDEX_LABEL;
4922
4923   vec_free (locator_name);
4924   vec_free (a->locators);
4925
4926   /* *INDENT-OFF* */
4927   REPLY_MACRO2 (VL_API_LISP_ADD_DEL_LOCATOR_SET_REPLY,
4928   ({
4929     rmp->ls_index = clib_host_to_net_u32 (ls_index);
4930   }));
4931   /* *INDENT-ON* */
4932 }
4933
4934 static void
4935 vl_api_lisp_add_del_locator_t_handler (vl_api_lisp_add_del_locator_t * mp)
4936 {
4937   vl_api_lisp_add_del_locator_reply_t *rmp;
4938   int rv = 0;
4939   locator_t locator, *locators = NULL;
4940   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
4941   u32 ls_index = ~0;
4942   u8 *locator_name = NULL;
4943
4944   memset (&locator, 0, sizeof (locator));
4945   memset (a, 0, sizeof (a[0]));
4946
4947   locator.sw_if_index = ntohl (mp->sw_if_index);
4948   locator.priority = mp->priority;
4949   locator.weight = mp->weight;
4950   locator.local = 1;
4951   vec_add1 (locators, locator);
4952
4953   locator_name = format (0, "%s", mp->locator_set_name);
4954
4955   a->name = locator_name;
4956   a->locators = locators;
4957   a->is_add = mp->is_add;
4958   a->local = 1;
4959
4960   rv = vnet_lisp_add_del_locator (a, NULL, &ls_index);
4961
4962   vec_free (locators);
4963   vec_free (locator_name);
4964
4965   REPLY_MACRO (VL_API_LISP_ADD_DEL_LOCATOR_REPLY);
4966 }
4967
4968 static int
4969 unformat_lisp_eid_api (gid_address_t * dst, u32 vni, u8 type, void *src,
4970                        u8 len)
4971 {
4972   switch (type)
4973     {
4974     case 0:                     /* ipv4 */
4975       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
4976       gid_address_ip_set (dst, src, IP4);
4977       gid_address_ippref_len (dst) = len;
4978       ip_prefix_normalize (&gid_address_ippref (dst));
4979       break;
4980     case 1:                     /* ipv6 */
4981       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
4982       gid_address_ip_set (dst, src, IP6);
4983       gid_address_ippref_len (dst) = len;
4984       ip_prefix_normalize (&gid_address_ippref (dst));
4985       break;
4986     case 2:                     /* l2 mac */
4987       gid_address_type (dst) = GID_ADDR_MAC;
4988       clib_memcpy (&gid_address_mac (dst), src, 6);
4989       break;
4990     default:
4991       /* unknown type */
4992       return VNET_API_ERROR_INVALID_VALUE;
4993     }
4994
4995   gid_address_vni (dst) = vni;
4996
4997   return 0;
4998 }
4999
5000 static void
5001 vl_api_lisp_add_del_local_eid_t_handler (vl_api_lisp_add_del_local_eid_t * mp)
5002 {
5003   vl_api_lisp_add_del_local_eid_reply_t *rmp;
5004   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5005   int rv = 0;
5006   gid_address_t _eid, *eid = &_eid;
5007   uword *p = NULL;
5008   u32 locator_set_index = ~0, map_index = ~0;
5009   vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
5010   u8 *name = NULL;
5011   memset (a, 0, sizeof (a[0]));
5012   memset (eid, 0, sizeof (eid[0]));
5013
5014   rv = unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
5015                               mp->eid_type, mp->eid, mp->prefix_len);
5016   if (rv)
5017     goto out;
5018
5019   name = format (0, "%s", mp->locator_set_name);
5020   p = hash_get_mem (lcm->locator_set_index_by_name, name);
5021   if (!p)
5022     {
5023       rv = VNET_API_ERROR_INVALID_VALUE;
5024       goto out;
5025     }
5026   locator_set_index = p[0];
5027
5028   /* XXX treat batch configuration */
5029   a->is_add = mp->is_add;
5030   gid_address_copy (&a->eid, eid);
5031   a->locator_set_index = locator_set_index;
5032   a->local = 1;
5033   rv = vnet_lisp_add_del_local_mapping (a, &map_index);
5034
5035 out:
5036   vec_free (name);
5037   gid_address_free (&a->eid);
5038
5039   REPLY_MACRO (VL_API_LISP_ADD_DEL_LOCAL_EID_REPLY);
5040 }
5041
5042 static void
5043   vl_api_lisp_eid_table_add_del_map_t_handler
5044   (vl_api_lisp_eid_table_add_del_map_t * mp)
5045 {
5046   vl_api_lisp_eid_table_add_del_map_reply_t *rmp;
5047   int rv = 0;
5048   rv = vnet_lisp_eid_table_map (clib_net_to_host_u32 (mp->vni),
5049                                 clib_net_to_host_u32 (mp->dp_table),
5050                                 mp->is_l2, mp->is_add);
5051 REPLY_MACRO (VL_API_LISP_EID_TABLE_ADD_DEL_MAP_REPLY)}
5052
5053 /** Used for transferring locators via VPP API */
5054 /* *INDENT-OFF* */
5055 typedef CLIB_PACKED (struct {
5056   u8 is_ip4; /**< is locator an IPv4 address */
5057   u8 priority; /**< locator priority */
5058   u8 weight; /**< locator weight */
5059   u8 addr[16]; /**< IPv4/IPv6 address */
5060 }) rloc_t;
5061 /* *INDENT-ON* */
5062
5063 static locator_pair_t *
5064 unformat_lisp_loc_pairs (void *lcl_locs, void *rmt_locs, u32 rloc_num)
5065 {
5066   u32 i;
5067   locator_pair_t *pairs = 0, pair;
5068   rloc_t *r;
5069
5070   for (i = 0; i < rloc_num; i++)
5071     {
5072       /* local locator */
5073       r = &((rloc_t *) lcl_locs)[i];
5074       memset (&pair.lcl_loc, 0, sizeof (pair.lcl_loc));
5075       ip_address_set (&pair.lcl_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
5076
5077       /* remote locators */
5078       r = &((rloc_t *) rmt_locs)[i];
5079       memset (&pair.rmt_loc, 0, sizeof (pair.rmt_loc));
5080       ip_address_set (&pair.rmt_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
5081
5082       pair.priority = r->priority;
5083       pair.weight = r->weight;
5084
5085       vec_add1 (pairs, pair);
5086     }
5087   return pairs;
5088 }
5089
5090 static locator_t *
5091 unformat_lisp_locs (void *rmt_locs, u32 rloc_num)
5092 {
5093   u32 i;
5094   locator_t *locs = 0, loc;
5095   rloc_t *r;
5096
5097   for (i = 0; i < rloc_num; i++)
5098     {
5099       /* remote locators */
5100       r = &((rloc_t *) rmt_locs)[i];
5101       memset (&loc, 0, sizeof (loc));
5102       gid_address_ip_set (&loc.address, &r->addr, r->is_ip4 ? IP4 : IP6);
5103
5104       loc.priority = r->priority;
5105       loc.weight = r->weight;
5106
5107       vec_add1 (locs, loc);
5108     }
5109   return locs;
5110 }
5111
5112 static void
5113   vl_api_lisp_gpe_add_del_fwd_entry_t_handler
5114   (vl_api_lisp_gpe_add_del_fwd_entry_t * mp)
5115 {
5116   vl_api_lisp_gpe_add_del_fwd_entry_reply_t *rmp;
5117   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
5118   locator_pair_t *pairs = 0;
5119   int rv = 0;
5120
5121   memset (a, 0, sizeof (a[0]));
5122
5123   rv = unformat_lisp_eid_api (&a->rmt_eid, mp->vni, mp->eid_type,
5124                               mp->rmt_eid, mp->rmt_len);
5125   rv |= unformat_lisp_eid_api (&a->lcl_eid, mp->vni, mp->eid_type,
5126                                mp->lcl_eid, mp->lcl_len);
5127
5128   pairs = unformat_lisp_loc_pairs (mp->lcl_locs, mp->rmt_locs, mp->loc_num);
5129
5130   if (rv || 0 == pairs)
5131     goto send_reply;
5132
5133   a->is_add = mp->is_add;
5134   a->locator_pairs = pairs;
5135   a->dp_table = mp->dp_table;
5136   a->vni = mp->vni;
5137   a->action = mp->action;
5138
5139   rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
5140   vec_free (pairs);
5141 send_reply:
5142   REPLY_MACRO (VL_API_LISP_GPE_ADD_DEL_FWD_ENTRY_REPLY);
5143 }
5144
5145 static void
5146 vl_api_lisp_add_del_map_resolver_t_handler (vl_api_lisp_add_del_map_resolver_t
5147                                             * mp)
5148 {
5149   vl_api_lisp_add_del_map_resolver_reply_t *rmp;
5150   int rv = 0;
5151   vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
5152
5153   memset (a, 0, sizeof (a[0]));
5154
5155   a->is_add = mp->is_add;
5156   ip_address_set (&a->address, mp->ip_address, mp->is_ipv6 ? IP6 : IP4);
5157
5158   rv = vnet_lisp_add_del_map_resolver (a);
5159
5160   REPLY_MACRO (VL_API_LISP_ADD_DEL_MAP_RESOLVER_REPLY);
5161 }
5162
5163 static void
5164 vl_api_lisp_gpe_enable_disable_t_handler (vl_api_lisp_gpe_enable_disable_t *
5165                                           mp)
5166 {
5167   vl_api_lisp_gpe_enable_disable_reply_t *rmp;
5168   int rv = 0;
5169   vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
5170
5171   a->is_en = mp->is_en;
5172   vnet_lisp_gpe_enable_disable (a);
5173
5174   REPLY_MACRO (VL_API_LISP_GPE_ENABLE_DISABLE_REPLY);
5175 }
5176
5177 static void
5178 vl_api_lisp_enable_disable_t_handler (vl_api_lisp_enable_disable_t * mp)
5179 {
5180   vl_api_lisp_enable_disable_reply_t *rmp;
5181   int rv = 0;
5182
5183   vnet_lisp_enable_disable (mp->is_en);
5184   REPLY_MACRO (VL_API_LISP_ENABLE_DISABLE_REPLY);
5185 }
5186
5187 static void
5188 vl_api_lisp_gpe_add_del_iface_t_handler (vl_api_lisp_gpe_add_del_iface_t * mp)
5189 {
5190   vl_api_lisp_gpe_add_del_iface_reply_t *rmp;
5191   int rv = 0;
5192
5193   if (mp->is_l2)
5194     {
5195       if (mp->is_add)
5196         {
5197           if (~0 ==
5198               lisp_gpe_tenant_l2_iface_add_or_lock (mp->vni, mp->dp_table))
5199             rv = 1;
5200         }
5201       else
5202         lisp_gpe_tenant_l2_iface_unlock (mp->vni);
5203     }
5204   else
5205     {
5206       if (mp->is_add)
5207         {
5208           if (~0 ==
5209               lisp_gpe_tenant_l3_iface_add_or_lock (mp->vni, mp->dp_table))
5210             rv = 1;
5211         }
5212       else
5213         lisp_gpe_tenant_l3_iface_unlock (mp->vni);
5214     }
5215
5216   REPLY_MACRO (VL_API_LISP_GPE_ADD_DEL_IFACE_REPLY);
5217 }
5218
5219 static void
5220   vl_api_show_lisp_map_request_mode_t_handler
5221   (vl_api_show_lisp_map_request_mode_t * mp)
5222 {
5223   int rv = 0;
5224   vl_api_show_lisp_map_request_mode_reply_t *rmp;
5225
5226   /* *INDENT-OFF* */
5227   REPLY_MACRO2(VL_API_SHOW_LISP_MAP_REQUEST_MODE_REPLY,
5228   ({
5229     rmp->mode = vnet_lisp_get_map_request_mode ();
5230   }));
5231   /* *INDENT-ON* */
5232 }
5233
5234 static void
5235 vl_api_lisp_map_request_mode_t_handler (vl_api_lisp_map_request_mode_t * mp)
5236 {
5237   vl_api_lisp_map_request_mode_reply_t *rmp;
5238   int rv = 0;
5239
5240   rv = vnet_lisp_set_map_request_mode (mp->mode);
5241
5242   REPLY_MACRO (VL_API_LISP_MAP_REQUEST_MODE_REPLY);
5243 }
5244
5245 static void
5246 vl_api_lisp_pitr_set_locator_set_t_handler (vl_api_lisp_pitr_set_locator_set_t
5247                                             * mp)
5248 {
5249   vl_api_lisp_pitr_set_locator_set_reply_t *rmp;
5250   int rv = 0;
5251   u8 *ls_name = 0;
5252
5253   ls_name = format (0, "%s", mp->ls_name);
5254   rv = vnet_lisp_pitr_set_locator_set (ls_name, mp->is_add);
5255   vec_free (ls_name);
5256
5257   REPLY_MACRO (VL_API_LISP_PITR_SET_LOCATOR_SET_REPLY);
5258 }
5259
5260 static void
5261   vl_api_lisp_add_del_map_request_itr_rlocs_t_handler
5262   (vl_api_lisp_add_del_map_request_itr_rlocs_t * mp)
5263 {
5264   vl_api_lisp_add_del_map_request_itr_rlocs_reply_t *rmp;
5265   int rv = 0;
5266   u8 *locator_set_name = NULL;
5267   vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
5268
5269   locator_set_name = format (0, "%s", mp->locator_set_name);
5270
5271   a->is_add = mp->is_add;
5272   a->locator_set_name = locator_set_name;
5273
5274   rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
5275
5276   vec_free (locator_set_name);
5277
5278   REPLY_MACRO (VL_API_LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS_REPLY);
5279 }
5280
5281 static void
5282   vl_api_lisp_add_del_remote_mapping_t_handler
5283   (vl_api_lisp_add_del_remote_mapping_t * mp)
5284 {
5285   locator_t *rlocs = 0;
5286   vl_api_lisp_add_del_remote_mapping_reply_t *rmp;
5287   int rv = 0;
5288   gid_address_t _eid, *eid = &_eid;
5289   u32 rloc_num = clib_net_to_host_u32 (mp->rloc_num);
5290
5291   memset (eid, 0, sizeof (eid[0]));
5292
5293   rv = unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
5294                               mp->eid_type, mp->eid, mp->eid_len);
5295   if (rv)
5296     goto send_reply;
5297
5298   rlocs = unformat_lisp_locs (mp->rlocs, rloc_num);
5299
5300   if (!mp->is_add)
5301     {
5302       vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
5303       gid_address_copy (&a->reid, eid);
5304       a->is_add = 0;
5305       rv = vnet_lisp_add_del_adjacency (a);
5306       if (rv)
5307         {
5308           goto out;
5309         }
5310     }
5311
5312   /* NOTE: for now this works as a static remote mapping, i.e.,
5313    * not authoritative and ttl infinite. */
5314   rv = vnet_lisp_add_del_mapping (eid, rlocs, mp->action, 0, ~0,
5315                                   mp->is_add, 1 /* is_static */ , 0);
5316
5317   if (mp->del_all)
5318     vnet_lisp_clear_all_remote_adjacencies ();
5319
5320 out:
5321   vec_free (rlocs);
5322 send_reply:
5323   REPLY_MACRO (VL_API_LISP_ADD_DEL_REMOTE_MAPPING_REPLY);
5324 }
5325
5326 static void
5327 vl_api_lisp_add_del_adjacency_t_handler (vl_api_lisp_add_del_adjacency_t * mp)
5328 {
5329   vl_api_lisp_add_del_adjacency_reply_t *rmp;
5330   vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
5331
5332   int rv = 0;
5333   memset (a, 0, sizeof (a[0]));
5334
5335   rv = unformat_lisp_eid_api (&a->leid, clib_net_to_host_u32 (mp->vni),
5336                               mp->eid_type, mp->leid, mp->leid_len);
5337   rv |= unformat_lisp_eid_api (&a->reid, clib_net_to_host_u32 (mp->vni),
5338                                mp->eid_type, mp->reid, mp->reid_len);
5339
5340   if (rv)
5341     goto send_reply;
5342
5343   a->is_add = mp->is_add;
5344   rv = vnet_lisp_add_del_adjacency (a);
5345
5346 send_reply:
5347   REPLY_MACRO (VL_API_LISP_ADD_DEL_ADJACENCY_REPLY);
5348 }
5349
5350 static void
5351 send_lisp_locator_details (lisp_cp_main_t * lcm,
5352                            locator_t * loc,
5353                            unix_shared_memory_queue_t * q, u32 context)
5354 {
5355   vl_api_lisp_locator_details_t *rmp;
5356
5357   rmp = vl_msg_api_alloc (sizeof (*rmp));
5358   memset (rmp, 0, sizeof (*rmp));
5359   rmp->_vl_msg_id = ntohs (VL_API_LISP_LOCATOR_DETAILS);
5360   rmp->context = context;
5361
5362   rmp->local = loc->local;
5363   if (loc->local)
5364     {
5365       rmp->sw_if_index = ntohl (loc->sw_if_index);
5366     }
5367   else
5368     {
5369       rmp->is_ipv6 = gid_address_ip_version (&loc->address);
5370       ip_address_copy_addr (rmp->ip_address, &gid_address_ip (&loc->address));
5371     }
5372   rmp->priority = loc->priority;
5373   rmp->weight = loc->weight;
5374
5375   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5376 }
5377
5378 static void
5379 vl_api_lisp_locator_dump_t_handler (vl_api_lisp_locator_dump_t * mp)
5380 {
5381   u8 *ls_name = 0;
5382   unix_shared_memory_queue_t *q = 0;
5383   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5384   locator_set_t *lsit = 0;
5385   locator_t *loc = 0;
5386   u32 ls_index = ~0, *locit = 0;
5387   uword *p = 0;
5388
5389   q = vl_api_client_index_to_input_queue (mp->client_index);
5390   if (q == 0)
5391     {
5392       return;
5393     }
5394
5395   if (mp->is_index_set)
5396     ls_index = htonl (mp->ls_index);
5397   else
5398     {
5399       /* make sure we get a proper C-string */
5400       mp->ls_name[sizeof (mp->ls_name) - 1] = 0;
5401       ls_name = format (0, "%s", mp->ls_name);
5402       p = hash_get_mem (lcm->locator_set_index_by_name, ls_name);
5403       if (!p)
5404         goto out;
5405       ls_index = p[0];
5406     }
5407
5408   if (pool_is_free_index (lcm->locator_set_pool, ls_index))
5409     return;
5410
5411   lsit = pool_elt_at_index (lcm->locator_set_pool, ls_index);
5412
5413   vec_foreach (locit, lsit->locator_indices)
5414   {
5415     loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
5416     send_lisp_locator_details (lcm, loc, q, mp->context);
5417   };
5418 out:
5419   vec_free (ls_name);
5420 }
5421
5422 static void
5423 send_lisp_locator_set_details (lisp_cp_main_t * lcm,
5424                                locator_set_t * lsit,
5425                                unix_shared_memory_queue_t * q,
5426                                u32 context, u32 ls_index)
5427 {
5428   vl_api_lisp_locator_set_details_t *rmp;
5429   u8 *str = 0;
5430
5431   rmp = vl_msg_api_alloc (sizeof (*rmp));
5432   memset (rmp, 0, sizeof (*rmp));
5433   rmp->_vl_msg_id = ntohs (VL_API_LISP_LOCATOR_SET_DETAILS);
5434   rmp->context = context;
5435
5436   rmp->ls_index = htonl (ls_index);
5437   if (lsit->local)
5438     {
5439       ASSERT (lsit->name != NULL);
5440       strncpy ((char *) rmp->ls_name, (char *) lsit->name,
5441                vec_len (lsit->name));
5442     }
5443   else
5444     {
5445       str = format (0, "<remote-%d>", ls_index);
5446       strncpy ((char *) rmp->ls_name, (char *) str, vec_len (str));
5447       vec_free (str);
5448     }
5449
5450   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5451 }
5452
5453 static void
5454 vl_api_lisp_locator_set_dump_t_handler (vl_api_lisp_locator_set_dump_t * mp)
5455 {
5456   unix_shared_memory_queue_t *q = NULL;
5457   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5458   locator_set_t *lsit = NULL;
5459   u8 filter;
5460
5461   q = vl_api_client_index_to_input_queue (mp->client_index);
5462   if (q == 0)
5463     {
5464       return;
5465     }
5466
5467   filter = mp->filter;
5468   /* *INDENT-OFF* */
5469   pool_foreach (lsit, lcm->locator_set_pool,
5470   ({
5471     if (filter && !((1 == filter && lsit->local) ||
5472                     (2 == filter && !lsit->local)))
5473       {
5474         continue;
5475       }
5476     send_lisp_locator_set_details (lcm, lsit, q, mp->context,
5477                                    lsit - lcm->locator_set_pool);
5478   }));
5479   /* *INDENT-ON* */
5480 }
5481
5482 static void
5483 lisp_fid_put_api (u8 * dst, fid_address_t * src, u8 * prefix_length)
5484 {
5485   ASSERT (prefix_length);
5486   ip_prefix_t *ippref = &fid_addr_ippref (src);
5487
5488   switch (fid_addr_type (src))
5489     {
5490     case FID_ADDR_IP_PREF:
5491       if (ip_prefix_version (ippref) == IP4)
5492         clib_memcpy (dst, &ip_prefix_v4 (ippref), 4);
5493       else
5494         clib_memcpy (dst, &ip_prefix_v6 (ippref), 16);
5495       prefix_length[0] = ip_prefix_len (ippref);
5496       break;
5497
5498     case FID_ADDR_MAC:
5499       prefix_length[0] = 0;
5500       clib_memcpy (dst, fid_addr_mac (src), 6);
5501       break;
5502
5503     default:
5504       clib_warning ("Unknown FID type %d!", fid_addr_type (src));
5505       break;
5506     }
5507 }
5508
5509 static u8
5510 fid_type_to_api_type (fid_address_t * fid)
5511 {
5512   ip_prefix_t *ippref;
5513
5514   switch (fid_addr_type (fid))
5515     {
5516     case FID_ADDR_IP_PREF:
5517       ippref = &fid_addr_ippref (fid);
5518       if (ip_prefix_version (ippref) == IP4)
5519         return 0;
5520       else if (ip_prefix_version (ippref) == IP6)
5521         return 1;
5522       else
5523         return ~0;
5524
5525     case FID_ADDR_MAC:
5526       return 2;
5527     }
5528
5529   return ~0;
5530 }
5531
5532 static void
5533 send_lisp_eid_table_details (mapping_t * mapit,
5534                              unix_shared_memory_queue_t * q,
5535                              u32 context, u8 filter)
5536 {
5537   fid_address_t *fid;
5538   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5539   locator_set_t *ls = 0;
5540   vl_api_lisp_eid_table_details_t *rmp = NULL;
5541   gid_address_t *gid = NULL;
5542   u8 *mac = 0;
5543   ip_prefix_t *ip_prefix = NULL;
5544
5545   switch (filter)
5546     {
5547     case 0:                     /* all mappings */
5548       break;
5549
5550     case 1:                     /* local only */
5551       if (!mapit->local)
5552         return;
5553       break;
5554     case 2:                     /* remote only */
5555       if (mapit->local)
5556         return;
5557       break;
5558     default:
5559       clib_warning ("Filter error, unknown filter: %d", filter);
5560       return;
5561     }
5562
5563   gid = &mapit->eid;
5564   ip_prefix = &gid_address_ippref (gid);
5565   mac = gid_address_mac (gid);
5566
5567   rmp = vl_msg_api_alloc (sizeof (*rmp));
5568   memset (rmp, 0, sizeof (*rmp));
5569   rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_DETAILS);
5570
5571   ls = pool_elt_at_index (lcm->locator_set_pool, mapit->locator_set_index);
5572   if (vec_len (ls->locator_indices) == 0)
5573     rmp->locator_set_index = ~0;
5574   else
5575     rmp->locator_set_index = clib_host_to_net_u32 (mapit->locator_set_index);
5576
5577   rmp->is_local = mapit->local;
5578   rmp->ttl = clib_host_to_net_u32 (mapit->ttl);
5579   rmp->action = mapit->action;
5580   rmp->authoritative = mapit->authoritative;
5581
5582   switch (gid_address_type (gid))
5583     {
5584     case GID_ADDR_SRC_DST:
5585       rmp->is_src_dst = 1;
5586       fid = &gid_address_sd_src (gid);
5587       rmp->eid_type = fid_type_to_api_type (fid);
5588       lisp_fid_put_api (rmp->seid, &gid_address_sd_src (gid),
5589                         &rmp->seid_prefix_len);
5590       lisp_fid_put_api (rmp->eid, &gid_address_sd_dst (gid),
5591                         &rmp->eid_prefix_len);
5592       break;
5593     case GID_ADDR_IP_PREFIX:
5594       rmp->eid_prefix_len = ip_prefix_len (ip_prefix);
5595       if (ip_prefix_version (ip_prefix) == IP4)
5596         {
5597           rmp->eid_type = 0;    /* ipv4 type */
5598           clib_memcpy (rmp->eid, &ip_prefix_v4 (ip_prefix),
5599                        sizeof (ip_prefix_v4 (ip_prefix)));
5600         }
5601       else
5602         {
5603           rmp->eid_type = 1;    /* ipv6 type */
5604           clib_memcpy (rmp->eid, &ip_prefix_v6 (ip_prefix),
5605                        sizeof (ip_prefix_v6 (ip_prefix)));
5606         }
5607       break;
5608     case GID_ADDR_MAC:
5609       rmp->eid_type = 2;        /* l2 mac type */
5610       clib_memcpy (rmp->eid, mac, 6);
5611       break;
5612     default:
5613       ASSERT (0);
5614     }
5615   rmp->context = context;
5616   rmp->vni = clib_host_to_net_u32 (gid_address_vni (gid));
5617   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5618 }
5619
5620 static void
5621 vl_api_lisp_eid_table_dump_t_handler (vl_api_lisp_eid_table_dump_t * mp)
5622 {
5623   u32 mi;
5624   unix_shared_memory_queue_t *q = NULL;
5625   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5626   mapping_t *mapit = NULL;
5627   gid_address_t _eid, *eid = &_eid;
5628
5629   q = vl_api_client_index_to_input_queue (mp->client_index);
5630   if (q == 0)
5631     {
5632       return;
5633     }
5634
5635   if (mp->eid_set)
5636     {
5637       memset (eid, 0, sizeof (*eid));
5638
5639       unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
5640                              mp->eid_type, mp->eid, mp->prefix_length);
5641
5642       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
5643       if ((u32) ~ 0 == mi)
5644         return;
5645
5646       mapit = pool_elt_at_index (lcm->mapping_pool, mi);
5647       send_lisp_eid_table_details (mapit, q, mp->context,
5648                                    0 /* ignore filter */ );
5649     }
5650   else
5651     {
5652       /* *INDENT-OFF* */
5653       pool_foreach (mapit, lcm->mapping_pool,
5654       ({
5655         send_lisp_eid_table_details(mapit, q, mp->context,
5656                                     mp->filter);
5657       }));
5658       /* *INDENT-ON* */
5659     }
5660 }
5661
5662 static void
5663 send_lisp_gpe_fwd_entry_details (lisp_gpe_fwd_entry_t * lfe,
5664                                  unix_shared_memory_queue_t * q, u32 context)
5665 {
5666   vl_api_lisp_gpe_tunnel_details_t *rmp;
5667   lisp_gpe_main_t *lgm = &lisp_gpe_main;
5668
5669   rmp = vl_msg_api_alloc (sizeof (*rmp));
5670   memset (rmp, 0, sizeof (*rmp));
5671   rmp->_vl_msg_id = ntohs (VL_API_LISP_GPE_TUNNEL_DETAILS);
5672
5673   rmp->tunnels = lfe - lgm->lisp_fwd_entry_pool;
5674
5675   rmp->is_ipv6 = ip_prefix_version (&(lfe->key->rmt.ippref)) == IP6 ? 1 : 0;
5676   ip_address_copy_addr (rmp->source_ip,
5677                         &ip_prefix_addr (&(lfe->key->rmt.ippref)));
5678   ip_address_copy_addr (rmp->destination_ip,
5679                         &ip_prefix_addr (&(lfe->key->rmt.ippref)));
5680
5681   rmp->encap_fib_id = htonl (0);
5682   rmp->decap_fib_id = htonl (lfe->eid_fib_index);
5683   rmp->iid = htonl (lfe->key->vni);
5684   rmp->context = context;
5685
5686   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5687 }
5688
5689 static void
5690 vl_api_lisp_gpe_tunnel_dump_t_handler (vl_api_lisp_gpe_tunnel_dump_t * mp)
5691 {
5692   unix_shared_memory_queue_t *q = NULL;
5693   lisp_gpe_main_t *lgm = &lisp_gpe_main;
5694   lisp_gpe_fwd_entry_t *lfe = NULL;
5695
5696   if (pool_elts (lgm->lisp_fwd_entry_pool) == 0)
5697     {
5698       return;
5699     }
5700
5701   q = vl_api_client_index_to_input_queue (mp->client_index);
5702   if (q == 0)
5703     {
5704       return;
5705     }
5706
5707   /* *INDENT-OFF* */
5708   pool_foreach(lfe, lgm->lisp_fwd_entry_pool,
5709   ({
5710     send_lisp_gpe_fwd_entry_details(lfe, q, mp->context);
5711   }));
5712   /* *INDENT-ON* */
5713 }
5714
5715 static void
5716 send_lisp_map_resolver_details (ip_address_t * ip,
5717                                 unix_shared_memory_queue_t * q, u32 context)
5718 {
5719   vl_api_lisp_map_resolver_details_t *rmp = NULL;
5720
5721   rmp = vl_msg_api_alloc (sizeof (*rmp));
5722   memset (rmp, 0, sizeof (*rmp));
5723   rmp->_vl_msg_id = ntohs (VL_API_LISP_MAP_RESOLVER_DETAILS);
5724
5725   switch (ip_addr_version (ip))
5726     {
5727     case IP4:
5728       rmp->is_ipv6 = 0;
5729       clib_memcpy (rmp->ip_address, &ip_addr_v4 (ip),
5730                    sizeof (ip_addr_v4 (ip)));
5731       break;
5732
5733     case IP6:
5734       rmp->is_ipv6 = 1;
5735       clib_memcpy (rmp->ip_address, &ip_addr_v6 (ip),
5736                    sizeof (ip_addr_v6 (ip)));
5737       break;
5738
5739     default:
5740       ASSERT (0);
5741     }
5742   rmp->context = context;
5743
5744   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5745 }
5746
5747 static void
5748 vl_api_lisp_map_resolver_dump_t_handler (vl_api_lisp_map_resolver_dump_t * mp)
5749 {
5750   unix_shared_memory_queue_t *q = NULL;
5751   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5752   map_resolver_t *mr;
5753
5754   q = vl_api_client_index_to_input_queue (mp->client_index);
5755   if (q == 0)
5756     {
5757       return;
5758     }
5759
5760   vec_foreach (mr, lcm->map_resolvers)
5761   {
5762     send_lisp_map_resolver_details (&mr->address, q, mp->context);
5763   }
5764 }
5765
5766 static void
5767 send_eid_table_map_pair (hash_pair_t * p,
5768                          unix_shared_memory_queue_t * q, u32 context)
5769 {
5770   vl_api_lisp_eid_table_map_details_t *rmp = NULL;
5771
5772   rmp = vl_msg_api_alloc (sizeof (*rmp));
5773   memset (rmp, 0, sizeof (*rmp));
5774   rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_MAP_DETAILS);
5775
5776   rmp->vni = clib_host_to_net_u32 (p->key);
5777   rmp->dp_table = clib_host_to_net_u32 (p->value[0]);
5778   rmp->context = context;
5779   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5780 }
5781
5782 static void
5783 vl_api_lisp_eid_table_map_dump_t_handler (vl_api_lisp_eid_table_map_dump_t *
5784                                           mp)
5785 {
5786   unix_shared_memory_queue_t *q = NULL;
5787   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5788   hash_pair_t *p;
5789   uword *vni_table = 0;
5790
5791   q = vl_api_client_index_to_input_queue (mp->client_index);
5792   if (q == 0)
5793     {
5794       return;
5795     }
5796
5797   if (mp->is_l2)
5798     {
5799       vni_table = lcm->bd_id_by_vni;
5800     }
5801   else
5802     {
5803       vni_table = lcm->table_id_by_vni;
5804     }
5805
5806   /* *INDENT-OFF* */
5807   hash_foreach_pair (p, vni_table,
5808   ({
5809     send_eid_table_map_pair (p, q, mp->context);
5810   }));
5811   /* *INDENT-ON* */
5812 }
5813
5814 static void
5815 send_eid_table_vni (u32 vni, unix_shared_memory_queue_t * q, u32 context)
5816 {
5817   vl_api_lisp_eid_table_vni_details_t *rmp = 0;
5818
5819   rmp = vl_msg_api_alloc (sizeof (*rmp));
5820   memset (rmp, 0, sizeof (*rmp));
5821   rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_VNI_DETAILS);
5822   rmp->context = context;
5823   rmp->vni = clib_host_to_net_u32 (vni);
5824   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5825 }
5826
5827 static void
5828 lisp_adjacency_copy (vl_api_lisp_adjacency_t * dst, lisp_adjacency_t * adjs)
5829 {
5830   lisp_adjacency_t *adj;
5831   vl_api_lisp_adjacency_t a;
5832   u32 i, n = vec_len (adjs);
5833
5834   for (i = 0; i < n; i++)
5835     {
5836       adj = vec_elt_at_index (adjs, i);
5837       memset (&a, 0, sizeof (a));
5838
5839       switch (gid_address_type (&adj->reid))
5840         {
5841         case GID_ADDR_IP_PREFIX:
5842           a.reid_prefix_len = gid_address_ippref_len (&adj->reid);
5843           a.leid_prefix_len = gid_address_ippref_len (&adj->leid);
5844           if (gid_address_ip_version (&adj->reid) == IP4)
5845             {
5846               a.eid_type = 0;   /* ipv4 type */
5847               clib_memcpy (a.reid, &gid_address_ip (&adj->reid), 4);
5848               clib_memcpy (a.leid, &gid_address_ip (&adj->leid), 4);
5849             }
5850           else
5851             {
5852               a.eid_type = 1;   /* ipv6 type */
5853               clib_memcpy (a.reid, &gid_address_ip (&adj->reid), 16);
5854               clib_memcpy (a.leid, &gid_address_ip (&adj->leid), 16);
5855             }
5856           break;
5857         case GID_ADDR_MAC:
5858           a.eid_type = 2;       /* l2 mac type */
5859           mac_copy (a.reid, gid_address_mac (&adj->reid));
5860           mac_copy (a.leid, gid_address_mac (&adj->leid));
5861           break;
5862         default:
5863           ASSERT (0);
5864         }
5865       dst[i] = a;
5866     }
5867 }
5868
5869 static void
5870 vl_api_lisp_adjacencies_get_t_handler (vl_api_lisp_adjacencies_get_t * mp)
5871 {
5872   vl_api_lisp_adjacencies_get_reply_t *rmp = 0;
5873   lisp_adjacency_t *adjs = 0;
5874   int rv = 0;
5875   vl_api_lisp_adjacency_t a;
5876   u32 size = ~0;
5877   u32 vni = clib_net_to_host_u32 (mp->vni);
5878
5879   adjs = vnet_lisp_adjacencies_get_by_vni (vni);
5880   size = vec_len (adjs) * sizeof (a);
5881
5882   /* *INDENT-OFF* */
5883   REPLY_MACRO4 (VL_API_LISP_ADJACENCIES_GET_REPLY, size,
5884   {
5885     rmp->count = clib_host_to_net_u32 (vec_len (adjs));
5886     lisp_adjacency_copy (rmp->adjacencies, adjs);
5887   });
5888   /* *INDENT-ON* */
5889
5890   vec_free (adjs);
5891 }
5892
5893 static void
5894 vl_api_lisp_eid_table_vni_dump_t_handler (vl_api_lisp_eid_table_vni_dump_t *
5895                                           mp)
5896 {
5897   hash_pair_t *p;
5898   u32 *vnis = 0;
5899   unix_shared_memory_queue_t *q = 0;
5900   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5901
5902   q = vl_api_client_index_to_input_queue (mp->client_index);
5903   if (q == 0)
5904     {
5905       return;
5906     }
5907
5908   /* *INDENT-OFF* */
5909   hash_foreach_pair (p, lcm->table_id_by_vni,
5910   ({
5911     hash_set (vnis, p->key, 0);
5912   }));
5913
5914   hash_foreach_pair (p, lcm->bd_id_by_vni,
5915   ({
5916     hash_set (vnis, p->key, 0);
5917   }));
5918
5919   hash_foreach_pair (p, vnis,
5920   ({
5921     send_eid_table_vni (p->key, q, mp->context);
5922   }));
5923   /* *INDENT-ON* */
5924
5925   hash_free (vnis);
5926 }
5927
5928 static void
5929 vl_api_show_lisp_status_t_handler (vl_api_show_lisp_status_t * mp)
5930 {
5931   unix_shared_memory_queue_t *q = NULL;
5932   vl_api_show_lisp_status_reply_t *rmp = NULL;
5933   int rv = 0;
5934
5935   q = vl_api_client_index_to_input_queue (mp->client_index);
5936   if (q == 0)
5937     {
5938       return;
5939     }
5940
5941   /* *INDENT-OFF* */
5942   REPLY_MACRO2(VL_API_SHOW_LISP_STATUS_REPLY,
5943   ({
5944     rmp->gpe_status = vnet_lisp_gpe_enable_disable_status ();
5945     rmp->feature_status = vnet_lisp_enable_disable_status ();
5946   }));
5947   /* *INDENT-ON* */
5948 }
5949
5950 static void
5951   vl_api_lisp_get_map_request_itr_rlocs_t_handler
5952   (vl_api_lisp_get_map_request_itr_rlocs_t * mp)
5953 {
5954   unix_shared_memory_queue_t *q = NULL;
5955   vl_api_lisp_get_map_request_itr_rlocs_reply_t *rmp = NULL;
5956   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5957   locator_set_t *loc_set = 0;
5958   u8 *tmp_str = 0;
5959   int rv = 0;
5960
5961   q = vl_api_client_index_to_input_queue (mp->client_index);
5962   if (q == 0)
5963     {
5964       return;
5965     }
5966
5967   if (~0 == lcm->mreq_itr_rlocs)
5968     {
5969       tmp_str = format (0, " ");
5970     }
5971   else
5972     {
5973       loc_set =
5974         pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
5975       tmp_str = format (0, "%s", loc_set->name);
5976     }
5977
5978   /* *INDENT-OFF* */
5979   REPLY_MACRO2(VL_API_LISP_GET_MAP_REQUEST_ITR_RLOCS_REPLY,
5980   ({
5981     strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
5982             ARRAY_LEN(rmp->locator_set_name) - 1);
5983   }));
5984   /* *INDENT-ON* */
5985
5986   vec_free (tmp_str);
5987 }
5988
5989 static void
5990 vl_api_show_lisp_pitr_t_handler (vl_api_show_lisp_pitr_t * mp)
5991 {
5992   unix_shared_memory_queue_t *q = NULL;
5993   vl_api_show_lisp_pitr_reply_t *rmp = NULL;
5994   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5995   mapping_t *m;
5996   locator_set_t *ls = 0;
5997   u8 *tmp_str = 0;
5998   int rv = 0;
5999
6000   q = vl_api_client_index_to_input_queue (mp->client_index);
6001   if (q == 0)
6002     {
6003       return;
6004     }
6005
6006   if (!lcm->lisp_pitr)
6007     {
6008       tmp_str = format (0, "N/A");
6009     }
6010   else
6011     {
6012       m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
6013       if (~0 != m->locator_set_index)
6014         {
6015           ls =
6016             pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
6017           tmp_str = format (0, "%s", ls->name);
6018         }
6019       else
6020         {
6021           tmp_str = format (0, "N/A");
6022         }
6023     }
6024   vec_add1 (tmp_str, 0);
6025
6026   /* *INDENT-OFF* */
6027   REPLY_MACRO2(VL_API_SHOW_LISP_PITR_REPLY,
6028   ({
6029     rmp->status = lcm->lisp_pitr;
6030     strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
6031             ARRAY_LEN(rmp->locator_set_name) - 1);
6032   }));
6033   /* *INDENT-ON* */
6034 }
6035
6036 static void
6037 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
6038                                           mp)
6039 {
6040   vl_api_interface_name_renumber_reply_t *rmp;
6041   int rv = 0;
6042
6043   VALIDATE_SW_IF_INDEX (mp);
6044
6045   rv = vnet_interface_name_renumber
6046     (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
6047
6048   BAD_SW_IF_INDEX_LABEL;
6049
6050   REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
6051 }
6052
6053 static int
6054 arp_change_data_callback (u32 pool_index, u8 * new_mac,
6055                           u32 sw_if_index, u32 address)
6056 {
6057   vpe_api_main_t *am = &vpe_api_main;
6058   vlib_main_t *vm = am->vlib_main;
6059   vl_api_ip4_arp_event_t *event;
6060   static f64 arp_event_last_time;
6061   f64 now = vlib_time_now (vm);
6062
6063   if (pool_is_free_index (am->arp_events, pool_index))
6064     return 1;
6065
6066   event = pool_elt_at_index (am->arp_events, pool_index);
6067   /* *INDENT-OFF* */
6068   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
6069     {
6070       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
6071     }
6072   else
6073     {                           /* same mac */
6074       if (sw_if_index == event->sw_if_index &&
6075           (!event->mac_ip ||
6076            /* for BD case, also check IP address with 10 sec timeout */
6077            (address == event->address &&
6078             (now - arp_event_last_time) < 10.0)))
6079         return 1;
6080     }
6081   /* *INDENT-ON* */
6082
6083   arp_event_last_time = now;
6084   event->sw_if_index = sw_if_index;
6085   if (event->mac_ip)
6086     event->address = address;
6087   return 0;
6088 }
6089
6090 static int
6091 nd_change_data_callback (u32 pool_index, u8 * new_mac,
6092                          u32 sw_if_index, ip6_address_t * address)
6093 {
6094   vpe_api_main_t *am = &vpe_api_main;
6095   vlib_main_t *vm = am->vlib_main;
6096   vl_api_ip6_nd_event_t *event;
6097   static f64 nd_event_last_time;
6098   f64 now = vlib_time_now (vm);
6099
6100   if (pool_is_free_index (am->nd_events, pool_index))
6101     return 1;
6102
6103   event = pool_elt_at_index (am->nd_events, pool_index);
6104
6105   /* *INDENT-OFF* */
6106   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
6107     {
6108       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
6109     }
6110   else
6111     {                           /* same mac */
6112       if (sw_if_index == event->sw_if_index &&
6113           (!event->mac_ip ||
6114            /* for BD case, also check IP address with 10 sec timeout */
6115            (ip6_address_is_equal (address,
6116                                   (ip6_address_t *) event->address) &&
6117             (now - nd_event_last_time) < 10.0)))
6118         return 1;
6119     }
6120   /* *INDENT-ON* */
6121
6122   nd_event_last_time = now;
6123   event->sw_if_index = sw_if_index;
6124   if (event->mac_ip)
6125     clib_memcpy (event->address, address, sizeof (event->address));
6126   return 0;
6127 }
6128
6129 static int
6130 arp_change_delete_callback (u32 pool_index, u8 * notused)
6131 {
6132   vpe_api_main_t *am = &vpe_api_main;
6133
6134   if (pool_is_free_index (am->arp_events, pool_index))
6135     return 1;
6136
6137   pool_put_index (am->arp_events, pool_index);
6138   return 0;
6139 }
6140
6141 static int
6142 nd_change_delete_callback (u32 pool_index, u8 * notused)
6143 {
6144   vpe_api_main_t *am = &vpe_api_main;
6145
6146   if (pool_is_free_index (am->nd_events, pool_index))
6147     return 1;
6148
6149   pool_put_index (am->nd_events, pool_index);
6150   return 0;
6151 }
6152
6153 static void
6154 vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
6155 {
6156   vpe_api_main_t *am = &vpe_api_main;
6157   vnet_main_t *vnm = vnet_get_main ();
6158   vl_api_want_ip4_arp_events_reply_t *rmp;
6159   vl_api_ip4_arp_event_t *event;
6160   int rv;
6161
6162   if (mp->enable_disable)
6163     {
6164       pool_get (am->arp_events, event);
6165       memset (event, 0, sizeof (*event));
6166
6167       event->_vl_msg_id = ntohs (VL_API_IP4_ARP_EVENT);
6168       event->client_index = mp->client_index;
6169       event->context = mp->context;
6170       event->address = mp->address;
6171       event->pid = mp->pid;
6172       if (mp->address == 0)
6173         event->mac_ip = 1;
6174
6175       rv = vnet_add_del_ip4_arp_change_event
6176         (vnm, arp_change_data_callback,
6177          mp->pid, &mp->address /* addr, in net byte order */ ,
6178          vpe_resolver_process_node.index,
6179          IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
6180     }
6181   else
6182     {
6183       rv = vnet_add_del_ip4_arp_change_event
6184         (vnm, arp_change_delete_callback,
6185          mp->pid, &mp->address /* addr, in net byte order */ ,
6186          vpe_resolver_process_node.index,
6187          IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
6188     }
6189   REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
6190 }
6191
6192 static void
6193 vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
6194 {
6195   vpe_api_main_t *am = &vpe_api_main;
6196   vnet_main_t *vnm = vnet_get_main ();
6197   vl_api_want_ip6_nd_events_reply_t *rmp;
6198   vl_api_ip6_nd_event_t *event;
6199   int rv;
6200
6201   if (mp->enable_disable)
6202     {
6203       pool_get (am->nd_events, event);
6204       memset (event, 0, sizeof (*event));
6205
6206       event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
6207       event->client_index = mp->client_index;
6208       event->context = mp->context;
6209       clib_memcpy (event->address, mp->address, 16);
6210       event->pid = mp->pid;
6211       if (ip6_address_is_zero ((ip6_address_t *) mp->address))
6212         event->mac_ip = 1;
6213
6214       rv = vnet_add_del_ip6_nd_change_event
6215         (vnm, nd_change_data_callback,
6216          mp->pid, mp->address /* addr, in net byte order */ ,
6217          vpe_resolver_process_node.index,
6218          IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
6219     }
6220   else
6221     {
6222       rv = vnet_add_del_ip6_nd_change_event
6223         (vnm, nd_change_delete_callback,
6224          mp->pid, mp->address /* addr, in net byte order */ ,
6225          vpe_resolver_process_node.index,
6226          IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
6227     }
6228   REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
6229 }
6230
6231 static void vl_api_input_acl_set_interface_t_handler
6232   (vl_api_input_acl_set_interface_t * mp)
6233 {
6234   vlib_main_t *vm = vlib_get_main ();
6235   vl_api_input_acl_set_interface_reply_t *rmp;
6236   int rv;
6237   u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
6238
6239   ip4_table_index = ntohl (mp->ip4_table_index);
6240   ip6_table_index = ntohl (mp->ip6_table_index);
6241   l2_table_index = ntohl (mp->l2_table_index);
6242   sw_if_index = ntohl (mp->sw_if_index);
6243
6244   VALIDATE_SW_IF_INDEX (mp);
6245
6246   rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
6247                                  ip6_table_index, l2_table_index, mp->is_add);
6248
6249   BAD_SW_IF_INDEX_LABEL;
6250
6251   REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
6252 }
6253
6254 static void vl_api_ipsec_spd_add_del_t_handler
6255   (vl_api_ipsec_spd_add_del_t * mp)
6256 {
6257 #if IPSEC == 0
6258   clib_warning ("unimplemented");
6259 #else
6260
6261   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6262   vl_api_ipsec_spd_add_del_reply_t *rmp;
6263   int rv;
6264
6265 #if DPDK > 0
6266   rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
6267 #else
6268   rv = VNET_API_ERROR_UNIMPLEMENTED;
6269 #endif
6270
6271   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
6272 #endif
6273 }
6274
6275 static void vl_api_ipsec_interface_add_del_spd_t_handler
6276   (vl_api_ipsec_interface_add_del_spd_t * mp)
6277 {
6278   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6279   vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
6280   int rv;
6281   u32 sw_if_index __attribute__ ((unused));
6282   u32 spd_id __attribute__ ((unused));
6283
6284   sw_if_index = ntohl (mp->sw_if_index);
6285   spd_id = ntohl (mp->spd_id);
6286
6287   VALIDATE_SW_IF_INDEX (mp);
6288
6289 #if IPSEC > 0
6290   rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
6291 #else
6292   rv = VNET_API_ERROR_UNIMPLEMENTED;
6293 #endif
6294
6295   BAD_SW_IF_INDEX_LABEL;
6296
6297   REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
6298 }
6299
6300 static void vl_api_ipsec_spd_add_del_entry_t_handler
6301   (vl_api_ipsec_spd_add_del_entry_t * mp)
6302 {
6303   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6304   vl_api_ipsec_spd_add_del_entry_reply_t *rmp;
6305   int rv;
6306
6307 #if IPSEC > 0
6308   ipsec_policy_t p;
6309
6310   memset (&p, 0, sizeof (p));
6311
6312   p.id = ntohl (mp->spd_id);
6313   p.priority = ntohl (mp->priority);
6314   p.is_outbound = mp->is_outbound;
6315   p.is_ipv6 = mp->is_ipv6;
6316
6317   if (mp->is_ipv6 || mp->is_ip_any)
6318     {
6319       clib_memcpy (&p.raddr.start, mp->remote_address_start, 16);
6320       clib_memcpy (&p.raddr.stop, mp->remote_address_stop, 16);
6321       clib_memcpy (&p.laddr.start, mp->local_address_start, 16);
6322       clib_memcpy (&p.laddr.stop, mp->local_address_stop, 16);
6323     }
6324   else
6325     {
6326       clib_memcpy (&p.raddr.start.ip4.data, mp->remote_address_start, 4);
6327       clib_memcpy (&p.raddr.stop.ip4.data, mp->remote_address_stop, 4);
6328       clib_memcpy (&p.laddr.start.ip4.data, mp->local_address_start, 4);
6329       clib_memcpy (&p.laddr.stop.ip4.data, mp->local_address_stop, 4);
6330     }
6331   p.protocol = mp->protocol;
6332   p.rport.start = ntohs (mp->remote_port_start);
6333   p.rport.stop = ntohs (mp->remote_port_stop);
6334   p.lport.start = ntohs (mp->local_port_start);
6335   p.lport.stop = ntohs (mp->local_port_stop);
6336   /* policy action resolve unsupported */
6337   if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE)
6338     {
6339       clib_warning ("unsupported action: 'resolve'");
6340       rv = VNET_API_ERROR_UNIMPLEMENTED;
6341       goto out;
6342     }
6343   p.policy = mp->policy;
6344   p.sa_id = ntohl (mp->sa_id);
6345
6346   rv = ipsec_add_del_policy (vm, &p, mp->is_add);
6347   if (rv)
6348     goto out;
6349
6350   if (mp->is_ip_any)
6351     {
6352       p.is_ipv6 = 1;
6353       rv = ipsec_add_del_policy (vm, &p, mp->is_add);
6354     }
6355 #else
6356   rv = VNET_API_ERROR_UNIMPLEMENTED;
6357   goto out;
6358 #endif
6359
6360 out:
6361   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
6362 }
6363
6364 static void vl_api_ipsec_sad_add_del_entry_t_handler
6365   (vl_api_ipsec_sad_add_del_entry_t * mp)
6366 {
6367   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6368   vl_api_ipsec_sad_add_del_entry_reply_t *rmp;
6369   int rv;
6370 #if IPSEC > 0
6371   ipsec_sa_t sa;
6372
6373   memset (&sa, 0, sizeof (sa));
6374
6375   sa.id = ntohl (mp->sad_id);
6376   sa.spi = ntohl (mp->spi);
6377   /* security protocol AH unsupported */
6378   if (mp->protocol == IPSEC_PROTOCOL_AH)
6379     {
6380       clib_warning ("unsupported security protocol 'AH'");
6381       rv = VNET_API_ERROR_UNIMPLEMENTED;
6382       goto out;
6383     }
6384   sa.protocol = mp->protocol;
6385   /* check for unsupported crypto-alg */
6386   if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
6387       mp->crypto_algorithm >= IPSEC_CRYPTO_N_ALG)
6388     {
6389       clib_warning ("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
6390                     mp->crypto_algorithm);
6391       rv = VNET_API_ERROR_UNIMPLEMENTED;
6392       goto out;
6393     }
6394   sa.crypto_alg = mp->crypto_algorithm;
6395   sa.crypto_key_len = mp->crypto_key_length;
6396   clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
6397   /* check for unsupported integ-alg */
6398 #if DPDK_CRYPTO==1
6399   if (mp->integrity_algorithm < IPSEC_INTEG_ALG_NONE ||
6400 #else
6401   if (mp->integrity_algorithm < IPSEC_INTEG_ALG_SHA1_96 ||
6402 #endif
6403       mp->integrity_algorithm >= IPSEC_INTEG_N_ALG)
6404     {
6405       clib_warning ("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
6406                     mp->integrity_algorithm);
6407       rv = VNET_API_ERROR_UNIMPLEMENTED;
6408       goto out;
6409     }
6410
6411 #if DPDK_CRYPTO==1
6412   /*Special cases, aes-gcm-128 encryption */
6413   if (mp->crypto_algorithm == IPSEC_CRYPTO_ALG_AES_GCM_128)
6414     {
6415       if (mp->integrity_algorithm != IPSEC_INTEG_ALG_NONE
6416           && mp->integrity_algorithm != IPSEC_INTEG_ALG_AES_GCM_128)
6417         {
6418           clib_warning
6419             ("unsupported: aes-gcm-128 crypto-alg needs none as integ-alg");
6420           rv = VNET_API_ERROR_UNIMPLEMENTED;
6421           goto out;
6422         }
6423       else                      /*set integ-alg internally to aes-gcm-128 */
6424         mp->integrity_algorithm = IPSEC_INTEG_ALG_AES_GCM_128;
6425     }
6426   else if (mp->integrity_algorithm == IPSEC_INTEG_ALG_AES_GCM_128)
6427     {
6428       clib_warning ("unsupported integ-alg: aes-gcm-128");
6429       rv = VNET_API_ERROR_UNIMPLEMENTED;
6430       goto out;
6431     }
6432   else if (mp->integrity_algorithm == IPSEC_INTEG_ALG_NONE)
6433     {
6434       clib_warning ("unsupported integ-alg: none");
6435       rv = VNET_API_ERROR_UNIMPLEMENTED;
6436       goto out;
6437     }
6438 #endif
6439
6440   sa.integ_alg = mp->integrity_algorithm;
6441   sa.integ_key_len = mp->integrity_key_length;
6442   clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
6443   sa.use_esn = mp->use_extended_sequence_number;
6444   sa.is_tunnel = mp->is_tunnel;
6445   sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
6446   if (sa.is_tunnel_ip6)
6447     {
6448       clib_memcpy (&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
6449       clib_memcpy (&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
6450     }
6451   else
6452     {
6453       clib_memcpy (&sa.tunnel_src_addr.ip4.data, mp->tunnel_src_address, 4);
6454       clib_memcpy (&sa.tunnel_dst_addr.ip4.data, mp->tunnel_dst_address, 4);
6455     }
6456
6457   rv = ipsec_add_del_sa (vm, &sa, mp->is_add);
6458 #else
6459   rv = VNET_API_ERROR_UNIMPLEMENTED;
6460   goto out;
6461 #endif
6462
6463 out:
6464   REPLY_MACRO (VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
6465 }
6466
6467 static void
6468 vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
6469 {
6470   vl_api_ikev2_profile_add_del_reply_t *rmp;
6471   int rv = 0;
6472
6473 #if IPSEC > 0
6474   vlib_main_t *vm = vlib_get_main ();
6475   clib_error_t *error;
6476   u8 *tmp = format (0, "%s", mp->name);
6477   error = ikev2_add_del_profile (vm, tmp, mp->is_add);
6478   vec_free (tmp);
6479   if (error)
6480     rv = VNET_API_ERROR_UNSPECIFIED;
6481 #else
6482   rv = VNET_API_ERROR_UNIMPLEMENTED;
6483 #endif
6484
6485   REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
6486 }
6487
6488 static void
6489   vl_api_ikev2_profile_set_auth_t_handler
6490   (vl_api_ikev2_profile_set_auth_t * mp)
6491 {
6492   vl_api_ikev2_profile_set_auth_reply_t *rmp;
6493   int rv = 0;
6494
6495 #if IPSEC > 0
6496   vlib_main_t *vm = vlib_get_main ();
6497   clib_error_t *error;
6498   u8 *tmp = format (0, "%s", mp->name);
6499   u8 *data = vec_new (u8, mp->data_len);
6500   clib_memcpy (data, mp->data, mp->data_len);
6501   error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
6502   vec_free (tmp);
6503   vec_free (data);
6504   if (error)
6505     rv = VNET_API_ERROR_UNSPECIFIED;
6506 #else
6507   rv = VNET_API_ERROR_UNIMPLEMENTED;
6508 #endif
6509
6510   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
6511 }
6512
6513 static void
6514 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
6515 {
6516   vl_api_ikev2_profile_add_del_reply_t *rmp;
6517   int rv = 0;
6518
6519 #if IPSEC > 0
6520   vlib_main_t *vm = vlib_get_main ();
6521   clib_error_t *error;
6522   u8 *tmp = format (0, "%s", mp->name);
6523   u8 *data = vec_new (u8, mp->data_len);
6524   clib_memcpy (data, mp->data, mp->data_len);
6525   error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
6526   vec_free (tmp);
6527   vec_free (data);
6528   if (error)
6529     rv = VNET_API_ERROR_UNSPECIFIED;
6530 #else
6531   rv = VNET_API_ERROR_UNIMPLEMENTED;
6532 #endif
6533
6534   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
6535 }
6536
6537 static void
6538 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
6539 {
6540   vl_api_ikev2_profile_set_ts_reply_t *rmp;
6541   int rv = 0;
6542
6543 #if IPSEC > 0
6544   vlib_main_t *vm = vlib_get_main ();
6545   clib_error_t *error;
6546   u8 *tmp = format (0, "%s", mp->name);
6547   error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
6548                                 mp->end_port, (ip4_address_t) mp->start_addr,
6549                                 (ip4_address_t) mp->end_addr, mp->is_local);
6550   vec_free (tmp);
6551   if (error)
6552     rv = VNET_API_ERROR_UNSPECIFIED;
6553 #else
6554   rv = VNET_API_ERROR_UNIMPLEMENTED;
6555 #endif
6556
6557   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
6558 }
6559
6560 static void
6561 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
6562 {
6563   vl_api_ikev2_profile_set_ts_reply_t *rmp;
6564   int rv = 0;
6565
6566 #if IPSEC > 0
6567   vlib_main_t *vm = vlib_get_main ();
6568   clib_error_t *error;
6569
6570   error = ikev2_set_local_key (vm, mp->key_file);
6571   if (error)
6572     rv = VNET_API_ERROR_UNSPECIFIED;
6573 #else
6574   rv = VNET_API_ERROR_UNIMPLEMENTED;
6575 #endif
6576
6577   REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
6578 }
6579
6580 static void
6581 vl_api_map_add_domain_t_handler (vl_api_map_add_domain_t * mp)
6582 {
6583   vl_api_map_add_domain_reply_t *rmp;
6584   int rv = 0;
6585   u32 index;
6586   u8 flags = mp->is_translation ? MAP_DOMAIN_TRANSLATION : 0;
6587   rv =
6588     map_create_domain ((ip4_address_t *) & mp->ip4_prefix, mp->ip4_prefix_len,
6589                        (ip6_address_t *) & mp->ip6_prefix, mp->ip6_prefix_len,
6590                        (ip6_address_t *) & mp->ip6_src,
6591                        mp->ip6_src_prefix_len, mp->ea_bits_len,
6592                        mp->psid_offset, mp->psid_length, &index,
6593                        ntohs (mp->mtu), flags);
6594
6595   /* *INDENT-OFF* */
6596   REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
6597   ({
6598     rmp->index = ntohl(index);
6599   }));
6600   /* *INDENT-ON* */
6601 }
6602
6603 static void
6604 vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp)
6605 {
6606   vl_api_map_del_domain_reply_t *rmp;
6607   int rv = 0;
6608
6609   rv = map_delete_domain (ntohl (mp->index));
6610
6611   REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY);
6612 }
6613
6614 static void
6615 vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp)
6616 {
6617   vl_api_map_del_domain_reply_t *rmp;
6618   int rv = 0;
6619
6620   rv =
6621     map_add_del_psid (ntohl (mp->index), ntohs (mp->psid),
6622                       (ip6_address_t *) mp->ip6_dst, mp->is_add);
6623
6624   REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY);
6625 }
6626
6627 static void
6628 vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
6629 {
6630   vl_api_map_domain_details_t *rmp;
6631   map_main_t *mm = &map_main;
6632   map_domain_t *d;
6633   unix_shared_memory_queue_t *q;
6634
6635   if (pool_elts (mm->domains) == 0)
6636     return;
6637
6638   q = vl_api_client_index_to_input_queue (mp->client_index);
6639   if (q == 0)
6640     {
6641       return;
6642     }
6643
6644   /* *INDENT-OFF* */
6645   pool_foreach(d, mm->domains,
6646   ({
6647     /* Make sure every field is initiated (or don't skip the memset()) */
6648     rmp = vl_msg_api_alloc (sizeof (*rmp));
6649     rmp->_vl_msg_id = ntohs(VL_API_MAP_DOMAIN_DETAILS);
6650     rmp->domain_index = htonl(d - mm->domains);
6651     rmp->ea_bits_len = d->ea_bits_len;
6652     rmp->psid_offset = d->psid_offset;
6653     rmp->psid_length = d->psid_length;
6654     clib_memcpy(rmp->ip4_prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix));
6655     rmp->ip4_prefix_len = d->ip4_prefix_len;
6656     clib_memcpy(rmp->ip6_prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix));
6657     rmp->ip6_prefix_len = d->ip6_prefix_len;
6658     clib_memcpy(rmp->ip6_src, &d->ip6_src, sizeof(rmp->ip6_src));
6659     rmp->ip6_src_len = d->ip6_src_len;
6660     rmp->mtu = htons(d->mtu);
6661     rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION);
6662     rmp->context = mp->context;
6663
6664     vl_msg_api_send_shmem (q, (u8 *)&rmp);
6665   }));
6666   /* *INDENT-ON* */
6667 }
6668
6669 static void
6670 vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
6671 {
6672   unix_shared_memory_queue_t *q;
6673   u16 i;
6674   ip6_address_t dst;
6675   vl_api_map_rule_details_t *rmp;
6676   map_main_t *mm = &map_main;
6677   u32 domain_index = ntohl (mp->domain_index);
6678   map_domain_t *d;
6679
6680   if (pool_elts (mm->domains) == 0)
6681     return;
6682
6683   d = pool_elt_at_index (mm->domains, domain_index);
6684   if (!d || !d->rules)
6685     {
6686       return;
6687     }
6688
6689   q = vl_api_client_index_to_input_queue (mp->client_index);
6690   if (q == 0)
6691     {
6692       return;
6693     }
6694
6695   for (i = 0; i < (0x1 << d->psid_length); i++)
6696     {
6697       dst = d->rules[i];
6698       if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
6699         {
6700           continue;
6701         }
6702       rmp = vl_msg_api_alloc (sizeof (*rmp));
6703       memset (rmp, 0, sizeof (*rmp));
6704       rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS);
6705       rmp->psid = htons (i);
6706       clib_memcpy (rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
6707       rmp->context = mp->context;
6708       vl_msg_api_send_shmem (q, (u8 *) & rmp);
6709     }
6710 }
6711
6712 static void
6713 vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
6714 {
6715   vl_api_map_summary_stats_reply_t *rmp;
6716   vlib_combined_counter_main_t *cm;
6717   vlib_counter_t v;
6718   int i, which;
6719   u64 total_pkts[VLIB_N_RX_TX];
6720   u64 total_bytes[VLIB_N_RX_TX];
6721   map_main_t *mm = &map_main;
6722   unix_shared_memory_queue_t *q =
6723     vl_api_client_index_to_input_queue (mp->client_index);
6724
6725   if (!q)
6726     return;
6727
6728   rmp = vl_msg_api_alloc (sizeof (*rmp));
6729   rmp->_vl_msg_id = ntohs (VL_API_MAP_SUMMARY_STATS_REPLY);
6730   rmp->context = mp->context;
6731   rmp->retval = 0;
6732
6733   memset (total_pkts, 0, sizeof (total_pkts));
6734   memset (total_bytes, 0, sizeof (total_bytes));
6735
6736   map_domain_counter_lock (mm);
6737   vec_foreach (cm, mm->domain_counters)
6738   {
6739     which = cm - mm->domain_counters;
6740
6741     for (i = 0; i < vec_len (cm->maxi); i++)
6742       {
6743         vlib_get_combined_counter (cm, i, &v);
6744         total_pkts[which] += v.packets;
6745         total_bytes[which] += v.bytes;
6746       }
6747   }
6748
6749   map_domain_counter_unlock (mm);
6750
6751   /* Note: in network byte order! */
6752   rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] =
6753     clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]);
6754   rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] =
6755     clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]);
6756   rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] =
6757     clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]);
6758   rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] =
6759     clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]);
6760   rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains));
6761   rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
6762   rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] =
6763     clib_host_to_net_u64 (map_error_counter_get
6764                           (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
6765   rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] =
6766     clib_host_to_net_u64 (map_error_counter_get
6767                           (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
6768
6769   vl_msg_api_send_shmem (q, (u8 *) & rmp);
6770 }
6771
6772 static void
6773 vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
6774 {
6775   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6776   vl_api_ipsec_sa_set_key_reply_t *rmp;
6777   int rv;
6778 #if IPSEC > 0
6779   ipsec_sa_t sa;
6780   sa.id = ntohl (mp->sa_id);
6781   sa.crypto_key_len = mp->crypto_key_length;
6782   clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
6783   sa.integ_key_len = mp->integrity_key_length;
6784   clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
6785
6786   rv = ipsec_set_sa_key (vm, &sa);
6787 #else
6788   rv = VNET_API_ERROR_UNIMPLEMENTED;
6789 #endif
6790
6791   REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
6792 }
6793
6794 static void vl_api_cop_interface_enable_disable_t_handler
6795   (vl_api_cop_interface_enable_disable_t * mp)
6796 {
6797   vl_api_cop_interface_enable_disable_reply_t *rmp;
6798   int rv;
6799   u32 sw_if_index = ntohl (mp->sw_if_index);
6800   int enable_disable;
6801
6802   VALIDATE_SW_IF_INDEX (mp);
6803
6804   enable_disable = (int) mp->enable_disable;
6805
6806   rv = cop_interface_enable_disable (sw_if_index, enable_disable);
6807
6808   BAD_SW_IF_INDEX_LABEL;
6809
6810   REPLY_MACRO (VL_API_COP_INTERFACE_ENABLE_DISABLE_REPLY);
6811 }
6812
6813 static void vl_api_cop_whitelist_enable_disable_t_handler
6814   (vl_api_cop_whitelist_enable_disable_t * mp)
6815 {
6816   vl_api_cop_whitelist_enable_disable_reply_t *rmp;
6817   cop_whitelist_enable_disable_args_t _a, *a = &_a;
6818   u32 sw_if_index = ntohl (mp->sw_if_index);
6819   int rv;
6820
6821   VALIDATE_SW_IF_INDEX (mp);
6822
6823   a->sw_if_index = sw_if_index;
6824   a->ip4 = mp->ip4;
6825   a->ip6 = mp->ip6;
6826   a->default_cop = mp->default_cop;
6827   a->fib_id = ntohl (mp->fib_id);
6828
6829   rv = cop_whitelist_enable_disable (a);
6830
6831   BAD_SW_IF_INDEX_LABEL;
6832
6833   REPLY_MACRO (VL_API_COP_WHITELIST_ENABLE_DISABLE_REPLY);
6834 }
6835
6836 static void
6837 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
6838 {
6839   int rv = 0;
6840   u8 *vector = 0;
6841   api_main_t *am = &api_main;
6842   vlib_main_t *vm = vlib_get_main ();
6843   void *oldheap;
6844   vl_api_get_node_graph_reply_t *rmp;
6845
6846   pthread_mutex_lock (&am->vlib_rp->mutex);
6847   oldheap = svm_push_data_heap (am->vlib_rp);
6848
6849   /*
6850    * Keep the number of memcpy ops to a minimum (e.g. 1).
6851    */
6852   vec_validate (vector, 16384);
6853   vec_reset_length (vector);
6854
6855   /* $$$$ FIXME */
6856   vector = vlib_node_serialize (&vm->node_main, vector,
6857                                 (u32) ~ 0 /* all threads */ ,
6858                                 1 /* include nexts */ ,
6859                                 1 /* include stats */ );
6860
6861   svm_pop_heap (oldheap);
6862   pthread_mutex_unlock (&am->vlib_rp->mutex);
6863
6864   /* *INDENT-OFF* */
6865   REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
6866   ({
6867     rmp->reply_in_shmem = (uword) vector;
6868   }));
6869   /* *INDENT-ON* */
6870 }
6871
6872 static void
6873 vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
6874 {
6875   int rv = 0;
6876   vl_api_ioam_enable_reply_t *rmp;
6877   clib_error_t *error;
6878
6879   /* Ignoring the profile id as currently a single profile
6880    * is supported */
6881   error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
6882                            mp->seqno, mp->analyse);
6883   if (error)
6884     {
6885       clib_error_report (error);
6886       rv = clib_error_get_code (error);
6887     }
6888
6889   REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
6890 }
6891
6892 static void
6893 vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
6894 {
6895   int rv = 0;
6896   vl_api_ioam_disable_reply_t *rmp;
6897   clib_error_t *error;
6898
6899   error = clear_ioam_rewrite_fn ();
6900   if (error)
6901     {
6902       clib_error_report (error);
6903       rv = clib_error_get_code (error);
6904     }
6905
6906   REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
6907 }
6908
6909 static void
6910 vl_api_af_packet_create_t_handler (vl_api_af_packet_create_t * mp)
6911 {
6912   vlib_main_t *vm = vlib_get_main ();
6913   vl_api_af_packet_create_reply_t *rmp;
6914   int rv = 0;
6915   u8 *host_if_name = NULL;
6916   u32 sw_if_index;
6917
6918   host_if_name = format (0, "%s", mp->host_if_name);
6919   vec_add1 (host_if_name, 0);
6920
6921   rv = af_packet_create_if (vm, host_if_name,
6922                             mp->use_random_hw_addr ? 0 : mp->hw_addr,
6923                             &sw_if_index);
6924
6925   vec_free (host_if_name);
6926
6927   /* *INDENT-OFF* */
6928   REPLY_MACRO2(VL_API_AF_PACKET_CREATE_REPLY,
6929   ({
6930     rmp->sw_if_index = clib_host_to_net_u32(sw_if_index);
6931   }));
6932   /* *INDENT-ON* */
6933 }
6934
6935 static void
6936 vl_api_af_packet_delete_t_handler (vl_api_af_packet_delete_t * mp)
6937 {
6938   vlib_main_t *vm = vlib_get_main ();
6939   vl_api_af_packet_delete_reply_t *rmp;
6940   int rv = 0;
6941   u8 *host_if_name = NULL;
6942
6943   host_if_name = format (0, "%s", mp->host_if_name);
6944   vec_add1 (host_if_name, 0);
6945
6946   rv = af_packet_delete_if (vm, host_if_name);
6947
6948   vec_free (host_if_name);
6949
6950   REPLY_MACRO (VL_API_AF_PACKET_DELETE_REPLY);
6951 }
6952
6953 static void
6954 vl_api_policer_add_del_t_handler (vl_api_policer_add_del_t * mp)
6955 {
6956   vlib_main_t *vm = vlib_get_main ();
6957   vl_api_policer_add_del_reply_t *rmp;
6958   int rv = 0;
6959   u8 *name = NULL;
6960   sse2_qos_pol_cfg_params_st cfg;
6961   clib_error_t *error;
6962   u32 policer_index;
6963
6964   name = format (0, "%s", mp->name);
6965
6966   memset (&cfg, 0, sizeof (cfg));
6967   cfg.rfc = mp->type;
6968   cfg.rnd_type = mp->round_type;
6969   cfg.rate_type = mp->rate_type;
6970   cfg.rb.kbps.cir_kbps = mp->cir;
6971   cfg.rb.kbps.eir_kbps = mp->eir;
6972   cfg.rb.kbps.cb_bytes = mp->cb;
6973   cfg.rb.kbps.eb_bytes = mp->eb;
6974   cfg.conform_action.action_type = mp->conform_action_type;
6975   cfg.conform_action.dscp = mp->conform_dscp;
6976   cfg.exceed_action.action_type = mp->exceed_action_type;
6977   cfg.exceed_action.dscp = mp->exceed_dscp;
6978   cfg.violate_action.action_type = mp->violate_action_type;
6979   cfg.violate_action.dscp = mp->violate_dscp;
6980   cfg.color_aware = mp->color_aware;
6981
6982   error = policer_add_del (vm, name, &cfg, &policer_index, mp->is_add);
6983
6984   if (error)
6985     rv = VNET_API_ERROR_UNSPECIFIED;
6986
6987   /* *INDENT-OFF* */
6988   REPLY_MACRO2(VL_API_POLICER_ADD_DEL_REPLY,
6989   ({
6990     if (rv == 0 &&  mp->is_add)
6991       rmp->policer_index = ntohl(policer_index);
6992     else
6993       rmp->policer_index = ~0;
6994   }));
6995   /* *INDENT-ON* */
6996 }
6997
6998 static void
6999 send_policer_details (u8 * name,
7000                       sse2_qos_pol_cfg_params_st * config,
7001                       policer_read_response_type_st * templ,
7002                       unix_shared_memory_queue_t * q, u32 context)
7003 {
7004   vl_api_policer_details_t *mp;
7005
7006   mp = vl_msg_api_alloc (sizeof (*mp));
7007   memset (mp, 0, sizeof (*mp));
7008   mp->_vl_msg_id = ntohs (VL_API_POLICER_DETAILS);
7009   mp->context = context;
7010   mp->cir = htonl (config->rb.kbps.cir_kbps);
7011   mp->eir = htonl (config->rb.kbps.eir_kbps);
7012   mp->cb = htonl (config->rb.kbps.cb_bytes);
7013   mp->eb = htonl (config->rb.kbps.eb_bytes);
7014   mp->rate_type = config->rate_type;
7015   mp->round_type = config->rnd_type;
7016   mp->type = config->rfc;
7017   mp->conform_action_type = config->conform_action.action_type;
7018   mp->conform_dscp = config->conform_action.dscp;
7019   mp->exceed_action_type = config->exceed_action.action_type;
7020   mp->exceed_dscp = config->exceed_action.dscp;
7021   mp->violate_action_type = config->violate_action.action_type;
7022   mp->violate_dscp = config->violate_action.dscp;
7023   mp->single_rate = templ->single_rate ? 1 : 0;
7024   mp->color_aware = templ->color_aware ? 1 : 0;
7025   mp->scale = htonl (templ->scale);
7026   mp->cir_tokens_per_period = htonl (templ->cir_tokens_per_period);
7027   mp->pir_tokens_per_period = htonl (templ->pir_tokens_per_period);
7028   mp->current_limit = htonl (templ->current_limit);
7029   mp->current_bucket = htonl (templ->current_bucket);
7030   mp->extended_limit = htonl (templ->extended_limit);
7031   mp->extended_bucket = htonl (templ->extended_bucket);
7032   mp->last_update_time = clib_host_to_net_u64 (templ->last_update_time);
7033
7034   strncpy ((char *) mp->name, (char *) name, ARRAY_LEN (mp->name) - 1);
7035
7036   vl_msg_api_send_shmem (q, (u8 *) & mp);
7037 }
7038
7039 static void
7040 vl_api_policer_dump_t_handler (vl_api_policer_dump_t * mp)
7041 {
7042   unix_shared_memory_queue_t *q;
7043   vnet_policer_main_t *pm = &vnet_policer_main;
7044   hash_pair_t *hp;
7045   uword *p;
7046   u32 pool_index;
7047   u8 *match_name = 0;
7048   u8 *name;
7049   sse2_qos_pol_cfg_params_st *config;
7050   policer_read_response_type_st *templ;
7051
7052   q = vl_api_client_index_to_input_queue (mp->client_index);
7053   if (q == 0)
7054     return;
7055
7056   if (mp->match_name_valid)
7057     {
7058       match_name = format (0, "%s%c", mp->match_name, 0);
7059     }
7060
7061   if (mp->match_name_valid)
7062     {
7063       p = hash_get_mem (pm->policer_config_by_name, match_name);
7064       if (p)
7065         {
7066           pool_index = p[0];
7067           config = pool_elt_at_index (pm->configs, pool_index);
7068           templ = pool_elt_at_index (pm->policer_templates, pool_index);
7069           send_policer_details (match_name, config, templ, q, mp->context);
7070         }
7071     }
7072   else
7073     {
7074       /* *INDENT-OFF* */
7075       hash_foreach_pair (hp, pm->policer_config_by_name,
7076       ({
7077         name = (u8 *) hp->key;
7078         pool_index = hp->value[0];
7079         config = pool_elt_at_index (pm->configs, pool_index);
7080         templ = pool_elt_at_index (pm->policer_templates, pool_index);
7081         send_policer_details(name, config, templ, q, mp->context);
7082       }));
7083       /* *INDENT-ON* */
7084     }
7085 }
7086
7087 static void
7088   vl_api_policer_classify_set_interface_t_handler
7089   (vl_api_policer_classify_set_interface_t * mp)
7090 {
7091   vlib_main_t *vm = vlib_get_main ();
7092   vl_api_policer_classify_set_interface_reply_t *rmp;
7093   int rv;
7094   u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
7095
7096   ip4_table_index = ntohl (mp->ip4_table_index);
7097   ip6_table_index = ntohl (mp->ip6_table_index);
7098   l2_table_index = ntohl (mp->l2_table_index);
7099   sw_if_index = ntohl (mp->sw_if_index);
7100
7101   VALIDATE_SW_IF_INDEX (mp);
7102
7103   rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index,
7104                                         ip6_table_index, l2_table_index,
7105                                         mp->is_add);
7106
7107   BAD_SW_IF_INDEX_LABEL;
7108
7109   REPLY_MACRO (VL_API_POLICER_CLASSIFY_SET_INTERFACE_REPLY);
7110 }
7111
7112 static void
7113 send_policer_classify_details (u32 sw_if_index,
7114                                u32 table_index,
7115                                unix_shared_memory_queue_t * q, u32 context)
7116 {
7117   vl_api_policer_classify_details_t *mp;
7118
7119   mp = vl_msg_api_alloc (sizeof (*mp));
7120   memset (mp, 0, sizeof (*mp));
7121   mp->_vl_msg_id = ntohs (VL_API_POLICER_CLASSIFY_DETAILS);
7122   mp->context = context;
7123   mp->sw_if_index = htonl (sw_if_index);
7124   mp->table_index = htonl (table_index);
7125
7126   vl_msg_api_send_shmem (q, (u8 *) & mp);
7127 }
7128
7129 static void
7130 vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
7131 {
7132   unix_shared_memory_queue_t *q;
7133   policer_classify_main_t *pcm = &policer_classify_main;
7134   u32 *vec_tbl;
7135   int i;
7136
7137   q = vl_api_client_index_to_input_queue (mp->client_index);
7138   if (q == 0)
7139     return;
7140
7141   vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
7142
7143   if (vec_len (vec_tbl))
7144     {
7145       for (i = 0; i < vec_len (vec_tbl); i++)
7146         {
7147           if (vec_elt (vec_tbl, i) == ~0)
7148             continue;
7149
7150           send_policer_classify_details (i, vec_elt (vec_tbl, i), q,
7151                                          mp->context);
7152         }
7153     }
7154 }
7155
7156 static void
7157 vl_api_netmap_create_t_handler (vl_api_netmap_create_t * mp)
7158 {
7159   vlib_main_t *vm = vlib_get_main ();
7160   vl_api_netmap_create_reply_t *rmp;
7161   int rv = 0;
7162   u8 *if_name = NULL;
7163
7164   if_name = format (0, "%s", mp->netmap_if_name);
7165   vec_add1 (if_name, 0);
7166
7167   rv =
7168     netmap_create_if (vm, if_name, mp->use_random_hw_addr ? 0 : mp->hw_addr,
7169                       mp->is_pipe, mp->is_master, 0);
7170
7171   vec_free (if_name);
7172
7173   REPLY_MACRO (VL_API_NETMAP_CREATE_REPLY);
7174 }
7175
7176 static void
7177 vl_api_netmap_delete_t_handler (vl_api_netmap_delete_t * mp)
7178 {
7179   vlib_main_t *vm = vlib_get_main ();
7180   vl_api_netmap_delete_reply_t *rmp;
7181   int rv = 0;
7182   u8 *if_name = NULL;
7183
7184   if_name = format (0, "%s", mp->netmap_if_name);
7185   vec_add1 (if_name, 0);
7186
7187   rv = netmap_delete_if (vm, if_name);
7188
7189   vec_free (if_name);
7190
7191   REPLY_MACRO (VL_API_NETMAP_DELETE_REPLY);
7192 }
7193
7194 static void
7195 vl_api_mpls_tunnel_details_t_handler (vl_api_mpls_fib_details_t * mp)
7196 {
7197   clib_warning ("BUG");
7198 }
7199
7200 typedef struct mpls_tunnel_send_walk_ctx_t_
7201 {
7202   unix_shared_memory_queue_t *q;
7203   u32 index;
7204   u32 context;
7205 } mpls_tunnel_send_walk_ctx_t;
7206
7207 static void
7208 send_mpls_tunnel_entry (u32 mti, void *arg)
7209 {
7210   mpls_tunnel_send_walk_ctx_t *ctx;
7211   vl_api_mpls_tunnel_details_t *mp;
7212   const mpls_tunnel_t *mt;
7213   u32 nlabels;
7214
7215   ctx = arg;
7216
7217   if (~0 != ctx->index && mti != ctx->index)
7218     return;
7219
7220   mt = mpls_tunnel_get (mti);
7221   nlabels = vec_len (mt->mt_label_stack);
7222
7223   mp = vl_msg_api_alloc (sizeof (*mp) + nlabels * sizeof (u32));
7224   memset (mp, 0, sizeof (*mp));
7225   mp->_vl_msg_id = ntohs (VL_API_MPLS_TUNNEL_DETAILS);
7226   mp->context = ctx->context;
7227
7228   mp->tunnel_index = ntohl (mti);
7229   memcpy (mp->mt_next_hop_out_labels,
7230           mt->mt_label_stack, nlabels * sizeof (u32));
7231
7232   // FIXME
7233
7234   vl_msg_api_send_shmem (ctx->q, (u8 *) & mp);
7235 }
7236
7237 static void
7238 vl_api_mpls_tunnel_dump_t_handler (vl_api_mpls_tunnel_dump_t * mp)
7239 {
7240   unix_shared_memory_queue_t *q;
7241
7242   q = vl_api_client_index_to_input_queue (mp->client_index);
7243   if (q == 0)
7244     return;
7245
7246   mpls_tunnel_send_walk_ctx_t ctx = {
7247     .q = q,
7248     .index = ntohl (mp->tunnel_index),
7249     .context = mp->context,
7250   };
7251   mpls_tunnel_walk (send_mpls_tunnel_entry, &ctx);
7252 }
7253
7254 static void
7255 vl_api_mpls_fib_details_t_handler (vl_api_mpls_fib_details_t * mp)
7256 {
7257   clib_warning ("BUG");
7258 }
7259
7260 static void
7261 vl_api_mpls_fib_details_t_endian (vl_api_mpls_fib_details_t * mp)
7262 {
7263   clib_warning ("BUG");
7264 }
7265
7266 static void
7267 vl_api_mpls_fib_details_t_print (vl_api_mpls_fib_details_t * mp)
7268 {
7269   clib_warning ("BUG");
7270 }
7271
7272
7273 static void
7274 copy_fib_next_hop (fib_route_path_encode_t * api_rpath,
7275                    vl_api_fib_path_t * fp)
7276 {
7277   int is_ip4;
7278
7279   if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP4)
7280     fp->afi = IP46_TYPE_IP4;
7281   else if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP6)
7282     fp->afi = IP46_TYPE_IP6;
7283   else
7284     {
7285       is_ip4 = ip46_address_is_ip4 (&api_rpath->rpath.frp_addr);
7286       if (is_ip4)
7287         fp->afi = IP46_TYPE_IP4;
7288       else
7289         fp->afi = IP46_TYPE_IP6;
7290     }
7291   if (fp->afi == IP46_TYPE_IP4)
7292     memcpy (fp->next_hop, &api_rpath->rpath.frp_addr.ip4,
7293             sizeof (api_rpath->rpath.frp_addr.ip4));
7294   else
7295     memcpy (fp->next_hop, &api_rpath->rpath.frp_addr.ip6,
7296             sizeof (api_rpath->rpath.frp_addr.ip6));
7297 }
7298
7299 static void
7300 send_mpls_fib_details (vpe_api_main_t * am,
7301                        unix_shared_memory_queue_t * q,
7302                        u32 table_id, u32 label, u32 eos,
7303                        fib_route_path_encode_t * api_rpaths, u32 context)
7304 {
7305   vl_api_mpls_fib_details_t *mp;
7306   fib_route_path_encode_t *api_rpath;
7307   vl_api_fib_path_t *fp;
7308   int path_count;
7309
7310   path_count = vec_len (api_rpaths);
7311   mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
7312   if (!mp)
7313     return;
7314   memset (mp, 0, sizeof (*mp));
7315   mp->_vl_msg_id = ntohs (VL_API_MPLS_FIB_DETAILS);
7316   mp->context = context;
7317
7318   mp->table_id = htonl (table_id);
7319   mp->eos_bit = eos;
7320   mp->label = htonl (label);
7321
7322   mp->count = htonl (path_count);
7323   fp = mp->path;
7324   vec_foreach (api_rpath, api_rpaths)
7325   {
7326     memset (fp, 0, sizeof (*fp));
7327     fp->weight = htonl (api_rpath->rpath.frp_weight);
7328     fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
7329     copy_fib_next_hop (api_rpath, fp);
7330     fp++;
7331   }
7332
7333   vl_msg_api_send_shmem (q, (u8 *) & mp);
7334 }
7335
7336 static void
7337 vl_api_mpls_fib_dump_t_handler (vl_api_mpls_fib_dump_t * mp)
7338 {
7339   vpe_api_main_t *am = &vpe_api_main;
7340   unix_shared_memory_queue_t *q;
7341   mpls_main_t *mm = &mpls_main;
7342   fib_table_t *fib_table;
7343   fib_node_index_t lfei, *lfeip, *lfeis = NULL;
7344   mpls_label_t key;
7345   fib_prefix_t pfx;
7346   u32 fib_index;
7347   fib_route_path_encode_t *api_rpaths;
7348
7349   q = vl_api_client_index_to_input_queue (mp->client_index);
7350   if (q == 0)
7351     return;
7352
7353   /* *INDENT-OFF* */
7354   pool_foreach (fib_table, mm->fibs,
7355   ({
7356     hash_foreach(key, lfei, fib_table->mpls.mf_entries,
7357     ({
7358         vec_add1(lfeis, lfei);
7359     }));
7360   }));
7361   vec_sort_with_function(lfeis, fib_entry_cmp_for_sort);
7362
7363   vec_foreach(lfeip, lfeis)
7364   {
7365     fib_entry_get_prefix(*lfeip, &pfx);
7366     fib_index = fib_entry_get_fib_index(*lfeip);
7367     fib_table = fib_table_get(fib_index, pfx.fp_proto);
7368     api_rpaths = NULL;
7369     fib_entry_encode(*lfeip, &api_rpaths);
7370     send_mpls_fib_details (am, q,
7371                            fib_table->ft_table_id,
7372                            pfx.fp_label,
7373                            pfx.fp_eos,
7374                            api_rpaths,
7375                            mp->context);
7376     vec_free(api_rpaths);
7377   }
7378
7379   vec_free (lfeis);
7380 }
7381
7382 static void
7383 vl_api_ip_fib_details_t_handler (vl_api_ip_fib_details_t * mp)
7384 {
7385   clib_warning ("BUG");
7386 }
7387
7388 static void
7389 vl_api_ip_fib_details_t_endian (vl_api_ip_fib_details_t * mp)
7390 {
7391   clib_warning ("BUG");
7392 }
7393
7394 static void
7395 vl_api_ip_fib_details_t_print (vl_api_ip_fib_details_t * mp)
7396 {
7397   clib_warning ("BUG");
7398 }
7399
7400 static void
7401 send_ip_fib_details (vpe_api_main_t * am,
7402                      unix_shared_memory_queue_t * q,
7403                      u32 table_id, fib_prefix_t *pfx,
7404                      fib_route_path_encode_t *api_rpaths, u32 context)
7405 {
7406   vl_api_ip_fib_details_t *mp;
7407   fib_route_path_encode_t *api_rpath;
7408   vl_api_fib_path_t *fp;
7409   int path_count;
7410
7411   path_count = vec_len(api_rpaths);
7412   mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
7413   if (!mp)
7414     return;
7415   memset (mp, 0, sizeof (*mp));
7416   mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
7417   mp->context = context;
7418
7419   mp->table_id = htonl (table_id);
7420   mp->address_length = pfx->fp_len;
7421   memcpy(mp->address, &pfx->fp_addr.ip4, sizeof(pfx->fp_addr.ip4));
7422
7423   mp->count = htonl (path_count);
7424   fp = mp->path;
7425   vec_foreach(api_rpath, api_rpaths)
7426   {
7427     memset (fp, 0, sizeof (*fp));
7428     switch (api_rpath->dpo.dpoi_type)
7429       {
7430       case DPO_RECEIVE:
7431         fp->is_local = true;
7432         break;
7433       case DPO_DROP:
7434         fp->is_drop = true;
7435         break;
7436       case DPO_IP_NULL:
7437         switch (api_rpath->dpo.dpoi_index)
7438           {
7439           case IP_NULL_ACTION_NONE:
7440             fp->is_drop = true;
7441             break;
7442           case IP_NULL_ACTION_SEND_ICMP_UNREACH:
7443             fp->is_unreach = true;
7444             break;
7445           case IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
7446             fp->is_prohibit = true;
7447             break;
7448           default:
7449             break;
7450           }
7451         break;
7452       default:
7453         break;
7454       }
7455     fp->weight = htonl(api_rpath->rpath.frp_weight);
7456     fp->sw_if_index = htonl(api_rpath->rpath.frp_sw_if_index);
7457     copy_fib_next_hop (api_rpath, fp);
7458     fp++;
7459   }
7460
7461   vl_msg_api_send_shmem (q, (u8 *) & mp);
7462 }
7463
7464 static void
7465 vl_api_ip_fib_dump_t_handler (vl_api_ip_fib_dump_t * mp)
7466 {
7467   vpe_api_main_t *am = &vpe_api_main;
7468   unix_shared_memory_queue_t *q;
7469   ip4_main_t *im = &ip4_main;
7470   fib_table_t *fib_table;
7471   fib_node_index_t lfei, *lfeip, *lfeis = NULL;
7472   mpls_label_t key;
7473   fib_prefix_t pfx;
7474   u32 fib_index;
7475   fib_route_path_encode_t *api_rpaths;
7476   int i;
7477
7478   q = vl_api_client_index_to_input_queue (mp->client_index);
7479   if (q == 0)
7480     return;
7481
7482   /* *INDENT-OFF* */
7483   pool_foreach (fib_table, im->fibs,
7484   ({
7485     for (i = 0; i < ARRAY_LEN (fib_table->v4.fib_entry_by_dst_address); i++)
7486       {
7487         hash_foreach(key, lfei, fib_table->v4.fib_entry_by_dst_address[i],
7488         ({
7489           vec_add1(lfeis, lfei);
7490         }));
7491       }
7492   }));
7493
7494   vec_sort_with_function(lfeis, fib_entry_cmp_for_sort);
7495
7496   vec_foreach(lfeip, lfeis)
7497   {
7498     fib_entry_get_prefix(*lfeip, &pfx);
7499     fib_index = fib_entry_get_fib_index(*lfeip);
7500     fib_table = fib_table_get(fib_index, pfx.fp_proto);
7501     api_rpaths = NULL;
7502     fib_entry_encode(*lfeip, &api_rpaths);
7503     send_ip_fib_details (am, q,
7504                          fib_table->ft_table_id,
7505                          &pfx,
7506                          api_rpaths,
7507                          mp->context);
7508     vec_free(api_rpaths);
7509   }
7510
7511   vec_free (lfeis);
7512 }
7513
7514 static void
7515 vl_api_ip6_fib_details_t_handler (vl_api_ip6_fib_details_t * mp)
7516 {
7517   clib_warning ("BUG");
7518 }
7519
7520 static void
7521 vl_api_ip6_fib_details_t_endian (vl_api_ip6_fib_details_t * mp)
7522 {
7523   clib_warning ("BUG");
7524 }
7525
7526 static void
7527 vl_api_ip6_fib_details_t_print (vl_api_ip6_fib_details_t * mp)
7528 {
7529   clib_warning ("BUG");
7530 }
7531
7532 static void
7533 send_ip6_fib_details (vpe_api_main_t * am,
7534                       unix_shared_memory_queue_t * q,
7535                       u32 table_id, fib_prefix_t *pfx,
7536                       fib_route_path_encode_t *api_rpaths, u32 context)
7537 {
7538   vl_api_ip6_fib_details_t *mp;
7539   fib_route_path_encode_t *api_rpath;
7540   vl_api_fib_path_t *fp;
7541   int path_count;
7542
7543   path_count = vec_len(api_rpaths);
7544   mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
7545   if (!mp)
7546     return;
7547   memset (mp, 0, sizeof (*mp));
7548   mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
7549   mp->context = context;
7550
7551   mp->table_id = htonl (table_id);
7552   mp->address_length = pfx->fp_len;
7553   memcpy(mp->address, &pfx->fp_addr.ip6, sizeof(pfx->fp_addr.ip6));
7554
7555   mp->count = htonl (path_count);
7556   fp = mp->path;
7557   vec_foreach(api_rpath, api_rpaths)
7558   {
7559     memset (fp, 0, sizeof (*fp));
7560     switch (api_rpath->dpo.dpoi_type)
7561       {
7562       case DPO_RECEIVE:
7563         fp->is_local = true;
7564         break;
7565       case DPO_DROP:
7566         fp->is_drop = true;
7567         break;
7568       case DPO_IP_NULL:
7569         switch (api_rpath->dpo.dpoi_index)
7570           {
7571           case IP_NULL_DPO_ACTION_NUM+IP_NULL_ACTION_NONE:
7572             fp->is_drop = true;
7573             break;
7574           case IP_NULL_DPO_ACTION_NUM+IP_NULL_ACTION_SEND_ICMP_UNREACH:
7575             fp->is_unreach = true;
7576             break;
7577           case IP_NULL_DPO_ACTION_NUM+IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
7578             fp->is_prohibit = true;
7579             break;
7580           default:
7581             break;
7582           }
7583         break;
7584       default:
7585         break;
7586       }
7587     fp->weight = htonl(api_rpath->rpath.frp_weight);
7588     fp->sw_if_index = htonl(api_rpath->rpath.frp_sw_if_index);
7589     copy_fib_next_hop (api_rpath, fp);
7590     fp++;
7591   }
7592
7593   vl_msg_api_send_shmem (q, (u8 *) & mp);
7594 }
7595
7596 typedef struct apt_ip6_fib_show_ctx_t_ {
7597     u32 fib_index;
7598     fib_node_index_t *entries;
7599 } api_ip6_fib_show_ctx_t;
7600
7601 static void
7602 api_ip6_fib_table_put_entries (clib_bihash_kv_24_8_t * kvp,
7603                                void *arg)
7604 {
7605   api_ip6_fib_show_ctx_t *ctx = arg;
7606
7607   if ((kvp->key[2] >> 32) == ctx->fib_index)
7608     {
7609       vec_add1(ctx->entries, kvp->value);
7610     }
7611 }
7612
7613 static void
7614 api_ip6_fib_table_get_all (unix_shared_memory_queue_t *q,
7615                            vl_api_ip6_fib_dump_t *mp,
7616                            fib_table_t *fib_table)
7617 {
7618   vpe_api_main_t *am = &vpe_api_main;
7619   ip6_main_t *im6 = &ip6_main;
7620   ip6_fib_t *fib = &fib_table->v6;
7621   fib_node_index_t *fib_entry_index;
7622   api_ip6_fib_show_ctx_t ctx = {
7623     .fib_index = fib->index,
7624     .entries = NULL,
7625   };
7626   fib_route_path_encode_t *api_rpaths;
7627   fib_prefix_t pfx;
7628
7629   BV(clib_bihash_foreach_key_value_pair)
7630     ((BVT(clib_bihash) *) &im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
7631      api_ip6_fib_table_put_entries,
7632      &ctx);
7633
7634   vec_sort_with_function(ctx.entries, fib_entry_cmp_for_sort);
7635
7636   vec_foreach(fib_entry_index, ctx.entries)
7637     {
7638       fib_entry_get_prefix(*fib_entry_index, &pfx);
7639       api_rpaths = NULL;
7640       fib_entry_encode(*fib_entry_index, &api_rpaths);
7641       send_ip6_fib_details (am, q,
7642                             fib_table->ft_table_id,
7643                             &pfx,
7644                             api_rpaths,
7645                             mp->context);
7646       vec_free(api_rpaths);
7647     }
7648
7649   vec_free(ctx.entries);
7650 }
7651
7652 static void
7653 vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
7654 {
7655   unix_shared_memory_queue_t *q;
7656   ip6_main_t *im6 = &ip6_main;
7657   fib_table_t *fib_table;
7658
7659   q = vl_api_client_index_to_input_queue (mp->client_index);
7660   if (q == 0)
7661     return;
7662
7663   /* *INDENT-OFF* */
7664   pool_foreach (fib_table, im6->fibs,
7665   ({
7666     api_ip6_fib_table_get_all(q, mp, fib_table);
7667   }));
7668 }
7669
7670 static void
7671 vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
7672 {
7673   unix_shared_memory_queue_t *q;
7674
7675   q = vl_api_client_index_to_input_queue (mp->client_index);
7676   if (q == 0)
7677     return;
7678
7679   vnet_classify_main_t *cm = &vnet_classify_main;
7680   vnet_classify_table_t *t;
7681   u32 *table_ids = 0;
7682   u32 count;
7683
7684   /* *INDENT-OFF* */
7685   pool_foreach (t, cm->tables,
7686   ({
7687     vec_add1 (table_ids, ntohl(t - cm->tables));
7688   }));
7689   /* *INDENT-ON* */
7690   count = vec_len (table_ids);
7691
7692   vl_api_classify_table_ids_reply_t *rmp;
7693   rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
7694   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
7695   rmp->context = mp->context;
7696   rmp->count = ntohl (count);
7697   clib_memcpy (rmp->ids, table_ids, count * sizeof (u32));
7698   rmp->retval = 0;
7699
7700   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7701
7702   vec_free (table_ids);
7703 }
7704
7705 static void
7706   vl_api_classify_table_by_interface_t_handler
7707   (vl_api_classify_table_by_interface_t * mp)
7708 {
7709   vl_api_classify_table_by_interface_reply_t *rmp;
7710   int rv = 0;
7711
7712   u32 sw_if_index = ntohl (mp->sw_if_index);
7713   u32 *acl = 0;
7714
7715   vec_validate (acl, INPUT_ACL_N_TABLES - 1);
7716   vec_set (acl, ~0);
7717
7718   VALIDATE_SW_IF_INDEX (mp);
7719
7720   input_acl_main_t *am = &input_acl_main;
7721
7722   int if_idx;
7723   u32 type;
7724
7725   for (type = 0; type < INPUT_ACL_N_TABLES; type++)
7726     {
7727       u32 *vec_tbl = am->classify_table_index_by_sw_if_index[type];
7728       if (vec_len (vec_tbl))
7729         {
7730           for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
7731             {
7732               if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
7733                 {
7734                   continue;
7735                 }
7736               acl[type] = vec_elt (vec_tbl, if_idx);
7737             }
7738         }
7739     }
7740
7741   BAD_SW_IF_INDEX_LABEL;
7742
7743   /* *INDENT-OFF* */
7744   REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
7745   ({
7746     rmp->sw_if_index = ntohl(sw_if_index);
7747     rmp->l2_table_id = ntohl(acl[INPUT_ACL_TABLE_L2]);
7748     rmp->ip4_table_id = ntohl(acl[INPUT_ACL_TABLE_IP4]);
7749     rmp->ip6_table_id = ntohl(acl[INPUT_ACL_TABLE_IP6]);
7750   }));
7751   /* *INDENT-ON* */
7752   vec_free (acl);
7753 }
7754
7755 static void
7756 vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
7757 {
7758   unix_shared_memory_queue_t *q;
7759
7760   q = vl_api_client_index_to_input_queue (mp->client_index);
7761   if (q == 0)
7762     return;
7763
7764   vl_api_classify_table_info_reply_t *rmp = 0;
7765
7766   vnet_classify_main_t *cm = &vnet_classify_main;
7767   u32 table_id = ntohl (mp->table_id);
7768   vnet_classify_table_t *t;
7769
7770   /* *INDENT-OFF* */
7771   pool_foreach (t, cm->tables,
7772   ({
7773     if (table_id == t - cm->tables)
7774       {
7775         rmp = vl_msg_api_alloc_as_if_client
7776           (sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
7777         rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
7778         rmp->context = mp->context;
7779         rmp->table_id = ntohl(table_id);
7780         rmp->nbuckets = ntohl(t->nbuckets);
7781         rmp->match_n_vectors = ntohl(t->match_n_vectors);
7782         rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
7783         rmp->active_sessions = ntohl(t->active_elements);
7784         rmp->next_table_index = ntohl(t->next_table_index);
7785         rmp->miss_next_index = ntohl(t->miss_next_index);
7786         rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
7787         clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
7788         rmp->retval = 0;
7789         break;
7790       }
7791   }));
7792   /* *INDENT-ON* */
7793
7794   if (rmp == 0)
7795     {
7796       rmp = vl_msg_api_alloc (sizeof (*rmp));
7797       rmp->_vl_msg_id = ntohs ((VL_API_CLASSIFY_TABLE_INFO_REPLY));
7798       rmp->context = mp->context;
7799       rmp->retval = ntohl (VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
7800     }
7801
7802   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7803 }
7804
7805 static void
7806 vl_api_classify_session_details_t_handler (vl_api_classify_session_details_t *
7807                                            mp)
7808 {
7809   clib_warning ("BUG");
7810 }
7811
7812 static void
7813 send_classify_session_details (unix_shared_memory_queue_t * q,
7814                                u32 table_id,
7815                                u32 match_length,
7816                                vnet_classify_entry_t * e, u32 context)
7817 {
7818   vl_api_classify_session_details_t *rmp;
7819
7820   rmp = vl_msg_api_alloc (sizeof (*rmp));
7821   memset (rmp, 0, sizeof (*rmp));
7822   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_SESSION_DETAILS);
7823   rmp->context = context;
7824   rmp->table_id = ntohl (table_id);
7825   rmp->hit_next_index = ntohl (e->next_index);
7826   rmp->advance = ntohl (e->advance);
7827   rmp->opaque_index = ntohl (e->opaque_index);
7828   rmp->match_length = ntohl (match_length);
7829   clib_memcpy (rmp->match, e->key, match_length);
7830
7831   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7832 }
7833
7834 static void
7835 vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
7836 {
7837   vnet_classify_main_t *cm = &vnet_classify_main;
7838   unix_shared_memory_queue_t *q;
7839
7840   u32 table_id = ntohl (mp->table_id);
7841   vnet_classify_table_t *t;
7842
7843   q = vl_api_client_index_to_input_queue (mp->client_index);
7844   if (!q)
7845     return;
7846
7847   /* *INDENT-OFF* */
7848   pool_foreach (t, cm->tables,
7849   ({
7850     if (table_id == t - cm->tables)
7851       {
7852         vnet_classify_bucket_t * b;
7853         vnet_classify_entry_t * v, * save_v;
7854         int i, j, k;
7855
7856         for (i = 0; i < t->nbuckets; i++)
7857           {
7858             b = &t->buckets [i];
7859             if (b->offset == 0)
7860               continue;
7861
7862             save_v = vnet_classify_get_entry (t, b->offset);
7863             for (j = 0; j < (1<<b->log2_pages); j++)
7864               {
7865                 for (k = 0; k < t->entries_per_page; k++)
7866                   {
7867                     v = vnet_classify_entry_at_index
7868                       (t, save_v, j*t->entries_per_page + k);
7869                     if (vnet_classify_entry_is_free (v))
7870                       continue;
7871
7872                     send_classify_session_details
7873                       (q, table_id, t->match_n_vectors * sizeof (u32x4),
7874                        v, mp->context);
7875                   }
7876               }
7877           }
7878         break;
7879       }
7880   }));
7881   /* *INDENT-ON* */
7882 }
7883
7884 static void
7885 vl_api_set_ipfix_exporter_t_handler (vl_api_set_ipfix_exporter_t * mp)
7886 {
7887   vlib_main_t *vm = vlib_get_main ();
7888   flow_report_main_t *frm = &flow_report_main;
7889   vl_api_set_ipfix_exporter_reply_t *rmp;
7890   ip4_address_t collector, src;
7891   u16 collector_port = UDP_DST_PORT_ipfix;
7892   u32 path_mtu;
7893   u32 template_interval;
7894   u8 udp_checksum;
7895   u32 fib_id;
7896   u32 fib_index = ~0;
7897   int rv = 0;
7898
7899   memcpy (collector.data, mp->collector_address, sizeof (collector.data));
7900   collector_port = ntohs (mp->collector_port);
7901   if (collector_port == (u16) ~ 0)
7902     collector_port = UDP_DST_PORT_ipfix;
7903   memcpy (src.data, mp->src_address, sizeof (src.data));
7904   fib_id = ntohl (mp->vrf_id);
7905
7906   ip4_main_t *im = &ip4_main;
7907   if (fib_id == ~0)
7908     {
7909       fib_index = ~0;
7910     }
7911   else
7912     {
7913       uword *p = hash_get (im->fib_index_by_table_id, fib_id);
7914       if (!p)
7915         {
7916           rv = VNET_API_ERROR_NO_SUCH_FIB;
7917           goto out;
7918         }
7919       fib_index = p[0];
7920     }
7921
7922   path_mtu = ntohl (mp->path_mtu);
7923   if (path_mtu == ~0)
7924     path_mtu = 512;             // RFC 7011 section 10.3.3.
7925   template_interval = ntohl (mp->template_interval);
7926   if (template_interval == ~0)
7927     template_interval = 20;
7928   udp_checksum = mp->udp_checksum;
7929
7930   if (collector.as_u32 == 0)
7931     {
7932       rv = VNET_API_ERROR_INVALID_VALUE;
7933       goto out;
7934     }
7935
7936   if (src.as_u32 == 0)
7937     {
7938       rv = VNET_API_ERROR_INVALID_VALUE;
7939       goto out;
7940     }
7941
7942   if (path_mtu > 1450 /* vpp does not support fragmentation */ )
7943     {
7944       rv = VNET_API_ERROR_INVALID_VALUE;
7945       goto out;
7946     }
7947
7948   if (path_mtu < 68)
7949     {
7950       rv = VNET_API_ERROR_INVALID_VALUE;
7951       goto out;
7952     }
7953
7954   /* Reset report streams if we are reconfiguring IP addresses */
7955   if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
7956       frm->src_address.as_u32 != src.as_u32 ||
7957       frm->collector_port != collector_port)
7958     vnet_flow_reports_reset (frm);
7959
7960   frm->ipfix_collector.as_u32 = collector.as_u32;
7961   frm->collector_port = collector_port;
7962   frm->src_address.as_u32 = src.as_u32;
7963   frm->fib_index = fib_index;
7964   frm->path_mtu = path_mtu;
7965   frm->template_interval = template_interval;
7966   frm->udp_checksum = udp_checksum;
7967
7968   /* Turn on the flow reporting process */
7969   vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
7970
7971 out:
7972   REPLY_MACRO (VL_API_SET_IPFIX_EXPORTER_REPLY);
7973 }
7974
7975 static void
7976 vl_api_ipfix_exporter_dump_t_handler (vl_api_ipfix_exporter_dump_t * mp)
7977 {
7978   flow_report_main_t *frm = &flow_report_main;
7979   unix_shared_memory_queue_t *q;
7980   vl_api_ipfix_exporter_details_t *rmp;
7981   ip4_main_t *im = &ip4_main;
7982   u32 vrf_id;
7983
7984   q = vl_api_client_index_to_input_queue (mp->client_index);
7985   if (!q)
7986     return;
7987
7988   rmp = vl_msg_api_alloc (sizeof (*rmp));
7989   memset (rmp, 0, sizeof (*rmp));
7990   rmp->_vl_msg_id = ntohs (VL_API_IPFIX_EXPORTER_DETAILS);
7991   rmp->context = mp->context;
7992   memcpy (rmp->collector_address, frm->ipfix_collector.data,
7993           sizeof (frm->ipfix_collector.data));
7994   rmp->collector_port = htons (frm->collector_port);
7995   memcpy (rmp->src_address, frm->src_address.data,
7996           sizeof (frm->src_address.data));
7997   if (frm->fib_index == ~0)
7998     vrf_id = ~0;
7999   else
8000     vrf_id = im->fibs[frm->fib_index].ft_table_id;
8001   rmp->vrf_id = htonl (vrf_id);
8002   rmp->path_mtu = htonl (frm->path_mtu);
8003   rmp->template_interval = htonl (frm->template_interval);
8004   rmp->udp_checksum = (frm->udp_checksum != 0);
8005
8006   vl_msg_api_send_shmem (q, (u8 *) & rmp);
8007 }
8008
8009 static void
8010   vl_api_set_ipfix_classify_stream_t_handler
8011   (vl_api_set_ipfix_classify_stream_t * mp)
8012 {
8013   vl_api_set_ipfix_classify_stream_reply_t *rmp;
8014   flow_report_classify_main_t *fcm = &flow_report_classify_main;
8015   flow_report_main_t *frm = &flow_report_main;
8016   u32 domain_id = 0;
8017   u32 src_port = UDP_DST_PORT_ipfix;
8018   int rv = 0;
8019
8020   domain_id = ntohl (mp->domain_id);
8021   src_port = ntohs (mp->src_port);
8022
8023   if (fcm->src_port != 0 &&
8024       (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
8025     {
8026       int rv = vnet_stream_change (frm, fcm->domain_id, fcm->src_port,
8027                                    domain_id, (u16) src_port);
8028       ASSERT (rv == 0);
8029     }
8030
8031   fcm->domain_id = domain_id;
8032   fcm->src_port = (u16) src_port;
8033
8034   REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
8035 }
8036
8037 static void
8038   vl_api_ipfix_classify_stream_dump_t_handler
8039   (vl_api_ipfix_classify_stream_dump_t * mp)
8040 {
8041   flow_report_classify_main_t *fcm = &flow_report_classify_main;
8042   unix_shared_memory_queue_t *q;
8043   vl_api_ipfix_classify_stream_details_t *rmp;
8044
8045   q = vl_api_client_index_to_input_queue (mp->client_index);
8046   if (!q)
8047     return;
8048
8049   rmp = vl_msg_api_alloc (sizeof (*rmp));
8050   memset (rmp, 0, sizeof (*rmp));
8051   rmp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_STREAM_DETAILS);
8052   rmp->context = mp->context;
8053   rmp->domain_id = htonl (fcm->domain_id);
8054   rmp->src_port = htons (fcm->src_port);
8055
8056   vl_msg_api_send_shmem (q, (u8 *) & rmp);
8057 }
8058
8059 static void
8060   vl_api_ipfix_classify_table_add_del_t_handler
8061   (vl_api_ipfix_classify_table_add_del_t * mp)
8062 {
8063   vl_api_ipfix_classify_table_add_del_reply_t *rmp;
8064   flow_report_classify_main_t *fcm = &flow_report_classify_main;
8065   flow_report_main_t *frm = &flow_report_main;
8066   vnet_flow_report_add_del_args_t args;
8067   ipfix_classify_table_t *table;
8068   int is_add;
8069   u32 classify_table_index;
8070   u8 ip_version;
8071   u8 transport_protocol;
8072   int rv = 0;
8073
8074   classify_table_index = ntohl (mp->table_id);
8075   ip_version = mp->ip_version;
8076   transport_protocol = mp->transport_protocol;
8077   is_add = mp->is_add;
8078
8079   if (fcm->src_port == 0)
8080     {
8081       /* call set_ipfix_classify_stream first */
8082       rv = VNET_API_ERROR_UNSPECIFIED;
8083       goto out;
8084     }
8085
8086   memset (&args, 0, sizeof (args));
8087
8088   table = 0;
8089   int i;
8090   for (i = 0; i < vec_len (fcm->tables); i++)
8091     if (ipfix_classify_table_index_valid (i))
8092       if (fcm->tables[i].classify_table_index == classify_table_index)
8093         {
8094           table = &fcm->tables[i];
8095           break;
8096         }
8097
8098   if (is_add)
8099     {
8100       if (table)
8101         {
8102           rv = VNET_API_ERROR_VALUE_EXIST;
8103           goto out;
8104         }
8105       table = ipfix_classify_add_table ();
8106       table->classify_table_index = classify_table_index;
8107     }
8108   else
8109     {
8110       if (!table)
8111         {
8112           rv = VNET_API_ERROR_NO_SUCH_ENTRY;
8113           goto out;
8114         }
8115     }
8116
8117   table->ip_version = ip_version;
8118   table->transport_protocol = transport_protocol;
8119
8120   args.opaque.as_uword = table - fcm->tables;
8121   args.rewrite_callback = ipfix_classify_template_rewrite;
8122   args.flow_data_callback = ipfix_classify_send_flows;
8123   args.is_add = is_add;
8124   args.domain_id = fcm->domain_id;
8125   args.src_port = fcm->src_port;
8126
8127   rv = vnet_flow_report_add_del (frm, &args);
8128
8129   /* If deleting, or add failed */
8130   if (is_add == 0 || (rv && is_add))
8131     ipfix_classify_delete_table (table - fcm->tables);
8132
8133 out:
8134   REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
8135 }
8136
8137 static void
8138 send_ipfix_classify_table_details (u32 table_index,
8139                                    unix_shared_memory_queue_t * q,
8140                                    u32 context)
8141 {
8142   flow_report_classify_main_t *fcm = &flow_report_classify_main;
8143   vl_api_ipfix_classify_table_details_t *mp;
8144
8145   ipfix_classify_table_t *table = &fcm->tables[table_index];
8146
8147   mp = vl_msg_api_alloc (sizeof (*mp));
8148   memset (mp, 0, sizeof (*mp));
8149   mp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_TABLE_DETAILS);
8150   mp->context = context;
8151   mp->table_id = htonl (table->classify_table_index);
8152   mp->ip_version = table->ip_version;
8153   mp->transport_protocol = table->transport_protocol;
8154
8155   vl_msg_api_send_shmem (q, (u8 *) & mp);
8156 }
8157
8158 static void
8159   vl_api_ipfix_classify_table_dump_t_handler
8160   (vl_api_ipfix_classify_table_dump_t * mp)
8161 {
8162   flow_report_classify_main_t *fcm = &flow_report_classify_main;
8163   unix_shared_memory_queue_t *q;
8164   u32 i;
8165
8166   q = vl_api_client_index_to_input_queue (mp->client_index);
8167   if (!q)
8168     return;
8169
8170   for (i = 0; i < vec_len (fcm->tables); i++)
8171     if (ipfix_classify_table_index_valid (i))
8172       send_ipfix_classify_table_details (i, q, mp->context);
8173 }
8174
8175 static void
8176   vl_api_sw_interface_span_enable_disable_t_handler
8177   (vl_api_sw_interface_span_enable_disable_t * mp)
8178 {
8179   vl_api_sw_interface_span_enable_disable_reply_t *rmp;
8180   int rv;
8181
8182   vlib_main_t *vm = vlib_get_main ();
8183
8184   rv = span_add_delete_entry (vm, ntohl (mp->sw_if_index_from),
8185                               ntohl (mp->sw_if_index_to), mp->enable);
8186
8187   REPLY_MACRO (VL_API_SW_INTERFACE_SPAN_ENABLE_DISABLE_REPLY);
8188 }
8189
8190 static void
8191 vl_api_sw_interface_span_dump_t_handler (vl_api_sw_interface_span_dump_t * mp)
8192 {
8193
8194   unix_shared_memory_queue_t *q;
8195   vl_api_sw_interface_span_details_t *rmp;
8196   span_main_t *sm = &span_main;
8197   u32 src_sw_if_index = 0, *dst_sw_if_index;
8198
8199   q = vl_api_client_index_to_input_queue (mp->client_index);
8200   if (!q)
8201     return;
8202
8203   vec_foreach (dst_sw_if_index, sm->dst_by_src_sw_if_index)
8204   {
8205     if (*dst_sw_if_index > 0)
8206       {
8207         rmp = vl_msg_api_alloc (sizeof (*rmp));
8208         memset (rmp, 0, sizeof (*rmp));
8209         rmp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SPAN_DETAILS);
8210         rmp->context = mp->context;
8211
8212         rmp->sw_if_index_from = htonl (src_sw_if_index);
8213         rmp->sw_if_index_to = htonl (*dst_sw_if_index);
8214
8215         vl_msg_api_send_shmem (q, (u8 *) & rmp);
8216       }
8217     src_sw_if_index++;
8218   }
8219 }
8220
8221 static void
8222 vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
8223 {
8224   vl_api_pg_create_interface_reply_t *rmp;
8225   int rv = 0;
8226
8227   pg_main_t *pg = &pg_main;
8228   u32 pg_if_id = pg_interface_add_or_get (pg, ntohl (mp->interface_id));
8229   pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
8230
8231   /* *INDENT-OFF* */
8232   REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
8233   ({
8234     rmp->sw_if_index = ntohl(pi->sw_if_index);
8235   }));
8236   /* *INDENT-ON* */
8237 }
8238
8239 static void
8240 vl_api_pg_capture_t_handler (vl_api_pg_capture_t * mp)
8241 {
8242   vl_api_pg_capture_reply_t *rmp;
8243   int rv = 0;
8244
8245   vnet_main_t *vnm = vnet_get_main ();
8246   vnet_interface_main_t *im = &vnm->interface_main;
8247   vnet_hw_interface_t *hi = 0;
8248
8249   u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
8250   u32 hw_if_index = ~0;
8251   uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
8252   if (p)
8253     hw_if_index = *p;
8254   vec_free (intf_name);
8255
8256   if (hw_if_index != ~0)
8257     {
8258       pg_capture_args_t _a, *a = &_a;
8259
8260       u32 len = ntohl (mp->pcap_name_length);
8261       u8 *pcap_file_name = vec_new (u8, len);
8262       clib_memcpy (pcap_file_name, mp->pcap_file_name, len);
8263
8264       hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
8265       a->hw_if_index = hw_if_index;
8266       a->dev_instance = hi->dev_instance;
8267       a->is_enabled = mp->is_enabled;
8268       a->pcap_file_name = pcap_file_name;
8269       a->count = ntohl (mp->count);
8270
8271       clib_error_t *e = pg_capture (a);
8272       if (e)
8273         {
8274           clib_error_report (e);
8275           rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
8276         }
8277
8278       vec_free (pcap_file_name);
8279     }
8280   REPLY_MACRO (VL_API_PG_CAPTURE_REPLY);
8281 }
8282
8283 static void
8284 vl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t * mp)
8285 {
8286   vl_api_pg_enable_disable_reply_t *rmp;
8287   int rv = 0;
8288
8289   pg_main_t *pg = &pg_main;
8290   u32 stream_index = ~0;
8291
8292   int is_enable = mp->is_enabled != 0;
8293   u32 len = ntohl (mp->stream_name_length) - 1;
8294
8295   if (len > 0)
8296     {
8297       u8 *stream_name = vec_new (u8, len);
8298       clib_memcpy (stream_name, mp->stream_name, len);
8299       uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
8300       if (p)
8301         stream_index = *p;
8302       vec_free (stream_name);
8303     }
8304
8305   pg_enable_disable (stream_index, is_enable);
8306
8307   REPLY_MACRO (VL_API_PG_ENABLE_DISABLE_REPLY);
8308 }
8309
8310 static void
8311   vl_api_ip_source_and_port_range_check_add_del_t_handler
8312   (vl_api_ip_source_and_port_range_check_add_del_t * mp)
8313 {
8314   vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
8315   int rv = 0;
8316
8317   u8 is_ipv6 = mp->is_ipv6;
8318   u8 is_add = mp->is_add;
8319   u8 mask_length = mp->mask_length;
8320   ip4_address_t ip4_addr;
8321   ip6_address_t ip6_addr;
8322   u16 *low_ports = 0;
8323   u16 *high_ports = 0;
8324   u32 vrf_id;
8325   u16 tmp_low, tmp_high;
8326   u8 num_ranges;
8327   int i;
8328
8329   // Validate port range
8330   num_ranges = mp->number_of_ranges;
8331   if (num_ranges > 32)
8332     {                           // This is size of array in VPE.API
8333       rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
8334       goto reply;
8335     }
8336
8337   vec_reset_length (low_ports);
8338   vec_reset_length (high_ports);
8339
8340   for (i = 0; i < num_ranges; i++)
8341     {
8342       tmp_low = mp->low_ports[i];
8343       tmp_high = mp->high_ports[i];
8344       // If tmp_low <= tmp_high then only need to check tmp_low = 0
8345       // If tmp_low <= tmp_high then only need to check tmp_high > 65535
8346       if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
8347         {
8348           rv = VNET_API_ERROR_INVALID_VALUE;
8349           goto reply;
8350         }
8351       vec_add1 (low_ports, tmp_low);
8352       vec_add1 (high_ports, tmp_high + 1);
8353     }
8354
8355   // Validate mask_length
8356   if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
8357     {
8358       rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
8359       goto reply;
8360     }
8361
8362   vrf_id = ntohl (mp->vrf_id);
8363
8364   if (vrf_id < 1)
8365     {
8366       rv = VNET_API_ERROR_INVALID_VALUE;
8367       goto reply;
8368     }
8369
8370
8371   if (is_ipv6)
8372     {
8373       clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
8374       rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
8375                                                     mask_length,
8376                                                     vrf_id,
8377                                                     low_ports,
8378                                                     high_ports, is_add);
8379     }
8380   else
8381     {
8382       clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
8383       rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
8384                                                     mask_length,
8385                                                     vrf_id,
8386                                                     low_ports,
8387                                                     high_ports, is_add);
8388     }
8389
8390 reply:
8391   vec_free (low_ports);
8392   vec_free (high_ports);
8393   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
8394 }
8395
8396 static void
8397   vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
8398   (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
8399 {
8400   vlib_main_t *vm = vlib_get_main ();
8401   vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
8402   ip4_main_t *im = &ip4_main;
8403   int rv;
8404   u32 sw_if_index;
8405   u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
8406   u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
8407   uword *p = 0;
8408   int i;
8409
8410   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
8411     ntohl (mp->tcp_out_vrf_id);
8412   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
8413     ntohl (mp->udp_out_vrf_id);
8414   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
8415     ntohl (mp->tcp_in_vrf_id);
8416   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
8417     ntohl (mp->udp_in_vrf_id);
8418
8419
8420   for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
8421     {
8422       if (vrf_id[i] != 0 && vrf_id[i] != ~0)
8423         {
8424           p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
8425
8426           if (p == 0)
8427             {
8428               rv = VNET_API_ERROR_INVALID_VALUE;
8429               goto reply;
8430             }
8431
8432           fib_index[i] = p[0];
8433         }
8434       else
8435         fib_index[i] = ~0;
8436     }
8437   sw_if_index = ntohl (mp->sw_if_index);
8438
8439   VALIDATE_SW_IF_INDEX (mp);
8440
8441   rv =
8442     set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
8443                                         mp->is_add);
8444
8445   BAD_SW_IF_INDEX_LABEL;
8446 reply:
8447
8448   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
8449 }
8450
8451 static void
8452 vl_api_ipsec_gre_add_del_tunnel_t_handler (vl_api_ipsec_gre_add_del_tunnel_t *
8453                                            mp)
8454 {
8455   vl_api_ipsec_gre_add_del_tunnel_reply_t *rmp;
8456   int rv = 0;
8457   vnet_ipsec_gre_add_del_tunnel_args_t _a, *a = &_a;
8458   u32 sw_if_index = ~0;
8459
8460   /* Check src & dst are different */
8461   if (memcmp (mp->src_address, mp->dst_address, 4) == 0)
8462     {
8463       rv = VNET_API_ERROR_SAME_SRC_DST;
8464       goto out;
8465     }
8466
8467   memset (a, 0, sizeof (*a));
8468
8469   /* ip addresses sent in network byte order */
8470   clib_memcpy (&(a->src), mp->src_address, 4);
8471   clib_memcpy (&(a->dst), mp->dst_address, 4);
8472   a->is_add = mp->is_add;
8473   a->lsa = ntohl (mp->local_sa_id);
8474   a->rsa = ntohl (mp->remote_sa_id);
8475
8476   rv = vnet_ipsec_gre_add_del_tunnel (a, &sw_if_index);
8477
8478 out:
8479     /* *INDENT-OFF* */
8480     REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY,
8481     ({
8482         rmp->sw_if_index = ntohl (sw_if_index);
8483     }));
8484     /* *INDENT-ON* */
8485 }
8486
8487 static void send_ipsec_gre_tunnel_details
8488   (ipsec_gre_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
8489 {
8490   vl_api_ipsec_gre_tunnel_details_t *rmp;
8491
8492   rmp = vl_msg_api_alloc (sizeof (*rmp));
8493   memset (rmp, 0, sizeof (*rmp));
8494   rmp->_vl_msg_id = ntohs (VL_API_IPSEC_GRE_TUNNEL_DETAILS);
8495   clib_memcpy (rmp->src_address, &(t->tunnel_src), 4);
8496   clib_memcpy (rmp->dst_address, &(t->tunnel_dst), 4);
8497   rmp->sw_if_index = htonl (t->sw_if_index);
8498   rmp->local_sa_id = htonl (t->local_sa_id);
8499   rmp->remote_sa_id = htonl (t->remote_sa_id);
8500   rmp->context = context;
8501
8502   vl_msg_api_send_shmem (q, (u8 *) & rmp);
8503 }
8504
8505 static void vl_api_ipsec_gre_tunnel_dump_t_handler
8506   (vl_api_ipsec_gre_tunnel_dump_t * mp)
8507 {
8508   unix_shared_memory_queue_t *q;
8509   ipsec_gre_main_t *igm = &ipsec_gre_main;
8510   ipsec_gre_tunnel_t *t;
8511   u32 sw_if_index;
8512
8513   q = vl_api_client_index_to_input_queue (mp->client_index);
8514   if (q == 0)
8515     {
8516       return;
8517     }
8518
8519   sw_if_index = ntohl (mp->sw_if_index);
8520
8521   if (~0 == sw_if_index)
8522     {
8523         /* *INDENT-OFF* */
8524         pool_foreach (t, igm->tunnels,
8525         ({
8526             send_ipsec_gre_tunnel_details(t, q, mp->context);
8527         }));
8528         /* *INDENT-ON* */
8529     }
8530   else
8531     {
8532       if ((sw_if_index >= vec_len (igm->tunnel_index_by_sw_if_index)) ||
8533           (~0 == igm->tunnel_index_by_sw_if_index[sw_if_index]))
8534         {
8535           return;
8536         }
8537       t = &igm->tunnels[igm->tunnel_index_by_sw_if_index[sw_if_index]];
8538       send_ipsec_gre_tunnel_details (t, q, mp->context);
8539     }
8540 }
8541
8542 static void
8543 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
8544 {
8545   vl_api_delete_subif_reply_t *rmp;
8546   int rv;
8547
8548   rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
8549
8550   REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
8551 }
8552
8553 static void
8554   vl_api_l2_interface_pbb_tag_rewrite_t_handler
8555   (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
8556 {
8557   vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
8558   vnet_main_t *vnm = vnet_get_main ();
8559   vlib_main_t *vm = vlib_get_main ();
8560   u32 vtr_op;
8561   int rv = 0;
8562
8563   VALIDATE_SW_IF_INDEX (mp);
8564
8565   vtr_op = ntohl (mp->vtr_op);
8566
8567   switch (vtr_op)
8568     {
8569     case L2_VTR_DISABLED:
8570     case L2_VTR_PUSH_2:
8571     case L2_VTR_POP_2:
8572     case L2_VTR_TRANSLATE_2_1:
8573       break;
8574
8575     default:
8576       rv = VNET_API_ERROR_INVALID_VALUE;
8577       goto bad_sw_if_index;
8578     }
8579
8580   rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
8581                         mp->b_dmac, mp->b_smac, ntohs (mp->b_vlanid),
8582                         ntohl (mp->i_sid), ntohs (mp->outer_tag));
8583
8584   BAD_SW_IF_INDEX_LABEL;
8585
8586   REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
8587 }
8588
8589 static void
8590 vl_api_punt_t_handler (vl_api_punt_t * mp)
8591 {
8592   vl_api_punt_reply_t *rmp;
8593   vlib_main_t *vm = vlib_get_main ();
8594   int rv = 0;
8595   clib_error_t *error;
8596
8597   error = vnet_punt_add_del (vm, mp->ipv, mp->l4_protocol,
8598                              ntohs (mp->l4_port), mp->is_add);
8599   if (error)
8600     {
8601       rv = -1;
8602       clib_error_report (error);
8603     }
8604
8605   REPLY_MACRO (VL_API_PUNT_REPLY);
8606 }
8607
8608 static void
8609   vl_api_flow_classify_set_interface_t_handler
8610   (vl_api_flow_classify_set_interface_t * mp)
8611 {
8612   vlib_main_t *vm = vlib_get_main ();
8613   vl_api_flow_classify_set_interface_reply_t *rmp;
8614   int rv;
8615   u32 sw_if_index, ip4_table_index, ip6_table_index;
8616
8617   ip4_table_index = ntohl (mp->ip4_table_index);
8618   ip6_table_index = ntohl (mp->ip6_table_index);
8619   sw_if_index = ntohl (mp->sw_if_index);
8620
8621   VALIDATE_SW_IF_INDEX (mp);
8622
8623   rv = vnet_set_flow_classify_intfc (vm, sw_if_index, ip4_table_index,
8624                                      ip6_table_index, mp->is_add);
8625
8626   BAD_SW_IF_INDEX_LABEL;
8627
8628   REPLY_MACRO (VL_API_FLOW_CLASSIFY_SET_INTERFACE_REPLY);
8629 }
8630
8631 static void
8632 send_flow_classify_details (u32 sw_if_index,
8633                             u32 table_index,
8634                             unix_shared_memory_queue_t * q, u32 context)
8635 {
8636   vl_api_flow_classify_details_t *mp;
8637
8638   mp = vl_msg_api_alloc (sizeof (*mp));
8639   memset (mp, 0, sizeof (*mp));
8640   mp->_vl_msg_id = ntohs (VL_API_FLOW_CLASSIFY_DETAILS);
8641   mp->context = context;
8642   mp->sw_if_index = htonl (sw_if_index);
8643   mp->table_index = htonl (table_index);
8644
8645   vl_msg_api_send_shmem (q, (u8 *) & mp);
8646 }
8647
8648 static void
8649 vl_api_flow_classify_dump_t_handler (vl_api_flow_classify_dump_t * mp)
8650 {
8651   unix_shared_memory_queue_t *q;
8652   flow_classify_main_t *pcm = &flow_classify_main;
8653   u32 *vec_tbl;
8654   int i;
8655
8656   q = vl_api_client_index_to_input_queue (mp->client_index);
8657   if (q == 0)
8658     return;
8659
8660   vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
8661
8662   if (vec_len (vec_tbl))
8663     {
8664       for (i = 0; i < vec_len (vec_tbl); i++)
8665         {
8666           if (vec_elt (vec_tbl, i) == ~0)
8667             continue;
8668
8669           send_flow_classify_details (i, vec_elt (vec_tbl, i), q,
8670                                       mp->context);
8671         }
8672     }
8673 }
8674
8675 static void
8676 send_ipsec_spd_details (ipsec_policy_t * p, unix_shared_memory_queue_t * q,
8677                         u32 context)
8678 {
8679   vl_api_ipsec_spd_details_t *mp;
8680
8681   mp = vl_msg_api_alloc (sizeof (*mp));
8682   memset (mp, 0, sizeof (*mp));
8683   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
8684   mp->context = context;
8685
8686   mp->spd_id = htonl (p->id);
8687   mp->priority = htonl (p->priority);
8688   mp->is_outbound = p->is_outbound;
8689   mp->is_ipv6 = p->is_ipv6;
8690   if (p->is_ipv6)
8691     {
8692       memcpy (mp->local_start_addr, &p->laddr.start.ip6, 16);
8693       memcpy (mp->local_stop_addr, &p->laddr.stop.ip6, 16);
8694       memcpy (mp->remote_start_addr, &p->raddr.start.ip6, 16);
8695       memcpy (mp->remote_stop_addr, &p->raddr.stop.ip6, 16);
8696     }
8697   else
8698     {
8699       memcpy (mp->local_start_addr, &p->laddr.start.ip4, 4);
8700       memcpy (mp->local_stop_addr, &p->laddr.stop.ip4, 4);
8701       memcpy (mp->remote_start_addr, &p->raddr.start.ip4, 4);
8702       memcpy (mp->remote_stop_addr, &p->raddr.stop.ip4, 4);
8703     }
8704   mp->local_start_port = htons (p->lport.start);
8705   mp->local_stop_port = htons (p->lport.stop);
8706   mp->remote_start_port = htons (p->rport.start);
8707   mp->remote_stop_port = htons (p->rport.stop);
8708   mp->protocol = p->protocol;
8709   mp->policy = p->policy;
8710   mp->sa_id = htonl (p->sa_id);
8711   mp->bytes = clib_host_to_net_u64 (p->counter.bytes);
8712   mp->packets = clib_host_to_net_u64 (p->counter.packets);
8713
8714   vl_msg_api_send_shmem (q, (u8 *) & mp);
8715 }
8716
8717 static void
8718 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
8719 {
8720   unix_shared_memory_queue_t *q;
8721   ipsec_main_t *im = &ipsec_main;
8722   ipsec_policy_t *policy;
8723   ipsec_spd_t *spd;
8724   uword *p;
8725   u32 spd_index;
8726 #if IPSEC > 0
8727   q = vl_api_client_index_to_input_queue (mp->client_index);
8728   if (q == 0)
8729     return;
8730
8731   p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
8732   if (!p)
8733     return;
8734
8735   spd_index = p[0];
8736   spd = pool_elt_at_index (im->spds, spd_index);
8737
8738   /* *INDENT-OFF* */
8739   pool_foreach (policy, spd->policies,
8740   ({
8741     if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
8742       send_ipsec_spd_details (policy, q,
8743                               mp->context);}
8744     ));
8745   /* *INDENT-ON* */
8746 #else
8747   clib_warning ("unimplemented");
8748 #endif
8749 }
8750
8751 static void
8752 vl_api_feature_enable_disable_t_handler (vl_api_feature_enable_disable_t * mp)
8753 {
8754   vl_api_feature_enable_disable_reply_t *rmp;
8755   int rv = 0;
8756   u8 *arc_name, *feature_name;
8757
8758   VALIDATE_SW_IF_INDEX (mp);
8759
8760   arc_name = format (0, "%s%c", mp->arc_name, 0);
8761   feature_name = format (0, "%s%c", mp->feature_name, 0);
8762
8763   vnet_feature_registration_t *reg;
8764   reg =
8765     vnet_get_feature_reg ((const char *) arc_name,
8766                           (const char *) feature_name);
8767   if (reg == 0)
8768     rv = VNET_API_ERROR_INVALID_VALUE;
8769   else
8770     {
8771       u32 sw_if_index;
8772       clib_error_t *error = 0;
8773
8774       sw_if_index = ntohl (mp->sw_if_index);
8775       if (reg->enable_disable_cb)
8776         error = reg->enable_disable_cb (sw_if_index, mp->enable);
8777       if (!error)
8778         vnet_feature_enable_disable ((const char *) arc_name,
8779                                      (const char *) feature_name,
8780                                      sw_if_index, mp->enable, 0, 0);
8781       else
8782         {
8783           clib_error_report (error);
8784           rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
8785         }
8786     }
8787
8788   vec_free (feature_name);
8789   vec_free (arc_name);
8790
8791   BAD_SW_IF_INDEX_LABEL;
8792
8793   REPLY_MACRO (VL_API_FEATURE_ENABLE_DISABLE_REPLY);
8794 }
8795
8796 static void vl_api_sw_interface_tag_add_del_t_handler
8797   (vl_api_sw_interface_tag_add_del_t * mp)
8798 {
8799   vnet_main_t *vnm = vnet_get_main ();
8800   vl_api_sw_interface_tag_add_del_reply_t *rmp;
8801   int rv = 0;
8802   u8 *tag;
8803   u32 sw_if_index = ntohl (mp->sw_if_index);
8804
8805   VALIDATE_SW_IF_INDEX (mp);
8806
8807   if (mp->is_add)
8808     {
8809       if (mp->tag[0] == 0)
8810         {
8811           rv = VNET_API_ERROR_INVALID_VALUE;
8812           goto out;
8813         }
8814
8815       mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
8816       tag = format (0, "%s%c", mp->tag, 0);
8817       vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
8818     }
8819   else
8820     vnet_clear_sw_interface_tag (vnm, sw_if_index);
8821
8822   BAD_SW_IF_INDEX_LABEL;
8823 out:
8824   REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
8825 }
8826
8827 #define BOUNCE_HANDLER(nn)                                              \
8828 static void vl_api_##nn##_t_handler (                                   \
8829     vl_api_##nn##_t *mp)                                                \
8830 {                                                                       \
8831     vpe_client_registration_t *reg;                                     \
8832     vpe_api_main_t * vam = &vpe_api_main;                               \
8833     unix_shared_memory_queue_t * q;                                     \
8834                                                                         \
8835     /* One registration only... */                                      \
8836     pool_foreach(reg, vam->nn##_registrations,                          \
8837     ({                                                                  \
8838         q = vl_api_client_index_to_input_queue (reg->client_index);     \
8839         if (q) {                                                        \
8840             /*                                                          \
8841              * If the queue is stuffed, turf the msg and complain       \
8842              * It's unlikely that the intended recipient is             \
8843              * alive; avoid deadlock at all costs.                      \
8844              */                                                         \
8845             if (q->cursize == q->maxsize) {                             \
8846                 clib_warning ("ERROR: receiver queue full, drop msg");  \
8847                 vl_msg_api_free (mp);                                   \
8848                 return;                                                 \
8849             }                                                           \
8850             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
8851             return;                                                     \
8852         }                                                               \
8853     }));                                                                \
8854     vl_msg_api_free (mp);                                               \
8855 }
8856
8857 static void setup_message_id_table (api_main_t * am);
8858
8859 /*
8860  * vpe_api_hookup
8861  * Add vpe's API message handlers to the table.
8862  * vlib has alread mapped shared memory and
8863  * added the client registration handlers.
8864  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
8865  */
8866 static clib_error_t *
8867 vpe_api_hookup (vlib_main_t * vm)
8868 {
8869   api_main_t *am = &api_main;
8870
8871 #define _(N,n)                                                  \
8872     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
8873                            vl_api_##n##_t_handler,              \
8874                            vl_noop_handler,                     \
8875                            vl_api_##n##_t_endian,               \
8876                            vl_api_##n##_t_print,                \
8877                            sizeof(vl_api_##n##_t), 1);
8878   foreach_vpe_api_msg;
8879 #undef _
8880
8881   /*
8882    * Manually register the sr tunnel add del msg, so we trace
8883    * enough bytes to capture a typical segment list
8884    */
8885   vl_msg_api_set_handlers (VL_API_SR_TUNNEL_ADD_DEL,
8886                            "sr_tunnel_add_del",
8887                            vl_api_sr_tunnel_add_del_t_handler,
8888                            vl_noop_handler,
8889                            vl_api_sr_tunnel_add_del_t_endian,
8890                            vl_api_sr_tunnel_add_del_t_print, 256, 1);
8891
8892
8893   /*
8894    * Manually register the sr policy add del msg, so we trace
8895    * enough bytes to capture a typical tunnel name list
8896    */
8897   vl_msg_api_set_handlers (VL_API_SR_POLICY_ADD_DEL,
8898                            "sr_policy_add_del",
8899                            vl_api_sr_policy_add_del_t_handler,
8900                            vl_noop_handler,
8901                            vl_api_sr_policy_add_del_t_endian,
8902                            vl_api_sr_policy_add_del_t_print, 256, 1);
8903
8904   /*
8905    * Trace space for 8 MPLS encap labels, classifier mask+match
8906    */
8907   am->api_trace_cfg[VL_API_MPLS_TUNNEL_ADD_DEL].size += 8 * sizeof (u32);
8908   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
8909   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size
8910     += 5 * sizeof (u32x4);
8911   am->api_trace_cfg[VL_API_VXLAN_ADD_DEL_TUNNEL].size += 16 * sizeof (u32);
8912
8913   /*
8914    * Thread-safe API messages
8915    */
8916   am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
8917   am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
8918
8919   /*
8920    * Set up the (msg_name, crc, message-id) table
8921    */
8922   setup_message_id_table (am);
8923
8924   return 0;
8925 }
8926
8927 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
8928
8929 static clib_error_t *
8930 vpe_api_init (vlib_main_t * vm)
8931 {
8932   vpe_api_main_t *am = &vpe_api_main;
8933
8934   am->vlib_main = vm;
8935   am->vnet_main = vnet_get_main ();
8936   am->interface_events_registration_hash = hash_create (0, sizeof (uword));
8937   am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
8938   am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
8939   am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
8940   am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
8941   am->oam_events_registration_hash = hash_create (0, sizeof (uword));
8942
8943   vl_api_init (vm);
8944   vl_set_memory_region_name ("/vpe-api");
8945   vl_enable_disable_memory_api (vm, 1 /* enable it */ );
8946
8947   return 0;
8948 }
8949
8950 VLIB_INIT_FUNCTION (vpe_api_init);
8951
8952
8953 static clib_error_t *
8954 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
8955 {
8956   u8 *chroot_path;
8957   u64 baseva, size, pvt_heap_size;
8958   int uid, gid, rv;
8959   const int max_buf_size = 4096;
8960   char *s, *buf;
8961   struct passwd _pw, *pw;
8962   struct group _grp, *grp;
8963   clib_error_t *e;
8964   buf = vec_new (char, 128);
8965   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
8966     {
8967       if (unformat (input, "prefix %s", &chroot_path))
8968         {
8969           vec_add1 (chroot_path, 0);
8970           vl_set_memory_root_path ((char *) chroot_path);
8971         }
8972       else if (unformat (input, "uid %d", &uid))
8973         vl_set_memory_uid (uid);
8974       else if (unformat (input, "gid %d", &gid))
8975         vl_set_memory_gid (gid);
8976       else if (unformat (input, "baseva %llx", &baseva))
8977         vl_set_global_memory_baseva (baseva);
8978       else if (unformat (input, "global-size %lldM", &size))
8979         vl_set_global_memory_size (size * (1ULL << 20));
8980       else if (unformat (input, "global-size %lldG", &size))
8981         vl_set_global_memory_size (size * (1ULL << 30));
8982       else if (unformat (input, "global-size %lld", &size))
8983         vl_set_global_memory_size (size);
8984       else if (unformat (input, "global-pvt-heap-size %lldM", &pvt_heap_size))
8985         vl_set_global_pvt_heap_size (pvt_heap_size * (1ULL << 20));
8986       else if (unformat (input, "global-pvt-heap-size size %lld",
8987                          &pvt_heap_size))
8988         vl_set_global_pvt_heap_size (pvt_heap_size);
8989       else if (unformat (input, "api-pvt-heap-size %lldM", &pvt_heap_size))
8990         vl_set_api_pvt_heap_size (pvt_heap_size * (1ULL << 20));
8991       else if (unformat (input, "api-pvt-heap-size size %lld",
8992                          &pvt_heap_size))
8993         vl_set_api_pvt_heap_size (pvt_heap_size);
8994       else if (unformat (input, "api-size %lldM", &size))
8995         vl_set_api_memory_size (size * (1ULL << 20));
8996       else if (unformat (input, "api-size %lldG", &size))
8997         vl_set_api_memory_size (size * (1ULL << 30));
8998       else if (unformat (input, "api-size %lld", &size))
8999         vl_set_api_memory_size (size);
9000       else if (unformat (input, "uid %s", &s))
9001         {
9002           /* lookup the username */
9003           pw = NULL;
9004           while (((rv =
9005                    getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
9006                  && (vec_len (buf) <= max_buf_size))
9007             {
9008               vec_resize (buf, vec_len (buf) * 2);
9009             }
9010           if (rv < 0)
9011             {
9012               e = clib_error_return_code (0, rv,
9013                                           CLIB_ERROR_ERRNO_VALID |
9014                                           CLIB_ERROR_FATAL,
9015                                           "cannot fetch username %s", s);
9016               vec_free (s);
9017               vec_free (buf);
9018               return e;
9019             }
9020           if (pw == NULL)
9021             {
9022               e =
9023                 clib_error_return_fatal (0, "username %s does not exist", s);
9024               vec_free (s);
9025               vec_free (buf);
9026               return e;
9027             }
9028           vec_free (s);
9029           vl_set_memory_uid (pw->pw_uid);
9030         }
9031       else if (unformat (input, "gid %s", &s))
9032         {
9033           /* lookup the group name */
9034           grp = NULL;
9035           while (((rv =
9036                    getgrnam_r (s, &_grp, buf, vec_len (buf), &grp)) == ERANGE)
9037                  && (vec_len (buf) <= max_buf_size))
9038             {
9039               vec_resize (buf, vec_len (buf) * 2);
9040             }
9041           if (rv != 0)
9042             {
9043               e = clib_error_return_code (0, rv,
9044                                           CLIB_ERROR_ERRNO_VALID |
9045                                           CLIB_ERROR_FATAL,
9046                                           "cannot fetch group %s", s);
9047               vec_free (s);
9048               vec_free (buf);
9049               return e;
9050             }
9051           if (grp == NULL)
9052             {
9053               e = clib_error_return_fatal (0, "group %s does not exist", s);
9054               vec_free (s);
9055               vec_free (buf);
9056               return e;
9057             }
9058           vec_free (s);
9059           vec_free (buf);
9060           vl_set_memory_gid (grp->gr_gid);
9061         }
9062       else
9063         return clib_error_return (0, "unknown input `%U'",
9064                                   format_unformat_error, input);
9065     }
9066   return 0;
9067 }
9068
9069 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
9070
9071 void *
9072 get_unformat_vnet_sw_interface (void)
9073 {
9074   return (void *) &unformat_vnet_sw_interface;
9075 }
9076
9077 static u8 *
9078 format_arp_event (u8 * s, va_list * args)
9079 {
9080   vl_api_ip4_arp_event_t *event = va_arg (*args, vl_api_ip4_arp_event_t *);
9081
9082   s = format (s, "pid %d: ", event->pid);
9083   if (event->mac_ip)
9084     s = format (s, "bd mac/ip4 binding events");
9085   else
9086     s = format (s, "resolution for %U", format_ip4_address, &event->address);
9087   return s;
9088 }
9089
9090 static u8 *
9091 format_nd_event (u8 * s, va_list * args)
9092 {
9093   vl_api_ip6_nd_event_t *event = va_arg (*args, vl_api_ip6_nd_event_t *);
9094
9095   s = format (s, "pid %d: ", event->pid);
9096   if (event->mac_ip)
9097     s = format (s, "bd mac/ip6 binding events");
9098   else
9099     s = format (s, "resolution for %U", format_ip6_address, event->address);
9100   return s;
9101 }
9102
9103 static clib_error_t *
9104 show_ip_arp_nd_events_fn (vlib_main_t * vm,
9105                           unformat_input_t * input, vlib_cli_command_t * cmd)
9106 {
9107   vpe_api_main_t *am = &vpe_api_main;
9108   vl_api_ip4_arp_event_t *arp_event;
9109   vl_api_ip6_nd_event_t *nd_event;
9110
9111   if ((pool_elts (am->arp_events) == 0) && (pool_elts (am->nd_events) == 0))
9112     {
9113       vlib_cli_output (vm, "No active arp or nd event registrations");
9114       return 0;
9115     }
9116
9117   /* *INDENT-OFF* */
9118   pool_foreach (arp_event, am->arp_events,
9119   ({
9120     vlib_cli_output (vm, "%U", format_arp_event, arp_event);
9121   }));
9122
9123   pool_foreach (nd_event, am->nd_events,
9124   ({
9125     vlib_cli_output (vm, "%U", format_nd_event, nd_event);
9126   }));
9127   /* *INDENT-ON* */
9128
9129   return 0;
9130 }
9131
9132 /* *INDENT-OFF* */
9133 VLIB_CLI_COMMAND (show_ip_arp_nd_events, static) = {
9134   .path = "show arp-nd-event registrations",
9135   .function = show_ip_arp_nd_events_fn,
9136   .short_help = "Show ip4 arp and ip6 nd event registrations",
9137 };
9138 /* *INDENT-ON* */
9139
9140 #define vl_msg_name_crc_list
9141 #include <vpp-api/vpe_all_api_h.h>
9142 #undef vl_msg_name_crc_list
9143
9144 static void
9145 setup_message_id_table (api_main_t * am)
9146 {
9147 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
9148   foreach_vl_msg_name_crc_memclnt;
9149   foreach_vl_msg_name_crc_vpe;
9150 #undef _
9151 }
9152
9153
9154 /*
9155  * fd.io coding-style-patch-verification: ON
9156  *
9157  * Local Variables:
9158  * eval: (c-set-style "gnu")
9159  * End:
9160  */