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