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