18063acc7d5b8bac5ab1cfbc262d3cb19d0270ea
[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   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5666   locator_set_t *ls = NULL;
5667   gid_address_t *gid = NULL;
5668   u8 *mac = 0;
5669   ip_prefix_t *ip_prefix = NULL;
5670
5671   ls = pool_elt_at_index (lcm->locator_set_pool, mapit->locator_set_index);
5672
5673   switch (filter)
5674     {
5675     case 0:
5676       break;
5677     case 1:
5678       if (!ls->local)
5679         {
5680           return;
5681         }
5682       break;
5683     case 2:
5684       if (ls->local)
5685         {
5686           return;
5687         }
5688       break;
5689     default:
5690       clib_warning ("Filter error, unknown filter: %d\n", filter);
5691       return;
5692     }
5693
5694   gid = &mapit->eid;
5695   ip_prefix = &gid_address_ippref (gid);
5696   mac = gid_address_mac (gid);
5697
5698   rmp = vl_msg_api_alloc (sizeof (*rmp));
5699   memset (rmp, 0, sizeof (*rmp));
5700   rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_DETAILS);
5701   rmp->locator_set_index = mapit->locator_set_index;
5702   rmp->is_local = ls->local;
5703   rmp->ttl = mapit->ttl;
5704   rmp->authoritative = mapit->authoritative;
5705
5706   switch (gid_address_type (gid))
5707     {
5708     case GID_ADDR_IP_PREFIX:
5709       rmp->eid_prefix_len = ip_prefix_len (ip_prefix);
5710       if (ip_prefix_version (ip_prefix) == IP4)
5711         {
5712           rmp->eid_type = 0;    /* ipv4 type */
5713           clib_memcpy (rmp->eid, &ip_prefix_v4 (ip_prefix),
5714                        sizeof (ip_prefix_v4 (ip_prefix)));
5715         }
5716       else
5717         {
5718           rmp->eid_type = 1;    /* ipv6 type */
5719           clib_memcpy (rmp->eid, &ip_prefix_v6 (ip_prefix),
5720                        sizeof (ip_prefix_v6 (ip_prefix)));
5721         }
5722       break;
5723     case GID_ADDR_MAC:
5724       rmp->eid_type = 2;        /* l2 mac type */
5725       clib_memcpy (rmp->eid, mac, 6);
5726       break;
5727     default:
5728       ASSERT (0);
5729     }
5730   rmp->context = context;
5731   rmp->vni = clib_host_to_net_u32 (gid_address_vni (gid));
5732   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5733 }
5734
5735 static void
5736 vl_api_lisp_eid_table_dump_t_handler (vl_api_lisp_eid_table_dump_t * mp)
5737 {
5738   u32 mi;
5739   unix_shared_memory_queue_t *q = NULL;
5740   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5741   mapping_t *mapit = NULL;
5742   gid_address_t _eid, *eid = &_eid;
5743
5744   q = vl_api_client_index_to_input_queue (mp->client_index);
5745   if (q == 0)
5746     {
5747       return;
5748     }
5749
5750   if (mp->eid_set)
5751     {
5752       memset (eid, 0, sizeof (*eid));
5753
5754       unformat_lisp_eid_api (eid, mp->eid_type,
5755                              clib_net_to_host_u32 (mp->vni), mp->eid,
5756                              mp->prefix_length);
5757
5758       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
5759       if ((u32) ~ 0 == mi)
5760         return;
5761
5762       mapit = pool_elt_at_index (lcm->mapping_pool, mi);
5763       send_lisp_eid_table_details (mapit, q, mp->context, mp->filter);
5764     }
5765   else
5766     {
5767       /* *INDENT-OFF* */
5768       pool_foreach (mapit, lcm->mapping_pool,
5769       ({
5770         send_lisp_eid_table_details(mapit, q, mp->context,
5771                                     mp->filter);
5772       }));
5773       /* *INDENT-ON* */
5774     }
5775 }
5776
5777 static void
5778 send_lisp_gpe_tunnel_details (lisp_gpe_tunnel_t * tunnel,
5779                               unix_shared_memory_queue_t * q, u32 context)
5780 {
5781   vl_api_lisp_gpe_tunnel_details_t *rmp;
5782   lisp_gpe_main_t *lgm = &lisp_gpe_main;
5783
5784   rmp = vl_msg_api_alloc (sizeof (*rmp));
5785   memset (rmp, 0, sizeof (*rmp));
5786   rmp->_vl_msg_id = ntohs (VL_API_LISP_GPE_TUNNEL_DETAILS);
5787
5788   rmp->tunnels = tunnel - lgm->tunnels;
5789
5790   rmp->is_ipv6 = ip_addr_version (&tunnel->src) == IP6 ? 1 : 0;
5791   ip_address_copy_addr (rmp->source_ip, &tunnel->src);
5792   ip_address_copy_addr (rmp->destination_ip, &tunnel->dst);
5793
5794   rmp->encap_fib_id = htonl (tunnel->encap_fib_index);
5795   rmp->decap_fib_id = htonl (tunnel->decap_fib_index);
5796   rmp->dcap_next = htonl (tunnel->decap_next_index);
5797   rmp->lisp_ver = tunnel->ver_res;
5798   rmp->next_protocol = tunnel->next_protocol;
5799   rmp->flags = tunnel->flags;
5800   rmp->ver_res = tunnel->ver_res;
5801   rmp->res = tunnel->res;
5802   rmp->iid = htonl (tunnel->vni);
5803   rmp->context = context;
5804
5805   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5806 }
5807
5808 static void
5809 vl_api_lisp_gpe_tunnel_dump_t_handler (vl_api_lisp_gpe_tunnel_dump_t * mp)
5810 {
5811   unix_shared_memory_queue_t *q = NULL;
5812   lisp_gpe_main_t *lgm = &lisp_gpe_main;
5813   lisp_gpe_tunnel_t *tunnel = NULL;
5814
5815   if (pool_elts (lgm->tunnels) == 0)
5816     {
5817       return;
5818     }
5819
5820   q = vl_api_client_index_to_input_queue (mp->client_index);
5821   if (q == 0)
5822     {
5823       return;
5824     }
5825
5826   /* *INDENT-OFF* */
5827   pool_foreach(tunnel, lgm->tunnels,
5828   ({
5829     send_lisp_gpe_tunnel_details(tunnel, q, mp->context);
5830   }));
5831   /* *INDENT-ON* */
5832 }
5833
5834 static void
5835 send_lisp_map_resolver_details (ip_address_t * ip,
5836                                 unix_shared_memory_queue_t * q, u32 context)
5837 {
5838   vl_api_lisp_map_resolver_details_t *rmp = NULL;
5839
5840   rmp = vl_msg_api_alloc (sizeof (*rmp));
5841   memset (rmp, 0, sizeof (*rmp));
5842   rmp->_vl_msg_id = ntohs (VL_API_LISP_MAP_RESOLVER_DETAILS);
5843
5844   switch (ip_addr_version (ip))
5845     {
5846     case IP4:
5847       rmp->is_ipv6 = 0;
5848       clib_memcpy (rmp->ip_address, &ip_addr_v4 (ip),
5849                    sizeof (ip_addr_v4 (ip)));
5850       break;
5851
5852     case IP6:
5853       rmp->is_ipv6 = 1;
5854       clib_memcpy (rmp->ip_address, &ip_addr_v6 (ip),
5855                    sizeof (ip_addr_v6 (ip)));
5856       break;
5857
5858     default:
5859       ASSERT (0);
5860     }
5861   rmp->context = context;
5862
5863   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5864 }
5865
5866 static void
5867 vl_api_lisp_map_resolver_dump_t_handler (vl_api_lisp_map_resolver_dump_t * mp)
5868 {
5869   unix_shared_memory_queue_t *q = NULL;
5870   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5871   ip_address_t *ip = NULL;
5872
5873   q = vl_api_client_index_to_input_queue (mp->client_index);
5874   if (q == 0)
5875     {
5876       return;
5877     }
5878
5879   vec_foreach (ip, lcm->map_resolvers)
5880   {
5881     send_lisp_map_resolver_details (ip, q, mp->context);
5882   }
5883
5884 }
5885
5886 static void
5887 send_eid_table_map_pair (hash_pair_t * p,
5888                          unix_shared_memory_queue_t * q, u32 context)
5889 {
5890   vl_api_lisp_eid_table_map_details_t *rmp = NULL;
5891
5892   rmp = vl_msg_api_alloc (sizeof (*rmp));
5893   memset (rmp, 0, sizeof (*rmp));
5894   rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_MAP_DETAILS);
5895
5896   rmp->vni = clib_host_to_net_u32 (p->key);
5897   rmp->vrf = clib_host_to_net_u32 (p->value[0]);
5898   rmp->context = context;
5899   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5900 }
5901
5902 static void
5903 vl_api_lisp_eid_table_map_dump_t_handler (vl_api_lisp_eid_table_map_dump_t *
5904                                           mp)
5905 {
5906   unix_shared_memory_queue_t *q = NULL;
5907   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5908   hash_pair_t *p;
5909
5910   q = vl_api_client_index_to_input_queue (mp->client_index);
5911   if (q == 0)
5912     {
5913       return;
5914     }
5915   /* *INDENT-OFF* */
5916   hash_foreach_pair (p, lcm->table_id_by_vni,
5917   ({
5918     send_eid_table_map_pair (p, q, mp->context);
5919   }));
5920   /* *INDENT-ON* */
5921 }
5922
5923 static void
5924 vl_api_show_lisp_status_t_handler (vl_api_show_lisp_status_t * mp)
5925 {
5926   unix_shared_memory_queue_t *q = NULL;
5927   vl_api_show_lisp_status_reply_t *rmp = NULL;
5928   int rv = 0;
5929
5930   q = vl_api_client_index_to_input_queue (mp->client_index);
5931   if (q == 0)
5932     {
5933       return;
5934     }
5935
5936   /* *INDENT-OFF* */
5937   REPLY_MACRO2(VL_API_SHOW_LISP_STATUS_REPLY,
5938   ({
5939     rmp->gpe_status = vnet_lisp_gpe_enable_disable_status ();
5940     rmp->feature_status = vnet_lisp_enable_disable_status ();
5941   }));
5942   /* *INDENT-ON* */
5943 }
5944
5945 static void
5946   vl_api_lisp_get_map_request_itr_rlocs_t_handler
5947   (vl_api_lisp_get_map_request_itr_rlocs_t * mp)
5948 {
5949   unix_shared_memory_queue_t *q = NULL;
5950   vl_api_lisp_get_map_request_itr_rlocs_reply_t *rmp = NULL;
5951   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5952   locator_set_t *loc_set = 0;
5953   u8 *tmp_str = 0;
5954   int rv = 0;
5955
5956   q = vl_api_client_index_to_input_queue (mp->client_index);
5957   if (q == 0)
5958     {
5959       return;
5960     }
5961
5962   if (~0 == lcm->mreq_itr_rlocs)
5963     {
5964       tmp_str = format (0, " ");
5965     }
5966   else
5967     {
5968       loc_set =
5969         pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
5970       tmp_str = format (0, "%s", loc_set->name);
5971     }
5972
5973   /* *INDENT-OFF* */
5974   REPLY_MACRO2(VL_API_LISP_GET_MAP_REQUEST_ITR_RLOCS_REPLY,
5975   ({
5976     strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
5977             ARRAY_LEN(rmp->locator_set_name) - 1);
5978   }));
5979   /* *INDENT-ON* */
5980
5981   vec_free (tmp_str);
5982 }
5983
5984 static void
5985 vl_api_show_lisp_pitr_t_handler (vl_api_show_lisp_pitr_t * mp)
5986 {
5987   unix_shared_memory_queue_t *q = NULL;
5988   vl_api_show_lisp_pitr_reply_t *rmp = NULL;
5989   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5990   mapping_t *m;
5991   locator_set_t *ls = 0;
5992   u8 *tmp_str = 0;
5993   int rv = 0;
5994
5995   q = vl_api_client_index_to_input_queue (mp->client_index);
5996   if (q == 0)
5997     {
5998       return;
5999     }
6000
6001   if (!lcm->lisp_pitr)
6002     {
6003       tmp_str = format (0, "N/A");
6004     }
6005   else
6006     {
6007       m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
6008       if (~0 != m->locator_set_index)
6009         {
6010           ls =
6011             pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
6012           tmp_str = format (0, "%s", ls->name);
6013         }
6014       else
6015         {
6016           tmp_str = format (0, "N/A");
6017         }
6018     }
6019   vec_add1 (tmp_str, 0);
6020
6021   /* *INDENT-OFF* */
6022   REPLY_MACRO2(VL_API_SHOW_LISP_PITR_REPLY,
6023   ({
6024     rmp->status = lcm->lisp_pitr;
6025     strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
6026             ARRAY_LEN(rmp->locator_set_name) - 1);
6027   }));
6028   /* *INDENT-ON* */
6029 }
6030
6031 static void
6032 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
6033                                           mp)
6034 {
6035   vl_api_interface_name_renumber_reply_t *rmp;
6036   int rv = 0;
6037
6038   VALIDATE_SW_IF_INDEX (mp);
6039
6040   rv = vnet_interface_name_renumber
6041     (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
6042
6043   BAD_SW_IF_INDEX_LABEL;
6044
6045   REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
6046 }
6047
6048 static int
6049 arp_change_data_callback (u32 pool_index, u8 * new_mac,
6050                           u32 sw_if_index, u32 address)
6051 {
6052   vpe_api_main_t *am = &vpe_api_main;
6053   vlib_main_t *vm = am->vlib_main;
6054   vl_api_ip4_arp_event_t *event;
6055   static f64 arp_event_last_time;
6056   f64 now = vlib_time_now (vm);
6057
6058   if (pool_is_free_index (am->arp_events, pool_index))
6059     return 1;
6060
6061   event = pool_elt_at_index (am->arp_events, pool_index);
6062   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
6063     {
6064       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
6065     }
6066   else
6067     {                           /* same mac */
6068       if ((sw_if_index == event->sw_if_index) && ((address == 0) ||
6069                                                   /* for BD case, also check IP address with 10 sec timeout */
6070                                                   ((address == event->address)
6071                                                    &&
6072                                                    ((now -
6073                                                      arp_event_last_time) <
6074                                                     10.0))))
6075         return 1;
6076     }
6077
6078   arp_event_last_time = now;
6079   event->sw_if_index = sw_if_index;
6080   if (address)
6081     event->address = address;
6082   return 0;
6083 }
6084
6085 static int
6086 arp_change_delete_callback (u32 pool_index, u8 * notused)
6087 {
6088   vpe_api_main_t *am = &vpe_api_main;
6089
6090   if (pool_is_free_index (am->arp_events, pool_index))
6091     return 1;
6092
6093   pool_put_index (am->arp_events, pool_index);
6094   return 0;
6095 }
6096
6097 static void
6098 vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
6099 {
6100   vpe_api_main_t *am = &vpe_api_main;
6101   vnet_main_t *vnm = vnet_get_main ();
6102   vl_api_want_ip4_arp_events_reply_t *rmp;
6103   vl_api_ip4_arp_event_t *event;
6104   int rv;
6105
6106   if (mp->enable_disable)
6107     {
6108       pool_get (am->arp_events, event);
6109       memset (event, 0, sizeof (*event));
6110
6111       event->_vl_msg_id = ntohs (VL_API_IP4_ARP_EVENT);
6112       event->client_index = mp->client_index;
6113       event->context = mp->context;
6114       event->address = mp->address;
6115       event->pid = mp->pid;
6116
6117       rv = vnet_add_del_ip4_arp_change_event
6118         (vnm, arp_change_data_callback,
6119          mp->pid, &mp->address /* addr, in net byte order */ ,
6120          vpe_resolver_process_node.index,
6121          IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
6122     }
6123   else
6124     {
6125       rv = vnet_add_del_ip4_arp_change_event
6126         (vnm, arp_change_delete_callback,
6127          mp->pid, &mp->address /* addr, in net byte order */ ,
6128          vpe_resolver_process_node.index,
6129          IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
6130     }
6131   REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
6132 }
6133
6134 static void vl_api_input_acl_set_interface_t_handler
6135   (vl_api_input_acl_set_interface_t * mp)
6136 {
6137   vlib_main_t *vm = vlib_get_main ();
6138   vl_api_input_acl_set_interface_reply_t *rmp;
6139   int rv;
6140   u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
6141
6142   ip4_table_index = ntohl (mp->ip4_table_index);
6143   ip6_table_index = ntohl (mp->ip6_table_index);
6144   l2_table_index = ntohl (mp->l2_table_index);
6145   sw_if_index = ntohl (mp->sw_if_index);
6146
6147   VALIDATE_SW_IF_INDEX (mp);
6148
6149   rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
6150                                  ip6_table_index, l2_table_index, mp->is_add);
6151
6152   BAD_SW_IF_INDEX_LABEL;
6153
6154   REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
6155 }
6156
6157 static void vl_api_ipsec_spd_add_del_t_handler
6158   (vl_api_ipsec_spd_add_del_t * mp)
6159 {
6160 #if IPSEC == 0
6161   clib_warning ("unimplemented");
6162 #else
6163
6164   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6165   vl_api_ipsec_spd_add_del_reply_t *rmp;
6166   int rv;
6167
6168 #if DPDK > 0
6169   rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
6170 #else
6171   rv = VNET_API_ERROR_UNIMPLEMENTED;
6172 #endif
6173
6174   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
6175 #endif
6176 }
6177
6178 static void vl_api_ipsec_interface_add_del_spd_t_handler
6179   (vl_api_ipsec_interface_add_del_spd_t * mp)
6180 {
6181   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6182   vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
6183   int rv;
6184   u32 sw_if_index __attribute__ ((unused));
6185   u32 spd_id __attribute__ ((unused));
6186
6187   sw_if_index = ntohl (mp->sw_if_index);
6188   spd_id = ntohl (mp->spd_id);
6189
6190   VALIDATE_SW_IF_INDEX (mp);
6191
6192 #if IPSEC > 0
6193   rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
6194 #else
6195   rv = VNET_API_ERROR_UNIMPLEMENTED;
6196 #endif
6197
6198   BAD_SW_IF_INDEX_LABEL;
6199
6200   REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
6201 }
6202
6203 static void vl_api_ipsec_spd_add_del_entry_t_handler
6204   (vl_api_ipsec_spd_add_del_entry_t * mp)
6205 {
6206   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6207   vl_api_ipsec_spd_add_del_entry_reply_t *rmp;
6208   int rv;
6209
6210 #if IPSEC > 0
6211   ipsec_policy_t p;
6212
6213   memset (&p, 0, sizeof (p));
6214
6215   p.id = ntohl (mp->spd_id);
6216   p.priority = ntohl (mp->priority);
6217   p.is_outbound = mp->is_outbound;
6218   p.is_ipv6 = mp->is_ipv6;
6219
6220   if (mp->is_ipv6 || mp->is_ip_any)
6221     {
6222       clib_memcpy (&p.raddr.start, mp->remote_address_start, 16);
6223       clib_memcpy (&p.raddr.stop, mp->remote_address_stop, 16);
6224       clib_memcpy (&p.laddr.start, mp->local_address_start, 16);
6225       clib_memcpy (&p.laddr.stop, mp->local_address_stop, 16);
6226     }
6227   else
6228     {
6229       clib_memcpy (&p.raddr.start.ip4.data, mp->remote_address_start, 4);
6230       clib_memcpy (&p.raddr.stop.ip4.data, mp->remote_address_stop, 4);
6231       clib_memcpy (&p.laddr.start.ip4.data, mp->local_address_start, 4);
6232       clib_memcpy (&p.laddr.stop.ip4.data, mp->local_address_stop, 4);
6233     }
6234   p.protocol = mp->protocol;
6235   p.rport.start = ntohs (mp->remote_port_start);
6236   p.rport.stop = ntohs (mp->remote_port_stop);
6237   p.lport.start = ntohs (mp->local_port_start);
6238   p.lport.stop = ntohs (mp->local_port_stop);
6239   /* policy action resolve unsupported */
6240   if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE)
6241     {
6242       clib_warning ("unsupported action: 'resolve'");
6243       rv = VNET_API_ERROR_UNIMPLEMENTED;
6244       goto out;
6245     }
6246   p.policy = mp->policy;
6247   p.sa_id = ntohl (mp->sa_id);
6248
6249   rv = ipsec_add_del_policy (vm, &p, mp->is_add);
6250   if (rv)
6251     goto out;
6252
6253   if (mp->is_ip_any)
6254     {
6255       p.is_ipv6 = 1;
6256       rv = ipsec_add_del_policy (vm, &p, mp->is_add);
6257     }
6258 #else
6259   rv = VNET_API_ERROR_UNIMPLEMENTED;
6260   goto out;
6261 #endif
6262
6263 out:
6264   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
6265 }
6266
6267 static void vl_api_ipsec_sad_add_del_entry_t_handler
6268   (vl_api_ipsec_sad_add_del_entry_t * mp)
6269 {
6270   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6271   vl_api_ipsec_sad_add_del_entry_reply_t *rmp;
6272   int rv;
6273 #if IPSEC > 0
6274   ipsec_sa_t sa;
6275
6276   memset (&sa, 0, sizeof (sa));
6277
6278   sa.id = ntohl (mp->sad_id);
6279   sa.spi = ntohl (mp->spi);
6280   /* security protocol AH unsupported */
6281   if (mp->protocol == IPSEC_PROTOCOL_AH)
6282     {
6283       clib_warning ("unsupported security protocol 'AH'");
6284       rv = VNET_API_ERROR_UNIMPLEMENTED;
6285       goto out;
6286     }
6287   sa.protocol = mp->protocol;
6288   /* check for unsupported crypto-alg */
6289   if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
6290       mp->crypto_algorithm > IPSEC_CRYPTO_ALG_AES_CBC_256)
6291     {
6292       clib_warning ("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
6293                     mp->crypto_algorithm);
6294       rv = VNET_API_ERROR_UNIMPLEMENTED;
6295       goto out;
6296     }
6297   sa.crypto_alg = mp->crypto_algorithm;
6298   sa.crypto_key_len = mp->crypto_key_length;
6299   clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
6300   /* check for unsupported integ-alg */
6301   if (mp->integrity_algorithm < IPSEC_INTEG_ALG_SHA1_96 ||
6302       mp->integrity_algorithm > IPSEC_INTEG_ALG_SHA_512_256)
6303     {
6304       clib_warning ("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
6305                     mp->integrity_algorithm);
6306       rv = VNET_API_ERROR_UNIMPLEMENTED;
6307       goto out;
6308     }
6309   sa.integ_alg = mp->integrity_algorithm;
6310   sa.integ_key_len = mp->integrity_key_length;
6311   clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
6312   sa.use_esn = mp->use_extended_sequence_number;
6313   sa.is_tunnel = mp->is_tunnel;
6314   sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
6315   if (sa.is_tunnel_ip6)
6316     {
6317       clib_memcpy (&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
6318       clib_memcpy (&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
6319     }
6320   else
6321     {
6322       clib_memcpy (&sa.tunnel_src_addr.ip4.data, mp->tunnel_src_address, 4);
6323       clib_memcpy (&sa.tunnel_dst_addr.ip4.data, mp->tunnel_dst_address, 4);
6324     }
6325
6326   rv = ipsec_add_del_sa (vm, &sa, mp->is_add);
6327 #else
6328   rv = VNET_API_ERROR_UNIMPLEMENTED;
6329   goto out;
6330 #endif
6331
6332 out:
6333   REPLY_MACRO (VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
6334 }
6335
6336 static void
6337 vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
6338 {
6339   vl_api_ikev2_profile_add_del_reply_t *rmp;
6340   int rv = 0;
6341
6342 #if IPSEC > 0
6343   vlib_main_t *vm = vlib_get_main ();
6344   clib_error_t *error;
6345   u8 *tmp = format (0, "%s", mp->name);
6346   error = ikev2_add_del_profile (vm, tmp, mp->is_add);
6347   vec_free (tmp);
6348   if (error)
6349     rv = VNET_API_ERROR_UNSPECIFIED;
6350 #else
6351   rv = VNET_API_ERROR_UNIMPLEMENTED;
6352 #endif
6353
6354   REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
6355 }
6356
6357 static void
6358   vl_api_ikev2_profile_set_auth_t_handler
6359   (vl_api_ikev2_profile_set_auth_t * mp)
6360 {
6361   vl_api_ikev2_profile_set_auth_reply_t *rmp;
6362   int rv = 0;
6363
6364 #if IPSEC > 0
6365   vlib_main_t *vm = vlib_get_main ();
6366   clib_error_t *error;
6367   u8 *tmp = format (0, "%s", mp->name);
6368   u8 *data = vec_new (u8, mp->data_len);
6369   clib_memcpy (data, mp->data, mp->data_len);
6370   error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
6371   vec_free (tmp);
6372   vec_free (data);
6373   if (error)
6374     rv = VNET_API_ERROR_UNSPECIFIED;
6375 #else
6376   rv = VNET_API_ERROR_UNIMPLEMENTED;
6377 #endif
6378
6379   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
6380 }
6381
6382 static void
6383 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
6384 {
6385   vl_api_ikev2_profile_add_del_reply_t *rmp;
6386   int rv = 0;
6387
6388 #if IPSEC > 0
6389   vlib_main_t *vm = vlib_get_main ();
6390   clib_error_t *error;
6391   u8 *tmp = format (0, "%s", mp->name);
6392   u8 *data = vec_new (u8, mp->data_len);
6393   clib_memcpy (data, mp->data, mp->data_len);
6394   error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
6395   vec_free (tmp);
6396   vec_free (data);
6397   if (error)
6398     rv = VNET_API_ERROR_UNSPECIFIED;
6399 #else
6400   rv = VNET_API_ERROR_UNIMPLEMENTED;
6401 #endif
6402
6403   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
6404 }
6405
6406 static void
6407 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
6408 {
6409   vl_api_ikev2_profile_set_ts_reply_t *rmp;
6410   int rv = 0;
6411
6412 #if IPSEC > 0
6413   vlib_main_t *vm = vlib_get_main ();
6414   clib_error_t *error;
6415   u8 *tmp = format (0, "%s", mp->name);
6416   error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
6417                                 mp->end_port, (ip4_address_t) mp->start_addr,
6418                                 (ip4_address_t) mp->end_addr, mp->is_local);
6419   vec_free (tmp);
6420   if (error)
6421     rv = VNET_API_ERROR_UNSPECIFIED;
6422 #else
6423   rv = VNET_API_ERROR_UNIMPLEMENTED;
6424 #endif
6425
6426   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
6427 }
6428
6429 static void
6430 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
6431 {
6432   vl_api_ikev2_profile_set_ts_reply_t *rmp;
6433   int rv = 0;
6434
6435 #if IPSEC > 0
6436   vlib_main_t *vm = vlib_get_main ();
6437   clib_error_t *error;
6438
6439   error = ikev2_set_local_key (vm, mp->key_file);
6440   if (error)
6441     rv = VNET_API_ERROR_UNSPECIFIED;
6442 #else
6443   rv = VNET_API_ERROR_UNIMPLEMENTED;
6444 #endif
6445
6446   REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
6447 }
6448
6449 static void
6450 vl_api_map_add_domain_t_handler (vl_api_map_add_domain_t * mp)
6451 {
6452   vl_api_map_add_domain_reply_t *rmp;
6453   int rv = 0;
6454   u32 index;
6455   u8 flags = mp->is_translation ? MAP_DOMAIN_TRANSLATION : 0;
6456   rv =
6457     map_create_domain ((ip4_address_t *) & mp->ip4_prefix, mp->ip4_prefix_len,
6458                        (ip6_address_t *) & mp->ip6_prefix, mp->ip6_prefix_len,
6459                        (ip6_address_t *) & mp->ip6_src,
6460                        mp->ip6_src_prefix_len, mp->ea_bits_len,
6461                        mp->psid_offset, mp->psid_length, &index,
6462                        ntohs (mp->mtu), flags);
6463
6464   /* *INDENT-OFF* */
6465   REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
6466   ({
6467     rmp->index = ntohl(index);
6468   }));
6469   /* *INDENT-ON* */
6470 }
6471
6472 static void
6473 vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp)
6474 {
6475   vl_api_map_del_domain_reply_t *rmp;
6476   int rv = 0;
6477
6478   rv = map_delete_domain (ntohl (mp->index));
6479
6480   REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY);
6481 }
6482
6483 static void
6484 vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp)
6485 {
6486   vl_api_map_del_domain_reply_t *rmp;
6487   int rv = 0;
6488
6489   rv =
6490     map_add_del_psid (ntohl (mp->index), ntohs (mp->psid),
6491                       (ip6_address_t *) mp->ip6_dst, mp->is_add);
6492
6493   REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY);
6494 }
6495
6496 static void
6497 vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
6498 {
6499   vl_api_map_domain_details_t *rmp;
6500   map_main_t *mm = &map_main;
6501   map_domain_t *d;
6502   unix_shared_memory_queue_t *q;
6503
6504   if (pool_elts (mm->domains) == 0)
6505     return;
6506
6507   q = vl_api_client_index_to_input_queue (mp->client_index);
6508   if (q == 0)
6509     {
6510       return;
6511     }
6512
6513   /* *INDENT-OFF* */
6514   pool_foreach(d, mm->domains,
6515   ({
6516     /* Make sure every field is initiated (or don't skip the memset()) */
6517     rmp = vl_msg_api_alloc (sizeof (*rmp));
6518     rmp->_vl_msg_id = ntohs(VL_API_MAP_DOMAIN_DETAILS);
6519     rmp->domain_index = htonl(d - mm->domains);
6520     rmp->ea_bits_len = d->ea_bits_len;
6521     rmp->psid_offset = d->psid_offset;
6522     rmp->psid_length = d->psid_length;
6523     clib_memcpy(rmp->ip4_prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix));
6524     rmp->ip4_prefix_len = d->ip4_prefix_len;
6525     clib_memcpy(rmp->ip6_prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix));
6526     rmp->ip6_prefix_len = d->ip6_prefix_len;
6527     clib_memcpy(rmp->ip6_src, &d->ip6_src, sizeof(rmp->ip6_src));
6528     rmp->ip6_src_len = d->ip6_src_len;
6529     rmp->mtu = htons(d->mtu);
6530     rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION);
6531     rmp->context = mp->context;
6532
6533     vl_msg_api_send_shmem (q, (u8 *)&rmp);
6534   }));
6535   /* *INDENT-ON* */
6536 }
6537
6538 static void
6539 vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
6540 {
6541   unix_shared_memory_queue_t *q;
6542   u16 i;
6543   ip6_address_t dst;
6544   vl_api_map_rule_details_t *rmp;
6545   map_main_t *mm = &map_main;
6546   u32 domain_index = ntohl (mp->domain_index);
6547   map_domain_t *d;
6548
6549   if (pool_elts (mm->domains) == 0)
6550     return;
6551
6552   d = pool_elt_at_index (mm->domains, domain_index);
6553   if (!d || !d->rules)
6554     {
6555       return;
6556     }
6557
6558   q = vl_api_client_index_to_input_queue (mp->client_index);
6559   if (q == 0)
6560     {
6561       return;
6562     }
6563
6564   for (i = 0; i < (0x1 << d->psid_length); i++)
6565     {
6566       dst = d->rules[i];
6567       if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
6568         {
6569           continue;
6570         }
6571       rmp = vl_msg_api_alloc (sizeof (*rmp));
6572       memset (rmp, 0, sizeof (*rmp));
6573       rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS);
6574       rmp->psid = htons (i);
6575       clib_memcpy (rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
6576       rmp->context = mp->context;
6577       vl_msg_api_send_shmem (q, (u8 *) & rmp);
6578     }
6579 }
6580
6581 static void
6582 vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
6583 {
6584   vl_api_map_summary_stats_reply_t *rmp;
6585   vlib_combined_counter_main_t *cm;
6586   vlib_counter_t v;
6587   int i, which;
6588   u64 total_pkts[VLIB_N_RX_TX];
6589   u64 total_bytes[VLIB_N_RX_TX];
6590   map_main_t *mm = &map_main;
6591   unix_shared_memory_queue_t *q =
6592     vl_api_client_index_to_input_queue (mp->client_index);
6593
6594   if (!q)
6595     return;
6596
6597   rmp = vl_msg_api_alloc (sizeof (*rmp));
6598   rmp->_vl_msg_id = ntohs (VL_API_MAP_SUMMARY_STATS_REPLY);
6599   rmp->context = mp->context;
6600   rmp->retval = 0;
6601
6602   memset (total_pkts, 0, sizeof (total_pkts));
6603   memset (total_bytes, 0, sizeof (total_bytes));
6604
6605   map_domain_counter_lock (mm);
6606   vec_foreach (cm, mm->domain_counters)
6607   {
6608     which = cm - mm->domain_counters;
6609
6610     for (i = 0; i < vec_len (cm->maxi); i++)
6611       {
6612         vlib_get_combined_counter (cm, i, &v);
6613         total_pkts[which] += v.packets;
6614         total_bytes[which] += v.bytes;
6615       }
6616   }
6617
6618   map_domain_counter_unlock (mm);
6619
6620   /* Note: in network byte order! */
6621   rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] =
6622     clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]);
6623   rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] =
6624     clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]);
6625   rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] =
6626     clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]);
6627   rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] =
6628     clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]);
6629   rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains));
6630   rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
6631   rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] =
6632     clib_host_to_net_u64 (map_error_counter_get
6633                           (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
6634   rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] =
6635     clib_host_to_net_u64 (map_error_counter_get
6636                           (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
6637
6638   vl_msg_api_send_shmem (q, (u8 *) & rmp);
6639 }
6640
6641 static void
6642 vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
6643 {
6644   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6645   vl_api_ipsec_sa_set_key_reply_t *rmp;
6646   int rv;
6647 #if IPSEC > 0
6648   ipsec_sa_t sa;
6649   sa.id = ntohl (mp->sa_id);
6650   sa.crypto_key_len = mp->crypto_key_length;
6651   clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
6652   sa.integ_key_len = mp->integrity_key_length;
6653   clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
6654
6655   rv = ipsec_set_sa_key (vm, &sa);
6656 #else
6657   rv = VNET_API_ERROR_UNIMPLEMENTED;
6658 #endif
6659
6660   REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
6661 }
6662
6663 static void vl_api_cop_interface_enable_disable_t_handler
6664   (vl_api_cop_interface_enable_disable_t * mp)
6665 {
6666   vl_api_cop_interface_enable_disable_reply_t *rmp;
6667   int rv;
6668   u32 sw_if_index = ntohl (mp->sw_if_index);
6669   int enable_disable;
6670
6671   VALIDATE_SW_IF_INDEX (mp);
6672
6673   enable_disable = (int) mp->enable_disable;
6674
6675   rv = cop_interface_enable_disable (sw_if_index, enable_disable);
6676
6677   BAD_SW_IF_INDEX_LABEL;
6678
6679   REPLY_MACRO (VL_API_COP_INTERFACE_ENABLE_DISABLE_REPLY);
6680 }
6681
6682 static void vl_api_cop_whitelist_enable_disable_t_handler
6683   (vl_api_cop_whitelist_enable_disable_t * mp)
6684 {
6685   vl_api_cop_whitelist_enable_disable_reply_t *rmp;
6686   cop_whitelist_enable_disable_args_t _a, *a = &_a;
6687   u32 sw_if_index = ntohl (mp->sw_if_index);
6688   int rv;
6689
6690   VALIDATE_SW_IF_INDEX (mp);
6691
6692   a->sw_if_index = sw_if_index;
6693   a->ip4 = mp->ip4;
6694   a->ip6 = mp->ip6;
6695   a->default_cop = mp->default_cop;
6696   a->fib_id = ntohl (mp->fib_id);
6697
6698   rv = cop_whitelist_enable_disable (a);
6699
6700   BAD_SW_IF_INDEX_LABEL;
6701
6702   REPLY_MACRO (VL_API_COP_WHITELIST_ENABLE_DISABLE_REPLY);
6703 }
6704
6705 static void
6706 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
6707 {
6708   int rv = 0;
6709   u8 *vector = 0;
6710   api_main_t *am = &api_main;
6711   vlib_main_t *vm = vlib_get_main ();
6712   void *oldheap;
6713   vl_api_get_node_graph_reply_t *rmp;
6714
6715   pthread_mutex_lock (&am->vlib_rp->mutex);
6716   oldheap = svm_push_data_heap (am->vlib_rp);
6717
6718   /*
6719    * Keep the number of memcpy ops to a minimum (e.g. 1).
6720    */
6721   vec_validate (vector, 16384);
6722   vec_reset_length (vector);
6723
6724   /* $$$$ FIXME */
6725   vector = vlib_node_serialize (&vm->node_main, vector,
6726                                 (u32) ~ 0 /* all threads */ ,
6727                                 1 /* include nexts */ ,
6728                                 1 /* include stats */ );
6729
6730   svm_pop_heap (oldheap);
6731   pthread_mutex_unlock (&am->vlib_rp->mutex);
6732
6733   /* *INDENT-OFF* */
6734   REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
6735   ({
6736     rmp->reply_in_shmem = (uword) vector;
6737   }));
6738   /* *INDENT-ON* */
6739 }
6740
6741 static void vl_api_trace_profile_add_t_handler
6742   (vl_api_trace_profile_add_t * mp)
6743 {
6744   int rv = 0;
6745   vl_api_trace_profile_add_reply_t *rmp;
6746   clib_error_t *error;
6747
6748   /* Ignoring the profile id as currently a single profile
6749    * is supported */
6750   error = ip6_ioam_trace_profile_set (mp->trace_num_elt, mp->trace_type,
6751                                       ntohl (mp->node_id),
6752                                       ntohl (mp->trace_app_data),
6753                                       mp->pow_enable, mp->trace_tsp,
6754                                       mp->trace_ppc);
6755   if (error)
6756     {
6757       clib_error_report (error);
6758       rv = clib_error_get_code (error);
6759     }
6760
6761   REPLY_MACRO (VL_API_TRACE_PROFILE_ADD_REPLY);
6762 }
6763
6764 static void vl_api_trace_profile_apply_t_handler
6765   (vl_api_trace_profile_apply_t * mp)
6766 {
6767   int rv = 0;
6768   vl_api_trace_profile_apply_reply_t *rmp;
6769
6770   if (mp->enable != 0)
6771     {
6772       rv = ip6_ioam_set_destination ((ip6_address_t *) (&mp->dest_ipv6),
6773                                      ntohl (mp->prefix_length),
6774                                      ntohl (mp->vrf_id),
6775                                      mp->trace_op == IOAM_HBYH_ADD,
6776                                      mp->trace_op == IOAM_HBYH_POP,
6777                                      mp->trace_op == IOAM_HBYH_MOD);
6778     }
6779   else
6780     {
6781       //ip6_ioam_clear_destination(&ip6, mp->prefix_length, mp->vrf_id);
6782     }
6783   REPLY_MACRO (VL_API_TRACE_PROFILE_APPLY_REPLY);
6784 }
6785
6786 static void vl_api_trace_profile_del_t_handler
6787   (vl_api_trace_profile_del_t * mp)
6788 {
6789   int rv = 0;
6790   vl_api_trace_profile_del_reply_t *rmp;
6791   clib_error_t *error;
6792
6793   error = clear_ioam_rewrite_fn ();
6794   if (error)
6795     {
6796       clib_error_report (error);
6797       rv = clib_error_get_code (error);
6798     }
6799
6800   REPLY_MACRO (VL_API_TRACE_PROFILE_DEL_REPLY);
6801 }
6802
6803 static void
6804 vl_api_af_packet_create_t_handler (vl_api_af_packet_create_t * mp)
6805 {
6806   vlib_main_t *vm = vlib_get_main ();
6807   vl_api_af_packet_create_reply_t *rmp;
6808   int rv = 0;
6809   u8 *host_if_name = NULL;
6810   u32 sw_if_index;
6811
6812   host_if_name = format (0, "%s", mp->host_if_name);
6813   vec_add1 (host_if_name, 0);
6814
6815   rv = af_packet_create_if (vm, host_if_name,
6816                             mp->use_random_hw_addr ? 0 : mp->hw_addr,
6817                             &sw_if_index);
6818
6819   vec_free (host_if_name);
6820
6821   /* *INDENT-OFF* */
6822   REPLY_MACRO2(VL_API_AF_PACKET_CREATE_REPLY,
6823   ({
6824     rmp->sw_if_index = clib_host_to_net_u32(sw_if_index);
6825   }));
6826   /* *INDENT-ON* */
6827 }
6828
6829 static void
6830 vl_api_af_packet_delete_t_handler (vl_api_af_packet_delete_t * mp)
6831 {
6832   vlib_main_t *vm = vlib_get_main ();
6833   vl_api_af_packet_delete_reply_t *rmp;
6834   int rv = 0;
6835   u8 *host_if_name = NULL;
6836
6837   host_if_name = format (0, "%s", mp->host_if_name);
6838   vec_add1 (host_if_name, 0);
6839
6840   rv = af_packet_delete_if (vm, host_if_name);
6841
6842   vec_free (host_if_name);
6843
6844   REPLY_MACRO (VL_API_AF_PACKET_DELETE_REPLY);
6845 }
6846
6847 static void
6848 vl_api_policer_add_del_t_handler (vl_api_policer_add_del_t * mp)
6849 {
6850   vlib_main_t *vm = vlib_get_main ();
6851   vl_api_policer_add_del_reply_t *rmp;
6852   int rv = 0;
6853   u8 *name = NULL;
6854   sse2_qos_pol_cfg_params_st cfg;
6855   clib_error_t *error;
6856   u32 policer_index;
6857
6858   name = format (0, "%s", mp->name);
6859
6860   memset (&cfg, 0, sizeof (cfg));
6861   cfg.rfc = mp->type;
6862   cfg.rnd_type = mp->round_type;
6863   cfg.rate_type = mp->rate_type;
6864   cfg.rb.kbps.cir_kbps = mp->cir;
6865   cfg.rb.kbps.eir_kbps = mp->eir;
6866   cfg.rb.kbps.cb_bytes = mp->cb;
6867   cfg.rb.kbps.eb_bytes = mp->eb;
6868   cfg.conform_action.action_type = mp->conform_action_type;
6869   cfg.conform_action.dscp = mp->conform_dscp;
6870   cfg.exceed_action.action_type = mp->exceed_action_type;
6871   cfg.exceed_action.dscp = mp->exceed_dscp;
6872   cfg.violate_action.action_type = mp->violate_action_type;
6873   cfg.violate_action.dscp = mp->violate_dscp;
6874   cfg.color_aware = mp->color_aware;
6875
6876   error = policer_add_del (vm, name, &cfg, &policer_index, mp->is_add);
6877
6878   if (error)
6879     rv = VNET_API_ERROR_UNSPECIFIED;
6880
6881   /* *INDENT-OFF* */
6882   REPLY_MACRO2(VL_API_POLICER_ADD_DEL_REPLY,
6883   ({
6884     if (rv == 0 &&  mp->is_add)
6885       rmp->policer_index = ntohl(policer_index);
6886     else
6887       rmp->policer_index = ~0;
6888   }));
6889   /* *INDENT-ON* */
6890 }
6891
6892 static void
6893 send_policer_details (u8 * name,
6894                       sse2_qos_pol_cfg_params_st * config,
6895                       policer_read_response_type_st * templ,
6896                       unix_shared_memory_queue_t * q, u32 context)
6897 {
6898   vl_api_policer_details_t *mp;
6899
6900   mp = vl_msg_api_alloc (sizeof (*mp));
6901   memset (mp, 0, sizeof (*mp));
6902   mp->_vl_msg_id = ntohs (VL_API_POLICER_DETAILS);
6903   mp->context = context;
6904   mp->cir = htonl (config->rb.kbps.cir_kbps);
6905   mp->eir = htonl (config->rb.kbps.eir_kbps);
6906   mp->cb = htonl (config->rb.kbps.cb_bytes);
6907   mp->eb = htonl (config->rb.kbps.eb_bytes);
6908   mp->rate_type = config->rate_type;
6909   mp->round_type = config->rnd_type;
6910   mp->type = config->rfc;
6911   mp->conform_action_type = config->conform_action.action_type;
6912   mp->conform_dscp = config->conform_action.dscp;
6913   mp->exceed_action_type = config->exceed_action.action_type;
6914   mp->exceed_dscp = config->exceed_action.dscp;
6915   mp->violate_action_type = config->violate_action.action_type;
6916   mp->violate_dscp = config->violate_action.dscp;
6917   mp->single_rate = templ->single_rate ? 1 : 0;
6918   mp->color_aware = templ->color_aware ? 1 : 0;
6919   mp->scale = htonl (templ->scale);
6920   mp->cir_tokens_per_period = htonl (templ->cir_tokens_per_period);
6921   mp->pir_tokens_per_period = htonl (templ->pir_tokens_per_period);
6922   mp->current_limit = htonl (templ->current_limit);
6923   mp->current_bucket = htonl (templ->current_bucket);
6924   mp->extended_limit = htonl (templ->extended_limit);
6925   mp->extended_bucket = htonl (templ->extended_bucket);
6926   mp->last_update_time = clib_host_to_net_u64 (templ->last_update_time);
6927
6928   strncpy ((char *) mp->name, (char *) name, ARRAY_LEN (mp->name) - 1);
6929
6930   vl_msg_api_send_shmem (q, (u8 *) & mp);
6931 }
6932
6933 static void
6934 vl_api_policer_dump_t_handler (vl_api_policer_dump_t * mp)
6935 {
6936   unix_shared_memory_queue_t *q;
6937   vnet_policer_main_t *pm = &vnet_policer_main;
6938   hash_pair_t *hp;
6939   uword *p;
6940   u32 pool_index;
6941   u8 *match_name = 0;
6942   u8 *name;
6943   sse2_qos_pol_cfg_params_st *config;
6944   policer_read_response_type_st *templ;
6945
6946   q = vl_api_client_index_to_input_queue (mp->client_index);
6947   if (q == 0)
6948     return;
6949
6950   if (mp->match_name_valid)
6951     {
6952       match_name = format (0, "%s%c", mp->match_name, 0);
6953     }
6954
6955   if (mp->match_name_valid)
6956     {
6957       p = hash_get_mem (pm->policer_config_by_name, match_name);
6958       if (p)
6959         {
6960           pool_index = p[0];
6961           config = pool_elt_at_index (pm->configs, pool_index);
6962           templ = pool_elt_at_index (pm->policer_templates, pool_index);
6963           send_policer_details (match_name, config, templ, q, mp->context);
6964         }
6965     }
6966   else
6967     {
6968       /* *INDENT-OFF* */
6969       hash_foreach_pair (hp, pm->policer_config_by_name,
6970       ({
6971         name = (u8 *) hp->key;
6972         pool_index = hp->value[0];
6973         config = pool_elt_at_index (pm->configs, pool_index);
6974         templ = pool_elt_at_index (pm->policer_templates, pool_index);
6975         send_policer_details(name, config, templ, q, mp->context);
6976       }));
6977       /* *INDENT-ON* */
6978     }
6979 }
6980
6981 static void
6982   vl_api_policer_classify_set_interface_t_handler
6983   (vl_api_policer_classify_set_interface_t * mp)
6984 {
6985   vlib_main_t *vm = vlib_get_main ();
6986   vl_api_policer_classify_set_interface_reply_t *rmp;
6987   int rv;
6988   u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
6989
6990   ip4_table_index = ntohl (mp->ip4_table_index);
6991   ip6_table_index = ntohl (mp->ip6_table_index);
6992   l2_table_index = ntohl (mp->l2_table_index);
6993   sw_if_index = ntohl (mp->sw_if_index);
6994
6995   VALIDATE_SW_IF_INDEX (mp);
6996
6997   rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index,
6998                                         ip6_table_index, l2_table_index,
6999                                         mp->is_add);
7000
7001   BAD_SW_IF_INDEX_LABEL;
7002
7003   REPLY_MACRO (VL_API_POLICER_CLASSIFY_SET_INTERFACE_REPLY);
7004 }
7005
7006 static void
7007 send_policer_classify_details (u32 sw_if_index,
7008                                u32 table_index,
7009                                unix_shared_memory_queue_t * q, u32 context)
7010 {
7011   vl_api_policer_classify_details_t *mp;
7012
7013   mp = vl_msg_api_alloc (sizeof (*mp));
7014   memset (mp, 0, sizeof (*mp));
7015   mp->_vl_msg_id = ntohs (VL_API_POLICER_CLASSIFY_DETAILS);
7016   mp->context = context;
7017   mp->sw_if_index = htonl (sw_if_index);
7018   mp->table_index = htonl (table_index);
7019
7020   vl_msg_api_send_shmem (q, (u8 *) & mp);
7021 }
7022
7023 static void
7024 vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
7025 {
7026   unix_shared_memory_queue_t *q;
7027   policer_classify_main_t *pcm = &policer_classify_main;
7028   u32 *vec_tbl;
7029   int i;
7030
7031   q = vl_api_client_index_to_input_queue (mp->client_index);
7032   if (q == 0)
7033     return;
7034
7035   vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
7036
7037   if (vec_len (vec_tbl))
7038     {
7039       for (i = 0; i < vec_len (vec_tbl); i++)
7040         {
7041           if (vec_elt (vec_tbl, i) == ~0)
7042             continue;
7043
7044           send_policer_classify_details (i, vec_elt (vec_tbl, i), q,
7045                                          mp->context);
7046         }
7047     }
7048 }
7049
7050 static void
7051 vl_api_netmap_create_t_handler (vl_api_netmap_create_t * mp)
7052 {
7053   vlib_main_t *vm = vlib_get_main ();
7054   vl_api_netmap_create_reply_t *rmp;
7055   int rv = 0;
7056   u8 *if_name = NULL;
7057
7058   if_name = format (0, "%s", mp->netmap_if_name);
7059   vec_add1 (if_name, 0);
7060
7061   rv =
7062     netmap_create_if (vm, if_name, mp->use_random_hw_addr ? 0 : mp->hw_addr,
7063                       mp->is_pipe, mp->is_master, 0);
7064
7065   vec_free (if_name);
7066
7067   REPLY_MACRO (VL_API_NETMAP_CREATE_REPLY);
7068 }
7069
7070 static void
7071 vl_api_netmap_delete_t_handler (vl_api_netmap_delete_t * mp)
7072 {
7073   vlib_main_t *vm = vlib_get_main ();
7074   vl_api_netmap_delete_reply_t *rmp;
7075   int rv = 0;
7076   u8 *if_name = NULL;
7077
7078   if_name = format (0, "%s", mp->netmap_if_name);
7079   vec_add1 (if_name, 0);
7080
7081   rv = netmap_delete_if (vm, if_name);
7082
7083   vec_free (if_name);
7084
7085   REPLY_MACRO (VL_API_NETMAP_DELETE_REPLY);
7086 }
7087
7088 static void
7089 vl_api_mpls_gre_tunnel_details_t_handler (vl_api_mpls_gre_tunnel_details_t *
7090                                           mp)
7091 {
7092   clib_warning ("BUG");
7093 }
7094
7095 static void
7096 send_mpls_gre_tunnel_entry (vpe_api_main_t * am,
7097                             unix_shared_memory_queue_t * q,
7098                             mpls_gre_tunnel_t * gt, u32 index, u32 context)
7099 {
7100   vl_api_mpls_gre_tunnel_details_t *mp;
7101
7102   mp = vl_msg_api_alloc (sizeof (*mp));
7103   memset (mp, 0, sizeof (*mp));
7104   mp->_vl_msg_id = ntohs (VL_API_MPLS_GRE_TUNNEL_DETAILS);
7105   mp->context = context;
7106
7107   if (gt != NULL)
7108     {
7109       mp->tunnel_index = htonl (index);
7110       mp->tunnel_src = gt->tunnel_src.as_u32;
7111       mp->tunnel_dst = gt->tunnel_dst.as_u32;
7112       mp->intfc_address = gt->intfc_address.as_u32;
7113       mp->mask_width = htonl (gt->mask_width);
7114       mp->inner_fib_index = htonl (gt->inner_fib_index);
7115       mp->outer_fib_index = htonl (gt->outer_fib_index);
7116       mp->encap_index = htonl (gt->encap_index);
7117       mp->hw_if_index = htonl (gt->hw_if_index);
7118       mp->l2_only = htonl (gt->l2_only);
7119     }
7120
7121   mpls_main_t *mm = &mpls_main;
7122   mpls_encap_t *e;
7123   int i;
7124   u32 len = 0;
7125
7126   e = pool_elt_at_index (mm->encaps, gt->encap_index);
7127   len = vec_len (e->labels);
7128   mp->nlabels = htonl (len);
7129
7130   for (i = 0; i < len; i++)
7131     {
7132       mp->labels[i] =
7133         htonl (vnet_mpls_uc_get_label
7134                (clib_host_to_net_u32 (e->labels[i].label_exp_s_ttl)));
7135     }
7136
7137   vl_msg_api_send_shmem (q, (u8 *) & mp);
7138 }
7139
7140 static void
7141 vl_api_mpls_gre_tunnel_dump_t_handler (vl_api_mpls_gre_tunnel_dump_t * mp)
7142 {
7143   vpe_api_main_t *am = &vpe_api_main;
7144   unix_shared_memory_queue_t *q;
7145   vlib_main_t *vm = &vlib_global_main;
7146   mpls_main_t *mm = &mpls_main;
7147   mpls_gre_tunnel_t *gt;
7148   u32 index = ntohl (mp->tunnel_index);
7149
7150   q = vl_api_client_index_to_input_queue (mp->client_index);
7151   if (q == 0)
7152     return;
7153
7154   if (pool_elts (mm->gre_tunnels))
7155     {
7156       if (mp->tunnel_index >= 0)
7157         {
7158           vlib_cli_output (vm, "MPLS-GRE tunnel %u", index);
7159           gt = pool_elt_at_index (mm->gre_tunnels, index);
7160           send_mpls_gre_tunnel_entry (am, q, gt, gt - mm->gre_tunnels,
7161                                       mp->context);
7162         }
7163       else
7164         {
7165           vlib_cli_output (vm, "MPLS-GRE tunnels");
7166           /* *INDENT-OFF* */
7167           pool_foreach (gt, mm->gre_tunnels,
7168           ({
7169             send_mpls_gre_tunnel_entry (am, q, gt, gt - mm->gre_tunnels,
7170                                         mp->context);
7171           }));
7172           /* *INDENT-ON* */
7173         }
7174     }
7175   else
7176     {
7177       vlib_cli_output (vm, "No MPLS-GRE tunnels");
7178     }
7179 }
7180
7181 static void
7182 vl_api_mpls_eth_tunnel_details_t_handler (vl_api_mpls_eth_tunnel_details_t *
7183                                           mp)
7184 {
7185   clib_warning ("BUG");
7186 }
7187
7188 static void
7189 send_mpls_eth_tunnel_entry (vpe_api_main_t * am,
7190                             unix_shared_memory_queue_t * q,
7191                             mpls_eth_tunnel_t * et, u32 index, u32 context)
7192 {
7193   vl_api_mpls_eth_tunnel_details_t *mp;
7194
7195   mp = vl_msg_api_alloc (sizeof (*mp));
7196   memset (mp, 0, sizeof (*mp));
7197   mp->_vl_msg_id = ntohs (VL_API_MPLS_ETH_TUNNEL_DETAILS);
7198   mp->context = context;
7199
7200   if (et != NULL)
7201     {
7202       mp->tunnel_index = htonl (index);
7203       memcpy (mp->tunnel_dst_mac, et->tunnel_dst, 6);
7204       mp->intfc_address = et->intfc_address.as_u32;
7205       mp->tx_sw_if_index = htonl (et->tx_sw_if_index);
7206       mp->inner_fib_index = htonl (et->inner_fib_index);
7207       mp->mask_width = htonl (et->mask_width);
7208       mp->encap_index = htonl (et->encap_index);
7209       mp->hw_if_index = htonl (et->hw_if_index);
7210       mp->l2_only = htonl (et->l2_only);
7211     }
7212
7213   mpls_main_t *mm = &mpls_main;
7214   mpls_encap_t *e;
7215   int i;
7216   u32 len = 0;
7217
7218   e = pool_elt_at_index (mm->encaps, et->encap_index);
7219   len = vec_len (e->labels);
7220   mp->nlabels = htonl (len);
7221
7222   for (i = 0; i < len; i++)
7223     {
7224       mp->labels[i] =
7225         htonl (vnet_mpls_uc_get_label
7226                (clib_host_to_net_u32 (e->labels[i].label_exp_s_ttl)));
7227     }
7228
7229   vl_msg_api_send_shmem (q, (u8 *) & mp);
7230 }
7231
7232 static void
7233 vl_api_mpls_eth_tunnel_dump_t_handler (vl_api_mpls_eth_tunnel_dump_t * mp)
7234 {
7235   vpe_api_main_t *am = &vpe_api_main;
7236   unix_shared_memory_queue_t *q;
7237   vlib_main_t *vm = &vlib_global_main;
7238   mpls_main_t *mm = &mpls_main;
7239   mpls_eth_tunnel_t *et;
7240   u32 index = ntohl (mp->tunnel_index);
7241
7242   q = vl_api_client_index_to_input_queue (mp->client_index);
7243   if (q == 0)
7244     return;
7245
7246   clib_warning ("Received mpls_eth_tunnel_dump");
7247   clib_warning ("Received tunnel index: %u from client %u", index,
7248                 mp->client_index);
7249
7250   if (pool_elts (mm->eth_tunnels))
7251     {
7252       if (mp->tunnel_index >= 0)
7253         {
7254           vlib_cli_output (vm, "MPLS-Ethernet tunnel %u", index);
7255           et = pool_elt_at_index (mm->eth_tunnels, index);
7256           send_mpls_eth_tunnel_entry (am, q, et, et - mm->eth_tunnels,
7257                                       mp->context);
7258         }
7259       else
7260         {
7261           clib_warning ("MPLS-Ethernet tunnels");
7262           /* *INDENT-OFF* */
7263           pool_foreach (et, mm->eth_tunnels,
7264           ({
7265             send_mpls_eth_tunnel_entry (am, q, et, et - mm->eth_tunnels,
7266                                         mp->context);
7267           }));
7268           /* *INDENT-ON* */
7269         }
7270     }
7271   else
7272     {
7273       clib_warning ("No MPLS-Ethernet tunnels");
7274     }
7275 }
7276
7277 static void
7278 vl_api_mpls_fib_encap_details_t_handler (vl_api_mpls_fib_encap_details_t * mp)
7279 {
7280   clib_warning ("BUG");
7281 }
7282
7283 static void
7284 send_mpls_fib_encap_details (vpe_api_main_t * am,
7285                              unix_shared_memory_queue_t * q,
7286                              show_mpls_fib_t * s, u32 context)
7287 {
7288   vl_api_mpls_fib_encap_details_t *mp;
7289
7290   mp = vl_msg_api_alloc (sizeof (*mp));
7291   memset (mp, 0, sizeof (*mp));
7292   mp->_vl_msg_id = ntohs (VL_API_MPLS_FIB_ENCAP_DETAILS);
7293   mp->context = context;
7294
7295   mp->fib_index = htonl (s->fib_index);
7296   mp->entry_index = htonl (s->entry_index);
7297   mp->dest = s->dest;
7298   mp->s_bit = htonl (s->s_bit);
7299
7300   mpls_main_t *mm = &mpls_main;
7301   mpls_encap_t *e;
7302   int i;
7303   u32 len = 0;
7304
7305   e = pool_elt_at_index (mm->encaps, s->entry_index);
7306   len = vec_len (e->labels);
7307   mp->nlabels = htonl (len);
7308
7309   for (i = 0; i < len; i++)
7310     {
7311       mp->labels[i] =
7312         htonl (vnet_mpls_uc_get_label
7313                (clib_host_to_net_u32 (e->labels[i].label_exp_s_ttl)));
7314     }
7315
7316   vl_msg_api_send_shmem (q, (u8 *) & mp);
7317 }
7318
7319 static void
7320 vl_api_mpls_fib_encap_dump_t_handler (vl_api_mpls_fib_encap_dump_t * mp)
7321 {
7322   vpe_api_main_t *am = &vpe_api_main;
7323   unix_shared_memory_queue_t *q;
7324   vlib_main_t *vm = &vlib_global_main;
7325   u64 key;
7326   u32 value;
7327   show_mpls_fib_t *records = 0;
7328   show_mpls_fib_t *s;
7329   mpls_main_t *mm = &mpls_main;
7330   ip4_main_t *im = &ip4_main;
7331   ip4_fib_t *rx_fib;
7332
7333   q = vl_api_client_index_to_input_queue (mp->client_index);
7334   if (q == 0)
7335     return;
7336
7337   /* *INDENT-OFF* */
7338   hash_foreach (key, value, mm->mpls_encap_by_fib_and_dest,
7339   ({
7340     vec_add2 (records, s, 1);
7341     s->fib_index = (u32)(key>>32);
7342     s->dest = (u32)(key & 0xFFFFFFFF);
7343     s->entry_index = (u32) value;
7344   }));
7345   /* *INDENT-ON* */
7346
7347   if (0 == vec_len (records))
7348     {
7349       vlib_cli_output (vm, "MPLS encap table empty");
7350       goto out;
7351     }
7352
7353   /* sort output by dst address within fib */
7354   vec_sort_with_function (records, mpls_dest_cmp);
7355   vec_sort_with_function (records, mpls_fib_index_cmp);
7356   vlib_cli_output (vm, "MPLS encap table");
7357   vlib_cli_output (vm, "%=6s%=16s%=16s", "Table", "Dest address", "Labels");
7358   vec_foreach (s, records)
7359   {
7360     rx_fib = vec_elt_at_index (im->fibs, s->fib_index);
7361     vlib_cli_output (vm, "%=6d%=16U%=16U", rx_fib->table_id,
7362                      format_ip4_address, &s->dest, format_mpls_encap_index,
7363                      mm, s->entry_index);
7364     send_mpls_fib_encap_details (am, q, s, mp->context);
7365   }
7366
7367 out:
7368   vec_free (records);
7369 }
7370
7371 static void
7372 vl_api_mpls_fib_decap_details_t_handler (vl_api_mpls_fib_decap_details_t * mp)
7373 {
7374   clib_warning ("BUG");
7375 }
7376
7377 static void
7378 send_mpls_fib_decap_details (vpe_api_main_t * am,
7379                              unix_shared_memory_queue_t * q,
7380                              show_mpls_fib_t * s,
7381                              u32 rx_table_id,
7382                              u32 tx_table_id, char *swif_tag, u32 context)
7383 {
7384   vl_api_mpls_fib_decap_details_t *mp;
7385
7386   mp = vl_msg_api_alloc (sizeof (*mp));
7387   memset (mp, 0, sizeof (*mp));
7388   mp->_vl_msg_id = ntohs (VL_API_MPLS_FIB_DECAP_DETAILS);
7389   mp->context = context;
7390
7391   mp->fib_index = htonl (s->fib_index);
7392   mp->entry_index = htonl (s->entry_index);
7393   mp->dest = s->dest;
7394   mp->s_bit = htonl (s->s_bit);
7395   mp->label = htonl (s->label);
7396   mp->rx_table_id = htonl (rx_table_id);
7397   mp->tx_table_id = htonl (tx_table_id);
7398   strncpy ((char *) mp->swif_tag,
7399            (char *) swif_tag, ARRAY_LEN (mp->swif_tag) - 1);
7400
7401   vl_msg_api_send_shmem (q, (u8 *) & mp);
7402 }
7403
7404 static void
7405 vl_api_mpls_fib_decap_dump_t_handler (vl_api_mpls_fib_decap_dump_t * mp)
7406 {
7407   vpe_api_main_t *am = &vpe_api_main;
7408   unix_shared_memory_queue_t *q;
7409   vlib_main_t *vm = &vlib_global_main;
7410   u64 key;
7411   u32 value;
7412   show_mpls_fib_t *records = 0;
7413   show_mpls_fib_t *s;
7414   mpls_main_t *mm = &mpls_main;
7415   ip4_main_t *im = &ip4_main;
7416   ip4_fib_t *rx_fib;
7417   ip4_fib_t *tx_fib;
7418   u32 tx_table_id;
7419   char *swif_tag;
7420
7421   q = vl_api_client_index_to_input_queue (mp->client_index);
7422   if (q == 0)
7423     return;
7424
7425   /* *INDENT-OFF* */
7426   hash_foreach (key, value, mm->mpls_decap_by_rx_fib_and_label,
7427   ({
7428     vec_add2 (records, s, 1);
7429     s->fib_index = (u32)(key>>32);
7430     s->entry_index = (u32) value;
7431     s->label = ((u32) key)>>12;
7432     s->s_bit = (key & (1<<8)) != 0;
7433   }));
7434   /* *INDENT-ON* */
7435
7436   if (!vec_len (records))
7437     {
7438       vlib_cli_output (vm, "MPLS decap table empty");
7439       goto out;
7440     }
7441
7442   vec_sort_with_function (records, mpls_label_cmp);
7443   vlib_cli_output (vm, "MPLS decap table");
7444   vlib_cli_output (vm, "%=10s%=15s%=6s%=6s", "RX Table", "TX Table/Intfc",
7445                    "Label", "S-bit");
7446   vec_foreach (s, records)
7447   {
7448     mpls_decap_t *d;
7449     d = pool_elt_at_index (mm->decaps, s->entry_index);
7450     if (d->next_index == MPLS_INPUT_NEXT_IP4_INPUT)
7451       {
7452         tx_fib = vec_elt_at_index (im->fibs, d->tx_fib_index);
7453         tx_table_id = tx_fib->table_id;
7454         swif_tag = "     ";
7455       }
7456     else
7457       {
7458         tx_table_id = d->tx_fib_index;
7459         swif_tag = "(i)  ";
7460       }
7461     rx_fib = vec_elt_at_index (im->fibs, s->fib_index);
7462
7463     vlib_cli_output (vm, "%=10d%=10d%=5s%=6d%=6d", rx_fib->table_id,
7464                      tx_table_id, swif_tag, s->label, s->s_bit);
7465
7466     send_mpls_fib_decap_details (am, q, s, rx_fib->table_id,
7467                                  tx_table_id, swif_tag, mp->context);
7468   }
7469
7470 out:
7471   vec_free (records);
7472 }
7473
7474 static void
7475 vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
7476 {
7477   unix_shared_memory_queue_t *q;
7478
7479   q = vl_api_client_index_to_input_queue (mp->client_index);
7480   if (q == 0)
7481     return;
7482
7483   vnet_classify_main_t *cm = &vnet_classify_main;
7484   vnet_classify_table_t *t;
7485   u32 *table_ids = 0;
7486   u32 count;
7487
7488   /* *INDENT-OFF* */
7489   pool_foreach (t, cm->tables,
7490   ({
7491     vec_add1 (table_ids, ntohl(t - cm->tables));
7492   }));
7493   /* *INDENT-ON* */
7494   count = vec_len (table_ids);
7495
7496   vl_api_classify_table_ids_reply_t *rmp;
7497   rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
7498   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
7499   rmp->context = mp->context;
7500   rmp->count = ntohl (count);
7501   clib_memcpy (rmp->ids, table_ids, count * sizeof (u32));
7502   rmp->retval = 0;
7503
7504   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7505
7506   vec_free (table_ids);
7507 }
7508
7509 static void
7510   vl_api_classify_table_by_interface_t_handler
7511   (vl_api_classify_table_by_interface_t * mp)
7512 {
7513   vl_api_classify_table_by_interface_reply_t *rmp;
7514   int rv = 0;
7515
7516   u32 sw_if_index = ntohl (mp->sw_if_index);
7517   u32 *acl = 0;
7518
7519   vec_validate (acl, INPUT_ACL_N_TABLES - 1);
7520   vec_set (acl, ~0);
7521
7522   VALIDATE_SW_IF_INDEX (mp);
7523
7524   input_acl_main_t *am = &input_acl_main;
7525
7526   int if_idx;
7527   u32 type;
7528
7529   for (type = 0; type < INPUT_ACL_N_TABLES; type++)
7530     {
7531       u32 *vec_tbl = am->classify_table_index_by_sw_if_index[type];
7532       if (vec_len (vec_tbl))
7533         {
7534           for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
7535             {
7536               if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
7537                 {
7538                   continue;
7539                 }
7540               acl[type] = vec_elt (vec_tbl, if_idx);
7541             }
7542         }
7543     }
7544
7545   BAD_SW_IF_INDEX_LABEL;
7546
7547   /* *INDENT-OFF* */
7548   REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
7549   ({
7550     rmp->sw_if_index = ntohl(sw_if_index);
7551     rmp->l2_table_id = ntohl(acl[INPUT_ACL_TABLE_L2]);
7552     rmp->ip4_table_id = ntohl(acl[INPUT_ACL_TABLE_IP4]);
7553     rmp->ip6_table_id = ntohl(acl[INPUT_ACL_TABLE_IP6]);
7554   }));
7555   /* *INDENT-ON* */
7556   vec_free (acl);
7557 }
7558
7559 static void
7560 vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
7561 {
7562   unix_shared_memory_queue_t *q;
7563
7564   q = vl_api_client_index_to_input_queue (mp->client_index);
7565   if (q == 0)
7566     return;
7567
7568   vl_api_classify_table_info_reply_t *rmp = 0;
7569
7570   vnet_classify_main_t *cm = &vnet_classify_main;
7571   u32 table_id = ntohl (mp->table_id);
7572   vnet_classify_table_t *t;
7573
7574   /* *INDENT-OFF* */
7575   pool_foreach (t, cm->tables,
7576   ({
7577     if (table_id == t - cm->tables)
7578       {
7579         rmp = vl_msg_api_alloc_as_if_client
7580           (sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
7581         rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
7582         rmp->context = mp->context;
7583         rmp->table_id = ntohl(table_id);
7584         rmp->nbuckets = ntohl(t->nbuckets);
7585         rmp->match_n_vectors = ntohl(t->match_n_vectors);
7586         rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
7587         rmp->active_sessions = ntohl(t->active_elements);
7588         rmp->next_table_index = ntohl(t->next_table_index);
7589         rmp->miss_next_index = ntohl(t->miss_next_index);
7590         rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
7591         clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
7592         rmp->retval = 0;
7593         break;
7594       }
7595   }));
7596   /* *INDENT-ON* */
7597
7598   if (rmp == 0)
7599     {
7600       rmp = vl_msg_api_alloc (sizeof (*rmp));
7601       rmp->_vl_msg_id = ntohs ((VL_API_CLASSIFY_TABLE_INFO_REPLY));
7602       rmp->context = mp->context;
7603       rmp->retval = ntohl (VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
7604     }
7605
7606   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7607 }
7608
7609 static void
7610 vl_api_classify_session_details_t_handler (vl_api_classify_session_details_t *
7611                                            mp)
7612 {
7613   clib_warning ("BUG");
7614 }
7615
7616 static void
7617 send_classify_session_details (unix_shared_memory_queue_t * q,
7618                                u32 table_id,
7619                                u32 match_length,
7620                                vnet_classify_entry_t * e, u32 context)
7621 {
7622   vl_api_classify_session_details_t *rmp;
7623
7624   rmp = vl_msg_api_alloc (sizeof (*rmp));
7625   memset (rmp, 0, sizeof (*rmp));
7626   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_SESSION_DETAILS);
7627   rmp->context = context;
7628   rmp->table_id = ntohl (table_id);
7629   rmp->hit_next_index = ntohl (e->next_index);
7630   rmp->advance = ntohl (e->advance);
7631   rmp->opaque_index = ntohl (e->opaque_index);
7632   rmp->match_length = ntohl (match_length);
7633   clib_memcpy (rmp->match, e->key, match_length);
7634
7635   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7636 }
7637
7638 static void
7639 vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
7640 {
7641   vnet_classify_main_t *cm = &vnet_classify_main;
7642   unix_shared_memory_queue_t *q;
7643
7644   u32 table_id = ntohl (mp->table_id);
7645   vnet_classify_table_t *t;
7646
7647   q = vl_api_client_index_to_input_queue (mp->client_index);
7648
7649   /* *INDENT-OFF* */
7650   pool_foreach (t, cm->tables,
7651   ({
7652     if (table_id == t - cm->tables)
7653       {
7654         vnet_classify_bucket_t * b;
7655         vnet_classify_entry_t * v, * save_v;
7656         int i, j, k;
7657
7658         for (i = 0; i < t->nbuckets; i++)
7659           {
7660             b = &t->buckets [i];
7661             if (b->offset == 0)
7662               continue;
7663
7664             save_v = vnet_classify_get_entry (t, b->offset);
7665             for (j = 0; j < (1<<b->log2_pages); j++)
7666               {
7667                 for (k = 0; k < t->entries_per_page; k++)
7668                   {
7669                     v = vnet_classify_entry_at_index
7670                       (t, save_v, j*t->entries_per_page + k);
7671                     if (vnet_classify_entry_is_free (v))
7672                       continue;
7673
7674                     send_classify_session_details
7675                       (q, table_id, t->match_n_vectors * sizeof (u32x4),
7676                        v, mp->context);
7677                   }
7678               }
7679           }
7680         break;
7681       }
7682   }));
7683   /* *INDENT-ON* */
7684 }
7685
7686 static void
7687 vl_api_ipfix_enable_t_handler (vl_api_ipfix_enable_t * mp)
7688 {
7689   vlib_main_t *vm = vlib_get_main ();
7690   flow_report_main_t *frm = &flow_report_main;
7691   vl_api_ipfix_enable_reply_t *rmp;
7692   ip4_address_t collector, src;
7693   u16 collector_port = UDP_DST_PORT_ipfix;
7694   u32 path_mtu;
7695   u32 template_interval;
7696   u32 fib_id;
7697   u32 fib_index = ~0;
7698   int rv = 0;
7699
7700   memcpy (collector.data, mp->collector_address, sizeof (collector.data));
7701   collector_port = ntohs (mp->collector_port);
7702   if (collector_port == (u16) ~ 0)
7703     collector_port = UDP_DST_PORT_ipfix;
7704   memcpy (src.data, mp->src_address, sizeof (src.data));
7705   fib_id = ntohl (mp->vrf_id);
7706
7707   ip4_main_t *im = &ip4_main;
7708   uword *p = hash_get (im->fib_index_by_table_id, fib_id);
7709   if (!p)
7710     {
7711       rv = VNET_API_ERROR_NO_SUCH_FIB;
7712       goto out;
7713     }
7714   fib_index = p[0];
7715
7716   path_mtu = ntohl (mp->path_mtu);
7717   if (path_mtu == ~0)
7718     path_mtu = 512;             // RFC 7011 section 10.3.3.
7719   template_interval = ntohl (mp->template_interval);
7720   if (template_interval == ~0)
7721     template_interval = 20;
7722
7723   if (collector.as_u32 == 0)
7724     {
7725       rv = VNET_API_ERROR_INVALID_VALUE;
7726       goto out;
7727     }
7728
7729   if (src.as_u32 == 0)
7730     {
7731       rv = VNET_API_ERROR_INVALID_VALUE;
7732       goto out;
7733     }
7734
7735   if (path_mtu > 1450 /* vpp does not support fragmentation */ )
7736     {
7737       rv = VNET_API_ERROR_INVALID_VALUE;
7738       goto out;
7739     }
7740
7741   if (path_mtu < 68)
7742     {
7743       rv = VNET_API_ERROR_INVALID_VALUE;
7744       goto out;
7745     }
7746
7747   /* Reset report streams if we are reconfiguring IP addresses */
7748   if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
7749       frm->src_address.as_u32 != src.as_u32 ||
7750       frm->collector_port != collector_port)
7751     vnet_flow_reports_reset (frm);
7752
7753   frm->ipfix_collector.as_u32 = collector.as_u32;
7754   frm->collector_port = collector_port;
7755   frm->src_address.as_u32 = src.as_u32;
7756   frm->fib_index = fib_index;
7757   frm->path_mtu = path_mtu;
7758   frm->template_interval = template_interval;
7759
7760   /* Turn on the flow reporting process */
7761   vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
7762
7763 out:
7764   REPLY_MACRO (VL_API_IPFIX_ENABLE_REPLY);
7765 }
7766
7767 static void
7768 vl_api_ipfix_dump_t_handler (vl_api_ipfix_dump_t * mp)
7769 {
7770   flow_report_main_t *frm = &flow_report_main;
7771   unix_shared_memory_queue_t *q;
7772   vl_api_ipfix_details_t *rmp;
7773
7774   q = vl_api_client_index_to_input_queue (mp->client_index);
7775   if (!q)
7776     return;
7777
7778   rmp = vl_msg_api_alloc (sizeof (*rmp));
7779   memset (rmp, 0, sizeof (*rmp));
7780   rmp->_vl_msg_id = ntohs (VL_API_IPFIX_DETAILS);
7781   rmp->context = mp->context;
7782   memcpy (rmp->collector_address, frm->ipfix_collector.data,
7783           sizeof (frm->ipfix_collector.data));
7784   rmp->collector_port = htons (frm->collector_port);
7785   memcpy (rmp->src_address, frm->src_address.data,
7786           sizeof (frm->src_address.data));
7787   rmp->fib_index = htonl (frm->fib_index);
7788   rmp->path_mtu = htonl (frm->path_mtu);
7789   rmp->template_interval = htonl (frm->template_interval);
7790
7791   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7792 }
7793
7794 static void
7795 vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
7796 {
7797   vl_api_pg_create_interface_reply_t *rmp;
7798   int rv = 0;
7799
7800   pg_main_t *pg = &pg_main;
7801   u32 sw_if_index = pg_interface_add_or_get (pg, ntohl (mp->interface_id));
7802
7803   /* *INDENT-OFF* */
7804   REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
7805   ({
7806     rmp->sw_if_index = ntohl(sw_if_index);
7807   }));
7808   /* *INDENT-ON* */
7809 }
7810
7811 static void
7812 vl_api_pg_capture_t_handler (vl_api_pg_capture_t * mp)
7813 {
7814   vl_api_pg_capture_reply_t *rmp;
7815   int rv = 0;
7816
7817   vnet_main_t *vnm = vnet_get_main ();
7818   vnet_interface_main_t *im = &vnm->interface_main;
7819   vnet_hw_interface_t *hi = 0;
7820
7821   u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
7822   u32 hw_if_index = ~0;
7823   uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
7824   if (p)
7825     hw_if_index = *p;
7826   vec_free (intf_name);
7827
7828   if (hw_if_index != ~0)
7829     {
7830       pg_capture_args_t _a, *a = &_a;
7831
7832       u32 len = ntohl (mp->pcap_name_length);
7833       u8 *pcap_file_name = vec_new (u8, len);
7834       clib_memcpy (pcap_file_name, mp->pcap_file_name, len);
7835
7836       hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
7837       a->hw_if_index = hw_if_index;
7838       a->dev_instance = hi->dev_instance;
7839       a->is_enabled = mp->is_enabled;
7840       a->pcap_file_name = pcap_file_name;
7841       a->count = ntohl (mp->count);
7842
7843       clib_error_t *e = pg_capture (a);
7844       if (e)
7845         {
7846           clib_error_report (e);
7847           rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
7848         }
7849
7850       vec_free (pcap_file_name);
7851     }
7852   REPLY_MACRO (VL_API_PG_CAPTURE_REPLY);
7853 }
7854
7855 static void
7856 vl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t * mp)
7857 {
7858   vl_api_pg_enable_disable_reply_t *rmp;
7859   int rv = 0;
7860
7861   pg_main_t *pg = &pg_main;
7862   u32 stream_index = ~0;
7863
7864   int is_enable = mp->is_enabled != 0;
7865   u32 len = ntohl (mp->stream_name_length) - 1;
7866
7867   if (len > 0)
7868     {
7869       u8 *stream_name = vec_new (u8, len);
7870       clib_memcpy (stream_name, mp->stream_name, len);
7871       uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
7872       if (p)
7873         stream_index = *p;
7874       vec_free (stream_name);
7875     }
7876
7877   pg_enable_disable (stream_index, is_enable);
7878
7879   REPLY_MACRO (VL_API_PG_ENABLE_DISABLE_REPLY);
7880 }
7881
7882 static void
7883   vl_api_ip_source_and_port_range_check_add_del_t_handler
7884   (vl_api_ip_source_and_port_range_check_add_del_t * mp)
7885 {
7886   vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
7887   int rv = 0;
7888
7889   u8 is_ipv6 = mp->is_ipv6;
7890   u8 is_add = mp->is_add;
7891   u8 mask_length = mp->mask_length;
7892   ip4_address_t ip4_addr;
7893   ip6_address_t ip6_addr;
7894   u16 *low_ports = 0;
7895   u16 *high_ports = 0;
7896   u32 vrf_id;
7897   u16 tmp_low, tmp_high;
7898   u8 num_ranges;
7899   int i;
7900
7901   // Validate port range
7902   num_ranges = mp->number_of_ranges;
7903   if (num_ranges > 32)
7904     {                           // This is size of array in VPE.API
7905       rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
7906       goto reply;
7907     }
7908
7909   vec_reset_length (low_ports);
7910   vec_reset_length (high_ports);
7911
7912   for (i = 0; i < num_ranges; i++)
7913     {
7914       tmp_low = mp->low_ports[i];
7915       tmp_high = mp->high_ports[i];
7916       // If tmp_low <= tmp_high then only need to check tmp_low = 0
7917       // If tmp_low <= tmp_high then only need to check tmp_high > 65535
7918       if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
7919         {
7920           rv = VNET_API_ERROR_INVALID_VALUE;
7921           goto reply;
7922         }
7923       vec_add1 (low_ports, tmp_low);
7924       vec_add1 (high_ports, tmp_high + 1);
7925     }
7926
7927   // Validate mask_length
7928   if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
7929     {
7930       rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
7931       goto reply;
7932     }
7933
7934   vrf_id = ntohl (mp->vrf_id);
7935
7936   if (vrf_id < 1)
7937     {
7938       rv = VNET_API_ERROR_INVALID_VALUE;
7939       goto reply;
7940     }
7941
7942
7943   if (is_ipv6)
7944     {
7945       clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
7946       rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
7947                                                     mask_length,
7948                                                     vrf_id,
7949                                                     low_ports,
7950                                                     high_ports, is_add);
7951     }
7952   else
7953     {
7954       clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
7955       rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
7956                                                     mask_length,
7957                                                     vrf_id,
7958                                                     low_ports,
7959                                                     high_ports, is_add);
7960     }
7961
7962 reply:
7963   vec_free (low_ports);
7964   vec_free (high_ports);
7965   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
7966 }
7967
7968 static void
7969   vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
7970   (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
7971 {
7972   vlib_main_t *vm = vlib_get_main ();
7973   vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
7974   ip4_main_t *im = &ip4_main;
7975   int rv;
7976   u32 sw_if_index;
7977   u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
7978   u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
7979   uword *p = 0;
7980   int i;
7981
7982   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
7983     ntohl (mp->tcp_out_vrf_id);
7984   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
7985     ntohl (mp->udp_out_vrf_id);
7986   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
7987     ntohl (mp->tcp_in_vrf_id);
7988   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
7989     ntohl (mp->udp_in_vrf_id);
7990
7991
7992   for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
7993     {
7994       if (vrf_id[i] != 0 && vrf_id[i] != ~0)
7995         {
7996           p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
7997
7998           if (p == 0)
7999             {
8000               rv = VNET_API_ERROR_INVALID_VALUE;
8001               goto reply;
8002             }
8003
8004           fib_index[i] = p[0];
8005         }
8006       else
8007         fib_index[i] = ~0;
8008     }
8009   sw_if_index = ntohl (mp->sw_if_index);
8010
8011   VALIDATE_SW_IF_INDEX (mp);
8012
8013   rv =
8014     set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
8015                                         mp->is_add);
8016
8017   BAD_SW_IF_INDEX_LABEL;
8018 reply:
8019
8020   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
8021 }
8022
8023 #define BOUNCE_HANDLER(nn)                                              \
8024 static void vl_api_##nn##_t_handler (                                   \
8025     vl_api_##nn##_t *mp)                                                \
8026 {                                                                       \
8027     vpe_client_registration_t *reg;                                     \
8028     vpe_api_main_t * vam = &vpe_api_main;                               \
8029     unix_shared_memory_queue_t * q;                                     \
8030                                                                         \
8031     /* One registration only... */                                      \
8032     pool_foreach(reg, vam->nn##_registrations,                          \
8033     ({                                                                  \
8034         q = vl_api_client_index_to_input_queue (reg->client_index);     \
8035         if (q) {                                                        \
8036             /*                                                          \
8037              * If the queue is stuffed, turf the msg and complain       \
8038              * It's unlikely that the intended recipient is             \
8039              * alive; avoid deadlock at all costs.                      \
8040              */                                                         \
8041             if (q->cursize == q->maxsize) {                             \
8042                 clib_warning ("ERROR: receiver queue full, drop msg");  \
8043                 vl_msg_api_free (mp);                                   \
8044                 return;                                                 \
8045             }                                                           \
8046             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
8047             return;                                                     \
8048         }                                                               \
8049     }));                                                                \
8050     vl_msg_api_free (mp);                                               \
8051 }
8052
8053 /*
8054  * vpe_api_hookup
8055  * Add vpe's API message handlers to the table.
8056  * vlib has alread mapped shared memory and
8057  * added the client registration handlers.
8058  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
8059  */
8060
8061 static clib_error_t *
8062 vpe_api_hookup (vlib_main_t * vm)
8063 {
8064   api_main_t *am = &api_main;
8065
8066 #define _(N,n)                                                  \
8067     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
8068                            vl_api_##n##_t_handler,              \
8069                            vl_noop_handler,                     \
8070                            vl_api_##n##_t_endian,               \
8071                            vl_api_##n##_t_print,                \
8072                            sizeof(vl_api_##n##_t), 1);
8073   foreach_vpe_api_msg;
8074 #undef _
8075
8076   /*
8077    * Manually register the sr tunnel add del msg, so we trace
8078    * enough bytes to capture a typical segment list
8079    */
8080   vl_msg_api_set_handlers (VL_API_SR_TUNNEL_ADD_DEL,
8081                            "sr_tunnel_add_del",
8082                            vl_api_sr_tunnel_add_del_t_handler,
8083                            vl_noop_handler,
8084                            vl_api_sr_tunnel_add_del_t_endian,
8085                            vl_api_sr_tunnel_add_del_t_print, 256, 1);
8086
8087
8088   /*
8089    * Manually register the sr policy add del msg, so we trace
8090    * enough bytes to capture a typical tunnel name list
8091    */
8092   vl_msg_api_set_handlers (VL_API_SR_POLICY_ADD_DEL,
8093                            "sr_policy_add_del",
8094                            vl_api_sr_policy_add_del_t_handler,
8095                            vl_noop_handler,
8096                            vl_api_sr_policy_add_del_t_endian,
8097                            vl_api_sr_policy_add_del_t_print, 256, 1);
8098
8099   /*
8100    * Trace space for 8 MPLS encap labels, classifier mask+match
8101    */
8102   am->api_trace_cfg[VL_API_MPLS_ADD_DEL_ENCAP].size += 8 * sizeof (u32);
8103   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
8104   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size
8105     += 5 * sizeof (u32x4);
8106   am->api_trace_cfg[VL_API_VXLAN_ADD_DEL_TUNNEL].size += 16 * sizeof (u32);
8107
8108   /*
8109    * Thread-safe API messages
8110    */
8111   am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
8112   am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
8113
8114   return 0;
8115 }
8116
8117 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
8118
8119 static clib_error_t *
8120 vpe_api_init (vlib_main_t * vm)
8121 {
8122   vpe_api_main_t *am = &vpe_api_main;
8123
8124   am->vlib_main = vm;
8125   am->vnet_main = vnet_get_main ();
8126   am->interface_events_registration_hash = hash_create (0, sizeof (uword));
8127   am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
8128   am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
8129   am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
8130   am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
8131   am->oam_events_registration_hash = hash_create (0, sizeof (uword));
8132
8133   vl_api_init (vm);
8134   vl_set_memory_region_name ("/vpe-api");
8135   vl_enable_disable_memory_api (vm, 1 /* enable it */ );
8136
8137   return 0;
8138 }
8139
8140 VLIB_INIT_FUNCTION (vpe_api_init);
8141
8142
8143 static clib_error_t *
8144 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
8145 {
8146   u8 *chroot_path;
8147   int uid, gid, rv;
8148   char *s, buf[128];
8149   struct passwd _pw, *pw;
8150   struct group _grp, *grp;
8151   clib_error_t *e;
8152
8153   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
8154     {
8155       if (unformat (input, "prefix %s", &chroot_path))
8156         {
8157           vec_add1 (chroot_path, 0);
8158           vl_set_memory_root_path ((char *) chroot_path);
8159         }
8160       else if (unformat (input, "uid %d", &uid))
8161         vl_set_memory_uid (uid);
8162       else if (unformat (input, "gid %d", &gid))
8163         vl_set_memory_gid (gid);
8164       else if (unformat (input, "uid %s", &s))
8165         {
8166           /* lookup the username */
8167           pw = NULL;
8168           rv = getpwnam_r (s, &_pw, buf, sizeof (buf), &pw);
8169           if (rv < 0)
8170             {
8171               e = clib_error_return_code (0, rv,
8172                                           CLIB_ERROR_ERRNO_VALID |
8173                                           CLIB_ERROR_FATAL,
8174                                           "cannot fetch username %s", s);
8175               vec_free (s);
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               return e;
8184             }
8185           vec_free (s);
8186           vl_set_memory_uid (pw->pw_uid);
8187         }
8188       else if (unformat (input, "gid %s", &s))
8189         {
8190           /* lookup the group name */
8191           grp = NULL;
8192           rv = getgrnam_r (s, &_grp, buf, sizeof (buf), &grp);
8193           if (rv != 0)
8194             {
8195               e = clib_error_return_code (0, rv,
8196                                           CLIB_ERROR_ERRNO_VALID |
8197                                           CLIB_ERROR_FATAL,
8198                                           "cannot fetch group %s", s);
8199               vec_free (s);
8200               return e;
8201             }
8202           if (grp == NULL)
8203             {
8204               e = clib_error_return_fatal (0, "group %s does not exist", s);
8205               vec_free (s);
8206               return e;
8207             }
8208           vec_free (s);
8209           vl_set_memory_gid (grp->gr_gid);
8210         }
8211       else
8212         return clib_error_return (0, "unknown input `%U'",
8213                                   format_unformat_error, input);
8214     }
8215   return 0;
8216 }
8217
8218 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
8219
8220 void *
8221 get_unformat_vnet_sw_interface (void)
8222 {
8223   return (void *) &unformat_vnet_sw_interface;
8224 }
8225
8226 #undef vl_api_version
8227 #define vl_api_version(n,v) static u32 vpe_api_version = v;
8228 #include <vpp-api/vpe.api.h>
8229 #undef vl_api_version
8230
8231 int
8232 vl_msg_api_version_check (vl_api_memclnt_create_t * mp)
8233 {
8234   if (clib_host_to_net_u32 (mp->api_versions[0]) != vpe_api_version)
8235     {
8236       clib_warning ("vpe API mismatch: 0x%08x instead of 0x%08x",
8237                     clib_host_to_net_u32 (mp->api_versions[0]),
8238                     vpe_api_version);
8239       return -1;
8240     }
8241   return 0;
8242 }
8243
8244 static u8 *
8245 format_arp_event (u8 * s, va_list * args)
8246 {
8247   vl_api_ip4_arp_event_t *event = va_arg (*args, vl_api_ip4_arp_event_t *);
8248
8249   s = format (s, "pid %d: %U", event->pid,
8250               format_ip4_address, &event->address);
8251   return s;
8252 }
8253
8254 static clib_error_t *
8255 show_ip4_arp_events_fn (vlib_main_t * vm,
8256                         unformat_input_t * input, vlib_cli_command_t * cmd)
8257 {
8258   vpe_api_main_t *am = &vpe_api_main;
8259   vl_api_ip4_arp_event_t *event;
8260
8261   if (pool_elts (am->arp_events) == 0)
8262     {
8263       vlib_cli_output (vm, "No active arp event registrations");
8264       return 0;
8265     }
8266
8267   /* *INDENT-OFF* */
8268   pool_foreach (event, am->arp_events,
8269   ({
8270     vlib_cli_output (vm, "%U", format_arp_event, event);
8271   }));
8272   /* *INDENT-ON* */
8273
8274   return 0;
8275 }
8276
8277 /* *INDENT-OFF* */
8278 VLIB_CLI_COMMAND (show_ip4_arp_events, static) = {
8279   .path = "show arp event registrations",
8280   .function = show_ip4_arp_events_fn,
8281   .short_help = "Show arp event registrations",
8282 };
8283 /* *INDENT-ON* */
8284
8285 /*
8286  * fd.io coding-style-patch-verification: ON
8287  *
8288  * Local Variables:
8289  * eval: (c-set-style "gnu")
8290  * End:
8291  */