f9e69024ae01ab99317a1273bda5e3acecc3913b
[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/map/map.h>
72 #include <vnet/cop/cop.h>
73 #include <vnet/ip/ip6_hop_by_hop.h>
74
75 #undef BIHASH_TYPE
76 #undef __included_bihash_template_h__
77 #include <vnet/l2/l2_fib.h>
78
79 #if IPSEC > 0
80 #include <vnet/ipsec/ipsec.h>
81 #include <vnet/ipsec/ikev2.h>
82 #endif /* IPSEC */
83 #if DPDK > 0
84 #include <vnet/devices/virtio/vhost-user.h>
85 #endif
86
87 #include <stats/stats.h>
88 #include <oam/oam.h>
89
90 #include <vnet/ethernet/ethernet.h>
91 #include <vnet/ethernet/arp_packet.h>
92 #include <vnet/interface.h>
93
94 #include <vnet/l2/l2_fib.h>
95 #include <vnet/l2/l2_bd.h>
96 #include <api/vpe_msg_enum.h>
97
98 #define f64_endian(a)
99 #define f64_print(a,b)
100
101 #define vl_typedefs             /* define message structures */
102 #include <api/vpe_all_api_h.h> 
103 #undef vl_typedefs
104
105 #define vl_endianfun             /* define message structures */
106 #include <api/vpe_all_api_h.h> 
107 #undef vl_endianfun
108
109 /* instantiate all the print functions we know about */
110 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
111 #define vl_printfun
112 #include <api/vpe_all_api_h.h>
113 #undef vl_printfun
114
115 #define REPLY_MACRO(t)                                          \
116 do {                                                            \
117     unix_shared_memory_queue_t * q;                             \
118     rv = vl_msg_api_pd_handler (mp, rv);                        \
119     q = vl_api_client_index_to_input_queue (mp->client_index);  \
120     if (!q)                                                     \
121         return;                                                 \
122                                                                 \
123     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
124     rmp->_vl_msg_id = ntohs((t));                               \
125     rmp->context = mp->context;                                 \
126     rmp->retval = ntohl(rv);                                    \
127                                                                 \
128     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
129 } while(0);
130
131 #define REPLY_MACRO2(t, body)                                   \
132 do {                                                            \
133     unix_shared_memory_queue_t * q;                             \
134     rv = vl_msg_api_pd_handler (mp, rv);                        \
135     q = vl_api_client_index_to_input_queue (mp->client_index);  \
136     if (!q)                                                     \
137         return;                                                 \
138                                                                 \
139     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
140     rmp->_vl_msg_id = ntohs((t));                               \
141     rmp->context = mp->context;                                 \
142     rmp->retval = ntohl(rv);                                    \
143     do {body;} while (0);                                       \
144     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
145 } while(0);
146
147 #if (1 || CLIB_DEBUG > 0)       /* "trust, but verify" */
148
149 #define VALIDATE_SW_IF_INDEX(mp)                                \
150  do { u32 __sw_if_index = ntohl(mp->sw_if_index);               \
151     vnet_main_t *__vnm = vnet_get_main();                       \
152     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
153                            __sw_if_index)) {                    \
154         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
155         goto bad_sw_if_index;                                   \
156     }                                                           \
157 } while(0);
158
159 #define BAD_SW_IF_INDEX_LABEL                   \
160 do {                                            \
161 bad_sw_if_index:                                \
162     ;                                           \
163 } while (0);
164
165 #define VALIDATE_RX_SW_IF_INDEX(mp)                             \
166  do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index);         \
167     vnet_main_t *__vnm = vnet_get_main();                       \
168     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
169                            __rx_sw_if_index)) {                 \
170         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
171         goto bad_rx_sw_if_index;                                \
172     }                                                           \
173 } while(0);
174
175 #define BAD_RX_SW_IF_INDEX_LABEL                \
176 do {                                            \
177 bad_rx_sw_if_index:                             \
178     ;                                           \
179 } while (0);
180
181 #define VALIDATE_TX_SW_IF_INDEX(mp)                             \
182  do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index);         \
183     vnet_main_t *__vnm = vnet_get_main();                       \
184     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
185                            __tx_sw_if_index)) {                 \
186         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
187         goto bad_tx_sw_if_index;                                \
188     }                                                           \
189 } while(0);
190
191 #define BAD_TX_SW_IF_INDEX_LABEL                \
192 do {                                            \
193 bad_tx_sw_if_index:                             \
194     ;                                           \
195 } while (0);
196
197 #else
198
199 #define VALIDATE_SW_IF_INDEX(mp)
200 #define BAD_SW_IF_INDEX_LABEL
201 #define VALIDATE_RX_SW_IF_INDEX(mp)
202 #define BAD_RX_SW_IF_INDEX_LABEL
203 #define VALIDATE_TX_SW_IF_INDEX(mp)
204 #define BAD_TX_SW_IF_INDEX_LABEL
205
206 #endif  /* CLIB_DEBUG > 0 */
207
208 #define foreach_vpe_api_msg                                             \
209 _(WANT_INTERFACE_EVENTS, want_interface_events)                         \
210 _(WANT_FROM_NETCONF_SERVER, want_from_netconf_server)                   \
211 _(WANT_TO_NETCONF_SERVER, want_to_netconf_server)                       \
212 _(WANT_FROM_NETCONF_CLIENT, want_from_netconf_client)                   \
213 _(WANT_TO_NETCONF_CLIENT, want_to_netconf_client)                       \
214 _(FROM_NETCONF_SERVER, from_netconf_server)                             \
215 _(TO_NETCONF_SERVER, to_netconf_server)                                 \
216 _(FROM_NETCONF_CLIENT, from_netconf_client)                             \
217 _(TO_NETCONF_CLIENT, to_netconf_client)                                 \
218 _(WANT_OAM_EVENTS, want_oam_events)                                     \
219 _(OAM_ADD_DEL, oam_add_del)                                             \
220 _(SW_INTERFACE_DUMP, sw_interface_dump)                                 \
221 _(SW_INTERFACE_DETAILS, sw_interface_details)                           \
222 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags)                       \
223 _(IP_ADD_DEL_ROUTE, ip_add_del_route)                                   \
224 _(IS_ADDRESS_REACHABLE, is_address_reachable)                           \
225 _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address)           \
226 _(SW_INTERFACE_SET_TABLE, sw_interface_set_table)                       \
227 _(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath)                       \
228 _(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect)           \
229 _(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge)               \
230 _(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del)                         \
231 _(BRIDGE_DOMAIN_DUMP, bridge_domain_dump)                               \
232 _(BRIDGE_DOMAIN_DETAILS, bridge_domain_details)                         \
233 _(BRIDGE_DOMAIN_SW_IF_DETAILS, bridge_domain_sw_if_details)             \
234 _(L2FIB_ADD_DEL, l2fib_add_del)                                         \
235 _(L2_FLAGS, l2_flags)                                                   \
236 _(BRIDGE_FLAGS, bridge_flags)                                           \
237 _(TAP_CONNECT, tap_connect)                                             \
238 _(TAP_MODIFY, tap_modify)                                               \
239 _(TAP_DELETE, tap_delete)                                               \
240 _(SW_INTERFACE_TAP_DUMP, sw_interface_tap_dump)                         \
241 _(CREATE_VLAN_SUBIF, create_vlan_subif)                                 \
242 _(CREATE_SUBIF, create_subif)                                           \
243 _(MPLS_GRE_ADD_DEL_TUNNEL, mpls_gre_add_del_tunnel)                     \
244 _(MPLS_ETHERNET_ADD_DEL_TUNNEL, mpls_ethernet_add_del_tunnel)           \
245 _(MPLS_ETHERNET_ADD_DEL_TUNNEL_2, mpls_ethernet_add_del_tunnel_2)       \
246 _(MPLS_ADD_DEL_ENCAP, mpls_add_del_encap)                               \
247 _(MPLS_ADD_DEL_DECAP, mpls_add_del_decap)                               \
248 _(PROXY_ARP_ADD_DEL, proxy_arp_add_del)                                 \
249 _(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable)       \
250 _(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del)                             \
251 _(VNET_GET_SUMMARY_STATS, vnet_get_summary_stats)                       \
252 _(RESET_FIB, reset_fib)                                                 \
253 _(DHCP_PROXY_CONFIG,dhcp_proxy_config)                                  \
254 _(DHCP_PROXY_CONFIG_2,dhcp_proxy_config_2)                              \
255 _(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss)                                \
256 _(DHCP_CLIENT_CONFIG, dhcp_client_config)                               \
257 _(SET_IP_FLOW_HASH,set_ip_flow_hash)                                    \
258 _(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config)           \
259 _(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix)           \
260 _(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable )    \
261 _(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS,                              \
262   sw_interface_ip6_set_link_local_address)                              \
263 _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered)             \
264 _(CREATE_LOOPBACK, create_loopback)                                     \
265 _(CONTROL_PING, control_ping)                                           \
266 _(CLI_REQUEST, cli_request)                                             \
267 _(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit)                       \
268 _(L2_PATCH_ADD_DEL, l2_patch_add_del)                                   \
269 _(CLASSIFY_ADD_DEL_TABLE, classify_add_del_table)                       \
270 _(CLASSIFY_ADD_DEL_SESSION, classify_add_del_session)                   \
271 _(CLASSIFY_SET_INTERFACE_IP_TABLE, classify_set_interface_ip_table)     \
272 _(CLASSIFY_SET_INTERFACE_L2_TABLES, classify_set_interface_l2_tables)   \
273 _(GET_NODE_INDEX, get_node_index)                                       \
274 _(ADD_NODE_NEXT, add_node_next)                                         \
275 _(L2TPV3_CREATE_TUNNEL, l2tpv3_create_tunnel)                           \
276 _(L2TPV3_SET_TUNNEL_COOKIES, l2tpv3_set_tunnel_cookies)                 \
277 _(L2TPV3_INTERFACE_ENABLE_DISABLE, l2tpv3_interface_enable_disable)     \
278 _(L2TPV3_SET_LOOKUP_KEY, l2tpv3_set_lookup_key)                         \
279 _(SW_IF_L2TPV3_TUNNEL_DUMP, sw_if_l2tpv3_tunnel_dump)                   \
280 _(VXLAN_ADD_DEL_TUNNEL, vxlan_add_del_tunnel)                           \
281 _(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump)                                 \
282 _(L2_FIB_CLEAR_TABLE, l2_fib_clear_table)                               \
283 _(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter)                     \
284 _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite)         \
285 _(CREATE_VHOST_USER_IF, create_vhost_user_if)                           \
286 _(MODIFY_VHOST_USER_IF, modify_vhost_user_if)                           \
287 _(DELETE_VHOST_USER_IF, delete_vhost_user_if)                           \
288 _(SW_INTERFACE_VHOST_USER_DUMP, sw_interface_vhost_user_dump)           \
289 _(IP_ADDRESS_DUMP, ip_address_dump)                                     \
290 _(IP_DUMP, ip_dump)                                                     \
291 _(SW_INTERFACE_VHOST_USER_DETAILS, sw_interface_vhost_user_details)     \
292 _(SHOW_VERSION, show_version)                                           \
293 _(NSH_GRE_ADD_DEL_TUNNEL, nsh_gre_add_del_tunnel)                       \
294 _(L2_FIB_TABLE_DUMP, l2_fib_table_dump)                                 \
295 _(L2_FIB_TABLE_ENTRY, l2_fib_table_entry)                               \
296 _(NSH_VXLAN_GPE_ADD_DEL_TUNNEL, nsh_vxlan_gpe_add_del_tunnel)           \
297 _(LISP_GPE_ADD_DEL_TUNNEL, lisp_gpe_add_del_tunnel)                     \
298 _(INTERFACE_NAME_RENUMBER, interface_name_renumber)                     \
299 _(WANT_IP4_ARP_EVENTS, want_ip4_arp_events)                             \
300 _(INPUT_ACL_SET_INTERFACE, input_acl_set_interface)                     \
301 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del)                                 \
302 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd)             \
303 _(IPSEC_SPD_ADD_DEL_ENTRY, ipsec_spd_add_del_entry)                     \
304 _(IPSEC_SAD_ADD_DEL_ENTRY, ipsec_sad_add_del_entry)                     \
305 _(IPSEC_SA_SET_KEY, ipsec_sa_set_key)                                   \
306 _(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del)                         \
307 _(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth)                       \
308 _(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id)                           \
309 _(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts)                           \
310 _(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key)                             \
311 _(DELETE_LOOPBACK, delete_loopback)                                     \
312 _(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del)                                 \
313 _(MAP_ADD_DOMAIN, map_add_domain)                                       \
314 _(MAP_DEL_DOMAIN, map_del_domain)                                       \
315 _(MAP_ADD_DEL_RULE, map_add_del_rule)                                   \
316 _(MAP_DOMAIN_DUMP, map_domain_dump)                                     \
317 _(MAP_RULE_DUMP, map_rule_dump)                                         \
318 _(MAP_SUMMARY_STATS, map_summary_stats)                                 \
319 _(COP_INTERFACE_ENABLE_DISABLE, cop_interface_enable_disable)           \
320 _(COP_WHITELIST_ENABLE_DISABLE, cop_whitelist_enable_disable)           \
321 _(GET_NODE_GRAPH, get_node_graph)                                       \
322 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats)                   \
323 _(TRACE_PROFILE_ADD, trace_profile_add)                                 \
324 _(TRACE_PROFILE_APPLY, trace_profile_apply)                             \
325 _(TRACE_PROFILE_DEL, trace_profile_del)
326
327 #define QUOTE_(x) #x
328 #define QUOTE(x) QUOTE_(x)
329
330 #define foreach_registration_hash               \
331 _(interface_events)                             \
332 _(to_netconf_server)                            \
333 _(from_netconf_server)                          \
334 _(to_netconf_client)                            \
335 _(from_netconf_client)                          \
336 _(oam_events)
337
338 typedef enum {
339     RESOLVE_IP4_ADD_DEL_ROUTE=1,
340     RESOLVE_IP6_ADD_DEL_ROUTE,
341     RESOLVE_MPLS_ETHERNET_ADD_DEL,
342 } resolve_t;
343
344 typedef struct {
345     u8 resolve_type;
346     union {
347         vl_api_ip_add_del_route_t r;
348         vl_api_mpls_ethernet_add_del_tunnel_2_t t;
349     };
350 } pending_route_t;
351
352 typedef struct {
353
354 #define _(a) uword *a##_registration_hash;              \
355     vpe_client_registration_t * a##_registrations;
356 foreach_registration_hash
357 #undef _
358
359     /* notifications happen really early in the game */
360     u8 link_state_process_up;
361
362     /* ip4 pending route adds */
363     pending_route_t * pending_routes;
364
365     /* ip4 arp event registration pool */
366     vl_api_ip4_arp_event_t * arp_events;
367
368     /* convenience */
369     vlib_main_t * vlib_main;
370     vnet_main_t * vnet_main;
371 } vpe_api_main_t;
372
373 static vlib_node_registration_t vpe_resolver_process_node;
374 static vpe_api_main_t vpe_api_main;
375
376 static void send_sw_interface_flags (vpe_api_main_t * am,
377                                      unix_shared_memory_queue_t *q,
378                                      vnet_sw_interface_t * swif);
379 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
380                                      unix_shared_memory_queue_t *q,
381                                      u32 sw_if_index);
382
383 static int arp_change_delete_callback (u32 pool_index, u8 * notused);
384
385
386 /* Clean up all registrations belonging to the indicated client */
387 int vl_api_memclnt_delete_callback (u32 client_index) 
388 {
389     vpe_api_main_t * vam = &vpe_api_main;
390     vpe_client_registration_t *rp;
391     uword * p;
392     int stats_memclnt_delete_callback (u32 client_index);
393
394     stats_memclnt_delete_callback (client_index);
395
396 #define _(a)                                                    \
397     p = hash_get (vam->a##_registration_hash, client_index);    \
398     if (p) {                                                    \
399         rp = pool_elt_at_index (vam->a##_registrations, p[0]);  \
400         pool_put (vam->a##_registrations, rp);                  \
401         hash_unset (vam->a##_registration_hash, client_index);  \
402     }
403     foreach_registration_hash;
404 #undef _
405     return 0;
406 }
407
408 #define API_LINK_STATE_EVENT 1
409 #define API_ADMIN_UP_DOWN_EVENT 2
410
411 static int
412 event_data_cmp (void * a1, void * a2)
413 {
414   uword * e1 = a1;
415   uword * e2 = a2;
416
417   return (word) e1[0] - (word) e2[0];
418 }
419
420 static uword
421 link_state_process (vlib_main_t * vm,
422                     vlib_node_runtime_t * rt,
423                     vlib_frame_t * f)
424 {
425     vpe_api_main_t * vam = &vpe_api_main;
426     vnet_main_t * vnm = vam->vnet_main;
427     vnet_sw_interface_t * swif;
428     uword * event_data = 0;
429     vpe_client_registration_t *reg;
430     int i;
431     u32 prev_sw_if_index;
432     unix_shared_memory_queue_t * q;
433
434     vam->link_state_process_up = 1;
435
436     while (1) {
437         vlib_process_wait_for_event (vm);
438
439         /* Unified list of changed link or admin state sw_if_indices */
440         vlib_process_get_events_with_type 
441             (vm, &event_data, API_LINK_STATE_EVENT);
442         vlib_process_get_events_with_type 
443             (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
444
445         /* Sort, so we can eliminate duplicates */
446         vec_sort_with_function (event_data, event_data_cmp);
447
448         prev_sw_if_index = ~0;
449
450         for (i = 0; i < vec_len(event_data); i++) {
451             /* Only one message per swif */
452             if (prev_sw_if_index == event_data[i])
453                 continue;
454             prev_sw_if_index = event_data[i];
455
456             pool_foreach(reg, vam->interface_events_registrations, 
457             ({
458                 q = vl_api_client_index_to_input_queue (reg->client_index);
459                 if (q) {
460                     // sw_interface may be deleted already
461                     if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
462                             event_data[i]))
463                     {
464                         swif = vnet_get_sw_interface (vnm, event_data[i]);
465                         send_sw_interface_flags (vam, q, swif);
466                     }
467                 }
468             }));
469         }
470         vec_reset_length (event_data);
471     }
472
473     return 0;
474 }
475
476 static clib_error_t *
477 link_up_down_function (vnet_main_t *vm, u32 hw_if_index, u32 flags);
478 static clib_error_t *
479 admin_up_down_function (vnet_main_t *vm, u32 hw_if_index, u32 flags);
480
481 VLIB_REGISTER_NODE (link_state_process_node,static) = {
482     .function = link_state_process,
483     .type = VLIB_NODE_TYPE_PROCESS,
484     .name = "vpe-link-state-process",
485 };
486
487 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
488 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
489
490 static clib_error_t *
491 link_up_down_function (vnet_main_t *vm, u32 hw_if_index, u32 flags)
492 {
493     vpe_api_main_t * vam = &vpe_api_main;
494     vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
495     
496     if (vam->link_state_process_up)
497         vlib_process_signal_event (vam->vlib_main, 
498                                    link_state_process_node.index,
499                                    API_LINK_STATE_EVENT, 
500                                    hi->sw_if_index);
501     return 0;
502 }
503
504 static clib_error_t *
505 admin_up_down_function (vnet_main_t *vm, u32 sw_if_index, u32 flags)
506 {
507     vpe_api_main_t * vam = &vpe_api_main;
508
509     /* 
510      * Note: it's perfectly fair to set a subif admin up / admin down.
511      * Note the subtle distinction between this routine and the previous
512      * routine.
513      */
514     if (vam->link_state_process_up)
515         vlib_process_signal_event (vam->vlib_main, 
516                                    link_state_process_node.index,
517                                    API_ADMIN_UP_DOWN_EVENT, 
518                                    sw_if_index);
519     return 0;
520 }
521
522 #define pub_sub_handler(lca,UCA)                                        \
523 static void vl_api_want_##lca##_t_handler (                             \
524     vl_api_want_##lca##_t *mp)                                          \
525 {                                                                       \
526     vpe_api_main_t *vam = &vpe_api_main;                                \
527     vpe_client_registration_t *rp;                                      \
528     vl_api_want_##lca##_reply_t *rmp;                                   \
529     uword *p;                                                           \
530     i32 rv = 0;                                                         \
531                                                                         \
532     p = hash_get (vam->lca##_registration_hash, mp->client_index);      \
533     if (p) {                                                            \
534         if (mp->enable_disable) {                                       \
535             clib_warning ("pid %d: already enabled...", mp->pid);       \
536             rv = VNET_API_ERROR_INVALID_REGISTRATION;                   \
537             goto reply;                                                 \
538         } else {                                                        \
539             rp = pool_elt_at_index (vam->lca##_registrations, p[0]);    \
540             pool_put (vam->lca##_registrations, rp);                    \
541             hash_unset (vam->lca##_registration_hash,                   \
542                 mp->client_index);                                      \
543             goto reply;                                                 \
544         }                                                               \
545     }                                                                   \
546     if (mp->enable_disable == 0) {                                      \
547         clib_warning ("pid %d: already disabled...", mp->pid);          \
548         rv = VNET_API_ERROR_INVALID_REGISTRATION;                       \
549         goto reply;                                                     \
550     }                                                                   \
551     pool_get (vam->lca##_registrations, rp);                            \
552     rp->client_index = mp->client_index;                                \
553     rp->client_pid = mp->pid;                                           \
554     hash_set (vam->lca##_registration_hash, rp->client_index,           \
555               rp - vam->lca##_registrations);                           \
556                                                                         \
557 reply:                                                                  \
558     REPLY_MACRO (VL_API_WANT_##UCA##_REPLY);                            \
559 }
560
561 pub_sub_handler (interface_events,INTERFACE_EVENTS)
562 pub_sub_handler (from_netconf_server,FROM_NETCONF_SERVER)
563 pub_sub_handler (to_netconf_server,TO_NETCONF_SERVER)
564 pub_sub_handler (from_netconf_client,FROM_NETCONF_CLIENT)
565 pub_sub_handler (to_netconf_client,TO_NETCONF_CLIENT)
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         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     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             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     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     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             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     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     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             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));
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         memcpy (&a.ethernet, mp->mac_address, 6);
2258         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     memcpy (rmp, mp, sizeof (*rmp));
2299
2300     sw_if_index = mp->next_hop_sw_if_index;
2301     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         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             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     memcpy (&mp->hostname, hostname, vec_len(hostname));
3006     mp->hostname[vec_len(hostname) + 1] = '\n';
3007     memcpy (&mp->host_address[0], host_address, 16);
3008     memcpy (&mp->router_address[0], router_address, 16);
3009     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     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 *)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         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         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     memcpy(mp->client_address, &s->client_address, sizeof(s->client_address));
3770     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         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->iid = 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_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *mp)
4482 {
4483     vl_api_interface_name_renumber_reply_t * rmp;
4484     int rv = 0;
4485
4486     VALIDATE_SW_IF_INDEX(mp);
4487
4488     rv = vnet_interface_name_renumber 
4489         (ntohl(mp->sw_if_index), ntohl(mp->new_show_dev_instance));
4490     
4491     BAD_SW_IF_INDEX_LABEL;
4492
4493     REPLY_MACRO(VL_API_INTERFACE_NAME_RENUMBER_REPLY);
4494 }
4495
4496 static int arp_change_data_callback (u32 pool_index, u8 * new_mac, 
4497                                      u32 sw_if_index, u32 address)
4498 {
4499     vpe_api_main_t * am = &vpe_api_main;
4500     vlib_main_t * vm = am->vlib_main;
4501     vl_api_ip4_arp_event_t * event;
4502     static f64 arp_event_last_time;
4503     f64 now = vlib_time_now (vm);
4504
4505     if (pool_is_free_index (am->arp_events, pool_index))
4506         return 1;
4507
4508     event = pool_elt_at_index (am->arp_events, pool_index);
4509     if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac))) {
4510         memcpy (event->new_mac, new_mac, sizeof(event->new_mac));
4511     } else { /* same mac */
4512         if ((sw_if_index == event->sw_if_index) && 
4513             ((address == 0) || 
4514              /* for BD case, also check IP address with 10 sec timeout */
4515              ((address == event->address) && 
4516               ((now - arp_event_last_time) < 10.0))))
4517             return 1;
4518     }
4519     
4520     arp_event_last_time = now;
4521     event->sw_if_index = sw_if_index;
4522     if (address) event->address = address;
4523     return 0;
4524 }
4525
4526 static int arp_change_delete_callback (u32 pool_index, u8 * notused)
4527 {
4528     vpe_api_main_t * am = &vpe_api_main;
4529     
4530     if (pool_is_free_index (am->arp_events, pool_index))
4531         return 1;
4532
4533     pool_put_index (am->arp_events, pool_index);
4534     return 0;
4535 }
4536
4537 static void
4538 vl_api_want_ip4_arp_events_t_handler 
4539 (vl_api_want_ip4_arp_events_t * mp)
4540 {
4541     vpe_api_main_t * am = &vpe_api_main;
4542     vnet_main_t * vnm = vnet_get_main();
4543     vl_api_want_ip4_arp_events_reply_t *rmp;
4544     vl_api_ip4_arp_event_t * event;
4545     int rv;
4546     
4547     if (mp->enable_disable) {
4548         pool_get (am->arp_events, event);
4549         memset (event, 0, sizeof (*event));
4550
4551         event->_vl_msg_id = ntohs(VL_API_IP4_ARP_EVENT);
4552         event->client_index = mp->client_index;
4553         event->context = mp->context;
4554         event->address = mp->address;
4555         event->pid = mp->pid;
4556
4557         rv = vnet_add_del_ip4_arp_change_event 
4558             (vnm, arp_change_data_callback,
4559              mp->pid,
4560              &mp->address /* addr, in net byte order */, 
4561              vpe_resolver_process_node.index,
4562              IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */);
4563     } else {
4564         rv = vnet_add_del_ip4_arp_change_event 
4565             (vnm, arp_change_delete_callback,
4566              mp->pid,
4567              &mp->address /* addr, in net byte order */, 
4568              vpe_resolver_process_node.index,
4569              IP4_ARP_EVENT, ~0 /* pool index */, 0 /* is_add */);
4570     }
4571     REPLY_MACRO(VL_API_WANT_IP4_ARP_EVENTS_REPLY);
4572 }
4573
4574 static void vl_api_input_acl_set_interface_t_handler 
4575 (vl_api_input_acl_set_interface_t * mp)
4576 {
4577     vlib_main_t *vm = vlib_get_main();
4578     vl_api_input_acl_set_interface_reply_t * rmp;
4579     int rv;
4580     u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
4581
4582     ip4_table_index = ntohl(mp->ip4_table_index);
4583     ip6_table_index = ntohl(mp->ip6_table_index);
4584     l2_table_index = ntohl(mp->l2_table_index);
4585     sw_if_index = ntohl(mp->sw_if_index);
4586
4587     VALIDATE_SW_IF_INDEX(mp);
4588
4589     rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index, 
4590                                    ip6_table_index, l2_table_index,
4591                                    mp->is_add);
4592
4593     BAD_SW_IF_INDEX_LABEL;
4594
4595     REPLY_MACRO(VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
4596 }
4597
4598 static void vl_api_ipsec_spd_add_del_t_handler
4599 (vl_api_ipsec_spd_add_del_t * mp)
4600 {
4601 #if IPSEC == 0
4602     clib_warning ("unimplemented");
4603 #else
4604
4605     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
4606     vl_api_ipsec_spd_add_del_reply_t * rmp;
4607     int rv;
4608
4609 #if DPDK > 0
4610     rv = ipsec_add_del_spd (vm, ntohl(mp->spd_id), mp->is_add);
4611 #else
4612     rv = VNET_API_ERROR_UNIMPLEMENTED;
4613 #endif
4614
4615     REPLY_MACRO(VL_API_IPSEC_SPD_ADD_DEL_REPLY);
4616 #endif
4617 }
4618
4619 static void vl_api_ipsec_interface_add_del_spd_t_handler
4620 (vl_api_ipsec_interface_add_del_spd_t * mp)
4621 {
4622     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
4623     vl_api_ipsec_interface_add_del_spd_reply_t * rmp;
4624     int rv;
4625     u32 sw_if_index __attribute__((unused));
4626     u32 spd_id __attribute__((unused));
4627
4628     sw_if_index = ntohl(mp->sw_if_index);
4629     spd_id = ntohl(mp->spd_id);
4630
4631     VALIDATE_SW_IF_INDEX(mp);
4632
4633 #if IPSEC > 0
4634     rv = ipsec_set_interface_spd(vm, sw_if_index, spd_id, mp->is_add);
4635 #else
4636     rv = VNET_API_ERROR_UNIMPLEMENTED;
4637 #endif
4638
4639     BAD_SW_IF_INDEX_LABEL;
4640
4641     REPLY_MACRO(VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
4642 }
4643
4644 static void vl_api_ipsec_spd_add_del_entry_t_handler
4645 (vl_api_ipsec_spd_add_del_entry_t * mp)
4646 {
4647     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
4648     vl_api_ipsec_spd_add_del_entry_reply_t * rmp;
4649     int rv;
4650
4651 #if IPSEC > 0
4652     ipsec_policy_t p;
4653
4654     memset(&p, 0, sizeof(p));
4655
4656     p.id = ntohl(mp->spd_id);
4657     p.priority = ntohl(mp->priority);
4658     p.is_outbound = mp->is_outbound;
4659     p.is_ipv6 = mp->is_ipv6;
4660
4661     memcpy(&p.raddr.start, mp->remote_address_start, 16);
4662     memcpy(&p.raddr.stop, mp->remote_address_stop, 16);
4663     memcpy(&p.laddr.start, mp->local_address_start, 16);
4664     memcpy(&p.laddr.stop, mp->local_address_stop, 16);
4665
4666     p.protocol = mp->protocol;
4667     p.rport.start = ntohs(mp->remote_port_start);
4668     p.rport.stop  = ntohs(mp->remote_port_stop);
4669     p.lport.start = ntohs(mp->local_port_start);
4670     p.lport.stop  = ntohs(mp->local_port_stop);
4671     /* policy action resolve unsupported */
4672     if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE) {
4673         clib_warning("unsupported action: 'resolve'");
4674         rv = VNET_API_ERROR_UNIMPLEMENTED;
4675         goto out;
4676     }
4677     p.policy = mp->policy;
4678     p.sa_id = ntohl(mp->sa_id);
4679
4680     rv = ipsec_add_del_policy(vm, &p, mp->is_add);
4681     if (rv)
4682       goto out;
4683
4684     if (mp->is_ip_any) {
4685       p.is_ipv6 = 1;
4686       rv = ipsec_add_del_policy(vm, &p, mp->is_add);
4687     }
4688 #else
4689     rv = VNET_API_ERROR_UNIMPLEMENTED;
4690     goto out;
4691 #endif
4692
4693 out:
4694     REPLY_MACRO(VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
4695 }
4696
4697 static void vl_api_ipsec_sad_add_del_entry_t_handler
4698 (vl_api_ipsec_sad_add_del_entry_t * mp)
4699 {
4700     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
4701     vl_api_ipsec_sad_add_del_entry_reply_t * rmp;
4702     int rv;
4703 #if IPSEC > 0
4704     ipsec_sa_t sa;
4705
4706     memset(&sa, 0, sizeof(sa));
4707
4708     sa.id = ntohl(mp->sad_id);
4709     sa.spi = ntohl(mp->spi);
4710     /* security protocol AH unsupported */
4711     if (mp->protocol == IPSEC_PROTOCOL_AH) {
4712         clib_warning("unsupported security protocol 'AH'");
4713         rv = VNET_API_ERROR_UNIMPLEMENTED;
4714         goto out;
4715     }
4716     sa.protocol = mp->protocol;
4717     /* check for unsupported crypto-alg */
4718     if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
4719         mp->crypto_algorithm > IPSEC_CRYPTO_ALG_AES_CBC_256) {
4720         clib_warning("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
4721                      mp->crypto_algorithm);
4722         rv = VNET_API_ERROR_UNIMPLEMENTED;
4723         goto out;
4724     }
4725     sa.crypto_alg = mp->crypto_algorithm;
4726     sa.crypto_key_len = mp->crypto_key_length;
4727     memcpy(&sa.crypto_key, mp->crypto_key, sizeof(sa.crypto_key));
4728     /* check for unsupported integ-alg */
4729     if (mp->integrity_algorithm < IPSEC_INTEG_ALG_SHA1_96 ||
4730         mp->integrity_algorithm > IPSEC_INTEG_ALG_SHA_512_256) {
4731         clib_warning("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
4732                      mp->integrity_algorithm);
4733         rv = VNET_API_ERROR_UNIMPLEMENTED;
4734         goto out;
4735     }
4736     sa.integ_alg = mp->integrity_algorithm;
4737     sa.integ_key_len = mp->integrity_key_length;
4738     memcpy(&sa.integ_key, mp->integrity_key, sizeof(sa.integ_key));
4739     sa.use_esn = mp->use_extended_sequence_number;
4740     sa.is_tunnel = mp->is_tunnel;
4741     sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
4742     memcpy(&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
4743     memcpy(&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
4744
4745     rv = ipsec_add_del_sa(vm, &sa, mp->is_add);
4746 #else
4747     rv = VNET_API_ERROR_UNIMPLEMENTED;
4748     goto out;
4749 #endif
4750
4751 out:
4752     REPLY_MACRO(VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
4753 }
4754
4755 static void
4756 vl_api_ikev2_profile_add_del_t_handler
4757 (vl_api_ikev2_profile_add_del_t * mp)
4758 {
4759     vlib_main_t * vm = vlib_get_main();
4760     vl_api_ikev2_profile_add_del_reply_t * rmp;
4761     int rv = 0;
4762
4763 #if IPSEC > 0
4764     clib_error_t * error;
4765     u8 * tmp = format(0, "%s", mp->name);
4766     error = ikev2_add_del_profile(vm, tmp, mp->is_add);
4767     vec_free (tmp);
4768     if (error)
4769       rv = VNET_API_ERROR_UNSPECIFIED;
4770 #else
4771     rv = VNET_API_ERROR_UNIMPLEMENTED;
4772 #endif
4773
4774    REPLY_MACRO(VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
4775 }
4776
4777 static void
4778 vl_api_ikev2_profile_set_auth_t_handler
4779 (vl_api_ikev2_profile_set_auth_t * mp)
4780 {
4781     vlib_main_t * vm = vlib_get_main();
4782     vl_api_ikev2_profile_set_auth_reply_t * rmp;
4783     int rv = 0;
4784
4785 #if IPSEC > 0
4786     clib_error_t * error;
4787     u8 * tmp = format(0, "%s", mp->name);
4788     u8 * data = vec_new (u8, mp->data_len);
4789     memcpy(data, mp->data, mp->data_len);
4790     error = ikev2_set_profile_auth(vm, tmp, mp->auth_method, data, mp->is_hex);
4791     vec_free (tmp);
4792     vec_free (data);
4793     if (error)
4794       rv = VNET_API_ERROR_UNSPECIFIED;
4795 #else
4796     rv = VNET_API_ERROR_UNIMPLEMENTED;
4797 #endif
4798
4799    REPLY_MACRO(VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
4800 }
4801
4802 static void
4803 vl_api_ikev2_profile_set_id_t_handler
4804 (vl_api_ikev2_profile_set_id_t * mp)
4805 {
4806     vlib_main_t * vm = vlib_get_main();
4807     vl_api_ikev2_profile_set_id_reply_t * rmp;
4808     int rv = 0;
4809
4810 #if IPSEC > 0
4811     clib_error_t * error;
4812     u8 * tmp = format(0, "%s", mp->name);
4813     u8 * data = vec_new (u8, mp->data_len);
4814     memcpy(data, mp->data, mp->data_len);
4815     error = ikev2_set_profile_id(vm, tmp, mp->id_type, data, mp->is_local);
4816     vec_free (tmp);
4817     vec_free (data);
4818     if (error)
4819       rv = VNET_API_ERROR_UNSPECIFIED;
4820 #else
4821     rv = VNET_API_ERROR_UNIMPLEMENTED;
4822 #endif
4823
4824    REPLY_MACRO(VL_API_IKEV2_PROFILE_SET_ID_REPLY);
4825 }
4826
4827 static void
4828 vl_api_ikev2_profile_set_ts_t_handler
4829 (vl_api_ikev2_profile_set_ts_t * mp)
4830 {
4831     vlib_main_t * vm = vlib_get_main();
4832     vl_api_ikev2_profile_set_ts_reply_t * rmp;
4833     int rv = 0;
4834
4835 #if IPSEC > 0
4836     clib_error_t * error;
4837     u8 * tmp = format(0, "%s", mp->name);
4838     error = ikev2_set_profile_ts(vm, tmp, mp->proto, mp->start_port,
4839                                  mp->end_port, (ip4_address_t) mp->start_addr,
4840                                  (ip4_address_t) mp->end_addr, mp->is_local);
4841     vec_free (tmp);
4842     if (error)
4843       rv = VNET_API_ERROR_UNSPECIFIED;
4844 #else
4845     rv = VNET_API_ERROR_UNIMPLEMENTED;
4846 #endif
4847
4848    REPLY_MACRO(VL_API_IKEV2_PROFILE_SET_TS_REPLY);
4849 }
4850
4851 static void
4852 vl_api_ikev2_set_local_key_t_handler
4853 (vl_api_ikev2_set_local_key_t * mp)
4854 {
4855     vlib_main_t * vm = vlib_get_main();
4856     vl_api_ikev2_set_local_key_reply_t * rmp;
4857     int rv = 0;
4858
4859 #if IPSEC > 0
4860     clib_error_t * error;
4861
4862     error = ikev2_set_local_key(vm, mp->key_file);
4863     if (error)
4864       rv = VNET_API_ERROR_UNSPECIFIED;
4865 #else
4866     rv = VNET_API_ERROR_UNIMPLEMENTED;
4867 #endif
4868
4869    REPLY_MACRO(VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
4870 }
4871
4872 static void
4873 vl_api_map_add_domain_t_handler
4874 (vl_api_map_add_domain_t * mp)
4875 {
4876   vl_api_map_add_domain_reply_t * rmp;
4877   int rv = 0;
4878   u32 index;
4879   u8 flags = mp->is_translation ? MAP_DOMAIN_TRANSLATION : 0;
4880   rv = map_create_domain((ip4_address_t *)&mp->ip4_prefix, mp->ip4_prefix_len,
4881                          (ip6_address_t *)&mp->ip6_prefix, mp->ip6_prefix_len,
4882                          (ip6_address_t *)&mp->ip6_src, mp->ip6_src_prefix_len,
4883                          mp->ea_bits_len, mp->psid_offset, mp->psid_length, &index, ntohs(mp->mtu), flags);
4884
4885   REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
4886                ({
4887                  rmp->index = ntohl(index);
4888                }));
4889 }
4890
4891 static void
4892 vl_api_map_del_domain_t_handler
4893 (vl_api_map_del_domain_t * mp)
4894 {
4895   vl_api_map_del_domain_reply_t * rmp;
4896   int rv = 0;
4897
4898   rv = map_delete_domain(ntohl(mp->index));
4899
4900   REPLY_MACRO(VL_API_MAP_DEL_DOMAIN_REPLY);
4901 }
4902
4903 static void
4904 vl_api_map_add_del_rule_t_handler
4905 (vl_api_map_add_del_rule_t * mp)
4906 {
4907   vl_api_map_del_domain_reply_t * rmp;
4908   int rv = 0;
4909
4910   rv = map_add_del_psid(ntohl(mp->index), ntohs(mp->psid), (ip6_address_t *)mp->ip6_dst, mp->is_add);
4911
4912   REPLY_MACRO(VL_API_MAP_ADD_DEL_RULE_REPLY);
4913 }
4914
4915 static void
4916 vl_api_map_domain_dump_t_handler
4917 (vl_api_map_domain_dump_t * mp)
4918 {
4919   vl_api_map_domain_details_t * rmp;
4920   map_main_t *mm = &map_main;
4921   map_domain_t *d;
4922   unix_shared_memory_queue_t * q;
4923
4924   if (pool_elts (mm->domains) == 0)
4925       return;
4926
4927   q = vl_api_client_index_to_input_queue (mp->client_index);
4928   if (q == 0) {
4929     return;
4930   }
4931
4932   pool_foreach(d, mm->domains, ({
4933     rmp = vl_msg_api_alloc (sizeof (*rmp));
4934     memset (rmp, 0, sizeof (*rmp));
4935     rmp->_vl_msg_id = ntohs(VL_API_MAP_DOMAIN_DETAILS);
4936     rmp->domain_index = htonl(d - mm->domains);
4937     rmp->ea_bits_len = d->ea_bits_len;
4938     rmp->psid_offset = d->psid_offset;
4939     rmp->psid_length = d->psid_length;
4940     memcpy(rmp->ip4_prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix));
4941     rmp->ip4_prefix_len = d->ip4_prefix_len;
4942     memcpy(rmp->ip6_prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix));
4943     rmp->ip6_prefix_len = d->ip6_prefix_len;
4944     memcpy(rmp->ip6_src, &d->ip6_src, sizeof(rmp->ip6_src));
4945     rmp->ip6_src_len = d->ip6_src_len;
4946     rmp->mtu = htons(d->mtu);
4947     rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION);
4948
4949     vl_msg_api_send_shmem (q, (u8 *)&rmp);
4950   }));
4951 }
4952
4953 static void
4954 vl_api_map_rule_dump_t_handler
4955 (vl_api_map_rule_dump_t * mp)
4956 {
4957   unix_shared_memory_queue_t * q;
4958   u16 i;
4959   ip6_address_t dst;
4960   vl_api_map_rule_details_t * rmp;
4961   map_main_t *mm = &map_main;
4962   u32 domain_index = ntohl(mp->domain_index);
4963   map_domain_t *d;
4964
4965   if (pool_elts (mm->domains) == 0)
4966    return;
4967
4968   d = pool_elt_at_index(mm->domains, domain_index);
4969   if (!d || !d->rules) {
4970     return;
4971   }
4972
4973   q = vl_api_client_index_to_input_queue (mp->client_index);
4974   if (q == 0) {
4975     return;
4976   }
4977
4978   for (i = 0; i < (0x1 << d->psid_length); i++) {
4979     dst = d->rules[i];
4980     if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0) {
4981       continue;
4982     }
4983     rmp = vl_msg_api_alloc(sizeof(*rmp));
4984     memset(rmp, 0, sizeof(*rmp));
4985     rmp->_vl_msg_id = ntohs(VL_API_MAP_RULE_DETAILS);
4986     rmp->psid = htons(i);
4987     memcpy(rmp->ip6_dst, &dst, sizeof(rmp->ip6_dst));
4988     vl_msg_api_send_shmem(q, (u8 *)&rmp);
4989   }
4990 }
4991
4992 static void
4993 vl_api_map_summary_stats_t_handler (
4994     vl_api_map_summary_stats_t *mp)
4995 {
4996     vl_api_map_summary_stats_reply_t *rmp;
4997     vlib_combined_counter_main_t *cm;
4998     vlib_counter_t v;
4999     int i, which;
5000     u64 total_pkts[VLIB_N_RX_TX];
5001     u64 total_bytes[VLIB_N_RX_TX];
5002     map_main_t *mm = &map_main;
5003     unix_shared_memory_queue_t *q =
5004         vl_api_client_index_to_input_queue(mp->client_index);
5005
5006     if (!q)
5007         return;
5008     
5009     rmp = vl_msg_api_alloc (sizeof (*rmp));
5010     rmp->_vl_msg_id = ntohs(VL_API_MAP_SUMMARY_STATS_REPLY);
5011     rmp->context = mp->context;
5012     rmp->retval = 0;
5013
5014     memset (total_pkts, 0, sizeof (total_pkts));
5015     memset (total_bytes, 0, sizeof (total_bytes));
5016
5017     map_domain_counter_lock (mm);
5018     vec_foreach(cm, mm->domain_counters) {
5019       which = cm - mm->domain_counters;
5020
5021       for (i = 0; i < vec_len(cm->maxi); i++) {
5022         vlib_get_combined_counter (cm, i, &v);
5023         total_pkts[which] += v.packets;
5024         total_bytes[which] += v.bytes;
5025       }
5026     }
5027
5028     map_domain_counter_unlock (mm);
5029
5030     /* Note: in HOST byte order! */
5031     rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] = total_pkts[MAP_DOMAIN_COUNTER_RX];
5032     rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] = total_bytes[MAP_DOMAIN_COUNTER_RX];
5033     rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] = total_pkts[MAP_DOMAIN_COUNTER_TX];
5034     rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] = total_bytes[MAP_DOMAIN_COUNTER_TX];
5035     rmp->total_bindings = pool_elts(mm->domains);
5036     rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
5037     rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] = map_error_counter_get(ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK);
5038     rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] = map_error_counter_get(ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK);
5039
5040     vl_msg_api_send_shmem(q, (u8 *)&rmp);
5041 }
5042
5043 static void vl_api_ipsec_sa_set_key_t_handler
5044 (vl_api_ipsec_sa_set_key_t * mp)
5045 {
5046     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5047     vl_api_ipsec_sa_set_key_reply_t *rmp;
5048     int rv;
5049 #if IPSEC > 0
5050     ipsec_sa_t sa;
5051     sa.id = ntohl(mp->sa_id);
5052     sa.crypto_key_len = mp->crypto_key_length;
5053     memcpy(&sa.crypto_key, mp->crypto_key, sizeof(sa.crypto_key));
5054     sa.integ_key_len = mp->integrity_key_length;
5055     memcpy(&sa.integ_key, mp->integrity_key, sizeof(sa.integ_key));
5056
5057     rv = ipsec_set_sa_key(vm, &sa);
5058 #else
5059     rv = VNET_API_ERROR_UNIMPLEMENTED;
5060 #endif
5061
5062     REPLY_MACRO(VL_API_IPSEC_SA_SET_KEY_REPLY);
5063 }
5064
5065 static void vl_api_cop_interface_enable_disable_t_handler
5066 (vl_api_cop_interface_enable_disable_t * mp)
5067 {
5068     vl_api_cop_interface_enable_disable_reply_t * rmp;
5069     int rv;
5070     u32 sw_if_index = ntohl(mp->sw_if_index);
5071     int enable_disable;
5072
5073     VALIDATE_SW_IF_INDEX(mp);
5074
5075     enable_disable = (int) mp->enable_disable;
5076
5077     rv = cop_interface_enable_disable (sw_if_index, enable_disable);
5078
5079     BAD_SW_IF_INDEX_LABEL;
5080
5081     REPLY_MACRO(VL_API_COP_INTERFACE_ENABLE_DISABLE_REPLY);
5082 }
5083
5084 static void vl_api_cop_whitelist_enable_disable_t_handler
5085 (vl_api_cop_whitelist_enable_disable_t * mp)
5086 {
5087     vl_api_cop_whitelist_enable_disable_reply_t * rmp;
5088     cop_whitelist_enable_disable_args_t _a, *a=&_a;
5089     u32 sw_if_index = ntohl(mp->sw_if_index);
5090     int rv;
5091
5092     VALIDATE_SW_IF_INDEX(mp);
5093
5094     a->sw_if_index = sw_if_index;
5095     a->ip4 = mp->ip4;
5096     a->ip6 = mp->ip6;
5097     a->default_cop = mp->default_cop;
5098     a->fib_id = ntohl(mp->fib_id);
5099
5100     rv = cop_whitelist_enable_disable (a);
5101
5102     BAD_SW_IF_INDEX_LABEL;
5103
5104     REPLY_MACRO(VL_API_COP_WHITELIST_ENABLE_DISABLE_REPLY);
5105 }
5106
5107 static void vl_api_get_node_graph_t_handler
5108 (vl_api_get_node_graph_t * mp)
5109 {
5110     int rv = 0;
5111     u8 * vector = 0;
5112     api_main_t * am = &api_main;
5113     vlib_main_t * vm = vlib_get_main();
5114     void * oldheap;
5115     vl_api_get_node_graph_reply_t * rmp;
5116     
5117     pthread_mutex_lock (&am->vlib_rp->mutex);
5118     oldheap = svm_push_data_heap (am->vlib_rp);
5119     
5120     /* 
5121      * Keep the number of memcpy ops to a minimum (e.g. 1).
5122      * The current size of the serialized vector is
5123      * slightly under 4K.
5124      */
5125     vec_validate (vector, 4095);
5126     vec_reset_length (vector);
5127
5128     vector = vlib_node_serialize (&vm->node_main, vector);
5129     
5130     svm_pop_heap (oldheap);
5131     pthread_mutex_unlock (&am->vlib_rp->mutex);
5132
5133     REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
5134                  rmp->reply_in_shmem = (uword) vector);
5135 }
5136
5137 static void vl_api_trace_profile_add_t_handler
5138 (vl_api_trace_profile_add_t *mp)
5139 {
5140     int rv = 0;
5141     vl_api_trace_profile_add_reply_t * rmp;
5142     clib_error_t *error;
5143
5144     /* Ignoring the profile id as currently a single profile 
5145      * is supported */
5146     error = ip6_ioam_trace_profile_set(mp->trace_num_elt, mp->trace_type, 
5147                                ntohl(mp->node_id), ntohl(mp->trace_app_data), 
5148                                mp->pow_enable, mp->trace_tsp, 
5149                                mp->trace_ppc);
5150     if (error) {
5151       clib_error_report(error);
5152       rv = clib_error_get_code(error);
5153     }
5154     
5155     REPLY_MACRO(VL_API_TRACE_PROFILE_ADD_REPLY);
5156 }
5157
5158 static void vl_api_trace_profile_apply_t_handler
5159 (vl_api_trace_profile_apply_t *mp)
5160 {
5161     int rv = 0;
5162     vl_api_trace_profile_apply_reply_t * rmp;
5163  
5164     if (mp->enable != 0) {
5165       rv = ip6_ioam_set_destination ((ip6_address_t *)(&mp->dest_ipv6),
5166                                 ntohl(mp->prefix_length),
5167                                 ntohl(mp->vrf_id), 
5168                         mp->trace_op == IOAM_HBYH_ADD,
5169                         mp->trace_op == IOAM_HBYH_POP,
5170                         mp->trace_op == IOAM_HBYH_MOD);
5171     } else {
5172       //ip6_ioam_clear_destination(&ip6, mp->prefix_length, mp->vrf_id);
5173     }
5174     REPLY_MACRO(VL_API_TRACE_PROFILE_APPLY_REPLY);
5175 }
5176
5177 static void vl_api_trace_profile_del_t_handler
5178 (vl_api_trace_profile_del_t *mp)
5179 {
5180     int rv = 0;
5181     vl_api_trace_profile_del_reply_t * rmp;
5182     clib_error_t *error;
5183
5184     error = clear_ioam_rewrite_fn();
5185     if (error) {
5186       clib_error_report(error);
5187       rv = clib_error_get_code(error);
5188     }
5189     
5190     REPLY_MACRO(VL_API_TRACE_PROFILE_DEL_REPLY);
5191 }
5192
5193
5194 #define BOUNCE_HANDLER(nn)                                              \
5195 static void vl_api_##nn##_t_handler (                                   \
5196     vl_api_##nn##_t *mp)                                                \
5197 {                                                                       \
5198     vpe_client_registration_t *reg;                                     \
5199     vpe_api_main_t * vam = &vpe_api_main;                               \
5200     unix_shared_memory_queue_t * q;                                     \
5201                                                                         \
5202     /* One registration only... */                                      \
5203     pool_foreach(reg, vam->nn##_registrations,                          \
5204     ({                                                                  \
5205         q = vl_api_client_index_to_input_queue (reg->client_index);     \
5206         if (q) {                                                        \
5207             /*                                                          \
5208              * If the queue is stuffed, turf the msg and complain       \
5209              * It's unlikely that the intended recipient is             \
5210              * alive; avoid deadlock at all costs.                      \
5211              */                                                         \
5212             if (q->cursize == q->maxsize) {                             \
5213                 clib_warning ("ERROR: receiver queue full, drop msg");  \
5214                 vl_msg_api_free (mp);                                   \
5215                 return;                                                 \
5216             }                                                           \
5217             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
5218             return;                                                     \
5219         }                                                               \
5220     }));                                                                \
5221     vl_msg_api_free (mp);                                               \
5222 }
5223
5224 BOUNCE_HANDLER(to_netconf_server);
5225 BOUNCE_HANDLER(from_netconf_server);
5226 BOUNCE_HANDLER(to_netconf_client);
5227 BOUNCE_HANDLER(from_netconf_client);
5228
5229 /*
5230  * vpe_api_hookup
5231  * Add vpe's API message handlers to the table.
5232  * vlib has alread mapped shared memory and
5233  * added the client registration handlers. 
5234  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
5235  */
5236
5237 static clib_error_t *
5238 vpe_api_hookup (vlib_main_t *vm)
5239 {
5240     api_main_t * am = &api_main;
5241
5242 #define _(N,n)                                                  \
5243     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
5244                            vl_api_##n##_t_handler,              \
5245                            vl_noop_handler,                     \
5246                            vl_api_##n##_t_endian,               \
5247                            vl_api_##n##_t_print,                \
5248                            sizeof(vl_api_##n##_t), 1); 
5249     foreach_vpe_api_msg;
5250 #undef _
5251
5252     /* 
5253      * Manually register the sr tunnel add del msg, so we trace
5254      * enough bytes to capture a typical segment list
5255      */
5256     vl_msg_api_set_handlers (VL_API_SR_TUNNEL_ADD_DEL, 
5257                              "sr_tunnel_add_del",
5258                              vl_api_sr_tunnel_add_del_t_handler,
5259                              vl_noop_handler,
5260                              vl_api_sr_tunnel_add_del_t_endian,
5261                              vl_api_sr_tunnel_add_del_t_print,
5262                              256, 1);
5263
5264     am->message_bounce [VL_API_FROM_NETCONF_SERVER] = 1;
5265     am->message_bounce [VL_API_TO_NETCONF_SERVER] = 1;
5266     am->message_bounce [VL_API_FROM_NETCONF_CLIENT] = 1;
5267     am->message_bounce [VL_API_TO_NETCONF_CLIENT] = 1;
5268
5269     /* 
5270      * Trace space for 8 MPLS encap labels, classifier mask+match
5271      */
5272     am->api_trace_cfg [VL_API_MPLS_ADD_DEL_ENCAP].size += 8 * sizeof(u32);
5273     am->api_trace_cfg [VL_API_CLASSIFY_ADD_DEL_TABLE].size
5274         += 5 * sizeof (u32x4);
5275     am->api_trace_cfg [VL_API_CLASSIFY_ADD_DEL_SESSION].size
5276         += 5 * sizeof (u32x4);
5277     am->api_trace_cfg [VL_API_VXLAN_ADD_DEL_TUNNEL].size
5278         += 16 * sizeof (u32);
5279     
5280     /*
5281      * trace space for 4 nsh-gre variable TLV words
5282      */
5283     am->api_trace_cfg [VL_API_NSH_GRE_ADD_DEL_TUNNEL].size
5284         += 4 * sizeof (u32);
5285
5286     /* 
5287      * Thread-safe API messages
5288      */
5289     am->is_mp_safe [VL_API_IP_ADD_DEL_ROUTE] = 1;
5290
5291     return 0;
5292 }
5293
5294 VLIB_API_INIT_FUNCTION(vpe_api_hookup);
5295
5296 static clib_error_t *
5297 vpe_api_init (vlib_main_t *vm)
5298 {
5299     vpe_api_main_t *am = &vpe_api_main;
5300
5301     am->vlib_main = vm;
5302     am->vnet_main = vnet_get_main();
5303     am->interface_events_registration_hash = hash_create (0, sizeof (uword));
5304     am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
5305     am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
5306     am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
5307     am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
5308     am->oam_events_registration_hash = hash_create (0, sizeof (uword));
5309
5310     vl_api_init (vm);
5311     vl_set_memory_region_name ("/vpe-api");
5312     vl_enable_disable_memory_api (vm, 1 /* enable it */);
5313
5314     return 0;
5315 }
5316
5317 VLIB_INIT_FUNCTION(vpe_api_init);
5318
5319 static clib_error_t *
5320 chroot_config (vlib_main_t * vm, unformat_input_t * input)
5321 {
5322   u8 * chroot_path;
5323
5324   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
5325     {
5326       if (unformat (input, "prefix %s", &chroot_path))
5327         {
5328           vec_add1 (chroot_path, 0);
5329           vl_set_memory_root_path ((char *)chroot_path);
5330         }
5331       else
5332         return clib_error_return (0, "unknown input `%U'",
5333                                   format_unformat_error, input);
5334     }
5335   return 0;
5336 }
5337 VLIB_EARLY_CONFIG_FUNCTION (chroot_config, "chroot");
5338
5339 void * get_unformat_vnet_sw_interface (void)
5340 {
5341     return (void *) &unformat_vnet_sw_interface;
5342 }
5343
5344 #undef vl_api_version
5345 #define vl_api_version(n,v) static u32 vpe_api_version = v;
5346 #include <api/vpe.api.h>
5347 #undef vl_api_version
5348
5349 int vl_msg_api_version_check (vl_api_memclnt_create_t * mp)
5350 {
5351     if (clib_host_to_net_u32(mp->api_versions[0]) != vpe_api_version) {
5352         clib_warning ("vpe API mismatch: 0x%08x instead of 0x%08x",
5353                       clib_host_to_net_u32 (mp->api_versions[0]),
5354                       vpe_api_version);
5355         return -1;
5356     }
5357     return 0;
5358 }
5359
5360 static u8 * format_arp_event (u8 * s, va_list * args)
5361 {
5362     vl_api_ip4_arp_event_t * event = va_arg (*args, vl_api_ip4_arp_event_t *);
5363
5364     s = format (s, "pid %d: %U", event->pid,
5365                 format_ip4_address, &event->address);
5366     return s;
5367 }
5368
5369 static clib_error_t * 
5370 show_ip4_arp_events_fn (vlib_main_t * vm,
5371                         unformat_input_t * input, 
5372                         vlib_cli_command_t * cmd)
5373 {
5374     vpe_api_main_t * am = &vpe_api_main;
5375     vl_api_ip4_arp_event_t * event;
5376
5377     if (pool_elts (am->arp_events) == 0) {
5378         vlib_cli_output (vm, "No active arp event registrations");
5379         return 0;
5380     }
5381
5382     pool_foreach (event, am->arp_events, 
5383     ({
5384         vlib_cli_output (vm, "%U", format_arp_event, event);
5385     }));
5386
5387     return 0;
5388 }
5389
5390 VLIB_CLI_COMMAND (show_ip4_arp_events, static) = {
5391   .path = "show arp event registrations",
5392   .function = show_ip4_arp_events_fn,
5393   .short_help = "Show arp event registrations",
5394 };