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