Clean up binary api message handler registration issues
[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_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   /* pbb tag rewrite data */
206   u32 vtr_op = L2_VTR_DISABLED;
207   u16 outer_tag = 0;
208   u8 b_dmac[6];
209   u8 b_smac[6];
210   u16 b_vlanid = 0;
211   u32 i_sid = 0;
212   memset (b_dmac, 0, sizeof (b_dmac));
213   memset (b_smac, 0, sizeof (b_smac));
214
215   if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
216                   &vtr_op, &outer_tag, b_dmac, b_smac, &b_vlanid, &i_sid))
217     {
218       mp->sub_dot1ah = 1;
219       clib_memcpy (mp->b_dmac, b_dmac, sizeof (b_dmac));
220       clib_memcpy (mp->b_smac, b_smac, sizeof (b_smac));
221       mp->b_vlanid = b_vlanid;
222       mp->i_sid = i_sid;
223     }
224
225   tag = vnet_get_sw_interface_tag (vnm, swif->sw_if_index);
226   if (tag)
227     strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
228
229   vl_msg_api_send_shmem (q, (u8 *) & mp);
230 }
231
232 static void
233 vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
234 {
235   vpe_api_main_t *am = &vpe_api_main;
236   vnet_sw_interface_t *swif;
237   vnet_interface_main_t *im = &am->vnet_main->interface_main;
238   u8 *filter_string = 0, *name_string = 0;
239   unix_shared_memory_queue_t *q;
240   char *strcasestr (char *, char *);    /* lnx hdr file botch */
241
242   q = vl_api_client_index_to_input_queue (mp->client_index);
243
244   if (q == 0)
245     return;
246
247   if (mp->name_filter_valid)
248     {
249       mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
250       filter_string = format (0, "%s%c", mp->name_filter, 0);
251     }
252
253   /* *INDENT-OFF* */
254   pool_foreach (swif, im->sw_interfaces,
255   ({
256     name_string = format (name_string, "%U%c",
257                           format_vnet_sw_interface_name,
258                           am->vnet_main, swif, 0);
259
260     if (mp->name_filter_valid == 0 ||
261         strcasestr((char *) name_string, (char *) filter_string)) {
262
263       send_sw_interface_details (am, q, swif, name_string, mp->context);
264     }
265     _vec_len (name_string) = 0;
266   }));
267   /* *INDENT-ON* */
268
269   vec_free (name_string);
270   vec_free (filter_string);
271 }
272
273 static void
274   vl_api_sw_interface_add_del_address_t_handler
275   (vl_api_sw_interface_add_del_address_t * mp)
276 {
277   vlib_main_t *vm = vlib_get_main ();
278   vl_api_sw_interface_add_del_address_reply_t *rmp;
279   int rv = 0;
280   u32 is_del;
281
282   VALIDATE_SW_IF_INDEX (mp);
283
284   is_del = mp->is_add == 0;
285
286   if (mp->del_all)
287     ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
288   else if (mp->is_ipv6)
289     ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
290                                    (void *) mp->address,
291                                    mp->address_length, is_del);
292   else
293     ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
294                                    (void *) mp->address,
295                                    mp->address_length, is_del);
296
297   BAD_SW_IF_INDEX_LABEL;
298
299   REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
300 }
301
302 void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
303 void
304 stats_dslock_with_hint (int hint, int tag)
305 {
306 }
307
308 void stats_dsunlock (void) __attribute__ ((weak));
309 void
310 stats_dsunlock (void)
311 {
312 }
313
314 static void
315 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
316 {
317   int rv = 0;
318   u32 table_id = ntohl (mp->vrf_id);
319   u32 sw_if_index = ntohl (mp->sw_if_index);
320   vl_api_sw_interface_set_table_reply_t *rmp;
321   u32 fib_index;
322
323   VALIDATE_SW_IF_INDEX (mp);
324
325   stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
326
327   if (mp->is_ipv6)
328     {
329       fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
330                                                      table_id);
331
332       vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
333       ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
334     }
335   else
336     {
337
338       fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
339                                                      table_id);
340
341       vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
342       ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
343     }
344   stats_dsunlock ();
345
346   BAD_SW_IF_INDEX_LABEL;
347
348   REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
349 }
350
351 static void
352 send_sw_interface_get_table_reply (unix_shared_memory_queue_t * q,
353                                    u32 context, int retval, u32 vrf_id)
354 {
355   vl_api_sw_interface_get_table_reply_t *mp;
356
357   mp = vl_msg_api_alloc (sizeof (*mp));
358   memset (mp, 0, sizeof (*mp));
359   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
360   mp->context = context;
361   mp->retval = htonl (retval);
362   mp->vrf_id = htonl (vrf_id);
363
364   vl_msg_api_send_shmem (q, (u8 *) & mp);
365 }
366
367 static void
368 vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
369 {
370   unix_shared_memory_queue_t *q;
371   fib_table_t *fib_table = 0;
372   u32 sw_if_index = ~0;
373   u32 fib_index = ~0;
374   u32 table_id = ~0;
375   fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
376   int rv = 0;
377
378   q = vl_api_client_index_to_input_queue (mp->client_index);
379   if (q == 0)
380     return;
381
382   VALIDATE_SW_IF_INDEX (mp);
383
384   sw_if_index = ntohl (mp->sw_if_index);
385
386   if (mp->is_ipv6)
387     fib_proto = FIB_PROTOCOL_IP6;
388
389   fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
390   if (fib_index != ~0)
391     {
392       fib_table = fib_table_get (fib_index, fib_proto);
393       table_id = fib_table->ft_table_id;
394     }
395
396   BAD_SW_IF_INDEX_LABEL;
397
398   send_sw_interface_get_table_reply (q, mp->context, rv, table_id);
399 }
400
401 static void vl_api_sw_interface_set_unnumbered_t_handler
402   (vl_api_sw_interface_set_unnumbered_t * mp)
403 {
404   vl_api_sw_interface_set_unnumbered_reply_t *rmp;
405   int rv = 0;
406   vnet_sw_interface_t *si;
407   vnet_main_t *vnm = vnet_get_main ();
408   u32 sw_if_index, unnumbered_sw_if_index;
409
410   sw_if_index = ntohl (mp->sw_if_index);
411   unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
412
413   /*
414    * The API message field names are backwards from
415    * the underlying data structure names.
416    * It's not worth changing them now.
417    */
418   if (pool_is_free_index (vnm->interface_main.sw_interfaces,
419                           unnumbered_sw_if_index))
420     {
421       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
422       goto done;
423     }
424
425   /* Only check the "use loop0" field when setting the binding */
426   if (mp->is_add &&
427       pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
428     {
429       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
430       goto done;
431     }
432
433   si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
434
435   if (mp->is_add)
436     {
437       si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
438       si->unnumbered_sw_if_index = sw_if_index;
439       ip4_sw_interface_enable_disable (unnumbered_sw_if_index, 1);
440       ip6_sw_interface_enable_disable (unnumbered_sw_if_index, 1);
441     }
442   else
443     {
444       si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
445       si->unnumbered_sw_if_index = (u32) ~ 0;
446       ip4_sw_interface_enable_disable (unnumbered_sw_if_index, 0);
447       ip6_sw_interface_enable_disable (unnumbered_sw_if_index, 0);
448     }
449
450 done:
451   REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
452 }
453
454 static void
455 vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
456                                            mp)
457 {
458   vl_api_sw_interface_clear_stats_reply_t *rmp;
459
460   vnet_main_t *vnm = vnet_get_main ();
461   vnet_interface_main_t *im = &vnm->interface_main;
462   vlib_simple_counter_main_t *sm;
463   vlib_combined_counter_main_t *cm;
464   static vnet_main_t **my_vnet_mains;
465   int i, j, n_counters;
466   int rv = 0;
467
468   if (mp->sw_if_index != ~0)
469     VALIDATE_SW_IF_INDEX (mp);
470
471   vec_reset_length (my_vnet_mains);
472
473   for (i = 0; i < vec_len (vnet_mains); i++)
474     {
475       if (vnet_mains[i])
476         vec_add1 (my_vnet_mains, vnet_mains[i]);
477     }
478
479   if (vec_len (vnet_mains) == 0)
480     vec_add1 (my_vnet_mains, vnm);
481
482   n_counters = vec_len (im->combined_sw_if_counters);
483
484   for (j = 0; j < n_counters; j++)
485     {
486       for (i = 0; i < vec_len (my_vnet_mains); i++)
487         {
488           im = &my_vnet_mains[i]->interface_main;
489           cm = im->combined_sw_if_counters + j;
490           if (mp->sw_if_index == (u32) ~ 0)
491             vlib_clear_combined_counters (cm);
492           else
493             vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
494         }
495     }
496
497   n_counters = vec_len (im->sw_if_counters);
498
499   for (j = 0; j < n_counters; j++)
500     {
501       for (i = 0; i < vec_len (my_vnet_mains); i++)
502         {
503           im = &my_vnet_mains[i]->interface_main;
504           sm = im->sw_if_counters + j;
505           if (mp->sw_if_index == (u32) ~ 0)
506             vlib_clear_simple_counters (sm);
507           else
508             vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
509         }
510     }
511
512   BAD_SW_IF_INDEX_LABEL;
513
514   REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
515 }
516
517 #define API_LINK_STATE_EVENT 1
518 #define API_ADMIN_UP_DOWN_EVENT 2
519
520 static int
521 event_data_cmp (void *a1, void *a2)
522 {
523   uword *e1 = a1;
524   uword *e2 = a2;
525
526   return (word) e1[0] - (word) e2[0];
527 }
528
529 static void
530 send_sw_interface_flags (vpe_api_main_t * am,
531                          unix_shared_memory_queue_t * q,
532                          vnet_sw_interface_t * swif)
533 {
534   vl_api_sw_interface_set_flags_t *mp;
535   vnet_main_t *vnm = am->vnet_main;
536
537   vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
538                                                        swif->sw_if_index);
539   mp = vl_msg_api_alloc (sizeof (*mp));
540   memset (mp, 0, sizeof (*mp));
541   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SET_FLAGS);
542   mp->sw_if_index = ntohl (swif->sw_if_index);
543
544   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
545   mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
546   vl_msg_api_send_shmem (q, (u8 *) & mp);
547 }
548
549 static uword
550 link_state_process (vlib_main_t * vm,
551                     vlib_node_runtime_t * rt, vlib_frame_t * f)
552 {
553   vpe_api_main_t *vam = &vpe_api_main;
554   vnet_main_t *vnm = vam->vnet_main;
555   vnet_sw_interface_t *swif;
556   uword *event_data = 0;
557   vpe_client_registration_t *reg;
558   int i;
559   u32 prev_sw_if_index;
560   unix_shared_memory_queue_t *q;
561
562   vam->link_state_process_up = 1;
563
564   while (1)
565     {
566       vlib_process_wait_for_event (vm);
567
568       /* Unified list of changed link or admin state sw_if_indices */
569       vlib_process_get_events_with_type
570         (vm, &event_data, API_LINK_STATE_EVENT);
571       vlib_process_get_events_with_type
572         (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
573
574       /* Sort, so we can eliminate duplicates */
575       vec_sort_with_function (event_data, event_data_cmp);
576
577       prev_sw_if_index = ~0;
578
579       for (i = 0; i < vec_len (event_data); i++)
580         {
581           /* Only one message per swif */
582           if (prev_sw_if_index == event_data[i])
583             continue;
584           prev_sw_if_index = event_data[i];
585
586           /* *INDENT-OFF* */
587           pool_foreach(reg, vam->interface_events_registrations,
588           ({
589             q = vl_api_client_index_to_input_queue (reg->client_index);
590             if (q)
591               {
592                 /* sw_interface may be deleted already */
593                 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
594                                          event_data[i]))
595                   {
596                     swif = vnet_get_sw_interface (vnm, event_data[i]);
597                     send_sw_interface_flags (vam, q, swif);
598                   }
599               }
600           }));
601           /* *INDENT-ON* */
602         }
603       vec_reset_length (event_data);
604     }
605
606   return 0;
607 }
608
609 static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
610                                             u32 flags);
611 static clib_error_t *admin_up_down_function (vnet_main_t * vm,
612                                              u32 hw_if_index, u32 flags);
613
614 /* *INDENT-OFF* */
615 VLIB_REGISTER_NODE (link_state_process_node,static) = {
616   .function = link_state_process,
617   .type = VLIB_NODE_TYPE_PROCESS,
618   .name = "vpe-link-state-process",
619 };
620 /* *INDENT-ON* */
621
622 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
623 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
624
625 static clib_error_t *
626 link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
627 {
628   vpe_api_main_t *vam = &vpe_api_main;
629   vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
630
631   if (vam->link_state_process_up)
632     vlib_process_signal_event (vam->vlib_main,
633                                link_state_process_node.index,
634                                API_LINK_STATE_EVENT, hi->sw_if_index);
635   return 0;
636 }
637
638 static clib_error_t *
639 admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
640 {
641   vpe_api_main_t *vam = &vpe_api_main;
642
643   /*
644    * Note: it's perfectly fair to set a subif admin up / admin down.
645    * Note the subtle distinction between this routine and the previous
646    * routine.
647    */
648   if (vam->link_state_process_up)
649     vlib_process_signal_event (vam->vlib_main,
650                                link_state_process_node.index,
651                                API_ADMIN_UP_DOWN_EVENT, sw_if_index);
652   return 0;
653 }
654
655 static void vl_api_sw_interface_tag_add_del_t_handler
656   (vl_api_sw_interface_tag_add_del_t * mp)
657 {
658   vnet_main_t *vnm = vnet_get_main ();
659   vl_api_sw_interface_tag_add_del_reply_t *rmp;
660   int rv = 0;
661   u8 *tag;
662   u32 sw_if_index = ntohl (mp->sw_if_index);
663
664   VALIDATE_SW_IF_INDEX (mp);
665
666   if (mp->is_add)
667     {
668       if (mp->tag[0] == 0)
669         {
670           rv = VNET_API_ERROR_INVALID_VALUE;
671           goto out;
672         }
673
674       mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
675       tag = format (0, "%s%c", mp->tag, 0);
676       vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
677     }
678   else
679     vnet_clear_sw_interface_tag (vnm, sw_if_index);
680
681   BAD_SW_IF_INDEX_LABEL;
682 out:
683   REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
684 }
685
686 /*
687  * vpe_api_hookup
688  * Add vpe's API message handlers to the table.
689  * vlib has alread mapped shared memory and
690  * added the client registration handlers.
691  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
692  */
693 #define vl_msg_name_crc_list
694 #include <vnet/interface.api.h>
695 #undef vl_msg_name_crc_list
696
697 static void
698 setup_message_id_table (api_main_t * am)
699 {
700 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
701   foreach_vl_msg_name_crc_interface;
702 #undef _
703 }
704
705 pub_sub_handler (interface_events, INTERFACE_EVENTS);
706
707 static clib_error_t *
708 interface_api_hookup (vlib_main_t * vm)
709 {
710   api_main_t *am = &api_main;
711
712 #define _(N,n)                                                  \
713     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
714                            vl_api_##n##_t_handler,              \
715                            vl_noop_handler,                     \
716                            vl_api_##n##_t_endian,               \
717                            vl_api_##n##_t_print,                \
718                            sizeof(vl_api_##n##_t), 1);
719   foreach_vpe_api_msg;
720 #undef _
721
722   /*
723    * Set up the (msg_name, crc, message-id) table
724    */
725   setup_message_id_table (am);
726
727   return 0;
728 }
729
730 VLIB_API_INIT_FUNCTION (interface_api_hookup);
731
732 /*
733  * fd.io coding-style-patch-verification: ON
734  *
735  * Local Variables:
736  * eval: (c-set-style "gnu")
737  * End:
738  */