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