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