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