VPP-226 IPv4 src-address + port range checker
[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 && DPDK_VHOST_USER
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 = dpdk_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 void
4884 vl_api_lisp_add_del_local_eid_t_handler(
4885     vl_api_lisp_add_del_local_eid_t *mp)
4886 {
4887     vl_api_lisp_add_del_local_eid_reply_t *rmp;
4888     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
4889     int rv = 0;
4890     ip_prefix_t  *prefp = NULL;
4891     ip_address_t *ip_eid = NULL;
4892     gid_address_t eid;
4893     u8 * mac = gid_address_mac (&eid);
4894     uword * p = NULL;
4895     u32 locator_set_index = ~0, map_index = ~0;
4896     vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
4897     u8 *name = NULL;
4898     memset (a, 0, sizeof (a[0]));
4899     memset (&eid, 0, sizeof (eid));
4900
4901     prefp = &gid_address_ippref(&eid);
4902     ip_eid = &ip_prefix_addr(prefp);
4903
4904     switch (mp->eid_type)
4905       {
4906       case 0: /* ipv4*/
4907         gid_address_type (&eid) = GID_ADDR_IP_PREFIX;
4908         ip_address_set(ip_eid, mp->eid, IP4);
4909         ip_prefix_len(prefp) = mp->prefix_len;
4910         ip_prefix_normalize(prefp);
4911         break;
4912       case 1: /* ipv6 */
4913         gid_address_type (&eid) = GID_ADDR_IP_PREFIX;
4914         ip_address_set(ip_eid, mp->eid, IP6);
4915         ip_prefix_len(prefp) = mp->prefix_len;
4916         ip_prefix_normalize(prefp);
4917         break;
4918       case 2: /* l2 mac */
4919         gid_address_type (&eid) = GID_ADDR_MAC;
4920         clib_memcpy(mac, mp->eid, 6);
4921         break;
4922       default:
4923         rv = VNET_API_ERROR_INVALID_EID_TYPE;
4924         goto out;
4925       }
4926
4927     name = format(0, "%s", mp->locator_set_name);
4928     p = hash_get_mem(lcm->locator_set_index_by_name, name);
4929     if (!p) {
4930         rv = VNET_API_ERROR_INVALID_VALUE;
4931         goto out;
4932     }
4933     locator_set_index = p[0];
4934
4935   /* XXX treat batch configuration */
4936     a->is_add = mp->is_add;
4937     gid_address_vni (&eid) = clib_net_to_host_u32 (mp->vni);
4938     gid_address_copy (&a->eid, &eid);
4939     a->locator_set_index = locator_set_index;
4940     a->local = 1;
4941     rv = vnet_lisp_add_del_local_mapping(a, &map_index);
4942
4943 out:
4944     vec_free(name);
4945     gid_address_free (&a->eid);
4946
4947     REPLY_MACRO(VL_API_LISP_ADD_DEL_LOCAL_EID_REPLY);
4948 }
4949
4950 static void
4951 vl_api_lisp_eid_table_add_del_map_t_handler(
4952     vl_api_lisp_eid_table_add_del_map_t *mp)
4953 {
4954     vl_api_lisp_eid_table_add_del_map_reply_t *rmp;
4955     int rv = 0;
4956     rv = vnet_lisp_eid_table_map (clib_net_to_host_u32 (mp->vni),
4957                                   clib_net_to_host_u32 (mp->dp_table),
4958                                   mp->is_l2, mp->is_add);
4959     REPLY_MACRO(VL_API_LISP_EID_TABLE_ADD_DEL_MAP_REPLY)
4960 }
4961
4962 static void
4963 lisp_gpe_add_del_fwd_entry_set_address(
4964     vl_api_lisp_gpe_add_del_fwd_entry_t *mp,
4965     ip_address_t                        *slocator,
4966     ip_address_t                        *dlocator,
4967     gid_address_t                       *eid)
4968 {
4969     ip_address_t *ip_eid = NULL;
4970     ip_prefix_t *prefp = NULL;
4971
4972     prefp = &gid_address_ippref(eid);
4973     ip_eid = &ip_prefix_addr(prefp);
4974
4975     if (mp->eid_is_ipv6) {
4976         clib_memcpy(&ip_addr_v6(ip_eid), mp->eid_ip_address,
4977                sizeof(ip_addr_v6(ip_eid)));
4978         ip_addr_version(ip_eid) = IP6;
4979     } else {
4980         clib_memcpy(&ip_addr_v4(ip_eid), mp->eid_ip_address,
4981                sizeof(ip_addr_v4(ip_eid)));
4982         ip_addr_version(ip_eid) = IP4;
4983     }
4984     ip_prefix_len(prefp) = mp->eid_prefix_len;
4985     ip_prefix_normalize(prefp);
4986
4987     if (mp->address_is_ipv6) {
4988         clib_memcpy(&ip_addr_v6(slocator), mp->source_ip_address,
4989                sizeof(ip_addr_v6(slocator)));
4990         ip_addr_version(slocator) = IP6;
4991         clib_memcpy(&ip_addr_v6(dlocator), mp->destination_ip_address,
4992                sizeof(ip_addr_v6(dlocator)));
4993         ip_addr_version(dlocator) = IP6;
4994     } else {
4995         clib_memcpy(&ip_addr_v4(slocator), mp->source_ip_address,
4996                sizeof(ip_addr_v4(slocator)));
4997         ip_addr_version(slocator) = IP4;
4998         clib_memcpy(&ip_addr_v4(dlocator), mp->destination_ip_address,
4999                sizeof(ip_addr_v4(dlocator)));
5000         ip_addr_version(dlocator) = IP4;
5001     }
5002 }
5003
5004 static void
5005 vl_api_lisp_gpe_add_del_fwd_entry_t_handler(
5006     vl_api_lisp_gpe_add_del_fwd_entry_t *mp)
5007 {
5008     vl_api_lisp_gpe_add_del_fwd_entry_reply_t *rmp;
5009     int rv = 0;
5010     ip_address_t slocator, dlocator;
5011     gid_address_t eid;
5012     vnet_lisp_gpe_add_del_fwd_entry_args_t a;
5013
5014     lisp_gpe_add_del_fwd_entry_set_address(mp, &slocator, &dlocator, &eid);
5015
5016     memset (&a, 0, sizeof(a));
5017
5018     a.is_add = mp->is_add;
5019     a.rmt_eid = eid;
5020     a.lcl_loc = slocator;
5021     a.rmt_loc = dlocator;
5022     rv = vnet_lisp_gpe_add_del_fwd_entry (&a, 0);
5023
5024     REPLY_MACRO(VL_API_LISP_GPE_ADD_DEL_FWD_ENTRY_REPLY);
5025 }
5026
5027 static void
5028 vl_api_lisp_add_del_map_resolver_t_handler(
5029     vl_api_lisp_add_del_map_resolver_t *mp)
5030 {
5031     vl_api_lisp_add_del_map_resolver_reply_t *rmp;
5032     int rv = 0;
5033     ip_address_t *ip_addr = NULL;
5034     vnet_lisp_add_del_map_resolver_args_t _a, * a = &_a;
5035
5036     a->is_add = mp->is_add;
5037     ip_addr = &a->address;
5038
5039     if (mp->is_ipv6) {
5040         clib_memcpy(&ip_addr_v6(ip_addr), mp->ip_address,
5041                sizeof(ip_addr_v6(ip_addr)));
5042         ip_addr_version(ip_addr) = IP6;
5043     } else {
5044         clib_memcpy(&ip_addr_v4(ip_addr), mp->ip_address,
5045                sizeof(ip_addr_v4(ip_addr)));
5046         ip_addr_version(ip_addr) = IP4;
5047     }
5048
5049     rv = vnet_lisp_add_del_map_resolver (a);
5050
5051     REPLY_MACRO(VL_API_LISP_ADD_DEL_MAP_RESOLVER_REPLY);
5052 }
5053
5054 static void
5055 vl_api_lisp_gpe_enable_disable_t_handler(
5056     vl_api_lisp_gpe_enable_disable_t *mp)
5057 {
5058     vl_api_lisp_gpe_enable_disable_reply_t *rmp;
5059     int rv = 0;
5060     vnet_lisp_gpe_enable_disable_args_t _a, * a = &_a;
5061
5062     a->is_en = mp->is_en;
5063     vnet_lisp_gpe_enable_disable (a);
5064
5065     REPLY_MACRO(VL_API_LISP_GPE_ENABLE_DISABLE_REPLY);
5066 }
5067
5068 static void
5069 vl_api_lisp_enable_disable_t_handler(
5070     vl_api_lisp_enable_disable_t *mp)
5071 {
5072     vl_api_lisp_enable_disable_reply_t *rmp;
5073     int rv = 0;
5074
5075     vnet_lisp_enable_disable (mp->is_en);
5076     REPLY_MACRO(VL_API_LISP_ENABLE_DISABLE_REPLY);
5077 }
5078
5079 static void
5080 vl_api_lisp_gpe_add_del_iface_t_handler(
5081     vl_api_lisp_gpe_add_del_iface_t *mp)
5082 {
5083     vl_api_lisp_gpe_add_del_iface_reply_t *rmp;
5084     int rv = 0;
5085     vnet_lisp_gpe_add_del_iface_args_t _a, * a = &_a;
5086
5087     a->is_add = mp->is_add;
5088     a->table_id = mp->table_id;
5089     a->vni = mp->vni;
5090     rv = vnet_lisp_gpe_add_del_iface (a, 0);
5091
5092     REPLY_MACRO(VL_API_LISP_GPE_ADD_DEL_IFACE_REPLY);
5093 }
5094
5095 static void
5096 vl_api_lisp_pitr_set_locator_set_t_handler(
5097   vl_api_lisp_pitr_set_locator_set_t *mp)
5098 {
5099     vl_api_lisp_pitr_set_locator_set_reply_t *rmp;
5100     int rv = 0;
5101     u8 * ls_name = 0;
5102
5103     ls_name = format (0, "%s", mp->ls_name);
5104     rv = vnet_lisp_pitr_set_locator_set (ls_name, mp->is_add);
5105     vec_free (ls_name);
5106
5107     REPLY_MACRO(VL_API_LISP_PITR_SET_LOCATOR_SET_REPLY);
5108 }
5109
5110 static void
5111 vl_api_lisp_add_del_map_request_itr_rlocs_t_handler
5112 (vl_api_lisp_add_del_map_request_itr_rlocs_t *mp)
5113 {
5114     vl_api_lisp_add_del_map_request_itr_rlocs_reply_t *rmp;
5115     int rv = 0;
5116     u8 * locator_set_name = NULL;
5117     vnet_lisp_add_del_mreq_itr_rloc_args_t _a, * a = &_a;
5118
5119     locator_set_name = format (0, "%s", mp->locator_set_name);
5120
5121     a->is_add = mp->is_add;
5122     a->locator_set_name = locator_set_name;
5123
5124     rv = vnet_lisp_add_del_mreq_itr_rlocs(a);
5125
5126     vec_free(locator_set_name);
5127
5128     REPLY_MACRO(VL_API_LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS_REPLY);
5129 }
5130
5131 /** Used for transferring locators via VPP API */
5132 typedef CLIB_PACKED(struct
5133 {
5134   u8 is_ip4; /**< is locator an IPv4 address */
5135   u8 priority; /**< locator priority */
5136   u8 weight;   /**< locator weight */
5137   u8 addr[16]; /**< IPv4/IPv6 address */
5138 }) rloc_t;
5139
5140 static void
5141 vl_api_lisp_add_del_remote_mapping_t_handler (
5142     vl_api_lisp_add_del_remote_mapping_t *mp)
5143 {
5144     u32 i;
5145     locator_t rloc, * rlocs = 0;
5146     vl_api_lisp_add_del_remote_mapping_reply_t * rmp;
5147     int rv = 0;
5148     gid_address_t _eid, * eid = &_eid;
5149     ip_prefix_t * eid_pref = &gid_address_ippref(eid);
5150
5151     memset (eid, 0, sizeof (eid[0]));
5152     ip_address_t * eid_addr = &ip_prefix_addr(eid_pref);
5153     ip_prefix_len(eid_pref) = mp->eid_len;
5154     u8 * eid_mac = gid_address_mac (eid);
5155     gid_address_vni (eid) = ntohl (mp->vni);
5156
5157     switch (mp->eid_type)
5158       {
5159       case 0: /* ipv4 */
5160         gid_address_type(eid) = GID_ADDR_IP_PREFIX;
5161         ip_address_set (eid_addr, mp->eid, IP4);
5162         ip_prefix_normalize (eid_pref);
5163         break;
5164       case 1: /* ipv6 */
5165         gid_address_type(eid) = GID_ADDR_IP_PREFIX;
5166         ip_address_set (eid_addr, mp->eid, IP6);
5167         ip_prefix_normalize (eid_pref);
5168         break;
5169       case 2: /* l2 mac */
5170         gid_address_type(eid) = GID_ADDR_MAC;
5171         clib_memcpy (eid_mac, mp->eid, 6);
5172         break;
5173       default:
5174         rv = VNET_API_ERROR_INVALID_EID_TYPE;
5175         goto send_reply;
5176       }
5177
5178     for (i = 0; i < mp->rloc_num; i++) {
5179         rloc_t * r = &((rloc_t *) mp->rlocs)[i];
5180         memset(&rloc, 0, sizeof(rloc));
5181         ip_address_set(&gid_address_ip(&rloc.address), &r->addr,
5182                        r->is_ip4 ? IP4 : IP6);
5183         gid_address_ippref_len(&rloc.address) = r->is_ip4 ? 32: 128;
5184         gid_address_type(&rloc.address) = GID_ADDR_IP_PREFIX;
5185         rloc.priority = r->priority;
5186         rloc.weight = r->weight;
5187         vec_add1 (rlocs, rloc);
5188     }
5189
5190     if (!mp->is_add) {
5191         vnet_lisp_add_del_adjacency_args_t _a, * a = &_a;
5192         gid_address_copy(&a->deid, eid);
5193         a->is_add = 0;
5194         rv = vnet_lisp_add_del_adjacency (a);
5195         if (rv) {
5196           goto out;
5197         }
5198     }
5199
5200     /* NOTE: for now this works as a static remote mapping, i.e.,
5201      * not authoritative and ttl infinite. */
5202     rv = vnet_lisp_add_del_mapping (eid, rlocs, mp->action, 0, ~0,
5203                                     mp->is_add, 0);
5204
5205     if (mp->del_all)
5206       vnet_lisp_clear_all_remote_adjacencies ();
5207
5208 out:
5209     vec_free (rlocs);
5210 send_reply:
5211     REPLY_MACRO(VL_API_LISP_ADD_DEL_REMOTE_MAPPING_REPLY);
5212 }
5213
5214 static void
5215 vl_api_lisp_add_del_adjacency_t_handler (
5216     vl_api_lisp_add_del_adjacency_t *mp)
5217 {
5218     vl_api_lisp_add_del_adjacency_reply_t * rmp;
5219     vnet_lisp_add_del_adjacency_args_t _a, * a = &_a;
5220
5221     int rv = 0;
5222     memset(a, 0, sizeof(a[0]));
5223
5224     ip_prefix_t * seid_pref = &gid_address_ippref(&a->seid);
5225     ip_prefix_t * deid_pref = &gid_address_ippref(&a->deid);
5226
5227     ip_address_t * seid_addr = &ip_prefix_addr(seid_pref);
5228     ip_address_t * deid_addr = &ip_prefix_addr(deid_pref);
5229     ip_prefix_len(seid_pref) = mp->seid_len;
5230     ip_prefix_len(deid_pref) = mp->deid_len;
5231     u8 * seid_mac = gid_address_mac (&a->seid);
5232     u8 * deid_mac = gid_address_mac (&a->deid);
5233     gid_address_vni(&a->seid) = ntohl (mp->vni);
5234     gid_address_vni(&a->deid) = ntohl (mp->vni);
5235
5236     switch (mp->eid_type)
5237       {
5238       case 0: /* ipv4 */
5239         gid_address_type(&a->seid) = GID_ADDR_IP_PREFIX;
5240         gid_address_type(&a->deid) = GID_ADDR_IP_PREFIX;
5241         ip_address_set (seid_addr, mp->seid, IP4);
5242         ip_address_set (deid_addr, mp->deid, IP4);
5243         ip_prefix_normalize (seid_pref);
5244         ip_prefix_normalize (deid_pref);
5245         break;
5246       case 1: /* ipv6 */
5247         gid_address_type(&a->seid) = GID_ADDR_IP_PREFIX;
5248         gid_address_type(&a->deid) = GID_ADDR_IP_PREFIX;
5249         ip_address_set (seid_addr, mp->seid, IP6);
5250         ip_address_set (deid_addr, mp->deid, IP6);
5251         ip_prefix_normalize (seid_pref);
5252         ip_prefix_normalize (deid_pref);
5253         break;
5254       case 2: /* l2 mac */
5255         gid_address_type(&a->seid) = GID_ADDR_MAC;
5256         gid_address_type(&a->deid) = GID_ADDR_MAC;
5257         clib_memcpy (seid_mac, mp->seid, 6);
5258         clib_memcpy (deid_mac, mp->deid, 6);
5259         break;
5260       default:
5261         rv = VNET_API_ERROR_INVALID_EID_TYPE;
5262         goto send_reply;
5263       }
5264
5265     a->is_add = mp->is_add;
5266     rv = vnet_lisp_add_del_adjacency (a);
5267
5268 send_reply:
5269     REPLY_MACRO(VL_API_LISP_ADD_DEL_ADJACENCY_REPLY);
5270 }
5271
5272 static void
5273 send_lisp_locator_details (lisp_cp_main_t *lcm,
5274                            locator_t *loc,
5275                            unix_shared_memory_queue_t *q,
5276                            u32 context)
5277 {
5278     vl_api_lisp_locator_details_t *rmp;
5279
5280     rmp = vl_msg_api_alloc (sizeof (*rmp));
5281     memset (rmp, 0, sizeof (*rmp));
5282     rmp->_vl_msg_id = ntohs(VL_API_LISP_LOCATOR_DETAILS);
5283     rmp->context = context;
5284
5285     rmp->local = loc->local;
5286     if (loc->local) {
5287         rmp->sw_if_index = ntohl(loc->sw_if_index);
5288     } else {
5289         rmp->is_ipv6 = gid_address_ip_version(&loc->address);
5290         ip_address_copy_addr(rmp->ip_address, &gid_address_ip(&loc->address));
5291     }
5292     rmp->priority = loc->priority;
5293     rmp->weight = loc->weight;
5294
5295     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5296 }
5297
5298 static void
5299 vl_api_lisp_locator_dump_t_handler (vl_api_lisp_locator_dump_t *mp)
5300 {
5301     unix_shared_memory_queue_t * q = 0;
5302     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5303     locator_set_t * lsit = 0;
5304     locator_t * loc = 0;
5305     u32 ls_index = ~0, * locit = 0;
5306     u8 filter;
5307
5308     q = vl_api_client_index_to_input_queue (mp->client_index);
5309     if (q == 0) {
5310         return;
5311     }
5312
5313     ls_index = htonl(mp->locator_set_index);
5314
5315     lsit = pool_elt_at_index(lcm->locator_set_pool, ls_index);
5316
5317     filter = mp->filter;
5318     if (filter && !((1 == filter && lsit->local) ||
5319                     (2 == filter && !lsit->local))) {
5320           return;
5321       }
5322
5323     vec_foreach(locit, lsit->locator_indices) {
5324         loc = pool_elt_at_index(lcm->locator_pool, locit[0]);
5325         send_lisp_locator_details(lcm, loc, q, mp->context);
5326     };
5327 }
5328
5329 static void
5330 send_lisp_locator_set_details (lisp_cp_main_t *lcm,
5331                                locator_set_t *lsit,
5332                                unix_shared_memory_queue_t *q,
5333                                u32 context,
5334                                u32 ls_index)
5335 {
5336     vl_api_lisp_locator_set_details_t *rmp;
5337     u8 * str = 0;
5338
5339     rmp = vl_msg_api_alloc (sizeof (*rmp));
5340     memset (rmp, 0, sizeof (*rmp));
5341     rmp->_vl_msg_id = ntohs(VL_API_LISP_LOCATOR_SET_DETAILS);
5342     rmp->context = context;
5343
5344     rmp->local = lsit->local;
5345     rmp->locator_set_index= htonl(ls_index);
5346     if (lsit->local) {
5347       ASSERT(lsit->name != NULL);
5348       strncpy((char *) rmp->locator_set_name,
5349               (char *) lsit->name, ARRAY_LEN(rmp->locator_set_name) - 1);
5350     } else {
5351       str = format(0, "remote-%d", ls_index);
5352       strncpy((char *) rmp->locator_set_name, (char *) str,
5353               ARRAY_LEN(rmp->locator_set_name) - 1);
5354       vec_free(str);
5355     }
5356
5357     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5358 }
5359
5360 static void
5361 vl_api_lisp_locator_set_dump_t_handler (vl_api_lisp_locator_set_dump_t *mp)
5362 {
5363     unix_shared_memory_queue_t * q = NULL;
5364     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5365     locator_set_t * lsit = NULL;
5366     u32 index;
5367     u8 filter;
5368
5369     q = vl_api_client_index_to_input_queue (mp->client_index);
5370     if (q == 0) {
5371         return;
5372     }
5373
5374     filter = mp->filter;
5375     index = 0;
5376     pool_foreach (lsit, lcm->locator_set_pool,
5377         ({
5378             if (filter && !((1 == filter && lsit->local) ||
5379                             (2 == filter && !lsit->local))) {
5380                 index++;
5381                 continue;
5382             }
5383
5384             send_lisp_locator_set_details(lcm, lsit, q, mp->context, index++);
5385         }));
5386 }
5387
5388 static void
5389 send_lisp_eid_table_details (mapping_t *mapit,
5390                                    unix_shared_memory_queue_t *q,
5391                                    u32 context, u8 filter)
5392 {
5393     vl_api_lisp_eid_table_details_t *rmp = NULL;
5394     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5395     locator_set_t *ls = NULL;
5396     gid_address_t *gid = NULL;
5397     u8 * mac = 0;
5398     ip_prefix_t *ip_prefix = NULL;
5399
5400     ls = pool_elt_at_index (lcm->locator_set_pool,
5401                             mapit->locator_set_index);
5402
5403     switch (filter) {
5404     case 0:
5405         break;
5406     case 1:
5407         if (!ls->local) {
5408           return;
5409         }
5410         break;
5411     case 2:
5412         if (ls->local) {
5413           return;
5414         }
5415         break;
5416     default:
5417         clib_warning("Filter error, unknown filter: %d\n", filter);
5418         return;
5419     }
5420
5421     gid = &mapit->eid;
5422     ip_prefix = &gid_address_ippref(gid);
5423     mac = gid_address_mac(gid);
5424
5425     rmp = vl_msg_api_alloc (sizeof (*rmp));
5426     memset (rmp, 0, sizeof (*rmp));
5427     rmp->_vl_msg_id = ntohs(VL_API_LISP_EID_TABLE_DETAILS);
5428     rmp->locator_set_index = mapit->locator_set_index;
5429     rmp->is_local = ls->local;
5430     rmp->ttl = mapit->ttl;
5431     rmp->authoritative = mapit->authoritative;
5432
5433     switch (gid_address_type (gid))
5434       {
5435       case GID_ADDR_IP_PREFIX:
5436         rmp->eid_prefix_len = ip_prefix_len(ip_prefix);
5437         if (ip_prefix_version(ip_prefix) == IP4)
5438           {
5439             rmp->eid_type = 0; /* ipv4 type */
5440             clib_memcpy(rmp->eid, &ip_prefix_v4(ip_prefix),
5441                    sizeof(ip_prefix_v4(ip_prefix)));
5442           }
5443         else
5444           {
5445             rmp->eid_type = 1; /* ipv6 type */
5446             clib_memcpy(rmp->eid, &ip_prefix_v6(ip_prefix),
5447                    sizeof(ip_prefix_v6(ip_prefix)));
5448           }
5449         break;
5450       case GID_ADDR_MAC:
5451         rmp->eid_type = 2; /* l2 mac type */
5452         clib_memcpy(rmp->eid, mac, 6);
5453         break;
5454       default:
5455         ASSERT(0);
5456       }
5457     rmp->context = context;
5458     rmp->vni = clib_host_to_net_u32 (gid_address_vni (gid));
5459     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5460 }
5461
5462 static void
5463 vl_api_lisp_eid_table_dump_t_handler (
5464     vl_api_lisp_eid_table_dump_t *mp)
5465 {
5466     u32 mi;
5467     unix_shared_memory_queue_t * q = NULL;
5468     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5469     mapping_t * mapit = NULL;
5470     gid_address_t _eid, * eid = &_eid;
5471
5472     q = vl_api_client_index_to_input_queue (mp->client_index);
5473     if (q == 0) {
5474         return;
5475     }
5476
5477     if (mp->eid_set) {
5478         memset (eid, 0, sizeof (*eid));
5479         gid_address_vni(eid) = ntohl(mp->vni);
5480         switch (mp->eid_type) {
5481         case 0:
5482             gid_address_type(eid) = GID_ADDR_IP_PREFIX;
5483             gid_address_ippref_len(eid) = mp->prefix_length;
5484             gid_address_ip_version(eid) = IP4;
5485             clib_memcpy (&gid_address_ippref(eid), mp->eid, 4);
5486             break;
5487         case 1:
5488             gid_address_type(eid) = GID_ADDR_IP_PREFIX;
5489             gid_address_ippref_len(eid) = mp->prefix_length;
5490             gid_address_ip_version(eid) = IP6;
5491             clib_memcpy (&gid_address_ippref(eid), mp->eid, 16);
5492             break;
5493         case 2:
5494             gid_address_type(eid) = GID_ADDR_MAC;
5495             clib_memcpy (gid_address_mac(eid), mp->eid, 6);
5496             break;
5497         }
5498         mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
5499         if ((u32)~0 == mi)
5500           return;
5501
5502         mapit = pool_elt_at_index (lcm->mapping_pool, mi);
5503         send_lisp_eid_table_details(mapit, q, mp->context, mp->filter);
5504     } else {
5505         pool_foreach (mapit, lcm->mapping_pool,
5506         ({
5507             send_lisp_eid_table_details(mapit, q, mp->context,
5508                                         mp->filter);
5509         }));
5510     }
5511 }
5512
5513 static void
5514 send_lisp_gpe_tunnel_details (lisp_gpe_tunnel_t *tunnel,
5515                               unix_shared_memory_queue_t *q,
5516                               u32 context)
5517 {
5518     vl_api_lisp_gpe_tunnel_details_t *rmp;
5519     lisp_gpe_main_t * lgm = &lisp_gpe_main;
5520
5521     rmp = vl_msg_api_alloc (sizeof (*rmp));
5522     memset (rmp, 0, sizeof (*rmp));
5523     rmp->_vl_msg_id = ntohs(VL_API_LISP_GPE_TUNNEL_DETAILS);
5524
5525     rmp->tunnels = tunnel - lgm->tunnels;
5526
5527     rmp->is_ipv6 = ip_addr_version(&tunnel->src) == IP6 ? 1 : 0;
5528     ip_address_copy_addr(rmp->source_ip, &tunnel->src);
5529     ip_address_copy_addr(rmp->destination_ip, &tunnel->dst);
5530
5531     rmp->encap_fib_id = htonl(tunnel->encap_fib_index);
5532     rmp->decap_fib_id = htonl(tunnel->decap_fib_index);
5533     rmp->dcap_next = htonl(tunnel->decap_next_index);
5534     rmp->lisp_ver = tunnel->ver_res;
5535     rmp->next_protocol = tunnel->next_protocol;
5536     rmp->flags = tunnel->flags;
5537     rmp->ver_res = tunnel->ver_res;
5538     rmp->res = tunnel->res;
5539     rmp->iid = htonl(tunnel->vni);
5540     rmp->context = context;
5541
5542     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5543 }
5544
5545 static void
5546 vl_api_lisp_gpe_tunnel_dump_t_handler (
5547     vl_api_lisp_gpe_tunnel_dump_t *mp)
5548 {
5549     unix_shared_memory_queue_t * q = NULL;
5550     lisp_gpe_main_t * lgm = &lisp_gpe_main;
5551     lisp_gpe_tunnel_t * tunnel = NULL;
5552
5553     if (pool_elts(lgm->tunnels) == 0) {
5554         return;
5555     }
5556
5557     q = vl_api_client_index_to_input_queue (mp->client_index);
5558     if (q == 0) {
5559         return;
5560     }
5561
5562     pool_foreach(tunnel, lgm->tunnels,
5563                  ({
5564                      send_lisp_gpe_tunnel_details(tunnel, q, mp->context);
5565                 }));
5566 }
5567
5568 static void
5569 send_lisp_map_resolver_details (ip_address_t *ip,
5570                                 unix_shared_memory_queue_t *q,
5571                                 u32 context)
5572 {
5573     vl_api_lisp_map_resolver_details_t *rmp = NULL;
5574
5575     rmp = vl_msg_api_alloc (sizeof (*rmp));
5576     memset (rmp, 0, sizeof (*rmp));
5577     rmp->_vl_msg_id = ntohs(VL_API_LISP_MAP_RESOLVER_DETAILS);
5578
5579     switch (ip_addr_version(ip)) {
5580         case IP4:
5581             rmp->is_ipv6 = 0;
5582             clib_memcpy(rmp->ip_address, &ip_addr_v4(ip), sizeof(ip_addr_v4(ip)));
5583             break;
5584
5585         case IP6:
5586             rmp->is_ipv6 = 1;
5587             clib_memcpy(rmp->ip_address, &ip_addr_v6(ip), sizeof(ip_addr_v6(ip)));
5588             break;
5589
5590         default:
5591             ASSERT(0);
5592     }
5593     rmp->context = context;
5594
5595     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5596 }
5597
5598 static void
5599 vl_api_lisp_map_resolver_dump_t_handler (
5600     vl_api_lisp_map_resolver_dump_t *mp)
5601 {
5602     unix_shared_memory_queue_t * q = NULL;
5603     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5604     ip_address_t *ip = NULL;
5605
5606     q = vl_api_client_index_to_input_queue (mp->client_index);
5607     if (q == 0) {
5608         return;
5609     }
5610
5611     vec_foreach(ip, lcm->map_resolvers) {
5612         send_lisp_map_resolver_details(ip, q, mp->context);
5613     }
5614
5615 }
5616
5617 static void
5618 send_eid_table_map_pair (hash_pair_t * p,
5619                          unix_shared_memory_queue_t * q,
5620                          u32 context)
5621 {
5622     vl_api_lisp_eid_table_map_details_t * rmp = NULL;
5623
5624     rmp = vl_msg_api_alloc (sizeof (*rmp));
5625     memset (rmp, 0, sizeof (*rmp));
5626     rmp->_vl_msg_id = ntohs(VL_API_LISP_EID_TABLE_MAP_DETAILS);
5627
5628     rmp->vni = clib_host_to_net_u32 (p->key);
5629     rmp->vrf = clib_host_to_net_u32 (p->value[0]);
5630     rmp->context = context;
5631     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5632 }
5633
5634 static void
5635 vl_api_lisp_eid_table_map_dump_t_handler (
5636     vl_api_lisp_eid_table_map_dump_t *mp)
5637 {
5638     unix_shared_memory_queue_t * q = NULL;
5639     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5640     hash_pair_t * p;
5641
5642     q = vl_api_client_index_to_input_queue (mp->client_index);
5643     if (q == 0) {
5644         return;
5645     }
5646     hash_foreach_pair (p, lcm->table_id_by_vni, {
5647         send_eid_table_map_pair (p, q, mp->context);
5648     });
5649 }
5650
5651 static void
5652 vl_api_show_lisp_status_t_handler
5653 (vl_api_show_lisp_status_t *mp)
5654 {
5655     unix_shared_memory_queue_t * q = NULL;
5656     vl_api_show_lisp_status_reply_t *rmp = NULL;
5657     int rv = 0;
5658
5659     q = vl_api_client_index_to_input_queue (mp->client_index);
5660     if (q == 0) {
5661         return;
5662     }
5663
5664     REPLY_MACRO2(VL_API_SHOW_LISP_STATUS_REPLY,
5665     ({
5666       rmp->gpe_status = vnet_lisp_gpe_enable_disable_status ();
5667       rmp->feature_status = vnet_lisp_enable_disable_status ();
5668     }));
5669 }
5670
5671 static void
5672 vl_api_lisp_get_map_request_itr_rlocs_t_handler (
5673     vl_api_lisp_get_map_request_itr_rlocs_t *mp)
5674 {
5675     unix_shared_memory_queue_t * q = NULL;
5676     vl_api_lisp_get_map_request_itr_rlocs_reply_t *rmp = NULL;
5677     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5678     locator_set_t * loc_set = 0;
5679     u8 * tmp_str = 0;
5680     int rv = 0;
5681
5682     q = vl_api_client_index_to_input_queue (mp->client_index);
5683     if (q == 0) {
5684         return;
5685     }
5686
5687     if (~0 == lcm->mreq_itr_rlocs) {
5688       tmp_str = format(0, " ");
5689     } else {
5690       loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
5691       tmp_str = format(0, "%s", loc_set->name);
5692     }
5693
5694     REPLY_MACRO2(VL_API_LISP_GET_MAP_REQUEST_ITR_RLOCS_REPLY,
5695     ({
5696       strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
5697               ARRAY_LEN(rmp->locator_set_name) - 1);
5698     }));
5699
5700     vec_free(tmp_str);
5701 }
5702
5703 static void
5704 vl_api_show_lisp_pitr_t_handler (vl_api_show_lisp_pitr_t * mp)
5705 {
5706     unix_shared_memory_queue_t * q = NULL;
5707     vl_api_show_lisp_pitr_reply_t * rmp = NULL;
5708     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5709     mapping_t * m;
5710     locator_set_t * ls = 0;
5711     u8 * tmp_str = 0;
5712     int rv = 0;
5713
5714     q = vl_api_client_index_to_input_queue (mp->client_index);
5715     if (q == 0) {
5716         return;
5717     }
5718
5719     if (~0 == lcm->pitr_map_index) {
5720       tmp_str = format(0, "N/A");
5721     } else {
5722       m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
5723       if (~0 != m->locator_set_index) {
5724         ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
5725         tmp_str = format(0, "%s", ls->name);
5726       } else {
5727         tmp_str = format(0, "N/A");
5728       }
5729     }
5730     vec_add1(tmp_str, 0);
5731
5732     REPLY_MACRO2(VL_API_SHOW_LISP_PITR_REPLY,
5733     ({
5734       rmp->status = lcm->lisp_pitr;
5735       strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
5736               ARRAY_LEN(rmp->locator_set_name) - 1);
5737     }));
5738 }
5739
5740 static void
5741 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *mp)
5742 {
5743     vl_api_interface_name_renumber_reply_t * rmp;
5744     int rv = 0;
5745
5746     VALIDATE_SW_IF_INDEX(mp);
5747
5748     rv = vnet_interface_name_renumber
5749         (ntohl(mp->sw_if_index), ntohl(mp->new_show_dev_instance));
5750
5751     BAD_SW_IF_INDEX_LABEL;
5752
5753     REPLY_MACRO(VL_API_INTERFACE_NAME_RENUMBER_REPLY);
5754 }
5755
5756 static int arp_change_data_callback (u32 pool_index, u8 * new_mac,
5757                                      u32 sw_if_index, u32 address)
5758 {
5759     vpe_api_main_t * am = &vpe_api_main;
5760     vlib_main_t * vm = am->vlib_main;
5761     vl_api_ip4_arp_event_t * event;
5762     static f64 arp_event_last_time;
5763     f64 now = vlib_time_now (vm);
5764
5765     if (pool_is_free_index (am->arp_events, pool_index))
5766         return 1;
5767
5768     event = pool_elt_at_index (am->arp_events, pool_index);
5769     if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac))) {
5770         clib_memcpy (event->new_mac, new_mac, sizeof(event->new_mac));
5771     } else { /* same mac */
5772         if ((sw_if_index == event->sw_if_index) &&
5773             ((address == 0) ||
5774              /* for BD case, also check IP address with 10 sec timeout */
5775              ((address == event->address) &&
5776               ((now - arp_event_last_time) < 10.0))))
5777             return 1;
5778     }
5779
5780     arp_event_last_time = now;
5781     event->sw_if_index = sw_if_index;
5782     if (address) event->address = address;
5783     return 0;
5784 }
5785
5786 static int arp_change_delete_callback (u32 pool_index, u8 * notused)
5787 {
5788     vpe_api_main_t * am = &vpe_api_main;
5789
5790     if (pool_is_free_index (am->arp_events, pool_index))
5791         return 1;
5792
5793     pool_put_index (am->arp_events, pool_index);
5794     return 0;
5795 }
5796
5797 static void
5798 vl_api_want_ip4_arp_events_t_handler
5799 (vl_api_want_ip4_arp_events_t * mp)
5800 {
5801     vpe_api_main_t * am = &vpe_api_main;
5802     vnet_main_t * vnm = vnet_get_main();
5803     vl_api_want_ip4_arp_events_reply_t *rmp;
5804     vl_api_ip4_arp_event_t * event;
5805     int rv;
5806
5807     if (mp->enable_disable) {
5808         pool_get (am->arp_events, event);
5809         memset (event, 0, sizeof (*event));
5810
5811         event->_vl_msg_id = ntohs(VL_API_IP4_ARP_EVENT);
5812         event->client_index = mp->client_index;
5813         event->context = mp->context;
5814         event->address = mp->address;
5815         event->pid = mp->pid;
5816
5817         rv = vnet_add_del_ip4_arp_change_event
5818             (vnm, arp_change_data_callback,
5819              mp->pid,
5820              &mp->address /* addr, in net byte order */,
5821              vpe_resolver_process_node.index,
5822              IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */);
5823     } else {
5824         rv = vnet_add_del_ip4_arp_change_event
5825             (vnm, arp_change_delete_callback,
5826              mp->pid,
5827              &mp->address /* addr, in net byte order */,
5828              vpe_resolver_process_node.index,
5829              IP4_ARP_EVENT, ~0 /* pool index */, 0 /* is_add */);
5830     }
5831     REPLY_MACRO(VL_API_WANT_IP4_ARP_EVENTS_REPLY);
5832 }
5833
5834 static void vl_api_input_acl_set_interface_t_handler
5835 (vl_api_input_acl_set_interface_t * mp)
5836 {
5837     vlib_main_t *vm = vlib_get_main();
5838     vl_api_input_acl_set_interface_reply_t * rmp;
5839     int rv;
5840     u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
5841
5842     ip4_table_index = ntohl(mp->ip4_table_index);
5843     ip6_table_index = ntohl(mp->ip6_table_index);
5844     l2_table_index = ntohl(mp->l2_table_index);
5845     sw_if_index = ntohl(mp->sw_if_index);
5846
5847     VALIDATE_SW_IF_INDEX(mp);
5848
5849     rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
5850                                    ip6_table_index, l2_table_index,
5851                                    mp->is_add);
5852
5853     BAD_SW_IF_INDEX_LABEL;
5854
5855     REPLY_MACRO(VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
5856 }
5857
5858 static void vl_api_ipsec_spd_add_del_t_handler
5859 (vl_api_ipsec_spd_add_del_t * mp)
5860 {
5861 #if IPSEC == 0
5862     clib_warning ("unimplemented");
5863 #else
5864
5865     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5866     vl_api_ipsec_spd_add_del_reply_t * rmp;
5867     int rv;
5868
5869 #if DPDK > 0
5870     rv = ipsec_add_del_spd (vm, ntohl(mp->spd_id), mp->is_add);
5871 #else
5872     rv = VNET_API_ERROR_UNIMPLEMENTED;
5873 #endif
5874
5875     REPLY_MACRO(VL_API_IPSEC_SPD_ADD_DEL_REPLY);
5876 #endif
5877 }
5878
5879 static void vl_api_ipsec_interface_add_del_spd_t_handler
5880 (vl_api_ipsec_interface_add_del_spd_t * mp)
5881 {
5882     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5883     vl_api_ipsec_interface_add_del_spd_reply_t * rmp;
5884     int rv;
5885     u32 sw_if_index __attribute__((unused));
5886     u32 spd_id __attribute__((unused));
5887
5888     sw_if_index = ntohl(mp->sw_if_index);
5889     spd_id = ntohl(mp->spd_id);
5890
5891     VALIDATE_SW_IF_INDEX(mp);
5892
5893 #if IPSEC > 0
5894     rv = ipsec_set_interface_spd(vm, sw_if_index, spd_id, mp->is_add);
5895 #else
5896     rv = VNET_API_ERROR_UNIMPLEMENTED;
5897 #endif
5898
5899     BAD_SW_IF_INDEX_LABEL;
5900
5901     REPLY_MACRO(VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
5902 }
5903
5904 static void vl_api_ipsec_spd_add_del_entry_t_handler
5905 (vl_api_ipsec_spd_add_del_entry_t * mp)
5906 {
5907     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5908     vl_api_ipsec_spd_add_del_entry_reply_t * rmp;
5909     int rv;
5910
5911 #if IPSEC > 0
5912     ipsec_policy_t p;
5913
5914     memset(&p, 0, sizeof(p));
5915
5916     p.id = ntohl(mp->spd_id);
5917     p.priority = ntohl(mp->priority);
5918     p.is_outbound = mp->is_outbound;
5919     p.is_ipv6 = mp->is_ipv6;
5920
5921     if (mp->is_ipv6 || mp->is_ip_any) {
5922         clib_memcpy(&p.raddr.start, mp->remote_address_start, 16);
5923         clib_memcpy(&p.raddr.stop, mp->remote_address_stop, 16);
5924         clib_memcpy(&p.laddr.start, mp->local_address_start, 16);
5925         clib_memcpy(&p.laddr.stop, mp->local_address_stop, 16);
5926     } else {
5927         clib_memcpy(&p.raddr.start.ip4.data, mp->remote_address_start, 4);
5928         clib_memcpy(&p.raddr.stop.ip4.data, mp->remote_address_stop, 4);
5929         clib_memcpy(&p.laddr.start.ip4.data, mp->local_address_start, 4);
5930         clib_memcpy(&p.laddr.stop.ip4.data, mp->local_address_stop, 4);
5931     }
5932     p.protocol = mp->protocol;
5933     p.rport.start = ntohs(mp->remote_port_start);
5934     p.rport.stop  = ntohs(mp->remote_port_stop);
5935     p.lport.start = ntohs(mp->local_port_start);
5936     p.lport.stop  = ntohs(mp->local_port_stop);
5937     /* policy action resolve unsupported */
5938     if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE) {
5939         clib_warning("unsupported action: 'resolve'");
5940         rv = VNET_API_ERROR_UNIMPLEMENTED;
5941         goto out;
5942     }
5943     p.policy = mp->policy;
5944     p.sa_id = ntohl(mp->sa_id);
5945
5946     rv = ipsec_add_del_policy(vm, &p, mp->is_add);
5947     if (rv)
5948       goto out;
5949
5950     if (mp->is_ip_any) {
5951       p.is_ipv6 = 1;
5952       rv = ipsec_add_del_policy(vm, &p, mp->is_add);
5953     }
5954 #else
5955     rv = VNET_API_ERROR_UNIMPLEMENTED;
5956     goto out;
5957 #endif
5958
5959 out:
5960     REPLY_MACRO(VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
5961 }
5962
5963 static void vl_api_ipsec_sad_add_del_entry_t_handler
5964 (vl_api_ipsec_sad_add_del_entry_t * mp)
5965 {
5966     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5967     vl_api_ipsec_sad_add_del_entry_reply_t * rmp;
5968     int rv;
5969 #if IPSEC > 0
5970     ipsec_sa_t sa;
5971
5972     memset(&sa, 0, sizeof(sa));
5973
5974     sa.id = ntohl(mp->sad_id);
5975     sa.spi = ntohl(mp->spi);
5976     /* security protocol AH unsupported */
5977     if (mp->protocol == IPSEC_PROTOCOL_AH) {
5978         clib_warning("unsupported security protocol 'AH'");
5979         rv = VNET_API_ERROR_UNIMPLEMENTED;
5980         goto out;
5981     }
5982     sa.protocol = mp->protocol;
5983     /* check for unsupported crypto-alg */
5984     if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
5985         mp->crypto_algorithm > IPSEC_CRYPTO_ALG_AES_CBC_256) {
5986         clib_warning("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
5987                      mp->crypto_algorithm);
5988         rv = VNET_API_ERROR_UNIMPLEMENTED;
5989         goto out;
5990     }
5991     sa.crypto_alg = mp->crypto_algorithm;
5992     sa.crypto_key_len = mp->crypto_key_length;
5993     clib_memcpy(&sa.crypto_key, mp->crypto_key, sizeof(sa.crypto_key));
5994     /* check for unsupported integ-alg */
5995     if (mp->integrity_algorithm < IPSEC_INTEG_ALG_SHA1_96 ||
5996         mp->integrity_algorithm > IPSEC_INTEG_ALG_SHA_512_256) {
5997         clib_warning("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
5998                      mp->integrity_algorithm);
5999         rv = VNET_API_ERROR_UNIMPLEMENTED;
6000         goto out;
6001     }
6002     sa.integ_alg = mp->integrity_algorithm;
6003     sa.integ_key_len = mp->integrity_key_length;
6004     clib_memcpy(&sa.integ_key, mp->integrity_key, sizeof(sa.integ_key));
6005     sa.use_esn = mp->use_extended_sequence_number;
6006     sa.is_tunnel = mp->is_tunnel;
6007     sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
6008     if (sa.is_tunnel_ip6) {
6009         clib_memcpy(&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
6010         clib_memcpy(&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
6011     } else {
6012         clib_memcpy(&sa.tunnel_src_addr.ip4.data, mp->tunnel_src_address, 4);
6013         clib_memcpy(&sa.tunnel_dst_addr.ip4.data, mp->tunnel_dst_address, 4);
6014     }
6015
6016     rv = ipsec_add_del_sa(vm, &sa, mp->is_add);
6017 #else
6018     rv = VNET_API_ERROR_UNIMPLEMENTED;
6019     goto out;
6020 #endif
6021
6022 out:
6023     REPLY_MACRO(VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
6024 }
6025
6026 static void
6027 vl_api_ikev2_profile_add_del_t_handler
6028 (vl_api_ikev2_profile_add_del_t * mp)
6029 {
6030     vl_api_ikev2_profile_add_del_reply_t * rmp;
6031     int rv = 0;
6032
6033 #if IPSEC > 0
6034     vlib_main_t * vm = vlib_get_main();
6035     clib_error_t * error;
6036     u8 * tmp = format(0, "%s", mp->name);
6037     error = ikev2_add_del_profile(vm, tmp, mp->is_add);
6038     vec_free (tmp);
6039     if (error)
6040       rv = VNET_API_ERROR_UNSPECIFIED;
6041 #else
6042     rv = VNET_API_ERROR_UNIMPLEMENTED;
6043 #endif
6044
6045    REPLY_MACRO(VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
6046 }
6047
6048 static void
6049 vl_api_ikev2_profile_set_auth_t_handler
6050 (vl_api_ikev2_profile_set_auth_t * mp)
6051 {
6052     vl_api_ikev2_profile_set_auth_reply_t * rmp;
6053     int rv = 0;
6054
6055 #if IPSEC > 0
6056     vlib_main_t * vm = vlib_get_main();
6057     clib_error_t * error;
6058     u8 * tmp = format(0, "%s", mp->name);
6059     u8 * data = vec_new (u8, mp->data_len);
6060     clib_memcpy(data, mp->data, mp->data_len);
6061     error = ikev2_set_profile_auth(vm, tmp, mp->auth_method, data, mp->is_hex);
6062     vec_free (tmp);
6063     vec_free (data);
6064     if (error)
6065       rv = VNET_API_ERROR_UNSPECIFIED;
6066 #else
6067     rv = VNET_API_ERROR_UNIMPLEMENTED;
6068 #endif
6069
6070    REPLY_MACRO(VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
6071 }
6072
6073 static void
6074 vl_api_ikev2_profile_set_id_t_handler
6075 (vl_api_ikev2_profile_set_id_t * mp)
6076 {
6077     vl_api_ikev2_profile_add_del_reply_t * rmp;
6078     int rv = 0;
6079
6080 #if IPSEC > 0
6081     vlib_main_t * vm = vlib_get_main();
6082     clib_error_t * error;
6083     u8 * tmp = format(0, "%s", mp->name);
6084     u8 * data = vec_new (u8, mp->data_len);
6085     clib_memcpy(data, mp->data, mp->data_len);
6086     error = ikev2_set_profile_id(vm, tmp, mp->id_type, data, mp->is_local);
6087     vec_free (tmp);
6088     vec_free (data);
6089     if (error)
6090       rv = VNET_API_ERROR_UNSPECIFIED;
6091 #else
6092     rv = VNET_API_ERROR_UNIMPLEMENTED;
6093 #endif
6094
6095    REPLY_MACRO(VL_API_IKEV2_PROFILE_SET_ID_REPLY);
6096 }
6097
6098 static void
6099 vl_api_ikev2_profile_set_ts_t_handler
6100 (vl_api_ikev2_profile_set_ts_t * mp)
6101 {
6102     vl_api_ikev2_profile_set_ts_reply_t * rmp;
6103     int rv = 0;
6104
6105 #if IPSEC > 0
6106     vlib_main_t * vm = vlib_get_main();
6107     clib_error_t * error;
6108     u8 * tmp = format(0, "%s", mp->name);
6109     error = ikev2_set_profile_ts(vm, tmp, mp->proto, mp->start_port,
6110                                  mp->end_port, (ip4_address_t) mp->start_addr,
6111                                  (ip4_address_t) mp->end_addr, mp->is_local);
6112     vec_free (tmp);
6113     if (error)
6114       rv = VNET_API_ERROR_UNSPECIFIED;
6115 #else
6116     rv = VNET_API_ERROR_UNIMPLEMENTED;
6117 #endif
6118
6119    REPLY_MACRO(VL_API_IKEV2_PROFILE_SET_TS_REPLY);
6120 }
6121
6122 static void
6123 vl_api_ikev2_set_local_key_t_handler
6124 (vl_api_ikev2_set_local_key_t * mp)
6125 {
6126     vl_api_ikev2_profile_set_ts_reply_t * rmp;
6127     int rv = 0;
6128
6129 #if IPSEC > 0
6130     vlib_main_t * vm = vlib_get_main();
6131     clib_error_t * error;
6132
6133     error = ikev2_set_local_key(vm, mp->key_file);
6134     if (error)
6135       rv = VNET_API_ERROR_UNSPECIFIED;
6136 #else
6137     rv = VNET_API_ERROR_UNIMPLEMENTED;
6138 #endif
6139
6140    REPLY_MACRO(VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
6141 }
6142
6143 static void
6144 vl_api_map_add_domain_t_handler
6145 (vl_api_map_add_domain_t * mp)
6146 {
6147   vl_api_map_add_domain_reply_t * rmp;
6148   int rv = 0;
6149   u32 index;
6150   u8 flags = mp->is_translation ? MAP_DOMAIN_TRANSLATION : 0;
6151   rv = map_create_domain((ip4_address_t *)&mp->ip4_prefix, mp->ip4_prefix_len,
6152                          (ip6_address_t *)&mp->ip6_prefix, mp->ip6_prefix_len,
6153                          (ip6_address_t *)&mp->ip6_src, mp->ip6_src_prefix_len,
6154                          mp->ea_bits_len, mp->psid_offset, mp->psid_length, &index, ntohs(mp->mtu), flags);
6155
6156   REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
6157                ({
6158                  rmp->index = ntohl(index);
6159                }));
6160 }
6161
6162 static void
6163 vl_api_map_del_domain_t_handler
6164 (vl_api_map_del_domain_t * mp)
6165 {
6166   vl_api_map_del_domain_reply_t * rmp;
6167   int rv = 0;
6168
6169   rv = map_delete_domain(ntohl(mp->index));
6170
6171   REPLY_MACRO(VL_API_MAP_DEL_DOMAIN_REPLY);
6172 }
6173
6174 static void
6175 vl_api_map_add_del_rule_t_handler
6176 (vl_api_map_add_del_rule_t * mp)
6177 {
6178   vl_api_map_del_domain_reply_t * rmp;
6179   int rv = 0;
6180
6181   rv = map_add_del_psid(ntohl(mp->index), ntohs(mp->psid), (ip6_address_t *)mp->ip6_dst, mp->is_add);
6182
6183   REPLY_MACRO(VL_API_MAP_ADD_DEL_RULE_REPLY);
6184 }
6185
6186 static void
6187 vl_api_map_domain_dump_t_handler
6188 (vl_api_map_domain_dump_t * mp)
6189 {
6190   vl_api_map_domain_details_t * rmp;
6191   map_main_t *mm = &map_main;
6192   map_domain_t *d;
6193   unix_shared_memory_queue_t * q;
6194
6195   if (pool_elts (mm->domains) == 0)
6196       return;
6197
6198   q = vl_api_client_index_to_input_queue (mp->client_index);
6199   if (q == 0) {
6200     return;
6201   }
6202
6203   pool_foreach(d, mm->domains, ({
6204     /* Make sure every field is initiated (or don't skip the memset()) */
6205     rmp = vl_msg_api_alloc (sizeof (*rmp));
6206     rmp->_vl_msg_id = ntohs(VL_API_MAP_DOMAIN_DETAILS);
6207     rmp->domain_index = htonl(d - mm->domains);
6208     rmp->ea_bits_len = d->ea_bits_len;
6209     rmp->psid_offset = d->psid_offset;
6210     rmp->psid_length = d->psid_length;
6211     clib_memcpy(rmp->ip4_prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix));
6212     rmp->ip4_prefix_len = d->ip4_prefix_len;
6213     clib_memcpy(rmp->ip6_prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix));
6214     rmp->ip6_prefix_len = d->ip6_prefix_len;
6215     clib_memcpy(rmp->ip6_src, &d->ip6_src, sizeof(rmp->ip6_src));
6216     rmp->ip6_src_len = d->ip6_src_len;
6217     rmp->mtu = htons(d->mtu);
6218     rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION);
6219     rmp->context = mp->context;
6220
6221     vl_msg_api_send_shmem (q, (u8 *)&rmp);
6222   }));
6223 }
6224
6225 static void
6226 vl_api_map_rule_dump_t_handler
6227 (vl_api_map_rule_dump_t * mp)
6228 {
6229   unix_shared_memory_queue_t * q;
6230   u16 i;
6231   ip6_address_t dst;
6232   vl_api_map_rule_details_t * rmp;
6233   map_main_t *mm = &map_main;
6234   u32 domain_index = ntohl(mp->domain_index);
6235   map_domain_t *d;
6236
6237   if (pool_elts (mm->domains) == 0)
6238    return;
6239
6240   d = pool_elt_at_index(mm->domains, domain_index);
6241   if (!d || !d->rules) {
6242     return;
6243   }
6244
6245   q = vl_api_client_index_to_input_queue (mp->client_index);
6246   if (q == 0) {
6247     return;
6248   }
6249
6250   for (i = 0; i < (0x1 << d->psid_length); i++) {
6251     dst = d->rules[i];
6252     if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0) {
6253       continue;
6254     }
6255     rmp = vl_msg_api_alloc(sizeof(*rmp));
6256     memset(rmp, 0, sizeof(*rmp));
6257     rmp->_vl_msg_id = ntohs(VL_API_MAP_RULE_DETAILS);
6258     rmp->psid = htons(i);
6259     clib_memcpy(rmp->ip6_dst, &dst, sizeof(rmp->ip6_dst));
6260     rmp->context = mp->context;
6261     vl_msg_api_send_shmem(q, (u8 *)&rmp);
6262   }
6263 }
6264
6265 static void
6266 vl_api_map_summary_stats_t_handler (
6267     vl_api_map_summary_stats_t *mp)
6268 {
6269     vl_api_map_summary_stats_reply_t *rmp;
6270     vlib_combined_counter_main_t *cm;
6271     vlib_counter_t v;
6272     int i, which;
6273     u64 total_pkts[VLIB_N_RX_TX];
6274     u64 total_bytes[VLIB_N_RX_TX];
6275     map_main_t *mm = &map_main;
6276     unix_shared_memory_queue_t *q =
6277         vl_api_client_index_to_input_queue(mp->client_index);
6278
6279     if (!q)
6280         return;
6281
6282     rmp = vl_msg_api_alloc (sizeof (*rmp));
6283     rmp->_vl_msg_id = ntohs(VL_API_MAP_SUMMARY_STATS_REPLY);
6284     rmp->context = mp->context;
6285     rmp->retval = 0;
6286
6287     memset (total_pkts, 0, sizeof (total_pkts));
6288     memset (total_bytes, 0, sizeof (total_bytes));
6289
6290     map_domain_counter_lock (mm);
6291     vec_foreach(cm, mm->domain_counters) {
6292       which = cm - mm->domain_counters;
6293
6294       for (i = 0; i < vec_len(cm->maxi); i++) {
6295         vlib_get_combined_counter (cm, i, &v);
6296         total_pkts[which] += v.packets;
6297         total_bytes[which] += v.bytes;
6298       }
6299     }
6300
6301     map_domain_counter_unlock (mm);
6302
6303     /* Note: in network byte order! */
6304     rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] = clib_host_to_net_u64(total_pkts[MAP_DOMAIN_COUNTER_RX]);
6305     rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] = clib_host_to_net_u64(total_bytes[MAP_DOMAIN_COUNTER_RX]);
6306     rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] = clib_host_to_net_u64(total_pkts[MAP_DOMAIN_COUNTER_TX]);
6307     rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] = clib_host_to_net_u64(total_bytes[MAP_DOMAIN_COUNTER_TX]);
6308     rmp->total_bindings = clib_host_to_net_u64(pool_elts(mm->domains));
6309     rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
6310     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));
6311     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));
6312
6313     vl_msg_api_send_shmem(q, (u8 *)&rmp);
6314 }
6315
6316 static void vl_api_ipsec_sa_set_key_t_handler
6317 (vl_api_ipsec_sa_set_key_t * mp)
6318 {
6319     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
6320     vl_api_ipsec_sa_set_key_reply_t *rmp;
6321     int rv;
6322 #if IPSEC > 0
6323     ipsec_sa_t sa;
6324     sa.id = ntohl(mp->sa_id);
6325     sa.crypto_key_len = mp->crypto_key_length;
6326     clib_memcpy(&sa.crypto_key, mp->crypto_key, sizeof(sa.crypto_key));
6327     sa.integ_key_len = mp->integrity_key_length;
6328     clib_memcpy(&sa.integ_key, mp->integrity_key, sizeof(sa.integ_key));
6329
6330     rv = ipsec_set_sa_key(vm, &sa);
6331 #else
6332     rv = VNET_API_ERROR_UNIMPLEMENTED;
6333 #endif
6334
6335     REPLY_MACRO(VL_API_IPSEC_SA_SET_KEY_REPLY);
6336 }
6337
6338 static void vl_api_cop_interface_enable_disable_t_handler
6339 (vl_api_cop_interface_enable_disable_t * mp)
6340 {
6341     vl_api_cop_interface_enable_disable_reply_t * rmp;
6342     int rv;
6343     u32 sw_if_index = ntohl(mp->sw_if_index);
6344     int enable_disable;
6345
6346     VALIDATE_SW_IF_INDEX(mp);
6347
6348     enable_disable = (int) mp->enable_disable;
6349
6350     rv = cop_interface_enable_disable (sw_if_index, enable_disable);
6351
6352     BAD_SW_IF_INDEX_LABEL;
6353
6354     REPLY_MACRO(VL_API_COP_INTERFACE_ENABLE_DISABLE_REPLY);
6355 }
6356
6357 static void vl_api_cop_whitelist_enable_disable_t_handler
6358 (vl_api_cop_whitelist_enable_disable_t * mp)
6359 {
6360     vl_api_cop_whitelist_enable_disable_reply_t * rmp;
6361     cop_whitelist_enable_disable_args_t _a, *a=&_a;
6362     u32 sw_if_index = ntohl(mp->sw_if_index);
6363     int rv;
6364
6365     VALIDATE_SW_IF_INDEX(mp);
6366
6367     a->sw_if_index = sw_if_index;
6368     a->ip4 = mp->ip4;
6369     a->ip6 = mp->ip6;
6370     a->default_cop = mp->default_cop;
6371     a->fib_id = ntohl(mp->fib_id);
6372
6373     rv = cop_whitelist_enable_disable (a);
6374
6375     BAD_SW_IF_INDEX_LABEL;
6376
6377     REPLY_MACRO(VL_API_COP_WHITELIST_ENABLE_DISABLE_REPLY);
6378 }
6379
6380 static void vl_api_get_node_graph_t_handler
6381 (vl_api_get_node_graph_t * mp)
6382 {
6383     int rv = 0;
6384     u8 * vector = 0;
6385     api_main_t * am = &api_main;
6386     vlib_main_t * vm = vlib_get_main();
6387     void * oldheap;
6388     vl_api_get_node_graph_reply_t * rmp;
6389
6390     pthread_mutex_lock (&am->vlib_rp->mutex);
6391     oldheap = svm_push_data_heap (am->vlib_rp);
6392
6393     /*
6394      * Keep the number of memcpy ops to a minimum (e.g. 1).
6395      */
6396     vec_validate (vector, 16384);
6397     vec_reset_length (vector);
6398
6399     /* $$$$ FIXME */
6400     vector = vlib_node_serialize (&vm->node_main, vector,
6401                                   (u32)~0 /* all threads */,
6402                                   1 /* include nexts */,
6403                                   1 /* include stats */);
6404
6405     svm_pop_heap (oldheap);
6406     pthread_mutex_unlock (&am->vlib_rp->mutex);
6407
6408     REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
6409                  rmp->reply_in_shmem = (uword) vector);
6410 }
6411
6412 static void vl_api_trace_profile_add_t_handler
6413 (vl_api_trace_profile_add_t *mp)
6414 {
6415     int rv = 0;
6416     vl_api_trace_profile_add_reply_t * rmp;
6417     clib_error_t *error;
6418
6419     /* Ignoring the profile id as currently a single profile
6420      * is supported */
6421     error = ip6_ioam_trace_profile_set(mp->trace_num_elt, mp->trace_type,
6422                                ntohl(mp->node_id), ntohl(mp->trace_app_data),
6423                                mp->pow_enable, mp->trace_tsp,
6424                                mp->trace_ppc);
6425     if (error) {
6426       clib_error_report(error);
6427       rv = clib_error_get_code(error);
6428     }
6429
6430     REPLY_MACRO(VL_API_TRACE_PROFILE_ADD_REPLY);
6431 }
6432
6433 static void vl_api_trace_profile_apply_t_handler
6434 (vl_api_trace_profile_apply_t *mp)
6435 {
6436     int rv = 0;
6437     vl_api_trace_profile_apply_reply_t * rmp;
6438
6439     if (mp->enable != 0) {
6440       rv = ip6_ioam_set_destination ((ip6_address_t *)(&mp->dest_ipv6),
6441                                 ntohl(mp->prefix_length),
6442                                 ntohl(mp->vrf_id),
6443                         mp->trace_op == IOAM_HBYH_ADD,
6444                         mp->trace_op == IOAM_HBYH_POP,
6445                         mp->trace_op == IOAM_HBYH_MOD);
6446     } else {
6447       //ip6_ioam_clear_destination(&ip6, mp->prefix_length, mp->vrf_id);
6448     }
6449     REPLY_MACRO(VL_API_TRACE_PROFILE_APPLY_REPLY);
6450 }
6451
6452 static void vl_api_trace_profile_del_t_handler
6453 (vl_api_trace_profile_del_t *mp)
6454 {
6455     int rv = 0;
6456     vl_api_trace_profile_del_reply_t * rmp;
6457     clib_error_t *error;
6458
6459     error = clear_ioam_rewrite_fn();
6460     if (error) {
6461       clib_error_report(error);
6462       rv = clib_error_get_code(error);
6463     }
6464
6465     REPLY_MACRO(VL_API_TRACE_PROFILE_DEL_REPLY);
6466 }
6467
6468 static void
6469 vl_api_af_packet_create_t_handler
6470 (vl_api_af_packet_create_t *mp)
6471 {
6472     vlib_main_t *vm = vlib_get_main();
6473     vl_api_af_packet_create_reply_t *rmp;
6474     int rv = 0;
6475     u8 *host_if_name = NULL;
6476     u32 sw_if_index;
6477
6478     host_if_name = format(0, "%s", mp->host_if_name);
6479     vec_add1 (host_if_name, 0);
6480
6481     rv = af_packet_create_if(vm, host_if_name,
6482                              mp->use_random_hw_addr ? 0 : mp->hw_addr, &sw_if_index);
6483
6484     vec_free(host_if_name);
6485
6486     REPLY_MACRO2(VL_API_AF_PACKET_CREATE_REPLY,
6487                  rmp->sw_if_index = clib_host_to_net_u32(sw_if_index));
6488 }
6489
6490 static void
6491 vl_api_af_packet_delete_t_handler
6492 (vl_api_af_packet_delete_t *mp)
6493 {
6494     vlib_main_t * vm = vlib_get_main();
6495     vl_api_af_packet_delete_reply_t *rmp;
6496     int rv = 0;
6497     u8 *host_if_name = NULL;
6498
6499     host_if_name = format(0, "%s", mp->host_if_name);
6500     vec_add1 (host_if_name, 0);
6501
6502     rv = af_packet_delete_if(vm, host_if_name);
6503
6504     vec_free(host_if_name);
6505
6506     REPLY_MACRO(VL_API_AF_PACKET_DELETE_REPLY);
6507 }
6508
6509 static void
6510 vl_api_policer_add_del_t_handler
6511 (vl_api_policer_add_del_t *mp)
6512 {
6513     vlib_main_t * vm = vlib_get_main();
6514     vl_api_policer_add_del_reply_t *rmp;
6515     int rv = 0;
6516     u8 *name = NULL;
6517     sse2_qos_pol_cfg_params_st cfg;
6518     clib_error_t * error;
6519     u32 policer_index;
6520
6521     name = format(0, "%s", mp->name);
6522
6523     memset (&cfg, 0, sizeof (cfg));
6524     cfg.rfc = mp->type;
6525     cfg.rnd_type = mp->round_type;
6526     cfg.rate_type = mp->rate_type;
6527     cfg.rb.kbps.cir_kbps = mp->cir;
6528     cfg.rb.kbps.eir_kbps = mp->eir;
6529     cfg.rb.kbps.cb_bytes = mp->cb;
6530     cfg.rb.kbps.eb_bytes = mp->eb;
6531     cfg.conform_action.action_type = mp->conform_action_type;
6532     cfg.conform_action.dscp = mp->conform_dscp;
6533     cfg.exceed_action.action_type = mp->exceed_action_type;
6534     cfg.exceed_action.dscp = mp->exceed_dscp;
6535     cfg.violate_action.action_type = mp->violate_action_type;
6536     cfg.violate_action.dscp = mp->violate_dscp;
6537     cfg.color_aware = mp->color_aware;
6538
6539     error = policer_add_del(vm, name, &cfg, &policer_index, mp->is_add);
6540
6541     if (error)
6542       rv = VNET_API_ERROR_UNSPECIFIED;
6543
6544     REPLY_MACRO2(VL_API_POLICER_ADD_DEL_REPLY,
6545     ({
6546         if (rv == 0 &&  mp->is_add)
6547           rmp->policer_index = ntohl(policer_index);
6548         else
6549           rmp->policer_index = ~0;
6550     }));
6551 }
6552
6553 static void
6554 send_policer_details (u8 *name,
6555                       sse2_qos_pol_cfg_params_st *config,
6556                       policer_read_response_type_st *templ,
6557                       unix_shared_memory_queue_t *q,
6558                       u32 context)
6559 {
6560     vl_api_policer_details_t * mp;
6561
6562     mp = vl_msg_api_alloc (sizeof (*mp));
6563     memset (mp, 0, sizeof (*mp));
6564     mp->_vl_msg_id = ntohs (VL_API_POLICER_DETAILS);
6565     mp->context = context;
6566     mp->cir = htonl(config->rb.kbps.cir_kbps);
6567     mp->eir = htonl(config->rb.kbps.eir_kbps);
6568     mp->cb = htonl(config->rb.kbps.cb_bytes);
6569     mp->eb = htonl(config->rb.kbps.eb_bytes);
6570     mp->rate_type = config->rate_type;
6571     mp->round_type = config->rnd_type;
6572     mp->type = config->rfc;
6573     mp->conform_action_type = config->conform_action.action_type;
6574     mp->conform_dscp = config->conform_action.dscp;
6575     mp->exceed_action_type = config->exceed_action.action_type;
6576     mp->exceed_dscp = config->exceed_action.dscp;
6577     mp->violate_action_type = config->violate_action.action_type;
6578     mp->violate_dscp = config->violate_action.dscp;
6579     mp->single_rate = templ->single_rate ? 1 : 0;
6580     mp->color_aware = templ->color_aware ? 1 : 0;
6581     mp->scale = htonl(templ->scale);
6582     mp->cir_tokens_per_period = htonl(templ->cir_tokens_per_period);
6583     mp->pir_tokens_per_period = htonl(templ->pir_tokens_per_period);
6584     mp->current_limit = htonl(templ->current_limit);
6585     mp->current_bucket = htonl(templ->current_bucket);
6586     mp->extended_limit = htonl(templ->extended_limit);
6587     mp->extended_bucket = htonl(templ->extended_bucket);
6588     mp->last_update_time = clib_host_to_net_u64(templ->last_update_time);
6589
6590     strncpy ((char *) mp->name, (char *) name, ARRAY_LEN(mp->name) - 1);
6591
6592     vl_msg_api_send_shmem (q, (u8 *)&mp);
6593 }
6594
6595 static void
6596 vl_api_policer_dump_t_handler
6597 (vl_api_policer_dump_t *mp)
6598 {
6599     unix_shared_memory_queue_t * q;
6600     vnet_policer_main_t * pm = &vnet_policer_main;
6601     hash_pair_t * hp;
6602     uword * p;
6603     u32 pool_index;
6604     u8 * match_name = 0;
6605     u8 * name;
6606     sse2_qos_pol_cfg_params_st *config;
6607     policer_read_response_type_st *templ;
6608
6609     q = vl_api_client_index_to_input_queue (mp->client_index);
6610     if (q == 0)
6611         return;
6612
6613     if (mp->match_name_valid) {
6614         match_name = format(0, "%s%c", mp->match_name, 0);
6615     }
6616
6617     if (mp->match_name_valid) {
6618         p = hash_get_mem (pm->policer_config_by_name, match_name);
6619         if (p) {
6620             pool_index = p[0];
6621             config = pool_elt_at_index (pm->configs, pool_index);
6622             templ = pool_elt_at_index (pm->policer_templates, pool_index);
6623             send_policer_details(match_name, config, templ, q, mp->context);
6624         }
6625     } else {
6626         hash_foreach_pair (hp, pm->policer_config_by_name,
6627         ({
6628             name = (u8 *) hp->key;
6629             pool_index = hp->value[0];
6630             config = pool_elt_at_index (pm->configs, pool_index);
6631             templ = pool_elt_at_index (pm->policer_templates, pool_index);
6632             send_policer_details(name, config, templ, q, mp->context);
6633         }));
6634     }
6635 }
6636
6637 static void
6638 vl_api_policer_classify_set_interface_t_handler
6639 (vl_api_policer_classify_set_interface_t * mp)
6640 {
6641     vlib_main_t *vm = vlib_get_main();
6642     vl_api_policer_classify_set_interface_reply_t * rmp;
6643     int rv;
6644     u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
6645
6646     ip4_table_index = ntohl(mp->ip4_table_index);
6647     ip6_table_index = ntohl(mp->ip6_table_index);
6648     l2_table_index = ntohl(mp->l2_table_index);
6649     sw_if_index = ntohl(mp->sw_if_index);
6650
6651     VALIDATE_SW_IF_INDEX(mp);
6652
6653     rv = vnet_set_policer_classify_intfc(vm, sw_if_index, ip4_table_index,
6654                                          ip6_table_index, l2_table_index,
6655                                          mp->is_add);
6656
6657     BAD_SW_IF_INDEX_LABEL;
6658
6659     REPLY_MACRO(VL_API_POLICER_CLASSIFY_SET_INTERFACE_REPLY);
6660 }
6661
6662 static void
6663 send_policer_classify_details (u32 sw_if_index,
6664                                u32 table_index,
6665                                unix_shared_memory_queue_t *q,
6666                                u32 context)
6667 {
6668     vl_api_policer_classify_details_t * mp;
6669
6670     mp = vl_msg_api_alloc (sizeof (*mp));
6671     memset (mp, 0, sizeof (*mp));
6672     mp->_vl_msg_id = ntohs (VL_API_POLICER_CLASSIFY_DETAILS);
6673     mp->context = context;
6674     mp->sw_if_index= htonl(sw_if_index);
6675     mp->table_index= htonl(table_index);
6676
6677     vl_msg_api_send_shmem (q, (u8 *)&mp);
6678 }
6679
6680 static void
6681 vl_api_policer_classify_dump_t_handler
6682 (vl_api_policer_classify_dump_t *mp)
6683 {
6684     unix_shared_memory_queue_t * q;
6685     policer_classify_main_t * pcm = &policer_classify_main;
6686     u32 * vec_tbl;
6687     int i;
6688
6689     q = vl_api_client_index_to_input_queue (mp->client_index);
6690     if (q == 0)
6691         return;
6692
6693     vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
6694
6695     if (vec_len(vec_tbl)) {
6696         for (i = 0; i < vec_len (vec_tbl); i++) {
6697             if (vec_elt(vec_tbl, i) == ~0)
6698                 continue;
6699
6700             send_policer_classify_details(i, vec_elt(vec_tbl, i), q,
6701                                           mp->context);
6702         }
6703     }
6704 }
6705
6706 static void
6707 vl_api_netmap_create_t_handler
6708 (vl_api_netmap_create_t *mp)
6709 {
6710     vlib_main_t *vm = vlib_get_main();
6711     vl_api_netmap_create_reply_t *rmp;
6712     int rv = 0;
6713     u8 *if_name = NULL;
6714
6715     if_name = format(0, "%s", mp->netmap_if_name);
6716     vec_add1 (if_name, 0);
6717
6718     rv = netmap_create_if(vm, if_name, mp->use_random_hw_addr ? 0 : mp->hw_addr,
6719                           mp->is_pipe, mp->is_master, 0);
6720
6721     vec_free(if_name);
6722
6723     REPLY_MACRO(VL_API_NETMAP_CREATE_REPLY);
6724 }
6725
6726 static void
6727 vl_api_netmap_delete_t_handler
6728 (vl_api_netmap_delete_t *mp)
6729 {
6730     vlib_main_t * vm = vlib_get_main();
6731     vl_api_netmap_delete_reply_t *rmp;
6732     int rv = 0;
6733     u8 *if_name = NULL;
6734
6735     if_name = format(0, "%s", mp->netmap_if_name);
6736     vec_add1 (if_name, 0);
6737
6738     rv = netmap_delete_if(vm, if_name);
6739
6740     vec_free(if_name);
6741
6742     REPLY_MACRO(VL_API_NETMAP_DELETE_REPLY);
6743 }
6744
6745 static void vl_api_mpls_gre_tunnel_details_t_handler (
6746     vl_api_mpls_gre_tunnel_details_t * mp)
6747 {
6748     clib_warning ("BUG");
6749 }
6750
6751 static void send_mpls_gre_tunnel_entry (vpe_api_main_t * am,
6752                                     unix_shared_memory_queue_t *q,
6753                                     mpls_gre_tunnel_t * gt,
6754                                     u32 index,
6755                                     u32 context)
6756 {
6757     vl_api_mpls_gre_tunnel_details_t * mp;
6758
6759     mp = vl_msg_api_alloc (sizeof (*mp));
6760     memset (mp, 0, sizeof (*mp));
6761     mp->_vl_msg_id = ntohs(VL_API_MPLS_GRE_TUNNEL_DETAILS);
6762     mp->context = context;
6763
6764     if (gt != NULL) {
6765         mp->tunnel_index    = htonl(index);
6766         mp->tunnel_src      = gt->tunnel_src.as_u32;
6767         mp->tunnel_dst      = gt->tunnel_dst.as_u32;
6768         mp->intfc_address   = gt->intfc_address.as_u32;
6769         mp->mask_width      = htonl(gt->mask_width);
6770         mp->inner_fib_index = htonl(gt->inner_fib_index);
6771         mp->outer_fib_index = htonl(gt->outer_fib_index);
6772         mp->encap_index     = htonl(gt->encap_index);
6773         mp->hw_if_index     = htonl(gt->hw_if_index);
6774         mp->l2_only         = htonl(gt->l2_only);
6775     }
6776
6777     mpls_main_t * mm = &mpls_main;
6778     mpls_encap_t * e;
6779     int i;
6780     u32 len = 0;
6781
6782     e = pool_elt_at_index (mm->encaps, gt->encap_index);
6783     len = vec_len (e->labels);
6784     mp->nlabels = htonl(len);
6785
6786     for (i = 0; i < len; i++) {
6787         mp->labels[i] = htonl(vnet_mpls_uc_get_label(
6788                 clib_host_to_net_u32(e->labels[i].label_exp_s_ttl)));
6789     }
6790
6791     vl_msg_api_send_shmem (q, (u8 *)&mp);
6792 }
6793
6794 static void
6795 vl_api_mpls_gre_tunnel_dump_t_handler (vl_api_mpls_gre_tunnel_dump_t *mp)
6796 {
6797     vpe_api_main_t * am = &vpe_api_main;
6798     unix_shared_memory_queue_t * q;
6799     vlib_main_t * vm = &vlib_global_main;
6800     mpls_main_t * mm = &mpls_main;
6801     mpls_gre_tunnel_t * gt;
6802     u32 index = ntohl(mp->tunnel_index);
6803
6804     q = vl_api_client_index_to_input_queue (mp->client_index);
6805     if (q == 0)
6806         return;
6807
6808     if (pool_elts (mm->gre_tunnels)) {
6809         if(mp->tunnel_index >= 0) {
6810             vlib_cli_output (vm, "MPLS-GRE tunnel %u", index);
6811             gt = pool_elt_at_index (mm->gre_tunnels, index);
6812             send_mpls_gre_tunnel_entry (am, q, gt, gt - mm->gre_tunnels, mp->context);
6813         } else {
6814             vlib_cli_output (vm, "MPLS-GRE tunnels");
6815             pool_foreach (gt, mm->gre_tunnels,
6816             ({
6817               send_mpls_gre_tunnel_entry (am, q, gt, gt - mm->gre_tunnels, mp->context);
6818             }));
6819         }
6820     } else {
6821         vlib_cli_output (vm, "No MPLS-GRE tunnels");
6822     }
6823 }
6824
6825 static void vl_api_mpls_eth_tunnel_details_t_handler (
6826     vl_api_mpls_eth_tunnel_details_t * mp)
6827 {
6828     clib_warning ("BUG");
6829 }
6830
6831 static void send_mpls_eth_tunnel_entry (vpe_api_main_t * am,
6832                                     unix_shared_memory_queue_t *q,
6833                                     mpls_eth_tunnel_t * et,
6834                                     u32 index,
6835                                     u32 context)
6836 {
6837     vl_api_mpls_eth_tunnel_details_t * mp;
6838
6839     mp = vl_msg_api_alloc (sizeof (*mp));
6840     memset (mp, 0, sizeof (*mp));
6841     mp->_vl_msg_id = ntohs(VL_API_MPLS_ETH_TUNNEL_DETAILS);
6842     mp->context = context;
6843
6844     if (et != NULL) {
6845         mp->tunnel_index    = htonl(index);
6846         memcpy(mp->tunnel_dst_mac, et->tunnel_dst, 6);
6847         mp->intfc_address   = et->intfc_address.as_u32;
6848         mp->tx_sw_if_index  = htonl(et->tx_sw_if_index);
6849         mp->inner_fib_index = htonl(et->inner_fib_index);
6850         mp->mask_width      = htonl(et->mask_width);
6851         mp->encap_index     = htonl(et->encap_index);
6852         mp->hw_if_index     = htonl(et->hw_if_index);
6853         mp->l2_only         = htonl(et->l2_only);
6854     }
6855
6856     mpls_main_t * mm = &mpls_main;
6857     mpls_encap_t * e;
6858     int i;
6859     u32 len = 0;
6860
6861     e = pool_elt_at_index (mm->encaps, et->encap_index);
6862     len = vec_len (e->labels);
6863     mp->nlabels = htonl(len);
6864
6865     for (i = 0; i < len; i++) {
6866         mp->labels[i] = htonl(vnet_mpls_uc_get_label(
6867                 clib_host_to_net_u32(e->labels[i].label_exp_s_ttl)));
6868     }
6869
6870     vl_msg_api_send_shmem (q, (u8 *)&mp);
6871 }
6872
6873 static void
6874 vl_api_mpls_eth_tunnel_dump_t_handler (vl_api_mpls_eth_tunnel_dump_t *mp)
6875 {
6876     vpe_api_main_t * am = &vpe_api_main;
6877     unix_shared_memory_queue_t * q;
6878     vlib_main_t * vm = &vlib_global_main;
6879     mpls_main_t * mm = &mpls_main;
6880     mpls_eth_tunnel_t * et;
6881     u32 index = ntohl(mp->tunnel_index);
6882
6883     q = vl_api_client_index_to_input_queue (mp->client_index);
6884     if (q == 0)
6885         return;
6886
6887     clib_warning("Received mpls_eth_tunnel_dump");
6888     clib_warning("Received tunnel index: %u from client %u", index, mp->client_index);
6889
6890     if (pool_elts (mm->eth_tunnels)) {
6891         if(mp->tunnel_index >= 0) {
6892             vlib_cli_output (vm, "MPLS-Ethernet tunnel %u", index);
6893             et = pool_elt_at_index (mm->eth_tunnels, index);
6894             send_mpls_eth_tunnel_entry (am, q, et, et - mm->eth_tunnels, mp->context);
6895         } else {
6896             clib_warning("MPLS-Ethernet tunnels");
6897             pool_foreach (et, mm->eth_tunnels,
6898             ({
6899                 send_mpls_eth_tunnel_entry (am, q, et, et - mm->eth_tunnels, mp->context);
6900             }));
6901         }
6902     } else {
6903         clib_warning("No MPLS-Ethernet tunnels");
6904     }
6905 }
6906
6907 static void vl_api_mpls_fib_encap_details_t_handler (
6908     vl_api_mpls_fib_encap_details_t * mp)
6909 {
6910     clib_warning ("BUG");
6911 }
6912
6913 static void send_mpls_fib_encap_details (vpe_api_main_t * am,
6914                                     unix_shared_memory_queue_t *q,
6915                                     show_mpls_fib_t *s,
6916                                     u32 context)
6917 {
6918     vl_api_mpls_fib_encap_details_t * mp;
6919
6920     mp = vl_msg_api_alloc (sizeof (*mp));
6921     memset (mp, 0, sizeof (*mp));
6922     mp->_vl_msg_id = ntohs(VL_API_MPLS_FIB_ENCAP_DETAILS);
6923     mp->context = context;
6924
6925     mp->fib_index   = htonl(s->fib_index);
6926     mp->entry_index = htonl(s->entry_index);
6927     mp->dest        = s->dest;
6928     mp->s_bit       = htonl(s->s_bit);
6929
6930     mpls_main_t * mm = &mpls_main;
6931     mpls_encap_t * e;
6932     int i;
6933     u32 len = 0;
6934
6935     e = pool_elt_at_index (mm->encaps, s->entry_index);
6936     len = vec_len (e->labels);
6937     mp->nlabels = htonl(len);
6938
6939     for (i = 0; i < len; i++) {
6940         mp->labels[i] = htonl(vnet_mpls_uc_get_label(
6941                 clib_host_to_net_u32(e->labels[i].label_exp_s_ttl)));
6942     }
6943
6944     vl_msg_api_send_shmem (q, (u8 *)&mp);
6945 }
6946
6947 static void
6948 vl_api_mpls_fib_encap_dump_t_handler (vl_api_mpls_fib_encap_dump_t *mp)
6949 {
6950     vpe_api_main_t * am = &vpe_api_main;
6951     unix_shared_memory_queue_t * q;
6952     vlib_main_t * vm = &vlib_global_main;
6953     u64 key;
6954     u32 value;
6955     show_mpls_fib_t *records = 0;
6956     show_mpls_fib_t *s;
6957     mpls_main_t * mm = &mpls_main;
6958     ip4_main_t * im = &ip4_main;
6959     ip4_fib_t * rx_fib;
6960
6961     q = vl_api_client_index_to_input_queue (mp->client_index);
6962     if (q == 0)
6963         return;
6964
6965     hash_foreach (key, value, mm->mpls_encap_by_fib_and_dest,
6966     ({
6967         vec_add2 (records, s, 1);
6968         s->fib_index = (u32)(key>>32);
6969         s->dest = (u32)(key & 0xFFFFFFFF);
6970         s->entry_index = (u32) value;
6971     }));
6972
6973     if (0 == vec_len(records)) {
6974         vlib_cli_output(vm, "MPLS encap table empty");
6975         goto out;
6976     }
6977
6978     /* sort output by dst address within fib */
6979     vec_sort_with_function(records, mpls_dest_cmp);
6980     vec_sort_with_function(records, mpls_fib_index_cmp);
6981     vlib_cli_output(vm, "MPLS encap table");
6982     vlib_cli_output(vm, "%=6s%=16s%=16s", "Table", "Dest address", "Labels");
6983     vec_foreach (s, records)
6984     {
6985         rx_fib = vec_elt_at_index(im->fibs, s->fib_index);
6986         vlib_cli_output(vm, "%=6d%=16U%=16U", rx_fib->table_id,
6987                 format_ip4_address, &s->dest, format_mpls_encap_index, mm,
6988                 s->entry_index);
6989         send_mpls_fib_encap_details (am, q, s, mp->context);
6990     }
6991
6992 out:
6993     vec_free(records);
6994 }
6995
6996 static void vl_api_mpls_fib_decap_details_t_handler (
6997     vl_api_mpls_fib_decap_details_t * mp)
6998 {
6999     clib_warning ("BUG");
7000 }
7001
7002 static void send_mpls_fib_decap_details (vpe_api_main_t * am,
7003                                     unix_shared_memory_queue_t *q,
7004                                     show_mpls_fib_t *s,
7005                                     u32 rx_table_id,
7006                                     u32 tx_table_id,
7007                                     char *swif_tag,
7008                                     u32 context)
7009 {
7010     vl_api_mpls_fib_decap_details_t * mp;
7011
7012     mp = vl_msg_api_alloc (sizeof (*mp));
7013     memset (mp, 0, sizeof (*mp));
7014     mp->_vl_msg_id = ntohs(VL_API_MPLS_FIB_DECAP_DETAILS);
7015     mp->context = context;
7016
7017     mp->fib_index   = htonl(s->fib_index);
7018     mp->entry_index = htonl(s->entry_index);
7019     mp->dest        = s->dest;
7020     mp->s_bit       = htonl(s->s_bit);
7021     mp->label       = htonl(s->label);
7022     mp->rx_table_id = htonl(rx_table_id);
7023     mp->tx_table_id = htonl(tx_table_id);
7024     strncpy ((char *) mp->swif_tag,
7025              (char *) swif_tag, ARRAY_LEN(mp->swif_tag)-1);
7026
7027     vl_msg_api_send_shmem (q, (u8 *)&mp);
7028 }
7029
7030 static void
7031 vl_api_mpls_fib_decap_dump_t_handler (vl_api_mpls_fib_decap_dump_t *mp)
7032 {
7033     vpe_api_main_t * am = &vpe_api_main;
7034     unix_shared_memory_queue_t * q;
7035     vlib_main_t * vm = &vlib_global_main;
7036     u64 key;
7037     u32 value;
7038     show_mpls_fib_t *records = 0;
7039     show_mpls_fib_t *s;
7040     mpls_main_t * mm = &mpls_main;
7041     ip4_main_t * im = &ip4_main;
7042     ip4_fib_t * rx_fib;
7043     ip4_fib_t *tx_fib;
7044     u32 tx_table_id;
7045     char *swif_tag;
7046
7047     q = vl_api_client_index_to_input_queue (mp->client_index);
7048     if (q == 0)
7049         return;
7050
7051     hash_foreach (key, value, mm->mpls_decap_by_rx_fib_and_label,
7052     ({
7053         vec_add2 (records, s, 1);
7054         s->fib_index = (u32)(key>>32);
7055         s->entry_index = (u32) value;
7056         s->label = ((u32) key)>>12;
7057         s->s_bit = (key & (1<<8)) != 0;
7058     }));
7059
7060     if (!vec_len(records)) {
7061         vlib_cli_output(vm, "MPLS decap table empty");
7062         goto out;
7063     }
7064
7065     vec_sort_with_function(records, mpls_label_cmp);
7066     vlib_cli_output(vm, "MPLS decap table");
7067     vlib_cli_output(vm, "%=10s%=15s%=6s%=6s", "RX Table", "TX Table/Intfc",
7068             "Label", "S-bit");
7069     vec_foreach (s, records)
7070     {
7071         mpls_decap_t * d;
7072         d = pool_elt_at_index(mm->decaps, s->entry_index);
7073         if (d->next_index == MPLS_INPUT_NEXT_IP4_INPUT) {
7074             tx_fib = vec_elt_at_index(im->fibs, d->tx_fib_index);
7075             tx_table_id = tx_fib->table_id;
7076             swif_tag = "     ";
7077         } else {
7078             tx_table_id = d->tx_fib_index;
7079             swif_tag = "(i)  ";
7080         }
7081         rx_fib = vec_elt_at_index(im->fibs, s->fib_index);
7082
7083         vlib_cli_output(vm, "%=10d%=10d%=5s%=6d%=6d", rx_fib->table_id,
7084                 tx_table_id, swif_tag, s->label, s->s_bit);
7085
7086         send_mpls_fib_decap_details (am, q, s, rx_fib->table_id,
7087                 tx_table_id, swif_tag, mp->context);
7088     }
7089
7090 out:
7091     vec_free(records);
7092 }
7093
7094 static void vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t *mp)
7095 {
7096     unix_shared_memory_queue_t * q;
7097
7098     q = vl_api_client_index_to_input_queue (mp->client_index);
7099     if (q == 0)
7100         return;
7101
7102     vnet_classify_main_t * cm = &vnet_classify_main;
7103     vnet_classify_table_t * t;
7104     u32 * table_ids = 0;
7105     u32 count;
7106
7107     pool_foreach (t, cm->tables,
7108     ({
7109         vec_add1 (table_ids, ntohl(t - cm->tables));
7110     }));
7111     count = vec_len(table_ids);
7112
7113     vl_api_classify_table_ids_reply_t *rmp;
7114     rmp = vl_msg_api_alloc_as_if_client(sizeof (*rmp) + count * sizeof(u32));
7115     rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
7116     rmp->context = mp->context;
7117     rmp->count = ntohl(count);
7118     clib_memcpy(rmp->ids, table_ids, count * sizeof(u32));
7119     rmp->retval = 0;
7120
7121     vl_msg_api_send_shmem (q, (u8 *)&rmp);
7122
7123     vec_free (table_ids);
7124 }
7125
7126 static void vl_api_classify_table_by_interface_t_handler (vl_api_classify_table_by_interface_t *mp)
7127 {
7128     vl_api_classify_table_by_interface_reply_t *rmp;
7129     int rv = 0;
7130
7131     u32 sw_if_index = ntohl(mp->sw_if_index);
7132     u32 * acl = 0;
7133
7134     vec_validate (acl, INPUT_ACL_N_TABLES - 1);
7135     vec_set (acl, ~0);
7136
7137     VALIDATE_SW_IF_INDEX(mp);
7138
7139     input_acl_main_t * am = &input_acl_main;
7140
7141     int if_idx;
7142     u32 type;
7143
7144     for (type = 0; type < INPUT_ACL_N_TABLES; type++)
7145     {
7146         u32 * vec_tbl = am->classify_table_index_by_sw_if_index[type];
7147                 if (vec_len(vec_tbl)) {
7148                         for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
7149                         {
7150                             if (vec_elt(vec_tbl, if_idx) == ~0 || sw_if_index != if_idx) {
7151                                     continue;
7152                             }
7153                             acl[type] = vec_elt(vec_tbl, if_idx);
7154                         }
7155                 }
7156     }
7157
7158     BAD_SW_IF_INDEX_LABEL;
7159
7160     REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
7161     ({
7162        rmp->sw_if_index = ntohl(sw_if_index);
7163        rmp->l2_table_id = ntohl(acl[INPUT_ACL_TABLE_L2]);
7164        rmp->ip4_table_id = ntohl(acl[INPUT_ACL_TABLE_IP4]);
7165        rmp->ip6_table_id = ntohl(acl[INPUT_ACL_TABLE_IP6]);
7166     }));
7167     vec_free(acl);
7168 }
7169
7170 static void vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t *mp)
7171 {
7172     unix_shared_memory_queue_t * q;
7173
7174     q = vl_api_client_index_to_input_queue (mp->client_index);
7175     if (q == 0)
7176         return;
7177
7178     vl_api_classify_table_info_reply_t *rmp = 0;
7179
7180     vnet_classify_main_t * cm = &vnet_classify_main;
7181     u32 table_id = ntohl(mp->table_id);
7182     vnet_classify_table_t * t;
7183
7184     pool_foreach (t, cm->tables,
7185     ({
7186         if (table_id == t - cm->tables) {
7187             rmp = vl_msg_api_alloc_as_if_client(sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
7188             rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
7189             rmp->context = mp->context;
7190             rmp->table_id = ntohl(table_id);
7191             rmp->nbuckets = ntohl(t->nbuckets);
7192             rmp->match_n_vectors = ntohl(t->match_n_vectors);
7193             rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
7194             rmp->active_sessions = ntohl(t->active_elements);
7195             rmp->next_table_index = ntohl(t->next_table_index);
7196             rmp->miss_next_index = ntohl(t->miss_next_index);
7197             rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
7198             clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
7199             rmp->retval = 0;
7200             break;
7201         }
7202     }));
7203
7204     if (rmp == 0) {
7205         rmp = vl_msg_api_alloc (sizeof (*rmp));
7206         rmp->_vl_msg_id = ntohs((VL_API_CLASSIFY_TABLE_INFO_REPLY));
7207         rmp->context = mp->context;
7208         rmp->retval = ntohl(VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
7209     }
7210
7211     vl_msg_api_send_shmem (q, (u8 *)&rmp);
7212 }
7213
7214 static void vl_api_classify_session_details_t_handler (vl_api_classify_session_details_t * mp)
7215 {
7216     clib_warning ("BUG");
7217 }
7218
7219 static void send_classify_session_details (unix_shared_memory_queue_t * q,
7220                                            u32 table_id,
7221                                            u32 match_length,
7222                                            vnet_classify_entry_t * e,
7223                                            u32 context)
7224 {
7225     vl_api_classify_session_details_t *rmp;
7226
7227     rmp = vl_msg_api_alloc (sizeof (*rmp));
7228     memset (rmp, 0, sizeof (*rmp));
7229     rmp->_vl_msg_id = ntohs(VL_API_CLASSIFY_SESSION_DETAILS);
7230     rmp->context = context;
7231     rmp->table_id = ntohl(table_id);
7232     rmp->hit_next_index = ntohl(e->next_index);
7233     rmp->advance = ntohl(e->advance);
7234     rmp->opaque_index = ntohl(e->opaque_index);
7235     rmp->match_length = ntohl(match_length);
7236     clib_memcpy(rmp->match, e->key, match_length);
7237
7238     vl_msg_api_send_shmem (q, (u8 *)&rmp);
7239 }
7240
7241 static void vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t *mp)
7242 {
7243     vnet_classify_main_t * cm = &vnet_classify_main;
7244     unix_shared_memory_queue_t * q;
7245
7246     u32 table_id = ntohl(mp->table_id);
7247     vnet_classify_table_t * t;
7248
7249     q = vl_api_client_index_to_input_queue (mp->client_index);
7250
7251     pool_foreach (t, cm->tables,
7252     ({
7253         if (table_id == t - cm->tables) {
7254             vnet_classify_bucket_t * b;
7255             vnet_classify_entry_t * v, * save_v;
7256             int i, j, k;
7257
7258             for (i = 0; i < t->nbuckets; i++)
7259             {
7260                 b = &t->buckets [i];
7261                 if (b->offset == 0)
7262                    continue;
7263
7264                 save_v = vnet_classify_get_entry (t, b->offset);
7265                 for (j = 0; j < (1<<b->log2_pages); j++)
7266                 {
7267                         for (k = 0; k < t->entries_per_page; k++)
7268                         {
7269                             v = vnet_classify_entry_at_index (t, save_v, j*t->entries_per_page + k);
7270                             if (vnet_classify_entry_is_free (v))
7271                                 continue;
7272
7273                                 send_classify_session_details(q, table_id,
7274                                                 t->match_n_vectors * sizeof (u32x4), v, mp->context);
7275                         }
7276                 }
7277             }
7278             break;
7279         }
7280     }));
7281 }
7282
7283 static void vl_api_ipfix_enable_t_handler (vl_api_ipfix_enable_t *mp)
7284 {
7285     vlib_main_t *vm = vlib_get_main();
7286         flow_report_main_t * frm = &flow_report_main;
7287         vl_api_ipfix_enable_reply_t *rmp;
7288         ip4_address_t collector, src;
7289         u16 collector_port = UDP_DST_PORT_ipfix;
7290     u32 path_mtu;
7291     u32 template_interval;
7292         u32 fib_id;
7293         u32 fib_index = ~0;
7294         int rv = 0;
7295
7296     memcpy(collector.data, mp->collector_address, sizeof(collector.data));
7297     collector_port = ntohs(mp->collector_port);
7298     if (collector_port == (u16)~0)
7299         collector_port = UDP_DST_PORT_ipfix;
7300     memcpy(src.data, mp->src_address, sizeof(src.data));
7301     fib_id = ntohl(mp->vrf_id);
7302
7303     ip4_main_t * im = &ip4_main;
7304     uword * p = hash_get (im->fib_index_by_table_id, fib_id);
7305     if (! p) {
7306         rv = VNET_API_ERROR_NO_SUCH_FIB;
7307         goto out;
7308     }
7309     fib_index = p[0];
7310
7311     path_mtu = ntohl(mp->path_mtu);
7312     if (path_mtu == ~0)
7313         path_mtu = 512; // RFC 7011 section 10.3.3.
7314     template_interval = ntohl(mp->template_interval);
7315     if (template_interval == ~0)
7316         template_interval = 20;
7317
7318     if (collector.as_u32 == 0) {
7319         rv = VNET_API_ERROR_INVALID_VALUE;
7320         goto out;
7321     }
7322
7323     if (src.as_u32 == 0) {
7324         rv = VNET_API_ERROR_INVALID_VALUE;
7325         goto out;
7326     }
7327
7328     if (path_mtu > 1450 /* vpp does not support fragmentation */) {
7329         rv = VNET_API_ERROR_INVALID_VALUE;
7330         goto out;
7331     }
7332
7333     if (path_mtu < 68) {
7334         rv = VNET_API_ERROR_INVALID_VALUE;
7335         goto out;
7336     }
7337
7338     /* Reset report streams if we are reconfiguring IP addresses */
7339     if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
7340         frm->src_address.as_u32 != src.as_u32 ||
7341         frm->collector_port != collector_port)
7342             vnet_flow_reports_reset(frm);
7343
7344     frm->ipfix_collector.as_u32 = collector.as_u32;
7345     frm->collector_port = collector_port;
7346     frm->src_address.as_u32 = src.as_u32;
7347     frm->fib_index = fib_index;
7348     frm->path_mtu = path_mtu;
7349     frm->template_interval = template_interval;
7350
7351     /* Turn on the flow reporting process */
7352     vlib_process_signal_event (vm, flow_report_process_node.index,
7353                                1, 0);
7354
7355 out:
7356     REPLY_MACRO(VL_API_IPFIX_ENABLE_REPLY);
7357 }
7358
7359 static void vl_api_ipfix_dump_t_handler (vl_api_ipfix_dump_t *mp)
7360 {
7361     flow_report_main_t * frm = &flow_report_main;
7362     unix_shared_memory_queue_t * q;
7363     vl_api_ipfix_details_t *rmp;
7364
7365     q = vl_api_client_index_to_input_queue (mp->client_index);
7366
7367     rmp = vl_msg_api_alloc (sizeof (*rmp));
7368     memset (rmp, 0, sizeof (*rmp));
7369     rmp->_vl_msg_id = ntohs(VL_API_IPFIX_DETAILS);
7370     rmp->context = mp->context;
7371     memcpy(rmp->collector_address, frm->ipfix_collector.data,
7372            sizeof(frm->ipfix_collector.data));
7373     rmp->collector_port = htons(frm->collector_port);
7374     memcpy(rmp->src_address, frm->src_address.data,
7375            sizeof(frm->src_address.data));
7376     rmp->fib_index = htonl(frm->fib_index);
7377     rmp->path_mtu = htonl(frm->path_mtu);
7378     rmp->template_interval = htonl(frm->template_interval);
7379
7380     vl_msg_api_send_shmem (q, (u8 *)&rmp);
7381 }
7382
7383 static void vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t *mp)
7384 {
7385     vl_api_pg_create_interface_reply_t *rmp;
7386     int rv = 0;
7387
7388     pg_main_t * pg = &pg_main;
7389     u32 sw_if_index = pg_interface_add_or_get (pg, ntohl(mp->interface_id));
7390
7391     REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
7392     ({
7393        rmp->sw_if_index = ntohl(sw_if_index);
7394     }));
7395 }
7396
7397 static void vl_api_pg_capture_t_handler (vl_api_pg_capture_t *mp)
7398 {
7399     vl_api_pg_capture_reply_t *rmp;
7400     int rv = 0;
7401
7402     vnet_main_t * vnm = vnet_get_main();
7403     vnet_interface_main_t * im = &vnm->interface_main;
7404     vnet_hw_interface_t * hi = 0;
7405
7406     u8 * intf_name = format (0, "pg%d", ntohl(mp->interface_id), 0);
7407     u32 hw_if_index = ~0;
7408     uword * p = hash_get_mem (im->hw_interface_by_name, intf_name);
7409     if (p)
7410         hw_if_index = *p;
7411     vec_free (intf_name);
7412
7413     if (hw_if_index != ~0) {
7414         pg_capture_args_t _a, *a=&_a;
7415
7416         u32 len = ntohl(mp->pcap_name_length);
7417         u8 * pcap_file_name = vec_new(u8, len);
7418         clib_memcpy(pcap_file_name, mp->pcap_file_name, len);
7419
7420         hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
7421         a->hw_if_index = hw_if_index;
7422         a->dev_instance = hi->dev_instance;
7423         a->is_enabled = mp->is_enabled;
7424         a->pcap_file_name = pcap_file_name;
7425         a->count = ntohl(mp->count);
7426
7427         clib_error_t * e = pg_capture (a);
7428         if (e) {
7429             clib_error_report(e);
7430             rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
7431         }
7432
7433         vec_free (pcap_file_name);
7434     }
7435         REPLY_MACRO(VL_API_PG_CAPTURE_REPLY);
7436 }
7437
7438 static void vl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t *mp)
7439 {
7440     vl_api_pg_enable_disable_reply_t *rmp;
7441     int rv = 0;
7442
7443     pg_main_t * pg = &pg_main;
7444     u32 stream_index = ~0;
7445
7446     int is_enable = mp->is_enabled != 0;
7447     u32 len = ntohl(mp->stream_name_length)-1;
7448
7449     if (len>0) {
7450         u8 * stream_name = vec_new(u8, len);
7451         clib_memcpy(stream_name, mp->stream_name, len);
7452         uword * p = hash_get_mem (pg->stream_index_by_name, stream_name);
7453         if (p)
7454             stream_index = *p;
7455         vec_free(stream_name);
7456     }
7457
7458     pg_enable_disable (stream_index, is_enable);
7459
7460     REPLY_MACRO(VL_API_PG_ENABLE_DISABLE_REPLY);
7461 }
7462
7463 static void vl_api_ip_source_and_port_range_check_add_del_t_handler (
7464     vl_api_ip_source_and_port_range_check_add_del_t *mp)
7465 {
7466     vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
7467     int rv = 0;
7468
7469     u8  is_ipv6 = mp->is_ipv6;
7470     u8  is_add = mp->is_add;
7471     u8  mask_length = mp->mask_length;
7472     ip4_address_t ip4_addr;
7473     //ip6_address_t ip6_addr;
7474     u16 * low_ports = 0 ;
7475     u16 * high_ports = 0 ;
7476     u16 tmp_low, tmp_high;
7477     u8 num_ranges ;
7478     int i;
7479     u32 vrf_id;
7480
7481     // Validate port range
7482     num_ranges = mp->number_of_ranges;
7483     if (num_ranges > 32) { // This is size of array in VPE.API
7484         rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
7485         goto reply;
7486     }
7487
7488     vec_reset_length (low_ports);
7489     vec_reset_length (high_ports);
7490
7491     for (i = 0; i < num_ranges; i++) {
7492         tmp_low = mp->low_ports[i];
7493         tmp_high = mp->high_ports[i];
7494         // If tmp_low <= tmp_high then only need to check tmp_low = 0
7495         // If tmp_low <= tmp_high then only need to check tmp_high > 65535
7496         if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535) {
7497             rv = VNET_API_ERROR_INVALID_VALUE;
7498             goto reply;
7499         }
7500         vec_add1 (low_ports, tmp_low );
7501         vec_add1 (high_ports, tmp_high+1 );
7502     }
7503
7504     // Validate mask_length
7505     if (mask_length < 0 ||
7506         ( is_ipv6 && mask_length > 128) ||
7507         ( !is_ipv6 && mask_length > 32)) {
7508             rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
7509             goto reply;
7510     }
7511
7512     vrf_id = ntohl (mp->vrf_id);
7513
7514     if ( vrf_id < 1 ) {
7515         rv = VNET_API_ERROR_INVALID_VALUE;
7516         goto reply;
7517     }
7518     //ip6
7519     if (is_ipv6) {
7520     /* clib_memcpy (ip6_addr.as_u8, mp->address, */
7521     /*         sizeof (ip6_addr.as_u8)); */
7522         /* rv = ip6_source_and_port_range_check_add_del (ip6_addr, */
7523         /*                                               mask_length, */
7524         /*                                               vrf_id, */
7525         /*                                               low_ports, */
7526         /*                                               high_ports, */
7527         /*                                               is_add); */
7528
7529     //ip4
7530     } else {
7531         clib_memcpy (ip4_addr.data, mp->address,
7532                      sizeof (ip4_addr));
7533         rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
7534                                                       mask_length,
7535                                                       vrf_id,
7536                                                       low_ports,
7537                                                       high_ports,
7538                                                       is_add);
7539     }
7540
7541  reply:
7542     vec_free (low_ports);
7543     vec_free (high_ports);
7544
7545     REPLY_MACRO(VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
7546 }
7547
7548 static void
7549 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
7550 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
7551 {
7552     vlib_main_t *vm = vlib_get_main();
7553     vl_api_ip_source_and_port_range_check_interface_add_del_reply_t * rmp;
7554     ip4_main_t * im = &ip4_main;
7555     int rv;
7556     u32 sw_if_index, fib_index, vrf_id;
7557     uword * p = 0;
7558
7559     vrf_id  = ntohl(mp->vrf_id);
7560
7561     p = hash_get (im->fib_index_by_table_id, vrf_id);
7562
7563     if (p == 0) {
7564         rv = VNET_API_ERROR_INVALID_VALUE;
7565         goto reply;
7566     }
7567
7568     fib_index = p[0];
7569
7570     sw_if_index = ntohl(mp->sw_if_index);
7571
7572     VALIDATE_SW_IF_INDEX(mp);
7573
7574     rv = set_ip_source_and_port_range_check (vm, fib_index, sw_if_index, mp->is_add);
7575
7576     BAD_SW_IF_INDEX_LABEL;
7577  reply:
7578
7579     REPLY_MACRO(VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
7580 }
7581
7582 #define BOUNCE_HANDLER(nn)                                              \
7583 static void vl_api_##nn##_t_handler (                                   \
7584     vl_api_##nn##_t *mp)                                                \
7585 {                                                                       \
7586     vpe_client_registration_t *reg;                                     \
7587     vpe_api_main_t * vam = &vpe_api_main;                               \
7588     unix_shared_memory_queue_t * q;                                     \
7589                                                                         \
7590     /* One registration only... */                                      \
7591     pool_foreach(reg, vam->nn##_registrations,                          \
7592     ({                                                                  \
7593         q = vl_api_client_index_to_input_queue (reg->client_index);     \
7594         if (q) {                                                        \
7595             /*                                                          \
7596              * If the queue is stuffed, turf the msg and complain       \
7597              * It's unlikely that the intended recipient is             \
7598              * alive; avoid deadlock at all costs.                      \
7599              */                                                         \
7600             if (q->cursize == q->maxsize) {                             \
7601                 clib_warning ("ERROR: receiver queue full, drop msg");  \
7602                 vl_msg_api_free (mp);                                   \
7603                 return;                                                 \
7604             }                                                           \
7605             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
7606             return;                                                     \
7607         }                                                               \
7608     }));                                                                \
7609     vl_msg_api_free (mp);                                               \
7610 }
7611
7612 /*
7613  * vpe_api_hookup
7614  * Add vpe's API message handlers to the table.
7615  * vlib has alread mapped shared memory and
7616  * added the client registration handlers.
7617  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
7618  */
7619
7620 static clib_error_t *
7621 vpe_api_hookup (vlib_main_t *vm)
7622 {
7623     api_main_t * am = &api_main;
7624
7625 #define _(N,n)                                                  \
7626     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
7627                            vl_api_##n##_t_handler,              \
7628                            vl_noop_handler,                     \
7629                            vl_api_##n##_t_endian,               \
7630                            vl_api_##n##_t_print,                \
7631                            sizeof(vl_api_##n##_t), 1);
7632     foreach_vpe_api_msg;
7633 #undef _
7634
7635     /*
7636      * Manually register the sr tunnel add del msg, so we trace
7637      * enough bytes to capture a typical segment list
7638      */
7639     vl_msg_api_set_handlers (VL_API_SR_TUNNEL_ADD_DEL,
7640                              "sr_tunnel_add_del",
7641                              vl_api_sr_tunnel_add_del_t_handler,
7642                              vl_noop_handler,
7643                              vl_api_sr_tunnel_add_del_t_endian,
7644                              vl_api_sr_tunnel_add_del_t_print,
7645                              256, 1);
7646
7647
7648     /*
7649      * Manually register the sr policy add del msg, so we trace
7650      * enough bytes to capture a typical tunnel name list
7651      */
7652     vl_msg_api_set_handlers (VL_API_SR_POLICY_ADD_DEL,
7653                              "sr_policy_add_del",
7654                              vl_api_sr_policy_add_del_t_handler,
7655                              vl_noop_handler,
7656                              vl_api_sr_policy_add_del_t_endian,
7657                              vl_api_sr_policy_add_del_t_print,
7658                              256, 1);
7659
7660     /*
7661      * Trace space for 8 MPLS encap labels, classifier mask+match
7662      */
7663     am->api_trace_cfg [VL_API_MPLS_ADD_DEL_ENCAP].size += 8 * sizeof(u32);
7664     am->api_trace_cfg [VL_API_CLASSIFY_ADD_DEL_TABLE].size
7665         += 5 * sizeof (u32x4);
7666     am->api_trace_cfg [VL_API_CLASSIFY_ADD_DEL_SESSION].size
7667         += 5 * sizeof (u32x4);
7668     am->api_trace_cfg [VL_API_VXLAN_ADD_DEL_TUNNEL].size
7669         += 16 * sizeof (u32);
7670
7671     /*
7672      * Thread-safe API messages
7673      */
7674     am->is_mp_safe [VL_API_IP_ADD_DEL_ROUTE] = 1;
7675     am->is_mp_safe [VL_API_GET_NODE_GRAPH] = 1;
7676
7677     return 0;
7678 }
7679
7680 VLIB_API_INIT_FUNCTION(vpe_api_hookup);
7681
7682 static clib_error_t *
7683 vpe_api_init (vlib_main_t *vm)
7684 {
7685     vpe_api_main_t *am = &vpe_api_main;
7686
7687     am->vlib_main = vm;
7688     am->vnet_main = vnet_get_main();
7689     am->interface_events_registration_hash = hash_create (0, sizeof (uword));
7690     am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
7691     am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
7692     am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
7693     am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
7694     am->oam_events_registration_hash = hash_create (0, sizeof (uword));
7695
7696     vl_api_init (vm);
7697     vl_set_memory_region_name ("/vpe-api");
7698     vl_enable_disable_memory_api (vm, 1 /* enable it */);
7699
7700     return 0;
7701 }
7702
7703 VLIB_INIT_FUNCTION(vpe_api_init);
7704
7705
7706 static clib_error_t *
7707 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
7708 {
7709   u8 * chroot_path;
7710   int uid, gid, rv;
7711   char *s, buf[128];
7712   struct passwd _pw, *pw;
7713   struct group _grp, *grp;
7714   clib_error_t *e;
7715
7716   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
7717     {
7718       if (unformat (input, "prefix %s", &chroot_path))
7719         {
7720           vec_add1 (chroot_path, 0);
7721           vl_set_memory_root_path ((char *)chroot_path);
7722         }
7723       else if (unformat (input, "uid %d", &uid))
7724         vl_set_memory_uid (uid);
7725       else if (unformat (input, "gid %d", &gid))
7726         vl_set_memory_gid (gid);
7727       else if (unformat (input, "uid %s", &s))
7728         {
7729           /* lookup the username */
7730           pw = NULL;
7731           rv = getpwnam_r(s, &_pw, buf, sizeof(buf), &pw);
7732           if (rv < 0)
7733             {
7734               e = clib_error_return_code(0, rv,
7735                        CLIB_ERROR_ERRNO_VALID | CLIB_ERROR_FATAL,
7736                        "cannot fetch username %s", s);
7737               vec_free (s);
7738               return e;
7739             }
7740           if (pw == NULL)
7741             {
7742               e = clib_error_return_fatal(0, "username %s does not exist", s);
7743               vec_free (s);
7744               return e;
7745             }
7746           vec_free (s);
7747           vl_set_memory_uid (pw->pw_uid);
7748         }
7749       else if (unformat (input, "gid %s", &s))
7750         {
7751           /* lookup the group name */
7752           grp = NULL;
7753           rv = getgrnam_r(s, &_grp, buf, sizeof(buf), &grp);
7754           if (rv != 0)
7755             {
7756               e = clib_error_return_code(0, rv,
7757                        CLIB_ERROR_ERRNO_VALID | CLIB_ERROR_FATAL,
7758                        "cannot fetch group %s", s);
7759               vec_free (s);
7760               return e;
7761             }
7762           if (grp == NULL)
7763             {
7764               e = clib_error_return_fatal(0, "group %s does not exist", s);
7765               vec_free (s);
7766               return e;
7767             }
7768           vec_free (s);
7769           vl_set_memory_gid (grp->gr_gid);
7770         }
7771       else
7772         return clib_error_return (0, "unknown input `%U'",
7773                                   format_unformat_error, input);
7774     }
7775   return 0;
7776 }
7777 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
7778
7779 void * get_unformat_vnet_sw_interface (void)
7780 {
7781     return (void *) &unformat_vnet_sw_interface;
7782 }
7783
7784 #undef vl_api_version
7785 #define vl_api_version(n,v) static u32 vpe_api_version = v;
7786 #include <vpp-api/vpe.api.h>
7787 #undef vl_api_version
7788
7789 int vl_msg_api_version_check (vl_api_memclnt_create_t * mp)
7790 {
7791     if (clib_host_to_net_u32(mp->api_versions[0]) != vpe_api_version) {
7792         clib_warning ("vpe API mismatch: 0x%08x instead of 0x%08x",
7793                       clib_host_to_net_u32 (mp->api_versions[0]),
7794                       vpe_api_version);
7795         return -1;
7796     }
7797     return 0;
7798 }
7799
7800 static u8 * format_arp_event (u8 * s, va_list * args)
7801 {
7802     vl_api_ip4_arp_event_t * event = va_arg (*args, vl_api_ip4_arp_event_t *);
7803
7804     s = format (s, "pid %d: %U", event->pid,
7805                 format_ip4_address, &event->address);
7806     return s;
7807 }
7808
7809 static clib_error_t *
7810 show_ip4_arp_events_fn (vlib_main_t * vm,
7811                         unformat_input_t * input,
7812                         vlib_cli_command_t * cmd)
7813 {
7814     vpe_api_main_t * am = &vpe_api_main;
7815     vl_api_ip4_arp_event_t * event;
7816
7817     if (pool_elts (am->arp_events) == 0) {
7818         vlib_cli_output (vm, "No active arp event registrations");
7819         return 0;
7820     }
7821
7822     pool_foreach (event, am->arp_events,
7823     ({
7824         vlib_cli_output (vm, "%U", format_arp_event, event);
7825     }));
7826
7827     return 0;
7828 }
7829
7830 VLIB_CLI_COMMAND (show_ip4_arp_events, static) = {
7831   .path = "show arp event registrations",
7832   .function = show_ip4_arp_events_fn,
7833   .short_help = "Show arp event registrations",
7834 };