Port glean neighbor entry support to IPv6
[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 {
2433     vl_api_sw_interface_details_t * mp;
2434     vnet_hw_interface_t * hi;
2435
2436     hi = vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
2437
2438     mp = vl_msg_api_alloc (sizeof (*mp));
2439     memset (mp, 0, sizeof (*mp));
2440     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_DETAILS);
2441     mp->sw_if_index = ntohl(swif->sw_if_index);
2442     mp->sup_sw_if_index = ntohl(swif->sup_sw_if_index);
2443     mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
2444         1 : 0;
2445     mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 
2446         1 : 0;
2447     mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
2448                        VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
2449     mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
2450                       VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
2451     mp->link_mtu = ntohs(hi->max_packet_bytes);
2452
2453     strncpy ((char *) mp->interface_name, 
2454              (char *) interface_name, ARRAY_LEN(mp->interface_name)-1);
2455     
2456     /* Send the L2 address for ethernet physical intfcs */
2457     if (swif->sup_sw_if_index == swif->sw_if_index
2458         && hi->hw_class_index == ethernet_hw_interface_class.index) {
2459         ethernet_main_t *em = ethernet_get_main (am->vlib_main);
2460         ethernet_interface_t *ei;
2461         
2462         ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
2463         ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
2464         clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
2465         mp->l2_address_length = ntohl(sizeof (ei->address));
2466     } else if (swif->sup_sw_if_index != swif->sw_if_index) {
2467         vnet_sub_interface_t *sub = &swif->sub;
2468         mp->sub_id = ntohl(sub->id);
2469         mp->sub_dot1ad = sub->eth.flags.dot1ad;
2470         mp->sub_number_of_tags = sub->eth.flags.one_tag + sub->eth.flags.two_tags*2;
2471         mp->sub_outer_vlan_id = ntohs(sub->eth.outer_vlan_id);
2472         mp->sub_inner_vlan_id = ntohs(sub->eth.inner_vlan_id);
2473         mp->sub_exact_match = sub->eth.flags.exact_match;
2474         mp->sub_default = sub->eth.flags.default_sub;
2475         mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
2476         mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
2477
2478         /* vlan tag rewrite data */
2479         u32 vtr_op = L2_VTR_DISABLED;
2480         u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
2481
2482         if (l2vtr_get(am->vlib_main, am->vnet_main, swif->sw_if_index,
2483                       &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0) {
2484             // error - default to disabled
2485             mp->vtr_op = ntohl(L2_VTR_DISABLED);
2486             clib_warning("cannot get vlan tag rewrite for sw_if_index %d", 
2487                     swif->sw_if_index);
2488         } else {
2489             mp->vtr_op = ntohl(vtr_op);
2490             mp->vtr_push_dot1q = ntohl(vtr_push_dot1q);
2491             mp->vtr_tag1 = ntohl(vtr_tag1);
2492             mp->vtr_tag2 = ntohl(vtr_tag2);
2493         }
2494     }
2495
2496     vl_msg_api_send_shmem (q, (u8 *)&mp);
2497 }
2498
2499 static void send_sw_interface_flags (vpe_api_main_t * am,
2500                                      unix_shared_memory_queue_t *q,
2501                                      vnet_sw_interface_t * swif)
2502 {
2503     vl_api_sw_interface_set_flags_t *mp;
2504     vnet_main_t * vnm = am->vnet_main;
2505
2506     vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, 
2507                                                          swif->sw_if_index);
2508     mp = vl_msg_api_alloc (sizeof (*mp));
2509     memset (mp, 0, sizeof (*mp));
2510     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_SET_FLAGS);
2511     mp->sw_if_index = ntohl(swif->sw_if_index);
2512
2513     mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
2514         1 : 0;
2515     mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 
2516         1 : 0;
2517     vl_msg_api_send_shmem (q, (u8 *)&mp);
2518 }
2519
2520 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
2521                                      unix_shared_memory_queue_t *q,
2522                                              u32 sw_if_index) 
2523     __attribute__((unused));
2524
2525 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
2526                                      unix_shared_memory_queue_t *q,
2527                                      u32 sw_if_index)
2528 {
2529     vl_api_sw_interface_set_flags_t *mp;
2530
2531     mp = vl_msg_api_alloc (sizeof (*mp));
2532     memset (mp, 0, sizeof (*mp));
2533     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_SET_FLAGS);
2534     mp->sw_if_index = ntohl(sw_if_index);
2535
2536     mp->admin_up_down = 0;
2537     mp->link_up_down = 0;
2538     mp->deleted = 1;
2539     vl_msg_api_send_shmem (q, (u8 *)&mp);
2540 }
2541
2542 static void vl_api_sw_interface_dump_t_handler (
2543     vl_api_sw_interface_dump_t *mp)
2544 {
2545     vpe_api_main_t * am = &vpe_api_main;
2546     vnet_sw_interface_t * swif;
2547     vnet_interface_main_t * im = &am->vnet_main->interface_main;
2548     u8 * filter_string = 0, * name_string = 0;
2549     unix_shared_memory_queue_t * q;
2550     char * strcasestr (char *, char *); /* lnx hdr file botch */
2551
2552     q = vl_api_client_index_to_input_queue (mp->client_index);
2553
2554     if (q == 0)
2555         return;
2556     
2557     if (mp->name_filter_valid) {
2558         mp->name_filter [ARRAY_LEN(mp->name_filter)-1] = 0;
2559         filter_string = format (0, "%s%c", mp->name_filter, 0);
2560     }
2561
2562     pool_foreach (swif, im->sw_interfaces, 
2563     ({
2564         name_string = format (name_string, "%U%c", 
2565                               format_vnet_sw_interface_name,
2566                               am->vnet_main, swif, 0);
2567
2568         if (mp->name_filter_valid == 0 ||
2569             strcasestr((char *) name_string, (char *) filter_string)) {
2570
2571             send_sw_interface_details (am, q, swif, name_string);
2572             send_sw_interface_flags (am, q, swif);
2573         }
2574         _vec_len (name_string) = 0;
2575     }));
2576
2577     vec_free (name_string);
2578     vec_free (filter_string);
2579 }
2580
2581 void send_oam_event (oam_target_t * t)
2582 {
2583     vpe_api_main_t * vam = &vpe_api_main;
2584     unix_shared_memory_queue_t * q;
2585     vpe_client_registration_t *reg;
2586     vl_api_oam_event_t * mp;
2587
2588     pool_foreach(reg, vam->oam_events_registrations, 
2589     ({
2590         q = vl_api_client_index_to_input_queue (reg->client_index);
2591         if (q) {
2592             mp = vl_msg_api_alloc (sizeof (*mp));
2593             mp->_vl_msg_id = ntohs (VL_API_OAM_EVENT);
2594             clib_memcpy (mp->dst_address, &t->dst_address, sizeof (mp->dst_address));
2595             mp->state = t->state;
2596             vl_msg_api_send_shmem (q, (u8 *)&mp);
2597         }
2598     }));
2599 }
2600
2601 static void
2602 vl_api_oam_add_del_t_handler (vl_api_oam_add_del_t *mp)
2603 {
2604     vl_api_oam_add_del_reply_t * rmp;
2605     int rv;
2606
2607     rv = vpe_oam_add_del_target ((ip4_address_t *)mp->src_address,
2608                                  (ip4_address_t *)mp->dst_address,
2609                                  ntohl(mp->vrf_id),
2610                                  (int)(mp->is_add));
2611
2612     REPLY_MACRO(VL_API_OAM_ADD_DEL_REPLY);
2613 }
2614
2615 static void
2616 vl_api_vnet_get_summary_stats_t_handler (
2617     vl_api_vnet_get_summary_stats_t *mp)
2618 {
2619     stats_main_t * sm = &stats_main;
2620     vnet_interface_main_t * im = sm->interface_main;
2621     vl_api_vnet_summary_stats_reply_t *rmp;
2622     vlib_combined_counter_main_t * cm;
2623     vlib_counter_t v;
2624     int i, which;
2625     u64 total_pkts[VLIB_N_RX_TX];
2626     u64 total_bytes[VLIB_N_RX_TX];
2627
2628     unix_shared_memory_queue_t * q =
2629         vl_api_client_index_to_input_queue (mp->client_index);
2630     
2631     if (!q)
2632         return;
2633     
2634     rmp = vl_msg_api_alloc (sizeof (*rmp));
2635     rmp->_vl_msg_id = ntohs(VL_API_VNET_SUMMARY_STATS_REPLY);
2636     rmp->context = mp->context;
2637     rmp->retval = 0;
2638
2639     memset (total_pkts, 0, sizeof (total_pkts));
2640     memset (total_bytes, 0, sizeof (total_bytes));
2641
2642     vnet_interface_counter_lock (im);
2643
2644     vec_foreach (cm, im->combined_sw_if_counters) {
2645         which = cm - im->combined_sw_if_counters;
2646
2647         for (i = 0; i < vec_len (cm->maxi); i++) {
2648             vlib_get_combined_counter (cm, i, &v);
2649             total_pkts[which] += v.packets;
2650             total_bytes[which] += v.bytes;
2651         }
2652     }
2653     vnet_interface_counter_unlock (im);
2654
2655     /* Note: in HOST byte order! */
2656     rmp->total_pkts[VLIB_RX] = total_pkts[VLIB_RX];
2657     rmp->total_bytes[VLIB_RX] = total_bytes[VLIB_RX];
2658     rmp->total_pkts[VLIB_TX] = total_pkts[VLIB_TX];
2659     rmp->total_bytes[VLIB_TX] = total_bytes[VLIB_TX];
2660     rmp->vector_rate = vlib_last_vector_length_per_node (sm->vlib_main);
2661
2662     vl_msg_api_send_shmem (q, (u8 *)&rmp);
2663 }
2664
2665 typedef CLIB_PACKED (struct {
2666   ip4_address_t address;
2667
2668   u32 address_length : 6;
2669
2670   u32 index : 26;
2671 }) ip4_route_t;
2672
2673 static int ip4_reset_fib_t_handler (vl_api_reset_fib_t *mp)
2674 {
2675     vnet_main_t * vnm = vnet_get_main();
2676     vnet_interface_main_t * im = &vnm->interface_main;
2677     ip4_main_t * im4 = &ip4_main;
2678     static ip4_route_t * routes;
2679     static u32 * sw_if_indices_to_shut;
2680     stats_main_t * sm = &stats_main;
2681     ip4_route_t * r;
2682     ip4_fib_t * fib;
2683     u32 sw_if_index;
2684     int i;
2685     int rv = VNET_API_ERROR_NO_SUCH_FIB;
2686     u32 target_fib_id = ntohl(mp->vrf_id);
2687
2688     dslock (sm, 1 /* release hint */, 8 /* tag */);
2689
2690     vec_foreach (fib, im4->fibs) {
2691         vnet_sw_interface_t * si;
2692
2693         if (fib->table_id != target_fib_id)
2694             continue;
2695         
2696         /* remove any mpls/gre tunnels in this fib */
2697         vnet_mpls_gre_delete_fib_tunnels (fib->table_id);
2698
2699         /* remove any mpls encap/decap labels */
2700         mpls_fib_reset_labels (fib->table_id);
2701
2702         /* remove any proxy arps in this fib */
2703         vnet_proxy_arp_fib_reset (fib->table_id);
2704
2705         /* Set the flow hash for this fib to the default */
2706         vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2707
2708         vec_reset_length (sw_if_indices_to_shut);
2709
2710         /* Shut down interfaces in this FIB / clean out intfc routes */
2711         pool_foreach (si, im->sw_interfaces,
2712         ({
2713             u32 sw_if_index = si->sw_if_index;
2714
2715             if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
2716                 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
2717                     fib - im4->fibs))
2718                 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2719         }));
2720                       
2721         for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2722             sw_if_index = sw_if_indices_to_shut[i];
2723             // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
2724
2725             u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2726             flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2727             vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2728         }
2729
2730         vec_reset_length (routes);
2731
2732         for (i = 0; i < ARRAY_LEN (fib->adj_index_by_dst_address); i++) {
2733             uword * hash = fib->adj_index_by_dst_address[i];
2734             hash_pair_t * p;
2735             ip4_route_t x;
2736
2737             x.address_length = i;
2738
2739             hash_foreach_pair (p, hash, 
2740             ({
2741                 x.address.data_u32 = p->key;
2742                 vec_add1 (routes, x);
2743             }));
2744         }
2745       
2746         vec_foreach (r, routes) {
2747             ip4_add_del_route_args_t a;
2748
2749             memset (&a, 0, sizeof (a));
2750             a.flags = IP4_ROUTE_FLAG_FIB_INDEX | IP4_ROUTE_FLAG_DEL;
2751             a.table_index_or_table_id = fib - im4->fibs;
2752             a.dst_address = r->address;
2753             a.dst_address_length = r->address_length;
2754             a.adj_index = ~0;
2755
2756             ip4_add_del_route (im4, &a);
2757             ip4_maybe_remap_adjacencies (im4, fib - im4->fibs, 
2758                                          IP4_ROUTE_FLAG_FIB_INDEX);
2759         } 
2760         rv = 0;
2761         break;
2762     } /* vec_foreach (fib) */
2763
2764     dsunlock(sm);
2765     return rv;
2766 }
2767
2768 typedef struct {
2769   ip6_address_t address;
2770   u32 address_length;
2771   u32 index;
2772 } ip6_route_t;
2773
2774 typedef struct {
2775   u32 fib_index;
2776   ip6_route_t ** routep;
2777 } add_routes_in_fib_arg_t;
2778
2779 static void add_routes_in_fib (clib_bihash_kv_24_8_t * kvp, void *arg)
2780 {
2781   add_routes_in_fib_arg_t * ap = arg;
2782
2783   if (kvp->key[2]>>32 == ap->fib_index)
2784     {
2785       ip6_address_t *addr;
2786       ip6_route_t * r;
2787       addr = (ip6_address_t *) kvp;
2788       vec_add2 (*ap->routep, r, 1);
2789       r->address = addr[0];
2790       r->address_length = kvp->key[2] & 0xFF;
2791       r->index = kvp->value;
2792     }
2793 }
2794
2795 static int ip6_reset_fib_t_handler (vl_api_reset_fib_t *mp)
2796 {
2797     vnet_main_t * vnm = vnet_get_main();
2798     vnet_interface_main_t * im = &vnm->interface_main;
2799     ip6_main_t * im6 = &ip6_main;
2800     stats_main_t * sm = &stats_main;
2801     static ip6_route_t * routes;
2802     static u32 * sw_if_indices_to_shut;
2803     ip6_route_t * r;
2804     ip6_fib_t * fib;
2805     u32 sw_if_index;
2806     int i;
2807     int rv = VNET_API_ERROR_NO_SUCH_FIB;
2808     u32 target_fib_id = ntohl(mp->vrf_id);
2809     add_routes_in_fib_arg_t _a, *a=&_a;
2810     clib_bihash_24_8_t * h = &im6->ip6_lookup_table;
2811
2812     dslock (sm, 1 /* release hint */, 9 /* tag */);
2813
2814     vec_foreach (fib, im6->fibs) {
2815         vnet_sw_interface_t * si;
2816
2817         if (fib->table_id != target_fib_id)
2818             continue;
2819
2820         vec_reset_length (sw_if_indices_to_shut);
2821
2822         /* Shut down interfaces in this FIB / clean out intfc routes */
2823         pool_foreach (si, im->sw_interfaces,
2824         ({
2825             if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
2826                 fib - im6->fibs)
2827                 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2828         }));
2829                       
2830         for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2831             sw_if_index = sw_if_indices_to_shut[i];
2832             // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
2833
2834             u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2835             flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2836             vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2837         }
2838
2839         vec_reset_length (routes);
2840
2841         a->fib_index = fib - im6->fibs;
2842         a->routep = &routes;
2843
2844         clib_bihash_foreach_key_value_pair_24_8 (h, add_routes_in_fib, a);
2845
2846         vec_foreach (r, routes) {
2847             ip6_add_del_route_args_t a;
2848
2849             memset (&a, 0, sizeof (a));
2850             a.flags = IP6_ROUTE_FLAG_FIB_INDEX | IP6_ROUTE_FLAG_DEL;
2851             a.table_index_or_table_id = fib - im6->fibs;
2852             a.dst_address = r->address;
2853             a.dst_address_length = r->address_length;
2854             a.adj_index = ~0;
2855
2856             ip6_add_del_route (im6, &a);
2857             ip6_maybe_remap_adjacencies (im6, fib - im6->fibs, 
2858                                          IP6_ROUTE_FLAG_FIB_INDEX);
2859         } 
2860         rv = 0;
2861         /* Reinstall the neighbor / router discovery routes */
2862         vnet_ip6_fib_init (im6, fib - im6->fibs);
2863         break;
2864     } /* vec_foreach (fib) */
2865
2866     dsunlock(sm);
2867     return rv;
2868 }
2869
2870 static void vl_api_reset_fib_t_handler (vl_api_reset_fib_t *mp)
2871 {
2872     int rv;
2873     vl_api_reset_fib_reply_t * rmp;
2874
2875     if (mp->is_ipv6)
2876         rv = ip6_reset_fib_t_handler (mp);
2877     else
2878         rv = ip4_reset_fib_t_handler (mp);
2879     
2880     REPLY_MACRO(VL_API_RESET_FIB_REPLY);
2881 }
2882
2883
2884 static void
2885 dhcpv4_proxy_config (vl_api_dhcp_proxy_config_t *mp)
2886 {
2887     vl_api_dhcp_proxy_config_reply_t * rmp;
2888     int rv;
2889
2890     rv = dhcp_proxy_set_server ((ip4_address_t *)(&mp->dhcp_server),
2891                                 (ip4_address_t *)(&mp->dhcp_src_address),
2892                                 (u32) ntohl(mp->vrf_id),
2893                                 (int) mp->insert_circuit_id,
2894                                 (int) (mp->is_add == 0));
2895     
2896     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_REPLY);
2897 }
2898
2899
2900 static void
2901 dhcpv6_proxy_config (vl_api_dhcp_proxy_config_t *mp)
2902 {
2903     vl_api_dhcp_proxy_config_reply_t * rmp;
2904     int rv = -1;
2905
2906     rv = dhcpv6_proxy_set_server ((ip6_address_t *)(&mp->dhcp_server),
2907                                 (ip6_address_t *)(&mp->dhcp_src_address),
2908                                 (u32) ntohl(mp->vrf_id),
2909                                 (int) mp->insert_circuit_id,
2910                                 (int) (mp->is_add == 0));
2911     
2912     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_REPLY);
2913 }
2914
2915 static void
2916 dhcpv4_proxy_config_2 (vl_api_dhcp_proxy_config_2_t *mp)
2917 {
2918     vl_api_dhcp_proxy_config_reply_t * rmp;
2919     int rv;
2920
2921     rv = dhcp_proxy_set_server_2 ((ip4_address_t *)(&mp->dhcp_server),
2922                                 (ip4_address_t *)(&mp->dhcp_src_address),
2923                                 (u32) ntohl(mp->rx_vrf_id),
2924                                 (u32) ntohl(mp->server_vrf_id),
2925                                 (int) mp->insert_circuit_id,
2926                                 (int) (mp->is_add == 0));
2927     
2928     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_2_REPLY);
2929 }
2930
2931
2932 static void
2933 dhcpv6_proxy_config_2 (vl_api_dhcp_proxy_config_2_t *mp)
2934 {
2935     vl_api_dhcp_proxy_config_reply_t * rmp;
2936     int rv = -1;
2937
2938 #if 0 // $$$$ FIXME
2939     rv = dhcpv6_proxy_set_server_2 ((ip6_address_t *)(&mp->dhcp_server),
2940                                 (ip6_address_t *)(&mp->dhcp_src_address),
2941                                 (u32) ntohl(mp->rx_vrf_id),
2942                                 (u32) ntohl(mp->server_vrf_id),
2943                                 (int) mp->insert_circuit_id,
2944                                 (int) (mp->is_add == 0));
2945 #else
2946     rv = VNET_API_ERROR_UNIMPLEMENTED;
2947 #endif
2948     
2949     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_2_REPLY);
2950 }
2951
2952
2953 static void
2954 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t *mp)
2955 {
2956     vl_api_dhcp_proxy_set_vss_reply_t *rmp;
2957     int rv;
2958     if (!mp->is_ipv6) 
2959         rv = dhcp_proxy_set_option82_vss(ntohl(mp->tbl_id),
2960                                          ntohl(mp->oui),
2961                                          ntohl(mp->fib_id),
2962                                          (int)mp->is_add == 0);
2963     else
2964          rv = dhcpv6_proxy_set_vss( ntohl(mp->tbl_id),
2965                                          ntohl(mp->oui),
2966                                          ntohl(mp->fib_id),
2967                                          (int)mp->is_add == 0);
2968     
2969     REPLY_MACRO(VL_API_DHCP_PROXY_SET_VSS_REPLY);
2970 }
2971
2972
2973 static void vl_api_dhcp_proxy_config_t_handler 
2974 (vl_api_dhcp_proxy_config_t *mp)
2975 {
2976     if (mp->is_ipv6 == 0)
2977         dhcpv4_proxy_config (mp);
2978     else
2979         dhcpv6_proxy_config (mp);
2980 }
2981
2982 static void vl_api_dhcp_proxy_config_2_t_handler 
2983 (vl_api_dhcp_proxy_config_2_t *mp)
2984 {
2985     if (mp->is_ipv6 == 0)
2986         dhcpv4_proxy_config_2 (mp);
2987     else
2988         dhcpv6_proxy_config_2 (mp);
2989 }
2990
2991 void dhcp_compl_event_callback (u32 client_index, u32 pid, u8 * hostname,
2992        u8 is_ipv6, u8 * host_address, u8 * router_address, u8 * host_mac)
2993 {
2994     unix_shared_memory_queue_t * q;
2995     vl_api_dhcp_compl_event_t * mp;
2996
2997     q = vl_api_client_index_to_input_queue (client_index);
2998     if (!q)
2999         return;
3000
3001     mp = vl_msg_api_alloc (sizeof (*mp));
3002     mp->client_index = client_index;
3003     mp->pid = pid;
3004     mp->is_ipv6 = is_ipv6;
3005     clib_memcpy (&mp->hostname, hostname, vec_len(hostname));
3006     mp->hostname[vec_len(hostname) + 1] = '\n';
3007     clib_memcpy (&mp->host_address[0], host_address, 16);
3008     clib_memcpy (&mp->router_address[0], router_address, 16);
3009     clib_memcpy (&mp->host_mac[0], host_mac, 6);
3010
3011     mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
3012
3013     vl_msg_api_send_shmem (q, (u8 *)&mp);
3014 }
3015
3016 static void vl_api_dhcp_client_config_t_handler 
3017 (vl_api_dhcp_client_config_t *mp)
3018 {
3019     vlib_main_t *vm = vlib_get_main();
3020     vl_api_dhcp_client_config_reply_t * rmp;
3021     int rv = 0;
3022
3023     VALIDATE_SW_IF_INDEX(mp);
3024
3025     rv = dhcp_client_config(vm, ntohl(mp->sw_if_index), 
3026              mp->hostname, mp->is_add, mp->client_index,
3027              mp->want_dhcp_event ? dhcp_compl_event_callback : NULL,
3028              mp->pid);
3029
3030     BAD_SW_IF_INDEX_LABEL;
3031
3032     REPLY_MACRO(VL_API_DHCP_CLIENT_CONFIG_REPLY);
3033 }
3034
3035 static void
3036 vl_api_sw_interface_ip6nd_ra_config_t_handler 
3037 (vl_api_sw_interface_ip6nd_ra_config_t *mp, vlib_main_t *vm)
3038 {
3039    vl_api_sw_interface_ip6nd_ra_config_reply_t * rmp;
3040     int rv = 0;
3041     u8  is_no,  surpress, managed, other, ll_option, send_unicast, cease, default_router;
3042  
3043     is_no = mp->is_no == 1;
3044     surpress = mp->surpress == 1;
3045     managed = mp->managed == 1;
3046    other = mp->other == 1;
3047     ll_option = mp->ll_option == 1;
3048     send_unicast = mp->send_unicast == 1;
3049     cease = mp->cease == 1;
3050     default_router = mp->default_router  == 1;
3051
3052     VALIDATE_SW_IF_INDEX(mp);
3053
3054     rv = ip6_neighbor_ra_config(vm, ntohl(mp->sw_if_index), 
3055                                 surpress,  managed,  other,
3056                                 ll_option,  send_unicast,  cease, 
3057                                 default_router, ntohl (mp->lifetime),
3058                                 ntohl(mp->initial_count),  ntohl(mp->initial_interval),  
3059                                 ntohl(mp->max_interval), ntohl( mp->min_interval),
3060                                 is_no);
3061
3062     BAD_SW_IF_INDEX_LABEL;
3063
3064     REPLY_MACRO(VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
3065 }
3066
3067 static void
3068 vl_api_sw_interface_ip6nd_ra_prefix_t_handler 
3069 (vl_api_sw_interface_ip6nd_ra_prefix_t *mp, vlib_main_t *vm)
3070 {
3071    vl_api_sw_interface_ip6nd_ra_prefix_reply_t * rmp;
3072     int rv = 0;
3073     u8  is_no,  use_default,  no_advertise, off_link, no_autoconfig, no_onlink;
3074  
3075     VALIDATE_SW_IF_INDEX(mp);
3076
3077     is_no = mp->is_no == 1;
3078     use_default = mp->use_default == 1;
3079     no_advertise = mp->no_advertise == 1;
3080     off_link = mp->off_link == 1;
3081     no_autoconfig = mp->no_autoconfig == 1;
3082     no_onlink = mp->no_onlink == 1;
3083  
3084     rv = ip6_neighbor_ra_prefix(vm,  ntohl(mp->sw_if_index),  
3085                                 (ip6_address_t *)mp->address,  mp->address_length,
3086                                 use_default,  ntohl(mp->val_lifetime), ntohl(mp->pref_lifetime),
3087                                 no_advertise,  off_link, no_autoconfig, no_onlink,
3088                                 is_no);
3089
3090     BAD_SW_IF_INDEX_LABEL;
3091     REPLY_MACRO(VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
3092 }
3093
3094 static void
3095 vl_api_sw_interface_ip6_enable_disable_t_handler 
3096 (vl_api_sw_interface_ip6_enable_disable_t *mp, vlib_main_t *vm)
3097 {
3098     vl_api_sw_interface_ip6_enable_disable_reply_t * rmp;
3099     vnet_main_t * vnm = vnet_get_main();
3100     int rv = 0;
3101     clib_error_t * error;
3102
3103     vnm->api_errno = 0;
3104
3105     VALIDATE_SW_IF_INDEX(mp);
3106
3107     error = ( mp->enable == 1) ? enable_ip6_interface(vm,ntohl(mp->sw_if_index)) :
3108         disable_ip6_interface(vm,ntohl(mp->sw_if_index));
3109     
3110     if (error)  {
3111         clib_error_report(error);
3112         rv = VNET_API_ERROR_UNSPECIFIED;
3113     } else {
3114         rv = vnm->api_errno;
3115     }
3116     
3117     BAD_SW_IF_INDEX_LABEL;
3118
3119     REPLY_MACRO(VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
3120 }
3121
3122 static void
3123 vl_api_sw_interface_ip6_set_link_local_address_t_handler 
3124 (vl_api_sw_interface_ip6_set_link_local_address_t *mp, vlib_main_t *vm)
3125 {
3126     vl_api_sw_interface_ip6_set_link_local_address_reply_t * rmp;
3127     int rv = 0;
3128     clib_error_t * error;
3129     vnet_main_t * vnm = vnet_get_main();
3130
3131     vnm->api_errno = 0;
3132
3133     VALIDATE_SW_IF_INDEX(mp);
3134
3135     error = set_ip6_link_local_address(vm,
3136                                        ntohl(mp->sw_if_index),
3137                                        (ip6_address_t *)mp->address,
3138                                        mp->address_length);
3139     if (error)  {
3140       clib_error_report(error);
3141       rv = VNET_API_ERROR_UNSPECIFIED;
3142     } else {
3143         rv = vnm->api_errno;
3144     }
3145
3146     BAD_SW_IF_INDEX_LABEL;
3147     
3148     REPLY_MACRO(VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
3149 }
3150
3151 static void set_ip6_flow_hash (vl_api_set_ip_flow_hash_t *mp)
3152 {
3153     vl_api_set_ip_flow_hash_reply_t *rmp;
3154     int rv = VNET_API_ERROR_UNIMPLEMENTED;
3155
3156     clib_warning ("unimplemented...");
3157     
3158     REPLY_MACRO(VL_API_SET_IP_FLOW_HASH_REPLY);
3159 }
3160
3161 static void set_ip4_flow_hash (vl_api_set_ip_flow_hash_t *mp)
3162 {
3163     vl_api_set_ip_flow_hash_reply_t *rmp;
3164     int rv;
3165     u32 table_id;
3166     u32 flow_hash_config = 0;
3167
3168     table_id = ntohl(mp->vrf_id);
3169
3170 #define _(a,b) if (mp->a) flow_hash_config |= b;
3171     foreach_flow_hash_bit;
3172 #undef _
3173
3174     rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
3175
3176     REPLY_MACRO(VL_API_SET_IP_FLOW_HASH_REPLY);
3177 }
3178
3179
3180 static void vl_api_set_ip_flow_hash_t_handler
3181 (vl_api_set_ip_flow_hash_t *mp)
3182 {
3183     if (mp->is_ipv6 == 0)
3184         set_ip4_flow_hash (mp);
3185     else
3186         set_ip6_flow_hash (mp);
3187 }
3188
3189 static void vl_api_sw_interface_set_unnumbered_t_handler 
3190 (vl_api_sw_interface_set_unnumbered_t *mp)
3191 {
3192     vl_api_sw_interface_set_unnumbered_reply_t * rmp;
3193     int rv = 0;
3194     vnet_sw_interface_t * si;
3195     vnet_main_t *vnm = vnet_get_main();
3196     u32 sw_if_index, unnumbered_sw_if_index;
3197
3198     sw_if_index = ntohl(mp->sw_if_index);
3199     unnumbered_sw_if_index = ntohl(mp->unnumbered_sw_if_index);
3200
3201     /*
3202      * The API message field names are backwards from 
3203      * the underlying data structure names.
3204      * It's not worth changing them now.
3205      */
3206     if (pool_is_free_index (vnm->interface_main.sw_interfaces, 
3207                             unnumbered_sw_if_index)) {
3208         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
3209         goto done;
3210     }
3211     
3212     /* Only check the "use loop0" field when setting the binding */
3213     if (mp->is_add && 
3214         pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index)) {
3215         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
3216         goto done;
3217     }
3218
3219     si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
3220
3221     if (mp->is_add) {
3222         si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
3223         si->unnumbered_sw_if_index = sw_if_index;
3224     } else {
3225         si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
3226         si->unnumbered_sw_if_index = (u32)~0;
3227     }
3228     
3229  done:
3230     REPLY_MACRO(VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
3231 }
3232
3233 static void vl_api_create_loopback_t_handler
3234 (vl_api_create_loopback_t *mp)
3235 {
3236     vl_api_create_loopback_reply_t * rmp;
3237     u32 sw_if_index;
3238     int rv;
3239     
3240     rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address);
3241
3242     REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
3243     ({
3244         rmp->sw_if_index = ntohl (sw_if_index);
3245     }));
3246 }
3247
3248 static void vl_api_delete_loopback_t_handler
3249 (vl_api_delete_loopback_t *mp)
3250 {
3251     vl_api_delete_loopback_reply_t * rmp;
3252     u32 sw_if_index;
3253     int rv;
3254
3255     sw_if_index = ntohl (mp->sw_if_index);
3256     rv = vnet_delete_loopback_interface (sw_if_index);
3257
3258     REPLY_MACRO(VL_API_DELETE_LOOPBACK_REPLY);
3259 }
3260
3261 static void vl_api_control_ping_t_handler
3262 (vl_api_control_ping_t *mp)
3263 {
3264     vl_api_control_ping_reply_t * rmp;
3265     int rv = 0;
3266
3267     REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
3268     ({
3269         rmp->vpe_pid = ntohl (getpid());
3270     }));
3271 }
3272
3273 static void shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
3274 {
3275     u8 **shmem_vecp = (u8 **)arg;
3276     u8 *shmem_vec;
3277     void *oldheap;
3278     api_main_t * am = &api_main;
3279     u32 offset;
3280
3281     shmem_vec = *shmem_vecp;
3282
3283     offset = vec_len (shmem_vec);
3284
3285     pthread_mutex_lock (&am->vlib_rp->mutex);
3286     oldheap = svm_push_data_heap (am->vlib_rp);
3287     
3288     vec_validate (shmem_vec, offset + buffer_bytes - 1);
3289
3290     clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
3291
3292     svm_pop_heap (oldheap);
3293     pthread_mutex_unlock (&am->vlib_rp->mutex);
3294
3295     *shmem_vecp = shmem_vec;
3296 }
3297
3298
3299 static void vl_api_cli_request_t_handler 
3300 (vl_api_cli_request_t *mp)
3301 {
3302     vl_api_cli_reply_t *rp;
3303     unix_shared_memory_queue_t *q;
3304     vlib_main_t * vm = vlib_get_main();
3305     api_main_t * am = &api_main;
3306     unformat_input_t input;
3307     u8 *shmem_vec=0;
3308     void *oldheap;
3309
3310     q = vl_api_client_index_to_input_queue (mp->client_index);
3311     if (!q)
3312         return;
3313
3314     rp = vl_msg_api_alloc (sizeof (*rp));
3315     rp->_vl_msg_id = ntohs(VL_API_CLI_REPLY);
3316     rp->context = mp->context;
3317
3318     unformat_init_vector (&input, (u8 *)(uword)mp->cmd_in_shmem);
3319
3320     vlib_cli_input (vm, &input, shmem_cli_output, 
3321                     (uword)&shmem_vec);
3322
3323     pthread_mutex_lock (&am->vlib_rp->mutex);
3324     oldheap = svm_push_data_heap (am->vlib_rp);
3325     
3326     vec_add1(shmem_vec, 0);
3327
3328     svm_pop_heap (oldheap);
3329     pthread_mutex_unlock (&am->vlib_rp->mutex);
3330
3331     rp->reply_in_shmem = (uword)shmem_vec;
3332
3333     vl_msg_api_send_shmem (q, (u8 *)&rp);
3334 }
3335
3336 static void vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t *mp)
3337 {
3338     int rv;
3339     vl_api_set_arp_neighbor_limit_reply_t * rmp;
3340     vnet_main_t *vnm = vnet_get_main();
3341     clib_error_t * error;
3342
3343     vnm->api_errno = 0;
3344
3345     if (mp->is_ipv6)
3346         error = ip6_set_neighbor_limit (ntohl(mp->arp_neighbor_limit));
3347     else
3348         error = ip4_set_arp_limit (ntohl(mp->arp_neighbor_limit));
3349     
3350     if (error)  {
3351         clib_error_report(error);
3352         rv = VNET_API_ERROR_UNSPECIFIED;
3353     } else {
3354         rv = vnm->api_errno;
3355     }
3356
3357     REPLY_MACRO(VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3358 }
3359
3360 static void vl_api_sr_tunnel_add_del_t_handler
3361 (vl_api_sr_tunnel_add_del_t *mp)
3362 {
3363 #if IPV6SR == 0
3364     clib_warning ("unimplemented");
3365 #else
3366     ip6_sr_add_del_tunnel_args_t _a, *a=&_a;
3367     int rv = 0;
3368     vl_api_sr_tunnel_add_del_reply_t * rmp;
3369     ip6_address_t * segments = 0, * seg;
3370     ip6_address_t * tags = 0, *tag;
3371     ip6_address_t * this_address;
3372     int i;
3373
3374     if (mp->n_segments == 0) {
3375         rv = -11;
3376         goto out;
3377     }
3378
3379     memset (a, 0, sizeof (*a));
3380     a->src_address = (ip6_address_t *)&mp->src_address;
3381     a->dst_address = (ip6_address_t *)&mp->dst_address;
3382     a->dst_mask_width = mp->dst_mask_width;
3383     a->flags_net_byte_order = mp->flags_net_byte_order;
3384     a->is_del = (mp->is_add == 0);
3385     a->rx_table_id = ntohl(mp->outer_vrf_id);
3386     a->tx_table_id = ntohl(mp->inner_vrf_id);
3387
3388     /* Yank segments and tags out of the API message */
3389     this_address = (ip6_address_t *)mp->segs_and_tags;
3390     for (i = 0; i < mp->n_segments; i++) {
3391         vec_add2 (segments, seg, 1);
3392         clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
3393         this_address++;
3394     }
3395     for (i = 0; i < mp->n_tags; i++) {
3396         vec_add2 (tags, tag, 1);
3397         clib_memcpy (tag->as_u8, this_address->as_u8, sizeof (*this_address));
3398         this_address++;
3399     }
3400
3401     a->segments = segments;
3402     a->tags = tags;
3403
3404     rv = ip6_sr_add_del_tunnel (a);
3405
3406 out:
3407
3408     REPLY_MACRO(VL_API_SR_TUNNEL_ADD_DEL_REPLY);
3409 #endif
3410 }
3411
3412 #define foreach_classify_add_del_table_field    \
3413 _(table_index)                                  \
3414 _(nbuckets)                                     \
3415 _(memory_size)                                  \
3416 _(skip_n_vectors)                               \
3417 _(match_n_vectors)                              \
3418 _(next_table_index)                             \
3419 _(miss_next_index)
3420
3421 static void vl_api_classify_add_del_table_t_handler
3422 (vl_api_classify_add_del_table_t * mp)
3423 {
3424     vl_api_classify_add_del_table_reply_t * rmp;
3425     vnet_classify_main_t * cm = &vnet_classify_main;
3426     vnet_classify_table_t * t;
3427     int rv;
3428
3429 #define _(a) u32 a;
3430     foreach_classify_add_del_table_field;
3431 #undef _
3432
3433 #define _(a) a = ntohl(mp->a);    
3434     foreach_classify_add_del_table_field;
3435 #undef _
3436
3437     /* The underlying API fails silently, on purpose, so check here */
3438     if (mp->is_add == 0) 
3439         if (pool_is_free_index (cm->tables, table_index)) {
3440             rv = VNET_API_ERROR_NO_SUCH_TABLE;
3441             goto out;
3442         }
3443
3444     rv = vnet_classify_add_del_table 
3445         (cm, mp->mask, nbuckets, memory_size, 
3446          skip_n_vectors, match_n_vectors, 
3447          next_table_index, miss_next_index,
3448          &table_index, mp->is_add);
3449     
3450 out:
3451     REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
3452     ({
3453         if (rv == 0 && mp->is_add) {
3454             t = pool_elt_at_index (cm->tables, table_index);
3455             rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
3456             rmp->match_n_vectors = ntohl(t->match_n_vectors);
3457             rmp->new_table_index = ntohl(table_index);
3458         } else {
3459             rmp->skip_n_vectors = ~0;
3460             rmp->match_n_vectors = ~0;
3461             rmp->new_table_index = ~0;
3462         }
3463     }));
3464 }
3465
3466 static void vl_api_classify_add_del_session_t_handler
3467 (vl_api_classify_add_del_session_t * mp)
3468 {
3469     vnet_classify_main_t * cm = &vnet_classify_main;
3470     vl_api_classify_add_del_session_reply_t * rmp;
3471     int rv;
3472     u32 table_index, hit_next_index, opaque_index;
3473     i32 advance;
3474
3475     table_index = ntohl (mp->table_index);
3476     hit_next_index = ntohl (mp->hit_next_index);
3477     opaque_index = ntohl (mp->opaque_index);
3478     advance = ntohl (mp->advance);
3479     
3480     rv = vnet_classify_add_del_session 
3481         (cm, table_index, mp->match, hit_next_index, opaque_index,
3482          advance, mp->is_add);
3483
3484     REPLY_MACRO(VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
3485 }
3486
3487 static void vl_api_classify_set_interface_ip_table_t_handler 
3488 (vl_api_classify_set_interface_ip_table_t * mp)
3489 {
3490     vlib_main_t * vm = vlib_get_main();
3491     vl_api_classify_set_interface_ip_table_reply_t * rmp;
3492     int rv;
3493     u32 table_index, sw_if_index;
3494
3495     table_index = ntohl (mp->table_index);
3496     sw_if_index = ntohl (mp->sw_if_index);
3497     
3498     VALIDATE_SW_IF_INDEX(mp);
3499
3500     if (mp->is_ipv6)
3501         rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3502     else
3503         rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
3504
3505     BAD_SW_IF_INDEX_LABEL;
3506
3507     REPLY_MACRO(VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
3508 }
3509
3510 static void vl_api_classify_set_interface_l2_tables_t_handler 
3511 (vl_api_classify_set_interface_l2_tables_t * mp)
3512 {
3513     vl_api_classify_set_interface_l2_tables_reply_t * rmp;
3514     int rv;
3515     u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
3516     int enable;
3517
3518     ip4_table_index = ntohl(mp->ip4_table_index);
3519     ip6_table_index = ntohl(mp->ip6_table_index);
3520     other_table_index = ntohl(mp->other_table_index);
3521     sw_if_index = ntohl(mp->sw_if_index);
3522
3523     VALIDATE_SW_IF_INDEX(mp);
3524
3525     rv = vnet_l2_classify_set_tables (sw_if_index, ip4_table_index, 
3526                                       ip6_table_index, other_table_index);
3527
3528     if (rv == 0) {
3529         if (ip4_table_index != ~0 || ip6_table_index != ~0 
3530             || other_table_index != ~0)
3531             enable = 1;
3532         else
3533             enable = 0;
3534             
3535         vnet_l2_classify_enable_disable (sw_if_index, enable);
3536     }
3537
3538     BAD_SW_IF_INDEX_LABEL;
3539
3540     REPLY_MACRO(VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
3541 }
3542
3543 static void 
3544 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t *mp)
3545 {
3546     int rv = 0;
3547     vl_api_l2_fib_clear_table_reply_t * rmp;
3548
3549     /* DAW-FIXME: This API should only clear non-static l2fib entries, but
3550      *            that is not currently implemented.  When that TODO is fixed
3551      *            this call should be changed to pass 1 instead of 0.
3552      */
3553     l2fib_clear_table (0);
3554
3555     REPLY_MACRO(VL_API_L2_FIB_CLEAR_TABLE_REPLY);
3556 }
3557
3558 extern void l2_efp_filter_configure(vnet_main_t * vnet_main,
3559                                     u32           sw_if_index,
3560                                     u32           enable);
3561
3562 static void 
3563 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *mp)
3564 {
3565     int rv;
3566     vl_api_l2_interface_efp_filter_reply_t * rmp;
3567     vnet_main_t *vnm = vnet_get_main();
3568
3569     // enable/disable the feature
3570     l2_efp_filter_configure (vnm, mp->sw_if_index, mp->enable_disable);
3571     rv = vnm->api_errno;
3572
3573     REPLY_MACRO(VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
3574 }
3575
3576 static void 
3577 vl_api_l2_interface_vlan_tag_rewrite_t_handler (vl_api_l2_interface_vlan_tag_rewrite_t *mp)
3578 {
3579     int rv = 0;
3580     vl_api_l2_interface_vlan_tag_rewrite_reply_t * rmp;
3581     vnet_main_t * vnm = vnet_get_main();
3582     vlib_main_t * vm = vlib_get_main();
3583     u32 vtr_op;
3584
3585     VALIDATE_SW_IF_INDEX(mp);
3586
3587     vtr_op = ntohl(mp->vtr_op);
3588
3589     /* The L2 code is unsuspicious */
3590     switch(vtr_op) {
3591     case L2_VTR_DISABLED:
3592     case L2_VTR_PUSH_1:
3593     case L2_VTR_PUSH_2:
3594     case L2_VTR_POP_1:
3595     case L2_VTR_POP_2:
3596     case L2_VTR_TRANSLATE_1_1:
3597     case L2_VTR_TRANSLATE_1_2:
3598     case L2_VTR_TRANSLATE_2_1:
3599     case L2_VTR_TRANSLATE_2_2:
3600         break;
3601
3602     default:
3603         rv = VNET_API_ERROR_INVALID_VALUE;
3604         goto bad_sw_if_index;
3605     }
3606
3607     rv = l2vtr_configure (vm, vnm, ntohl(mp->sw_if_index), vtr_op, 
3608                           ntohl(mp->push_dot1q), ntohl(mp->tag1), 
3609                           ntohl(mp->tag2));
3610     
3611     BAD_SW_IF_INDEX_LABEL;
3612
3613     REPLY_MACRO(VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
3614 }
3615
3616 static void
3617 vl_api_create_vhost_user_if_t_handler (vl_api_create_vhost_user_if_t *mp)
3618 {
3619 #if DPDK > 0
3620     int rv = 0;
3621     vl_api_create_vhost_user_if_reply_t * rmp;
3622     u32 sw_if_index = (u32)~0;
3623
3624     vnet_main_t * vnm = vnet_get_main();
3625     vlib_main_t * vm = vlib_get_main();
3626
3627     rv = dpdk_vhost_user_create_if(vnm, vm, (char *)mp->sock_filename,
3628                               mp->is_server, &sw_if_index, (u64)~0,
3629                               mp->renumber, ntohl(mp->custom_dev_instance),
3630                               (mp->use_custom_mac)?mp->mac_address:NULL);
3631
3632     REPLY_MACRO2(VL_API_CREATE_VHOST_USER_IF_REPLY,
3633     ({
3634       rmp->sw_if_index = ntohl (sw_if_index);
3635     }));
3636 #endif
3637 }
3638
3639 static void
3640 vl_api_modify_vhost_user_if_t_handler (vl_api_modify_vhost_user_if_t *mp)
3641 {
3642 #if DPDK > 0
3643     int rv = 0;
3644     vl_api_modify_vhost_user_if_reply_t * rmp;
3645     u32 sw_if_index = ntohl(mp->sw_if_index);
3646
3647     vnet_main_t * vnm = vnet_get_main();
3648     vlib_main_t * vm = vlib_get_main();
3649
3650     rv = dpdk_vhost_user_modify_if(vnm, vm, (char *)mp->sock_filename,
3651                               mp->is_server, sw_if_index, (u64)~0,
3652                               mp->renumber, ntohl(mp->custom_dev_instance));
3653
3654     REPLY_MACRO(VL_API_MODIFY_VHOST_USER_IF_REPLY);
3655 #endif
3656 }
3657
3658 static void
3659 vl_api_delete_vhost_user_if_t_handler (vl_api_delete_vhost_user_if_t *mp)
3660 {
3661 #if DPDK > 0
3662     int rv = 0;
3663     vpe_api_main_t * vam = &vpe_api_main;
3664     vl_api_delete_vhost_user_if_reply_t * rmp;
3665     u32 sw_if_index = ntohl(mp->sw_if_index);
3666
3667     vnet_main_t * vnm = vnet_get_main();
3668     vlib_main_t * vm = vlib_get_main();
3669
3670     rv = dpdk_vhost_user_delete_if(vnm, vm, sw_if_index);
3671
3672     REPLY_MACRO(VL_API_DELETE_VHOST_USER_IF_REPLY);
3673     if (!rv) {
3674         unix_shared_memory_queue_t * q =
3675             vl_api_client_index_to_input_queue (mp->client_index);
3676         if (!q)
3677             return;
3678
3679         send_sw_interface_flags_deleted (vam, q, sw_if_index);
3680     }
3681 #endif
3682 }
3683
3684 static void vl_api_sw_interface_vhost_user_details_t_handler (
3685     vl_api_sw_interface_vhost_user_details_t * mp)
3686 {
3687     clib_warning ("BUG");
3688 }
3689
3690 #if DPDK > 0
3691 static void send_sw_interface_vhost_user_details (vpe_api_main_t * am,
3692                                        unix_shared_memory_queue_t *q,
3693                                        vhost_user_intf_details_t * vui)
3694 {
3695     vl_api_sw_interface_vhost_user_details_t * mp;
3696
3697     mp = vl_msg_api_alloc (sizeof (*mp));
3698     memset (mp, 0, sizeof (*mp));
3699     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_VHOST_USER_DETAILS);
3700     mp->sw_if_index = ntohl(vui->sw_if_index);
3701     mp->virtio_net_hdr_sz = ntohl (vui->virtio_net_hdr_sz);
3702     mp->features = clib_net_to_host_u64 (vui->features);
3703     mp->is_server = vui->is_server;
3704     mp->num_regions = ntohl(vui->num_regions);
3705     mp->sock_errno = ntohl(vui->sock_errno);
3706
3707     strncpy ((char *) mp->sock_filename,
3708              (char *) vui->sock_filename, ARRAY_LEN(mp->sock_filename)-1);
3709     strncpy ((char *) mp->interface_name,
3710              (char *) vui->if_name, ARRAY_LEN(mp->interface_name)-1);
3711
3712     vl_msg_api_send_shmem (q, (u8 *)&mp);
3713 }
3714 #endif
3715
3716 static void
3717 vl_api_sw_interface_vhost_user_dump_t_handler (
3718         vl_api_sw_interface_vhost_user_dump_t *mp)
3719 {
3720 #if DPDK > 0
3721     int rv = 0;
3722     vpe_api_main_t * am = &vpe_api_main;
3723     vnet_main_t * vnm = vnet_get_main();
3724     vlib_main_t * vm = vlib_get_main();
3725     vhost_user_intf_details_t *ifaces = NULL;
3726     vhost_user_intf_details_t *vuid = NULL;
3727     unix_shared_memory_queue_t * q;
3728
3729     q = vl_api_client_index_to_input_queue (mp->client_index);
3730     if (q == 0)
3731         return;
3732
3733     rv = dpdk_vhost_user_dump_ifs(vnm, vm, &ifaces);
3734     if (rv)
3735         return;
3736
3737     vec_foreach (vuid, ifaces) {
3738         send_sw_interface_vhost_user_details (am, q, vuid);
3739     }
3740     vec_free(ifaces);
3741 #endif
3742 }
3743
3744 static void send_sw_if_l2tpv3_tunnel_details (vpe_api_main_t * am,
3745                                        unix_shared_memory_queue_t *q,
3746                                        l2t_session_t *s,
3747                                        l2t_main_t * lm)
3748 {
3749     vl_api_sw_if_l2tpv3_tunnel_details_t * mp;
3750     u8 * if_name = NULL;
3751     vnet_sw_interface_t * si = NULL;
3752
3753     si = vnet_get_hw_sw_interface (lm->vnet_main, s->hw_if_index);
3754
3755     if_name = format(if_name, "%U",
3756                      format_vnet_sw_interface_name, lm->vnet_main, si);
3757
3758     mp = vl_msg_api_alloc (sizeof (*mp));
3759     memset (mp, 0, sizeof (*mp));
3760     mp->_vl_msg_id = ntohs(VL_API_SW_IF_L2TPV3_TUNNEL_DETAILS);
3761     strncpy((char *)mp->interface_name,
3762             (char *)if_name, ARRAY_LEN(mp->interface_name)-1);
3763     mp->sw_if_index = ntohl(si->sw_if_index);
3764     mp->local_session_id = s->local_session_id;
3765     mp->remote_session_id = s->remote_session_id;
3766     mp->local_cookie[0] = s->local_cookie[0];
3767     mp->local_cookie[1] = s->local_cookie[1];
3768     mp->remote_cookie = s->remote_cookie;
3769     clib_memcpy(mp->client_address, &s->client_address, sizeof(s->client_address));
3770     clib_memcpy(mp->our_address, &s->our_address, sizeof(s->our_address));
3771     mp->l2_sublayer_present = s->l2_sublayer_present;
3772
3773     vl_msg_api_send_shmem (q, (u8 *)&mp);
3774 }
3775
3776 static void send_ip_address_details (vpe_api_main_t * am,
3777                                      unix_shared_memory_queue_t * q,
3778                                      u8 * ip,
3779                                      u16 prefix_length,
3780                                      u8 is_ipv6)
3781 {
3782     vl_api_ip_address_details_t * mp;
3783
3784     mp = vl_msg_api_alloc (sizeof (*mp));
3785     memset (mp, 0, sizeof (*mp));
3786     mp->_vl_msg_id = ntohs(VL_API_IP_ADDRESS_DETAILS);
3787
3788     if (is_ipv6) {
3789         clib_memcpy(&mp->ip, ip, sizeof(mp->ip));
3790     } else {
3791         u32 * tp = (u32 *)mp->ip;
3792         *tp = ntohl(*(u32*)ip);
3793     }
3794     mp->prefix_length = prefix_length;
3795
3796     vl_msg_api_send_shmem (q, (u8 *)&mp);
3797 }
3798
3799 static void
3800 vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t *mp)
3801 {
3802     vpe_api_main_t * am = &vpe_api_main;
3803     unix_shared_memory_queue_t * q;
3804     ip6_address_t * r6;
3805     ip4_address_t * r4;
3806     ip6_main_t * im6 = &ip6_main;
3807     ip4_main_t * im4 = &ip4_main;
3808     ip_lookup_main_t * lm6 = &im6->lookup_main;
3809     ip_lookup_main_t * lm4 = &im4->lookup_main;
3810     ip_interface_address_t * ia = 0;
3811     u32 sw_if_index = ~0;
3812
3813     sw_if_index = ntohl(mp->sw_if_index);
3814
3815     q = vl_api_client_index_to_input_queue (mp->client_index);
3816     if (q == 0) {
3817         return;
3818     }
3819
3820     if (mp->is_ipv6) {
3821         foreach_ip_interface_address (lm6, ia, sw_if_index,
3822                                       1 /* honor unnumbered */,
3823         ({
3824             r6 = ip_interface_address_get_address (lm6, ia);
3825             u16 prefix_length = ia->address_length;
3826             send_ip_address_details(am, q, (u8*)r6, prefix_length, 1);
3827         }));
3828     } else {
3829         foreach_ip_interface_address (lm4, ia, sw_if_index,
3830                                       1 /* honor unnumbered */,
3831         ({
3832             r4 = ip_interface_address_get_address (lm4, ia);
3833             u16 prefix_length = ia->address_length;
3834             send_ip_address_details(am, q, (u8*)r4, prefix_length, 0);
3835         }));
3836     }
3837 }
3838
3839 static void send_ip_details (vpe_api_main_t * am,
3840                                unix_shared_memory_queue_t *q,
3841                                u32 sw_if_index)
3842 {
3843     vl_api_ip_details_t * mp;
3844
3845     mp = vl_msg_api_alloc (sizeof (*mp));
3846     memset (mp, 0, sizeof (*mp));
3847     mp->_vl_msg_id = ntohs(VL_API_IP_DETAILS);
3848
3849     mp->sw_if_index = ntohl(sw_if_index);
3850
3851     vl_msg_api_send_shmem (q, (u8 *)&mp);
3852 }
3853
3854 static void
3855 vl_api_sw_if_l2tpv3_tunnel_dump_t_handler (
3856         vl_api_sw_if_l2tpv3_tunnel_dump_t *mp)
3857 {
3858     vpe_api_main_t * am = &vpe_api_main;
3859     l2t_main_t * lm = &l2t_main;
3860     unix_shared_memory_queue_t * q;
3861     l2t_session_t *session;
3862
3863     q = vl_api_client_index_to_input_queue (mp->client_index);
3864     if (q == 0)
3865         return;
3866
3867     pool_foreach (session, lm->sessions, 
3868     ({
3869         send_sw_if_l2tpv3_tunnel_details (am, q, session, lm);
3870     }));
3871 }
3872
3873
3874 static void send_sw_interface_tap_details (vpe_api_main_t * am,
3875                                        unix_shared_memory_queue_t *q,
3876                                        tapcli_interface_details_t *tap_if)
3877 {
3878     vl_api_sw_interface_tap_details_t * mp;
3879     mp = vl_msg_api_alloc (sizeof (*mp));
3880     memset (mp, 0, sizeof (*mp));
3881     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_TAP_DETAILS);
3882     mp->sw_if_index = ntohl(tap_if->sw_if_index);
3883     strncpy((char *)mp->dev_name,
3884             (char *)tap_if->dev_name, ARRAY_LEN(mp->dev_name)-1);
3885
3886     vl_msg_api_send_shmem (q, (u8 *)&mp);
3887 }
3888
3889 static void
3890 vl_api_sw_interface_tap_dump_t_handler (
3891         vl_api_sw_interface_tap_dump_t *mp)
3892 {
3893     int rv = 0;
3894     vpe_api_main_t * am = &vpe_api_main;
3895     unix_shared_memory_queue_t * q;
3896     tapcli_interface_details_t *tapifs = NULL;
3897     tapcli_interface_details_t *tap_if = NULL;
3898
3899     q = vl_api_client_index_to_input_queue (mp->client_index);
3900     if (q == 0)
3901         return;
3902
3903     rv = vnet_tap_dump_ifs(&tapifs);
3904     if (rv)
3905         return;
3906
3907     vec_foreach(tap_if, tapifs) {
3908         send_sw_interface_tap_details(am, q, tap_if);
3909     }
3910
3911     vec_free(tapifs);
3912 }
3913
3914 static void
3915 vl_api_ip_dump_t_handler (vl_api_ip_dump_t *mp)
3916 {
3917     vpe_api_main_t * am = &vpe_api_main;
3918     vnet_main_t * vnm = vnet_get_main();
3919     vlib_main_t * vm = vlib_get_main();
3920     vnet_interface_main_t * im = &vnm->interface_main;
3921     unix_shared_memory_queue_t * q;
3922     vnet_sw_interface_t * si, * sorted_sis;
3923     u32 sw_if_index = ~0;
3924
3925     q = vl_api_client_index_to_input_queue (mp->client_index);
3926     if (q == 0) {
3927         return;
3928     }
3929
3930     /* Gather interfaces. */
3931     sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
3932     _vec_len (sorted_sis) = 0;
3933     pool_foreach (si, im->sw_interfaces, ({ vec_add1 (sorted_sis, si[0]); }));
3934
3935     vec_foreach (si, sorted_sis) {
3936         if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)) {
3937             if (mp->is_ipv6 && !ip6_interface_enabled(vm, si->sw_if_index)) {
3938                 continue;
3939             }
3940             sw_if_index = si->sw_if_index;
3941             send_ip_details(am, q, sw_if_index);
3942         }
3943     }
3944 }
3945
3946 static void vl_api_l2_fib_table_entry_t_handler (
3947     vl_api_l2_fib_table_entry_t * mp)
3948 {
3949     clib_warning ("BUG");
3950 }
3951
3952 static void send_l2fib_table_entry (vpe_api_main_t * am,
3953                                     unix_shared_memory_queue_t *q,
3954                                     l2fib_entry_key_t * l2fe_key,
3955                                     l2fib_entry_result_t * l2fe_res)
3956 {
3957     vl_api_l2_fib_table_entry_t * mp;
3958
3959     mp = vl_msg_api_alloc (sizeof (*mp));
3960     memset (mp, 0, sizeof (*mp));
3961     mp->_vl_msg_id = ntohs(VL_API_L2_FIB_TABLE_ENTRY);
3962
3963     mp->bd_id = ntohl(l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
3964
3965     mp->mac = l2fib_make_key (l2fe_key->fields.mac, 0);
3966     mp->sw_if_index = ntohl(l2fe_res->fields.sw_if_index);
3967     mp->static_mac = l2fe_res->fields.static_mac;
3968     mp->filter_mac = l2fe_res->fields.filter;
3969     mp->bvi_mac = l2fe_res->fields.bvi;
3970
3971     vl_msg_api_send_shmem (q, (u8 *)&mp);
3972 }
3973
3974 static void
3975 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t *mp)
3976 {
3977     vpe_api_main_t * am = &vpe_api_main;
3978     bd_main_t * bdm = &bd_main;
3979     l2fib_entry_key_t *l2fe_key = NULL;
3980     l2fib_entry_result_t *l2fe_res = NULL;
3981     u32 ni, bd_id = ntohl (mp->bd_id);
3982     u32 bd_index;
3983     unix_shared_memory_queue_t * q;
3984     uword * p;
3985
3986     q = vl_api_client_index_to_input_queue (mp->client_index);
3987     if (q == 0)
3988         return;
3989
3990     /* see l2fib_table_dump: ~0 means "any" */
3991     if (bd_id == ~0)
3992         bd_index = ~0;
3993     else {
3994         p = hash_get (bdm->bd_index_by_bd_id, bd_id);
3995         if (p == 0)
3996             return;
3997         
3998         bd_index = p[0];
3999     }
4000
4001     l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
4002
4003     vec_foreach_index (ni, l2fe_key) {
4004         send_l2fib_table_entry (am, q, vec_elt_at_index(l2fe_key, ni),
4005                                 vec_elt_at_index(l2fe_res, ni));
4006     }
4007     vec_free(l2fe_key);
4008     vec_free(l2fe_res);
4009 }
4010
4011 static void
4012 vl_api_show_version_t_handler (vl_api_show_version_t *mp)
4013 {
4014     vl_api_show_version_reply_t *rmp;
4015     int rv = 0;
4016     char * vpe_api_get_build_directory(void);
4017     char * vpe_api_get_version(void);
4018     char * vpe_api_get_build_date(void);
4019
4020     unix_shared_memory_queue_t * q =
4021         vl_api_client_index_to_input_queue (mp->client_index);
4022     
4023     if (!q)
4024         return;
4025     
4026     REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
4027     ({
4028         strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
4029         strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
4030                  ARRAY_LEN(rmp->build_directory)-1);
4031         strncpy ((char *) rmp->version, vpe_api_get_version(),
4032                  ARRAY_LEN(rmp->version)-1);
4033         strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
4034                  ARRAY_LEN(rmp->build_date)-1);
4035     }));
4036 }
4037
4038 static void vl_api_get_node_index_t_handler
4039 (vl_api_get_node_index_t * mp)
4040 {
4041     vlib_main_t * vm = vlib_get_main();
4042     vl_api_get_node_index_reply_t * rmp;
4043     vlib_node_t * n;
4044     int rv = 0;
4045     u32 node_index = ~0;
4046
4047     n = vlib_get_node_by_name (vm, mp->node_name);
4048
4049     if (n == 0)
4050         rv = VNET_API_ERROR_NO_SUCH_NODE;
4051     else
4052         node_index = n->index;
4053
4054     REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY, 
4055     ({
4056         rmp->node_index = ntohl(node_index);
4057     }))
4058 }
4059
4060 static void vl_api_add_node_next_t_handler
4061 (vl_api_add_node_next_t * mp)
4062 {
4063     vlib_main_t * vm = vlib_get_main();
4064     vl_api_add_node_next_reply_t * rmp;
4065     vlib_node_t * n, * next;
4066     int rv = 0;
4067     u32 next_index = ~0;
4068
4069     n = vlib_get_node_by_name (vm, mp->node_name);
4070
4071     if (n == 0) {
4072         rv = VNET_API_ERROR_NO_SUCH_NODE;
4073         goto out;
4074     }
4075
4076     next = vlib_get_node_by_name (vm, mp->next_name);
4077
4078     if (next == 0) 
4079         rv = VNET_API_ERROR_NO_SUCH_NODE2;
4080     else 
4081         next_index = vlib_node_add_next (vm, n->index, next->index);
4082
4083 out:
4084     REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY, 
4085     ({
4086         rmp->next_index = ntohl(next_index);
4087     }))
4088 }
4089
4090 static void vl_api_l2tpv3_create_tunnel_t_handler 
4091 (vl_api_l2tpv3_create_tunnel_t *mp)
4092 {
4093     vl_api_l2tpv3_create_tunnel_reply_t * rmp;
4094     l2t_main_t *lm = &l2t_main;
4095     u32 sw_if_index = (u32)~0;
4096     int rv;
4097     
4098     if (mp->is_ipv6 != 1) {
4099         rv = VNET_API_ERROR_UNIMPLEMENTED;
4100         goto out;
4101     }
4102
4103     rv = create_l2tpv3_ipv6_tunnel (lm,
4104                                (ip6_address_t *) mp->client_address,
4105                                (ip6_address_t *) mp->our_address,
4106                                ntohl(mp->local_session_id),
4107                                ntohl(mp->remote_session_id),
4108                                clib_net_to_host_u64(mp->local_cookie),
4109                                clib_net_to_host_u64(mp->remote_cookie),
4110                                mp->l2_sublayer_present, 
4111                                &sw_if_index);
4112
4113 out:
4114     REPLY_MACRO2(VL_API_L2TPV3_CREATE_TUNNEL_REPLY,
4115     ({
4116         rmp->sw_if_index = ntohl (sw_if_index);
4117     }))
4118 }
4119
4120 static void vl_api_l2tpv3_set_tunnel_cookies_t_handler 
4121 (vl_api_l2tpv3_set_tunnel_cookies_t *mp)
4122 {
4123     vl_api_l2tpv3_set_tunnel_cookies_reply_t * rmp;
4124     l2t_main_t *lm = &l2t_main;
4125     int rv;
4126
4127     VALIDATE_SW_IF_INDEX(mp);
4128
4129     rv = l2tpv3_set_tunnel_cookies (lm, ntohl(mp->sw_if_index),
4130                                   clib_net_to_host_u64(mp->new_local_cookie),
4131                                   clib_net_to_host_u64(mp->new_remote_cookie));
4132
4133     BAD_SW_IF_INDEX_LABEL;
4134     
4135     REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY);
4136 }
4137
4138 static void vl_api_l2tpv3_interface_enable_disable_t_handler 
4139 (vl_api_l2tpv3_interface_enable_disable_t * mp)
4140 {
4141     int rv;
4142     vnet_main_t * vnm = vnet_get_main();
4143     vl_api_l2tpv3_interface_enable_disable_reply_t * rmp;
4144
4145     VALIDATE_SW_IF_INDEX(mp);
4146
4147     rv = l2tpv3_interface_enable_disable 
4148         (vnm, ntohl(mp->sw_if_index), mp->enable_disable);
4149
4150     BAD_SW_IF_INDEX_LABEL;
4151
4152     REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY);
4153 }
4154
4155 static void vl_api_l2tpv3_set_lookup_key_t_handler 
4156 (vl_api_l2tpv3_set_lookup_key_t * mp)
4157 {
4158     int rv = 0;
4159     l2t_main_t *lm = &l2t_main;
4160     vl_api_l2tpv3_set_lookup_key_reply_t * rmp;
4161
4162     if (mp->key > L2T_LOOKUP_SESSION_ID) {
4163         rv = VNET_API_ERROR_INVALID_VALUE;
4164         goto out;
4165     }
4166     
4167     lm->lookup_type = mp->key;
4168
4169 out:
4170     REPLY_MACRO (VL_API_L2TPV3_SET_LOOKUP_KEY_REPLY);
4171 }
4172
4173 static void vl_api_vxlan_add_del_tunnel_t_handler 
4174 (vl_api_vxlan_add_del_tunnel_t * mp)
4175 {
4176     vl_api_vxlan_add_del_tunnel_reply_t * rmp;
4177     int rv = 0;
4178     vnet_vxlan_add_del_tunnel_args_t _a, *a = &_a;
4179     u32 encap_fib_index;
4180     uword * p;
4181     ip4_main_t * im = &ip4_main;
4182     u32 sw_if_index = ~0;
4183
4184     p = hash_get (im->fib_index_by_table_id, ntohl(mp->encap_vrf_id));
4185     if (! p) {
4186         rv = VNET_API_ERROR_NO_SUCH_FIB;
4187         goto out;
4188     }
4189     encap_fib_index = p[0];
4190
4191     memset (a, 0, sizeof (*a));
4192
4193     a->is_add = mp->is_add;
4194
4195     /* ip addresses sent in network byte order */
4196     a->src.as_u32 = mp->src_address;
4197     a->dst.as_u32 = mp->dst_address;
4198
4199     a->encap_fib_index = encap_fib_index;
4200     a->decap_next_index = ntohl(mp->decap_next_index);
4201     a->vni = ntohl(mp->vni);
4202     rv = vnet_vxlan_add_del_tunnel (a, &sw_if_index);
4203     
4204 out:
4205     REPLY_MACRO2(VL_API_VXLAN_ADD_DEL_TUNNEL_REPLY,
4206     ({
4207         rmp->sw_if_index = ntohl (sw_if_index);
4208     }));
4209 }
4210
4211 static void send_vxlan_tunnel_details
4212 (vxlan_tunnel_t * t, unix_shared_memory_queue_t * q)
4213 {
4214     vl_api_vxlan_tunnel_details_t * rmp;
4215     ip4_main_t * im = &ip4_main;
4216
4217     rmp = vl_msg_api_alloc (sizeof (*rmp));
4218     memset (rmp, 0, sizeof (*rmp));
4219     rmp->_vl_msg_id = ntohs(VL_API_VXLAN_TUNNEL_DETAILS);
4220     rmp->src_address = t->src.data_u32;
4221     rmp->dst_address = t->dst.data_u32;
4222     rmp->encap_vrf_id = htonl(im->fibs[t->encap_fib_index].table_id);
4223     rmp->vni = htonl(t->vni);
4224     rmp->decap_next_index = htonl(t->decap_next_index);
4225     rmp->sw_if_index = htonl(t->sw_if_index);
4226
4227     vl_msg_api_send_shmem (q, (u8 *)&rmp);
4228 }
4229
4230 static void vl_api_vxlan_tunnel_dump_t_handler
4231 (vl_api_vxlan_tunnel_dump_t * mp)
4232 {
4233     unix_shared_memory_queue_t * q;
4234     vxlan_main_t * vxm = &vxlan_main;
4235     vxlan_tunnel_t * t;
4236     u32 sw_if_index;
4237
4238     q = vl_api_client_index_to_input_queue (mp->client_index);
4239     if (q == 0) {
4240         return;
4241     }
4242
4243     sw_if_index = ntohl(mp->sw_if_index);
4244
4245     if (~0 == sw_if_index) {
4246         pool_foreach (t, vxm->tunnels,
4247         ({
4248             send_vxlan_tunnel_details(t, q);
4249         }));
4250     } else {
4251         if ((sw_if_index >= vec_len(vxm->tunnel_index_by_sw_if_index)) ||
4252                 (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index])) {
4253             return;
4254         }
4255         t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
4256         send_vxlan_tunnel_details(t, q);
4257     }
4258 }
4259
4260 static void 
4261 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t *mp)
4262 {
4263     extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
4264                                       int is_add);
4265     vl_api_l2_patch_add_del_reply_t * rmp;
4266     int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index, 
4267                                int is_add);
4268     int rv = 0;
4269
4270     VALIDATE_RX_SW_IF_INDEX(mp);
4271     VALIDATE_TX_SW_IF_INDEX(mp);
4272
4273     rv = vnet_l2_patch_add_del (ntohl(mp->rx_sw_if_index), 
4274                                 ntohl(mp->tx_sw_if_index), 
4275                                 (int)(mp->is_add != 0));
4276     
4277     BAD_RX_SW_IF_INDEX_LABEL;
4278     BAD_TX_SW_IF_INDEX_LABEL;
4279
4280     REPLY_MACRO(VL_API_L2_PATCH_ADD_DEL_REPLY);
4281 }
4282
4283 static void
4284 vl_api_nsh_gre_add_del_tunnel_t_handler 
4285 (vl_api_nsh_gre_add_del_tunnel_t * mp)
4286 {
4287     vl_api_nsh_gre_add_del_tunnel_reply_t * rmp;
4288     int rv = 0;
4289     vnet_nsh_gre_add_del_tunnel_args_t _a, *a = &_a;
4290     u32 encap_fib_index, decap_fib_index;
4291     u32 decap_next_index;
4292     uword * p;
4293     ip4_main_t * im = &ip4_main;
4294     u32 * tlvs = 0;
4295     u32 sw_if_index = ~0;
4296     int i;
4297
4298     p = hash_get (im->fib_index_by_table_id, ntohl(mp->encap_vrf_id));
4299     if (! p) {
4300         rv = VNET_API_ERROR_NO_SUCH_FIB;
4301         goto out;
4302     }
4303     encap_fib_index = p[0];
4304
4305     decap_next_index = ntohl(mp->decap_next_index);
4306
4307     /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
4308     if (decap_next_index == NSH_INPUT_NEXT_IP4_INPUT) {
4309         p = hash_get (im->fib_index_by_table_id, ntohl(mp->decap_vrf_id));
4310         if (! p) {
4311             rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
4312             goto out;
4313         }
4314         decap_fib_index = p[0];
4315     } else {
4316         decap_fib_index = ntohl(mp->decap_vrf_id);
4317     }
4318
4319     memset (a, 0, sizeof (*a));
4320
4321     a->is_add = mp->is_add;
4322     /* ip addresses sent in network byte order */
4323     a->src.as_u32 = ntohl(mp->src);
4324     a->dst.as_u32 = ntohl(mp->dst);
4325     a->encap_fib_index = encap_fib_index;
4326     a->decap_fib_index = decap_fib_index;
4327     a->decap_next_index = decap_next_index;
4328     a->ver_o_c = mp->ver_o_c;
4329     a->length = mp->length;
4330     a->md_type = mp->md_type;
4331     a->next_protocol = mp->next_protocol;
4332     a->spi_si = ntohl(mp->spi_si);
4333     a->c1 = ntohl(mp->c1);
4334     a->c2 = ntohl(mp->c2);
4335     a->c3 = ntohl(mp->c3);
4336     a->c4 = ntohl(mp->c4);
4337
4338     for (i = 0; i < mp->tlv_len_in_words; i++)
4339         vec_add1 (tlvs, ntohl(mp->tlvs[i]));
4340
4341     a->tlvs = tlvs;
4342
4343     rv = vnet_nsh_gre_add_del_tunnel (a, &sw_if_index);
4344     
4345 out:
4346     REPLY_MACRO2(VL_API_NSH_GRE_ADD_DEL_TUNNEL_REPLY,
4347     ({
4348         rmp->sw_if_index = ntohl (sw_if_index);
4349     }));
4350 }
4351
4352 static void
4353 vl_api_nsh_vxlan_gpe_add_del_tunnel_t_handler 
4354 (vl_api_nsh_vxlan_gpe_add_del_tunnel_t * mp)
4355 {
4356     vl_api_nsh_vxlan_gpe_add_del_tunnel_reply_t * rmp;
4357     int rv = 0;
4358     vnet_nsh_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
4359     u32 encap_fib_index, decap_fib_index;
4360     u32 decap_next_index;
4361     uword * p;
4362     ip4_main_t * im = &ip4_main;
4363     u32 * tlvs = 0;
4364     u32 sw_if_index = ~0;
4365     int i;
4366
4367     p = hash_get (im->fib_index_by_table_id, ntohl(mp->encap_vrf_id));
4368     if (! p) {
4369         rv = VNET_API_ERROR_NO_SUCH_FIB;
4370         goto out;
4371     }
4372     encap_fib_index = p[0];
4373
4374     decap_next_index = ntohl(mp->decap_next_index);
4375
4376     /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
4377     if (decap_next_index == NSH_INPUT_NEXT_IP4_INPUT) {
4378         p = hash_get (im->fib_index_by_table_id, ntohl(mp->decap_vrf_id));
4379         if (! p) {
4380             rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
4381             goto out;
4382         }
4383         decap_fib_index = p[0];
4384     } else {
4385         decap_fib_index = ntohl(mp->decap_vrf_id);
4386     }
4387
4388     memset (a, 0, sizeof (*a));
4389
4390     a->is_add = mp->is_add;
4391     /* ip addresses sent in network byte order */
4392     a->src.as_u32 = ntohl(mp->src);
4393     a->dst.as_u32 = ntohl(mp->dst);
4394     a->encap_fib_index = encap_fib_index;
4395     a->decap_fib_index = decap_fib_index;
4396     a->decap_next_index = decap_next_index;
4397     a->vni = ntohl(mp->vni);
4398     a->ver_o_c = mp->ver_o_c;
4399     a->length = mp->length;
4400     a->md_type = mp->md_type;
4401     a->next_protocol = mp->next_protocol;
4402     a->spi_si = ntohl(mp->spi_si);
4403     a->c1 = ntohl(mp->c1);
4404     a->c2 = ntohl(mp->c2);
4405     a->c3 = ntohl(mp->c3);
4406     a->c4 = ntohl(mp->c4);
4407
4408     for (i = 0; i < mp->tlv_len_in_words; i++)
4409         vec_add1 (tlvs, ntohl(mp->tlvs[i]));
4410
4411     a->tlvs = tlvs;
4412
4413     rv = vnet_nsh_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
4414     
4415 out:
4416     REPLY_MACRO2(VL_API_NSH_VXLAN_GPE_ADD_DEL_TUNNEL_REPLY,
4417     ({
4418         rmp->sw_if_index = ntohl (sw_if_index);
4419     }));
4420 }
4421
4422 static void
4423 vl_api_lisp_gpe_add_del_tunnel_t_handler 
4424 (vl_api_lisp_gpe_add_del_tunnel_t * mp)
4425 {
4426     vl_api_lisp_gpe_add_del_tunnel_reply_t * rmp;
4427     int rv = 0;
4428     vnet_lisp_gpe_add_del_tunnel_args_t _a, *a = &_a;
4429     u32 encap_fib_index, decap_fib_index;
4430     u32 decap_next_index;
4431     uword * p;
4432     ip4_main_t * im = &ip4_main;
4433     u32 sw_if_index = ~0;
4434
4435     p = hash_get (im->fib_index_by_table_id, ntohl(mp->encap_vrf_id));
4436     if (! p) {
4437         rv = VNET_API_ERROR_NO_SUCH_FIB;
4438         goto out;
4439     }
4440     encap_fib_index = p[0];
4441
4442     decap_next_index = ntohl(mp->decap_next_index);
4443
4444     /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
4445     if (decap_next_index == NSH_INPUT_NEXT_IP4_INPUT) {
4446         p = hash_get (im->fib_index_by_table_id, ntohl(mp->decap_vrf_id));
4447         if (! p) {
4448             rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
4449             goto out;
4450         }
4451         decap_fib_index = p[0];
4452     } else {
4453         decap_fib_index = ntohl(mp->decap_vrf_id);
4454     }
4455
4456     memset (a, 0, sizeof (*a));
4457
4458     a->is_add = mp->is_add;
4459     /* ip addresses sent in network byte order */
4460     a->src.as_u32 = mp->src;
4461     a->dst.as_u32 = mp->dst;
4462     a->encap_fib_index = encap_fib_index;
4463     a->decap_fib_index = decap_fib_index;
4464     a->decap_next_index = decap_next_index;
4465     a->flags = mp->flags;
4466     a->ver_res = mp->ver_res;
4467     a->res = mp->res;
4468     a->next_protocol = mp->next_protocol;
4469     a->vni = clib_net_to_host_u32 (mp->iid);
4470
4471     rv = vnet_lisp_gpe_add_del_tunnel (a, &sw_if_index);
4472     
4473 out:
4474     REPLY_MACRO2(VL_API_LISP_GPE_ADD_DEL_TUNNEL_REPLY,
4475     ({
4476         rmp->sw_if_index = ntohl (sw_if_index);
4477     }));
4478 }
4479
4480 static void
4481 vl_api_lisp_add_del_locator_set_t_handler(vl_api_lisp_add_del_locator_set_t *mp)
4482 {
4483     vl_api_lisp_add_del_locator_set_reply_t *rmp;
4484     int rv = 0;
4485     vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
4486     u32 ls_index = ~0;
4487     u8 *locator_name = NULL;
4488
4489     memset(a, 0, sizeof(a[0]));
4490
4491     locator_name = format(0, "%s", mp->locator_set_name);
4492
4493     a->name = locator_name;
4494     a->locators = NULL;
4495     a->is_add = mp->is_add;
4496     a->local = 1;
4497
4498     rv = vnet_lisp_add_del_locator_set_name(a, &ls_index);
4499
4500     vec_free(locator_name);
4501
4502     REPLY_MACRO(VL_API_LISP_ADD_DEL_LOCATOR_SET_REPLY);
4503 }
4504
4505 static void
4506 vl_api_lisp_add_del_locator_t_handler(
4507     vl_api_lisp_add_del_locator_t *mp)
4508 {
4509     vl_api_lisp_add_del_locator_reply_t *rmp;
4510     int rv = 0;
4511     locator_t locator, *locators = NULL;
4512     vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
4513     u32 ls_index = ~0;
4514     u8 *locator_name = NULL;
4515
4516     memset(&locator, 0, sizeof(locator));
4517     memset(a, 0, sizeof(a[0]));
4518
4519     locator.sw_if_index = ntohl(mp->sw_if_index);
4520     locator.priority = mp->priority;
4521     locator.weight = mp->weight;
4522     locator.local = 1;
4523     vec_add1(locators, locator);
4524
4525     locator_name = format(0, "%s", mp->locator_set_name);
4526
4527     a->name = locator_name;
4528     a->locators = locators;
4529     a->is_add = mp->is_add;
4530     a->local = 1;
4531
4532     rv = vnet_lisp_add_del_locator(a, &ls_index);
4533
4534     vec_free(locators);
4535     vec_free(locator_name);
4536
4537     REPLY_MACRO(VL_API_LISP_ADD_DEL_LOCATOR_REPLY);
4538 }
4539
4540 static void
4541 vl_api_lisp_add_del_local_eid_t_handler(
4542     vl_api_lisp_add_del_local_eid_t *mp)
4543 {
4544     vl_api_lisp_add_del_local_eid_reply_t *rmp;
4545     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
4546     int rv = 0;
4547     ip_prefix_t  *prefp = NULL;
4548     ip_address_t *ip_eid = NULL;
4549     gid_address_t eid;
4550     uword * p = NULL;
4551     u32 locator_set_index = ~0, map_index = ~0;
4552     vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
4553     u8 *name = NULL;
4554
4555     prefp = &gid_address_ippref(&eid);
4556     ip_eid = &ip_prefix_addr(prefp);
4557     gid_address_type (&eid) = IP_PREFIX;
4558
4559     if (mp->is_ipv6) {
4560         clib_memcpy(&ip_addr_v6(ip_eid), mp->ip_address,
4561                sizeof(ip_addr_v6(ip_eid)));
4562         ip_addr_version(ip_eid) = IP6;
4563     } else {
4564         clib_memcpy(&ip_addr_v4(ip_eid), mp->ip_address,
4565                sizeof(ip_addr_v4(ip_eid)));
4566         ip_addr_version(ip_eid) = IP4;
4567     }
4568     ip_prefix_len(prefp) = mp->prefix_len;
4569
4570     name = format(0, "%s", mp->locator_set_name);
4571     p = hash_get_mem(lcm->locator_set_index_by_name, name);
4572     if (!p) {
4573         rv = VNET_API_ERROR_INVALID_VALUE;
4574         goto out;
4575     }
4576     locator_set_index = p[0];
4577
4578   /* XXX treat batch configuration */
4579     a->is_add = mp->is_add;
4580     a->deid = eid;
4581     a->locator_set_index = locator_set_index;
4582     a->local = 1;
4583
4584     rv = vnet_lisp_add_del_local_mapping(a, &map_index);
4585
4586 out:
4587     vec_free(name);
4588
4589     REPLY_MACRO(VL_API_LISP_ADD_DEL_LOCAL_EID_REPLY);
4590 }
4591
4592 static void
4593 lisp_gpe_add_del_fwd_entry_set_address(
4594     vl_api_lisp_gpe_add_del_fwd_entry_t *mp,
4595     ip_address_t                        *slocator,
4596     ip_address_t                        *dlocator,
4597     gid_address_t                       *eid)
4598 {
4599     ip_address_t *ip_eid = NULL;
4600     ip_prefix_t *prefp = NULL;
4601
4602     prefp = &gid_address_ippref(eid);
4603     ip_eid = &ip_prefix_addr(prefp);
4604
4605     if (mp->eid_is_ipv6) {
4606         clib_memcpy(&ip_addr_v6(ip_eid), mp->eid_ip_address,
4607                sizeof(ip_addr_v6(ip_eid)));
4608         ip_addr_version(ip_eid) = IP6;
4609     } else {
4610         clib_memcpy(&ip_addr_v4(ip_eid), mp->eid_ip_address,
4611                sizeof(ip_addr_v4(ip_eid)));
4612         ip_addr_version(ip_eid) = IP4;
4613     }
4614     ip_prefix_len(prefp) = mp->eid_prefix_len;
4615
4616     if (mp->address_is_ipv6) {
4617         clib_memcpy(&ip_addr_v6(slocator), mp->source_ip_address,
4618                sizeof(ip_addr_v6(slocator)));
4619         ip_addr_version(slocator) = IP6;
4620         clib_memcpy(&ip_addr_v6(dlocator), mp->destination_ip_address,
4621                sizeof(ip_addr_v6(dlocator)));
4622         ip_addr_version(dlocator) = IP6;
4623     } else {
4624         clib_memcpy(&ip_addr_v4(slocator), mp->source_ip_address,
4625                sizeof(ip_addr_v4(slocator)));
4626         ip_addr_version(slocator) = IP4;
4627         clib_memcpy(&ip_addr_v4(dlocator), mp->destination_ip_address,
4628                sizeof(ip_addr_v4(dlocator)));
4629         ip_addr_version(dlocator) = IP4;
4630     }
4631 }
4632
4633 static void
4634 vl_api_lisp_gpe_add_del_fwd_entry_t_handler(
4635     vl_api_lisp_gpe_add_del_fwd_entry_t *mp)
4636 {
4637     vl_api_lisp_gpe_add_del_fwd_entry_reply_t *rmp;
4638     int rv = 0;
4639     ip_address_t slocator, dlocator;
4640     ip_prefix_t * prefp = NULL;
4641     gid_address_t eid;
4642     vnet_lisp_gpe_add_del_fwd_entry_args_t a;
4643
4644     lisp_gpe_add_del_fwd_entry_set_address(mp, &slocator, &dlocator, &eid);
4645
4646     memset (&a, 0, sizeof(a));
4647
4648     a.is_add = mp->is_add;
4649     a.deid = eid;
4650     a.slocator = slocator;
4651     a.dlocator = dlocator;
4652     prefp = &gid_address_ippref(&a.deid);
4653     a.decap_next_index = (ip_prefix_version(prefp) == IP4) ?
4654     LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
4655     rv = vnet_lisp_gpe_add_del_fwd_entry (&a, 0);
4656
4657     REPLY_MACRO(VL_API_LISP_GPE_ADD_DEL_FWD_ENTRY_REPLY);
4658 }
4659
4660 static void
4661 vl_api_lisp_add_del_map_resolver_t_handler(
4662     vl_api_lisp_add_del_map_resolver_t *mp)
4663 {
4664     vl_api_lisp_add_del_map_resolver_reply_t *rmp;
4665     int rv = 0;
4666     ip_address_t *ip_addr = NULL;
4667     vnet_lisp_add_del_map_resolver_args_t _a, * a = &_a;
4668
4669     a->is_add = mp->is_add;
4670     ip_addr = &a->address;
4671
4672     if (mp->is_ipv6) {
4673         clib_memcpy(&ip_addr_v6(ip_addr), mp->ip_address,
4674                sizeof(ip_addr_v6(ip_addr)));
4675         ip_addr_version(ip_addr) = IP6;
4676     } else {
4677         clib_memcpy(&ip_addr_v4(ip_addr), mp->ip_address,
4678                sizeof(ip_addr_v4(ip_addr)));
4679         ip_addr_version(ip_addr) = IP4;
4680     }
4681
4682     rv = vnet_lisp_add_del_map_resolver (a);
4683
4684     REPLY_MACRO(VL_API_LISP_ADD_DEL_MAP_RESOLVER_REPLY);
4685 }
4686
4687 static void
4688 vl_api_lisp_gpe_enable_disable_t_handler(
4689     vl_api_lisp_gpe_enable_disable_t *mp)
4690 {
4691     vl_api_lisp_gpe_enable_disable_reply_t *rmp;
4692     int rv = 0;
4693     vnet_lisp_gpe_enable_disable_args_t _a, * a = &_a;
4694
4695     a->is_en = mp->is_en;
4696     vnet_lisp_gpe_enable_disable (a);
4697
4698     REPLY_MACRO(VL_API_LISP_GPE_ENABLE_DISABLE_REPLY);
4699 }
4700
4701 static void
4702 vl_api_lisp_gpe_add_del_iface_t_handler(
4703     vl_api_lisp_gpe_add_del_iface_t *mp)
4704 {
4705     vl_api_lisp_gpe_add_del_iface_reply_t *rmp;
4706     int rv = 0;
4707     vnet_lisp_gpe_add_del_iface_args_t _a, * a = &_a;
4708
4709     a->is_add = mp->is_add;
4710     a->table_id = mp->table_id;
4711     a->vni = mp->vni;
4712     vnet_lisp_gpe_add_del_iface (a, 0);
4713
4714     REPLY_MACRO(VL_API_LISP_GPE_ADD_DEL_IFACE_REPLY);
4715 }
4716
4717 static void
4718 send_lisp_locator_set_details (lisp_cp_main_t *lcm,
4719                                locator_set_t *lsit,
4720                                unix_shared_memory_queue_t *q)
4721 {
4722     vl_api_lisp_locator_set_details_t *rmp;
4723     locator_t *loc = NULL;
4724     u32 * locit = NULL;
4725
4726     vec_foreach (locit, lsit->locator_indices) {
4727         loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
4728         rmp = vl_msg_api_alloc (sizeof (*rmp));
4729         memset (rmp, 0, sizeof (*rmp));
4730         rmp->_vl_msg_id = ntohs(VL_API_LISP_LOCATOR_SET_DETAILS);
4731         strncpy((char *) rmp->locator_set_name,
4732                 (char *) lsit->name, ARRAY_LEN(rmp->locator_set_name) - 1);
4733         rmp->sw_if_index = htonl(loc->sw_if_index);
4734         rmp->priority = loc->priority;
4735         rmp->weight = loc->weight;
4736
4737         vl_msg_api_send_shmem (q, (u8 *)&rmp);
4738     }
4739 }
4740
4741 static void
4742 vl_api_lisp_locator_set_dump_t_handler (vl_api_lisp_locator_set_dump_t *mp)
4743 {
4744     unix_shared_memory_queue_t * q = NULL;
4745     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
4746     locator_set_t * lsit = NULL;
4747
4748     q = vl_api_client_index_to_input_queue (mp->client_index);
4749     if (q == 0) {
4750         return;
4751     }
4752
4753     pool_foreach (lsit, lcm->locator_set_pool,
4754         ({
4755             send_lisp_locator_set_details(lcm, lsit, q);
4756         }));
4757 }
4758
4759 static void
4760 send_lisp_local_eid_table_details (mapping_t *mapit,
4761                                    unix_shared_memory_queue_t *q)
4762 {
4763     vl_api_lisp_local_eid_table_details_t *rmp = NULL;
4764     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
4765     locator_set_t *ls = NULL;
4766     gid_address_t *gid = NULL;
4767     ip_prefix_t *ip_prefix = NULL;
4768     u8 type = ~0;
4769
4770     ls = pool_elt_at_index (lcm->locator_set_pool,
4771                             mapit->locator_set_index);
4772
4773     gid = &mapit->eid;
4774     type = gid_address_type(gid);
4775
4776     if (type != IP_PREFIX) {
4777         return;
4778     }
4779
4780     ip_prefix = &gid_address_ippref(gid);
4781
4782     rmp = vl_msg_api_alloc (sizeof (*rmp));
4783     memset (rmp, 0, sizeof (*rmp));
4784     rmp->_vl_msg_id = ntohs(VL_API_LISP_LOCAL_EID_TABLE_DETAILS);
4785     strncpy((char *) rmp->locator_set_name,
4786             (char *) ls->name, ARRAY_LEN(rmp->locator_set_name) - 1);
4787
4788     switch (ip_prefix_version(ip_prefix)) {
4789         case IP4:
4790             rmp->eid_is_ipv6 = 0;
4791             clib_memcpy(rmp->eid_ip_address, &ip_prefix_v4(ip_prefix),
4792                    sizeof(ip_prefix_v4(ip_prefix)));
4793             break;
4794
4795         case IP6:
4796             rmp->eid_is_ipv6 = 1;
4797             clib_memcpy(rmp->eid_ip_address, &ip_prefix_v6(ip_prefix),
4798                    sizeof(ip_prefix_v6(ip_prefix)));
4799             break;
4800
4801         default:
4802             ASSERT(0);
4803     }
4804     rmp->eid_prefix_len = ip_prefix_len(ip_prefix);
4805
4806     vl_msg_api_send_shmem (q, (u8 *)&rmp);
4807 }
4808
4809 static void
4810 vl_api_lisp_local_eid_table_dump_t_handler (
4811     vl_api_lisp_local_eid_table_dump_t *mp)
4812 {
4813     unix_shared_memory_queue_t * q = NULL;
4814     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
4815     mapping_t * mapit = NULL;
4816
4817     q = vl_api_client_index_to_input_queue (mp->client_index);
4818     if (q == 0) {
4819         return;
4820     }
4821
4822     pool_foreach (mapit, lcm->mapping_pool,
4823         ({
4824             send_lisp_local_eid_table_details(mapit, q);
4825         }));
4826 }
4827
4828 static void
4829 send_lisp_gpe_tunnel_details (lisp_gpe_tunnel_t *tunnel,
4830                               unix_shared_memory_queue_t *q)
4831 {
4832     vl_api_lisp_gpe_tunnel_details_t *rmp;
4833     lisp_gpe_main_t * lgm = &lisp_gpe_main;
4834     ip4_address_t *ip4 = NULL;
4835
4836     rmp = vl_msg_api_alloc (sizeof (*rmp));
4837     memset (rmp, 0, sizeof (*rmp));
4838     rmp->_vl_msg_id = ntohs(VL_API_LISP_GPE_TUNNEL_DETAILS);
4839
4840     rmp->tunnels = tunnel - lgm->tunnels;
4841
4842     /*list_gpe_tunnel now support only IPv4*/
4843     rmp->is_ipv6 = 0;
4844     ip4 = &tunnel->src;
4845     clib_memcpy(rmp->source_ip, ip4, sizeof(*ip4));
4846     ip4 = &tunnel->dst;
4847     clib_memcpy(rmp->destination_ip, ip4, sizeof(*ip4));
4848
4849     rmp->encap_fib_id = htonl(tunnel->encap_fib_index);
4850     rmp->decap_fib_id = htonl(tunnel->decap_fib_index);
4851     rmp->dcap_next = htonl(tunnel->decap_next_index);
4852     rmp->lisp_ver = tunnel->ver_res;
4853     rmp->next_protocol = tunnel->next_protocol;
4854     rmp->flags = tunnel->flags;
4855     rmp->ver_res = tunnel->ver_res;
4856     rmp->res = tunnel->res;
4857     rmp->iid = htonl(tunnel->vni);
4858
4859     vl_msg_api_send_shmem (q, (u8 *)&rmp);
4860 }
4861
4862 static void
4863 vl_api_lisp_gpe_tunnel_dump_t_handler (
4864     vl_api_lisp_local_eid_table_dump_t *mp)
4865 {
4866     unix_shared_memory_queue_t * q = NULL;
4867     lisp_gpe_main_t * lgm = &lisp_gpe_main;
4868     lisp_gpe_tunnel_t * tunnel = NULL;
4869
4870     if (pool_elts(lgm->tunnels) == 0) {
4871         return;
4872     }
4873
4874     q = vl_api_client_index_to_input_queue (mp->client_index);
4875     if (q == 0) {
4876         return;
4877     }
4878
4879     pool_foreach(tunnel, lgm->tunnels,
4880                  ({
4881                      send_lisp_gpe_tunnel_details(tunnel, q);
4882                 }));
4883 }
4884
4885 static void
4886 send_lisp_map_resolver_details (ip_address_t *ip,
4887                                 unix_shared_memory_queue_t *q)
4888 {
4889     vl_api_lisp_map_resolver_details_t *rmp = NULL;
4890
4891     rmp = vl_msg_api_alloc (sizeof (*rmp));
4892     memset (rmp, 0, sizeof (*rmp));
4893     rmp->_vl_msg_id = ntohs(VL_API_LISP_MAP_RESOLVER_DETAILS);
4894
4895     switch (ip_addr_version(ip)) {
4896         case IP4:
4897             rmp->is_ipv6 = 0;
4898             clib_memcpy(rmp->ip_address, &ip_addr_v4(ip), sizeof(ip_addr_v4(ip)));
4899             break;
4900
4901         case IP6:
4902             rmp->is_ipv6 = 1;
4903             clib_memcpy(rmp->ip_address, &ip_addr_v6(ip), sizeof(ip_addr_v6(ip)));
4904             break;
4905
4906         default:
4907             ASSERT(0);
4908     }
4909
4910     vl_msg_api_send_shmem (q, (u8 *)&rmp);
4911 }
4912
4913 static void
4914 vl_api_lisp_map_resolver_dump_t_handler (
4915     vl_api_lisp_local_eid_table_dump_t *mp)
4916 {
4917     unix_shared_memory_queue_t * q = NULL;
4918     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
4919     ip_address_t *ip = NULL;
4920
4921     q = vl_api_client_index_to_input_queue (mp->client_index);
4922     if (q == 0) {
4923         return;
4924     }
4925
4926     vec_foreach(ip, lcm->map_resolvers) {
4927         send_lisp_map_resolver_details(ip, q);
4928     }
4929
4930 }
4931
4932 static void 
4933 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *mp)
4934 {
4935     vl_api_interface_name_renumber_reply_t * rmp;
4936     int rv = 0;
4937
4938     VALIDATE_SW_IF_INDEX(mp);
4939
4940     rv = vnet_interface_name_renumber 
4941         (ntohl(mp->sw_if_index), ntohl(mp->new_show_dev_instance));
4942     
4943     BAD_SW_IF_INDEX_LABEL;
4944
4945     REPLY_MACRO(VL_API_INTERFACE_NAME_RENUMBER_REPLY);
4946 }
4947
4948 static int arp_change_data_callback (u32 pool_index, u8 * new_mac, 
4949                                      u32 sw_if_index, u32 address)
4950 {
4951     vpe_api_main_t * am = &vpe_api_main;
4952     vlib_main_t * vm = am->vlib_main;
4953     vl_api_ip4_arp_event_t * event;
4954     static f64 arp_event_last_time;
4955     f64 now = vlib_time_now (vm);
4956
4957     if (pool_is_free_index (am->arp_events, pool_index))
4958         return 1;
4959
4960     event = pool_elt_at_index (am->arp_events, pool_index);
4961     if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac))) {
4962         clib_memcpy (event->new_mac, new_mac, sizeof(event->new_mac));
4963     } else { /* same mac */
4964         if ((sw_if_index == event->sw_if_index) && 
4965             ((address == 0) || 
4966              /* for BD case, also check IP address with 10 sec timeout */
4967              ((address == event->address) && 
4968               ((now - arp_event_last_time) < 10.0))))
4969             return 1;
4970     }
4971     
4972     arp_event_last_time = now;
4973     event->sw_if_index = sw_if_index;
4974     if (address) event->address = address;
4975     return 0;
4976 }
4977
4978 static int arp_change_delete_callback (u32 pool_index, u8 * notused)
4979 {
4980     vpe_api_main_t * am = &vpe_api_main;
4981     
4982     if (pool_is_free_index (am->arp_events, pool_index))
4983         return 1;
4984
4985     pool_put_index (am->arp_events, pool_index);
4986     return 0;
4987 }
4988
4989 static void
4990 vl_api_want_ip4_arp_events_t_handler 
4991 (vl_api_want_ip4_arp_events_t * mp)
4992 {
4993     vpe_api_main_t * am = &vpe_api_main;
4994     vnet_main_t * vnm = vnet_get_main();
4995     vl_api_want_ip4_arp_events_reply_t *rmp;
4996     vl_api_ip4_arp_event_t * event;
4997     int rv;
4998     
4999     if (mp->enable_disable) {
5000         pool_get (am->arp_events, event);
5001         memset (event, 0, sizeof (*event));
5002
5003         event->_vl_msg_id = ntohs(VL_API_IP4_ARP_EVENT);
5004         event->client_index = mp->client_index;
5005         event->context = mp->context;
5006         event->address = mp->address;
5007         event->pid = mp->pid;
5008
5009         rv = vnet_add_del_ip4_arp_change_event 
5010             (vnm, arp_change_data_callback,
5011              mp->pid,
5012              &mp->address /* addr, in net byte order */, 
5013              vpe_resolver_process_node.index,
5014              IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */);
5015     } else {
5016         rv = vnet_add_del_ip4_arp_change_event 
5017             (vnm, arp_change_delete_callback,
5018              mp->pid,
5019              &mp->address /* addr, in net byte order */, 
5020              vpe_resolver_process_node.index,
5021              IP4_ARP_EVENT, ~0 /* pool index */, 0 /* is_add */);
5022     }
5023     REPLY_MACRO(VL_API_WANT_IP4_ARP_EVENTS_REPLY);
5024 }
5025
5026 static void vl_api_input_acl_set_interface_t_handler 
5027 (vl_api_input_acl_set_interface_t * mp)
5028 {
5029     vlib_main_t *vm = vlib_get_main();
5030     vl_api_input_acl_set_interface_reply_t * rmp;
5031     int rv;
5032     u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
5033
5034     ip4_table_index = ntohl(mp->ip4_table_index);
5035     ip6_table_index = ntohl(mp->ip6_table_index);
5036     l2_table_index = ntohl(mp->l2_table_index);
5037     sw_if_index = ntohl(mp->sw_if_index);
5038
5039     VALIDATE_SW_IF_INDEX(mp);
5040
5041     rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index, 
5042                                    ip6_table_index, l2_table_index,
5043                                    mp->is_add);
5044
5045     BAD_SW_IF_INDEX_LABEL;
5046
5047     REPLY_MACRO(VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
5048 }
5049
5050 static void vl_api_ipsec_spd_add_del_t_handler
5051 (vl_api_ipsec_spd_add_del_t * mp)
5052 {
5053 #if IPSEC == 0
5054     clib_warning ("unimplemented");
5055 #else
5056
5057     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5058     vl_api_ipsec_spd_add_del_reply_t * rmp;
5059     int rv;
5060
5061 #if DPDK > 0
5062     rv = ipsec_add_del_spd (vm, ntohl(mp->spd_id), mp->is_add);
5063 #else
5064     rv = VNET_API_ERROR_UNIMPLEMENTED;
5065 #endif
5066
5067     REPLY_MACRO(VL_API_IPSEC_SPD_ADD_DEL_REPLY);
5068 #endif
5069 }
5070
5071 static void vl_api_ipsec_interface_add_del_spd_t_handler
5072 (vl_api_ipsec_interface_add_del_spd_t * mp)
5073 {
5074     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5075     vl_api_ipsec_interface_add_del_spd_reply_t * rmp;
5076     int rv;
5077     u32 sw_if_index __attribute__((unused));
5078     u32 spd_id __attribute__((unused));
5079
5080     sw_if_index = ntohl(mp->sw_if_index);
5081     spd_id = ntohl(mp->spd_id);
5082
5083     VALIDATE_SW_IF_INDEX(mp);
5084
5085 #if IPSEC > 0
5086     rv = ipsec_set_interface_spd(vm, sw_if_index, spd_id, mp->is_add);
5087 #else
5088     rv = VNET_API_ERROR_UNIMPLEMENTED;
5089 #endif
5090
5091     BAD_SW_IF_INDEX_LABEL;
5092
5093     REPLY_MACRO(VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
5094 }
5095
5096 static void vl_api_ipsec_spd_add_del_entry_t_handler
5097 (vl_api_ipsec_spd_add_del_entry_t * mp)
5098 {
5099     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5100     vl_api_ipsec_spd_add_del_entry_reply_t * rmp;
5101     int rv;
5102
5103 #if IPSEC > 0
5104     ipsec_policy_t p;
5105
5106     memset(&p, 0, sizeof(p));
5107
5108     p.id = ntohl(mp->spd_id);
5109     p.priority = ntohl(mp->priority);
5110     p.is_outbound = mp->is_outbound;
5111     p.is_ipv6 = mp->is_ipv6;
5112
5113     clib_memcpy(&p.raddr.start, mp->remote_address_start, 16);
5114     clib_memcpy(&p.raddr.stop, mp->remote_address_stop, 16);
5115     clib_memcpy(&p.laddr.start, mp->local_address_start, 16);
5116     clib_memcpy(&p.laddr.stop, mp->local_address_stop, 16);
5117
5118     p.protocol = mp->protocol;
5119     p.rport.start = ntohs(mp->remote_port_start);
5120     p.rport.stop  = ntohs(mp->remote_port_stop);
5121     p.lport.start = ntohs(mp->local_port_start);
5122     p.lport.stop  = ntohs(mp->local_port_stop);
5123     /* policy action resolve unsupported */
5124     if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE) {
5125         clib_warning("unsupported action: 'resolve'");
5126         rv = VNET_API_ERROR_UNIMPLEMENTED;
5127         goto out;
5128     }
5129     p.policy = mp->policy;
5130     p.sa_id = ntohl(mp->sa_id);
5131
5132     rv = ipsec_add_del_policy(vm, &p, mp->is_add);
5133     if (rv)
5134       goto out;
5135
5136     if (mp->is_ip_any) {
5137       p.is_ipv6 = 1;
5138       rv = ipsec_add_del_policy(vm, &p, mp->is_add);
5139     }
5140 #else
5141     rv = VNET_API_ERROR_UNIMPLEMENTED;
5142     goto out;
5143 #endif
5144
5145 out:
5146     REPLY_MACRO(VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
5147 }
5148
5149 static void vl_api_ipsec_sad_add_del_entry_t_handler
5150 (vl_api_ipsec_sad_add_del_entry_t * mp)
5151 {
5152     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5153     vl_api_ipsec_sad_add_del_entry_reply_t * rmp;
5154     int rv;
5155 #if IPSEC > 0
5156     ipsec_sa_t sa;
5157
5158     memset(&sa, 0, sizeof(sa));
5159
5160     sa.id = ntohl(mp->sad_id);
5161     sa.spi = ntohl(mp->spi);
5162     /* security protocol AH unsupported */
5163     if (mp->protocol == IPSEC_PROTOCOL_AH) {
5164         clib_warning("unsupported security protocol 'AH'");
5165         rv = VNET_API_ERROR_UNIMPLEMENTED;
5166         goto out;
5167     }
5168     sa.protocol = mp->protocol;
5169     /* check for unsupported crypto-alg */
5170     if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
5171         mp->crypto_algorithm > IPSEC_CRYPTO_ALG_AES_CBC_256) {
5172         clib_warning("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
5173                      mp->crypto_algorithm);
5174         rv = VNET_API_ERROR_UNIMPLEMENTED;
5175         goto out;
5176     }
5177     sa.crypto_alg = mp->crypto_algorithm;
5178     sa.crypto_key_len = mp->crypto_key_length;
5179     clib_memcpy(&sa.crypto_key, mp->crypto_key, sizeof(sa.crypto_key));
5180     /* check for unsupported integ-alg */
5181     if (mp->integrity_algorithm < IPSEC_INTEG_ALG_SHA1_96 ||
5182         mp->integrity_algorithm > IPSEC_INTEG_ALG_SHA_512_256) {
5183         clib_warning("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
5184                      mp->integrity_algorithm);
5185         rv = VNET_API_ERROR_UNIMPLEMENTED;
5186         goto out;
5187     }
5188     sa.integ_alg = mp->integrity_algorithm;
5189     sa.integ_key_len = mp->integrity_key_length;
5190     clib_memcpy(&sa.integ_key, mp->integrity_key, sizeof(sa.integ_key));
5191     sa.use_esn = mp->use_extended_sequence_number;
5192     sa.is_tunnel = mp->is_tunnel;
5193     sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
5194     clib_memcpy(&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
5195     clib_memcpy(&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
5196
5197     rv = ipsec_add_del_sa(vm, &sa, mp->is_add);
5198 #else
5199     rv = VNET_API_ERROR_UNIMPLEMENTED;
5200     goto out;
5201 #endif
5202
5203 out:
5204     REPLY_MACRO(VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
5205 }
5206
5207 static void
5208 vl_api_ikev2_profile_add_del_t_handler
5209 (vl_api_ikev2_profile_add_del_t * mp)
5210 {
5211     vl_api_ikev2_profile_add_del_reply_t * rmp;
5212     int rv = 0;
5213
5214 #if IPSEC > 0
5215     vlib_main_t * vm = vlib_get_main();
5216     clib_error_t * error;
5217     u8 * tmp = format(0, "%s", mp->name);
5218     error = ikev2_add_del_profile(vm, tmp, mp->is_add);
5219     vec_free (tmp);
5220     if (error)
5221       rv = VNET_API_ERROR_UNSPECIFIED;
5222 #else
5223     rv = VNET_API_ERROR_UNIMPLEMENTED;
5224 #endif
5225
5226    REPLY_MACRO(VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
5227 }
5228
5229 static void
5230 vl_api_ikev2_profile_set_auth_t_handler
5231 (vl_api_ikev2_profile_set_auth_t * mp)
5232 {
5233     vl_api_ikev2_profile_set_auth_reply_t * rmp;
5234     int rv = 0;
5235
5236 #if IPSEC > 0
5237     vlib_main_t * vm = vlib_get_main();
5238     clib_error_t * error;
5239     u8 * tmp = format(0, "%s", mp->name);
5240     u8 * data = vec_new (u8, mp->data_len);
5241     clib_memcpy(data, mp->data, mp->data_len);
5242     error = ikev2_set_profile_auth(vm, tmp, mp->auth_method, data, mp->is_hex);
5243     vec_free (tmp);
5244     vec_free (data);
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_SET_AUTH_REPLY);
5252 }
5253
5254 static void
5255 vl_api_ikev2_profile_set_id_t_handler
5256 (vl_api_ikev2_profile_set_id_t * mp)
5257 {
5258     vl_api_ikev2_profile_add_del_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_id(vm, tmp, mp->id_type, data, mp->is_local);
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_ID_REPLY);
5277 }
5278
5279 static void
5280 vl_api_ikev2_profile_set_ts_t_handler
5281 (vl_api_ikev2_profile_set_ts_t * mp)
5282 {
5283     vl_api_ikev2_profile_set_ts_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     error = ikev2_set_profile_ts(vm, tmp, mp->proto, mp->start_port,
5291                                  mp->end_port, (ip4_address_t) mp->start_addr,
5292                                  (ip4_address_t) mp->end_addr, mp->is_local);
5293     vec_free (tmp);
5294     if (error)
5295       rv = VNET_API_ERROR_UNSPECIFIED;
5296 #else
5297     rv = VNET_API_ERROR_UNIMPLEMENTED;
5298 #endif
5299
5300    REPLY_MACRO(VL_API_IKEV2_PROFILE_SET_TS_REPLY);
5301 }
5302
5303 static void
5304 vl_api_ikev2_set_local_key_t_handler
5305 (vl_api_ikev2_set_local_key_t * mp)
5306 {
5307     vl_api_ikev2_profile_set_ts_reply_t * rmp;
5308     int rv = 0;
5309
5310 #if IPSEC > 0
5311     vlib_main_t * vm = vlib_get_main();
5312     clib_error_t * error;
5313
5314     error = ikev2_set_local_key(vm, mp->key_file);
5315     if (error)
5316       rv = VNET_API_ERROR_UNSPECIFIED;
5317 #else
5318     rv = VNET_API_ERROR_UNIMPLEMENTED;
5319 #endif
5320
5321    REPLY_MACRO(VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
5322 }
5323
5324 static void
5325 vl_api_map_add_domain_t_handler
5326 (vl_api_map_add_domain_t * mp)
5327 {
5328   vl_api_map_add_domain_reply_t * rmp;
5329   int rv = 0;
5330   u32 index;
5331   u8 flags = mp->is_translation ? MAP_DOMAIN_TRANSLATION : 0;
5332   rv = map_create_domain((ip4_address_t *)&mp->ip4_prefix, mp->ip4_prefix_len,
5333                          (ip6_address_t *)&mp->ip6_prefix, mp->ip6_prefix_len,
5334                          (ip6_address_t *)&mp->ip6_src, mp->ip6_src_prefix_len,
5335                          mp->ea_bits_len, mp->psid_offset, mp->psid_length, &index, ntohs(mp->mtu), flags);
5336
5337   REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
5338                ({
5339                  rmp->index = ntohl(index);
5340                }));
5341 }
5342
5343 static void
5344 vl_api_map_del_domain_t_handler
5345 (vl_api_map_del_domain_t * mp)
5346 {
5347   vl_api_map_del_domain_reply_t * rmp;
5348   int rv = 0;
5349
5350   rv = map_delete_domain(ntohl(mp->index));
5351
5352   REPLY_MACRO(VL_API_MAP_DEL_DOMAIN_REPLY);
5353 }
5354
5355 static void
5356 vl_api_map_add_del_rule_t_handler
5357 (vl_api_map_add_del_rule_t * mp)
5358 {
5359   vl_api_map_del_domain_reply_t * rmp;
5360   int rv = 0;
5361
5362   rv = map_add_del_psid(ntohl(mp->index), ntohs(mp->psid), (ip6_address_t *)mp->ip6_dst, mp->is_add);
5363
5364   REPLY_MACRO(VL_API_MAP_ADD_DEL_RULE_REPLY);
5365 }
5366
5367 static void
5368 vl_api_map_domain_dump_t_handler
5369 (vl_api_map_domain_dump_t * mp)
5370 {
5371   vl_api_map_domain_details_t * rmp;
5372   map_main_t *mm = &map_main;
5373   map_domain_t *d;
5374   unix_shared_memory_queue_t * q;
5375
5376   if (pool_elts (mm->domains) == 0)
5377       return;
5378
5379   q = vl_api_client_index_to_input_queue (mp->client_index);
5380   if (q == 0) {
5381     return;
5382   }
5383
5384   pool_foreach(d, mm->domains, ({
5385     /* Make sure every field is initiated (or don't skip the memset()) */
5386     rmp = vl_msg_api_alloc (sizeof (*rmp));
5387     rmp->_vl_msg_id = ntohs(VL_API_MAP_DOMAIN_DETAILS);
5388     rmp->domain_index = htonl(d - mm->domains);
5389     rmp->ea_bits_len = d->ea_bits_len;
5390     rmp->psid_offset = d->psid_offset;
5391     rmp->psid_length = d->psid_length;
5392     clib_memcpy(rmp->ip4_prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix));
5393     rmp->ip4_prefix_len = d->ip4_prefix_len;
5394     clib_memcpy(rmp->ip6_prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix));
5395     rmp->ip6_prefix_len = d->ip6_prefix_len;
5396     clib_memcpy(rmp->ip6_src, &d->ip6_src, sizeof(rmp->ip6_src));
5397     rmp->ip6_src_len = d->ip6_src_len;
5398     rmp->mtu = htons(d->mtu);
5399     rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION);
5400
5401     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5402   }));
5403 }
5404
5405 static void
5406 vl_api_map_rule_dump_t_handler
5407 (vl_api_map_rule_dump_t * mp)
5408 {
5409   unix_shared_memory_queue_t * q;
5410   u16 i;
5411   ip6_address_t dst;
5412   vl_api_map_rule_details_t * rmp;
5413   map_main_t *mm = &map_main;
5414   u32 domain_index = ntohl(mp->domain_index);
5415   map_domain_t *d;
5416
5417   if (pool_elts (mm->domains) == 0)
5418    return;
5419
5420   d = pool_elt_at_index(mm->domains, domain_index);
5421   if (!d || !d->rules) {
5422     return;
5423   }
5424
5425   q = vl_api_client_index_to_input_queue (mp->client_index);
5426   if (q == 0) {
5427     return;
5428   }
5429
5430   for (i = 0; i < (0x1 << d->psid_length); i++) {
5431     dst = d->rules[i];
5432     if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0) {
5433       continue;
5434     }
5435     rmp = vl_msg_api_alloc(sizeof(*rmp));
5436     memset(rmp, 0, sizeof(*rmp));
5437     rmp->_vl_msg_id = ntohs(VL_API_MAP_RULE_DETAILS);
5438     rmp->psid = htons(i);
5439     clib_memcpy(rmp->ip6_dst, &dst, sizeof(rmp->ip6_dst));
5440     vl_msg_api_send_shmem(q, (u8 *)&rmp);
5441   }
5442 }
5443
5444 static void
5445 vl_api_map_summary_stats_t_handler (
5446     vl_api_map_summary_stats_t *mp)
5447 {
5448     vl_api_map_summary_stats_reply_t *rmp;
5449     vlib_combined_counter_main_t *cm;
5450     vlib_counter_t v;
5451     int i, which;
5452     u64 total_pkts[VLIB_N_RX_TX];
5453     u64 total_bytes[VLIB_N_RX_TX];
5454     map_main_t *mm = &map_main;
5455     unix_shared_memory_queue_t *q =
5456         vl_api_client_index_to_input_queue(mp->client_index);
5457
5458     if (!q)
5459         return;
5460     
5461     rmp = vl_msg_api_alloc (sizeof (*rmp));
5462     rmp->_vl_msg_id = ntohs(VL_API_MAP_SUMMARY_STATS_REPLY);
5463     rmp->context = mp->context;
5464     rmp->retval = 0;
5465
5466     memset (total_pkts, 0, sizeof (total_pkts));
5467     memset (total_bytes, 0, sizeof (total_bytes));
5468
5469     map_domain_counter_lock (mm);
5470     vec_foreach(cm, mm->domain_counters) {
5471       which = cm - mm->domain_counters;
5472
5473       for (i = 0; i < vec_len(cm->maxi); i++) {
5474         vlib_get_combined_counter (cm, i, &v);
5475         total_pkts[which] += v.packets;
5476         total_bytes[which] += v.bytes;
5477       }
5478     }
5479
5480     map_domain_counter_unlock (mm);
5481
5482     /* Note: in network byte order! */
5483     rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] = clib_host_to_net_u64(total_pkts[MAP_DOMAIN_COUNTER_RX]);
5484     rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] = clib_host_to_net_u64(total_bytes[MAP_DOMAIN_COUNTER_RX]);
5485     rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] = clib_host_to_net_u64(total_pkts[MAP_DOMAIN_COUNTER_TX]);
5486     rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] = clib_host_to_net_u64(total_bytes[MAP_DOMAIN_COUNTER_TX]);
5487     rmp->total_bindings = clib_host_to_net_u64(pool_elts(mm->domains));
5488     rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
5489     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));
5490     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));
5491
5492     vl_msg_api_send_shmem(q, (u8 *)&rmp);
5493 }
5494
5495 static void vl_api_ipsec_sa_set_key_t_handler
5496 (vl_api_ipsec_sa_set_key_t * mp)
5497 {
5498     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5499     vl_api_ipsec_sa_set_key_reply_t *rmp;
5500     int rv;
5501 #if IPSEC > 0
5502     ipsec_sa_t sa;
5503     sa.id = ntohl(mp->sa_id);
5504     sa.crypto_key_len = mp->crypto_key_length;
5505     clib_memcpy(&sa.crypto_key, mp->crypto_key, sizeof(sa.crypto_key));
5506     sa.integ_key_len = mp->integrity_key_length;
5507     clib_memcpy(&sa.integ_key, mp->integrity_key, sizeof(sa.integ_key));
5508
5509     rv = ipsec_set_sa_key(vm, &sa);
5510 #else
5511     rv = VNET_API_ERROR_UNIMPLEMENTED;
5512 #endif
5513
5514     REPLY_MACRO(VL_API_IPSEC_SA_SET_KEY_REPLY);
5515 }
5516
5517 static void vl_api_cop_interface_enable_disable_t_handler
5518 (vl_api_cop_interface_enable_disable_t * mp)
5519 {
5520     vl_api_cop_interface_enable_disable_reply_t * rmp;
5521     int rv;
5522     u32 sw_if_index = ntohl(mp->sw_if_index);
5523     int enable_disable;
5524
5525     VALIDATE_SW_IF_INDEX(mp);
5526
5527     enable_disable = (int) mp->enable_disable;
5528
5529     rv = cop_interface_enable_disable (sw_if_index, enable_disable);
5530
5531     BAD_SW_IF_INDEX_LABEL;
5532
5533     REPLY_MACRO(VL_API_COP_INTERFACE_ENABLE_DISABLE_REPLY);
5534 }
5535
5536 static void vl_api_cop_whitelist_enable_disable_t_handler
5537 (vl_api_cop_whitelist_enable_disable_t * mp)
5538 {
5539     vl_api_cop_whitelist_enable_disable_reply_t * rmp;
5540     cop_whitelist_enable_disable_args_t _a, *a=&_a;
5541     u32 sw_if_index = ntohl(mp->sw_if_index);
5542     int rv;
5543
5544     VALIDATE_SW_IF_INDEX(mp);
5545
5546     a->sw_if_index = sw_if_index;
5547     a->ip4 = mp->ip4;
5548     a->ip6 = mp->ip6;
5549     a->default_cop = mp->default_cop;
5550     a->fib_id = ntohl(mp->fib_id);
5551
5552     rv = cop_whitelist_enable_disable (a);
5553
5554     BAD_SW_IF_INDEX_LABEL;
5555
5556     REPLY_MACRO(VL_API_COP_WHITELIST_ENABLE_DISABLE_REPLY);
5557 }
5558
5559 static void vl_api_get_node_graph_t_handler
5560 (vl_api_get_node_graph_t * mp)
5561 {
5562     int rv = 0;
5563     u8 * vector = 0;
5564     api_main_t * am = &api_main;
5565     vlib_main_t * vm = vlib_get_main();
5566     void * oldheap;
5567     vl_api_get_node_graph_reply_t * rmp;
5568     
5569     pthread_mutex_lock (&am->vlib_rp->mutex);
5570     oldheap = svm_push_data_heap (am->vlib_rp);
5571     
5572     /* 
5573      * Keep the number of memcpy ops to a minimum (e.g. 1).
5574      * The current size of the serialized vector is
5575      * slightly under 4K.
5576      */
5577     vec_validate (vector, 4095);
5578     vec_reset_length (vector);
5579
5580     vector = vlib_node_serialize (&vm->node_main, vector);
5581     
5582     svm_pop_heap (oldheap);
5583     pthread_mutex_unlock (&am->vlib_rp->mutex);
5584
5585     REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
5586                  rmp->reply_in_shmem = (uword) vector);
5587 }
5588
5589 static void vl_api_trace_profile_add_t_handler
5590 (vl_api_trace_profile_add_t *mp)
5591 {
5592     int rv = 0;
5593     vl_api_trace_profile_add_reply_t * rmp;
5594     clib_error_t *error;
5595
5596     /* Ignoring the profile id as currently a single profile 
5597      * is supported */
5598     error = ip6_ioam_trace_profile_set(mp->trace_num_elt, mp->trace_type, 
5599                                ntohl(mp->node_id), ntohl(mp->trace_app_data), 
5600                                mp->pow_enable, mp->trace_tsp, 
5601                                mp->trace_ppc);
5602     if (error) {
5603       clib_error_report(error);
5604       rv = clib_error_get_code(error);
5605     }
5606     
5607     REPLY_MACRO(VL_API_TRACE_PROFILE_ADD_REPLY);
5608 }
5609
5610 static void vl_api_trace_profile_apply_t_handler
5611 (vl_api_trace_profile_apply_t *mp)
5612 {
5613     int rv = 0;
5614     vl_api_trace_profile_apply_reply_t * rmp;
5615  
5616     if (mp->enable != 0) {
5617       rv = ip6_ioam_set_destination ((ip6_address_t *)(&mp->dest_ipv6),
5618                                 ntohl(mp->prefix_length),
5619                                 ntohl(mp->vrf_id), 
5620                         mp->trace_op == IOAM_HBYH_ADD,
5621                         mp->trace_op == IOAM_HBYH_POP,
5622                         mp->trace_op == IOAM_HBYH_MOD);
5623     } else {
5624       //ip6_ioam_clear_destination(&ip6, mp->prefix_length, mp->vrf_id);
5625     }
5626     REPLY_MACRO(VL_API_TRACE_PROFILE_APPLY_REPLY);
5627 }
5628
5629 static void vl_api_trace_profile_del_t_handler
5630 (vl_api_trace_profile_del_t *mp)
5631 {
5632     int rv = 0;
5633     vl_api_trace_profile_del_reply_t * rmp;
5634     clib_error_t *error;
5635
5636     error = clear_ioam_rewrite_fn();
5637     if (error) {
5638       clib_error_report(error);
5639       rv = clib_error_get_code(error);
5640     }
5641     
5642     REPLY_MACRO(VL_API_TRACE_PROFILE_DEL_REPLY);
5643 }
5644
5645
5646 #define BOUNCE_HANDLER(nn)                                              \
5647 static void vl_api_##nn##_t_handler (                                   \
5648     vl_api_##nn##_t *mp)                                                \
5649 {                                                                       \
5650     vpe_client_registration_t *reg;                                     \
5651     vpe_api_main_t * vam = &vpe_api_main;                               \
5652     unix_shared_memory_queue_t * q;                                     \
5653                                                                         \
5654     /* One registration only... */                                      \
5655     pool_foreach(reg, vam->nn##_registrations,                          \
5656     ({                                                                  \
5657         q = vl_api_client_index_to_input_queue (reg->client_index);     \
5658         if (q) {                                                        \
5659             /*                                                          \
5660              * If the queue is stuffed, turf the msg and complain       \
5661              * It's unlikely that the intended recipient is             \
5662              * alive; avoid deadlock at all costs.                      \
5663              */                                                         \
5664             if (q->cursize == q->maxsize) {                             \
5665                 clib_warning ("ERROR: receiver queue full, drop msg");  \
5666                 vl_msg_api_free (mp);                                   \
5667                 return;                                                 \
5668             }                                                           \
5669             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
5670             return;                                                     \
5671         }                                                               \
5672     }));                                                                \
5673     vl_msg_api_free (mp);                                               \
5674 }
5675
5676 /*
5677  * vpe_api_hookup
5678  * Add vpe's API message handlers to the table.
5679  * vlib has alread mapped shared memory and
5680  * added the client registration handlers. 
5681  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
5682  */
5683
5684 static clib_error_t *
5685 vpe_api_hookup (vlib_main_t *vm)
5686 {
5687     api_main_t * am = &api_main;
5688
5689 #define _(N,n)                                                  \
5690     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
5691                            vl_api_##n##_t_handler,              \
5692                            vl_noop_handler,                     \
5693                            vl_api_##n##_t_endian,               \
5694                            vl_api_##n##_t_print,                \
5695                            sizeof(vl_api_##n##_t), 1); 
5696     foreach_vpe_api_msg;
5697 #undef _
5698
5699     /* 
5700      * Manually register the sr tunnel add del msg, so we trace
5701      * enough bytes to capture a typical segment list
5702      */
5703     vl_msg_api_set_handlers (VL_API_SR_TUNNEL_ADD_DEL, 
5704                              "sr_tunnel_add_del",
5705                              vl_api_sr_tunnel_add_del_t_handler,
5706                              vl_noop_handler,
5707                              vl_api_sr_tunnel_add_del_t_endian,
5708                              vl_api_sr_tunnel_add_del_t_print,
5709                              256, 1);
5710
5711     /* 
5712      * Trace space for 8 MPLS encap labels, classifier mask+match
5713      */
5714     am->api_trace_cfg [VL_API_MPLS_ADD_DEL_ENCAP].size += 8 * sizeof(u32);
5715     am->api_trace_cfg [VL_API_CLASSIFY_ADD_DEL_TABLE].size
5716         += 5 * sizeof (u32x4);
5717     am->api_trace_cfg [VL_API_CLASSIFY_ADD_DEL_SESSION].size
5718         += 5 * sizeof (u32x4);
5719     am->api_trace_cfg [VL_API_VXLAN_ADD_DEL_TUNNEL].size
5720         += 16 * sizeof (u32);
5721     
5722     /*
5723      * trace space for 4 nsh-gre variable TLV words
5724      */
5725     am->api_trace_cfg [VL_API_NSH_GRE_ADD_DEL_TUNNEL].size
5726         += 4 * sizeof (u32);
5727
5728     /* 
5729      * Thread-safe API messages
5730      */
5731     am->is_mp_safe [VL_API_IP_ADD_DEL_ROUTE] = 1;
5732
5733     return 0;
5734 }
5735
5736 VLIB_API_INIT_FUNCTION(vpe_api_hookup);
5737
5738 static clib_error_t *
5739 vpe_api_init (vlib_main_t *vm)
5740 {
5741     vpe_api_main_t *am = &vpe_api_main;
5742
5743     am->vlib_main = vm;
5744     am->vnet_main = vnet_get_main();
5745     am->interface_events_registration_hash = hash_create (0, sizeof (uword));
5746     am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
5747     am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
5748     am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
5749     am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
5750     am->oam_events_registration_hash = hash_create (0, sizeof (uword));
5751
5752     vl_api_init (vm);
5753     vl_set_memory_region_name ("/vpe-api");
5754     vl_enable_disable_memory_api (vm, 1 /* enable it */);
5755
5756     return 0;
5757 }
5758
5759 VLIB_INIT_FUNCTION(vpe_api_init);
5760
5761 static clib_error_t *
5762 chroot_config (vlib_main_t * vm, unformat_input_t * input)
5763 {
5764   u8 * chroot_path;
5765
5766   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
5767     {
5768       if (unformat (input, "prefix %s", &chroot_path))
5769         {
5770           vec_add1 (chroot_path, 0);
5771           vl_set_memory_root_path ((char *)chroot_path);
5772         }
5773       else
5774         return clib_error_return (0, "unknown input `%U'",
5775                                   format_unformat_error, input);
5776     }
5777   return 0;
5778 }
5779 VLIB_EARLY_CONFIG_FUNCTION (chroot_config, "chroot");
5780
5781 void * get_unformat_vnet_sw_interface (void)
5782 {
5783     return (void *) &unformat_vnet_sw_interface;
5784 }
5785
5786 #undef vl_api_version
5787 #define vl_api_version(n,v) static u32 vpe_api_version = v;
5788 #include <api/vpe.api.h>
5789 #undef vl_api_version
5790
5791 int vl_msg_api_version_check (vl_api_memclnt_create_t * mp)
5792 {
5793     if (clib_host_to_net_u32(mp->api_versions[0]) != vpe_api_version) {
5794         clib_warning ("vpe API mismatch: 0x%08x instead of 0x%08x",
5795                       clib_host_to_net_u32 (mp->api_versions[0]),
5796                       vpe_api_version);
5797         return -1;
5798     }
5799     return 0;
5800 }
5801
5802 static u8 * format_arp_event (u8 * s, va_list * args)
5803 {
5804     vl_api_ip4_arp_event_t * event = va_arg (*args, vl_api_ip4_arp_event_t *);
5805
5806     s = format (s, "pid %d: %U", event->pid,
5807                 format_ip4_address, &event->address);
5808     return s;
5809 }
5810
5811 static clib_error_t * 
5812 show_ip4_arp_events_fn (vlib_main_t * vm,
5813                         unformat_input_t * input, 
5814                         vlib_cli_command_t * cmd)
5815 {
5816     vpe_api_main_t * am = &vpe_api_main;
5817     vl_api_ip4_arp_event_t * event;
5818
5819     if (pool_elts (am->arp_events) == 0) {
5820         vlib_cli_output (vm, "No active arp event registrations");
5821         return 0;
5822     }
5823
5824     pool_foreach (event, am->arp_events, 
5825     ({
5826         vlib_cli_output (vm, "%U", format_arp_event, event);
5827     }));
5828
5829     return 0;
5830 }
5831
5832 VLIB_CLI_COMMAND (show_ip4_arp_events, static) = {
5833   .path = "show arp event registrations",
5834   .function = show_ip4_arp_events_fn,
5835   .short_help = "Show arp event registrations",
5836 };