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