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