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