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