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