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