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