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