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