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