API: Cleaning up message naming that does not follow the conventions
[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 #if WITH_LIBSSL > 0
56 #include <vnet/srv6/sr.h>
57 #endif
58 #include <vlib/vlib.h>
59 #include <vlib/unix/unix.h>
60 #include <vlibapi/api.h>
61 #include <vlibmemory/api.h>
62 #include <vnet/classify/input_acl.h>
63 #include <vnet/l2/l2_classify.h>
64 #include <vnet/map/map.h>
65 #include <vnet/ip/ip6_hop_by_hop.h>
66 #include <vnet/ip/ip_source_and_port_range_check.h>
67 #include <vnet/ip/punt.h>
68 #include <vnet/feature/feature.h>
69
70 #undef BIHASH_TYPE
71 #undef __included_bihash_template_h__
72 #include <vnet/l2/l2_fib.h>
73
74 #include <vpp/stats/stats.h>
75 #include <vpp/oam/oam.h>
76
77 #include <vnet/ethernet/ethernet.h>
78 #include <vnet/ethernet/arp_packet.h>
79 #include <vnet/interface.h>
80 #include <vnet/l2/l2_fib.h>
81 #include <vnet/l2/l2_bd.h>
82 #include <vpp/api/vpe_msg_enum.h>
83 #include <vnet/span/span.h>
84 #include <vnet/fib/ip6_fib.h>
85 #include <vnet/fib/ip4_fib.h>
86 #include <vnet/fib/fib_api.h>
87 #include <vnet/dpo/drop_dpo.h>
88 #include <vnet/dpo/receive_dpo.h>
89 #include <vnet/dpo/lookup_dpo.h>
90 #include <vnet/dpo/classify_dpo.h>
91 #include <vnet/dpo/ip_null_dpo.h>
92 #define vl_typedefs             /* define message structures */
93 #include <vpp/api/vpe_all_api_h.h>
94 #undef vl_typedefs
95 #define vl_endianfun            /* define message structures */
96 #include <vpp/api/vpe_all_api_h.h>
97 #undef vl_endianfun
98 /* instantiate all the print functions we know about */
99 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
100 #define vl_printfun
101 #include <vpp/api/vpe_all_api_h.h>
102 #undef vl_printfun
103 #include <vlibapi/api_helper_macros.h>
104 #define foreach_vpe_api_msg                                             \
105 _(WANT_OAM_EVENTS, want_oam_events)                                     \
106 _(OAM_ADD_DEL, oam_add_del)                                             \
107 _(SW_INTERFACE_SET_MPLS_ENABLE, sw_interface_set_mpls_enable)           \
108 _(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath)                       \
109 _(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect)           \
110 _(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge)               \
111 _(CREATE_VLAN_SUBIF, create_vlan_subif)                                 \
112 _(CREATE_SUBIF, create_subif)                                           \
113 _(PROXY_ARP_ADD_DEL, proxy_arp_add_del)                                 \
114 _(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable)       \
115 _(VNET_GET_SUMMARY_STATS, vnet_get_summary_stats)                       \
116 _(RESET_FIB, reset_fib)                                                 \
117 _(CREATE_LOOPBACK, create_loopback)                                     \
118 _(CREATE_LOOPBACK_INSTANCE, create_loopback_instance)                   \
119 _(CONTROL_PING, control_ping)                                           \
120 _(CLI, cli)                                                             \
121 _(CLI_INBAND, cli_inband)                                               \
122 _(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit)                       \
123 _(L2_PATCH_ADD_DEL, l2_patch_add_del)                                   \
124 _(CLASSIFY_SET_INTERFACE_IP_TABLE, classify_set_interface_ip_table)     \
125 _(CLASSIFY_SET_INTERFACE_L2_TABLES, classify_set_interface_l2_tables)   \
126 _(GET_NODE_INDEX, get_node_index)                                       \
127 _(ADD_NODE_NEXT, add_node_next)                                         \
128 _(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter)                     \
129 _(SHOW_VERSION, show_version)                                           \
130 _(INTERFACE_NAME_RENUMBER, interface_name_renumber)                     \
131 _(WANT_IP4_ARP_EVENTS, want_ip4_arp_events)                             \
132 _(WANT_IP6_ND_EVENTS, want_ip6_nd_events)                               \
133 _(INPUT_ACL_SET_INTERFACE, input_acl_set_interface)                     \
134 _(DELETE_LOOPBACK, delete_loopback)                                     \
135 _(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del)                                 \
136 _(GET_NODE_GRAPH, get_node_graph)                                       \
137 _(IOAM_ENABLE, ioam_enable)                                             \
138 _(IOAM_DISABLE, ioam_disable)                                           \
139 _(GET_NEXT_INDEX, get_next_index)                                       \
140 _(PG_CREATE_INTERFACE, pg_create_interface)                             \
141 _(PG_CAPTURE, pg_capture)                                               \
142 _(PG_ENABLE_DISABLE, pg_enable_disable)                                 \
143 _(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL,                               \
144   ip_source_and_port_range_check_add_del)                               \
145 _(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL,                     \
146   ip_source_and_port_range_check_interface_add_del)                     \
147 _(DELETE_SUBIF, delete_subif)                                           \
148 _(PUNT, punt)                                                           \
149 _(FEATURE_ENABLE_DISABLE, feature_enable_disable)
150
151 #define QUOTE_(x) #x
152 #define QUOTE(x) QUOTE_(x)
153 typedef enum
154 {
155   RESOLVE_IP4_ADD_DEL_ROUTE = 1,
156   RESOLVE_IP6_ADD_DEL_ROUTE,
157 } resolve_t;
158
159 static vlib_node_registration_t vpe_resolver_process_node;
160 extern vpe_api_main_t vpe_api_main;
161
162 static int arp_change_delete_callback (u32 pool_index, u8 * notused);
163 static int nd_change_delete_callback (u32 pool_index, u8 * notused);
164
165 /* Clean up all registrations belonging to the indicated client */
166 static clib_error_t *
167 memclnt_delete_callback (u32 client_index)
168 {
169   vpe_api_main_t *vam = &vpe_api_main;
170   vpe_client_registration_t *rp;
171   uword *p;
172   int stats_memclnt_delete_callback (u32 client_index);
173
174   stats_memclnt_delete_callback (client_index);
175
176 #define _(a)                                                    \
177     p = hash_get (vam->a##_registration_hash, client_index);    \
178     if (p) {                                                    \
179         rp = pool_elt_at_index (vam->a##_registrations, p[0]);  \
180         pool_put (vam->a##_registrations, rp);                  \
181         hash_unset (vam->a##_registration_hash, client_index);  \
182     }
183   foreach_registration_hash;
184 #undef _
185   return 0;
186 }
187
188 VL_MSG_API_REAPER_FUNCTION (memclnt_delete_callback);
189
190 pub_sub_handler (oam_events, OAM_EVENTS);
191
192 #define RESOLUTION_EVENT 1
193 #define RESOLUTION_PENDING_EVENT 2
194 #define IP4_ARP_EVENT 3
195 #define IP6_ND_EVENT 4
196
197 int ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp);
198
199 int ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp);
200
201 static void
202 handle_ip4_arp_event (u32 pool_index)
203 {
204   vpe_api_main_t *vam = &vpe_api_main;
205   vnet_main_t *vnm = vam->vnet_main;
206   vlib_main_t *vm = vam->vlib_main;
207   vl_api_ip4_arp_event_t *event;
208   vl_api_ip4_arp_event_t *mp;
209   unix_shared_memory_queue_t *q;
210
211   /* Client can cancel, die, etc. */
212   if (pool_is_free_index (vam->arp_events, pool_index))
213     return;
214
215   event = pool_elt_at_index (vam->arp_events, pool_index);
216
217   q = vl_api_client_index_to_input_queue (event->client_index);
218   if (!q)
219     {
220       (void) vnet_add_del_ip4_arp_change_event
221         (vnm, arp_change_delete_callback,
222          event->pid, &event->address,
223          vpe_resolver_process_node.index, IP4_ARP_EVENT,
224          ~0 /* pool index, notused */ , 0 /* is_add */ );
225       return;
226     }
227
228   if (q->cursize < q->maxsize)
229     {
230       mp = vl_msg_api_alloc (sizeof (*mp));
231       clib_memcpy (mp, event, sizeof (*mp));
232       vl_msg_api_send_shmem (q, (u8 *) & mp);
233     }
234   else
235     {
236       static f64 last_time;
237       /*
238        * Throttle syslog msgs.
239        * It's pretty tempting to just revoke the registration...
240        */
241       if (vlib_time_now (vm) > last_time + 10.0)
242         {
243           clib_warning ("arp event for %U to pid %d: queue stuffed!",
244                         format_ip4_address, &event->address, event->pid);
245           last_time = vlib_time_now (vm);
246         }
247     }
248 }
249
250 void
251 handle_ip6_nd_event (u32 pool_index)
252 {
253   vpe_api_main_t *vam = &vpe_api_main;
254   vnet_main_t *vnm = vam->vnet_main;
255   vlib_main_t *vm = vam->vlib_main;
256   vl_api_ip6_nd_event_t *event;
257   vl_api_ip6_nd_event_t *mp;
258   unix_shared_memory_queue_t *q;
259
260   /* Client can cancel, die, etc. */
261   if (pool_is_free_index (vam->nd_events, pool_index))
262     return;
263
264   event = pool_elt_at_index (vam->nd_events, pool_index);
265
266   q = vl_api_client_index_to_input_queue (event->client_index);
267   if (!q)
268     {
269       (void) vnet_add_del_ip6_nd_change_event
270         (vnm, nd_change_delete_callback,
271          event->pid, &event->address,
272          vpe_resolver_process_node.index, IP6_ND_EVENT,
273          ~0 /* pool index, notused */ , 0 /* is_add */ );
274       return;
275     }
276
277   if (q->cursize < q->maxsize)
278     {
279       mp = vl_msg_api_alloc (sizeof (*mp));
280       clib_memcpy (mp, event, sizeof (*mp));
281       vl_msg_api_send_shmem (q, (u8 *) & mp);
282     }
283   else
284     {
285       static f64 last_time;
286       /*
287        * Throttle syslog msgs.
288        * It's pretty tempting to just revoke the registration...
289        */
290       if (vlib_time_now (vm) > last_time + 10.0)
291         {
292           clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
293                         format_ip6_address, &event->address, event->pid);
294           last_time = vlib_time_now (vm);
295         }
296     }
297 }
298
299 static uword
300 resolver_process (vlib_main_t * vm,
301                   vlib_node_runtime_t * rt, vlib_frame_t * f)
302 {
303   uword event_type;
304   uword *event_data = 0;
305   f64 timeout = 100.0;
306   int i;
307
308   while (1)
309     {
310       vlib_process_wait_for_event_or_clock (vm, timeout);
311
312       event_type = vlib_process_get_events (vm, &event_data);
313
314       switch (event_type)
315         {
316         case RESOLUTION_PENDING_EVENT:
317           timeout = 1.0;
318           break;
319
320         case RESOLUTION_EVENT:
321           clib_warning ("resolver: BOGUS TYPE");
322           break;
323
324         case IP4_ARP_EVENT:
325           for (i = 0; i < vec_len (event_data); i++)
326             handle_ip4_arp_event (event_data[i]);
327           break;
328
329         case IP6_ND_EVENT:
330           for (i = 0; i < vec_len (event_data); i++)
331             handle_ip6_nd_event (event_data[i]);
332           break;
333
334         case ~0:                /* timeout */
335           break;
336         }
337
338       vec_reset_length (event_data);
339     }
340   return 0;                     /* or not */
341 }
342
343 /* *INDENT-OFF* */
344 VLIB_REGISTER_NODE (vpe_resolver_process_node,static) = {
345   .function = resolver_process,
346   .type = VLIB_NODE_TYPE_PROCESS,
347   .name = "vpe-route-resolver-process",
348 };
349 /* *INDENT-ON* */
350
351 static void
352 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
353 {
354   vl_api_sw_interface_set_vpath_reply_t *rmp;
355   int rv = 0;
356   u32 sw_if_index = ntohl (mp->sw_if_index);
357
358   VALIDATE_SW_IF_INDEX (mp);
359
360   l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
361   vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
362                                sw_if_index, mp->enable, 0, 0);
363   vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
364                                sw_if_index, mp->enable, 0, 0);
365   vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
366                                sw_if_index, mp->enable, 0, 0);
367   vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
368                                sw_if_index, mp->enable, 0, 0);
369
370   BAD_SW_IF_INDEX_LABEL;
371
372   REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
373 }
374
375 static void
376   vl_api_sw_interface_set_l2_xconnect_t_handler
377   (vl_api_sw_interface_set_l2_xconnect_t * mp)
378 {
379   vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
380   int rv = 0;
381   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
382   u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
383   vlib_main_t *vm = vlib_get_main ();
384   vnet_main_t *vnm = vnet_get_main ();
385
386   VALIDATE_RX_SW_IF_INDEX (mp);
387
388   if (mp->enable)
389     {
390       VALIDATE_TX_SW_IF_INDEX (mp);
391       rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
392                             rx_sw_if_index, 0, 0, 0, tx_sw_if_index);
393     }
394   else
395     {
396       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
397     }
398
399   BAD_RX_SW_IF_INDEX_LABEL;
400   BAD_TX_SW_IF_INDEX_LABEL;
401
402   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
403 }
404
405 static void
406   vl_api_sw_interface_set_l2_bridge_t_handler
407   (vl_api_sw_interface_set_l2_bridge_t * mp)
408 {
409   bd_main_t *bdm = &bd_main;
410   vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
411   int rv = 0;
412   vlib_main_t *vm = vlib_get_main ();
413   vnet_main_t *vnm = vnet_get_main ();
414
415   VALIDATE_RX_SW_IF_INDEX (mp);
416   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
417
418
419   if (mp->enable)
420     {
421       VALIDATE_BD_ID (mp);
422       u32 bd_id = ntohl (mp->bd_id);
423       u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
424       u32 bvi = mp->bvi;
425       u8 shg = mp->shg;
426       rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
427                             rx_sw_if_index, bd_index, bvi, shg, 0);
428     }
429   else
430     {
431       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
432     }
433
434   BAD_RX_SW_IF_INDEX_LABEL;
435   BAD_BD_ID_LABEL;
436
437   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
438 }
439
440 static void
441 vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
442 {
443   bd_main_t *bdm = &bd_main;
444   vl_api_bd_ip_mac_add_del_reply_t *rmp;
445   int rv = 0;
446   u32 bd_id = ntohl (mp->bd_id);
447   u32 bd_index;
448   uword *p;
449
450   if (bd_id == 0)
451     {
452       rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
453       goto out;
454     }
455
456   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
457   if (p == 0)
458     {
459       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
460       goto out;
461     }
462
463   bd_index = p[0];
464   if (bd_add_del_ip_mac (bd_index, mp->ip_address,
465                          mp->mac_address, mp->is_ipv6, mp->is_add))
466     rv = VNET_API_ERROR_UNSPECIFIED;
467
468 out:
469   REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
470 }
471
472 static void
473 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
474 {
475   vl_api_create_vlan_subif_reply_t *rmp;
476   vnet_main_t *vnm = vnet_get_main ();
477   u32 sw_if_index = (u32) ~ 0;
478   vnet_hw_interface_t *hi;
479   int rv = 0;
480   u32 id;
481   vnet_sw_interface_t template;
482   uword *p;
483   vnet_interface_main_t *im = &vnm->interface_main;
484   u64 sup_and_sub_key;
485   unix_shared_memory_queue_t *q;
486   clib_error_t *error;
487
488   VALIDATE_SW_IF_INDEX (mp);
489
490   hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
491
492   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
493     {
494       rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
495       goto out;
496     }
497
498   id = ntohl (mp->vlan_id);
499   if (id == 0 || id > 4095)
500     {
501       rv = VNET_API_ERROR_INVALID_VLAN;
502       goto out;
503     }
504
505   sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
506
507   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
508   if (p)
509     {
510       rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
511       goto out;
512     }
513
514   memset (&template, 0, sizeof (template));
515   template.type = VNET_SW_INTERFACE_TYPE_SUB;
516   template.sup_sw_if_index = hi->sw_if_index;
517   template.sub.id = id;
518   template.sub.eth.raw_flags = 0;
519   template.sub.eth.flags.one_tag = 1;
520   template.sub.eth.outer_vlan_id = id;
521   template.sub.eth.flags.exact_match = 1;
522
523   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
524   if (error)
525     {
526       clib_error_report (error);
527       rv = VNET_API_ERROR_INVALID_REGISTRATION;
528       goto out;
529     }
530
531   u64 *kp = clib_mem_alloc (sizeof (*kp));
532   *kp = sup_and_sub_key;
533
534   hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
535   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
536
537   BAD_SW_IF_INDEX_LABEL;
538
539 out:
540   q = vl_api_client_index_to_input_queue (mp->client_index);
541   if (!q)
542     return;
543
544   rmp = vl_msg_api_alloc (sizeof (*rmp));
545   rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
546   rmp->context = mp->context;
547   rmp->retval = htonl (rv);
548   rmp->sw_if_index = htonl (sw_if_index);
549   vl_msg_api_send_shmem (q, (u8 *) & rmp);
550 }
551
552 static void
553 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
554 {
555   vl_api_create_subif_reply_t *rmp;
556   vnet_main_t *vnm = vnet_get_main ();
557   u32 sw_if_index = ~0;
558   int rv = 0;
559   u32 sub_id;
560   vnet_sw_interface_t *si;
561   vnet_hw_interface_t *hi;
562   vnet_sw_interface_t template;
563   uword *p;
564   vnet_interface_main_t *im = &vnm->interface_main;
565   u64 sup_and_sub_key;
566   clib_error_t *error;
567
568   VALIDATE_SW_IF_INDEX (mp);
569
570   si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
571   hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
572
573   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
574     {
575       rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
576       goto out;
577     }
578
579   sw_if_index = si->sw_if_index;
580   sub_id = ntohl (mp->sub_id);
581
582   sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
583
584   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
585   if (p)
586     {
587       if (CLIB_DEBUG > 0)
588         clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
589                       sw_if_index, sub_id);
590       rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
591       goto out;
592     }
593
594   memset (&template, 0, sizeof (template));
595   template.type = VNET_SW_INTERFACE_TYPE_SUB;
596   template.sup_sw_if_index = sw_if_index;
597   template.sub.id = sub_id;
598   template.sub.eth.flags.no_tags = mp->no_tags;
599   template.sub.eth.flags.one_tag = mp->one_tag;
600   template.sub.eth.flags.two_tags = mp->two_tags;
601   template.sub.eth.flags.dot1ad = mp->dot1ad;
602   template.sub.eth.flags.exact_match = mp->exact_match;
603   template.sub.eth.flags.default_sub = mp->default_sub;
604   template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
605   template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
606   template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
607   template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
608
609   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
610   if (error)
611     {
612       clib_error_report (error);
613       rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
614       goto out;
615     }
616
617   u64 *kp = clib_mem_alloc (sizeof (*kp));
618   *kp = sup_and_sub_key;
619
620   hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
621   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
622
623   BAD_SW_IF_INDEX_LABEL;
624
625 out:
626
627   /* *INDENT-OFF* */
628   REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
629   ({
630     rmp->sw_if_index = ntohl(sw_if_index);
631   }));
632   /* *INDENT-ON* */
633 }
634
635 static void
636 vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
637 {
638   vl_api_proxy_arp_add_del_reply_t *rmp;
639   u32 fib_index;
640   int rv;
641   ip4_main_t *im = &ip4_main;
642   stats_main_t *sm = &stats_main;
643   int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
644                               ip4_address_t * hi_addr,
645                               u32 fib_index, int is_del);
646   uword *p;
647
648   dslock (sm, 1 /* release hint */ , 6 /* tag */ );
649
650   p = hash_get (im->fib_index_by_table_id, ntohl (mp->vrf_id));
651
652   if (!p)
653     {
654       rv = VNET_API_ERROR_NO_SUCH_FIB;
655       goto out;
656     }
657
658   fib_index = p[0];
659
660   rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->low_address,
661                                (ip4_address_t *) mp->hi_address,
662                                fib_index, mp->is_add == 0);
663
664 out:
665   dsunlock (sm);
666   REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
667 }
668
669 static void
670   vl_api_proxy_arp_intfc_enable_disable_t_handler
671   (vl_api_proxy_arp_intfc_enable_disable_t * mp)
672 {
673   int rv = 0;
674   vnet_main_t *vnm = vnet_get_main ();
675   vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
676
677   VALIDATE_SW_IF_INDEX (mp);
678
679   vnet_sw_interface_t *si =
680     vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
681
682   ASSERT (si);
683
684   if (mp->enable_disable)
685     si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
686   else
687     si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
688
689   BAD_SW_IF_INDEX_LABEL;
690
691 out:
692   REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
693 }
694
695 static void
696   vl_api_sw_interface_set_mpls_enable_t_handler
697   (vl_api_sw_interface_set_mpls_enable_t * mp)
698 {
699   vl_api_sw_interface_set_mpls_enable_reply_t *rmp;
700   int rv = 0;
701
702   VALIDATE_SW_IF_INDEX (mp);
703
704   mpls_sw_interface_enable_disable (&mpls_main,
705                                     ntohl (mp->sw_if_index), mp->enable);
706
707   BAD_SW_IF_INDEX_LABEL;
708   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MPLS_ENABLE_REPLY);
709 }
710
711 void
712 send_oam_event (oam_target_t * t)
713 {
714   vpe_api_main_t *vam = &vpe_api_main;
715   unix_shared_memory_queue_t *q;
716   vpe_client_registration_t *reg;
717   vl_api_oam_event_t *mp;
718
719   /* *INDENT-OFF* */
720   pool_foreach(reg, vam->oam_events_registrations,
721   ({
722     q = vl_api_client_index_to_input_queue (reg->client_index);
723     if (q)
724       {
725         mp = vl_msg_api_alloc (sizeof (*mp));
726         mp->_vl_msg_id = ntohs (VL_API_OAM_EVENT);
727         clib_memcpy (mp->dst_address, &t->dst_address,
728                      sizeof (mp->dst_address));
729         mp->state = t->state;
730         vl_msg_api_send_shmem (q, (u8 *)&mp);
731       }
732   }));
733   /* *INDENT-ON* */
734 }
735
736 static void
737 vl_api_oam_add_del_t_handler (vl_api_oam_add_del_t * mp)
738 {
739   vl_api_oam_add_del_reply_t *rmp;
740   int rv;
741
742   rv = vpe_oam_add_del_target ((ip4_address_t *) mp->src_address,
743                                (ip4_address_t *) mp->dst_address,
744                                ntohl (mp->vrf_id), (int) (mp->is_add));
745
746   REPLY_MACRO (VL_API_OAM_ADD_DEL_REPLY);
747 }
748
749 static void
750 vl_api_vnet_get_summary_stats_t_handler (vl_api_vnet_get_summary_stats_t * mp)
751 {
752   stats_main_t *sm = &stats_main;
753   vnet_interface_main_t *im = sm->interface_main;
754   vl_api_vnet_get_summary_stats_reply_t *rmp;
755   vlib_combined_counter_main_t *cm;
756   vlib_counter_t v;
757   int i, which;
758   u64 total_pkts[VLIB_N_RX_TX];
759   u64 total_bytes[VLIB_N_RX_TX];
760
761   unix_shared_memory_queue_t *q =
762     vl_api_client_index_to_input_queue (mp->client_index);
763
764   if (!q)
765     return;
766
767   rmp = vl_msg_api_alloc (sizeof (*rmp));
768   rmp->_vl_msg_id = ntohs (VL_API_VNET_GET_SUMMARY_STATS_REPLY);
769   rmp->context = mp->context;
770   rmp->retval = 0;
771
772   memset (total_pkts, 0, sizeof (total_pkts));
773   memset (total_bytes, 0, sizeof (total_bytes));
774
775   vnet_interface_counter_lock (im);
776
777   vec_foreach (cm, im->combined_sw_if_counters)
778   {
779     which = cm - im->combined_sw_if_counters;
780
781     for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
782       {
783         vlib_get_combined_counter (cm, i, &v);
784         total_pkts[which] += v.packets;
785         total_bytes[which] += v.bytes;
786       }
787   }
788   vnet_interface_counter_unlock (im);
789
790   rmp->total_pkts[VLIB_RX] = clib_host_to_net_u64 (total_pkts[VLIB_RX]);
791   rmp->total_bytes[VLIB_RX] = clib_host_to_net_u64 (total_bytes[VLIB_RX]);
792   rmp->total_pkts[VLIB_TX] = clib_host_to_net_u64 (total_pkts[VLIB_TX]);
793   rmp->total_bytes[VLIB_TX] = clib_host_to_net_u64 (total_bytes[VLIB_TX]);
794   rmp->vector_rate =
795     clib_host_to_net_u64 (vlib_last_vector_length_per_node (sm->vlib_main));
796
797   vl_msg_api_send_shmem (q, (u8 *) & rmp);
798 }
799
800 /* *INDENT-OFF* */
801 typedef CLIB_PACKED (struct {
802   ip4_address_t address;
803   u32 address_length: 6;
804   u32 index:26;
805 }) ip4_route_t;
806 /* *INDENT-ON* */
807
808 static int
809 ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
810 {
811   vnet_main_t *vnm = vnet_get_main ();
812   vnet_interface_main_t *im = &vnm->interface_main;
813   ip4_main_t *im4 = &ip4_main;
814   static u32 *sw_if_indices_to_shut;
815   stats_main_t *sm = &stats_main;
816   fib_table_t *fib_table;
817   ip4_fib_t *fib;
818   u32 sw_if_index;
819   int i;
820   int rv = VNET_API_ERROR_NO_SUCH_FIB;
821   u32 target_fib_id = ntohl (mp->vrf_id);
822
823   dslock (sm, 1 /* release hint */ , 8 /* tag */ );
824
825   /* *INDENT-OFF* */
826   pool_foreach (fib_table, im4->fibs,
827   ({
828     vnet_sw_interface_t * si;
829
830     fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
831
832     if (fib->table_id != target_fib_id)
833       continue;
834
835     /* remove any mpls encap/decap labels */
836     mpls_fib_reset_labels (fib->table_id);
837
838     /* remove any proxy arps in this fib */
839     vnet_proxy_arp_fib_reset (fib->table_id);
840
841     /* Set the flow hash for this fib to the default */
842     vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
843
844     vec_reset_length (sw_if_indices_to_shut);
845
846     /* Shut down interfaces in this FIB / clean out intfc routes */
847     pool_foreach (si, im->sw_interfaces,
848     ({
849       u32 sw_if_index = si->sw_if_index;
850
851       if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
852           && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
853               fib->index))
854         vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
855     }));
856
857     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
858       sw_if_index = sw_if_indices_to_shut[i];
859
860       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
861       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
862       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
863     }
864
865     fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
866
867     rv = 0;
868     break;
869     })); /* pool_foreach (fib) */
870     /* *INDENT-ON* */
871
872   dsunlock (sm);
873   return rv;
874 }
875
876 static int
877 ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
878 {
879   vnet_main_t *vnm = vnet_get_main ();
880   vnet_interface_main_t *im = &vnm->interface_main;
881   ip6_main_t *im6 = &ip6_main;
882   stats_main_t *sm = &stats_main;
883   static u32 *sw_if_indices_to_shut;
884   fib_table_t *fib_table;
885   ip6_fib_t *fib;
886   u32 sw_if_index;
887   int i;
888   int rv = VNET_API_ERROR_NO_SUCH_FIB;
889   u32 target_fib_id = ntohl (mp->vrf_id);
890
891   dslock (sm, 1 /* release hint */ , 9 /* tag */ );
892
893   /* *INDENT-OFF* */
894   pool_foreach (fib_table, im6->fibs,
895   ({
896     vnet_sw_interface_t * si;
897
898     fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
899
900     if (fib->table_id != target_fib_id)
901       continue;
902
903     vec_reset_length (sw_if_indices_to_shut);
904
905     /* Set the flow hash for this fib to the default */
906     vnet_set_ip6_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
907
908     /* Shut down interfaces in this FIB / clean out intfc routes */
909     pool_foreach (si, im->sw_interfaces,
910     ({
911       if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
912           fib->index)
913         vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
914     }));
915
916     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
917       sw_if_index = sw_if_indices_to_shut[i];
918
919       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
920       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
921       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
922     }
923
924     fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
925
926     rv = 0;
927     break;
928   })); /* pool_foreach (fib) */
929   /* *INDENT-ON* */
930
931   dsunlock (sm);
932   return rv;
933 }
934
935 static void
936 vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
937 {
938   int rv;
939   vl_api_reset_fib_reply_t *rmp;
940
941   if (mp->is_ipv6)
942     rv = ip6_reset_fib_t_handler (mp);
943   else
944     rv = ip4_reset_fib_t_handler (mp);
945
946   REPLY_MACRO (VL_API_RESET_FIB_REPLY);
947 }
948
949 static void
950 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
951 {
952   vl_api_create_loopback_reply_t *rmp;
953   u32 sw_if_index;
954   int rv;
955
956   rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address, 0, 0);
957
958   /* *INDENT-OFF* */
959   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
960   ({
961     rmp->sw_if_index = ntohl (sw_if_index);
962   }));
963   /* *INDENT-ON* */
964 }
965
966 static void vl_api_create_loopback_instance_t_handler
967   (vl_api_create_loopback_instance_t * mp)
968 {
969   vl_api_create_loopback_instance_reply_t *rmp;
970   u32 sw_if_index;
971   u8 is_specified = mp->is_specified;
972   u32 user_instance = ntohl (mp->user_instance);
973   int rv;
974
975   rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address,
976                                        is_specified, user_instance);
977
978   /* *INDENT-OFF* */
979   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY,
980   ({
981     rmp->sw_if_index = ntohl (sw_if_index);
982   }));
983   /* *INDENT-ON* */
984 }
985
986 static void
987 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
988 {
989   vl_api_delete_loopback_reply_t *rmp;
990   u32 sw_if_index;
991   int rv;
992
993   sw_if_index = ntohl (mp->sw_if_index);
994   rv = vnet_delete_loopback_interface (sw_if_index);
995
996   REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
997 }
998
999 static void
1000 vl_api_control_ping_t_handler (vl_api_control_ping_t * mp)
1001 {
1002   vl_api_control_ping_reply_t *rmp;
1003   int rv = 0;
1004
1005   /* *INDENT-OFF* */
1006   REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
1007   ({
1008     rmp->vpe_pid = ntohl (getpid());
1009   }));
1010   /* *INDENT-ON* */
1011 }
1012
1013 static void
1014 shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
1015 {
1016   u8 **shmem_vecp = (u8 **) arg;
1017   u8 *shmem_vec;
1018   void *oldheap;
1019   api_main_t *am = &api_main;
1020   u32 offset;
1021
1022   shmem_vec = *shmem_vecp;
1023
1024   offset = vec_len (shmem_vec);
1025
1026   pthread_mutex_lock (&am->vlib_rp->mutex);
1027   oldheap = svm_push_data_heap (am->vlib_rp);
1028
1029   vec_validate (shmem_vec, offset + buffer_bytes - 1);
1030
1031   clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
1032
1033   svm_pop_heap (oldheap);
1034   pthread_mutex_unlock (&am->vlib_rp->mutex);
1035
1036   *shmem_vecp = shmem_vec;
1037 }
1038
1039
1040 static void
1041 vl_api_cli_t_handler (vl_api_cli_t * mp)
1042 {
1043   vl_api_cli_reply_t *rp;
1044   unix_shared_memory_queue_t *q;
1045   vlib_main_t *vm = vlib_get_main ();
1046   api_main_t *am = &api_main;
1047   unformat_input_t input;
1048   u8 *shmem_vec = 0;
1049   void *oldheap;
1050
1051   q = vl_api_client_index_to_input_queue (mp->client_index);
1052   if (!q)
1053     return;
1054
1055   rp = vl_msg_api_alloc (sizeof (*rp));
1056   rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY);
1057   rp->context = mp->context;
1058
1059   unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
1060
1061   vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
1062
1063   pthread_mutex_lock (&am->vlib_rp->mutex);
1064   oldheap = svm_push_data_heap (am->vlib_rp);
1065
1066   vec_add1 (shmem_vec, 0);
1067
1068   svm_pop_heap (oldheap);
1069   pthread_mutex_unlock (&am->vlib_rp->mutex);
1070
1071   rp->reply_in_shmem = (uword) shmem_vec;
1072
1073   vl_msg_api_send_shmem (q, (u8 *) & rp);
1074 }
1075
1076 static void
1077 inband_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
1078 {
1079   u8 **mem_vecp = (u8 **) arg;
1080   u8 *mem_vec = *mem_vecp;
1081   u32 offset = vec_len (mem_vec);
1082
1083   vec_validate (mem_vec, offset + buffer_bytes - 1);
1084   clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
1085   *mem_vecp = mem_vec;
1086 }
1087
1088 static void
1089 vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
1090 {
1091   vl_api_cli_inband_reply_t *rmp;
1092   int rv = 0;
1093   unix_shared_memory_queue_t *q;
1094   vlib_main_t *vm = vlib_get_main ();
1095   unformat_input_t input;
1096   u8 *out_vec = 0;
1097
1098   q = vl_api_client_index_to_input_queue (mp->client_index);
1099   if (!q)
1100     return;
1101
1102   unformat_init_string (&input, (char *) mp->cmd, ntohl (mp->length));
1103   vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
1104
1105   u32 len = vec_len (out_vec);
1106   /* *INDENT-OFF* */
1107   REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, len,
1108   ({
1109     rmp->length = htonl (len);
1110     clib_memcpy (rmp->reply, out_vec, len);
1111   }));
1112   /* *INDENT-ON* */
1113   vec_free (out_vec);
1114 }
1115
1116 static void
1117 vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
1118 {
1119   int rv;
1120   vl_api_set_arp_neighbor_limit_reply_t *rmp;
1121   vnet_main_t *vnm = vnet_get_main ();
1122   clib_error_t *error;
1123
1124   vnm->api_errno = 0;
1125
1126   if (mp->is_ipv6)
1127     error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
1128   else
1129     error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
1130
1131   if (error)
1132     {
1133       clib_error_report (error);
1134       rv = VNET_API_ERROR_UNSPECIFIED;
1135     }
1136   else
1137     {
1138       rv = vnm->api_errno;
1139     }
1140
1141   REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
1142 }
1143
1144 static void vl_api_classify_set_interface_ip_table_t_handler
1145   (vl_api_classify_set_interface_ip_table_t * mp)
1146 {
1147   vlib_main_t *vm = vlib_get_main ();
1148   vl_api_classify_set_interface_ip_table_reply_t *rmp;
1149   int rv;
1150
1151   VALIDATE_SW_IF_INDEX (mp);
1152
1153   u32 table_index = ntohl (mp->table_index);
1154   u32 sw_if_index = ntohl (mp->sw_if_index);
1155
1156   if (mp->is_ipv6)
1157     rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
1158   else
1159     rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
1160
1161   BAD_SW_IF_INDEX_LABEL;
1162
1163   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
1164 }
1165
1166 static void vl_api_classify_set_interface_l2_tables_t_handler
1167   (vl_api_classify_set_interface_l2_tables_t * mp)
1168 {
1169   vl_api_classify_set_interface_l2_tables_reply_t *rmp;
1170   int rv;
1171   u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
1172   int enable;
1173
1174   ip4_table_index = ntohl (mp->ip4_table_index);
1175   ip6_table_index = ntohl (mp->ip6_table_index);
1176   other_table_index = ntohl (mp->other_table_index);
1177   sw_if_index = ntohl (mp->sw_if_index);
1178
1179   VALIDATE_SW_IF_INDEX (mp);
1180
1181   if (mp->is_input)
1182     rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
1183                                             ip6_table_index,
1184                                             other_table_index);
1185   else
1186     rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
1187                                              ip6_table_index,
1188                                              other_table_index);
1189
1190   if (rv == 0)
1191     {
1192       if (ip4_table_index != ~0 || ip6_table_index != ~0
1193           || other_table_index != ~0)
1194         enable = 1;
1195       else
1196         enable = 0;
1197
1198       if (mp->is_input)
1199         vnet_l2_input_classify_enable_disable (sw_if_index, enable);
1200       else
1201         vnet_l2_output_classify_enable_disable (sw_if_index, enable);
1202     }
1203
1204   BAD_SW_IF_INDEX_LABEL;
1205
1206   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
1207 }
1208
1209 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
1210                                      u32 sw_if_index, u32 enable);
1211
1212 static void
1213 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
1214                                           mp)
1215 {
1216   int rv;
1217   vl_api_l2_interface_efp_filter_reply_t *rmp;
1218   vnet_main_t *vnm = vnet_get_main ();
1219
1220   // enable/disable the feature
1221   l2_efp_filter_configure (vnm, mp->sw_if_index, mp->enable_disable);
1222   rv = vnm->api_errno;
1223
1224   REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
1225 }
1226
1227 static void
1228 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
1229 {
1230   vl_api_show_version_reply_t *rmp;
1231   int rv = 0;
1232   char *vpe_api_get_build_directory (void);
1233   char *vpe_api_get_version (void);
1234   char *vpe_api_get_build_date (void);
1235
1236   unix_shared_memory_queue_t *q =
1237     vl_api_client_index_to_input_queue (mp->client_index);
1238
1239   if (!q)
1240     return;
1241
1242   /* *INDENT-OFF* */
1243   REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
1244   ({
1245     strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
1246     strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
1247              ARRAY_LEN(rmp->build_directory)-1);
1248     strncpy ((char *) rmp->version, vpe_api_get_version(),
1249              ARRAY_LEN(rmp->version)-1);
1250     strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
1251              ARRAY_LEN(rmp->build_date)-1);
1252   }));
1253   /* *INDENT-ON* */
1254 }
1255
1256 static void
1257 vl_api_get_node_index_t_handler (vl_api_get_node_index_t * mp)
1258 {
1259   vlib_main_t *vm = vlib_get_main ();
1260   vl_api_get_node_index_reply_t *rmp;
1261   vlib_node_t *n;
1262   int rv = 0;
1263   u32 node_index = ~0;
1264
1265   n = vlib_get_node_by_name (vm, mp->node_name);
1266
1267   if (n == 0)
1268     rv = VNET_API_ERROR_NO_SUCH_NODE;
1269   else
1270     node_index = n->index;
1271
1272   /* *INDENT-OFF* */
1273   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
1274   ({
1275     rmp->node_index = ntohl(node_index);
1276   }));
1277   /* *INDENT-ON* */
1278 }
1279
1280 static void
1281 vl_api_get_next_index_t_handler (vl_api_get_next_index_t * mp)
1282 {
1283   vlib_main_t *vm = vlib_get_main ();
1284   vl_api_get_next_index_reply_t *rmp;
1285   vlib_node_t *node, *next_node;
1286   int rv = 0;
1287   u32 next_node_index = ~0, next_index = ~0;
1288   uword *p;
1289
1290   node = vlib_get_node_by_name (vm, mp->node_name);
1291
1292   if (node == 0)
1293     {
1294       rv = VNET_API_ERROR_NO_SUCH_NODE;
1295       goto out;
1296     }
1297
1298   next_node = vlib_get_node_by_name (vm, mp->next_name);
1299
1300   if (next_node == 0)
1301     {
1302       rv = VNET_API_ERROR_NO_SUCH_NODE2;
1303       goto out;
1304     }
1305   else
1306     next_node_index = next_node->index;
1307
1308   p = hash_get (node->next_slot_by_node, next_node_index);
1309
1310   if (p == 0)
1311     {
1312       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1313       goto out;
1314     }
1315   else
1316     next_index = p[0];
1317
1318 out:
1319   /* *INDENT-OFF* */
1320   REPLY_MACRO2(VL_API_GET_NEXT_INDEX_REPLY,
1321   ({
1322     rmp->next_index = ntohl(next_index);
1323   }));
1324   /* *INDENT-ON* */
1325 }
1326
1327 static void
1328 vl_api_add_node_next_t_handler (vl_api_add_node_next_t * mp)
1329 {
1330   vlib_main_t *vm = vlib_get_main ();
1331   vl_api_add_node_next_reply_t *rmp;
1332   vlib_node_t *n, *next;
1333   int rv = 0;
1334   u32 next_index = ~0;
1335
1336   n = vlib_get_node_by_name (vm, mp->node_name);
1337
1338   if (n == 0)
1339     {
1340       rv = VNET_API_ERROR_NO_SUCH_NODE;
1341       goto out;
1342     }
1343
1344   next = vlib_get_node_by_name (vm, mp->next_name);
1345
1346   if (next == 0)
1347     rv = VNET_API_ERROR_NO_SUCH_NODE2;
1348   else
1349     next_index = vlib_node_add_next (vm, n->index, next->index);
1350
1351 out:
1352   /* *INDENT-OFF* */
1353   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
1354   ({
1355     rmp->next_index = ntohl(next_index);
1356   }));
1357   /* *INDENT-ON* */
1358 }
1359
1360 static void
1361 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
1362 {
1363   extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1364                                     int is_add);
1365   vl_api_l2_patch_add_del_reply_t *rmp;
1366   int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1367                              int is_add);
1368   int rv = 0;
1369
1370   VALIDATE_RX_SW_IF_INDEX (mp);
1371   VALIDATE_TX_SW_IF_INDEX (mp);
1372
1373   rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
1374                               ntohl (mp->tx_sw_if_index),
1375                               (int) (mp->is_add != 0));
1376
1377   BAD_RX_SW_IF_INDEX_LABEL;
1378   BAD_TX_SW_IF_INDEX_LABEL;
1379
1380   REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
1381 }
1382
1383 static void
1384 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
1385                                           mp)
1386 {
1387   vl_api_interface_name_renumber_reply_t *rmp;
1388   int rv = 0;
1389
1390   VALIDATE_SW_IF_INDEX (mp);
1391
1392   rv = vnet_interface_name_renumber
1393     (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
1394
1395   BAD_SW_IF_INDEX_LABEL;
1396
1397   REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
1398 }
1399
1400 static int
1401 arp_change_data_callback (u32 pool_index, u8 * new_mac,
1402                           u32 sw_if_index, u32 address)
1403 {
1404   vpe_api_main_t *am = &vpe_api_main;
1405   vlib_main_t *vm = am->vlib_main;
1406   vl_api_ip4_arp_event_t *event;
1407   static f64 arp_event_last_time;
1408   f64 now = vlib_time_now (vm);
1409
1410   if (pool_is_free_index (am->arp_events, pool_index))
1411     return 1;
1412
1413   event = pool_elt_at_index (am->arp_events, pool_index);
1414   /* *INDENT-OFF* */
1415   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
1416     {
1417       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
1418     }
1419   else
1420     {                           /* same mac */
1421       if (sw_if_index == ntohl(event->sw_if_index) &&
1422           (!event->mac_ip ||
1423            /* for BD case, also check IP address with 10 sec timeout */
1424            (address == event->address &&
1425             (now - arp_event_last_time) < 10.0)))
1426         return 1;
1427     }
1428   /* *INDENT-ON* */
1429
1430   arp_event_last_time = now;
1431   event->sw_if_index = htonl (sw_if_index);
1432   if (event->mac_ip)
1433     event->address = address;
1434   return 0;
1435 }
1436
1437 static int
1438 nd_change_data_callback (u32 pool_index, u8 * new_mac,
1439                          u32 sw_if_index, ip6_address_t * address)
1440 {
1441   vpe_api_main_t *am = &vpe_api_main;
1442   vlib_main_t *vm = am->vlib_main;
1443   vl_api_ip6_nd_event_t *event;
1444   static f64 nd_event_last_time;
1445   f64 now = vlib_time_now (vm);
1446
1447   if (pool_is_free_index (am->nd_events, pool_index))
1448     return 1;
1449
1450   event = pool_elt_at_index (am->nd_events, pool_index);
1451
1452   /* *INDENT-OFF* */
1453   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
1454     {
1455       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
1456     }
1457   else
1458     {                           /* same mac */
1459       if (sw_if_index == ntohl(event->sw_if_index) &&
1460           (!event->mac_ip ||
1461            /* for BD case, also check IP address with 10 sec timeout */
1462            (ip6_address_is_equal (address,
1463                                   (ip6_address_t *) event->address) &&
1464             (now - nd_event_last_time) < 10.0)))
1465         return 1;
1466     }
1467   /* *INDENT-ON* */
1468
1469   nd_event_last_time = now;
1470   event->sw_if_index = htonl (sw_if_index);
1471   if (event->mac_ip)
1472     clib_memcpy (event->address, address, sizeof (event->address));
1473   return 0;
1474 }
1475
1476 static int
1477 arp_change_delete_callback (u32 pool_index, u8 * notused)
1478 {
1479   vpe_api_main_t *am = &vpe_api_main;
1480
1481   if (pool_is_free_index (am->arp_events, pool_index))
1482     return 1;
1483
1484   pool_put_index (am->arp_events, pool_index);
1485   return 0;
1486 }
1487
1488 static int
1489 nd_change_delete_callback (u32 pool_index, u8 * notused)
1490 {
1491   vpe_api_main_t *am = &vpe_api_main;
1492
1493   if (pool_is_free_index (am->nd_events, pool_index))
1494     return 1;
1495
1496   pool_put_index (am->nd_events, pool_index);
1497   return 0;
1498 }
1499
1500 static void
1501 vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
1502 {
1503   vpe_api_main_t *am = &vpe_api_main;
1504   vnet_main_t *vnm = vnet_get_main ();
1505   vl_api_want_ip4_arp_events_reply_t *rmp;
1506   int rv;
1507
1508   if (mp->enable_disable)
1509     {
1510       vl_api_ip4_arp_event_t *event;
1511       pool_get (am->arp_events, event);
1512       rv = vnet_add_del_ip4_arp_change_event
1513         (vnm, arp_change_data_callback,
1514          mp->pid, &mp->address /* addr, in net byte order */ ,
1515          vpe_resolver_process_node.index,
1516          IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
1517
1518       if (rv)
1519         {
1520           pool_put (am->arp_events, event);
1521           goto out;
1522         }
1523       memset (event, 0, sizeof (*event));
1524
1525       event->_vl_msg_id = ntohs (VL_API_IP4_ARP_EVENT);
1526       event->client_index = mp->client_index;
1527       event->context = mp->context;
1528       event->address = mp->address;
1529       event->pid = mp->pid;
1530       if (mp->address == 0)
1531         event->mac_ip = 1;
1532     }
1533   else
1534     {
1535       rv = vnet_add_del_ip4_arp_change_event
1536         (vnm, arp_change_delete_callback,
1537          mp->pid, &mp->address /* addr, in net byte order */ ,
1538          vpe_resolver_process_node.index,
1539          IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
1540     }
1541 out:
1542   REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
1543 }
1544
1545 static void
1546 vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
1547 {
1548   vpe_api_main_t *am = &vpe_api_main;
1549   vnet_main_t *vnm = vnet_get_main ();
1550   vl_api_want_ip6_nd_events_reply_t *rmp;
1551   int rv;
1552
1553   if (mp->enable_disable)
1554     {
1555       vl_api_ip6_nd_event_t *event;
1556       pool_get (am->nd_events, event);
1557
1558       rv = vnet_add_del_ip6_nd_change_event
1559         (vnm, nd_change_data_callback,
1560          mp->pid, mp->address /* addr, in net byte order */ ,
1561          vpe_resolver_process_node.index,
1562          IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
1563
1564       if (rv)
1565         {
1566           pool_put (am->nd_events, event);
1567           goto out;
1568         }
1569       memset (event, 0, sizeof (*event));
1570
1571       event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
1572       event->client_index = mp->client_index;
1573       event->context = mp->context;
1574       clib_memcpy (event->address, mp->address, 16);
1575       event->pid = mp->pid;
1576       if (ip6_address_is_zero ((ip6_address_t *) mp->address))
1577         event->mac_ip = 1;
1578
1579     }
1580   else
1581     {
1582       rv = vnet_add_del_ip6_nd_change_event
1583         (vnm, nd_change_delete_callback,
1584          mp->pid, mp->address /* addr, in net byte order */ ,
1585          vpe_resolver_process_node.index,
1586          IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
1587     }
1588 out:
1589   REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
1590 }
1591
1592 static void vl_api_input_acl_set_interface_t_handler
1593   (vl_api_input_acl_set_interface_t * mp)
1594 {
1595   vlib_main_t *vm = vlib_get_main ();
1596   vl_api_input_acl_set_interface_reply_t *rmp;
1597   int rv;
1598
1599   VALIDATE_SW_IF_INDEX (mp);
1600
1601   u32 ip4_table_index = ntohl (mp->ip4_table_index);
1602   u32 ip6_table_index = ntohl (mp->ip6_table_index);
1603   u32 l2_table_index = ntohl (mp->l2_table_index);
1604   u32 sw_if_index = ntohl (mp->sw_if_index);
1605
1606   rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
1607                                  ip6_table_index, l2_table_index, mp->is_add);
1608
1609   BAD_SW_IF_INDEX_LABEL;
1610
1611   REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
1612 }
1613
1614 static void
1615 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
1616 {
1617   int rv = 0;
1618   u8 *vector = 0;
1619   api_main_t *am = &api_main;
1620   vlib_main_t *vm = vlib_get_main ();
1621   void *oldheap;
1622   vl_api_get_node_graph_reply_t *rmp;
1623
1624   pthread_mutex_lock (&am->vlib_rp->mutex);
1625   oldheap = svm_push_data_heap (am->vlib_rp);
1626
1627   /*
1628    * Keep the number of memcpy ops to a minimum (e.g. 1).
1629    */
1630   vec_validate (vector, 16384);
1631   vec_reset_length (vector);
1632
1633   /* $$$$ FIXME */
1634   vector = vlib_node_serialize (&vm->node_main, vector,
1635                                 (u32) ~ 0 /* all threads */ ,
1636                                 1 /* include nexts */ ,
1637                                 1 /* include stats */ );
1638
1639   svm_pop_heap (oldheap);
1640   pthread_mutex_unlock (&am->vlib_rp->mutex);
1641
1642   /* *INDENT-OFF* */
1643   REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
1644   ({
1645     rmp->reply_in_shmem = (uword) vector;
1646   }));
1647   /* *INDENT-ON* */
1648 }
1649
1650 static void
1651 vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1652 {
1653   int rv = 0;
1654   vl_api_ioam_enable_reply_t *rmp;
1655   clib_error_t *error;
1656
1657   /* Ignoring the profile id as currently a single profile
1658    * is supported */
1659   error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1660                            mp->seqno, mp->analyse);
1661   if (error)
1662     {
1663       clib_error_report (error);
1664       rv = clib_error_get_code (error);
1665     }
1666
1667   REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1668 }
1669
1670 static void
1671 vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1672 {
1673   int rv = 0;
1674   vl_api_ioam_disable_reply_t *rmp;
1675   clib_error_t *error;
1676
1677   error = clear_ioam_rewrite_fn ();
1678   if (error)
1679     {
1680       clib_error_report (error);
1681       rv = clib_error_get_code (error);
1682     }
1683
1684   REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1685 }
1686
1687 static void
1688 vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
1689 {
1690   vl_api_pg_create_interface_reply_t *rmp;
1691   int rv = 0;
1692
1693   pg_main_t *pg = &pg_main;
1694   u32 pg_if_id = pg_interface_add_or_get (pg, ntohl (mp->interface_id));
1695   pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
1696
1697   /* *INDENT-OFF* */
1698   REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
1699   ({
1700     rmp->sw_if_index = ntohl(pi->sw_if_index);
1701   }));
1702   /* *INDENT-ON* */
1703 }
1704
1705 static void
1706 vl_api_pg_capture_t_handler (vl_api_pg_capture_t * mp)
1707 {
1708   vl_api_pg_capture_reply_t *rmp;
1709   int rv = 0;
1710
1711   vnet_main_t *vnm = vnet_get_main ();
1712   vnet_interface_main_t *im = &vnm->interface_main;
1713   vnet_hw_interface_t *hi = 0;
1714
1715   u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
1716   u32 hw_if_index = ~0;
1717   uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
1718   if (p)
1719     hw_if_index = *p;
1720   vec_free (intf_name);
1721
1722   if (hw_if_index != ~0)
1723     {
1724       pg_capture_args_t _a, *a = &_a;
1725
1726       u32 len = ntohl (mp->pcap_name_length);
1727       u8 *pcap_file_name = vec_new (u8, len);
1728       clib_memcpy (pcap_file_name, mp->pcap_file_name, len);
1729
1730       hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
1731       a->hw_if_index = hw_if_index;
1732       a->dev_instance = hi->dev_instance;
1733       a->is_enabled = mp->is_enabled;
1734       a->pcap_file_name = pcap_file_name;
1735       a->count = ntohl (mp->count);
1736
1737       clib_error_t *e = pg_capture (a);
1738       if (e)
1739         {
1740           clib_error_report (e);
1741           rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
1742         }
1743
1744       vec_free (pcap_file_name);
1745     }
1746   REPLY_MACRO (VL_API_PG_CAPTURE_REPLY);
1747 }
1748
1749 static void
1750 vl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t * mp)
1751 {
1752   vl_api_pg_enable_disable_reply_t *rmp;
1753   int rv = 0;
1754
1755   pg_main_t *pg = &pg_main;
1756   u32 stream_index = ~0;
1757
1758   int is_enable = mp->is_enabled != 0;
1759   u32 len = ntohl (mp->stream_name_length) - 1;
1760
1761   if (len > 0)
1762     {
1763       u8 *stream_name = vec_new (u8, len);
1764       clib_memcpy (stream_name, mp->stream_name, len);
1765       uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
1766       if (p)
1767         stream_index = *p;
1768       vec_free (stream_name);
1769     }
1770
1771   pg_enable_disable (stream_index, is_enable);
1772
1773   REPLY_MACRO (VL_API_PG_ENABLE_DISABLE_REPLY);
1774 }
1775
1776 static void
1777   vl_api_ip_source_and_port_range_check_add_del_t_handler
1778   (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1779 {
1780   vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1781   int rv = 0;
1782
1783   u8 is_ipv6 = mp->is_ipv6;
1784   u8 is_add = mp->is_add;
1785   u8 mask_length = mp->mask_length;
1786   ip4_address_t ip4_addr;
1787   ip6_address_t ip6_addr;
1788   u16 *low_ports = 0;
1789   u16 *high_ports = 0;
1790   u32 vrf_id;
1791   u16 tmp_low, tmp_high;
1792   u8 num_ranges;
1793   int i;
1794
1795   // Validate port range
1796   num_ranges = mp->number_of_ranges;
1797   if (num_ranges > 32)
1798     {                           // This is size of array in VPE.API
1799       rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1800       goto reply;
1801     }
1802
1803   vec_reset_length (low_ports);
1804   vec_reset_length (high_ports);
1805
1806   for (i = 0; i < num_ranges; i++)
1807     {
1808       tmp_low = mp->low_ports[i];
1809       tmp_high = mp->high_ports[i];
1810       // If tmp_low <= tmp_high then only need to check tmp_low = 0
1811       // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1812       if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1813         {
1814           rv = VNET_API_ERROR_INVALID_VALUE;
1815           goto reply;
1816         }
1817       vec_add1 (low_ports, tmp_low);
1818       vec_add1 (high_ports, tmp_high + 1);
1819     }
1820
1821   // Validate mask_length
1822   if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
1823     {
1824       rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
1825       goto reply;
1826     }
1827
1828   vrf_id = ntohl (mp->vrf_id);
1829
1830   if (vrf_id < 1)
1831     {
1832       rv = VNET_API_ERROR_INVALID_VALUE;
1833       goto reply;
1834     }
1835
1836
1837   if (is_ipv6)
1838     {
1839       clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
1840       rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
1841                                                     mask_length,
1842                                                     vrf_id,
1843                                                     low_ports,
1844                                                     high_ports, is_add);
1845     }
1846   else
1847     {
1848       clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
1849       rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
1850                                                     mask_length,
1851                                                     vrf_id,
1852                                                     low_ports,
1853                                                     high_ports, is_add);
1854     }
1855
1856 reply:
1857   vec_free (low_ports);
1858   vec_free (high_ports);
1859   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
1860 }
1861
1862 static void
1863   vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
1864   (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
1865 {
1866   vlib_main_t *vm = vlib_get_main ();
1867   vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
1868   ip4_main_t *im = &ip4_main;
1869   int rv;
1870   u32 sw_if_index;
1871   u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1872   u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1873   uword *p = 0;
1874   int i;
1875
1876   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
1877     ntohl (mp->tcp_out_vrf_id);
1878   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
1879     ntohl (mp->udp_out_vrf_id);
1880   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
1881     ntohl (mp->tcp_in_vrf_id);
1882   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
1883     ntohl (mp->udp_in_vrf_id);
1884
1885
1886   for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
1887     {
1888       if (vrf_id[i] != 0 && vrf_id[i] != ~0)
1889         {
1890           p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
1891
1892           if (p == 0)
1893             {
1894               rv = VNET_API_ERROR_INVALID_VALUE;
1895               goto reply;
1896             }
1897
1898           fib_index[i] = p[0];
1899         }
1900       else
1901         fib_index[i] = ~0;
1902     }
1903   sw_if_index = ntohl (mp->sw_if_index);
1904
1905   VALIDATE_SW_IF_INDEX (mp);
1906
1907   rv =
1908     set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
1909                                         mp->is_add);
1910
1911   BAD_SW_IF_INDEX_LABEL;
1912 reply:
1913
1914   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
1915 }
1916
1917 static void
1918 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
1919 {
1920   vl_api_delete_subif_reply_t *rmp;
1921   int rv;
1922
1923   rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
1924
1925   REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
1926 }
1927
1928 static void
1929 vl_api_punt_t_handler (vl_api_punt_t * mp)
1930 {
1931   vl_api_punt_reply_t *rmp;
1932   vlib_main_t *vm = vlib_get_main ();
1933   int rv = 0;
1934   clib_error_t *error;
1935
1936   error = vnet_punt_add_del (vm, mp->ipv, mp->l4_protocol,
1937                              ntohs (mp->l4_port), mp->is_add);
1938   if (error)
1939     {
1940       rv = -1;
1941       clib_error_report (error);
1942     }
1943
1944   REPLY_MACRO (VL_API_PUNT_REPLY);
1945 }
1946
1947 static void
1948 vl_api_feature_enable_disable_t_handler (vl_api_feature_enable_disable_t * mp)
1949 {
1950   vl_api_feature_enable_disable_reply_t *rmp;
1951   int rv = 0;
1952
1953   VALIDATE_SW_IF_INDEX (mp);
1954
1955   u8 *arc_name = format (0, "%s%c", mp->arc_name, 0);
1956   u8 *feature_name = format (0, "%s%c", mp->feature_name, 0);
1957
1958   vnet_feature_registration_t *reg =
1959     vnet_get_feature_reg ((const char *) arc_name,
1960                           (const char *) feature_name);
1961   if (reg == 0)
1962     rv = VNET_API_ERROR_INVALID_VALUE;
1963   else
1964     {
1965       u32 sw_if_index = ntohl (mp->sw_if_index);
1966       clib_error_t *error = 0;
1967
1968       if (reg->enable_disable_cb)
1969         error = reg->enable_disable_cb (sw_if_index, mp->enable);
1970       if (!error)
1971         vnet_feature_enable_disable ((const char *) arc_name,
1972                                      (const char *) feature_name,
1973                                      sw_if_index, mp->enable, 0, 0);
1974       else
1975         {
1976           clib_error_report (error);
1977           rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
1978         }
1979     }
1980
1981   vec_free (feature_name);
1982   vec_free (arc_name);
1983
1984   BAD_SW_IF_INDEX_LABEL;
1985
1986   REPLY_MACRO (VL_API_FEATURE_ENABLE_DISABLE_REPLY);
1987 }
1988
1989 #define BOUNCE_HANDLER(nn)                                              \
1990 static void vl_api_##nn##_t_handler (                                   \
1991     vl_api_##nn##_t *mp)                                                \
1992 {                                                                       \
1993     vpe_client_registration_t *reg;                                     \
1994     vpe_api_main_t * vam = &vpe_api_main;                               \
1995     unix_shared_memory_queue_t * q;                                     \
1996                                                                         \
1997     /* One registration only... */                                      \
1998     pool_foreach(reg, vam->nn##_registrations,                          \
1999     ({                                                                  \
2000         q = vl_api_client_index_to_input_queue (reg->client_index);     \
2001         if (q) {                                                        \
2002             /*                                                          \
2003              * If the queue is stuffed, turf the msg and complain       \
2004              * It's unlikely that the intended recipient is             \
2005              * alive; avoid deadlock at all costs.                      \
2006              */                                                         \
2007             if (q->cursize == q->maxsize) {                             \
2008                 clib_warning ("ERROR: receiver queue full, drop msg");  \
2009                 vl_msg_api_free (mp);                                   \
2010                 return;                                                 \
2011             }                                                           \
2012             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
2013             return;                                                     \
2014         }                                                               \
2015     }));                                                                \
2016     vl_msg_api_free (mp);                                               \
2017 }
2018
2019 static void setup_message_id_table (api_main_t * am);
2020
2021 /*
2022  * vpe_api_hookup
2023  * Add vpe's API message handlers to the table.
2024  * vlib has alread mapped shared memory and
2025  * added the client registration handlers.
2026  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
2027  */
2028 static clib_error_t *
2029 vpe_api_hookup (vlib_main_t * vm)
2030 {
2031   api_main_t *am = &api_main;
2032
2033 #define _(N,n)                                                  \
2034     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
2035                            vl_api_##n##_t_handler,              \
2036                            vl_noop_handler,                     \
2037                            vl_api_##n##_t_endian,               \
2038                            vl_api_##n##_t_print,                \
2039                            sizeof(vl_api_##n##_t), 1);
2040   foreach_vpe_api_msg;
2041 #undef _
2042
2043   /*
2044    * Trace space for classifier mask+match
2045    */
2046   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
2047   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size
2048     += 5 * sizeof (u32x4);
2049
2050   /*
2051    * Thread-safe API messages
2052    */
2053   am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
2054   am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
2055
2056   /*
2057    * Set up the (msg_name, crc, message-id) table
2058    */
2059   setup_message_id_table (am);
2060
2061   return 0;
2062 }
2063
2064 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
2065
2066 static clib_error_t *
2067 vpe_api_init (vlib_main_t * vm)
2068 {
2069   vpe_api_main_t *am = &vpe_api_main;
2070
2071   am->vlib_main = vm;
2072   am->vnet_main = vnet_get_main ();
2073   am->interface_events_registration_hash = hash_create (0, sizeof (uword));
2074   am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
2075   am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
2076   am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
2077   am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
2078   am->oam_events_registration_hash = hash_create (0, sizeof (uword));
2079   am->bfd_events_registration_hash = hash_create (0, sizeof (uword));
2080
2081   vl_set_memory_region_name ("/vpe-api");
2082   vl_enable_disable_memory_api (vm, 1 /* enable it */ );
2083
2084   return 0;
2085 }
2086
2087 VLIB_INIT_FUNCTION (vpe_api_init);
2088
2089
2090 static clib_error_t *
2091 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
2092 {
2093   u8 *chroot_path;
2094   u64 baseva, size, pvt_heap_size;
2095   int uid, gid, rv;
2096   const int max_buf_size = 4096;
2097   char *s, *buf;
2098   struct passwd _pw, *pw;
2099   struct group _grp, *grp;
2100   clib_error_t *e;
2101   buf = vec_new (char, 128);
2102   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2103     {
2104       if (unformat (input, "prefix %s", &chroot_path))
2105         {
2106           vec_add1 (chroot_path, 0);
2107           vl_set_memory_root_path ((char *) chroot_path);
2108         }
2109       else if (unformat (input, "uid %d", &uid))
2110         vl_set_memory_uid (uid);
2111       else if (unformat (input, "gid %d", &gid))
2112         vl_set_memory_gid (gid);
2113       else if (unformat (input, "baseva %llx", &baseva))
2114         vl_set_global_memory_baseva (baseva);
2115       else if (unformat (input, "global-size %lldM", &size))
2116         vl_set_global_memory_size (size * (1ULL << 20));
2117       else if (unformat (input, "global-size %lldG", &size))
2118         vl_set_global_memory_size (size * (1ULL << 30));
2119       else if (unformat (input, "global-size %lld", &size))
2120         vl_set_global_memory_size (size);
2121       else if (unformat (input, "global-pvt-heap-size %lldM", &pvt_heap_size))
2122         vl_set_global_pvt_heap_size (pvt_heap_size * (1ULL << 20));
2123       else if (unformat (input, "global-pvt-heap-size size %lld",
2124                          &pvt_heap_size))
2125         vl_set_global_pvt_heap_size (pvt_heap_size);
2126       else if (unformat (input, "api-pvt-heap-size %lldM", &pvt_heap_size))
2127         vl_set_api_pvt_heap_size (pvt_heap_size * (1ULL << 20));
2128       else if (unformat (input, "api-pvt-heap-size size %lld",
2129                          &pvt_heap_size))
2130         vl_set_api_pvt_heap_size (pvt_heap_size);
2131       else if (unformat (input, "api-size %lldM", &size))
2132         vl_set_api_memory_size (size * (1ULL << 20));
2133       else if (unformat (input, "api-size %lldG", &size))
2134         vl_set_api_memory_size (size * (1ULL << 30));
2135       else if (unformat (input, "api-size %lld", &size))
2136         vl_set_api_memory_size (size);
2137       else if (unformat (input, "uid %s", &s))
2138         {
2139           /* lookup the username */
2140           pw = NULL;
2141           while (((rv =
2142                    getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
2143                  && (vec_len (buf) <= max_buf_size))
2144             {
2145               vec_resize (buf, vec_len (buf) * 2);
2146             }
2147           if (rv < 0)
2148             {
2149               e = clib_error_return_code (0, rv,
2150                                           CLIB_ERROR_ERRNO_VALID |
2151                                           CLIB_ERROR_FATAL,
2152                                           "cannot fetch username %s", s);
2153               vec_free (s);
2154               vec_free (buf);
2155               return e;
2156             }
2157           if (pw == NULL)
2158             {
2159               e =
2160                 clib_error_return_fatal (0, "username %s does not exist", s);
2161               vec_free (s);
2162               vec_free (buf);
2163               return e;
2164             }
2165           vec_free (s);
2166           vl_set_memory_uid (pw->pw_uid);
2167         }
2168       else if (unformat (input, "gid %s", &s))
2169         {
2170           /* lookup the group name */
2171           grp = NULL;
2172           while (((rv =
2173                    getgrnam_r (s, &_grp, buf, vec_len (buf), &grp)) == ERANGE)
2174                  && (vec_len (buf) <= max_buf_size))
2175             {
2176               vec_resize (buf, vec_len (buf) * 2);
2177             }
2178           if (rv != 0)
2179             {
2180               e = clib_error_return_code (0, rv,
2181                                           CLIB_ERROR_ERRNO_VALID |
2182                                           CLIB_ERROR_FATAL,
2183                                           "cannot fetch group %s", s);
2184               vec_free (s);
2185               vec_free (buf);
2186               return e;
2187             }
2188           if (grp == NULL)
2189             {
2190               e = clib_error_return_fatal (0, "group %s does not exist", s);
2191               vec_free (s);
2192               vec_free (buf);
2193               return e;
2194             }
2195           vec_free (s);
2196           vec_free (buf);
2197           vl_set_memory_gid (grp->gr_gid);
2198         }
2199       else
2200         return clib_error_return (0, "unknown input `%U'",
2201                                   format_unformat_error, input);
2202     }
2203   return 0;
2204 }
2205
2206 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
2207
2208 void *
2209 get_unformat_vnet_sw_interface (void)
2210 {
2211   return (void *) &unformat_vnet_sw_interface;
2212 }
2213
2214 static u8 *
2215 format_arp_event (u8 * s, va_list * args)
2216 {
2217   vl_api_ip4_arp_event_t *event = va_arg (*args, vl_api_ip4_arp_event_t *);
2218
2219   s = format (s, "pid %d: ", ntohl (event->pid));
2220   if (event->mac_ip)
2221     s = format (s, "bd mac/ip4 binding events");
2222   else
2223     s = format (s, "resolution for %U", format_ip4_address, &event->address);
2224   return s;
2225 }
2226
2227 static u8 *
2228 format_nd_event (u8 * s, va_list * args)
2229 {
2230   vl_api_ip6_nd_event_t *event = va_arg (*args, vl_api_ip6_nd_event_t *);
2231
2232   s = format (s, "pid %d: ", ntohl (event->pid));
2233   if (event->mac_ip)
2234     s = format (s, "bd mac/ip6 binding events");
2235   else
2236     s = format (s, "resolution for %U", format_ip6_address, event->address);
2237   return s;
2238 }
2239
2240 static clib_error_t *
2241 show_ip_arp_nd_events_fn (vlib_main_t * vm,
2242                           unformat_input_t * input, vlib_cli_command_t * cmd)
2243 {
2244   vpe_api_main_t *am = &vpe_api_main;
2245   vl_api_ip4_arp_event_t *arp_event;
2246   vl_api_ip6_nd_event_t *nd_event;
2247
2248   if ((pool_elts (am->arp_events) == 0) && (pool_elts (am->nd_events) == 0))
2249     {
2250       vlib_cli_output (vm, "No active arp or nd event registrations");
2251       return 0;
2252     }
2253
2254   /* *INDENT-OFF* */
2255   pool_foreach (arp_event, am->arp_events,
2256   ({
2257     vlib_cli_output (vm, "%U", format_arp_event, arp_event);
2258   }));
2259
2260   pool_foreach (nd_event, am->nd_events,
2261   ({
2262     vlib_cli_output (vm, "%U", format_nd_event, nd_event);
2263   }));
2264   /* *INDENT-ON* */
2265
2266   return 0;
2267 }
2268
2269 /* *INDENT-OFF* */
2270 VLIB_CLI_COMMAND (show_ip_arp_nd_events, static) = {
2271   .path = "show arp-nd-event registrations",
2272   .function = show_ip_arp_nd_events_fn,
2273   .short_help = "Show ip4 arp and ip6 nd event registrations",
2274 };
2275 /* *INDENT-ON* */
2276
2277 #define vl_msg_name_crc_list
2278 #include <vpp/api/vpe_all_api_h.h>
2279 #undef vl_msg_name_crc_list
2280
2281 static void
2282 setup_message_id_table (api_main_t * am)
2283 {
2284 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
2285   foreach_vl_msg_name_crc_memclnt;
2286   foreach_vl_msg_name_crc_vpe;
2287 #undef _
2288 }
2289
2290
2291 /*
2292  * fd.io coding-style-patch-verification: ON
2293  *
2294  * Local Variables:
2295  * eval: (c-set-style "gnu")
2296  * End:
2297  */