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