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