Coverity errors in MPLS code
[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       tunnel_sw_if_index = ntohl (mp->mt_sw_if_index);
2153       vnet_mpls_tunnel_del (tunnel_sw_if_index);
2154     }
2155
2156   dsunlock (sm);
2157
2158   /* *INDENT-OFF* */
2159   REPLY_MACRO2(VL_API_MPLS_TUNNEL_ADD_DEL_REPLY,
2160   ({
2161     rmp->sw_if_index = ntohl(tunnel_sw_if_index);
2162   }));
2163   /* *INDENT-ON* */
2164 }
2165
2166 static void
2167 vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2168 {
2169   vl_api_proxy_arp_add_del_reply_t *rmp;
2170   u32 fib_index;
2171   int rv;
2172   ip4_main_t *im = &ip4_main;
2173   stats_main_t *sm = &stats_main;
2174   int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
2175                               ip4_address_t * hi_addr,
2176                               u32 fib_index, int is_del);
2177   uword *p;
2178
2179   dslock (sm, 1 /* release hint */ , 6 /* tag */ );
2180
2181   p = hash_get (im->fib_index_by_table_id, ntohl (mp->vrf_id));
2182
2183   if (!p)
2184     {
2185       rv = VNET_API_ERROR_NO_SUCH_FIB;
2186       goto out;
2187     }
2188
2189   fib_index = p[0];
2190
2191   rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->low_address,
2192                                (ip4_address_t *) mp->hi_address,
2193                                fib_index, mp->is_add == 0);
2194
2195 out:
2196   dsunlock (sm);
2197   REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2198 }
2199
2200 static void
2201   vl_api_proxy_arp_intfc_enable_disable_t_handler
2202   (vl_api_proxy_arp_intfc_enable_disable_t * mp)
2203 {
2204   int rv = 0;
2205   vnet_main_t *vnm = vnet_get_main ();
2206   vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
2207   vnet_sw_interface_t *si;
2208   u32 sw_if_index;
2209
2210   VALIDATE_SW_IF_INDEX (mp);
2211
2212   sw_if_index = ntohl (mp->sw_if_index);
2213
2214   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
2215     {
2216       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2217       goto out;
2218     }
2219
2220   si = vnet_get_sw_interface (vnm, sw_if_index);
2221
2222   ASSERT (si);
2223
2224   if (mp->enable_disable)
2225     si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2226   else
2227     si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2228
2229   BAD_SW_IF_INDEX_LABEL;
2230
2231 out:
2232   REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
2233 }
2234
2235 static void
2236 vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
2237                                       vlib_main_t * vm)
2238 {
2239   vl_api_ip_neighbor_add_del_reply_t *rmp;
2240   vnet_main_t *vnm = vnet_get_main ();
2241   stats_main_t *sm = &stats_main;
2242   int rv = 0;
2243
2244   VALIDATE_SW_IF_INDEX (mp);
2245
2246   dslock (sm, 1 /* release hint */ , 7 /* tag */ );
2247
2248   /*
2249    * there's no validation here of the ND/ARP entry being added.
2250    * The expectation is that the FIB will ensure that nothing bad
2251    * will come of adding bogus entries.
2252    */
2253   if (mp->is_ipv6)
2254     {
2255       if (mp->is_add)
2256         rv = vnet_set_ip6_ethernet_neighbor
2257           (vm, ntohl (mp->sw_if_index),
2258            (ip6_address_t *) (mp->dst_address),
2259            mp->mac_address, sizeof (mp->mac_address), mp->is_static);
2260       else
2261         rv = vnet_unset_ip6_ethernet_neighbor
2262           (vm, ntohl (mp->sw_if_index),
2263            (ip6_address_t *) (mp->dst_address),
2264            mp->mac_address, sizeof (mp->mac_address));
2265     }
2266   else
2267     {
2268       ethernet_arp_ip4_over_ethernet_address_t a;
2269
2270       clib_memcpy (&a.ethernet, mp->mac_address, 6);
2271       clib_memcpy (&a.ip4, mp->dst_address, 4);
2272
2273       if (mp->is_add)
2274         rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index),
2275                                              &a, mp->is_static);
2276       else
2277         rv =
2278           vnet_arp_unset_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index), &a);
2279     }
2280
2281   BAD_SW_IF_INDEX_LABEL;
2282
2283   dsunlock (sm);
2284   REPLY_MACRO (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
2285 }
2286
2287 static void
2288 vl_api_is_address_reachable_t_handler (vl_api_is_address_reachable_t * mp)
2289 {
2290 #if 0
2291   vpe_main_t *rm = &vpe_main;
2292   ip4_main_t *im4 = &ip4_main;
2293   ip6_main_t *im6 = &ip6_main;
2294   ip_lookup_main_t *lm;
2295   union
2296   {
2297     ip4_address_t ip4;
2298     ip6_address_t ip6;
2299   } addr;
2300   u32 adj_index, sw_if_index;
2301   vl_api_is_address_reachable_t *rmp;
2302   ip_adjacency_t *adj;
2303   unix_shared_memory_queue_t *q;
2304
2305   q = vl_api_client_index_to_input_queue (mp->client_index);
2306   if (!q)
2307     {
2308       increment_missing_api_client_counter (rm->vlib_main);
2309       return;
2310     }
2311
2312   rmp = vl_msg_api_alloc (sizeof (*rmp));
2313   clib_memcpy (rmp, mp, sizeof (*rmp));
2314
2315   sw_if_index = mp->next_hop_sw_if_index;
2316   clib_memcpy (&addr, mp->address, sizeof (addr));
2317   if (mp->is_ipv6)
2318     {
2319       lm = &im6->lookup_main;
2320       adj_index = ip6_fib_lookup (im6, sw_if_index, &addr.ip6);
2321     }
2322   else
2323     {
2324       lm = &im4->lookup_main;
2325       // FIXME NOT an ADJ
2326       adj_index = ip4_fib_lookup (im4, sw_if_index, &addr.ip4);
2327     }
2328   if (adj_index == ~0)
2329     {
2330       rmp->is_error = 1;
2331       goto send;
2332     }
2333   adj = ip_get_adjacency (lm, adj_index);
2334
2335   if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE
2336       && adj->rewrite_header.sw_if_index == sw_if_index)
2337     {
2338       rmp->is_known = 1;
2339     }
2340   else
2341     {
2342       if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP
2343           && adj->rewrite_header.sw_if_index == sw_if_index)
2344         {
2345           if (mp->is_ipv6)
2346             ip6_probe_neighbor (rm->vlib_main, &addr.ip6, sw_if_index);
2347           else
2348             ip4_probe_neighbor (rm->vlib_main, &addr.ip4, sw_if_index);
2349         }
2350       else if (adj->lookup_next_index == IP_LOOKUP_NEXT_DROP)
2351         {
2352           rmp->is_known = 1;
2353           goto send;
2354         }
2355       rmp->is_known = 0;
2356     }
2357
2358 send:
2359   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2360 #endif
2361 }
2362
2363 static void
2364 vl_api_sw_interface_details_t_handler (vl_api_sw_interface_details_t * mp)
2365 {
2366   clib_warning ("BUG");
2367 }
2368
2369 static void
2370   vl_api_sw_interface_set_mpls_enable_t_handler
2371   (vl_api_sw_interface_set_mpls_enable_t * mp)
2372 {
2373   vl_api_sw_interface_set_mpls_enable_reply_t *rmp;
2374   int rv = 0;
2375
2376   VALIDATE_SW_IF_INDEX (mp);
2377
2378   mpls_sw_interface_enable_disable (&mpls_main,
2379                                     ntohl (mp->sw_if_index), mp->enable);
2380
2381   BAD_SW_IF_INDEX_LABEL;
2382   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MPLS_ENABLE_REPLY);
2383 }
2384
2385 static void
2386 vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
2387                                            mp)
2388 {
2389   vl_api_sw_interface_clear_stats_reply_t *rmp;
2390
2391   vnet_main_t *vnm = vnet_get_main ();
2392   vnet_interface_main_t *im = &vnm->interface_main;
2393   vlib_simple_counter_main_t *sm;
2394   vlib_combined_counter_main_t *cm;
2395   static vnet_main_t **my_vnet_mains;
2396   int i, j, n_counters;
2397   int rv = 0;
2398
2399   if (mp->sw_if_index != ~0)
2400     VALIDATE_SW_IF_INDEX (mp);
2401
2402   vec_reset_length (my_vnet_mains);
2403
2404   for (i = 0; i < vec_len (vnet_mains); i++)
2405     {
2406       if (vnet_mains[i])
2407         vec_add1 (my_vnet_mains, vnet_mains[i]);
2408     }
2409
2410   if (vec_len (vnet_mains) == 0)
2411     vec_add1 (my_vnet_mains, vnm);
2412
2413   n_counters = vec_len (im->combined_sw_if_counters);
2414
2415   for (j = 0; j < n_counters; j++)
2416     {
2417       for (i = 0; i < vec_len (my_vnet_mains); i++)
2418         {
2419           im = &my_vnet_mains[i]->interface_main;
2420           cm = im->combined_sw_if_counters + j;
2421           if (mp->sw_if_index == (u32) ~ 0)
2422             vlib_clear_combined_counters (cm);
2423           else
2424             vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
2425         }
2426     }
2427
2428   n_counters = vec_len (im->sw_if_counters);
2429
2430   for (j = 0; j < n_counters; j++)
2431     {
2432       for (i = 0; i < vec_len (my_vnet_mains); i++)
2433         {
2434           im = &my_vnet_mains[i]->interface_main;
2435           sm = im->sw_if_counters + j;
2436           if (mp->sw_if_index == (u32) ~ 0)
2437             vlib_clear_simple_counters (sm);
2438           else
2439             vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
2440         }
2441     }
2442
2443   BAD_SW_IF_INDEX_LABEL;
2444
2445   REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
2446 }
2447
2448 static void
2449 send_sw_interface_details (vpe_api_main_t * am,
2450                            unix_shared_memory_queue_t * q,
2451                            vnet_sw_interface_t * swif,
2452                            u8 * interface_name, u32 context)
2453 {
2454   vl_api_sw_interface_details_t *mp;
2455   vnet_main_t *vnm = vnet_get_main ();
2456   vnet_hw_interface_t *hi;
2457   u8 *tag;
2458
2459   hi = vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
2460
2461   mp = vl_msg_api_alloc (sizeof (*mp));
2462   memset (mp, 0, sizeof (*mp));
2463   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
2464   mp->sw_if_index = ntohl (swif->sw_if_index);
2465   mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
2466   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
2467   mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
2468   mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
2469                      VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
2470   mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
2471                     VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
2472   mp->link_mtu = ntohs (hi->max_packet_bytes);
2473   mp->context = context;
2474
2475   strncpy ((char *) mp->interface_name,
2476            (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
2477
2478   /* Send the L2 address for ethernet physical intfcs */
2479   if (swif->sup_sw_if_index == swif->sw_if_index
2480       && hi->hw_class_index == ethernet_hw_interface_class.index)
2481     {
2482       ethernet_main_t *em = ethernet_get_main (am->vlib_main);
2483       ethernet_interface_t *ei;
2484
2485       ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
2486       ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
2487       clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
2488       mp->l2_address_length = ntohl (sizeof (ei->address));
2489     }
2490   else if (swif->sup_sw_if_index != swif->sw_if_index)
2491     {
2492       vnet_sub_interface_t *sub = &swif->sub;
2493       mp->sub_id = ntohl (sub->id);
2494       mp->sub_dot1ad = sub->eth.flags.dot1ad;
2495       mp->sub_number_of_tags =
2496         sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
2497       mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
2498       mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
2499       mp->sub_exact_match = sub->eth.flags.exact_match;
2500       mp->sub_default = sub->eth.flags.default_sub;
2501       mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
2502       mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
2503
2504       /* vlan tag rewrite data */
2505       u32 vtr_op = L2_VTR_DISABLED;
2506       u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
2507
2508       if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
2509                      &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
2510         {
2511           // error - default to disabled
2512           mp->vtr_op = ntohl (L2_VTR_DISABLED);
2513           clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
2514                         swif->sw_if_index);
2515         }
2516       else
2517         {
2518           mp->vtr_op = ntohl (vtr_op);
2519           mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
2520           mp->vtr_tag1 = ntohl (vtr_tag1);
2521           mp->vtr_tag2 = ntohl (vtr_tag2);
2522         }
2523     }
2524
2525   tag = vnet_get_sw_interface_tag (vnm, swif->sw_if_index);
2526   if (tag)
2527     strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
2528
2529   vl_msg_api_send_shmem (q, (u8 *) & mp);
2530 }
2531
2532 static void
2533 send_sw_interface_flags (vpe_api_main_t * am,
2534                          unix_shared_memory_queue_t * q,
2535                          vnet_sw_interface_t * swif)
2536 {
2537   vl_api_sw_interface_set_flags_t *mp;
2538   vnet_main_t *vnm = am->vnet_main;
2539
2540   vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
2541                                                        swif->sw_if_index);
2542   mp = vl_msg_api_alloc (sizeof (*mp));
2543   memset (mp, 0, sizeof (*mp));
2544   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SET_FLAGS);
2545   mp->sw_if_index = ntohl (swif->sw_if_index);
2546
2547   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
2548   mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
2549   vl_msg_api_send_shmem (q, (u8 *) & mp);
2550 }
2551
2552 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
2553                                              unix_shared_memory_queue_t * q,
2554                                              u32 sw_if_index)
2555   __attribute__ ((unused));
2556
2557 static void
2558 send_sw_interface_flags_deleted (vpe_api_main_t * am,
2559                                  unix_shared_memory_queue_t * q,
2560                                  u32 sw_if_index)
2561 {
2562   vl_api_sw_interface_set_flags_t *mp;
2563
2564   mp = vl_msg_api_alloc (sizeof (*mp));
2565   memset (mp, 0, sizeof (*mp));
2566   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SET_FLAGS);
2567   mp->sw_if_index = ntohl (sw_if_index);
2568
2569   mp->admin_up_down = 0;
2570   mp->link_up_down = 0;
2571   mp->deleted = 1;
2572   vl_msg_api_send_shmem (q, (u8 *) & mp);
2573 }
2574
2575 static void
2576 vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
2577 {
2578   vpe_api_main_t *am = &vpe_api_main;
2579   vnet_sw_interface_t *swif;
2580   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2581   u8 *filter_string = 0, *name_string = 0;
2582   unix_shared_memory_queue_t *q;
2583   char *strcasestr (char *, char *);    /* lnx hdr file botch */
2584
2585   q = vl_api_client_index_to_input_queue (mp->client_index);
2586
2587   if (q == 0)
2588     return;
2589
2590   if (mp->name_filter_valid)
2591     {
2592       mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
2593       filter_string = format (0, "%s%c", mp->name_filter, 0);
2594     }
2595
2596   /* *INDENT-OFF* */
2597   pool_foreach (swif, im->sw_interfaces,
2598   ({
2599     name_string = format (name_string, "%U%c",
2600                           format_vnet_sw_interface_name,
2601                           am->vnet_main, swif, 0);
2602
2603     if (mp->name_filter_valid == 0 ||
2604         strcasestr((char *) name_string, (char *) filter_string)) {
2605
2606       send_sw_interface_details (am, q, swif, name_string, mp->context);
2607     }
2608     _vec_len (name_string) = 0;
2609   }));
2610   /* *INDENT-ON* */
2611
2612   vec_free (name_string);
2613   vec_free (filter_string);
2614 }
2615
2616 void
2617 send_oam_event (oam_target_t * t)
2618 {
2619   vpe_api_main_t *vam = &vpe_api_main;
2620   unix_shared_memory_queue_t *q;
2621   vpe_client_registration_t *reg;
2622   vl_api_oam_event_t *mp;
2623
2624   /* *INDENT-OFF* */
2625   pool_foreach(reg, vam->oam_events_registrations,
2626   ({
2627     q = vl_api_client_index_to_input_queue (reg->client_index);
2628     if (q)
2629       {
2630         mp = vl_msg_api_alloc (sizeof (*mp));
2631         mp->_vl_msg_id = ntohs (VL_API_OAM_EVENT);
2632         clib_memcpy (mp->dst_address, &t->dst_address,
2633                      sizeof (mp->dst_address));
2634         mp->state = t->state;
2635         vl_msg_api_send_shmem (q, (u8 *)&mp);
2636       }
2637   }));
2638   /* *INDENT-ON* */
2639 }
2640
2641 static void
2642 vl_api_oam_add_del_t_handler (vl_api_oam_add_del_t * mp)
2643 {
2644   vl_api_oam_add_del_reply_t *rmp;
2645   int rv;
2646
2647   rv = vpe_oam_add_del_target ((ip4_address_t *) mp->src_address,
2648                                (ip4_address_t *) mp->dst_address,
2649                                ntohl (mp->vrf_id), (int) (mp->is_add));
2650
2651   REPLY_MACRO (VL_API_OAM_ADD_DEL_REPLY);
2652 }
2653
2654 static void
2655 vl_api_vnet_get_summary_stats_t_handler (vl_api_vnet_get_summary_stats_t * mp)
2656 {
2657   stats_main_t *sm = &stats_main;
2658   vnet_interface_main_t *im = sm->interface_main;
2659   vl_api_vnet_summary_stats_reply_t *rmp;
2660   vlib_combined_counter_main_t *cm;
2661   vlib_counter_t v;
2662   int i, which;
2663   u64 total_pkts[VLIB_N_RX_TX];
2664   u64 total_bytes[VLIB_N_RX_TX];
2665
2666   unix_shared_memory_queue_t *q =
2667     vl_api_client_index_to_input_queue (mp->client_index);
2668
2669   if (!q)
2670     return;
2671
2672   rmp = vl_msg_api_alloc (sizeof (*rmp));
2673   rmp->_vl_msg_id = ntohs (VL_API_VNET_SUMMARY_STATS_REPLY);
2674   rmp->context = mp->context;
2675   rmp->retval = 0;
2676
2677   memset (total_pkts, 0, sizeof (total_pkts));
2678   memset (total_bytes, 0, sizeof (total_bytes));
2679
2680   vnet_interface_counter_lock (im);
2681
2682   vec_foreach (cm, im->combined_sw_if_counters)
2683   {
2684     which = cm - im->combined_sw_if_counters;
2685
2686     for (i = 0; i < vec_len (cm->maxi); i++)
2687       {
2688         vlib_get_combined_counter (cm, i, &v);
2689         total_pkts[which] += v.packets;
2690         total_bytes[which] += v.bytes;
2691       }
2692   }
2693   vnet_interface_counter_unlock (im);
2694
2695   rmp->total_pkts[VLIB_RX] = clib_host_to_net_u64 (total_pkts[VLIB_RX]);
2696   rmp->total_bytes[VLIB_RX] = clib_host_to_net_u64 (total_bytes[VLIB_RX]);
2697   rmp->total_pkts[VLIB_TX] = clib_host_to_net_u64 (total_pkts[VLIB_TX]);
2698   rmp->total_bytes[VLIB_TX] = clib_host_to_net_u64 (total_bytes[VLIB_TX]);
2699   rmp->vector_rate =
2700     clib_host_to_net_u64 (vlib_last_vector_length_per_node (sm->vlib_main));
2701
2702   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2703 }
2704
2705 /* *INDENT-OFF* */
2706 typedef CLIB_PACKED (struct {
2707   ip4_address_t address;
2708   u32 address_length: 6;
2709   u32 index:26;
2710 }) ip4_route_t;
2711 /* *INDENT-ON* */
2712
2713 static int
2714 ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2715 {
2716   vnet_main_t *vnm = vnet_get_main ();
2717   vnet_interface_main_t *im = &vnm->interface_main;
2718   ip4_main_t *im4 = &ip4_main;
2719   static u32 *sw_if_indices_to_shut;
2720   stats_main_t *sm = &stats_main;
2721   fib_table_t *fib_table;
2722   ip4_fib_t *fib;
2723   u32 sw_if_index;
2724   int i;
2725   int rv = VNET_API_ERROR_NO_SUCH_FIB;
2726   u32 target_fib_id = ntohl (mp->vrf_id);
2727
2728   dslock (sm, 1 /* release hint */ , 8 /* tag */ );
2729
2730   /* *INDENT-OFF* */
2731   pool_foreach (fib_table, im4->fibs,
2732   ({
2733     fib = &fib_table->v4;
2734     vnet_sw_interface_t * si;
2735
2736     if (fib->table_id != target_fib_id)
2737       continue;
2738
2739     /* remove any mpls encap/decap labels */
2740     mpls_fib_reset_labels (fib->table_id);
2741
2742     /* remove any proxy arps in this fib */
2743     vnet_proxy_arp_fib_reset (fib->table_id);
2744
2745     /* Set the flow hash for this fib to the default */
2746     vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2747
2748     vec_reset_length (sw_if_indices_to_shut);
2749
2750     /* Shut down interfaces in this FIB / clean out intfc routes */
2751     pool_foreach (si, im->sw_interfaces,
2752     ({
2753       u32 sw_if_index = si->sw_if_index;
2754
2755       if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
2756           && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
2757               fib->index))
2758         vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2759     }));
2760
2761     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2762       sw_if_index = sw_if_indices_to_shut[i];
2763       // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
2764
2765       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2766       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2767       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2768     }
2769
2770     fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
2771     fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_INTERFACE);
2772
2773     rv = 0;
2774     break;
2775     })); /* pool_foreach (fib) */
2776     /* *INDENT-ON* */
2777
2778   dsunlock (sm);
2779   return rv;
2780 }
2781
2782 static int
2783 ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2784 {
2785   vnet_main_t *vnm = vnet_get_main ();
2786   vnet_interface_main_t *im = &vnm->interface_main;
2787   ip6_main_t *im6 = &ip6_main;
2788   stats_main_t *sm = &stats_main;
2789   static u32 *sw_if_indices_to_shut;
2790   fib_table_t *fib_table;
2791   ip6_fib_t *fib;
2792   u32 sw_if_index;
2793   int i;
2794   int rv = VNET_API_ERROR_NO_SUCH_FIB;
2795   u32 target_fib_id = ntohl (mp->vrf_id);
2796
2797   dslock (sm, 1 /* release hint */ , 9 /* tag */ );
2798
2799   /* *INDENT-OFF* */
2800   pool_foreach (fib_table, im6->fibs,
2801   ({
2802     vnet_sw_interface_t * si;
2803     fib = &(fib_table->v6);
2804
2805     if (fib->table_id != target_fib_id)
2806       continue;
2807
2808     vec_reset_length (sw_if_indices_to_shut);
2809
2810     /* Shut down interfaces in this FIB / clean out intfc routes */
2811     pool_foreach (si, im->sw_interfaces,
2812                   ({
2813                     if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
2814                         fib->index)
2815                       vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2816                   }));
2817
2818     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2819       sw_if_index = sw_if_indices_to_shut[i];
2820       // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
2821
2822       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2823       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2824       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2825     }
2826
2827     fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
2828     fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_INTERFACE);
2829
2830     rv = 0;
2831     break;
2832   })); /* pool_foreach (fib) */
2833   /* *INDENT-ON* */
2834
2835   dsunlock (sm);
2836   return rv;
2837 }
2838
2839 static void
2840 vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2841 {
2842   int rv;
2843   vl_api_reset_fib_reply_t *rmp;
2844
2845   if (mp->is_ipv6)
2846     rv = ip6_reset_fib_t_handler (mp);
2847   else
2848     rv = ip4_reset_fib_t_handler (mp);
2849
2850   REPLY_MACRO (VL_API_RESET_FIB_REPLY);
2851 }
2852
2853
2854 static void
2855 dhcpv4_proxy_config (vl_api_dhcp_proxy_config_t * mp)
2856 {
2857   vl_api_dhcp_proxy_config_reply_t *rmp;
2858   int rv;
2859
2860   rv = dhcp_proxy_set_server ((ip4_address_t *) (&mp->dhcp_server),
2861                               (ip4_address_t *) (&mp->dhcp_src_address),
2862                               (u32) ntohl (mp->vrf_id),
2863                               (int) mp->insert_circuit_id,
2864                               (int) (mp->is_add == 0));
2865
2866   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
2867 }
2868
2869
2870 static void
2871 dhcpv6_proxy_config (vl_api_dhcp_proxy_config_t * mp)
2872 {
2873   vl_api_dhcp_proxy_config_reply_t *rmp;
2874   int rv = -1;
2875
2876   rv = dhcpv6_proxy_set_server ((ip6_address_t *) (&mp->dhcp_server),
2877                                 (ip6_address_t *) (&mp->dhcp_src_address),
2878                                 (u32) ntohl (mp->vrf_id),
2879                                 (int) mp->insert_circuit_id,
2880                                 (int) (mp->is_add == 0));
2881
2882   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
2883 }
2884
2885 static void
2886 dhcpv4_proxy_config_2 (vl_api_dhcp_proxy_config_2_t * mp)
2887 {
2888   vl_api_dhcp_proxy_config_reply_t *rmp;
2889   int rv;
2890
2891   rv = dhcp_proxy_set_server_2 ((ip4_address_t *) (&mp->dhcp_server),
2892                                 (ip4_address_t *) (&mp->dhcp_src_address),
2893                                 (u32) ntohl (mp->rx_vrf_id),
2894                                 (u32) ntohl (mp->server_vrf_id),
2895                                 (int) mp->insert_circuit_id,
2896                                 (int) (mp->is_add == 0));
2897
2898   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_2_REPLY);
2899 }
2900
2901
2902 static void
2903 dhcpv6_proxy_config_2 (vl_api_dhcp_proxy_config_2_t * mp)
2904 {
2905   vl_api_dhcp_proxy_config_reply_t *rmp;
2906   int rv = -1;
2907
2908   rv = dhcpv6_proxy_set_server_2 ((ip6_address_t *) (&mp->dhcp_server),
2909                                   (ip6_address_t *) (&mp->dhcp_src_address),
2910                                   (u32) ntohl (mp->rx_vrf_id),
2911                                   (u32) ntohl (mp->server_vrf_id),
2912                                   (int) mp->insert_circuit_id,
2913                                   (int) (mp->is_add == 0));
2914
2915   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_2_REPLY);
2916 }
2917
2918
2919 static void
2920 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t * mp)
2921 {
2922   vl_api_dhcp_proxy_set_vss_reply_t *rmp;
2923   int rv;
2924   if (!mp->is_ipv6)
2925     rv = dhcp_proxy_set_option82_vss (ntohl (mp->tbl_id),
2926                                       ntohl (mp->oui),
2927                                       ntohl (mp->fib_id),
2928                                       (int) mp->is_add == 0);
2929   else
2930     rv = dhcpv6_proxy_set_vss (ntohl (mp->tbl_id),
2931                                ntohl (mp->oui),
2932                                ntohl (mp->fib_id), (int) mp->is_add == 0);
2933
2934   REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
2935 }
2936
2937
2938 static void vl_api_dhcp_proxy_config_t_handler
2939   (vl_api_dhcp_proxy_config_t * mp)
2940 {
2941   if (mp->is_ipv6 == 0)
2942     dhcpv4_proxy_config (mp);
2943   else
2944     dhcpv6_proxy_config (mp);
2945 }
2946
2947 static void vl_api_dhcp_proxy_config_2_t_handler
2948   (vl_api_dhcp_proxy_config_2_t * mp)
2949 {
2950   if (mp->is_ipv6 == 0)
2951     dhcpv4_proxy_config_2 (mp);
2952   else
2953     dhcpv6_proxy_config_2 (mp);
2954 }
2955
2956 void
2957 dhcp_compl_event_callback (u32 client_index, u32 pid, u8 * hostname,
2958                            u8 is_ipv6, u8 * host_address, u8 * router_address,
2959                            u8 * host_mac)
2960 {
2961   unix_shared_memory_queue_t *q;
2962   vl_api_dhcp_compl_event_t *mp;
2963
2964   q = vl_api_client_index_to_input_queue (client_index);
2965   if (!q)
2966     return;
2967
2968   mp = vl_msg_api_alloc (sizeof (*mp));
2969   mp->client_index = client_index;
2970   mp->pid = pid;
2971   mp->is_ipv6 = is_ipv6;
2972   clib_memcpy (&mp->hostname, hostname, vec_len (hostname));
2973   mp->hostname[vec_len (hostname) + 1] = '\n';
2974   clib_memcpy (&mp->host_address[0], host_address, 16);
2975   clib_memcpy (&mp->router_address[0], router_address, 16);
2976
2977   if (NULL != host_mac)
2978     clib_memcpy (&mp->host_mac[0], host_mac, 6);
2979
2980   mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
2981
2982   vl_msg_api_send_shmem (q, (u8 *) & mp);
2983 }
2984
2985 static void vl_api_dhcp_client_config_t_handler
2986   (vl_api_dhcp_client_config_t * mp)
2987 {
2988   vlib_main_t *vm = vlib_get_main ();
2989   vl_api_dhcp_client_config_reply_t *rmp;
2990   int rv = 0;
2991
2992   VALIDATE_SW_IF_INDEX (mp);
2993
2994   rv = dhcp_client_config (vm, ntohl (mp->sw_if_index),
2995                            mp->hostname, mp->is_add, mp->client_index,
2996                            mp->want_dhcp_event ? dhcp_compl_event_callback :
2997                            NULL, mp->pid);
2998
2999   BAD_SW_IF_INDEX_LABEL;
3000
3001   REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
3002 }
3003
3004 static void
3005   vl_api_sw_interface_ip6nd_ra_config_t_handler
3006   (vl_api_sw_interface_ip6nd_ra_config_t * mp, vlib_main_t * vm)
3007 {
3008   vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
3009   int rv = 0;
3010   u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
3011     default_router;
3012
3013   is_no = mp->is_no == 1;
3014   suppress = mp->suppress == 1;
3015   managed = mp->managed == 1;
3016   other = mp->other == 1;
3017   ll_option = mp->ll_option == 1;
3018   send_unicast = mp->send_unicast == 1;
3019   cease = mp->cease == 1;
3020   default_router = mp->default_router == 1;
3021
3022   VALIDATE_SW_IF_INDEX (mp);
3023
3024   rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
3025                                suppress, managed, other,
3026                                ll_option, send_unicast, cease,
3027                                default_router, ntohl (mp->lifetime),
3028                                ntohl (mp->initial_count),
3029                                ntohl (mp->initial_interval),
3030                                ntohl (mp->max_interval),
3031                                ntohl (mp->min_interval), is_no);
3032
3033   BAD_SW_IF_INDEX_LABEL;
3034
3035   REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
3036 }
3037
3038 static void
3039   vl_api_sw_interface_ip6nd_ra_prefix_t_handler
3040   (vl_api_sw_interface_ip6nd_ra_prefix_t * mp, vlib_main_t * vm)
3041 {
3042   vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
3043   int rv = 0;
3044   u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
3045
3046   VALIDATE_SW_IF_INDEX (mp);
3047
3048   is_no = mp->is_no == 1;
3049   use_default = mp->use_default == 1;
3050   no_advertise = mp->no_advertise == 1;
3051   off_link = mp->off_link == 1;
3052   no_autoconfig = mp->no_autoconfig == 1;
3053   no_onlink = mp->no_onlink == 1;
3054
3055   rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
3056                                (ip6_address_t *) mp->address,
3057                                mp->address_length, use_default,
3058                                ntohl (mp->val_lifetime),
3059                                ntohl (mp->pref_lifetime), no_advertise,
3060                                off_link, no_autoconfig, no_onlink, is_no);
3061
3062   BAD_SW_IF_INDEX_LABEL;
3063   REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
3064 }
3065
3066 static void
3067   vl_api_sw_interface_ip6_enable_disable_t_handler
3068   (vl_api_sw_interface_ip6_enable_disable_t * mp, vlib_main_t * vm)
3069 {
3070   vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
3071   vnet_main_t *vnm = vnet_get_main ();
3072   int rv = 0;
3073   clib_error_t *error;
3074
3075   vnm->api_errno = 0;
3076
3077   VALIDATE_SW_IF_INDEX (mp);
3078
3079   error =
3080     (mp->enable == 1) ? enable_ip6_interface (vm,
3081                                               ntohl (mp->sw_if_index)) :
3082     disable_ip6_interface (vm, ntohl (mp->sw_if_index));
3083
3084   if (error)
3085     {
3086       clib_error_report (error);
3087       rv = VNET_API_ERROR_UNSPECIFIED;
3088     }
3089   else
3090     {
3091       rv = vnm->api_errno;
3092     }
3093
3094   BAD_SW_IF_INDEX_LABEL;
3095
3096   REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
3097 }
3098
3099 static void
3100   vl_api_sw_interface_ip6_set_link_local_address_t_handler
3101   (vl_api_sw_interface_ip6_set_link_local_address_t * mp, vlib_main_t * vm)
3102 {
3103   vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
3104   int rv = 0;
3105   clib_error_t *error;
3106   vnet_main_t *vnm = vnet_get_main ();
3107
3108   vnm->api_errno = 0;
3109
3110   VALIDATE_SW_IF_INDEX (mp);
3111
3112   error = set_ip6_link_local_address (vm,
3113                                       ntohl (mp->sw_if_index),
3114                                       (ip6_address_t *) mp->address,
3115                                       mp->address_length);
3116   if (error)
3117     {
3118       clib_error_report (error);
3119       rv = VNET_API_ERROR_UNSPECIFIED;
3120     }
3121   else
3122     {
3123       rv = vnm->api_errno;
3124     }
3125
3126   BAD_SW_IF_INDEX_LABEL;
3127
3128   REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
3129 }
3130
3131 static void
3132 set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
3133 {
3134   vl_api_set_ip_flow_hash_reply_t *rmp;
3135   int rv = VNET_API_ERROR_UNIMPLEMENTED;
3136
3137   clib_warning ("unimplemented...");
3138
3139   REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
3140 }
3141
3142 static void
3143 set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
3144 {
3145   vl_api_set_ip_flow_hash_reply_t *rmp;
3146   int rv;
3147   u32 table_id;
3148   flow_hash_config_t flow_hash_config = 0;
3149
3150   table_id = ntohl (mp->vrf_id);
3151
3152 #define _(a,b) if (mp->a) flow_hash_config |= b;
3153   foreach_flow_hash_bit;
3154 #undef _
3155
3156   rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
3157
3158   REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
3159 }
3160
3161
3162 static void
3163 vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
3164 {
3165   if (mp->is_ipv6 == 0)
3166     set_ip4_flow_hash (mp);
3167   else
3168     set_ip6_flow_hash (mp);
3169 }
3170
3171 static void vl_api_sw_interface_set_unnumbered_t_handler
3172   (vl_api_sw_interface_set_unnumbered_t * mp)
3173 {
3174   vl_api_sw_interface_set_unnumbered_reply_t *rmp;
3175   int rv = 0;
3176   vnet_sw_interface_t *si;
3177   vnet_main_t *vnm = vnet_get_main ();
3178   u32 sw_if_index, unnumbered_sw_if_index;
3179
3180   sw_if_index = ntohl (mp->sw_if_index);
3181   unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
3182
3183   /*
3184    * The API message field names are backwards from
3185    * the underlying data structure names.
3186    * It's not worth changing them now.
3187    */
3188   if (pool_is_free_index (vnm->interface_main.sw_interfaces,
3189                           unnumbered_sw_if_index))
3190     {
3191       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
3192       goto done;
3193     }
3194
3195   /* Only check the "use loop0" field when setting the binding */
3196   if (mp->is_add &&
3197       pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
3198     {
3199       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
3200       goto done;
3201     }
3202
3203   si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
3204
3205   if (mp->is_add)
3206     {
3207       si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
3208       si->unnumbered_sw_if_index = sw_if_index;
3209       ip4_sw_interface_enable_disable (unnumbered_sw_if_index, 1);
3210       ip6_sw_interface_enable_disable (unnumbered_sw_if_index, 1);
3211     }
3212   else
3213     {
3214       si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
3215       si->unnumbered_sw_if_index = (u32) ~ 0;
3216       ip4_sw_interface_enable_disable (unnumbered_sw_if_index, 0);
3217       ip6_sw_interface_enable_disable (unnumbered_sw_if_index, 0);
3218     }
3219
3220 done:
3221   REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
3222 }
3223
3224 static void
3225 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
3226 {
3227   vl_api_create_loopback_reply_t *rmp;
3228   u32 sw_if_index;
3229   int rv;
3230
3231   rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address);
3232
3233   /* *INDENT-OFF* */
3234   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
3235   ({
3236     rmp->sw_if_index = ntohl (sw_if_index);
3237   }));
3238   /* *INDENT-ON* */
3239 }
3240
3241 static void
3242 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
3243 {
3244   vl_api_delete_loopback_reply_t *rmp;
3245   u32 sw_if_index;
3246   int rv;
3247
3248   sw_if_index = ntohl (mp->sw_if_index);
3249   rv = vnet_delete_loopback_interface (sw_if_index);
3250
3251   REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
3252 }
3253
3254 static void
3255 vl_api_control_ping_t_handler (vl_api_control_ping_t * mp)
3256 {
3257   vl_api_control_ping_reply_t *rmp;
3258   int rv = 0;
3259
3260   /* *INDENT-OFF* */
3261   REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
3262   ({
3263     rmp->vpe_pid = ntohl (getpid());
3264   }));
3265   /* *INDENT-ON* */
3266 }
3267
3268 static void
3269 shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
3270 {
3271   u8 **shmem_vecp = (u8 **) arg;
3272   u8 *shmem_vec;
3273   void *oldheap;
3274   api_main_t *am = &api_main;
3275   u32 offset;
3276
3277   shmem_vec = *shmem_vecp;
3278
3279   offset = vec_len (shmem_vec);
3280
3281   pthread_mutex_lock (&am->vlib_rp->mutex);
3282   oldheap = svm_push_data_heap (am->vlib_rp);
3283
3284   vec_validate (shmem_vec, offset + buffer_bytes - 1);
3285
3286   clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
3287
3288   svm_pop_heap (oldheap);
3289   pthread_mutex_unlock (&am->vlib_rp->mutex);
3290
3291   *shmem_vecp = shmem_vec;
3292 }
3293
3294
3295 static void
3296 vl_api_cli_request_t_handler (vl_api_cli_request_t * mp)
3297 {
3298   vl_api_cli_reply_t *rp;
3299   unix_shared_memory_queue_t *q;
3300   vlib_main_t *vm = vlib_get_main ();
3301   api_main_t *am = &api_main;
3302   unformat_input_t input;
3303   u8 *shmem_vec = 0;
3304   void *oldheap;
3305
3306   q = vl_api_client_index_to_input_queue (mp->client_index);
3307   if (!q)
3308     return;
3309
3310   rp = vl_msg_api_alloc (sizeof (*rp));
3311   rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY);
3312   rp->context = mp->context;
3313
3314   unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
3315
3316   vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
3317
3318   pthread_mutex_lock (&am->vlib_rp->mutex);
3319   oldheap = svm_push_data_heap (am->vlib_rp);
3320
3321   vec_add1 (shmem_vec, 0);
3322
3323   svm_pop_heap (oldheap);
3324   pthread_mutex_unlock (&am->vlib_rp->mutex);
3325
3326   rp->reply_in_shmem = (uword) shmem_vec;
3327
3328   vl_msg_api_send_shmem (q, (u8 *) & rp);
3329 }
3330
3331 static void
3332 inband_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
3333 {
3334   u8 **mem_vecp = (u8 **) arg;
3335   u8 *mem_vec = *mem_vecp;
3336   u32 offset = vec_len (mem_vec);
3337
3338   vec_validate (mem_vec, offset + buffer_bytes - 1);
3339   clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
3340   *mem_vecp = mem_vec;
3341 }
3342
3343 static void
3344 vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
3345 {
3346   vl_api_cli_inband_reply_t *rmp;
3347   int rv = 0;
3348   unix_shared_memory_queue_t *q;
3349   vlib_main_t *vm = vlib_get_main ();
3350   unformat_input_t input;
3351   u8 *out_vec = 0;
3352
3353   q = vl_api_client_index_to_input_queue (mp->client_index);
3354   if (!q)
3355     return;
3356
3357   unformat_init_string (&input, (char *) mp->cmd, ntohl (mp->length));
3358   vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
3359
3360   u32 len = vec_len (out_vec);
3361   /* *INDENT-OFF* */
3362   REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, len,
3363   ({
3364     rmp->length = htonl (len);
3365     clib_memcpy (rmp->reply, out_vec, len);
3366   }));
3367   /* *INDENT-ON* */
3368   vec_free (out_vec);
3369 }
3370
3371 static void
3372 vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
3373 {
3374   int rv;
3375   vl_api_set_arp_neighbor_limit_reply_t *rmp;
3376   vnet_main_t *vnm = vnet_get_main ();
3377   clib_error_t *error;
3378
3379   vnm->api_errno = 0;
3380
3381   if (mp->is_ipv6)
3382     error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
3383   else
3384     error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
3385
3386   if (error)
3387     {
3388       clib_error_report (error);
3389       rv = VNET_API_ERROR_UNSPECIFIED;
3390     }
3391   else
3392     {
3393       rv = vnm->api_errno;
3394     }
3395
3396   REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3397 }
3398
3399 static void vl_api_sr_tunnel_add_del_t_handler
3400   (vl_api_sr_tunnel_add_del_t * mp)
3401 {
3402 #if IP6SR == 0
3403   clib_warning ("unimplemented");
3404 #else
3405   ip6_sr_add_del_tunnel_args_t _a, *a = &_a;
3406   int rv = 0;
3407   vl_api_sr_tunnel_add_del_reply_t *rmp;
3408   ip6_address_t *segments = 0, *seg;
3409   ip6_address_t *tags = 0, *tag;
3410   ip6_address_t *this_address;
3411   int i;
3412
3413   if (mp->n_segments == 0)
3414     {
3415       rv = -11;
3416       goto out;
3417     }
3418
3419   memset (a, 0, sizeof (*a));
3420   a->src_address = (ip6_address_t *) & mp->src_address;
3421   a->dst_address = (ip6_address_t *) & mp->dst_address;
3422   a->dst_mask_width = mp->dst_mask_width;
3423   a->flags_net_byte_order = mp->flags_net_byte_order;
3424   a->is_del = (mp->is_add == 0);
3425   a->rx_table_id = ntohl (mp->outer_vrf_id);
3426   a->tx_table_id = ntohl (mp->inner_vrf_id);
3427
3428   a->name = format (0, "%s", mp->name);
3429   if (!(vec_len (a->name)))
3430     a->name = 0;
3431
3432   a->policy_name = format (0, "%s", mp->policy_name);
3433   if (!(vec_len (a->policy_name)))
3434     a->policy_name = 0;
3435
3436   /* Yank segments and tags out of the API message */
3437   this_address = (ip6_address_t *) mp->segs_and_tags;
3438   for (i = 0; i < mp->n_segments; i++)
3439     {
3440       vec_add2 (segments, seg, 1);
3441       clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
3442       this_address++;
3443     }
3444   for (i = 0; i < mp->n_tags; i++)
3445     {
3446       vec_add2 (tags, tag, 1);
3447       clib_memcpy (tag->as_u8, this_address->as_u8, sizeof (*this_address));
3448       this_address++;
3449     }
3450
3451   a->segments = segments;
3452   a->tags = tags;
3453
3454   rv = ip6_sr_add_del_tunnel (a);
3455
3456 out:
3457
3458   REPLY_MACRO (VL_API_SR_TUNNEL_ADD_DEL_REPLY);
3459 #endif
3460 }
3461
3462 static void vl_api_sr_policy_add_del_t_handler
3463   (vl_api_sr_policy_add_del_t * mp)
3464 {
3465 #if IP6SR == 0
3466   clib_warning ("unimplemented");
3467 #else
3468   ip6_sr_add_del_policy_args_t _a, *a = &_a;
3469   int rv = 0;
3470   vl_api_sr_policy_add_del_reply_t *rmp;
3471   int i;
3472
3473   memset (a, 0, sizeof (*a));
3474   a->is_del = (mp->is_add == 0);
3475
3476   a->name = format (0, "%s", mp->name);
3477   if (!(vec_len (a->name)))
3478     {
3479       rv = VNET_API_ERROR_NO_SUCH_NODE2;
3480       goto out;
3481     }
3482
3483   if (!(mp->tunnel_names[0]))
3484     {
3485       rv = VNET_API_ERROR_NO_SUCH_NODE2;
3486       goto out;
3487     }
3488
3489   // start deserializing tunnel_names
3490   int num_tunnels = mp->tunnel_names[0];        //number of tunnels
3491   u8 *deser_tun_names = mp->tunnel_names;
3492   deser_tun_names += 1;         //moving along
3493
3494   u8 *tun_name = 0;
3495   int tun_name_len = 0;
3496
3497   for (i = 0; i < num_tunnels; i++)
3498     {
3499       tun_name_len = *deser_tun_names;
3500       deser_tun_names += 1;
3501       vec_resize (tun_name, tun_name_len);
3502       memcpy (tun_name, deser_tun_names, tun_name_len);
3503       vec_add1 (a->tunnel_names, tun_name);
3504       deser_tun_names += tun_name_len;
3505       tun_name = 0;
3506     }
3507
3508   rv = ip6_sr_add_del_policy (a);
3509
3510 out:
3511
3512   REPLY_MACRO (VL_API_SR_POLICY_ADD_DEL_REPLY);
3513 #endif
3514 }
3515
3516 static void vl_api_sr_multicast_map_add_del_t_handler
3517   (vl_api_sr_multicast_map_add_del_t * mp)
3518 {
3519 #if IP6SR == 0
3520   clib_warning ("unimplemented");
3521 #else
3522   ip6_sr_add_del_multicastmap_args_t _a, *a = &_a;
3523   int rv = 0;
3524   vl_api_sr_multicast_map_add_del_reply_t *rmp;
3525
3526   memset (a, 0, sizeof (*a));
3527   a->is_del = (mp->is_add == 0);
3528
3529   a->multicast_address = (ip6_address_t *) & mp->multicast_address;
3530   a->policy_name = format (0, "%s", mp->policy_name);
3531
3532   if (a->multicast_address == 0)
3533     {
3534       rv = -1;
3535       goto out;
3536     }
3537
3538   if (!(a->policy_name))
3539     {
3540       rv = -2;
3541       goto out;
3542     }
3543
3544 #if DPDK > 0                    /* Cannot call replicate without DPDK */
3545   rv = ip6_sr_add_del_multicastmap (a);
3546 #else
3547   clib_warning ("multicast replication without DPDK not implemented");
3548   rv = VNET_API_ERROR_UNIMPLEMENTED;
3549 #endif /* DPDK */
3550
3551 out:
3552
3553   REPLY_MACRO (VL_API_SR_MULTICAST_MAP_ADD_DEL_REPLY);
3554 #endif
3555 }
3556
3557 #define foreach_classify_add_del_table_field    \
3558 _(table_index)                                  \
3559 _(nbuckets)                                     \
3560 _(memory_size)                                  \
3561 _(skip_n_vectors)                               \
3562 _(match_n_vectors)                              \
3563 _(next_table_index)                             \
3564 _(miss_next_index)                              \
3565 _(current_data_flag)                            \
3566 _(current_data_offset)
3567
3568 static void vl_api_classify_add_del_table_t_handler
3569   (vl_api_classify_add_del_table_t * mp)
3570 {
3571   vl_api_classify_add_del_table_reply_t *rmp;
3572   vnet_classify_main_t *cm = &vnet_classify_main;
3573   vnet_classify_table_t *t;
3574   int rv;
3575
3576 #define _(a) u32 a;
3577   foreach_classify_add_del_table_field;
3578 #undef _
3579
3580 #define _(a) a = ntohl(mp->a);
3581   foreach_classify_add_del_table_field;
3582 #undef _
3583
3584   /* The underlying API fails silently, on purpose, so check here */
3585   if (mp->is_add == 0)          /* delete */
3586     {
3587       if (pool_is_free_index (cm->tables, table_index))
3588         {
3589           rv = VNET_API_ERROR_NO_SUCH_TABLE;
3590           goto out;
3591         }
3592     }
3593   else                          /* add or update */
3594     {
3595       if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3596         table_index = ~0;
3597     }
3598
3599   rv = vnet_classify_add_del_table
3600     (cm, mp->mask, nbuckets, memory_size,
3601      skip_n_vectors, match_n_vectors,
3602      next_table_index, miss_next_index, &table_index,
3603      current_data_flag, current_data_offset, mp->is_add);
3604
3605 out:
3606   /* *INDENT-OFF* */
3607   REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
3608   ({
3609     if (rv == 0 && mp->is_add)
3610       {
3611         t = pool_elt_at_index (cm->tables, table_index);
3612         rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
3613         rmp->match_n_vectors = ntohl(t->match_n_vectors);
3614         rmp->new_table_index = ntohl(table_index);
3615       }
3616     else
3617       {
3618         rmp->skip_n_vectors = ~0;
3619         rmp->match_n_vectors = ~0;
3620         rmp->new_table_index = ~0;
3621       }
3622   }));
3623   /* *INDENT-ON* */
3624 }
3625
3626 static void vl_api_classify_add_del_session_t_handler
3627   (vl_api_classify_add_del_session_t * mp)
3628 {
3629   vnet_classify_main_t *cm = &vnet_classify_main;
3630   vl_api_classify_add_del_session_reply_t *rmp;
3631   int rv;
3632   u32 table_index, hit_next_index, opaque_index, metadata;
3633   i32 advance;
3634   u8 action;
3635
3636   table_index = ntohl (mp->table_index);
3637   hit_next_index = ntohl (mp->hit_next_index);
3638   opaque_index = ntohl (mp->opaque_index);
3639   advance = ntohl (mp->advance);
3640   action = mp->action;
3641   metadata = ntohl (mp->metadata);
3642
3643   rv = vnet_classify_add_del_session
3644     (cm, table_index, mp->match, hit_next_index, opaque_index,
3645      advance, action, metadata, mp->is_add);
3646
3647   REPLY_MACRO (VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
3648 }
3649
3650 static void vl_api_classify_set_interface_ip_table_t_handler
3651   (vl_api_classify_set_interface_ip_table_t * mp)
3652 {
3653   vlib_main_t *vm = vlib_get_main ();
3654   vl_api_classify_set_interface_ip_table_reply_t *rmp;
3655   int rv;
3656   u32 table_index, sw_if_index;
3657
3658   table_index = ntohl (mp->table_index);
3659   sw_if_index = ntohl (mp->sw_if_index);
3660
3661   VALIDATE_SW_IF_INDEX (mp);
3662
3663   if (mp->is_ipv6)
3664     rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3665   else
3666     rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
3667
3668   BAD_SW_IF_INDEX_LABEL;
3669
3670   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
3671 }
3672
3673 static void vl_api_classify_set_interface_l2_tables_t_handler
3674   (vl_api_classify_set_interface_l2_tables_t * mp)
3675 {
3676   vl_api_classify_set_interface_l2_tables_reply_t *rmp;
3677   int rv;
3678   u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
3679   int enable;
3680
3681   ip4_table_index = ntohl (mp->ip4_table_index);
3682   ip6_table_index = ntohl (mp->ip6_table_index);
3683   other_table_index = ntohl (mp->other_table_index);
3684   sw_if_index = ntohl (mp->sw_if_index);
3685
3686   VALIDATE_SW_IF_INDEX (mp);
3687
3688   if (mp->is_input)
3689     rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
3690                                             ip6_table_index,
3691                                             other_table_index);
3692   else
3693     rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
3694                                              ip6_table_index,
3695                                              other_table_index);
3696
3697   if (rv == 0)
3698     {
3699       if (ip4_table_index != ~0 || ip6_table_index != ~0
3700           || other_table_index != ~0)
3701         enable = 1;
3702       else
3703         enable = 0;
3704
3705       if (mp->is_input)
3706         vnet_l2_input_classify_enable_disable (sw_if_index, enable);
3707       else
3708         vnet_l2_output_classify_enable_disable (sw_if_index, enable);
3709     }
3710
3711   BAD_SW_IF_INDEX_LABEL;
3712
3713   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
3714 }
3715
3716 static void
3717 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
3718 {
3719   int rv = 0;
3720   vl_api_l2_fib_clear_table_reply_t *rmp;
3721
3722   /* DAW-FIXME: This API should only clear non-static l2fib entries, but
3723    *            that is not currently implemented.  When that TODO is fixed
3724    *            this call should be changed to pass 1 instead of 0.
3725    */
3726   l2fib_clear_table (0);
3727
3728   REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
3729 }
3730
3731 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
3732                                      u32 sw_if_index, u32 enable);
3733
3734 static void
3735 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
3736                                           mp)
3737 {
3738   int rv;
3739   vl_api_l2_interface_efp_filter_reply_t *rmp;
3740   vnet_main_t *vnm = vnet_get_main ();
3741
3742   // enable/disable the feature
3743   l2_efp_filter_configure (vnm, mp->sw_if_index, mp->enable_disable);
3744   rv = vnm->api_errno;
3745
3746   REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
3747 }
3748
3749 static void
3750   vl_api_l2_interface_vlan_tag_rewrite_t_handler
3751   (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
3752 {
3753   int rv = 0;
3754   vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
3755   vnet_main_t *vnm = vnet_get_main ();
3756   vlib_main_t *vm = vlib_get_main ();
3757   u32 vtr_op;
3758
3759   VALIDATE_SW_IF_INDEX (mp);
3760
3761   vtr_op = ntohl (mp->vtr_op);
3762
3763   /* The L2 code is unsuspicious */
3764   switch (vtr_op)
3765     {
3766     case L2_VTR_DISABLED:
3767     case L2_VTR_PUSH_1:
3768     case L2_VTR_PUSH_2:
3769     case L2_VTR_POP_1:
3770     case L2_VTR_POP_2:
3771     case L2_VTR_TRANSLATE_1_1:
3772     case L2_VTR_TRANSLATE_1_2:
3773     case L2_VTR_TRANSLATE_2_1:
3774     case L2_VTR_TRANSLATE_2_2:
3775       break;
3776
3777     default:
3778       rv = VNET_API_ERROR_INVALID_VALUE;
3779       goto bad_sw_if_index;
3780     }
3781
3782   rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
3783                         ntohl (mp->push_dot1q), ntohl (mp->tag1),
3784                         ntohl (mp->tag2));
3785
3786   BAD_SW_IF_INDEX_LABEL;
3787
3788   REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
3789 }
3790
3791 static void
3792 vl_api_create_vhost_user_if_t_handler (vl_api_create_vhost_user_if_t * mp)
3793 {
3794   int rv = 0;
3795   vl_api_create_vhost_user_if_reply_t *rmp;
3796   u32 sw_if_index = (u32) ~ 0;
3797   vnet_main_t *vnm = vnet_get_main ();
3798   vlib_main_t *vm = vlib_get_main ();
3799
3800   rv = vhost_user_create_if (vnm, vm, (char *) mp->sock_filename,
3801                              mp->is_server, &sw_if_index, (u64) ~ 0,
3802                              mp->renumber, ntohl (mp->custom_dev_instance),
3803                              (mp->use_custom_mac) ? mp->mac_address : NULL);
3804
3805   /* Remember an interface tag for the new interface */
3806   if (rv == 0)
3807     {
3808       /* If a tag was supplied... */
3809       if (mp->tag[0])
3810         {
3811           /* Make sure it's a proper C-string */
3812           mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
3813           u8 *tag = format (0, "%s%c", mp->tag, 0);
3814           vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
3815         }
3816     }
3817
3818   /* *INDENT-OFF* */
3819   REPLY_MACRO2(VL_API_CREATE_VHOST_USER_IF_REPLY,
3820   ({
3821     rmp->sw_if_index = ntohl (sw_if_index);
3822   }));
3823   /* *INDENT-ON* */
3824 }
3825
3826 static void
3827 vl_api_modify_vhost_user_if_t_handler (vl_api_modify_vhost_user_if_t * mp)
3828 {
3829   int rv = 0;
3830   vl_api_modify_vhost_user_if_reply_t *rmp;
3831   u32 sw_if_index = ntohl (mp->sw_if_index);
3832
3833   vnet_main_t *vnm = vnet_get_main ();
3834   vlib_main_t *vm = vlib_get_main ();
3835
3836   rv = vhost_user_modify_if (vnm, vm, (char *) mp->sock_filename,
3837                              mp->is_server, sw_if_index, (u64) ~ 0,
3838                              mp->renumber, ntohl (mp->custom_dev_instance));
3839
3840   REPLY_MACRO (VL_API_MODIFY_VHOST_USER_IF_REPLY);
3841 }
3842
3843 static void
3844 vl_api_delete_vhost_user_if_t_handler (vl_api_delete_vhost_user_if_t * mp)
3845 {
3846   int rv = 0;
3847   vl_api_delete_vhost_user_if_reply_t *rmp;
3848   vpe_api_main_t *vam = &vpe_api_main;
3849   u32 sw_if_index = ntohl (mp->sw_if_index);
3850
3851   vnet_main_t *vnm = vnet_get_main ();
3852   vlib_main_t *vm = vlib_get_main ();
3853
3854   rv = vhost_user_delete_if (vnm, vm, sw_if_index);
3855
3856   REPLY_MACRO (VL_API_DELETE_VHOST_USER_IF_REPLY);
3857   if (!rv)
3858     {
3859       unix_shared_memory_queue_t *q =
3860         vl_api_client_index_to_input_queue (mp->client_index);
3861       if (!q)
3862         return;
3863
3864       vnet_clear_sw_interface_tag (vnm, sw_if_index);
3865       send_sw_interface_flags_deleted (vam, q, sw_if_index);
3866     }
3867 }
3868
3869 static void
3870   vl_api_sw_interface_vhost_user_details_t_handler
3871   (vl_api_sw_interface_vhost_user_details_t * mp)
3872 {
3873   clib_warning ("BUG");
3874 }
3875
3876 static void
3877 send_sw_interface_vhost_user_details (vpe_api_main_t * am,
3878                                       unix_shared_memory_queue_t * q,
3879                                       vhost_user_intf_details_t * vui,
3880                                       u32 context)
3881 {
3882   vl_api_sw_interface_vhost_user_details_t *mp;
3883
3884   mp = vl_msg_api_alloc (sizeof (*mp));
3885   memset (mp, 0, sizeof (*mp));
3886   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_VHOST_USER_DETAILS);
3887   mp->sw_if_index = ntohl (vui->sw_if_index);
3888   mp->virtio_net_hdr_sz = ntohl (vui->virtio_net_hdr_sz);
3889   mp->features = clib_net_to_host_u64 (vui->features);
3890   mp->is_server = vui->is_server;
3891   mp->num_regions = ntohl (vui->num_regions);
3892   mp->sock_errno = ntohl (vui->sock_errno);
3893   mp->context = context;
3894
3895   strncpy ((char *) mp->sock_filename,
3896            (char *) vui->sock_filename, ARRAY_LEN (mp->sock_filename) - 1);
3897   strncpy ((char *) mp->interface_name,
3898            (char *) vui->if_name, ARRAY_LEN (mp->interface_name) - 1);
3899
3900   vl_msg_api_send_shmem (q, (u8 *) & mp);
3901 }
3902
3903 static void
3904   vl_api_sw_interface_vhost_user_dump_t_handler
3905   (vl_api_sw_interface_vhost_user_dump_t * mp)
3906 {
3907   int rv = 0;
3908   vpe_api_main_t *am = &vpe_api_main;
3909   vnet_main_t *vnm = vnet_get_main ();
3910   vlib_main_t *vm = vlib_get_main ();
3911   vhost_user_intf_details_t *ifaces = NULL;
3912   vhost_user_intf_details_t *vuid = NULL;
3913   unix_shared_memory_queue_t *q;
3914
3915   q = vl_api_client_index_to_input_queue (mp->client_index);
3916   if (q == 0)
3917     return;
3918
3919   rv = vhost_user_dump_ifs (vnm, vm, &ifaces);
3920   if (rv)
3921     return;
3922
3923   vec_foreach (vuid, ifaces)
3924   {
3925     send_sw_interface_vhost_user_details (am, q, vuid, mp->context);
3926   }
3927   vec_free (ifaces);
3928 }
3929
3930 static void
3931 send_sw_if_l2tpv3_tunnel_details (vpe_api_main_t * am,
3932                                   unix_shared_memory_queue_t * q,
3933                                   l2t_session_t * s,
3934                                   l2t_main_t * lm, u32 context)
3935 {
3936   vl_api_sw_if_l2tpv3_tunnel_details_t *mp;
3937   u8 *if_name = NULL;
3938   vnet_sw_interface_t *si = NULL;
3939
3940   si = vnet_get_hw_sw_interface (lm->vnet_main, s->hw_if_index);
3941
3942   if_name = format (if_name, "%U",
3943                     format_vnet_sw_interface_name, lm->vnet_main, si);
3944
3945   mp = vl_msg_api_alloc (sizeof (*mp));
3946   memset (mp, 0, sizeof (*mp));
3947   mp->_vl_msg_id = ntohs (VL_API_SW_IF_L2TPV3_TUNNEL_DETAILS);
3948   strncpy ((char *) mp->interface_name,
3949            (char *) if_name, ARRAY_LEN (mp->interface_name) - 1);
3950   mp->sw_if_index = ntohl (si->sw_if_index);
3951   mp->local_session_id = s->local_session_id;
3952   mp->remote_session_id = s->remote_session_id;
3953   mp->local_cookie[0] = s->local_cookie[0];
3954   mp->local_cookie[1] = s->local_cookie[1];
3955   mp->remote_cookie = s->remote_cookie;
3956   clib_memcpy (mp->client_address, &s->client_address,
3957                sizeof (s->client_address));
3958   clib_memcpy (mp->our_address, &s->our_address, sizeof (s->our_address));
3959   mp->l2_sublayer_present = s->l2_sublayer_present;
3960   mp->context = context;
3961
3962   vl_msg_api_send_shmem (q, (u8 *) & mp);
3963 }
3964
3965 static void
3966 send_ip_address_details (vpe_api_main_t * am,
3967                          unix_shared_memory_queue_t * q,
3968                          u8 * ip, u16 prefix_length, u8 is_ipv6, u32 context)
3969 {
3970   vl_api_ip_address_details_t *mp;
3971
3972   mp = vl_msg_api_alloc (sizeof (*mp));
3973   memset (mp, 0, sizeof (*mp));
3974   mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
3975
3976   if (is_ipv6)
3977     {
3978       clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
3979     }
3980   else
3981     {
3982       u32 *tp = (u32 *) mp->ip;
3983       *tp = *(u32 *) ip;
3984     }
3985   mp->prefix_length = prefix_length;
3986   mp->context = context;
3987
3988   vl_msg_api_send_shmem (q, (u8 *) & mp);
3989 }
3990
3991 static void
3992 vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
3993 {
3994   vpe_api_main_t *am = &vpe_api_main;
3995   unix_shared_memory_queue_t *q;
3996   ip6_address_t *r6;
3997   ip4_address_t *r4;
3998   ip6_main_t *im6 = &ip6_main;
3999   ip4_main_t *im4 = &ip4_main;
4000   ip_lookup_main_t *lm6 = &im6->lookup_main;
4001   ip_lookup_main_t *lm4 = &im4->lookup_main;
4002   ip_interface_address_t *ia = 0;
4003   u32 sw_if_index = ~0;
4004   int rv __attribute__ ((unused)) = 0;
4005
4006   VALIDATE_SW_IF_INDEX (mp);
4007
4008   sw_if_index = ntohl (mp->sw_if_index);
4009
4010   q = vl_api_client_index_to_input_queue (mp->client_index);
4011   if (q == 0)
4012     {
4013       return;
4014     }
4015
4016   if (mp->is_ipv6)
4017     {
4018       /* *INDENT-OFF* */
4019       foreach_ip_interface_address (lm6, ia, sw_if_index,
4020                                     1 /* honor unnumbered */,
4021       ({
4022         r6 = ip_interface_address_get_address (lm6, ia);
4023         u16 prefix_length = ia->address_length;
4024         send_ip_address_details(am, q, (u8*)r6, prefix_length, 1, mp->context);
4025       }));
4026       /* *INDENT-ON* */
4027     }
4028   else
4029     {
4030       /* *INDENT-OFF* */
4031       foreach_ip_interface_address (lm4, ia, sw_if_index,
4032                                     1 /* honor unnumbered */,
4033       ({
4034         r4 = ip_interface_address_get_address (lm4, ia);
4035         u16 prefix_length = ia->address_length;
4036         send_ip_address_details(am, q, (u8*)r4, prefix_length, 0, mp->context);
4037       }));
4038       /* *INDENT-ON* */
4039     }
4040   BAD_SW_IF_INDEX_LABEL;
4041 }
4042
4043 static void
4044 send_ip_details (vpe_api_main_t * am,
4045                  unix_shared_memory_queue_t * q, u32 sw_if_index, u32 context)
4046 {
4047   vl_api_ip_details_t *mp;
4048
4049   mp = vl_msg_api_alloc (sizeof (*mp));
4050   memset (mp, 0, sizeof (*mp));
4051   mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
4052
4053   mp->sw_if_index = ntohl (sw_if_index);
4054   mp->context = context;
4055
4056   vl_msg_api_send_shmem (q, (u8 *) & mp);
4057 }
4058
4059 static void
4060 vl_api_sw_if_l2tpv3_tunnel_dump_t_handler (vl_api_sw_if_l2tpv3_tunnel_dump_t *
4061                                            mp)
4062 {
4063   vpe_api_main_t *am = &vpe_api_main;
4064   l2t_main_t *lm = &l2t_main;
4065   unix_shared_memory_queue_t *q;
4066   l2t_session_t *session;
4067
4068   q = vl_api_client_index_to_input_queue (mp->client_index);
4069   if (q == 0)
4070     return;
4071
4072   /* *INDENT-OFF* */
4073   pool_foreach (session, lm->sessions,
4074   ({
4075     send_sw_if_l2tpv3_tunnel_details (am, q, session, lm, mp->context);
4076   }));
4077   /* *INDENT-ON* */
4078 }
4079
4080
4081 static void
4082 send_sw_interface_tap_details (vpe_api_main_t * am,
4083                                unix_shared_memory_queue_t * q,
4084                                tapcli_interface_details_t * tap_if,
4085                                u32 context)
4086 {
4087   vl_api_sw_interface_tap_details_t *mp;
4088   mp = vl_msg_api_alloc (sizeof (*mp));
4089   memset (mp, 0, sizeof (*mp));
4090   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_TAP_DETAILS);
4091   mp->sw_if_index = ntohl (tap_if->sw_if_index);
4092   strncpy ((char *) mp->dev_name,
4093            (char *) tap_if->dev_name, ARRAY_LEN (mp->dev_name) - 1);
4094   mp->context = context;
4095
4096   vl_msg_api_send_shmem (q, (u8 *) & mp);
4097 }
4098
4099 static void
4100 vl_api_sw_interface_tap_dump_t_handler (vl_api_sw_interface_tap_dump_t * mp)
4101 {
4102   int rv = 0;
4103   vpe_api_main_t *am = &vpe_api_main;
4104   unix_shared_memory_queue_t *q;
4105   tapcli_interface_details_t *tapifs = NULL;
4106   tapcli_interface_details_t *tap_if = NULL;
4107
4108   q = vl_api_client_index_to_input_queue (mp->client_index);
4109   if (q == 0)
4110     return;
4111
4112   rv = vnet_tap_dump_ifs (&tapifs);
4113   if (rv)
4114     return;
4115
4116   vec_foreach (tap_if, tapifs)
4117   {
4118     send_sw_interface_tap_details (am, q, tap_if, mp->context);
4119   }
4120
4121   vec_free (tapifs);
4122 }
4123
4124 static void
4125 vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
4126 {
4127   vpe_api_main_t *am = &vpe_api_main;
4128   vnet_main_t *vnm = vnet_get_main ();
4129   vlib_main_t *vm = vlib_get_main ();
4130   vnet_interface_main_t *im = &vnm->interface_main;
4131   unix_shared_memory_queue_t *q;
4132   vnet_sw_interface_t *si, *sorted_sis;
4133   u32 sw_if_index = ~0;
4134
4135   q = vl_api_client_index_to_input_queue (mp->client_index);
4136   if (q == 0)
4137     {
4138       return;
4139     }
4140
4141   /* Gather interfaces. */
4142   sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
4143   _vec_len (sorted_sis) = 0;
4144   /* *INDENT-OFF* */
4145   pool_foreach (si, im->sw_interfaces,
4146   ({
4147     vec_add1 (sorted_sis, si[0]);
4148   }));
4149   /* *INDENT-ON* */
4150
4151   vec_foreach (si, sorted_sis)
4152   {
4153     if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
4154       {
4155         if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
4156           {
4157             continue;
4158           }
4159         sw_if_index = si->sw_if_index;
4160         send_ip_details (am, q, sw_if_index, mp->context);
4161       }
4162   }
4163 }
4164
4165 static void
4166 vl_api_l2_fib_table_entry_t_handler (vl_api_l2_fib_table_entry_t * mp)
4167 {
4168   clib_warning ("BUG");
4169 }
4170
4171 static void
4172 send_l2fib_table_entry (vpe_api_main_t * am,
4173                         unix_shared_memory_queue_t * q,
4174                         l2fib_entry_key_t * l2fe_key,
4175                         l2fib_entry_result_t * l2fe_res, u32 context)
4176 {
4177   vl_api_l2_fib_table_entry_t *mp;
4178
4179   mp = vl_msg_api_alloc (sizeof (*mp));
4180   memset (mp, 0, sizeof (*mp));
4181   mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_ENTRY);
4182
4183   mp->bd_id =
4184     ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
4185
4186   mp->mac = l2fib_make_key (l2fe_key->fields.mac, 0);
4187   mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
4188   mp->static_mac = l2fe_res->fields.static_mac;
4189   mp->filter_mac = l2fe_res->fields.filter;
4190   mp->bvi_mac = l2fe_res->fields.bvi;
4191   mp->context = context;
4192
4193   vl_msg_api_send_shmem (q, (u8 *) & mp);
4194 }
4195
4196 static void
4197 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
4198 {
4199   vpe_api_main_t *am = &vpe_api_main;
4200   bd_main_t *bdm = &bd_main;
4201   l2fib_entry_key_t *l2fe_key = NULL;
4202   l2fib_entry_result_t *l2fe_res = NULL;
4203   u32 ni, bd_id = ntohl (mp->bd_id);
4204   u32 bd_index;
4205   unix_shared_memory_queue_t *q;
4206   uword *p;
4207
4208   q = vl_api_client_index_to_input_queue (mp->client_index);
4209   if (q == 0)
4210     return;
4211
4212   /* see l2fib_table_dump: ~0 means "any" */
4213   if (bd_id == ~0)
4214     bd_index = ~0;
4215   else
4216     {
4217       p = hash_get (bdm->bd_index_by_bd_id, bd_id);
4218       if (p == 0)
4219         return;
4220
4221       bd_index = p[0];
4222     }
4223
4224   l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
4225
4226   vec_foreach_index (ni, l2fe_key)
4227   {
4228     send_l2fib_table_entry (am, q, vec_elt_at_index (l2fe_key, ni),
4229                             vec_elt_at_index (l2fe_res, ni), mp->context);
4230   }
4231   vec_free (l2fe_key);
4232   vec_free (l2fe_res);
4233 }
4234
4235 static void
4236 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
4237 {
4238   vl_api_show_version_reply_t *rmp;
4239   int rv = 0;
4240   char *vpe_api_get_build_directory (void);
4241   char *vpe_api_get_version (void);
4242   char *vpe_api_get_build_date (void);
4243
4244   unix_shared_memory_queue_t *q =
4245     vl_api_client_index_to_input_queue (mp->client_index);
4246
4247   if (!q)
4248     return;
4249
4250   /* *INDENT-OFF* */
4251   REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
4252   ({
4253     strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
4254     strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
4255              ARRAY_LEN(rmp->build_directory)-1);
4256     strncpy ((char *) rmp->version, vpe_api_get_version(),
4257              ARRAY_LEN(rmp->version)-1);
4258     strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
4259              ARRAY_LEN(rmp->build_date)-1);
4260   }));
4261   /* *INDENT-ON* */
4262 }
4263
4264 static void
4265 vl_api_get_node_index_t_handler (vl_api_get_node_index_t * mp)
4266 {
4267   vlib_main_t *vm = vlib_get_main ();
4268   vl_api_get_node_index_reply_t *rmp;
4269   vlib_node_t *n;
4270   int rv = 0;
4271   u32 node_index = ~0;
4272
4273   n = vlib_get_node_by_name (vm, mp->node_name);
4274
4275   if (n == 0)
4276     rv = VNET_API_ERROR_NO_SUCH_NODE;
4277   else
4278     node_index = n->index;
4279
4280   /* *INDENT-OFF* */
4281   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
4282   ({
4283     rmp->node_index = ntohl(node_index);
4284   }));
4285   /* *INDENT-ON* */
4286 }
4287
4288 static void
4289 vl_api_get_next_index_t_handler (vl_api_get_next_index_t * mp)
4290 {
4291   vlib_main_t *vm = vlib_get_main ();
4292   vl_api_get_next_index_reply_t *rmp;
4293   vlib_node_t *node, *next_node;
4294   int rv = 0;
4295   u32 next_node_index = ~0, next_index = ~0;
4296   uword *p;
4297
4298   node = vlib_get_node_by_name (vm, mp->node_name);
4299
4300   if (node == 0)
4301     {
4302       rv = VNET_API_ERROR_NO_SUCH_NODE;
4303       goto out;
4304     }
4305
4306   next_node = vlib_get_node_by_name (vm, mp->next_name);
4307
4308   if (next_node == 0)
4309     {
4310       rv = VNET_API_ERROR_NO_SUCH_NODE2;
4311       goto out;
4312     }
4313   else
4314     next_node_index = next_node->index;
4315
4316   p = hash_get (node->next_slot_by_node, next_node_index);
4317
4318   if (p == 0)
4319     {
4320       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
4321       goto out;
4322     }
4323   else
4324     next_index = p[0];
4325
4326 out:
4327   /* *INDENT-OFF* */
4328   REPLY_MACRO2(VL_API_GET_NEXT_INDEX_REPLY,
4329   ({
4330     rmp->next_index = ntohl(next_index);
4331   }));
4332   /* *INDENT-ON* */
4333 }
4334
4335 static void
4336 vl_api_add_node_next_t_handler (vl_api_add_node_next_t * mp)
4337 {
4338   vlib_main_t *vm = vlib_get_main ();
4339   vl_api_add_node_next_reply_t *rmp;
4340   vlib_node_t *n, *next;
4341   int rv = 0;
4342   u32 next_index = ~0;
4343
4344   n = vlib_get_node_by_name (vm, mp->node_name);
4345
4346   if (n == 0)
4347     {
4348       rv = VNET_API_ERROR_NO_SUCH_NODE;
4349       goto out;
4350     }
4351
4352   next = vlib_get_node_by_name (vm, mp->next_name);
4353
4354   if (next == 0)
4355     rv = VNET_API_ERROR_NO_SUCH_NODE2;
4356   else
4357     next_index = vlib_node_add_next (vm, n->index, next->index);
4358
4359 out:
4360   /* *INDENT-OFF* */
4361   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
4362   ({
4363     rmp->next_index = ntohl(next_index);
4364   }));
4365   /* *INDENT-ON* */
4366 }
4367
4368 static void vl_api_l2tpv3_create_tunnel_t_handler
4369   (vl_api_l2tpv3_create_tunnel_t * mp)
4370 {
4371   vl_api_l2tpv3_create_tunnel_reply_t *rmp;
4372   l2t_main_t *lm = &l2t_main;
4373   u32 sw_if_index = (u32) ~ 0;
4374   int rv;
4375
4376   if (mp->is_ipv6 != 1)
4377     {
4378       rv = VNET_API_ERROR_UNIMPLEMENTED;
4379       goto out;
4380     }
4381
4382   u32 encap_fib_index;
4383
4384   if (mp->encap_vrf_id != ~0)
4385     {
4386       uword *p;
4387       ip6_main_t *im = &ip6_main;
4388       if (!
4389           (p =
4390            hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id))))
4391         {
4392           rv = VNET_API_ERROR_NO_SUCH_FIB;
4393           goto out;
4394         }
4395       encap_fib_index = p[0];
4396     }
4397   else
4398     {
4399       encap_fib_index = ~0;
4400     }
4401
4402   rv = create_l2tpv3_ipv6_tunnel (lm,
4403                                   (ip6_address_t *) mp->client_address,
4404                                   (ip6_address_t *) mp->our_address,
4405                                   ntohl (mp->local_session_id),
4406                                   ntohl (mp->remote_session_id),
4407                                   clib_net_to_host_u64 (mp->local_cookie),
4408                                   clib_net_to_host_u64 (mp->remote_cookie),
4409                                   mp->l2_sublayer_present,
4410                                   encap_fib_index, &sw_if_index);
4411
4412 out:
4413   /* *INDENT-OFF* */
4414   REPLY_MACRO2(VL_API_L2TPV3_CREATE_TUNNEL_REPLY,
4415   ({
4416     rmp->sw_if_index = ntohl (sw_if_index);
4417   }));
4418   /* *INDENT-ON* */
4419 }
4420
4421 static void vl_api_l2tpv3_set_tunnel_cookies_t_handler
4422   (vl_api_l2tpv3_set_tunnel_cookies_t * mp)
4423 {
4424   vl_api_l2tpv3_set_tunnel_cookies_reply_t *rmp;
4425   l2t_main_t *lm = &l2t_main;
4426   int rv;
4427
4428   VALIDATE_SW_IF_INDEX (mp);
4429
4430   rv = l2tpv3_set_tunnel_cookies (lm, ntohl (mp->sw_if_index),
4431                                   clib_net_to_host_u64 (mp->new_local_cookie),
4432                                   clib_net_to_host_u64
4433                                   (mp->new_remote_cookie));
4434
4435   BAD_SW_IF_INDEX_LABEL;
4436
4437   REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY);
4438 }
4439
4440 static void vl_api_l2tpv3_interface_enable_disable_t_handler
4441   (vl_api_l2tpv3_interface_enable_disable_t * mp)
4442 {
4443   int rv;
4444   vnet_main_t *vnm = vnet_get_main ();
4445   vl_api_l2tpv3_interface_enable_disable_reply_t *rmp;
4446
4447   VALIDATE_SW_IF_INDEX (mp);
4448
4449   rv = l2tpv3_interface_enable_disable
4450     (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
4451
4452   BAD_SW_IF_INDEX_LABEL;
4453
4454   REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY);
4455 }
4456
4457 static void vl_api_l2tpv3_set_lookup_key_t_handler
4458   (vl_api_l2tpv3_set_lookup_key_t * mp)
4459 {
4460   int rv = 0;
4461   l2t_main_t *lm = &l2t_main;
4462   vl_api_l2tpv3_set_lookup_key_reply_t *rmp;
4463
4464   if (mp->key > L2T_LOOKUP_SESSION_ID)
4465     {
4466       rv = VNET_API_ERROR_INVALID_VALUE;
4467       goto out;
4468     }
4469
4470   lm->lookup_type = mp->key;
4471
4472 out:
4473   REPLY_MACRO (VL_API_L2TPV3_SET_LOOKUP_KEY_REPLY);
4474 }
4475
4476 static void vl_api_vxlan_add_del_tunnel_t_handler
4477   (vl_api_vxlan_add_del_tunnel_t * mp)
4478 {
4479   vl_api_vxlan_add_del_tunnel_reply_t *rmp;
4480   int rv = 0;
4481   vnet_vxlan_add_del_tunnel_args_t _a, *a = &_a;
4482   u32 encap_fib_index;
4483   uword *p;
4484   ip4_main_t *im = &ip4_main;
4485   u32 sw_if_index = ~0;
4486
4487   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
4488   if (!p)
4489     {
4490       rv = VNET_API_ERROR_NO_SUCH_FIB;
4491       goto out;
4492     }
4493   encap_fib_index = p[0];
4494   memset (a, 0, sizeof (*a));
4495
4496   a->is_add = mp->is_add;
4497   a->is_ip6 = mp->is_ipv6;
4498
4499   /* ip addresses sent in network byte order */
4500   ip46_from_addr_buf (mp->is_ipv6, mp->dst_address, &a->dst);
4501   ip46_from_addr_buf (mp->is_ipv6, mp->src_address, &a->src);
4502
4503   /* Check src & dst are different */
4504   if (ip46_address_cmp (&a->dst, &a->src) == 0)
4505     {
4506       rv = VNET_API_ERROR_SAME_SRC_DST;
4507       goto out;
4508     }
4509   a->mcast_sw_if_index = ntohl (mp->mcast_sw_if_index);
4510   a->encap_fib_index = encap_fib_index;
4511   a->decap_next_index = ntohl (mp->decap_next_index);
4512   a->vni = ntohl (mp->vni);
4513   rv = vnet_vxlan_add_del_tunnel (a, &sw_if_index);
4514
4515 out:
4516   /* *INDENT-OFF* */
4517   REPLY_MACRO2(VL_API_VXLAN_ADD_DEL_TUNNEL_REPLY,
4518   ({
4519     rmp->sw_if_index = ntohl (sw_if_index);
4520   }));
4521   /* *INDENT-ON* */
4522 }
4523
4524 static void send_vxlan_tunnel_details
4525   (vxlan_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4526 {
4527   vl_api_vxlan_tunnel_details_t *rmp;
4528   ip4_main_t *im4 = &ip4_main;
4529   ip6_main_t *im6 = &ip6_main;
4530   u8 is_ipv6 = !ip46_address_is_ip4 (&t->dst);
4531
4532   rmp = vl_msg_api_alloc (sizeof (*rmp));
4533   memset (rmp, 0, sizeof (*rmp));
4534   rmp->_vl_msg_id = ntohs (VL_API_VXLAN_TUNNEL_DETAILS);
4535   if (is_ipv6)
4536     {
4537       memcpy (rmp->src_address, t->src.ip6.as_u8, 16);
4538       memcpy (rmp->dst_address, t->dst.ip6.as_u8, 16);
4539       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
4540     }
4541   else
4542     {
4543       memcpy (rmp->src_address, t->src.ip4.as_u8, 4);
4544       memcpy (rmp->dst_address, t->dst.ip4.as_u8, 4);
4545       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
4546     }
4547   rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
4548   rmp->vni = htonl (t->vni);
4549   rmp->decap_next_index = htonl (t->decap_next_index);
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  */