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