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