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