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