Enforce Bridge Domain ID range to match 24-bit VNI range
[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 _(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 extern 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 static 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   vlib_main_t *vm = vlib_get_main ();
414   vnet_main_t *vnm = vnet_get_main ();
415
416   VALIDATE_RX_SW_IF_INDEX (mp);
417   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
418
419
420   if (mp->enable)
421     {
422       VALIDATE_BD_ID (mp);
423       u32 bd_id = ntohl (mp->bd_id);
424       u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
425       u32 bvi = mp->bvi;
426       u8 shg = mp->shg;
427       rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
428                             rx_sw_if_index, bd_index, bvi, shg, 0);
429     }
430   else
431     {
432       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
433     }
434
435   BAD_RX_SW_IF_INDEX_LABEL;
436   BAD_BD_ID_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   if (bd_id == 0)
452     {
453       rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
454       goto out;
455     }
456
457   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
458   if (p == 0)
459     {
460       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
461       goto out;
462     }
463
464   bd_index = p[0];
465   if (bd_add_del_ip_mac (bd_index, mp->ip_address,
466                          mp->mac_address, mp->is_ipv6, mp->is_add))
467     rv = VNET_API_ERROR_UNSPECIFIED;
468
469 out:
470   REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
471 }
472
473 static void
474 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
475 {
476   vl_api_create_vlan_subif_reply_t *rmp;
477   vnet_main_t *vnm = vnet_get_main ();
478   u32 sw_if_index = (u32) ~ 0;
479   vnet_hw_interface_t *hi;
480   int rv = 0;
481   u32 id;
482   vnet_sw_interface_t template;
483   uword *p;
484   vnet_interface_main_t *im = &vnm->interface_main;
485   u64 sup_and_sub_key;
486   unix_shared_memory_queue_t *q;
487   clib_error_t *error;
488
489   VALIDATE_SW_IF_INDEX (mp);
490
491   hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
492
493   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
494     {
495       rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
496       goto out;
497     }
498
499   id = ntohl (mp->vlan_id);
500   if (id == 0 || id > 4095)
501     {
502       rv = VNET_API_ERROR_INVALID_VLAN;
503       goto out;
504     }
505
506   sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
507
508   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
509   if (p)
510     {
511       rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
512       goto out;
513     }
514
515   memset (&template, 0, sizeof (template));
516   template.type = VNET_SW_INTERFACE_TYPE_SUB;
517   template.sup_sw_if_index = hi->sw_if_index;
518   template.sub.id = id;
519   template.sub.eth.raw_flags = 0;
520   template.sub.eth.flags.one_tag = 1;
521   template.sub.eth.outer_vlan_id = id;
522   template.sub.eth.flags.exact_match = 1;
523
524   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
525   if (error)
526     {
527       clib_error_report (error);
528       rv = VNET_API_ERROR_INVALID_REGISTRATION;
529       goto out;
530     }
531
532   u64 *kp = clib_mem_alloc (sizeof (*kp));
533   *kp = sup_and_sub_key;
534
535   hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
536   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
537
538   BAD_SW_IF_INDEX_LABEL;
539
540 out:
541   q = vl_api_client_index_to_input_queue (mp->client_index);
542   if (!q)
543     return;
544
545   rmp = vl_msg_api_alloc (sizeof (*rmp));
546   rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
547   rmp->context = mp->context;
548   rmp->retval = htonl (rv);
549   rmp->sw_if_index = htonl (sw_if_index);
550   vl_msg_api_send_shmem (q, (u8 *) & rmp);
551 }
552
553 static void
554 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
555 {
556   vl_api_create_subif_reply_t *rmp;
557   vnet_main_t *vnm = vnet_get_main ();
558   u32 sw_if_index = ~0;
559   int rv = 0;
560   u32 sub_id;
561   vnet_sw_interface_t *si;
562   vnet_hw_interface_t *hi;
563   vnet_sw_interface_t template;
564   uword *p;
565   vnet_interface_main_t *im = &vnm->interface_main;
566   u64 sup_and_sub_key;
567   clib_error_t *error;
568
569   VALIDATE_SW_IF_INDEX (mp);
570
571   si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
572   hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
573
574   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
575     {
576       rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
577       goto out;
578     }
579
580   sw_if_index = si->sw_if_index;
581   sub_id = ntohl (mp->sub_id);
582
583   sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
584
585   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
586   if (p)
587     {
588       if (CLIB_DEBUG > 0)
589         clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
590                       sw_if_index, sub_id);
591       rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
592       goto out;
593     }
594
595   memset (&template, 0, sizeof (template));
596   template.type = VNET_SW_INTERFACE_TYPE_SUB;
597   template.sup_sw_if_index = sw_if_index;
598   template.sub.id = sub_id;
599   template.sub.eth.flags.no_tags = mp->no_tags;
600   template.sub.eth.flags.one_tag = mp->one_tag;
601   template.sub.eth.flags.two_tags = mp->two_tags;
602   template.sub.eth.flags.dot1ad = mp->dot1ad;
603   template.sub.eth.flags.exact_match = mp->exact_match;
604   template.sub.eth.flags.default_sub = mp->default_sub;
605   template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
606   template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
607   template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
608   template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
609
610   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
611   if (error)
612     {
613       clib_error_report (error);
614       rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
615       goto out;
616     }
617
618   u64 *kp = clib_mem_alloc (sizeof (*kp));
619   *kp = sup_and_sub_key;
620
621   hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
622   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
623
624   BAD_SW_IF_INDEX_LABEL;
625
626 out:
627
628   /* *INDENT-OFF* */
629   REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
630   ({
631     rmp->sw_if_index = ntohl(sw_if_index);
632   }));
633   /* *INDENT-ON* */
634 }
635
636 static void
637 vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
638 {
639   vl_api_proxy_arp_add_del_reply_t *rmp;
640   u32 fib_index;
641   int rv;
642   ip4_main_t *im = &ip4_main;
643   stats_main_t *sm = &stats_main;
644   int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
645                               ip4_address_t * hi_addr,
646                               u32 fib_index, int is_del);
647   uword *p;
648
649   dslock (sm, 1 /* release hint */ , 6 /* tag */ );
650
651   p = hash_get (im->fib_index_by_table_id, ntohl (mp->vrf_id));
652
653   if (!p)
654     {
655       rv = VNET_API_ERROR_NO_SUCH_FIB;
656       goto out;
657     }
658
659   fib_index = p[0];
660
661   rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->low_address,
662                                (ip4_address_t *) mp->hi_address,
663                                fib_index, mp->is_add == 0);
664
665 out:
666   dsunlock (sm);
667   REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
668 }
669
670 static void
671   vl_api_proxy_arp_intfc_enable_disable_t_handler
672   (vl_api_proxy_arp_intfc_enable_disable_t * mp)
673 {
674   int rv = 0;
675   vnet_main_t *vnm = vnet_get_main ();
676   vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
677
678   VALIDATE_SW_IF_INDEX (mp);
679
680   vnet_sw_interface_t *si =
681     vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
682
683   ASSERT (si);
684
685   if (mp->enable_disable)
686     si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
687   else
688     si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
689
690   BAD_SW_IF_INDEX_LABEL;
691
692 out:
693   REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
694 }
695
696 static void
697 vl_api_is_address_reachable_t_handler (vl_api_is_address_reachable_t * mp)
698 {
699 #if 0
700   vpe_main_t *rm = &vpe_main;
701   ip4_main_t *im4 = &ip4_main;
702   ip6_main_t *im6 = &ip6_main;
703   ip_lookup_main_t *lm;
704   union
705   {
706     ip4_address_t ip4;
707     ip6_address_t ip6;
708   } addr;
709   u32 adj_index, sw_if_index;
710   vl_api_is_address_reachable_t *rmp;
711   ip_adjacency_t *adj;
712   unix_shared_memory_queue_t *q;
713
714   q = vl_api_client_index_to_input_queue (mp->client_index);
715   if (!q)
716     {
717       increment_missing_api_client_counter (rm->vlib_main);
718       return;
719     }
720
721   rmp = vl_msg_api_alloc (sizeof (*rmp));
722   clib_memcpy (rmp, mp, sizeof (*rmp));
723
724   sw_if_index = mp->next_hop_sw_if_index;
725   clib_memcpy (&addr, mp->address, sizeof (addr));
726   if (mp->is_ipv6)
727     {
728       lm = &im6->lookup_main;
729       adj_index = ip6_fib_lookup (im6, sw_if_index, &addr.ip6);
730     }
731   else
732     {
733       lm = &im4->lookup_main;
734       // FIXME NOT an ADJ
735       adj_index = ip4_fib_lookup (im4, sw_if_index, &addr.ip4);
736     }
737   if (adj_index == ~0)
738     {
739       rmp->is_error = 1;
740       goto send;
741     }
742   adj = ip_get_adjacency (lm, adj_index);
743
744   if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE
745       && adj->rewrite_header.sw_if_index == sw_if_index)
746     {
747       rmp->is_known = 1;
748     }
749   else
750     {
751       if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP
752           && adj->rewrite_header.sw_if_index == sw_if_index)
753         {
754           if (mp->is_ipv6)
755             ip6_probe_neighbor (rm->vlib_main, &addr.ip6, sw_if_index);
756           else
757             ip4_probe_neighbor (rm->vlib_main, &addr.ip4, sw_if_index);
758         }
759       else if (adj->lookup_next_index == IP_LOOKUP_NEXT_DROP)
760         {
761           rmp->is_known = 1;
762           goto send;
763         }
764       rmp->is_known = 0;
765     }
766
767 send:
768   vl_msg_api_send_shmem (q, (u8 *) & rmp);
769 #endif
770 }
771
772 static void
773   vl_api_sw_interface_set_mpls_enable_t_handler
774   (vl_api_sw_interface_set_mpls_enable_t * mp)
775 {
776   vl_api_sw_interface_set_mpls_enable_reply_t *rmp;
777   int rv = 0;
778
779   VALIDATE_SW_IF_INDEX (mp);
780
781   mpls_sw_interface_enable_disable (&mpls_main,
782                                     ntohl (mp->sw_if_index), mp->enable);
783
784   BAD_SW_IF_INDEX_LABEL;
785   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MPLS_ENABLE_REPLY);
786 }
787
788 void
789 send_oam_event (oam_target_t * t)
790 {
791   vpe_api_main_t *vam = &vpe_api_main;
792   unix_shared_memory_queue_t *q;
793   vpe_client_registration_t *reg;
794   vl_api_oam_event_t *mp;
795
796   /* *INDENT-OFF* */
797   pool_foreach(reg, vam->oam_events_registrations,
798   ({
799     q = vl_api_client_index_to_input_queue (reg->client_index);
800     if (q)
801       {
802         mp = vl_msg_api_alloc (sizeof (*mp));
803         mp->_vl_msg_id = ntohs (VL_API_OAM_EVENT);
804         clib_memcpy (mp->dst_address, &t->dst_address,
805                      sizeof (mp->dst_address));
806         mp->state = t->state;
807         vl_msg_api_send_shmem (q, (u8 *)&mp);
808       }
809   }));
810   /* *INDENT-ON* */
811 }
812
813 static void
814 vl_api_oam_add_del_t_handler (vl_api_oam_add_del_t * mp)
815 {
816   vl_api_oam_add_del_reply_t *rmp;
817   int rv;
818
819   rv = vpe_oam_add_del_target ((ip4_address_t *) mp->src_address,
820                                (ip4_address_t *) mp->dst_address,
821                                ntohl (mp->vrf_id), (int) (mp->is_add));
822
823   REPLY_MACRO (VL_API_OAM_ADD_DEL_REPLY);
824 }
825
826 static void
827 vl_api_vnet_get_summary_stats_t_handler (vl_api_vnet_get_summary_stats_t * mp)
828 {
829   stats_main_t *sm = &stats_main;
830   vnet_interface_main_t *im = sm->interface_main;
831   vl_api_vnet_summary_stats_reply_t *rmp;
832   vlib_combined_counter_main_t *cm;
833   vlib_counter_t v;
834   int i, which;
835   u64 total_pkts[VLIB_N_RX_TX];
836   u64 total_bytes[VLIB_N_RX_TX];
837
838   unix_shared_memory_queue_t *q =
839     vl_api_client_index_to_input_queue (mp->client_index);
840
841   if (!q)
842     return;
843
844   rmp = vl_msg_api_alloc (sizeof (*rmp));
845   rmp->_vl_msg_id = ntohs (VL_API_VNET_SUMMARY_STATS_REPLY);
846   rmp->context = mp->context;
847   rmp->retval = 0;
848
849   memset (total_pkts, 0, sizeof (total_pkts));
850   memset (total_bytes, 0, sizeof (total_bytes));
851
852   vnet_interface_counter_lock (im);
853
854   vec_foreach (cm, im->combined_sw_if_counters)
855   {
856     which = cm - im->combined_sw_if_counters;
857
858     for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
859       {
860         vlib_get_combined_counter (cm, i, &v);
861         total_pkts[which] += v.packets;
862         total_bytes[which] += v.bytes;
863       }
864   }
865   vnet_interface_counter_unlock (im);
866
867   rmp->total_pkts[VLIB_RX] = clib_host_to_net_u64 (total_pkts[VLIB_RX]);
868   rmp->total_bytes[VLIB_RX] = clib_host_to_net_u64 (total_bytes[VLIB_RX]);
869   rmp->total_pkts[VLIB_TX] = clib_host_to_net_u64 (total_pkts[VLIB_TX]);
870   rmp->total_bytes[VLIB_TX] = clib_host_to_net_u64 (total_bytes[VLIB_TX]);
871   rmp->vector_rate =
872     clib_host_to_net_u64 (vlib_last_vector_length_per_node (sm->vlib_main));
873
874   vl_msg_api_send_shmem (q, (u8 *) & rmp);
875 }
876
877 /* *INDENT-OFF* */
878 typedef CLIB_PACKED (struct {
879   ip4_address_t address;
880   u32 address_length: 6;
881   u32 index:26;
882 }) ip4_route_t;
883 /* *INDENT-ON* */
884
885 static int
886 ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
887 {
888   vnet_main_t *vnm = vnet_get_main ();
889   vnet_interface_main_t *im = &vnm->interface_main;
890   ip4_main_t *im4 = &ip4_main;
891   static u32 *sw_if_indices_to_shut;
892   stats_main_t *sm = &stats_main;
893   fib_table_t *fib_table;
894   ip4_fib_t *fib;
895   u32 sw_if_index;
896   int i;
897   int rv = VNET_API_ERROR_NO_SUCH_FIB;
898   u32 target_fib_id = ntohl (mp->vrf_id);
899
900   dslock (sm, 1 /* release hint */ , 8 /* tag */ );
901
902   /* *INDENT-OFF* */
903   pool_foreach (fib_table, im4->fibs,
904   ({
905     vnet_sw_interface_t * si;
906
907     fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
908
909     if (fib->table_id != target_fib_id)
910       continue;
911
912     /* remove any mpls encap/decap labels */
913     mpls_fib_reset_labels (fib->table_id);
914
915     /* remove any proxy arps in this fib */
916     vnet_proxy_arp_fib_reset (fib->table_id);
917
918     /* Set the flow hash for this fib to the default */
919     vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
920
921     vec_reset_length (sw_if_indices_to_shut);
922
923     /* Shut down interfaces in this FIB / clean out intfc routes */
924     pool_foreach (si, im->sw_interfaces,
925     ({
926       u32 sw_if_index = si->sw_if_index;
927
928       if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
929           && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
930               fib->index))
931         vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
932     }));
933
934     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
935       sw_if_index = sw_if_indices_to_shut[i];
936
937       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
938       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
939       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
940     }
941
942     fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
943
944     rv = 0;
945     break;
946     })); /* pool_foreach (fib) */
947     /* *INDENT-ON* */
948
949   dsunlock (sm);
950   return rv;
951 }
952
953 static int
954 ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
955 {
956   vnet_main_t *vnm = vnet_get_main ();
957   vnet_interface_main_t *im = &vnm->interface_main;
958   ip6_main_t *im6 = &ip6_main;
959   stats_main_t *sm = &stats_main;
960   static u32 *sw_if_indices_to_shut;
961   fib_table_t *fib_table;
962   ip6_fib_t *fib;
963   u32 sw_if_index;
964   int i;
965   int rv = VNET_API_ERROR_NO_SUCH_FIB;
966   u32 target_fib_id = ntohl (mp->vrf_id);
967
968   dslock (sm, 1 /* release hint */ , 9 /* tag */ );
969
970   /* *INDENT-OFF* */
971   pool_foreach (fib_table, im6->fibs,
972   ({
973     vnet_sw_interface_t * si;
974
975     fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
976
977     if (fib->table_id != target_fib_id)
978       continue;
979
980     vec_reset_length (sw_if_indices_to_shut);
981
982     /* Set the flow hash for this fib to the default */
983     vnet_set_ip6_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
984
985     /* Shut down interfaces in this FIB / clean out intfc routes */
986     pool_foreach (si, im->sw_interfaces,
987     ({
988       if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
989           fib->index)
990         vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
991     }));
992
993     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
994       sw_if_index = sw_if_indices_to_shut[i];
995
996       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
997       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
998       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
999     }
1000
1001     fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
1002
1003     rv = 0;
1004     break;
1005   })); /* pool_foreach (fib) */
1006   /* *INDENT-ON* */
1007
1008   dsunlock (sm);
1009   return rv;
1010 }
1011
1012 static void
1013 vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
1014 {
1015   int rv;
1016   vl_api_reset_fib_reply_t *rmp;
1017
1018   if (mp->is_ipv6)
1019     rv = ip6_reset_fib_t_handler (mp);
1020   else
1021     rv = ip4_reset_fib_t_handler (mp);
1022
1023   REPLY_MACRO (VL_API_RESET_FIB_REPLY);
1024 }
1025
1026 static void
1027 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
1028 {
1029   vl_api_create_loopback_reply_t *rmp;
1030   u32 sw_if_index;
1031   int rv;
1032
1033   rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address, 0, 0);
1034
1035   /* *INDENT-OFF* */
1036   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
1037   ({
1038     rmp->sw_if_index = ntohl (sw_if_index);
1039   }));
1040   /* *INDENT-ON* */
1041 }
1042
1043 static void vl_api_create_loopback_instance_t_handler
1044   (vl_api_create_loopback_instance_t * mp)
1045 {
1046   vl_api_create_loopback_instance_reply_t *rmp;
1047   u32 sw_if_index;
1048   u8 is_specified = mp->is_specified;
1049   u32 user_instance = ntohl (mp->user_instance);
1050   int rv;
1051
1052   rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address,
1053                                        is_specified, user_instance);
1054
1055   /* *INDENT-OFF* */
1056   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY,
1057   ({
1058     rmp->sw_if_index = ntohl (sw_if_index);
1059   }));
1060   /* *INDENT-ON* */
1061 }
1062
1063 static void
1064 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
1065 {
1066   vl_api_delete_loopback_reply_t *rmp;
1067   u32 sw_if_index;
1068   int rv;
1069
1070   sw_if_index = ntohl (mp->sw_if_index);
1071   rv = vnet_delete_loopback_interface (sw_if_index);
1072
1073   REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
1074 }
1075
1076 static void
1077 vl_api_control_ping_t_handler (vl_api_control_ping_t * mp)
1078 {
1079   vl_api_control_ping_reply_t *rmp;
1080   int rv = 0;
1081
1082   /* *INDENT-OFF* */
1083   REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
1084   ({
1085     rmp->vpe_pid = ntohl (getpid());
1086   }));
1087   /* *INDENT-ON* */
1088 }
1089
1090 static void
1091 shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
1092 {
1093   u8 **shmem_vecp = (u8 **) arg;
1094   u8 *shmem_vec;
1095   void *oldheap;
1096   api_main_t *am = &api_main;
1097   u32 offset;
1098
1099   shmem_vec = *shmem_vecp;
1100
1101   offset = vec_len (shmem_vec);
1102
1103   pthread_mutex_lock (&am->vlib_rp->mutex);
1104   oldheap = svm_push_data_heap (am->vlib_rp);
1105
1106   vec_validate (shmem_vec, offset + buffer_bytes - 1);
1107
1108   clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
1109
1110   svm_pop_heap (oldheap);
1111   pthread_mutex_unlock (&am->vlib_rp->mutex);
1112
1113   *shmem_vecp = shmem_vec;
1114 }
1115
1116
1117 static void
1118 vl_api_cli_request_t_handler (vl_api_cli_request_t * mp)
1119 {
1120   vl_api_cli_reply_t *rp;
1121   unix_shared_memory_queue_t *q;
1122   vlib_main_t *vm = vlib_get_main ();
1123   api_main_t *am = &api_main;
1124   unformat_input_t input;
1125   u8 *shmem_vec = 0;
1126   void *oldheap;
1127
1128   q = vl_api_client_index_to_input_queue (mp->client_index);
1129   if (!q)
1130     return;
1131
1132   rp = vl_msg_api_alloc (sizeof (*rp));
1133   rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY);
1134   rp->context = mp->context;
1135
1136   unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
1137
1138   vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
1139
1140   pthread_mutex_lock (&am->vlib_rp->mutex);
1141   oldheap = svm_push_data_heap (am->vlib_rp);
1142
1143   vec_add1 (shmem_vec, 0);
1144
1145   svm_pop_heap (oldheap);
1146   pthread_mutex_unlock (&am->vlib_rp->mutex);
1147
1148   rp->reply_in_shmem = (uword) shmem_vec;
1149
1150   vl_msg_api_send_shmem (q, (u8 *) & rp);
1151 }
1152
1153 static void
1154 inband_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
1155 {
1156   u8 **mem_vecp = (u8 **) arg;
1157   u8 *mem_vec = *mem_vecp;
1158   u32 offset = vec_len (mem_vec);
1159
1160   vec_validate (mem_vec, offset + buffer_bytes - 1);
1161   clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
1162   *mem_vecp = mem_vec;
1163 }
1164
1165 static void
1166 vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
1167 {
1168   vl_api_cli_inband_reply_t *rmp;
1169   int rv = 0;
1170   unix_shared_memory_queue_t *q;
1171   vlib_main_t *vm = vlib_get_main ();
1172   unformat_input_t input;
1173   u8 *out_vec = 0;
1174
1175   q = vl_api_client_index_to_input_queue (mp->client_index);
1176   if (!q)
1177     return;
1178
1179   unformat_init_string (&input, (char *) mp->cmd, ntohl (mp->length));
1180   vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
1181
1182   u32 len = vec_len (out_vec);
1183   /* *INDENT-OFF* */
1184   REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, len,
1185   ({
1186     rmp->length = htonl (len);
1187     clib_memcpy (rmp->reply, out_vec, len);
1188   }));
1189   /* *INDENT-ON* */
1190   vec_free (out_vec);
1191 }
1192
1193 static void
1194 vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
1195 {
1196   int rv;
1197   vl_api_set_arp_neighbor_limit_reply_t *rmp;
1198   vnet_main_t *vnm = vnet_get_main ();
1199   clib_error_t *error;
1200
1201   vnm->api_errno = 0;
1202
1203   if (mp->is_ipv6)
1204     error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
1205   else
1206     error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
1207
1208   if (error)
1209     {
1210       clib_error_report (error);
1211       rv = VNET_API_ERROR_UNSPECIFIED;
1212     }
1213   else
1214     {
1215       rv = vnm->api_errno;
1216     }
1217
1218   REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
1219 }
1220
1221 static void vl_api_classify_set_interface_ip_table_t_handler
1222   (vl_api_classify_set_interface_ip_table_t * mp)
1223 {
1224   vlib_main_t *vm = vlib_get_main ();
1225   vl_api_classify_set_interface_ip_table_reply_t *rmp;
1226   int rv;
1227
1228   VALIDATE_SW_IF_INDEX (mp);
1229
1230   u32 table_index = ntohl (mp->table_index);
1231   u32 sw_if_index = ntohl (mp->sw_if_index);
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   int rv;
1584
1585   if (mp->enable_disable)
1586     {
1587       vl_api_ip4_arp_event_t *event;
1588       pool_get (am->arp_events, event);
1589       rv = vnet_add_del_ip4_arp_change_event
1590         (vnm, arp_change_data_callback,
1591          mp->pid, &mp->address /* addr, in net byte order */ ,
1592          vpe_resolver_process_node.index,
1593          IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
1594
1595       if (rv)
1596         {
1597           pool_put (am->arp_events, event);
1598           goto out;
1599         }
1600       memset (event, 0, sizeof (*event));
1601
1602       event->_vl_msg_id = ntohs (VL_API_IP4_ARP_EVENT);
1603       event->client_index = mp->client_index;
1604       event->context = mp->context;
1605       event->address = mp->address;
1606       event->pid = mp->pid;
1607       if (mp->address == 0)
1608         event->mac_ip = 1;
1609     }
1610   else
1611     {
1612       rv = vnet_add_del_ip4_arp_change_event
1613         (vnm, arp_change_delete_callback,
1614          mp->pid, &mp->address /* addr, in net byte order */ ,
1615          vpe_resolver_process_node.index,
1616          IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
1617     }
1618 out:
1619   REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
1620 }
1621
1622 static void
1623 vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
1624 {
1625   vpe_api_main_t *am = &vpe_api_main;
1626   vnet_main_t *vnm = vnet_get_main ();
1627   vl_api_want_ip6_nd_events_reply_t *rmp;
1628   int rv;
1629
1630   if (mp->enable_disable)
1631     {
1632       vl_api_ip6_nd_event_t *event;
1633       pool_get (am->nd_events, event);
1634
1635       rv = vnet_add_del_ip6_nd_change_event
1636         (vnm, nd_change_data_callback,
1637          mp->pid, mp->address /* addr, in net byte order */ ,
1638          vpe_resolver_process_node.index,
1639          IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
1640
1641       if (rv)
1642         {
1643           pool_put (am->nd_events, event);
1644           goto out;
1645         }
1646       memset (event, 0, sizeof (*event));
1647
1648       event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
1649       event->client_index = mp->client_index;
1650       event->context = mp->context;
1651       clib_memcpy (event->address, mp->address, 16);
1652       event->pid = mp->pid;
1653       if (ip6_address_is_zero ((ip6_address_t *) mp->address))
1654         event->mac_ip = 1;
1655
1656     }
1657   else
1658     {
1659       rv = vnet_add_del_ip6_nd_change_event
1660         (vnm, nd_change_delete_callback,
1661          mp->pid, mp->address /* addr, in net byte order */ ,
1662          vpe_resolver_process_node.index,
1663          IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
1664     }
1665 out:
1666   REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
1667 }
1668
1669 static void vl_api_input_acl_set_interface_t_handler
1670   (vl_api_input_acl_set_interface_t * mp)
1671 {
1672   vlib_main_t *vm = vlib_get_main ();
1673   vl_api_input_acl_set_interface_reply_t *rmp;
1674   int rv;
1675
1676   VALIDATE_SW_IF_INDEX (mp);
1677
1678   u32 ip4_table_index = ntohl (mp->ip4_table_index);
1679   u32 ip6_table_index = ntohl (mp->ip6_table_index);
1680   u32 l2_table_index = ntohl (mp->l2_table_index);
1681   u32 sw_if_index = ntohl (mp->sw_if_index);
1682
1683   rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
1684                                  ip6_table_index, l2_table_index, mp->is_add);
1685
1686   BAD_SW_IF_INDEX_LABEL;
1687
1688   REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
1689 }
1690
1691 static void
1692 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
1693 {
1694   int rv = 0;
1695   u8 *vector = 0;
1696   api_main_t *am = &api_main;
1697   vlib_main_t *vm = vlib_get_main ();
1698   void *oldheap;
1699   vl_api_get_node_graph_reply_t *rmp;
1700
1701   pthread_mutex_lock (&am->vlib_rp->mutex);
1702   oldheap = svm_push_data_heap (am->vlib_rp);
1703
1704   /*
1705    * Keep the number of memcpy ops to a minimum (e.g. 1).
1706    */
1707   vec_validate (vector, 16384);
1708   vec_reset_length (vector);
1709
1710   /* $$$$ FIXME */
1711   vector = vlib_node_serialize (&vm->node_main, vector,
1712                                 (u32) ~ 0 /* all threads */ ,
1713                                 1 /* include nexts */ ,
1714                                 1 /* include stats */ );
1715
1716   svm_pop_heap (oldheap);
1717   pthread_mutex_unlock (&am->vlib_rp->mutex);
1718
1719   /* *INDENT-OFF* */
1720   REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
1721   ({
1722     rmp->reply_in_shmem = (uword) vector;
1723   }));
1724   /* *INDENT-ON* */
1725 }
1726
1727 static void
1728 vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1729 {
1730   int rv = 0;
1731   vl_api_ioam_enable_reply_t *rmp;
1732   clib_error_t *error;
1733
1734   /* Ignoring the profile id as currently a single profile
1735    * is supported */
1736   error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1737                            mp->seqno, mp->analyse);
1738   if (error)
1739     {
1740       clib_error_report (error);
1741       rv = clib_error_get_code (error);
1742     }
1743
1744   REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1745 }
1746
1747 static void
1748 vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1749 {
1750   int rv = 0;
1751   vl_api_ioam_disable_reply_t *rmp;
1752   clib_error_t *error;
1753
1754   error = clear_ioam_rewrite_fn ();
1755   if (error)
1756     {
1757       clib_error_report (error);
1758       rv = clib_error_get_code (error);
1759     }
1760
1761   REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1762 }
1763
1764 static void
1765 vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
1766 {
1767   vl_api_pg_create_interface_reply_t *rmp;
1768   int rv = 0;
1769
1770   pg_main_t *pg = &pg_main;
1771   u32 pg_if_id = pg_interface_add_or_get (pg, ntohl (mp->interface_id));
1772   pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
1773
1774   /* *INDENT-OFF* */
1775   REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
1776   ({
1777     rmp->sw_if_index = ntohl(pi->sw_if_index);
1778   }));
1779   /* *INDENT-ON* */
1780 }
1781
1782 static void
1783 vl_api_pg_capture_t_handler (vl_api_pg_capture_t * mp)
1784 {
1785   vl_api_pg_capture_reply_t *rmp;
1786   int rv = 0;
1787
1788   vnet_main_t *vnm = vnet_get_main ();
1789   vnet_interface_main_t *im = &vnm->interface_main;
1790   vnet_hw_interface_t *hi = 0;
1791
1792   u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
1793   u32 hw_if_index = ~0;
1794   uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
1795   if (p)
1796     hw_if_index = *p;
1797   vec_free (intf_name);
1798
1799   if (hw_if_index != ~0)
1800     {
1801       pg_capture_args_t _a, *a = &_a;
1802
1803       u32 len = ntohl (mp->pcap_name_length);
1804       u8 *pcap_file_name = vec_new (u8, len);
1805       clib_memcpy (pcap_file_name, mp->pcap_file_name, len);
1806
1807       hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
1808       a->hw_if_index = hw_if_index;
1809       a->dev_instance = hi->dev_instance;
1810       a->is_enabled = mp->is_enabled;
1811       a->pcap_file_name = pcap_file_name;
1812       a->count = ntohl (mp->count);
1813
1814       clib_error_t *e = pg_capture (a);
1815       if (e)
1816         {
1817           clib_error_report (e);
1818           rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
1819         }
1820
1821       vec_free (pcap_file_name);
1822     }
1823   REPLY_MACRO (VL_API_PG_CAPTURE_REPLY);
1824 }
1825
1826 static void
1827 vl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t * mp)
1828 {
1829   vl_api_pg_enable_disable_reply_t *rmp;
1830   int rv = 0;
1831
1832   pg_main_t *pg = &pg_main;
1833   u32 stream_index = ~0;
1834
1835   int is_enable = mp->is_enabled != 0;
1836   u32 len = ntohl (mp->stream_name_length) - 1;
1837
1838   if (len > 0)
1839     {
1840       u8 *stream_name = vec_new (u8, len);
1841       clib_memcpy (stream_name, mp->stream_name, len);
1842       uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
1843       if (p)
1844         stream_index = *p;
1845       vec_free (stream_name);
1846     }
1847
1848   pg_enable_disable (stream_index, is_enable);
1849
1850   REPLY_MACRO (VL_API_PG_ENABLE_DISABLE_REPLY);
1851 }
1852
1853 static void
1854   vl_api_ip_source_and_port_range_check_add_del_t_handler
1855   (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1856 {
1857   vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1858   int rv = 0;
1859
1860   u8 is_ipv6 = mp->is_ipv6;
1861   u8 is_add = mp->is_add;
1862   u8 mask_length = mp->mask_length;
1863   ip4_address_t ip4_addr;
1864   ip6_address_t ip6_addr;
1865   u16 *low_ports = 0;
1866   u16 *high_ports = 0;
1867   u32 vrf_id;
1868   u16 tmp_low, tmp_high;
1869   u8 num_ranges;
1870   int i;
1871
1872   // Validate port range
1873   num_ranges = mp->number_of_ranges;
1874   if (num_ranges > 32)
1875     {                           // This is size of array in VPE.API
1876       rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1877       goto reply;
1878     }
1879
1880   vec_reset_length (low_ports);
1881   vec_reset_length (high_ports);
1882
1883   for (i = 0; i < num_ranges; i++)
1884     {
1885       tmp_low = mp->low_ports[i];
1886       tmp_high = mp->high_ports[i];
1887       // If tmp_low <= tmp_high then only need to check tmp_low = 0
1888       // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1889       if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1890         {
1891           rv = VNET_API_ERROR_INVALID_VALUE;
1892           goto reply;
1893         }
1894       vec_add1 (low_ports, tmp_low);
1895       vec_add1 (high_ports, tmp_high + 1);
1896     }
1897
1898   // Validate mask_length
1899   if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
1900     {
1901       rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
1902       goto reply;
1903     }
1904
1905   vrf_id = ntohl (mp->vrf_id);
1906
1907   if (vrf_id < 1)
1908     {
1909       rv = VNET_API_ERROR_INVALID_VALUE;
1910       goto reply;
1911     }
1912
1913
1914   if (is_ipv6)
1915     {
1916       clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
1917       rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
1918                                                     mask_length,
1919                                                     vrf_id,
1920                                                     low_ports,
1921                                                     high_ports, is_add);
1922     }
1923   else
1924     {
1925       clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
1926       rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
1927                                                     mask_length,
1928                                                     vrf_id,
1929                                                     low_ports,
1930                                                     high_ports, is_add);
1931     }
1932
1933 reply:
1934   vec_free (low_ports);
1935   vec_free (high_ports);
1936   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
1937 }
1938
1939 static void
1940   vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
1941   (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
1942 {
1943   vlib_main_t *vm = vlib_get_main ();
1944   vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
1945   ip4_main_t *im = &ip4_main;
1946   int rv;
1947   u32 sw_if_index;
1948   u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1949   u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1950   uword *p = 0;
1951   int i;
1952
1953   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
1954     ntohl (mp->tcp_out_vrf_id);
1955   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
1956     ntohl (mp->udp_out_vrf_id);
1957   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
1958     ntohl (mp->tcp_in_vrf_id);
1959   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
1960     ntohl (mp->udp_in_vrf_id);
1961
1962
1963   for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
1964     {
1965       if (vrf_id[i] != 0 && vrf_id[i] != ~0)
1966         {
1967           p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
1968
1969           if (p == 0)
1970             {
1971               rv = VNET_API_ERROR_INVALID_VALUE;
1972               goto reply;
1973             }
1974
1975           fib_index[i] = p[0];
1976         }
1977       else
1978         fib_index[i] = ~0;
1979     }
1980   sw_if_index = ntohl (mp->sw_if_index);
1981
1982   VALIDATE_SW_IF_INDEX (mp);
1983
1984   rv =
1985     set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
1986                                         mp->is_add);
1987
1988   BAD_SW_IF_INDEX_LABEL;
1989 reply:
1990
1991   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
1992 }
1993
1994 static void
1995 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
1996 {
1997   vl_api_delete_subif_reply_t *rmp;
1998   int rv;
1999
2000   rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
2001
2002   REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
2003 }
2004
2005 static void
2006 vl_api_punt_t_handler (vl_api_punt_t * mp)
2007 {
2008   vl_api_punt_reply_t *rmp;
2009   vlib_main_t *vm = vlib_get_main ();
2010   int rv = 0;
2011   clib_error_t *error;
2012
2013   error = vnet_punt_add_del (vm, mp->ipv, mp->l4_protocol,
2014                              ntohs (mp->l4_port), mp->is_add);
2015   if (error)
2016     {
2017       rv = -1;
2018       clib_error_report (error);
2019     }
2020
2021   REPLY_MACRO (VL_API_PUNT_REPLY);
2022 }
2023
2024 static void
2025 vl_api_feature_enable_disable_t_handler (vl_api_feature_enable_disable_t * mp)
2026 {
2027   vl_api_feature_enable_disable_reply_t *rmp;
2028   int rv = 0;
2029
2030   VALIDATE_SW_IF_INDEX (mp);
2031
2032   u8 *arc_name = format (0, "%s%c", mp->arc_name, 0);
2033   u8 *feature_name = format (0, "%s%c", mp->feature_name, 0);
2034
2035   vnet_feature_registration_t *reg =
2036     vnet_get_feature_reg ((const char *) arc_name,
2037                           (const char *) feature_name);
2038   if (reg == 0)
2039     rv = VNET_API_ERROR_INVALID_VALUE;
2040   else
2041     {
2042       u32 sw_if_index = ntohl (mp->sw_if_index);
2043       clib_error_t *error = 0;
2044
2045       if (reg->enable_disable_cb)
2046         error = reg->enable_disable_cb (sw_if_index, mp->enable);
2047       if (!error)
2048         vnet_feature_enable_disable ((const char *) arc_name,
2049                                      (const char *) feature_name,
2050                                      sw_if_index, mp->enable, 0, 0);
2051       else
2052         {
2053           clib_error_report (error);
2054           rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
2055         }
2056     }
2057
2058   vec_free (feature_name);
2059   vec_free (arc_name);
2060
2061   BAD_SW_IF_INDEX_LABEL;
2062
2063   REPLY_MACRO (VL_API_FEATURE_ENABLE_DISABLE_REPLY);
2064 }
2065
2066 #define BOUNCE_HANDLER(nn)                                              \
2067 static void vl_api_##nn##_t_handler (                                   \
2068     vl_api_##nn##_t *mp)                                                \
2069 {                                                                       \
2070     vpe_client_registration_t *reg;                                     \
2071     vpe_api_main_t * vam = &vpe_api_main;                               \
2072     unix_shared_memory_queue_t * q;                                     \
2073                                                                         \
2074     /* One registration only... */                                      \
2075     pool_foreach(reg, vam->nn##_registrations,                          \
2076     ({                                                                  \
2077         q = vl_api_client_index_to_input_queue (reg->client_index);     \
2078         if (q) {                                                        \
2079             /*                                                          \
2080              * If the queue is stuffed, turf the msg and complain       \
2081              * It's unlikely that the intended recipient is             \
2082              * alive; avoid deadlock at all costs.                      \
2083              */                                                         \
2084             if (q->cursize == q->maxsize) {                             \
2085                 clib_warning ("ERROR: receiver queue full, drop msg");  \
2086                 vl_msg_api_free (mp);                                   \
2087                 return;                                                 \
2088             }                                                           \
2089             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
2090             return;                                                     \
2091         }                                                               \
2092     }));                                                                \
2093     vl_msg_api_free (mp);                                               \
2094 }
2095
2096 static void setup_message_id_table (api_main_t * am);
2097
2098 /*
2099  * vpe_api_hookup
2100  * Add vpe's API message handlers to the table.
2101  * vlib has alread mapped shared memory and
2102  * added the client registration handlers.
2103  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
2104  */
2105 static clib_error_t *
2106 vpe_api_hookup (vlib_main_t * vm)
2107 {
2108   api_main_t *am = &api_main;
2109
2110 #define _(N,n)                                                  \
2111     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
2112                            vl_api_##n##_t_handler,              \
2113                            vl_noop_handler,                     \
2114                            vl_api_##n##_t_endian,               \
2115                            vl_api_##n##_t_print,                \
2116                            sizeof(vl_api_##n##_t), 1);
2117   foreach_vpe_api_msg;
2118 #undef _
2119
2120   /*
2121    * Trace space for classifier mask+match
2122    */
2123   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
2124   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size
2125     += 5 * sizeof (u32x4);
2126
2127   /*
2128    * Thread-safe API messages
2129    */
2130   am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
2131   am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
2132
2133   /*
2134    * Set up the (msg_name, crc, message-id) table
2135    */
2136   setup_message_id_table (am);
2137
2138   return 0;
2139 }
2140
2141 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
2142
2143 static clib_error_t *
2144 vpe_api_init (vlib_main_t * vm)
2145 {
2146   vpe_api_main_t *am = &vpe_api_main;
2147
2148   am->vlib_main = vm;
2149   am->vnet_main = vnet_get_main ();
2150   am->interface_events_registration_hash = hash_create (0, sizeof (uword));
2151   am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
2152   am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
2153   am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
2154   am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
2155   am->oam_events_registration_hash = hash_create (0, sizeof (uword));
2156   am->bfd_events_registration_hash = hash_create (0, sizeof (uword));
2157
2158   vl_set_memory_region_name ("/vpe-api");
2159   vl_enable_disable_memory_api (vm, 1 /* enable it */ );
2160
2161   return 0;
2162 }
2163
2164 VLIB_INIT_FUNCTION (vpe_api_init);
2165
2166
2167 static clib_error_t *
2168 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
2169 {
2170   u8 *chroot_path;
2171   u64 baseva, size, pvt_heap_size;
2172   int uid, gid, rv;
2173   const int max_buf_size = 4096;
2174   char *s, *buf;
2175   struct passwd _pw, *pw;
2176   struct group _grp, *grp;
2177   clib_error_t *e;
2178   buf = vec_new (char, 128);
2179   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2180     {
2181       if (unformat (input, "prefix %s", &chroot_path))
2182         {
2183           vec_add1 (chroot_path, 0);
2184           vl_set_memory_root_path ((char *) chroot_path);
2185         }
2186       else if (unformat (input, "uid %d", &uid))
2187         vl_set_memory_uid (uid);
2188       else if (unformat (input, "gid %d", &gid))
2189         vl_set_memory_gid (gid);
2190       else if (unformat (input, "baseva %llx", &baseva))
2191         vl_set_global_memory_baseva (baseva);
2192       else if (unformat (input, "global-size %lldM", &size))
2193         vl_set_global_memory_size (size * (1ULL << 20));
2194       else if (unformat (input, "global-size %lldG", &size))
2195         vl_set_global_memory_size (size * (1ULL << 30));
2196       else if (unformat (input, "global-size %lld", &size))
2197         vl_set_global_memory_size (size);
2198       else if (unformat (input, "global-pvt-heap-size %lldM", &pvt_heap_size))
2199         vl_set_global_pvt_heap_size (pvt_heap_size * (1ULL << 20));
2200       else if (unformat (input, "global-pvt-heap-size size %lld",
2201                          &pvt_heap_size))
2202         vl_set_global_pvt_heap_size (pvt_heap_size);
2203       else if (unformat (input, "api-pvt-heap-size %lldM", &pvt_heap_size))
2204         vl_set_api_pvt_heap_size (pvt_heap_size * (1ULL << 20));
2205       else if (unformat (input, "api-pvt-heap-size size %lld",
2206                          &pvt_heap_size))
2207         vl_set_api_pvt_heap_size (pvt_heap_size);
2208       else if (unformat (input, "api-size %lldM", &size))
2209         vl_set_api_memory_size (size * (1ULL << 20));
2210       else if (unformat (input, "api-size %lldG", &size))
2211         vl_set_api_memory_size (size * (1ULL << 30));
2212       else if (unformat (input, "api-size %lld", &size))
2213         vl_set_api_memory_size (size);
2214       else if (unformat (input, "uid %s", &s))
2215         {
2216           /* lookup the username */
2217           pw = NULL;
2218           while (((rv =
2219                    getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
2220                  && (vec_len (buf) <= max_buf_size))
2221             {
2222               vec_resize (buf, vec_len (buf) * 2);
2223             }
2224           if (rv < 0)
2225             {
2226               e = clib_error_return_code (0, rv,
2227                                           CLIB_ERROR_ERRNO_VALID |
2228                                           CLIB_ERROR_FATAL,
2229                                           "cannot fetch username %s", s);
2230               vec_free (s);
2231               vec_free (buf);
2232               return e;
2233             }
2234           if (pw == NULL)
2235             {
2236               e =
2237                 clib_error_return_fatal (0, "username %s does not exist", s);
2238               vec_free (s);
2239               vec_free (buf);
2240               return e;
2241             }
2242           vec_free (s);
2243           vl_set_memory_uid (pw->pw_uid);
2244         }
2245       else if (unformat (input, "gid %s", &s))
2246         {
2247           /* lookup the group name */
2248           grp = NULL;
2249           while (((rv =
2250                    getgrnam_r (s, &_grp, buf, vec_len (buf), &grp)) == ERANGE)
2251                  && (vec_len (buf) <= max_buf_size))
2252             {
2253               vec_resize (buf, vec_len (buf) * 2);
2254             }
2255           if (rv != 0)
2256             {
2257               e = clib_error_return_code (0, rv,
2258                                           CLIB_ERROR_ERRNO_VALID |
2259                                           CLIB_ERROR_FATAL,
2260                                           "cannot fetch group %s", s);
2261               vec_free (s);
2262               vec_free (buf);
2263               return e;
2264             }
2265           if (grp == NULL)
2266             {
2267               e = clib_error_return_fatal (0, "group %s does not exist", s);
2268               vec_free (s);
2269               vec_free (buf);
2270               return e;
2271             }
2272           vec_free (s);
2273           vec_free (buf);
2274           vl_set_memory_gid (grp->gr_gid);
2275         }
2276       else
2277         return clib_error_return (0, "unknown input `%U'",
2278                                   format_unformat_error, input);
2279     }
2280   return 0;
2281 }
2282
2283 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
2284
2285 void *
2286 get_unformat_vnet_sw_interface (void)
2287 {
2288   return (void *) &unformat_vnet_sw_interface;
2289 }
2290
2291 static u8 *
2292 format_arp_event (u8 * s, va_list * args)
2293 {
2294   vl_api_ip4_arp_event_t *event = va_arg (*args, vl_api_ip4_arp_event_t *);
2295
2296   s = format (s, "pid %d: ", ntohl (event->pid));
2297   if (event->mac_ip)
2298     s = format (s, "bd mac/ip4 binding events");
2299   else
2300     s = format (s, "resolution for %U", format_ip4_address, &event->address);
2301   return s;
2302 }
2303
2304 static u8 *
2305 format_nd_event (u8 * s, va_list * args)
2306 {
2307   vl_api_ip6_nd_event_t *event = va_arg (*args, vl_api_ip6_nd_event_t *);
2308
2309   s = format (s, "pid %d: ", ntohl (event->pid));
2310   if (event->mac_ip)
2311     s = format (s, "bd mac/ip6 binding events");
2312   else
2313     s = format (s, "resolution for %U", format_ip6_address, event->address);
2314   return s;
2315 }
2316
2317 static clib_error_t *
2318 show_ip_arp_nd_events_fn (vlib_main_t * vm,
2319                           unformat_input_t * input, vlib_cli_command_t * cmd)
2320 {
2321   vpe_api_main_t *am = &vpe_api_main;
2322   vl_api_ip4_arp_event_t *arp_event;
2323   vl_api_ip6_nd_event_t *nd_event;
2324
2325   if ((pool_elts (am->arp_events) == 0) && (pool_elts (am->nd_events) == 0))
2326     {
2327       vlib_cli_output (vm, "No active arp or nd event registrations");
2328       return 0;
2329     }
2330
2331   /* *INDENT-OFF* */
2332   pool_foreach (arp_event, am->arp_events,
2333   ({
2334     vlib_cli_output (vm, "%U", format_arp_event, arp_event);
2335   }));
2336
2337   pool_foreach (nd_event, am->nd_events,
2338   ({
2339     vlib_cli_output (vm, "%U", format_nd_event, nd_event);
2340   }));
2341   /* *INDENT-ON* */
2342
2343   return 0;
2344 }
2345
2346 /* *INDENT-OFF* */
2347 VLIB_CLI_COMMAND (show_ip_arp_nd_events, static) = {
2348   .path = "show arp-nd-event registrations",
2349   .function = show_ip_arp_nd_events_fn,
2350   .short_help = "Show ip4 arp and ip6 nd event registrations",
2351 };
2352 /* *INDENT-ON* */
2353
2354 #define vl_msg_name_crc_list
2355 #include <vpp/api/vpe_all_api_h.h>
2356 #undef vl_msg_name_crc_list
2357
2358 static void
2359 setup_message_id_table (api_main_t * am)
2360 {
2361 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
2362   foreach_vl_msg_name_crc_memclnt;
2363   foreach_vl_msg_name_crc_vpe;
2364 #undef _
2365 }
2366
2367
2368 /*
2369  * fd.io coding-style-patch-verification: ON
2370  *
2371  * Local Variables:
2372  * eval: (c-set-style "gnu")
2373  * End:
2374  */