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