blob: 9949f83660b93200f4fe8f53b02e2c6b8f38e96d [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>
Dave Barachf1586612017-03-30 09:33:01 -040049vpe_api_main_t vpe_api_main;
Dave Barachaff70772016-10-31 11:59:07 -040050
Dave Barach6d963c22016-12-05 09:50:05 -050051#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) \
Dave Barach6d963c22016-12-05 09:50:05 -050056_(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address) \
57_(SW_INTERFACE_SET_TABLE, sw_interface_set_table) \
Juraj Slobodadfc19232016-12-05 13:20:37 +010058_(SW_INTERFACE_GET_TABLE, sw_interface_get_table) \
Dave Barach6d963c22016-12-05 09:50:05 -050059_(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered) \
60_(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats) \
Jon Loeliger10c273b2017-03-30 08:39:33 -050061_(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del) \
62_(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address)
Dave Barachaff70772016-10-31 11:59:07 -040063
64static void
65vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
66{
67 vl_api_sw_interface_set_flags_reply_t *rmp;
68 vnet_main_t *vnm = vnet_get_main ();
69 int rv = 0;
70 clib_error_t *error;
71 u16 flags;
72
73 VALIDATE_SW_IF_INDEX (mp);
74
75 flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
76
77 error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
78 if (error)
79 {
80 rv = -1;
81 clib_error_report (error);
82 }
83
84 BAD_SW_IF_INDEX_LABEL;
85 REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
86}
87
Matus Fabiand162f3d2016-12-05 01:05:35 -080088static void
89vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
90{
91 vl_api_sw_interface_set_mtu_reply_t *rmp;
92 vnet_main_t *vnm = vnet_get_main ();
93 u32 flags = ETHERNET_INTERFACE_FLAG_MTU;
94 u32 sw_if_index = ntohl (mp->sw_if_index);
95 u16 mtu = ntohs (mp->mtu);
96 ethernet_main_t *em = &ethernet_main;
97 int rv = 0;
98
99 VALIDATE_SW_IF_INDEX (mp);
100
101 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, sw_if_index);
102 ethernet_interface_t *eif = ethernet_get_interface (em, sw_if_index);
103
104 if (!eif)
105 {
106 rv = VNET_API_ERROR_FEATURE_DISABLED;
107 goto bad_sw_if_index;
108 }
109
110 if (mtu < hi->min_supported_packet_bytes)
111 {
112 rv = VNET_API_ERROR_INVALID_VALUE;
113 goto bad_sw_if_index;
114 }
115
116 if (mtu > hi->max_supported_packet_bytes)
117 {
118 rv = VNET_API_ERROR_INVALID_VALUE;
119 goto bad_sw_if_index;
120 }
121
122 if (hi->max_packet_bytes != mtu)
123 {
124 hi->max_packet_bytes = mtu;
125 ethernet_set_flags (vnm, sw_if_index, flags);
126 }
127
128 BAD_SW_IF_INDEX_LABEL;
129 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
130}
131
Dave Barach6d963c22016-12-05 09:50:05 -0500132static void
133send_sw_interface_details (vpe_api_main_t * am,
134 unix_shared_memory_queue_t * q,
135 vnet_sw_interface_t * swif,
136 u8 * interface_name, u32 context)
137{
Eyal Bari1c82cd42017-03-14 14:39:51 +0200138 vnet_hw_interface_t *hi =
139 vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500140
Eyal Bari1c82cd42017-03-14 14:39:51 +0200141 vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barach6d963c22016-12-05 09:50:05 -0500142 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
Pavel Kotucek65e84572017-01-16 17:01:56 +0100205 /* pbb tag rewrite data */
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100206 ethernet_header_t eth_hdr;
Pavel Kotucek65e84572017-01-16 17:01:56 +0100207 u32 vtr_op = L2_VTR_DISABLED;
208 u16 outer_tag = 0;
Pavel Kotucek65e84572017-01-16 17:01:56 +0100209 u16 b_vlanid = 0;
210 u32 i_sid = 0;
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100211 memset (&eth_hdr, 0, sizeof (eth_hdr));
Pavel Kotucek65e84572017-01-16 17:01:56 +0100212
213 if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100214 &vtr_op, &outer_tag, &eth_hdr, &b_vlanid, &i_sid))
Pavel Kotucek65e84572017-01-16 17:01:56 +0100215 {
216 mp->sub_dot1ah = 1;
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100217 clib_memcpy (mp->b_dmac, eth_hdr.dst_address,
218 sizeof (eth_hdr.dst_address));
219 clib_memcpy (mp->b_smac, eth_hdr.src_address,
220 sizeof (eth_hdr.src_address));
Pavel Kotucek65e84572017-01-16 17:01:56 +0100221 mp->b_vlanid = b_vlanid;
222 mp->i_sid = i_sid;
223 }
224
Eyal Bari1c82cd42017-03-14 14:39:51 +0200225 u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500226 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
232static void
233vl_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;
Dave Barach6d963c22016-12-05 09:50:05 -0500238
Eyal Bari1c82cd42017-03-14 14:39:51 +0200239 unix_shared_memory_queue_t *q =
240 vl_api_client_index_to_input_queue (mp->client_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500241 if (q == 0)
242 return;
243
Eyal Bari1c82cd42017-03-14 14:39:51 +0200244 u8 *filter = 0, *name = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500245 if (mp->name_filter_valid)
246 {
247 mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
Eyal Bari1c82cd42017-03-14 14:39:51 +0200248 filter = format (0, "%s%c", mp->name_filter, 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500249 }
250
Eyal Bari1c82cd42017-03-14 14:39:51 +0200251 char *strcasestr (char *, char *); /* lnx hdr file botch */
Dave Barach6d963c22016-12-05 09:50:05 -0500252 /* *INDENT-OFF* */
253 pool_foreach (swif, im->sw_interfaces,
254 ({
Eyal Bari1c82cd42017-03-14 14:39:51 +0200255 if (!vnet_swif_is_api_visible (swif))
256 continue;
257 vec_reset_length(name);
258 name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
259 swif, 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500260
Eyal Bari1c82cd42017-03-14 14:39:51 +0200261 if (filter && !strcasestr((char *) name, (char *) filter))
262 continue;
Dave Barach6d963c22016-12-05 09:50:05 -0500263
Eyal Bari1c82cd42017-03-14 14:39:51 +0200264 send_sw_interface_details (am, q, swif, name, mp->context);
Dave Barach6d963c22016-12-05 09:50:05 -0500265 }));
266 /* *INDENT-ON* */
267
Eyal Bari1c82cd42017-03-14 14:39:51 +0200268 vec_free (name);
269 vec_free (filter);
Dave Barach6d963c22016-12-05 09:50:05 -0500270}
271
272static void
273 vl_api_sw_interface_add_del_address_t_handler
274 (vl_api_sw_interface_add_del_address_t * mp)
275{
276 vlib_main_t *vm = vlib_get_main ();
277 vl_api_sw_interface_add_del_address_reply_t *rmp;
278 int rv = 0;
279 u32 is_del;
280
281 VALIDATE_SW_IF_INDEX (mp);
282
283 is_del = mp->is_add == 0;
284
285 if (mp->del_all)
286 ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
287 else if (mp->is_ipv6)
288 ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
289 (void *) mp->address,
290 mp->address_length, is_del);
291 else
292 ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
293 (void *) mp->address,
294 mp->address_length, is_del);
295
296 BAD_SW_IF_INDEX_LABEL;
297
298 REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
299}
300
301void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
302void
303stats_dslock_with_hint (int hint, int tag)
304{
305}
306
307void stats_dsunlock (void) __attribute__ ((weak));
308void
309stats_dsunlock (void)
310{
311}
312
313static void
314vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
315{
316 int rv = 0;
317 u32 table_id = ntohl (mp->vrf_id);
318 u32 sw_if_index = ntohl (mp->sw_if_index);
319 vl_api_sw_interface_set_table_reply_t *rmp;
Neale Ranns4008ac92017-02-13 23:20:04 -0800320 CLIB_UNUSED (ip_interface_address_t * ia);
Dave Barach6d963c22016-12-05 09:50:05 -0500321 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 {
Neale Ranns4008ac92017-02-13 23:20:04 -0800329 /* *INDENT-OFF* */
330 foreach_ip_interface_address (&ip6_main.lookup_main,
331 ia, sw_if_index,
332 1 /* honor unnumbered */ ,
333 ({
334 rv = VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE;
335 goto done;
336 }));
337 /* *INDENT-ON* */
338
Dave Barach6d963c22016-12-05 09:50:05 -0500339 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
340 table_id);
Dave Barach6d963c22016-12-05 09:50:05 -0500341 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
342 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
Neale Ranns358425b2017-02-20 09:42:36 -0800343
Neale Ranns4008ac92017-02-13 23:20:04 -0800344 fib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
345 table_id);
346 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
347 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = fib_index;
Dave Barach6d963c22016-12-05 09:50:05 -0500348 }
349 else
350 {
Neale Ranns4008ac92017-02-13 23:20:04 -0800351 /* *INDENT-OFF* */
352 foreach_ip_interface_address (&ip4_main.lookup_main,
353 ia, sw_if_index,
354 1 /* honor unnumbered */ ,
355 ({
356 rv = VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE;
357 goto done;
358 }));
359 /* *INDENT-ON* */
Dave Barach6d963c22016-12-05 09:50:05 -0500360
361 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
362 table_id);
Dave Barach6d963c22016-12-05 09:50:05 -0500363 vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
364 ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
Neale Ranns4008ac92017-02-13 23:20:04 -0800365
366 fib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
367 table_id);
368 vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
369 ip4_main.mfib_index_by_sw_if_index[sw_if_index] = fib_index;
Dave Barach6d963c22016-12-05 09:50:05 -0500370 }
Neale Ranns4008ac92017-02-13 23:20:04 -0800371
372done:
Dave Barach6d963c22016-12-05 09:50:05 -0500373 stats_dsunlock ();
374
375 BAD_SW_IF_INDEX_LABEL;
376
377 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
378}
379
Juraj Slobodadfc19232016-12-05 13:20:37 +0100380static void
381send_sw_interface_get_table_reply (unix_shared_memory_queue_t * q,
382 u32 context, int retval, u32 vrf_id)
383{
384 vl_api_sw_interface_get_table_reply_t *mp;
385
386 mp = vl_msg_api_alloc (sizeof (*mp));
387 memset (mp, 0, sizeof (*mp));
388 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
389 mp->context = context;
390 mp->retval = htonl (retval);
391 mp->vrf_id = htonl (vrf_id);
392
393 vl_msg_api_send_shmem (q, (u8 *) & mp);
394}
395
396static void
397vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
398{
399 unix_shared_memory_queue_t *q;
400 fib_table_t *fib_table = 0;
401 u32 sw_if_index = ~0;
402 u32 fib_index = ~0;
403 u32 table_id = ~0;
404 fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
405 int rv = 0;
406
407 q = vl_api_client_index_to_input_queue (mp->client_index);
408 if (q == 0)
409 return;
410
411 VALIDATE_SW_IF_INDEX (mp);
412
413 sw_if_index = ntohl (mp->sw_if_index);
414
415 if (mp->is_ipv6)
416 fib_proto = FIB_PROTOCOL_IP6;
417
418 fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
419 if (fib_index != ~0)
420 {
421 fib_table = fib_table_get (fib_index, fib_proto);
422 table_id = fib_table->ft_table_id;
423 }
424
425 BAD_SW_IF_INDEX_LABEL;
426
427 send_sw_interface_get_table_reply (q, mp->context, rv, table_id);
428}
429
Dave Barach6d963c22016-12-05 09:50:05 -0500430static void vl_api_sw_interface_set_unnumbered_t_handler
431 (vl_api_sw_interface_set_unnumbered_t * mp)
432{
433 vl_api_sw_interface_set_unnumbered_reply_t *rmp;
434 int rv = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500435 vnet_main_t *vnm = vnet_get_main ();
Eyal Bari1c82cd42017-03-14 14:39:51 +0200436 u32 sw_if_index = ntohl (mp->sw_if_index);
437 u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
Neale Ranns898273f2017-03-18 02:57:38 -0700438 u32 was_unnum;
Dave Barach6d963c22016-12-05 09:50:05 -0500439
440 /*
441 * The API message field names are backwards from
442 * the underlying data structure names.
443 * It's not worth changing them now.
444 */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200445 if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500446 {
447 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
448 goto done;
449 }
450
451 /* Only check the "use loop0" field when setting the binding */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200452 if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500453 {
454 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
455 goto done;
456 }
457
Eyal Bari1c82cd42017-03-14 14:39:51 +0200458 vnet_sw_interface_t *si =
459 vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
Neale Ranns898273f2017-03-18 02:57:38 -0700460 was_unnum = (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED);
Dave Barach6d963c22016-12-05 09:50:05 -0500461
462 if (mp->is_add)
463 {
464 si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
465 si->unnumbered_sw_if_index = sw_if_index;
Neale Ranns4b919a52017-03-11 05:55:21 -0800466
467 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
468 [unnumbered_sw_if_index] =
469 ip4_main.
470 lookup_main.if_address_pool_index_by_sw_if_index[sw_if_index];
471 ip6_main.
472 lookup_main.if_address_pool_index_by_sw_if_index
473 [unnumbered_sw_if_index] =
474 ip6_main.
475 lookup_main.if_address_pool_index_by_sw_if_index[sw_if_index];
Dave Barach6d963c22016-12-05 09:50:05 -0500476 }
477 else
478 {
479 si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
480 si->unnumbered_sw_if_index = (u32) ~ 0;
Neale Ranns4b919a52017-03-11 05:55:21 -0800481
482 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
483 [unnumbered_sw_if_index] = ~0;
484 ip6_main.lookup_main.if_address_pool_index_by_sw_if_index
485 [unnumbered_sw_if_index] = ~0;
Dave Barach6d963c22016-12-05 09:50:05 -0500486 }
Neale Ranns898273f2017-03-18 02:57:38 -0700487
488 if (was_unnum != (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
489 {
490 ip4_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add);
491 ip6_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add);
492 }
Dave Barach6d963c22016-12-05 09:50:05 -0500493
494done:
495 REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
496}
497
498static void
499vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
500 mp)
501{
502 vl_api_sw_interface_clear_stats_reply_t *rmp;
503
504 vnet_main_t *vnm = vnet_get_main ();
505 vnet_interface_main_t *im = &vnm->interface_main;
506 vlib_simple_counter_main_t *sm;
507 vlib_combined_counter_main_t *cm;
508 static vnet_main_t **my_vnet_mains;
509 int i, j, n_counters;
510 int rv = 0;
511
512 if (mp->sw_if_index != ~0)
513 VALIDATE_SW_IF_INDEX (mp);
514
515 vec_reset_length (my_vnet_mains);
516
517 for (i = 0; i < vec_len (vnet_mains); i++)
518 {
519 if (vnet_mains[i])
520 vec_add1 (my_vnet_mains, vnet_mains[i]);
521 }
522
523 if (vec_len (vnet_mains) == 0)
524 vec_add1 (my_vnet_mains, vnm);
525
526 n_counters = vec_len (im->combined_sw_if_counters);
527
528 for (j = 0; j < n_counters; j++)
529 {
530 for (i = 0; i < vec_len (my_vnet_mains); i++)
531 {
532 im = &my_vnet_mains[i]->interface_main;
533 cm = im->combined_sw_if_counters + j;
534 if (mp->sw_if_index == (u32) ~ 0)
535 vlib_clear_combined_counters (cm);
536 else
537 vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
538 }
539 }
540
541 n_counters = vec_len (im->sw_if_counters);
542
543 for (j = 0; j < n_counters; j++)
544 {
545 for (i = 0; i < vec_len (my_vnet_mains); i++)
546 {
547 im = &my_vnet_mains[i]->interface_main;
548 sm = im->sw_if_counters + j;
549 if (mp->sw_if_index == (u32) ~ 0)
550 vlib_clear_simple_counters (sm);
551 else
552 vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
553 }
554 }
555
556 BAD_SW_IF_INDEX_LABEL;
557
558 REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
559}
560
561#define API_LINK_STATE_EVENT 1
562#define API_ADMIN_UP_DOWN_EVENT 2
563
564static int
565event_data_cmp (void *a1, void *a2)
566{
567 uword *e1 = a1;
568 uword *e2 = a2;
569
570 return (word) e1[0] - (word) e2[0];
571}
572
573static void
Neale Rannsa07bd702017-08-07 07:53:49 -0700574send_sw_interface_event (vpe_api_main_t * am,
Neale Rannsd292ab12017-08-15 12:29:48 -0700575 vpe_client_registration_t * reg,
Dave Barach6d963c22016-12-05 09:50:05 -0500576 unix_shared_memory_queue_t * q,
577 vnet_sw_interface_t * swif)
578{
Neale Rannsa07bd702017-08-07 07:53:49 -0700579 vl_api_sw_interface_event_t *mp;
Dave Barach6d963c22016-12-05 09:50:05 -0500580 vnet_main_t *vnm = am->vnet_main;
581
582 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
583 swif->sw_if_index);
584 mp = vl_msg_api_alloc (sizeof (*mp));
585 memset (mp, 0, sizeof (*mp));
Neale Rannsa07bd702017-08-07 07:53:49 -0700586 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
Dave Barach6d963c22016-12-05 09:50:05 -0500587 mp->sw_if_index = ntohl (swif->sw_if_index);
Neale Rannsd292ab12017-08-15 12:29:48 -0700588 mp->client_index = reg->client_index;
589 mp->pid = reg->client_pid;
Dave Barach6d963c22016-12-05 09:50:05 -0500590
591 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
592 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
593 vl_msg_api_send_shmem (q, (u8 *) & mp);
594}
595
596static uword
597link_state_process (vlib_main_t * vm,
598 vlib_node_runtime_t * rt, vlib_frame_t * f)
599{
600 vpe_api_main_t *vam = &vpe_api_main;
601 vnet_main_t *vnm = vam->vnet_main;
602 vnet_sw_interface_t *swif;
603 uword *event_data = 0;
604 vpe_client_registration_t *reg;
605 int i;
606 u32 prev_sw_if_index;
607 unix_shared_memory_queue_t *q;
608
609 vam->link_state_process_up = 1;
610
611 while (1)
612 {
613 vlib_process_wait_for_event (vm);
614
615 /* Unified list of changed link or admin state sw_if_indices */
616 vlib_process_get_events_with_type
617 (vm, &event_data, API_LINK_STATE_EVENT);
618 vlib_process_get_events_with_type
619 (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
620
621 /* Sort, so we can eliminate duplicates */
622 vec_sort_with_function (event_data, event_data_cmp);
623
624 prev_sw_if_index = ~0;
625
626 for (i = 0; i < vec_len (event_data); i++)
627 {
628 /* Only one message per swif */
629 if (prev_sw_if_index == event_data[i])
630 continue;
631 prev_sw_if_index = event_data[i];
632
633 /* *INDENT-OFF* */
634 pool_foreach(reg, vam->interface_events_registrations,
635 ({
636 q = vl_api_client_index_to_input_queue (reg->client_index);
637 if (q)
638 {
639 /* sw_interface may be deleted already */
640 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
641 event_data[i]))
642 {
643 swif = vnet_get_sw_interface (vnm, event_data[i]);
Neale Rannsd292ab12017-08-15 12:29:48 -0700644 send_sw_interface_event (vam, reg, q, swif);
Dave Barach6d963c22016-12-05 09:50:05 -0500645 }
646 }
647 }));
648 /* *INDENT-ON* */
649 }
650 vec_reset_length (event_data);
651 }
652
653 return 0;
654}
655
656static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
657 u32 flags);
658static clib_error_t *admin_up_down_function (vnet_main_t * vm,
659 u32 hw_if_index, u32 flags);
660
661/* *INDENT-OFF* */
662VLIB_REGISTER_NODE (link_state_process_node,static) = {
663 .function = link_state_process,
664 .type = VLIB_NODE_TYPE_PROCESS,
665 .name = "vpe-link-state-process",
666};
667/* *INDENT-ON* */
668
669VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
670VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
671
672static clib_error_t *
673link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
674{
675 vpe_api_main_t *vam = &vpe_api_main;
676 vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
677
678 if (vam->link_state_process_up)
679 vlib_process_signal_event (vam->vlib_main,
680 link_state_process_node.index,
681 API_LINK_STATE_EVENT, hi->sw_if_index);
682 return 0;
683}
684
685static clib_error_t *
686admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
687{
688 vpe_api_main_t *vam = &vpe_api_main;
689
690 /*
691 * Note: it's perfectly fair to set a subif admin up / admin down.
692 * Note the subtle distinction between this routine and the previous
693 * routine.
694 */
695 if (vam->link_state_process_up)
696 vlib_process_signal_event (vam->vlib_main,
697 link_state_process_node.index,
698 API_ADMIN_UP_DOWN_EVENT, sw_if_index);
699 return 0;
700}
701
702static void vl_api_sw_interface_tag_add_del_t_handler
703 (vl_api_sw_interface_tag_add_del_t * mp)
704{
705 vnet_main_t *vnm = vnet_get_main ();
706 vl_api_sw_interface_tag_add_del_reply_t *rmp;
707 int rv = 0;
708 u8 *tag;
709 u32 sw_if_index = ntohl (mp->sw_if_index);
710
711 VALIDATE_SW_IF_INDEX (mp);
712
713 if (mp->is_add)
714 {
715 if (mp->tag[0] == 0)
716 {
717 rv = VNET_API_ERROR_INVALID_VALUE;
718 goto out;
719 }
720
721 mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
722 tag = format (0, "%s%c", mp->tag, 0);
723 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
724 }
725 else
726 vnet_clear_sw_interface_tag (vnm, sw_if_index);
727
728 BAD_SW_IF_INDEX_LABEL;
729out:
730 REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
731}
732
Jon Loeliger10c273b2017-03-30 08:39:33 -0500733static void vl_api_sw_interface_set_mac_address_t_handler
734 (vl_api_sw_interface_set_mac_address_t * mp)
735{
736 vl_api_sw_interface_set_mac_address_reply_t *rmp;
737 vnet_main_t *vnm = vnet_get_main ();
738 u32 sw_if_index = ntohl (mp->sw_if_index);
739 u64 mac;
740 clib_error_t *error;
741 int rv = 0;
742
743 VALIDATE_SW_IF_INDEX (mp);
744
745 mac = ((u64) mp->mac_address[0] << (8 * 0)
746 | (u64) mp->mac_address[1] << (8 * 1)
747 | (u64) mp->mac_address[2] << (8 * 2)
748 | (u64) mp->mac_address[3] << (8 * 3)
749 | (u64) mp->mac_address[4] << (8 * 4)
750 | (u64) mp->mac_address[5] << (8 * 5));
751
752 error = vnet_hw_interface_change_mac_address (vnm, sw_if_index, mac);
753 if (error)
754 {
755 rv = VNET_API_ERROR_UNIMPLEMENTED;
756 clib_error_report (error);
757 goto out;
758 }
759
760 BAD_SW_IF_INDEX_LABEL;
761out:
762 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
763}
764
Dave Barachaff70772016-10-31 11:59:07 -0400765/*
766 * vpe_api_hookup
767 * Add vpe's API message handlers to the table.
768 * vlib has alread mapped shared memory and
769 * added the client registration handlers.
770 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
771 */
772#define vl_msg_name_crc_list
Dave Barachb5e8a772016-12-06 12:04:42 -0500773#include <vnet/interface.api.h>
Dave Barachaff70772016-10-31 11:59:07 -0400774#undef vl_msg_name_crc_list
775
776static void
777setup_message_id_table (api_main_t * am)
778{
779#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
780 foreach_vl_msg_name_crc_interface;
781#undef _
782}
783
Dave Barach6d963c22016-12-05 09:50:05 -0500784pub_sub_handler (interface_events, INTERFACE_EVENTS);
785
Dave Barachaff70772016-10-31 11:59:07 -0400786static clib_error_t *
787interface_api_hookup (vlib_main_t * vm)
788{
789 api_main_t *am = &api_main;
790
791#define _(N,n) \
792 vl_msg_api_set_handlers(VL_API_##N, #n, \
793 vl_api_##n##_t_handler, \
794 vl_noop_handler, \
795 vl_api_##n##_t_endian, \
796 vl_api_##n##_t_print, \
797 sizeof(vl_api_##n##_t), 1);
798 foreach_vpe_api_msg;
799#undef _
800
801 /*
802 * Set up the (msg_name, crc, message-id) table
803 */
804 setup_message_id_table (am);
805
806 return 0;
807}
808
809VLIB_API_INIT_FUNCTION (interface_api_hookup);
810
811/*
812 * fd.io coding-style-patch-verification: ON
813 *
814 * Local Variables:
815 * eval: (c-set-style "gnu")
816 * End:
817 */