LISP Source/Dest control plane support, VPP-197
[vpp.git] / vpp / vpp-api / api.c
1 /*
2  *------------------------------------------------------------------
3  * api.c - message handler registration
4  *
5  * Copyright (c) 2010 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/mman.h>
25 #include <sys/stat.h>
26 #include <netinet/in.h>
27 #include <signal.h>
28 #include <pthread.h>
29 #include <unistd.h>
30 #include <time.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <pwd.h>
34 #include <grp.h>
35
36 #include <vppinfra/clib.h>
37 #include <vppinfra/vec.h>
38 #include <vppinfra/hash.h>
39 #include <vppinfra/bitmap.h>
40 #include <vppinfra/fifo.h>
41 #include <vppinfra/time.h>
42 #include <vppinfra/mheap.h>
43 #include <vppinfra/heap.h>
44 #include <vppinfra/pool.h>
45 #include <vppinfra/format.h>
46 #include <vppinfra/error.h>
47
48 #include <vnet/api_errno.h>
49 #include <vnet/vnet.h>
50 #include <vnet/l2/l2_input.h>
51 #include <vnet/l2/l2_bd.h>
52 #include <vnet/l2tp/l2tp.h>
53 #include <vnet/ip/ip.h>
54 #include <vnet/ip/ip6.h>
55 #include <vnet/unix/tuntap.h>
56 #include <vnet/unix/tapcli.h>
57 #include <vnet/mpls/mpls.h>
58 #include <vnet/dhcp/proxy.h>
59 #include <vnet/dhcp/client.h>
60 #if IPV6SR > 0
61 #include <vnet/sr/sr.h>
62 #endif
63 #include <vnet/dhcpv6/proxy.h>
64 #include <vlib/vlib.h>
65 #include <vlib/unix/unix.h>
66 #include <vlibapi/api.h>
67 #include <vlibmemory/api.h>
68 #include <vnet/classify/vnet_classify.h>
69 #include <vnet/classify/input_acl.h>
70 #include <vnet/classify/policer_classify.h>
71 #include <vnet/l2/l2_classify.h>
72 #include <vnet/vxlan/vxlan.h>
73 #include <vnet/gre/gre.h>
74 #include <vnet/l2/l2_vtr.h>
75 #include <vnet/vxlan-gpe/vxlan_gpe.h>
76 #include <vnet/lisp-gpe/lisp_gpe.h>
77 #include <vnet/lisp-cp/control.h>
78 #include <vnet/map/map.h>
79 #include <vnet/cop/cop.h>
80 #include <vnet/ip/ip6_hop_by_hop.h>
81 #include <vnet/ip/ip_source_and_port_range_check.h>
82 #include <vnet/devices/af_packet/af_packet.h>
83 #include <vnet/policer/policer.h>
84 #include <vnet/devices/netmap/netmap.h>
85 #include <vnet/flow/flow_report.h>
86 #include <vnet/ipsec-gre/ipsec_gre.h>
87 #include <vnet/flow/flow_report_classify.h>
88
89 #undef BIHASH_TYPE
90 #undef __included_bihash_template_h__
91 #include <vnet/l2/l2_fib.h>
92
93 #if IPSEC > 0
94 #include <vnet/ipsec/ipsec.h>
95 #include <vnet/ipsec/ikev2.h>
96 #endif /* IPSEC */
97 #include <vnet/devices/virtio/vhost-user.h>
98
99 #include <stats/stats.h>
100 #include <oam/oam.h>
101
102 #include <vnet/ethernet/ethernet.h>
103 #include <vnet/ethernet/arp_packet.h>
104 #include <vnet/interface.h>
105
106 #include <vnet/l2/l2_fib.h>
107 #include <vnet/l2/l2_bd.h>
108 #include <vpp-api/vpe_msg_enum.h>
109
110 #include <vnet/fib/ip6_fib.h>
111 #include <vnet/fib/ip4_fib.h>
112 #include <vnet/dpo/drop_dpo.h>
113 #include <vnet/dpo/receive_dpo.h>
114 #include <vnet/dpo/lookup_dpo.h>
115 #include <vnet/dpo/classify_dpo.h>
116
117 #define f64_endian(a)
118 #define f64_print(a,b)
119
120 #define vl_typedefs             /* define message structures */
121 #include <vpp-api/vpe_all_api_h.h>
122 #undef vl_typedefs
123
124 #define vl_endianfun            /* define message structures */
125 #include <vpp-api/vpe_all_api_h.h>
126 #undef vl_endianfun
127
128 /* instantiate all the print functions we know about */
129 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
130 #define vl_printfun
131 #include <vpp-api/vpe_all_api_h.h>
132 #undef vl_printfun
133
134 #define REPLY_MACRO(t)                                          \
135 do {                                                            \
136     unix_shared_memory_queue_t * q;                             \
137     rv = vl_msg_api_pd_handler (mp, rv);                        \
138     q = vl_api_client_index_to_input_queue (mp->client_index);  \
139     if (!q)                                                     \
140         return;                                                 \
141                                                                 \
142     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
143     rmp->_vl_msg_id = ntohs((t));                               \
144     rmp->context = mp->context;                                 \
145     rmp->retval = ntohl(rv);                                    \
146                                                                 \
147     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
148 } while(0);
149
150 #define REPLY_MACRO2(t, body)                                   \
151 do {                                                            \
152     unix_shared_memory_queue_t * q;                             \
153     rv = vl_msg_api_pd_handler (mp, rv);                        \
154     q = vl_api_client_index_to_input_queue (mp->client_index);  \
155     if (!q)                                                     \
156         return;                                                 \
157                                                                 \
158     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
159     rmp->_vl_msg_id = ntohs((t));                               \
160     rmp->context = mp->context;                                 \
161     rmp->retval = ntohl(rv);                                    \
162     do {body;} while (0);                                       \
163     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
164 } while(0);
165
166 #define REPLY_MACRO3(t, n, body)                                \
167 do {                                                            \
168     unix_shared_memory_queue_t * q;                             \
169     rv = vl_msg_api_pd_handler (mp, rv);                        \
170     q = vl_api_client_index_to_input_queue (mp->client_index);  \
171     if (!q)                                                     \
172         return;                                                 \
173                                                                 \
174     rmp = vl_msg_api_alloc (sizeof (*rmp) + n);                 \
175     rmp->_vl_msg_id = ntohs((t));                               \
176     rmp->context = mp->context;                                 \
177     rmp->retval = ntohl(rv);                                    \
178     do {body;} while (0);                                       \
179     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
180 } while(0);
181
182 #if (1 || CLIB_DEBUG > 0)       /* "trust, but verify" */
183
184 #define VALIDATE_SW_IF_INDEX(mp)                                \
185  do { u32 __sw_if_index = ntohl(mp->sw_if_index);               \
186     vnet_main_t *__vnm = vnet_get_main();                       \
187     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
188                            __sw_if_index)) {                    \
189         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
190         goto bad_sw_if_index;                                   \
191     }                                                           \
192 } while(0);
193
194 #define BAD_SW_IF_INDEX_LABEL                   \
195 do {                                            \
196 bad_sw_if_index:                                \
197     ;                                           \
198 } while (0);
199
200 #define VALIDATE_RX_SW_IF_INDEX(mp)                             \
201  do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index);         \
202     vnet_main_t *__vnm = vnet_get_main();                       \
203     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
204                            __rx_sw_if_index)) {                 \
205         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
206         goto bad_rx_sw_if_index;                                \
207     }                                                           \
208 } while(0);
209
210 #define BAD_RX_SW_IF_INDEX_LABEL                \
211 do {                                            \
212 bad_rx_sw_if_index:                             \
213     ;                                           \
214 } while (0);
215
216 #define VALIDATE_TX_SW_IF_INDEX(mp)                             \
217  do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index);         \
218     vnet_main_t *__vnm = vnet_get_main();                       \
219     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
220                            __tx_sw_if_index)) {                 \
221         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
222         goto bad_tx_sw_if_index;                                \
223     }                                                           \
224 } while(0);
225
226 #define BAD_TX_SW_IF_INDEX_LABEL                \
227 do {                                            \
228 bad_tx_sw_if_index:                             \
229     ;                                           \
230 } while (0);
231
232 #else
233
234 #define VALIDATE_SW_IF_INDEX(mp)
235 #define BAD_SW_IF_INDEX_LABEL
236 #define VALIDATE_RX_SW_IF_INDEX(mp)
237 #define BAD_RX_SW_IF_INDEX_LABEL
238 #define VALIDATE_TX_SW_IF_INDEX(mp)
239 #define BAD_TX_SW_IF_INDEX_LABEL
240
241 #endif /* CLIB_DEBUG > 0 */
242
243 #define foreach_vpe_api_msg                                             \
244 _(WANT_INTERFACE_EVENTS, want_interface_events)                         \
245 _(WANT_OAM_EVENTS, want_oam_events)                                     \
246 _(OAM_ADD_DEL, oam_add_del)                                             \
247 _(SW_INTERFACE_DUMP, sw_interface_dump)                                 \
248 _(SW_INTERFACE_DETAILS, sw_interface_details)                           \
249 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags)                       \
250 _(IP_ADD_DEL_ROUTE, ip_add_del_route)                                   \
251 _(IS_ADDRESS_REACHABLE, is_address_reachable)                           \
252 _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address)           \
253 _(SW_INTERFACE_SET_TABLE, sw_interface_set_table)                       \
254 _(SW_INTERFACE_SET_MPLS_ENABLE, sw_interface_set_mpls_enable)           \
255 _(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath)                       \
256 _(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect)           \
257 _(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge)               \
258 _(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del)                         \
259 _(BRIDGE_DOMAIN_DUMP, bridge_domain_dump)                               \
260 _(BRIDGE_DOMAIN_DETAILS, bridge_domain_details)                         \
261 _(BRIDGE_DOMAIN_SW_IF_DETAILS, bridge_domain_sw_if_details)             \
262 _(L2FIB_ADD_DEL, l2fib_add_del)                                         \
263 _(L2_FLAGS, l2_flags)                                                   \
264 _(BRIDGE_FLAGS, bridge_flags)                                           \
265 _(TAP_CONNECT, tap_connect)                                             \
266 _(TAP_MODIFY, tap_modify)                                               \
267 _(TAP_DELETE, tap_delete)                                               \
268 _(SW_INTERFACE_TAP_DUMP, sw_interface_tap_dump)                         \
269 _(CREATE_VLAN_SUBIF, create_vlan_subif)                                 \
270 _(CREATE_SUBIF, create_subif)                                           \
271 _(MPLS_GRE_ADD_DEL_TUNNEL, mpls_gre_add_del_tunnel)                     \
272 _(MPLS_ETHERNET_ADD_DEL_TUNNEL, mpls_ethernet_add_del_tunnel)           \
273 _(MPLS_ETHERNET_ADD_DEL_TUNNEL_2, mpls_ethernet_add_del_tunnel_2)       \
274 _(MPLS_ADD_DEL_ENCAP, mpls_add_del_encap)                               \
275 _(MPLS_ADD_DEL_DECAP, mpls_add_del_decap)                               \
276 _(PROXY_ARP_ADD_DEL, proxy_arp_add_del)                                 \
277 _(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable)       \
278 _(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del)                             \
279 _(VNET_GET_SUMMARY_STATS, vnet_get_summary_stats)                       \
280 _(RESET_FIB, reset_fib)                                                 \
281 _(DHCP_PROXY_CONFIG,dhcp_proxy_config)                                  \
282 _(DHCP_PROXY_CONFIG_2,dhcp_proxy_config_2)                              \
283 _(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss)                                \
284 _(DHCP_CLIENT_CONFIG, dhcp_client_config)                               \
285 _(SET_IP_FLOW_HASH,set_ip_flow_hash)                                    \
286 _(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config)           \
287 _(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix)           \
288 _(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable )    \
289 _(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS,                              \
290   sw_interface_ip6_set_link_local_address)                              \
291 _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered)             \
292 _(CREATE_LOOPBACK, create_loopback)                                     \
293 _(CONTROL_PING, control_ping)                                           \
294 _(CLI_REQUEST, cli_request)                                             \
295 _(CLI_INBAND, cli_inband)                                               \
296 _(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit)                       \
297 _(L2_PATCH_ADD_DEL, l2_patch_add_del)                                   \
298 _(CLASSIFY_ADD_DEL_TABLE, classify_add_del_table)                       \
299 _(CLASSIFY_ADD_DEL_SESSION, classify_add_del_session)                   \
300 _(CLASSIFY_SET_INTERFACE_IP_TABLE, classify_set_interface_ip_table)     \
301 _(CLASSIFY_SET_INTERFACE_L2_TABLES, classify_set_interface_l2_tables)   \
302 _(GET_NODE_INDEX, get_node_index)                                       \
303 _(ADD_NODE_NEXT, add_node_next)                                         \
304 _(L2TPV3_CREATE_TUNNEL, l2tpv3_create_tunnel)                           \
305 _(L2TPV3_SET_TUNNEL_COOKIES, l2tpv3_set_tunnel_cookies)                 \
306 _(L2TPV3_INTERFACE_ENABLE_DISABLE, l2tpv3_interface_enable_disable)     \
307 _(L2TPV3_SET_LOOKUP_KEY, l2tpv3_set_lookup_key)                         \
308 _(SW_IF_L2TPV3_TUNNEL_DUMP, sw_if_l2tpv3_tunnel_dump)                   \
309 _(VXLAN_ADD_DEL_TUNNEL, vxlan_add_del_tunnel)                           \
310 _(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump)                                 \
311 _(GRE_ADD_DEL_TUNNEL, gre_add_del_tunnel)                               \
312 _(GRE_TUNNEL_DUMP, gre_tunnel_dump)                                     \
313 _(L2_FIB_CLEAR_TABLE, l2_fib_clear_table)                               \
314 _(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter)                     \
315 _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite)         \
316 _(CREATE_VHOST_USER_IF, create_vhost_user_if)                           \
317 _(MODIFY_VHOST_USER_IF, modify_vhost_user_if)                           \
318 _(DELETE_VHOST_USER_IF, delete_vhost_user_if)                           \
319 _(SW_INTERFACE_VHOST_USER_DUMP, sw_interface_vhost_user_dump)           \
320 _(IP_ADDRESS_DUMP, ip_address_dump)                                     \
321 _(IP_DUMP, ip_dump)                                                     \
322 _(SW_INTERFACE_VHOST_USER_DETAILS, sw_interface_vhost_user_details)     \
323 _(SHOW_VERSION, show_version)                                           \
324 _(L2_FIB_TABLE_DUMP, l2_fib_table_dump)                                 \
325 _(L2_FIB_TABLE_ENTRY, l2_fib_table_entry)                               \
326 _(VXLAN_GPE_ADD_DEL_TUNNEL, vxlan_gpe_add_del_tunnel)                   \
327 _(VXLAN_GPE_TUNNEL_DUMP, vxlan_gpe_tunnel_dump)                         \
328 _(INTERFACE_NAME_RENUMBER, interface_name_renumber)                     \
329 _(WANT_IP4_ARP_EVENTS, want_ip4_arp_events)                             \
330 _(WANT_IP6_ND_EVENTS, want_ip6_nd_events)                               \
331 _(INPUT_ACL_SET_INTERFACE, input_acl_set_interface)                     \
332 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del)                                 \
333 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd)             \
334 _(IPSEC_SPD_ADD_DEL_ENTRY, ipsec_spd_add_del_entry)                     \
335 _(IPSEC_SAD_ADD_DEL_ENTRY, ipsec_sad_add_del_entry)                     \
336 _(IPSEC_SA_SET_KEY, ipsec_sa_set_key)                                   \
337 _(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del)                         \
338 _(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth)                       \
339 _(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id)                           \
340 _(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts)                           \
341 _(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key)                             \
342 _(DELETE_LOOPBACK, delete_loopback)                                     \
343 _(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del)                                 \
344 _(MAP_ADD_DOMAIN, map_add_domain)                                       \
345 _(MAP_DEL_DOMAIN, map_del_domain)                                       \
346 _(MAP_ADD_DEL_RULE, map_add_del_rule)                                   \
347 _(MAP_DOMAIN_DUMP, map_domain_dump)                                     \
348 _(MAP_RULE_DUMP, map_rule_dump)                                         \
349 _(MAP_SUMMARY_STATS, map_summary_stats)                                 \
350 _(COP_INTERFACE_ENABLE_DISABLE, cop_interface_enable_disable)           \
351 _(COP_WHITELIST_ENABLE_DISABLE, cop_whitelist_enable_disable)           \
352 _(GET_NODE_GRAPH, get_node_graph)                                       \
353 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats)                   \
354 _(IOAM_ENABLE, ioam_enable)                                 \
355 _(IOAM_DISABLE, ioam_disable)                                 \
356 _(LISP_ADD_DEL_LOCATOR_SET, lisp_add_del_locator_set)                   \
357 _(LISP_ADD_DEL_LOCATOR, lisp_add_del_locator)                           \
358 _(LISP_ADD_DEL_LOCAL_EID, lisp_add_del_local_eid)                       \
359 _(LISP_GPE_ADD_DEL_FWD_ENTRY, lisp_gpe_add_del_fwd_entry)               \
360 _(LISP_ADD_DEL_MAP_RESOLVER, lisp_add_del_map_resolver)                 \
361 _(LISP_GPE_ENABLE_DISABLE, lisp_gpe_enable_disable)                     \
362 _(LISP_ENABLE_DISABLE, lisp_enable_disable)                             \
363 _(LISP_GPE_ADD_DEL_IFACE, lisp_gpe_add_del_iface)                       \
364 _(LISP_ADD_DEL_REMOTE_MAPPING, lisp_add_del_remote_mapping)             \
365 _(LISP_ADD_DEL_ADJACENCY, lisp_add_del_adjacency)                       \
366 _(LISP_PITR_SET_LOCATOR_SET, lisp_pitr_set_locator_set)                 \
367 _(LISP_MAP_REQUEST_MODE, lisp_map_request_mode)                         \
368 _(LISP_EID_TABLE_ADD_DEL_MAP, lisp_eid_table_add_del_map)               \
369 _(LISP_LOCATOR_SET_DUMP, lisp_locator_set_dump)                         \
370 _(LISP_LOCATOR_DUMP, lisp_locator_dump)                                 \
371 _(LISP_EID_TABLE_DUMP, lisp_eid_table_dump)                             \
372 _(LISP_GPE_TUNNEL_DUMP, lisp_gpe_tunnel_dump)                           \
373 _(LISP_MAP_RESOLVER_DUMP, lisp_map_resolver_dump)                       \
374 _(LISP_EID_TABLE_MAP_DUMP, lisp_eid_table_map_dump)                     \
375 _(LISP_EID_TABLE_VNI_DUMP, lisp_eid_table_vni_dump)                     \
376 _(SHOW_LISP_STATUS, show_lisp_status)                                   \
377 _(LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS,                                   \
378   lisp_add_del_map_request_itr_rlocs)                                   \
379 _(LISP_GET_MAP_REQUEST_ITR_RLOCS, lisp_get_map_request_itr_rlocs)       \
380 _(SHOW_LISP_PITR, show_lisp_pitr)                                       \
381 _(SHOW_LISP_MAP_REQUEST_MODE, show_lisp_map_request_mode)               \
382 _(SR_MULTICAST_MAP_ADD_DEL, sr_multicast_map_add_del)                   \
383 _(AF_PACKET_CREATE, af_packet_create)                                   \
384 _(AF_PACKET_DELETE, af_packet_delete)                                   \
385 _(POLICER_ADD_DEL, policer_add_del)                                     \
386 _(POLICER_DUMP, policer_dump)                                           \
387 _(POLICER_CLASSIFY_SET_INTERFACE, policer_classify_set_interface)       \
388 _(POLICER_CLASSIFY_DUMP, policer_classify_dump)                         \
389 _(NETMAP_CREATE, netmap_create)                                         \
390 _(NETMAP_DELETE, netmap_delete)                                         \
391 _(MPLS_GRE_TUNNEL_DUMP, mpls_gre_tunnel_dump)                           \
392 _(MPLS_GRE_TUNNEL_DETAILS, mpls_gre_tunnel_details)                     \
393 _(MPLS_ETH_TUNNEL_DUMP, mpls_eth_tunnel_dump)                           \
394 _(MPLS_ETH_TUNNEL_DETAILS, mpls_eth_tunnel_details)                     \
395 _(MPLS_FIB_ENCAP_DUMP, mpls_fib_encap_dump)                             \
396 _(MPLS_FIB_ENCAP_DETAILS, mpls_fib_encap_details)                       \
397 _(MPLS_FIB_DECAP_DUMP, mpls_fib_decap_dump)                             \
398 _(MPLS_FIB_DECAP_DETAILS, mpls_fib_decap_details)                       \
399 _(CLASSIFY_TABLE_IDS,classify_table_ids)                                \
400 _(CLASSIFY_TABLE_BY_INTERFACE, classify_table_by_interface)             \
401 _(CLASSIFY_TABLE_INFO,classify_table_info)                              \
402 _(CLASSIFY_SESSION_DUMP,classify_session_dump)                          \
403 _(CLASSIFY_SESSION_DETAILS,classify_session_details)                    \
404 _(SET_IPFIX_EXPORTER, set_ipfix_exporter)                               \
405 _(IPFIX_EXPORTER_DUMP, ipfix_exporter_dump)                             \
406 _(SET_IPFIX_CLASSIFY_STREAM, set_ipfix_classify_stream)                 \
407 _(IPFIX_CLASSIFY_STREAM_DUMP, ipfix_classify_stream_dump)               \
408 _(IPFIX_CLASSIFY_TABLE_ADD_DEL, ipfix_classify_table_add_del)           \
409 _(IPFIX_CLASSIFY_TABLE_DUMP, ipfix_classify_table_dump)                 \
410 _(GET_NEXT_INDEX, get_next_index)                                       \
411 _(PG_CREATE_INTERFACE, pg_create_interface)                             \
412 _(PG_CAPTURE, pg_capture)                                               \
413 _(PG_ENABLE_DISABLE, pg_enable_disable)                                 \
414 _(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL,                               \
415   ip_source_and_port_range_check_add_del)                               \
416 _(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL,                     \
417   ip_source_and_port_range_check_interface_add_del)                     \
418 _(IPSEC_GRE_ADD_DEL_TUNNEL, ipsec_gre_add_del_tunnel)                   \
419 _(IPSEC_GRE_TUNNEL_DUMP, ipsec_gre_tunnel_dump)                         \
420 _(DELETE_SUBIF, delete_subif)
421
422 #define QUOTE_(x) #x
423 #define QUOTE(x) QUOTE_(x)
424
425 #define foreach_registration_hash               \
426 _(interface_events)                             \
427 _(to_netconf_server)                            \
428 _(from_netconf_server)                          \
429 _(to_netconf_client)                            \
430 _(from_netconf_client)                          \
431 _(oam_events)
432
433 typedef enum
434 {
435   RESOLVE_IP4_ADD_DEL_ROUTE = 1,
436   RESOLVE_IP6_ADD_DEL_ROUTE,
437   RESOLVE_MPLS_ETHERNET_ADD_DEL,
438 } resolve_t;
439
440 typedef struct
441 {
442   u8 resolve_type;
443   union
444   {
445     vl_api_ip_add_del_route_t r;
446     vl_api_mpls_ethernet_add_del_tunnel_2_t t;
447   };
448 } pending_route_t;
449
450 typedef struct
451 {
452
453 #define _(a) uword *a##_registration_hash;              \
454     vpe_client_registration_t * a##_registrations;
455   foreach_registration_hash
456 #undef _
457     /* notifications happen really early in the game */
458   u8 link_state_process_up;
459
460   /* ip4 and ip6 pending route adds */
461   pending_route_t *pending_routes;
462
463   /* ip4 arp event registration pool */
464   vl_api_ip4_arp_event_t *arp_events;
465
466   /* ip6 nd event registration pool */
467   vl_api_ip6_nd_event_t *nd_events;
468
469   /* convenience */
470   vlib_main_t *vlib_main;
471   vnet_main_t *vnet_main;
472 } vpe_api_main_t;
473
474 static vlib_node_registration_t vpe_resolver_process_node;
475 static vpe_api_main_t vpe_api_main;
476
477 static void send_sw_interface_flags (vpe_api_main_t * am,
478                                      unix_shared_memory_queue_t * q,
479                                      vnet_sw_interface_t * swif);
480 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
481                                              unix_shared_memory_queue_t * q,
482                                              u32 sw_if_index);
483
484 static int arp_change_delete_callback (u32 pool_index, u8 * notused);
485 static int nd_change_delete_callback (u32 pool_index, u8 * notused);
486
487
488 /* Clean up all registrations belonging to the indicated client */
489 int
490 vl_api_memclnt_delete_callback (u32 client_index)
491 {
492   vpe_api_main_t *vam = &vpe_api_main;
493   vpe_client_registration_t *rp;
494   uword *p;
495   int stats_memclnt_delete_callback (u32 client_index);
496
497   stats_memclnt_delete_callback (client_index);
498
499 #define _(a)                                                    \
500     p = hash_get (vam->a##_registration_hash, client_index);    \
501     if (p) {                                                    \
502         rp = pool_elt_at_index (vam->a##_registrations, p[0]);  \
503         pool_put (vam->a##_registrations, rp);                  \
504         hash_unset (vam->a##_registration_hash, client_index);  \
505     }
506   foreach_registration_hash;
507 #undef _
508   return 0;
509 }
510
511 #define API_LINK_STATE_EVENT 1
512 #define API_ADMIN_UP_DOWN_EVENT 2
513
514 static int
515 event_data_cmp (void *a1, void *a2)
516 {
517   uword *e1 = a1;
518   uword *e2 = a2;
519
520   return (word) e1[0] - (word) e2[0];
521 }
522
523 static uword
524 link_state_process (vlib_main_t * vm,
525                     vlib_node_runtime_t * rt, vlib_frame_t * f)
526 {
527   vpe_api_main_t *vam = &vpe_api_main;
528   vnet_main_t *vnm = vam->vnet_main;
529   vnet_sw_interface_t *swif;
530   uword *event_data = 0;
531   vpe_client_registration_t *reg;
532   int i;
533   u32 prev_sw_if_index;
534   unix_shared_memory_queue_t *q;
535
536   vam->link_state_process_up = 1;
537
538   while (1)
539     {
540       vlib_process_wait_for_event (vm);
541
542       /* Unified list of changed link or admin state sw_if_indices */
543       vlib_process_get_events_with_type
544         (vm, &event_data, API_LINK_STATE_EVENT);
545       vlib_process_get_events_with_type
546         (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
547
548       /* Sort, so we can eliminate duplicates */
549       vec_sort_with_function (event_data, event_data_cmp);
550
551       prev_sw_if_index = ~0;
552
553       for (i = 0; i < vec_len (event_data); i++)
554         {
555           /* Only one message per swif */
556           if (prev_sw_if_index == event_data[i])
557             continue;
558           prev_sw_if_index = event_data[i];
559
560           /* *INDENT-OFF* */
561           pool_foreach(reg, vam->interface_events_registrations,
562           ({
563             q = vl_api_client_index_to_input_queue (reg->client_index);
564             if (q)
565               {
566                 /* sw_interface may be deleted already */
567                 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
568                                          event_data[i]))
569                   {
570                     swif = vnet_get_sw_interface (vnm, event_data[i]);
571                     send_sw_interface_flags (vam, q, swif);
572                   }
573               }
574           }));
575           /* *INDENT-ON* */
576         }
577       vec_reset_length (event_data);
578     }
579
580   return 0;
581 }
582
583 static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
584                                             u32 flags);
585 static clib_error_t *admin_up_down_function (vnet_main_t * vm,
586                                              u32 hw_if_index, u32 flags);
587
588 /* *INDENT-OFF* */
589 VLIB_REGISTER_NODE (link_state_process_node,static) = {
590   .function = link_state_process,
591   .type = VLIB_NODE_TYPE_PROCESS,
592   .name = "vpe-link-state-process",
593 };
594 /* *INDENT-ON* */
595
596 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
597 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
598
599 static clib_error_t *
600 link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
601 {
602   vpe_api_main_t *vam = &vpe_api_main;
603   vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
604
605   if (vam->link_state_process_up)
606     vlib_process_signal_event (vam->vlib_main,
607                                link_state_process_node.index,
608                                API_LINK_STATE_EVENT, hi->sw_if_index);
609   return 0;
610 }
611
612 static clib_error_t *
613 admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
614 {
615   vpe_api_main_t *vam = &vpe_api_main;
616
617   /*
618    * Note: it's perfectly fair to set a subif admin up / admin down.
619    * Note the subtle distinction between this routine and the previous
620    * routine.
621    */
622   if (vam->link_state_process_up)
623     vlib_process_signal_event (vam->vlib_main,
624                                link_state_process_node.index,
625                                API_ADMIN_UP_DOWN_EVENT, sw_if_index);
626   return 0;
627 }
628
629 #define pub_sub_handler(lca,UCA)                                        \
630 static void vl_api_want_##lca##_t_handler (                             \
631     vl_api_want_##lca##_t *mp)                                          \
632 {                                                                       \
633     vpe_api_main_t *vam = &vpe_api_main;                                \
634     vpe_client_registration_t *rp;                                      \
635     vl_api_want_##lca##_reply_t *rmp;                                   \
636     uword *p;                                                           \
637     i32 rv = 0;                                                         \
638                                                                         \
639     p = hash_get (vam->lca##_registration_hash, mp->client_index);      \
640     if (p) {                                                            \
641         if (mp->enable_disable) {                                       \
642             clib_warning ("pid %d: already enabled...", mp->pid);       \
643             rv = VNET_API_ERROR_INVALID_REGISTRATION;                   \
644             goto reply;                                                 \
645         } else {                                                        \
646             rp = pool_elt_at_index (vam->lca##_registrations, p[0]);    \
647             pool_put (vam->lca##_registrations, rp);                    \
648             hash_unset (vam->lca##_registration_hash,                   \
649                 mp->client_index);                                      \
650             goto reply;                                                 \
651         }                                                               \
652     }                                                                   \
653     if (mp->enable_disable == 0) {                                      \
654         clib_warning ("pid %d: already disabled...", mp->pid);          \
655         rv = VNET_API_ERROR_INVALID_REGISTRATION;                       \
656         goto reply;                                                     \
657     }                                                                   \
658     pool_get (vam->lca##_registrations, rp);                            \
659     rp->client_index = mp->client_index;                                \
660     rp->client_pid = mp->pid;                                           \
661     hash_set (vam->lca##_registration_hash, rp->client_index,           \
662               rp - vam->lca##_registrations);                           \
663                                                                         \
664 reply:                                                                  \
665     REPLY_MACRO (VL_API_WANT_##UCA##_REPLY);                            \
666 }
667
668 /* *INDENT-OFF* */
669 pub_sub_handler (interface_events, INTERFACE_EVENTS)
670 pub_sub_handler (oam_events, OAM_EVENTS)
671 /* *INDENT-ON* */
672
673 #define RESOLUTION_EVENT 1
674 #define RESOLUTION_PENDING_EVENT 2
675 #define IP4_ARP_EVENT 3
676 #define IP6_ND_EVENT 4
677
678 static int ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp);
679
680 static int ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp);
681
682 static int mpls_ethernet_add_del_tunnel_2_t_handler
683   (vl_api_mpls_ethernet_add_del_tunnel_2_t * mp);
684
685 void
686 handle_ip4_arp_event (u32 pool_index)
687 {
688   vpe_api_main_t *vam = &vpe_api_main;
689   vnet_main_t *vnm = vam->vnet_main;
690   vlib_main_t *vm = vam->vlib_main;
691   vl_api_ip4_arp_event_t *event;
692   vl_api_ip4_arp_event_t *mp;
693   unix_shared_memory_queue_t *q;
694
695   /* Client can cancel, die, etc. */
696   if (pool_is_free_index (vam->arp_events, pool_index))
697     return;
698
699   event = pool_elt_at_index (vam->arp_events, pool_index);
700
701   q = vl_api_client_index_to_input_queue (event->client_index);
702   if (!q)
703     {
704       (void) vnet_add_del_ip4_arp_change_event
705         (vnm, arp_change_delete_callback,
706          event->pid, &event->address,
707          vpe_resolver_process_node.index, IP4_ARP_EVENT,
708          ~0 /* pool index, notused */ , 0 /* is_add */ );
709       return;
710     }
711
712   if (q->cursize < q->maxsize)
713     {
714       mp = vl_msg_api_alloc (sizeof (*mp));
715       clib_memcpy (mp, event, sizeof (*mp));
716       vl_msg_api_send_shmem (q, (u8 *) & mp);
717     }
718   else
719     {
720       static f64 last_time;
721       /*
722        * Throttle syslog msgs.
723        * It's pretty tempting to just revoke the registration...
724        */
725       if (vlib_time_now (vm) > last_time + 10.0)
726         {
727           clib_warning ("arp event for %U to pid %d: queue stuffed!",
728                         format_ip4_address, &event->address, event->pid);
729           last_time = vlib_time_now (vm);
730         }
731     }
732 }
733
734 void
735 handle_ip6_nd_event (u32 pool_index)
736 {
737   vpe_api_main_t *vam = &vpe_api_main;
738   vnet_main_t *vnm = vam->vnet_main;
739   vlib_main_t *vm = vam->vlib_main;
740   vl_api_ip6_nd_event_t *event;
741   vl_api_ip6_nd_event_t *mp;
742   unix_shared_memory_queue_t *q;
743
744   /* Client can cancel, die, etc. */
745   if (pool_is_free_index (vam->nd_events, pool_index))
746     return;
747
748   event = pool_elt_at_index (vam->nd_events, pool_index);
749
750   q = vl_api_client_index_to_input_queue (event->client_index);
751   if (!q)
752     {
753       (void) vnet_add_del_ip6_nd_change_event
754         (vnm, nd_change_delete_callback,
755          event->pid, &event->address,
756          vpe_resolver_process_node.index, IP6_ND_EVENT,
757          ~0 /* pool index, notused */ , 0 /* is_add */ );
758       return;
759     }
760
761   if (q->cursize < q->maxsize)
762     {
763       mp = vl_msg_api_alloc (sizeof (*mp));
764       clib_memcpy (mp, event, sizeof (*mp));
765       vl_msg_api_send_shmem (q, (u8 *) & mp);
766     }
767   else
768     {
769       static f64 last_time;
770       /*
771        * Throttle syslog msgs.
772        * It's pretty tempting to just revoke the registration...
773        */
774       if (vlib_time_now (vm) > last_time + 10.0)
775         {
776           clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
777                         format_ip6_address, &event->address, event->pid);
778           last_time = vlib_time_now (vm);
779         }
780     }
781 }
782
783 static uword
784 resolver_process (vlib_main_t * vm,
785                   vlib_node_runtime_t * rt, vlib_frame_t * f)
786 {
787   uword event_type;
788   uword *event_data = 0;
789   f64 timeout = 100.0;
790   vpe_api_main_t *vam = &vpe_api_main;
791   pending_route_t *pr;
792   vl_api_ip_add_del_route_t *adr;
793   vl_api_mpls_ethernet_add_del_tunnel_2_t *pme;
794   u32 *resolution_failures = 0;
795   int i, rv;
796   clib_error_t *e;
797
798   while (1)
799     {
800       vlib_process_wait_for_event_or_clock (vm, timeout);
801
802       event_type = vlib_process_get_events (vm, &event_data);
803
804       switch (event_type)
805         {
806         case RESOLUTION_PENDING_EVENT:
807           timeout = 1.0;
808           break;
809
810         case RESOLUTION_EVENT:
811           for (i = 0; i < vec_len (event_data); i++)
812             {
813               /*
814                * Resolution events can occur long after the
815                * original request has timed out. $$$ add a cancel
816                * mechanism..
817                */
818               if (pool_is_free_index (vam->pending_routes, event_data[i]))
819                 continue;
820
821               pr = pool_elt_at_index (vam->pending_routes, event_data[i]);
822               adr = &pr->r;
823               pme = &pr->t;
824
825               switch (pr->resolve_type)
826                 {
827                 case RESOLVE_IP4_ADD_DEL_ROUTE:
828                   rv = ip4_add_del_route_t_handler (adr);
829                   clib_warning ("resolver: add %U/%d via %U %s",
830                                 format_ip4_address,
831                                 (ip4_address_t *) & (adr->dst_address),
832                                 adr->dst_address_length,
833                                 format_ip4_address,
834                                 (ip4_address_t *) & (adr->next_hop_address),
835                                 (rv >= 0) ? "succeeded" : "failed");
836                   break;
837
838                 case RESOLVE_IP6_ADD_DEL_ROUTE:
839                   rv = ip6_add_del_route_t_handler (adr);
840                   clib_warning ("resolver: add %U/%d via %U %s",
841                                 format_ip6_address,
842                                 (ip6_address_t *) & (adr->dst_address),
843                                 adr->dst_address_length,
844                                 format_ip6_address,
845                                 (ip6_address_t *) & (adr->next_hop_address),
846                                 (rv >= 0) ? "succeeded" : "failed");
847                   break;
848
849                 case RESOLVE_MPLS_ETHERNET_ADD_DEL:
850                   rv = mpls_ethernet_add_del_tunnel_2_t_handler (pme);
851                   clib_warning ("resolver: add mpls-o-e via %U %s",
852                                 format_ip4_address,
853                                 (ip4_address_t *) &
854                                 (pme->next_hop_ip4_address_in_outer_vrf),
855                                 (rv >= 0) ? "succeeded" : "failed");
856                   break;
857
858                 default:
859                   clib_warning ("resolver: BOGUS TYPE %d", pr->resolve_type);
860                 }
861               pool_put (vam->pending_routes, pr);
862             }
863           break;
864
865         case IP4_ARP_EVENT:
866           for (i = 0; i < vec_len (event_data); i++)
867             handle_ip4_arp_event (event_data[i]);
868           break;
869
870         case IP6_ND_EVENT:
871           for (i = 0; i < vec_len (event_data); i++)
872             handle_ip6_nd_event (event_data[i]);
873           break;
874
875         case ~0:                /* timeout, retry pending resolutions */
876           /* *INDENT-OFF* */
877           pool_foreach (pr, vam->pending_routes,
878           ({
879             int is_adr = 1;
880             adr = &pr->r;
881             pme = &pr->t;
882
883             /* May fail, e.g. due to interface down */
884             switch (pr->resolve_type)
885               {
886               case RESOLVE_IP4_ADD_DEL_ROUTE:
887                 e = ip4_probe_neighbor
888                   (vm, (ip4_address_t *)&(adr->next_hop_address),
889                    ntohl(adr->next_hop_sw_if_index));
890                 break;
891
892               case RESOLVE_IP6_ADD_DEL_ROUTE:
893                 e = ip6_probe_neighbor
894                   (vm, (ip6_address_t *)&(adr->next_hop_address),
895                    ntohl(adr->next_hop_sw_if_index));
896                 break;
897
898               case RESOLVE_MPLS_ETHERNET_ADD_DEL:
899                 is_adr = 0;
900                 e = ip4_probe_neighbor
901                   (vm,
902                    (ip4_address_t *)&(pme->next_hop_ip4_address_in_outer_vrf),
903                    pme->resolve_opaque);
904                 break;
905
906               default:
907                 e = clib_error_return (0, "resolver: BOGUS TYPE %d",
908                                        pr->resolve_type);
909               }
910             if (e)
911               {
912                 clib_error_report (e);
913                 if (is_adr)
914                   adr->resolve_attempts = 1;
915                 else
916                   pme->resolve_attempts = 1;
917               }
918             if (is_adr)
919               {
920                 adr->resolve_attempts -= 1;
921                 if (adr->resolve_attempts == 0)
922                   vec_add1 (resolution_failures,
923                             pr - vam->pending_routes);
924               }
925             else
926               {
927                 pme->resolve_attempts -= 1;
928                 if (pme->resolve_attempts == 0)
929                   vec_add1 (resolution_failures,
930                             pr - vam->pending_routes);
931               }
932           }));
933           /* *INDENT-ON* */
934           for (i = 0; i < vec_len (resolution_failures); i++)
935             {
936               pr = pool_elt_at_index (vam->pending_routes,
937                                       resolution_failures[i]);
938               adr = &pr->r;
939               pme = &pr->t;
940
941               switch (pr->resolve_type)
942                 {
943                 case RESOLVE_IP4_ADD_DEL_ROUTE:
944                   clib_warning ("resolver: add %U/%d via %U retry failure",
945                                 format_ip4_address,
946                                 (ip4_address_t *) & (adr->dst_address),
947                                 adr->dst_address_length,
948                                 format_ip4_address,
949                                 (ip4_address_t *) & (adr->next_hop_address));
950                   break;
951
952                 case RESOLVE_IP6_ADD_DEL_ROUTE:
953                   clib_warning ("resolver: add %U/%d via %U retry failure",
954                                 format_ip6_address,
955                                 (ip6_address_t *) & (adr->dst_address),
956                                 adr->dst_address_length,
957                                 format_ip6_address,
958                                 (ip6_address_t *) & (adr->next_hop_address));
959                   break;
960
961                 case RESOLVE_MPLS_ETHERNET_ADD_DEL:
962                   clib_warning ("resolver: add mpls-o-e via %U retry failure",
963                                 format_ip4_address,
964                                 (ip4_address_t *) &
965                                 (pme->next_hop_ip4_address_in_outer_vrf));
966                   break;
967
968                 default:
969                   clib_warning ("BUG");
970                 }
971               pool_put (vam->pending_routes, pr);
972             }
973           vec_reset_length (resolution_failures);
974           break;
975         }
976       if (pool_elts (vam->pending_routes) == 0)
977         timeout = 100.0;
978       vec_reset_length (event_data);
979     }
980   return 0;                     /* or not */
981 }
982
983 /* *INDENT-OFF* */
984 VLIB_REGISTER_NODE (vpe_resolver_process_node,static) = {
985   .function = resolver_process,
986   .type = VLIB_NODE_TYPE_PROCESS,
987   .name = "vpe-route-resolver-process",
988 };
989 /* *INDENT-ON* */
990
991 static int
992 ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp,
993                             u32 fib_index,
994                             const fib_prefix_t * prefix,
995                             const ip46_address_t * next_hop,
996                             u32 next_hop_sw_if_index,
997                             u32 next_hop_fib_index, u32 next_hop_weight)
998 {
999   vnet_classify_main_t *cm = &vnet_classify_main;
1000   fib_protocol_t proto = prefix->fp_proto;
1001   stats_main_t *sm = &stats_main;
1002
1003   if (mp->is_multipath)
1004     {
1005       fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
1006
1007       dslock (sm, 1 /* release hint */ , 10 /* tag */ );
1008
1009       if (mp->is_resolve_host)
1010         path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
1011       if (mp->is_resolve_attached)
1012         path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
1013
1014       if (mp->is_add)
1015         fib_table_entry_path_add (fib_index,
1016                                   prefix,
1017                                   FIB_SOURCE_API,
1018                                   FIB_ENTRY_FLAG_NONE,
1019                                   prefix->fp_proto,
1020                                   next_hop,
1021                                   next_hop_sw_if_index,
1022                                   next_hop_fib_index,
1023                                   next_hop_weight,
1024                                   MPLS_LABEL_INVALID, path_flags);
1025       else
1026         fib_table_entry_path_remove (fib_index,
1027                                      prefix,
1028                                      FIB_SOURCE_API,
1029                                      prefix->fp_proto,
1030                                      next_hop,
1031                                      next_hop_sw_if_index,
1032                                      next_hop_fib_index,
1033                                      next_hop_weight, path_flags);
1034
1035       dsunlock (sm);
1036       return 0;
1037     }
1038
1039   dslock (sm, 1 /* release hint */ , 2 /* tag */ );
1040
1041   if (mp->is_drop || mp->is_local || mp->is_classify || mp->lookup_in_vrf)
1042     {
1043       /*
1044        * special route types that link directly to the adj
1045        */
1046       if (mp->is_add)
1047         {
1048           dpo_id_t dpo = DPO_NULL;
1049           dpo_proto_t dproto;
1050
1051           dproto = fib_proto_to_dpo (prefix->fp_proto);
1052
1053           if (mp->is_drop)
1054             dpo_copy (&dpo, drop_dpo_get (dproto));
1055           else if (mp->is_local)
1056             receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
1057           else if (mp->is_classify)
1058             {
1059               if (pool_is_free_index (cm->tables,
1060                                       ntohl (mp->classify_table_index)))
1061                 {
1062                   dsunlock (sm);
1063                   return VNET_API_ERROR_NO_SUCH_TABLE;
1064                 }
1065
1066               dpo_set (&dpo, DPO_CLASSIFY, proto,
1067                        classify_dpo_create (prefix->fp_proto,
1068                                             ntohl
1069                                             (mp->classify_table_index)));
1070             }
1071           else if (mp->lookup_in_vrf)
1072             {
1073               next_hop_fib_index =
1074                 fib_table_id_find_fib_index (dproto,
1075                                              ntohl (mp->lookup_in_vrf));
1076               if (~0 == next_hop_fib_index)
1077                 {
1078                   dsunlock (sm);
1079                   return VNET_API_ERROR_NO_SUCH_INNER_FIB;
1080                 }
1081
1082               lookup_dpo_add_or_lock_w_fib_index (next_hop_fib_index,
1083                                                   dproto,
1084                                                   LOOKUP_INPUT_DST_ADDR,
1085                                                   LOOKUP_TABLE_FROM_CONFIG,
1086                                                   &dpo);
1087             }
1088           else
1089             {
1090               dsunlock (sm);
1091               return VNET_API_ERROR_NO_SUCH_TABLE;
1092             }
1093
1094           fib_table_entry_special_dpo_add (fib_index,
1095                                            prefix,
1096                                            FIB_SOURCE_API,
1097                                            FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1098           dpo_reset (&dpo);
1099         }
1100       else
1101         {
1102           fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
1103         }
1104     }
1105   else
1106     {
1107       if (mp->is_add)
1108         {
1109           fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
1110
1111           if (mp->is_resolve_host)
1112             path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
1113           if (mp->is_resolve_attached)
1114             path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
1115
1116           fib_table_entry_update_one_path (fib_index,
1117                                            prefix,
1118                                            FIB_SOURCE_API,
1119                                            FIB_ENTRY_FLAG_NONE,
1120                                            prefix->fp_proto,
1121                                            next_hop,
1122                                            next_hop_sw_if_index,
1123                                            next_hop_fib_index,
1124                                            next_hop_weight,
1125                                            MPLS_LABEL_INVALID, path_flags);
1126         }
1127       else
1128         {
1129           fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
1130         }
1131     }
1132
1133   dsunlock (sm);
1134   return (0);
1135 }
1136
1137 static int
1138 ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1139 {
1140   vpe_api_main_t *vam = &vpe_api_main;
1141   vnet_main_t *vnm = vam->vnet_main;
1142   u32 fib_index;
1143
1144   fib_index = ip4_fib_index_from_table_id (ntohl (mp->vrf_id));
1145   if (~0 == fib_index)
1146     {
1147       if (mp->create_vrf_if_needed)
1148         {
1149           fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1150                                                          ntohl (mp->vrf_id));
1151         }
1152       else
1153         {
1154           /* No such VRF, and we weren't asked to create one */
1155           return VNET_API_ERROR_NO_SUCH_FIB;
1156         }
1157     }
1158
1159   if (~0 != ntohl (mp->next_hop_sw_if_index) &&
1160       pool_is_free_index (vnm->interface_main.sw_interfaces,
1161                           ntohl (mp->next_hop_sw_if_index)))
1162     return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1163
1164   fib_prefix_t pfx = {
1165     .fp_len = mp->dst_address_length,
1166     .fp_proto = FIB_PROTOCOL_IP4,
1167   };
1168   clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
1169
1170   ip46_address_t nh;
1171   memset (&nh, 0, sizeof (nh));
1172   memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
1173
1174   return (ip_add_del_route_t_handler (mp, fib_index, &pfx, &nh,
1175                                       ntohl (mp->next_hop_sw_if_index),
1176                                       fib_index, (u32) mp->next_hop_weight));
1177 }
1178
1179 static int
1180 ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1181 {
1182   vnet_main_t *vnm = vnet_get_main ();
1183   u32 fib_index;
1184
1185   fib_index = ip6_fib_index_from_table_id (ntohl (mp->vrf_id));
1186   if (~0 == fib_index)
1187     {
1188       if (mp->create_vrf_if_needed)
1189         {
1190           fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
1191                                                          ntohl (mp->vrf_id));
1192         }
1193       else
1194         {
1195           /* No such VRF, and we weren't asked to create one */
1196           return VNET_API_ERROR_NO_SUCH_FIB;
1197         }
1198     }
1199
1200   if (~0 != ntohl (mp->next_hop_sw_if_index) &&
1201       pool_is_free_index (vnm->interface_main.sw_interfaces,
1202                           ntohl (mp->next_hop_sw_if_index)))
1203     return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1204
1205   fib_prefix_t pfx = {
1206     .fp_len = mp->dst_address_length,
1207     .fp_proto = FIB_PROTOCOL_IP6,
1208   };
1209   clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1210
1211   ip46_address_t nh;
1212   memset (&nh, 0, sizeof (nh));
1213   memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1214
1215   return (ip_add_del_route_t_handler (mp, fib_index, &pfx,
1216                                       &nh, ntohl (mp->next_hop_sw_if_index),
1217                                       fib_index, (u32) mp->next_hop_weight));
1218 }
1219
1220 void
1221 vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1222 {
1223   vl_api_ip_add_del_route_reply_t *rmp;
1224   int rv;
1225   vnet_main_t *vnm = vnet_get_main ();
1226
1227   vnm->api_errno = 0;
1228
1229   if (mp->is_ipv6)
1230     rv = ip6_add_del_route_t_handler (mp);
1231   else
1232     rv = ip4_add_del_route_t_handler (mp);
1233
1234   rv = (rv == 0) ? vnm->api_errno : rv;
1235
1236   REPLY_MACRO (VL_API_IP_ADD_DEL_ROUTE_REPLY);
1237 }
1238
1239 static void
1240   vl_api_sw_interface_add_del_address_t_handler
1241   (vl_api_sw_interface_add_del_address_t * mp)
1242 {
1243   vlib_main_t *vm = vlib_get_main ();
1244   vl_api_sw_interface_add_del_address_reply_t *rmp;
1245   int rv = 0;
1246   u32 is_del;
1247
1248   VALIDATE_SW_IF_INDEX (mp);
1249
1250   is_del = mp->is_add == 0;
1251
1252   if (mp->del_all)
1253     ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
1254   else if (mp->is_ipv6)
1255     ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
1256                                    (void *) mp->address,
1257                                    mp->address_length, is_del);
1258   else
1259     ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
1260                                    (void *) mp->address,
1261                                    mp->address_length, is_del);
1262
1263   BAD_SW_IF_INDEX_LABEL;
1264
1265   REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
1266 }
1267
1268 static void
1269 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
1270 {
1271   int rv = 0;
1272   u32 table_id = ntohl (mp->vrf_id);
1273   u32 sw_if_index = ntohl (mp->sw_if_index);
1274   vl_api_sw_interface_set_table_reply_t *rmp;
1275   stats_main_t *sm = &stats_main;
1276   u32 fib_index;
1277
1278   VALIDATE_SW_IF_INDEX (mp);
1279
1280   dslock (sm, 1 /* release hint */ , 4 /* tag */ );
1281
1282   if (mp->is_ipv6)
1283     {
1284       fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
1285                                                      table_id);
1286
1287       vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
1288       ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
1289     }
1290   else
1291     {
1292
1293       fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1294                                                      table_id);
1295
1296       vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
1297       ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
1298     }
1299   dsunlock (sm);
1300
1301   BAD_SW_IF_INDEX_LABEL;
1302
1303   REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
1304 }
1305
1306 static void
1307 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
1308 {
1309   vlib_main_t *vm = vlib_get_main ();
1310   ip4_main_t *im4 = &ip4_main;
1311   ip6_main_t *im6 = &ip6_main;
1312   vl_api_sw_interface_set_vpath_reply_t *rmp;
1313   int rv = 0;
1314   u32 ci;
1315   u32 sw_if_index = ntohl (mp->sw_if_index);
1316   ip4_main_t *ip4m = &ip4_main;
1317   ip6_main_t *ip6m = &ip6_main;
1318   ip_lookup_main_t *ip4lm = &ip4m->lookup_main;
1319   ip_lookup_main_t *ip6lm = &ip6m->lookup_main;
1320   ip_config_main_t *rx_cm4u =
1321     &ip4lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
1322   ip_config_main_t *rx_cm4m =
1323     &ip4lm->feature_config_mains[VNET_IP_RX_MULTICAST_FEAT];
1324   ip_config_main_t *rx_cm6u =
1325     &ip6lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
1326   ip_config_main_t *rx_cm6m =
1327     &ip6lm->feature_config_mains[VNET_IP_RX_MULTICAST_FEAT];
1328
1329   VALIDATE_SW_IF_INDEX (mp);
1330
1331   l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
1332   if (mp->enable)
1333     {
1334       ci = rx_cm4u->config_index_by_sw_if_index[sw_if_index];   //IP4 unicast
1335       ci = vnet_config_add_feature (vm, &rx_cm4u->config_main,
1336                                     ci,
1337                                     im4->ip4_unicast_rx_feature_vpath, 0, 0);
1338       rx_cm4u->config_index_by_sw_if_index[sw_if_index] = ci;
1339       ci = rx_cm4m->config_index_by_sw_if_index[sw_if_index];   //IP4 mcast
1340       ci = vnet_config_add_feature (vm, &rx_cm4m->config_main,
1341                                     ci,
1342                                     im4->ip4_multicast_rx_feature_vpath,
1343                                     0, 0);
1344       rx_cm4m->config_index_by_sw_if_index[sw_if_index] = ci;
1345       ci = rx_cm6u->config_index_by_sw_if_index[sw_if_index];   //IP6 unicast
1346       ci = vnet_config_add_feature (vm, &rx_cm6u->config_main,
1347                                     ci,
1348                                     im6->ip6_unicast_rx_feature_vpath, 0, 0);
1349       rx_cm6u->config_index_by_sw_if_index[sw_if_index] = ci;
1350       ci = rx_cm6m->config_index_by_sw_if_index[sw_if_index];   //IP6 mcast
1351       ci = vnet_config_add_feature (vm, &rx_cm6m->config_main,
1352                                     ci,
1353                                     im6->ip6_multicast_rx_feature_vpath,
1354                                     0, 0);
1355       rx_cm6m->config_index_by_sw_if_index[sw_if_index] = ci;
1356     }
1357   else
1358     {
1359       ci = rx_cm4u->config_index_by_sw_if_index[sw_if_index];   //IP4 unicast
1360       ci = vnet_config_del_feature (vm, &rx_cm4u->config_main,
1361                                     ci,
1362                                     im4->ip4_unicast_rx_feature_vpath, 0, 0);
1363       rx_cm4u->config_index_by_sw_if_index[sw_if_index] = ci;
1364       ci = rx_cm4m->config_index_by_sw_if_index[sw_if_index];   //IP4 mcast
1365       ci = vnet_config_del_feature (vm, &rx_cm4m->config_main,
1366                                     ci,
1367                                     im4->ip4_multicast_rx_feature_vpath,
1368                                     0, 0);
1369       rx_cm4m->config_index_by_sw_if_index[sw_if_index] = ci;
1370       ci = rx_cm6u->config_index_by_sw_if_index[sw_if_index];   //IP6 unicast
1371       ci = vnet_config_del_feature (vm, &rx_cm6u->config_main,
1372                                     ci,
1373                                     im6->ip6_unicast_rx_feature_vpath, 0, 0);
1374       rx_cm6u->config_index_by_sw_if_index[sw_if_index] = ci;
1375       ci = rx_cm6m->config_index_by_sw_if_index[sw_if_index];   //IP6 mcast
1376       ci = vnet_config_del_feature (vm, &rx_cm6m->config_main,
1377                                     ci,
1378                                     im6->ip6_multicast_rx_feature_vpath,
1379                                     0, 0);
1380       rx_cm6m->config_index_by_sw_if_index[sw_if_index] = ci;
1381     }
1382
1383   BAD_SW_IF_INDEX_LABEL;
1384
1385   REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
1386 }
1387
1388 static void
1389   vl_api_sw_interface_set_l2_xconnect_t_handler
1390   (vl_api_sw_interface_set_l2_xconnect_t * mp)
1391 {
1392   vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
1393   int rv = 0;
1394   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
1395   u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
1396   vlib_main_t *vm = vlib_get_main ();
1397   vnet_main_t *vnm = vnet_get_main ();
1398
1399   VALIDATE_RX_SW_IF_INDEX (mp);
1400
1401   if (mp->enable)
1402     {
1403       VALIDATE_TX_SW_IF_INDEX (mp);
1404       rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
1405                             rx_sw_if_index, 0, 0, 0, tx_sw_if_index);
1406     }
1407   else
1408     {
1409       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
1410     }
1411
1412   BAD_RX_SW_IF_INDEX_LABEL;
1413   BAD_TX_SW_IF_INDEX_LABEL;
1414
1415   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
1416 }
1417
1418 static void
1419   vl_api_sw_interface_set_l2_bridge_t_handler
1420   (vl_api_sw_interface_set_l2_bridge_t * mp)
1421 {
1422   bd_main_t *bdm = &bd_main;
1423   vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
1424   int rv = 0;
1425   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
1426   u32 bd_id = ntohl (mp->bd_id);
1427   u32 bd_index;
1428   u32 bvi = mp->bvi;
1429   u8 shg = mp->shg;
1430   vlib_main_t *vm = vlib_get_main ();
1431   vnet_main_t *vnm = vnet_get_main ();
1432
1433   VALIDATE_RX_SW_IF_INDEX (mp);
1434
1435   bd_index = bd_find_or_add_bd_index (bdm, bd_id);
1436
1437   if (mp->enable)
1438     {
1439       //VALIDATE_TX_SW_IF_INDEX(mp);
1440       rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
1441                             rx_sw_if_index, bd_index, bvi, shg, 0);
1442     }
1443   else
1444     {
1445       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
1446     }
1447
1448   BAD_RX_SW_IF_INDEX_LABEL;
1449
1450   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
1451 }
1452
1453 static void
1454 vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
1455 {
1456   vlib_main_t *vm = vlib_get_main ();
1457   bd_main_t *bdm = &bd_main;
1458   vl_api_bridge_domain_add_del_reply_t *rmp;
1459   int rv = 0;
1460   u32 enable_flags = 0, disable_flags = 0;
1461   u32 bd_id = ntohl (mp->bd_id);
1462   u32 bd_index;
1463
1464   if (mp->is_add)
1465     {
1466       bd_index = bd_find_or_add_bd_index (bdm, bd_id);
1467
1468       if (mp->flood)
1469         enable_flags |= L2_FLOOD;
1470       else
1471         disable_flags |= L2_FLOOD;
1472
1473       if (mp->uu_flood)
1474         enable_flags |= L2_UU_FLOOD;
1475       else
1476         disable_flags |= L2_UU_FLOOD;
1477
1478       if (mp->forward)
1479         enable_flags |= L2_FWD;
1480       else
1481         disable_flags |= L2_FWD;
1482
1483       if (mp->arp_term)
1484         enable_flags |= L2_ARP_TERM;
1485       else
1486         disable_flags |= L2_ARP_TERM;
1487
1488       if (mp->learn)
1489         enable_flags |= L2_LEARN;
1490       else
1491         disable_flags |= L2_LEARN;
1492
1493       if (enable_flags)
1494         bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
1495
1496       if (disable_flags)
1497         bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
1498
1499     }
1500   else
1501     rv = bd_delete_bd_index (bdm, bd_id);
1502
1503   REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
1504 }
1505
1506 static void
1507 vl_api_bridge_domain_details_t_handler (vl_api_bridge_domain_details_t * mp)
1508 {
1509   clib_warning ("BUG");
1510 }
1511
1512 static void
1513   vl_api_bridge_domain_sw_if_details_t_handler
1514   (vl_api_bridge_domain_sw_if_details_t * mp)
1515 {
1516   clib_warning ("BUG");
1517 }
1518
1519 static void
1520 send_bridge_domain_details (unix_shared_memory_queue_t * q,
1521                             l2_bridge_domain_t * bd_config,
1522                             u32 n_sw_ifs, u32 context)
1523 {
1524   vl_api_bridge_domain_details_t *mp;
1525
1526   mp = vl_msg_api_alloc (sizeof (*mp));
1527   memset (mp, 0, sizeof (*mp));
1528   mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
1529   mp->bd_id = ntohl (bd_config->bd_id);
1530   mp->flood = bd_feature_flood (bd_config);
1531   mp->uu_flood = bd_feature_uu_flood (bd_config);
1532   mp->forward = bd_feature_forward (bd_config);
1533   mp->learn = bd_feature_learn (bd_config);
1534   mp->arp_term = bd_feature_arp_term (bd_config);
1535   mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
1536   mp->n_sw_ifs = ntohl (n_sw_ifs);
1537   mp->context = context;
1538
1539   vl_msg_api_send_shmem (q, (u8 *) & mp);
1540 }
1541
1542 static void
1543 send_bd_sw_if_details (l2input_main_t * l2im,
1544                        unix_shared_memory_queue_t * q,
1545                        l2_flood_member_t * member, u32 bd_id, u32 context)
1546 {
1547   vl_api_bridge_domain_sw_if_details_t *mp;
1548   l2_input_config_t *input_cfg;
1549
1550   mp = vl_msg_api_alloc (sizeof (*mp));
1551   memset (mp, 0, sizeof (*mp));
1552   mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_SW_IF_DETAILS);
1553   mp->bd_id = ntohl (bd_id);
1554   mp->sw_if_index = ntohl (member->sw_if_index);
1555   input_cfg = vec_elt_at_index (l2im->configs, member->sw_if_index);
1556   mp->shg = input_cfg->shg;
1557   mp->context = context;
1558
1559   vl_msg_api_send_shmem (q, (u8 *) & mp);
1560 }
1561
1562 static void
1563 vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
1564 {
1565   bd_main_t *bdm = &bd_main;
1566   l2input_main_t *l2im = &l2input_main;
1567   unix_shared_memory_queue_t *q;
1568   l2_bridge_domain_t *bd_config;
1569   u32 bd_id, bd_index;
1570   u32 end;
1571
1572   q = vl_api_client_index_to_input_queue (mp->client_index);
1573
1574   if (q == 0)
1575     return;
1576
1577   bd_id = ntohl (mp->bd_id);
1578
1579   bd_index = (bd_id == ~0) ? 0 : bd_find_or_add_bd_index (bdm, bd_id);
1580   end = (bd_id == ~0) ? vec_len (l2im->bd_configs) : bd_index + 1;
1581   for (; bd_index < end; bd_index++)
1582     {
1583       bd_config = l2input_bd_config_from_index (l2im, bd_index);
1584       /* skip dummy bd_id 0 */
1585       if (bd_config && (bd_config->bd_id > 0))
1586         {
1587           u32 n_sw_ifs;
1588           l2_flood_member_t *m;
1589
1590           n_sw_ifs = vec_len (bd_config->members);
1591           send_bridge_domain_details (q, bd_config, n_sw_ifs, mp->context);
1592
1593           vec_foreach (m, bd_config->members)
1594           {
1595             send_bd_sw_if_details (l2im, q, m, bd_config->bd_id, mp->context);
1596           }
1597         }
1598     }
1599 }
1600
1601 static void
1602 vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
1603 {
1604   bd_main_t *bdm = &bd_main;
1605   l2input_main_t *l2im = &l2input_main;
1606   vl_api_l2fib_add_del_reply_t *rmp;
1607   int rv = 0;
1608   u64 mac = 0;
1609   u32 sw_if_index = ntohl (mp->sw_if_index);
1610   u32 bd_id = ntohl (mp->bd_id);
1611   u32 bd_index;
1612   u32 static_mac;
1613   u32 filter_mac;
1614   u32 bvi_mac;
1615   uword *p;
1616
1617   mac = mp->mac;
1618
1619   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1620   if (!p)
1621     {
1622       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1623       goto bad_sw_if_index;
1624     }
1625   bd_index = p[0];
1626
1627   if (mp->is_add)
1628     {
1629       VALIDATE_SW_IF_INDEX (mp);
1630       if (vec_len (l2im->configs) <= sw_if_index)
1631         {
1632           rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1633           goto bad_sw_if_index;
1634         }
1635       else
1636         {
1637           l2_input_config_t *config;
1638           config = vec_elt_at_index (l2im->configs, sw_if_index);
1639           if (config->bridge == 0)
1640             {
1641               rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1642               goto bad_sw_if_index;
1643             }
1644         }
1645       static_mac = mp->static_mac ? 1 : 0;
1646       filter_mac = mp->filter_mac ? 1 : 0;
1647       bvi_mac = mp->bvi_mac ? 1 : 0;
1648       l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac,
1649                        bvi_mac);
1650     }
1651   else
1652     {
1653       l2fib_del_entry (mac, bd_index);
1654     }
1655
1656   BAD_SW_IF_INDEX_LABEL;
1657
1658   REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
1659 }
1660
1661 static void
1662 vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
1663 {
1664   vl_api_l2_flags_reply_t *rmp;
1665   int rv = 0;
1666   u32 sw_if_index = ntohl (mp->sw_if_index);
1667   u32 flags = ntohl (mp->feature_bitmap);
1668   u32 rbm = 0;
1669
1670   VALIDATE_SW_IF_INDEX (mp);
1671
1672 #define _(a,b) \
1673     if (flags & L2INPUT_FEAT_ ## a) \
1674         rbm = l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_ ## a, mp->is_set);
1675   foreach_l2input_feat;
1676 #undef _
1677
1678   BAD_SW_IF_INDEX_LABEL;
1679
1680   /* *INDENT-OFF* */
1681   REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
1682   ({
1683     rmp->resulting_feature_bitmap = ntohl(rbm);
1684   }));
1685   /* *INDENT-ON* */
1686 }
1687
1688 static void
1689 vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
1690 {
1691   vlib_main_t *vm = vlib_get_main ();
1692   bd_main_t *bdm = &bd_main;
1693   vl_api_bridge_flags_reply_t *rmp;
1694   int rv = 0;
1695   u32 bd_id = ntohl (mp->bd_id);
1696   u32 bd_index;
1697   u32 flags = ntohl (mp->feature_bitmap);
1698   uword *p;
1699
1700   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1701   if (p == 0)
1702     {
1703       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1704       goto out;
1705     }
1706
1707   bd_index = p[0];
1708
1709   bd_set_flags (vm, bd_index, flags, mp->is_set);
1710
1711 out:
1712   /* *INDENT-OFF* */
1713   REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
1714   ({
1715     rmp->resulting_feature_bitmap = ntohl(flags);
1716   }));
1717   /* *INDENT-ON* */
1718 }
1719
1720 static void
1721 vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
1722 {
1723   bd_main_t *bdm = &bd_main;
1724   vl_api_bd_ip_mac_add_del_reply_t *rmp;
1725   int rv = 0;
1726   u32 bd_id = ntohl (mp->bd_id);
1727   u32 bd_index;
1728   uword *p;
1729
1730   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1731   if (p == 0)
1732     {
1733       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1734       goto out;
1735     }
1736
1737   bd_index = p[0];
1738   if (bd_add_del_ip_mac (bd_index, mp->ip_address,
1739                          mp->mac_address, mp->is_ipv6, mp->is_add))
1740     rv = VNET_API_ERROR_UNSPECIFIED;
1741
1742 out:
1743   REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
1744 }
1745
1746 static void
1747 vl_api_tap_connect_t_handler (vl_api_tap_connect_t * mp, vlib_main_t * vm)
1748 {
1749   int rv;
1750   vl_api_tap_connect_reply_t *rmp;
1751   unix_shared_memory_queue_t *q;
1752   u32 sw_if_index = (u32) ~ 0;
1753
1754   rv = vnet_tap_connect_renumber (vm, mp->tap_name,
1755                                   mp->use_random_mac ? 0 : mp->mac_address,
1756                                   &sw_if_index, mp->renumber,
1757                                   ntohl (mp->custom_dev_instance));
1758
1759   q = vl_api_client_index_to_input_queue (mp->client_index);
1760   if (!q)
1761     return;
1762
1763   rmp = vl_msg_api_alloc (sizeof (*rmp));
1764   rmp->_vl_msg_id = ntohs (VL_API_TAP_CONNECT_REPLY);
1765   rmp->context = mp->context;
1766   rmp->retval = ntohl (rv);
1767   rmp->sw_if_index = ntohl (sw_if_index);
1768
1769   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1770 }
1771
1772 static void
1773 vl_api_tap_modify_t_handler (vl_api_tap_modify_t * mp, vlib_main_t * vm)
1774 {
1775   int rv;
1776   vl_api_tap_modify_reply_t *rmp;
1777   unix_shared_memory_queue_t *q;
1778   u32 sw_if_index = (u32) ~ 0;
1779
1780   rv = vnet_tap_modify (vm, ntohl (mp->sw_if_index), mp->tap_name,
1781                         mp->use_random_mac ? 0 : mp->mac_address,
1782                         &sw_if_index, mp->renumber,
1783                         ntohl (mp->custom_dev_instance));
1784
1785   q = vl_api_client_index_to_input_queue (mp->client_index);
1786   if (!q)
1787     return;
1788
1789   rmp = vl_msg_api_alloc (sizeof (*rmp));
1790   rmp->_vl_msg_id = ntohs (VL_API_TAP_MODIFY_REPLY);
1791   rmp->context = mp->context;
1792   rmp->retval = ntohl (rv);
1793   rmp->sw_if_index = ntohl (sw_if_index);
1794
1795   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1796 }
1797
1798 static void
1799 vl_api_tap_delete_t_handler (vl_api_tap_delete_t * mp, vlib_main_t * vm)
1800 {
1801   int rv;
1802   vpe_api_main_t *vam = &vpe_api_main;
1803   vl_api_tap_delete_reply_t *rmp;
1804   unix_shared_memory_queue_t *q;
1805   u32 sw_if_index = ntohl (mp->sw_if_index);
1806
1807   rv = vnet_tap_delete (vm, sw_if_index);
1808
1809   q = vl_api_client_index_to_input_queue (mp->client_index);
1810   if (!q)
1811     return;
1812
1813   rmp = vl_msg_api_alloc (sizeof (*rmp));
1814   rmp->_vl_msg_id = ntohs (VL_API_TAP_DELETE_REPLY);
1815   rmp->context = mp->context;
1816   rmp->retval = ntohl (rv);
1817
1818   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1819
1820   if (!rv)
1821     send_sw_interface_flags_deleted (vam, q, sw_if_index);
1822 }
1823
1824 static void
1825 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
1826 {
1827   vl_api_create_vlan_subif_reply_t *rmp;
1828   vnet_main_t *vnm = vnet_get_main ();
1829   u32 hw_if_index, sw_if_index = (u32) ~ 0;
1830   vnet_hw_interface_t *hi;
1831   int rv = 0;
1832   u32 id;
1833   vnet_sw_interface_t template;
1834   uword *p;
1835   vnet_interface_main_t *im = &vnm->interface_main;
1836   u64 sup_and_sub_key;
1837   u64 *kp;
1838   unix_shared_memory_queue_t *q;
1839   clib_error_t *error;
1840
1841   VALIDATE_SW_IF_INDEX (mp);
1842
1843   hw_if_index = ntohl (mp->sw_if_index);
1844   hi = vnet_get_hw_interface (vnm, hw_if_index);
1845
1846   id = ntohl (mp->vlan_id);
1847   if (id == 0 || id > 4095)
1848     {
1849       rv = VNET_API_ERROR_INVALID_VLAN;
1850       goto out;
1851     }
1852
1853   sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
1854
1855   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1856   if (p)
1857     {
1858       rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
1859       goto out;
1860     }
1861
1862   kp = clib_mem_alloc (sizeof (*kp));
1863   *kp = sup_and_sub_key;
1864
1865   memset (&template, 0, sizeof (template));
1866   template.type = VNET_SW_INTERFACE_TYPE_SUB;
1867   template.sup_sw_if_index = hi->sw_if_index;
1868   template.sub.id = id;
1869   template.sub.eth.raw_flags = 0;
1870   template.sub.eth.flags.one_tag = 1;
1871   template.sub.eth.outer_vlan_id = id;
1872   template.sub.eth.flags.exact_match = 1;
1873
1874   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1875   if (error)
1876     {
1877       clib_error_report (error);
1878       rv = VNET_API_ERROR_INVALID_REGISTRATION;
1879       goto out;
1880     }
1881   hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
1882   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1883
1884   BAD_SW_IF_INDEX_LABEL;
1885
1886 out:
1887   q = vl_api_client_index_to_input_queue (mp->client_index);
1888   if (!q)
1889     return;
1890
1891   rmp = vl_msg_api_alloc (sizeof (*rmp));
1892   rmp->_vl_msg_id = ntohs (VL_API_CREATE_VLAN_SUBIF_REPLY);
1893   rmp->context = mp->context;
1894   rmp->retval = ntohl (rv);
1895   rmp->sw_if_index = ntohl (sw_if_index);
1896   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1897 }
1898
1899 static void
1900 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
1901 {
1902   vl_api_create_subif_reply_t *rmp;
1903   vnet_main_t *vnm = vnet_get_main ();
1904   u32 sw_if_index = ~0;
1905   int rv = 0;
1906   u32 sub_id;
1907   vnet_sw_interface_t *si;
1908   vnet_hw_interface_t *hi;
1909   vnet_sw_interface_t template;
1910   uword *p;
1911   vnet_interface_main_t *im = &vnm->interface_main;
1912   u64 sup_and_sub_key;
1913   u64 *kp;
1914   clib_error_t *error;
1915
1916   VALIDATE_SW_IF_INDEX (mp);
1917
1918   si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
1919   hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1920
1921   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1922     {
1923       rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1924       goto out;
1925     }
1926
1927   sw_if_index = si->sw_if_index;
1928   sub_id = ntohl (mp->sub_id);
1929
1930   sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
1931
1932   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1933   if (p)
1934     {
1935       if (CLIB_DEBUG > 0)
1936         clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
1937                       sw_if_index, sub_id);
1938       rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
1939       goto out;
1940     }
1941
1942   kp = clib_mem_alloc (sizeof (*kp));
1943   *kp = sup_and_sub_key;
1944
1945   memset (&template, 0, sizeof (template));
1946   template.type = VNET_SW_INTERFACE_TYPE_SUB;
1947   template.sup_sw_if_index = sw_if_index;
1948   template.sub.id = sub_id;
1949   template.sub.eth.flags.no_tags = mp->no_tags;
1950   template.sub.eth.flags.one_tag = mp->one_tag;
1951   template.sub.eth.flags.two_tags = mp->two_tags;
1952   template.sub.eth.flags.dot1ad = mp->dot1ad;
1953   template.sub.eth.flags.exact_match = mp->exact_match;
1954   template.sub.eth.flags.default_sub = mp->default_sub;
1955   template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
1956   template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
1957   template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
1958   template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
1959
1960   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1961   if (error)
1962     {
1963       clib_error_report (error);
1964       rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
1965       goto out;
1966     }
1967
1968   hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
1969   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1970
1971   BAD_SW_IF_INDEX_LABEL;
1972
1973 out:
1974
1975   /* *INDENT-OFF* */
1976   REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
1977   ({
1978     rmp->sw_if_index = ntohl(sw_if_index);
1979   }));
1980   /* *INDENT-ON* */
1981 }
1982
1983 static void
1984 vl_api_mpls_gre_add_del_tunnel_t_handler (vl_api_mpls_gre_add_del_tunnel_t *
1985                                           mp)
1986 {
1987   vl_api_mpls_gre_add_del_tunnel_reply_t *rmp;
1988   int rv = 0;
1989   stats_main_t *sm = &stats_main;
1990   u32 tunnel_sw_if_index = ~0;
1991
1992   dslock (sm, 1 /* release hint */ , 5 /* tag */ );
1993
1994   rv = vnet_mpls_gre_add_del_tunnel ((ip4_address_t *) (mp->src_address),
1995                                      (ip4_address_t *) (mp->dst_address),
1996                                      (ip4_address_t *) (mp->intfc_address),
1997                                      (u32) (mp->intfc_address_length),
1998                                      ntohl (mp->inner_vrf_id),
1999                                      ntohl (mp->outer_vrf_id),
2000                                      &tunnel_sw_if_index,
2001                                      mp->l2_only, mp->is_add);
2002   dsunlock (sm);
2003
2004   /* *INDENT-OFF* */
2005   REPLY_MACRO2(VL_API_MPLS_GRE_ADD_DEL_TUNNEL_REPLY,
2006   ({
2007     rmp->tunnel_sw_if_index = ntohl(tunnel_sw_if_index);
2008   }));
2009   /* *INDENT-ON* */
2010 }
2011
2012 static void
2013   vl_api_mpls_ethernet_add_del_tunnel_t_handler
2014   (vl_api_mpls_ethernet_add_del_tunnel_t * mp)
2015 {
2016   vl_api_mpls_ethernet_add_del_tunnel_reply_t *rmp;
2017   int rv = 0;
2018   stats_main_t *sm = &stats_main;
2019   u32 tunnel_sw_if_index;
2020
2021   dslock (sm, 1 /* release hint */ , 5 /* tag */ );
2022
2023   rv = vnet_mpls_ethernet_add_del_tunnel
2024     (mp->dst_mac_address, (ip4_address_t *) (mp->adj_address),
2025      (u32) (mp->adj_address_length), ntohl (mp->vrf_id),
2026      ntohl (mp->tx_sw_if_index),
2027      &tunnel_sw_if_index, mp->l2_only, mp->is_add);
2028
2029   dsunlock (sm);
2030
2031   /* *INDENT-OFF* */
2032   REPLY_MACRO2(VL_API_MPLS_ETHERNET_ADD_DEL_TUNNEL_REPLY,
2033   ({
2034     rmp->tunnel_sw_if_index = ntohl(tunnel_sw_if_index);
2035   }));
2036   /* *INDENT-ON* */
2037 }
2038
2039 /*
2040  * This piece of misery brought to you because the control-plane
2041  * can't figure out the tx interface + dst-mac address all by itself
2042  */
2043 static int mpls_ethernet_add_del_tunnel_2_t_handler
2044   (vl_api_mpls_ethernet_add_del_tunnel_2_t * mp)
2045 {
2046   pending_route_t *pr;
2047   vl_api_mpls_ethernet_add_del_tunnel_2_t *pme;
2048   vnet_main_t *vnm = vnet_get_main ();
2049   vlib_main_t *vm = vlib_get_main ();
2050   stats_main_t *sm = &stats_main;
2051   vpe_api_main_t *vam = &vpe_api_main;
2052   u32 inner_fib_index, outer_fib_index;
2053   ip4_main_t *im = &ip4_main;
2054   ip_lookup_main_t *lm = &im->lookup_main;
2055   ip_adjacency_t *adj = 0;
2056   u32 lookup_result;
2057   u32 tx_sw_if_index;
2058   u8 *dst_mac_address;
2059   clib_error_t *e;
2060   uword *p;
2061   int rv;
2062   u32 tunnel_sw_if_index;
2063
2064   p = hash_get (im->fib_index_by_table_id, ntohl (mp->outer_vrf_id));
2065   if (!p)
2066     return VNET_API_ERROR_NO_SUCH_FIB;
2067   else
2068     outer_fib_index = p[0];
2069
2070
2071   p = hash_get (im->fib_index_by_table_id, ntohl (mp->inner_vrf_id));
2072   if (!p)
2073     return VNET_API_ERROR_NO_SUCH_INNER_FIB;
2074   else
2075     inner_fib_index = p[0];
2076
2077   if (inner_fib_index == outer_fib_index)
2078     return VNET_API_ERROR_INVALID_VALUE;
2079
2080   // FIXME not an ADJ
2081   lookup_result = ip4_fib_table_lookup_lb (ip4_fib_get (outer_fib_index),
2082                                            (ip4_address_t *)
2083                                            mp->
2084                                            next_hop_ip4_address_in_outer_vrf);
2085
2086   adj = ip_get_adjacency (lm, lookup_result);
2087   tx_sw_if_index = adj->rewrite_header.sw_if_index;
2088
2089   if (mp->is_add && mp->resolve_if_needed)
2090     {
2091       if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP)
2092         {
2093           pool_get (vam->pending_routes, pr);
2094           pr->resolve_type = RESOLVE_MPLS_ETHERNET_ADD_DEL;
2095           pme = &pr->t;
2096           clib_memcpy (pme, mp, sizeof (*pme));
2097           /* recursion block, "just in case" */
2098           pme->resolve_if_needed = 0;
2099           pme->resolve_attempts = ntohl (mp->resolve_attempts);
2100           pme->resolve_opaque = tx_sw_if_index;
2101           vnet_register_ip4_arp_resolution_event
2102             (vnm,
2103              (ip4_address_t *) & (pme->next_hop_ip4_address_in_outer_vrf),
2104              vpe_resolver_process_node.index,
2105              RESOLUTION_EVENT, pr - vam->pending_routes);
2106
2107           vlib_process_signal_event
2108             (vm, vpe_resolver_process_node.index,
2109              RESOLUTION_PENDING_EVENT, 0 /* data */ );
2110
2111           /* The interface may be down, etc. */
2112           e = ip4_probe_neighbor
2113             (vm, (ip4_address_t *) & (mp->next_hop_ip4_address_in_outer_vrf),
2114              tx_sw_if_index);
2115
2116           if (e)
2117             clib_error_report (e);
2118
2119           return VNET_API_ERROR_IN_PROGRESS;
2120         }
2121     }
2122
2123   if (adj->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
2124     return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
2125
2126   dst_mac_address =
2127     vnet_rewrite_get_data_internal
2128     (&adj->rewrite_header, sizeof (adj->rewrite_data));
2129
2130   dslock (sm, 1 /* release hint */ , 10 /* tag */ );
2131
2132   rv = vnet_mpls_ethernet_add_del_tunnel
2133     (dst_mac_address, (ip4_address_t *) (mp->adj_address),
2134      (u32) (mp->adj_address_length), ntohl (mp->inner_vrf_id),
2135      tx_sw_if_index, &tunnel_sw_if_index, mp->l2_only, mp->is_add);
2136
2137   dsunlock (sm);
2138
2139   return rv;
2140 }
2141
2142 static void
2143   vl_api_mpls_ethernet_add_del_tunnel_2_t_handler
2144   (vl_api_mpls_ethernet_add_del_tunnel_2_t * mp)
2145 {
2146   vl_api_mpls_ethernet_add_del_tunnel_reply_t *rmp;
2147   int rv = 0;
2148
2149   rv = mpls_ethernet_add_del_tunnel_2_t_handler (mp);
2150
2151   REPLY_MACRO (VL_API_MPLS_ETHERNET_ADD_DEL_TUNNEL_2_REPLY);
2152 }
2153
2154
2155 static void
2156 vl_api_mpls_add_del_encap_t_handler (vl_api_mpls_add_del_encap_t * mp)
2157 {
2158   vl_api_mpls_add_del_encap_reply_t *rmp;
2159   int rv;
2160   static u32 *labels;
2161   int i;
2162
2163   vec_reset_length (labels);
2164
2165   for (i = 0; i < mp->nlabels; i++)
2166     vec_add1 (labels, ntohl (mp->labels[i]));
2167
2168   /* $$$$ fixme */
2169   rv = vnet_mpls_add_del_encap ((ip4_address_t *) mp->dst_address,
2170                                 ntohl (mp->vrf_id), labels,
2171                                 ~0 /* policy_tunnel_index */ ,
2172                                 0 /* no_dst_hash */ ,
2173                                 0 /* indexp */ ,
2174                                 mp->is_add);
2175
2176   REPLY_MACRO (VL_API_MPLS_ADD_DEL_ENCAP_REPLY);
2177 }
2178
2179 static void
2180 vl_api_mpls_add_del_decap_t_handler (vl_api_mpls_add_del_decap_t * mp)
2181 {
2182   vl_api_mpls_add_del_decap_reply_t *rmp;
2183   int rv;
2184
2185   rv = vnet_mpls_add_del_decap (ntohl (mp->rx_vrf_id), ntohl (mp->tx_vrf_id),
2186                                 ntohl (mp->label), ntohl (mp->next_index),
2187                                 mp->s_bit, mp->is_add);
2188
2189   REPLY_MACRO (VL_API_MPLS_ADD_DEL_DECAP_REPLY);
2190 }
2191
2192 static void
2193 vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2194 {
2195   vl_api_proxy_arp_add_del_reply_t *rmp;
2196   u32 fib_index;
2197   int rv;
2198   ip4_main_t *im = &ip4_main;
2199   stats_main_t *sm = &stats_main;
2200   int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
2201                               ip4_address_t * hi_addr,
2202                               u32 fib_index, int is_del);
2203   uword *p;
2204
2205   dslock (sm, 1 /* release hint */ , 6 /* tag */ );
2206
2207   p = hash_get (im->fib_index_by_table_id, ntohl (mp->vrf_id));
2208
2209   if (!p)
2210     {
2211       rv = VNET_API_ERROR_NO_SUCH_FIB;
2212       goto out;
2213     }
2214
2215   fib_index = p[0];
2216
2217   rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->low_address,
2218                                (ip4_address_t *) mp->hi_address,
2219                                fib_index, mp->is_add == 0);
2220
2221 out:
2222   dsunlock (sm);
2223   REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2224 }
2225
2226 static void
2227   vl_api_proxy_arp_intfc_enable_disable_t_handler
2228   (vl_api_proxy_arp_intfc_enable_disable_t * mp)
2229 {
2230   int rv = 0;
2231   vnet_main_t *vnm = vnet_get_main ();
2232   vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
2233   vnet_sw_interface_t *si;
2234   u32 sw_if_index;
2235
2236   VALIDATE_SW_IF_INDEX (mp);
2237
2238   sw_if_index = ntohl (mp->sw_if_index);
2239
2240   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
2241     {
2242       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2243       goto out;
2244     }
2245
2246   si = vnet_get_sw_interface (vnm, sw_if_index);
2247
2248   ASSERT (si);
2249
2250   if (mp->enable_disable)
2251     si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2252   else
2253     si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2254
2255   BAD_SW_IF_INDEX_LABEL;
2256
2257 out:
2258   REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
2259 }
2260
2261 static void
2262 vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
2263                                       vlib_main_t * vm)
2264 {
2265   vl_api_ip_neighbor_add_del_reply_t *rmp;
2266   vnet_main_t *vnm = vnet_get_main ();
2267   stats_main_t *sm = &stats_main;
2268   int rv = 0;
2269
2270   VALIDATE_SW_IF_INDEX (mp);
2271
2272   dslock (sm, 1 /* release hint */ , 7 /* tag */ );
2273
2274   /*
2275    * there's no validation here of the ND/ARP entry being added.
2276    * The expectation is that the FIB will ensure that nothing bad
2277    * will come of adding bogus entries.
2278    */
2279   if (mp->is_ipv6)
2280     {
2281       if (mp->is_add)
2282         rv = vnet_set_ip6_ethernet_neighbor
2283           (vm, ntohl (mp->sw_if_index),
2284            (ip6_address_t *) (mp->dst_address),
2285            mp->mac_address, sizeof (mp->mac_address), mp->is_static);
2286       else
2287         rv = vnet_unset_ip6_ethernet_neighbor
2288           (vm, ntohl (mp->sw_if_index),
2289            (ip6_address_t *) (mp->dst_address),
2290            mp->mac_address, sizeof (mp->mac_address));
2291     }
2292   else
2293     {
2294       ethernet_arp_ip4_over_ethernet_address_t a;
2295
2296       clib_memcpy (&a.ethernet, mp->mac_address, 6);
2297       clib_memcpy (&a.ip4, mp->dst_address, 4);
2298
2299       if (mp->is_add)
2300         rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index),
2301                                              &a, mp->is_static);
2302       else
2303         rv =
2304           vnet_arp_unset_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index), &a);
2305     }
2306
2307   BAD_SW_IF_INDEX_LABEL;
2308
2309   dsunlock (sm);
2310   REPLY_MACRO (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
2311 }
2312
2313 static void
2314 vl_api_is_address_reachable_t_handler (vl_api_is_address_reachable_t * mp)
2315 {
2316 #if 0
2317   vpe_main_t *rm = &vpe_main;
2318   ip4_main_t *im4 = &ip4_main;
2319   ip6_main_t *im6 = &ip6_main;
2320   ip_lookup_main_t *lm;
2321   union
2322   {
2323     ip4_address_t ip4;
2324     ip6_address_t ip6;
2325   } addr;
2326   u32 adj_index, sw_if_index;
2327   vl_api_is_address_reachable_t *rmp;
2328   ip_adjacency_t *adj;
2329   unix_shared_memory_queue_t *q;
2330
2331   q = vl_api_client_index_to_input_queue (mp->client_index);
2332   if (!q)
2333     {
2334       increment_missing_api_client_counter (rm->vlib_main);
2335       return;
2336     }
2337
2338   rmp = vl_msg_api_alloc (sizeof (*rmp));
2339   clib_memcpy (rmp, mp, sizeof (*rmp));
2340
2341   sw_if_index = mp->next_hop_sw_if_index;
2342   clib_memcpy (&addr, mp->address, sizeof (addr));
2343   if (mp->is_ipv6)
2344     {
2345       lm = &im6->lookup_main;
2346       adj_index = ip6_fib_lookup (im6, sw_if_index, &addr.ip6);
2347     }
2348   else
2349     {
2350       lm = &im4->lookup_main;
2351       // FIXME NOT an ADJ
2352       adj_index = ip4_fib_lookup (im4, sw_if_index, &addr.ip4);
2353     }
2354   if (adj_index == ~0)
2355     {
2356       rmp->is_error = 1;
2357       goto send;
2358     }
2359   adj = ip_get_adjacency (lm, adj_index);
2360
2361   if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE
2362       && adj->rewrite_header.sw_if_index == sw_if_index)
2363     {
2364       rmp->is_known = 1;
2365     }
2366   else
2367     {
2368       if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP
2369           && adj->rewrite_header.sw_if_index == sw_if_index)
2370         {
2371           if (mp->is_ipv6)
2372             ip6_probe_neighbor (rm->vlib_main, &addr.ip6, sw_if_index);
2373           else
2374             ip4_probe_neighbor (rm->vlib_main, &addr.ip4, sw_if_index);
2375         }
2376       else if (adj->lookup_next_index == IP_LOOKUP_NEXT_DROP)
2377         {
2378           rmp->is_known = 1;
2379           goto send;
2380         }
2381       rmp->is_known = 0;
2382     }
2383
2384 send:
2385   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2386 #endif
2387 }
2388
2389 static void
2390 vl_api_sw_interface_details_t_handler (vl_api_sw_interface_details_t * mp)
2391 {
2392   clib_warning ("BUG");
2393 }
2394
2395 static void
2396 vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
2397 {
2398   vl_api_sw_interface_set_flags_reply_t *rmp;
2399   vnet_main_t *vnm = vnet_get_main ();
2400   int rv = 0;
2401   clib_error_t *error;
2402   u16 flags;
2403
2404   VALIDATE_SW_IF_INDEX (mp);
2405
2406   flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
2407
2408   error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
2409   if (error)
2410     {
2411       rv = -1;
2412       clib_error_report (error);
2413     }
2414
2415   BAD_SW_IF_INDEX_LABEL;
2416   REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
2417 }
2418
2419 static void
2420   vl_api_sw_interface_set_mpls_enable_t_handler
2421   (vl_api_sw_interface_set_mpls_enable_t * mp)
2422 {
2423   vl_api_sw_interface_set_mpls_enable_reply_t *rmp;
2424   int rv = 0;
2425
2426   VALIDATE_SW_IF_INDEX (mp);
2427
2428   mpls_sw_interface_enable_disable (&mpls_main,
2429                                     ntohl (mp->sw_if_index), mp->enable);
2430
2431   BAD_SW_IF_INDEX_LABEL;
2432   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MPLS_ENABLE_REPLY);
2433 }
2434
2435 static void
2436 vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
2437                                            mp)
2438 {
2439   vl_api_sw_interface_clear_stats_reply_t *rmp;
2440
2441   vnet_main_t *vnm = vnet_get_main ();
2442   vnet_interface_main_t *im = &vnm->interface_main;
2443   vlib_simple_counter_main_t *sm;
2444   vlib_combined_counter_main_t *cm;
2445   static vnet_main_t **my_vnet_mains;
2446   int i, j, n_counters;
2447   int rv = 0;
2448
2449   if (mp->sw_if_index != ~0)
2450     VALIDATE_SW_IF_INDEX (mp);
2451
2452   vec_reset_length (my_vnet_mains);
2453
2454   for (i = 0; i < vec_len (vnet_mains); i++)
2455     {
2456       if (vnet_mains[i])
2457         vec_add1 (my_vnet_mains, vnet_mains[i]);
2458     }
2459
2460   if (vec_len (vnet_mains) == 0)
2461     vec_add1 (my_vnet_mains, vnm);
2462
2463   n_counters = vec_len (im->combined_sw_if_counters);
2464
2465   for (j = 0; j < n_counters; j++)
2466     {
2467       for (i = 0; i < vec_len (my_vnet_mains); i++)
2468         {
2469           im = &my_vnet_mains[i]->interface_main;
2470           cm = im->combined_sw_if_counters + j;
2471           if (mp->sw_if_index == (u32) ~ 0)
2472             vlib_clear_combined_counters (cm);
2473           else
2474             vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
2475         }
2476     }
2477
2478   n_counters = vec_len (im->sw_if_counters);
2479
2480   for (j = 0; j < n_counters; j++)
2481     {
2482       for (i = 0; i < vec_len (my_vnet_mains); i++)
2483         {
2484           im = &my_vnet_mains[i]->interface_main;
2485           sm = im->sw_if_counters + j;
2486           if (mp->sw_if_index == (u32) ~ 0)
2487             vlib_clear_simple_counters (sm);
2488           else
2489             vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
2490         }
2491     }
2492
2493   BAD_SW_IF_INDEX_LABEL;
2494
2495   REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
2496 }
2497
2498 static void
2499 send_sw_interface_details (vpe_api_main_t * am,
2500                            unix_shared_memory_queue_t * q,
2501                            vnet_sw_interface_t * swif,
2502                            u8 * interface_name, u32 context)
2503 {
2504   vl_api_sw_interface_details_t *mp;
2505   vnet_hw_interface_t *hi;
2506
2507   hi = vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
2508
2509   mp = vl_msg_api_alloc (sizeof (*mp));
2510   memset (mp, 0, sizeof (*mp));
2511   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
2512   mp->sw_if_index = ntohl (swif->sw_if_index);
2513   mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
2514   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
2515   mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
2516   mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
2517                      VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
2518   mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
2519                     VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
2520   mp->link_mtu = ntohs (hi->max_packet_bytes);
2521   mp->context = context;
2522
2523   strncpy ((char *) mp->interface_name,
2524            (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
2525
2526   /* Send the L2 address for ethernet physical intfcs */
2527   if (swif->sup_sw_if_index == swif->sw_if_index
2528       && hi->hw_class_index == ethernet_hw_interface_class.index)
2529     {
2530       ethernet_main_t *em = ethernet_get_main (am->vlib_main);
2531       ethernet_interface_t *ei;
2532
2533       ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
2534       ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
2535       clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
2536       mp->l2_address_length = ntohl (sizeof (ei->address));
2537     }
2538   else if (swif->sup_sw_if_index != swif->sw_if_index)
2539     {
2540       vnet_sub_interface_t *sub = &swif->sub;
2541       mp->sub_id = ntohl (sub->id);
2542       mp->sub_dot1ad = sub->eth.flags.dot1ad;
2543       mp->sub_number_of_tags =
2544         sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
2545       mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
2546       mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
2547       mp->sub_exact_match = sub->eth.flags.exact_match;
2548       mp->sub_default = sub->eth.flags.default_sub;
2549       mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
2550       mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
2551
2552       /* vlan tag rewrite data */
2553       u32 vtr_op = L2_VTR_DISABLED;
2554       u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
2555
2556       if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
2557                      &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
2558         {
2559           // error - default to disabled
2560           mp->vtr_op = ntohl (L2_VTR_DISABLED);
2561           clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
2562                         swif->sw_if_index);
2563         }
2564       else
2565         {
2566           mp->vtr_op = ntohl (vtr_op);
2567           mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
2568           mp->vtr_tag1 = ntohl (vtr_tag1);
2569           mp->vtr_tag2 = ntohl (vtr_tag2);
2570         }
2571     }
2572
2573   vl_msg_api_send_shmem (q, (u8 *) & mp);
2574 }
2575
2576 static void
2577 send_sw_interface_flags (vpe_api_main_t * am,
2578                          unix_shared_memory_queue_t * q,
2579                          vnet_sw_interface_t * swif)
2580 {
2581   vl_api_sw_interface_set_flags_t *mp;
2582   vnet_main_t *vnm = am->vnet_main;
2583
2584   vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
2585                                                        swif->sw_if_index);
2586   mp = vl_msg_api_alloc (sizeof (*mp));
2587   memset (mp, 0, sizeof (*mp));
2588   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SET_FLAGS);
2589   mp->sw_if_index = ntohl (swif->sw_if_index);
2590
2591   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
2592   mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
2593   vl_msg_api_send_shmem (q, (u8 *) & mp);
2594 }
2595
2596 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
2597                                              unix_shared_memory_queue_t * q,
2598                                              u32 sw_if_index)
2599   __attribute__ ((unused));
2600
2601 static void
2602 send_sw_interface_flags_deleted (vpe_api_main_t * am,
2603                                  unix_shared_memory_queue_t * q,
2604                                  u32 sw_if_index)
2605 {
2606   vl_api_sw_interface_set_flags_t *mp;
2607
2608   mp = vl_msg_api_alloc (sizeof (*mp));
2609   memset (mp, 0, sizeof (*mp));
2610   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SET_FLAGS);
2611   mp->sw_if_index = ntohl (sw_if_index);
2612
2613   mp->admin_up_down = 0;
2614   mp->link_up_down = 0;
2615   mp->deleted = 1;
2616   vl_msg_api_send_shmem (q, (u8 *) & mp);
2617 }
2618
2619 static void
2620 vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
2621 {
2622   vpe_api_main_t *am = &vpe_api_main;
2623   vnet_sw_interface_t *swif;
2624   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2625   u8 *filter_string = 0, *name_string = 0;
2626   unix_shared_memory_queue_t *q;
2627   char *strcasestr (char *, char *);    /* lnx hdr file botch */
2628
2629   q = vl_api_client_index_to_input_queue (mp->client_index);
2630
2631   if (q == 0)
2632     return;
2633
2634   if (mp->name_filter_valid)
2635     {
2636       mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
2637       filter_string = format (0, "%s%c", mp->name_filter, 0);
2638     }
2639
2640   /* *INDENT-OFF* */
2641   pool_foreach (swif, im->sw_interfaces,
2642   ({
2643     name_string = format (name_string, "%U%c",
2644                           format_vnet_sw_interface_name,
2645                           am->vnet_main, swif, 0);
2646
2647     if (mp->name_filter_valid == 0 ||
2648         strcasestr((char *) name_string, (char *) filter_string)) {
2649
2650       send_sw_interface_details (am, q, swif, name_string, mp->context);
2651     }
2652     _vec_len (name_string) = 0;
2653   }));
2654   /* *INDENT-ON* */
2655
2656   vec_free (name_string);
2657   vec_free (filter_string);
2658 }
2659
2660 void
2661 send_oam_event (oam_target_t * t)
2662 {
2663   vpe_api_main_t *vam = &vpe_api_main;
2664   unix_shared_memory_queue_t *q;
2665   vpe_client_registration_t *reg;
2666   vl_api_oam_event_t *mp;
2667
2668   /* *INDENT-OFF* */
2669   pool_foreach(reg, vam->oam_events_registrations,
2670   ({
2671     q = vl_api_client_index_to_input_queue (reg->client_index);
2672     if (q)
2673       {
2674         mp = vl_msg_api_alloc (sizeof (*mp));
2675         mp->_vl_msg_id = ntohs (VL_API_OAM_EVENT);
2676         clib_memcpy (mp->dst_address, &t->dst_address,
2677                      sizeof (mp->dst_address));
2678         mp->state = t->state;
2679         vl_msg_api_send_shmem (q, (u8 *)&mp);
2680       }
2681   }));
2682   /* *INDENT-ON* */
2683 }
2684
2685 static void
2686 vl_api_oam_add_del_t_handler (vl_api_oam_add_del_t * mp)
2687 {
2688   vl_api_oam_add_del_reply_t *rmp;
2689   int rv;
2690
2691   rv = vpe_oam_add_del_target ((ip4_address_t *) mp->src_address,
2692                                (ip4_address_t *) mp->dst_address,
2693                                ntohl (mp->vrf_id), (int) (mp->is_add));
2694
2695   REPLY_MACRO (VL_API_OAM_ADD_DEL_REPLY);
2696 }
2697
2698 static void
2699 vl_api_vnet_get_summary_stats_t_handler (vl_api_vnet_get_summary_stats_t * mp)
2700 {
2701   stats_main_t *sm = &stats_main;
2702   vnet_interface_main_t *im = sm->interface_main;
2703   vl_api_vnet_summary_stats_reply_t *rmp;
2704   vlib_combined_counter_main_t *cm;
2705   vlib_counter_t v;
2706   int i, which;
2707   u64 total_pkts[VLIB_N_RX_TX];
2708   u64 total_bytes[VLIB_N_RX_TX];
2709
2710   unix_shared_memory_queue_t *q =
2711     vl_api_client_index_to_input_queue (mp->client_index);
2712
2713   if (!q)
2714     return;
2715
2716   rmp = vl_msg_api_alloc (sizeof (*rmp));
2717   rmp->_vl_msg_id = ntohs (VL_API_VNET_SUMMARY_STATS_REPLY);
2718   rmp->context = mp->context;
2719   rmp->retval = 0;
2720
2721   memset (total_pkts, 0, sizeof (total_pkts));
2722   memset (total_bytes, 0, sizeof (total_bytes));
2723
2724   vnet_interface_counter_lock (im);
2725
2726   vec_foreach (cm, im->combined_sw_if_counters)
2727   {
2728     which = cm - im->combined_sw_if_counters;
2729
2730     for (i = 0; i < vec_len (cm->maxi); i++)
2731       {
2732         vlib_get_combined_counter (cm, i, &v);
2733         total_pkts[which] += v.packets;
2734         total_bytes[which] += v.bytes;
2735       }
2736   }
2737   vnet_interface_counter_unlock (im);
2738
2739   rmp->total_pkts[VLIB_RX] = clib_host_to_net_u64 (total_pkts[VLIB_RX]);
2740   rmp->total_bytes[VLIB_RX] = clib_host_to_net_u64 (total_bytes[VLIB_RX]);
2741   rmp->total_pkts[VLIB_TX] = clib_host_to_net_u64 (total_pkts[VLIB_TX]);
2742   rmp->total_bytes[VLIB_TX] = clib_host_to_net_u64 (total_bytes[VLIB_TX]);
2743   rmp->vector_rate =
2744     clib_host_to_net_u64 (vlib_last_vector_length_per_node (sm->vlib_main));
2745
2746   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2747 }
2748
2749 /* *INDENT-OFF* */
2750 typedef CLIB_PACKED (struct {
2751   ip4_address_t address;
2752   u32 address_length: 6;
2753   u32 index:26;
2754 }) ip4_route_t;
2755 /* *INDENT-ON* */
2756
2757 static int
2758 ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2759 {
2760   vnet_main_t *vnm = vnet_get_main ();
2761   vnet_interface_main_t *im = &vnm->interface_main;
2762   ip4_main_t *im4 = &ip4_main;
2763   static u32 *sw_if_indices_to_shut;
2764   stats_main_t *sm = &stats_main;
2765   fib_table_t *fib_table;
2766   ip4_fib_t *fib;
2767   u32 sw_if_index;
2768   int i;
2769   int rv = VNET_API_ERROR_NO_SUCH_FIB;
2770   u32 target_fib_id = ntohl (mp->vrf_id);
2771
2772   dslock (sm, 1 /* release hint */ , 8 /* tag */ );
2773
2774   /* *INDENT-OFF* */
2775   pool_foreach (fib_table, im4->fibs,
2776   ({
2777     fib = &fib_table->v4;
2778     vnet_sw_interface_t * si;
2779
2780     if (fib->table_id != target_fib_id)
2781       continue;
2782
2783     /* remove any mpls/gre tunnels in this fib */
2784     vnet_mpls_gre_delete_fib_tunnels (fib->table_id);
2785
2786     /* remove any mpls encap/decap labels */
2787     mpls_fib_reset_labels (fib->table_id);
2788
2789     /* remove any proxy arps in this fib */
2790     vnet_proxy_arp_fib_reset (fib->table_id);
2791
2792     /* Set the flow hash for this fib to the default */
2793     vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2794
2795     vec_reset_length (sw_if_indices_to_shut);
2796
2797     /* Shut down interfaces in this FIB / clean out intfc routes */
2798     pool_foreach (si, im->sw_interfaces,
2799     ({
2800       u32 sw_if_index = si->sw_if_index;
2801
2802       if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
2803           && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
2804               fib->index))
2805         vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2806     }));
2807
2808     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2809       sw_if_index = sw_if_indices_to_shut[i];
2810       // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
2811
2812       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2813       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2814       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2815     }
2816
2817     fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
2818     fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_INTERFACE);
2819
2820     rv = 0;
2821     break;
2822     })); /* pool_foreach (fib) */
2823     /* *INDENT-ON* */
2824
2825   dsunlock (sm);
2826   return rv;
2827 }
2828
2829 static int
2830 ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2831 {
2832   vnet_main_t *vnm = vnet_get_main ();
2833   vnet_interface_main_t *im = &vnm->interface_main;
2834   ip6_main_t *im6 = &ip6_main;
2835   stats_main_t *sm = &stats_main;
2836   static u32 *sw_if_indices_to_shut;
2837   fib_table_t *fib_table;
2838   ip6_fib_t *fib;
2839   u32 sw_if_index;
2840   int i;
2841   int rv = VNET_API_ERROR_NO_SUCH_FIB;
2842   u32 target_fib_id = ntohl (mp->vrf_id);
2843
2844   dslock (sm, 1 /* release hint */ , 9 /* tag */ );
2845
2846   /* *INDENT-OFF* */
2847   pool_foreach (fib_table, im6->fibs,
2848   ({
2849     vnet_sw_interface_t * si;
2850     fib = &(fib_table->v6);
2851
2852     if (fib->table_id != target_fib_id)
2853       continue;
2854
2855     vec_reset_length (sw_if_indices_to_shut);
2856
2857     /* Shut down interfaces in this FIB / clean out intfc routes */
2858     pool_foreach (si, im->sw_interfaces,
2859                   ({
2860                     if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
2861                         fib->index)
2862                       vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2863                   }));
2864
2865     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2866       sw_if_index = sw_if_indices_to_shut[i];
2867       // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
2868
2869       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2870       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2871       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2872     }
2873
2874     fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
2875     fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_INTERFACE);
2876
2877     rv = 0;
2878     break;
2879   })); /* pool_foreach (fib) */
2880   /* *INDENT-ON* */
2881
2882   dsunlock (sm);
2883   return rv;
2884 }
2885
2886 static void
2887 vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2888 {
2889   int rv;
2890   vl_api_reset_fib_reply_t *rmp;
2891
2892   if (mp->is_ipv6)
2893     rv = ip6_reset_fib_t_handler (mp);
2894   else
2895     rv = ip4_reset_fib_t_handler (mp);
2896
2897   REPLY_MACRO (VL_API_RESET_FIB_REPLY);
2898 }
2899
2900
2901 static void
2902 dhcpv4_proxy_config (vl_api_dhcp_proxy_config_t * mp)
2903 {
2904   vl_api_dhcp_proxy_config_reply_t *rmp;
2905   int rv;
2906
2907   rv = dhcp_proxy_set_server ((ip4_address_t *) (&mp->dhcp_server),
2908                               (ip4_address_t *) (&mp->dhcp_src_address),
2909                               (u32) ntohl (mp->vrf_id),
2910                               (int) mp->insert_circuit_id,
2911                               (int) (mp->is_add == 0));
2912
2913   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
2914 }
2915
2916
2917 static void
2918 dhcpv6_proxy_config (vl_api_dhcp_proxy_config_t * mp)
2919 {
2920   vl_api_dhcp_proxy_config_reply_t *rmp;
2921   int rv = -1;
2922
2923   rv = dhcpv6_proxy_set_server ((ip6_address_t *) (&mp->dhcp_server),
2924                                 (ip6_address_t *) (&mp->dhcp_src_address),
2925                                 (u32) ntohl (mp->vrf_id),
2926                                 (int) mp->insert_circuit_id,
2927                                 (int) (mp->is_add == 0));
2928
2929   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
2930 }
2931
2932 static void
2933 dhcpv4_proxy_config_2 (vl_api_dhcp_proxy_config_2_t * mp)
2934 {
2935   vl_api_dhcp_proxy_config_reply_t *rmp;
2936   int rv;
2937
2938   rv = dhcp_proxy_set_server_2 ((ip4_address_t *) (&mp->dhcp_server),
2939                                 (ip4_address_t *) (&mp->dhcp_src_address),
2940                                 (u32) ntohl (mp->rx_vrf_id),
2941                                 (u32) ntohl (mp->server_vrf_id),
2942                                 (int) mp->insert_circuit_id,
2943                                 (int) (mp->is_add == 0));
2944
2945   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_2_REPLY);
2946 }
2947
2948
2949 static void
2950 dhcpv6_proxy_config_2 (vl_api_dhcp_proxy_config_2_t * mp)
2951 {
2952   vl_api_dhcp_proxy_config_reply_t *rmp;
2953   int rv = -1;
2954
2955 #if 0                           // $$$$ FIXME
2956   rv = dhcpv6_proxy_set_server_2 ((ip6_address_t *) (&mp->dhcp_server),
2957                                   (ip6_address_t *) (&mp->dhcp_src_address),
2958                                   (u32) ntohl (mp->rx_vrf_id),
2959                                   (u32) ntohl (mp->server_vrf_id),
2960                                   (int) mp->insert_circuit_id,
2961                                   (int) (mp->is_add == 0));
2962 #else
2963   rv = VNET_API_ERROR_UNIMPLEMENTED;
2964 #endif
2965
2966   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_2_REPLY);
2967 }
2968
2969
2970 static void
2971 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t * mp)
2972 {
2973   vl_api_dhcp_proxy_set_vss_reply_t *rmp;
2974   int rv;
2975   if (!mp->is_ipv6)
2976     rv = dhcp_proxy_set_option82_vss (ntohl (mp->tbl_id),
2977                                       ntohl (mp->oui),
2978                                       ntohl (mp->fib_id),
2979                                       (int) mp->is_add == 0);
2980   else
2981     rv = dhcpv6_proxy_set_vss (ntohl (mp->tbl_id),
2982                                ntohl (mp->oui),
2983                                ntohl (mp->fib_id), (int) mp->is_add == 0);
2984
2985   REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
2986 }
2987
2988
2989 static void vl_api_dhcp_proxy_config_t_handler
2990   (vl_api_dhcp_proxy_config_t * mp)
2991 {
2992   if (mp->is_ipv6 == 0)
2993     dhcpv4_proxy_config (mp);
2994   else
2995     dhcpv6_proxy_config (mp);
2996 }
2997
2998 static void vl_api_dhcp_proxy_config_2_t_handler
2999   (vl_api_dhcp_proxy_config_2_t * mp)
3000 {
3001   if (mp->is_ipv6 == 0)
3002     dhcpv4_proxy_config_2 (mp);
3003   else
3004     dhcpv6_proxy_config_2 (mp);
3005 }
3006
3007 void
3008 dhcp_compl_event_callback (u32 client_index, u32 pid, u8 * hostname,
3009                            u8 is_ipv6, u8 * host_address, u8 * router_address,
3010                            u8 * host_mac)
3011 {
3012   unix_shared_memory_queue_t *q;
3013   vl_api_dhcp_compl_event_t *mp;
3014
3015   q = vl_api_client_index_to_input_queue (client_index);
3016   if (!q)
3017     return;
3018
3019   mp = vl_msg_api_alloc (sizeof (*mp));
3020   mp->client_index = client_index;
3021   mp->pid = pid;
3022   mp->is_ipv6 = is_ipv6;
3023   clib_memcpy (&mp->hostname, hostname, vec_len (hostname));
3024   mp->hostname[vec_len (hostname) + 1] = '\n';
3025   clib_memcpy (&mp->host_address[0], host_address, 16);
3026   clib_memcpy (&mp->router_address[0], router_address, 16);
3027   clib_memcpy (&mp->host_mac[0], host_mac, 6);
3028
3029   mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
3030
3031   vl_msg_api_send_shmem (q, (u8 *) & mp);
3032 }
3033
3034 static void vl_api_dhcp_client_config_t_handler
3035   (vl_api_dhcp_client_config_t * mp)
3036 {
3037   vlib_main_t *vm = vlib_get_main ();
3038   vl_api_dhcp_client_config_reply_t *rmp;
3039   int rv = 0;
3040
3041   VALIDATE_SW_IF_INDEX (mp);
3042
3043   rv = dhcp_client_config (vm, ntohl (mp->sw_if_index),
3044                            mp->hostname, mp->is_add, mp->client_index,
3045                            mp->want_dhcp_event ? dhcp_compl_event_callback :
3046                            NULL, mp->pid);
3047
3048   BAD_SW_IF_INDEX_LABEL;
3049
3050   REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
3051 }
3052
3053 static void
3054   vl_api_sw_interface_ip6nd_ra_config_t_handler
3055   (vl_api_sw_interface_ip6nd_ra_config_t * mp, vlib_main_t * vm)
3056 {
3057   vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
3058   int rv = 0;
3059   u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
3060     default_router;
3061
3062   is_no = mp->is_no == 1;
3063   suppress = mp->suppress == 1;
3064   managed = mp->managed == 1;
3065   other = mp->other == 1;
3066   ll_option = mp->ll_option == 1;
3067   send_unicast = mp->send_unicast == 1;
3068   cease = mp->cease == 1;
3069   default_router = mp->default_router == 1;
3070
3071   VALIDATE_SW_IF_INDEX (mp);
3072
3073   rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
3074                                suppress, managed, other,
3075                                ll_option, send_unicast, cease,
3076                                default_router, ntohl (mp->lifetime),
3077                                ntohl (mp->initial_count),
3078                                ntohl (mp->initial_interval),
3079                                ntohl (mp->max_interval),
3080                                ntohl (mp->min_interval), is_no);
3081
3082   BAD_SW_IF_INDEX_LABEL;
3083
3084   REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
3085 }
3086
3087 static void
3088   vl_api_sw_interface_ip6nd_ra_prefix_t_handler
3089   (vl_api_sw_interface_ip6nd_ra_prefix_t * mp, vlib_main_t * vm)
3090 {
3091   vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
3092   int rv = 0;
3093   u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
3094
3095   VALIDATE_SW_IF_INDEX (mp);
3096
3097   is_no = mp->is_no == 1;
3098   use_default = mp->use_default == 1;
3099   no_advertise = mp->no_advertise == 1;
3100   off_link = mp->off_link == 1;
3101   no_autoconfig = mp->no_autoconfig == 1;
3102   no_onlink = mp->no_onlink == 1;
3103
3104   rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
3105                                (ip6_address_t *) mp->address,
3106                                mp->address_length, use_default,
3107                                ntohl (mp->val_lifetime),
3108                                ntohl (mp->pref_lifetime), no_advertise,
3109                                off_link, no_autoconfig, no_onlink, is_no);
3110
3111   BAD_SW_IF_INDEX_LABEL;
3112   REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
3113 }
3114
3115 static void
3116   vl_api_sw_interface_ip6_enable_disable_t_handler
3117   (vl_api_sw_interface_ip6_enable_disable_t * mp, vlib_main_t * vm)
3118 {
3119   vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
3120   vnet_main_t *vnm = vnet_get_main ();
3121   int rv = 0;
3122   clib_error_t *error;
3123
3124   vnm->api_errno = 0;
3125
3126   VALIDATE_SW_IF_INDEX (mp);
3127
3128   error =
3129     (mp->enable == 1) ? enable_ip6_interface (vm,
3130                                               ntohl (mp->sw_if_index)) :
3131     disable_ip6_interface (vm, ntohl (mp->sw_if_index));
3132
3133   if (error)
3134     {
3135       clib_error_report (error);
3136       rv = VNET_API_ERROR_UNSPECIFIED;
3137     }
3138   else
3139     {
3140       rv = vnm->api_errno;
3141     }
3142
3143   BAD_SW_IF_INDEX_LABEL;
3144
3145   REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
3146 }
3147
3148 static void
3149   vl_api_sw_interface_ip6_set_link_local_address_t_handler
3150   (vl_api_sw_interface_ip6_set_link_local_address_t * mp, vlib_main_t * vm)
3151 {
3152   vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
3153   int rv = 0;
3154   clib_error_t *error;
3155   vnet_main_t *vnm = vnet_get_main ();
3156
3157   vnm->api_errno = 0;
3158
3159   VALIDATE_SW_IF_INDEX (mp);
3160
3161   error = set_ip6_link_local_address (vm,
3162                                       ntohl (mp->sw_if_index),
3163                                       (ip6_address_t *) mp->address,
3164                                       mp->address_length);
3165   if (error)
3166     {
3167       clib_error_report (error);
3168       rv = VNET_API_ERROR_UNSPECIFIED;
3169     }
3170   else
3171     {
3172       rv = vnm->api_errno;
3173     }
3174
3175   BAD_SW_IF_INDEX_LABEL;
3176
3177   REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
3178 }
3179
3180 static void
3181 set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
3182 {
3183   vl_api_set_ip_flow_hash_reply_t *rmp;
3184   int rv = VNET_API_ERROR_UNIMPLEMENTED;
3185
3186   clib_warning ("unimplemented...");
3187
3188   REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
3189 }
3190
3191 static void
3192 set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
3193 {
3194   vl_api_set_ip_flow_hash_reply_t *rmp;
3195   int rv;
3196   u32 table_id;
3197   flow_hash_config_t flow_hash_config = 0;
3198
3199   table_id = ntohl (mp->vrf_id);
3200
3201 #define _(a,b) if (mp->a) flow_hash_config |= b;
3202   foreach_flow_hash_bit;
3203 #undef _
3204
3205   rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
3206
3207   REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
3208 }
3209
3210
3211 static void
3212 vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
3213 {
3214   if (mp->is_ipv6 == 0)
3215     set_ip4_flow_hash (mp);
3216   else
3217     set_ip6_flow_hash (mp);
3218 }
3219
3220 static void vl_api_sw_interface_set_unnumbered_t_handler
3221   (vl_api_sw_interface_set_unnumbered_t * mp)
3222 {
3223   vl_api_sw_interface_set_unnumbered_reply_t *rmp;
3224   int rv = 0;
3225   vnet_sw_interface_t *si;
3226   vnet_main_t *vnm = vnet_get_main ();
3227   u32 sw_if_index, unnumbered_sw_if_index;
3228
3229   sw_if_index = ntohl (mp->sw_if_index);
3230   unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
3231
3232   /*
3233    * The API message field names are backwards from
3234    * the underlying data structure names.
3235    * It's not worth changing them now.
3236    */
3237   if (pool_is_free_index (vnm->interface_main.sw_interfaces,
3238                           unnumbered_sw_if_index))
3239     {
3240       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
3241       goto done;
3242     }
3243
3244   /* Only check the "use loop0" field when setting the binding */
3245   if (mp->is_add &&
3246       pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
3247     {
3248       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
3249       goto done;
3250     }
3251
3252   si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
3253
3254   if (mp->is_add)
3255     {
3256       si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
3257       si->unnumbered_sw_if_index = sw_if_index;
3258       ip4_sw_interface_enable_disable (sw_if_index, 1);
3259       ip6_sw_interface_enable_disable (sw_if_index, 1);
3260     }
3261   else
3262     {
3263       si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
3264       si->unnumbered_sw_if_index = (u32) ~ 0;
3265       ip4_sw_interface_enable_disable (sw_if_index, 0);
3266       ip6_sw_interface_enable_disable (sw_if_index, 0);
3267     }
3268
3269 done:
3270   REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
3271 }
3272
3273 static void
3274 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
3275 {
3276   vl_api_create_loopback_reply_t *rmp;
3277   u32 sw_if_index;
3278   int rv;
3279
3280   rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address);
3281
3282   /* *INDENT-OFF* */
3283   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
3284   ({
3285     rmp->sw_if_index = ntohl (sw_if_index);
3286   }));
3287   /* *INDENT-ON* */
3288 }
3289
3290 static void
3291 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
3292 {
3293   vl_api_delete_loopback_reply_t *rmp;
3294   u32 sw_if_index;
3295   int rv;
3296
3297   sw_if_index = ntohl (mp->sw_if_index);
3298   rv = vnet_delete_loopback_interface (sw_if_index);
3299
3300   REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
3301 }
3302
3303 static void
3304 vl_api_control_ping_t_handler (vl_api_control_ping_t * mp)
3305 {
3306   vl_api_control_ping_reply_t *rmp;
3307   int rv = 0;
3308
3309   /* *INDENT-OFF* */
3310   REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
3311   ({
3312     rmp->vpe_pid = ntohl (getpid());
3313   }));
3314   /* *INDENT-ON* */
3315 }
3316
3317 static void
3318 shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
3319 {
3320   u8 **shmem_vecp = (u8 **) arg;
3321   u8 *shmem_vec;
3322   void *oldheap;
3323   api_main_t *am = &api_main;
3324   u32 offset;
3325
3326   shmem_vec = *shmem_vecp;
3327
3328   offset = vec_len (shmem_vec);
3329
3330   pthread_mutex_lock (&am->vlib_rp->mutex);
3331   oldheap = svm_push_data_heap (am->vlib_rp);
3332
3333   vec_validate (shmem_vec, offset + buffer_bytes - 1);
3334
3335   clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
3336
3337   svm_pop_heap (oldheap);
3338   pthread_mutex_unlock (&am->vlib_rp->mutex);
3339
3340   *shmem_vecp = shmem_vec;
3341 }
3342
3343
3344 static void
3345 vl_api_cli_request_t_handler (vl_api_cli_request_t * mp)
3346 {
3347   vl_api_cli_reply_t *rp;
3348   unix_shared_memory_queue_t *q;
3349   vlib_main_t *vm = vlib_get_main ();
3350   api_main_t *am = &api_main;
3351   unformat_input_t input;
3352   u8 *shmem_vec = 0;
3353   void *oldheap;
3354
3355   q = vl_api_client_index_to_input_queue (mp->client_index);
3356   if (!q)
3357     return;
3358
3359   rp = vl_msg_api_alloc (sizeof (*rp));
3360   rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY);
3361   rp->context = mp->context;
3362
3363   unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
3364
3365   vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
3366
3367   pthread_mutex_lock (&am->vlib_rp->mutex);
3368   oldheap = svm_push_data_heap (am->vlib_rp);
3369
3370   vec_add1 (shmem_vec, 0);
3371
3372   svm_pop_heap (oldheap);
3373   pthread_mutex_unlock (&am->vlib_rp->mutex);
3374
3375   rp->reply_in_shmem = (uword) shmem_vec;
3376
3377   vl_msg_api_send_shmem (q, (u8 *) & rp);
3378 }
3379
3380 static void
3381 inband_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
3382 {
3383   u8 **mem_vecp = (u8 **) arg;
3384   u8 *mem_vec = *mem_vecp;
3385   u32 offset = vec_len (mem_vec);
3386
3387   vec_validate (mem_vec, offset + buffer_bytes - 1);
3388   clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
3389   *mem_vecp = mem_vec;
3390 }
3391
3392 static void
3393 vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
3394 {
3395   vl_api_cli_inband_reply_t *rmp;
3396   int rv = 0;
3397   unix_shared_memory_queue_t *q;
3398   vlib_main_t *vm = vlib_get_main ();
3399   unformat_input_t input;
3400   u8 *out_vec = 0;
3401
3402   q = vl_api_client_index_to_input_queue (mp->client_index);
3403   if (!q)
3404     return;
3405
3406   unformat_init_string (&input, (char *) mp->cmd, ntohl (mp->length));
3407   vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
3408
3409   u32 len = vec_len (out_vec);
3410   /* *INDENT-OFF* */
3411   REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, len,
3412   ({
3413     rmp->length = htonl (len);
3414     clib_memcpy (rmp->reply, out_vec, len);
3415   }));
3416   /* *INDENT-ON* */
3417   vec_free (out_vec);
3418 }
3419
3420 static void
3421 vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
3422 {
3423   int rv;
3424   vl_api_set_arp_neighbor_limit_reply_t *rmp;
3425   vnet_main_t *vnm = vnet_get_main ();
3426   clib_error_t *error;
3427
3428   vnm->api_errno = 0;
3429
3430   if (mp->is_ipv6)
3431     error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
3432   else
3433     error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
3434
3435   if (error)
3436     {
3437       clib_error_report (error);
3438       rv = VNET_API_ERROR_UNSPECIFIED;
3439     }
3440   else
3441     {
3442       rv = vnm->api_errno;
3443     }
3444
3445   REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3446 }
3447
3448 static void vl_api_sr_tunnel_add_del_t_handler
3449   (vl_api_sr_tunnel_add_del_t * mp)
3450 {
3451 #if IP6SR == 0
3452   clib_warning ("unimplemented");
3453 #else
3454   ip6_sr_add_del_tunnel_args_t _a, *a = &_a;
3455   int rv = 0;
3456   vl_api_sr_tunnel_add_del_reply_t *rmp;
3457   ip6_address_t *segments = 0, *seg;
3458   ip6_address_t *tags = 0, *tag;
3459   ip6_address_t *this_address;
3460   int i;
3461
3462   if (mp->n_segments == 0)
3463     {
3464       rv = -11;
3465       goto out;
3466     }
3467
3468   memset (a, 0, sizeof (*a));
3469   a->src_address = (ip6_address_t *) & mp->src_address;
3470   a->dst_address = (ip6_address_t *) & mp->dst_address;
3471   a->dst_mask_width = mp->dst_mask_width;
3472   a->flags_net_byte_order = mp->flags_net_byte_order;
3473   a->is_del = (mp->is_add == 0);
3474   a->rx_table_id = ntohl (mp->outer_vrf_id);
3475   a->tx_table_id = ntohl (mp->inner_vrf_id);
3476
3477   a->name = format (0, "%s", mp->name);
3478   if (!(vec_len (a->name)))
3479     a->name = 0;
3480
3481   a->policy_name = format (0, "%s", mp->policy_name);
3482   if (!(vec_len (a->policy_name)))
3483     a->policy_name = 0;
3484
3485   /* Yank segments and tags out of the API message */
3486   this_address = (ip6_address_t *) mp->segs_and_tags;
3487   for (i = 0; i < mp->n_segments; i++)
3488     {
3489       vec_add2 (segments, seg, 1);
3490       clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
3491       this_address++;
3492     }
3493   for (i = 0; i < mp->n_tags; i++)
3494     {
3495       vec_add2 (tags, tag, 1);
3496       clib_memcpy (tag->as_u8, this_address->as_u8, sizeof (*this_address));
3497       this_address++;
3498     }
3499
3500   a->segments = segments;
3501   a->tags = tags;
3502
3503   rv = ip6_sr_add_del_tunnel (a);
3504
3505 out:
3506
3507   REPLY_MACRO (VL_API_SR_TUNNEL_ADD_DEL_REPLY);
3508 #endif
3509 }
3510
3511 static void vl_api_sr_policy_add_del_t_handler
3512   (vl_api_sr_policy_add_del_t * mp)
3513 {
3514 #if IP6SR == 0
3515   clib_warning ("unimplemented");
3516 #else
3517   ip6_sr_add_del_policy_args_t _a, *a = &_a;
3518   int rv = 0;
3519   vl_api_sr_policy_add_del_reply_t *rmp;
3520   int i;
3521
3522   memset (a, 0, sizeof (*a));
3523   a->is_del = (mp->is_add == 0);
3524
3525   a->name = format (0, "%s", mp->name);
3526   if (!(vec_len (a->name)))
3527     {
3528       rv = VNET_API_ERROR_NO_SUCH_NODE2;
3529       goto out;
3530     }
3531
3532   if (!(mp->tunnel_names[0]))
3533     {
3534       rv = VNET_API_ERROR_NO_SUCH_NODE2;
3535       goto out;
3536     }
3537
3538   // start deserializing tunnel_names
3539   int num_tunnels = mp->tunnel_names[0];        //number of tunnels
3540   u8 *deser_tun_names = mp->tunnel_names;
3541   deser_tun_names += 1;         //moving along
3542
3543   u8 *tun_name = 0;
3544   int tun_name_len = 0;
3545
3546   for (i = 0; i < num_tunnels; i++)
3547     {
3548       tun_name_len = *deser_tun_names;
3549       deser_tun_names += 1;
3550       vec_resize (tun_name, tun_name_len);
3551       memcpy (tun_name, deser_tun_names, tun_name_len);
3552       vec_add1 (a->tunnel_names, tun_name);
3553       deser_tun_names += tun_name_len;
3554       tun_name = 0;
3555     }
3556
3557   rv = ip6_sr_add_del_policy (a);
3558
3559 out:
3560
3561   REPLY_MACRO (VL_API_SR_POLICY_ADD_DEL_REPLY);
3562 #endif
3563 }
3564
3565 static void vl_api_sr_multicast_map_add_del_t_handler
3566   (vl_api_sr_multicast_map_add_del_t * mp)
3567 {
3568 #if IP6SR == 0
3569   clib_warning ("unimplemented");
3570 #else
3571   ip6_sr_add_del_multicastmap_args_t _a, *a = &_a;
3572   int rv = 0;
3573   vl_api_sr_multicast_map_add_del_reply_t *rmp;
3574
3575   memset (a, 0, sizeof (*a));
3576   a->is_del = (mp->is_add == 0);
3577
3578   a->multicast_address = (ip6_address_t *) & mp->multicast_address;
3579   a->policy_name = format (0, "%s", mp->policy_name);
3580
3581   if (a->multicast_address == 0)
3582     {
3583       rv = -1;
3584       goto out;
3585     }
3586
3587   if (!(a->policy_name))
3588     {
3589       rv = -2;
3590       goto out;
3591     }
3592
3593 #if DPDK > 0                    /* Cannot call replicate without DPDK */
3594   rv = ip6_sr_add_del_multicastmap (a);
3595 #else
3596   clib_warning ("multicast replication without DPDK not implemented");
3597   rv = VNET_API_ERROR_UNIMPLEMENTED;
3598 #endif /* DPDK */
3599
3600 out:
3601
3602   REPLY_MACRO (VL_API_SR_MULTICAST_MAP_ADD_DEL_REPLY);
3603 #endif
3604 }
3605
3606 #define foreach_classify_add_del_table_field    \
3607 _(table_index)                                  \
3608 _(nbuckets)                                     \
3609 _(memory_size)                                  \
3610 _(skip_n_vectors)                               \
3611 _(match_n_vectors)                              \
3612 _(next_table_index)                             \
3613 _(miss_next_index)
3614
3615 static void vl_api_classify_add_del_table_t_handler
3616   (vl_api_classify_add_del_table_t * mp)
3617 {
3618   vl_api_classify_add_del_table_reply_t *rmp;
3619   vnet_classify_main_t *cm = &vnet_classify_main;
3620   vnet_classify_table_t *t;
3621   int rv;
3622
3623 #define _(a) u32 a;
3624   foreach_classify_add_del_table_field;
3625 #undef _
3626
3627 #define _(a) a = ntohl(mp->a);
3628   foreach_classify_add_del_table_field;
3629 #undef _
3630
3631   /* The underlying API fails silently, on purpose, so check here */
3632   if (mp->is_add == 0)
3633     if (pool_is_free_index (cm->tables, table_index))
3634       {
3635         rv = VNET_API_ERROR_NO_SUCH_TABLE;
3636         goto out;
3637       }
3638
3639   rv = vnet_classify_add_del_table
3640     (cm, mp->mask, nbuckets, memory_size,
3641      skip_n_vectors, match_n_vectors,
3642      next_table_index, miss_next_index, &table_index, mp->is_add);
3643
3644 out:
3645   /* *INDENT-OFF* */
3646   REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
3647   ({
3648     if (rv == 0 && mp->is_add)
3649       {
3650         t = pool_elt_at_index (cm->tables, table_index);
3651         rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
3652         rmp->match_n_vectors = ntohl(t->match_n_vectors);
3653         rmp->new_table_index = ntohl(table_index);
3654       }
3655     else
3656       {
3657         rmp->skip_n_vectors = ~0;
3658         rmp->match_n_vectors = ~0;
3659         rmp->new_table_index = ~0;
3660       }
3661   }));
3662   /* *INDENT-ON* */
3663 }
3664
3665 static void vl_api_classify_add_del_session_t_handler
3666   (vl_api_classify_add_del_session_t * mp)
3667 {
3668   vnet_classify_main_t *cm = &vnet_classify_main;
3669   vl_api_classify_add_del_session_reply_t *rmp;
3670   int rv;
3671   u32 table_index, hit_next_index, opaque_index;
3672   i32 advance;
3673
3674   table_index = ntohl (mp->table_index);
3675   hit_next_index = ntohl (mp->hit_next_index);
3676   opaque_index = ntohl (mp->opaque_index);
3677   advance = ntohl (mp->advance);
3678
3679   rv = vnet_classify_add_del_session
3680     (cm, table_index, mp->match, hit_next_index, opaque_index,
3681      advance, mp->is_add);
3682
3683   REPLY_MACRO (VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
3684 }
3685
3686 static void vl_api_classify_set_interface_ip_table_t_handler
3687   (vl_api_classify_set_interface_ip_table_t * mp)
3688 {
3689   vlib_main_t *vm = vlib_get_main ();
3690   vl_api_classify_set_interface_ip_table_reply_t *rmp;
3691   int rv;
3692   u32 table_index, sw_if_index;
3693
3694   table_index = ntohl (mp->table_index);
3695   sw_if_index = ntohl (mp->sw_if_index);
3696
3697   VALIDATE_SW_IF_INDEX (mp);
3698
3699   if (mp->is_ipv6)
3700     rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3701   else
3702     rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
3703
3704   BAD_SW_IF_INDEX_LABEL;
3705
3706   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
3707 }
3708
3709 static void vl_api_classify_set_interface_l2_tables_t_handler
3710   (vl_api_classify_set_interface_l2_tables_t * mp)
3711 {
3712   vl_api_classify_set_interface_l2_tables_reply_t *rmp;
3713   int rv;
3714   u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
3715   int enable;
3716
3717   ip4_table_index = ntohl (mp->ip4_table_index);
3718   ip6_table_index = ntohl (mp->ip6_table_index);
3719   other_table_index = ntohl (mp->other_table_index);
3720   sw_if_index = ntohl (mp->sw_if_index);
3721
3722   VALIDATE_SW_IF_INDEX (mp);
3723
3724   if (mp->is_input)
3725     rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
3726                                             ip6_table_index,
3727                                             other_table_index);
3728   else
3729     rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
3730                                              ip6_table_index,
3731                                              other_table_index);
3732
3733   if (rv == 0)
3734     {
3735       if (ip4_table_index != ~0 || ip6_table_index != ~0
3736           || other_table_index != ~0)
3737         enable = 1;
3738       else
3739         enable = 0;
3740
3741       if (mp->is_input)
3742         vnet_l2_input_classify_enable_disable (sw_if_index, enable);
3743       else
3744         vnet_l2_output_classify_enable_disable (sw_if_index, enable);
3745     }
3746
3747   BAD_SW_IF_INDEX_LABEL;
3748
3749   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
3750 }
3751
3752 static void
3753 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
3754 {
3755   int rv = 0;
3756   vl_api_l2_fib_clear_table_reply_t *rmp;
3757
3758   /* DAW-FIXME: This API should only clear non-static l2fib entries, but
3759    *            that is not currently implemented.  When that TODO is fixed
3760    *            this call should be changed to pass 1 instead of 0.
3761    */
3762   l2fib_clear_table (0);
3763
3764   REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
3765 }
3766
3767 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
3768                                      u32 sw_if_index, u32 enable);
3769
3770 static void
3771 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
3772                                           mp)
3773 {
3774   int rv;
3775   vl_api_l2_interface_efp_filter_reply_t *rmp;
3776   vnet_main_t *vnm = vnet_get_main ();
3777
3778   // enable/disable the feature
3779   l2_efp_filter_configure (vnm, mp->sw_if_index, mp->enable_disable);
3780   rv = vnm->api_errno;
3781
3782   REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
3783 }
3784
3785 static void
3786   vl_api_l2_interface_vlan_tag_rewrite_t_handler
3787   (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
3788 {
3789   int rv = 0;
3790   vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
3791   vnet_main_t *vnm = vnet_get_main ();
3792   vlib_main_t *vm = vlib_get_main ();
3793   u32 vtr_op;
3794
3795   VALIDATE_SW_IF_INDEX (mp);
3796
3797   vtr_op = ntohl (mp->vtr_op);
3798
3799   /* The L2 code is unsuspicious */
3800   switch (vtr_op)
3801     {
3802     case L2_VTR_DISABLED:
3803     case L2_VTR_PUSH_1:
3804     case L2_VTR_PUSH_2:
3805     case L2_VTR_POP_1:
3806     case L2_VTR_POP_2:
3807     case L2_VTR_TRANSLATE_1_1:
3808     case L2_VTR_TRANSLATE_1_2:
3809     case L2_VTR_TRANSLATE_2_1:
3810     case L2_VTR_TRANSLATE_2_2:
3811       break;
3812
3813     default:
3814       rv = VNET_API_ERROR_INVALID_VALUE;
3815       goto bad_sw_if_index;
3816     }
3817
3818   rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
3819                         ntohl (mp->push_dot1q), ntohl (mp->tag1),
3820                         ntohl (mp->tag2));
3821
3822   BAD_SW_IF_INDEX_LABEL;
3823
3824   REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
3825 }
3826
3827 static void
3828 vl_api_create_vhost_user_if_t_handler (vl_api_create_vhost_user_if_t * mp)
3829 {
3830   int rv = 0;
3831   vl_api_create_vhost_user_if_reply_t *rmp;
3832   u32 sw_if_index = (u32) ~ 0;
3833
3834   vnet_main_t *vnm = vnet_get_main ();
3835   vlib_main_t *vm = vlib_get_main ();
3836
3837 #if DPDK > 0 && DPDK_VHOST_USER
3838   rv = dpdk_vhost_user_create_if (
3839 #else
3840   rv = vhost_user_create_if (
3841 #endif
3842                               vnm, vm, (char *) mp->sock_filename,
3843                               mp->is_server, &sw_if_index, (u64) ~ 0,
3844                               mp->renumber, ntohl (mp->custom_dev_instance),
3845                               (mp->use_custom_mac) ? mp->mac_address : NULL);
3846
3847   /* *INDENT-OFF* */
3848   REPLY_MACRO2(VL_API_CREATE_VHOST_USER_IF_REPLY,
3849   ({
3850     rmp->sw_if_index = ntohl (sw_if_index);
3851   }));
3852   /* *INDENT-ON* */
3853 }
3854
3855 static void
3856 vl_api_modify_vhost_user_if_t_handler (vl_api_modify_vhost_user_if_t * mp)
3857 {
3858   int rv = 0;
3859   vl_api_modify_vhost_user_if_reply_t *rmp;
3860   u32 sw_if_index = ntohl (mp->sw_if_index);
3861
3862   vnet_main_t *vnm = vnet_get_main ();
3863   vlib_main_t *vm = vlib_get_main ();
3864
3865 #if DPDK > 0 && DPDK_VHOST_USER
3866   rv = dpdk_vhost_user_modify_if (
3867 #else
3868   rv = vhost_user_modify_if (
3869 #endif
3870                               vnm, vm, (char *) mp->sock_filename,
3871                               mp->is_server, sw_if_index, (u64) ~ 0,
3872                               mp->renumber, ntohl (mp->custom_dev_instance));
3873   REPLY_MACRO (VL_API_MODIFY_VHOST_USER_IF_REPLY);
3874 }
3875
3876 static void
3877 vl_api_delete_vhost_user_if_t_handler (vl_api_delete_vhost_user_if_t * mp)
3878 {
3879   int rv = 0;
3880   vl_api_delete_vhost_user_if_reply_t *rmp;
3881   vpe_api_main_t *vam = &vpe_api_main;
3882   u32 sw_if_index = ntohl (mp->sw_if_index);
3883
3884   vnet_main_t *vnm = vnet_get_main ();
3885   vlib_main_t *vm = vlib_get_main ();
3886
3887 #if DPDK > 0 && DPDK_VHOST_USER
3888   rv = dpdk_vhost_user_delete_if (vnm, vm, sw_if_index);
3889 #else
3890   rv = vhost_user_delete_if (vnm, vm, sw_if_index);
3891 #endif
3892
3893   REPLY_MACRO (VL_API_DELETE_VHOST_USER_IF_REPLY);
3894   if (!rv)
3895     {
3896       unix_shared_memory_queue_t *q =
3897         vl_api_client_index_to_input_queue (mp->client_index);
3898       if (!q)
3899         return;
3900
3901       send_sw_interface_flags_deleted (vam, q, sw_if_index);
3902     }
3903 }
3904
3905 static void
3906   vl_api_sw_interface_vhost_user_details_t_handler
3907   (vl_api_sw_interface_vhost_user_details_t * mp)
3908 {
3909   clib_warning ("BUG");
3910 }
3911
3912 static void
3913 send_sw_interface_vhost_user_details (vpe_api_main_t * am,
3914                                       unix_shared_memory_queue_t * q,
3915                                       vhost_user_intf_details_t * vui,
3916                                       u32 context)
3917 {
3918   vl_api_sw_interface_vhost_user_details_t *mp;
3919
3920   mp = vl_msg_api_alloc (sizeof (*mp));
3921   memset (mp, 0, sizeof (*mp));
3922   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_VHOST_USER_DETAILS);
3923   mp->sw_if_index = ntohl (vui->sw_if_index);
3924   mp->virtio_net_hdr_sz = ntohl (vui->virtio_net_hdr_sz);
3925   mp->features = clib_net_to_host_u64 (vui->features);
3926   mp->is_server = vui->is_server;
3927   mp->num_regions = ntohl (vui->num_regions);
3928   mp->sock_errno = ntohl (vui->sock_errno);
3929   mp->context = context;
3930
3931   strncpy ((char *) mp->sock_filename,
3932            (char *) vui->sock_filename, ARRAY_LEN (mp->sock_filename) - 1);
3933   strncpy ((char *) mp->interface_name,
3934            (char *) vui->if_name, ARRAY_LEN (mp->interface_name) - 1);
3935
3936   vl_msg_api_send_shmem (q, (u8 *) & mp);
3937 }
3938
3939 static void
3940   vl_api_sw_interface_vhost_user_dump_t_handler
3941   (vl_api_sw_interface_vhost_user_dump_t * mp)
3942 {
3943   int rv = 0;
3944   vpe_api_main_t *am = &vpe_api_main;
3945   vnet_main_t *vnm = vnet_get_main ();
3946   vlib_main_t *vm = vlib_get_main ();
3947   vhost_user_intf_details_t *ifaces = NULL;
3948   vhost_user_intf_details_t *vuid = NULL;
3949   unix_shared_memory_queue_t *q;
3950
3951   q = vl_api_client_index_to_input_queue (mp->client_index);
3952   if (q == 0)
3953     return;
3954
3955 #if DPDK > 0 && DPDK_VHOST_USER
3956   rv = dpdk_vhost_user_dump_ifs (vnm, vm, &ifaces);
3957 #else
3958   rv = vhost_user_dump_ifs (vnm, vm, &ifaces);
3959 #endif
3960   if (rv)
3961     return;
3962
3963   vec_foreach (vuid, ifaces)
3964   {
3965     send_sw_interface_vhost_user_details (am, q, vuid, mp->context);
3966   }
3967   vec_free (ifaces);
3968 }
3969
3970 static void
3971 send_sw_if_l2tpv3_tunnel_details (vpe_api_main_t * am,
3972                                   unix_shared_memory_queue_t * q,
3973                                   l2t_session_t * s,
3974                                   l2t_main_t * lm, u32 context)
3975 {
3976   vl_api_sw_if_l2tpv3_tunnel_details_t *mp;
3977   u8 *if_name = NULL;
3978   vnet_sw_interface_t *si = NULL;
3979
3980   si = vnet_get_hw_sw_interface (lm->vnet_main, s->hw_if_index);
3981
3982   if_name = format (if_name, "%U",
3983                     format_vnet_sw_interface_name, lm->vnet_main, si);
3984
3985   mp = vl_msg_api_alloc (sizeof (*mp));
3986   memset (mp, 0, sizeof (*mp));
3987   mp->_vl_msg_id = ntohs (VL_API_SW_IF_L2TPV3_TUNNEL_DETAILS);
3988   strncpy ((char *) mp->interface_name,
3989            (char *) if_name, ARRAY_LEN (mp->interface_name) - 1);
3990   mp->sw_if_index = ntohl (si->sw_if_index);
3991   mp->local_session_id = s->local_session_id;
3992   mp->remote_session_id = s->remote_session_id;
3993   mp->local_cookie[0] = s->local_cookie[0];
3994   mp->local_cookie[1] = s->local_cookie[1];
3995   mp->remote_cookie = s->remote_cookie;
3996   clib_memcpy (mp->client_address, &s->client_address,
3997                sizeof (s->client_address));
3998   clib_memcpy (mp->our_address, &s->our_address, sizeof (s->our_address));
3999   mp->l2_sublayer_present = s->l2_sublayer_present;
4000   mp->context = context;
4001
4002   vl_msg_api_send_shmem (q, (u8 *) & mp);
4003 }
4004
4005 static void
4006 send_ip_address_details (vpe_api_main_t * am,
4007                          unix_shared_memory_queue_t * q,
4008                          u8 * ip, u16 prefix_length, u8 is_ipv6, u32 context)
4009 {
4010   vl_api_ip_address_details_t *mp;
4011
4012   mp = vl_msg_api_alloc (sizeof (*mp));
4013   memset (mp, 0, sizeof (*mp));
4014   mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
4015
4016   if (is_ipv6)
4017     {
4018       clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
4019     }
4020   else
4021     {
4022       u32 *tp = (u32 *) mp->ip;
4023       *tp = ntohl (*(u32 *) ip);
4024     }
4025   mp->prefix_length = prefix_length;
4026   mp->context = context;
4027
4028   vl_msg_api_send_shmem (q, (u8 *) & mp);
4029 }
4030
4031 static void
4032 vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
4033 {
4034   vpe_api_main_t *am = &vpe_api_main;
4035   unix_shared_memory_queue_t *q;
4036   ip6_address_t *r6;
4037   ip4_address_t *r4;
4038   ip6_main_t *im6 = &ip6_main;
4039   ip4_main_t *im4 = &ip4_main;
4040   ip_lookup_main_t *lm6 = &im6->lookup_main;
4041   ip_lookup_main_t *lm4 = &im4->lookup_main;
4042   ip_interface_address_t *ia = 0;
4043   u32 sw_if_index = ~0;
4044   int rv __attribute__ ((unused)) = 0;
4045
4046   VALIDATE_SW_IF_INDEX (mp);
4047
4048   sw_if_index = ntohl (mp->sw_if_index);
4049
4050   q = vl_api_client_index_to_input_queue (mp->client_index);
4051   if (q == 0)
4052     {
4053       return;
4054     }
4055
4056   if (mp->is_ipv6)
4057     {
4058       /* *INDENT-OFF* */
4059       foreach_ip_interface_address (lm6, ia, sw_if_index,
4060                                     1 /* honor unnumbered */,
4061       ({
4062         r6 = ip_interface_address_get_address (lm6, ia);
4063         u16 prefix_length = ia->address_length;
4064         send_ip_address_details(am, q, (u8*)r6, prefix_length, 1, mp->context);
4065       }));
4066       /* *INDENT-ON* */
4067     }
4068   else
4069     {
4070       /* *INDENT-OFF* */
4071       foreach_ip_interface_address (lm4, ia, sw_if_index,
4072                                     1 /* honor unnumbered */,
4073       ({
4074         r4 = ip_interface_address_get_address (lm4, ia);
4075         u16 prefix_length = ia->address_length;
4076         send_ip_address_details(am, q, (u8*)r4, prefix_length, 0, mp->context);
4077       }));
4078       /* *INDENT-ON* */
4079     }
4080   BAD_SW_IF_INDEX_LABEL;
4081 }
4082
4083 static void
4084 send_ip_details (vpe_api_main_t * am,
4085                  unix_shared_memory_queue_t * q, u32 sw_if_index, u32 context)
4086 {
4087   vl_api_ip_details_t *mp;
4088
4089   mp = vl_msg_api_alloc (sizeof (*mp));
4090   memset (mp, 0, sizeof (*mp));
4091   mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
4092
4093   mp->sw_if_index = ntohl (sw_if_index);
4094   mp->context = context;
4095
4096   vl_msg_api_send_shmem (q, (u8 *) & mp);
4097 }
4098
4099 static void
4100 vl_api_sw_if_l2tpv3_tunnel_dump_t_handler (vl_api_sw_if_l2tpv3_tunnel_dump_t *
4101                                            mp)
4102 {
4103   vpe_api_main_t *am = &vpe_api_main;
4104   l2t_main_t *lm = &l2t_main;
4105   unix_shared_memory_queue_t *q;
4106   l2t_session_t *session;
4107
4108   q = vl_api_client_index_to_input_queue (mp->client_index);
4109   if (q == 0)
4110     return;
4111
4112   /* *INDENT-OFF* */
4113   pool_foreach (session, lm->sessions,
4114   ({
4115     send_sw_if_l2tpv3_tunnel_details (am, q, session, lm, mp->context);
4116   }));
4117   /* *INDENT-ON* */
4118 }
4119
4120
4121 static void
4122 send_sw_interface_tap_details (vpe_api_main_t * am,
4123                                unix_shared_memory_queue_t * q,
4124                                tapcli_interface_details_t * tap_if,
4125                                u32 context)
4126 {
4127   vl_api_sw_interface_tap_details_t *mp;
4128   mp = vl_msg_api_alloc (sizeof (*mp));
4129   memset (mp, 0, sizeof (*mp));
4130   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_TAP_DETAILS);
4131   mp->sw_if_index = ntohl (tap_if->sw_if_index);
4132   strncpy ((char *) mp->dev_name,
4133            (char *) tap_if->dev_name, ARRAY_LEN (mp->dev_name) - 1);
4134   mp->context = context;
4135
4136   vl_msg_api_send_shmem (q, (u8 *) & mp);
4137 }
4138
4139 static void
4140 vl_api_sw_interface_tap_dump_t_handler (vl_api_sw_interface_tap_dump_t * mp)
4141 {
4142   int rv = 0;
4143   vpe_api_main_t *am = &vpe_api_main;
4144   unix_shared_memory_queue_t *q;
4145   tapcli_interface_details_t *tapifs = NULL;
4146   tapcli_interface_details_t *tap_if = NULL;
4147
4148   q = vl_api_client_index_to_input_queue (mp->client_index);
4149   if (q == 0)
4150     return;
4151
4152   rv = vnet_tap_dump_ifs (&tapifs);
4153   if (rv)
4154     return;
4155
4156   vec_foreach (tap_if, tapifs)
4157   {
4158     send_sw_interface_tap_details (am, q, tap_if, mp->context);
4159   }
4160
4161   vec_free (tapifs);
4162 }
4163
4164 static void
4165 vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
4166 {
4167   vpe_api_main_t *am = &vpe_api_main;
4168   vnet_main_t *vnm = vnet_get_main ();
4169   vlib_main_t *vm = vlib_get_main ();
4170   vnet_interface_main_t *im = &vnm->interface_main;
4171   unix_shared_memory_queue_t *q;
4172   vnet_sw_interface_t *si, *sorted_sis;
4173   u32 sw_if_index = ~0;
4174
4175   q = vl_api_client_index_to_input_queue (mp->client_index);
4176   if (q == 0)
4177     {
4178       return;
4179     }
4180
4181   /* Gather interfaces. */
4182   sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
4183   _vec_len (sorted_sis) = 0;
4184   /* *INDENT-OFF* */
4185   pool_foreach (si, im->sw_interfaces,
4186   ({
4187     vec_add1 (sorted_sis, si[0]);
4188   }));
4189   /* *INDENT-ON* */
4190
4191   vec_foreach (si, sorted_sis)
4192   {
4193     if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
4194       {
4195         if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
4196           {
4197             continue;
4198           }
4199         sw_if_index = si->sw_if_index;
4200         send_ip_details (am, q, sw_if_index, mp->context);
4201       }
4202   }
4203 }
4204
4205 static void
4206 vl_api_l2_fib_table_entry_t_handler (vl_api_l2_fib_table_entry_t * mp)
4207 {
4208   clib_warning ("BUG");
4209 }
4210
4211 static void
4212 send_l2fib_table_entry (vpe_api_main_t * am,
4213                         unix_shared_memory_queue_t * q,
4214                         l2fib_entry_key_t * l2fe_key,
4215                         l2fib_entry_result_t * l2fe_res, u32 context)
4216 {
4217   vl_api_l2_fib_table_entry_t *mp;
4218
4219   mp = vl_msg_api_alloc (sizeof (*mp));
4220   memset (mp, 0, sizeof (*mp));
4221   mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_ENTRY);
4222
4223   mp->bd_id =
4224     ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
4225
4226   mp->mac = l2fib_make_key (l2fe_key->fields.mac, 0);
4227   mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
4228   mp->static_mac = l2fe_res->fields.static_mac;
4229   mp->filter_mac = l2fe_res->fields.filter;
4230   mp->bvi_mac = l2fe_res->fields.bvi;
4231   mp->context = context;
4232
4233   vl_msg_api_send_shmem (q, (u8 *) & mp);
4234 }
4235
4236 static void
4237 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
4238 {
4239   vpe_api_main_t *am = &vpe_api_main;
4240   bd_main_t *bdm = &bd_main;
4241   l2fib_entry_key_t *l2fe_key = NULL;
4242   l2fib_entry_result_t *l2fe_res = NULL;
4243   u32 ni, bd_id = ntohl (mp->bd_id);
4244   u32 bd_index;
4245   unix_shared_memory_queue_t *q;
4246   uword *p;
4247
4248   q = vl_api_client_index_to_input_queue (mp->client_index);
4249   if (q == 0)
4250     return;
4251
4252   /* see l2fib_table_dump: ~0 means "any" */
4253   if (bd_id == ~0)
4254     bd_index = ~0;
4255   else
4256     {
4257       p = hash_get (bdm->bd_index_by_bd_id, bd_id);
4258       if (p == 0)
4259         return;
4260
4261       bd_index = p[0];
4262     }
4263
4264   l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
4265
4266   vec_foreach_index (ni, l2fe_key)
4267   {
4268     send_l2fib_table_entry (am, q, vec_elt_at_index (l2fe_key, ni),
4269                             vec_elt_at_index (l2fe_res, ni), mp->context);
4270   }
4271   vec_free (l2fe_key);
4272   vec_free (l2fe_res);
4273 }
4274
4275 static void
4276 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
4277 {
4278   vl_api_show_version_reply_t *rmp;
4279   int rv = 0;
4280   char *vpe_api_get_build_directory (void);
4281   char *vpe_api_get_version (void);
4282   char *vpe_api_get_build_date (void);
4283
4284   unix_shared_memory_queue_t *q =
4285     vl_api_client_index_to_input_queue (mp->client_index);
4286
4287   if (!q)
4288     return;
4289
4290   /* *INDENT-OFF* */
4291   REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
4292   ({
4293     strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
4294     strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
4295              ARRAY_LEN(rmp->build_directory)-1);
4296     strncpy ((char *) rmp->version, vpe_api_get_version(),
4297              ARRAY_LEN(rmp->version)-1);
4298     strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
4299              ARRAY_LEN(rmp->build_date)-1);
4300   }));
4301   /* *INDENT-ON* */
4302 }
4303
4304 static void
4305 vl_api_get_node_index_t_handler (vl_api_get_node_index_t * mp)
4306 {
4307   vlib_main_t *vm = vlib_get_main ();
4308   vl_api_get_node_index_reply_t *rmp;
4309   vlib_node_t *n;
4310   int rv = 0;
4311   u32 node_index = ~0;
4312
4313   n = vlib_get_node_by_name (vm, mp->node_name);
4314
4315   if (n == 0)
4316     rv = VNET_API_ERROR_NO_SUCH_NODE;
4317   else
4318     node_index = n->index;
4319
4320   /* *INDENT-OFF* */
4321   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
4322   ({
4323     rmp->node_index = ntohl(node_index);
4324   }));
4325   /* *INDENT-ON* */
4326 }
4327
4328 static void
4329 vl_api_get_next_index_t_handler (vl_api_get_next_index_t * mp)
4330 {
4331   vlib_main_t *vm = vlib_get_main ();
4332   vl_api_get_next_index_reply_t *rmp;
4333   vlib_node_t *node, *next_node;
4334   int rv = 0;
4335   u32 next_node_index = ~0, next_index = ~0;
4336   uword *p;
4337
4338   node = vlib_get_node_by_name (vm, mp->node_name);
4339
4340   if (node == 0)
4341     {
4342       rv = VNET_API_ERROR_NO_SUCH_NODE;
4343       goto out;
4344     }
4345
4346   next_node = vlib_get_node_by_name (vm, mp->next_name);
4347
4348   if (next_node == 0)
4349     {
4350       rv = VNET_API_ERROR_NO_SUCH_NODE2;
4351       goto out;
4352     }
4353   else
4354     next_node_index = next_node->index;
4355
4356   p = hash_get (node->next_slot_by_node, next_node_index);
4357
4358   if (p == 0)
4359     {
4360       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
4361       goto out;
4362     }
4363   else
4364     next_index = p[0];
4365
4366 out:
4367   /* *INDENT-OFF* */
4368   REPLY_MACRO2(VL_API_GET_NEXT_INDEX_REPLY,
4369   ({
4370     rmp->next_index = ntohl(next_index);
4371   }));
4372   /* *INDENT-ON* */
4373 }
4374
4375 static void
4376 vl_api_add_node_next_t_handler (vl_api_add_node_next_t * mp)
4377 {
4378   vlib_main_t *vm = vlib_get_main ();
4379   vl_api_add_node_next_reply_t *rmp;
4380   vlib_node_t *n, *next;
4381   int rv = 0;
4382   u32 next_index = ~0;
4383
4384   n = vlib_get_node_by_name (vm, mp->node_name);
4385
4386   if (n == 0)
4387     {
4388       rv = VNET_API_ERROR_NO_SUCH_NODE;
4389       goto out;
4390     }
4391
4392   next = vlib_get_node_by_name (vm, mp->next_name);
4393
4394   if (next == 0)
4395     rv = VNET_API_ERROR_NO_SUCH_NODE2;
4396   else
4397     next_index = vlib_node_add_next (vm, n->index, next->index);
4398
4399 out:
4400   /* *INDENT-OFF* */
4401   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
4402   ({
4403     rmp->next_index = ntohl(next_index);
4404   }));
4405   /* *INDENT-ON* */
4406 }
4407
4408 static void vl_api_l2tpv3_create_tunnel_t_handler
4409   (vl_api_l2tpv3_create_tunnel_t * mp)
4410 {
4411   vl_api_l2tpv3_create_tunnel_reply_t *rmp;
4412   l2t_main_t *lm = &l2t_main;
4413   u32 sw_if_index = (u32) ~ 0;
4414   int rv;
4415
4416   if (mp->is_ipv6 != 1)
4417     {
4418       rv = VNET_API_ERROR_UNIMPLEMENTED;
4419       goto out;
4420     }
4421
4422   u32 encap_fib_index;
4423
4424   if (mp->encap_vrf_id != ~0)
4425     {
4426       uword *p;
4427       ip6_main_t *im = &ip6_main;
4428       if (!
4429           (p =
4430            hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id))))
4431         {
4432           rv = VNET_API_ERROR_NO_SUCH_FIB;
4433           goto out;
4434         }
4435       encap_fib_index = p[0];
4436     }
4437   else
4438     {
4439       encap_fib_index = ~0;
4440     }
4441
4442   rv = create_l2tpv3_ipv6_tunnel (lm,
4443                                   (ip6_address_t *) mp->client_address,
4444                                   (ip6_address_t *) mp->our_address,
4445                                   ntohl (mp->local_session_id),
4446                                   ntohl (mp->remote_session_id),
4447                                   clib_net_to_host_u64 (mp->local_cookie),
4448                                   clib_net_to_host_u64 (mp->remote_cookie),
4449                                   mp->l2_sublayer_present,
4450                                   encap_fib_index, &sw_if_index);
4451
4452 out:
4453   /* *INDENT-OFF* */
4454   REPLY_MACRO2(VL_API_L2TPV3_CREATE_TUNNEL_REPLY,
4455   ({
4456     rmp->sw_if_index = ntohl (sw_if_index);
4457   }));
4458   /* *INDENT-ON* */
4459 }
4460
4461 static void vl_api_l2tpv3_set_tunnel_cookies_t_handler
4462   (vl_api_l2tpv3_set_tunnel_cookies_t * mp)
4463 {
4464   vl_api_l2tpv3_set_tunnel_cookies_reply_t *rmp;
4465   l2t_main_t *lm = &l2t_main;
4466   int rv;
4467
4468   VALIDATE_SW_IF_INDEX (mp);
4469
4470   rv = l2tpv3_set_tunnel_cookies (lm, ntohl (mp->sw_if_index),
4471                                   clib_net_to_host_u64 (mp->new_local_cookie),
4472                                   clib_net_to_host_u64
4473                                   (mp->new_remote_cookie));
4474
4475   BAD_SW_IF_INDEX_LABEL;
4476
4477   REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY);
4478 }
4479
4480 static void vl_api_l2tpv3_interface_enable_disable_t_handler
4481   (vl_api_l2tpv3_interface_enable_disable_t * mp)
4482 {
4483   int rv;
4484   vnet_main_t *vnm = vnet_get_main ();
4485   vl_api_l2tpv3_interface_enable_disable_reply_t *rmp;
4486
4487   VALIDATE_SW_IF_INDEX (mp);
4488
4489   rv = l2tpv3_interface_enable_disable
4490     (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
4491
4492   BAD_SW_IF_INDEX_LABEL;
4493
4494   REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY);
4495 }
4496
4497 static void vl_api_l2tpv3_set_lookup_key_t_handler
4498   (vl_api_l2tpv3_set_lookup_key_t * mp)
4499 {
4500   int rv = 0;
4501   l2t_main_t *lm = &l2t_main;
4502   vl_api_l2tpv3_set_lookup_key_reply_t *rmp;
4503
4504   if (mp->key > L2T_LOOKUP_SESSION_ID)
4505     {
4506       rv = VNET_API_ERROR_INVALID_VALUE;
4507       goto out;
4508     }
4509
4510   lm->lookup_type = mp->key;
4511
4512 out:
4513   REPLY_MACRO (VL_API_L2TPV3_SET_LOOKUP_KEY_REPLY);
4514 }
4515
4516 static void vl_api_vxlan_add_del_tunnel_t_handler
4517   (vl_api_vxlan_add_del_tunnel_t * mp)
4518 {
4519   vl_api_vxlan_add_del_tunnel_reply_t *rmp;
4520   int rv = 0;
4521   vnet_vxlan_add_del_tunnel_args_t _a, *a = &_a;
4522   u32 encap_fib_index;
4523   uword *p;
4524   ip4_main_t *im = &ip4_main;
4525   u32 sw_if_index = ~0;
4526
4527   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
4528   if (!p)
4529     {
4530       rv = VNET_API_ERROR_NO_SUCH_FIB;
4531       goto out;
4532     }
4533   encap_fib_index = p[0];
4534
4535   /* Check src & dst are different */
4536   if ((mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 16) == 0) ||
4537       (!mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 4) == 0))
4538     {
4539       rv = VNET_API_ERROR_SAME_SRC_DST;
4540       goto out;
4541     }
4542   memset (a, 0, sizeof (*a));
4543
4544   a->is_add = mp->is_add;
4545   a->is_ip6 = mp->is_ipv6;
4546
4547   /* ip addresses sent in network byte order */
4548   if (a->is_ip6)
4549     {
4550       memcpy (&(a->src.ip6), mp->src_address, 16);
4551       memcpy (&(a->dst.ip6), mp->dst_address, 16);
4552     }
4553   else
4554     {
4555       memcpy (&(a->src.ip4), mp->src_address, 4);
4556       memcpy (&(a->dst.ip4), mp->dst_address, 4);
4557     }
4558
4559   a->encap_fib_index = encap_fib_index;
4560   a->decap_next_index = ntohl (mp->decap_next_index);
4561   a->vni = ntohl (mp->vni);
4562   rv = vnet_vxlan_add_del_tunnel (a, &sw_if_index);
4563
4564 out:
4565   /* *INDENT-OFF* */
4566   REPLY_MACRO2(VL_API_VXLAN_ADD_DEL_TUNNEL_REPLY,
4567   ({
4568     rmp->sw_if_index = ntohl (sw_if_index);
4569   }));
4570   /* *INDENT-ON* */
4571 }
4572
4573 static void send_vxlan_tunnel_details
4574   (vxlan_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4575 {
4576   vl_api_vxlan_tunnel_details_t *rmp;
4577   ip4_main_t *im4 = &ip4_main;
4578   ip6_main_t *im6 = &ip6_main;
4579   u8 is_ipv6 = !(t->flags & VXLAN_TUNNEL_IS_IPV4);
4580
4581   rmp = vl_msg_api_alloc (sizeof (*rmp));
4582   memset (rmp, 0, sizeof (*rmp));
4583   rmp->_vl_msg_id = ntohs (VL_API_VXLAN_TUNNEL_DETAILS);
4584   if (is_ipv6)
4585     {
4586       memcpy (rmp->src_address, &(t->src.ip6), 16);
4587       memcpy (rmp->dst_address, &(t->dst.ip6), 16);
4588       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
4589     }
4590   else
4591     {
4592       memcpy (rmp->src_address, &(t->src.ip4), 4);
4593       memcpy (rmp->dst_address, &(t->dst.ip4), 4);
4594       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
4595     }
4596   rmp->vni = htonl (t->vni);
4597   rmp->decap_next_index = htonl (t->decap_next_index);
4598   rmp->sw_if_index = htonl (t->sw_if_index);
4599   rmp->is_ipv6 = is_ipv6;
4600   rmp->context = context;
4601
4602   vl_msg_api_send_shmem (q, (u8 *) & rmp);
4603 }
4604
4605 static void vl_api_vxlan_tunnel_dump_t_handler
4606   (vl_api_vxlan_tunnel_dump_t * mp)
4607 {
4608   unix_shared_memory_queue_t *q;
4609   vxlan_main_t *vxm = &vxlan_main;
4610   vxlan_tunnel_t *t;
4611   u32 sw_if_index;
4612
4613   q = vl_api_client_index_to_input_queue (mp->client_index);
4614   if (q == 0)
4615     {
4616       return;
4617     }
4618
4619   sw_if_index = ntohl (mp->sw_if_index);
4620
4621   if (~0 == sw_if_index)
4622     {
4623       /* *INDENT-OFF* */
4624       pool_foreach (t, vxm->tunnels,
4625       ({
4626         send_vxlan_tunnel_details(t, q, mp->context);
4627       }));
4628       /* *INDENT-ON* */
4629     }
4630   else
4631     {
4632       if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) ||
4633           (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index]))
4634         {
4635           return;
4636         }
4637       t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
4638       send_vxlan_tunnel_details (t, q, mp->context);
4639     }
4640 }
4641
4642 static void vl_api_gre_add_del_tunnel_t_handler
4643   (vl_api_gre_add_del_tunnel_t * mp)
4644 {
4645   vl_api_gre_add_del_tunnel_reply_t *rmp;
4646   int rv = 0;
4647   vnet_gre_add_del_tunnel_args_t _a, *a = &_a;
4648   u32 outer_fib_id;
4649   uword *p;
4650   ip4_main_t *im = &ip4_main;
4651   u32 sw_if_index = ~0;
4652
4653   p = hash_get (im->fib_index_by_table_id, ntohl (mp->outer_fib_id));
4654   if (!p)
4655     {
4656       rv = VNET_API_ERROR_NO_SUCH_FIB;
4657       goto out;
4658     }
4659   outer_fib_id = p[0];
4660
4661   /* Check src & dst are different */
4662   if ((mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 16) == 0) ||
4663       (!mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 4) == 0))
4664     {
4665       rv = VNET_API_ERROR_SAME_SRC_DST;
4666       goto out;
4667     }
4668   memset (a, 0, sizeof (*a));
4669
4670   a->is_add = mp->is_add;
4671   a->teb = mp->teb;
4672
4673   /* ip addresses sent in network byte order */
4674   clib_memcpy (&(a->src), mp->src_address, 4);
4675   clib_memcpy (&(a->dst), mp->dst_address, 4);
4676
4677   a->outer_fib_id = outer_fib_id;
4678   rv = vnet_gre_add_del_tunnel (a, &sw_if_index);
4679
4680 out:
4681   /* *INDENT-OFF* */
4682   REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY,
4683   ({
4684     rmp->sw_if_index = ntohl (sw_if_index);
4685   }));
4686   /* *INDENT-ON* */
4687 }
4688
4689 static void send_gre_tunnel_details
4690   (gre_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4691 {
4692   vl_api_gre_tunnel_details_t *rmp;
4693   ip4_main_t *im = &ip4_main;
4694
4695   rmp = vl_msg_api_alloc (sizeof (*rmp));
4696   memset (rmp, 0, sizeof (*rmp));
4697   rmp->_vl_msg_id = ntohs (VL_API_GRE_TUNNEL_DETAILS);
4698   clib_memcpy (rmp->src_address, &(t->tunnel_src), 4);
4699   clib_memcpy (rmp->dst_address, &(t->tunnel_dst), 4);
4700   rmp->outer_fib_id = htonl (im->fibs[t->outer_fib_index].ft_table_id);
4701   rmp->teb = t->teb;
4702   rmp->sw_if_index = htonl (t->sw_if_index);
4703   rmp->context = context;
4704
4705   vl_msg_api_send_shmem (q, (u8 *) & rmp);
4706 }
4707
4708 static void
4709 vl_api_gre_tunnel_dump_t_handler (vl_api_gre_tunnel_dump_t * mp)
4710 {
4711   unix_shared_memory_queue_t *q;
4712   gre_main_t *gm = &gre_main;
4713   gre_tunnel_t *t;
4714   u32 sw_if_index;
4715
4716   q = vl_api_client_index_to_input_queue (mp->client_index);
4717   if (q == 0)
4718     {
4719       return;
4720     }
4721
4722   sw_if_index = ntohl (mp->sw_if_index);
4723
4724   if (~0 == sw_if_index)
4725     {
4726       /* *INDENT-OFF* */
4727       pool_foreach (t, gm->tunnels,
4728       ({
4729         send_gre_tunnel_details(t, q, mp->context);
4730       }));
4731       /* *INDENT-ON* */
4732     }
4733   else
4734     {
4735       if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) ||
4736           (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
4737         {
4738           return;
4739         }
4740       t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
4741       send_gre_tunnel_details (t, q, mp->context);
4742     }
4743 }
4744
4745 static void
4746 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
4747 {
4748   extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
4749                                     int is_add);
4750   vl_api_l2_patch_add_del_reply_t *rmp;
4751   int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
4752                              int is_add);
4753   int rv = 0;
4754
4755   VALIDATE_RX_SW_IF_INDEX (mp);
4756   VALIDATE_TX_SW_IF_INDEX (mp);
4757
4758   rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
4759                               ntohl (mp->tx_sw_if_index),
4760                               (int) (mp->is_add != 0));
4761
4762   BAD_RX_SW_IF_INDEX_LABEL;
4763   BAD_TX_SW_IF_INDEX_LABEL;
4764
4765   REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
4766 }
4767
4768 static void
4769   vl_api_vxlan_gpe_add_del_tunnel_t_handler
4770   (vl_api_vxlan_gpe_add_del_tunnel_t * mp)
4771 {
4772   vl_api_vxlan_gpe_add_del_tunnel_reply_t *rmp;
4773   int rv = 0;
4774   vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
4775   u32 encap_fib_index, decap_fib_index;
4776   u8 protocol;
4777   uword *p;
4778   ip4_main_t *im = &ip4_main;
4779   u32 sw_if_index = ~0;
4780
4781
4782   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
4783   if (!p)
4784     {
4785       rv = VNET_API_ERROR_NO_SUCH_FIB;
4786       goto out;
4787     }
4788   encap_fib_index = p[0];
4789
4790   protocol = mp->protocol;
4791
4792   /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
4793   if (protocol == VXLAN_GPE_INPUT_NEXT_IP4_INPUT)
4794     {
4795       p = hash_get (im->fib_index_by_table_id, ntohl (mp->decap_vrf_id));
4796       if (!p)
4797         {
4798           rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
4799           goto out;
4800         }
4801       decap_fib_index = p[0];
4802     }
4803   else
4804     {
4805       decap_fib_index = ntohl (mp->decap_vrf_id);
4806     }
4807
4808   /* Check src & dst are different */
4809   if ((mp->is_ipv6 && memcmp (mp->local, mp->remote, 16) == 0) ||
4810       (!mp->is_ipv6 && memcmp (mp->local, mp->remote, 4) == 0))
4811     {
4812       rv = VNET_API_ERROR_SAME_SRC_DST;
4813       goto out;
4814     }
4815   memset (a, 0, sizeof (*a));
4816
4817   a->is_add = mp->is_add;
4818   a->is_ip6 = mp->is_ipv6;
4819   /* ip addresses sent in network byte order */
4820   if (a->is_ip6)
4821     {
4822       clib_memcpy (&(a->local.ip6), mp->local, 16);
4823       clib_memcpy (&(a->remote.ip6), mp->remote, 16);
4824     }
4825   else
4826     {
4827       clib_memcpy (&(a->local.ip4), mp->local, 4);
4828       clib_memcpy (&(a->remote.ip4), mp->remote, 4);
4829     }
4830   a->encap_fib_index = encap_fib_index;
4831   a->decap_fib_index = decap_fib_index;
4832   a->protocol = protocol;
4833   a->vni = ntohl (mp->vni);
4834   rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
4835
4836 out:
4837   /* *INDENT-OFF* */
4838   REPLY_MACRO2(VL_API_VXLAN_GPE_ADD_DEL_TUNNEL_REPLY,
4839   ({
4840     rmp->sw_if_index = ntohl (sw_if_index);
4841   }));
4842   /* *INDENT-ON* */
4843 }
4844
4845 static void send_vxlan_gpe_tunnel_details
4846   (vxlan_gpe_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4847 {
4848   vl_api_vxlan_gpe_tunnel_details_t *rmp;
4849   ip4_main_t *im4 = &ip4_main;
4850   ip6_main_t *im6 = &ip6_main;
4851   u8 is_ipv6 = !(t->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
4852
4853   rmp = vl_msg_api_alloc (sizeof (*rmp));
4854   memset (rmp, 0, sizeof (*rmp));
4855   rmp->_vl_msg_id = ntohs (VL_API_VXLAN_GPE_TUNNEL_DETAILS);
4856   if (is_ipv6)
4857     {
4858       memcpy (rmp->local, &(t->local.ip6), 16);
4859       memcpy (rmp->remote, &(t->remote.ip6), 16);
4860       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
4861       rmp->decap_vrf_id = htonl (im6->fibs[t->decap_fib_index].ft_table_id);
4862     }
4863   else
4864     {
4865       memcpy (rmp->local, &(t->local.ip4), 4);
4866       memcpy (rmp->remote, &(t->remote.ip4), 4);
4867       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
4868       rmp->decap_vrf_id = htonl (im4->fibs[t->decap_fib_index].ft_table_id);
4869     }
4870   rmp->vni = htonl (t->vni);
4871   rmp->protocol = t->protocol;
4872   rmp->sw_if_index = htonl (t->sw_if_index);
4873   rmp->is_ipv6 = is_ipv6;
4874   rmp->context = context;
4875
4876   vl_msg_api_send_shmem (q, (u8 *) & rmp);
4877 }
4878
4879 static void vl_api_vxlan_gpe_tunnel_dump_t_handler
4880   (vl_api_vxlan_gpe_tunnel_dump_t * mp)
4881 {
4882   unix_shared_memory_queue_t *q;
4883   vxlan_gpe_main_t *vgm = &vxlan_gpe_main;
4884   vxlan_gpe_tunnel_t *t;
4885   u32 sw_if_index;
4886
4887   q = vl_api_client_index_to_input_queue (mp->client_index);
4888   if (q == 0)
4889     {
4890       return;
4891     }
4892
4893   sw_if_index = ntohl (mp->sw_if_index);
4894
4895   if (~0 == sw_if_index)
4896     {
4897       /* *INDENT-OFF* */
4898       pool_foreach (t, vgm->tunnels,
4899       ({
4900         send_vxlan_gpe_tunnel_details(t, q, mp->context);
4901       }));
4902       /* *INDENT-ON* */
4903     }
4904   else
4905     {
4906       if ((sw_if_index >= vec_len (vgm->tunnel_index_by_sw_if_index)) ||
4907           (~0 == vgm->tunnel_index_by_sw_if_index[sw_if_index]))
4908         {
4909           return;
4910         }
4911       t = &vgm->tunnels[vgm->tunnel_index_by_sw_if_index[sw_if_index]];
4912       send_vxlan_gpe_tunnel_details (t, q, mp->context);
4913     }
4914 }
4915
4916 /** Used for transferring locators via VPP API */
4917 /* *INDENT-OFF* */
4918 typedef CLIB_PACKED (struct {
4919   u32 sw_if_index; /**< locator sw_if_index */
4920   u8 priority; /**< locator priority */
4921   u8 weight; /**< locator weight */
4922 }) ls_locator_t;
4923 /* *INDENT-ON* */
4924
4925 static void
4926 vl_api_lisp_add_del_locator_set_t_handler (vl_api_lisp_add_del_locator_set_t *
4927                                            mp)
4928 {
4929   vl_api_lisp_add_del_locator_set_reply_t *rmp;
4930   int rv = 0;
4931   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
4932   locator_t locator;
4933   ls_locator_t *ls_loc;
4934   u32 ls_index = ~0, locator_num;
4935   u8 *locator_name = NULL;
4936   int i;
4937
4938   memset (a, 0, sizeof (a[0]));
4939
4940   locator_name = format (0, "%s", mp->locator_set_name);
4941
4942   a->name = locator_name;
4943   a->is_add = mp->is_add;
4944   a->local = 1;
4945   locator_num = clib_net_to_host_u32 (mp->locator_num);
4946
4947   memset (&locator, 0, sizeof (locator));
4948   for (i = 0; i < locator_num; i++)
4949     {
4950       ls_loc = &((ls_locator_t *) mp->locators)[i];
4951       VALIDATE_SW_IF_INDEX (ls_loc);
4952
4953       locator.sw_if_index = htonl (ls_loc->sw_if_index);
4954       locator.priority = ls_loc->priority;
4955       locator.weight = ls_loc->weight;
4956       locator.local = 1;
4957       vec_add1 (a->locators, locator);
4958     }
4959
4960   rv = vnet_lisp_add_del_locator_set (a, &ls_index);
4961
4962   BAD_SW_IF_INDEX_LABEL;
4963
4964   vec_free (locator_name);
4965   vec_free (a->locators);
4966
4967   /* *INDENT-OFF* */
4968   REPLY_MACRO2 (VL_API_LISP_ADD_DEL_LOCATOR_SET_REPLY,
4969   ({
4970     rmp->ls_index = clib_host_to_net_u32 (ls_index);
4971   }));
4972   /* *INDENT-ON* */
4973 }
4974
4975 static void
4976 vl_api_lisp_add_del_locator_t_handler (vl_api_lisp_add_del_locator_t * mp)
4977 {
4978   vl_api_lisp_add_del_locator_reply_t *rmp;
4979   int rv = 0;
4980   locator_t locator, *locators = NULL;
4981   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
4982   u32 ls_index = ~0;
4983   u8 *locator_name = NULL;
4984
4985   memset (&locator, 0, sizeof (locator));
4986   memset (a, 0, sizeof (a[0]));
4987
4988   locator.sw_if_index = ntohl (mp->sw_if_index);
4989   locator.priority = mp->priority;
4990   locator.weight = mp->weight;
4991   locator.local = 1;
4992   vec_add1 (locators, locator);
4993
4994   locator_name = format (0, "%s", mp->locator_set_name);
4995
4996   a->name = locator_name;
4997   a->locators = locators;
4998   a->is_add = mp->is_add;
4999   a->local = 1;
5000
5001   rv = vnet_lisp_add_del_locator (a, NULL, &ls_index);
5002
5003   vec_free (locators);
5004   vec_free (locator_name);
5005
5006   REPLY_MACRO (VL_API_LISP_ADD_DEL_LOCATOR_REPLY);
5007 }
5008
5009 static int
5010 unformat_lisp_eid_api (gid_address_t * dst, u32 vni, u8 type, void *src,
5011                        u8 len)
5012 {
5013   switch (type)
5014     {
5015     case 0:                     /* ipv4 */
5016       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
5017       gid_address_ip_set (dst, src, IP4);
5018       gid_address_ippref_len (dst) = len;
5019       ip_prefix_normalize (&gid_address_ippref (dst));
5020       break;
5021     case 1:                     /* ipv6 */
5022       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
5023       gid_address_ip_set (dst, src, IP6);
5024       gid_address_ippref_len (dst) = len;
5025       ip_prefix_normalize (&gid_address_ippref (dst));
5026       break;
5027     case 2:                     /* l2 mac */
5028       gid_address_type (dst) = GID_ADDR_MAC;
5029       clib_memcpy (&gid_address_mac (dst), src, 6);
5030       break;
5031     default:
5032       /* unknown type */
5033       return VNET_API_ERROR_INVALID_VALUE;
5034     }
5035
5036   gid_address_vni (dst) = vni;
5037
5038   return 0;
5039 }
5040
5041 static void
5042 vl_api_lisp_add_del_local_eid_t_handler (vl_api_lisp_add_del_local_eid_t * mp)
5043 {
5044   vl_api_lisp_add_del_local_eid_reply_t *rmp;
5045   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5046   int rv = 0;
5047   gid_address_t _eid, *eid = &_eid;
5048   uword *p = NULL;
5049   u32 locator_set_index = ~0, map_index = ~0;
5050   vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
5051   u8 *name = NULL;
5052   memset (a, 0, sizeof (a[0]));
5053   memset (eid, 0, sizeof (eid[0]));
5054
5055   rv = unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
5056                               mp->eid_type, mp->eid, mp->prefix_len);
5057   if (rv)
5058     goto out;
5059
5060   name = format (0, "%s", mp->locator_set_name);
5061   p = hash_get_mem (lcm->locator_set_index_by_name, name);
5062   if (!p)
5063     {
5064       rv = VNET_API_ERROR_INVALID_VALUE;
5065       goto out;
5066     }
5067   locator_set_index = p[0];
5068
5069   /* XXX treat batch configuration */
5070   a->is_add = mp->is_add;
5071   gid_address_copy (&a->eid, eid);
5072   a->locator_set_index = locator_set_index;
5073   a->local = 1;
5074   rv = vnet_lisp_add_del_local_mapping (a, &map_index);
5075
5076 out:
5077   vec_free (name);
5078   gid_address_free (&a->eid);
5079
5080   REPLY_MACRO (VL_API_LISP_ADD_DEL_LOCAL_EID_REPLY);
5081 }
5082
5083 static void
5084   vl_api_lisp_eid_table_add_del_map_t_handler
5085   (vl_api_lisp_eid_table_add_del_map_t * mp)
5086 {
5087   vl_api_lisp_eid_table_add_del_map_reply_t *rmp;
5088   int rv = 0;
5089   rv = vnet_lisp_eid_table_map (clib_net_to_host_u32 (mp->vni),
5090                                 clib_net_to_host_u32 (mp->dp_table),
5091                                 mp->is_l2, mp->is_add);
5092 REPLY_MACRO (VL_API_LISP_EID_TABLE_ADD_DEL_MAP_REPLY)}
5093
5094 /** Used for transferring locators via VPP API */
5095 /* *INDENT-OFF* */
5096 typedef CLIB_PACKED (struct {
5097   u8 is_ip4; /**< is locator an IPv4 address */
5098   u8 priority; /**< locator priority */
5099   u8 weight; /**< locator weight */
5100   u8 addr[16]; /**< IPv4/IPv6 address */
5101 }) rloc_t;
5102 /* *INDENT-ON* */
5103
5104 static locator_pair_t *
5105 unformat_lisp_loc_pairs (void *lcl_locs, void *rmt_locs, u32 rloc_num)
5106 {
5107   u32 i;
5108   locator_pair_t *pairs = 0, pair;
5109   rloc_t *r;
5110
5111   for (i = 0; i < rloc_num; i++)
5112     {
5113       /* local locator */
5114       r = &((rloc_t *) lcl_locs)[i];
5115       memset (&pair.lcl_loc, 0, sizeof (pair.lcl_loc));
5116       ip_address_set (&pair.lcl_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
5117
5118       /* remote locators */
5119       r = &((rloc_t *) rmt_locs)[i];
5120       memset (&pair.rmt_loc, 0, sizeof (pair.rmt_loc));
5121       ip_address_set (&pair.rmt_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
5122
5123       pair.priority = r->priority;
5124       pair.weight = r->weight;
5125
5126       vec_add1 (pairs, pair);
5127     }
5128   return pairs;
5129 }
5130
5131 static locator_t *
5132 unformat_lisp_locs (void *rmt_locs, u32 rloc_num)
5133 {
5134   u32 i;
5135   locator_t *locs = 0, loc;
5136   rloc_t *r;
5137
5138   for (i = 0; i < rloc_num; i++)
5139     {
5140       /* remote locators */
5141       r = &((rloc_t *) rmt_locs)[i];
5142       memset (&loc, 0, sizeof (loc));
5143       gid_address_ip_set (&loc.address, &r->addr, r->is_ip4 ? IP4 : IP6);
5144
5145       loc.priority = r->priority;
5146       loc.weight = r->weight;
5147
5148       vec_add1 (locs, loc);
5149     }
5150   return locs;
5151 }
5152
5153 static void
5154   vl_api_lisp_gpe_add_del_fwd_entry_t_handler
5155   (vl_api_lisp_gpe_add_del_fwd_entry_t * mp)
5156 {
5157   vl_api_lisp_gpe_add_del_fwd_entry_reply_t *rmp;
5158   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
5159   locator_pair_t *pairs = 0;
5160   int rv = 0;
5161
5162   memset (a, 0, sizeof (a[0]));
5163
5164   rv = unformat_lisp_eid_api (&a->rmt_eid, mp->vni, mp->eid_type,
5165                               mp->rmt_eid, mp->rmt_len);
5166   rv |= unformat_lisp_eid_api (&a->lcl_eid, mp->vni, mp->eid_type,
5167                                mp->lcl_eid, mp->lcl_len);
5168
5169   pairs = unformat_lisp_loc_pairs (mp->lcl_locs, mp->rmt_locs, mp->loc_num);
5170
5171   if (rv || 0 == pairs)
5172     goto send_reply;
5173
5174   a->is_add = mp->is_add;
5175   a->locator_pairs = pairs;
5176   a->dp_table = mp->dp_table;
5177   a->vni = mp->vni;
5178   a->action = mp->action;
5179
5180   rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
5181   vec_free (pairs);
5182 send_reply:
5183   REPLY_MACRO (VL_API_LISP_GPE_ADD_DEL_FWD_ENTRY_REPLY);
5184 }
5185
5186 static void
5187 vl_api_lisp_add_del_map_resolver_t_handler (vl_api_lisp_add_del_map_resolver_t
5188                                             * mp)
5189 {
5190   vl_api_lisp_add_del_map_resolver_reply_t *rmp;
5191   int rv = 0;
5192   vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
5193
5194   memset (a, 0, sizeof (a[0]));
5195
5196   a->is_add = mp->is_add;
5197   ip_address_set (&a->address, mp->ip_address, mp->is_ipv6 ? IP6 : IP4);
5198
5199   rv = vnet_lisp_add_del_map_resolver (a);
5200
5201   REPLY_MACRO (VL_API_LISP_ADD_DEL_MAP_RESOLVER_REPLY);
5202 }
5203
5204 static void
5205 vl_api_lisp_gpe_enable_disable_t_handler (vl_api_lisp_gpe_enable_disable_t *
5206                                           mp)
5207 {
5208   vl_api_lisp_gpe_enable_disable_reply_t *rmp;
5209   int rv = 0;
5210   vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
5211
5212   a->is_en = mp->is_en;
5213   vnet_lisp_gpe_enable_disable (a);
5214
5215   REPLY_MACRO (VL_API_LISP_GPE_ENABLE_DISABLE_REPLY);
5216 }
5217
5218 static void
5219 vl_api_lisp_enable_disable_t_handler (vl_api_lisp_enable_disable_t * mp)
5220 {
5221   vl_api_lisp_enable_disable_reply_t *rmp;
5222   int rv = 0;
5223
5224   vnet_lisp_enable_disable (mp->is_en);
5225   REPLY_MACRO (VL_API_LISP_ENABLE_DISABLE_REPLY);
5226 }
5227
5228 static void
5229 vl_api_lisp_gpe_add_del_iface_t_handler (vl_api_lisp_gpe_add_del_iface_t * mp)
5230 {
5231   vl_api_lisp_gpe_add_del_iface_reply_t *rmp;
5232   int rv = 0;
5233   vnet_lisp_gpe_add_del_iface_args_t _a, *a = &_a;
5234
5235   a->is_add = mp->is_add;
5236   a->dp_table = mp->dp_table;
5237   a->vni = mp->vni;
5238   a->is_l2 = mp->is_l2;
5239   rv = vnet_lisp_gpe_add_del_iface (a, 0);
5240
5241   REPLY_MACRO (VL_API_LISP_GPE_ADD_DEL_IFACE_REPLY);
5242 }
5243
5244 static void
5245   vl_api_show_lisp_map_request_mode_t_handler
5246   (vl_api_show_lisp_map_request_mode_t * mp)
5247 {
5248   int rv = 0;
5249   vl_api_show_lisp_map_request_mode_reply_t *rmp;
5250
5251   /* *INDENT-OFF* */
5252   REPLY_MACRO2(VL_API_SHOW_LISP_MAP_REQUEST_MODE_REPLY,
5253   ({
5254     rmp->mode = vnet_lisp_get_map_request_mode ();
5255   }));
5256   /* *INDENT-ON* */
5257 }
5258
5259 static void
5260 vl_api_lisp_map_request_mode_t_handler (vl_api_lisp_map_request_mode_t * mp)
5261 {
5262   vl_api_lisp_map_request_mode_reply_t *rmp;
5263   int rv = 0;
5264
5265   rv = vnet_lisp_set_map_request_mode (mp->mode);
5266
5267   REPLY_MACRO (VL_API_LISP_MAP_REQUEST_MODE_REPLY);
5268 }
5269
5270 static void
5271 vl_api_lisp_pitr_set_locator_set_t_handler (vl_api_lisp_pitr_set_locator_set_t
5272                                             * mp)
5273 {
5274   vl_api_lisp_pitr_set_locator_set_reply_t *rmp;
5275   int rv = 0;
5276   u8 *ls_name = 0;
5277
5278   ls_name = format (0, "%s", mp->ls_name);
5279   rv = vnet_lisp_pitr_set_locator_set (ls_name, mp->is_add);
5280   vec_free (ls_name);
5281
5282   REPLY_MACRO (VL_API_LISP_PITR_SET_LOCATOR_SET_REPLY);
5283 }
5284
5285 static void
5286   vl_api_lisp_add_del_map_request_itr_rlocs_t_handler
5287   (vl_api_lisp_add_del_map_request_itr_rlocs_t * mp)
5288 {
5289   vl_api_lisp_add_del_map_request_itr_rlocs_reply_t *rmp;
5290   int rv = 0;
5291   u8 *locator_set_name = NULL;
5292   vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
5293
5294   locator_set_name = format (0, "%s", mp->locator_set_name);
5295
5296   a->is_add = mp->is_add;
5297   a->locator_set_name = locator_set_name;
5298
5299   rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
5300
5301   vec_free (locator_set_name);
5302
5303   REPLY_MACRO (VL_API_LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS_REPLY);
5304 }
5305
5306 static void
5307   vl_api_lisp_add_del_remote_mapping_t_handler
5308   (vl_api_lisp_add_del_remote_mapping_t * mp)
5309 {
5310   locator_t *rlocs = 0;
5311   vl_api_lisp_add_del_remote_mapping_reply_t *rmp;
5312   int rv = 0;
5313   gid_address_t _eid, *eid = &_eid;
5314   u32 rloc_num = clib_net_to_host_u32 (mp->rloc_num);
5315
5316   memset (eid, 0, sizeof (eid[0]));
5317
5318   rv = unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
5319                               mp->eid_type, mp->eid, mp->eid_len);
5320   if (rv)
5321     goto send_reply;
5322
5323   rlocs = unformat_lisp_locs (mp->rlocs, rloc_num);
5324
5325   if (!mp->is_add)
5326     {
5327       vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
5328       gid_address_copy (&a->deid, eid);
5329       a->is_add = 0;
5330       rv = vnet_lisp_add_del_adjacency (a);
5331       if (rv)
5332         {
5333           goto out;
5334         }
5335     }
5336
5337   /* NOTE: for now this works as a static remote mapping, i.e.,
5338    * not authoritative and ttl infinite. */
5339   rv = vnet_lisp_add_del_mapping (eid, rlocs, mp->action, 0, ~0,
5340                                   mp->is_add, 1 /* is_static */ , 0);
5341
5342   if (mp->del_all)
5343     vnet_lisp_clear_all_remote_adjacencies ();
5344
5345 out:
5346   vec_free (rlocs);
5347 send_reply:
5348   REPLY_MACRO (VL_API_LISP_ADD_DEL_REMOTE_MAPPING_REPLY);
5349 }
5350
5351 static void
5352 vl_api_lisp_add_del_adjacency_t_handler (vl_api_lisp_add_del_adjacency_t * mp)
5353 {
5354   vl_api_lisp_add_del_adjacency_reply_t *rmp;
5355   vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
5356
5357   int rv = 0;
5358   memset (a, 0, sizeof (a[0]));
5359
5360   rv = unformat_lisp_eid_api (&a->seid, clib_net_to_host_u32 (mp->vni),
5361                               mp->eid_type, mp->seid, mp->seid_len);
5362   rv |= unformat_lisp_eid_api (&a->deid, clib_net_to_host_u32 (mp->vni),
5363                                mp->eid_type, mp->deid, mp->deid_len);
5364
5365   if (rv)
5366     goto send_reply;
5367
5368   a->is_add = mp->is_add;
5369   rv = vnet_lisp_add_del_adjacency (a);
5370
5371 send_reply:
5372   REPLY_MACRO (VL_API_LISP_ADD_DEL_ADJACENCY_REPLY);
5373 }
5374
5375 static void
5376 send_lisp_locator_details (lisp_cp_main_t * lcm,
5377                            locator_t * loc,
5378                            unix_shared_memory_queue_t * q, u32 context)
5379 {
5380   vl_api_lisp_locator_details_t *rmp;
5381
5382   rmp = vl_msg_api_alloc (sizeof (*rmp));
5383   memset (rmp, 0, sizeof (*rmp));
5384   rmp->_vl_msg_id = ntohs (VL_API_LISP_LOCATOR_DETAILS);
5385   rmp->context = context;
5386
5387   rmp->local = loc->local;
5388   if (loc->local)
5389     {
5390       rmp->sw_if_index = ntohl (loc->sw_if_index);
5391     }
5392   else
5393     {
5394       rmp->is_ipv6 = gid_address_ip_version (&loc->address);
5395       ip_address_copy_addr (rmp->ip_address, &gid_address_ip (&loc->address));
5396     }
5397   rmp->priority = loc->priority;
5398   rmp->weight = loc->weight;
5399
5400   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5401 }
5402
5403 static void
5404 vl_api_lisp_locator_dump_t_handler (vl_api_lisp_locator_dump_t * mp)
5405 {
5406   u8 *ls_name = 0;
5407   unix_shared_memory_queue_t *q = 0;
5408   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5409   locator_set_t *lsit = 0;
5410   locator_t *loc = 0;
5411   u32 ls_index = ~0, *locit = 0;
5412   uword *p = 0;
5413
5414   q = vl_api_client_index_to_input_queue (mp->client_index);
5415   if (q == 0)
5416     {
5417       return;
5418     }
5419
5420   if (mp->is_index_set)
5421     ls_index = htonl (mp->ls_index);
5422   else
5423     {
5424       /* make sure we get a proper C-string */
5425       mp->ls_name[sizeof (mp->ls_name) - 1] = 0;
5426       ls_name = format (0, "%s", mp->ls_name);
5427       p = hash_get_mem (lcm->locator_set_index_by_name, ls_name);
5428       if (!p)
5429         goto out;
5430       ls_index = p[0];
5431     }
5432
5433   if (pool_is_free_index (lcm->locator_set_pool, ls_index))
5434     return;
5435
5436   lsit = pool_elt_at_index (lcm->locator_set_pool, ls_index);
5437
5438   vec_foreach (locit, lsit->locator_indices)
5439   {
5440     loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
5441     send_lisp_locator_details (lcm, loc, q, mp->context);
5442   };
5443 out:
5444   vec_free (ls_name);
5445 }
5446
5447 static void
5448 send_lisp_locator_set_details (lisp_cp_main_t * lcm,
5449                                locator_set_t * lsit,
5450                                unix_shared_memory_queue_t * q,
5451                                u32 context, u32 ls_index)
5452 {
5453   vl_api_lisp_locator_set_details_t *rmp;
5454   u8 *str = 0;
5455
5456   rmp = vl_msg_api_alloc (sizeof (*rmp));
5457   memset (rmp, 0, sizeof (*rmp));
5458   rmp->_vl_msg_id = ntohs (VL_API_LISP_LOCATOR_SET_DETAILS);
5459   rmp->context = context;
5460
5461   rmp->ls_index = htonl (ls_index);
5462   if (lsit->local)
5463     {
5464       ASSERT (lsit->name != NULL);
5465       strncpy ((char *) rmp->ls_name, (char *) lsit->name,
5466                vec_len (lsit->name));
5467     }
5468   else
5469     {
5470       str = format (0, "<remote-%d>", ls_index);
5471       strncpy ((char *) rmp->ls_name, (char *) str, vec_len (str));
5472       vec_free (str);
5473     }
5474
5475   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5476 }
5477
5478 static void
5479 vl_api_lisp_locator_set_dump_t_handler (vl_api_lisp_locator_set_dump_t * mp)
5480 {
5481   unix_shared_memory_queue_t *q = NULL;
5482   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5483   locator_set_t *lsit = NULL;
5484   u8 filter;
5485
5486   q = vl_api_client_index_to_input_queue (mp->client_index);
5487   if (q == 0)
5488     {
5489       return;
5490     }
5491
5492   filter = mp->filter;
5493   /* *INDENT-OFF* */
5494   pool_foreach (lsit, lcm->locator_set_pool,
5495   ({
5496     if (filter && !((1 == filter && lsit->local) ||
5497                     (2 == filter && !lsit->local)))
5498       {
5499         continue;
5500       }
5501     send_lisp_locator_set_details (lcm, lsit, q, mp->context,
5502                                    lsit - lcm->locator_set_pool);
5503   }));
5504   /* *INDENT-ON* */
5505 }
5506
5507 static void
5508 lisp_fid_put_api (u8 * dst, fid_address_t * src, u8 * prefix_length)
5509 {
5510   ASSERT (prefix_length);
5511   ip_prefix_t *ippref = &fid_addr_ippref (src);
5512
5513   switch (fid_addr_type (src))
5514     {
5515     case FID_ADDR_IP_PREF:
5516       if (ip_prefix_version (ippref) == IP4)
5517         clib_memcpy (dst, &ip_prefix_v4 (ippref), 4);
5518       else
5519         clib_memcpy (dst, &ip_prefix_v6 (ippref), 16);
5520       prefix_length[0] = ip_prefix_len (ippref);
5521       break;
5522
5523     case FID_ADDR_MAC:
5524       prefix_length[0] = 0;
5525       clib_memcpy (dst, fid_addr_mac (src), 6);
5526       break;
5527
5528     default:
5529       clib_warning ("Unknown FID type %d!", fid_addr_type (src));
5530       break;
5531     }
5532 }
5533
5534 static u8
5535 fid_type_to_api_type (fid_address_t * fid)
5536 {
5537   ip_prefix_t *ippref;
5538
5539   switch (fid_addr_type (fid))
5540     {
5541     case FID_ADDR_IP_PREF:
5542       ippref = &fid_addr_ippref (fid);
5543       if (ip_prefix_version (ippref) == IP4)
5544         return 0;
5545       else if (ip_prefix_version (ippref) == IP6)
5546         return 1;
5547       else
5548         return ~0;
5549
5550     case FID_ADDR_MAC:
5551       return 2;
5552     }
5553
5554   return ~0;
5555 }
5556
5557 static void
5558 send_lisp_eid_table_details (mapping_t * mapit,
5559                              unix_shared_memory_queue_t * q,
5560                              u32 context, u8 filter)
5561 {
5562   fid_address_t *fid;
5563   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5564   locator_set_t *ls = 0;
5565   vl_api_lisp_eid_table_details_t *rmp = NULL;
5566   gid_address_t *gid = NULL;
5567   u8 *mac = 0;
5568   ip_prefix_t *ip_prefix = NULL;
5569
5570   switch (filter)
5571     {
5572     case 0:                     /* all mappings */
5573       break;
5574
5575     case 1:                     /* local only */
5576       if (!mapit->local)
5577         return;
5578       break;
5579     case 2:                     /* remote only */
5580       if (mapit->local)
5581         return;
5582       break;
5583     default:
5584       clib_warning ("Filter error, unknown filter: %d", filter);
5585       return;
5586     }
5587
5588   gid = &mapit->eid;
5589   ip_prefix = &gid_address_ippref (gid);
5590   mac = gid_address_mac (gid);
5591
5592   rmp = vl_msg_api_alloc (sizeof (*rmp));
5593   memset (rmp, 0, sizeof (*rmp));
5594   rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_DETAILS);
5595
5596   ls = pool_elt_at_index (lcm->locator_set_pool, mapit->locator_set_index);
5597   if (vec_len (ls->locator_indices) == 0)
5598     rmp->locator_set_index = ~0;
5599   else
5600     rmp->locator_set_index = clib_host_to_net_u32 (mapit->locator_set_index);
5601
5602   rmp->is_local = mapit->local;
5603   rmp->ttl = clib_host_to_net_u32 (mapit->ttl);
5604   rmp->action = mapit->action;
5605   rmp->authoritative = mapit->authoritative;
5606
5607   switch (gid_address_type (gid))
5608     {
5609     case GID_ADDR_SRC_DST:
5610       rmp->is_src_dst = 1;
5611       fid = &gid_address_sd_src (gid);
5612       rmp->eid_type = fid_type_to_api_type (fid);
5613       lisp_fid_put_api (rmp->seid, &gid_address_sd_src (gid),
5614                         &rmp->seid_prefix_len);
5615       lisp_fid_put_api (rmp->eid, &gid_address_sd_dst (gid),
5616                         &rmp->eid_prefix_len);
5617       break;
5618     case GID_ADDR_IP_PREFIX:
5619       rmp->eid_prefix_len = ip_prefix_len (ip_prefix);
5620       if (ip_prefix_version (ip_prefix) == IP4)
5621         {
5622           rmp->eid_type = 0;    /* ipv4 type */
5623           clib_memcpy (rmp->eid, &ip_prefix_v4 (ip_prefix),
5624                        sizeof (ip_prefix_v4 (ip_prefix)));
5625         }
5626       else
5627         {
5628           rmp->eid_type = 1;    /* ipv6 type */
5629           clib_memcpy (rmp->eid, &ip_prefix_v6 (ip_prefix),
5630                        sizeof (ip_prefix_v6 (ip_prefix)));
5631         }
5632       break;
5633     case GID_ADDR_MAC:
5634       rmp->eid_type = 2;        /* l2 mac type */
5635       clib_memcpy (rmp->eid, mac, 6);
5636       break;
5637     default:
5638       ASSERT (0);
5639     }
5640   rmp->context = context;
5641   rmp->vni = clib_host_to_net_u32 (gid_address_vni (gid));
5642   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5643 }
5644
5645 static void
5646 vl_api_lisp_eid_table_dump_t_handler (vl_api_lisp_eid_table_dump_t * mp)
5647 {
5648   u32 mi;
5649   unix_shared_memory_queue_t *q = NULL;
5650   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5651   mapping_t *mapit = NULL;
5652   gid_address_t _eid, *eid = &_eid;
5653
5654   q = vl_api_client_index_to_input_queue (mp->client_index);
5655   if (q == 0)
5656     {
5657       return;
5658     }
5659
5660   if (mp->eid_set)
5661     {
5662       memset (eid, 0, sizeof (*eid));
5663
5664       unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
5665                              mp->eid_type, mp->eid, mp->prefix_length);
5666
5667       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
5668       if ((u32) ~ 0 == mi)
5669         return;
5670
5671       mapit = pool_elt_at_index (lcm->mapping_pool, mi);
5672       send_lisp_eid_table_details (mapit, q, mp->context,
5673                                    0 /* ignore filter */ );
5674     }
5675   else
5676     {
5677       /* *INDENT-OFF* */
5678       pool_foreach (mapit, lcm->mapping_pool,
5679       ({
5680         send_lisp_eid_table_details(mapit, q, mp->context,
5681                                     mp->filter);
5682       }));
5683       /* *INDENT-ON* */
5684     }
5685 }
5686
5687 static void
5688 send_lisp_gpe_tunnel_details (lisp_gpe_tunnel_t * tunnel,
5689                               unix_shared_memory_queue_t * q, u32 context)
5690 {
5691   vl_api_lisp_gpe_tunnel_details_t *rmp;
5692   lisp_gpe_main_t *lgm = &lisp_gpe_main;
5693
5694   rmp = vl_msg_api_alloc (sizeof (*rmp));
5695   memset (rmp, 0, sizeof (*rmp));
5696   rmp->_vl_msg_id = ntohs (VL_API_LISP_GPE_TUNNEL_DETAILS);
5697
5698   rmp->tunnels = tunnel - lgm->tunnels;
5699
5700   rmp->is_ipv6 = ip_addr_version (&tunnel->src) == IP6 ? 1 : 0;
5701   ip_address_copy_addr (rmp->source_ip, &tunnel->src);
5702   ip_address_copy_addr (rmp->destination_ip, &tunnel->dst);
5703
5704   rmp->encap_fib_id = htonl (tunnel->encap_fib_index);
5705   rmp->decap_fib_id = htonl (tunnel->decap_fib_index);
5706   rmp->dcap_next = htonl (tunnel->decap_next_index);
5707   rmp->lisp_ver = tunnel->ver_res;
5708   rmp->next_protocol = tunnel->next_protocol;
5709   rmp->flags = tunnel->flags;
5710   rmp->ver_res = tunnel->ver_res;
5711   rmp->res = tunnel->res;
5712   rmp->iid = htonl (tunnel->vni);
5713   rmp->context = context;
5714
5715   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5716 }
5717
5718 static void
5719 vl_api_lisp_gpe_tunnel_dump_t_handler (vl_api_lisp_gpe_tunnel_dump_t * mp)
5720 {
5721   unix_shared_memory_queue_t *q = NULL;
5722   lisp_gpe_main_t *lgm = &lisp_gpe_main;
5723   lisp_gpe_tunnel_t *tunnel = NULL;
5724
5725   if (pool_elts (lgm->tunnels) == 0)
5726     {
5727       return;
5728     }
5729
5730   q = vl_api_client_index_to_input_queue (mp->client_index);
5731   if (q == 0)
5732     {
5733       return;
5734     }
5735
5736   /* *INDENT-OFF* */
5737   pool_foreach(tunnel, lgm->tunnels,
5738   ({
5739     send_lisp_gpe_tunnel_details(tunnel, q, mp->context);
5740   }));
5741   /* *INDENT-ON* */
5742 }
5743
5744 static void
5745 send_lisp_map_resolver_details (ip_address_t * ip,
5746                                 unix_shared_memory_queue_t * q, u32 context)
5747 {
5748   vl_api_lisp_map_resolver_details_t *rmp = NULL;
5749
5750   rmp = vl_msg_api_alloc (sizeof (*rmp));
5751   memset (rmp, 0, sizeof (*rmp));
5752   rmp->_vl_msg_id = ntohs (VL_API_LISP_MAP_RESOLVER_DETAILS);
5753
5754   switch (ip_addr_version (ip))
5755     {
5756     case IP4:
5757       rmp->is_ipv6 = 0;
5758       clib_memcpy (rmp->ip_address, &ip_addr_v4 (ip),
5759                    sizeof (ip_addr_v4 (ip)));
5760       break;
5761
5762     case IP6:
5763       rmp->is_ipv6 = 1;
5764       clib_memcpy (rmp->ip_address, &ip_addr_v6 (ip),
5765                    sizeof (ip_addr_v6 (ip)));
5766       break;
5767
5768     default:
5769       ASSERT (0);
5770     }
5771   rmp->context = context;
5772
5773   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5774 }
5775
5776 static void
5777 vl_api_lisp_map_resolver_dump_t_handler (vl_api_lisp_map_resolver_dump_t * mp)
5778 {
5779   unix_shared_memory_queue_t *q = NULL;
5780   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5781   map_resolver_t *mr;
5782
5783   q = vl_api_client_index_to_input_queue (mp->client_index);
5784   if (q == 0)
5785     {
5786       return;
5787     }
5788
5789   vec_foreach (mr, lcm->map_resolvers)
5790   {
5791     send_lisp_map_resolver_details (&mr->address, q, mp->context);
5792   }
5793 }
5794
5795 static void
5796 send_eid_table_map_pair (hash_pair_t * p,
5797                          unix_shared_memory_queue_t * q, u32 context)
5798 {
5799   vl_api_lisp_eid_table_map_details_t *rmp = NULL;
5800
5801   rmp = vl_msg_api_alloc (sizeof (*rmp));
5802   memset (rmp, 0, sizeof (*rmp));
5803   rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_MAP_DETAILS);
5804
5805   rmp->vni = clib_host_to_net_u32 (p->key);
5806   rmp->dp_table = clib_host_to_net_u32 (p->value[0]);
5807   rmp->context = context;
5808   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5809 }
5810
5811 static void
5812 vl_api_lisp_eid_table_map_dump_t_handler (vl_api_lisp_eid_table_map_dump_t *
5813                                           mp)
5814 {
5815   unix_shared_memory_queue_t *q = NULL;
5816   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5817   hash_pair_t *p;
5818   uword *vni_table = 0;
5819
5820   q = vl_api_client_index_to_input_queue (mp->client_index);
5821   if (q == 0)
5822     {
5823       return;
5824     }
5825
5826   if (mp->is_l2)
5827     {
5828       vni_table = lcm->bd_id_by_vni;
5829     }
5830   else
5831     {
5832       vni_table = lcm->table_id_by_vni;
5833     }
5834
5835   /* *INDENT-OFF* */
5836   hash_foreach_pair (p, vni_table,
5837   ({
5838     send_eid_table_map_pair (p, q, mp->context);
5839   }));
5840   /* *INDENT-ON* */
5841 }
5842
5843 static void
5844 send_eid_table_vni (u32 vni, unix_shared_memory_queue_t * q, u32 context)
5845 {
5846   vl_api_lisp_eid_table_vni_details_t *rmp = 0;
5847
5848   rmp = vl_msg_api_alloc (sizeof (*rmp));
5849   memset (rmp, 0, sizeof (*rmp));
5850   rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_VNI_DETAILS);
5851   rmp->context = context;
5852   rmp->vni = clib_host_to_net_u32 (vni);
5853   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5854 }
5855
5856 static void
5857 vl_api_lisp_eid_table_vni_dump_t_handler (vl_api_lisp_eid_table_vni_dump_t *
5858                                           mp)
5859 {
5860   hash_pair_t *p;
5861   u32 *vnis = 0;
5862   unix_shared_memory_queue_t *q = 0;
5863   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5864
5865   q = vl_api_client_index_to_input_queue (mp->client_index);
5866   if (q == 0)
5867     {
5868       return;
5869     }
5870
5871   /* *INDENT-OFF* */
5872   hash_foreach_pair (p, lcm->table_id_by_vni,
5873   ({
5874     hash_set (vnis, p->key, 0);
5875   }));
5876
5877   hash_foreach_pair (p, lcm->bd_id_by_vni,
5878   ({
5879     hash_set (vnis, p->key, 0);
5880   }));
5881
5882   hash_foreach_pair (p, vnis,
5883   ({
5884     send_eid_table_vni (p->key, q, mp->context);
5885   }));
5886   /* *INDENT-ON* */
5887
5888   hash_free (vnis);
5889 }
5890
5891 static void
5892 vl_api_show_lisp_status_t_handler (vl_api_show_lisp_status_t * mp)
5893 {
5894   unix_shared_memory_queue_t *q = NULL;
5895   vl_api_show_lisp_status_reply_t *rmp = NULL;
5896   int rv = 0;
5897
5898   q = vl_api_client_index_to_input_queue (mp->client_index);
5899   if (q == 0)
5900     {
5901       return;
5902     }
5903
5904   /* *INDENT-OFF* */
5905   REPLY_MACRO2(VL_API_SHOW_LISP_STATUS_REPLY,
5906   ({
5907     rmp->gpe_status = vnet_lisp_gpe_enable_disable_status ();
5908     rmp->feature_status = vnet_lisp_enable_disable_status ();
5909   }));
5910   /* *INDENT-ON* */
5911 }
5912
5913 static void
5914   vl_api_lisp_get_map_request_itr_rlocs_t_handler
5915   (vl_api_lisp_get_map_request_itr_rlocs_t * mp)
5916 {
5917   unix_shared_memory_queue_t *q = NULL;
5918   vl_api_lisp_get_map_request_itr_rlocs_reply_t *rmp = NULL;
5919   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5920   locator_set_t *loc_set = 0;
5921   u8 *tmp_str = 0;
5922   int rv = 0;
5923
5924   q = vl_api_client_index_to_input_queue (mp->client_index);
5925   if (q == 0)
5926     {
5927       return;
5928     }
5929
5930   if (~0 == lcm->mreq_itr_rlocs)
5931     {
5932       tmp_str = format (0, " ");
5933     }
5934   else
5935     {
5936       loc_set =
5937         pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
5938       tmp_str = format (0, "%s", loc_set->name);
5939     }
5940
5941   /* *INDENT-OFF* */
5942   REPLY_MACRO2(VL_API_LISP_GET_MAP_REQUEST_ITR_RLOCS_REPLY,
5943   ({
5944     strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
5945             ARRAY_LEN(rmp->locator_set_name) - 1);
5946   }));
5947   /* *INDENT-ON* */
5948
5949   vec_free (tmp_str);
5950 }
5951
5952 static void
5953 vl_api_show_lisp_pitr_t_handler (vl_api_show_lisp_pitr_t * mp)
5954 {
5955   unix_shared_memory_queue_t *q = NULL;
5956   vl_api_show_lisp_pitr_reply_t *rmp = NULL;
5957   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5958   mapping_t *m;
5959   locator_set_t *ls = 0;
5960   u8 *tmp_str = 0;
5961   int rv = 0;
5962
5963   q = vl_api_client_index_to_input_queue (mp->client_index);
5964   if (q == 0)
5965     {
5966       return;
5967     }
5968
5969   if (!lcm->lisp_pitr)
5970     {
5971       tmp_str = format (0, "N/A");
5972     }
5973   else
5974     {
5975       m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
5976       if (~0 != m->locator_set_index)
5977         {
5978           ls =
5979             pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
5980           tmp_str = format (0, "%s", ls->name);
5981         }
5982       else
5983         {
5984           tmp_str = format (0, "N/A");
5985         }
5986     }
5987   vec_add1 (tmp_str, 0);
5988
5989   /* *INDENT-OFF* */
5990   REPLY_MACRO2(VL_API_SHOW_LISP_PITR_REPLY,
5991   ({
5992     rmp->status = lcm->lisp_pitr;
5993     strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
5994             ARRAY_LEN(rmp->locator_set_name) - 1);
5995   }));
5996   /* *INDENT-ON* */
5997 }
5998
5999 static void
6000 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
6001                                           mp)
6002 {
6003   vl_api_interface_name_renumber_reply_t *rmp;
6004   int rv = 0;
6005
6006   VALIDATE_SW_IF_INDEX (mp);
6007
6008   rv = vnet_interface_name_renumber
6009     (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
6010
6011   BAD_SW_IF_INDEX_LABEL;
6012
6013   REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
6014 }
6015
6016 static int
6017 arp_change_data_callback (u32 pool_index, u8 * new_mac,
6018                           u32 sw_if_index, u32 address)
6019 {
6020   vpe_api_main_t *am = &vpe_api_main;
6021   vlib_main_t *vm = am->vlib_main;
6022   vl_api_ip4_arp_event_t *event;
6023   static f64 arp_event_last_time;
6024   f64 now = vlib_time_now (vm);
6025
6026   if (pool_is_free_index (am->arp_events, pool_index))
6027     return 1;
6028
6029   event = pool_elt_at_index (am->arp_events, pool_index);
6030   /* *INDENT-OFF* */
6031   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
6032     {
6033       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
6034     }
6035   else
6036     {                           /* same mac */
6037       if (sw_if_index == event->sw_if_index &&
6038           (!event->mac_ip ||
6039            /* for BD case, also check IP address with 10 sec timeout */
6040            (address == event->address &&
6041             (now - arp_event_last_time) < 10.0)))
6042         return 1;
6043     }
6044   /* *INDENT-ON* */
6045
6046   arp_event_last_time = now;
6047   event->sw_if_index = sw_if_index;
6048   if (event->mac_ip)
6049     event->address = address;
6050   return 0;
6051 }
6052
6053 static int
6054 nd_change_data_callback (u32 pool_index, u8 * new_mac,
6055                          u32 sw_if_index, ip6_address_t * address)
6056 {
6057   vpe_api_main_t *am = &vpe_api_main;
6058   vlib_main_t *vm = am->vlib_main;
6059   vl_api_ip6_nd_event_t *event;
6060   static f64 nd_event_last_time;
6061   f64 now = vlib_time_now (vm);
6062
6063   if (pool_is_free_index (am->nd_events, pool_index))
6064     return 1;
6065
6066   event = pool_elt_at_index (am->nd_events, pool_index);
6067
6068   /* *INDENT-OFF* */
6069   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
6070     {
6071       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
6072     }
6073   else
6074     {                           /* same mac */
6075       if (sw_if_index == event->sw_if_index &&
6076           (!event->mac_ip ||
6077            /* for BD case, also check IP address with 10 sec timeout */
6078            (ip6_address_is_equal (address,
6079                                   (ip6_address_t *) event->address) &&
6080             (now - nd_event_last_time) < 10.0)))
6081         return 1;
6082     }
6083   /* *INDENT-ON* */
6084
6085   nd_event_last_time = now;
6086   event->sw_if_index = sw_if_index;
6087   if (event->mac_ip)
6088     clib_memcpy (event->address, address, sizeof (event->address));
6089   return 0;
6090 }
6091
6092 static int
6093 arp_change_delete_callback (u32 pool_index, u8 * notused)
6094 {
6095   vpe_api_main_t *am = &vpe_api_main;
6096
6097   if (pool_is_free_index (am->arp_events, pool_index))
6098     return 1;
6099
6100   pool_put_index (am->arp_events, pool_index);
6101   return 0;
6102 }
6103
6104 static int
6105 nd_change_delete_callback (u32 pool_index, u8 * notused)
6106 {
6107   vpe_api_main_t *am = &vpe_api_main;
6108
6109   if (pool_is_free_index (am->nd_events, pool_index))
6110     return 1;
6111
6112   pool_put_index (am->nd_events, pool_index);
6113   return 0;
6114 }
6115
6116 static void
6117 vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
6118 {
6119   vpe_api_main_t *am = &vpe_api_main;
6120   vnet_main_t *vnm = vnet_get_main ();
6121   vl_api_want_ip4_arp_events_reply_t *rmp;
6122   vl_api_ip4_arp_event_t *event;
6123   int rv;
6124
6125   if (mp->enable_disable)
6126     {
6127       pool_get (am->arp_events, event);
6128       memset (event, 0, sizeof (*event));
6129
6130       event->_vl_msg_id = ntohs (VL_API_IP4_ARP_EVENT);
6131       event->client_index = mp->client_index;
6132       event->context = mp->context;
6133       event->address = mp->address;
6134       event->pid = mp->pid;
6135       if (mp->address == 0)
6136         event->mac_ip = 1;
6137
6138       rv = vnet_add_del_ip4_arp_change_event
6139         (vnm, arp_change_data_callback,
6140          mp->pid, &mp->address /* addr, in net byte order */ ,
6141          vpe_resolver_process_node.index,
6142          IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
6143     }
6144   else
6145     {
6146       rv = vnet_add_del_ip4_arp_change_event
6147         (vnm, arp_change_delete_callback,
6148          mp->pid, &mp->address /* addr, in net byte order */ ,
6149          vpe_resolver_process_node.index,
6150          IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
6151     }
6152   REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
6153 }
6154
6155 static void
6156 vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
6157 {
6158   vpe_api_main_t *am = &vpe_api_main;
6159   vnet_main_t *vnm = vnet_get_main ();
6160   vl_api_want_ip6_nd_events_reply_t *rmp;
6161   vl_api_ip6_nd_event_t *event;
6162   int rv;
6163
6164   if (mp->enable_disable)
6165     {
6166       pool_get (am->nd_events, event);
6167       memset (event, 0, sizeof (*event));
6168
6169       event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
6170       event->client_index = mp->client_index;
6171       event->context = mp->context;
6172       clib_memcpy (event->address, mp->address, 16);
6173       event->pid = mp->pid;
6174       if (ip6_address_is_zero ((ip6_address_t *) mp->address))
6175         event->mac_ip = 1;
6176
6177       rv = vnet_add_del_ip6_nd_change_event
6178         (vnm, nd_change_data_callback,
6179          mp->pid, mp->address /* addr, in net byte order */ ,
6180          vpe_resolver_process_node.index,
6181          IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
6182     }
6183   else
6184     {
6185       rv = vnet_add_del_ip6_nd_change_event
6186         (vnm, nd_change_delete_callback,
6187          mp->pid, mp->address /* addr, in net byte order */ ,
6188          vpe_resolver_process_node.index,
6189          IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
6190     }
6191   REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
6192 }
6193
6194 static void vl_api_input_acl_set_interface_t_handler
6195   (vl_api_input_acl_set_interface_t * mp)
6196 {
6197   vlib_main_t *vm = vlib_get_main ();
6198   vl_api_input_acl_set_interface_reply_t *rmp;
6199   int rv;
6200   u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
6201
6202   ip4_table_index = ntohl (mp->ip4_table_index);
6203   ip6_table_index = ntohl (mp->ip6_table_index);
6204   l2_table_index = ntohl (mp->l2_table_index);
6205   sw_if_index = ntohl (mp->sw_if_index);
6206
6207   VALIDATE_SW_IF_INDEX (mp);
6208
6209   rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
6210                                  ip6_table_index, l2_table_index, mp->is_add);
6211
6212   BAD_SW_IF_INDEX_LABEL;
6213
6214   REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
6215 }
6216
6217 static void vl_api_ipsec_spd_add_del_t_handler
6218   (vl_api_ipsec_spd_add_del_t * mp)
6219 {
6220 #if IPSEC == 0
6221   clib_warning ("unimplemented");
6222 #else
6223
6224   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6225   vl_api_ipsec_spd_add_del_reply_t *rmp;
6226   int rv;
6227
6228 #if DPDK > 0
6229   rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
6230 #else
6231   rv = VNET_API_ERROR_UNIMPLEMENTED;
6232 #endif
6233
6234   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
6235 #endif
6236 }
6237
6238 static void vl_api_ipsec_interface_add_del_spd_t_handler
6239   (vl_api_ipsec_interface_add_del_spd_t * mp)
6240 {
6241   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6242   vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
6243   int rv;
6244   u32 sw_if_index __attribute__ ((unused));
6245   u32 spd_id __attribute__ ((unused));
6246
6247   sw_if_index = ntohl (mp->sw_if_index);
6248   spd_id = ntohl (mp->spd_id);
6249
6250   VALIDATE_SW_IF_INDEX (mp);
6251
6252 #if IPSEC > 0
6253   rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
6254 #else
6255   rv = VNET_API_ERROR_UNIMPLEMENTED;
6256 #endif
6257
6258   BAD_SW_IF_INDEX_LABEL;
6259
6260   REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
6261 }
6262
6263 static void vl_api_ipsec_spd_add_del_entry_t_handler
6264   (vl_api_ipsec_spd_add_del_entry_t * mp)
6265 {
6266   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6267   vl_api_ipsec_spd_add_del_entry_reply_t *rmp;
6268   int rv;
6269
6270 #if IPSEC > 0
6271   ipsec_policy_t p;
6272
6273   memset (&p, 0, sizeof (p));
6274
6275   p.id = ntohl (mp->spd_id);
6276   p.priority = ntohl (mp->priority);
6277   p.is_outbound = mp->is_outbound;
6278   p.is_ipv6 = mp->is_ipv6;
6279
6280   if (mp->is_ipv6 || mp->is_ip_any)
6281     {
6282       clib_memcpy (&p.raddr.start, mp->remote_address_start, 16);
6283       clib_memcpy (&p.raddr.stop, mp->remote_address_stop, 16);
6284       clib_memcpy (&p.laddr.start, mp->local_address_start, 16);
6285       clib_memcpy (&p.laddr.stop, mp->local_address_stop, 16);
6286     }
6287   else
6288     {
6289       clib_memcpy (&p.raddr.start.ip4.data, mp->remote_address_start, 4);
6290       clib_memcpy (&p.raddr.stop.ip4.data, mp->remote_address_stop, 4);
6291       clib_memcpy (&p.laddr.start.ip4.data, mp->local_address_start, 4);
6292       clib_memcpy (&p.laddr.stop.ip4.data, mp->local_address_stop, 4);
6293     }
6294   p.protocol = mp->protocol;
6295   p.rport.start = ntohs (mp->remote_port_start);
6296   p.rport.stop = ntohs (mp->remote_port_stop);
6297   p.lport.start = ntohs (mp->local_port_start);
6298   p.lport.stop = ntohs (mp->local_port_stop);
6299   /* policy action resolve unsupported */
6300   if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE)
6301     {
6302       clib_warning ("unsupported action: 'resolve'");
6303       rv = VNET_API_ERROR_UNIMPLEMENTED;
6304       goto out;
6305     }
6306   p.policy = mp->policy;
6307   p.sa_id = ntohl (mp->sa_id);
6308
6309   rv = ipsec_add_del_policy (vm, &p, mp->is_add);
6310   if (rv)
6311     goto out;
6312
6313   if (mp->is_ip_any)
6314     {
6315       p.is_ipv6 = 1;
6316       rv = ipsec_add_del_policy (vm, &p, mp->is_add);
6317     }
6318 #else
6319   rv = VNET_API_ERROR_UNIMPLEMENTED;
6320   goto out;
6321 #endif
6322
6323 out:
6324   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
6325 }
6326
6327 static void vl_api_ipsec_sad_add_del_entry_t_handler
6328   (vl_api_ipsec_sad_add_del_entry_t * mp)
6329 {
6330   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6331   vl_api_ipsec_sad_add_del_entry_reply_t *rmp;
6332   int rv;
6333 #if IPSEC > 0
6334   ipsec_sa_t sa;
6335
6336   memset (&sa, 0, sizeof (sa));
6337
6338   sa.id = ntohl (mp->sad_id);
6339   sa.spi = ntohl (mp->spi);
6340   /* security protocol AH unsupported */
6341   if (mp->protocol == IPSEC_PROTOCOL_AH)
6342     {
6343       clib_warning ("unsupported security protocol 'AH'");
6344       rv = VNET_API_ERROR_UNIMPLEMENTED;
6345       goto out;
6346     }
6347   sa.protocol = mp->protocol;
6348   /* check for unsupported crypto-alg */
6349   if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
6350       mp->crypto_algorithm > IPSEC_CRYPTO_ALG_AES_CBC_256)
6351     {
6352       clib_warning ("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
6353                     mp->crypto_algorithm);
6354       rv = VNET_API_ERROR_UNIMPLEMENTED;
6355       goto out;
6356     }
6357   sa.crypto_alg = mp->crypto_algorithm;
6358   sa.crypto_key_len = mp->crypto_key_length;
6359   clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
6360   /* check for unsupported integ-alg */
6361   if (mp->integrity_algorithm < IPSEC_INTEG_ALG_SHA1_96 ||
6362       mp->integrity_algorithm > IPSEC_INTEG_ALG_SHA_512_256)
6363     {
6364       clib_warning ("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
6365                     mp->integrity_algorithm);
6366       rv = VNET_API_ERROR_UNIMPLEMENTED;
6367       goto out;
6368     }
6369   sa.integ_alg = mp->integrity_algorithm;
6370   sa.integ_key_len = mp->integrity_key_length;
6371   clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
6372   sa.use_esn = mp->use_extended_sequence_number;
6373   sa.is_tunnel = mp->is_tunnel;
6374   sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
6375   if (sa.is_tunnel_ip6)
6376     {
6377       clib_memcpy (&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
6378       clib_memcpy (&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
6379     }
6380   else
6381     {
6382       clib_memcpy (&sa.tunnel_src_addr.ip4.data, mp->tunnel_src_address, 4);
6383       clib_memcpy (&sa.tunnel_dst_addr.ip4.data, mp->tunnel_dst_address, 4);
6384     }
6385
6386   rv = ipsec_add_del_sa (vm, &sa, mp->is_add);
6387 #else
6388   rv = VNET_API_ERROR_UNIMPLEMENTED;
6389   goto out;
6390 #endif
6391
6392 out:
6393   REPLY_MACRO (VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
6394 }
6395
6396 static void
6397 vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
6398 {
6399   vl_api_ikev2_profile_add_del_reply_t *rmp;
6400   int rv = 0;
6401
6402 #if IPSEC > 0
6403   vlib_main_t *vm = vlib_get_main ();
6404   clib_error_t *error;
6405   u8 *tmp = format (0, "%s", mp->name);
6406   error = ikev2_add_del_profile (vm, tmp, mp->is_add);
6407   vec_free (tmp);
6408   if (error)
6409     rv = VNET_API_ERROR_UNSPECIFIED;
6410 #else
6411   rv = VNET_API_ERROR_UNIMPLEMENTED;
6412 #endif
6413
6414   REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
6415 }
6416
6417 static void
6418   vl_api_ikev2_profile_set_auth_t_handler
6419   (vl_api_ikev2_profile_set_auth_t * mp)
6420 {
6421   vl_api_ikev2_profile_set_auth_reply_t *rmp;
6422   int rv = 0;
6423
6424 #if IPSEC > 0
6425   vlib_main_t *vm = vlib_get_main ();
6426   clib_error_t *error;
6427   u8 *tmp = format (0, "%s", mp->name);
6428   u8 *data = vec_new (u8, mp->data_len);
6429   clib_memcpy (data, mp->data, mp->data_len);
6430   error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
6431   vec_free (tmp);
6432   vec_free (data);
6433   if (error)
6434     rv = VNET_API_ERROR_UNSPECIFIED;
6435 #else
6436   rv = VNET_API_ERROR_UNIMPLEMENTED;
6437 #endif
6438
6439   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
6440 }
6441
6442 static void
6443 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
6444 {
6445   vl_api_ikev2_profile_add_del_reply_t *rmp;
6446   int rv = 0;
6447
6448 #if IPSEC > 0
6449   vlib_main_t *vm = vlib_get_main ();
6450   clib_error_t *error;
6451   u8 *tmp = format (0, "%s", mp->name);
6452   u8 *data = vec_new (u8, mp->data_len);
6453   clib_memcpy (data, mp->data, mp->data_len);
6454   error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
6455   vec_free (tmp);
6456   vec_free (data);
6457   if (error)
6458     rv = VNET_API_ERROR_UNSPECIFIED;
6459 #else
6460   rv = VNET_API_ERROR_UNIMPLEMENTED;
6461 #endif
6462
6463   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
6464 }
6465
6466 static void
6467 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
6468 {
6469   vl_api_ikev2_profile_set_ts_reply_t *rmp;
6470   int rv = 0;
6471
6472 #if IPSEC > 0
6473   vlib_main_t *vm = vlib_get_main ();
6474   clib_error_t *error;
6475   u8 *tmp = format (0, "%s", mp->name);
6476   error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
6477                                 mp->end_port, (ip4_address_t) mp->start_addr,
6478                                 (ip4_address_t) mp->end_addr, mp->is_local);
6479   vec_free (tmp);
6480   if (error)
6481     rv = VNET_API_ERROR_UNSPECIFIED;
6482 #else
6483   rv = VNET_API_ERROR_UNIMPLEMENTED;
6484 #endif
6485
6486   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
6487 }
6488
6489 static void
6490 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
6491 {
6492   vl_api_ikev2_profile_set_ts_reply_t *rmp;
6493   int rv = 0;
6494
6495 #if IPSEC > 0
6496   vlib_main_t *vm = vlib_get_main ();
6497   clib_error_t *error;
6498
6499   error = ikev2_set_local_key (vm, mp->key_file);
6500   if (error)
6501     rv = VNET_API_ERROR_UNSPECIFIED;
6502 #else
6503   rv = VNET_API_ERROR_UNIMPLEMENTED;
6504 #endif
6505
6506   REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
6507 }
6508
6509 static void
6510 vl_api_map_add_domain_t_handler (vl_api_map_add_domain_t * mp)
6511 {
6512   vl_api_map_add_domain_reply_t *rmp;
6513   int rv = 0;
6514   u32 index;
6515   u8 flags = mp->is_translation ? MAP_DOMAIN_TRANSLATION : 0;
6516   rv =
6517     map_create_domain ((ip4_address_t *) & mp->ip4_prefix, mp->ip4_prefix_len,
6518                        (ip6_address_t *) & mp->ip6_prefix, mp->ip6_prefix_len,
6519                        (ip6_address_t *) & mp->ip6_src,
6520                        mp->ip6_src_prefix_len, mp->ea_bits_len,
6521                        mp->psid_offset, mp->psid_length, &index,
6522                        ntohs (mp->mtu), flags);
6523
6524   /* *INDENT-OFF* */
6525   REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
6526   ({
6527     rmp->index = ntohl(index);
6528   }));
6529   /* *INDENT-ON* */
6530 }
6531
6532 static void
6533 vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp)
6534 {
6535   vl_api_map_del_domain_reply_t *rmp;
6536   int rv = 0;
6537
6538   rv = map_delete_domain (ntohl (mp->index));
6539
6540   REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY);
6541 }
6542
6543 static void
6544 vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp)
6545 {
6546   vl_api_map_del_domain_reply_t *rmp;
6547   int rv = 0;
6548
6549   rv =
6550     map_add_del_psid (ntohl (mp->index), ntohs (mp->psid),
6551                       (ip6_address_t *) mp->ip6_dst, mp->is_add);
6552
6553   REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY);
6554 }
6555
6556 static void
6557 vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
6558 {
6559   vl_api_map_domain_details_t *rmp;
6560   map_main_t *mm = &map_main;
6561   map_domain_t *d;
6562   unix_shared_memory_queue_t *q;
6563
6564   if (pool_elts (mm->domains) == 0)
6565     return;
6566
6567   q = vl_api_client_index_to_input_queue (mp->client_index);
6568   if (q == 0)
6569     {
6570       return;
6571     }
6572
6573   /* *INDENT-OFF* */
6574   pool_foreach(d, mm->domains,
6575   ({
6576     /* Make sure every field is initiated (or don't skip the memset()) */
6577     rmp = vl_msg_api_alloc (sizeof (*rmp));
6578     rmp->_vl_msg_id = ntohs(VL_API_MAP_DOMAIN_DETAILS);
6579     rmp->domain_index = htonl(d - mm->domains);
6580     rmp->ea_bits_len = d->ea_bits_len;
6581     rmp->psid_offset = d->psid_offset;
6582     rmp->psid_length = d->psid_length;
6583     clib_memcpy(rmp->ip4_prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix));
6584     rmp->ip4_prefix_len = d->ip4_prefix_len;
6585     clib_memcpy(rmp->ip6_prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix));
6586     rmp->ip6_prefix_len = d->ip6_prefix_len;
6587     clib_memcpy(rmp->ip6_src, &d->ip6_src, sizeof(rmp->ip6_src));
6588     rmp->ip6_src_len = d->ip6_src_len;
6589     rmp->mtu = htons(d->mtu);
6590     rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION);
6591     rmp->context = mp->context;
6592
6593     vl_msg_api_send_shmem (q, (u8 *)&rmp);
6594   }));
6595   /* *INDENT-ON* */
6596 }
6597
6598 static void
6599 vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
6600 {
6601   unix_shared_memory_queue_t *q;
6602   u16 i;
6603   ip6_address_t dst;
6604   vl_api_map_rule_details_t *rmp;
6605   map_main_t *mm = &map_main;
6606   u32 domain_index = ntohl (mp->domain_index);
6607   map_domain_t *d;
6608
6609   if (pool_elts (mm->domains) == 0)
6610     return;
6611
6612   d = pool_elt_at_index (mm->domains, domain_index);
6613   if (!d || !d->rules)
6614     {
6615       return;
6616     }
6617
6618   q = vl_api_client_index_to_input_queue (mp->client_index);
6619   if (q == 0)
6620     {
6621       return;
6622     }
6623
6624   for (i = 0; i < (0x1 << d->psid_length); i++)
6625     {
6626       dst = d->rules[i];
6627       if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
6628         {
6629           continue;
6630         }
6631       rmp = vl_msg_api_alloc (sizeof (*rmp));
6632       memset (rmp, 0, sizeof (*rmp));
6633       rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS);
6634       rmp->psid = htons (i);
6635       clib_memcpy (rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
6636       rmp->context = mp->context;
6637       vl_msg_api_send_shmem (q, (u8 *) & rmp);
6638     }
6639 }
6640
6641 static void
6642 vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
6643 {
6644   vl_api_map_summary_stats_reply_t *rmp;
6645   vlib_combined_counter_main_t *cm;
6646   vlib_counter_t v;
6647   int i, which;
6648   u64 total_pkts[VLIB_N_RX_TX];
6649   u64 total_bytes[VLIB_N_RX_TX];
6650   map_main_t *mm = &map_main;
6651   unix_shared_memory_queue_t *q =
6652     vl_api_client_index_to_input_queue (mp->client_index);
6653
6654   if (!q)
6655     return;
6656
6657   rmp = vl_msg_api_alloc (sizeof (*rmp));
6658   rmp->_vl_msg_id = ntohs (VL_API_MAP_SUMMARY_STATS_REPLY);
6659   rmp->context = mp->context;
6660   rmp->retval = 0;
6661
6662   memset (total_pkts, 0, sizeof (total_pkts));
6663   memset (total_bytes, 0, sizeof (total_bytes));
6664
6665   map_domain_counter_lock (mm);
6666   vec_foreach (cm, mm->domain_counters)
6667   {
6668     which = cm - mm->domain_counters;
6669
6670     for (i = 0; i < vec_len (cm->maxi); i++)
6671       {
6672         vlib_get_combined_counter (cm, i, &v);
6673         total_pkts[which] += v.packets;
6674         total_bytes[which] += v.bytes;
6675       }
6676   }
6677
6678   map_domain_counter_unlock (mm);
6679
6680   /* Note: in network byte order! */
6681   rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] =
6682     clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]);
6683   rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] =
6684     clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]);
6685   rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] =
6686     clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]);
6687   rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] =
6688     clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]);
6689   rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains));
6690   rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
6691   rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] =
6692     clib_host_to_net_u64 (map_error_counter_get
6693                           (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
6694   rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] =
6695     clib_host_to_net_u64 (map_error_counter_get
6696                           (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
6697
6698   vl_msg_api_send_shmem (q, (u8 *) & rmp);
6699 }
6700
6701 static void
6702 vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
6703 {
6704   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6705   vl_api_ipsec_sa_set_key_reply_t *rmp;
6706   int rv;
6707 #if IPSEC > 0
6708   ipsec_sa_t sa;
6709   sa.id = ntohl (mp->sa_id);
6710   sa.crypto_key_len = mp->crypto_key_length;
6711   clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
6712   sa.integ_key_len = mp->integrity_key_length;
6713   clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
6714
6715   rv = ipsec_set_sa_key (vm, &sa);
6716 #else
6717   rv = VNET_API_ERROR_UNIMPLEMENTED;
6718 #endif
6719
6720   REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
6721 }
6722
6723 static void vl_api_cop_interface_enable_disable_t_handler
6724   (vl_api_cop_interface_enable_disable_t * mp)
6725 {
6726   vl_api_cop_interface_enable_disable_reply_t *rmp;
6727   int rv;
6728   u32 sw_if_index = ntohl (mp->sw_if_index);
6729   int enable_disable;
6730
6731   VALIDATE_SW_IF_INDEX (mp);
6732
6733   enable_disable = (int) mp->enable_disable;
6734
6735   rv = cop_interface_enable_disable (sw_if_index, enable_disable);
6736
6737   BAD_SW_IF_INDEX_LABEL;
6738
6739   REPLY_MACRO (VL_API_COP_INTERFACE_ENABLE_DISABLE_REPLY);
6740 }
6741
6742 static void vl_api_cop_whitelist_enable_disable_t_handler
6743   (vl_api_cop_whitelist_enable_disable_t * mp)
6744 {
6745   vl_api_cop_whitelist_enable_disable_reply_t *rmp;
6746   cop_whitelist_enable_disable_args_t _a, *a = &_a;
6747   u32 sw_if_index = ntohl (mp->sw_if_index);
6748   int rv;
6749
6750   VALIDATE_SW_IF_INDEX (mp);
6751
6752   a->sw_if_index = sw_if_index;
6753   a->ip4 = mp->ip4;
6754   a->ip6 = mp->ip6;
6755   a->default_cop = mp->default_cop;
6756   a->fib_id = ntohl (mp->fib_id);
6757
6758   rv = cop_whitelist_enable_disable (a);
6759
6760   BAD_SW_IF_INDEX_LABEL;
6761
6762   REPLY_MACRO (VL_API_COP_WHITELIST_ENABLE_DISABLE_REPLY);
6763 }
6764
6765 static void
6766 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
6767 {
6768   int rv = 0;
6769   u8 *vector = 0;
6770   api_main_t *am = &api_main;
6771   vlib_main_t *vm = vlib_get_main ();
6772   void *oldheap;
6773   vl_api_get_node_graph_reply_t *rmp;
6774
6775   pthread_mutex_lock (&am->vlib_rp->mutex);
6776   oldheap = svm_push_data_heap (am->vlib_rp);
6777
6778   /*
6779    * Keep the number of memcpy ops to a minimum (e.g. 1).
6780    */
6781   vec_validate (vector, 16384);
6782   vec_reset_length (vector);
6783
6784   /* $$$$ FIXME */
6785   vector = vlib_node_serialize (&vm->node_main, vector,
6786                                 (u32) ~ 0 /* all threads */ ,
6787                                 1 /* include nexts */ ,
6788                                 1 /* include stats */ );
6789
6790   svm_pop_heap (oldheap);
6791   pthread_mutex_unlock (&am->vlib_rp->mutex);
6792
6793   /* *INDENT-OFF* */
6794   REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
6795   ({
6796     rmp->reply_in_shmem = (uword) vector;
6797   }));
6798   /* *INDENT-ON* */
6799 }
6800
6801 static void
6802 vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
6803 {
6804   int rv = 0;
6805   vl_api_ioam_enable_reply_t *rmp;
6806   clib_error_t *error;
6807
6808   /* Ignoring the profile id as currently a single profile
6809    * is supported */
6810   error = ip6_ioam_enable (mp->trace_enable, mp->pow_enable, mp->trace_ppc);
6811   if (error)
6812     {
6813       clib_error_report (error);
6814       rv = clib_error_get_code (error);
6815     }
6816
6817   REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
6818 }
6819
6820 static void
6821 vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
6822 {
6823   int rv = 0;
6824   vl_api_ioam_disable_reply_t *rmp;
6825   clib_error_t *error;
6826
6827   error = clear_ioam_rewrite_fn ();
6828   if (error)
6829     {
6830       clib_error_report (error);
6831       rv = clib_error_get_code (error);
6832     }
6833
6834   REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
6835 }
6836
6837 static void
6838 vl_api_af_packet_create_t_handler (vl_api_af_packet_create_t * mp)
6839 {
6840   vlib_main_t *vm = vlib_get_main ();
6841   vl_api_af_packet_create_reply_t *rmp;
6842   int rv = 0;
6843   u8 *host_if_name = NULL;
6844   u32 sw_if_index;
6845
6846   host_if_name = format (0, "%s", mp->host_if_name);
6847   vec_add1 (host_if_name, 0);
6848
6849   rv = af_packet_create_if (vm, host_if_name,
6850                             mp->use_random_hw_addr ? 0 : mp->hw_addr,
6851                             &sw_if_index);
6852
6853   vec_free (host_if_name);
6854
6855   /* *INDENT-OFF* */
6856   REPLY_MACRO2(VL_API_AF_PACKET_CREATE_REPLY,
6857   ({
6858     rmp->sw_if_index = clib_host_to_net_u32(sw_if_index);
6859   }));
6860   /* *INDENT-ON* */
6861 }
6862
6863 static void
6864 vl_api_af_packet_delete_t_handler (vl_api_af_packet_delete_t * mp)
6865 {
6866   vlib_main_t *vm = vlib_get_main ();
6867   vl_api_af_packet_delete_reply_t *rmp;
6868   int rv = 0;
6869   u8 *host_if_name = NULL;
6870
6871   host_if_name = format (0, "%s", mp->host_if_name);
6872   vec_add1 (host_if_name, 0);
6873
6874   rv = af_packet_delete_if (vm, host_if_name);
6875
6876   vec_free (host_if_name);
6877
6878   REPLY_MACRO (VL_API_AF_PACKET_DELETE_REPLY);
6879 }
6880
6881 static void
6882 vl_api_policer_add_del_t_handler (vl_api_policer_add_del_t * mp)
6883 {
6884   vlib_main_t *vm = vlib_get_main ();
6885   vl_api_policer_add_del_reply_t *rmp;
6886   int rv = 0;
6887   u8 *name = NULL;
6888   sse2_qos_pol_cfg_params_st cfg;
6889   clib_error_t *error;
6890   u32 policer_index;
6891
6892   name = format (0, "%s", mp->name);
6893
6894   memset (&cfg, 0, sizeof (cfg));
6895   cfg.rfc = mp->type;
6896   cfg.rnd_type = mp->round_type;
6897   cfg.rate_type = mp->rate_type;
6898   cfg.rb.kbps.cir_kbps = mp->cir;
6899   cfg.rb.kbps.eir_kbps = mp->eir;
6900   cfg.rb.kbps.cb_bytes = mp->cb;
6901   cfg.rb.kbps.eb_bytes = mp->eb;
6902   cfg.conform_action.action_type = mp->conform_action_type;
6903   cfg.conform_action.dscp = mp->conform_dscp;
6904   cfg.exceed_action.action_type = mp->exceed_action_type;
6905   cfg.exceed_action.dscp = mp->exceed_dscp;
6906   cfg.violate_action.action_type = mp->violate_action_type;
6907   cfg.violate_action.dscp = mp->violate_dscp;
6908   cfg.color_aware = mp->color_aware;
6909
6910   error = policer_add_del (vm, name, &cfg, &policer_index, mp->is_add);
6911
6912   if (error)
6913     rv = VNET_API_ERROR_UNSPECIFIED;
6914
6915   /* *INDENT-OFF* */
6916   REPLY_MACRO2(VL_API_POLICER_ADD_DEL_REPLY,
6917   ({
6918     if (rv == 0 &&  mp->is_add)
6919       rmp->policer_index = ntohl(policer_index);
6920     else
6921       rmp->policer_index = ~0;
6922   }));
6923   /* *INDENT-ON* */
6924 }
6925
6926 static void
6927 send_policer_details (u8 * name,
6928                       sse2_qos_pol_cfg_params_st * config,
6929                       policer_read_response_type_st * templ,
6930                       unix_shared_memory_queue_t * q, u32 context)
6931 {
6932   vl_api_policer_details_t *mp;
6933
6934   mp = vl_msg_api_alloc (sizeof (*mp));
6935   memset (mp, 0, sizeof (*mp));
6936   mp->_vl_msg_id = ntohs (VL_API_POLICER_DETAILS);
6937   mp->context = context;
6938   mp->cir = htonl (config->rb.kbps.cir_kbps);
6939   mp->eir = htonl (config->rb.kbps.eir_kbps);
6940   mp->cb = htonl (config->rb.kbps.cb_bytes);
6941   mp->eb = htonl (config->rb.kbps.eb_bytes);
6942   mp->rate_type = config->rate_type;
6943   mp->round_type = config->rnd_type;
6944   mp->type = config->rfc;
6945   mp->conform_action_type = config->conform_action.action_type;
6946   mp->conform_dscp = config->conform_action.dscp;
6947   mp->exceed_action_type = config->exceed_action.action_type;
6948   mp->exceed_dscp = config->exceed_action.dscp;
6949   mp->violate_action_type = config->violate_action.action_type;
6950   mp->violate_dscp = config->violate_action.dscp;
6951   mp->single_rate = templ->single_rate ? 1 : 0;
6952   mp->color_aware = templ->color_aware ? 1 : 0;
6953   mp->scale = htonl (templ->scale);
6954   mp->cir_tokens_per_period = htonl (templ->cir_tokens_per_period);
6955   mp->pir_tokens_per_period = htonl (templ->pir_tokens_per_period);
6956   mp->current_limit = htonl (templ->current_limit);
6957   mp->current_bucket = htonl (templ->current_bucket);
6958   mp->extended_limit = htonl (templ->extended_limit);
6959   mp->extended_bucket = htonl (templ->extended_bucket);
6960   mp->last_update_time = clib_host_to_net_u64 (templ->last_update_time);
6961
6962   strncpy ((char *) mp->name, (char *) name, ARRAY_LEN (mp->name) - 1);
6963
6964   vl_msg_api_send_shmem (q, (u8 *) & mp);
6965 }
6966
6967 static void
6968 vl_api_policer_dump_t_handler (vl_api_policer_dump_t * mp)
6969 {
6970   unix_shared_memory_queue_t *q;
6971   vnet_policer_main_t *pm = &vnet_policer_main;
6972   hash_pair_t *hp;
6973   uword *p;
6974   u32 pool_index;
6975   u8 *match_name = 0;
6976   u8 *name;
6977   sse2_qos_pol_cfg_params_st *config;
6978   policer_read_response_type_st *templ;
6979
6980   q = vl_api_client_index_to_input_queue (mp->client_index);
6981   if (q == 0)
6982     return;
6983
6984   if (mp->match_name_valid)
6985     {
6986       match_name = format (0, "%s%c", mp->match_name, 0);
6987     }
6988
6989   if (mp->match_name_valid)
6990     {
6991       p = hash_get_mem (pm->policer_config_by_name, match_name);
6992       if (p)
6993         {
6994           pool_index = p[0];
6995           config = pool_elt_at_index (pm->configs, pool_index);
6996           templ = pool_elt_at_index (pm->policer_templates, pool_index);
6997           send_policer_details (match_name, config, templ, q, mp->context);
6998         }
6999     }
7000   else
7001     {
7002       /* *INDENT-OFF* */
7003       hash_foreach_pair (hp, pm->policer_config_by_name,
7004       ({
7005         name = (u8 *) hp->key;
7006         pool_index = hp->value[0];
7007         config = pool_elt_at_index (pm->configs, pool_index);
7008         templ = pool_elt_at_index (pm->policer_templates, pool_index);
7009         send_policer_details(name, config, templ, q, mp->context);
7010       }));
7011       /* *INDENT-ON* */
7012     }
7013 }
7014
7015 static void
7016   vl_api_policer_classify_set_interface_t_handler
7017   (vl_api_policer_classify_set_interface_t * mp)
7018 {
7019   vlib_main_t *vm = vlib_get_main ();
7020   vl_api_policer_classify_set_interface_reply_t *rmp;
7021   int rv;
7022   u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
7023
7024   ip4_table_index = ntohl (mp->ip4_table_index);
7025   ip6_table_index = ntohl (mp->ip6_table_index);
7026   l2_table_index = ntohl (mp->l2_table_index);
7027   sw_if_index = ntohl (mp->sw_if_index);
7028
7029   VALIDATE_SW_IF_INDEX (mp);
7030
7031   rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index,
7032                                         ip6_table_index, l2_table_index,
7033                                         mp->is_add);
7034
7035   BAD_SW_IF_INDEX_LABEL;
7036
7037   REPLY_MACRO (VL_API_POLICER_CLASSIFY_SET_INTERFACE_REPLY);
7038 }
7039
7040 static void
7041 send_policer_classify_details (u32 sw_if_index,
7042                                u32 table_index,
7043                                unix_shared_memory_queue_t * q, u32 context)
7044 {
7045   vl_api_policer_classify_details_t *mp;
7046
7047   mp = vl_msg_api_alloc (sizeof (*mp));
7048   memset (mp, 0, sizeof (*mp));
7049   mp->_vl_msg_id = ntohs (VL_API_POLICER_CLASSIFY_DETAILS);
7050   mp->context = context;
7051   mp->sw_if_index = htonl (sw_if_index);
7052   mp->table_index = htonl (table_index);
7053
7054   vl_msg_api_send_shmem (q, (u8 *) & mp);
7055 }
7056
7057 static void
7058 vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
7059 {
7060   unix_shared_memory_queue_t *q;
7061   policer_classify_main_t *pcm = &policer_classify_main;
7062   u32 *vec_tbl;
7063   int i;
7064
7065   q = vl_api_client_index_to_input_queue (mp->client_index);
7066   if (q == 0)
7067     return;
7068
7069   vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
7070
7071   if (vec_len (vec_tbl))
7072     {
7073       for (i = 0; i < vec_len (vec_tbl); i++)
7074         {
7075           if (vec_elt (vec_tbl, i) == ~0)
7076             continue;
7077
7078           send_policer_classify_details (i, vec_elt (vec_tbl, i), q,
7079                                          mp->context);
7080         }
7081     }
7082 }
7083
7084 static void
7085 vl_api_netmap_create_t_handler (vl_api_netmap_create_t * mp)
7086 {
7087   vlib_main_t *vm = vlib_get_main ();
7088   vl_api_netmap_create_reply_t *rmp;
7089   int rv = 0;
7090   u8 *if_name = NULL;
7091
7092   if_name = format (0, "%s", mp->netmap_if_name);
7093   vec_add1 (if_name, 0);
7094
7095   rv =
7096     netmap_create_if (vm, if_name, mp->use_random_hw_addr ? 0 : mp->hw_addr,
7097                       mp->is_pipe, mp->is_master, 0);
7098
7099   vec_free (if_name);
7100
7101   REPLY_MACRO (VL_API_NETMAP_CREATE_REPLY);
7102 }
7103
7104 static void
7105 vl_api_netmap_delete_t_handler (vl_api_netmap_delete_t * mp)
7106 {
7107   vlib_main_t *vm = vlib_get_main ();
7108   vl_api_netmap_delete_reply_t *rmp;
7109   int rv = 0;
7110   u8 *if_name = NULL;
7111
7112   if_name = format (0, "%s", mp->netmap_if_name);
7113   vec_add1 (if_name, 0);
7114
7115   rv = netmap_delete_if (vm, if_name);
7116
7117   vec_free (if_name);
7118
7119   REPLY_MACRO (VL_API_NETMAP_DELETE_REPLY);
7120 }
7121
7122 static void
7123 vl_api_mpls_gre_tunnel_details_t_handler (vl_api_mpls_gre_tunnel_details_t *
7124                                           mp)
7125 {
7126   clib_warning ("BUG");
7127 }
7128
7129 static void
7130 send_mpls_gre_tunnel_entry (vpe_api_main_t * am,
7131                             unix_shared_memory_queue_t * q,
7132                             mpls_gre_tunnel_t * gt, u32 index, u32 context)
7133 {
7134   vl_api_mpls_gre_tunnel_details_t *mp;
7135   mpls_main_t *mm = &mpls_main;
7136   mpls_encap_t *e;
7137   int i;
7138   u32 nlabels;
7139
7140   e = pool_elt_at_index (mm->encaps, gt->encap_index);
7141   nlabels = vec_len (e->labels);
7142
7143   mp = vl_msg_api_alloc (sizeof (*mp) + nlabels * sizeof (u32));
7144   memset (mp, 0, sizeof (*mp));
7145   mp->_vl_msg_id = ntohs (VL_API_MPLS_GRE_TUNNEL_DETAILS);
7146   mp->context = context;
7147
7148   mp->tunnel_index = htonl (index);
7149   mp->tunnel_src = gt->tunnel_src.as_u32;
7150   mp->tunnel_dst = gt->tunnel_dst.as_u32;
7151   mp->intfc_address = gt->intfc_address.as_u32;
7152   mp->mask_width = htonl (gt->mask_width);
7153   mp->inner_fib_index = htonl (gt->inner_fib_index);
7154   mp->outer_fib_index = htonl (gt->outer_fib_index);
7155   mp->encap_index = htonl (gt->encap_index);
7156   mp->hw_if_index = htonl (gt->hw_if_index);
7157   mp->l2_only = htonl (gt->l2_only);
7158   mp->nlabels = htonl (nlabels);
7159
7160   for (i = 0; i < nlabels; i++)
7161     {
7162       mp->labels[i] =
7163         htonl (vnet_mpls_uc_get_label
7164                (clib_host_to_net_u32 (e->labels[i].label_exp_s_ttl)));
7165     }
7166
7167   vl_msg_api_send_shmem (q, (u8 *) & mp);
7168 }
7169
7170 static void
7171 vl_api_mpls_gre_tunnel_dump_t_handler (vl_api_mpls_gre_tunnel_dump_t * mp)
7172 {
7173   vpe_api_main_t *am = &vpe_api_main;
7174   unix_shared_memory_queue_t *q;
7175   mpls_main_t *mm = &mpls_main;
7176   mpls_gre_tunnel_t *gt;
7177   u32 index = ntohl (mp->tunnel_index);
7178
7179   q = vl_api_client_index_to_input_queue (mp->client_index);
7180   if (q == 0)
7181     return;
7182
7183   if (index != ~0)
7184     {
7185       if (!pool_is_free_index (mm->gre_tunnels, index))
7186         {
7187           gt = pool_elt_at_index (mm->gre_tunnels, index);
7188           send_mpls_gre_tunnel_entry (am, q, gt, gt - mm->gre_tunnels,
7189                                       mp->context);
7190         }
7191     }
7192   else
7193     {
7194       /* *INDENT-OFF* */
7195       pool_foreach (gt, mm->gre_tunnels,
7196       ({
7197         send_mpls_gre_tunnel_entry (am, q, gt, gt - mm->gre_tunnels,
7198                                     mp->context);
7199       }));
7200       /* *INDENT-ON* */
7201     }
7202 }
7203
7204 static void
7205 vl_api_mpls_eth_tunnel_details_t_handler (vl_api_mpls_eth_tunnel_details_t *
7206                                           mp)
7207 {
7208   clib_warning ("BUG");
7209 }
7210
7211 static void
7212 send_mpls_eth_tunnel_entry (vpe_api_main_t * am,
7213                             unix_shared_memory_queue_t * q,
7214                             mpls_eth_tunnel_t * et, u32 index, u32 context)
7215 {
7216   mpls_main_t *mm = &mpls_main;
7217   mpls_encap_t *e;
7218   int i;
7219   u32 nlabels;
7220   vl_api_mpls_eth_tunnel_details_t *mp;
7221
7222   e = pool_elt_at_index (mm->encaps, et->encap_index);
7223   nlabels = vec_len (e->labels);
7224
7225   mp = vl_msg_api_alloc (sizeof (*mp) + nlabels * sizeof (u32));
7226   memset (mp, 0, sizeof (*mp));
7227   mp->_vl_msg_id = ntohs (VL_API_MPLS_ETH_TUNNEL_DETAILS);
7228   mp->context = context;
7229
7230   mp->tunnel_index = htonl (index);
7231   memcpy (mp->tunnel_dst_mac, et->tunnel_dst, 6);
7232   mp->intfc_address = et->intfc_address.as_u32;
7233   mp->tx_sw_if_index = htonl (et->tx_sw_if_index);
7234   mp->inner_fib_index = htonl (et->inner_fib_index);
7235   mp->mask_width = htonl (et->mask_width);
7236   mp->encap_index = htonl (et->encap_index);
7237   mp->hw_if_index = htonl (et->hw_if_index);
7238   mp->l2_only = htonl (et->l2_only);
7239   mp->nlabels = htonl (nlabels);
7240
7241   for (i = 0; i < nlabels; i++)
7242     {
7243       mp->labels[i] =
7244         htonl (vnet_mpls_uc_get_label
7245                (clib_host_to_net_u32 (e->labels[i].label_exp_s_ttl)));
7246     }
7247
7248   vl_msg_api_send_shmem (q, (u8 *) & mp);
7249 }
7250
7251 static void
7252 vl_api_mpls_eth_tunnel_dump_t_handler (vl_api_mpls_eth_tunnel_dump_t * mp)
7253 {
7254   vpe_api_main_t *am = &vpe_api_main;
7255   unix_shared_memory_queue_t *q;
7256   mpls_main_t *mm = &mpls_main;
7257   mpls_eth_tunnel_t *et;
7258   u32 index = ntohl (mp->tunnel_index);
7259
7260   q = vl_api_client_index_to_input_queue (mp->client_index);
7261   if (q == 0)
7262     return;
7263
7264   if (index != ~0)
7265     {
7266       if (!pool_is_free_index (mm->eth_tunnels, index))
7267         {
7268           et = pool_elt_at_index (mm->eth_tunnels, index);
7269           send_mpls_eth_tunnel_entry (am, q, et, et - mm->eth_tunnels,
7270                                       mp->context);
7271         }
7272     }
7273   else
7274     {
7275       /* *INDENT-OFF* */
7276       pool_foreach (et, mm->eth_tunnels,
7277       ({
7278         send_mpls_eth_tunnel_entry (am, q, et, et - mm->eth_tunnels,
7279                                     mp->context);
7280       }));
7281       /* *INDENT-ON* */
7282     }
7283 }
7284
7285 static void
7286 vl_api_mpls_fib_encap_details_t_handler (vl_api_mpls_fib_encap_details_t * mp)
7287 {
7288   clib_warning ("BUG");
7289 }
7290
7291 static void
7292 send_mpls_fib_encap_details (vpe_api_main_t * am,
7293                              unix_shared_memory_queue_t * q,
7294                              show_mpls_fib_t * s, u32 context)
7295 {
7296   vl_api_mpls_fib_encap_details_t *mp;
7297   mpls_main_t *mm = &mpls_main;
7298   mpls_encap_t *e;
7299   int i;
7300   u32 nlabels;
7301
7302   e = pool_elt_at_index (mm->encaps, s->entry_index);
7303   nlabels = vec_len (e->labels);
7304
7305   mp = vl_msg_api_alloc (sizeof (*mp) + nlabels * sizeof (u32));
7306   memset (mp, 0, sizeof (*mp));
7307   mp->_vl_msg_id = ntohs (VL_API_MPLS_FIB_ENCAP_DETAILS);
7308   mp->context = context;
7309
7310   mp->fib_index = htonl (s->fib_index);
7311   mp->entry_index = htonl (s->entry_index);
7312   mp->dest = s->dest;
7313   mp->s_bit = htonl (s->s_bit);
7314
7315   mp->nlabels = htonl (nlabels);
7316
7317   for (i = 0; i < nlabels; i++)
7318     {
7319       mp->labels[i] =
7320         htonl (vnet_mpls_uc_get_label
7321                (clib_host_to_net_u32 (e->labels[i].label_exp_s_ttl)));
7322     }
7323
7324   vl_msg_api_send_shmem (q, (u8 *) & mp);
7325 }
7326
7327 static void
7328 vl_api_mpls_fib_encap_dump_t_handler (vl_api_mpls_fib_encap_dump_t * mp)
7329 {
7330   vpe_api_main_t *am = &vpe_api_main;
7331   unix_shared_memory_queue_t *q;
7332   vlib_main_t *vm = &vlib_global_main;
7333   u64 key;
7334   u32 value;
7335   show_mpls_fib_t *records = 0;
7336   show_mpls_fib_t *s;
7337   mpls_main_t *mm = &mpls_main;
7338   ip4_fib_t *rx_fib;
7339
7340   q = vl_api_client_index_to_input_queue (mp->client_index);
7341   if (q == 0)
7342     return;
7343
7344   /* *INDENT-OFF* */
7345   hash_foreach (key, value, mm->mpls_encap_by_fib_and_dest,
7346   ({
7347     vec_add2 (records, s, 1);
7348     s->fib_index = (u32)(key>>32);
7349     s->dest = (u32)(key & 0xFFFFFFFF);
7350     s->entry_index = (u32) value;
7351   }));
7352   /* *INDENT-ON* */
7353
7354   if (0 == vec_len (records))
7355     {
7356       vlib_cli_output (vm, "MPLS encap table empty");
7357       goto out;
7358     }
7359
7360   /* sort output by dst address within fib */
7361   vec_sort_with_function (records, mpls_dest_cmp);
7362   vec_sort_with_function (records, mpls_fib_index_cmp);
7363   vlib_cli_output (vm, "MPLS encap table");
7364   vlib_cli_output (vm, "%=6s%=16s%=16s", "Table", "Dest address", "Labels");
7365   vec_foreach (s, records)
7366   {
7367     rx_fib = ip4_fib_get (s->fib_index);
7368     vlib_cli_output (vm, "%=6d%=16U%=16U", rx_fib->table_id,
7369                      format_ip4_address, &s->dest, format_mpls_encap_index,
7370                      mm, s->entry_index);
7371     send_mpls_fib_encap_details (am, q, s, mp->context);
7372   }
7373
7374 out:
7375   vec_free (records);
7376 }
7377
7378 static void
7379 vl_api_mpls_fib_decap_details_t_handler (vl_api_mpls_fib_decap_details_t * mp)
7380 {
7381   clib_warning ("BUG");
7382 }
7383
7384 static void
7385 send_mpls_fib_decap_details (vpe_api_main_t * am,
7386                              unix_shared_memory_queue_t * q,
7387                              show_mpls_fib_t * s,
7388                              u32 rx_table_id,
7389                              u32 tx_table_id, char *swif_tag, u32 context)
7390 {
7391   vl_api_mpls_fib_decap_details_t *mp;
7392
7393   mp = vl_msg_api_alloc (sizeof (*mp));
7394   memset (mp, 0, sizeof (*mp));
7395   mp->_vl_msg_id = ntohs (VL_API_MPLS_FIB_DECAP_DETAILS);
7396   mp->context = context;
7397
7398   mp->fib_index = htonl (s->fib_index);
7399   mp->entry_index = htonl (s->entry_index);
7400   mp->dest = s->dest;
7401   mp->s_bit = htonl (s->s_bit);
7402   mp->label = htonl (s->label);
7403   mp->rx_table_id = htonl (rx_table_id);
7404   mp->tx_table_id = htonl (tx_table_id);
7405   strncpy ((char *) mp->swif_tag,
7406            (char *) swif_tag, ARRAY_LEN (mp->swif_tag) - 1);
7407
7408   vl_msg_api_send_shmem (q, (u8 *) & mp);
7409 }
7410
7411 static void
7412 vl_api_mpls_fib_decap_dump_t_handler (vl_api_mpls_fib_decap_dump_t * mp)
7413 {
7414   vpe_api_main_t *am = &vpe_api_main;
7415   unix_shared_memory_queue_t *q;
7416   vlib_main_t *vm = &vlib_global_main;
7417   u64 key;
7418   u32 value;
7419   show_mpls_fib_t *records = 0;
7420   show_mpls_fib_t *s;
7421   mpls_main_t *mm = &mpls_main;
7422   ip4_fib_t *rx_fib;
7423   ip4_fib_t *tx_fib;
7424   u32 tx_table_id;
7425   char *swif_tag;
7426
7427   q = vl_api_client_index_to_input_queue (mp->client_index);
7428   if (q == 0)
7429     return;
7430
7431   /* *INDENT-OFF* */
7432   hash_foreach (key, value, mm->mpls_decap_by_rx_fib_and_label,
7433   ({
7434     vec_add2 (records, s, 1);
7435     s->fib_index = (u32)(key>>32);
7436     s->entry_index = (u32) value;
7437     s->label = ((u32) key)>>12;
7438     s->s_bit = (key & (1<<8)) != 0;
7439   }));
7440   /* *INDENT-ON* */
7441
7442   if (!vec_len (records))
7443     {
7444       vlib_cli_output (vm, "MPLS decap table empty");
7445       goto out;
7446     }
7447
7448   vec_sort_with_function (records, mpls_label_cmp);
7449   vlib_cli_output (vm, "MPLS decap table");
7450   vlib_cli_output (vm, "%=10s%=15s%=6s%=6s", "RX Table", "TX Table/Intfc",
7451                    "Label", "S-bit");
7452   vec_foreach (s, records)
7453   {
7454     mpls_decap_t *d;
7455     d = pool_elt_at_index (mm->decaps, s->entry_index);
7456     if (d->next_index == MPLS_LOOKUP_NEXT_IP4_INPUT)
7457       {
7458         tx_fib = ip4_fib_get (d->tx_fib_index);
7459         tx_table_id = tx_fib->table_id;
7460         swif_tag = "     ";
7461       }
7462     else
7463       {
7464         tx_table_id = d->tx_fib_index;
7465         swif_tag = "(i)  ";
7466       }
7467     rx_fib = ip4_fib_get (s->fib_index);
7468
7469     vlib_cli_output (vm, "%=10d%=10d%=5s%=6d%=6d", rx_fib->table_id,
7470                      tx_table_id, swif_tag, s->label, s->s_bit);
7471
7472     send_mpls_fib_decap_details (am, q, s, rx_fib->table_id,
7473                                  tx_table_id, swif_tag, mp->context);
7474   }
7475
7476 out:
7477   vec_free (records);
7478 }
7479
7480 static void
7481 vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
7482 {
7483   unix_shared_memory_queue_t *q;
7484
7485   q = vl_api_client_index_to_input_queue (mp->client_index);
7486   if (q == 0)
7487     return;
7488
7489   vnet_classify_main_t *cm = &vnet_classify_main;
7490   vnet_classify_table_t *t;
7491   u32 *table_ids = 0;
7492   u32 count;
7493
7494   /* *INDENT-OFF* */
7495   pool_foreach (t, cm->tables,
7496   ({
7497     vec_add1 (table_ids, ntohl(t - cm->tables));
7498   }));
7499   /* *INDENT-ON* */
7500   count = vec_len (table_ids);
7501
7502   vl_api_classify_table_ids_reply_t *rmp;
7503   rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
7504   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
7505   rmp->context = mp->context;
7506   rmp->count = ntohl (count);
7507   clib_memcpy (rmp->ids, table_ids, count * sizeof (u32));
7508   rmp->retval = 0;
7509
7510   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7511
7512   vec_free (table_ids);
7513 }
7514
7515 static void
7516   vl_api_classify_table_by_interface_t_handler
7517   (vl_api_classify_table_by_interface_t * mp)
7518 {
7519   vl_api_classify_table_by_interface_reply_t *rmp;
7520   int rv = 0;
7521
7522   u32 sw_if_index = ntohl (mp->sw_if_index);
7523   u32 *acl = 0;
7524
7525   vec_validate (acl, INPUT_ACL_N_TABLES - 1);
7526   vec_set (acl, ~0);
7527
7528   VALIDATE_SW_IF_INDEX (mp);
7529
7530   input_acl_main_t *am = &input_acl_main;
7531
7532   int if_idx;
7533   u32 type;
7534
7535   for (type = 0; type < INPUT_ACL_N_TABLES; type++)
7536     {
7537       u32 *vec_tbl = am->classify_table_index_by_sw_if_index[type];
7538       if (vec_len (vec_tbl))
7539         {
7540           for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
7541             {
7542               if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
7543                 {
7544                   continue;
7545                 }
7546               acl[type] = vec_elt (vec_tbl, if_idx);
7547             }
7548         }
7549     }
7550
7551   BAD_SW_IF_INDEX_LABEL;
7552
7553   /* *INDENT-OFF* */
7554   REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
7555   ({
7556     rmp->sw_if_index = ntohl(sw_if_index);
7557     rmp->l2_table_id = ntohl(acl[INPUT_ACL_TABLE_L2]);
7558     rmp->ip4_table_id = ntohl(acl[INPUT_ACL_TABLE_IP4]);
7559     rmp->ip6_table_id = ntohl(acl[INPUT_ACL_TABLE_IP6]);
7560   }));
7561   /* *INDENT-ON* */
7562   vec_free (acl);
7563 }
7564
7565 static void
7566 vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
7567 {
7568   unix_shared_memory_queue_t *q;
7569
7570   q = vl_api_client_index_to_input_queue (mp->client_index);
7571   if (q == 0)
7572     return;
7573
7574   vl_api_classify_table_info_reply_t *rmp = 0;
7575
7576   vnet_classify_main_t *cm = &vnet_classify_main;
7577   u32 table_id = ntohl (mp->table_id);
7578   vnet_classify_table_t *t;
7579
7580   /* *INDENT-OFF* */
7581   pool_foreach (t, cm->tables,
7582   ({
7583     if (table_id == t - cm->tables)
7584       {
7585         rmp = vl_msg_api_alloc_as_if_client
7586           (sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
7587         rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
7588         rmp->context = mp->context;
7589         rmp->table_id = ntohl(table_id);
7590         rmp->nbuckets = ntohl(t->nbuckets);
7591         rmp->match_n_vectors = ntohl(t->match_n_vectors);
7592         rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
7593         rmp->active_sessions = ntohl(t->active_elements);
7594         rmp->next_table_index = ntohl(t->next_table_index);
7595         rmp->miss_next_index = ntohl(t->miss_next_index);
7596         rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
7597         clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
7598         rmp->retval = 0;
7599         break;
7600       }
7601   }));
7602   /* *INDENT-ON* */
7603
7604   if (rmp == 0)
7605     {
7606       rmp = vl_msg_api_alloc (sizeof (*rmp));
7607       rmp->_vl_msg_id = ntohs ((VL_API_CLASSIFY_TABLE_INFO_REPLY));
7608       rmp->context = mp->context;
7609       rmp->retval = ntohl (VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
7610     }
7611
7612   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7613 }
7614
7615 static void
7616 vl_api_classify_session_details_t_handler (vl_api_classify_session_details_t *
7617                                            mp)
7618 {
7619   clib_warning ("BUG");
7620 }
7621
7622 static void
7623 send_classify_session_details (unix_shared_memory_queue_t * q,
7624                                u32 table_id,
7625                                u32 match_length,
7626                                vnet_classify_entry_t * e, u32 context)
7627 {
7628   vl_api_classify_session_details_t *rmp;
7629
7630   rmp = vl_msg_api_alloc (sizeof (*rmp));
7631   memset (rmp, 0, sizeof (*rmp));
7632   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_SESSION_DETAILS);
7633   rmp->context = context;
7634   rmp->table_id = ntohl (table_id);
7635   rmp->hit_next_index = ntohl (e->next_index);
7636   rmp->advance = ntohl (e->advance);
7637   rmp->opaque_index = ntohl (e->opaque_index);
7638   rmp->match_length = ntohl (match_length);
7639   clib_memcpy (rmp->match, e->key, match_length);
7640
7641   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7642 }
7643
7644 static void
7645 vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
7646 {
7647   vnet_classify_main_t *cm = &vnet_classify_main;
7648   unix_shared_memory_queue_t *q;
7649
7650   u32 table_id = ntohl (mp->table_id);
7651   vnet_classify_table_t *t;
7652
7653   q = vl_api_client_index_to_input_queue (mp->client_index);
7654   if (!q)
7655     return;
7656
7657   /* *INDENT-OFF* */
7658   pool_foreach (t, cm->tables,
7659   ({
7660     if (table_id == t - cm->tables)
7661       {
7662         vnet_classify_bucket_t * b;
7663         vnet_classify_entry_t * v, * save_v;
7664         int i, j, k;
7665
7666         for (i = 0; i < t->nbuckets; i++)
7667           {
7668             b = &t->buckets [i];
7669             if (b->offset == 0)
7670               continue;
7671
7672             save_v = vnet_classify_get_entry (t, b->offset);
7673             for (j = 0; j < (1<<b->log2_pages); j++)
7674               {
7675                 for (k = 0; k < t->entries_per_page; k++)
7676                   {
7677                     v = vnet_classify_entry_at_index
7678                       (t, save_v, j*t->entries_per_page + k);
7679                     if (vnet_classify_entry_is_free (v))
7680                       continue;
7681
7682                     send_classify_session_details
7683                       (q, table_id, t->match_n_vectors * sizeof (u32x4),
7684                        v, mp->context);
7685                   }
7686               }
7687           }
7688         break;
7689       }
7690   }));
7691   /* *INDENT-ON* */
7692 }
7693
7694 static void
7695 vl_api_set_ipfix_exporter_t_handler (vl_api_set_ipfix_exporter_t * mp)
7696 {
7697   vlib_main_t *vm = vlib_get_main ();
7698   flow_report_main_t *frm = &flow_report_main;
7699   vl_api_set_ipfix_exporter_reply_t *rmp;
7700   ip4_address_t collector, src;
7701   u16 collector_port = UDP_DST_PORT_ipfix;
7702   u32 path_mtu;
7703   u32 template_interval;
7704   u8 udp_checksum;
7705   u32 fib_id;
7706   u32 fib_index = ~0;
7707   int rv = 0;
7708
7709   memcpy (collector.data, mp->collector_address, sizeof (collector.data));
7710   collector_port = ntohs (mp->collector_port);
7711   if (collector_port == (u16) ~ 0)
7712     collector_port = UDP_DST_PORT_ipfix;
7713   memcpy (src.data, mp->src_address, sizeof (src.data));
7714   fib_id = ntohl (mp->vrf_id);
7715
7716   ip4_main_t *im = &ip4_main;
7717   if (fib_id == ~0)
7718     {
7719       fib_index = ~0;
7720     }
7721   else
7722     {
7723       uword *p = hash_get (im->fib_index_by_table_id, fib_id);
7724       if (!p)
7725         {
7726           rv = VNET_API_ERROR_NO_SUCH_FIB;
7727           goto out;
7728         }
7729       fib_index = p[0];
7730     }
7731
7732   path_mtu = ntohl (mp->path_mtu);
7733   if (path_mtu == ~0)
7734     path_mtu = 512;             // RFC 7011 section 10.3.3.
7735   template_interval = ntohl (mp->template_interval);
7736   if (template_interval == ~0)
7737     template_interval = 20;
7738   udp_checksum = mp->udp_checksum;
7739
7740   if (collector.as_u32 == 0)
7741     {
7742       rv = VNET_API_ERROR_INVALID_VALUE;
7743       goto out;
7744     }
7745
7746   if (src.as_u32 == 0)
7747     {
7748       rv = VNET_API_ERROR_INVALID_VALUE;
7749       goto out;
7750     }
7751
7752   if (path_mtu > 1450 /* vpp does not support fragmentation */ )
7753     {
7754       rv = VNET_API_ERROR_INVALID_VALUE;
7755       goto out;
7756     }
7757
7758   if (path_mtu < 68)
7759     {
7760       rv = VNET_API_ERROR_INVALID_VALUE;
7761       goto out;
7762     }
7763
7764   /* Reset report streams if we are reconfiguring IP addresses */
7765   if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
7766       frm->src_address.as_u32 != src.as_u32 ||
7767       frm->collector_port != collector_port)
7768     vnet_flow_reports_reset (frm);
7769
7770   frm->ipfix_collector.as_u32 = collector.as_u32;
7771   frm->collector_port = collector_port;
7772   frm->src_address.as_u32 = src.as_u32;
7773   frm->fib_index = fib_index;
7774   frm->path_mtu = path_mtu;
7775   frm->template_interval = template_interval;
7776   frm->udp_checksum = udp_checksum;
7777
7778   /* Turn on the flow reporting process */
7779   vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
7780
7781 out:
7782   REPLY_MACRO (VL_API_SET_IPFIX_EXPORTER_REPLY);
7783 }
7784
7785 static void
7786 vl_api_ipfix_exporter_dump_t_handler (vl_api_ipfix_exporter_dump_t * mp)
7787 {
7788   flow_report_main_t *frm = &flow_report_main;
7789   unix_shared_memory_queue_t *q;
7790   vl_api_ipfix_exporter_details_t *rmp;
7791   ip4_main_t *im = &ip4_main;
7792   u32 vrf_id;
7793
7794   q = vl_api_client_index_to_input_queue (mp->client_index);
7795   if (!q)
7796     return;
7797
7798   rmp = vl_msg_api_alloc (sizeof (*rmp));
7799   memset (rmp, 0, sizeof (*rmp));
7800   rmp->_vl_msg_id = ntohs (VL_API_IPFIX_EXPORTER_DETAILS);
7801   rmp->context = mp->context;
7802   memcpy (rmp->collector_address, frm->ipfix_collector.data,
7803           sizeof (frm->ipfix_collector.data));
7804   rmp->collector_port = htons (frm->collector_port);
7805   memcpy (rmp->src_address, frm->src_address.data,
7806           sizeof (frm->src_address.data));
7807   if (frm->fib_index == ~0)
7808     vrf_id = ~0;
7809   else
7810     vrf_id = im->fibs[frm->fib_index].ft_table_id;
7811   rmp->vrf_id = htonl (vrf_id);
7812   rmp->path_mtu = htonl (frm->path_mtu);
7813   rmp->template_interval = htonl (frm->template_interval);
7814   rmp->udp_checksum = (frm->udp_checksum != 0);
7815
7816   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7817 }
7818
7819 static void
7820   vl_api_set_ipfix_classify_stream_t_handler
7821   (vl_api_set_ipfix_classify_stream_t * mp)
7822 {
7823   vl_api_set_ipfix_classify_stream_reply_t *rmp;
7824   flow_report_classify_main_t *fcm = &flow_report_classify_main;
7825   flow_report_main_t *frm = &flow_report_main;
7826   u32 domain_id = 0;
7827   u32 src_port = UDP_DST_PORT_ipfix;
7828   int rv = 0;
7829
7830   domain_id = ntohl (mp->domain_id);
7831   src_port = ntohs (mp->src_port);
7832
7833   if (fcm->src_port != 0 &&
7834       (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
7835     {
7836       int rv = vnet_stream_change (frm, fcm->domain_id, fcm->src_port,
7837                                    domain_id, (u16) src_port);
7838       ASSERT (rv == 0);
7839     }
7840
7841   fcm->domain_id = domain_id;
7842   fcm->src_port = (u16) src_port;
7843
7844   REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
7845 }
7846
7847 static void
7848   vl_api_ipfix_classify_stream_dump_t_handler
7849   (vl_api_ipfix_classify_stream_dump_t * mp)
7850 {
7851   flow_report_classify_main_t *fcm = &flow_report_classify_main;
7852   unix_shared_memory_queue_t *q;
7853   vl_api_ipfix_classify_stream_details_t *rmp;
7854
7855   q = vl_api_client_index_to_input_queue (mp->client_index);
7856   if (!q)
7857     return;
7858
7859   rmp = vl_msg_api_alloc (sizeof (*rmp));
7860   memset (rmp, 0, sizeof (*rmp));
7861   rmp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_STREAM_DETAILS);
7862   rmp->context = mp->context;
7863   rmp->domain_id = htonl (fcm->domain_id);
7864   rmp->src_port = htons (fcm->src_port);
7865
7866   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7867 }
7868
7869 static void
7870   vl_api_ipfix_classify_table_add_del_t_handler
7871   (vl_api_ipfix_classify_table_add_del_t * mp)
7872 {
7873   vl_api_ipfix_classify_table_add_del_reply_t *rmp;
7874   flow_report_classify_main_t *fcm = &flow_report_classify_main;
7875   flow_report_main_t *frm = &flow_report_main;
7876   vnet_flow_report_add_del_args_t args;
7877   ipfix_classify_table_t *table;
7878   int is_add;
7879   u32 classify_table_index;
7880   u8 ip_version;
7881   u8 transport_protocol;
7882   int rv = 0;
7883
7884   classify_table_index = ntohl (mp->table_id);
7885   ip_version = mp->ip_version;
7886   transport_protocol = mp->transport_protocol;
7887   is_add = mp->is_add;
7888
7889   if (fcm->src_port == 0)
7890     {
7891       /* call set_ipfix_classify_stream first */
7892       rv = VNET_API_ERROR_UNSPECIFIED;
7893       goto out;
7894     }
7895
7896   memset (&args, 0, sizeof (args));
7897
7898   table = 0;
7899   int i;
7900   for (i = 0; i < vec_len (fcm->tables); i++)
7901     if (ipfix_classify_table_index_valid (i))
7902       if (fcm->tables[i].classify_table_index == classify_table_index)
7903         {
7904           table = &fcm->tables[i];
7905           break;
7906         }
7907
7908   if (is_add)
7909     {
7910       if (table)
7911         {
7912           rv = VNET_API_ERROR_VALUE_EXIST;
7913           goto out;
7914         }
7915       table = ipfix_classify_add_table ();
7916       table->classify_table_index = classify_table_index;
7917     }
7918   else
7919     {
7920       if (!table)
7921         {
7922           rv = VNET_API_ERROR_NO_SUCH_ENTRY;
7923           goto out;
7924         }
7925     }
7926
7927   table->ip_version = ip_version;
7928   table->transport_protocol = transport_protocol;
7929
7930   args.opaque.as_uword = table - fcm->tables;
7931   args.rewrite_callback = ipfix_classify_template_rewrite;
7932   args.flow_data_callback = ipfix_classify_send_flows;
7933   args.is_add = is_add;
7934   args.domain_id = fcm->domain_id;
7935   args.src_port = fcm->src_port;
7936
7937   rv = vnet_flow_report_add_del (frm, &args);
7938
7939   /* If deleting, or add failed */
7940   if (is_add == 0 || (rv && is_add))
7941     ipfix_classify_delete_table (table - fcm->tables);
7942
7943 out:
7944   REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
7945 }
7946
7947 static void
7948 send_ipfix_classify_table_details (u32 table_index,
7949                                    unix_shared_memory_queue_t * q,
7950                                    u32 context)
7951 {
7952   flow_report_classify_main_t *fcm = &flow_report_classify_main;
7953   vl_api_ipfix_classify_table_details_t *mp;
7954
7955   ipfix_classify_table_t *table = &fcm->tables[table_index];
7956
7957   mp = vl_msg_api_alloc (sizeof (*mp));
7958   memset (mp, 0, sizeof (*mp));
7959   mp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_TABLE_DETAILS);
7960   mp->context = context;
7961   mp->table_id = htonl (table->classify_table_index);
7962   mp->ip_version = table->ip_version;
7963   mp->transport_protocol = table->transport_protocol;
7964
7965   vl_msg_api_send_shmem (q, (u8 *) & mp);
7966 }
7967
7968 static void
7969   vl_api_ipfix_classify_table_dump_t_handler
7970   (vl_api_ipfix_classify_table_dump_t * mp)
7971 {
7972   flow_report_classify_main_t *fcm = &flow_report_classify_main;
7973   unix_shared_memory_queue_t *q;
7974   u32 i;
7975
7976   q = vl_api_client_index_to_input_queue (mp->client_index);
7977   if (!q)
7978     return;
7979
7980   for (i = 0; i < vec_len (fcm->tables); i++)
7981     if (ipfix_classify_table_index_valid (i))
7982       send_ipfix_classify_table_details (i, q, mp->context);
7983 }
7984
7985 static void
7986 vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
7987 {
7988   vl_api_pg_create_interface_reply_t *rmp;
7989   int rv = 0;
7990
7991   pg_main_t *pg = &pg_main;
7992   u32 sw_if_index = pg_interface_add_or_get (pg, ntohl (mp->interface_id));
7993
7994   /* *INDENT-OFF* */
7995   REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
7996   ({
7997     rmp->sw_if_index = ntohl(sw_if_index);
7998   }));
7999   /* *INDENT-ON* */
8000 }
8001
8002 static void
8003 vl_api_pg_capture_t_handler (vl_api_pg_capture_t * mp)
8004 {
8005   vl_api_pg_capture_reply_t *rmp;
8006   int rv = 0;
8007
8008   vnet_main_t *vnm = vnet_get_main ();
8009   vnet_interface_main_t *im = &vnm->interface_main;
8010   vnet_hw_interface_t *hi = 0;
8011
8012   u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
8013   u32 hw_if_index = ~0;
8014   uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
8015   if (p)
8016     hw_if_index = *p;
8017   vec_free (intf_name);
8018
8019   if (hw_if_index != ~0)
8020     {
8021       pg_capture_args_t _a, *a = &_a;
8022
8023       u32 len = ntohl (mp->pcap_name_length);
8024       u8 *pcap_file_name = vec_new (u8, len);
8025       clib_memcpy (pcap_file_name, mp->pcap_file_name, len);
8026
8027       hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
8028       a->hw_if_index = hw_if_index;
8029       a->dev_instance = hi->dev_instance;
8030       a->is_enabled = mp->is_enabled;
8031       a->pcap_file_name = pcap_file_name;
8032       a->count = ntohl (mp->count);
8033
8034       clib_error_t *e = pg_capture (a);
8035       if (e)
8036         {
8037           clib_error_report (e);
8038           rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
8039         }
8040
8041       vec_free (pcap_file_name);
8042     }
8043   REPLY_MACRO (VL_API_PG_CAPTURE_REPLY);
8044 }
8045
8046 static void
8047 vl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t * mp)
8048 {
8049   vl_api_pg_enable_disable_reply_t *rmp;
8050   int rv = 0;
8051
8052   pg_main_t *pg = &pg_main;
8053   u32 stream_index = ~0;
8054
8055   int is_enable = mp->is_enabled != 0;
8056   u32 len = ntohl (mp->stream_name_length) - 1;
8057
8058   if (len > 0)
8059     {
8060       u8 *stream_name = vec_new (u8, len);
8061       clib_memcpy (stream_name, mp->stream_name, len);
8062       uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
8063       if (p)
8064         stream_index = *p;
8065       vec_free (stream_name);
8066     }
8067
8068   pg_enable_disable (stream_index, is_enable);
8069
8070   REPLY_MACRO (VL_API_PG_ENABLE_DISABLE_REPLY);
8071 }
8072
8073 static void
8074   vl_api_ip_source_and_port_range_check_add_del_t_handler
8075   (vl_api_ip_source_and_port_range_check_add_del_t * mp)
8076 {
8077   vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
8078   int rv = 0;
8079
8080   u8 is_ipv6 = mp->is_ipv6;
8081   u8 is_add = mp->is_add;
8082   u8 mask_length = mp->mask_length;
8083   ip4_address_t ip4_addr;
8084   ip6_address_t ip6_addr;
8085   u16 *low_ports = 0;
8086   u16 *high_ports = 0;
8087   u32 vrf_id;
8088   u16 tmp_low, tmp_high;
8089   u8 num_ranges;
8090   int i;
8091
8092   // Validate port range
8093   num_ranges = mp->number_of_ranges;
8094   if (num_ranges > 32)
8095     {                           // This is size of array in VPE.API
8096       rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
8097       goto reply;
8098     }
8099
8100   vec_reset_length (low_ports);
8101   vec_reset_length (high_ports);
8102
8103   for (i = 0; i < num_ranges; i++)
8104     {
8105       tmp_low = mp->low_ports[i];
8106       tmp_high = mp->high_ports[i];
8107       // If tmp_low <= tmp_high then only need to check tmp_low = 0
8108       // If tmp_low <= tmp_high then only need to check tmp_high > 65535
8109       if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
8110         {
8111           rv = VNET_API_ERROR_INVALID_VALUE;
8112           goto reply;
8113         }
8114       vec_add1 (low_ports, tmp_low);
8115       vec_add1 (high_ports, tmp_high + 1);
8116     }
8117
8118   // Validate mask_length
8119   if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
8120     {
8121       rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
8122       goto reply;
8123     }
8124
8125   vrf_id = ntohl (mp->vrf_id);
8126
8127   if (vrf_id < 1)
8128     {
8129       rv = VNET_API_ERROR_INVALID_VALUE;
8130       goto reply;
8131     }
8132
8133
8134   if (is_ipv6)
8135     {
8136       clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
8137       rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
8138                                                     mask_length,
8139                                                     vrf_id,
8140                                                     low_ports,
8141                                                     high_ports, is_add);
8142     }
8143   else
8144     {
8145       clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
8146       rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
8147                                                     mask_length,
8148                                                     vrf_id,
8149                                                     low_ports,
8150                                                     high_ports, is_add);
8151     }
8152
8153 reply:
8154   vec_free (low_ports);
8155   vec_free (high_ports);
8156   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
8157 }
8158
8159 static void
8160   vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
8161   (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
8162 {
8163   vlib_main_t *vm = vlib_get_main ();
8164   vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
8165   ip4_main_t *im = &ip4_main;
8166   int rv;
8167   u32 sw_if_index;
8168   u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
8169   u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
8170   uword *p = 0;
8171   int i;
8172
8173   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
8174     ntohl (mp->tcp_out_vrf_id);
8175   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
8176     ntohl (mp->udp_out_vrf_id);
8177   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
8178     ntohl (mp->tcp_in_vrf_id);
8179   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
8180     ntohl (mp->udp_in_vrf_id);
8181
8182
8183   for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
8184     {
8185       if (vrf_id[i] != 0 && vrf_id[i] != ~0)
8186         {
8187           p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
8188
8189           if (p == 0)
8190             {
8191               rv = VNET_API_ERROR_INVALID_VALUE;
8192               goto reply;
8193             }
8194
8195           fib_index[i] = p[0];
8196         }
8197       else
8198         fib_index[i] = ~0;
8199     }
8200   sw_if_index = ntohl (mp->sw_if_index);
8201
8202   VALIDATE_SW_IF_INDEX (mp);
8203
8204   rv =
8205     set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
8206                                         mp->is_add);
8207
8208   BAD_SW_IF_INDEX_LABEL;
8209 reply:
8210
8211   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
8212 }
8213
8214 static void
8215 vl_api_ipsec_gre_add_del_tunnel_t_handler (vl_api_ipsec_gre_add_del_tunnel_t *
8216                                            mp)
8217 {
8218   vl_api_ipsec_gre_add_del_tunnel_reply_t *rmp;
8219   int rv = 0;
8220   vnet_ipsec_gre_add_del_tunnel_args_t _a, *a = &_a;
8221   u32 sw_if_index = ~0;
8222
8223   /* Check src & dst are different */
8224   if (memcmp (mp->src_address, mp->dst_address, 4) == 0)
8225     {
8226       rv = VNET_API_ERROR_SAME_SRC_DST;
8227       goto out;
8228     }
8229
8230   memset (a, 0, sizeof (*a));
8231
8232   /* ip addresses sent in network byte order */
8233   clib_memcpy (&(a->src), mp->src_address, 4);
8234   clib_memcpy (&(a->dst), mp->dst_address, 4);
8235   a->is_add = mp->is_add;
8236   a->lsa = ntohl (mp->local_sa_id);
8237   a->rsa = ntohl (mp->remote_sa_id);
8238
8239   rv = vnet_ipsec_gre_add_del_tunnel (a, &sw_if_index);
8240
8241 out:
8242     /* *INDENT-OFF* */
8243     REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY,
8244     ({
8245         rmp->sw_if_index = ntohl (sw_if_index);
8246     }));
8247     /* *INDENT-ON* */
8248 }
8249
8250 static void send_ipsec_gre_tunnel_details
8251   (ipsec_gre_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
8252 {
8253   vl_api_ipsec_gre_tunnel_details_t *rmp;
8254
8255   rmp = vl_msg_api_alloc (sizeof (*rmp));
8256   memset (rmp, 0, sizeof (*rmp));
8257   rmp->_vl_msg_id = ntohs (VL_API_IPSEC_GRE_TUNNEL_DETAILS);
8258   clib_memcpy (rmp->src_address, &(t->tunnel_src), 4);
8259   clib_memcpy (rmp->dst_address, &(t->tunnel_dst), 4);
8260   rmp->sw_if_index = htonl (t->sw_if_index);
8261   rmp->local_sa_id = htonl (t->local_sa_id);
8262   rmp->remote_sa_id = htonl (t->remote_sa_id);
8263   rmp->context = context;
8264
8265   vl_msg_api_send_shmem (q, (u8 *) & rmp);
8266 }
8267
8268 static void vl_api_ipsec_gre_tunnel_dump_t_handler
8269   (vl_api_ipsec_gre_tunnel_dump_t * mp)
8270 {
8271   unix_shared_memory_queue_t *q;
8272   ipsec_gre_main_t *igm = &ipsec_gre_main;
8273   ipsec_gre_tunnel_t *t;
8274   u32 sw_if_index;
8275
8276   q = vl_api_client_index_to_input_queue (mp->client_index);
8277   if (q == 0)
8278     {
8279       return;
8280     }
8281
8282   sw_if_index = ntohl (mp->sw_if_index);
8283
8284   if (~0 == sw_if_index)
8285     {
8286         /* *INDENT-OFF* */
8287         pool_foreach (t, igm->tunnels,
8288         ({
8289             send_ipsec_gre_tunnel_details(t, q, mp->context);
8290         }));
8291         /* *INDENT-ON* */
8292     }
8293   else
8294     {
8295       if ((sw_if_index >= vec_len (igm->tunnel_index_by_sw_if_index)) ||
8296           (~0 == igm->tunnel_index_by_sw_if_index[sw_if_index]))
8297         {
8298           return;
8299         }
8300       t = &igm->tunnels[igm->tunnel_index_by_sw_if_index[sw_if_index]];
8301       send_ipsec_gre_tunnel_details (t, q, mp->context);
8302     }
8303 }
8304
8305 static void
8306 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
8307 {
8308   vl_api_delete_subif_reply_t *rmp;
8309   int rv;
8310
8311   rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
8312
8313   REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
8314 }
8315
8316 #define BOUNCE_HANDLER(nn)                                              \
8317 static void vl_api_##nn##_t_handler (                                   \
8318     vl_api_##nn##_t *mp)                                                \
8319 {                                                                       \
8320     vpe_client_registration_t *reg;                                     \
8321     vpe_api_main_t * vam = &vpe_api_main;                               \
8322     unix_shared_memory_queue_t * q;                                     \
8323                                                                         \
8324     /* One registration only... */                                      \
8325     pool_foreach(reg, vam->nn##_registrations,                          \
8326     ({                                                                  \
8327         q = vl_api_client_index_to_input_queue (reg->client_index);     \
8328         if (q) {                                                        \
8329             /*                                                          \
8330              * If the queue is stuffed, turf the msg and complain       \
8331              * It's unlikely that the intended recipient is             \
8332              * alive; avoid deadlock at all costs.                      \
8333              */                                                         \
8334             if (q->cursize == q->maxsize) {                             \
8335                 clib_warning ("ERROR: receiver queue full, drop msg");  \
8336                 vl_msg_api_free (mp);                                   \
8337                 return;                                                 \
8338             }                                                           \
8339             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
8340             return;                                                     \
8341         }                                                               \
8342     }));                                                                \
8343     vl_msg_api_free (mp);                                               \
8344 }
8345
8346 /*
8347  * vpe_api_hookup
8348  * Add vpe's API message handlers to the table.
8349  * vlib has alread mapped shared memory and
8350  * added the client registration handlers.
8351  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
8352  */
8353
8354 static clib_error_t *
8355 vpe_api_hookup (vlib_main_t * vm)
8356 {
8357   api_main_t *am = &api_main;
8358
8359 #define _(N,n)                                                  \
8360     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
8361                            vl_api_##n##_t_handler,              \
8362                            vl_noop_handler,                     \
8363                            vl_api_##n##_t_endian,               \
8364                            vl_api_##n##_t_print,                \
8365                            sizeof(vl_api_##n##_t), 1);
8366   foreach_vpe_api_msg;
8367 #undef _
8368
8369   /*
8370    * Manually register the sr tunnel add del msg, so we trace
8371    * enough bytes to capture a typical segment list
8372    */
8373   vl_msg_api_set_handlers (VL_API_SR_TUNNEL_ADD_DEL,
8374                            "sr_tunnel_add_del",
8375                            vl_api_sr_tunnel_add_del_t_handler,
8376                            vl_noop_handler,
8377                            vl_api_sr_tunnel_add_del_t_endian,
8378                            vl_api_sr_tunnel_add_del_t_print, 256, 1);
8379
8380
8381   /*
8382    * Manually register the sr policy add del msg, so we trace
8383    * enough bytes to capture a typical tunnel name list
8384    */
8385   vl_msg_api_set_handlers (VL_API_SR_POLICY_ADD_DEL,
8386                            "sr_policy_add_del",
8387                            vl_api_sr_policy_add_del_t_handler,
8388                            vl_noop_handler,
8389                            vl_api_sr_policy_add_del_t_endian,
8390                            vl_api_sr_policy_add_del_t_print, 256, 1);
8391
8392   /*
8393    * Trace space for 8 MPLS encap labels, classifier mask+match
8394    */
8395   am->api_trace_cfg[VL_API_MPLS_ADD_DEL_ENCAP].size += 8 * sizeof (u32);
8396   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
8397   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size
8398     += 5 * sizeof (u32x4);
8399   am->api_trace_cfg[VL_API_VXLAN_ADD_DEL_TUNNEL].size += 16 * sizeof (u32);
8400
8401   /*
8402    * Thread-safe API messages
8403    */
8404   am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
8405   am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
8406
8407   return 0;
8408 }
8409
8410 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
8411
8412 static clib_error_t *
8413 vpe_api_init (vlib_main_t * vm)
8414 {
8415   vpe_api_main_t *am = &vpe_api_main;
8416
8417   am->vlib_main = vm;
8418   am->vnet_main = vnet_get_main ();
8419   am->interface_events_registration_hash = hash_create (0, sizeof (uword));
8420   am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
8421   am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
8422   am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
8423   am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
8424   am->oam_events_registration_hash = hash_create (0, sizeof (uword));
8425
8426   vl_api_init (vm);
8427   vl_set_memory_region_name ("/vpe-api");
8428   vl_enable_disable_memory_api (vm, 1 /* enable it */ );
8429
8430   return 0;
8431 }
8432
8433 VLIB_INIT_FUNCTION (vpe_api_init);
8434
8435
8436 static clib_error_t *
8437 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
8438 {
8439   u8 *chroot_path;
8440   u64 baseva, size, pvt_heap_size;
8441   int uid, gid, rv;
8442   const int max_buf_size = 4096;
8443   char *s, *buf;
8444   struct passwd _pw, *pw;
8445   struct group _grp, *grp;
8446   clib_error_t *e;
8447   buf = vec_new (char, 128);
8448   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
8449     {
8450       if (unformat (input, "prefix %s", &chroot_path))
8451         {
8452           vec_add1 (chroot_path, 0);
8453           vl_set_memory_root_path ((char *) chroot_path);
8454         }
8455       else if (unformat (input, "uid %d", &uid))
8456         vl_set_memory_uid (uid);
8457       else if (unformat (input, "gid %d", &gid))
8458         vl_set_memory_gid (gid);
8459       else if (unformat (input, "baseva %llx", &baseva))
8460         vl_set_global_memory_baseva (baseva);
8461       else if (unformat (input, "global-size %lldM", &size))
8462         vl_set_global_memory_size (size * (1ULL << 20));
8463       else if (unformat (input, "global-size %lldG", &size))
8464         vl_set_global_memory_size (size * (1ULL << 30));
8465       else if (unformat (input, "global-size %lld", &size))
8466         vl_set_global_memory_size (size);
8467       else if (unformat (input, "global-pvt-heap-size %lldM", &pvt_heap_size))
8468         vl_set_global_pvt_heap_size (pvt_heap_size * (1ULL << 20));
8469       else if (unformat (input, "global-pvt-heap-size size %lld",
8470                          &pvt_heap_size))
8471         vl_set_global_pvt_heap_size (pvt_heap_size);
8472       else if (unformat (input, "api-pvt-heap-size %lldM", &pvt_heap_size))
8473         vl_set_api_pvt_heap_size (pvt_heap_size * (1ULL << 20));
8474       else if (unformat (input, "api-pvt-heap-size size %lld",
8475                          &pvt_heap_size))
8476         vl_set_api_pvt_heap_size (pvt_heap_size);
8477       else if (unformat (input, "api-size %lldM", &size))
8478         vl_set_api_memory_size (size * (1ULL << 20));
8479       else if (unformat (input, "api-size %lldG", &size))
8480         vl_set_api_memory_size (size * (1ULL << 30));
8481       else if (unformat (input, "api-size %lld", &size))
8482         vl_set_api_memory_size (size);
8483       else if (unformat (input, "uid %s", &s))
8484         {
8485           /* lookup the username */
8486           pw = NULL;
8487           while (((rv =
8488                    getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
8489                  && (vec_len (buf) <= max_buf_size))
8490             {
8491               vec_resize (buf, vec_len (buf) * 2);
8492             }
8493           if (rv < 0)
8494             {
8495               e = clib_error_return_code (0, rv,
8496                                           CLIB_ERROR_ERRNO_VALID |
8497                                           CLIB_ERROR_FATAL,
8498                                           "cannot fetch username %s", s);
8499               vec_free (s);
8500               vec_free (buf);
8501               return e;
8502             }
8503           if (pw == NULL)
8504             {
8505               e =
8506                 clib_error_return_fatal (0, "username %s does not exist", s);
8507               vec_free (s);
8508               vec_free (buf);
8509               return e;
8510             }
8511           vec_free (s);
8512           vl_set_memory_uid (pw->pw_uid);
8513         }
8514       else if (unformat (input, "gid %s", &s))
8515         {
8516           /* lookup the group name */
8517           grp = NULL;
8518           while (((rv =
8519                    getgrnam_r (s, &_grp, buf, vec_len (buf), &grp)) == ERANGE)
8520                  && (vec_len (buf) <= max_buf_size))
8521             {
8522               vec_resize (buf, vec_len (buf) * 2);
8523             }
8524           if (rv != 0)
8525             {
8526               e = clib_error_return_code (0, rv,
8527                                           CLIB_ERROR_ERRNO_VALID |
8528                                           CLIB_ERROR_FATAL,
8529                                           "cannot fetch group %s", s);
8530               vec_free (s);
8531               vec_free (buf);
8532               return e;
8533             }
8534           if (grp == NULL)
8535             {
8536               e = clib_error_return_fatal (0, "group %s does not exist", s);
8537               vec_free (s);
8538               vec_free (buf);
8539               return e;
8540             }
8541           vec_free (s);
8542           vec_free (buf);
8543           vl_set_memory_gid (grp->gr_gid);
8544         }
8545       else
8546         return clib_error_return (0, "unknown input `%U'",
8547                                   format_unformat_error, input);
8548     }
8549   return 0;
8550 }
8551
8552 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
8553
8554 void *
8555 get_unformat_vnet_sw_interface (void)
8556 {
8557   return (void *) &unformat_vnet_sw_interface;
8558 }
8559
8560 #undef vl_api_version
8561 #define vl_api_version(n,v) static u32 vpe_api_version = v;
8562 #include <vpp-api/vpe.api.h>
8563 #undef vl_api_version
8564
8565 int
8566 vl_msg_api_version_check (vl_api_memclnt_create_t * mp)
8567 {
8568   if (clib_host_to_net_u32 (mp->api_versions[0]) != vpe_api_version)
8569     {
8570       clib_warning ("vpe API mismatch: 0x%08x instead of 0x%08x",
8571                     clib_host_to_net_u32 (mp->api_versions[0]),
8572                     vpe_api_version);
8573       return -1;
8574     }
8575   return 0;
8576 }
8577
8578 static u8 *
8579 format_arp_event (u8 * s, va_list * args)
8580 {
8581   vl_api_ip4_arp_event_t *event = va_arg (*args, vl_api_ip4_arp_event_t *);
8582
8583   s = format (s, "pid %d: ", event->pid);
8584   if (event->mac_ip)
8585     s = format (s, "bd mac/ip4 binding events");
8586   else
8587     s = format (s, "resolution for %U", format_ip4_address, &event->address);
8588   return s;
8589 }
8590
8591 static u8 *
8592 format_nd_event (u8 * s, va_list * args)
8593 {
8594   vl_api_ip6_nd_event_t *event = va_arg (*args, vl_api_ip6_nd_event_t *);
8595
8596   s = format (s, "pid %d: ", event->pid);
8597   if (event->mac_ip)
8598     s = format (s, "bd mac/ip6 binding events");
8599   else
8600     s = format (s, "resolution for %U", format_ip6_address, event->address);
8601   return s;
8602 }
8603
8604 static clib_error_t *
8605 show_ip_arp_nd_events_fn (vlib_main_t * vm,
8606                           unformat_input_t * input, vlib_cli_command_t * cmd)
8607 {
8608   vpe_api_main_t *am = &vpe_api_main;
8609   vl_api_ip4_arp_event_t *arp_event;
8610   vl_api_ip6_nd_event_t *nd_event;
8611
8612   if ((pool_elts (am->arp_events) == 0) && (pool_elts (am->nd_events) == 0))
8613     {
8614       vlib_cli_output (vm, "No active arp or nd event registrations");
8615       return 0;
8616     }
8617
8618   /* *INDENT-OFF* */
8619   pool_foreach (arp_event, am->arp_events,
8620   ({
8621     vlib_cli_output (vm, "%U", format_arp_event, arp_event);
8622   }));
8623
8624   pool_foreach (nd_event, am->nd_events,
8625   ({
8626     vlib_cli_output (vm, "%U", format_nd_event, nd_event);
8627   }));
8628   /* *INDENT-ON* */
8629
8630   return 0;
8631 }
8632
8633 /* *INDENT-OFF* */
8634 VLIB_CLI_COMMAND (show_ip_arp_nd_events, static) = {
8635   .path = "show arp-nd-event registrations",
8636   .function = show_ip_arp_nd_events_fn,
8637   .short_help = "Show ip4 arp and ip6 nd event registrations",
8638 };
8639 /* *INDENT-ON* */
8640
8641 /*
8642  * fd.io coding-style-patch-verification: ON
8643  *
8644  * Local Variables:
8645  * eval: (c-set-style "gnu")
8646  * End:
8647  */