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