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