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