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