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