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