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