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