Add binary API for reading interface/vrf assignment (VPP-439)
[vpp.git] / vnet / 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/l2/l2_vtr.h>
29 #include <vnet/vnet_msg_enum.h>
30
31 #define vl_typedefs             /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
33 #undef vl_typedefs
34
35 #define vl_endianfun            /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
37 #undef vl_endianfun
38
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #define vl_printfun
42 #include <vnet/vnet_all_api_h.h>
43 #undef vl_printfun
44
45 #include <vlibapi/api_helper_macros.h>
46
47 #define foreach_vpe_api_msg                                     \
48 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags)               \
49 _(SW_INTERFACE_SET_MTU, sw_interface_set_mtu)                   \
50 _(WANT_INTERFACE_EVENTS, want_interface_events)                 \
51 _(SW_INTERFACE_DUMP, sw_interface_dump)                         \
52 _(SW_INTERFACE_DETAILS, sw_interface_details)                   \
53 _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address)   \
54 _(SW_INTERFACE_SET_TABLE, sw_interface_set_table)               \
55 _(SW_INTERFACE_GET_TABLE, sw_interface_get_table)               \
56 _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered)     \
57 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats)           \
58 _(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del)
59
60 static void
61 vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
62 {
63   vl_api_sw_interface_set_flags_reply_t *rmp;
64   vnet_main_t *vnm = vnet_get_main ();
65   int rv = 0;
66   clib_error_t *error;
67   u16 flags;
68
69   VALIDATE_SW_IF_INDEX (mp);
70
71   flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
72
73   error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
74   if (error)
75     {
76       rv = -1;
77       clib_error_report (error);
78     }
79
80   BAD_SW_IF_INDEX_LABEL;
81   REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
82 }
83
84 static void
85 vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
86 {
87   vl_api_sw_interface_set_mtu_reply_t *rmp;
88   vnet_main_t *vnm = vnet_get_main ();
89   u32 flags = ETHERNET_INTERFACE_FLAG_MTU;
90   u32 sw_if_index = ntohl (mp->sw_if_index);
91   u16 mtu = ntohs (mp->mtu);
92   ethernet_main_t *em = &ethernet_main;
93   int rv = 0;
94
95   VALIDATE_SW_IF_INDEX (mp);
96
97   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, sw_if_index);
98   ethernet_interface_t *eif = ethernet_get_interface (em, sw_if_index);
99
100   if (!eif)
101     {
102       rv = VNET_API_ERROR_FEATURE_DISABLED;
103       goto bad_sw_if_index;
104     }
105
106   if (mtu < hi->min_supported_packet_bytes)
107     {
108       rv = VNET_API_ERROR_INVALID_VALUE;
109       goto bad_sw_if_index;
110     }
111
112   if (mtu > hi->max_supported_packet_bytes)
113     {
114       rv = VNET_API_ERROR_INVALID_VALUE;
115       goto bad_sw_if_index;
116     }
117
118   if (hi->max_packet_bytes != mtu)
119     {
120       hi->max_packet_bytes = mtu;
121       ethernet_set_flags (vnm, sw_if_index, flags);
122     }
123
124   BAD_SW_IF_INDEX_LABEL;
125   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
126 }
127
128 static void
129 send_sw_interface_details (vpe_api_main_t * am,
130                            unix_shared_memory_queue_t * q,
131                            vnet_sw_interface_t * swif,
132                            u8 * interface_name, u32 context)
133 {
134   vl_api_sw_interface_details_t *mp;
135   vnet_main_t *vnm = vnet_get_main ();
136   vnet_hw_interface_t *hi;
137   u8 *tag;
138
139   hi = vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
140
141   mp = vl_msg_api_alloc (sizeof (*mp));
142   memset (mp, 0, sizeof (*mp));
143   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
144   mp->sw_if_index = ntohl (swif->sw_if_index);
145   mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
146   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
147   mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
148   mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
149                      VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
150   mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
151                     VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
152   mp->link_mtu = ntohs (hi->max_packet_bytes);
153   mp->context = context;
154
155   strncpy ((char *) mp->interface_name,
156            (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
157
158   /* Send the L2 address for ethernet physical intfcs */
159   if (swif->sup_sw_if_index == swif->sw_if_index
160       && hi->hw_class_index == ethernet_hw_interface_class.index)
161     {
162       ethernet_main_t *em = ethernet_get_main (am->vlib_main);
163       ethernet_interface_t *ei;
164
165       ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
166       ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
167       clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
168       mp->l2_address_length = ntohl (sizeof (ei->address));
169     }
170   else if (swif->sup_sw_if_index != swif->sw_if_index)
171     {
172       vnet_sub_interface_t *sub = &swif->sub;
173       mp->sub_id = ntohl (sub->id);
174       mp->sub_dot1ad = sub->eth.flags.dot1ad;
175       mp->sub_number_of_tags =
176         sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
177       mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
178       mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
179       mp->sub_exact_match = sub->eth.flags.exact_match;
180       mp->sub_default = sub->eth.flags.default_sub;
181       mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
182       mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
183
184       /* vlan tag rewrite data */
185       u32 vtr_op = L2_VTR_DISABLED;
186       u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
187
188       if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
189                      &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
190         {
191           // error - default to disabled
192           mp->vtr_op = ntohl (L2_VTR_DISABLED);
193           clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
194                         swif->sw_if_index);
195         }
196       else
197         {
198           mp->vtr_op = ntohl (vtr_op);
199           mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
200           mp->vtr_tag1 = ntohl (vtr_tag1);
201           mp->vtr_tag2 = ntohl (vtr_tag2);
202         }
203     }
204
205   tag = vnet_get_sw_interface_tag (vnm, swif->sw_if_index);
206   if (tag)
207     strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
208
209   vl_msg_api_send_shmem (q, (u8 *) & mp);
210 }
211
212 static void
213 vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
214 {
215   vpe_api_main_t *am = &vpe_api_main;
216   vnet_sw_interface_t *swif;
217   vnet_interface_main_t *im = &am->vnet_main->interface_main;
218   u8 *filter_string = 0, *name_string = 0;
219   unix_shared_memory_queue_t *q;
220   char *strcasestr (char *, char *);    /* lnx hdr file botch */
221
222   q = vl_api_client_index_to_input_queue (mp->client_index);
223
224   if (q == 0)
225     return;
226
227   if (mp->name_filter_valid)
228     {
229       mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
230       filter_string = format (0, "%s%c", mp->name_filter, 0);
231     }
232
233   /* *INDENT-OFF* */
234   pool_foreach (swif, im->sw_interfaces,
235   ({
236     name_string = format (name_string, "%U%c",
237                           format_vnet_sw_interface_name,
238                           am->vnet_main, swif, 0);
239
240     if (mp->name_filter_valid == 0 ||
241         strcasestr((char *) name_string, (char *) filter_string)) {
242
243       send_sw_interface_details (am, q, swif, name_string, mp->context);
244     }
245     _vec_len (name_string) = 0;
246   }));
247   /* *INDENT-ON* */
248
249   vec_free (name_string);
250   vec_free (filter_string);
251 }
252
253 static void
254   vl_api_sw_interface_add_del_address_t_handler
255   (vl_api_sw_interface_add_del_address_t * mp)
256 {
257   vlib_main_t *vm = vlib_get_main ();
258   vl_api_sw_interface_add_del_address_reply_t *rmp;
259   int rv = 0;
260   u32 is_del;
261
262   VALIDATE_SW_IF_INDEX (mp);
263
264   is_del = mp->is_add == 0;
265
266   if (mp->del_all)
267     ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
268   else if (mp->is_ipv6)
269     ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
270                                    (void *) mp->address,
271                                    mp->address_length, is_del);
272   else
273     ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
274                                    (void *) mp->address,
275                                    mp->address_length, is_del);
276
277   BAD_SW_IF_INDEX_LABEL;
278
279   REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
280 }
281
282 void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
283 void
284 stats_dslock_with_hint (int hint, int tag)
285 {
286 }
287
288 void stats_dsunlock (void) __attribute__ ((weak));
289 void
290 stats_dsunlock (void)
291 {
292 }
293
294 static void
295 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
296 {
297   int rv = 0;
298   u32 table_id = ntohl (mp->vrf_id);
299   u32 sw_if_index = ntohl (mp->sw_if_index);
300   vl_api_sw_interface_set_table_reply_t *rmp;
301   u32 fib_index;
302
303   VALIDATE_SW_IF_INDEX (mp);
304
305   stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
306
307   if (mp->is_ipv6)
308     {
309       fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
310                                                      table_id);
311
312       vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
313       ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
314     }
315   else
316     {
317
318       fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
319                                                      table_id);
320
321       vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
322       ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
323     }
324   stats_dsunlock ();
325
326   BAD_SW_IF_INDEX_LABEL;
327
328   REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
329 }
330
331 static void
332 send_sw_interface_get_table_reply (unix_shared_memory_queue_t * q,
333                                    u32 context, int retval, u32 vrf_id)
334 {
335   vl_api_sw_interface_get_table_reply_t *mp;
336
337   mp = vl_msg_api_alloc (sizeof (*mp));
338   memset (mp, 0, sizeof (*mp));
339   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
340   mp->context = context;
341   mp->retval = htonl (retval);
342   mp->vrf_id = htonl (vrf_id);
343
344   vl_msg_api_send_shmem (q, (u8 *) & mp);
345 }
346
347 static void
348 vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
349 {
350   unix_shared_memory_queue_t *q;
351   fib_table_t *fib_table = 0;
352   u32 sw_if_index = ~0;
353   u32 fib_index = ~0;
354   u32 table_id = ~0;
355   fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
356   int rv = 0;
357
358   q = vl_api_client_index_to_input_queue (mp->client_index);
359   if (q == 0)
360     return;
361
362   VALIDATE_SW_IF_INDEX (mp);
363
364   sw_if_index = ntohl (mp->sw_if_index);
365
366   if (mp->is_ipv6)
367     fib_proto = FIB_PROTOCOL_IP6;
368
369   fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
370   if (fib_index != ~0)
371     {
372       fib_table = fib_table_get (fib_index, fib_proto);
373       table_id = fib_table->ft_table_id;
374     }
375
376   BAD_SW_IF_INDEX_LABEL;
377
378   send_sw_interface_get_table_reply (q, mp->context, rv, table_id);
379 }
380
381 static void vl_api_sw_interface_set_unnumbered_t_handler
382   (vl_api_sw_interface_set_unnumbered_t * mp)
383 {
384   vl_api_sw_interface_set_unnumbered_reply_t *rmp;
385   int rv = 0;
386   vnet_sw_interface_t *si;
387   vnet_main_t *vnm = vnet_get_main ();
388   u32 sw_if_index, unnumbered_sw_if_index;
389
390   sw_if_index = ntohl (mp->sw_if_index);
391   unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
392
393   /*
394    * The API message field names are backwards from
395    * the underlying data structure names.
396    * It's not worth changing them now.
397    */
398   if (pool_is_free_index (vnm->interface_main.sw_interfaces,
399                           unnumbered_sw_if_index))
400     {
401       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
402       goto done;
403     }
404
405   /* Only check the "use loop0" field when setting the binding */
406   if (mp->is_add &&
407       pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
408     {
409       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
410       goto done;
411     }
412
413   si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
414
415   if (mp->is_add)
416     {
417       si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
418       si->unnumbered_sw_if_index = sw_if_index;
419       ip4_sw_interface_enable_disable (unnumbered_sw_if_index, 1);
420       ip6_sw_interface_enable_disable (unnumbered_sw_if_index, 1);
421     }
422   else
423     {
424       si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
425       si->unnumbered_sw_if_index = (u32) ~ 0;
426       ip4_sw_interface_enable_disable (unnumbered_sw_if_index, 0);
427       ip6_sw_interface_enable_disable (unnumbered_sw_if_index, 0);
428     }
429
430 done:
431   REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
432 }
433
434 static void
435 vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
436                                            mp)
437 {
438   vl_api_sw_interface_clear_stats_reply_t *rmp;
439
440   vnet_main_t *vnm = vnet_get_main ();
441   vnet_interface_main_t *im = &vnm->interface_main;
442   vlib_simple_counter_main_t *sm;
443   vlib_combined_counter_main_t *cm;
444   static vnet_main_t **my_vnet_mains;
445   int i, j, n_counters;
446   int rv = 0;
447
448   if (mp->sw_if_index != ~0)
449     VALIDATE_SW_IF_INDEX (mp);
450
451   vec_reset_length (my_vnet_mains);
452
453   for (i = 0; i < vec_len (vnet_mains); i++)
454     {
455       if (vnet_mains[i])
456         vec_add1 (my_vnet_mains, vnet_mains[i]);
457     }
458
459   if (vec_len (vnet_mains) == 0)
460     vec_add1 (my_vnet_mains, vnm);
461
462   n_counters = vec_len (im->combined_sw_if_counters);
463
464   for (j = 0; j < n_counters; j++)
465     {
466       for (i = 0; i < vec_len (my_vnet_mains); i++)
467         {
468           im = &my_vnet_mains[i]->interface_main;
469           cm = im->combined_sw_if_counters + j;
470           if (mp->sw_if_index == (u32) ~ 0)
471             vlib_clear_combined_counters (cm);
472           else
473             vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
474         }
475     }
476
477   n_counters = vec_len (im->sw_if_counters);
478
479   for (j = 0; j < n_counters; j++)
480     {
481       for (i = 0; i < vec_len (my_vnet_mains); i++)
482         {
483           im = &my_vnet_mains[i]->interface_main;
484           sm = im->sw_if_counters + j;
485           if (mp->sw_if_index == (u32) ~ 0)
486             vlib_clear_simple_counters (sm);
487           else
488             vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
489         }
490     }
491
492   BAD_SW_IF_INDEX_LABEL;
493
494   REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
495 }
496
497 #define API_LINK_STATE_EVENT 1
498 #define API_ADMIN_UP_DOWN_EVENT 2
499
500 static int
501 event_data_cmp (void *a1, void *a2)
502 {
503   uword *e1 = a1;
504   uword *e2 = a2;
505
506   return (word) e1[0] - (word) e2[0];
507 }
508
509 static void
510 send_sw_interface_flags (vpe_api_main_t * am,
511                          unix_shared_memory_queue_t * q,
512                          vnet_sw_interface_t * swif)
513 {
514   vl_api_sw_interface_set_flags_t *mp;
515   vnet_main_t *vnm = am->vnet_main;
516
517   vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
518                                                        swif->sw_if_index);
519   mp = vl_msg_api_alloc (sizeof (*mp));
520   memset (mp, 0, sizeof (*mp));
521   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SET_FLAGS);
522   mp->sw_if_index = ntohl (swif->sw_if_index);
523
524   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
525   mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
526   vl_msg_api_send_shmem (q, (u8 *) & mp);
527 }
528
529 static uword
530 link_state_process (vlib_main_t * vm,
531                     vlib_node_runtime_t * rt, vlib_frame_t * f)
532 {
533   vpe_api_main_t *vam = &vpe_api_main;
534   vnet_main_t *vnm = vam->vnet_main;
535   vnet_sw_interface_t *swif;
536   uword *event_data = 0;
537   vpe_client_registration_t *reg;
538   int i;
539   u32 prev_sw_if_index;
540   unix_shared_memory_queue_t *q;
541
542   vam->link_state_process_up = 1;
543
544   while (1)
545     {
546       vlib_process_wait_for_event (vm);
547
548       /* Unified list of changed link or admin state sw_if_indices */
549       vlib_process_get_events_with_type
550         (vm, &event_data, API_LINK_STATE_EVENT);
551       vlib_process_get_events_with_type
552         (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
553
554       /* Sort, so we can eliminate duplicates */
555       vec_sort_with_function (event_data, event_data_cmp);
556
557       prev_sw_if_index = ~0;
558
559       for (i = 0; i < vec_len (event_data); i++)
560         {
561           /* Only one message per swif */
562           if (prev_sw_if_index == event_data[i])
563             continue;
564           prev_sw_if_index = event_data[i];
565
566           /* *INDENT-OFF* */
567           pool_foreach(reg, vam->interface_events_registrations,
568           ({
569             q = vl_api_client_index_to_input_queue (reg->client_index);
570             if (q)
571               {
572                 /* sw_interface may be deleted already */
573                 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
574                                          event_data[i]))
575                   {
576                     swif = vnet_get_sw_interface (vnm, event_data[i]);
577                     send_sw_interface_flags (vam, q, swif);
578                   }
579               }
580           }));
581           /* *INDENT-ON* */
582         }
583       vec_reset_length (event_data);
584     }
585
586   return 0;
587 }
588
589 static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
590                                             u32 flags);
591 static clib_error_t *admin_up_down_function (vnet_main_t * vm,
592                                              u32 hw_if_index, u32 flags);
593
594 /* *INDENT-OFF* */
595 VLIB_REGISTER_NODE (link_state_process_node,static) = {
596   .function = link_state_process,
597   .type = VLIB_NODE_TYPE_PROCESS,
598   .name = "vpe-link-state-process",
599 };
600 /* *INDENT-ON* */
601
602 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
603 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
604
605 static clib_error_t *
606 link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
607 {
608   vpe_api_main_t *vam = &vpe_api_main;
609   vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
610
611   if (vam->link_state_process_up)
612     vlib_process_signal_event (vam->vlib_main,
613                                link_state_process_node.index,
614                                API_LINK_STATE_EVENT, hi->sw_if_index);
615   return 0;
616 }
617
618 static clib_error_t *
619 admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
620 {
621   vpe_api_main_t *vam = &vpe_api_main;
622
623   /*
624    * Note: it's perfectly fair to set a subif admin up / admin down.
625    * Note the subtle distinction between this routine and the previous
626    * routine.
627    */
628   if (vam->link_state_process_up)
629     vlib_process_signal_event (vam->vlib_main,
630                                link_state_process_node.index,
631                                API_ADMIN_UP_DOWN_EVENT, sw_if_index);
632   return 0;
633 }
634
635 static void vl_api_sw_interface_tag_add_del_t_handler
636   (vl_api_sw_interface_tag_add_del_t * mp)
637 {
638   vnet_main_t *vnm = vnet_get_main ();
639   vl_api_sw_interface_tag_add_del_reply_t *rmp;
640   int rv = 0;
641   u8 *tag;
642   u32 sw_if_index = ntohl (mp->sw_if_index);
643
644   VALIDATE_SW_IF_INDEX (mp);
645
646   if (mp->is_add)
647     {
648       if (mp->tag[0] == 0)
649         {
650           rv = VNET_API_ERROR_INVALID_VALUE;
651           goto out;
652         }
653
654       mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
655       tag = format (0, "%s%c", mp->tag, 0);
656       vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
657     }
658   else
659     vnet_clear_sw_interface_tag (vnm, sw_if_index);
660
661   BAD_SW_IF_INDEX_LABEL;
662 out:
663   REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
664 }
665
666 static void
667 vl_api_sw_interface_details_t_handler (vl_api_sw_interface_details_t * mp)
668 {
669   clib_warning ("BUG");
670 }
671
672 /*
673  * vpe_api_hookup
674  * Add vpe's API message handlers to the table.
675  * vlib has alread mapped shared memory and
676  * added the client registration handlers.
677  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
678  */
679 #define vl_msg_name_crc_list
680 #include <vnet/vnet_all_api_h.h>
681 #undef vl_msg_name_crc_list
682
683 static void
684 setup_message_id_table (api_main_t * am)
685 {
686 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
687   foreach_vl_msg_name_crc_interface;
688 #undef _
689 }
690
691 pub_sub_handler (interface_events, INTERFACE_EVENTS);
692
693 static clib_error_t *
694 interface_api_hookup (vlib_main_t * vm)
695 {
696   api_main_t *am = &api_main;
697
698 #define _(N,n)                                                  \
699     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
700                            vl_api_##n##_t_handler,              \
701                            vl_noop_handler,                     \
702                            vl_api_##n##_t_endian,               \
703                            vl_api_##n##_t_print,                \
704                            sizeof(vl_api_##n##_t), 1);
705   foreach_vpe_api_msg;
706 #undef _
707
708   /*
709    * Set up the (msg_name, crc, message-id) table
710    */
711   setup_message_id_table (am);
712
713   return 0;
714 }
715
716 VLIB_API_INIT_FUNCTION (interface_api_hookup);
717
718 /*
719  * fd.io coding-style-patch-verification: ON
720  *
721  * Local Variables:
722  * eval: (c-set-style "gnu")
723  * End:
724  */