API refactoring : l2, mpls, sr
[vpp.git] / src / vpp / api / api.c
1 /*
2  *------------------------------------------------------------------
3  * api.c - message handler registration
4  *
5  * Copyright (c) 2010-2016 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 <pwd.h>
34 #include <grp.h>
35
36 #include <vppinfra/clib.h>
37 #include <vppinfra/vec.h>
38 #include <vppinfra/hash.h>
39 #include <vppinfra/bitmap.h>
40 #include <vppinfra/fifo.h>
41 #include <vppinfra/time.h>
42 #include <vppinfra/mheap.h>
43 #include <vppinfra/heap.h>
44 #include <vppinfra/pool.h>
45 #include <vppinfra/format.h>
46 #include <vppinfra/error.h>
47
48 #include <vnet/api_errno.h>
49 #include <vnet/vnet.h>
50 #include <vnet/l2/l2_input.h>
51 #include <vnet/l2/l2_bd.h>
52 #include <vnet/ip/ip.h>
53 #include <vnet/ip/ip6.h>
54 #include <vnet/ip/ip6_neighbor.h>
55 #include <vnet/dhcp/proxy.h>
56 #include <vnet/dhcp/client.h>
57 #if IPV6SR > 0
58 #include <vnet/sr/sr.h>
59 #endif
60 #include <vnet/dhcpv6/proxy.h>
61 #include <vlib/vlib.h>
62 #include <vlib/unix/unix.h>
63 #include <vlibapi/api.h>
64 #include <vlibmemory/api.h>
65 #include <vnet/classify/vnet_classify.h>
66 #include <vnet/classify/input_acl.h>
67 #include <vnet/classify/policer_classify.h>
68 #include <vnet/classify/flow_classify.h>
69 #include <vnet/l2/l2_classify.h>
70 #include <vnet/vxlan/vxlan.h>
71 #include <vnet/l2/l2_vtr.h>
72 #include <vnet/vxlan-gpe/vxlan_gpe.h>
73 #include <vnet/map/map.h>
74 #include <vnet/cop/cop.h>
75 #include <vnet/ip/ip6_hop_by_hop.h>
76 #include <vnet/ip/ip_source_and_port_range_check.h>
77 #include <vnet/policer/policer.h>
78 #include <vnet/flow/flow_report.h>
79 #include <vnet/flow/flow_report_classify.h>
80 #include <vnet/ip/punt.h>
81 #include <vnet/feature/feature.h>
82
83 #undef BIHASH_TYPE
84 #undef __included_bihash_template_h__
85 #include <vnet/l2/l2_fib.h>
86
87 #include <vpp/stats/stats.h>
88 #include <vpp/oam/oam.h>
89
90 #include <vnet/ethernet/ethernet.h>
91 #include <vnet/ethernet/arp_packet.h>
92 #include <vnet/interface.h>
93 #include <vnet/l2/l2_fib.h>
94 #include <vnet/l2/l2_bd.h>
95 #include <vpp/api/vpe_msg_enum.h>
96 #include <vnet/span/span.h>
97 #include <vnet/fib/ip6_fib.h>
98 #include <vnet/fib/ip4_fib.h>
99 #include <vnet/fib/fib_api.h>
100 #include <vnet/dpo/drop_dpo.h>
101 #include <vnet/dpo/receive_dpo.h>
102 #include <vnet/dpo/lookup_dpo.h>
103 #include <vnet/dpo/classify_dpo.h>
104 #include <vnet/dpo/ip_null_dpo.h>
105 #define vl_typedefs             /* define message structures */
106 #include <vpp/api/vpe_all_api_h.h>
107 #undef vl_typedefs
108 #define vl_endianfun            /* define message structures */
109 #include <vpp/api/vpe_all_api_h.h>
110 #undef vl_endianfun
111 /* instantiate all the print functions we know about */
112 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
113 #define vl_printfun
114 #include <vpp/api/vpe_all_api_h.h>
115 #undef vl_printfun
116 #include <vlibapi/api_helper_macros.h>
117 #define foreach_vpe_api_msg                                             \
118 _(WANT_OAM_EVENTS, want_oam_events)                                     \
119 _(OAM_ADD_DEL, oam_add_del)                                             \
120 _(IS_ADDRESS_REACHABLE, is_address_reachable)                           \
121 _(SW_INTERFACE_SET_MPLS_ENABLE, sw_interface_set_mpls_enable)           \
122 _(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath)                       \
123 _(SW_INTERFACE_SET_VXLAN_BYPASS, sw_interface_set_vxlan_bypass)         \
124 _(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect)           \
125 _(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge)               \
126 _(CREATE_VLAN_SUBIF, create_vlan_subif)                                 \
127 _(CREATE_SUBIF, create_subif)                                           \
128 _(PROXY_ARP_ADD_DEL, proxy_arp_add_del)                                 \
129 _(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable)       \
130 _(VNET_GET_SUMMARY_STATS, vnet_get_summary_stats)                       \
131 _(RESET_FIB, reset_fib)                                                 \
132 _(DHCP_PROXY_CONFIG,dhcp_proxy_config)                                  \
133 _(DHCP_PROXY_CONFIG_2,dhcp_proxy_config_2)                              \
134 _(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss)                                \
135 _(DHCP_CLIENT_CONFIG, dhcp_client_config)                               \
136 _(CREATE_LOOPBACK, create_loopback)                                     \
137 _(CONTROL_PING, control_ping)                                           \
138 _(CLI_REQUEST, cli_request)                                             \
139 _(CLI_INBAND, cli_inband)                                               \
140 _(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit)                       \
141 _(L2_PATCH_ADD_DEL, l2_patch_add_del)                                   \
142 _(CLASSIFY_ADD_DEL_TABLE, classify_add_del_table)                       \
143 _(CLASSIFY_ADD_DEL_SESSION, classify_add_del_session)                   \
144 _(CLASSIFY_SET_INTERFACE_IP_TABLE, classify_set_interface_ip_table)     \
145 _(CLASSIFY_SET_INTERFACE_L2_TABLES, classify_set_interface_l2_tables)   \
146 _(GET_NODE_INDEX, get_node_index)                                       \
147 _(ADD_NODE_NEXT, add_node_next)                                         \
148 _(VXLAN_ADD_DEL_TUNNEL, vxlan_add_del_tunnel)                           \
149 _(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump)                                 \
150 _(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter)                     \
151 _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite)         \
152 _(SHOW_VERSION, show_version)                                           \
153 _(VXLAN_GPE_ADD_DEL_TUNNEL, vxlan_gpe_add_del_tunnel)                   \
154 _(VXLAN_GPE_TUNNEL_DUMP, vxlan_gpe_tunnel_dump)                         \
155 _(INTERFACE_NAME_RENUMBER, interface_name_renumber)                     \
156 _(WANT_IP4_ARP_EVENTS, want_ip4_arp_events)                             \
157 _(WANT_IP6_ND_EVENTS, want_ip6_nd_events)                               \
158 _(INPUT_ACL_SET_INTERFACE, input_acl_set_interface)                     \
159 _(DELETE_LOOPBACK, delete_loopback)                                     \
160 _(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del)                                 \
161 _(COP_INTERFACE_ENABLE_DISABLE, cop_interface_enable_disable)           \
162 _(COP_WHITELIST_ENABLE_DISABLE, cop_whitelist_enable_disable)           \
163 _(GET_NODE_GRAPH, get_node_graph)                                       \
164 _(IOAM_ENABLE, ioam_enable)                                             \
165 _(IOAM_DISABLE, ioam_disable)                                           \
166 _(POLICER_ADD_DEL, policer_add_del)                                     \
167 _(POLICER_DUMP, policer_dump)                                           \
168 _(POLICER_CLASSIFY_SET_INTERFACE, policer_classify_set_interface)       \
169 _(POLICER_CLASSIFY_DUMP, policer_classify_dump)                         \
170 _(CLASSIFY_TABLE_IDS,classify_table_ids)                                \
171 _(CLASSIFY_TABLE_BY_INTERFACE, classify_table_by_interface)             \
172 _(CLASSIFY_TABLE_INFO,classify_table_info)                              \
173 _(CLASSIFY_SESSION_DUMP,classify_session_dump)                          \
174 _(CLASSIFY_SESSION_DETAILS,classify_session_details)                    \
175 _(SET_IPFIX_EXPORTER, set_ipfix_exporter)                               \
176 _(IPFIX_EXPORTER_DUMP, ipfix_exporter_dump)                             \
177 _(SET_IPFIX_CLASSIFY_STREAM, set_ipfix_classify_stream)                 \
178 _(IPFIX_CLASSIFY_STREAM_DUMP, ipfix_classify_stream_dump)               \
179 _(IPFIX_CLASSIFY_TABLE_ADD_DEL, ipfix_classify_table_add_del)           \
180 _(IPFIX_CLASSIFY_TABLE_DUMP, ipfix_classify_table_dump)                 \
181 _(GET_NEXT_INDEX, get_next_index)                                       \
182 _(PG_CREATE_INTERFACE, pg_create_interface)                             \
183 _(PG_CAPTURE, pg_capture)                                               \
184 _(PG_ENABLE_DISABLE, pg_enable_disable)                                 \
185 _(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL,                               \
186   ip_source_and_port_range_check_add_del)                               \
187 _(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL,                     \
188   ip_source_and_port_range_check_interface_add_del)                     \
189 _(DELETE_SUBIF, delete_subif)                                           \
190 _(L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite)           \
191 _(PUNT, punt)                                                           \
192 _(FLOW_CLASSIFY_SET_INTERFACE, flow_classify_set_interface)             \
193 _(FLOW_CLASSIFY_DUMP, flow_classify_dump)                               \
194 _(FEATURE_ENABLE_DISABLE, feature_enable_disable)
195
196 #define QUOTE_(x) #x
197 #define QUOTE(x) QUOTE_(x)
198 typedef enum
199 {
200   RESOLVE_IP4_ADD_DEL_ROUTE = 1,
201   RESOLVE_IP6_ADD_DEL_ROUTE,
202 } resolve_t;
203
204 static vlib_node_registration_t vpe_resolver_process_node;
205 vpe_api_main_t vpe_api_main;
206
207 static int arp_change_delete_callback (u32 pool_index, u8 * notused);
208 static int nd_change_delete_callback (u32 pool_index, u8 * notused);
209
210 /* Clean up all registrations belonging to the indicated client */
211 int
212 vl_api_memclnt_delete_callback (u32 client_index)
213 {
214   vpe_api_main_t *vam = &vpe_api_main;
215   vpe_client_registration_t *rp;
216   uword *p;
217   int stats_memclnt_delete_callback (u32 client_index);
218
219   stats_memclnt_delete_callback (client_index);
220
221 #define _(a)                                                    \
222     p = hash_get (vam->a##_registration_hash, client_index);    \
223     if (p) {                                                    \
224         rp = pool_elt_at_index (vam->a##_registrations, p[0]);  \
225         pool_put (vam->a##_registrations, rp);                  \
226         hash_unset (vam->a##_registration_hash, client_index);  \
227     }
228   foreach_registration_hash;
229 #undef _
230   return 0;
231 }
232
233 pub_sub_handler (oam_events, OAM_EVENTS);
234
235 #define RESOLUTION_EVENT 1
236 #define RESOLUTION_PENDING_EVENT 2
237 #define IP4_ARP_EVENT 3
238 #define IP6_ND_EVENT 4
239
240 int ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp);
241
242 int ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp);
243
244 void
245 handle_ip4_arp_event (u32 pool_index)
246 {
247   vpe_api_main_t *vam = &vpe_api_main;
248   vnet_main_t *vnm = vam->vnet_main;
249   vlib_main_t *vm = vam->vlib_main;
250   vl_api_ip4_arp_event_t *event;
251   vl_api_ip4_arp_event_t *mp;
252   unix_shared_memory_queue_t *q;
253
254   /* Client can cancel, die, etc. */
255   if (pool_is_free_index (vam->arp_events, pool_index))
256     return;
257
258   event = pool_elt_at_index (vam->arp_events, pool_index);
259
260   q = vl_api_client_index_to_input_queue (event->client_index);
261   if (!q)
262     {
263       (void) vnet_add_del_ip4_arp_change_event
264         (vnm, arp_change_delete_callback,
265          event->pid, &event->address,
266          vpe_resolver_process_node.index, IP4_ARP_EVENT,
267          ~0 /* pool index, notused */ , 0 /* is_add */ );
268       return;
269     }
270
271   if (q->cursize < q->maxsize)
272     {
273       mp = vl_msg_api_alloc (sizeof (*mp));
274       clib_memcpy (mp, event, sizeof (*mp));
275       vl_msg_api_send_shmem (q, (u8 *) & mp);
276     }
277   else
278     {
279       static f64 last_time;
280       /*
281        * Throttle syslog msgs.
282        * It's pretty tempting to just revoke the registration...
283        */
284       if (vlib_time_now (vm) > last_time + 10.0)
285         {
286           clib_warning ("arp event for %U to pid %d: queue stuffed!",
287                         format_ip4_address, &event->address, event->pid);
288           last_time = vlib_time_now (vm);
289         }
290     }
291 }
292
293 void
294 handle_ip6_nd_event (u32 pool_index)
295 {
296   vpe_api_main_t *vam = &vpe_api_main;
297   vnet_main_t *vnm = vam->vnet_main;
298   vlib_main_t *vm = vam->vlib_main;
299   vl_api_ip6_nd_event_t *event;
300   vl_api_ip6_nd_event_t *mp;
301   unix_shared_memory_queue_t *q;
302
303   /* Client can cancel, die, etc. */
304   if (pool_is_free_index (vam->nd_events, pool_index))
305     return;
306
307   event = pool_elt_at_index (vam->nd_events, pool_index);
308
309   q = vl_api_client_index_to_input_queue (event->client_index);
310   if (!q)
311     {
312       (void) vnet_add_del_ip6_nd_change_event
313         (vnm, nd_change_delete_callback,
314          event->pid, &event->address,
315          vpe_resolver_process_node.index, IP6_ND_EVENT,
316          ~0 /* pool index, notused */ , 0 /* is_add */ );
317       return;
318     }
319
320   if (q->cursize < q->maxsize)
321     {
322       mp = vl_msg_api_alloc (sizeof (*mp));
323       clib_memcpy (mp, event, sizeof (*mp));
324       vl_msg_api_send_shmem (q, (u8 *) & mp);
325     }
326   else
327     {
328       static f64 last_time;
329       /*
330        * Throttle syslog msgs.
331        * It's pretty tempting to just revoke the registration...
332        */
333       if (vlib_time_now (vm) > last_time + 10.0)
334         {
335           clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
336                         format_ip6_address, &event->address, event->pid);
337           last_time = vlib_time_now (vm);
338         }
339     }
340 }
341
342 static uword
343 resolver_process (vlib_main_t * vm,
344                   vlib_node_runtime_t * rt, vlib_frame_t * f)
345 {
346   uword event_type;
347   uword *event_data = 0;
348   f64 timeout = 100.0;
349   int i;
350
351   while (1)
352     {
353       vlib_process_wait_for_event_or_clock (vm, timeout);
354
355       event_type = vlib_process_get_events (vm, &event_data);
356
357       switch (event_type)
358         {
359         case RESOLUTION_PENDING_EVENT:
360           timeout = 1.0;
361           break;
362
363         case RESOLUTION_EVENT:
364           clib_warning ("resolver: BOGUS TYPE");
365           break;
366
367         case IP4_ARP_EVENT:
368           for (i = 0; i < vec_len (event_data); i++)
369             handle_ip4_arp_event (event_data[i]);
370           break;
371
372         case IP6_ND_EVENT:
373           for (i = 0; i < vec_len (event_data); i++)
374             handle_ip6_nd_event (event_data[i]);
375           break;
376
377         case ~0:                /* timeout */
378           break;
379         }
380
381       vec_reset_length (event_data);
382     }
383   return 0;                     /* or not */
384 }
385
386 /* *INDENT-OFF* */
387 VLIB_REGISTER_NODE (vpe_resolver_process_node,static) = {
388   .function = resolver_process,
389   .type = VLIB_NODE_TYPE_PROCESS,
390   .name = "vpe-route-resolver-process",
391 };
392 /* *INDENT-ON* */
393
394 static void
395 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
396 {
397   vl_api_sw_interface_set_vpath_reply_t *rmp;
398   int rv = 0;
399   u32 sw_if_index = ntohl (mp->sw_if_index);
400
401   VALIDATE_SW_IF_INDEX (mp);
402
403   l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
404   vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
405                                sw_if_index, mp->enable, 0, 0);
406   vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
407                                sw_if_index, mp->enable, 0, 0);
408   vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
409                                sw_if_index, mp->enable, 0, 0);
410   vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
411                                sw_if_index, mp->enable, 0, 0);
412
413   BAD_SW_IF_INDEX_LABEL;
414
415   REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
416 }
417
418 static void
419   vl_api_sw_interface_set_vxlan_bypass_t_handler
420   (vl_api_sw_interface_set_vxlan_bypass_t * mp)
421 {
422   vl_api_sw_interface_set_vxlan_bypass_reply_t *rmp;
423   int rv = 0;
424   u32 sw_if_index = ntohl (mp->sw_if_index);
425
426   VALIDATE_SW_IF_INDEX (mp);
427
428   if (mp->is_ipv6)
429     {
430       /* not yet implemented */
431     }
432   else
433     vnet_feature_enable_disable ("ip4-unicast", "ip4-vxlan-bypass",
434                                  sw_if_index, mp->enable, 0, 0);
435
436   BAD_SW_IF_INDEX_LABEL;
437
438   REPLY_MACRO (VL_API_SW_INTERFACE_SET_VXLAN_BYPASS_REPLY);
439 }
440
441 static void
442   vl_api_sw_interface_set_l2_xconnect_t_handler
443   (vl_api_sw_interface_set_l2_xconnect_t * mp)
444 {
445   vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
446   int rv = 0;
447   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
448   u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
449   vlib_main_t *vm = vlib_get_main ();
450   vnet_main_t *vnm = vnet_get_main ();
451
452   VALIDATE_RX_SW_IF_INDEX (mp);
453
454   if (mp->enable)
455     {
456       VALIDATE_TX_SW_IF_INDEX (mp);
457       rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
458                             rx_sw_if_index, 0, 0, 0, tx_sw_if_index);
459     }
460   else
461     {
462       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
463     }
464
465   BAD_RX_SW_IF_INDEX_LABEL;
466   BAD_TX_SW_IF_INDEX_LABEL;
467
468   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
469 }
470
471 static void
472   vl_api_sw_interface_set_l2_bridge_t_handler
473   (vl_api_sw_interface_set_l2_bridge_t * mp)
474 {
475   bd_main_t *bdm = &bd_main;
476   vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
477   int rv = 0;
478   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
479   u32 bd_id = ntohl (mp->bd_id);
480   u32 bd_index;
481   u32 bvi = mp->bvi;
482   u8 shg = mp->shg;
483   vlib_main_t *vm = vlib_get_main ();
484   vnet_main_t *vnm = vnet_get_main ();
485
486   VALIDATE_RX_SW_IF_INDEX (mp);
487
488   bd_index = bd_find_or_add_bd_index (bdm, bd_id);
489
490   if (mp->enable)
491     {
492       //VALIDATE_TX_SW_IF_INDEX(mp);
493       rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
494                             rx_sw_if_index, bd_index, bvi, shg, 0);
495     }
496   else
497     {
498       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
499     }
500
501   BAD_RX_SW_IF_INDEX_LABEL;
502
503   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
504 }
505
506 static void
507 vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
508 {
509   bd_main_t *bdm = &bd_main;
510   vl_api_bd_ip_mac_add_del_reply_t *rmp;
511   int rv = 0;
512   u32 bd_id = ntohl (mp->bd_id);
513   u32 bd_index;
514   uword *p;
515
516   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
517   if (p == 0)
518     {
519       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
520       goto out;
521     }
522
523   bd_index = p[0];
524   if (bd_add_del_ip_mac (bd_index, mp->ip_address,
525                          mp->mac_address, mp->is_ipv6, mp->is_add))
526     rv = VNET_API_ERROR_UNSPECIFIED;
527
528 out:
529   REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
530 }
531
532 static void
533 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
534 {
535   vl_api_create_vlan_subif_reply_t *rmp;
536   vnet_main_t *vnm = vnet_get_main ();
537   u32 hw_if_index, sw_if_index = (u32) ~ 0;
538   vnet_hw_interface_t *hi;
539   int rv = 0;
540   u32 id;
541   vnet_sw_interface_t template;
542   uword *p;
543   vnet_interface_main_t *im = &vnm->interface_main;
544   u64 sup_and_sub_key;
545   u64 *kp;
546   unix_shared_memory_queue_t *q;
547   clib_error_t *error;
548
549   VALIDATE_SW_IF_INDEX (mp);
550
551   hw_if_index = ntohl (mp->sw_if_index);
552   hi = vnet_get_hw_interface (vnm, hw_if_index);
553
554   id = ntohl (mp->vlan_id);
555   if (id == 0 || id > 4095)
556     {
557       rv = VNET_API_ERROR_INVALID_VLAN;
558       goto out;
559     }
560
561   sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
562
563   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
564   if (p)
565     {
566       rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
567       goto out;
568     }
569
570   kp = clib_mem_alloc (sizeof (*kp));
571   *kp = sup_and_sub_key;
572
573   memset (&template, 0, sizeof (template));
574   template.type = VNET_SW_INTERFACE_TYPE_SUB;
575   template.sup_sw_if_index = hi->sw_if_index;
576   template.sub.id = id;
577   template.sub.eth.raw_flags = 0;
578   template.sub.eth.flags.one_tag = 1;
579   template.sub.eth.outer_vlan_id = id;
580   template.sub.eth.flags.exact_match = 1;
581
582   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
583   if (error)
584     {
585       clib_error_report (error);
586       rv = VNET_API_ERROR_INVALID_REGISTRATION;
587       goto out;
588     }
589   hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
590   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
591
592   BAD_SW_IF_INDEX_LABEL;
593
594 out:
595   q = vl_api_client_index_to_input_queue (mp->client_index);
596   if (!q)
597     return;
598
599   rmp = vl_msg_api_alloc (sizeof (*rmp));
600   rmp->_vl_msg_id = ntohs (VL_API_CREATE_VLAN_SUBIF_REPLY);
601   rmp->context = mp->context;
602   rmp->retval = ntohl (rv);
603   rmp->sw_if_index = ntohl (sw_if_index);
604   vl_msg_api_send_shmem (q, (u8 *) & rmp);
605 }
606
607 static void
608 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
609 {
610   vl_api_create_subif_reply_t *rmp;
611   vnet_main_t *vnm = vnet_get_main ();
612   u32 sw_if_index = ~0;
613   int rv = 0;
614   u32 sub_id;
615   vnet_sw_interface_t *si;
616   vnet_hw_interface_t *hi;
617   vnet_sw_interface_t template;
618   uword *p;
619   vnet_interface_main_t *im = &vnm->interface_main;
620   u64 sup_and_sub_key;
621   u64 *kp;
622   clib_error_t *error;
623
624   VALIDATE_SW_IF_INDEX (mp);
625
626   si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
627   hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
628
629   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
630     {
631       rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
632       goto out;
633     }
634
635   sw_if_index = si->sw_if_index;
636   sub_id = ntohl (mp->sub_id);
637
638   sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
639
640   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
641   if (p)
642     {
643       if (CLIB_DEBUG > 0)
644         clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
645                       sw_if_index, sub_id);
646       rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
647       goto out;
648     }
649
650   kp = clib_mem_alloc (sizeof (*kp));
651   *kp = sup_and_sub_key;
652
653   memset (&template, 0, sizeof (template));
654   template.type = VNET_SW_INTERFACE_TYPE_SUB;
655   template.sup_sw_if_index = sw_if_index;
656   template.sub.id = sub_id;
657   template.sub.eth.flags.no_tags = mp->no_tags;
658   template.sub.eth.flags.one_tag = mp->one_tag;
659   template.sub.eth.flags.two_tags = mp->two_tags;
660   template.sub.eth.flags.dot1ad = mp->dot1ad;
661   template.sub.eth.flags.exact_match = mp->exact_match;
662   template.sub.eth.flags.default_sub = mp->default_sub;
663   template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
664   template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
665   template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
666   template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
667
668   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
669   if (error)
670     {
671       clib_error_report (error);
672       rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
673       goto out;
674     }
675
676   hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
677   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
678
679   BAD_SW_IF_INDEX_LABEL;
680
681 out:
682
683   /* *INDENT-OFF* */
684   REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
685   ({
686     rmp->sw_if_index = ntohl(sw_if_index);
687   }));
688   /* *INDENT-ON* */
689 }
690
691 static void
692 vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
693 {
694   vl_api_proxy_arp_add_del_reply_t *rmp;
695   u32 fib_index;
696   int rv;
697   ip4_main_t *im = &ip4_main;
698   stats_main_t *sm = &stats_main;
699   int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
700                               ip4_address_t * hi_addr,
701                               u32 fib_index, int is_del);
702   uword *p;
703
704   dslock (sm, 1 /* release hint */ , 6 /* tag */ );
705
706   p = hash_get (im->fib_index_by_table_id, ntohl (mp->vrf_id));
707
708   if (!p)
709     {
710       rv = VNET_API_ERROR_NO_SUCH_FIB;
711       goto out;
712     }
713
714   fib_index = p[0];
715
716   rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->low_address,
717                                (ip4_address_t *) mp->hi_address,
718                                fib_index, mp->is_add == 0);
719
720 out:
721   dsunlock (sm);
722   REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
723 }
724
725 static void
726   vl_api_proxy_arp_intfc_enable_disable_t_handler
727   (vl_api_proxy_arp_intfc_enable_disable_t * mp)
728 {
729   int rv = 0;
730   vnet_main_t *vnm = vnet_get_main ();
731   vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
732   vnet_sw_interface_t *si;
733   u32 sw_if_index;
734
735   VALIDATE_SW_IF_INDEX (mp);
736
737   sw_if_index = ntohl (mp->sw_if_index);
738
739   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
740     {
741       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
742       goto out;
743     }
744
745   si = vnet_get_sw_interface (vnm, sw_if_index);
746
747   ASSERT (si);
748
749   if (mp->enable_disable)
750     si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
751   else
752     si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
753
754   BAD_SW_IF_INDEX_LABEL;
755
756 out:
757   REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
758 }
759
760 static void
761 vl_api_is_address_reachable_t_handler (vl_api_is_address_reachable_t * mp)
762 {
763 #if 0
764   vpe_main_t *rm = &vpe_main;
765   ip4_main_t *im4 = &ip4_main;
766   ip6_main_t *im6 = &ip6_main;
767   ip_lookup_main_t *lm;
768   union
769   {
770     ip4_address_t ip4;
771     ip6_address_t ip6;
772   } addr;
773   u32 adj_index, sw_if_index;
774   vl_api_is_address_reachable_t *rmp;
775   ip_adjacency_t *adj;
776   unix_shared_memory_queue_t *q;
777
778   q = vl_api_client_index_to_input_queue (mp->client_index);
779   if (!q)
780     {
781       increment_missing_api_client_counter (rm->vlib_main);
782       return;
783     }
784
785   rmp = vl_msg_api_alloc (sizeof (*rmp));
786   clib_memcpy (rmp, mp, sizeof (*rmp));
787
788   sw_if_index = mp->next_hop_sw_if_index;
789   clib_memcpy (&addr, mp->address, sizeof (addr));
790   if (mp->is_ipv6)
791     {
792       lm = &im6->lookup_main;
793       adj_index = ip6_fib_lookup (im6, sw_if_index, &addr.ip6);
794     }
795   else
796     {
797       lm = &im4->lookup_main;
798       // FIXME NOT an ADJ
799       adj_index = ip4_fib_lookup (im4, sw_if_index, &addr.ip4);
800     }
801   if (adj_index == ~0)
802     {
803       rmp->is_error = 1;
804       goto send;
805     }
806   adj = ip_get_adjacency (lm, adj_index);
807
808   if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE
809       && adj->rewrite_header.sw_if_index == sw_if_index)
810     {
811       rmp->is_known = 1;
812     }
813   else
814     {
815       if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP
816           && adj->rewrite_header.sw_if_index == sw_if_index)
817         {
818           if (mp->is_ipv6)
819             ip6_probe_neighbor (rm->vlib_main, &addr.ip6, sw_if_index);
820           else
821             ip4_probe_neighbor (rm->vlib_main, &addr.ip4, sw_if_index);
822         }
823       else if (adj->lookup_next_index == IP_LOOKUP_NEXT_DROP)
824         {
825           rmp->is_known = 1;
826           goto send;
827         }
828       rmp->is_known = 0;
829     }
830
831 send:
832   vl_msg_api_send_shmem (q, (u8 *) & rmp);
833 #endif
834 }
835
836 static void
837   vl_api_sw_interface_set_mpls_enable_t_handler
838   (vl_api_sw_interface_set_mpls_enable_t * mp)
839 {
840   vl_api_sw_interface_set_mpls_enable_reply_t *rmp;
841   int rv = 0;
842
843   VALIDATE_SW_IF_INDEX (mp);
844
845   mpls_sw_interface_enable_disable (&mpls_main,
846                                     ntohl (mp->sw_if_index), mp->enable);
847
848   BAD_SW_IF_INDEX_LABEL;
849   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MPLS_ENABLE_REPLY);
850 }
851
852 void
853 send_oam_event (oam_target_t * t)
854 {
855   vpe_api_main_t *vam = &vpe_api_main;
856   unix_shared_memory_queue_t *q;
857   vpe_client_registration_t *reg;
858   vl_api_oam_event_t *mp;
859
860   /* *INDENT-OFF* */
861   pool_foreach(reg, vam->oam_events_registrations,
862   ({
863     q = vl_api_client_index_to_input_queue (reg->client_index);
864     if (q)
865       {
866         mp = vl_msg_api_alloc (sizeof (*mp));
867         mp->_vl_msg_id = ntohs (VL_API_OAM_EVENT);
868         clib_memcpy (mp->dst_address, &t->dst_address,
869                      sizeof (mp->dst_address));
870         mp->state = t->state;
871         vl_msg_api_send_shmem (q, (u8 *)&mp);
872       }
873   }));
874   /* *INDENT-ON* */
875 }
876
877 static void
878 vl_api_oam_add_del_t_handler (vl_api_oam_add_del_t * mp)
879 {
880   vl_api_oam_add_del_reply_t *rmp;
881   int rv;
882
883   rv = vpe_oam_add_del_target ((ip4_address_t *) mp->src_address,
884                                (ip4_address_t *) mp->dst_address,
885                                ntohl (mp->vrf_id), (int) (mp->is_add));
886
887   REPLY_MACRO (VL_API_OAM_ADD_DEL_REPLY);
888 }
889
890 static void
891 vl_api_vnet_get_summary_stats_t_handler (vl_api_vnet_get_summary_stats_t * mp)
892 {
893   stats_main_t *sm = &stats_main;
894   vnet_interface_main_t *im = sm->interface_main;
895   vl_api_vnet_summary_stats_reply_t *rmp;
896   vlib_combined_counter_main_t *cm;
897   vlib_counter_t v;
898   int i, which;
899   u64 total_pkts[VLIB_N_RX_TX];
900   u64 total_bytes[VLIB_N_RX_TX];
901
902   unix_shared_memory_queue_t *q =
903     vl_api_client_index_to_input_queue (mp->client_index);
904
905   if (!q)
906     return;
907
908   rmp = vl_msg_api_alloc (sizeof (*rmp));
909   rmp->_vl_msg_id = ntohs (VL_API_VNET_SUMMARY_STATS_REPLY);
910   rmp->context = mp->context;
911   rmp->retval = 0;
912
913   memset (total_pkts, 0, sizeof (total_pkts));
914   memset (total_bytes, 0, sizeof (total_bytes));
915
916   vnet_interface_counter_lock (im);
917
918   vec_foreach (cm, im->combined_sw_if_counters)
919   {
920     which = cm - im->combined_sw_if_counters;
921
922     for (i = 0; i < vec_len (cm->maxi); i++)
923       {
924         vlib_get_combined_counter (cm, i, &v);
925         total_pkts[which] += v.packets;
926         total_bytes[which] += v.bytes;
927       }
928   }
929   vnet_interface_counter_unlock (im);
930
931   rmp->total_pkts[VLIB_RX] = clib_host_to_net_u64 (total_pkts[VLIB_RX]);
932   rmp->total_bytes[VLIB_RX] = clib_host_to_net_u64 (total_bytes[VLIB_RX]);
933   rmp->total_pkts[VLIB_TX] = clib_host_to_net_u64 (total_pkts[VLIB_TX]);
934   rmp->total_bytes[VLIB_TX] = clib_host_to_net_u64 (total_bytes[VLIB_TX]);
935   rmp->vector_rate =
936     clib_host_to_net_u64 (vlib_last_vector_length_per_node (sm->vlib_main));
937
938   vl_msg_api_send_shmem (q, (u8 *) & rmp);
939 }
940
941 /* *INDENT-OFF* */
942 typedef CLIB_PACKED (struct {
943   ip4_address_t address;
944   u32 address_length: 6;
945   u32 index:26;
946 }) ip4_route_t;
947 /* *INDENT-ON* */
948
949 static int
950 ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
951 {
952   vnet_main_t *vnm = vnet_get_main ();
953   vnet_interface_main_t *im = &vnm->interface_main;
954   ip4_main_t *im4 = &ip4_main;
955   static u32 *sw_if_indices_to_shut;
956   stats_main_t *sm = &stats_main;
957   fib_table_t *fib_table;
958   ip4_fib_t *fib;
959   u32 sw_if_index;
960   int i;
961   int rv = VNET_API_ERROR_NO_SUCH_FIB;
962   u32 target_fib_id = ntohl (mp->vrf_id);
963
964   dslock (sm, 1 /* release hint */ , 8 /* tag */ );
965
966   /* *INDENT-OFF* */
967   pool_foreach (fib_table, im4->fibs,
968   ({
969     fib = &fib_table->v4;
970     vnet_sw_interface_t * si;
971
972     if (fib->table_id != target_fib_id)
973       continue;
974
975     /* remove any mpls encap/decap labels */
976     mpls_fib_reset_labels (fib->table_id);
977
978     /* remove any proxy arps in this fib */
979     vnet_proxy_arp_fib_reset (fib->table_id);
980
981     /* Set the flow hash for this fib to the default */
982     vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
983
984     vec_reset_length (sw_if_indices_to_shut);
985
986     /* Shut down interfaces in this FIB / clean out intfc routes */
987     pool_foreach (si, im->sw_interfaces,
988     ({
989       u32 sw_if_index = si->sw_if_index;
990
991       if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
992           && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
993               fib->index))
994         vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
995     }));
996
997     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
998       sw_if_index = sw_if_indices_to_shut[i];
999       // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
1000
1001       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
1002       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
1003       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
1004     }
1005
1006     fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
1007     fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_INTERFACE);
1008
1009     rv = 0;
1010     break;
1011     })); /* pool_foreach (fib) */
1012     /* *INDENT-ON* */
1013
1014   dsunlock (sm);
1015   return rv;
1016 }
1017
1018 static int
1019 ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
1020 {
1021   vnet_main_t *vnm = vnet_get_main ();
1022   vnet_interface_main_t *im = &vnm->interface_main;
1023   ip6_main_t *im6 = &ip6_main;
1024   stats_main_t *sm = &stats_main;
1025   static u32 *sw_if_indices_to_shut;
1026   fib_table_t *fib_table;
1027   ip6_fib_t *fib;
1028   u32 sw_if_index;
1029   int i;
1030   int rv = VNET_API_ERROR_NO_SUCH_FIB;
1031   u32 target_fib_id = ntohl (mp->vrf_id);
1032
1033   dslock (sm, 1 /* release hint */ , 9 /* tag */ );
1034
1035   /* *INDENT-OFF* */
1036   pool_foreach (fib_table, im6->fibs,
1037   ({
1038     vnet_sw_interface_t * si;
1039     fib = &(fib_table->v6);
1040
1041     if (fib->table_id != target_fib_id)
1042       continue;
1043
1044     vec_reset_length (sw_if_indices_to_shut);
1045
1046     /* Shut down interfaces in this FIB / clean out intfc routes */
1047     pool_foreach (si, im->sw_interfaces,
1048                   ({
1049                     if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
1050                         fib->index)
1051                       vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
1052                   }));
1053
1054     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
1055       sw_if_index = sw_if_indices_to_shut[i];
1056       // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
1057
1058       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
1059       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
1060       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
1061     }
1062
1063     fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
1064     fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_INTERFACE);
1065
1066     rv = 0;
1067     break;
1068   })); /* pool_foreach (fib) */
1069   /* *INDENT-ON* */
1070
1071   dsunlock (sm);
1072   return rv;
1073 }
1074
1075 static void
1076 vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
1077 {
1078   int rv;
1079   vl_api_reset_fib_reply_t *rmp;
1080
1081   if (mp->is_ipv6)
1082     rv = ip6_reset_fib_t_handler (mp);
1083   else
1084     rv = ip4_reset_fib_t_handler (mp);
1085
1086   REPLY_MACRO (VL_API_RESET_FIB_REPLY);
1087 }
1088
1089
1090 static void
1091 dhcpv4_proxy_config (vl_api_dhcp_proxy_config_t * mp)
1092 {
1093   vl_api_dhcp_proxy_config_reply_t *rmp;
1094   int rv;
1095
1096   rv = dhcp_proxy_set_server ((ip4_address_t *) (&mp->dhcp_server),
1097                               (ip4_address_t *) (&mp->dhcp_src_address),
1098                               (u32) ntohl (mp->vrf_id),
1099                               (int) mp->insert_circuit_id,
1100                               (int) (mp->is_add == 0));
1101
1102   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
1103 }
1104
1105
1106 static void
1107 dhcpv6_proxy_config (vl_api_dhcp_proxy_config_t * mp)
1108 {
1109   vl_api_dhcp_proxy_config_reply_t *rmp;
1110   int rv = -1;
1111
1112   rv = dhcpv6_proxy_set_server ((ip6_address_t *) (&mp->dhcp_server),
1113                                 (ip6_address_t *) (&mp->dhcp_src_address),
1114                                 (u32) ntohl (mp->vrf_id),
1115                                 (int) mp->insert_circuit_id,
1116                                 (int) (mp->is_add == 0));
1117
1118   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
1119 }
1120
1121 static void
1122 dhcpv4_proxy_config_2 (vl_api_dhcp_proxy_config_2_t * mp)
1123 {
1124   vl_api_dhcp_proxy_config_reply_t *rmp;
1125   int rv;
1126
1127   rv = dhcp_proxy_set_server_2 ((ip4_address_t *) (&mp->dhcp_server),
1128                                 (ip4_address_t *) (&mp->dhcp_src_address),
1129                                 (u32) ntohl (mp->rx_vrf_id),
1130                                 (u32) ntohl (mp->server_vrf_id),
1131                                 (int) mp->insert_circuit_id,
1132                                 (int) (mp->is_add == 0));
1133
1134   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_2_REPLY);
1135 }
1136
1137
1138 static void
1139 dhcpv6_proxy_config_2 (vl_api_dhcp_proxy_config_2_t * mp)
1140 {
1141   vl_api_dhcp_proxy_config_reply_t *rmp;
1142   int rv = -1;
1143
1144   rv = dhcpv6_proxy_set_server_2 ((ip6_address_t *) (&mp->dhcp_server),
1145                                   (ip6_address_t *) (&mp->dhcp_src_address),
1146                                   (u32) ntohl (mp->rx_vrf_id),
1147                                   (u32) ntohl (mp->server_vrf_id),
1148                                   (int) mp->insert_circuit_id,
1149                                   (int) (mp->is_add == 0));
1150
1151   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_2_REPLY);
1152 }
1153
1154
1155 static void
1156 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t * mp)
1157 {
1158   vl_api_dhcp_proxy_set_vss_reply_t *rmp;
1159   int rv;
1160   if (!mp->is_ipv6)
1161     rv = dhcp_proxy_set_option82_vss (ntohl (mp->tbl_id),
1162                                       ntohl (mp->oui),
1163                                       ntohl (mp->fib_id),
1164                                       (int) mp->is_add == 0);
1165   else
1166     rv = dhcpv6_proxy_set_vss (ntohl (mp->tbl_id),
1167                                ntohl (mp->oui),
1168                                ntohl (mp->fib_id), (int) mp->is_add == 0);
1169
1170   REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
1171 }
1172
1173
1174 static void vl_api_dhcp_proxy_config_t_handler
1175   (vl_api_dhcp_proxy_config_t * mp)
1176 {
1177   if (mp->is_ipv6 == 0)
1178     dhcpv4_proxy_config (mp);
1179   else
1180     dhcpv6_proxy_config (mp);
1181 }
1182
1183 static void vl_api_dhcp_proxy_config_2_t_handler
1184   (vl_api_dhcp_proxy_config_2_t * mp)
1185 {
1186   if (mp->is_ipv6 == 0)
1187     dhcpv4_proxy_config_2 (mp);
1188   else
1189     dhcpv6_proxy_config_2 (mp);
1190 }
1191
1192 void
1193 dhcp_compl_event_callback (u32 client_index, u32 pid, u8 * hostname,
1194                            u8 is_ipv6, u8 * host_address, u8 * router_address,
1195                            u8 * host_mac)
1196 {
1197   unix_shared_memory_queue_t *q;
1198   vl_api_dhcp_compl_event_t *mp;
1199
1200   q = vl_api_client_index_to_input_queue (client_index);
1201   if (!q)
1202     return;
1203
1204   mp = vl_msg_api_alloc (sizeof (*mp));
1205   mp->client_index = client_index;
1206   mp->pid = pid;
1207   mp->is_ipv6 = is_ipv6;
1208   clib_memcpy (&mp->hostname, hostname, vec_len (hostname));
1209   mp->hostname[vec_len (hostname) + 1] = '\n';
1210   clib_memcpy (&mp->host_address[0], host_address, 16);
1211   clib_memcpy (&mp->router_address[0], router_address, 16);
1212
1213   if (NULL != host_mac)
1214     clib_memcpy (&mp->host_mac[0], host_mac, 6);
1215
1216   mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
1217
1218   vl_msg_api_send_shmem (q, (u8 *) & mp);
1219 }
1220
1221 static void vl_api_dhcp_client_config_t_handler
1222   (vl_api_dhcp_client_config_t * mp)
1223 {
1224   vlib_main_t *vm = vlib_get_main ();
1225   vl_api_dhcp_client_config_reply_t *rmp;
1226   int rv = 0;
1227
1228   VALIDATE_SW_IF_INDEX (mp);
1229
1230   rv = dhcp_client_config (vm, ntohl (mp->sw_if_index),
1231                            mp->hostname, mp->is_add, mp->client_index,
1232                            mp->want_dhcp_event ? dhcp_compl_event_callback :
1233                            NULL, mp->pid);
1234
1235   BAD_SW_IF_INDEX_LABEL;
1236
1237   REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
1238 }
1239
1240 static void
1241 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
1242 {
1243   vl_api_create_loopback_reply_t *rmp;
1244   u32 sw_if_index;
1245   int rv;
1246
1247   rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address);
1248
1249   /* *INDENT-OFF* */
1250   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
1251   ({
1252     rmp->sw_if_index = ntohl (sw_if_index);
1253   }));
1254   /* *INDENT-ON* */
1255 }
1256
1257 static void
1258 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
1259 {
1260   vl_api_delete_loopback_reply_t *rmp;
1261   u32 sw_if_index;
1262   int rv;
1263
1264   sw_if_index = ntohl (mp->sw_if_index);
1265   rv = vnet_delete_loopback_interface (sw_if_index);
1266
1267   REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
1268 }
1269
1270 static void
1271 vl_api_control_ping_t_handler (vl_api_control_ping_t * mp)
1272 {
1273   vl_api_control_ping_reply_t *rmp;
1274   int rv = 0;
1275
1276   /* *INDENT-OFF* */
1277   REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
1278   ({
1279     rmp->vpe_pid = ntohl (getpid());
1280   }));
1281   /* *INDENT-ON* */
1282 }
1283
1284 static void
1285 shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
1286 {
1287   u8 **shmem_vecp = (u8 **) arg;
1288   u8 *shmem_vec;
1289   void *oldheap;
1290   api_main_t *am = &api_main;
1291   u32 offset;
1292
1293   shmem_vec = *shmem_vecp;
1294
1295   offset = vec_len (shmem_vec);
1296
1297   pthread_mutex_lock (&am->vlib_rp->mutex);
1298   oldheap = svm_push_data_heap (am->vlib_rp);
1299
1300   vec_validate (shmem_vec, offset + buffer_bytes - 1);
1301
1302   clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
1303
1304   svm_pop_heap (oldheap);
1305   pthread_mutex_unlock (&am->vlib_rp->mutex);
1306
1307   *shmem_vecp = shmem_vec;
1308 }
1309
1310
1311 static void
1312 vl_api_cli_request_t_handler (vl_api_cli_request_t * mp)
1313 {
1314   vl_api_cli_reply_t *rp;
1315   unix_shared_memory_queue_t *q;
1316   vlib_main_t *vm = vlib_get_main ();
1317   api_main_t *am = &api_main;
1318   unformat_input_t input;
1319   u8 *shmem_vec = 0;
1320   void *oldheap;
1321
1322   q = vl_api_client_index_to_input_queue (mp->client_index);
1323   if (!q)
1324     return;
1325
1326   rp = vl_msg_api_alloc (sizeof (*rp));
1327   rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY);
1328   rp->context = mp->context;
1329
1330   unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
1331
1332   vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
1333
1334   pthread_mutex_lock (&am->vlib_rp->mutex);
1335   oldheap = svm_push_data_heap (am->vlib_rp);
1336
1337   vec_add1 (shmem_vec, 0);
1338
1339   svm_pop_heap (oldheap);
1340   pthread_mutex_unlock (&am->vlib_rp->mutex);
1341
1342   rp->reply_in_shmem = (uword) shmem_vec;
1343
1344   vl_msg_api_send_shmem (q, (u8 *) & rp);
1345 }
1346
1347 static void
1348 inband_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
1349 {
1350   u8 **mem_vecp = (u8 **) arg;
1351   u8 *mem_vec = *mem_vecp;
1352   u32 offset = vec_len (mem_vec);
1353
1354   vec_validate (mem_vec, offset + buffer_bytes - 1);
1355   clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
1356   *mem_vecp = mem_vec;
1357 }
1358
1359 static void
1360 vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
1361 {
1362   vl_api_cli_inband_reply_t *rmp;
1363   int rv = 0;
1364   unix_shared_memory_queue_t *q;
1365   vlib_main_t *vm = vlib_get_main ();
1366   unformat_input_t input;
1367   u8 *out_vec = 0;
1368
1369   q = vl_api_client_index_to_input_queue (mp->client_index);
1370   if (!q)
1371     return;
1372
1373   unformat_init_string (&input, (char *) mp->cmd, ntohl (mp->length));
1374   vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
1375
1376   u32 len = vec_len (out_vec);
1377   /* *INDENT-OFF* */
1378   REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, len,
1379   ({
1380     rmp->length = htonl (len);
1381     clib_memcpy (rmp->reply, out_vec, len);
1382   }));
1383   /* *INDENT-ON* */
1384   vec_free (out_vec);
1385 }
1386
1387 static void
1388 vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
1389 {
1390   int rv;
1391   vl_api_set_arp_neighbor_limit_reply_t *rmp;
1392   vnet_main_t *vnm = vnet_get_main ();
1393   clib_error_t *error;
1394
1395   vnm->api_errno = 0;
1396
1397   if (mp->is_ipv6)
1398     error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
1399   else
1400     error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
1401
1402   if (error)
1403     {
1404       clib_error_report (error);
1405       rv = VNET_API_ERROR_UNSPECIFIED;
1406     }
1407   else
1408     {
1409       rv = vnm->api_errno;
1410     }
1411
1412   REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
1413 }
1414
1415 #define foreach_classify_add_del_table_field    \
1416 _(table_index)                                  \
1417 _(nbuckets)                                     \
1418 _(memory_size)                                  \
1419 _(skip_n_vectors)                               \
1420 _(match_n_vectors)                              \
1421 _(next_table_index)                             \
1422 _(miss_next_index)                              \
1423 _(current_data_flag)                            \
1424 _(current_data_offset)
1425
1426 static void vl_api_classify_add_del_table_t_handler
1427   (vl_api_classify_add_del_table_t * mp)
1428 {
1429   vl_api_classify_add_del_table_reply_t *rmp;
1430   vnet_classify_main_t *cm = &vnet_classify_main;
1431   vnet_classify_table_t *t;
1432   int rv;
1433
1434 #define _(a) u32 a;
1435   foreach_classify_add_del_table_field;
1436 #undef _
1437
1438 #define _(a) a = ntohl(mp->a);
1439   foreach_classify_add_del_table_field;
1440 #undef _
1441
1442   /* The underlying API fails silently, on purpose, so check here */
1443   if (mp->is_add == 0)          /* delete */
1444     {
1445       if (pool_is_free_index (cm->tables, table_index))
1446         {
1447           rv = VNET_API_ERROR_NO_SUCH_TABLE;
1448           goto out;
1449         }
1450     }
1451   else                          /* add or update */
1452     {
1453       if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
1454         table_index = ~0;
1455     }
1456
1457   rv = vnet_classify_add_del_table
1458     (cm, mp->mask, nbuckets, memory_size,
1459      skip_n_vectors, match_n_vectors,
1460      next_table_index, miss_next_index, &table_index,
1461      current_data_flag, current_data_offset, mp->is_add, mp->del_chain);
1462
1463 out:
1464   /* *INDENT-OFF* */
1465   REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
1466   ({
1467     if (rv == 0 && mp->is_add)
1468       {
1469         t = pool_elt_at_index (cm->tables, table_index);
1470         rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
1471         rmp->match_n_vectors = ntohl(t->match_n_vectors);
1472         rmp->new_table_index = ntohl(table_index);
1473       }
1474     else
1475       {
1476         rmp->skip_n_vectors = ~0;
1477         rmp->match_n_vectors = ~0;
1478         rmp->new_table_index = ~0;
1479       }
1480   }));
1481   /* *INDENT-ON* */
1482 }
1483
1484 static void vl_api_classify_add_del_session_t_handler
1485   (vl_api_classify_add_del_session_t * mp)
1486 {
1487   vnet_classify_main_t *cm = &vnet_classify_main;
1488   vl_api_classify_add_del_session_reply_t *rmp;
1489   int rv;
1490   u32 table_index, hit_next_index, opaque_index, metadata;
1491   i32 advance;
1492   u8 action;
1493
1494   table_index = ntohl (mp->table_index);
1495   hit_next_index = ntohl (mp->hit_next_index);
1496   opaque_index = ntohl (mp->opaque_index);
1497   advance = ntohl (mp->advance);
1498   action = mp->action;
1499   metadata = ntohl (mp->metadata);
1500
1501   rv = vnet_classify_add_del_session
1502     (cm, table_index, mp->match, hit_next_index, opaque_index,
1503      advance, action, metadata, mp->is_add);
1504
1505   REPLY_MACRO (VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
1506 }
1507
1508 static void vl_api_classify_set_interface_ip_table_t_handler
1509   (vl_api_classify_set_interface_ip_table_t * mp)
1510 {
1511   vlib_main_t *vm = vlib_get_main ();
1512   vl_api_classify_set_interface_ip_table_reply_t *rmp;
1513   int rv;
1514   u32 table_index, sw_if_index;
1515
1516   table_index = ntohl (mp->table_index);
1517   sw_if_index = ntohl (mp->sw_if_index);
1518
1519   VALIDATE_SW_IF_INDEX (mp);
1520
1521   if (mp->is_ipv6)
1522     rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
1523   else
1524     rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
1525
1526   BAD_SW_IF_INDEX_LABEL;
1527
1528   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
1529 }
1530
1531 static void vl_api_classify_set_interface_l2_tables_t_handler
1532   (vl_api_classify_set_interface_l2_tables_t * mp)
1533 {
1534   vl_api_classify_set_interface_l2_tables_reply_t *rmp;
1535   int rv;
1536   u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
1537   int enable;
1538
1539   ip4_table_index = ntohl (mp->ip4_table_index);
1540   ip6_table_index = ntohl (mp->ip6_table_index);
1541   other_table_index = ntohl (mp->other_table_index);
1542   sw_if_index = ntohl (mp->sw_if_index);
1543
1544   VALIDATE_SW_IF_INDEX (mp);
1545
1546   if (mp->is_input)
1547     rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
1548                                             ip6_table_index,
1549                                             other_table_index);
1550   else
1551     rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
1552                                              ip6_table_index,
1553                                              other_table_index);
1554
1555   if (rv == 0)
1556     {
1557       if (ip4_table_index != ~0 || ip6_table_index != ~0
1558           || other_table_index != ~0)
1559         enable = 1;
1560       else
1561         enable = 0;
1562
1563       if (mp->is_input)
1564         vnet_l2_input_classify_enable_disable (sw_if_index, enable);
1565       else
1566         vnet_l2_output_classify_enable_disable (sw_if_index, enable);
1567     }
1568
1569   BAD_SW_IF_INDEX_LABEL;
1570
1571   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
1572 }
1573
1574 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
1575                                      u32 sw_if_index, u32 enable);
1576
1577 static void
1578 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
1579                                           mp)
1580 {
1581   int rv;
1582   vl_api_l2_interface_efp_filter_reply_t *rmp;
1583   vnet_main_t *vnm = vnet_get_main ();
1584
1585   // enable/disable the feature
1586   l2_efp_filter_configure (vnm, mp->sw_if_index, mp->enable_disable);
1587   rv = vnm->api_errno;
1588
1589   REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
1590 }
1591
1592 static void
1593   vl_api_l2_interface_vlan_tag_rewrite_t_handler
1594   (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
1595 {
1596   int rv = 0;
1597   vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
1598   vnet_main_t *vnm = vnet_get_main ();
1599   vlib_main_t *vm = vlib_get_main ();
1600   u32 vtr_op;
1601
1602   VALIDATE_SW_IF_INDEX (mp);
1603
1604   vtr_op = ntohl (mp->vtr_op);
1605
1606   /* The L2 code is unsuspicious */
1607   switch (vtr_op)
1608     {
1609     case L2_VTR_DISABLED:
1610     case L2_VTR_PUSH_1:
1611     case L2_VTR_PUSH_2:
1612     case L2_VTR_POP_1:
1613     case L2_VTR_POP_2:
1614     case L2_VTR_TRANSLATE_1_1:
1615     case L2_VTR_TRANSLATE_1_2:
1616     case L2_VTR_TRANSLATE_2_1:
1617     case L2_VTR_TRANSLATE_2_2:
1618       break;
1619
1620     default:
1621       rv = VNET_API_ERROR_INVALID_VALUE;
1622       goto bad_sw_if_index;
1623     }
1624
1625   rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
1626                         ntohl (mp->push_dot1q), ntohl (mp->tag1),
1627                         ntohl (mp->tag2));
1628
1629   BAD_SW_IF_INDEX_LABEL;
1630
1631   REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
1632 }
1633
1634 static void
1635 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
1636 {
1637   vl_api_show_version_reply_t *rmp;
1638   int rv = 0;
1639   char *vpe_api_get_build_directory (void);
1640   char *vpe_api_get_version (void);
1641   char *vpe_api_get_build_date (void);
1642
1643   unix_shared_memory_queue_t *q =
1644     vl_api_client_index_to_input_queue (mp->client_index);
1645
1646   if (!q)
1647     return;
1648
1649   /* *INDENT-OFF* */
1650   REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
1651   ({
1652     strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
1653     strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
1654              ARRAY_LEN(rmp->build_directory)-1);
1655     strncpy ((char *) rmp->version, vpe_api_get_version(),
1656              ARRAY_LEN(rmp->version)-1);
1657     strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
1658              ARRAY_LEN(rmp->build_date)-1);
1659   }));
1660   /* *INDENT-ON* */
1661 }
1662
1663 static void
1664 vl_api_get_node_index_t_handler (vl_api_get_node_index_t * mp)
1665 {
1666   vlib_main_t *vm = vlib_get_main ();
1667   vl_api_get_node_index_reply_t *rmp;
1668   vlib_node_t *n;
1669   int rv = 0;
1670   u32 node_index = ~0;
1671
1672   n = vlib_get_node_by_name (vm, mp->node_name);
1673
1674   if (n == 0)
1675     rv = VNET_API_ERROR_NO_SUCH_NODE;
1676   else
1677     node_index = n->index;
1678
1679   /* *INDENT-OFF* */
1680   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
1681   ({
1682     rmp->node_index = ntohl(node_index);
1683   }));
1684   /* *INDENT-ON* */
1685 }
1686
1687 static void
1688 vl_api_get_next_index_t_handler (vl_api_get_next_index_t * mp)
1689 {
1690   vlib_main_t *vm = vlib_get_main ();
1691   vl_api_get_next_index_reply_t *rmp;
1692   vlib_node_t *node, *next_node;
1693   int rv = 0;
1694   u32 next_node_index = ~0, next_index = ~0;
1695   uword *p;
1696
1697   node = vlib_get_node_by_name (vm, mp->node_name);
1698
1699   if (node == 0)
1700     {
1701       rv = VNET_API_ERROR_NO_SUCH_NODE;
1702       goto out;
1703     }
1704
1705   next_node = vlib_get_node_by_name (vm, mp->next_name);
1706
1707   if (next_node == 0)
1708     {
1709       rv = VNET_API_ERROR_NO_SUCH_NODE2;
1710       goto out;
1711     }
1712   else
1713     next_node_index = next_node->index;
1714
1715   p = hash_get (node->next_slot_by_node, next_node_index);
1716
1717   if (p == 0)
1718     {
1719       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1720       goto out;
1721     }
1722   else
1723     next_index = p[0];
1724
1725 out:
1726   /* *INDENT-OFF* */
1727   REPLY_MACRO2(VL_API_GET_NEXT_INDEX_REPLY,
1728   ({
1729     rmp->next_index = ntohl(next_index);
1730   }));
1731   /* *INDENT-ON* */
1732 }
1733
1734 static void
1735 vl_api_add_node_next_t_handler (vl_api_add_node_next_t * mp)
1736 {
1737   vlib_main_t *vm = vlib_get_main ();
1738   vl_api_add_node_next_reply_t *rmp;
1739   vlib_node_t *n, *next;
1740   int rv = 0;
1741   u32 next_index = ~0;
1742
1743   n = vlib_get_node_by_name (vm, mp->node_name);
1744
1745   if (n == 0)
1746     {
1747       rv = VNET_API_ERROR_NO_SUCH_NODE;
1748       goto out;
1749     }
1750
1751   next = vlib_get_node_by_name (vm, mp->next_name);
1752
1753   if (next == 0)
1754     rv = VNET_API_ERROR_NO_SUCH_NODE2;
1755   else
1756     next_index = vlib_node_add_next (vm, n->index, next->index);
1757
1758 out:
1759   /* *INDENT-OFF* */
1760   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
1761   ({
1762     rmp->next_index = ntohl(next_index);
1763   }));
1764   /* *INDENT-ON* */
1765 }
1766
1767 static void vl_api_vxlan_add_del_tunnel_t_handler
1768   (vl_api_vxlan_add_del_tunnel_t * mp)
1769 {
1770   vl_api_vxlan_add_del_tunnel_reply_t *rmp;
1771   int rv = 0;
1772   vnet_vxlan_add_del_tunnel_args_t _a, *a = &_a;
1773   u32 encap_fib_index;
1774   uword *p;
1775   ip4_main_t *im = &ip4_main;
1776   vnet_main_t *vnm = vnet_get_main ();
1777   u32 sw_if_index = ~0;
1778
1779   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
1780   if (!p)
1781     {
1782       rv = VNET_API_ERROR_NO_SUCH_FIB;
1783       goto out;
1784     }
1785   encap_fib_index = p[0];
1786   memset (a, 0, sizeof (*a));
1787
1788   a->is_add = mp->is_add;
1789   a->is_ip6 = mp->is_ipv6;
1790
1791   /* ip addresses sent in network byte order */
1792   ip46_from_addr_buf (mp->is_ipv6, mp->dst_address, &a->dst);
1793   ip46_from_addr_buf (mp->is_ipv6, mp->src_address, &a->src);
1794
1795   /* Check src & dst are different */
1796   if (ip46_address_cmp (&a->dst, &a->src) == 0)
1797     {
1798       rv = VNET_API_ERROR_SAME_SRC_DST;
1799       goto out;
1800     }
1801   a->mcast_sw_if_index = ntohl (mp->mcast_sw_if_index);
1802   if (ip46_address_is_multicast (&a->dst) &&
1803       pool_is_free_index (vnm->interface_main.sw_interfaces,
1804                           a->mcast_sw_if_index))
1805     {
1806       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1807       goto out;
1808     }
1809   a->encap_fib_index = encap_fib_index;
1810   a->decap_next_index = ntohl (mp->decap_next_index);
1811   a->vni = ntohl (mp->vni);
1812   rv = vnet_vxlan_add_del_tunnel (a, &sw_if_index);
1813
1814 out:
1815   /* *INDENT-OFF* */
1816   REPLY_MACRO2(VL_API_VXLAN_ADD_DEL_TUNNEL_REPLY,
1817   ({
1818     rmp->sw_if_index = ntohl (sw_if_index);
1819   }));
1820   /* *INDENT-ON* */
1821 }
1822
1823 static void send_vxlan_tunnel_details
1824   (vxlan_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
1825 {
1826   vl_api_vxlan_tunnel_details_t *rmp;
1827   ip4_main_t *im4 = &ip4_main;
1828   ip6_main_t *im6 = &ip6_main;
1829   u8 is_ipv6 = !ip46_address_is_ip4 (&t->dst);
1830
1831   rmp = vl_msg_api_alloc (sizeof (*rmp));
1832   memset (rmp, 0, sizeof (*rmp));
1833   rmp->_vl_msg_id = ntohs (VL_API_VXLAN_TUNNEL_DETAILS);
1834   if (is_ipv6)
1835     {
1836       memcpy (rmp->src_address, t->src.ip6.as_u8, 16);
1837       memcpy (rmp->dst_address, t->dst.ip6.as_u8, 16);
1838       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
1839     }
1840   else
1841     {
1842       memcpy (rmp->src_address, t->src.ip4.as_u8, 4);
1843       memcpy (rmp->dst_address, t->dst.ip4.as_u8, 4);
1844       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
1845     }
1846   rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
1847   rmp->vni = htonl (t->vni);
1848   rmp->decap_next_index = htonl (t->decap_next_index);
1849   rmp->sw_if_index = htonl (t->sw_if_index);
1850   rmp->is_ipv6 = is_ipv6;
1851   rmp->context = context;
1852
1853   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1854 }
1855
1856 static void vl_api_vxlan_tunnel_dump_t_handler
1857   (vl_api_vxlan_tunnel_dump_t * mp)
1858 {
1859   unix_shared_memory_queue_t *q;
1860   vxlan_main_t *vxm = &vxlan_main;
1861   vxlan_tunnel_t *t;
1862   u32 sw_if_index;
1863
1864   q = vl_api_client_index_to_input_queue (mp->client_index);
1865   if (q == 0)
1866     {
1867       return;
1868     }
1869
1870   sw_if_index = ntohl (mp->sw_if_index);
1871
1872   if (~0 == sw_if_index)
1873     {
1874       /* *INDENT-OFF* */
1875       pool_foreach (t, vxm->tunnels,
1876       ({
1877         send_vxlan_tunnel_details(t, q, mp->context);
1878       }));
1879       /* *INDENT-ON* */
1880     }
1881   else
1882     {
1883       if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) ||
1884           (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index]))
1885         {
1886           return;
1887         }
1888       t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
1889       send_vxlan_tunnel_details (t, q, mp->context);
1890     }
1891 }
1892
1893 static void
1894 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
1895 {
1896   extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1897                                     int is_add);
1898   vl_api_l2_patch_add_del_reply_t *rmp;
1899   int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1900                              int is_add);
1901   int rv = 0;
1902
1903   VALIDATE_RX_SW_IF_INDEX (mp);
1904   VALIDATE_TX_SW_IF_INDEX (mp);
1905
1906   rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
1907                               ntohl (mp->tx_sw_if_index),
1908                               (int) (mp->is_add != 0));
1909
1910   BAD_RX_SW_IF_INDEX_LABEL;
1911   BAD_TX_SW_IF_INDEX_LABEL;
1912
1913   REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
1914 }
1915
1916 static void
1917   vl_api_vxlan_gpe_add_del_tunnel_t_handler
1918   (vl_api_vxlan_gpe_add_del_tunnel_t * mp)
1919 {
1920   vl_api_vxlan_gpe_add_del_tunnel_reply_t *rmp;
1921   int rv = 0;
1922   vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
1923   u32 encap_fib_index, decap_fib_index;
1924   u8 protocol;
1925   uword *p;
1926   ip4_main_t *im = &ip4_main;
1927   u32 sw_if_index = ~0;
1928
1929
1930   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
1931   if (!p)
1932     {
1933       rv = VNET_API_ERROR_NO_SUCH_FIB;
1934       goto out;
1935     }
1936   encap_fib_index = p[0];
1937
1938   protocol = mp->protocol;
1939
1940   /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
1941   if (protocol == VXLAN_GPE_INPUT_NEXT_IP4_INPUT)
1942     {
1943       p = hash_get (im->fib_index_by_table_id, ntohl (mp->decap_vrf_id));
1944       if (!p)
1945         {
1946           rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
1947           goto out;
1948         }
1949       decap_fib_index = p[0];
1950     }
1951   else
1952     {
1953       decap_fib_index = ntohl (mp->decap_vrf_id);
1954     }
1955
1956   /* Check src & dst are different */
1957   if ((mp->is_ipv6 && memcmp (mp->local, mp->remote, 16) == 0) ||
1958       (!mp->is_ipv6 && memcmp (mp->local, mp->remote, 4) == 0))
1959     {
1960       rv = VNET_API_ERROR_SAME_SRC_DST;
1961       goto out;
1962     }
1963   memset (a, 0, sizeof (*a));
1964
1965   a->is_add = mp->is_add;
1966   a->is_ip6 = mp->is_ipv6;
1967   /* ip addresses sent in network byte order */
1968   if (a->is_ip6)
1969     {
1970       clib_memcpy (&(a->local.ip6), mp->local, 16);
1971       clib_memcpy (&(a->remote.ip6), mp->remote, 16);
1972     }
1973   else
1974     {
1975       clib_memcpy (&(a->local.ip4), mp->local, 4);
1976       clib_memcpy (&(a->remote.ip4), mp->remote, 4);
1977     }
1978   a->encap_fib_index = encap_fib_index;
1979   a->decap_fib_index = decap_fib_index;
1980   a->protocol = protocol;
1981   a->vni = ntohl (mp->vni);
1982   rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
1983
1984 out:
1985   /* *INDENT-OFF* */
1986   REPLY_MACRO2(VL_API_VXLAN_GPE_ADD_DEL_TUNNEL_REPLY,
1987   ({
1988     rmp->sw_if_index = ntohl (sw_if_index);
1989   }));
1990   /* *INDENT-ON* */
1991 }
1992
1993 static void send_vxlan_gpe_tunnel_details
1994   (vxlan_gpe_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
1995 {
1996   vl_api_vxlan_gpe_tunnel_details_t *rmp;
1997   ip4_main_t *im4 = &ip4_main;
1998   ip6_main_t *im6 = &ip6_main;
1999   u8 is_ipv6 = !(t->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
2000
2001   rmp = vl_msg_api_alloc (sizeof (*rmp));
2002   memset (rmp, 0, sizeof (*rmp));
2003   rmp->_vl_msg_id = ntohs (VL_API_VXLAN_GPE_TUNNEL_DETAILS);
2004   if (is_ipv6)
2005     {
2006       memcpy (rmp->local, &(t->local.ip6), 16);
2007       memcpy (rmp->remote, &(t->remote.ip6), 16);
2008       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
2009       rmp->decap_vrf_id = htonl (im6->fibs[t->decap_fib_index].ft_table_id);
2010     }
2011   else
2012     {
2013       memcpy (rmp->local, &(t->local.ip4), 4);
2014       memcpy (rmp->remote, &(t->remote.ip4), 4);
2015       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
2016       rmp->decap_vrf_id = htonl (im4->fibs[t->decap_fib_index].ft_table_id);
2017     }
2018   rmp->vni = htonl (t->vni);
2019   rmp->protocol = t->protocol;
2020   rmp->sw_if_index = htonl (t->sw_if_index);
2021   rmp->is_ipv6 = is_ipv6;
2022   rmp->context = context;
2023
2024   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2025 }
2026
2027 static void vl_api_vxlan_gpe_tunnel_dump_t_handler
2028   (vl_api_vxlan_gpe_tunnel_dump_t * mp)
2029 {
2030   unix_shared_memory_queue_t *q;
2031   vxlan_gpe_main_t *vgm = &vxlan_gpe_main;
2032   vxlan_gpe_tunnel_t *t;
2033   u32 sw_if_index;
2034
2035   q = vl_api_client_index_to_input_queue (mp->client_index);
2036   if (q == 0)
2037     {
2038       return;
2039     }
2040
2041   sw_if_index = ntohl (mp->sw_if_index);
2042
2043   if (~0 == sw_if_index)
2044     {
2045       /* *INDENT-OFF* */
2046       pool_foreach (t, vgm->tunnels,
2047       ({
2048         send_vxlan_gpe_tunnel_details(t, q, mp->context);
2049       }));
2050       /* *INDENT-ON* */
2051     }
2052   else
2053     {
2054       if ((sw_if_index >= vec_len (vgm->tunnel_index_by_sw_if_index)) ||
2055           (~0 == vgm->tunnel_index_by_sw_if_index[sw_if_index]))
2056         {
2057           return;
2058         }
2059       t = &vgm->tunnels[vgm->tunnel_index_by_sw_if_index[sw_if_index]];
2060       send_vxlan_gpe_tunnel_details (t, q, mp->context);
2061     }
2062 }
2063
2064 static void
2065 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
2066                                           mp)
2067 {
2068   vl_api_interface_name_renumber_reply_t *rmp;
2069   int rv = 0;
2070
2071   VALIDATE_SW_IF_INDEX (mp);
2072
2073   rv = vnet_interface_name_renumber
2074     (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
2075
2076   BAD_SW_IF_INDEX_LABEL;
2077
2078   REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
2079 }
2080
2081 static int
2082 arp_change_data_callback (u32 pool_index, u8 * new_mac,
2083                           u32 sw_if_index, u32 address)
2084 {
2085   vpe_api_main_t *am = &vpe_api_main;
2086   vlib_main_t *vm = am->vlib_main;
2087   vl_api_ip4_arp_event_t *event;
2088   static f64 arp_event_last_time;
2089   f64 now = vlib_time_now (vm);
2090
2091   if (pool_is_free_index (am->arp_events, pool_index))
2092     return 1;
2093
2094   event = pool_elt_at_index (am->arp_events, pool_index);
2095   /* *INDENT-OFF* */
2096   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
2097     {
2098       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2099     }
2100   else
2101     {                           /* same mac */
2102       if (sw_if_index == event->sw_if_index &&
2103           (!event->mac_ip ||
2104            /* for BD case, also check IP address with 10 sec timeout */
2105            (address == event->address &&
2106             (now - arp_event_last_time) < 10.0)))
2107         return 1;
2108     }
2109   /* *INDENT-ON* */
2110
2111   arp_event_last_time = now;
2112   event->sw_if_index = sw_if_index;
2113   if (event->mac_ip)
2114     event->address = address;
2115   return 0;
2116 }
2117
2118 static int
2119 nd_change_data_callback (u32 pool_index, u8 * new_mac,
2120                          u32 sw_if_index, ip6_address_t * address)
2121 {
2122   vpe_api_main_t *am = &vpe_api_main;
2123   vlib_main_t *vm = am->vlib_main;
2124   vl_api_ip6_nd_event_t *event;
2125   static f64 nd_event_last_time;
2126   f64 now = vlib_time_now (vm);
2127
2128   if (pool_is_free_index (am->nd_events, pool_index))
2129     return 1;
2130
2131   event = pool_elt_at_index (am->nd_events, pool_index);
2132
2133   /* *INDENT-OFF* */
2134   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
2135     {
2136       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2137     }
2138   else
2139     {                           /* same mac */
2140       if (sw_if_index == event->sw_if_index &&
2141           (!event->mac_ip ||
2142            /* for BD case, also check IP address with 10 sec timeout */
2143            (ip6_address_is_equal (address,
2144                                   (ip6_address_t *) event->address) &&
2145             (now - nd_event_last_time) < 10.0)))
2146         return 1;
2147     }
2148   /* *INDENT-ON* */
2149
2150   nd_event_last_time = now;
2151   event->sw_if_index = sw_if_index;
2152   if (event->mac_ip)
2153     clib_memcpy (event->address, address, sizeof (event->address));
2154   return 0;
2155 }
2156
2157 static int
2158 arp_change_delete_callback (u32 pool_index, u8 * notused)
2159 {
2160   vpe_api_main_t *am = &vpe_api_main;
2161
2162   if (pool_is_free_index (am->arp_events, pool_index))
2163     return 1;
2164
2165   pool_put_index (am->arp_events, pool_index);
2166   return 0;
2167 }
2168
2169 static int
2170 nd_change_delete_callback (u32 pool_index, u8 * notused)
2171 {
2172   vpe_api_main_t *am = &vpe_api_main;
2173
2174   if (pool_is_free_index (am->nd_events, pool_index))
2175     return 1;
2176
2177   pool_put_index (am->nd_events, pool_index);
2178   return 0;
2179 }
2180
2181 static void
2182 vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
2183 {
2184   vpe_api_main_t *am = &vpe_api_main;
2185   vnet_main_t *vnm = vnet_get_main ();
2186   vl_api_want_ip4_arp_events_reply_t *rmp;
2187   vl_api_ip4_arp_event_t *event;
2188   int rv;
2189
2190   if (mp->enable_disable)
2191     {
2192       pool_get (am->arp_events, event);
2193       memset (event, 0, sizeof (*event));
2194
2195       event->_vl_msg_id = ntohs (VL_API_IP4_ARP_EVENT);
2196       event->client_index = mp->client_index;
2197       event->context = mp->context;
2198       event->address = mp->address;
2199       event->pid = mp->pid;
2200       if (mp->address == 0)
2201         event->mac_ip = 1;
2202
2203       rv = vnet_add_del_ip4_arp_change_event
2204         (vnm, arp_change_data_callback,
2205          mp->pid, &mp->address /* addr, in net byte order */ ,
2206          vpe_resolver_process_node.index,
2207          IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
2208     }
2209   else
2210     {
2211       rv = vnet_add_del_ip4_arp_change_event
2212         (vnm, arp_change_delete_callback,
2213          mp->pid, &mp->address /* addr, in net byte order */ ,
2214          vpe_resolver_process_node.index,
2215          IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2216     }
2217   REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
2218 }
2219
2220 static void
2221 vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
2222 {
2223   vpe_api_main_t *am = &vpe_api_main;
2224   vnet_main_t *vnm = vnet_get_main ();
2225   vl_api_want_ip6_nd_events_reply_t *rmp;
2226   vl_api_ip6_nd_event_t *event;
2227   int rv;
2228
2229   if (mp->enable_disable)
2230     {
2231       pool_get (am->nd_events, event);
2232       memset (event, 0, sizeof (*event));
2233
2234       event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
2235       event->client_index = mp->client_index;
2236       event->context = mp->context;
2237       clib_memcpy (event->address, mp->address, 16);
2238       event->pid = mp->pid;
2239       if (ip6_address_is_zero ((ip6_address_t *) mp->address))
2240         event->mac_ip = 1;
2241
2242       rv = vnet_add_del_ip6_nd_change_event
2243         (vnm, nd_change_data_callback,
2244          mp->pid, mp->address /* addr, in net byte order */ ,
2245          vpe_resolver_process_node.index,
2246          IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
2247     }
2248   else
2249     {
2250       rv = vnet_add_del_ip6_nd_change_event
2251         (vnm, nd_change_delete_callback,
2252          mp->pid, mp->address /* addr, in net byte order */ ,
2253          vpe_resolver_process_node.index,
2254          IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2255     }
2256   REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
2257 }
2258
2259 static void vl_api_input_acl_set_interface_t_handler
2260   (vl_api_input_acl_set_interface_t * mp)
2261 {
2262   vlib_main_t *vm = vlib_get_main ();
2263   vl_api_input_acl_set_interface_reply_t *rmp;
2264   int rv;
2265   u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
2266
2267   ip4_table_index = ntohl (mp->ip4_table_index);
2268   ip6_table_index = ntohl (mp->ip6_table_index);
2269   l2_table_index = ntohl (mp->l2_table_index);
2270   sw_if_index = ntohl (mp->sw_if_index);
2271
2272   VALIDATE_SW_IF_INDEX (mp);
2273
2274   rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
2275                                  ip6_table_index, l2_table_index, mp->is_add);
2276
2277   BAD_SW_IF_INDEX_LABEL;
2278
2279   REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
2280 }
2281
2282 static void vl_api_cop_interface_enable_disable_t_handler
2283   (vl_api_cop_interface_enable_disable_t * mp)
2284 {
2285   vl_api_cop_interface_enable_disable_reply_t *rmp;
2286   int rv;
2287   u32 sw_if_index = ntohl (mp->sw_if_index);
2288   int enable_disable;
2289
2290   VALIDATE_SW_IF_INDEX (mp);
2291
2292   enable_disable = (int) mp->enable_disable;
2293
2294   rv = cop_interface_enable_disable (sw_if_index, enable_disable);
2295
2296   BAD_SW_IF_INDEX_LABEL;
2297
2298   REPLY_MACRO (VL_API_COP_INTERFACE_ENABLE_DISABLE_REPLY);
2299 }
2300
2301 static void vl_api_cop_whitelist_enable_disable_t_handler
2302   (vl_api_cop_whitelist_enable_disable_t * mp)
2303 {
2304   vl_api_cop_whitelist_enable_disable_reply_t *rmp;
2305   cop_whitelist_enable_disable_args_t _a, *a = &_a;
2306   u32 sw_if_index = ntohl (mp->sw_if_index);
2307   int rv;
2308
2309   VALIDATE_SW_IF_INDEX (mp);
2310
2311   a->sw_if_index = sw_if_index;
2312   a->ip4 = mp->ip4;
2313   a->ip6 = mp->ip6;
2314   a->default_cop = mp->default_cop;
2315   a->fib_id = ntohl (mp->fib_id);
2316
2317   rv = cop_whitelist_enable_disable (a);
2318
2319   BAD_SW_IF_INDEX_LABEL;
2320
2321   REPLY_MACRO (VL_API_COP_WHITELIST_ENABLE_DISABLE_REPLY);
2322 }
2323
2324 static void
2325 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
2326 {
2327   int rv = 0;
2328   u8 *vector = 0;
2329   api_main_t *am = &api_main;
2330   vlib_main_t *vm = vlib_get_main ();
2331   void *oldheap;
2332   vl_api_get_node_graph_reply_t *rmp;
2333
2334   pthread_mutex_lock (&am->vlib_rp->mutex);
2335   oldheap = svm_push_data_heap (am->vlib_rp);
2336
2337   /*
2338    * Keep the number of memcpy ops to a minimum (e.g. 1).
2339    */
2340   vec_validate (vector, 16384);
2341   vec_reset_length (vector);
2342
2343   /* $$$$ FIXME */
2344   vector = vlib_node_serialize (&vm->node_main, vector,
2345                                 (u32) ~ 0 /* all threads */ ,
2346                                 1 /* include nexts */ ,
2347                                 1 /* include stats */ );
2348
2349   svm_pop_heap (oldheap);
2350   pthread_mutex_unlock (&am->vlib_rp->mutex);
2351
2352   /* *INDENT-OFF* */
2353   REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
2354   ({
2355     rmp->reply_in_shmem = (uword) vector;
2356   }));
2357   /* *INDENT-ON* */
2358 }
2359
2360 static void
2361 vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
2362 {
2363   int rv = 0;
2364   vl_api_ioam_enable_reply_t *rmp;
2365   clib_error_t *error;
2366
2367   /* Ignoring the profile id as currently a single profile
2368    * is supported */
2369   error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
2370                            mp->seqno, mp->analyse);
2371   if (error)
2372     {
2373       clib_error_report (error);
2374       rv = clib_error_get_code (error);
2375     }
2376
2377   REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
2378 }
2379
2380 static void
2381 vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
2382 {
2383   int rv = 0;
2384   vl_api_ioam_disable_reply_t *rmp;
2385   clib_error_t *error;
2386
2387   error = clear_ioam_rewrite_fn ();
2388   if (error)
2389     {
2390       clib_error_report (error);
2391       rv = clib_error_get_code (error);
2392     }
2393
2394   REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
2395 }
2396
2397 static void
2398 vl_api_policer_add_del_t_handler (vl_api_policer_add_del_t * mp)
2399 {
2400   vlib_main_t *vm = vlib_get_main ();
2401   vl_api_policer_add_del_reply_t *rmp;
2402   int rv = 0;
2403   u8 *name = NULL;
2404   sse2_qos_pol_cfg_params_st cfg;
2405   clib_error_t *error;
2406   u32 policer_index;
2407
2408   name = format (0, "%s", mp->name);
2409
2410   memset (&cfg, 0, sizeof (cfg));
2411   cfg.rfc = mp->type;
2412   cfg.rnd_type = mp->round_type;
2413   cfg.rate_type = mp->rate_type;
2414   cfg.rb.kbps.cir_kbps = mp->cir;
2415   cfg.rb.kbps.eir_kbps = mp->eir;
2416   cfg.rb.kbps.cb_bytes = mp->cb;
2417   cfg.rb.kbps.eb_bytes = mp->eb;
2418   cfg.conform_action.action_type = mp->conform_action_type;
2419   cfg.conform_action.dscp = mp->conform_dscp;
2420   cfg.exceed_action.action_type = mp->exceed_action_type;
2421   cfg.exceed_action.dscp = mp->exceed_dscp;
2422   cfg.violate_action.action_type = mp->violate_action_type;
2423   cfg.violate_action.dscp = mp->violate_dscp;
2424   cfg.color_aware = mp->color_aware;
2425
2426   error = policer_add_del (vm, name, &cfg, &policer_index, mp->is_add);
2427
2428   if (error)
2429     rv = VNET_API_ERROR_UNSPECIFIED;
2430
2431   /* *INDENT-OFF* */
2432   REPLY_MACRO2(VL_API_POLICER_ADD_DEL_REPLY,
2433   ({
2434     if (rv == 0 &&  mp->is_add)
2435       rmp->policer_index = ntohl(policer_index);
2436     else
2437       rmp->policer_index = ~0;
2438   }));
2439   /* *INDENT-ON* */
2440 }
2441
2442 static void
2443 send_policer_details (u8 * name,
2444                       sse2_qos_pol_cfg_params_st * config,
2445                       policer_read_response_type_st * templ,
2446                       unix_shared_memory_queue_t * q, u32 context)
2447 {
2448   vl_api_policer_details_t *mp;
2449
2450   mp = vl_msg_api_alloc (sizeof (*mp));
2451   memset (mp, 0, sizeof (*mp));
2452   mp->_vl_msg_id = ntohs (VL_API_POLICER_DETAILS);
2453   mp->context = context;
2454   mp->cir = htonl (config->rb.kbps.cir_kbps);
2455   mp->eir = htonl (config->rb.kbps.eir_kbps);
2456   mp->cb = htonl (config->rb.kbps.cb_bytes);
2457   mp->eb = htonl (config->rb.kbps.eb_bytes);
2458   mp->rate_type = config->rate_type;
2459   mp->round_type = config->rnd_type;
2460   mp->type = config->rfc;
2461   mp->conform_action_type = config->conform_action.action_type;
2462   mp->conform_dscp = config->conform_action.dscp;
2463   mp->exceed_action_type = config->exceed_action.action_type;
2464   mp->exceed_dscp = config->exceed_action.dscp;
2465   mp->violate_action_type = config->violate_action.action_type;
2466   mp->violate_dscp = config->violate_action.dscp;
2467   mp->single_rate = templ->single_rate ? 1 : 0;
2468   mp->color_aware = templ->color_aware ? 1 : 0;
2469   mp->scale = htonl (templ->scale);
2470   mp->cir_tokens_per_period = htonl (templ->cir_tokens_per_period);
2471   mp->pir_tokens_per_period = htonl (templ->pir_tokens_per_period);
2472   mp->current_limit = htonl (templ->current_limit);
2473   mp->current_bucket = htonl (templ->current_bucket);
2474   mp->extended_limit = htonl (templ->extended_limit);
2475   mp->extended_bucket = htonl (templ->extended_bucket);
2476   mp->last_update_time = clib_host_to_net_u64 (templ->last_update_time);
2477
2478   strncpy ((char *) mp->name, (char *) name, ARRAY_LEN (mp->name) - 1);
2479
2480   vl_msg_api_send_shmem (q, (u8 *) & mp);
2481 }
2482
2483 static void
2484 vl_api_policer_dump_t_handler (vl_api_policer_dump_t * mp)
2485 {
2486   unix_shared_memory_queue_t *q;
2487   vnet_policer_main_t *pm = &vnet_policer_main;
2488   hash_pair_t *hp;
2489   uword *p;
2490   u32 pool_index;
2491   u8 *match_name = 0;
2492   u8 *name;
2493   sse2_qos_pol_cfg_params_st *config;
2494   policer_read_response_type_st *templ;
2495
2496   q = vl_api_client_index_to_input_queue (mp->client_index);
2497   if (q == 0)
2498     return;
2499
2500   if (mp->match_name_valid)
2501     {
2502       match_name = format (0, "%s%c", mp->match_name, 0);
2503     }
2504
2505   if (mp->match_name_valid)
2506     {
2507       p = hash_get_mem (pm->policer_config_by_name, match_name);
2508       if (p)
2509         {
2510           pool_index = p[0];
2511           config = pool_elt_at_index (pm->configs, pool_index);
2512           templ = pool_elt_at_index (pm->policer_templates, pool_index);
2513           send_policer_details (match_name, config, templ, q, mp->context);
2514         }
2515     }
2516   else
2517     {
2518       /* *INDENT-OFF* */
2519       hash_foreach_pair (hp, pm->policer_config_by_name,
2520       ({
2521         name = (u8 *) hp->key;
2522         pool_index = hp->value[0];
2523         config = pool_elt_at_index (pm->configs, pool_index);
2524         templ = pool_elt_at_index (pm->policer_templates, pool_index);
2525         send_policer_details(name, config, templ, q, mp->context);
2526       }));
2527       /* *INDENT-ON* */
2528     }
2529 }
2530
2531 static void
2532   vl_api_policer_classify_set_interface_t_handler
2533   (vl_api_policer_classify_set_interface_t * mp)
2534 {
2535   vlib_main_t *vm = vlib_get_main ();
2536   vl_api_policer_classify_set_interface_reply_t *rmp;
2537   int rv;
2538   u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
2539
2540   ip4_table_index = ntohl (mp->ip4_table_index);
2541   ip6_table_index = ntohl (mp->ip6_table_index);
2542   l2_table_index = ntohl (mp->l2_table_index);
2543   sw_if_index = ntohl (mp->sw_if_index);
2544
2545   VALIDATE_SW_IF_INDEX (mp);
2546
2547   rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index,
2548                                         ip6_table_index, l2_table_index,
2549                                         mp->is_add);
2550
2551   BAD_SW_IF_INDEX_LABEL;
2552
2553   REPLY_MACRO (VL_API_POLICER_CLASSIFY_SET_INTERFACE_REPLY);
2554 }
2555
2556 static void
2557 send_policer_classify_details (u32 sw_if_index,
2558                                u32 table_index,
2559                                unix_shared_memory_queue_t * q, u32 context)
2560 {
2561   vl_api_policer_classify_details_t *mp;
2562
2563   mp = vl_msg_api_alloc (sizeof (*mp));
2564   memset (mp, 0, sizeof (*mp));
2565   mp->_vl_msg_id = ntohs (VL_API_POLICER_CLASSIFY_DETAILS);
2566   mp->context = context;
2567   mp->sw_if_index = htonl (sw_if_index);
2568   mp->table_index = htonl (table_index);
2569
2570   vl_msg_api_send_shmem (q, (u8 *) & mp);
2571 }
2572
2573 static void
2574 vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
2575 {
2576   unix_shared_memory_queue_t *q;
2577   policer_classify_main_t *pcm = &policer_classify_main;
2578   u32 *vec_tbl;
2579   int i;
2580
2581   q = vl_api_client_index_to_input_queue (mp->client_index);
2582   if (q == 0)
2583     return;
2584
2585   vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
2586
2587   if (vec_len (vec_tbl))
2588     {
2589       for (i = 0; i < vec_len (vec_tbl); i++)
2590         {
2591           if (vec_elt (vec_tbl, i) == ~0)
2592             continue;
2593
2594           send_policer_classify_details (i, vec_elt (vec_tbl, i), q,
2595                                          mp->context);
2596         }
2597     }
2598 }
2599
2600 static void
2601 vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
2602 {
2603   unix_shared_memory_queue_t *q;
2604
2605   q = vl_api_client_index_to_input_queue (mp->client_index);
2606   if (q == 0)
2607     return;
2608
2609   vnet_classify_main_t *cm = &vnet_classify_main;
2610   vnet_classify_table_t *t;
2611   u32 *table_ids = 0;
2612   u32 count;
2613
2614   /* *INDENT-OFF* */
2615   pool_foreach (t, cm->tables,
2616   ({
2617     vec_add1 (table_ids, ntohl(t - cm->tables));
2618   }));
2619   /* *INDENT-ON* */
2620   count = vec_len (table_ids);
2621
2622   vl_api_classify_table_ids_reply_t *rmp;
2623   rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
2624   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
2625   rmp->context = mp->context;
2626   rmp->count = ntohl (count);
2627   clib_memcpy (rmp->ids, table_ids, count * sizeof (u32));
2628   rmp->retval = 0;
2629
2630   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2631
2632   vec_free (table_ids);
2633 }
2634
2635 static void
2636   vl_api_classify_table_by_interface_t_handler
2637   (vl_api_classify_table_by_interface_t * mp)
2638 {
2639   vl_api_classify_table_by_interface_reply_t *rmp;
2640   int rv = 0;
2641
2642   u32 sw_if_index = ntohl (mp->sw_if_index);
2643   u32 *acl = 0;
2644
2645   vec_validate (acl, INPUT_ACL_N_TABLES - 1);
2646   vec_set (acl, ~0);
2647
2648   VALIDATE_SW_IF_INDEX (mp);
2649
2650   input_acl_main_t *am = &input_acl_main;
2651
2652   int if_idx;
2653   u32 type;
2654
2655   for (type = 0; type < INPUT_ACL_N_TABLES; type++)
2656     {
2657       u32 *vec_tbl = am->classify_table_index_by_sw_if_index[type];
2658       if (vec_len (vec_tbl))
2659         {
2660           for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
2661             {
2662               if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
2663                 {
2664                   continue;
2665                 }
2666               acl[type] = vec_elt (vec_tbl, if_idx);
2667             }
2668         }
2669     }
2670
2671   BAD_SW_IF_INDEX_LABEL;
2672
2673   /* *INDENT-OFF* */
2674   REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
2675   ({
2676     rmp->sw_if_index = ntohl(sw_if_index);
2677     rmp->l2_table_id = ntohl(acl[INPUT_ACL_TABLE_L2]);
2678     rmp->ip4_table_id = ntohl(acl[INPUT_ACL_TABLE_IP4]);
2679     rmp->ip6_table_id = ntohl(acl[INPUT_ACL_TABLE_IP6]);
2680   }));
2681   /* *INDENT-ON* */
2682   vec_free (acl);
2683 }
2684
2685 static void
2686 vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
2687 {
2688   unix_shared_memory_queue_t *q;
2689
2690   q = vl_api_client_index_to_input_queue (mp->client_index);
2691   if (q == 0)
2692     return;
2693
2694   vl_api_classify_table_info_reply_t *rmp = 0;
2695
2696   vnet_classify_main_t *cm = &vnet_classify_main;
2697   u32 table_id = ntohl (mp->table_id);
2698   vnet_classify_table_t *t;
2699
2700   /* *INDENT-OFF* */
2701   pool_foreach (t, cm->tables,
2702   ({
2703     if (table_id == t - cm->tables)
2704       {
2705         rmp = vl_msg_api_alloc_as_if_client
2706           (sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
2707         rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
2708         rmp->context = mp->context;
2709         rmp->table_id = ntohl(table_id);
2710         rmp->nbuckets = ntohl(t->nbuckets);
2711         rmp->match_n_vectors = ntohl(t->match_n_vectors);
2712         rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
2713         rmp->active_sessions = ntohl(t->active_elements);
2714         rmp->next_table_index = ntohl(t->next_table_index);
2715         rmp->miss_next_index = ntohl(t->miss_next_index);
2716         rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
2717         clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
2718         rmp->retval = 0;
2719         break;
2720       }
2721   }));
2722   /* *INDENT-ON* */
2723
2724   if (rmp == 0)
2725     {
2726       rmp = vl_msg_api_alloc (sizeof (*rmp));
2727       rmp->_vl_msg_id = ntohs ((VL_API_CLASSIFY_TABLE_INFO_REPLY));
2728       rmp->context = mp->context;
2729       rmp->retval = ntohl (VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
2730     }
2731
2732   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2733 }
2734
2735 static void
2736 vl_api_classify_session_details_t_handler (vl_api_classify_session_details_t *
2737                                            mp)
2738 {
2739   clib_warning ("BUG");
2740 }
2741
2742 static void
2743 send_classify_session_details (unix_shared_memory_queue_t * q,
2744                                u32 table_id,
2745                                u32 match_length,
2746                                vnet_classify_entry_t * e, u32 context)
2747 {
2748   vl_api_classify_session_details_t *rmp;
2749
2750   rmp = vl_msg_api_alloc (sizeof (*rmp));
2751   memset (rmp, 0, sizeof (*rmp));
2752   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_SESSION_DETAILS);
2753   rmp->context = context;
2754   rmp->table_id = ntohl (table_id);
2755   rmp->hit_next_index = ntohl (e->next_index);
2756   rmp->advance = ntohl (e->advance);
2757   rmp->opaque_index = ntohl (e->opaque_index);
2758   rmp->match_length = ntohl (match_length);
2759   clib_memcpy (rmp->match, e->key, match_length);
2760
2761   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2762 }
2763
2764 static void
2765 vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
2766 {
2767   vnet_classify_main_t *cm = &vnet_classify_main;
2768   unix_shared_memory_queue_t *q;
2769
2770   u32 table_id = ntohl (mp->table_id);
2771   vnet_classify_table_t *t;
2772
2773   q = vl_api_client_index_to_input_queue (mp->client_index);
2774   if (!q)
2775     return;
2776
2777   /* *INDENT-OFF* */
2778   pool_foreach (t, cm->tables,
2779   ({
2780     if (table_id == t - cm->tables)
2781       {
2782         vnet_classify_bucket_t * b;
2783         vnet_classify_entry_t * v, * save_v;
2784         int i, j, k;
2785
2786         for (i = 0; i < t->nbuckets; i++)
2787           {
2788             b = &t->buckets [i];
2789             if (b->offset == 0)
2790               continue;
2791
2792             save_v = vnet_classify_get_entry (t, b->offset);
2793             for (j = 0; j < (1<<b->log2_pages); j++)
2794               {
2795                 for (k = 0; k < t->entries_per_page; k++)
2796                   {
2797                     v = vnet_classify_entry_at_index
2798                       (t, save_v, j*t->entries_per_page + k);
2799                     if (vnet_classify_entry_is_free (v))
2800                       continue;
2801
2802                     send_classify_session_details
2803                       (q, table_id, t->match_n_vectors * sizeof (u32x4),
2804                        v, mp->context);
2805                   }
2806               }
2807           }
2808         break;
2809       }
2810   }));
2811   /* *INDENT-ON* */
2812 }
2813
2814 static void
2815 vl_api_set_ipfix_exporter_t_handler (vl_api_set_ipfix_exporter_t * mp)
2816 {
2817   vlib_main_t *vm = vlib_get_main ();
2818   flow_report_main_t *frm = &flow_report_main;
2819   vl_api_set_ipfix_exporter_reply_t *rmp;
2820   ip4_address_t collector, src;
2821   u16 collector_port = UDP_DST_PORT_ipfix;
2822   u32 path_mtu;
2823   u32 template_interval;
2824   u8 udp_checksum;
2825   u32 fib_id;
2826   u32 fib_index = ~0;
2827   int rv = 0;
2828
2829   memcpy (collector.data, mp->collector_address, sizeof (collector.data));
2830   collector_port = ntohs (mp->collector_port);
2831   if (collector_port == (u16) ~ 0)
2832     collector_port = UDP_DST_PORT_ipfix;
2833   memcpy (src.data, mp->src_address, sizeof (src.data));
2834   fib_id = ntohl (mp->vrf_id);
2835
2836   ip4_main_t *im = &ip4_main;
2837   if (fib_id == ~0)
2838     {
2839       fib_index = ~0;
2840     }
2841   else
2842     {
2843       uword *p = hash_get (im->fib_index_by_table_id, fib_id);
2844       if (!p)
2845         {
2846           rv = VNET_API_ERROR_NO_SUCH_FIB;
2847           goto out;
2848         }
2849       fib_index = p[0];
2850     }
2851
2852   path_mtu = ntohl (mp->path_mtu);
2853   if (path_mtu == ~0)
2854     path_mtu = 512;             // RFC 7011 section 10.3.3.
2855   template_interval = ntohl (mp->template_interval);
2856   if (template_interval == ~0)
2857     template_interval = 20;
2858   udp_checksum = mp->udp_checksum;
2859
2860   if (collector.as_u32 == 0)
2861     {
2862       rv = VNET_API_ERROR_INVALID_VALUE;
2863       goto out;
2864     }
2865
2866   if (src.as_u32 == 0)
2867     {
2868       rv = VNET_API_ERROR_INVALID_VALUE;
2869       goto out;
2870     }
2871
2872   if (path_mtu > 1450 /* vpp does not support fragmentation */ )
2873     {
2874       rv = VNET_API_ERROR_INVALID_VALUE;
2875       goto out;
2876     }
2877
2878   if (path_mtu < 68)
2879     {
2880       rv = VNET_API_ERROR_INVALID_VALUE;
2881       goto out;
2882     }
2883
2884   /* Reset report streams if we are reconfiguring IP addresses */
2885   if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
2886       frm->src_address.as_u32 != src.as_u32 ||
2887       frm->collector_port != collector_port)
2888     vnet_flow_reports_reset (frm);
2889
2890   frm->ipfix_collector.as_u32 = collector.as_u32;
2891   frm->collector_port = collector_port;
2892   frm->src_address.as_u32 = src.as_u32;
2893   frm->fib_index = fib_index;
2894   frm->path_mtu = path_mtu;
2895   frm->template_interval = template_interval;
2896   frm->udp_checksum = udp_checksum;
2897
2898   /* Turn on the flow reporting process */
2899   vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
2900
2901 out:
2902   REPLY_MACRO (VL_API_SET_IPFIX_EXPORTER_REPLY);
2903 }
2904
2905 static void
2906 vl_api_ipfix_exporter_dump_t_handler (vl_api_ipfix_exporter_dump_t * mp)
2907 {
2908   flow_report_main_t *frm = &flow_report_main;
2909   unix_shared_memory_queue_t *q;
2910   vl_api_ipfix_exporter_details_t *rmp;
2911   ip4_main_t *im = &ip4_main;
2912   u32 vrf_id;
2913
2914   q = vl_api_client_index_to_input_queue (mp->client_index);
2915   if (!q)
2916     return;
2917
2918   rmp = vl_msg_api_alloc (sizeof (*rmp));
2919   memset (rmp, 0, sizeof (*rmp));
2920   rmp->_vl_msg_id = ntohs (VL_API_IPFIX_EXPORTER_DETAILS);
2921   rmp->context = mp->context;
2922   memcpy (rmp->collector_address, frm->ipfix_collector.data,
2923           sizeof (frm->ipfix_collector.data));
2924   rmp->collector_port = htons (frm->collector_port);
2925   memcpy (rmp->src_address, frm->src_address.data,
2926           sizeof (frm->src_address.data));
2927   if (frm->fib_index == ~0)
2928     vrf_id = ~0;
2929   else
2930     vrf_id = im->fibs[frm->fib_index].ft_table_id;
2931   rmp->vrf_id = htonl (vrf_id);
2932   rmp->path_mtu = htonl (frm->path_mtu);
2933   rmp->template_interval = htonl (frm->template_interval);
2934   rmp->udp_checksum = (frm->udp_checksum != 0);
2935
2936   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2937 }
2938
2939 static void
2940   vl_api_set_ipfix_classify_stream_t_handler
2941   (vl_api_set_ipfix_classify_stream_t * mp)
2942 {
2943   vl_api_set_ipfix_classify_stream_reply_t *rmp;
2944   flow_report_classify_main_t *fcm = &flow_report_classify_main;
2945   flow_report_main_t *frm = &flow_report_main;
2946   u32 domain_id = 0;
2947   u32 src_port = UDP_DST_PORT_ipfix;
2948   int rv = 0;
2949
2950   domain_id = ntohl (mp->domain_id);
2951   src_port = ntohs (mp->src_port);
2952
2953   if (fcm->src_port != 0 &&
2954       (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
2955     {
2956       int rv = vnet_stream_change (frm, fcm->domain_id, fcm->src_port,
2957                                    domain_id, (u16) src_port);
2958       ASSERT (rv == 0);
2959     }
2960
2961   fcm->domain_id = domain_id;
2962   fcm->src_port = (u16) src_port;
2963
2964   REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
2965 }
2966
2967 static void
2968   vl_api_ipfix_classify_stream_dump_t_handler
2969   (vl_api_ipfix_classify_stream_dump_t * mp)
2970 {
2971   flow_report_classify_main_t *fcm = &flow_report_classify_main;
2972   unix_shared_memory_queue_t *q;
2973   vl_api_ipfix_classify_stream_details_t *rmp;
2974
2975   q = vl_api_client_index_to_input_queue (mp->client_index);
2976   if (!q)
2977     return;
2978
2979   rmp = vl_msg_api_alloc (sizeof (*rmp));
2980   memset (rmp, 0, sizeof (*rmp));
2981   rmp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_STREAM_DETAILS);
2982   rmp->context = mp->context;
2983   rmp->domain_id = htonl (fcm->domain_id);
2984   rmp->src_port = htons (fcm->src_port);
2985
2986   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2987 }
2988
2989 static void
2990   vl_api_ipfix_classify_table_add_del_t_handler
2991   (vl_api_ipfix_classify_table_add_del_t * mp)
2992 {
2993   vl_api_ipfix_classify_table_add_del_reply_t *rmp;
2994   flow_report_classify_main_t *fcm = &flow_report_classify_main;
2995   flow_report_main_t *frm = &flow_report_main;
2996   vnet_flow_report_add_del_args_t args;
2997   ipfix_classify_table_t *table;
2998   int is_add;
2999   u32 classify_table_index;
3000   u8 ip_version;
3001   u8 transport_protocol;
3002   int rv = 0;
3003
3004   classify_table_index = ntohl (mp->table_id);
3005   ip_version = mp->ip_version;
3006   transport_protocol = mp->transport_protocol;
3007   is_add = mp->is_add;
3008
3009   if (fcm->src_port == 0)
3010     {
3011       /* call set_ipfix_classify_stream first */
3012       rv = VNET_API_ERROR_UNSPECIFIED;
3013       goto out;
3014     }
3015
3016   memset (&args, 0, sizeof (args));
3017
3018   table = 0;
3019   int i;
3020   for (i = 0; i < vec_len (fcm->tables); i++)
3021     if (ipfix_classify_table_index_valid (i))
3022       if (fcm->tables[i].classify_table_index == classify_table_index)
3023         {
3024           table = &fcm->tables[i];
3025           break;
3026         }
3027
3028   if (is_add)
3029     {
3030       if (table)
3031         {
3032           rv = VNET_API_ERROR_VALUE_EXIST;
3033           goto out;
3034         }
3035       table = ipfix_classify_add_table ();
3036       table->classify_table_index = classify_table_index;
3037     }
3038   else
3039     {
3040       if (!table)
3041         {
3042           rv = VNET_API_ERROR_NO_SUCH_ENTRY;
3043           goto out;
3044         }
3045     }
3046
3047   table->ip_version = ip_version;
3048   table->transport_protocol = transport_protocol;
3049
3050   args.opaque.as_uword = table - fcm->tables;
3051   args.rewrite_callback = ipfix_classify_template_rewrite;
3052   args.flow_data_callback = ipfix_classify_send_flows;
3053   args.is_add = is_add;
3054   args.domain_id = fcm->domain_id;
3055   args.src_port = fcm->src_port;
3056
3057   rv = vnet_flow_report_add_del (frm, &args);
3058
3059   /* If deleting, or add failed */
3060   if (is_add == 0 || (rv && is_add))
3061     ipfix_classify_delete_table (table - fcm->tables);
3062
3063 out:
3064   REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
3065 }
3066
3067 static void
3068 send_ipfix_classify_table_details (u32 table_index,
3069                                    unix_shared_memory_queue_t * q,
3070                                    u32 context)
3071 {
3072   flow_report_classify_main_t *fcm = &flow_report_classify_main;
3073   vl_api_ipfix_classify_table_details_t *mp;
3074
3075   ipfix_classify_table_t *table = &fcm->tables[table_index];
3076
3077   mp = vl_msg_api_alloc (sizeof (*mp));
3078   memset (mp, 0, sizeof (*mp));
3079   mp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_TABLE_DETAILS);
3080   mp->context = context;
3081   mp->table_id = htonl (table->classify_table_index);
3082   mp->ip_version = table->ip_version;
3083   mp->transport_protocol = table->transport_protocol;
3084
3085   vl_msg_api_send_shmem (q, (u8 *) & mp);
3086 }
3087
3088 static void
3089   vl_api_ipfix_classify_table_dump_t_handler
3090   (vl_api_ipfix_classify_table_dump_t * mp)
3091 {
3092   flow_report_classify_main_t *fcm = &flow_report_classify_main;
3093   unix_shared_memory_queue_t *q;
3094   u32 i;
3095
3096   q = vl_api_client_index_to_input_queue (mp->client_index);
3097   if (!q)
3098     return;
3099
3100   for (i = 0; i < vec_len (fcm->tables); i++)
3101     if (ipfix_classify_table_index_valid (i))
3102       send_ipfix_classify_table_details (i, q, mp->context);
3103 }
3104
3105 static void
3106 vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
3107 {
3108   vl_api_pg_create_interface_reply_t *rmp;
3109   int rv = 0;
3110
3111   pg_main_t *pg = &pg_main;
3112   u32 pg_if_id = pg_interface_add_or_get (pg, ntohl (mp->interface_id));
3113   pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
3114
3115   /* *INDENT-OFF* */
3116   REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
3117   ({
3118     rmp->sw_if_index = ntohl(pi->sw_if_index);
3119   }));
3120   /* *INDENT-ON* */
3121 }
3122
3123 static void
3124 vl_api_pg_capture_t_handler (vl_api_pg_capture_t * mp)
3125 {
3126   vl_api_pg_capture_reply_t *rmp;
3127   int rv = 0;
3128
3129   vnet_main_t *vnm = vnet_get_main ();
3130   vnet_interface_main_t *im = &vnm->interface_main;
3131   vnet_hw_interface_t *hi = 0;
3132
3133   u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
3134   u32 hw_if_index = ~0;
3135   uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
3136   if (p)
3137     hw_if_index = *p;
3138   vec_free (intf_name);
3139
3140   if (hw_if_index != ~0)
3141     {
3142       pg_capture_args_t _a, *a = &_a;
3143
3144       u32 len = ntohl (mp->pcap_name_length);
3145       u8 *pcap_file_name = vec_new (u8, len);
3146       clib_memcpy (pcap_file_name, mp->pcap_file_name, len);
3147
3148       hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
3149       a->hw_if_index = hw_if_index;
3150       a->dev_instance = hi->dev_instance;
3151       a->is_enabled = mp->is_enabled;
3152       a->pcap_file_name = pcap_file_name;
3153       a->count = ntohl (mp->count);
3154
3155       clib_error_t *e = pg_capture (a);
3156       if (e)
3157         {
3158           clib_error_report (e);
3159           rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
3160         }
3161
3162       vec_free (pcap_file_name);
3163     }
3164   REPLY_MACRO (VL_API_PG_CAPTURE_REPLY);
3165 }
3166
3167 static void
3168 vl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t * mp)
3169 {
3170   vl_api_pg_enable_disable_reply_t *rmp;
3171   int rv = 0;
3172
3173   pg_main_t *pg = &pg_main;
3174   u32 stream_index = ~0;
3175
3176   int is_enable = mp->is_enabled != 0;
3177   u32 len = ntohl (mp->stream_name_length) - 1;
3178
3179   if (len > 0)
3180     {
3181       u8 *stream_name = vec_new (u8, len);
3182       clib_memcpy (stream_name, mp->stream_name, len);
3183       uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
3184       if (p)
3185         stream_index = *p;
3186       vec_free (stream_name);
3187     }
3188
3189   pg_enable_disable (stream_index, is_enable);
3190
3191   REPLY_MACRO (VL_API_PG_ENABLE_DISABLE_REPLY);
3192 }
3193
3194 static void
3195   vl_api_ip_source_and_port_range_check_add_del_t_handler
3196   (vl_api_ip_source_and_port_range_check_add_del_t * mp)
3197 {
3198   vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
3199   int rv = 0;
3200
3201   u8 is_ipv6 = mp->is_ipv6;
3202   u8 is_add = mp->is_add;
3203   u8 mask_length = mp->mask_length;
3204   ip4_address_t ip4_addr;
3205   ip6_address_t ip6_addr;
3206   u16 *low_ports = 0;
3207   u16 *high_ports = 0;
3208   u32 vrf_id;
3209   u16 tmp_low, tmp_high;
3210   u8 num_ranges;
3211   int i;
3212
3213   // Validate port range
3214   num_ranges = mp->number_of_ranges;
3215   if (num_ranges > 32)
3216     {                           // This is size of array in VPE.API
3217       rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
3218       goto reply;
3219     }
3220
3221   vec_reset_length (low_ports);
3222   vec_reset_length (high_ports);
3223
3224   for (i = 0; i < num_ranges; i++)
3225     {
3226       tmp_low = mp->low_ports[i];
3227       tmp_high = mp->high_ports[i];
3228       // If tmp_low <= tmp_high then only need to check tmp_low = 0
3229       // If tmp_low <= tmp_high then only need to check tmp_high > 65535
3230       if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
3231         {
3232           rv = VNET_API_ERROR_INVALID_VALUE;
3233           goto reply;
3234         }
3235       vec_add1 (low_ports, tmp_low);
3236       vec_add1 (high_ports, tmp_high + 1);
3237     }
3238
3239   // Validate mask_length
3240   if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
3241     {
3242       rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
3243       goto reply;
3244     }
3245
3246   vrf_id = ntohl (mp->vrf_id);
3247
3248   if (vrf_id < 1)
3249     {
3250       rv = VNET_API_ERROR_INVALID_VALUE;
3251       goto reply;
3252     }
3253
3254
3255   if (is_ipv6)
3256     {
3257       clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
3258       rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
3259                                                     mask_length,
3260                                                     vrf_id,
3261                                                     low_ports,
3262                                                     high_ports, is_add);
3263     }
3264   else
3265     {
3266       clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
3267       rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
3268                                                     mask_length,
3269                                                     vrf_id,
3270                                                     low_ports,
3271                                                     high_ports, is_add);
3272     }
3273
3274 reply:
3275   vec_free (low_ports);
3276   vec_free (high_ports);
3277   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
3278 }
3279
3280 static void
3281   vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
3282   (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
3283 {
3284   vlib_main_t *vm = vlib_get_main ();
3285   vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
3286   ip4_main_t *im = &ip4_main;
3287   int rv;
3288   u32 sw_if_index;
3289   u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
3290   u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
3291   uword *p = 0;
3292   int i;
3293
3294   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
3295     ntohl (mp->tcp_out_vrf_id);
3296   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
3297     ntohl (mp->udp_out_vrf_id);
3298   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
3299     ntohl (mp->tcp_in_vrf_id);
3300   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
3301     ntohl (mp->udp_in_vrf_id);
3302
3303
3304   for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
3305     {
3306       if (vrf_id[i] != 0 && vrf_id[i] != ~0)
3307         {
3308           p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
3309
3310           if (p == 0)
3311             {
3312               rv = VNET_API_ERROR_INVALID_VALUE;
3313               goto reply;
3314             }
3315
3316           fib_index[i] = p[0];
3317         }
3318       else
3319         fib_index[i] = ~0;
3320     }
3321   sw_if_index = ntohl (mp->sw_if_index);
3322
3323   VALIDATE_SW_IF_INDEX (mp);
3324
3325   rv =
3326     set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
3327                                         mp->is_add);
3328
3329   BAD_SW_IF_INDEX_LABEL;
3330 reply:
3331
3332   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
3333 }
3334
3335 static void
3336 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
3337 {
3338   vl_api_delete_subif_reply_t *rmp;
3339   int rv;
3340
3341   rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
3342
3343   REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
3344 }
3345
3346 static void
3347   vl_api_l2_interface_pbb_tag_rewrite_t_handler
3348   (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
3349 {
3350   vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
3351   vnet_main_t *vnm = vnet_get_main ();
3352   vlib_main_t *vm = vlib_get_main ();
3353   u32 vtr_op;
3354   int rv = 0;
3355
3356   VALIDATE_SW_IF_INDEX (mp);
3357
3358   vtr_op = ntohl (mp->vtr_op);
3359
3360   switch (vtr_op)
3361     {
3362     case L2_VTR_DISABLED:
3363     case L2_VTR_PUSH_2:
3364     case L2_VTR_POP_2:
3365     case L2_VTR_TRANSLATE_2_1:
3366       break;
3367
3368     default:
3369       rv = VNET_API_ERROR_INVALID_VALUE;
3370       goto bad_sw_if_index;
3371     }
3372
3373   rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
3374                         mp->b_dmac, mp->b_smac, ntohs (mp->b_vlanid),
3375                         ntohl (mp->i_sid), ntohs (mp->outer_tag));
3376
3377   BAD_SW_IF_INDEX_LABEL;
3378
3379   REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
3380
3381 }
3382
3383 static void
3384 vl_api_punt_t_handler (vl_api_punt_t * mp)
3385 {
3386   vl_api_punt_reply_t *rmp;
3387   vlib_main_t *vm = vlib_get_main ();
3388   int rv = 0;
3389   clib_error_t *error;
3390
3391   error = vnet_punt_add_del (vm, mp->ipv, mp->l4_protocol,
3392                              ntohs (mp->l4_port), mp->is_add);
3393   if (error)
3394     {
3395       rv = -1;
3396       clib_error_report (error);
3397     }
3398
3399   REPLY_MACRO (VL_API_PUNT_REPLY);
3400 }
3401
3402 static void
3403   vl_api_flow_classify_set_interface_t_handler
3404   (vl_api_flow_classify_set_interface_t * mp)
3405 {
3406   vlib_main_t *vm = vlib_get_main ();
3407   vl_api_flow_classify_set_interface_reply_t *rmp;
3408   int rv;
3409   u32 sw_if_index, ip4_table_index, ip6_table_index;
3410
3411   ip4_table_index = ntohl (mp->ip4_table_index);
3412   ip6_table_index = ntohl (mp->ip6_table_index);
3413   sw_if_index = ntohl (mp->sw_if_index);
3414
3415   VALIDATE_SW_IF_INDEX (mp);
3416
3417   rv = vnet_set_flow_classify_intfc (vm, sw_if_index, ip4_table_index,
3418                                      ip6_table_index, mp->is_add);
3419
3420   BAD_SW_IF_INDEX_LABEL;
3421
3422   REPLY_MACRO (VL_API_FLOW_CLASSIFY_SET_INTERFACE_REPLY);
3423 }
3424
3425 static void
3426 send_flow_classify_details (u32 sw_if_index,
3427                             u32 table_index,
3428                             unix_shared_memory_queue_t * q, u32 context)
3429 {
3430   vl_api_flow_classify_details_t *mp;
3431
3432   mp = vl_msg_api_alloc (sizeof (*mp));
3433   memset (mp, 0, sizeof (*mp));
3434   mp->_vl_msg_id = ntohs (VL_API_FLOW_CLASSIFY_DETAILS);
3435   mp->context = context;
3436   mp->sw_if_index = htonl (sw_if_index);
3437   mp->table_index = htonl (table_index);
3438
3439   vl_msg_api_send_shmem (q, (u8 *) & mp);
3440 }
3441
3442 static void
3443 vl_api_flow_classify_dump_t_handler (vl_api_flow_classify_dump_t * mp)
3444 {
3445   unix_shared_memory_queue_t *q;
3446   flow_classify_main_t *pcm = &flow_classify_main;
3447   u32 *vec_tbl;
3448   int i;
3449
3450   q = vl_api_client_index_to_input_queue (mp->client_index);
3451   if (q == 0)
3452     return;
3453
3454   vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
3455
3456   if (vec_len (vec_tbl))
3457     {
3458       for (i = 0; i < vec_len (vec_tbl); i++)
3459         {
3460           if (vec_elt (vec_tbl, i) == ~0)
3461             continue;
3462
3463           send_flow_classify_details (i, vec_elt (vec_tbl, i), q,
3464                                       mp->context);
3465         }
3466     }
3467 }
3468
3469 static void
3470 vl_api_feature_enable_disable_t_handler (vl_api_feature_enable_disable_t * mp)
3471 {
3472   vl_api_feature_enable_disable_reply_t *rmp;
3473   int rv = 0;
3474   u8 *arc_name, *feature_name;
3475
3476   VALIDATE_SW_IF_INDEX (mp);
3477
3478   arc_name = format (0, "%s%c", mp->arc_name, 0);
3479   feature_name = format (0, "%s%c", mp->feature_name, 0);
3480
3481   vnet_feature_registration_t *reg;
3482   reg =
3483     vnet_get_feature_reg ((const char *) arc_name,
3484                           (const char *) feature_name);
3485   if (reg == 0)
3486     rv = VNET_API_ERROR_INVALID_VALUE;
3487   else
3488     {
3489       u32 sw_if_index;
3490       clib_error_t *error = 0;
3491
3492       sw_if_index = ntohl (mp->sw_if_index);
3493       if (reg->enable_disable_cb)
3494         error = reg->enable_disable_cb (sw_if_index, mp->enable);
3495       if (!error)
3496         vnet_feature_enable_disable ((const char *) arc_name,
3497                                      (const char *) feature_name,
3498                                      sw_if_index, mp->enable, 0, 0);
3499       else
3500         {
3501           clib_error_report (error);
3502           rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
3503         }
3504     }
3505
3506   vec_free (feature_name);
3507   vec_free (arc_name);
3508
3509   BAD_SW_IF_INDEX_LABEL;
3510
3511   REPLY_MACRO (VL_API_FEATURE_ENABLE_DISABLE_REPLY);
3512 }
3513
3514 #define BOUNCE_HANDLER(nn)                                              \
3515 static void vl_api_##nn##_t_handler (                                   \
3516     vl_api_##nn##_t *mp)                                                \
3517 {                                                                       \
3518     vpe_client_registration_t *reg;                                     \
3519     vpe_api_main_t * vam = &vpe_api_main;                               \
3520     unix_shared_memory_queue_t * q;                                     \
3521                                                                         \
3522     /* One registration only... */                                      \
3523     pool_foreach(reg, vam->nn##_registrations,                          \
3524     ({                                                                  \
3525         q = vl_api_client_index_to_input_queue (reg->client_index);     \
3526         if (q) {                                                        \
3527             /*                                                          \
3528              * If the queue is stuffed, turf the msg and complain       \
3529              * It's unlikely that the intended recipient is             \
3530              * alive; avoid deadlock at all costs.                      \
3531              */                                                         \
3532             if (q->cursize == q->maxsize) {                             \
3533                 clib_warning ("ERROR: receiver queue full, drop msg");  \
3534                 vl_msg_api_free (mp);                                   \
3535                 return;                                                 \
3536             }                                                           \
3537             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
3538             return;                                                     \
3539         }                                                               \
3540     }));                                                                \
3541     vl_msg_api_free (mp);                                               \
3542 }
3543
3544 static void setup_message_id_table (api_main_t * am);
3545
3546 /*
3547  * vpe_api_hookup
3548  * Add vpe's API message handlers to the table.
3549  * vlib has alread mapped shared memory and
3550  * added the client registration handlers.
3551  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
3552  */
3553 static clib_error_t *
3554 vpe_api_hookup (vlib_main_t * vm)
3555 {
3556   api_main_t *am = &api_main;
3557
3558 #define _(N,n)                                                  \
3559     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
3560                            vl_api_##n##_t_handler,              \
3561                            vl_noop_handler,                     \
3562                            vl_api_##n##_t_endian,               \
3563                            vl_api_##n##_t_print,                \
3564                            sizeof(vl_api_##n##_t), 1);
3565   foreach_vpe_api_msg;
3566 #undef _
3567
3568   /*
3569    * Trace space for classifier mask+match
3570    */
3571   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
3572   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size
3573     += 5 * sizeof (u32x4);
3574   am->api_trace_cfg[VL_API_VXLAN_ADD_DEL_TUNNEL].size += 16 * sizeof (u32);
3575
3576   /*
3577    * Thread-safe API messages
3578    */
3579   am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
3580   am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
3581
3582   /*
3583    * Set up the (msg_name, crc, message-id) table
3584    */
3585   setup_message_id_table (am);
3586
3587   return 0;
3588 }
3589
3590 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
3591
3592 static clib_error_t *
3593 vpe_api_init (vlib_main_t * vm)
3594 {
3595   vpe_api_main_t *am = &vpe_api_main;
3596
3597   am->vlib_main = vm;
3598   am->vnet_main = vnet_get_main ();
3599   am->interface_events_registration_hash = hash_create (0, sizeof (uword));
3600   am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
3601   am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
3602   am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
3603   am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
3604   am->oam_events_registration_hash = hash_create (0, sizeof (uword));
3605   am->bfd_events_registration_hash = hash_create (0, sizeof (uword));
3606
3607   vl_api_init (vm);
3608   vl_set_memory_region_name ("/vpe-api");
3609   vl_enable_disable_memory_api (vm, 1 /* enable it */ );
3610
3611   return 0;
3612 }
3613
3614 VLIB_INIT_FUNCTION (vpe_api_init);
3615
3616
3617 static clib_error_t *
3618 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
3619 {
3620   u8 *chroot_path;
3621   u64 baseva, size, pvt_heap_size;
3622   int uid, gid, rv;
3623   const int max_buf_size = 4096;
3624   char *s, *buf;
3625   struct passwd _pw, *pw;
3626   struct group _grp, *grp;
3627   clib_error_t *e;
3628   buf = vec_new (char, 128);
3629   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3630     {
3631       if (unformat (input, "prefix %s", &chroot_path))
3632         {
3633           vec_add1 (chroot_path, 0);
3634           vl_set_memory_root_path ((char *) chroot_path);
3635         }
3636       else if (unformat (input, "uid %d", &uid))
3637         vl_set_memory_uid (uid);
3638       else if (unformat (input, "gid %d", &gid))
3639         vl_set_memory_gid (gid);
3640       else if (unformat (input, "baseva %llx", &baseva))
3641         vl_set_global_memory_baseva (baseva);
3642       else if (unformat (input, "global-size %lldM", &size))
3643         vl_set_global_memory_size (size * (1ULL << 20));
3644       else if (unformat (input, "global-size %lldG", &size))
3645         vl_set_global_memory_size (size * (1ULL << 30));
3646       else if (unformat (input, "global-size %lld", &size))
3647         vl_set_global_memory_size (size);
3648       else if (unformat (input, "global-pvt-heap-size %lldM", &pvt_heap_size))
3649         vl_set_global_pvt_heap_size (pvt_heap_size * (1ULL << 20));
3650       else if (unformat (input, "global-pvt-heap-size size %lld",
3651                          &pvt_heap_size))
3652         vl_set_global_pvt_heap_size (pvt_heap_size);
3653       else if (unformat (input, "api-pvt-heap-size %lldM", &pvt_heap_size))
3654         vl_set_api_pvt_heap_size (pvt_heap_size * (1ULL << 20));
3655       else if (unformat (input, "api-pvt-heap-size size %lld",
3656                          &pvt_heap_size))
3657         vl_set_api_pvt_heap_size (pvt_heap_size);
3658       else if (unformat (input, "api-size %lldM", &size))
3659         vl_set_api_memory_size (size * (1ULL << 20));
3660       else if (unformat (input, "api-size %lldG", &size))
3661         vl_set_api_memory_size (size * (1ULL << 30));
3662       else if (unformat (input, "api-size %lld", &size))
3663         vl_set_api_memory_size (size);
3664       else if (unformat (input, "uid %s", &s))
3665         {
3666           /* lookup the username */
3667           pw = NULL;
3668           while (((rv =
3669                    getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
3670                  && (vec_len (buf) <= max_buf_size))
3671             {
3672               vec_resize (buf, vec_len (buf) * 2);
3673             }
3674           if (rv < 0)
3675             {
3676               e = clib_error_return_code (0, rv,
3677                                           CLIB_ERROR_ERRNO_VALID |
3678                                           CLIB_ERROR_FATAL,
3679                                           "cannot fetch username %s", s);
3680               vec_free (s);
3681               vec_free (buf);
3682               return e;
3683             }
3684           if (pw == NULL)
3685             {
3686               e =
3687                 clib_error_return_fatal (0, "username %s does not exist", s);
3688               vec_free (s);
3689               vec_free (buf);
3690               return e;
3691             }
3692           vec_free (s);
3693           vl_set_memory_uid (pw->pw_uid);
3694         }
3695       else if (unformat (input, "gid %s", &s))
3696         {
3697           /* lookup the group name */
3698           grp = NULL;
3699           while (((rv =
3700                    getgrnam_r (s, &_grp, buf, vec_len (buf), &grp)) == ERANGE)
3701                  && (vec_len (buf) <= max_buf_size))
3702             {
3703               vec_resize (buf, vec_len (buf) * 2);
3704             }
3705           if (rv != 0)
3706             {
3707               e = clib_error_return_code (0, rv,
3708                                           CLIB_ERROR_ERRNO_VALID |
3709                                           CLIB_ERROR_FATAL,
3710                                           "cannot fetch group %s", s);
3711               vec_free (s);
3712               vec_free (buf);
3713               return e;
3714             }
3715           if (grp == NULL)
3716             {
3717               e = clib_error_return_fatal (0, "group %s does not exist", s);
3718               vec_free (s);
3719               vec_free (buf);
3720               return e;
3721             }
3722           vec_free (s);
3723           vec_free (buf);
3724           vl_set_memory_gid (grp->gr_gid);
3725         }
3726       else
3727         return clib_error_return (0, "unknown input `%U'",
3728                                   format_unformat_error, input);
3729     }
3730   return 0;
3731 }
3732
3733 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
3734
3735 void *
3736 get_unformat_vnet_sw_interface (void)
3737 {
3738   return (void *) &unformat_vnet_sw_interface;
3739 }
3740
3741 static u8 *
3742 format_arp_event (u8 * s, va_list * args)
3743 {
3744   vl_api_ip4_arp_event_t *event = va_arg (*args, vl_api_ip4_arp_event_t *);
3745
3746   s = format (s, "pid %d: ", event->pid);
3747   if (event->mac_ip)
3748     s = format (s, "bd mac/ip4 binding events");
3749   else
3750     s = format (s, "resolution for %U", format_ip4_address, &event->address);
3751   return s;
3752 }
3753
3754 static u8 *
3755 format_nd_event (u8 * s, va_list * args)
3756 {
3757   vl_api_ip6_nd_event_t *event = va_arg (*args, vl_api_ip6_nd_event_t *);
3758
3759   s = format (s, "pid %d: ", event->pid);
3760   if (event->mac_ip)
3761     s = format (s, "bd mac/ip6 binding events");
3762   else
3763     s = format (s, "resolution for %U", format_ip6_address, event->address);
3764   return s;
3765 }
3766
3767 static clib_error_t *
3768 show_ip_arp_nd_events_fn (vlib_main_t * vm,
3769                           unformat_input_t * input, vlib_cli_command_t * cmd)
3770 {
3771   vpe_api_main_t *am = &vpe_api_main;
3772   vl_api_ip4_arp_event_t *arp_event;
3773   vl_api_ip6_nd_event_t *nd_event;
3774
3775   if ((pool_elts (am->arp_events) == 0) && (pool_elts (am->nd_events) == 0))
3776     {
3777       vlib_cli_output (vm, "No active arp or nd event registrations");
3778       return 0;
3779     }
3780
3781   /* *INDENT-OFF* */
3782   pool_foreach (arp_event, am->arp_events,
3783   ({
3784     vlib_cli_output (vm, "%U", format_arp_event, arp_event);
3785   }));
3786
3787   pool_foreach (nd_event, am->nd_events,
3788   ({
3789     vlib_cli_output (vm, "%U", format_nd_event, nd_event);
3790   }));
3791   /* *INDENT-ON* */
3792
3793   return 0;
3794 }
3795
3796 /* *INDENT-OFF* */
3797 VLIB_CLI_COMMAND (show_ip_arp_nd_events, static) = {
3798   .path = "show arp-nd-event registrations",
3799   .function = show_ip_arp_nd_events_fn,
3800   .short_help = "Show ip4 arp and ip6 nd event registrations",
3801 };
3802 /* *INDENT-ON* */
3803
3804 #define vl_msg_name_crc_list
3805 #include <vpp/api/vpe_all_api_h.h>
3806 #undef vl_msg_name_crc_list
3807
3808 static void
3809 setup_message_id_table (api_main_t * am)
3810 {
3811 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
3812   foreach_vl_msg_name_crc_memclnt;
3813   foreach_vl_msg_name_crc_vpe;
3814 #undef _
3815 }
3816
3817
3818 /*
3819  * fd.io coding-style-patch-verification: ON
3820  *
3821  * Local Variables:
3822  * eval: (c-set-style "gnu")
3823  * End:
3824  */