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