IP directed broadcast
[vpp.git] / src / vnet / interface_api.c
1 /*
2  *------------------------------------------------------------------
3  * interface_api.c - vnet interface api
4  *
5  * Copyright (c) 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 <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/ip/ip.h>
27 #include <vnet/fib/fib_table.h>
28 #include <vnet/mfib/mfib_table.h>
29 #include <vnet/l2/l2_vtr.h>
30 #include <vnet/vnet_msg_enum.h>
31 #include <vnet/fib/fib_api.h>
32 #include <vnet/mfib/mfib_table.h>
33
34 #define vl_typedefs             /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
36 #undef vl_typedefs
37
38 #define vl_endianfun            /* define message structures */
39 #include <vnet/vnet_all_api_h.h>
40 #undef vl_endianfun
41
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #define vl_printfun
45 #include <vnet/vnet_all_api_h.h>
46 #undef vl_printfun
47
48 #include <vlibapi/api_helper_macros.h>
49 vpe_api_main_t vpe_api_main;
50
51 #define foreach_vpe_api_msg                                     \
52 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags)               \
53 _(HW_INTERFACE_SET_MTU, hw_interface_set_mtu)                   \
54 _(SW_INTERFACE_SET_MTU, sw_interface_set_mtu)                   \
55 _(WANT_INTERFACE_EVENTS, want_interface_events)                 \
56 _(SW_INTERFACE_DUMP, sw_interface_dump)                         \
57 _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address)   \
58 _(SW_INTERFACE_SET_RX_MODE, sw_interface_set_rx_mode)           \
59 _(SW_INTERFACE_SET_TABLE, sw_interface_set_table)               \
60 _(SW_INTERFACE_GET_TABLE, sw_interface_get_table)               \
61 _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered)     \
62 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats)           \
63 _(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del)           \
64 _(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address)   \
65 _(SW_INTERFACE_GET_MAC_ADDRESS, sw_interface_get_mac_address)   \
66 _(CREATE_VLAN_SUBIF, create_vlan_subif)                         \
67 _(CREATE_SUBIF, create_subif)                                   \
68 _(DELETE_SUBIF, delete_subif)                                   \
69 _(CREATE_LOOPBACK, create_loopback)                             \
70 _(CREATE_LOOPBACK_INSTANCE, create_loopback_instance)           \
71 _(DELETE_LOOPBACK, delete_loopback)                             \
72 _(INTERFACE_NAME_RENUMBER, interface_name_renumber)             \
73 _(COLLECT_DETAILED_INTERFACE_STATS, collect_detailed_interface_stats) \
74 _(SW_INTERFACE_SET_IP_DIRECTED_BROADCAST,                            \
75   sw_interface_set_ip_directed_broadcast)
76
77 static void
78 vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
79 {
80   vl_api_sw_interface_set_flags_reply_t *rmp;
81   vnet_main_t *vnm = vnet_get_main ();
82   int rv = 0;
83   clib_error_t *error;
84   u16 flags;
85
86   VALIDATE_SW_IF_INDEX (mp);
87
88   flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
89
90   error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
91   if (error)
92     {
93       rv = -1;
94       clib_error_report (error);
95     }
96
97   BAD_SW_IF_INDEX_LABEL;
98   REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
99 }
100
101 static void
102 vl_api_hw_interface_set_mtu_t_handler (vl_api_hw_interface_set_mtu_t * mp)
103 {
104   vl_api_hw_interface_set_mtu_reply_t *rmp;
105   vnet_main_t *vnm = vnet_get_main ();
106   u32 sw_if_index = ntohl (mp->sw_if_index);
107   u16 mtu = ntohs (mp->mtu);
108   ethernet_main_t *em = &ethernet_main;
109   int rv = 0;
110
111   VALIDATE_SW_IF_INDEX (mp);
112
113   vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
114   if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
115     {
116       rv = VNET_API_ERROR_INVALID_VALUE;
117       goto bad_sw_if_index;
118     }
119
120   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index);
121   ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index);
122
123   if (!eif)
124     {
125       rv = VNET_API_ERROR_FEATURE_DISABLED;
126       goto bad_sw_if_index;
127     }
128
129   if (mtu < hi->min_supported_packet_bytes)
130     {
131       rv = VNET_API_ERROR_INVALID_VALUE;
132       goto bad_sw_if_index;
133     }
134
135   if (mtu > hi->max_supported_packet_bytes)
136     {
137       rv = VNET_API_ERROR_INVALID_VALUE;
138       goto bad_sw_if_index;
139     }
140
141   vnet_hw_interface_set_mtu (vnm, si->hw_if_index, mtu);
142
143   BAD_SW_IF_INDEX_LABEL;
144   REPLY_MACRO (VL_API_HW_INTERFACE_SET_MTU_REPLY);
145 }
146
147 static void
148 vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
149 {
150   vl_api_sw_interface_set_mtu_reply_t *rmp;
151   vnet_main_t *vnm = vnet_get_main ();
152   u32 sw_if_index = ntohl (mp->sw_if_index);
153   int rv = 0;
154   int i;
155   u32 per_protocol_mtu[VNET_N_MTU];
156
157   VALIDATE_SW_IF_INDEX (mp);
158
159   for (i = 0; i < VNET_N_MTU; i++)
160     per_protocol_mtu[i] = ntohl (mp->mtu[i]);
161
162   vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, per_protocol_mtu);
163
164   BAD_SW_IF_INDEX_LABEL;
165   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
166 }
167
168 static void
169   vl_api_sw_interface_set_ip_directed_broadcast_t_handler
170   (vl_api_sw_interface_set_ip_directed_broadcast_t * mp)
171 {
172   vl_api_sw_interface_set_ip_directed_broadcast_reply_t *rmp;
173   u32 sw_if_index = ntohl (mp->sw_if_index);
174   int rv = 0;
175
176   VALIDATE_SW_IF_INDEX (mp);
177
178   vnet_sw_interface_ip_directed_broadcast (vnet_get_main (),
179                                            sw_if_index, mp->enable);
180
181   BAD_SW_IF_INDEX_LABEL;
182   REPLY_MACRO (VL_API_SW_INTERFACE_SET_IP_DIRECTED_BROADCAST_REPLY);
183 }
184
185 static void
186 send_sw_interface_details (vpe_api_main_t * am,
187                            vl_api_registration_t * rp,
188                            vnet_sw_interface_t * swif,
189                            u8 * interface_name, u32 context)
190 {
191   vnet_hw_interface_t *hi =
192     vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
193
194   vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
195   memset (mp, 0, sizeof (*mp));
196   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
197   mp->sw_if_index = ntohl (swif->sw_if_index);
198   mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
199   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
200   mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
201   mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
202                      VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
203   mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
204                     VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
205   mp->link_mtu = ntohs (hi->max_packet_bytes);
206   mp->mtu[VNET_MTU_L3] = ntohl (swif->mtu[VNET_MTU_L3]);
207   mp->mtu[VNET_MTU_IP4] = ntohl (swif->mtu[VNET_MTU_IP4]);
208   mp->mtu[VNET_MTU_IP6] = ntohl (swif->mtu[VNET_MTU_IP6]);
209   mp->mtu[VNET_MTU_MPLS] = ntohl (swif->mtu[VNET_MTU_MPLS]);
210
211   mp->context = context;
212
213   strncpy ((char *) mp->interface_name,
214            (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
215
216   /* Send the L2 address for ethernet physical intfcs */
217   if (swif->sup_sw_if_index == swif->sw_if_index
218       && hi->hw_class_index == ethernet_hw_interface_class.index)
219     {
220       ethernet_main_t *em = ethernet_get_main (am->vlib_main);
221       ethernet_interface_t *ei;
222
223       ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
224       ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
225       clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
226       mp->l2_address_length = ntohl (sizeof (ei->address));
227     }
228   else if (swif->sup_sw_if_index != swif->sw_if_index)
229     {
230       vnet_sub_interface_t *sub = &swif->sub;
231       mp->sub_id = ntohl (sub->id);
232       mp->sub_dot1ad = sub->eth.flags.dot1ad;
233       mp->sub_number_of_tags =
234         sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
235       mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
236       mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
237       mp->sub_exact_match = sub->eth.flags.exact_match;
238       mp->sub_default = sub->eth.flags.default_sub;
239       mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
240       mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
241
242       /* vlan tag rewrite data */
243       u32 vtr_op = L2_VTR_DISABLED;
244       u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
245
246       if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
247                      &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
248         {
249           // error - default to disabled
250           mp->vtr_op = ntohl (L2_VTR_DISABLED);
251           clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
252                         swif->sw_if_index);
253         }
254       else
255         {
256           mp->vtr_op = ntohl (vtr_op);
257           mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
258           mp->vtr_tag1 = ntohl (vtr_tag1);
259           mp->vtr_tag2 = ntohl (vtr_tag2);
260         }
261     }
262
263   /* pbb tag rewrite data */
264   ethernet_header_t eth_hdr;
265   u32 vtr_op = L2_VTR_DISABLED;
266   u16 outer_tag = 0;
267   u16 b_vlanid = 0;
268   u32 i_sid = 0;
269   memset (&eth_hdr, 0, sizeof (eth_hdr));
270
271   if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
272                   &vtr_op, &outer_tag, &eth_hdr, &b_vlanid, &i_sid))
273     {
274       mp->sub_dot1ah = 1;
275       clib_memcpy (mp->b_dmac, eth_hdr.dst_address,
276                    sizeof (eth_hdr.dst_address));
277       clib_memcpy (mp->b_smac, eth_hdr.src_address,
278                    sizeof (eth_hdr.src_address));
279       mp->b_vlanid = b_vlanid;
280       mp->i_sid = i_sid;
281     }
282
283   u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index);
284   if (tag)
285     strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
286
287   vl_api_send_msg (rp, (u8 *) mp);
288 }
289
290 static void
291 vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
292 {
293   vpe_api_main_t *am = &vpe_api_main;
294   vnet_sw_interface_t *swif;
295   vnet_interface_main_t *im = &am->vnet_main->interface_main;
296   vl_api_registration_t *rp;
297
298   rp = vl_api_client_index_to_registration (mp->client_index);
299
300   if (rp == 0)
301     {
302       clib_warning ("Client %d AWOL", mp->client_index);
303       return;
304     }
305
306   u8 *filter = 0, *name = 0;
307   if (mp->name_filter_valid)
308     {
309       mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
310       filter = format (0, "%s%c", mp->name_filter, 0);
311     }
312
313   char *strcasestr (char *, char *);    /* lnx hdr file botch */
314   /* *INDENT-OFF* */
315   pool_foreach (swif, im->sw_interfaces,
316   ({
317     if (!vnet_swif_is_api_visible (swif))
318         continue;
319     vec_reset_length(name);
320     name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
321                    swif, 0);
322
323     if (filter && !strcasestr((char *) name, (char *) filter))
324         continue;
325
326     send_sw_interface_details (am, rp, swif, name, mp->context);
327   }));
328   /* *INDENT-ON* */
329
330   vec_free (name);
331   vec_free (filter);
332 }
333
334 static void
335   vl_api_sw_interface_add_del_address_t_handler
336   (vl_api_sw_interface_add_del_address_t * mp)
337 {
338   vlib_main_t *vm = vlib_get_main ();
339   vnet_main_t *vnm = vnet_get_main ();
340   vl_api_sw_interface_add_del_address_reply_t *rmp;
341   int rv = 0;
342   u32 is_del;
343   clib_error_t *error = 0;
344
345   VALIDATE_SW_IF_INDEX (mp);
346
347   is_del = mp->is_add == 0;
348   vnm->api_errno = 0;
349
350   if (mp->del_all)
351     ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
352   else if (mp->is_ipv6)
353     error = ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
354                                            (void *) mp->address,
355                                            mp->address_length, is_del);
356   else
357     error = ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
358                                            (void *) mp->address,
359                                            mp->address_length, is_del);
360
361   if (error)
362     {
363       rv = vnm->api_errno;
364       clib_error_report (error);
365       goto done;
366     }
367
368   BAD_SW_IF_INDEX_LABEL;
369
370 done:
371   REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
372 }
373
374 void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
375 void
376 stats_dslock_with_hint (int hint, int tag)
377 {
378 }
379
380 void stats_dsunlock (void) __attribute__ ((weak));
381 void
382 stats_dsunlock (void)
383 {
384 }
385
386 static void
387 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
388 {
389   vl_api_sw_interface_set_table_reply_t *rmp;
390   u32 sw_if_index = ntohl (mp->sw_if_index);
391   u32 table_id = ntohl (mp->vrf_id);
392   int rv = 0;
393
394   VALIDATE_SW_IF_INDEX (mp);
395
396   stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
397
398   if (mp->is_ipv6)
399     rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
400   else
401     rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
402
403   stats_dsunlock ();
404
405   BAD_SW_IF_INDEX_LABEL;
406
407   REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
408 }
409
410 int
411 ip_table_bind (fib_protocol_t fproto,
412                u32 sw_if_index, u32 table_id, u8 is_api)
413 {
414   CLIB_UNUSED (ip_interface_address_t * ia);
415   u32 fib_index, mfib_index;
416   fib_source_t src;
417   mfib_source_t msrc;
418
419   if (is_api)
420     {
421       src = FIB_SOURCE_API;
422       msrc = MFIB_SOURCE_API;
423     }
424   else
425     {
426       src = FIB_SOURCE_CLI;
427       msrc = MFIB_SOURCE_CLI;
428     }
429
430   /*
431    * This if table does not exist = error is what we want in the end.
432    */
433   fib_index = fib_table_find (fproto, table_id);
434   mfib_index = mfib_table_find (fproto, table_id);
435
436   if (~0 == fib_index || ~0 == mfib_index)
437     {
438       return (VNET_API_ERROR_NO_SUCH_FIB);
439     }
440
441   if (FIB_PROTOCOL_IP6 == fproto)
442     {
443       /*
444        * If the interface already has in IP address, then a change int
445        * VRF is not allowed. The IP address applied must first be removed.
446        * We do not do that automatically here, since VPP has no knowledge
447        * of whether thoses subnets are valid in the destination VRF.
448        */
449       /* *INDENT-OFF* */
450       foreach_ip_interface_address (&ip6_main.lookup_main,
451                                     ia, sw_if_index,
452                                     1 /* honor unnumbered */ ,
453       ({
454         return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
455       }));
456       /* *INDENT-ON* */
457
458       vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
459       vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
460
461       /*
462        * tell those that are interested that the binding is changing.
463        */
464       ip6_table_bind_callback_t *cb;
465       vec_foreach (cb, ip6_main.table_bind_callbacks)
466         cb->function (&ip6_main, cb->function_opaque,
467                       sw_if_index,
468                       fib_index,
469                       ip6_main.fib_index_by_sw_if_index[sw_if_index]);
470
471       if (0 == table_id)
472         {
473           /* reset back to default */
474           if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
475             fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
476                               FIB_PROTOCOL_IP6, src);
477           if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
478             mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
479                                [sw_if_index], FIB_PROTOCOL_IP6, msrc);
480
481         }
482       else
483         {
484           /* we need to lock the table now it's inuse */
485           fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
486           mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
487         }
488
489       ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
490       ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
491     }
492   else
493     {
494       /*
495        * If the interface already has in IP address, then a change int
496        * VRF is not allowed. The IP address applied must first be removed.
497        * We do not do that automatically here, since VPP has no knowledge
498        * of whether thoses subnets are valid in the destination VRF.
499        */
500       /* *INDENT-OFF* */
501       foreach_ip_interface_address (&ip4_main.lookup_main,
502                                     ia, sw_if_index,
503                                     1 /* honor unnumbered */ ,
504       ({
505         return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
506       }));
507       /* *INDENT-ON* */
508
509       vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
510       vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
511
512       /*
513        * tell those that are interested that the binding is changing.
514        */
515       ip4_table_bind_callback_t *cb;
516       vec_foreach (cb, ip4_main.table_bind_callbacks)
517         cb->function (&ip4_main, cb->function_opaque,
518                       sw_if_index,
519                       fib_index,
520                       ip4_main.fib_index_by_sw_if_index[sw_if_index]);
521
522       if (0 == table_id)
523         {
524           /* reset back to default */
525           if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
526             fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
527                               FIB_PROTOCOL_IP4, src);
528           if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
529             mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
530                                [sw_if_index], FIB_PROTOCOL_IP4, msrc);
531
532         }
533       else
534         {
535           /* we need to lock the table now it's inuse */
536           fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
537                                                          table_id, src);
538
539           mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
540                                                            table_id, msrc);
541         }
542
543       ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
544       ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
545     }
546
547   return (0);
548 }
549
550 static void
551 send_sw_interface_get_table_reply (vl_api_registration_t * reg,
552                                    u32 context, int retval, u32 vrf_id)
553 {
554   vl_api_sw_interface_get_table_reply_t *mp;
555
556   mp = vl_msg_api_alloc (sizeof (*mp));
557   memset (mp, 0, sizeof (*mp));
558   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
559   mp->context = context;
560   mp->retval = htonl (retval);
561   mp->vrf_id = htonl (vrf_id);
562
563   vl_api_send_msg (reg, (u8 *) mp);
564 }
565
566 static void
567 vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
568 {
569   vl_api_registration_t *reg;
570   fib_table_t *fib_table = 0;
571   u32 sw_if_index = ~0;
572   u32 fib_index = ~0;
573   u32 table_id = ~0;
574   fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
575   int rv = 0;
576
577   reg = vl_api_client_index_to_registration (mp->client_index);
578   if (!reg)
579     return;
580
581   VALIDATE_SW_IF_INDEX (mp);
582
583   sw_if_index = ntohl (mp->sw_if_index);
584
585   if (mp->is_ipv6)
586     fib_proto = FIB_PROTOCOL_IP6;
587
588   fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
589   if (fib_index != ~0)
590     {
591       fib_table = fib_table_get (fib_index, fib_proto);
592       table_id = fib_table->ft_table_id;
593     }
594
595   BAD_SW_IF_INDEX_LABEL;
596
597   send_sw_interface_get_table_reply (reg, mp->context, rv, table_id);
598 }
599
600 static void vl_api_sw_interface_set_unnumbered_t_handler
601   (vl_api_sw_interface_set_unnumbered_t * mp)
602 {
603   vl_api_sw_interface_set_unnumbered_reply_t *rmp;
604   int rv = 0;
605   vnet_main_t *vnm = vnet_get_main ();
606   u32 sw_if_index = ntohl (mp->sw_if_index);
607   u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
608
609   /*
610    * The API message field names are backwards from
611    * the underlying data structure names.
612    * It's not worth changing them now.
613    */
614   if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
615     {
616       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
617       goto done;
618     }
619
620   /* Only check the "use loop0" field when setting the binding */
621   if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
622     {
623       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
624       goto done;
625     }
626
627   vnet_sw_interface_update_unnumbered (unnumbered_sw_if_index,
628                                        sw_if_index, mp->is_add);
629 done:
630   REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
631 }
632
633 static void
634 vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
635                                            mp)
636 {
637   vl_api_sw_interface_clear_stats_reply_t *rmp;
638
639   vnet_main_t *vnm = vnet_get_main ();
640   vnet_interface_main_t *im = &vnm->interface_main;
641   vlib_simple_counter_main_t *sm;
642   vlib_combined_counter_main_t *cm;
643   static vnet_main_t **my_vnet_mains;
644   int i, j, n_counters;
645   int rv = 0;
646
647   if (mp->sw_if_index != ~0)
648     VALIDATE_SW_IF_INDEX (mp);
649
650   vec_reset_length (my_vnet_mains);
651
652   for (i = 0; i < vec_len (vnet_mains); i++)
653     {
654       if (vnet_mains[i])
655         vec_add1 (my_vnet_mains, vnet_mains[i]);
656     }
657
658   if (vec_len (vnet_mains) == 0)
659     vec_add1 (my_vnet_mains, vnm);
660
661   n_counters = vec_len (im->combined_sw_if_counters);
662
663   for (j = 0; j < n_counters; j++)
664     {
665       for (i = 0; i < vec_len (my_vnet_mains); i++)
666         {
667           im = &my_vnet_mains[i]->interface_main;
668           cm = im->combined_sw_if_counters + j;
669           if (mp->sw_if_index == (u32) ~ 0)
670             vlib_clear_combined_counters (cm);
671           else
672             vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
673         }
674     }
675
676   n_counters = vec_len (im->sw_if_counters);
677
678   for (j = 0; j < n_counters; j++)
679     {
680       for (i = 0; i < vec_len (my_vnet_mains); i++)
681         {
682           im = &my_vnet_mains[i]->interface_main;
683           sm = im->sw_if_counters + j;
684           if (mp->sw_if_index == (u32) ~ 0)
685             vlib_clear_simple_counters (sm);
686           else
687             vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
688         }
689     }
690
691   BAD_SW_IF_INDEX_LABEL;
692
693   REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
694 }
695
696 #define API_LINK_STATE_EVENT 1
697 #define API_ADMIN_UP_DOWN_EVENT 2
698
699 static int
700 event_data_cmp (void *a1, void *a2)
701 {
702   uword *e1 = a1;
703   uword *e2 = a2;
704
705   return (word) e1[0] - (word) e2[0];
706 }
707
708 static void
709 send_sw_interface_event (vpe_api_main_t * am,
710                          vpe_client_registration_t * reg,
711                          vl_api_registration_t * vl_reg,
712                          vnet_sw_interface_t * swif)
713 {
714   vl_api_sw_interface_event_t *mp;
715   vnet_main_t *vnm = am->vnet_main;
716
717   vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
718                                                        swif->sw_if_index);
719   mp = vl_msg_api_alloc (sizeof (*mp));
720   memset (mp, 0, sizeof (*mp));
721   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
722   mp->sw_if_index = ntohl (swif->sw_if_index);
723   mp->client_index = reg->client_index;
724   mp->pid = reg->client_pid;
725
726   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
727   mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
728   vl_api_send_msg (vl_reg, (u8 *) mp);
729 }
730
731 static uword
732 link_state_process (vlib_main_t * vm,
733                     vlib_node_runtime_t * rt, vlib_frame_t * f)
734 {
735   vpe_api_main_t *vam = &vpe_api_main;
736   vnet_main_t *vnm = vam->vnet_main;
737   vnet_sw_interface_t *swif;
738   uword *event_data = 0;
739   vpe_client_registration_t *reg;
740   int i;
741   u32 prev_sw_if_index;
742   vl_api_registration_t *vl_reg;
743
744   vam->link_state_process_up = 1;
745
746   while (1)
747     {
748       vlib_process_wait_for_event (vm);
749
750       /* Unified list of changed link or admin state sw_if_indices */
751       vlib_process_get_events_with_type
752         (vm, &event_data, API_LINK_STATE_EVENT);
753       vlib_process_get_events_with_type
754         (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
755
756       /* Sort, so we can eliminate duplicates */
757       vec_sort_with_function (event_data, event_data_cmp);
758
759       prev_sw_if_index = ~0;
760
761       for (i = 0; i < vec_len (event_data); i++)
762         {
763           /* Only one message per swif */
764           if (prev_sw_if_index == event_data[i])
765             continue;
766           prev_sw_if_index = event_data[i];
767
768           /* *INDENT-OFF* */
769           pool_foreach(reg, vam->interface_events_registrations,
770           ({
771             vl_reg = vl_api_client_index_to_registration (reg->client_index);
772             if (vl_reg)
773               {
774                 /* sw_interface may be deleted already */
775                 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
776                                          event_data[i]))
777                   {
778                     swif = vnet_get_sw_interface (vnm, event_data[i]);
779                     send_sw_interface_event (vam, reg, vl_reg, swif);
780                   }
781               }
782           }));
783           /* *INDENT-ON* */
784         }
785       vec_reset_length (event_data);
786     }
787
788   return 0;
789 }
790
791 static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
792                                             u32 flags);
793 static clib_error_t *admin_up_down_function (vnet_main_t * vm,
794                                              u32 hw_if_index, u32 flags);
795
796 /* *INDENT-OFF* */
797 VLIB_REGISTER_NODE (link_state_process_node,static) = {
798   .function = link_state_process,
799   .type = VLIB_NODE_TYPE_PROCESS,
800   .name = "vpe-link-state-process",
801 };
802 /* *INDENT-ON* */
803
804 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
805 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
806
807 static clib_error_t *
808 link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
809 {
810   vpe_api_main_t *vam = &vpe_api_main;
811   vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
812
813   if (vam->link_state_process_up)
814     vlib_process_signal_event (vam->vlib_main,
815                                link_state_process_node.index,
816                                API_LINK_STATE_EVENT, hi->sw_if_index);
817   return 0;
818 }
819
820 static clib_error_t *
821 admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
822 {
823   vpe_api_main_t *vam = &vpe_api_main;
824
825   /*
826    * Note: it's perfectly fair to set a subif admin up / admin down.
827    * Note the subtle distinction between this routine and the previous
828    * routine.
829    */
830   if (vam->link_state_process_up)
831     vlib_process_signal_event (vam->vlib_main,
832                                link_state_process_node.index,
833                                API_ADMIN_UP_DOWN_EVENT, sw_if_index);
834   return 0;
835 }
836
837 static void vl_api_sw_interface_tag_add_del_t_handler
838   (vl_api_sw_interface_tag_add_del_t * mp)
839 {
840   vnet_main_t *vnm = vnet_get_main ();
841   vl_api_sw_interface_tag_add_del_reply_t *rmp;
842   int rv = 0;
843   u8 *tag;
844   u32 sw_if_index = ntohl (mp->sw_if_index);
845
846   VALIDATE_SW_IF_INDEX (mp);
847
848   if (mp->is_add)
849     {
850       if (mp->tag[0] == 0)
851         {
852           rv = VNET_API_ERROR_INVALID_VALUE;
853           goto out;
854         }
855
856       mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
857       tag = format (0, "%s%c", mp->tag, 0);
858       vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
859     }
860   else
861     vnet_clear_sw_interface_tag (vnm, sw_if_index);
862
863   BAD_SW_IF_INDEX_LABEL;
864 out:
865   REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
866 }
867
868 static void vl_api_sw_interface_set_mac_address_t_handler
869   (vl_api_sw_interface_set_mac_address_t * mp)
870 {
871   vl_api_sw_interface_set_mac_address_reply_t *rmp;
872   vnet_main_t *vnm = vnet_get_main ();
873   u32 sw_if_index = ntohl (mp->sw_if_index);
874   vnet_sw_interface_t *si;
875   clib_error_t *error;
876   int rv = 0;
877
878   VALIDATE_SW_IF_INDEX (mp);
879
880   si = vnet_get_sw_interface (vnm, sw_if_index);
881   error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index,
882                                                 mp->mac_address);
883   if (error)
884     {
885       rv = VNET_API_ERROR_UNIMPLEMENTED;
886       clib_error_report (error);
887       goto out;
888     }
889
890   BAD_SW_IF_INDEX_LABEL;
891 out:
892   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
893 }
894
895 static void vl_api_sw_interface_get_mac_address_t_handler
896   (vl_api_sw_interface_get_mac_address_t * mp)
897 {
898   vl_api_sw_interface_get_mac_address_reply_t *rmp;
899   vl_api_registration_t *reg;
900   vnet_main_t *vnm = vnet_get_main ();
901   u32 sw_if_index = ntohl (mp->sw_if_index);
902   vnet_sw_interface_t *si;
903   ethernet_interface_t *eth_if = 0;
904   int rv = 0;
905
906   VALIDATE_SW_IF_INDEX (mp);
907
908   si = vnet_get_sup_sw_interface (vnm, sw_if_index);
909   if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
910     eth_if = ethernet_get_interface (&ethernet_main, si->hw_if_index);
911
912   BAD_SW_IF_INDEX_LABEL;
913
914   reg = vl_api_client_index_to_registration (mp->client_index);
915   if (!reg)
916     return;
917   rmp = vl_msg_api_alloc (sizeof (*rmp));
918   rmp->_vl_msg_id = htons (VL_API_SW_INTERFACE_GET_MAC_ADDRESS_REPLY);
919   rmp->context = mp->context;
920   rmp->retval = htonl (rv);
921   if (!rv && eth_if)
922     memcpy (rmp->mac_address, eth_if->address, 6);
923   vl_api_send_msg (reg, (u8 *) rmp);
924 }
925
926 static void vl_api_sw_interface_set_rx_mode_t_handler
927   (vl_api_sw_interface_set_rx_mode_t * mp)
928 {
929   vl_api_sw_interface_set_rx_mode_reply_t *rmp;
930   vnet_main_t *vnm = vnet_get_main ();
931   u32 sw_if_index = ntohl (mp->sw_if_index);
932   vnet_sw_interface_t *si;
933   clib_error_t *error;
934   int rv = 0;
935
936   VALIDATE_SW_IF_INDEX (mp);
937
938   si = vnet_get_sw_interface (vnm, sw_if_index);
939   error = set_hw_interface_change_rx_mode (vnm, si->hw_if_index,
940                                            mp->queue_id_valid,
941                                            ntohl (mp->queue_id), mp->mode);
942   if (error)
943     {
944       rv = VNET_API_ERROR_UNIMPLEMENTED;
945       clib_error_report (error);
946       goto out;
947     }
948
949   BAD_SW_IF_INDEX_LABEL;
950 out:
951   REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_MODE_REPLY);
952 }
953
954 static void
955 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
956 {
957   vl_api_create_vlan_subif_reply_t *rmp;
958   vnet_main_t *vnm = vnet_get_main ();
959   u32 sw_if_index = (u32) ~ 0;
960   vnet_hw_interface_t *hi;
961   int rv = 0;
962   u32 id;
963   vnet_sw_interface_t template;
964   uword *p;
965   vnet_interface_main_t *im = &vnm->interface_main;
966   u64 sup_and_sub_key;
967   vl_api_registration_t *reg;
968   clib_error_t *error;
969
970   VALIDATE_SW_IF_INDEX (mp);
971
972   hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
973
974   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
975     {
976       rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
977       goto out;
978     }
979
980   id = ntohl (mp->vlan_id);
981   if (id == 0 || id > 4095)
982     {
983       rv = VNET_API_ERROR_INVALID_VLAN;
984       goto out;
985     }
986
987   sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
988
989   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
990   if (p)
991     {
992       rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
993       goto out;
994     }
995
996   memset (&template, 0, sizeof (template));
997   template.type = VNET_SW_INTERFACE_TYPE_SUB;
998   template.sup_sw_if_index = hi->sw_if_index;
999   template.sub.id = id;
1000   template.sub.eth.raw_flags = 0;
1001   template.sub.eth.flags.one_tag = 1;
1002   template.sub.eth.outer_vlan_id = id;
1003   template.sub.eth.flags.exact_match = 1;
1004
1005   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1006   if (error)
1007     {
1008       clib_error_report (error);
1009       rv = VNET_API_ERROR_INVALID_REGISTRATION;
1010       goto out;
1011     }
1012
1013   u64 *kp = clib_mem_alloc (sizeof (*kp));
1014   *kp = sup_and_sub_key;
1015
1016   hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
1017   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1018
1019   BAD_SW_IF_INDEX_LABEL;
1020
1021 out:
1022   reg = vl_api_client_index_to_registration (mp->client_index);
1023   if (!reg)
1024     return;
1025
1026   rmp = vl_msg_api_alloc (sizeof (*rmp));
1027   rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
1028   rmp->context = mp->context;
1029   rmp->retval = htonl (rv);
1030   rmp->sw_if_index = htonl (sw_if_index);
1031   vl_api_send_msg (reg, (u8 *) rmp);
1032 }
1033
1034 static void
1035 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
1036 {
1037   vl_api_create_subif_reply_t *rmp;
1038   vnet_main_t *vnm = vnet_get_main ();
1039   u32 sw_if_index = ~0;
1040   int rv = 0;
1041   u32 sub_id;
1042   vnet_sw_interface_t *si;
1043   vnet_hw_interface_t *hi;
1044   vnet_sw_interface_t template;
1045   uword *p;
1046   vnet_interface_main_t *im = &vnm->interface_main;
1047   u64 sup_and_sub_key;
1048   clib_error_t *error;
1049
1050   VALIDATE_SW_IF_INDEX (mp);
1051
1052   si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
1053   hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1054
1055   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1056     {
1057       rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1058       goto out;
1059     }
1060
1061   sw_if_index = si->sw_if_index;
1062   sub_id = ntohl (mp->sub_id);
1063
1064   sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
1065
1066   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1067   if (p)
1068     {
1069       if (CLIB_DEBUG > 0)
1070         clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
1071                       sw_if_index, sub_id);
1072       rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
1073       goto out;
1074     }
1075
1076   memset (&template, 0, sizeof (template));
1077   template.type = VNET_SW_INTERFACE_TYPE_SUB;
1078   template.sup_sw_if_index = sw_if_index;
1079   template.sub.id = sub_id;
1080   template.sub.eth.flags.no_tags = mp->no_tags;
1081   template.sub.eth.flags.one_tag = mp->one_tag;
1082   template.sub.eth.flags.two_tags = mp->two_tags;
1083   template.sub.eth.flags.dot1ad = mp->dot1ad;
1084   template.sub.eth.flags.exact_match = mp->exact_match;
1085   template.sub.eth.flags.default_sub = mp->default_sub;
1086   template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
1087   template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
1088   template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
1089   template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
1090
1091   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1092   if (error)
1093     {
1094       clib_error_report (error);
1095       rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
1096       goto out;
1097     }
1098
1099   u64 *kp = clib_mem_alloc (sizeof (*kp));
1100   *kp = sup_and_sub_key;
1101
1102   hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
1103   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1104
1105   BAD_SW_IF_INDEX_LABEL;
1106
1107 out:
1108
1109   /* *INDENT-OFF* */
1110   REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
1111   ({
1112     rmp->sw_if_index = ntohl(sw_if_index);
1113   }));
1114   /* *INDENT-ON* */
1115 }
1116
1117 static void
1118 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
1119 {
1120   vl_api_delete_subif_reply_t *rmp;
1121   int rv;
1122
1123   rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
1124
1125   REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
1126 }
1127
1128 static void
1129 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
1130                                           mp)
1131 {
1132   vl_api_interface_name_renumber_reply_t *rmp;
1133   int rv = 0;
1134
1135   VALIDATE_SW_IF_INDEX (mp);
1136
1137   rv = vnet_interface_name_renumber
1138     (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
1139
1140   BAD_SW_IF_INDEX_LABEL;
1141
1142   REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
1143 }
1144
1145 static void
1146 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
1147 {
1148   vl_api_create_loopback_reply_t *rmp;
1149   u32 sw_if_index;
1150   int rv;
1151
1152   rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address, 0, 0);
1153
1154   /* *INDENT-OFF* */
1155   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
1156   ({
1157     rmp->sw_if_index = ntohl (sw_if_index);
1158   }));
1159   /* *INDENT-ON* */
1160 }
1161
1162 static void vl_api_create_loopback_instance_t_handler
1163   (vl_api_create_loopback_instance_t * mp)
1164 {
1165   vl_api_create_loopback_instance_reply_t *rmp;
1166   u32 sw_if_index;
1167   u8 is_specified = mp->is_specified;
1168   u32 user_instance = ntohl (mp->user_instance);
1169   int rv;
1170
1171   rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address,
1172                                        is_specified, user_instance);
1173
1174   /* *INDENT-OFF* */
1175   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY,
1176   ({
1177     rmp->sw_if_index = ntohl (sw_if_index);
1178   }));
1179   /* *INDENT-ON* */
1180 }
1181
1182 static void
1183 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
1184 {
1185   vl_api_delete_loopback_reply_t *rmp;
1186   u32 sw_if_index;
1187   int rv;
1188
1189   sw_if_index = ntohl (mp->sw_if_index);
1190   rv = vnet_delete_loopback_interface (sw_if_index);
1191
1192   REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
1193 }
1194
1195 static void
1196   vl_api_collect_detailed_interface_stats_t_handler
1197   (vl_api_collect_detailed_interface_stats_t * mp)
1198 {
1199   vl_api_collect_detailed_interface_stats_reply_t *rmp;
1200   int rv = 0;
1201
1202   rv =
1203     vnet_sw_interface_stats_collect_enable_disable (ntohl (mp->sw_if_index),
1204                                                     mp->enable_disable);
1205
1206   REPLY_MACRO (VL_API_COLLECT_DETAILED_INTERFACE_STATS_REPLY);
1207 }
1208
1209 /*
1210  * vpe_api_hookup
1211  * Add vpe's API message handlers to the table.
1212  * vlib has alread mapped shared memory and
1213  * added the client registration handlers.
1214  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1215  */
1216 #define vl_msg_name_crc_list
1217 #include <vnet/interface.api.h>
1218 #undef vl_msg_name_crc_list
1219
1220 static void
1221 setup_message_id_table (api_main_t * am)
1222 {
1223 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1224   foreach_vl_msg_name_crc_interface;
1225 #undef _
1226 }
1227
1228 pub_sub_handler (interface_events, INTERFACE_EVENTS);
1229
1230 static clib_error_t *
1231 interface_api_hookup (vlib_main_t * vm)
1232 {
1233   api_main_t *am = &api_main;
1234
1235 #define _(N,n)                                                  \
1236     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1237                            vl_api_##n##_t_handler,              \
1238                            vl_noop_handler,                     \
1239                            vl_api_##n##_t_endian,               \
1240                            vl_api_##n##_t_print,                \
1241                            sizeof(vl_api_##n##_t), 1);
1242   foreach_vpe_api_msg;
1243 #undef _
1244
1245   /*
1246    * Set up the (msg_name, crc, message-id) table
1247    */
1248   setup_message_id_table (am);
1249
1250   return 0;
1251 }
1252
1253 VLIB_API_INIT_FUNCTION (interface_api_hookup);
1254
1255 /*
1256  * fd.io coding-style-patch-verification: ON
1257  *
1258  * Local Variables:
1259  * eval: (c-set-style "gnu")
1260  * End:
1261  */