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