VPP-242: Fix wrong checking of LISP Proxy-ITR mode
[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
2592   int rv = 0;
2593
2594   vec_reset_length (my_vnet_mains);
2595
2596   for (i = 0; i < vec_len (vnet_mains); i++)
2597     {
2598       if (vnet_mains[i])
2599         vec_add1 (my_vnet_mains, vnet_mains[i]);
2600     }
2601
2602   if (vec_len (vnet_mains) == 0)
2603     vec_add1 (my_vnet_mains, vnm);
2604
2605   n_counters = vec_len (im->combined_sw_if_counters);
2606
2607   for (j = 0; j < n_counters; j++)
2608     {
2609       for (i = 0; i < vec_len (my_vnet_mains); i++)
2610         {
2611           im = &my_vnet_mains[i]->interface_main;
2612           cm = im->combined_sw_if_counters + j;
2613           if (mp->sw_if_index == (u32) ~ 0)
2614             vlib_clear_combined_counters (cm);
2615           else
2616             vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
2617         }
2618     }
2619
2620   n_counters = vec_len (im->sw_if_counters);
2621
2622   for (j = 0; j < n_counters; j++)
2623     {
2624       for (i = 0; i < vec_len (my_vnet_mains); i++)
2625         {
2626           im = &my_vnet_mains[i]->interface_main;
2627           sm = im->sw_if_counters + j;
2628           if (mp->sw_if_index == (u32) ~ 0)
2629             vlib_clear_simple_counters (sm);
2630           else
2631             vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
2632         }
2633     }
2634
2635   REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
2636 }
2637
2638 static void
2639 send_sw_interface_details (vpe_api_main_t * am,
2640                            unix_shared_memory_queue_t * q,
2641                            vnet_sw_interface_t * swif,
2642                            u8 * interface_name, u32 context)
2643 {
2644   vl_api_sw_interface_details_t *mp;
2645   vnet_hw_interface_t *hi;
2646
2647   hi = vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
2648
2649   mp = vl_msg_api_alloc (sizeof (*mp));
2650   memset (mp, 0, sizeof (*mp));
2651   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
2652   mp->sw_if_index = ntohl (swif->sw_if_index);
2653   mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
2654   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
2655   mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
2656   mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
2657                      VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
2658   mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
2659                     VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
2660   mp->link_mtu = ntohs (hi->max_packet_bytes);
2661   mp->context = context;
2662
2663   strncpy ((char *) mp->interface_name,
2664            (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
2665
2666   /* Send the L2 address for ethernet physical intfcs */
2667   if (swif->sup_sw_if_index == swif->sw_if_index
2668       && hi->hw_class_index == ethernet_hw_interface_class.index)
2669     {
2670       ethernet_main_t *em = ethernet_get_main (am->vlib_main);
2671       ethernet_interface_t *ei;
2672
2673       ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
2674       ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
2675       clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
2676       mp->l2_address_length = ntohl (sizeof (ei->address));
2677     }
2678   else if (swif->sup_sw_if_index != swif->sw_if_index)
2679     {
2680       vnet_sub_interface_t *sub = &swif->sub;
2681       mp->sub_id = ntohl (sub->id);
2682       mp->sub_dot1ad = sub->eth.flags.dot1ad;
2683       mp->sub_number_of_tags =
2684         sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
2685       mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
2686       mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
2687       mp->sub_exact_match = sub->eth.flags.exact_match;
2688       mp->sub_default = sub->eth.flags.default_sub;
2689       mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
2690       mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
2691
2692       /* vlan tag rewrite data */
2693       u32 vtr_op = L2_VTR_DISABLED;
2694       u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
2695
2696       if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
2697                      &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
2698         {
2699           // error - default to disabled
2700           mp->vtr_op = ntohl (L2_VTR_DISABLED);
2701           clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
2702                         swif->sw_if_index);
2703         }
2704       else
2705         {
2706           mp->vtr_op = ntohl (vtr_op);
2707           mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
2708           mp->vtr_tag1 = ntohl (vtr_tag1);
2709           mp->vtr_tag2 = ntohl (vtr_tag2);
2710         }
2711     }
2712
2713   vl_msg_api_send_shmem (q, (u8 *) & mp);
2714 }
2715
2716 static void
2717 send_sw_interface_flags (vpe_api_main_t * am,
2718                          unix_shared_memory_queue_t * q,
2719                          vnet_sw_interface_t * swif)
2720 {
2721   vl_api_sw_interface_set_flags_t *mp;
2722   vnet_main_t *vnm = am->vnet_main;
2723
2724   vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
2725                                                        swif->sw_if_index);
2726   mp = vl_msg_api_alloc (sizeof (*mp));
2727   memset (mp, 0, sizeof (*mp));
2728   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SET_FLAGS);
2729   mp->sw_if_index = ntohl (swif->sw_if_index);
2730
2731   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
2732   mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
2733   vl_msg_api_send_shmem (q, (u8 *) & mp);
2734 }
2735
2736 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
2737                                              unix_shared_memory_queue_t * q,
2738                                              u32 sw_if_index)
2739   __attribute__ ((unused));
2740
2741 static void
2742 send_sw_interface_flags_deleted (vpe_api_main_t * am,
2743                                  unix_shared_memory_queue_t * q,
2744                                  u32 sw_if_index)
2745 {
2746   vl_api_sw_interface_set_flags_t *mp;
2747
2748   mp = vl_msg_api_alloc (sizeof (*mp));
2749   memset (mp, 0, sizeof (*mp));
2750   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SET_FLAGS);
2751   mp->sw_if_index = ntohl (sw_if_index);
2752
2753   mp->admin_up_down = 0;
2754   mp->link_up_down = 0;
2755   mp->deleted = 1;
2756   vl_msg_api_send_shmem (q, (u8 *) & mp);
2757 }
2758
2759 static void
2760 vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
2761 {
2762   vpe_api_main_t *am = &vpe_api_main;
2763   vnet_sw_interface_t *swif;
2764   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2765   u8 *filter_string = 0, *name_string = 0;
2766   unix_shared_memory_queue_t *q;
2767   char *strcasestr (char *, char *);    /* lnx hdr file botch */
2768
2769   q = vl_api_client_index_to_input_queue (mp->client_index);
2770
2771   if (q == 0)
2772     return;
2773
2774   if (mp->name_filter_valid)
2775     {
2776       mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
2777       filter_string = format (0, "%s%c", mp->name_filter, 0);
2778     }
2779
2780   /* *INDENT-OFF* */
2781   pool_foreach (swif, im->sw_interfaces,
2782   ({
2783     name_string = format (name_string, "%U%c",
2784                           format_vnet_sw_interface_name,
2785                           am->vnet_main, swif, 0);
2786
2787     if (mp->name_filter_valid == 0 ||
2788         strcasestr((char *) name_string, (char *) filter_string)) {
2789
2790       send_sw_interface_details (am, q, swif, name_string, mp->context);
2791     }
2792     _vec_len (name_string) = 0;
2793   }));
2794   /* *INDENT-ON* */
2795
2796   vec_free (name_string);
2797   vec_free (filter_string);
2798 }
2799
2800 void
2801 send_oam_event (oam_target_t * t)
2802 {
2803   vpe_api_main_t *vam = &vpe_api_main;
2804   unix_shared_memory_queue_t *q;
2805   vpe_client_registration_t *reg;
2806   vl_api_oam_event_t *mp;
2807
2808   /* *INDENT-OFF* */
2809   pool_foreach(reg, vam->oam_events_registrations,
2810   ({
2811     q = vl_api_client_index_to_input_queue (reg->client_index);
2812     if (q)
2813       {
2814         mp = vl_msg_api_alloc (sizeof (*mp));
2815         mp->_vl_msg_id = ntohs (VL_API_OAM_EVENT);
2816         clib_memcpy (mp->dst_address, &t->dst_address,
2817                      sizeof (mp->dst_address));
2818         mp->state = t->state;
2819         vl_msg_api_send_shmem (q, (u8 *)&mp);
2820       }
2821   }));
2822   /* *INDENT-ON* */
2823 }
2824
2825 static void
2826 vl_api_oam_add_del_t_handler (vl_api_oam_add_del_t * mp)
2827 {
2828   vl_api_oam_add_del_reply_t *rmp;
2829   int rv;
2830
2831   rv = vpe_oam_add_del_target ((ip4_address_t *) mp->src_address,
2832                                (ip4_address_t *) mp->dst_address,
2833                                ntohl (mp->vrf_id), (int) (mp->is_add));
2834
2835   REPLY_MACRO (VL_API_OAM_ADD_DEL_REPLY);
2836 }
2837
2838 static void
2839 vl_api_vnet_get_summary_stats_t_handler (vl_api_vnet_get_summary_stats_t * mp)
2840 {
2841   stats_main_t *sm = &stats_main;
2842   vnet_interface_main_t *im = sm->interface_main;
2843   vl_api_vnet_summary_stats_reply_t *rmp;
2844   vlib_combined_counter_main_t *cm;
2845   vlib_counter_t v;
2846   int i, which;
2847   u64 total_pkts[VLIB_N_RX_TX];
2848   u64 total_bytes[VLIB_N_RX_TX];
2849
2850   unix_shared_memory_queue_t *q =
2851     vl_api_client_index_to_input_queue (mp->client_index);
2852
2853   if (!q)
2854     return;
2855
2856   rmp = vl_msg_api_alloc (sizeof (*rmp));
2857   rmp->_vl_msg_id = ntohs (VL_API_VNET_SUMMARY_STATS_REPLY);
2858   rmp->context = mp->context;
2859   rmp->retval = 0;
2860
2861   memset (total_pkts, 0, sizeof (total_pkts));
2862   memset (total_bytes, 0, sizeof (total_bytes));
2863
2864   vnet_interface_counter_lock (im);
2865
2866   vec_foreach (cm, im->combined_sw_if_counters)
2867   {
2868     which = cm - im->combined_sw_if_counters;
2869
2870     for (i = 0; i < vec_len (cm->maxi); i++)
2871       {
2872         vlib_get_combined_counter (cm, i, &v);
2873         total_pkts[which] += v.packets;
2874         total_bytes[which] += v.bytes;
2875       }
2876   }
2877   vnet_interface_counter_unlock (im);
2878
2879   /* Note: in HOST byte order! */
2880   rmp->total_pkts[VLIB_RX] = total_pkts[VLIB_RX];
2881   rmp->total_bytes[VLIB_RX] = total_bytes[VLIB_RX];
2882   rmp->total_pkts[VLIB_TX] = total_pkts[VLIB_TX];
2883   rmp->total_bytes[VLIB_TX] = total_bytes[VLIB_TX];
2884   rmp->vector_rate = vlib_last_vector_length_per_node (sm->vlib_main);
2885
2886   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2887 }
2888
2889 /* *INDENT-OFF* */
2890 typedef CLIB_PACKED (struct {
2891   ip4_address_t address;
2892   u32 address_length: 6;
2893   u32 index:26;
2894 }) ip4_route_t;
2895 /* *INDENT-ON* */
2896
2897 static int
2898 ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2899 {
2900   vnet_main_t *vnm = vnet_get_main ();
2901   vnet_interface_main_t *im = &vnm->interface_main;
2902   ip4_main_t *im4 = &ip4_main;
2903   static ip4_route_t *routes;
2904   static u32 *sw_if_indices_to_shut;
2905   stats_main_t *sm = &stats_main;
2906   ip4_route_t *r;
2907   ip4_fib_t *fib;
2908   u32 sw_if_index;
2909   int i;
2910   int rv = VNET_API_ERROR_NO_SUCH_FIB;
2911   u32 target_fib_id = ntohl (mp->vrf_id);
2912
2913   dslock (sm, 1 /* release hint */ , 8 /* tag */ );
2914
2915   vec_foreach (fib, im4->fibs)
2916   {
2917     vnet_sw_interface_t *si;
2918
2919     if (fib->table_id != target_fib_id)
2920       continue;
2921
2922     /* remove any mpls/gre tunnels in this fib */
2923     vnet_mpls_gre_delete_fib_tunnels (fib->table_id);
2924
2925     /* remove any mpls encap/decap labels */
2926     mpls_fib_reset_labels (fib->table_id);
2927
2928     /* remove any proxy arps in this fib */
2929     vnet_proxy_arp_fib_reset (fib->table_id);
2930
2931     /* Set the flow hash for this fib to the default */
2932     vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2933
2934     vec_reset_length (sw_if_indices_to_shut);
2935
2936     /* Shut down interfaces in this FIB / clean out intfc routes */
2937     /* *INDENT-OFF* */
2938     pool_foreach (si, im->sw_interfaces,
2939     ({
2940       u32 sw_if_index = si->sw_if_index;
2941
2942       if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
2943           && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
2944               fib - im4->fibs))
2945         vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2946     }));
2947     /* *INDENT-ON* */
2948
2949     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++)
2950       {
2951         sw_if_index = sw_if_indices_to_shut[i];
2952         // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
2953
2954         u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2955         flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2956         vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2957       }
2958
2959     vec_reset_length (routes);
2960
2961     for (i = 0; i < ARRAY_LEN (fib->adj_index_by_dst_address); i++)
2962       {
2963         uword *hash = fib->adj_index_by_dst_address[i];
2964         hash_pair_t *p;
2965         ip4_route_t x;
2966
2967         x.address_length = i;
2968
2969         /* *INDENT-OFF* */
2970         hash_foreach_pair (p, hash,
2971         ({
2972           x.address.data_u32 = p->key;
2973           vec_add1 (routes, x);
2974         }));
2975         /* *INDENT-ON* */
2976       }
2977
2978     vec_foreach (r, routes)
2979     {
2980       ip4_add_del_route_args_t a;
2981
2982       memset (&a, 0, sizeof (a));
2983       a.flags = IP4_ROUTE_FLAG_FIB_INDEX | IP4_ROUTE_FLAG_DEL;
2984       a.table_index_or_table_id = fib - im4->fibs;
2985       a.dst_address = r->address;
2986       a.dst_address_length = r->address_length;
2987       a.adj_index = ~0;
2988
2989       ip4_add_del_route (im4, &a);
2990       ip4_maybe_remap_adjacencies (im4, fib - im4->fibs,
2991                                    IP4_ROUTE_FLAG_FIB_INDEX);
2992     }
2993     rv = 0;
2994     break;
2995   }                             /* vec_foreach (fib) */
2996
2997   dsunlock (sm);
2998   return rv;
2999 }
3000
3001 typedef struct
3002 {
3003   ip6_address_t address;
3004   u32 address_length;
3005   u32 index;
3006 } ip6_route_t;
3007
3008 typedef struct
3009 {
3010   u32 fib_index;
3011   ip6_route_t **routep;
3012 } add_routes_in_fib_arg_t;
3013
3014 static void
3015 add_routes_in_fib (clib_bihash_kv_24_8_t * kvp, void *arg)
3016 {
3017   add_routes_in_fib_arg_t *ap = arg;
3018
3019   if (kvp->key[2] >> 32 == ap->fib_index)
3020     {
3021       ip6_address_t *addr;
3022       ip6_route_t *r;
3023       addr = (ip6_address_t *) kvp;
3024       vec_add2 (*ap->routep, r, 1);
3025       r->address = addr[0];
3026       r->address_length = kvp->key[2] & 0xFF;
3027       r->index = kvp->value;
3028     }
3029 }
3030
3031 static int
3032 ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3033 {
3034   vnet_main_t *vnm = vnet_get_main ();
3035   vnet_interface_main_t *im = &vnm->interface_main;
3036   ip6_main_t *im6 = &ip6_main;
3037   stats_main_t *sm = &stats_main;
3038   static ip6_route_t *routes;
3039   static u32 *sw_if_indices_to_shut;
3040   ip6_route_t *r;
3041   ip6_fib_t *fib;
3042   u32 sw_if_index;
3043   int i;
3044   int rv = VNET_API_ERROR_NO_SUCH_FIB;
3045   u32 target_fib_id = ntohl (mp->vrf_id);
3046   add_routes_in_fib_arg_t _a, *a = &_a;
3047   clib_bihash_24_8_t *h = &im6->ip6_lookup_table;
3048
3049   dslock (sm, 1 /* release hint */ , 9 /* tag */ );
3050
3051   vec_foreach (fib, im6->fibs)
3052   {
3053     vnet_sw_interface_t *si;
3054
3055     if (fib->table_id != target_fib_id)
3056       continue;
3057
3058     vec_reset_length (sw_if_indices_to_shut);
3059
3060     /* Shut down interfaces in this FIB / clean out intfc routes */
3061     /* *INDENT-OFF* */
3062     pool_foreach (si, im->sw_interfaces,
3063     ({
3064       if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
3065           fib - im6->fibs)
3066         vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3067     }));
3068     /* *INDENT-ON* */
3069
3070     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++)
3071       {
3072         sw_if_index = sw_if_indices_to_shut[i];
3073         // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
3074
3075         u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3076         flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3077         vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3078       }
3079
3080     vec_reset_length (routes);
3081
3082     a->fib_index = fib - im6->fibs;
3083     a->routep = &routes;
3084
3085     clib_bihash_foreach_key_value_pair_24_8 (h, add_routes_in_fib, a);
3086
3087     vec_foreach (r, routes)
3088     {
3089       ip6_add_del_route_args_t a;
3090
3091       memset (&a, 0, sizeof (a));
3092       a.flags = IP6_ROUTE_FLAG_FIB_INDEX | IP6_ROUTE_FLAG_DEL;
3093       a.table_index_or_table_id = fib - im6->fibs;
3094       a.dst_address = r->address;
3095       a.dst_address_length = r->address_length;
3096       a.adj_index = ~0;
3097
3098       ip6_add_del_route (im6, &a);
3099       ip6_maybe_remap_adjacencies (im6, fib - im6->fibs,
3100                                    IP6_ROUTE_FLAG_FIB_INDEX);
3101     }
3102     rv = 0;
3103     /* Reinstall the neighbor / router discovery routes */
3104     vnet_ip6_fib_init (im6, fib - im6->fibs);
3105     break;
3106   }                             /* vec_foreach (fib) */
3107
3108   dsunlock (sm);
3109   return rv;
3110 }
3111
3112 static void
3113 vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3114 {
3115   int rv;
3116   vl_api_reset_fib_reply_t *rmp;
3117
3118   if (mp->is_ipv6)
3119     rv = ip6_reset_fib_t_handler (mp);
3120   else
3121     rv = ip4_reset_fib_t_handler (mp);
3122
3123   REPLY_MACRO (VL_API_RESET_FIB_REPLY);
3124 }
3125
3126
3127 static void
3128 dhcpv4_proxy_config (vl_api_dhcp_proxy_config_t * mp)
3129 {
3130   vl_api_dhcp_proxy_config_reply_t *rmp;
3131   int rv;
3132
3133   rv = dhcp_proxy_set_server ((ip4_address_t *) (&mp->dhcp_server),
3134                               (ip4_address_t *) (&mp->dhcp_src_address),
3135                               (u32) ntohl (mp->vrf_id),
3136                               (int) mp->insert_circuit_id,
3137                               (int) (mp->is_add == 0));
3138
3139   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
3140 }
3141
3142
3143 static void
3144 dhcpv6_proxy_config (vl_api_dhcp_proxy_config_t * mp)
3145 {
3146   vl_api_dhcp_proxy_config_reply_t *rmp;
3147   int rv = -1;
3148
3149   rv = dhcpv6_proxy_set_server ((ip6_address_t *) (&mp->dhcp_server),
3150                                 (ip6_address_t *) (&mp->dhcp_src_address),
3151                                 (u32) ntohl (mp->vrf_id),
3152                                 (int) mp->insert_circuit_id,
3153                                 (int) (mp->is_add == 0));
3154
3155   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
3156 }
3157
3158 static void
3159 dhcpv4_proxy_config_2 (vl_api_dhcp_proxy_config_2_t * mp)
3160 {
3161   vl_api_dhcp_proxy_config_reply_t *rmp;
3162   int rv;
3163
3164   rv = dhcp_proxy_set_server_2 ((ip4_address_t *) (&mp->dhcp_server),
3165                                 (ip4_address_t *) (&mp->dhcp_src_address),
3166                                 (u32) ntohl (mp->rx_vrf_id),
3167                                 (u32) ntohl (mp->server_vrf_id),
3168                                 (int) mp->insert_circuit_id,
3169                                 (int) (mp->is_add == 0));
3170
3171   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_2_REPLY);
3172 }
3173
3174
3175 static void
3176 dhcpv6_proxy_config_2 (vl_api_dhcp_proxy_config_2_t * mp)
3177 {
3178   vl_api_dhcp_proxy_config_reply_t *rmp;
3179   int rv = -1;
3180
3181 #if 0                           // $$$$ FIXME
3182   rv = dhcpv6_proxy_set_server_2 ((ip6_address_t *) (&mp->dhcp_server),
3183                                   (ip6_address_t *) (&mp->dhcp_src_address),
3184                                   (u32) ntohl (mp->rx_vrf_id),
3185                                   (u32) ntohl (mp->server_vrf_id),
3186                                   (int) mp->insert_circuit_id,
3187                                   (int) (mp->is_add == 0));
3188 #else
3189   rv = VNET_API_ERROR_UNIMPLEMENTED;
3190 #endif
3191
3192   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_2_REPLY);
3193 }
3194
3195
3196 static void
3197 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t * mp)
3198 {
3199   vl_api_dhcp_proxy_set_vss_reply_t *rmp;
3200   int rv;
3201   if (!mp->is_ipv6)
3202     rv = dhcp_proxy_set_option82_vss (ntohl (mp->tbl_id),
3203                                       ntohl (mp->oui),
3204                                       ntohl (mp->fib_id),
3205                                       (int) mp->is_add == 0);
3206   else
3207     rv = dhcpv6_proxy_set_vss (ntohl (mp->tbl_id),
3208                                ntohl (mp->oui),
3209                                ntohl (mp->fib_id), (int) mp->is_add == 0);
3210
3211   REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
3212 }
3213
3214
3215 static void vl_api_dhcp_proxy_config_t_handler
3216   (vl_api_dhcp_proxy_config_t * mp)
3217 {
3218   if (mp->is_ipv6 == 0)
3219     dhcpv4_proxy_config (mp);
3220   else
3221     dhcpv6_proxy_config (mp);
3222 }
3223
3224 static void vl_api_dhcp_proxy_config_2_t_handler
3225   (vl_api_dhcp_proxy_config_2_t * mp)
3226 {
3227   if (mp->is_ipv6 == 0)
3228     dhcpv4_proxy_config_2 (mp);
3229   else
3230     dhcpv6_proxy_config_2 (mp);
3231 }
3232
3233 void
3234 dhcp_compl_event_callback (u32 client_index, u32 pid, u8 * hostname,
3235                            u8 is_ipv6, u8 * host_address, u8 * router_address,
3236                            u8 * host_mac)
3237 {
3238   unix_shared_memory_queue_t *q;
3239   vl_api_dhcp_compl_event_t *mp;
3240
3241   q = vl_api_client_index_to_input_queue (client_index);
3242   if (!q)
3243     return;
3244
3245   mp = vl_msg_api_alloc (sizeof (*mp));
3246   mp->client_index = client_index;
3247   mp->pid = pid;
3248   mp->is_ipv6 = is_ipv6;
3249   clib_memcpy (&mp->hostname, hostname, vec_len (hostname));
3250   mp->hostname[vec_len (hostname) + 1] = '\n';
3251   clib_memcpy (&mp->host_address[0], host_address, 16);
3252   clib_memcpy (&mp->router_address[0], router_address, 16);
3253   clib_memcpy (&mp->host_mac[0], host_mac, 6);
3254
3255   mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
3256
3257   vl_msg_api_send_shmem (q, (u8 *) & mp);
3258 }
3259
3260 static void vl_api_dhcp_client_config_t_handler
3261   (vl_api_dhcp_client_config_t * mp)
3262 {
3263   vlib_main_t *vm = vlib_get_main ();
3264   vl_api_dhcp_client_config_reply_t *rmp;
3265   int rv = 0;
3266
3267   VALIDATE_SW_IF_INDEX (mp);
3268
3269   rv = dhcp_client_config (vm, ntohl (mp->sw_if_index),
3270                            mp->hostname, mp->is_add, mp->client_index,
3271                            mp->want_dhcp_event ? dhcp_compl_event_callback :
3272                            NULL, mp->pid);
3273
3274   BAD_SW_IF_INDEX_LABEL;
3275
3276   REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
3277 }
3278
3279 static void
3280   vl_api_sw_interface_ip6nd_ra_config_t_handler
3281   (vl_api_sw_interface_ip6nd_ra_config_t * mp, vlib_main_t * vm)
3282 {
3283   vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
3284   int rv = 0;
3285   u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
3286     default_router;
3287
3288   is_no = mp->is_no == 1;
3289   suppress = mp->suppress == 1;
3290   managed = mp->managed == 1;
3291   other = mp->other == 1;
3292   ll_option = mp->ll_option == 1;
3293   send_unicast = mp->send_unicast == 1;
3294   cease = mp->cease == 1;
3295   default_router = mp->default_router == 1;
3296
3297   VALIDATE_SW_IF_INDEX (mp);
3298
3299   rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
3300                                suppress, managed, other,
3301                                ll_option, send_unicast, cease,
3302                                default_router, ntohl (mp->lifetime),
3303                                ntohl (mp->initial_count),
3304                                ntohl (mp->initial_interval),
3305                                ntohl (mp->max_interval),
3306                                ntohl (mp->min_interval), is_no);
3307
3308   BAD_SW_IF_INDEX_LABEL;
3309
3310   REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
3311 }
3312
3313 static void
3314   vl_api_sw_interface_ip6nd_ra_prefix_t_handler
3315   (vl_api_sw_interface_ip6nd_ra_prefix_t * mp, vlib_main_t * vm)
3316 {
3317   vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
3318   int rv = 0;
3319   u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
3320
3321   VALIDATE_SW_IF_INDEX (mp);
3322
3323   is_no = mp->is_no == 1;
3324   use_default = mp->use_default == 1;
3325   no_advertise = mp->no_advertise == 1;
3326   off_link = mp->off_link == 1;
3327   no_autoconfig = mp->no_autoconfig == 1;
3328   no_onlink = mp->no_onlink == 1;
3329
3330   rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
3331                                (ip6_address_t *) mp->address,
3332                                mp->address_length, use_default,
3333                                ntohl (mp->val_lifetime),
3334                                ntohl (mp->pref_lifetime), no_advertise,
3335                                off_link, no_autoconfig, no_onlink, is_no);
3336
3337   BAD_SW_IF_INDEX_LABEL;
3338   REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
3339 }
3340
3341 static void
3342   vl_api_sw_interface_ip6_enable_disable_t_handler
3343   (vl_api_sw_interface_ip6_enable_disable_t * mp, vlib_main_t * vm)
3344 {
3345   vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
3346   vnet_main_t *vnm = vnet_get_main ();
3347   int rv = 0;
3348   clib_error_t *error;
3349
3350   vnm->api_errno = 0;
3351
3352   VALIDATE_SW_IF_INDEX (mp);
3353
3354   error =
3355     (mp->enable == 1) ? enable_ip6_interface (vm,
3356                                               ntohl (mp->sw_if_index)) :
3357     disable_ip6_interface (vm, ntohl (mp->sw_if_index));
3358
3359   if (error)
3360     {
3361       clib_error_report (error);
3362       rv = VNET_API_ERROR_UNSPECIFIED;
3363     }
3364   else
3365     {
3366       rv = vnm->api_errno;
3367     }
3368
3369   BAD_SW_IF_INDEX_LABEL;
3370
3371   REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
3372 }
3373
3374 static void
3375   vl_api_sw_interface_ip6_set_link_local_address_t_handler
3376   (vl_api_sw_interface_ip6_set_link_local_address_t * mp, vlib_main_t * vm)
3377 {
3378   vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
3379   int rv = 0;
3380   clib_error_t *error;
3381   vnet_main_t *vnm = vnet_get_main ();
3382
3383   vnm->api_errno = 0;
3384
3385   VALIDATE_SW_IF_INDEX (mp);
3386
3387   error = set_ip6_link_local_address (vm,
3388                                       ntohl (mp->sw_if_index),
3389                                       (ip6_address_t *) mp->address,
3390                                       mp->address_length);
3391   if (error)
3392     {
3393       clib_error_report (error);
3394       rv = VNET_API_ERROR_UNSPECIFIED;
3395     }
3396   else
3397     {
3398       rv = vnm->api_errno;
3399     }
3400
3401   BAD_SW_IF_INDEX_LABEL;
3402
3403   REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
3404 }
3405
3406 static void
3407 set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
3408 {
3409   vl_api_set_ip_flow_hash_reply_t *rmp;
3410   int rv = VNET_API_ERROR_UNIMPLEMENTED;
3411
3412   clib_warning ("unimplemented...");
3413
3414   REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
3415 }
3416
3417 static void
3418 set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
3419 {
3420   vl_api_set_ip_flow_hash_reply_t *rmp;
3421   int rv;
3422   u32 table_id;
3423   u32 flow_hash_config = 0;
3424
3425   table_id = ntohl (mp->vrf_id);
3426
3427 #define _(a,b) if (mp->a) flow_hash_config |= b;
3428   foreach_flow_hash_bit;
3429 #undef _
3430
3431   rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
3432
3433   REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
3434 }
3435
3436
3437 static void
3438 vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
3439 {
3440   if (mp->is_ipv6 == 0)
3441     set_ip4_flow_hash (mp);
3442   else
3443     set_ip6_flow_hash (mp);
3444 }
3445
3446 static void vl_api_sw_interface_set_unnumbered_t_handler
3447   (vl_api_sw_interface_set_unnumbered_t * mp)
3448 {
3449   vl_api_sw_interface_set_unnumbered_reply_t *rmp;
3450   int rv = 0;
3451   vnet_sw_interface_t *si;
3452   vnet_main_t *vnm = vnet_get_main ();
3453   u32 sw_if_index, unnumbered_sw_if_index;
3454
3455   sw_if_index = ntohl (mp->sw_if_index);
3456   unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
3457
3458   /*
3459    * The API message field names are backwards from
3460    * the underlying data structure names.
3461    * It's not worth changing them now.
3462    */
3463   if (pool_is_free_index (vnm->interface_main.sw_interfaces,
3464                           unnumbered_sw_if_index))
3465     {
3466       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
3467       goto done;
3468     }
3469
3470   /* Only check the "use loop0" field when setting the binding */
3471   if (mp->is_add &&
3472       pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
3473     {
3474       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
3475       goto done;
3476     }
3477
3478   si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
3479
3480   if (mp->is_add)
3481     {
3482       si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
3483       si->unnumbered_sw_if_index = sw_if_index;
3484     }
3485   else
3486     {
3487       si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
3488       si->unnumbered_sw_if_index = (u32) ~ 0;
3489     }
3490
3491 done:
3492   REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
3493 }
3494
3495 static void
3496 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
3497 {
3498   vl_api_create_loopback_reply_t *rmp;
3499   u32 sw_if_index;
3500   int rv;
3501
3502   rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address);
3503
3504   /* *INDENT-OFF* */
3505   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
3506   ({
3507     rmp->sw_if_index = ntohl (sw_if_index);
3508   }));
3509   /* *INDENT-ON* */
3510 }
3511
3512 static void
3513 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
3514 {
3515   vl_api_delete_loopback_reply_t *rmp;
3516   u32 sw_if_index;
3517   int rv;
3518
3519   sw_if_index = ntohl (mp->sw_if_index);
3520   rv = vnet_delete_loopback_interface (sw_if_index);
3521
3522   REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
3523 }
3524
3525 static void
3526 vl_api_control_ping_t_handler (vl_api_control_ping_t * mp)
3527 {
3528   vl_api_control_ping_reply_t *rmp;
3529   int rv = 0;
3530
3531   /* *INDENT-OFF* */
3532   REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
3533   ({
3534     rmp->vpe_pid = ntohl (getpid());
3535   }));
3536   /* *INDENT-ON* */
3537 }
3538
3539 static void vl_api_noprint_control_ping_t_handler
3540   (vl_api_noprint_control_ping_t * mp)
3541 {
3542   vl_api_noprint_control_ping_reply_t *rmp;
3543   int rv = 0;
3544
3545   /* *INDENT-OFF* */
3546   REPLY_MACRO2(VL_API_NOPRINT_CONTROL_PING_REPLY,
3547   ({
3548     rmp->vpe_pid = ntohl (getpid());
3549   }));
3550   /* *INDENT-ON* */
3551 }
3552
3553 static void
3554 shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
3555 {
3556   u8 **shmem_vecp = (u8 **) arg;
3557   u8 *shmem_vec;
3558   void *oldheap;
3559   api_main_t *am = &api_main;
3560   u32 offset;
3561
3562   shmem_vec = *shmem_vecp;
3563
3564   offset = vec_len (shmem_vec);
3565
3566   pthread_mutex_lock (&am->vlib_rp->mutex);
3567   oldheap = svm_push_data_heap (am->vlib_rp);
3568
3569   vec_validate (shmem_vec, offset + buffer_bytes - 1);
3570
3571   clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
3572
3573   svm_pop_heap (oldheap);
3574   pthread_mutex_unlock (&am->vlib_rp->mutex);
3575
3576   *shmem_vecp = shmem_vec;
3577 }
3578
3579
3580 static void
3581 vl_api_cli_request_t_handler (vl_api_cli_request_t * mp)
3582 {
3583   vl_api_cli_reply_t *rp;
3584   unix_shared_memory_queue_t *q;
3585   vlib_main_t *vm = vlib_get_main ();
3586   api_main_t *am = &api_main;
3587   unformat_input_t input;
3588   u8 *shmem_vec = 0;
3589   void *oldheap;
3590
3591   q = vl_api_client_index_to_input_queue (mp->client_index);
3592   if (!q)
3593     return;
3594
3595   rp = vl_msg_api_alloc (sizeof (*rp));
3596   rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY);
3597   rp->context = mp->context;
3598
3599   unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
3600
3601   vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
3602
3603   pthread_mutex_lock (&am->vlib_rp->mutex);
3604   oldheap = svm_push_data_heap (am->vlib_rp);
3605
3606   vec_add1 (shmem_vec, 0);
3607
3608   svm_pop_heap (oldheap);
3609   pthread_mutex_unlock (&am->vlib_rp->mutex);
3610
3611   rp->reply_in_shmem = (uword) shmem_vec;
3612
3613   vl_msg_api_send_shmem (q, (u8 *) & rp);
3614 }
3615
3616 static void
3617 vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
3618 {
3619   int rv;
3620   vl_api_set_arp_neighbor_limit_reply_t *rmp;
3621   vnet_main_t *vnm = vnet_get_main ();
3622   clib_error_t *error;
3623
3624   vnm->api_errno = 0;
3625
3626   if (mp->is_ipv6)
3627     error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
3628   else
3629     error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
3630
3631   if (error)
3632     {
3633       clib_error_report (error);
3634       rv = VNET_API_ERROR_UNSPECIFIED;
3635     }
3636   else
3637     {
3638       rv = vnm->api_errno;
3639     }
3640
3641   REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3642 }
3643
3644 static void vl_api_sr_tunnel_add_del_t_handler
3645   (vl_api_sr_tunnel_add_del_t * mp)
3646 {
3647 #if IPV6SR == 0
3648   clib_warning ("unimplemented");
3649 #else
3650   ip6_sr_add_del_tunnel_args_t _a, *a = &_a;
3651   int rv = 0;
3652   vl_api_sr_tunnel_add_del_reply_t *rmp;
3653   ip6_address_t *segments = 0, *seg;
3654   ip6_address_t *tags = 0, *tag;
3655   ip6_address_t *this_address;
3656   int i;
3657
3658   if (mp->n_segments == 0)
3659     {
3660       rv = -11;
3661       goto out;
3662     }
3663
3664   memset (a, 0, sizeof (*a));
3665   a->src_address = (ip6_address_t *) & mp->src_address;
3666   a->dst_address = (ip6_address_t *) & mp->dst_address;
3667   a->dst_mask_width = mp->dst_mask_width;
3668   a->flags_net_byte_order = mp->flags_net_byte_order;
3669   a->is_del = (mp->is_add == 0);
3670   a->rx_table_id = ntohl (mp->outer_vrf_id);
3671   a->tx_table_id = ntohl (mp->inner_vrf_id);
3672
3673   a->name = format (0, "%s", mp->name);
3674   if (!(vec_len (a->name)))
3675     a->name = 0;
3676
3677   a->policy_name = format (0, "%s", mp->policy_name);
3678   if (!(vec_len (a->policy_name)))
3679     a->policy_name = 0;
3680
3681   /* Yank segments and tags out of the API message */
3682   this_address = (ip6_address_t *) mp->segs_and_tags;
3683   for (i = 0; i < mp->n_segments; i++)
3684     {
3685       vec_add2 (segments, seg, 1);
3686       clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
3687       this_address++;
3688     }
3689   for (i = 0; i < mp->n_tags; i++)
3690     {
3691       vec_add2 (tags, tag, 1);
3692       clib_memcpy (tag->as_u8, this_address->as_u8, sizeof (*this_address));
3693       this_address++;
3694     }
3695
3696   a->segments = segments;
3697   a->tags = tags;
3698
3699   rv = ip6_sr_add_del_tunnel (a);
3700
3701 out:
3702
3703   REPLY_MACRO (VL_API_SR_TUNNEL_ADD_DEL_REPLY);
3704 #endif
3705 }
3706
3707 static void vl_api_sr_policy_add_del_t_handler
3708   (vl_api_sr_policy_add_del_t * mp)
3709 {
3710 #if IPV6SR == 0
3711   clib_warning ("unimplemented");
3712 #else
3713   ip6_sr_add_del_policy_args_t _a, *a = &_a;
3714   int rv = 0;
3715   vl_api_sr_policy_add_del_reply_t *rmp;
3716   int i;
3717
3718   memset (a, 0, sizeof (*a));
3719   a->is_del = (mp->is_add == 0);
3720
3721   a->name = format (0, "%s", mp->name);
3722   if (!(vec_len (a->name)))
3723     {
3724       rv = VNET_API_ERROR_NO_SUCH_NODE2;
3725       goto out;
3726     }
3727
3728   if (!(mp->tunnel_names[0]))
3729     {
3730       rv = VNET_API_ERROR_NO_SUCH_NODE2;
3731       goto out;
3732     }
3733
3734   // start deserializing tunnel_names
3735   int num_tunnels = mp->tunnel_names[0];        //number of tunnels
3736   u8 *deser_tun_names = mp->tunnel_names;
3737   deser_tun_names += 1;         //moving along
3738
3739   u8 *tun_name = 0;
3740   int tun_name_len = 0;
3741
3742   for (i = 0; i < num_tunnels; i++)
3743     {
3744       tun_name_len = *deser_tun_names;
3745       deser_tun_names += 1;
3746       vec_resize (tun_name, tun_name_len);
3747       memcpy (tun_name, deser_tun_names, tun_name_len);
3748       vec_add1 (a->tunnel_names, tun_name);
3749       deser_tun_names += tun_name_len;
3750       tun_name = 0;
3751     }
3752
3753   rv = ip6_sr_add_del_policy (a);
3754
3755 out:
3756
3757   REPLY_MACRO (VL_API_SR_POLICY_ADD_DEL_REPLY);
3758 #endif
3759 }
3760
3761 static void vl_api_sr_multicast_map_add_del_t_handler
3762   (vl_api_sr_multicast_map_add_del_t * mp)
3763 {
3764 #if IPV6SR == 0
3765   clib_warning ("unimplemented");
3766 #else
3767   ip6_sr_add_del_multicastmap_args_t _a, *a = &_a;
3768   int rv = 0;
3769   vl_api_sr_multicast_map_add_del_reply_t *rmp;
3770
3771   memset (a, 0, sizeof (*a));
3772   a->is_del = (mp->is_add == 0);
3773
3774   a->multicast_address = (ip6_address_t *) & mp->multicast_address;
3775   a->policy_name = format (0, "%s", mp->policy_name);
3776
3777   if (a->multicast_address == 0)
3778     {
3779       rv = -1;
3780       goto out;
3781     }
3782
3783   if (!(a->policy_name))
3784     {
3785       rv = -2;
3786       goto out;
3787     }
3788
3789 #if DPDK > 0                    /* Cannot call replicate without DPDK */
3790   rv = ip6_sr_add_del_multicastmap (a);
3791 #else
3792   clib_warning ("multicast replication without DPDK not implemented");
3793   rv = VNET_API_ERROR_UNIMPLEMENTED;
3794 #endif /* DPDK */
3795
3796 out:
3797
3798   REPLY_MACRO (VL_API_SR_MULTICAST_MAP_ADD_DEL_REPLY);
3799 #endif
3800 }
3801
3802 #define foreach_classify_add_del_table_field    \
3803 _(table_index)                                  \
3804 _(nbuckets)                                     \
3805 _(memory_size)                                  \
3806 _(skip_n_vectors)                               \
3807 _(match_n_vectors)                              \
3808 _(next_table_index)                             \
3809 _(miss_next_index)
3810
3811 static void vl_api_classify_add_del_table_t_handler
3812   (vl_api_classify_add_del_table_t * mp)
3813 {
3814   vl_api_classify_add_del_table_reply_t *rmp;
3815   vnet_classify_main_t *cm = &vnet_classify_main;
3816   vnet_classify_table_t *t;
3817   int rv;
3818
3819 #define _(a) u32 a;
3820   foreach_classify_add_del_table_field;
3821 #undef _
3822
3823 #define _(a) a = ntohl(mp->a);
3824   foreach_classify_add_del_table_field;
3825 #undef _
3826
3827   /* The underlying API fails silently, on purpose, so check here */
3828   if (mp->is_add == 0)
3829     if (pool_is_free_index (cm->tables, table_index))
3830       {
3831         rv = VNET_API_ERROR_NO_SUCH_TABLE;
3832         goto out;
3833       }
3834
3835   rv = vnet_classify_add_del_table
3836     (cm, mp->mask, nbuckets, memory_size,
3837      skip_n_vectors, match_n_vectors,
3838      next_table_index, miss_next_index, &table_index, mp->is_add);
3839
3840 out:
3841   /* *INDENT-OFF* */
3842   REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
3843   ({
3844     if (rv == 0 && mp->is_add)
3845       {
3846         t = pool_elt_at_index (cm->tables, table_index);
3847         rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
3848         rmp->match_n_vectors = ntohl(t->match_n_vectors);
3849         rmp->new_table_index = ntohl(table_index);
3850       }
3851     else
3852       {
3853         rmp->skip_n_vectors = ~0;
3854         rmp->match_n_vectors = ~0;
3855         rmp->new_table_index = ~0;
3856       }
3857   }));
3858   /* *INDENT-ON* */
3859 }
3860
3861 static void vl_api_classify_add_del_session_t_handler
3862   (vl_api_classify_add_del_session_t * mp)
3863 {
3864   vnet_classify_main_t *cm = &vnet_classify_main;
3865   vl_api_classify_add_del_session_reply_t *rmp;
3866   int rv;
3867   u32 table_index, hit_next_index, opaque_index;
3868   i32 advance;
3869
3870   table_index = ntohl (mp->table_index);
3871   hit_next_index = ntohl (mp->hit_next_index);
3872   opaque_index = ntohl (mp->opaque_index);
3873   advance = ntohl (mp->advance);
3874
3875   rv = vnet_classify_add_del_session
3876     (cm, table_index, mp->match, hit_next_index, opaque_index,
3877      advance, mp->is_add);
3878
3879   REPLY_MACRO (VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
3880 }
3881
3882 static void vl_api_classify_set_interface_ip_table_t_handler
3883   (vl_api_classify_set_interface_ip_table_t * mp)
3884 {
3885   vlib_main_t *vm = vlib_get_main ();
3886   vl_api_classify_set_interface_ip_table_reply_t *rmp;
3887   int rv;
3888   u32 table_index, sw_if_index;
3889
3890   table_index = ntohl (mp->table_index);
3891   sw_if_index = ntohl (mp->sw_if_index);
3892
3893   VALIDATE_SW_IF_INDEX (mp);
3894
3895   if (mp->is_ipv6)
3896     rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3897   else
3898     rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
3899
3900   BAD_SW_IF_INDEX_LABEL;
3901
3902   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
3903 }
3904
3905 static void vl_api_classify_set_interface_l2_tables_t_handler
3906   (vl_api_classify_set_interface_l2_tables_t * mp)
3907 {
3908   vl_api_classify_set_interface_l2_tables_reply_t *rmp;
3909   int rv;
3910   u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
3911   int enable;
3912
3913   ip4_table_index = ntohl (mp->ip4_table_index);
3914   ip6_table_index = ntohl (mp->ip6_table_index);
3915   other_table_index = ntohl (mp->other_table_index);
3916   sw_if_index = ntohl (mp->sw_if_index);
3917
3918   VALIDATE_SW_IF_INDEX (mp);
3919
3920   rv = vnet_l2_classify_set_tables (sw_if_index, ip4_table_index,
3921                                     ip6_table_index, other_table_index);
3922
3923   if (rv == 0)
3924     {
3925       if (ip4_table_index != ~0 || ip6_table_index != ~0
3926           || other_table_index != ~0)
3927         enable = 1;
3928       else
3929         enable = 0;
3930
3931       vnet_l2_classify_enable_disable (sw_if_index, enable);
3932     }
3933
3934   BAD_SW_IF_INDEX_LABEL;
3935
3936   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
3937 }
3938
3939 static void
3940 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
3941 {
3942   int rv = 0;
3943   vl_api_l2_fib_clear_table_reply_t *rmp;
3944
3945   /* DAW-FIXME: This API should only clear non-static l2fib entries, but
3946    *            that is not currently implemented.  When that TODO is fixed
3947    *            this call should be changed to pass 1 instead of 0.
3948    */
3949   l2fib_clear_table (0);
3950
3951   REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
3952 }
3953
3954 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
3955                                      u32 sw_if_index, u32 enable);
3956
3957 static void
3958 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
3959                                           mp)
3960 {
3961   int rv;
3962   vl_api_l2_interface_efp_filter_reply_t *rmp;
3963   vnet_main_t *vnm = vnet_get_main ();
3964
3965   // enable/disable the feature
3966   l2_efp_filter_configure (vnm, mp->sw_if_index, mp->enable_disable);
3967   rv = vnm->api_errno;
3968
3969   REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
3970 }
3971
3972 static void
3973   vl_api_l2_interface_vlan_tag_rewrite_t_handler
3974   (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
3975 {
3976   int rv = 0;
3977   vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
3978   vnet_main_t *vnm = vnet_get_main ();
3979   vlib_main_t *vm = vlib_get_main ();
3980   u32 vtr_op;
3981
3982   VALIDATE_SW_IF_INDEX (mp);
3983
3984   vtr_op = ntohl (mp->vtr_op);
3985
3986   /* The L2 code is unsuspicious */
3987   switch (vtr_op)
3988     {
3989     case L2_VTR_DISABLED:
3990     case L2_VTR_PUSH_1:
3991     case L2_VTR_PUSH_2:
3992     case L2_VTR_POP_1:
3993     case L2_VTR_POP_2:
3994     case L2_VTR_TRANSLATE_1_1:
3995     case L2_VTR_TRANSLATE_1_2:
3996     case L2_VTR_TRANSLATE_2_1:
3997     case L2_VTR_TRANSLATE_2_2:
3998       break;
3999
4000     default:
4001       rv = VNET_API_ERROR_INVALID_VALUE;
4002       goto bad_sw_if_index;
4003     }
4004
4005   rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
4006                         ntohl (mp->push_dot1q), ntohl (mp->tag1),
4007                         ntohl (mp->tag2));
4008
4009   BAD_SW_IF_INDEX_LABEL;
4010
4011   REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
4012 }
4013
4014 static void
4015 vl_api_create_vhost_user_if_t_handler (vl_api_create_vhost_user_if_t * mp)
4016 {
4017   int rv = 0;
4018   vl_api_create_vhost_user_if_reply_t *rmp;
4019 #if DPDK > 0
4020   u32 sw_if_index = (u32) ~ 0;
4021
4022   vnet_main_t *vnm = vnet_get_main ();
4023   vlib_main_t *vm = vlib_get_main ();
4024
4025   rv = vhost_user_create_if (vnm, vm, (char *) mp->sock_filename,
4026                              mp->is_server, &sw_if_index, (u64) ~ 0,
4027                              mp->renumber, ntohl (mp->custom_dev_instance),
4028                              (mp->use_custom_mac) ? mp->mac_address : NULL);
4029
4030   /* *INDENT-OFF* */
4031   REPLY_MACRO2(VL_API_CREATE_VHOST_USER_IF_REPLY,
4032   ({
4033     rmp->sw_if_index = ntohl (sw_if_index);
4034   }));
4035   /* *INDENT-ON* */
4036 #else
4037   rv = VNET_API_ERROR_UNIMPLEMENTED;
4038   REPLY_MACRO (VL_API_CREATE_VHOST_USER_IF_REPLY);
4039 #endif
4040 }
4041
4042 static void
4043 vl_api_modify_vhost_user_if_t_handler (vl_api_modify_vhost_user_if_t * mp)
4044 {
4045   int rv = 0;
4046   vl_api_modify_vhost_user_if_reply_t *rmp;
4047 #if DPDK > 0 && DPDK_VHOST_USER
4048   u32 sw_if_index = ntohl (mp->sw_if_index);
4049
4050   vnet_main_t *vnm = vnet_get_main ();
4051   vlib_main_t *vm = vlib_get_main ();
4052
4053   rv = dpdk_vhost_user_modify_if (vnm, vm, (char *) mp->sock_filename,
4054                                   mp->is_server, sw_if_index, (u64) ~ 0,
4055                                   mp->renumber,
4056                                   ntohl (mp->custom_dev_instance));
4057 #else
4058   rv = VNET_API_ERROR_UNIMPLEMENTED;
4059 #endif
4060   REPLY_MACRO (VL_API_MODIFY_VHOST_USER_IF_REPLY);
4061 }
4062
4063 static void
4064 vl_api_delete_vhost_user_if_t_handler (vl_api_delete_vhost_user_if_t * mp)
4065 {
4066   int rv = 0;
4067   vl_api_delete_vhost_user_if_reply_t *rmp;
4068 #if DPDK > 0 && DPDK_VHOST_USER
4069   vpe_api_main_t *vam = &vpe_api_main;
4070   u32 sw_if_index = ntohl (mp->sw_if_index);
4071
4072   vnet_main_t *vnm = vnet_get_main ();
4073   vlib_main_t *vm = vlib_get_main ();
4074
4075   rv = dpdk_vhost_user_delete_if (vnm, vm, sw_if_index);
4076
4077   REPLY_MACRO (VL_API_DELETE_VHOST_USER_IF_REPLY);
4078   if (!rv)
4079     {
4080       unix_shared_memory_queue_t *q =
4081         vl_api_client_index_to_input_queue (mp->client_index);
4082       if (!q)
4083         return;
4084
4085       send_sw_interface_flags_deleted (vam, q, sw_if_index);
4086     }
4087 #else
4088   rv = VNET_API_ERROR_UNIMPLEMENTED;
4089   REPLY_MACRO (VL_API_DELETE_VHOST_USER_IF_REPLY);
4090 #endif
4091 }
4092
4093 static void
4094   vl_api_sw_interface_vhost_user_details_t_handler
4095   (vl_api_sw_interface_vhost_user_details_t * mp)
4096 {
4097   clib_warning ("BUG");
4098 }
4099
4100 #if DPDK > 0 && DPDK_VHOST_USER
4101 static void
4102 send_sw_interface_vhost_user_details (vpe_api_main_t * am,
4103                                       unix_shared_memory_queue_t * q,
4104                                       vhost_user_intf_details_t * vui,
4105                                       u32 context)
4106 {
4107   vl_api_sw_interface_vhost_user_details_t *mp;
4108
4109   mp = vl_msg_api_alloc (sizeof (*mp));
4110   memset (mp, 0, sizeof (*mp));
4111   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_VHOST_USER_DETAILS);
4112   mp->sw_if_index = ntohl (vui->sw_if_index);
4113   mp->virtio_net_hdr_sz = ntohl (vui->virtio_net_hdr_sz);
4114   mp->features = clib_net_to_host_u64 (vui->features);
4115   mp->is_server = vui->is_server;
4116   mp->num_regions = ntohl (vui->num_regions);
4117   mp->sock_errno = ntohl (vui->sock_errno);
4118   mp->context = context;
4119
4120   strncpy ((char *) mp->sock_filename,
4121            (char *) vui->sock_filename, ARRAY_LEN (mp->sock_filename) - 1);
4122   strncpy ((char *) mp->interface_name,
4123            (char *) vui->if_name, ARRAY_LEN (mp->interface_name) - 1);
4124
4125   vl_msg_api_send_shmem (q, (u8 *) & mp);
4126 }
4127 #endif
4128
4129 static void
4130   vl_api_sw_interface_vhost_user_dump_t_handler
4131   (vl_api_sw_interface_vhost_user_dump_t * mp)
4132 {
4133 #if DPDK > 0 && DPDK_VHOST_USER
4134   int rv = 0;
4135   vpe_api_main_t *am = &vpe_api_main;
4136   vnet_main_t *vnm = vnet_get_main ();
4137   vlib_main_t *vm = vlib_get_main ();
4138   vhost_user_intf_details_t *ifaces = NULL;
4139   vhost_user_intf_details_t *vuid = NULL;
4140   unix_shared_memory_queue_t *q;
4141
4142   q = vl_api_client_index_to_input_queue (mp->client_index);
4143   if (q == 0)
4144     return;
4145
4146   rv = dpdk_vhost_user_dump_ifs (vnm, vm, &ifaces);
4147   if (rv)
4148     return;
4149
4150   vec_foreach (vuid, ifaces)
4151   {
4152     send_sw_interface_vhost_user_details (am, q, vuid, mp->context);
4153   }
4154   vec_free (ifaces);
4155 #endif
4156 }
4157
4158 static void
4159 send_sw_if_l2tpv3_tunnel_details (vpe_api_main_t * am,
4160                                   unix_shared_memory_queue_t * q,
4161                                   l2t_session_t * s,
4162                                   l2t_main_t * lm, u32 context)
4163 {
4164   vl_api_sw_if_l2tpv3_tunnel_details_t *mp;
4165   u8 *if_name = NULL;
4166   vnet_sw_interface_t *si = NULL;
4167
4168   si = vnet_get_hw_sw_interface (lm->vnet_main, s->hw_if_index);
4169
4170   if_name = format (if_name, "%U",
4171                     format_vnet_sw_interface_name, lm->vnet_main, si);
4172
4173   mp = vl_msg_api_alloc (sizeof (*mp));
4174   memset (mp, 0, sizeof (*mp));
4175   mp->_vl_msg_id = ntohs (VL_API_SW_IF_L2TPV3_TUNNEL_DETAILS);
4176   strncpy ((char *) mp->interface_name,
4177            (char *) if_name, ARRAY_LEN (mp->interface_name) - 1);
4178   mp->sw_if_index = ntohl (si->sw_if_index);
4179   mp->local_session_id = s->local_session_id;
4180   mp->remote_session_id = s->remote_session_id;
4181   mp->local_cookie[0] = s->local_cookie[0];
4182   mp->local_cookie[1] = s->local_cookie[1];
4183   mp->remote_cookie = s->remote_cookie;
4184   clib_memcpy (mp->client_address, &s->client_address,
4185                sizeof (s->client_address));
4186   clib_memcpy (mp->our_address, &s->our_address, sizeof (s->our_address));
4187   mp->l2_sublayer_present = s->l2_sublayer_present;
4188   mp->context = context;
4189
4190   vl_msg_api_send_shmem (q, (u8 *) & mp);
4191 }
4192
4193 static void
4194 send_ip_address_details (vpe_api_main_t * am,
4195                          unix_shared_memory_queue_t * q,
4196                          u8 * ip, u16 prefix_length, u8 is_ipv6, u32 context)
4197 {
4198   vl_api_ip_address_details_t *mp;
4199
4200   mp = vl_msg_api_alloc (sizeof (*mp));
4201   memset (mp, 0, sizeof (*mp));
4202   mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
4203
4204   if (is_ipv6)
4205     {
4206       clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
4207     }
4208   else
4209     {
4210       u32 *tp = (u32 *) mp->ip;
4211       *tp = ntohl (*(u32 *) ip);
4212     }
4213   mp->prefix_length = prefix_length;
4214   mp->context = context;
4215
4216   vl_msg_api_send_shmem (q, (u8 *) & mp);
4217 }
4218
4219 static void
4220 vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
4221 {
4222   vpe_api_main_t *am = &vpe_api_main;
4223   unix_shared_memory_queue_t *q;
4224   ip6_address_t *r6;
4225   ip4_address_t *r4;
4226   ip6_main_t *im6 = &ip6_main;
4227   ip4_main_t *im4 = &ip4_main;
4228   ip_lookup_main_t *lm6 = &im6->lookup_main;
4229   ip_lookup_main_t *lm4 = &im4->lookup_main;
4230   ip_interface_address_t *ia = 0;
4231   u32 sw_if_index = ~0;
4232   int rv __attribute__ ((unused)) = 0;
4233
4234   VALIDATE_SW_IF_INDEX (mp);
4235
4236   sw_if_index = ntohl (mp->sw_if_index);
4237
4238   q = vl_api_client_index_to_input_queue (mp->client_index);
4239   if (q == 0)
4240     {
4241       return;
4242     }
4243
4244   if (mp->is_ipv6)
4245     {
4246       /* *INDENT-OFF* */
4247       foreach_ip_interface_address (lm6, ia, sw_if_index,
4248                                     1 /* honor unnumbered */,
4249       ({
4250         r6 = ip_interface_address_get_address (lm6, ia);
4251         u16 prefix_length = ia->address_length;
4252         send_ip_address_details(am, q, (u8*)r6, prefix_length, 1, mp->context);
4253       }));
4254       /* *INDENT-ON* */
4255     }
4256   else
4257     {
4258       /* *INDENT-OFF* */
4259       foreach_ip_interface_address (lm4, ia, sw_if_index,
4260                                     1 /* honor unnumbered */,
4261       ({
4262         r4 = ip_interface_address_get_address (lm4, ia);
4263         u16 prefix_length = ia->address_length;
4264         send_ip_address_details(am, q, (u8*)r4, prefix_length, 0, mp->context);
4265       }));
4266       /* *INDENT-ON* */
4267     }
4268   BAD_SW_IF_INDEX_LABEL;
4269 }
4270
4271 static void
4272 send_ip_details (vpe_api_main_t * am,
4273                  unix_shared_memory_queue_t * q, u32 sw_if_index, u32 context)
4274 {
4275   vl_api_ip_details_t *mp;
4276
4277   mp = vl_msg_api_alloc (sizeof (*mp));
4278   memset (mp, 0, sizeof (*mp));
4279   mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
4280
4281   mp->sw_if_index = ntohl (sw_if_index);
4282   mp->context = context;
4283
4284   vl_msg_api_send_shmem (q, (u8 *) & mp);
4285 }
4286
4287 static void
4288 vl_api_sw_if_l2tpv3_tunnel_dump_t_handler (vl_api_sw_if_l2tpv3_tunnel_dump_t *
4289                                            mp)
4290 {
4291   vpe_api_main_t *am = &vpe_api_main;
4292   l2t_main_t *lm = &l2t_main;
4293   unix_shared_memory_queue_t *q;
4294   l2t_session_t *session;
4295
4296   q = vl_api_client_index_to_input_queue (mp->client_index);
4297   if (q == 0)
4298     return;
4299
4300   /* *INDENT-OFF* */
4301   pool_foreach (session, lm->sessions,
4302   ({
4303     send_sw_if_l2tpv3_tunnel_details (am, q, session, lm, mp->context);
4304   }));
4305   /* *INDENT-ON* */
4306 }
4307
4308
4309 static void
4310 send_sw_interface_tap_details (vpe_api_main_t * am,
4311                                unix_shared_memory_queue_t * q,
4312                                tapcli_interface_details_t * tap_if,
4313                                u32 context)
4314 {
4315   vl_api_sw_interface_tap_details_t *mp;
4316   mp = vl_msg_api_alloc (sizeof (*mp));
4317   memset (mp, 0, sizeof (*mp));
4318   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_TAP_DETAILS);
4319   mp->sw_if_index = ntohl (tap_if->sw_if_index);
4320   strncpy ((char *) mp->dev_name,
4321            (char *) tap_if->dev_name, ARRAY_LEN (mp->dev_name) - 1);
4322   mp->context = context;
4323
4324   vl_msg_api_send_shmem (q, (u8 *) & mp);
4325 }
4326
4327 static void
4328 vl_api_sw_interface_tap_dump_t_handler (vl_api_sw_interface_tap_dump_t * mp)
4329 {
4330   int rv = 0;
4331   vpe_api_main_t *am = &vpe_api_main;
4332   unix_shared_memory_queue_t *q;
4333   tapcli_interface_details_t *tapifs = NULL;
4334   tapcli_interface_details_t *tap_if = NULL;
4335
4336   q = vl_api_client_index_to_input_queue (mp->client_index);
4337   if (q == 0)
4338     return;
4339
4340   rv = vnet_tap_dump_ifs (&tapifs);
4341   if (rv)
4342     return;
4343
4344   vec_foreach (tap_if, tapifs)
4345   {
4346     send_sw_interface_tap_details (am, q, tap_if, mp->context);
4347   }
4348
4349   vec_free (tapifs);
4350 }
4351
4352 static void
4353 vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
4354 {
4355   vpe_api_main_t *am = &vpe_api_main;
4356   vnet_main_t *vnm = vnet_get_main ();
4357   vlib_main_t *vm = vlib_get_main ();
4358   vnet_interface_main_t *im = &vnm->interface_main;
4359   unix_shared_memory_queue_t *q;
4360   vnet_sw_interface_t *si, *sorted_sis;
4361   u32 sw_if_index = ~0;
4362
4363   q = vl_api_client_index_to_input_queue (mp->client_index);
4364   if (q == 0)
4365     {
4366       return;
4367     }
4368
4369   /* Gather interfaces. */
4370   sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
4371   _vec_len (sorted_sis) = 0;
4372   /* *INDENT-OFF* */
4373   pool_foreach (si, im->sw_interfaces,
4374   ({
4375     vec_add1 (sorted_sis, si[0]);
4376   }));
4377   /* *INDENT-ON* */
4378
4379   vec_foreach (si, sorted_sis)
4380   {
4381     if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
4382       {
4383         if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
4384           {
4385             continue;
4386           }
4387         sw_if_index = si->sw_if_index;
4388         send_ip_details (am, q, sw_if_index, mp->context);
4389       }
4390   }
4391 }
4392
4393 static void
4394 vl_api_l2_fib_table_entry_t_handler (vl_api_l2_fib_table_entry_t * mp)
4395 {
4396   clib_warning ("BUG");
4397 }
4398
4399 static void
4400 send_l2fib_table_entry (vpe_api_main_t * am,
4401                         unix_shared_memory_queue_t * q,
4402                         l2fib_entry_key_t * l2fe_key,
4403                         l2fib_entry_result_t * l2fe_res, u32 context)
4404 {
4405   vl_api_l2_fib_table_entry_t *mp;
4406
4407   mp = vl_msg_api_alloc (sizeof (*mp));
4408   memset (mp, 0, sizeof (*mp));
4409   mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_ENTRY);
4410
4411   mp->bd_id =
4412     ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
4413
4414   mp->mac = l2fib_make_key (l2fe_key->fields.mac, 0);
4415   mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
4416   mp->static_mac = l2fe_res->fields.static_mac;
4417   mp->filter_mac = l2fe_res->fields.filter;
4418   mp->bvi_mac = l2fe_res->fields.bvi;
4419   mp->context = context;
4420
4421   vl_msg_api_send_shmem (q, (u8 *) & mp);
4422 }
4423
4424 static void
4425 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
4426 {
4427   vpe_api_main_t *am = &vpe_api_main;
4428   bd_main_t *bdm = &bd_main;
4429   l2fib_entry_key_t *l2fe_key = NULL;
4430   l2fib_entry_result_t *l2fe_res = NULL;
4431   u32 ni, bd_id = ntohl (mp->bd_id);
4432   u32 bd_index;
4433   unix_shared_memory_queue_t *q;
4434   uword *p;
4435
4436   q = vl_api_client_index_to_input_queue (mp->client_index);
4437   if (q == 0)
4438     return;
4439
4440   /* see l2fib_table_dump: ~0 means "any" */
4441   if (bd_id == ~0)
4442     bd_index = ~0;
4443   else
4444     {
4445       p = hash_get (bdm->bd_index_by_bd_id, bd_id);
4446       if (p == 0)
4447         return;
4448
4449       bd_index = p[0];
4450     }
4451
4452   l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
4453
4454   vec_foreach_index (ni, l2fe_key)
4455   {
4456     send_l2fib_table_entry (am, q, vec_elt_at_index (l2fe_key, ni),
4457                             vec_elt_at_index (l2fe_res, ni), mp->context);
4458   }
4459   vec_free (l2fe_key);
4460   vec_free (l2fe_res);
4461 }
4462
4463 static void
4464 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
4465 {
4466   vl_api_show_version_reply_t *rmp;
4467   int rv = 0;
4468   char *vpe_api_get_build_directory (void);
4469   char *vpe_api_get_version (void);
4470   char *vpe_api_get_build_date (void);
4471
4472   unix_shared_memory_queue_t *q =
4473     vl_api_client_index_to_input_queue (mp->client_index);
4474
4475   if (!q)
4476     return;
4477
4478   /* *INDENT-OFF* */
4479   REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
4480   ({
4481     strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
4482     strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
4483              ARRAY_LEN(rmp->build_directory)-1);
4484     strncpy ((char *) rmp->version, vpe_api_get_version(),
4485              ARRAY_LEN(rmp->version)-1);
4486     strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
4487              ARRAY_LEN(rmp->build_date)-1);
4488   }));
4489   /* *INDENT-ON* */
4490 }
4491
4492 static void
4493 vl_api_get_node_index_t_handler (vl_api_get_node_index_t * mp)
4494 {
4495   vlib_main_t *vm = vlib_get_main ();
4496   vl_api_get_node_index_reply_t *rmp;
4497   vlib_node_t *n;
4498   int rv = 0;
4499   u32 node_index = ~0;
4500
4501   n = vlib_get_node_by_name (vm, mp->node_name);
4502
4503   if (n == 0)
4504     rv = VNET_API_ERROR_NO_SUCH_NODE;
4505   else
4506     node_index = n->index;
4507
4508   /* *INDENT-OFF* */
4509   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
4510   ({
4511     rmp->node_index = ntohl(node_index);
4512   }));
4513   /* *INDENT-ON* */
4514 }
4515
4516 static void
4517 vl_api_get_next_index_t_handler (vl_api_get_next_index_t * mp)
4518 {
4519   vlib_main_t *vm = vlib_get_main ();
4520   vl_api_get_next_index_reply_t *rmp;
4521   vlib_node_t *node, *next_node;
4522   int rv = 0;
4523   u32 next_node_index = ~0, next_index = ~0;
4524   uword *p;
4525
4526   node = vlib_get_node_by_name (vm, mp->node_name);
4527
4528   if (node == 0)
4529     {
4530       rv = VNET_API_ERROR_NO_SUCH_NODE;
4531       goto out;
4532     }
4533
4534   next_node = vlib_get_node_by_name (vm, mp->next_name);
4535
4536   if (next_node == 0)
4537     {
4538       rv = VNET_API_ERROR_NO_SUCH_NODE2;
4539       goto out;
4540     }
4541   else
4542     next_node_index = next_node->index;
4543
4544   p = hash_get (node->next_slot_by_node, next_node_index);
4545
4546   if (p == 0)
4547     {
4548       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
4549       goto out;
4550     }
4551   else
4552     next_index = p[0];
4553
4554 out:
4555   /* *INDENT-OFF* */
4556   REPLY_MACRO2(VL_API_GET_NEXT_INDEX_REPLY,
4557   ({
4558     rmp->next_index = ntohl(next_index);
4559   }));
4560   /* *INDENT-ON* */
4561 }
4562
4563 static void
4564 vl_api_add_node_next_t_handler (vl_api_add_node_next_t * mp)
4565 {
4566   vlib_main_t *vm = vlib_get_main ();
4567   vl_api_add_node_next_reply_t *rmp;
4568   vlib_node_t *n, *next;
4569   int rv = 0;
4570   u32 next_index = ~0;
4571
4572   n = vlib_get_node_by_name (vm, mp->node_name);
4573
4574   if (n == 0)
4575     {
4576       rv = VNET_API_ERROR_NO_SUCH_NODE;
4577       goto out;
4578     }
4579
4580   next = vlib_get_node_by_name (vm, mp->next_name);
4581
4582   if (next == 0)
4583     rv = VNET_API_ERROR_NO_SUCH_NODE2;
4584   else
4585     next_index = vlib_node_add_next (vm, n->index, next->index);
4586
4587 out:
4588   /* *INDENT-OFF* */
4589   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
4590   ({
4591     rmp->next_index = ntohl(next_index);
4592   }));
4593   /* *INDENT-ON* */
4594 }
4595
4596 static void vl_api_l2tpv3_create_tunnel_t_handler
4597   (vl_api_l2tpv3_create_tunnel_t * mp)
4598 {
4599   vl_api_l2tpv3_create_tunnel_reply_t *rmp;
4600   l2t_main_t *lm = &l2t_main;
4601   u32 sw_if_index = (u32) ~ 0;
4602   int rv;
4603
4604   if (mp->is_ipv6 != 1)
4605     {
4606       rv = VNET_API_ERROR_UNIMPLEMENTED;
4607       goto out;
4608     }
4609
4610   u32 encap_fib_index;
4611
4612   if (mp->encap_vrf_id != ~0)
4613     {
4614       uword *p;
4615       ip6_main_t *im = &ip6_main;
4616       if (!
4617           (p =
4618            hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id))))
4619         {
4620           rv = VNET_API_ERROR_NO_SUCH_FIB;
4621           goto out;
4622         }
4623       encap_fib_index = p[0];
4624     }
4625   else
4626     {
4627       encap_fib_index = ~0;
4628     }
4629
4630   rv = create_l2tpv3_ipv6_tunnel (lm,
4631                                   (ip6_address_t *) mp->client_address,
4632                                   (ip6_address_t *) mp->our_address,
4633                                   ntohl (mp->local_session_id),
4634                                   ntohl (mp->remote_session_id),
4635                                   clib_net_to_host_u64 (mp->local_cookie),
4636                                   clib_net_to_host_u64 (mp->remote_cookie),
4637                                   mp->l2_sublayer_present,
4638                                   encap_fib_index, &sw_if_index);
4639
4640 out:
4641   /* *INDENT-OFF* */
4642   REPLY_MACRO2(VL_API_L2TPV3_CREATE_TUNNEL_REPLY,
4643   ({
4644     rmp->sw_if_index = ntohl (sw_if_index);
4645   }));
4646   /* *INDENT-ON* */
4647 }
4648
4649 static void vl_api_l2tpv3_set_tunnel_cookies_t_handler
4650   (vl_api_l2tpv3_set_tunnel_cookies_t * mp)
4651 {
4652   vl_api_l2tpv3_set_tunnel_cookies_reply_t *rmp;
4653   l2t_main_t *lm = &l2t_main;
4654   int rv;
4655
4656   VALIDATE_SW_IF_INDEX (mp);
4657
4658   rv = l2tpv3_set_tunnel_cookies (lm, ntohl (mp->sw_if_index),
4659                                   clib_net_to_host_u64 (mp->new_local_cookie),
4660                                   clib_net_to_host_u64
4661                                   (mp->new_remote_cookie));
4662
4663   BAD_SW_IF_INDEX_LABEL;
4664
4665   REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY);
4666 }
4667
4668 static void vl_api_l2tpv3_interface_enable_disable_t_handler
4669   (vl_api_l2tpv3_interface_enable_disable_t * mp)
4670 {
4671   int rv;
4672   vnet_main_t *vnm = vnet_get_main ();
4673   vl_api_l2tpv3_interface_enable_disable_reply_t *rmp;
4674
4675   VALIDATE_SW_IF_INDEX (mp);
4676
4677   rv = l2tpv3_interface_enable_disable
4678     (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
4679
4680   BAD_SW_IF_INDEX_LABEL;
4681
4682   REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY);
4683 }
4684
4685 static void vl_api_l2tpv3_set_lookup_key_t_handler
4686   (vl_api_l2tpv3_set_lookup_key_t * mp)
4687 {
4688   int rv = 0;
4689   l2t_main_t *lm = &l2t_main;
4690   vl_api_l2tpv3_set_lookup_key_reply_t *rmp;
4691
4692   if (mp->key > L2T_LOOKUP_SESSION_ID)
4693     {
4694       rv = VNET_API_ERROR_INVALID_VALUE;
4695       goto out;
4696     }
4697
4698   lm->lookup_type = mp->key;
4699
4700 out:
4701   REPLY_MACRO (VL_API_L2TPV3_SET_LOOKUP_KEY_REPLY);
4702 }
4703
4704 static void vl_api_vxlan_add_del_tunnel_t_handler
4705   (vl_api_vxlan_add_del_tunnel_t * mp)
4706 {
4707   vl_api_vxlan_add_del_tunnel_reply_t *rmp;
4708   int rv = 0;
4709   vnet_vxlan_add_del_tunnel_args_t _a, *a = &_a;
4710   u32 encap_fib_index;
4711   uword *p;
4712   ip4_main_t *im = &ip4_main;
4713   u32 sw_if_index = ~0;
4714
4715   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
4716   if (!p)
4717     {
4718       rv = VNET_API_ERROR_NO_SUCH_FIB;
4719       goto out;
4720     }
4721   encap_fib_index = p[0];
4722
4723   /* Check src & dst are different */
4724   if ((mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 16) == 0) ||
4725       (!mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 4) == 0))
4726     {
4727       rv = VNET_API_ERROR_SAME_SRC_DST;
4728       goto out;
4729     }
4730   memset (a, 0, sizeof (*a));
4731
4732   a->is_add = mp->is_add;
4733   a->is_ip6 = mp->is_ipv6;
4734
4735   /* ip addresses sent in network byte order */
4736   if (a->is_ip6)
4737     {
4738       memcpy (&(a->src.ip6), mp->src_address, 16);
4739       memcpy (&(a->dst.ip6), mp->dst_address, 16);
4740     }
4741   else
4742     {
4743       memcpy (&(a->src.ip4), mp->src_address, 4);
4744       memcpy (&(a->dst.ip4), mp->dst_address, 4);
4745     }
4746
4747   a->encap_fib_index = encap_fib_index;
4748   a->decap_next_index = ntohl (mp->decap_next_index);
4749   a->vni = ntohl (mp->vni);
4750   rv = vnet_vxlan_add_del_tunnel (a, &sw_if_index);
4751
4752 out:
4753   /* *INDENT-OFF* */
4754   REPLY_MACRO2(VL_API_VXLAN_ADD_DEL_TUNNEL_REPLY,
4755   ({
4756     rmp->sw_if_index = ntohl (sw_if_index);
4757   }));
4758   /* *INDENT-ON* */
4759 }
4760
4761 static void send_vxlan_tunnel_details
4762   (vxlan_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4763 {
4764   vl_api_vxlan_tunnel_details_t *rmp;
4765   ip4_main_t *im4 = &ip4_main;
4766   ip6_main_t *im6 = &ip6_main;
4767   u8 is_ipv6 = !(t->flags & VXLAN_TUNNEL_IS_IPV4);
4768
4769   rmp = vl_msg_api_alloc (sizeof (*rmp));
4770   memset (rmp, 0, sizeof (*rmp));
4771   rmp->_vl_msg_id = ntohs (VL_API_VXLAN_TUNNEL_DETAILS);
4772   if (is_ipv6)
4773     {
4774       memcpy (rmp->src_address, &(t->src.ip6), 16);
4775       memcpy (rmp->dst_address, &(t->dst.ip6), 16);
4776       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].table_id);
4777     }
4778   else
4779     {
4780       memcpy (rmp->src_address, &(t->src.ip4), 4);
4781       memcpy (rmp->dst_address, &(t->dst.ip4), 4);
4782       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].table_id);
4783     }
4784   rmp->vni = htonl (t->vni);
4785   rmp->decap_next_index = htonl (t->decap_next_index);
4786   rmp->sw_if_index = htonl (t->sw_if_index);
4787   rmp->is_ipv6 = is_ipv6;
4788   rmp->context = context;
4789
4790   vl_msg_api_send_shmem (q, (u8 *) & rmp);
4791 }
4792
4793 static void vl_api_vxlan_tunnel_dump_t_handler
4794   (vl_api_vxlan_tunnel_dump_t * mp)
4795 {
4796   unix_shared_memory_queue_t *q;
4797   vxlan_main_t *vxm = &vxlan_main;
4798   vxlan_tunnel_t *t;
4799   u32 sw_if_index;
4800
4801   q = vl_api_client_index_to_input_queue (mp->client_index);
4802   if (q == 0)
4803     {
4804       return;
4805     }
4806
4807   sw_if_index = ntohl (mp->sw_if_index);
4808
4809   if (~0 == sw_if_index)
4810     {
4811       /* *INDENT-OFF* */
4812       pool_foreach (t, vxm->tunnels,
4813       ({
4814         send_vxlan_tunnel_details(t, q, mp->context);
4815       }));
4816       /* *INDENT-ON* */
4817     }
4818   else
4819     {
4820       if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) ||
4821           (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index]))
4822         {
4823           return;
4824         }
4825       t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
4826       send_vxlan_tunnel_details (t, q, mp->context);
4827     }
4828 }
4829
4830 static void vl_api_gre_add_del_tunnel_t_handler
4831   (vl_api_gre_add_del_tunnel_t * mp)
4832 {
4833   vl_api_gre_add_del_tunnel_reply_t *rmp;
4834   int rv = 0;
4835   vnet_gre_add_del_tunnel_args_t _a, *a = &_a;
4836   u32 outer_fib_id;
4837   uword *p;
4838   ip4_main_t *im = &ip4_main;
4839   u32 sw_if_index = ~0;
4840
4841   p = hash_get (im->fib_index_by_table_id, ntohl (mp->outer_fib_id));
4842   if (!p)
4843     {
4844       rv = VNET_API_ERROR_NO_SUCH_FIB;
4845       goto out;
4846     }
4847   outer_fib_id = p[0];
4848
4849   /* Check src & dst are different */
4850   if ((mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 16) == 0) ||
4851       (!mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 4) == 0))
4852     {
4853       rv = VNET_API_ERROR_SAME_SRC_DST;
4854       goto out;
4855     }
4856   memset (a, 0, sizeof (*a));
4857
4858   a->is_add = mp->is_add;
4859
4860   /* ip addresses sent in network byte order */
4861   clib_memcpy (&(a->src), mp->src_address, 4);
4862   clib_memcpy (&(a->dst), mp->dst_address, 4);
4863
4864   a->outer_fib_id = outer_fib_id;
4865   rv = vnet_gre_add_del_tunnel (a, &sw_if_index);
4866
4867 out:
4868   /* *INDENT-OFF* */
4869   REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY,
4870   ({
4871     rmp->sw_if_index = ntohl (sw_if_index);
4872   }));
4873   /* *INDENT-ON* */
4874 }
4875
4876 static void send_gre_tunnel_details
4877   (gre_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4878 {
4879   vl_api_gre_tunnel_details_t *rmp;
4880   ip4_main_t *im = &ip4_main;
4881
4882   rmp = vl_msg_api_alloc (sizeof (*rmp));
4883   memset (rmp, 0, sizeof (*rmp));
4884   rmp->_vl_msg_id = ntohs (VL_API_GRE_TUNNEL_DETAILS);
4885   clib_memcpy (rmp->src_address, &(t->tunnel_src), 4);
4886   clib_memcpy (rmp->dst_address, &(t->tunnel_dst), 4);
4887   rmp->outer_fib_id = htonl (im->fibs[t->outer_fib_index].table_id);
4888   rmp->sw_if_index = htonl (t->sw_if_index);
4889   rmp->context = context;
4890
4891   vl_msg_api_send_shmem (q, (u8 *) & rmp);
4892 }
4893
4894 static void
4895 vl_api_gre_tunnel_dump_t_handler (vl_api_gre_tunnel_dump_t * mp)
4896 {
4897   unix_shared_memory_queue_t *q;
4898   gre_main_t *gm = &gre_main;
4899   gre_tunnel_t *t;
4900   u32 sw_if_index;
4901
4902   q = vl_api_client_index_to_input_queue (mp->client_index);
4903   if (q == 0)
4904     {
4905       return;
4906     }
4907
4908   sw_if_index = ntohl (mp->sw_if_index);
4909
4910   if (~0 == sw_if_index)
4911     {
4912       /* *INDENT-OFF* */
4913       pool_foreach (t, gm->tunnels,
4914       ({
4915         send_gre_tunnel_details(t, q, mp->context);
4916       }));
4917       /* *INDENT-ON* */
4918     }
4919   else
4920     {
4921       if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) ||
4922           (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
4923         {
4924           return;
4925         }
4926       t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
4927       send_gre_tunnel_details (t, q, mp->context);
4928     }
4929 }
4930
4931 static void
4932 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
4933 {
4934   extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
4935                                     int is_add);
4936   vl_api_l2_patch_add_del_reply_t *rmp;
4937   int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
4938                              int is_add);
4939   int rv = 0;
4940
4941   VALIDATE_RX_SW_IF_INDEX (mp);
4942   VALIDATE_TX_SW_IF_INDEX (mp);
4943
4944   rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
4945                               ntohl (mp->tx_sw_if_index),
4946                               (int) (mp->is_add != 0));
4947
4948   BAD_RX_SW_IF_INDEX_LABEL;
4949   BAD_TX_SW_IF_INDEX_LABEL;
4950
4951   REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
4952 }
4953
4954 static void
4955   vl_api_vxlan_gpe_add_del_tunnel_t_handler
4956   (vl_api_vxlan_gpe_add_del_tunnel_t * mp)
4957 {
4958   vl_api_vxlan_gpe_add_del_tunnel_reply_t *rmp;
4959   int rv = 0;
4960   vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
4961   u32 encap_fib_index, decap_fib_index;
4962   u8 protocol;
4963   uword *p;
4964   ip4_main_t *im = &ip4_main;
4965   u32 sw_if_index = ~0;
4966
4967
4968   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
4969   if (!p)
4970     {
4971       rv = VNET_API_ERROR_NO_SUCH_FIB;
4972       goto out;
4973     }
4974   encap_fib_index = p[0];
4975
4976   protocol = mp->protocol;
4977
4978   /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
4979   if (protocol == VXLAN_GPE_INPUT_NEXT_IP4_INPUT)
4980     {
4981       p = hash_get (im->fib_index_by_table_id, ntohl (mp->decap_vrf_id));
4982       if (!p)
4983         {
4984           rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
4985           goto out;
4986         }
4987       decap_fib_index = p[0];
4988     }
4989   else
4990     {
4991       decap_fib_index = ntohl (mp->decap_vrf_id);
4992     }
4993
4994   /* Check src & dst are different */
4995   if ((mp->is_ipv6 && memcmp (mp->local, mp->remote, 16) == 0) ||
4996       (!mp->is_ipv6 && memcmp (mp->local, mp->remote, 4) == 0))
4997     {
4998       rv = VNET_API_ERROR_SAME_SRC_DST;
4999       goto out;
5000     }
5001   memset (a, 0, sizeof (*a));
5002
5003   a->is_add = mp->is_add;
5004   a->is_ip6 = mp->is_ipv6;
5005   /* ip addresses sent in network byte order */
5006   if (a->is_ip6)
5007     {
5008       clib_memcpy (&(a->local.ip6), mp->local, 16);
5009       clib_memcpy (&(a->remote.ip6), mp->remote, 16);
5010     }
5011   else
5012     {
5013       clib_memcpy (&(a->local.ip4), mp->local, 4);
5014       clib_memcpy (&(a->remote.ip4), mp->remote, 4);
5015     }
5016   a->encap_fib_index = encap_fib_index;
5017   a->decap_fib_index = decap_fib_index;
5018   a->protocol = protocol;
5019   a->vni = ntohl (mp->vni);
5020   rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
5021
5022 out:
5023   /* *INDENT-OFF* */
5024   REPLY_MACRO2(VL_API_VXLAN_GPE_ADD_DEL_TUNNEL_REPLY,
5025   ({
5026     rmp->sw_if_index = ntohl (sw_if_index);
5027   }));
5028   /* *INDENT-ON* */
5029 }
5030
5031 static void send_vxlan_gpe_tunnel_details
5032   (vxlan_gpe_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
5033 {
5034   vl_api_vxlan_gpe_tunnel_details_t *rmp;
5035   ip4_main_t *im4 = &ip4_main;
5036   ip6_main_t *im6 = &ip6_main;
5037   u8 is_ipv6 = !(t->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
5038
5039   rmp = vl_msg_api_alloc (sizeof (*rmp));
5040   memset (rmp, 0, sizeof (*rmp));
5041   rmp->_vl_msg_id = ntohs (VL_API_VXLAN_GPE_TUNNEL_DETAILS);
5042   if (is_ipv6)
5043     {
5044       memcpy (rmp->local, &(t->local.ip6), 16);
5045       memcpy (rmp->remote, &(t->remote.ip6), 16);
5046       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].table_id);
5047       rmp->decap_vrf_id = htonl (im6->fibs[t->decap_fib_index].table_id);
5048     }
5049   else
5050     {
5051       memcpy (rmp->local, &(t->local.ip4), 4);
5052       memcpy (rmp->remote, &(t->remote.ip4), 4);
5053       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].table_id);
5054       rmp->decap_vrf_id = htonl (im4->fibs[t->decap_fib_index].table_id);
5055     }
5056   rmp->vni = htonl (t->vni);
5057   rmp->protocol = t->protocol;
5058   rmp->sw_if_index = htonl (t->sw_if_index);
5059   rmp->is_ipv6 = is_ipv6;
5060   rmp->context = context;
5061
5062   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5063 }
5064
5065 static void vl_api_vxlan_gpe_tunnel_dump_t_handler
5066   (vl_api_vxlan_gpe_tunnel_dump_t * mp)
5067 {
5068   unix_shared_memory_queue_t *q;
5069   vxlan_gpe_main_t *vgm = &vxlan_gpe_main;
5070   vxlan_gpe_tunnel_t *t;
5071   u32 sw_if_index;
5072
5073   q = vl_api_client_index_to_input_queue (mp->client_index);
5074   if (q == 0)
5075     {
5076       return;
5077     }
5078
5079   sw_if_index = ntohl (mp->sw_if_index);
5080
5081   if (~0 == sw_if_index)
5082     {
5083       /* *INDENT-OFF* */
5084       pool_foreach (t, vgm->tunnels,
5085       ({
5086         send_vxlan_gpe_tunnel_details(t, q, mp->context);
5087       }));
5088       /* *INDENT-ON* */
5089     }
5090   else
5091     {
5092       if ((sw_if_index >= vec_len (vgm->tunnel_index_by_sw_if_index)) ||
5093           (~0 == vgm->tunnel_index_by_sw_if_index[sw_if_index]))
5094         {
5095           return;
5096         }
5097       t = &vgm->tunnels[vgm->tunnel_index_by_sw_if_index[sw_if_index]];
5098       send_vxlan_gpe_tunnel_details (t, q, mp->context);
5099     }
5100 }
5101
5102 /** Used for transferring locators via VPP API */
5103 /* *INDENT-OFF* */
5104 typedef CLIB_PACKED (struct {
5105   u32 sw_if_index; /**< locator sw_if_index */
5106   u8 priority; /**< locator priority */
5107   u8 weight; /**< locator weight */
5108 }) ls_locator_t;
5109 /* *INDENT-ON* */
5110
5111 static void
5112 vl_api_lisp_add_del_locator_set_t_handler (vl_api_lisp_add_del_locator_set_t *
5113                                            mp)
5114 {
5115   vl_api_lisp_add_del_locator_set_reply_t *rmp;
5116   int rv = 0;
5117   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
5118   locator_t locator;
5119   ls_locator_t *ls_loc;
5120   u32 ls_index = ~0;
5121   u8 *locator_name = NULL;
5122   int i;
5123
5124   memset (a, 0, sizeof (a[0]));
5125
5126   locator_name = format (0, "%s", mp->locator_set_name);
5127
5128   a->name = locator_name;
5129   a->is_add = mp->is_add;
5130   a->local = 1;
5131
5132   memset (&locator, 0, sizeof (locator));
5133   for (i = 0; i < mp->locator_num; i++)
5134     {
5135       ls_loc = &((ls_locator_t *) mp->locators)[i];
5136       VALIDATE_SW_IF_INDEX (ls_loc);
5137
5138       locator.sw_if_index = htonl (ls_loc->sw_if_index);
5139       locator.priority = ls_loc->priority;
5140       locator.weight = ls_loc->weight;
5141       locator.local = 1;
5142       vec_add1 (a->locators, locator);
5143     }
5144
5145   rv = vnet_lisp_add_del_locator_set (a, &ls_index);
5146
5147   BAD_SW_IF_INDEX_LABEL;
5148
5149   vec_free (locator_name);
5150   vec_free (a->locators);
5151
5152   REPLY_MACRO (VL_API_LISP_ADD_DEL_LOCATOR_SET_REPLY);
5153 }
5154
5155 static void
5156 vl_api_lisp_add_del_locator_t_handler (vl_api_lisp_add_del_locator_t * mp)
5157 {
5158   vl_api_lisp_add_del_locator_reply_t *rmp;
5159   int rv = 0;
5160   locator_t locator, *locators = NULL;
5161   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
5162   u32 ls_index = ~0;
5163   u8 *locator_name = NULL;
5164
5165   memset (&locator, 0, sizeof (locator));
5166   memset (a, 0, sizeof (a[0]));
5167
5168   locator.sw_if_index = ntohl (mp->sw_if_index);
5169   locator.priority = mp->priority;
5170   locator.weight = mp->weight;
5171   locator.local = 1;
5172   vec_add1 (locators, locator);
5173
5174   locator_name = format (0, "%s", mp->locator_set_name);
5175
5176   a->name = locator_name;
5177   a->locators = locators;
5178   a->is_add = mp->is_add;
5179   a->local = 1;
5180
5181   rv = vnet_lisp_add_del_locator (a, NULL, &ls_index);
5182
5183   vec_free (locators);
5184   vec_free (locator_name);
5185
5186   REPLY_MACRO (VL_API_LISP_ADD_DEL_LOCATOR_REPLY);
5187 }
5188
5189 static int
5190 unformat_lisp_eid_api (gid_address_t * dst, u32 vni, u8 type, void *src,
5191                        u8 len)
5192 {
5193   switch (type)
5194     {
5195     case 0:                     /* ipv4 */
5196       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
5197       gid_address_ip_set (dst, src, IP4);
5198       gid_address_ippref_len (dst) = len;
5199       ip_prefix_normalize (&gid_address_ippref (dst));
5200       break;
5201     case 1:                     /* ipv6 */
5202       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
5203       gid_address_ip_set (dst, src, IP6);
5204       gid_address_ippref_len (dst) = len;
5205       ip_prefix_normalize (&gid_address_ippref (dst));
5206       break;
5207     case 2:                     /* l2 mac */
5208       gid_address_type (dst) = GID_ADDR_MAC;
5209       clib_memcpy (&gid_address_mac (dst), src, 6);
5210       break;
5211     default:
5212       /* unknown type */
5213       return VNET_API_ERROR_INVALID_VALUE;
5214     }
5215
5216   gid_address_vni (dst) = vni;
5217
5218   return 0;
5219 }
5220
5221 static void
5222 vl_api_lisp_add_del_local_eid_t_handler (vl_api_lisp_add_del_local_eid_t * mp)
5223 {
5224   vl_api_lisp_add_del_local_eid_reply_t *rmp;
5225   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5226   int rv = 0;
5227   gid_address_t _eid, *eid = &_eid;
5228   uword *p = NULL;
5229   u32 locator_set_index = ~0, map_index = ~0;
5230   vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
5231   u8 *name = NULL;
5232   memset (a, 0, sizeof (a[0]));
5233   memset (eid, 0, sizeof (eid[0]));
5234
5235   rv = unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
5236                               mp->eid_type, mp->eid, mp->prefix_len);
5237   if (rv)
5238     goto out;
5239
5240   name = format (0, "%s", mp->locator_set_name);
5241   p = hash_get_mem (lcm->locator_set_index_by_name, name);
5242   if (!p)
5243     {
5244       rv = VNET_API_ERROR_INVALID_VALUE;
5245       goto out;
5246     }
5247   locator_set_index = p[0];
5248
5249   /* XXX treat batch configuration */
5250   a->is_add = mp->is_add;
5251   gid_address_copy (&a->eid, eid);
5252   a->locator_set_index = locator_set_index;
5253   a->local = 1;
5254   rv = vnet_lisp_add_del_local_mapping (a, &map_index);
5255
5256 out:
5257   vec_free (name);
5258   gid_address_free (&a->eid);
5259
5260   REPLY_MACRO (VL_API_LISP_ADD_DEL_LOCAL_EID_REPLY);
5261 }
5262
5263 static void
5264   vl_api_lisp_eid_table_add_del_map_t_handler
5265   (vl_api_lisp_eid_table_add_del_map_t * mp)
5266 {
5267   vl_api_lisp_eid_table_add_del_map_reply_t *rmp;
5268   int rv = 0;
5269   rv = vnet_lisp_eid_table_map (clib_net_to_host_u32 (mp->vni),
5270                                 clib_net_to_host_u32 (mp->dp_table),
5271                                 mp->is_l2, mp->is_add);
5272 REPLY_MACRO (VL_API_LISP_EID_TABLE_ADD_DEL_MAP_REPLY)}
5273
5274 /** Used for transferring locators via VPP API */
5275 /* *INDENT-OFF* */
5276 typedef CLIB_PACKED (struct {
5277   u8 is_ip4; /**< is locator an IPv4 address */
5278   u8 priority; /**< locator priority */
5279   u8 weight; /**< locator weight */
5280   u8 addr[16]; /**< IPv4/IPv6 address */
5281 }) rloc_t;
5282 /* *INDENT-ON* */
5283
5284 static locator_pair_t *
5285 unformat_lisp_loc_pairs (void *lcl_locs, void *rmt_locs, u32 rloc_num)
5286 {
5287   u32 i;
5288   locator_pair_t *pairs = 0, pair;
5289   rloc_t *r;
5290
5291   for (i = 0; i < rloc_num; i++)
5292     {
5293       /* local locator */
5294       r = &((rloc_t *) lcl_locs)[i];
5295       memset (&pair.lcl_loc, 0, sizeof (pair.lcl_loc));
5296       ip_address_set (&pair.lcl_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
5297
5298       /* remote locators */
5299       r = &((rloc_t *) rmt_locs)[i];
5300       memset (&pair.rmt_loc, 0, sizeof (pair.rmt_loc));
5301       ip_address_set (&pair.rmt_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
5302
5303       pair.priority = r->priority;
5304       pair.weight = r->weight;
5305
5306       vec_add1 (pairs, pair);
5307     }
5308   return pairs;
5309 }
5310
5311 static locator_t *
5312 unformat_lisp_locs (void *rmt_locs, u32 rloc_num)
5313 {
5314   u32 i;
5315   locator_t *locs = 0, loc;
5316   rloc_t *r;
5317
5318   for (i = 0; i < rloc_num; i++)
5319     {
5320       /* remote locators */
5321       r = &((rloc_t *) rmt_locs)[i];
5322       memset (&loc, 0, sizeof (loc));
5323       gid_address_ip_set (&loc.address, &r->addr, r->is_ip4 ? IP4 : IP6);
5324
5325       loc.priority = r->priority;
5326       loc.weight = r->weight;
5327
5328       vec_add1 (locs, loc);
5329     }
5330   return locs;
5331 }
5332
5333 static void
5334   vl_api_lisp_gpe_add_del_fwd_entry_t_handler
5335   (vl_api_lisp_gpe_add_del_fwd_entry_t * mp)
5336 {
5337   vl_api_lisp_gpe_add_del_fwd_entry_reply_t *rmp;
5338   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
5339   locator_pair_t *pairs = 0;
5340   int rv = 0;
5341
5342   memset (a, 0, sizeof (a[0]));
5343
5344   rv = unformat_lisp_eid_api (&a->rmt_eid, mp->vni, mp->eid_type,
5345                               mp->rmt_eid, mp->rmt_len);
5346   rv |= unformat_lisp_eid_api (&a->lcl_eid, mp->vni, mp->eid_type,
5347                                mp->lcl_eid, mp->lcl_len);
5348
5349   pairs = unformat_lisp_loc_pairs (mp->lcl_locs, mp->rmt_locs, mp->loc_num);
5350
5351   if (rv || 0 == pairs)
5352     goto send_reply;
5353
5354   a->is_add = mp->is_add;
5355   a->locator_pairs = pairs;
5356   a->dp_table = mp->dp_table;
5357   a->vni = mp->vni;
5358   a->action = mp->action;
5359
5360   rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
5361   vec_free (pairs);
5362 send_reply:
5363   REPLY_MACRO (VL_API_LISP_GPE_ADD_DEL_FWD_ENTRY_REPLY);
5364 }
5365
5366 static void
5367 vl_api_lisp_add_del_map_resolver_t_handler (vl_api_lisp_add_del_map_resolver_t
5368                                             * mp)
5369 {
5370   vl_api_lisp_add_del_map_resolver_reply_t *rmp;
5371   int rv = 0;
5372   vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
5373
5374   memset (a, 0, sizeof (a[0]));
5375
5376   a->is_add = mp->is_add;
5377   ip_address_set (&a->address, mp->ip_address, mp->is_ipv6 ? IP6 : IP4);
5378
5379   rv = vnet_lisp_add_del_map_resolver (a);
5380
5381   REPLY_MACRO (VL_API_LISP_ADD_DEL_MAP_RESOLVER_REPLY);
5382 }
5383
5384 static void
5385 vl_api_lisp_gpe_enable_disable_t_handler (vl_api_lisp_gpe_enable_disable_t *
5386                                           mp)
5387 {
5388   vl_api_lisp_gpe_enable_disable_reply_t *rmp;
5389   int rv = 0;
5390   vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
5391
5392   a->is_en = mp->is_en;
5393   vnet_lisp_gpe_enable_disable (a);
5394
5395   REPLY_MACRO (VL_API_LISP_GPE_ENABLE_DISABLE_REPLY);
5396 }
5397
5398 static void
5399 vl_api_lisp_enable_disable_t_handler (vl_api_lisp_enable_disable_t * mp)
5400 {
5401   vl_api_lisp_enable_disable_reply_t *rmp;
5402   int rv = 0;
5403
5404   vnet_lisp_enable_disable (mp->is_en);
5405   REPLY_MACRO (VL_API_LISP_ENABLE_DISABLE_REPLY);
5406 }
5407
5408 static void
5409 vl_api_lisp_gpe_add_del_iface_t_handler (vl_api_lisp_gpe_add_del_iface_t * mp)
5410 {
5411   vl_api_lisp_gpe_add_del_iface_reply_t *rmp;
5412   int rv = 0;
5413   vnet_lisp_gpe_add_del_iface_args_t _a, *a = &_a;
5414
5415   a->is_add = mp->is_add;
5416   a->dp_table = mp->dp_table;
5417   a->vni = mp->vni;
5418   a->is_l2 = mp->is_l2;
5419   rv = vnet_lisp_gpe_add_del_iface (a, 0);
5420
5421   REPLY_MACRO (VL_API_LISP_GPE_ADD_DEL_IFACE_REPLY);
5422 }
5423
5424 static void
5425 vl_api_lisp_pitr_set_locator_set_t_handler (vl_api_lisp_pitr_set_locator_set_t
5426                                             * mp)
5427 {
5428   vl_api_lisp_pitr_set_locator_set_reply_t *rmp;
5429   int rv = 0;
5430   u8 *ls_name = 0;
5431
5432   ls_name = format (0, "%s", mp->ls_name);
5433   rv = vnet_lisp_pitr_set_locator_set (ls_name, mp->is_add);
5434   vec_free (ls_name);
5435
5436   REPLY_MACRO (VL_API_LISP_PITR_SET_LOCATOR_SET_REPLY);
5437 }
5438
5439 static void
5440   vl_api_lisp_add_del_map_request_itr_rlocs_t_handler
5441   (vl_api_lisp_add_del_map_request_itr_rlocs_t * mp)
5442 {
5443   vl_api_lisp_add_del_map_request_itr_rlocs_reply_t *rmp;
5444   int rv = 0;
5445   u8 *locator_set_name = NULL;
5446   vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
5447
5448   locator_set_name = format (0, "%s", mp->locator_set_name);
5449
5450   a->is_add = mp->is_add;
5451   a->locator_set_name = locator_set_name;
5452
5453   rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
5454
5455   vec_free (locator_set_name);
5456
5457   REPLY_MACRO (VL_API_LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS_REPLY);
5458 }
5459
5460 static void
5461   vl_api_lisp_add_del_remote_mapping_t_handler
5462   (vl_api_lisp_add_del_remote_mapping_t * mp)
5463 {
5464   locator_t *rlocs = 0;
5465   vl_api_lisp_add_del_remote_mapping_reply_t *rmp;
5466   int rv = 0;
5467   gid_address_t _eid, *eid = &_eid;
5468
5469   memset (eid, 0, sizeof (eid[0]));
5470
5471   rv = unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
5472                               mp->eid_type, mp->eid, mp->eid_len);
5473   if (rv)
5474     goto send_reply;
5475
5476   rlocs = unformat_lisp_locs (mp->rlocs, mp->rloc_num);
5477   if (0 == rlocs)
5478     goto send_reply;
5479
5480   if (!mp->is_add)
5481     {
5482       vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
5483       gid_address_copy (&a->deid, eid);
5484       a->is_add = 0;
5485       rv = vnet_lisp_add_del_adjacency (a);
5486       if (rv)
5487         {
5488           goto out;
5489         }
5490     }
5491
5492   /* NOTE: for now this works as a static remote mapping, i.e.,
5493    * not authoritative and ttl infinite. */
5494   rv = vnet_lisp_add_del_mapping (eid, rlocs, mp->action, 0, ~0,
5495                                   mp->is_add, 0);
5496
5497   if (mp->del_all)
5498     vnet_lisp_clear_all_remote_adjacencies ();
5499
5500 out:
5501   vec_free (rlocs);
5502 send_reply:
5503   REPLY_MACRO (VL_API_LISP_ADD_DEL_REMOTE_MAPPING_REPLY);
5504 }
5505
5506 static void
5507 vl_api_lisp_add_del_adjacency_t_handler (vl_api_lisp_add_del_adjacency_t * mp)
5508 {
5509   vl_api_lisp_add_del_adjacency_reply_t *rmp;
5510   vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
5511
5512   int rv = 0;
5513   memset (a, 0, sizeof (a[0]));
5514
5515   rv = unformat_lisp_eid_api (&a->seid, clib_net_to_host_u32 (mp->vni),
5516                               mp->eid_type, mp->seid, mp->seid_len);
5517   rv |= unformat_lisp_eid_api (&a->deid, clib_net_to_host_u32 (mp->vni),
5518                                mp->eid_type, mp->deid, mp->deid_len);
5519
5520   if (rv)
5521     goto send_reply;
5522
5523   a->is_add = mp->is_add;
5524   rv = vnet_lisp_add_del_adjacency (a);
5525
5526 send_reply:
5527   REPLY_MACRO (VL_API_LISP_ADD_DEL_ADJACENCY_REPLY);
5528 }
5529
5530 static void
5531 send_lisp_locator_details (lisp_cp_main_t * lcm,
5532                            locator_t * loc,
5533                            unix_shared_memory_queue_t * q, u32 context)
5534 {
5535   vl_api_lisp_locator_details_t *rmp;
5536
5537   rmp = vl_msg_api_alloc (sizeof (*rmp));
5538   memset (rmp, 0, sizeof (*rmp));
5539   rmp->_vl_msg_id = ntohs (VL_API_LISP_LOCATOR_DETAILS);
5540   rmp->context = context;
5541
5542   rmp->local = loc->local;
5543   if (loc->local)
5544     {
5545       rmp->sw_if_index = ntohl (loc->sw_if_index);
5546     }
5547   else
5548     {
5549       rmp->is_ipv6 = gid_address_ip_version (&loc->address);
5550       ip_address_copy_addr (rmp->ip_address, &gid_address_ip (&loc->address));
5551     }
5552   rmp->priority = loc->priority;
5553   rmp->weight = loc->weight;
5554
5555   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5556 }
5557
5558 static void
5559 vl_api_lisp_locator_dump_t_handler (vl_api_lisp_locator_dump_t * mp)
5560 {
5561   unix_shared_memory_queue_t *q = 0;
5562   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5563   locator_set_t *lsit = 0;
5564   locator_t *loc = 0;
5565   u32 ls_index = ~0, *locit = 0;
5566   u8 filter;
5567
5568   q = vl_api_client_index_to_input_queue (mp->client_index);
5569   if (q == 0)
5570     {
5571       return;
5572     }
5573
5574   ls_index = htonl (mp->locator_set_index);
5575
5576   lsit = pool_elt_at_index (lcm->locator_set_pool, ls_index);
5577
5578   filter = mp->filter;
5579   if (filter && !((1 == filter && lsit->local) ||
5580                   (2 == filter && !lsit->local)))
5581     {
5582       return;
5583     }
5584
5585   vec_foreach (locit, lsit->locator_indices)
5586   {
5587     loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
5588     send_lisp_locator_details (lcm, loc, q, mp->context);
5589   };
5590 }
5591
5592 static void
5593 send_lisp_locator_set_details (lisp_cp_main_t * lcm,
5594                                locator_set_t * lsit,
5595                                unix_shared_memory_queue_t * q,
5596                                u32 context, u32 ls_index)
5597 {
5598   vl_api_lisp_locator_set_details_t *rmp;
5599   u8 *str = 0;
5600
5601   rmp = vl_msg_api_alloc (sizeof (*rmp));
5602   memset (rmp, 0, sizeof (*rmp));
5603   rmp->_vl_msg_id = ntohs (VL_API_LISP_LOCATOR_SET_DETAILS);
5604   rmp->context = context;
5605
5606   rmp->local = lsit->local;
5607   rmp->locator_set_index = htonl (ls_index);
5608   if (lsit->local)
5609     {
5610       ASSERT (lsit->name != NULL);
5611       strncpy ((char *) rmp->locator_set_name,
5612                (char *) lsit->name, ARRAY_LEN (rmp->locator_set_name) - 1);
5613     }
5614   else
5615     {
5616       str = format (0, "remote-%d", ls_index);
5617       strncpy ((char *) rmp->locator_set_name, (char *) str,
5618                ARRAY_LEN (rmp->locator_set_name) - 1);
5619       vec_free (str);
5620     }
5621
5622   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5623 }
5624
5625 static void
5626 vl_api_lisp_locator_set_dump_t_handler (vl_api_lisp_locator_set_dump_t * mp)
5627 {
5628   unix_shared_memory_queue_t *q = NULL;
5629   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5630   locator_set_t *lsit = NULL;
5631   u32 index;
5632   u8 filter;
5633
5634   q = vl_api_client_index_to_input_queue (mp->client_index);
5635   if (q == 0)
5636     {
5637       return;
5638     }
5639
5640   filter = mp->filter;
5641   index = 0;
5642   /* *INDENT-OFF* */
5643   pool_foreach (lsit, lcm->locator_set_pool,
5644   ({
5645     if (filter && !((1 == filter && lsit->local) ||
5646                     (2 == filter && !lsit->local))) {
5647       index++;
5648       continue;
5649     }
5650     send_lisp_locator_set_details(lcm, lsit, q, mp->context, index++);
5651   }));
5652   /* *INDENT-ON* */
5653 }
5654
5655 static void
5656 send_lisp_eid_table_details (mapping_t * mapit,
5657                              unix_shared_memory_queue_t * q,
5658                              u32 context, u8 filter)
5659 {
5660   vl_api_lisp_eid_table_details_t *rmp = NULL;
5661   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5662   locator_set_t *ls = NULL;
5663   gid_address_t *gid = NULL;
5664   u8 *mac = 0;
5665   ip_prefix_t *ip_prefix = NULL;
5666
5667   ls = pool_elt_at_index (lcm->locator_set_pool, mapit->locator_set_index);
5668
5669   switch (filter)
5670     {
5671     case 0:
5672       break;
5673     case 1:
5674       if (!ls->local)
5675         {
5676           return;
5677         }
5678       break;
5679     case 2:
5680       if (ls->local)
5681         {
5682           return;
5683         }
5684       break;
5685     default:
5686       clib_warning ("Filter error, unknown filter: %d\n", filter);
5687       return;
5688     }
5689
5690   gid = &mapit->eid;
5691   ip_prefix = &gid_address_ippref (gid);
5692   mac = gid_address_mac (gid);
5693
5694   rmp = vl_msg_api_alloc (sizeof (*rmp));
5695   memset (rmp, 0, sizeof (*rmp));
5696   rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_DETAILS);
5697   rmp->locator_set_index = mapit->locator_set_index;
5698   rmp->is_local = ls->local;
5699   rmp->ttl = mapit->ttl;
5700   rmp->authoritative = mapit->authoritative;
5701
5702   switch (gid_address_type (gid))
5703     {
5704     case GID_ADDR_IP_PREFIX:
5705       rmp->eid_prefix_len = ip_prefix_len (ip_prefix);
5706       if (ip_prefix_version (ip_prefix) == IP4)
5707         {
5708           rmp->eid_type = 0;    /* ipv4 type */
5709           clib_memcpy (rmp->eid, &ip_prefix_v4 (ip_prefix),
5710                        sizeof (ip_prefix_v4 (ip_prefix)));
5711         }
5712       else
5713         {
5714           rmp->eid_type = 1;    /* ipv6 type */
5715           clib_memcpy (rmp->eid, &ip_prefix_v6 (ip_prefix),
5716                        sizeof (ip_prefix_v6 (ip_prefix)));
5717         }
5718       break;
5719     case GID_ADDR_MAC:
5720       rmp->eid_type = 2;        /* l2 mac type */
5721       clib_memcpy (rmp->eid, mac, 6);
5722       break;
5723     default:
5724       ASSERT (0);
5725     }
5726   rmp->context = context;
5727   rmp->vni = clib_host_to_net_u32 (gid_address_vni (gid));
5728   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5729 }
5730
5731 static void
5732 vl_api_lisp_eid_table_dump_t_handler (vl_api_lisp_eid_table_dump_t * mp)
5733 {
5734   u32 mi;
5735   unix_shared_memory_queue_t *q = NULL;
5736   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5737   mapping_t *mapit = NULL;
5738   gid_address_t _eid, *eid = &_eid;
5739
5740   q = vl_api_client_index_to_input_queue (mp->client_index);
5741   if (q == 0)
5742     {
5743       return;
5744     }
5745
5746   if (mp->eid_set)
5747     {
5748       memset (eid, 0, sizeof (*eid));
5749
5750       unformat_lisp_eid_api (eid, mp->eid_type,
5751                              clib_net_to_host_u32 (mp->vni), mp->eid,
5752                              mp->prefix_length);
5753
5754       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
5755       if ((u32) ~ 0 == mi)
5756         return;
5757
5758       mapit = pool_elt_at_index (lcm->mapping_pool, mi);
5759       send_lisp_eid_table_details (mapit, q, mp->context, mp->filter);
5760     }
5761   else
5762     {
5763       /* *INDENT-OFF* */
5764       pool_foreach (mapit, lcm->mapping_pool,
5765       ({
5766         send_lisp_eid_table_details(mapit, q, mp->context,
5767                                     mp->filter);
5768       }));
5769       /* *INDENT-ON* */
5770     }
5771 }
5772
5773 static void
5774 send_lisp_gpe_tunnel_details (lisp_gpe_tunnel_t * tunnel,
5775                               unix_shared_memory_queue_t * q, u32 context)
5776 {
5777   vl_api_lisp_gpe_tunnel_details_t *rmp;
5778   lisp_gpe_main_t *lgm = &lisp_gpe_main;
5779
5780   rmp = vl_msg_api_alloc (sizeof (*rmp));
5781   memset (rmp, 0, sizeof (*rmp));
5782   rmp->_vl_msg_id = ntohs (VL_API_LISP_GPE_TUNNEL_DETAILS);
5783
5784   rmp->tunnels = tunnel - lgm->tunnels;
5785
5786   rmp->is_ipv6 = ip_addr_version (&tunnel->src) == IP6 ? 1 : 0;
5787   ip_address_copy_addr (rmp->source_ip, &tunnel->src);
5788   ip_address_copy_addr (rmp->destination_ip, &tunnel->dst);
5789
5790   rmp->encap_fib_id = htonl (tunnel->encap_fib_index);
5791   rmp->decap_fib_id = htonl (tunnel->decap_fib_index);
5792   rmp->dcap_next = htonl (tunnel->decap_next_index);
5793   rmp->lisp_ver = tunnel->ver_res;
5794   rmp->next_protocol = tunnel->next_protocol;
5795   rmp->flags = tunnel->flags;
5796   rmp->ver_res = tunnel->ver_res;
5797   rmp->res = tunnel->res;
5798   rmp->iid = htonl (tunnel->vni);
5799   rmp->context = context;
5800
5801   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5802 }
5803
5804 static void
5805 vl_api_lisp_gpe_tunnel_dump_t_handler (vl_api_lisp_gpe_tunnel_dump_t * mp)
5806 {
5807   unix_shared_memory_queue_t *q = NULL;
5808   lisp_gpe_main_t *lgm = &lisp_gpe_main;
5809   lisp_gpe_tunnel_t *tunnel = NULL;
5810
5811   if (pool_elts (lgm->tunnels) == 0)
5812     {
5813       return;
5814     }
5815
5816   q = vl_api_client_index_to_input_queue (mp->client_index);
5817   if (q == 0)
5818     {
5819       return;
5820     }
5821
5822   /* *INDENT-OFF* */
5823   pool_foreach(tunnel, lgm->tunnels,
5824   ({
5825     send_lisp_gpe_tunnel_details(tunnel, q, mp->context);
5826   }));
5827   /* *INDENT-ON* */
5828 }
5829
5830 static void
5831 send_lisp_map_resolver_details (ip_address_t * ip,
5832                                 unix_shared_memory_queue_t * q, u32 context)
5833 {
5834   vl_api_lisp_map_resolver_details_t *rmp = NULL;
5835
5836   rmp = vl_msg_api_alloc (sizeof (*rmp));
5837   memset (rmp, 0, sizeof (*rmp));
5838   rmp->_vl_msg_id = ntohs (VL_API_LISP_MAP_RESOLVER_DETAILS);
5839
5840   switch (ip_addr_version (ip))
5841     {
5842     case IP4:
5843       rmp->is_ipv6 = 0;
5844       clib_memcpy (rmp->ip_address, &ip_addr_v4 (ip),
5845                    sizeof (ip_addr_v4 (ip)));
5846       break;
5847
5848     case IP6:
5849       rmp->is_ipv6 = 1;
5850       clib_memcpy (rmp->ip_address, &ip_addr_v6 (ip),
5851                    sizeof (ip_addr_v6 (ip)));
5852       break;
5853
5854     default:
5855       ASSERT (0);
5856     }
5857   rmp->context = context;
5858
5859   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5860 }
5861
5862 static void
5863 vl_api_lisp_map_resolver_dump_t_handler (vl_api_lisp_map_resolver_dump_t * mp)
5864 {
5865   unix_shared_memory_queue_t *q = NULL;
5866   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5867   ip_address_t *ip = NULL;
5868
5869   q = vl_api_client_index_to_input_queue (mp->client_index);
5870   if (q == 0)
5871     {
5872       return;
5873     }
5874
5875   vec_foreach (ip, lcm->map_resolvers)
5876   {
5877     send_lisp_map_resolver_details (ip, q, mp->context);
5878   }
5879
5880 }
5881
5882 static void
5883 send_eid_table_map_pair (hash_pair_t * p,
5884                          unix_shared_memory_queue_t * q, u32 context)
5885 {
5886   vl_api_lisp_eid_table_map_details_t *rmp = NULL;
5887
5888   rmp = vl_msg_api_alloc (sizeof (*rmp));
5889   memset (rmp, 0, sizeof (*rmp));
5890   rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_MAP_DETAILS);
5891
5892   rmp->vni = clib_host_to_net_u32 (p->key);
5893   rmp->vrf = clib_host_to_net_u32 (p->value[0]);
5894   rmp->context = context;
5895   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5896 }
5897
5898 static void
5899 vl_api_lisp_eid_table_map_dump_t_handler (vl_api_lisp_eid_table_map_dump_t *
5900                                           mp)
5901 {
5902   unix_shared_memory_queue_t *q = NULL;
5903   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5904   hash_pair_t *p;
5905
5906   q = vl_api_client_index_to_input_queue (mp->client_index);
5907   if (q == 0)
5908     {
5909       return;
5910     }
5911   /* *INDENT-OFF* */
5912   hash_foreach_pair (p, lcm->table_id_by_vni,
5913   ({
5914     send_eid_table_map_pair (p, q, mp->context);
5915   }));
5916   /* *INDENT-ON* */
5917 }
5918
5919 static void
5920 vl_api_show_lisp_status_t_handler (vl_api_show_lisp_status_t * mp)
5921 {
5922   unix_shared_memory_queue_t *q = NULL;
5923   vl_api_show_lisp_status_reply_t *rmp = NULL;
5924   int rv = 0;
5925
5926   q = vl_api_client_index_to_input_queue (mp->client_index);
5927   if (q == 0)
5928     {
5929       return;
5930     }
5931
5932   /* *INDENT-OFF* */
5933   REPLY_MACRO2(VL_API_SHOW_LISP_STATUS_REPLY,
5934   ({
5935     rmp->gpe_status = vnet_lisp_gpe_enable_disable_status ();
5936     rmp->feature_status = vnet_lisp_enable_disable_status ();
5937   }));
5938   /* *INDENT-ON* */
5939 }
5940
5941 static void
5942   vl_api_lisp_get_map_request_itr_rlocs_t_handler
5943   (vl_api_lisp_get_map_request_itr_rlocs_t * mp)
5944 {
5945   unix_shared_memory_queue_t *q = NULL;
5946   vl_api_lisp_get_map_request_itr_rlocs_reply_t *rmp = NULL;
5947   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5948   locator_set_t *loc_set = 0;
5949   u8 *tmp_str = 0;
5950   int rv = 0;
5951
5952   q = vl_api_client_index_to_input_queue (mp->client_index);
5953   if (q == 0)
5954     {
5955       return;
5956     }
5957
5958   if (~0 == lcm->mreq_itr_rlocs)
5959     {
5960       tmp_str = format (0, " ");
5961     }
5962   else
5963     {
5964       loc_set =
5965         pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
5966       tmp_str = format (0, "%s", loc_set->name);
5967     }
5968
5969   /* *INDENT-OFF* */
5970   REPLY_MACRO2(VL_API_LISP_GET_MAP_REQUEST_ITR_RLOCS_REPLY,
5971   ({
5972     strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
5973             ARRAY_LEN(rmp->locator_set_name) - 1);
5974   }));
5975   /* *INDENT-ON* */
5976
5977   vec_free (tmp_str);
5978 }
5979
5980 static void
5981 vl_api_show_lisp_pitr_t_handler (vl_api_show_lisp_pitr_t * mp)
5982 {
5983   unix_shared_memory_queue_t *q = NULL;
5984   vl_api_show_lisp_pitr_reply_t *rmp = NULL;
5985   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5986   mapping_t *m;
5987   locator_set_t *ls = 0;
5988   u8 *tmp_str = 0;
5989   int rv = 0;
5990
5991   q = vl_api_client_index_to_input_queue (mp->client_index);
5992   if (q == 0)
5993     {
5994       return;
5995     }
5996
5997   if (!lcm->lisp_pitr)
5998     {
5999       tmp_str = format (0, "N/A");
6000     }
6001   else
6002     {
6003       m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
6004       if (~0 != m->locator_set_index)
6005         {
6006           ls =
6007             pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
6008           tmp_str = format (0, "%s", ls->name);
6009         }
6010       else
6011         {
6012           tmp_str = format (0, "N/A");
6013         }
6014     }
6015   vec_add1 (tmp_str, 0);
6016
6017   /* *INDENT-OFF* */
6018   REPLY_MACRO2(VL_API_SHOW_LISP_PITR_REPLY,
6019   ({
6020     rmp->status = lcm->lisp_pitr;
6021     strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
6022             ARRAY_LEN(rmp->locator_set_name) - 1);
6023   }));
6024   /* *INDENT-ON* */
6025 }
6026
6027 static void
6028 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
6029                                           mp)
6030 {
6031   vl_api_interface_name_renumber_reply_t *rmp;
6032   int rv = 0;
6033
6034   VALIDATE_SW_IF_INDEX (mp);
6035
6036   rv = vnet_interface_name_renumber
6037     (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
6038
6039   BAD_SW_IF_INDEX_LABEL;
6040
6041   REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
6042 }
6043
6044 static int
6045 arp_change_data_callback (u32 pool_index, u8 * new_mac,
6046                           u32 sw_if_index, u32 address)
6047 {
6048   vpe_api_main_t *am = &vpe_api_main;
6049   vlib_main_t *vm = am->vlib_main;
6050   vl_api_ip4_arp_event_t *event;
6051   static f64 arp_event_last_time;
6052   f64 now = vlib_time_now (vm);
6053
6054   if (pool_is_free_index (am->arp_events, pool_index))
6055     return 1;
6056
6057   event = pool_elt_at_index (am->arp_events, pool_index);
6058   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
6059     {
6060       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
6061     }
6062   else
6063     {                           /* same mac */
6064       if ((sw_if_index == event->sw_if_index) && ((address == 0) ||
6065                                                   /* for BD case, also check IP address with 10 sec timeout */
6066                                                   ((address == event->address)
6067                                                    &&
6068                                                    ((now -
6069                                                      arp_event_last_time) <
6070                                                     10.0))))
6071         return 1;
6072     }
6073
6074   arp_event_last_time = now;
6075   event->sw_if_index = sw_if_index;
6076   if (address)
6077     event->address = address;
6078   return 0;
6079 }
6080
6081 static int
6082 arp_change_delete_callback (u32 pool_index, u8 * notused)
6083 {
6084   vpe_api_main_t *am = &vpe_api_main;
6085
6086   if (pool_is_free_index (am->arp_events, pool_index))
6087     return 1;
6088
6089   pool_put_index (am->arp_events, pool_index);
6090   return 0;
6091 }
6092
6093 static void
6094 vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
6095 {
6096   vpe_api_main_t *am = &vpe_api_main;
6097   vnet_main_t *vnm = vnet_get_main ();
6098   vl_api_want_ip4_arp_events_reply_t *rmp;
6099   vl_api_ip4_arp_event_t *event;
6100   int rv;
6101
6102   if (mp->enable_disable)
6103     {
6104       pool_get (am->arp_events, event);
6105       memset (event, 0, sizeof (*event));
6106
6107       event->_vl_msg_id = ntohs (VL_API_IP4_ARP_EVENT);
6108       event->client_index = mp->client_index;
6109       event->context = mp->context;
6110       event->address = mp->address;
6111       event->pid = mp->pid;
6112
6113       rv = vnet_add_del_ip4_arp_change_event
6114         (vnm, arp_change_data_callback,
6115          mp->pid, &mp->address /* addr, in net byte order */ ,
6116          vpe_resolver_process_node.index,
6117          IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
6118     }
6119   else
6120     {
6121       rv = vnet_add_del_ip4_arp_change_event
6122         (vnm, arp_change_delete_callback,
6123          mp->pid, &mp->address /* addr, in net byte order */ ,
6124          vpe_resolver_process_node.index,
6125          IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
6126     }
6127   REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
6128 }
6129
6130 static void vl_api_input_acl_set_interface_t_handler
6131   (vl_api_input_acl_set_interface_t * mp)
6132 {
6133   vlib_main_t *vm = vlib_get_main ();
6134   vl_api_input_acl_set_interface_reply_t *rmp;
6135   int rv;
6136   u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
6137
6138   ip4_table_index = ntohl (mp->ip4_table_index);
6139   ip6_table_index = ntohl (mp->ip6_table_index);
6140   l2_table_index = ntohl (mp->l2_table_index);
6141   sw_if_index = ntohl (mp->sw_if_index);
6142
6143   VALIDATE_SW_IF_INDEX (mp);
6144
6145   rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
6146                                  ip6_table_index, l2_table_index, mp->is_add);
6147
6148   BAD_SW_IF_INDEX_LABEL;
6149
6150   REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
6151 }
6152
6153 static void vl_api_ipsec_spd_add_del_t_handler
6154   (vl_api_ipsec_spd_add_del_t * mp)
6155 {
6156 #if IPSEC == 0
6157   clib_warning ("unimplemented");
6158 #else
6159
6160   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6161   vl_api_ipsec_spd_add_del_reply_t *rmp;
6162   int rv;
6163
6164 #if DPDK > 0
6165   rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
6166 #else
6167   rv = VNET_API_ERROR_UNIMPLEMENTED;
6168 #endif
6169
6170   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
6171 #endif
6172 }
6173
6174 static void vl_api_ipsec_interface_add_del_spd_t_handler
6175   (vl_api_ipsec_interface_add_del_spd_t * mp)
6176 {
6177   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6178   vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
6179   int rv;
6180   u32 sw_if_index __attribute__ ((unused));
6181   u32 spd_id __attribute__ ((unused));
6182
6183   sw_if_index = ntohl (mp->sw_if_index);
6184   spd_id = ntohl (mp->spd_id);
6185
6186   VALIDATE_SW_IF_INDEX (mp);
6187
6188 #if IPSEC > 0
6189   rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
6190 #else
6191   rv = VNET_API_ERROR_UNIMPLEMENTED;
6192 #endif
6193
6194   BAD_SW_IF_INDEX_LABEL;
6195
6196   REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
6197 }
6198
6199 static void vl_api_ipsec_spd_add_del_entry_t_handler
6200   (vl_api_ipsec_spd_add_del_entry_t * mp)
6201 {
6202   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6203   vl_api_ipsec_spd_add_del_entry_reply_t *rmp;
6204   int rv;
6205
6206 #if IPSEC > 0
6207   ipsec_policy_t p;
6208
6209   memset (&p, 0, sizeof (p));
6210
6211   p.id = ntohl (mp->spd_id);
6212   p.priority = ntohl (mp->priority);
6213   p.is_outbound = mp->is_outbound;
6214   p.is_ipv6 = mp->is_ipv6;
6215
6216   if (mp->is_ipv6 || mp->is_ip_any)
6217     {
6218       clib_memcpy (&p.raddr.start, mp->remote_address_start, 16);
6219       clib_memcpy (&p.raddr.stop, mp->remote_address_stop, 16);
6220       clib_memcpy (&p.laddr.start, mp->local_address_start, 16);
6221       clib_memcpy (&p.laddr.stop, mp->local_address_stop, 16);
6222     }
6223   else
6224     {
6225       clib_memcpy (&p.raddr.start.ip4.data, mp->remote_address_start, 4);
6226       clib_memcpy (&p.raddr.stop.ip4.data, mp->remote_address_stop, 4);
6227       clib_memcpy (&p.laddr.start.ip4.data, mp->local_address_start, 4);
6228       clib_memcpy (&p.laddr.stop.ip4.data, mp->local_address_stop, 4);
6229     }
6230   p.protocol = mp->protocol;
6231   p.rport.start = ntohs (mp->remote_port_start);
6232   p.rport.stop = ntohs (mp->remote_port_stop);
6233   p.lport.start = ntohs (mp->local_port_start);
6234   p.lport.stop = ntohs (mp->local_port_stop);
6235   /* policy action resolve unsupported */
6236   if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE)
6237     {
6238       clib_warning ("unsupported action: 'resolve'");
6239       rv = VNET_API_ERROR_UNIMPLEMENTED;
6240       goto out;
6241     }
6242   p.policy = mp->policy;
6243   p.sa_id = ntohl (mp->sa_id);
6244
6245   rv = ipsec_add_del_policy (vm, &p, mp->is_add);
6246   if (rv)
6247     goto out;
6248
6249   if (mp->is_ip_any)
6250     {
6251       p.is_ipv6 = 1;
6252       rv = ipsec_add_del_policy (vm, &p, mp->is_add);
6253     }
6254 #else
6255   rv = VNET_API_ERROR_UNIMPLEMENTED;
6256   goto out;
6257 #endif
6258
6259 out:
6260   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
6261 }
6262
6263 static void vl_api_ipsec_sad_add_del_entry_t_handler
6264   (vl_api_ipsec_sad_add_del_entry_t * mp)
6265 {
6266   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6267   vl_api_ipsec_sad_add_del_entry_reply_t *rmp;
6268   int rv;
6269 #if IPSEC > 0
6270   ipsec_sa_t sa;
6271
6272   memset (&sa, 0, sizeof (sa));
6273
6274   sa.id = ntohl (mp->sad_id);
6275   sa.spi = ntohl (mp->spi);
6276   /* security protocol AH unsupported */
6277   if (mp->protocol == IPSEC_PROTOCOL_AH)
6278     {
6279       clib_warning ("unsupported security protocol 'AH'");
6280       rv = VNET_API_ERROR_UNIMPLEMENTED;
6281       goto out;
6282     }
6283   sa.protocol = mp->protocol;
6284   /* check for unsupported crypto-alg */
6285   if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
6286       mp->crypto_algorithm > IPSEC_CRYPTO_ALG_AES_CBC_256)
6287     {
6288       clib_warning ("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
6289                     mp->crypto_algorithm);
6290       rv = VNET_API_ERROR_UNIMPLEMENTED;
6291       goto out;
6292     }
6293   sa.crypto_alg = mp->crypto_algorithm;
6294   sa.crypto_key_len = mp->crypto_key_length;
6295   clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
6296   /* check for unsupported integ-alg */
6297   if (mp->integrity_algorithm < IPSEC_INTEG_ALG_SHA1_96 ||
6298       mp->integrity_algorithm > IPSEC_INTEG_ALG_SHA_512_256)
6299     {
6300       clib_warning ("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
6301                     mp->integrity_algorithm);
6302       rv = VNET_API_ERROR_UNIMPLEMENTED;
6303       goto out;
6304     }
6305   sa.integ_alg = mp->integrity_algorithm;
6306   sa.integ_key_len = mp->integrity_key_length;
6307   clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
6308   sa.use_esn = mp->use_extended_sequence_number;
6309   sa.is_tunnel = mp->is_tunnel;
6310   sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
6311   if (sa.is_tunnel_ip6)
6312     {
6313       clib_memcpy (&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
6314       clib_memcpy (&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
6315     }
6316   else
6317     {
6318       clib_memcpy (&sa.tunnel_src_addr.ip4.data, mp->tunnel_src_address, 4);
6319       clib_memcpy (&sa.tunnel_dst_addr.ip4.data, mp->tunnel_dst_address, 4);
6320     }
6321
6322   rv = ipsec_add_del_sa (vm, &sa, mp->is_add);
6323 #else
6324   rv = VNET_API_ERROR_UNIMPLEMENTED;
6325   goto out;
6326 #endif
6327
6328 out:
6329   REPLY_MACRO (VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
6330 }
6331
6332 static void
6333 vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
6334 {
6335   vl_api_ikev2_profile_add_del_reply_t *rmp;
6336   int rv = 0;
6337
6338 #if IPSEC > 0
6339   vlib_main_t *vm = vlib_get_main ();
6340   clib_error_t *error;
6341   u8 *tmp = format (0, "%s", mp->name);
6342   error = ikev2_add_del_profile (vm, tmp, mp->is_add);
6343   vec_free (tmp);
6344   if (error)
6345     rv = VNET_API_ERROR_UNSPECIFIED;
6346 #else
6347   rv = VNET_API_ERROR_UNIMPLEMENTED;
6348 #endif
6349
6350   REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
6351 }
6352
6353 static void
6354   vl_api_ikev2_profile_set_auth_t_handler
6355   (vl_api_ikev2_profile_set_auth_t * mp)
6356 {
6357   vl_api_ikev2_profile_set_auth_reply_t *rmp;
6358   int rv = 0;
6359
6360 #if IPSEC > 0
6361   vlib_main_t *vm = vlib_get_main ();
6362   clib_error_t *error;
6363   u8 *tmp = format (0, "%s", mp->name);
6364   u8 *data = vec_new (u8, mp->data_len);
6365   clib_memcpy (data, mp->data, mp->data_len);
6366   error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
6367   vec_free (tmp);
6368   vec_free (data);
6369   if (error)
6370     rv = VNET_API_ERROR_UNSPECIFIED;
6371 #else
6372   rv = VNET_API_ERROR_UNIMPLEMENTED;
6373 #endif
6374
6375   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
6376 }
6377
6378 static void
6379 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
6380 {
6381   vl_api_ikev2_profile_add_del_reply_t *rmp;
6382   int rv = 0;
6383
6384 #if IPSEC > 0
6385   vlib_main_t *vm = vlib_get_main ();
6386   clib_error_t *error;
6387   u8 *tmp = format (0, "%s", mp->name);
6388   u8 *data = vec_new (u8, mp->data_len);
6389   clib_memcpy (data, mp->data, mp->data_len);
6390   error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
6391   vec_free (tmp);
6392   vec_free (data);
6393   if (error)
6394     rv = VNET_API_ERROR_UNSPECIFIED;
6395 #else
6396   rv = VNET_API_ERROR_UNIMPLEMENTED;
6397 #endif
6398
6399   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
6400 }
6401
6402 static void
6403 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
6404 {
6405   vl_api_ikev2_profile_set_ts_reply_t *rmp;
6406   int rv = 0;
6407
6408 #if IPSEC > 0
6409   vlib_main_t *vm = vlib_get_main ();
6410   clib_error_t *error;
6411   u8 *tmp = format (0, "%s", mp->name);
6412   error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
6413                                 mp->end_port, (ip4_address_t) mp->start_addr,
6414                                 (ip4_address_t) mp->end_addr, mp->is_local);
6415   vec_free (tmp);
6416   if (error)
6417     rv = VNET_API_ERROR_UNSPECIFIED;
6418 #else
6419   rv = VNET_API_ERROR_UNIMPLEMENTED;
6420 #endif
6421
6422   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
6423 }
6424
6425 static void
6426 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
6427 {
6428   vl_api_ikev2_profile_set_ts_reply_t *rmp;
6429   int rv = 0;
6430
6431 #if IPSEC > 0
6432   vlib_main_t *vm = vlib_get_main ();
6433   clib_error_t *error;
6434
6435   error = ikev2_set_local_key (vm, mp->key_file);
6436   if (error)
6437     rv = VNET_API_ERROR_UNSPECIFIED;
6438 #else
6439   rv = VNET_API_ERROR_UNIMPLEMENTED;
6440 #endif
6441
6442   REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
6443 }
6444
6445 static void
6446 vl_api_map_add_domain_t_handler (vl_api_map_add_domain_t * mp)
6447 {
6448   vl_api_map_add_domain_reply_t *rmp;
6449   int rv = 0;
6450   u32 index;
6451   u8 flags = mp->is_translation ? MAP_DOMAIN_TRANSLATION : 0;
6452   rv =
6453     map_create_domain ((ip4_address_t *) & mp->ip4_prefix, mp->ip4_prefix_len,
6454                        (ip6_address_t *) & mp->ip6_prefix, mp->ip6_prefix_len,
6455                        (ip6_address_t *) & mp->ip6_src,
6456                        mp->ip6_src_prefix_len, mp->ea_bits_len,
6457                        mp->psid_offset, mp->psid_length, &index,
6458                        ntohs (mp->mtu), flags);
6459
6460   /* *INDENT-OFF* */
6461   REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
6462   ({
6463     rmp->index = ntohl(index);
6464   }));
6465   /* *INDENT-ON* */
6466 }
6467
6468 static void
6469 vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp)
6470 {
6471   vl_api_map_del_domain_reply_t *rmp;
6472   int rv = 0;
6473
6474   rv = map_delete_domain (ntohl (mp->index));
6475
6476   REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY);
6477 }
6478
6479 static void
6480 vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp)
6481 {
6482   vl_api_map_del_domain_reply_t *rmp;
6483   int rv = 0;
6484
6485   rv =
6486     map_add_del_psid (ntohl (mp->index), ntohs (mp->psid),
6487                       (ip6_address_t *) mp->ip6_dst, mp->is_add);
6488
6489   REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY);
6490 }
6491
6492 static void
6493 vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
6494 {
6495   vl_api_map_domain_details_t *rmp;
6496   map_main_t *mm = &map_main;
6497   map_domain_t *d;
6498   unix_shared_memory_queue_t *q;
6499
6500   if (pool_elts (mm->domains) == 0)
6501     return;
6502
6503   q = vl_api_client_index_to_input_queue (mp->client_index);
6504   if (q == 0)
6505     {
6506       return;
6507     }
6508
6509   /* *INDENT-OFF* */
6510   pool_foreach(d, mm->domains,
6511   ({
6512     /* Make sure every field is initiated (or don't skip the memset()) */
6513     rmp = vl_msg_api_alloc (sizeof (*rmp));
6514     rmp->_vl_msg_id = ntohs(VL_API_MAP_DOMAIN_DETAILS);
6515     rmp->domain_index = htonl(d - mm->domains);
6516     rmp->ea_bits_len = d->ea_bits_len;
6517     rmp->psid_offset = d->psid_offset;
6518     rmp->psid_length = d->psid_length;
6519     clib_memcpy(rmp->ip4_prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix));
6520     rmp->ip4_prefix_len = d->ip4_prefix_len;
6521     clib_memcpy(rmp->ip6_prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix));
6522     rmp->ip6_prefix_len = d->ip6_prefix_len;
6523     clib_memcpy(rmp->ip6_src, &d->ip6_src, sizeof(rmp->ip6_src));
6524     rmp->ip6_src_len = d->ip6_src_len;
6525     rmp->mtu = htons(d->mtu);
6526     rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION);
6527     rmp->context = mp->context;
6528
6529     vl_msg_api_send_shmem (q, (u8 *)&rmp);
6530   }));
6531   /* *INDENT-ON* */
6532 }
6533
6534 static void
6535 vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
6536 {
6537   unix_shared_memory_queue_t *q;
6538   u16 i;
6539   ip6_address_t dst;
6540   vl_api_map_rule_details_t *rmp;
6541   map_main_t *mm = &map_main;
6542   u32 domain_index = ntohl (mp->domain_index);
6543   map_domain_t *d;
6544
6545   if (pool_elts (mm->domains) == 0)
6546     return;
6547
6548   d = pool_elt_at_index (mm->domains, domain_index);
6549   if (!d || !d->rules)
6550     {
6551       return;
6552     }
6553
6554   q = vl_api_client_index_to_input_queue (mp->client_index);
6555   if (q == 0)
6556     {
6557       return;
6558     }
6559
6560   for (i = 0; i < (0x1 << d->psid_length); i++)
6561     {
6562       dst = d->rules[i];
6563       if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
6564         {
6565           continue;
6566         }
6567       rmp = vl_msg_api_alloc (sizeof (*rmp));
6568       memset (rmp, 0, sizeof (*rmp));
6569       rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS);
6570       rmp->psid = htons (i);
6571       clib_memcpy (rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
6572       rmp->context = mp->context;
6573       vl_msg_api_send_shmem (q, (u8 *) & rmp);
6574     }
6575 }
6576
6577 static void
6578 vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
6579 {
6580   vl_api_map_summary_stats_reply_t *rmp;
6581   vlib_combined_counter_main_t *cm;
6582   vlib_counter_t v;
6583   int i, which;
6584   u64 total_pkts[VLIB_N_RX_TX];
6585   u64 total_bytes[VLIB_N_RX_TX];
6586   map_main_t *mm = &map_main;
6587   unix_shared_memory_queue_t *q =
6588     vl_api_client_index_to_input_queue (mp->client_index);
6589
6590   if (!q)
6591     return;
6592
6593   rmp = vl_msg_api_alloc (sizeof (*rmp));
6594   rmp->_vl_msg_id = ntohs (VL_API_MAP_SUMMARY_STATS_REPLY);
6595   rmp->context = mp->context;
6596   rmp->retval = 0;
6597
6598   memset (total_pkts, 0, sizeof (total_pkts));
6599   memset (total_bytes, 0, sizeof (total_bytes));
6600
6601   map_domain_counter_lock (mm);
6602   vec_foreach (cm, mm->domain_counters)
6603   {
6604     which = cm - mm->domain_counters;
6605
6606     for (i = 0; i < vec_len (cm->maxi); i++)
6607       {
6608         vlib_get_combined_counter (cm, i, &v);
6609         total_pkts[which] += v.packets;
6610         total_bytes[which] += v.bytes;
6611       }
6612   }
6613
6614   map_domain_counter_unlock (mm);
6615
6616   /* Note: in network byte order! */
6617   rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] =
6618     clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]);
6619   rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] =
6620     clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]);
6621   rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] =
6622     clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]);
6623   rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] =
6624     clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]);
6625   rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains));
6626   rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
6627   rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] =
6628     clib_host_to_net_u64 (map_error_counter_get
6629                           (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
6630   rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] =
6631     clib_host_to_net_u64 (map_error_counter_get
6632                           (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
6633
6634   vl_msg_api_send_shmem (q, (u8 *) & rmp);
6635 }
6636
6637 static void
6638 vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
6639 {
6640   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6641   vl_api_ipsec_sa_set_key_reply_t *rmp;
6642   int rv;
6643 #if IPSEC > 0
6644   ipsec_sa_t sa;
6645   sa.id = ntohl (mp->sa_id);
6646   sa.crypto_key_len = mp->crypto_key_length;
6647   clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
6648   sa.integ_key_len = mp->integrity_key_length;
6649   clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
6650
6651   rv = ipsec_set_sa_key (vm, &sa);
6652 #else
6653   rv = VNET_API_ERROR_UNIMPLEMENTED;
6654 #endif
6655
6656   REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
6657 }
6658
6659 static void vl_api_cop_interface_enable_disable_t_handler
6660   (vl_api_cop_interface_enable_disable_t * mp)
6661 {
6662   vl_api_cop_interface_enable_disable_reply_t *rmp;
6663   int rv;
6664   u32 sw_if_index = ntohl (mp->sw_if_index);
6665   int enable_disable;
6666
6667   VALIDATE_SW_IF_INDEX (mp);
6668
6669   enable_disable = (int) mp->enable_disable;
6670
6671   rv = cop_interface_enable_disable (sw_if_index, enable_disable);
6672
6673   BAD_SW_IF_INDEX_LABEL;
6674
6675   REPLY_MACRO (VL_API_COP_INTERFACE_ENABLE_DISABLE_REPLY);
6676 }
6677
6678 static void vl_api_cop_whitelist_enable_disable_t_handler
6679   (vl_api_cop_whitelist_enable_disable_t * mp)
6680 {
6681   vl_api_cop_whitelist_enable_disable_reply_t *rmp;
6682   cop_whitelist_enable_disable_args_t _a, *a = &_a;
6683   u32 sw_if_index = ntohl (mp->sw_if_index);
6684   int rv;
6685
6686   VALIDATE_SW_IF_INDEX (mp);
6687
6688   a->sw_if_index = sw_if_index;
6689   a->ip4 = mp->ip4;
6690   a->ip6 = mp->ip6;
6691   a->default_cop = mp->default_cop;
6692   a->fib_id = ntohl (mp->fib_id);
6693
6694   rv = cop_whitelist_enable_disable (a);
6695
6696   BAD_SW_IF_INDEX_LABEL;
6697
6698   REPLY_MACRO (VL_API_COP_WHITELIST_ENABLE_DISABLE_REPLY);
6699 }
6700
6701 static void
6702 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
6703 {
6704   int rv = 0;
6705   u8 *vector = 0;
6706   api_main_t *am = &api_main;
6707   vlib_main_t *vm = vlib_get_main ();
6708   void *oldheap;
6709   vl_api_get_node_graph_reply_t *rmp;
6710
6711   pthread_mutex_lock (&am->vlib_rp->mutex);
6712   oldheap = svm_push_data_heap (am->vlib_rp);
6713
6714   /*
6715    * Keep the number of memcpy ops to a minimum (e.g. 1).
6716    */
6717   vec_validate (vector, 16384);
6718   vec_reset_length (vector);
6719
6720   /* $$$$ FIXME */
6721   vector = vlib_node_serialize (&vm->node_main, vector,
6722                                 (u32) ~ 0 /* all threads */ ,
6723                                 1 /* include nexts */ ,
6724                                 1 /* include stats */ );
6725
6726   svm_pop_heap (oldheap);
6727   pthread_mutex_unlock (&am->vlib_rp->mutex);
6728
6729   /* *INDENT-OFF* */
6730   REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
6731   ({
6732     rmp->reply_in_shmem = (uword) vector;
6733   }));
6734   /* *INDENT-ON* */
6735 }
6736
6737 static void vl_api_trace_profile_add_t_handler
6738   (vl_api_trace_profile_add_t * mp)
6739 {
6740   int rv = 0;
6741   vl_api_trace_profile_add_reply_t *rmp;
6742   clib_error_t *error;
6743
6744   /* Ignoring the profile id as currently a single profile
6745    * is supported */
6746   error = ip6_ioam_trace_profile_set (mp->trace_num_elt, mp->trace_type,
6747                                       ntohl (mp->node_id),
6748                                       ntohl (mp->trace_app_data),
6749                                       mp->pow_enable, mp->trace_tsp,
6750                                       mp->trace_ppc);
6751   if (error)
6752     {
6753       clib_error_report (error);
6754       rv = clib_error_get_code (error);
6755     }
6756
6757   REPLY_MACRO (VL_API_TRACE_PROFILE_ADD_REPLY);
6758 }
6759
6760 static void vl_api_trace_profile_apply_t_handler
6761   (vl_api_trace_profile_apply_t * mp)
6762 {
6763   int rv = 0;
6764   vl_api_trace_profile_apply_reply_t *rmp;
6765
6766   if (mp->enable != 0)
6767     {
6768       rv = ip6_ioam_set_destination ((ip6_address_t *) (&mp->dest_ipv6),
6769                                      ntohl (mp->prefix_length),
6770                                      ntohl (mp->vrf_id),
6771                                      mp->trace_op == IOAM_HBYH_ADD,
6772                                      mp->trace_op == IOAM_HBYH_POP,
6773                                      mp->trace_op == IOAM_HBYH_MOD);
6774     }
6775   else
6776     {
6777       //ip6_ioam_clear_destination(&ip6, mp->prefix_length, mp->vrf_id);
6778     }
6779   REPLY_MACRO (VL_API_TRACE_PROFILE_APPLY_REPLY);
6780 }
6781
6782 static void vl_api_trace_profile_del_t_handler
6783   (vl_api_trace_profile_del_t * mp)
6784 {
6785   int rv = 0;
6786   vl_api_trace_profile_del_reply_t *rmp;
6787   clib_error_t *error;
6788
6789   error = clear_ioam_rewrite_fn ();
6790   if (error)
6791     {
6792       clib_error_report (error);
6793       rv = clib_error_get_code (error);
6794     }
6795
6796   REPLY_MACRO (VL_API_TRACE_PROFILE_DEL_REPLY);
6797 }
6798
6799 static void
6800 vl_api_af_packet_create_t_handler (vl_api_af_packet_create_t * mp)
6801 {
6802   vlib_main_t *vm = vlib_get_main ();
6803   vl_api_af_packet_create_reply_t *rmp;
6804   int rv = 0;
6805   u8 *host_if_name = NULL;
6806   u32 sw_if_index;
6807
6808   host_if_name = format (0, "%s", mp->host_if_name);
6809   vec_add1 (host_if_name, 0);
6810
6811   rv = af_packet_create_if (vm, host_if_name,
6812                             mp->use_random_hw_addr ? 0 : mp->hw_addr,
6813                             &sw_if_index);
6814
6815   vec_free (host_if_name);
6816
6817   /* *INDENT-OFF* */
6818   REPLY_MACRO2(VL_API_AF_PACKET_CREATE_REPLY,
6819   ({
6820     rmp->sw_if_index = clib_host_to_net_u32(sw_if_index);
6821   }));
6822   /* *INDENT-ON* */
6823 }
6824
6825 static void
6826 vl_api_af_packet_delete_t_handler (vl_api_af_packet_delete_t * mp)
6827 {
6828   vlib_main_t *vm = vlib_get_main ();
6829   vl_api_af_packet_delete_reply_t *rmp;
6830   int rv = 0;
6831   u8 *host_if_name = NULL;
6832
6833   host_if_name = format (0, "%s", mp->host_if_name);
6834   vec_add1 (host_if_name, 0);
6835
6836   rv = af_packet_delete_if (vm, host_if_name);
6837
6838   vec_free (host_if_name);
6839
6840   REPLY_MACRO (VL_API_AF_PACKET_DELETE_REPLY);
6841 }
6842
6843 static void
6844 vl_api_policer_add_del_t_handler (vl_api_policer_add_del_t * mp)
6845 {
6846   vlib_main_t *vm = vlib_get_main ();
6847   vl_api_policer_add_del_reply_t *rmp;
6848   int rv = 0;
6849   u8 *name = NULL;
6850   sse2_qos_pol_cfg_params_st cfg;
6851   clib_error_t *error;
6852   u32 policer_index;
6853
6854   name = format (0, "%s", mp->name);
6855
6856   memset (&cfg, 0, sizeof (cfg));
6857   cfg.rfc = mp->type;
6858   cfg.rnd_type = mp->round_type;
6859   cfg.rate_type = mp->rate_type;
6860   cfg.rb.kbps.cir_kbps = mp->cir;
6861   cfg.rb.kbps.eir_kbps = mp->eir;
6862   cfg.rb.kbps.cb_bytes = mp->cb;
6863   cfg.rb.kbps.eb_bytes = mp->eb;
6864   cfg.conform_action.action_type = mp->conform_action_type;
6865   cfg.conform_action.dscp = mp->conform_dscp;
6866   cfg.exceed_action.action_type = mp->exceed_action_type;
6867   cfg.exceed_action.dscp = mp->exceed_dscp;
6868   cfg.violate_action.action_type = mp->violate_action_type;
6869   cfg.violate_action.dscp = mp->violate_dscp;
6870   cfg.color_aware = mp->color_aware;
6871
6872   error = policer_add_del (vm, name, &cfg, &policer_index, mp->is_add);
6873
6874   if (error)
6875     rv = VNET_API_ERROR_UNSPECIFIED;
6876
6877   /* *INDENT-OFF* */
6878   REPLY_MACRO2(VL_API_POLICER_ADD_DEL_REPLY,
6879   ({
6880     if (rv == 0 &&  mp->is_add)
6881       rmp->policer_index = ntohl(policer_index);
6882     else
6883       rmp->policer_index = ~0;
6884   }));
6885   /* *INDENT-ON* */
6886 }
6887
6888 static void
6889 send_policer_details (u8 * name,
6890                       sse2_qos_pol_cfg_params_st * config,
6891                       policer_read_response_type_st * templ,
6892                       unix_shared_memory_queue_t * q, u32 context)
6893 {
6894   vl_api_policer_details_t *mp;
6895
6896   mp = vl_msg_api_alloc (sizeof (*mp));
6897   memset (mp, 0, sizeof (*mp));
6898   mp->_vl_msg_id = ntohs (VL_API_POLICER_DETAILS);
6899   mp->context = context;
6900   mp->cir = htonl (config->rb.kbps.cir_kbps);
6901   mp->eir = htonl (config->rb.kbps.eir_kbps);
6902   mp->cb = htonl (config->rb.kbps.cb_bytes);
6903   mp->eb = htonl (config->rb.kbps.eb_bytes);
6904   mp->rate_type = config->rate_type;
6905   mp->round_type = config->rnd_type;
6906   mp->type = config->rfc;
6907   mp->conform_action_type = config->conform_action.action_type;
6908   mp->conform_dscp = config->conform_action.dscp;
6909   mp->exceed_action_type = config->exceed_action.action_type;
6910   mp->exceed_dscp = config->exceed_action.dscp;
6911   mp->violate_action_type = config->violate_action.action_type;
6912   mp->violate_dscp = config->violate_action.dscp;
6913   mp->single_rate = templ->single_rate ? 1 : 0;
6914   mp->color_aware = templ->color_aware ? 1 : 0;
6915   mp->scale = htonl (templ->scale);
6916   mp->cir_tokens_per_period = htonl (templ->cir_tokens_per_period);
6917   mp->pir_tokens_per_period = htonl (templ->pir_tokens_per_period);
6918   mp->current_limit = htonl (templ->current_limit);
6919   mp->current_bucket = htonl (templ->current_bucket);
6920   mp->extended_limit = htonl (templ->extended_limit);
6921   mp->extended_bucket = htonl (templ->extended_bucket);
6922   mp->last_update_time = clib_host_to_net_u64 (templ->last_update_time);
6923
6924   strncpy ((char *) mp->name, (char *) name, ARRAY_LEN (mp->name) - 1);
6925
6926   vl_msg_api_send_shmem (q, (u8 *) & mp);
6927 }
6928
6929 static void
6930 vl_api_policer_dump_t_handler (vl_api_policer_dump_t * mp)
6931 {
6932   unix_shared_memory_queue_t *q;
6933   vnet_policer_main_t *pm = &vnet_policer_main;
6934   hash_pair_t *hp;
6935   uword *p;
6936   u32 pool_index;
6937   u8 *match_name = 0;
6938   u8 *name;
6939   sse2_qos_pol_cfg_params_st *config;
6940   policer_read_response_type_st *templ;
6941
6942   q = vl_api_client_index_to_input_queue (mp->client_index);
6943   if (q == 0)
6944     return;
6945
6946   if (mp->match_name_valid)
6947     {
6948       match_name = format (0, "%s%c", mp->match_name, 0);
6949     }
6950
6951   if (mp->match_name_valid)
6952     {
6953       p = hash_get_mem (pm->policer_config_by_name, match_name);
6954       if (p)
6955         {
6956           pool_index = p[0];
6957           config = pool_elt_at_index (pm->configs, pool_index);
6958           templ = pool_elt_at_index (pm->policer_templates, pool_index);
6959           send_policer_details (match_name, config, templ, q, mp->context);
6960         }
6961     }
6962   else
6963     {
6964       /* *INDENT-OFF* */
6965       hash_foreach_pair (hp, pm->policer_config_by_name,
6966       ({
6967         name = (u8 *) hp->key;
6968         pool_index = hp->value[0];
6969         config = pool_elt_at_index (pm->configs, pool_index);
6970         templ = pool_elt_at_index (pm->policer_templates, pool_index);
6971         send_policer_details(name, config, templ, q, mp->context);
6972       }));
6973       /* *INDENT-ON* */
6974     }
6975 }
6976
6977 static void
6978   vl_api_policer_classify_set_interface_t_handler
6979   (vl_api_policer_classify_set_interface_t * mp)
6980 {
6981   vlib_main_t *vm = vlib_get_main ();
6982   vl_api_policer_classify_set_interface_reply_t *rmp;
6983   int rv;
6984   u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
6985
6986   ip4_table_index = ntohl (mp->ip4_table_index);
6987   ip6_table_index = ntohl (mp->ip6_table_index);
6988   l2_table_index = ntohl (mp->l2_table_index);
6989   sw_if_index = ntohl (mp->sw_if_index);
6990
6991   VALIDATE_SW_IF_INDEX (mp);
6992
6993   rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index,
6994                                         ip6_table_index, l2_table_index,
6995                                         mp->is_add);
6996
6997   BAD_SW_IF_INDEX_LABEL;
6998
6999   REPLY_MACRO (VL_API_POLICER_CLASSIFY_SET_INTERFACE_REPLY);
7000 }
7001
7002 static void
7003 send_policer_classify_details (u32 sw_if_index,
7004                                u32 table_index,
7005                                unix_shared_memory_queue_t * q, u32 context)
7006 {
7007   vl_api_policer_classify_details_t *mp;
7008
7009   mp = vl_msg_api_alloc (sizeof (*mp));
7010   memset (mp, 0, sizeof (*mp));
7011   mp->_vl_msg_id = ntohs (VL_API_POLICER_CLASSIFY_DETAILS);
7012   mp->context = context;
7013   mp->sw_if_index = htonl (sw_if_index);
7014   mp->table_index = htonl (table_index);
7015
7016   vl_msg_api_send_shmem (q, (u8 *) & mp);
7017 }
7018
7019 static void
7020 vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
7021 {
7022   unix_shared_memory_queue_t *q;
7023   policer_classify_main_t *pcm = &policer_classify_main;
7024   u32 *vec_tbl;
7025   int i;
7026
7027   q = vl_api_client_index_to_input_queue (mp->client_index);
7028   if (q == 0)
7029     return;
7030
7031   vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
7032
7033   if (vec_len (vec_tbl))
7034     {
7035       for (i = 0; i < vec_len (vec_tbl); i++)
7036         {
7037           if (vec_elt (vec_tbl, i) == ~0)
7038             continue;
7039
7040           send_policer_classify_details (i, vec_elt (vec_tbl, i), q,
7041                                          mp->context);
7042         }
7043     }
7044 }
7045
7046 static void
7047 vl_api_netmap_create_t_handler (vl_api_netmap_create_t * mp)
7048 {
7049   vlib_main_t *vm = vlib_get_main ();
7050   vl_api_netmap_create_reply_t *rmp;
7051   int rv = 0;
7052   u8 *if_name = NULL;
7053
7054   if_name = format (0, "%s", mp->netmap_if_name);
7055   vec_add1 (if_name, 0);
7056
7057   rv =
7058     netmap_create_if (vm, if_name, mp->use_random_hw_addr ? 0 : mp->hw_addr,
7059                       mp->is_pipe, mp->is_master, 0);
7060
7061   vec_free (if_name);
7062
7063   REPLY_MACRO (VL_API_NETMAP_CREATE_REPLY);
7064 }
7065
7066 static void
7067 vl_api_netmap_delete_t_handler (vl_api_netmap_delete_t * mp)
7068 {
7069   vlib_main_t *vm = vlib_get_main ();
7070   vl_api_netmap_delete_reply_t *rmp;
7071   int rv = 0;
7072   u8 *if_name = NULL;
7073
7074   if_name = format (0, "%s", mp->netmap_if_name);
7075   vec_add1 (if_name, 0);
7076
7077   rv = netmap_delete_if (vm, if_name);
7078
7079   vec_free (if_name);
7080
7081   REPLY_MACRO (VL_API_NETMAP_DELETE_REPLY);
7082 }
7083
7084 static void
7085 vl_api_mpls_gre_tunnel_details_t_handler (vl_api_mpls_gre_tunnel_details_t *
7086                                           mp)
7087 {
7088   clib_warning ("BUG");
7089 }
7090
7091 static void
7092 send_mpls_gre_tunnel_entry (vpe_api_main_t * am,
7093                             unix_shared_memory_queue_t * q,
7094                             mpls_gre_tunnel_t * gt, u32 index, u32 context)
7095 {
7096   vl_api_mpls_gre_tunnel_details_t *mp;
7097
7098   mp = vl_msg_api_alloc (sizeof (*mp));
7099   memset (mp, 0, sizeof (*mp));
7100   mp->_vl_msg_id = ntohs (VL_API_MPLS_GRE_TUNNEL_DETAILS);
7101   mp->context = context;
7102
7103   if (gt != NULL)
7104     {
7105       mp->tunnel_index = htonl (index);
7106       mp->tunnel_src = gt->tunnel_src.as_u32;
7107       mp->tunnel_dst = gt->tunnel_dst.as_u32;
7108       mp->intfc_address = gt->intfc_address.as_u32;
7109       mp->mask_width = htonl (gt->mask_width);
7110       mp->inner_fib_index = htonl (gt->inner_fib_index);
7111       mp->outer_fib_index = htonl (gt->outer_fib_index);
7112       mp->encap_index = htonl (gt->encap_index);
7113       mp->hw_if_index = htonl (gt->hw_if_index);
7114       mp->l2_only = htonl (gt->l2_only);
7115     }
7116
7117   mpls_main_t *mm = &mpls_main;
7118   mpls_encap_t *e;
7119   int i;
7120   u32 len = 0;
7121
7122   e = pool_elt_at_index (mm->encaps, gt->encap_index);
7123   len = vec_len (e->labels);
7124   mp->nlabels = htonl (len);
7125
7126   for (i = 0; i < len; i++)
7127     {
7128       mp->labels[i] =
7129         htonl (vnet_mpls_uc_get_label
7130                (clib_host_to_net_u32 (e->labels[i].label_exp_s_ttl)));
7131     }
7132
7133   vl_msg_api_send_shmem (q, (u8 *) & mp);
7134 }
7135
7136 static void
7137 vl_api_mpls_gre_tunnel_dump_t_handler (vl_api_mpls_gre_tunnel_dump_t * mp)
7138 {
7139   vpe_api_main_t *am = &vpe_api_main;
7140   unix_shared_memory_queue_t *q;
7141   vlib_main_t *vm = &vlib_global_main;
7142   mpls_main_t *mm = &mpls_main;
7143   mpls_gre_tunnel_t *gt;
7144   u32 index = ntohl (mp->tunnel_index);
7145
7146   q = vl_api_client_index_to_input_queue (mp->client_index);
7147   if (q == 0)
7148     return;
7149
7150   if (pool_elts (mm->gre_tunnels))
7151     {
7152       if (mp->tunnel_index >= 0)
7153         {
7154           vlib_cli_output (vm, "MPLS-GRE tunnel %u", index);
7155           gt = pool_elt_at_index (mm->gre_tunnels, index);
7156           send_mpls_gre_tunnel_entry (am, q, gt, gt - mm->gre_tunnels,
7157                                       mp->context);
7158         }
7159       else
7160         {
7161           vlib_cli_output (vm, "MPLS-GRE tunnels");
7162           /* *INDENT-OFF* */
7163           pool_foreach (gt, mm->gre_tunnels,
7164           ({
7165             send_mpls_gre_tunnel_entry (am, q, gt, gt - mm->gre_tunnels,
7166                                         mp->context);
7167           }));
7168           /* *INDENT-ON* */
7169         }
7170     }
7171   else
7172     {
7173       vlib_cli_output (vm, "No MPLS-GRE tunnels");
7174     }
7175 }
7176
7177 static void
7178 vl_api_mpls_eth_tunnel_details_t_handler (vl_api_mpls_eth_tunnel_details_t *
7179                                           mp)
7180 {
7181   clib_warning ("BUG");
7182 }
7183
7184 static void
7185 send_mpls_eth_tunnel_entry (vpe_api_main_t * am,
7186                             unix_shared_memory_queue_t * q,
7187                             mpls_eth_tunnel_t * et, u32 index, u32 context)
7188 {
7189   vl_api_mpls_eth_tunnel_details_t *mp;
7190
7191   mp = vl_msg_api_alloc (sizeof (*mp));
7192   memset (mp, 0, sizeof (*mp));
7193   mp->_vl_msg_id = ntohs (VL_API_MPLS_ETH_TUNNEL_DETAILS);
7194   mp->context = context;
7195
7196   if (et != NULL)
7197     {
7198       mp->tunnel_index = htonl (index);
7199       memcpy (mp->tunnel_dst_mac, et->tunnel_dst, 6);
7200       mp->intfc_address = et->intfc_address.as_u32;
7201       mp->tx_sw_if_index = htonl (et->tx_sw_if_index);
7202       mp->inner_fib_index = htonl (et->inner_fib_index);
7203       mp->mask_width = htonl (et->mask_width);
7204       mp->encap_index = htonl (et->encap_index);
7205       mp->hw_if_index = htonl (et->hw_if_index);
7206       mp->l2_only = htonl (et->l2_only);
7207     }
7208
7209   mpls_main_t *mm = &mpls_main;
7210   mpls_encap_t *e;
7211   int i;
7212   u32 len = 0;
7213
7214   e = pool_elt_at_index (mm->encaps, et->encap_index);
7215   len = vec_len (e->labels);
7216   mp->nlabels = htonl (len);
7217
7218   for (i = 0; i < len; i++)
7219     {
7220       mp->labels[i] =
7221         htonl (vnet_mpls_uc_get_label
7222                (clib_host_to_net_u32 (e->labels[i].label_exp_s_ttl)));
7223     }
7224
7225   vl_msg_api_send_shmem (q, (u8 *) & mp);
7226 }
7227
7228 static void
7229 vl_api_mpls_eth_tunnel_dump_t_handler (vl_api_mpls_eth_tunnel_dump_t * mp)
7230 {
7231   vpe_api_main_t *am = &vpe_api_main;
7232   unix_shared_memory_queue_t *q;
7233   vlib_main_t *vm = &vlib_global_main;
7234   mpls_main_t *mm = &mpls_main;
7235   mpls_eth_tunnel_t *et;
7236   u32 index = ntohl (mp->tunnel_index);
7237
7238   q = vl_api_client_index_to_input_queue (mp->client_index);
7239   if (q == 0)
7240     return;
7241
7242   clib_warning ("Received mpls_eth_tunnel_dump");
7243   clib_warning ("Received tunnel index: %u from client %u", index,
7244                 mp->client_index);
7245
7246   if (pool_elts (mm->eth_tunnels))
7247     {
7248       if (mp->tunnel_index >= 0)
7249         {
7250           vlib_cli_output (vm, "MPLS-Ethernet tunnel %u", index);
7251           et = pool_elt_at_index (mm->eth_tunnels, index);
7252           send_mpls_eth_tunnel_entry (am, q, et, et - mm->eth_tunnels,
7253                                       mp->context);
7254         }
7255       else
7256         {
7257           clib_warning ("MPLS-Ethernet tunnels");
7258           /* *INDENT-OFF* */
7259           pool_foreach (et, mm->eth_tunnels,
7260           ({
7261             send_mpls_eth_tunnel_entry (am, q, et, et - mm->eth_tunnels,
7262                                         mp->context);
7263           }));
7264           /* *INDENT-ON* */
7265         }
7266     }
7267   else
7268     {
7269       clib_warning ("No MPLS-Ethernet tunnels");
7270     }
7271 }
7272
7273 static void
7274 vl_api_mpls_fib_encap_details_t_handler (vl_api_mpls_fib_encap_details_t * mp)
7275 {
7276   clib_warning ("BUG");
7277 }
7278
7279 static void
7280 send_mpls_fib_encap_details (vpe_api_main_t * am,
7281                              unix_shared_memory_queue_t * q,
7282                              show_mpls_fib_t * s, u32 context)
7283 {
7284   vl_api_mpls_fib_encap_details_t *mp;
7285
7286   mp = vl_msg_api_alloc (sizeof (*mp));
7287   memset (mp, 0, sizeof (*mp));
7288   mp->_vl_msg_id = ntohs (VL_API_MPLS_FIB_ENCAP_DETAILS);
7289   mp->context = context;
7290
7291   mp->fib_index = htonl (s->fib_index);
7292   mp->entry_index = htonl (s->entry_index);
7293   mp->dest = s->dest;
7294   mp->s_bit = htonl (s->s_bit);
7295
7296   mpls_main_t *mm = &mpls_main;
7297   mpls_encap_t *e;
7298   int i;
7299   u32 len = 0;
7300
7301   e = pool_elt_at_index (mm->encaps, s->entry_index);
7302   len = vec_len (e->labels);
7303   mp->nlabels = htonl (len);
7304
7305   for (i = 0; i < len; i++)
7306     {
7307       mp->labels[i] =
7308         htonl (vnet_mpls_uc_get_label
7309                (clib_host_to_net_u32 (e->labels[i].label_exp_s_ttl)));
7310     }
7311
7312   vl_msg_api_send_shmem (q, (u8 *) & mp);
7313 }
7314
7315 static void
7316 vl_api_mpls_fib_encap_dump_t_handler (vl_api_mpls_fib_encap_dump_t * mp)
7317 {
7318   vpe_api_main_t *am = &vpe_api_main;
7319   unix_shared_memory_queue_t *q;
7320   vlib_main_t *vm = &vlib_global_main;
7321   u64 key;
7322   u32 value;
7323   show_mpls_fib_t *records = 0;
7324   show_mpls_fib_t *s;
7325   mpls_main_t *mm = &mpls_main;
7326   ip4_main_t *im = &ip4_main;
7327   ip4_fib_t *rx_fib;
7328
7329   q = vl_api_client_index_to_input_queue (mp->client_index);
7330   if (q == 0)
7331     return;
7332
7333   /* *INDENT-OFF* */
7334   hash_foreach (key, value, mm->mpls_encap_by_fib_and_dest,
7335   ({
7336     vec_add2 (records, s, 1);
7337     s->fib_index = (u32)(key>>32);
7338     s->dest = (u32)(key & 0xFFFFFFFF);
7339     s->entry_index = (u32) value;
7340   }));
7341   /* *INDENT-ON* */
7342
7343   if (0 == vec_len (records))
7344     {
7345       vlib_cli_output (vm, "MPLS encap table empty");
7346       goto out;
7347     }
7348
7349   /* sort output by dst address within fib */
7350   vec_sort_with_function (records, mpls_dest_cmp);
7351   vec_sort_with_function (records, mpls_fib_index_cmp);
7352   vlib_cli_output (vm, "MPLS encap table");
7353   vlib_cli_output (vm, "%=6s%=16s%=16s", "Table", "Dest address", "Labels");
7354   vec_foreach (s, records)
7355   {
7356     rx_fib = vec_elt_at_index (im->fibs, s->fib_index);
7357     vlib_cli_output (vm, "%=6d%=16U%=16U", rx_fib->table_id,
7358                      format_ip4_address, &s->dest, format_mpls_encap_index,
7359                      mm, s->entry_index);
7360     send_mpls_fib_encap_details (am, q, s, mp->context);
7361   }
7362
7363 out:
7364   vec_free (records);
7365 }
7366
7367 static void
7368 vl_api_mpls_fib_decap_details_t_handler (vl_api_mpls_fib_decap_details_t * mp)
7369 {
7370   clib_warning ("BUG");
7371 }
7372
7373 static void
7374 send_mpls_fib_decap_details (vpe_api_main_t * am,
7375                              unix_shared_memory_queue_t * q,
7376                              show_mpls_fib_t * s,
7377                              u32 rx_table_id,
7378                              u32 tx_table_id, char *swif_tag, u32 context)
7379 {
7380   vl_api_mpls_fib_decap_details_t *mp;
7381
7382   mp = vl_msg_api_alloc (sizeof (*mp));
7383   memset (mp, 0, sizeof (*mp));
7384   mp->_vl_msg_id = ntohs (VL_API_MPLS_FIB_DECAP_DETAILS);
7385   mp->context = context;
7386
7387   mp->fib_index = htonl (s->fib_index);
7388   mp->entry_index = htonl (s->entry_index);
7389   mp->dest = s->dest;
7390   mp->s_bit = htonl (s->s_bit);
7391   mp->label = htonl (s->label);
7392   mp->rx_table_id = htonl (rx_table_id);
7393   mp->tx_table_id = htonl (tx_table_id);
7394   strncpy ((char *) mp->swif_tag,
7395            (char *) swif_tag, ARRAY_LEN (mp->swif_tag) - 1);
7396
7397   vl_msg_api_send_shmem (q, (u8 *) & mp);
7398 }
7399
7400 static void
7401 vl_api_mpls_fib_decap_dump_t_handler (vl_api_mpls_fib_decap_dump_t * mp)
7402 {
7403   vpe_api_main_t *am = &vpe_api_main;
7404   unix_shared_memory_queue_t *q;
7405   vlib_main_t *vm = &vlib_global_main;
7406   u64 key;
7407   u32 value;
7408   show_mpls_fib_t *records = 0;
7409   show_mpls_fib_t *s;
7410   mpls_main_t *mm = &mpls_main;
7411   ip4_main_t *im = &ip4_main;
7412   ip4_fib_t *rx_fib;
7413   ip4_fib_t *tx_fib;
7414   u32 tx_table_id;
7415   char *swif_tag;
7416
7417   q = vl_api_client_index_to_input_queue (mp->client_index);
7418   if (q == 0)
7419     return;
7420
7421   /* *INDENT-OFF* */
7422   hash_foreach (key, value, mm->mpls_decap_by_rx_fib_and_label,
7423   ({
7424     vec_add2 (records, s, 1);
7425     s->fib_index = (u32)(key>>32);
7426     s->entry_index = (u32) value;
7427     s->label = ((u32) key)>>12;
7428     s->s_bit = (key & (1<<8)) != 0;
7429   }));
7430   /* *INDENT-ON* */
7431
7432   if (!vec_len (records))
7433     {
7434       vlib_cli_output (vm, "MPLS decap table empty");
7435       goto out;
7436     }
7437
7438   vec_sort_with_function (records, mpls_label_cmp);
7439   vlib_cli_output (vm, "MPLS decap table");
7440   vlib_cli_output (vm, "%=10s%=15s%=6s%=6s", "RX Table", "TX Table/Intfc",
7441                    "Label", "S-bit");
7442   vec_foreach (s, records)
7443   {
7444     mpls_decap_t *d;
7445     d = pool_elt_at_index (mm->decaps, s->entry_index);
7446     if (d->next_index == MPLS_INPUT_NEXT_IP4_INPUT)
7447       {
7448         tx_fib = vec_elt_at_index (im->fibs, d->tx_fib_index);
7449         tx_table_id = tx_fib->table_id;
7450         swif_tag = "     ";
7451       }
7452     else
7453       {
7454         tx_table_id = d->tx_fib_index;
7455         swif_tag = "(i)  ";
7456       }
7457     rx_fib = vec_elt_at_index (im->fibs, s->fib_index);
7458
7459     vlib_cli_output (vm, "%=10d%=10d%=5s%=6d%=6d", rx_fib->table_id,
7460                      tx_table_id, swif_tag, s->label, s->s_bit);
7461
7462     send_mpls_fib_decap_details (am, q, s, rx_fib->table_id,
7463                                  tx_table_id, swif_tag, mp->context);
7464   }
7465
7466 out:
7467   vec_free (records);
7468 }
7469
7470 static void
7471 vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
7472 {
7473   unix_shared_memory_queue_t *q;
7474
7475   q = vl_api_client_index_to_input_queue (mp->client_index);
7476   if (q == 0)
7477     return;
7478
7479   vnet_classify_main_t *cm = &vnet_classify_main;
7480   vnet_classify_table_t *t;
7481   u32 *table_ids = 0;
7482   u32 count;
7483
7484   /* *INDENT-OFF* */
7485   pool_foreach (t, cm->tables,
7486   ({
7487     vec_add1 (table_ids, ntohl(t - cm->tables));
7488   }));
7489   /* *INDENT-ON* */
7490   count = vec_len (table_ids);
7491
7492   vl_api_classify_table_ids_reply_t *rmp;
7493   rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
7494   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
7495   rmp->context = mp->context;
7496   rmp->count = ntohl (count);
7497   clib_memcpy (rmp->ids, table_ids, count * sizeof (u32));
7498   rmp->retval = 0;
7499
7500   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7501
7502   vec_free (table_ids);
7503 }
7504
7505 static void
7506   vl_api_classify_table_by_interface_t_handler
7507   (vl_api_classify_table_by_interface_t * mp)
7508 {
7509   vl_api_classify_table_by_interface_reply_t *rmp;
7510   int rv = 0;
7511
7512   u32 sw_if_index = ntohl (mp->sw_if_index);
7513   u32 *acl = 0;
7514
7515   vec_validate (acl, INPUT_ACL_N_TABLES - 1);
7516   vec_set (acl, ~0);
7517
7518   VALIDATE_SW_IF_INDEX (mp);
7519
7520   input_acl_main_t *am = &input_acl_main;
7521
7522   int if_idx;
7523   u32 type;
7524
7525   for (type = 0; type < INPUT_ACL_N_TABLES; type++)
7526     {
7527       u32 *vec_tbl = am->classify_table_index_by_sw_if_index[type];
7528       if (vec_len (vec_tbl))
7529         {
7530           for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
7531             {
7532               if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
7533                 {
7534                   continue;
7535                 }
7536               acl[type] = vec_elt (vec_tbl, if_idx);
7537             }
7538         }
7539     }
7540
7541   BAD_SW_IF_INDEX_LABEL;
7542
7543   /* *INDENT-OFF* */
7544   REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
7545   ({
7546     rmp->sw_if_index = ntohl(sw_if_index);
7547     rmp->l2_table_id = ntohl(acl[INPUT_ACL_TABLE_L2]);
7548     rmp->ip4_table_id = ntohl(acl[INPUT_ACL_TABLE_IP4]);
7549     rmp->ip6_table_id = ntohl(acl[INPUT_ACL_TABLE_IP6]);
7550   }));
7551   /* *INDENT-ON* */
7552   vec_free (acl);
7553 }
7554
7555 static void
7556 vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
7557 {
7558   unix_shared_memory_queue_t *q;
7559
7560   q = vl_api_client_index_to_input_queue (mp->client_index);
7561   if (q == 0)
7562     return;
7563
7564   vl_api_classify_table_info_reply_t *rmp = 0;
7565
7566   vnet_classify_main_t *cm = &vnet_classify_main;
7567   u32 table_id = ntohl (mp->table_id);
7568   vnet_classify_table_t *t;
7569
7570   /* *INDENT-OFF* */
7571   pool_foreach (t, cm->tables,
7572   ({
7573     if (table_id == t - cm->tables)
7574       {
7575         rmp = vl_msg_api_alloc_as_if_client
7576           (sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
7577         rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
7578         rmp->context = mp->context;
7579         rmp->table_id = ntohl(table_id);
7580         rmp->nbuckets = ntohl(t->nbuckets);
7581         rmp->match_n_vectors = ntohl(t->match_n_vectors);
7582         rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
7583         rmp->active_sessions = ntohl(t->active_elements);
7584         rmp->next_table_index = ntohl(t->next_table_index);
7585         rmp->miss_next_index = ntohl(t->miss_next_index);
7586         rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
7587         clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
7588         rmp->retval = 0;
7589         break;
7590       }
7591   }));
7592   /* *INDENT-ON* */
7593
7594   if (rmp == 0)
7595     {
7596       rmp = vl_msg_api_alloc (sizeof (*rmp));
7597       rmp->_vl_msg_id = ntohs ((VL_API_CLASSIFY_TABLE_INFO_REPLY));
7598       rmp->context = mp->context;
7599       rmp->retval = ntohl (VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
7600     }
7601
7602   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7603 }
7604
7605 static void
7606 vl_api_classify_session_details_t_handler (vl_api_classify_session_details_t *
7607                                            mp)
7608 {
7609   clib_warning ("BUG");
7610 }
7611
7612 static void
7613 send_classify_session_details (unix_shared_memory_queue_t * q,
7614                                u32 table_id,
7615                                u32 match_length,
7616                                vnet_classify_entry_t * e, u32 context)
7617 {
7618   vl_api_classify_session_details_t *rmp;
7619
7620   rmp = vl_msg_api_alloc (sizeof (*rmp));
7621   memset (rmp, 0, sizeof (*rmp));
7622   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_SESSION_DETAILS);
7623   rmp->context = context;
7624   rmp->table_id = ntohl (table_id);
7625   rmp->hit_next_index = ntohl (e->next_index);
7626   rmp->advance = ntohl (e->advance);
7627   rmp->opaque_index = ntohl (e->opaque_index);
7628   rmp->match_length = ntohl (match_length);
7629   clib_memcpy (rmp->match, e->key, match_length);
7630
7631   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7632 }
7633
7634 static void
7635 vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
7636 {
7637   vnet_classify_main_t *cm = &vnet_classify_main;
7638   unix_shared_memory_queue_t *q;
7639
7640   u32 table_id = ntohl (mp->table_id);
7641   vnet_classify_table_t *t;
7642
7643   q = vl_api_client_index_to_input_queue (mp->client_index);
7644
7645   /* *INDENT-OFF* */
7646   pool_foreach (t, cm->tables,
7647   ({
7648     if (table_id == t - cm->tables)
7649       {
7650         vnet_classify_bucket_t * b;
7651         vnet_classify_entry_t * v, * save_v;
7652         int i, j, k;
7653
7654         for (i = 0; i < t->nbuckets; i++)
7655           {
7656             b = &t->buckets [i];
7657             if (b->offset == 0)
7658               continue;
7659
7660             save_v = vnet_classify_get_entry (t, b->offset);
7661             for (j = 0; j < (1<<b->log2_pages); j++)
7662               {
7663                 for (k = 0; k < t->entries_per_page; k++)
7664                   {
7665                     v = vnet_classify_entry_at_index
7666                       (t, save_v, j*t->entries_per_page + k);
7667                     if (vnet_classify_entry_is_free (v))
7668                       continue;
7669
7670                     send_classify_session_details
7671                       (q, table_id, t->match_n_vectors * sizeof (u32x4),
7672                        v, mp->context);
7673                   }
7674               }
7675           }
7676         break;
7677       }
7678   }));
7679   /* *INDENT-ON* */
7680 }
7681
7682 static void
7683 vl_api_ipfix_enable_t_handler (vl_api_ipfix_enable_t * mp)
7684 {
7685   vlib_main_t *vm = vlib_get_main ();
7686   flow_report_main_t *frm = &flow_report_main;
7687   vl_api_ipfix_enable_reply_t *rmp;
7688   ip4_address_t collector, src;
7689   u16 collector_port = UDP_DST_PORT_ipfix;
7690   u32 path_mtu;
7691   u32 template_interval;
7692   u32 fib_id;
7693   u32 fib_index = ~0;
7694   int rv = 0;
7695
7696   memcpy (collector.data, mp->collector_address, sizeof (collector.data));
7697   collector_port = ntohs (mp->collector_port);
7698   if (collector_port == (u16) ~ 0)
7699     collector_port = UDP_DST_PORT_ipfix;
7700   memcpy (src.data, mp->src_address, sizeof (src.data));
7701   fib_id = ntohl (mp->vrf_id);
7702
7703   ip4_main_t *im = &ip4_main;
7704   uword *p = hash_get (im->fib_index_by_table_id, fib_id);
7705   if (!p)
7706     {
7707       rv = VNET_API_ERROR_NO_SUCH_FIB;
7708       goto out;
7709     }
7710   fib_index = p[0];
7711
7712   path_mtu = ntohl (mp->path_mtu);
7713   if (path_mtu == ~0)
7714     path_mtu = 512;             // RFC 7011 section 10.3.3.
7715   template_interval = ntohl (mp->template_interval);
7716   if (template_interval == ~0)
7717     template_interval = 20;
7718
7719   if (collector.as_u32 == 0)
7720     {
7721       rv = VNET_API_ERROR_INVALID_VALUE;
7722       goto out;
7723     }
7724
7725   if (src.as_u32 == 0)
7726     {
7727       rv = VNET_API_ERROR_INVALID_VALUE;
7728       goto out;
7729     }
7730
7731   if (path_mtu > 1450 /* vpp does not support fragmentation */ )
7732     {
7733       rv = VNET_API_ERROR_INVALID_VALUE;
7734       goto out;
7735     }
7736
7737   if (path_mtu < 68)
7738     {
7739       rv = VNET_API_ERROR_INVALID_VALUE;
7740       goto out;
7741     }
7742
7743   /* Reset report streams if we are reconfiguring IP addresses */
7744   if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
7745       frm->src_address.as_u32 != src.as_u32 ||
7746       frm->collector_port != collector_port)
7747     vnet_flow_reports_reset (frm);
7748
7749   frm->ipfix_collector.as_u32 = collector.as_u32;
7750   frm->collector_port = collector_port;
7751   frm->src_address.as_u32 = src.as_u32;
7752   frm->fib_index = fib_index;
7753   frm->path_mtu = path_mtu;
7754   frm->template_interval = template_interval;
7755
7756   /* Turn on the flow reporting process */
7757   vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
7758
7759 out:
7760   REPLY_MACRO (VL_API_IPFIX_ENABLE_REPLY);
7761 }
7762
7763 static void
7764 vl_api_ipfix_dump_t_handler (vl_api_ipfix_dump_t * mp)
7765 {
7766   flow_report_main_t *frm = &flow_report_main;
7767   unix_shared_memory_queue_t *q;
7768   vl_api_ipfix_details_t *rmp;
7769
7770   q = vl_api_client_index_to_input_queue (mp->client_index);
7771
7772   rmp = vl_msg_api_alloc (sizeof (*rmp));
7773   memset (rmp, 0, sizeof (*rmp));
7774   rmp->_vl_msg_id = ntohs (VL_API_IPFIX_DETAILS);
7775   rmp->context = mp->context;
7776   memcpy (rmp->collector_address, frm->ipfix_collector.data,
7777           sizeof (frm->ipfix_collector.data));
7778   rmp->collector_port = htons (frm->collector_port);
7779   memcpy (rmp->src_address, frm->src_address.data,
7780           sizeof (frm->src_address.data));
7781   rmp->fib_index = htonl (frm->fib_index);
7782   rmp->path_mtu = htonl (frm->path_mtu);
7783   rmp->template_interval = htonl (frm->template_interval);
7784
7785   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7786 }
7787
7788 static void
7789 vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
7790 {
7791   vl_api_pg_create_interface_reply_t *rmp;
7792   int rv = 0;
7793
7794   pg_main_t *pg = &pg_main;
7795   u32 sw_if_index = pg_interface_add_or_get (pg, ntohl (mp->interface_id));
7796
7797   /* *INDENT-OFF* */
7798   REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
7799   ({
7800     rmp->sw_if_index = ntohl(sw_if_index);
7801   }));
7802   /* *INDENT-ON* */
7803 }
7804
7805 static void
7806 vl_api_pg_capture_t_handler (vl_api_pg_capture_t * mp)
7807 {
7808   vl_api_pg_capture_reply_t *rmp;
7809   int rv = 0;
7810
7811   vnet_main_t *vnm = vnet_get_main ();
7812   vnet_interface_main_t *im = &vnm->interface_main;
7813   vnet_hw_interface_t *hi = 0;
7814
7815   u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
7816   u32 hw_if_index = ~0;
7817   uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
7818   if (p)
7819     hw_if_index = *p;
7820   vec_free (intf_name);
7821
7822   if (hw_if_index != ~0)
7823     {
7824       pg_capture_args_t _a, *a = &_a;
7825
7826       u32 len = ntohl (mp->pcap_name_length);
7827       u8 *pcap_file_name = vec_new (u8, len);
7828       clib_memcpy (pcap_file_name, mp->pcap_file_name, len);
7829
7830       hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
7831       a->hw_if_index = hw_if_index;
7832       a->dev_instance = hi->dev_instance;
7833       a->is_enabled = mp->is_enabled;
7834       a->pcap_file_name = pcap_file_name;
7835       a->count = ntohl (mp->count);
7836
7837       clib_error_t *e = pg_capture (a);
7838       if (e)
7839         {
7840           clib_error_report (e);
7841           rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
7842         }
7843
7844       vec_free (pcap_file_name);
7845     }
7846   REPLY_MACRO (VL_API_PG_CAPTURE_REPLY);
7847 }
7848
7849 static void
7850 vl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t * mp)
7851 {
7852   vl_api_pg_enable_disable_reply_t *rmp;
7853   int rv = 0;
7854
7855   pg_main_t *pg = &pg_main;
7856   u32 stream_index = ~0;
7857
7858   int is_enable = mp->is_enabled != 0;
7859   u32 len = ntohl (mp->stream_name_length) - 1;
7860
7861   if (len > 0)
7862     {
7863       u8 *stream_name = vec_new (u8, len);
7864       clib_memcpy (stream_name, mp->stream_name, len);
7865       uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
7866       if (p)
7867         stream_index = *p;
7868       vec_free (stream_name);
7869     }
7870
7871   pg_enable_disable (stream_index, is_enable);
7872
7873   REPLY_MACRO (VL_API_PG_ENABLE_DISABLE_REPLY);
7874 }
7875
7876 static void
7877   vl_api_ip_source_and_port_range_check_add_del_t_handler
7878   (vl_api_ip_source_and_port_range_check_add_del_t * mp)
7879 {
7880   vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
7881   int rv = 0;
7882
7883   u8 is_ipv6 = mp->is_ipv6;
7884   u8 is_add = mp->is_add;
7885   u8 mask_length = mp->mask_length;
7886   ip4_address_t ip4_addr;
7887   ip6_address_t ip6_addr;
7888   u16 *low_ports = 0;
7889   u16 *high_ports = 0;
7890   u32 vrf_id;
7891   u16 tmp_low, tmp_high;
7892   u8 num_ranges;
7893   int i;
7894
7895   // Validate port range
7896   num_ranges = mp->number_of_ranges;
7897   if (num_ranges > 32)
7898     {                           // This is size of array in VPE.API
7899       rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
7900       goto reply;
7901     }
7902
7903   vec_reset_length (low_ports);
7904   vec_reset_length (high_ports);
7905
7906   for (i = 0; i < num_ranges; i++)
7907     {
7908       tmp_low = mp->low_ports[i];
7909       tmp_high = mp->high_ports[i];
7910       // If tmp_low <= tmp_high then only need to check tmp_low = 0
7911       // If tmp_low <= tmp_high then only need to check tmp_high > 65535
7912       if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
7913         {
7914           rv = VNET_API_ERROR_INVALID_VALUE;
7915           goto reply;
7916         }
7917       vec_add1 (low_ports, tmp_low);
7918       vec_add1 (high_ports, tmp_high + 1);
7919     }
7920
7921   // Validate mask_length
7922   if (mask_length < 0 ||
7923       (is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
7924     {
7925       rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
7926       goto reply;
7927     }
7928
7929   vrf_id = ntohl (mp->vrf_id);
7930
7931   if (vrf_id < 1)
7932     {
7933       rv = VNET_API_ERROR_INVALID_VALUE;
7934       goto reply;
7935     }
7936
7937
7938   if (is_ipv6)
7939     {
7940       clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
7941       rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
7942                                                     mask_length,
7943                                                     vrf_id,
7944                                                     low_ports,
7945                                                     high_ports, is_add);
7946     }
7947   else
7948     {
7949       clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
7950       rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
7951                                                     mask_length,
7952                                                     vrf_id,
7953                                                     low_ports,
7954                                                     high_ports, is_add);
7955     }
7956
7957 reply:
7958   vec_free (low_ports);
7959   vec_free (high_ports);
7960   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
7961 }
7962
7963 static void
7964   vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
7965   (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
7966 {
7967   vlib_main_t *vm = vlib_get_main ();
7968   vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
7969   ip4_main_t *im = &ip4_main;
7970   int rv;
7971   u32 sw_if_index;
7972   u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
7973   u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
7974   uword *p = 0;
7975   int i;
7976
7977   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
7978     ntohl (mp->tcp_out_vrf_id);
7979   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
7980     ntohl (mp->udp_out_vrf_id);
7981   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
7982     ntohl (mp->tcp_in_vrf_id);
7983   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
7984     ntohl (mp->udp_in_vrf_id);
7985
7986
7987   for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
7988     {
7989       if (vrf_id[i] != 0 && vrf_id[i] != ~0)
7990         {
7991           p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
7992
7993           if (p == 0)
7994             {
7995               rv = VNET_API_ERROR_INVALID_VALUE;
7996               goto reply;
7997             }
7998
7999           fib_index[i] = p[0];
8000         }
8001       else
8002         fib_index[i] = ~0;
8003     }
8004   sw_if_index = ntohl (mp->sw_if_index);
8005
8006   VALIDATE_SW_IF_INDEX (mp);
8007
8008   rv =
8009     set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
8010                                         mp->is_add);
8011
8012   BAD_SW_IF_INDEX_LABEL;
8013 reply:
8014
8015   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
8016 }
8017
8018 #define BOUNCE_HANDLER(nn)                                              \
8019 static void vl_api_##nn##_t_handler (                                   \
8020     vl_api_##nn##_t *mp)                                                \
8021 {                                                                       \
8022     vpe_client_registration_t *reg;                                     \
8023     vpe_api_main_t * vam = &vpe_api_main;                               \
8024     unix_shared_memory_queue_t * q;                                     \
8025                                                                         \
8026     /* One registration only... */                                      \
8027     pool_foreach(reg, vam->nn##_registrations,                          \
8028     ({                                                                  \
8029         q = vl_api_client_index_to_input_queue (reg->client_index);     \
8030         if (q) {                                                        \
8031             /*                                                          \
8032              * If the queue is stuffed, turf the msg and complain       \
8033              * It's unlikely that the intended recipient is             \
8034              * alive; avoid deadlock at all costs.                      \
8035              */                                                         \
8036             if (q->cursize == q->maxsize) {                             \
8037                 clib_warning ("ERROR: receiver queue full, drop msg");  \
8038                 vl_msg_api_free (mp);                                   \
8039                 return;                                                 \
8040             }                                                           \
8041             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
8042             return;                                                     \
8043         }                                                               \
8044     }));                                                                \
8045     vl_msg_api_free (mp);                                               \
8046 }
8047
8048 /*
8049  * vpe_api_hookup
8050  * Add vpe's API message handlers to the table.
8051  * vlib has alread mapped shared memory and
8052  * added the client registration handlers.
8053  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
8054  */
8055
8056 static clib_error_t *
8057 vpe_api_hookup (vlib_main_t * vm)
8058 {
8059   api_main_t *am = &api_main;
8060
8061 #define _(N,n)                                                  \
8062     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
8063                            vl_api_##n##_t_handler,              \
8064                            vl_noop_handler,                     \
8065                            vl_api_##n##_t_endian,               \
8066                            vl_api_##n##_t_print,                \
8067                            sizeof(vl_api_##n##_t), 1);
8068   foreach_vpe_api_msg;
8069 #undef _
8070
8071   /*
8072    * Manually register the sr tunnel add del msg, so we trace
8073    * enough bytes to capture a typical segment list
8074    */
8075   vl_msg_api_set_handlers (VL_API_SR_TUNNEL_ADD_DEL,
8076                            "sr_tunnel_add_del",
8077                            vl_api_sr_tunnel_add_del_t_handler,
8078                            vl_noop_handler,
8079                            vl_api_sr_tunnel_add_del_t_endian,
8080                            vl_api_sr_tunnel_add_del_t_print, 256, 1);
8081
8082
8083   /*
8084    * Manually register the sr policy add del msg, so we trace
8085    * enough bytes to capture a typical tunnel name list
8086    */
8087   vl_msg_api_set_handlers (VL_API_SR_POLICY_ADD_DEL,
8088                            "sr_policy_add_del",
8089                            vl_api_sr_policy_add_del_t_handler,
8090                            vl_noop_handler,
8091                            vl_api_sr_policy_add_del_t_endian,
8092                            vl_api_sr_policy_add_del_t_print, 256, 1);
8093
8094   /*
8095    * Trace space for 8 MPLS encap labels, classifier mask+match
8096    */
8097   am->api_trace_cfg[VL_API_MPLS_ADD_DEL_ENCAP].size += 8 * sizeof (u32);
8098   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
8099   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size
8100     += 5 * sizeof (u32x4);
8101   am->api_trace_cfg[VL_API_VXLAN_ADD_DEL_TUNNEL].size += 16 * sizeof (u32);
8102
8103   /*
8104    * Thread-safe API messages
8105    */
8106   am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
8107   am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
8108
8109   return 0;
8110 }
8111
8112 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
8113
8114 static clib_error_t *
8115 vpe_api_init (vlib_main_t * vm)
8116 {
8117   vpe_api_main_t *am = &vpe_api_main;
8118
8119   am->vlib_main = vm;
8120   am->vnet_main = vnet_get_main ();
8121   am->interface_events_registration_hash = hash_create (0, sizeof (uword));
8122   am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
8123   am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
8124   am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
8125   am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
8126   am->oam_events_registration_hash = hash_create (0, sizeof (uword));
8127
8128   vl_api_init (vm);
8129   vl_set_memory_region_name ("/vpe-api");
8130   vl_enable_disable_memory_api (vm, 1 /* enable it */ );
8131
8132   return 0;
8133 }
8134
8135 VLIB_INIT_FUNCTION (vpe_api_init);
8136
8137
8138 static clib_error_t *
8139 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
8140 {
8141   u8 *chroot_path;
8142   int uid, gid, rv;
8143   char *s, buf[128];
8144   struct passwd _pw, *pw;
8145   struct group _grp, *grp;
8146   clib_error_t *e;
8147
8148   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
8149     {
8150       if (unformat (input, "prefix %s", &chroot_path))
8151         {
8152           vec_add1 (chroot_path, 0);
8153           vl_set_memory_root_path ((char *) chroot_path);
8154         }
8155       else if (unformat (input, "uid %d", &uid))
8156         vl_set_memory_uid (uid);
8157       else if (unformat (input, "gid %d", &gid))
8158         vl_set_memory_gid (gid);
8159       else if (unformat (input, "uid %s", &s))
8160         {
8161           /* lookup the username */
8162           pw = NULL;
8163           rv = getpwnam_r (s, &_pw, buf, sizeof (buf), &pw);
8164           if (rv < 0)
8165             {
8166               e = clib_error_return_code (0, rv,
8167                                           CLIB_ERROR_ERRNO_VALID |
8168                                           CLIB_ERROR_FATAL,
8169                                           "cannot fetch username %s", s);
8170               vec_free (s);
8171               return e;
8172             }
8173           if (pw == NULL)
8174             {
8175               e =
8176                 clib_error_return_fatal (0, "username %s does not exist", s);
8177               vec_free (s);
8178               return e;
8179             }
8180           vec_free (s);
8181           vl_set_memory_uid (pw->pw_uid);
8182         }
8183       else if (unformat (input, "gid %s", &s))
8184         {
8185           /* lookup the group name */
8186           grp = NULL;
8187           rv = getgrnam_r (s, &_grp, buf, sizeof (buf), &grp);
8188           if (rv != 0)
8189             {
8190               e = clib_error_return_code (0, rv,
8191                                           CLIB_ERROR_ERRNO_VALID |
8192                                           CLIB_ERROR_FATAL,
8193                                           "cannot fetch group %s", s);
8194               vec_free (s);
8195               return e;
8196             }
8197           if (grp == NULL)
8198             {
8199               e = clib_error_return_fatal (0, "group %s does not exist", s);
8200               vec_free (s);
8201               return e;
8202             }
8203           vec_free (s);
8204           vl_set_memory_gid (grp->gr_gid);
8205         }
8206       else
8207         return clib_error_return (0, "unknown input `%U'",
8208                                   format_unformat_error, input);
8209     }
8210   return 0;
8211 }
8212
8213 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
8214
8215 void *
8216 get_unformat_vnet_sw_interface (void)
8217 {
8218   return (void *) &unformat_vnet_sw_interface;
8219 }
8220
8221 #undef vl_api_version
8222 #define vl_api_version(n,v) static u32 vpe_api_version = v;
8223 #include <vpp-api/vpe.api.h>
8224 #undef vl_api_version
8225
8226 int
8227 vl_msg_api_version_check (vl_api_memclnt_create_t * mp)
8228 {
8229   if (clib_host_to_net_u32 (mp->api_versions[0]) != vpe_api_version)
8230     {
8231       clib_warning ("vpe API mismatch: 0x%08x instead of 0x%08x",
8232                     clib_host_to_net_u32 (mp->api_versions[0]),
8233                     vpe_api_version);
8234       return -1;
8235     }
8236   return 0;
8237 }
8238
8239 static u8 *
8240 format_arp_event (u8 * s, va_list * args)
8241 {
8242   vl_api_ip4_arp_event_t *event = va_arg (*args, vl_api_ip4_arp_event_t *);
8243
8244   s = format (s, "pid %d: %U", event->pid,
8245               format_ip4_address, &event->address);
8246   return s;
8247 }
8248
8249 static clib_error_t *
8250 show_ip4_arp_events_fn (vlib_main_t * vm,
8251                         unformat_input_t * input, vlib_cli_command_t * cmd)
8252 {
8253   vpe_api_main_t *am = &vpe_api_main;
8254   vl_api_ip4_arp_event_t *event;
8255
8256   if (pool_elts (am->arp_events) == 0)
8257     {
8258       vlib_cli_output (vm, "No active arp event registrations");
8259       return 0;
8260     }
8261
8262   /* *INDENT-OFF* */
8263   pool_foreach (event, am->arp_events,
8264   ({
8265     vlib_cli_output (vm, "%U", format_arp_event, event);
8266   }));
8267   /* *INDENT-ON* */
8268
8269   return 0;
8270 }
8271
8272 /* *INDENT-OFF* */
8273 VLIB_CLI_COMMAND (show_ip4_arp_events, static) = {
8274   .path = "show arp event registrations",
8275   .function = show_ip4_arp_events_fn,
8276   .short_help = "Show arp event registrations",
8277 };
8278 /* *INDENT-ON* */
8279
8280 /*
8281  * fd.io coding-style-patch-verification: ON
8282  *
8283  * Local Variables:
8284  * eval: (c-set-style "gnu")
8285  * End:
8286  */