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