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