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