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