blob: c19f0a84297e75e9bd765fecf77d3df13c9e8884 [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
Andrew Yourtchenko81d1e272017-08-22 13:10:01 +0200101 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
102 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
103 {
104 rv = VNET_API_ERROR_INVALID_VALUE;
105 goto bad_sw_if_index;
106 }
107
108 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index);
109 ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index);
Matus Fabiand162f3d2016-12-05 01:05:35 -0800110
111 if (!eif)
112 {
113 rv = VNET_API_ERROR_FEATURE_DISABLED;
114 goto bad_sw_if_index;
115 }
116
117 if (mtu < hi->min_supported_packet_bytes)
118 {
119 rv = VNET_API_ERROR_INVALID_VALUE;
120 goto bad_sw_if_index;
121 }
122
123 if (mtu > hi->max_supported_packet_bytes)
124 {
125 rv = VNET_API_ERROR_INVALID_VALUE;
126 goto bad_sw_if_index;
127 }
128
129 if (hi->max_packet_bytes != mtu)
130 {
131 hi->max_packet_bytes = mtu;
Andrew Yourtchenko81d1e272017-08-22 13:10:01 +0200132 ethernet_set_flags (vnm, si->hw_if_index, flags);
Matus Fabiand162f3d2016-12-05 01:05:35 -0800133 }
134
135 BAD_SW_IF_INDEX_LABEL;
136 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
137}
138
Dave Barach6d963c22016-12-05 09:50:05 -0500139static void
140send_sw_interface_details (vpe_api_main_t * am,
Dave Barach59b25652017-09-10 15:04:27 -0400141 vl_api_registration_t * rp,
Dave Barach6d963c22016-12-05 09:50:05 -0500142 vnet_sw_interface_t * swif,
143 u8 * interface_name, u32 context)
144{
Eyal Bari1c82cd42017-03-14 14:39:51 +0200145 vnet_hw_interface_t *hi =
146 vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500147
Eyal Bari1c82cd42017-03-14 14:39:51 +0200148 vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barach6d963c22016-12-05 09:50:05 -0500149 memset (mp, 0, sizeof (*mp));
150 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
151 mp->sw_if_index = ntohl (swif->sw_if_index);
152 mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
153 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
154 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
155 mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
156 VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
157 mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
158 VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
159 mp->link_mtu = ntohs (hi->max_packet_bytes);
160 mp->context = context;
161
162 strncpy ((char *) mp->interface_name,
163 (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
164
165 /* Send the L2 address for ethernet physical intfcs */
166 if (swif->sup_sw_if_index == swif->sw_if_index
167 && hi->hw_class_index == ethernet_hw_interface_class.index)
168 {
169 ethernet_main_t *em = ethernet_get_main (am->vlib_main);
170 ethernet_interface_t *ei;
171
172 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
173 ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
174 clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
175 mp->l2_address_length = ntohl (sizeof (ei->address));
176 }
177 else if (swif->sup_sw_if_index != swif->sw_if_index)
178 {
179 vnet_sub_interface_t *sub = &swif->sub;
180 mp->sub_id = ntohl (sub->id);
181 mp->sub_dot1ad = sub->eth.flags.dot1ad;
182 mp->sub_number_of_tags =
183 sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
184 mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
185 mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
186 mp->sub_exact_match = sub->eth.flags.exact_match;
187 mp->sub_default = sub->eth.flags.default_sub;
188 mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
189 mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
190
191 /* vlan tag rewrite data */
192 u32 vtr_op = L2_VTR_DISABLED;
193 u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
194
195 if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
196 &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
197 {
198 // error - default to disabled
199 mp->vtr_op = ntohl (L2_VTR_DISABLED);
200 clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
201 swif->sw_if_index);
202 }
203 else
204 {
205 mp->vtr_op = ntohl (vtr_op);
206 mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
207 mp->vtr_tag1 = ntohl (vtr_tag1);
208 mp->vtr_tag2 = ntohl (vtr_tag2);
209 }
210 }
211
Pavel Kotucek65e84572017-01-16 17:01:56 +0100212 /* pbb tag rewrite data */
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100213 ethernet_header_t eth_hdr;
Pavel Kotucek65e84572017-01-16 17:01:56 +0100214 u32 vtr_op = L2_VTR_DISABLED;
215 u16 outer_tag = 0;
Pavel Kotucek65e84572017-01-16 17:01:56 +0100216 u16 b_vlanid = 0;
217 u32 i_sid = 0;
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100218 memset (&eth_hdr, 0, sizeof (eth_hdr));
Pavel Kotucek65e84572017-01-16 17:01:56 +0100219
220 if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100221 &vtr_op, &outer_tag, &eth_hdr, &b_vlanid, &i_sid))
Pavel Kotucek65e84572017-01-16 17:01:56 +0100222 {
223 mp->sub_dot1ah = 1;
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100224 clib_memcpy (mp->b_dmac, eth_hdr.dst_address,
225 sizeof (eth_hdr.dst_address));
226 clib_memcpy (mp->b_smac, eth_hdr.src_address,
227 sizeof (eth_hdr.src_address));
Pavel Kotucek65e84572017-01-16 17:01:56 +0100228 mp->b_vlanid = b_vlanid;
229 mp->i_sid = i_sid;
230 }
231
Eyal Bari1c82cd42017-03-14 14:39:51 +0200232 u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500233 if (tag)
234 strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
235
Dave Barach59b25652017-09-10 15:04:27 -0400236 vl_msg_api_send (rp, (u8 *) mp);
Dave Barach6d963c22016-12-05 09:50:05 -0500237}
238
239static void
240vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
241{
242 vpe_api_main_t *am = &vpe_api_main;
243 vnet_sw_interface_t *swif;
244 vnet_interface_main_t *im = &am->vnet_main->interface_main;
Dave Barach59b25652017-09-10 15:04:27 -0400245 vl_api_registration_t *rp;
Dave Barach6d963c22016-12-05 09:50:05 -0500246
Dave Barach59b25652017-09-10 15:04:27 -0400247 rp = vl_api_client_index_to_registration (mp->client_index);
248
249 if (rp == 0)
250 {
251 clib_warning ("Client %d AWOL", mp->client_index);
252 return;
253 }
Dave Barach6d963c22016-12-05 09:50:05 -0500254
Eyal Bari1c82cd42017-03-14 14:39:51 +0200255 u8 *filter = 0, *name = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500256 if (mp->name_filter_valid)
257 {
258 mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
Eyal Bari1c82cd42017-03-14 14:39:51 +0200259 filter = format (0, "%s%c", mp->name_filter, 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500260 }
261
Eyal Bari1c82cd42017-03-14 14:39:51 +0200262 char *strcasestr (char *, char *); /* lnx hdr file botch */
Dave Barach6d963c22016-12-05 09:50:05 -0500263 /* *INDENT-OFF* */
264 pool_foreach (swif, im->sw_interfaces,
265 ({
Eyal Bari1c82cd42017-03-14 14:39:51 +0200266 if (!vnet_swif_is_api_visible (swif))
267 continue;
268 vec_reset_length(name);
269 name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
270 swif, 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500271
Eyal Bari1c82cd42017-03-14 14:39:51 +0200272 if (filter && !strcasestr((char *) name, (char *) filter))
273 continue;
Dave Barach6d963c22016-12-05 09:50:05 -0500274
Dave Barach59b25652017-09-10 15:04:27 -0400275 send_sw_interface_details (am, rp, swif, name, mp->context);
Dave Barach6d963c22016-12-05 09:50:05 -0500276 }));
277 /* *INDENT-ON* */
278
Eyal Bari1c82cd42017-03-14 14:39:51 +0200279 vec_free (name);
280 vec_free (filter);
Dave Barach6d963c22016-12-05 09:50:05 -0500281}
282
283static void
284 vl_api_sw_interface_add_del_address_t_handler
285 (vl_api_sw_interface_add_del_address_t * mp)
286{
287 vlib_main_t *vm = vlib_get_main ();
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500288 vnet_main_t *vnm = vnet_get_main ();
Dave Barach6d963c22016-12-05 09:50:05 -0500289 vl_api_sw_interface_add_del_address_reply_t *rmp;
290 int rv = 0;
291 u32 is_del;
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500292 clib_error_t *error = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500293
294 VALIDATE_SW_IF_INDEX (mp);
295
296 is_del = mp->is_add == 0;
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500297 vnm->api_errno = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500298
299 if (mp->del_all)
300 ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
301 else if (mp->is_ipv6)
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500302 error = ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
303 (void *) mp->address,
304 mp->address_length, is_del);
Dave Barach6d963c22016-12-05 09:50:05 -0500305 else
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500306 error = ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
307 (void *) mp->address,
308 mp->address_length, is_del);
309
310 if (error)
311 {
312 rv = vnm->api_errno;
313 clib_error_report (error);
314 goto done;
315 }
Dave Barach6d963c22016-12-05 09:50:05 -0500316
317 BAD_SW_IF_INDEX_LABEL;
318
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500319done:
Dave Barach6d963c22016-12-05 09:50:05 -0500320 REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
321}
322
323void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
324void
325stats_dslock_with_hint (int hint, int tag)
326{
327}
328
329void stats_dsunlock (void) __attribute__ ((weak));
330void
331stats_dsunlock (void)
332{
333}
334
335static void
336vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
337{
Dave Barach6d963c22016-12-05 09:50:05 -0500338 vl_api_sw_interface_set_table_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700339 u32 sw_if_index = ntohl (mp->sw_if_index);
340 u32 table_id = ntohl (mp->vrf_id);
341 int rv = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500342
343 VALIDATE_SW_IF_INDEX (mp);
344
345 stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
346
347 if (mp->is_ipv6)
Neale Ranns15002542017-09-10 04:39:11 -0700348 rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
Dave Barach6d963c22016-12-05 09:50:05 -0500349 else
Neale Ranns15002542017-09-10 04:39:11 -0700350 rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
Dave Barach6d963c22016-12-05 09:50:05 -0500351
Dave Barach6d963c22016-12-05 09:50:05 -0500352 stats_dsunlock ();
353
354 BAD_SW_IF_INDEX_LABEL;
355
356 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
357}
358
Neale Ranns15002542017-09-10 04:39:11 -0700359int
360ip_table_bind (fib_protocol_t fproto,
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700361 u32 sw_if_index, u32 table_id, u8 is_api)
Neale Ranns15002542017-09-10 04:39:11 -0700362{
363 CLIB_UNUSED (ip_interface_address_t * ia);
364 u32 fib_index, mfib_index;
365 fib_source_t src;
366 mfib_source_t msrc;
367
368 if (is_api)
369 {
370 src = FIB_SOURCE_API;
371 msrc = MFIB_SOURCE_API;
372 }
373 else
374 {
375 src = FIB_SOURCE_CLI;
376 msrc = MFIB_SOURCE_CLI;
377 }
378
Florin Corasd0a59722017-10-15 17:41:21 +0000379 /*
380 * This is temporary whilst I do the song and dance with the CSIT version
381 */
382 if (0 != table_id)
383 {
384 fib_index = fib_table_find_or_create_and_lock (fproto, table_id, src);
385 mfib_index =
386 mfib_table_find_or_create_and_lock (fproto, table_id, msrc);
387 }
388 else
389 {
390 fib_index = 0;
391 mfib_index = 0;
392 }
393
394 /*
395 * This if table does not exist = error is what we want in the end.
396 */
397 /* fib_index = fib_table_find (fproto, table_id); */
398 /* mfib_index = mfib_table_find (fproto, table_id); */
399
400 /* if (~0 == fib_index || ~0 == mfib_index) */
401 /* { */
402 /* return (VNET_API_ERROR_NO_SUCH_FIB); */
403 /* } */
404
Neale Ranns15002542017-09-10 04:39:11 -0700405 if (FIB_PROTOCOL_IP6 == fproto)
406 {
407 /*
408 * If the interface already has in IP address, then a change int
409 * VRF is not allowed. The IP address applied must first be removed.
410 * We do not do that automatically here, since VPP has no knowledge
411 * of whether thoses subnets are valid in the destination VRF.
412 */
413 /* *INDENT-OFF* */
414 foreach_ip_interface_address (&ip6_main.lookup_main,
415 ia, sw_if_index,
416 1 /* honor unnumbered */ ,
417 ({
418 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
419 }));
420 /* *INDENT-ON* */
421
422 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
423 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
424
425 /*
426 * tell those that are interested that the binding is changing.
427 */
428 ip6_table_bind_callback_t *cb;
429 vec_foreach (cb, ip6_main.table_bind_callbacks)
430 cb->function (&ip6_main, cb->function_opaque,
431 sw_if_index,
432 fib_index,
433 ip6_main.fib_index_by_sw_if_index[sw_if_index]);
434
435 if (0 == table_id)
436 {
437 /* reset back to default */
438 if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
439 fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
440 FIB_PROTOCOL_IP6, src);
441 if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
442 mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
443 [sw_if_index], FIB_PROTOCOL_IP6, msrc);
444
445 }
446 else
447 {
448 /* we need to lock the table now it's inuse */
449 fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
450 mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
451 }
452
453 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
454 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
455 }
456 else
457 {
458 /*
459 * If the interface already has in IP address, then a change int
460 * VRF is not allowed. The IP address applied must first be removed.
461 * We do not do that automatically here, since VPP has no knowledge
462 * of whether thoses subnets are valid in the destination VRF.
463 */
464 /* *INDENT-OFF* */
465 foreach_ip_interface_address (&ip4_main.lookup_main,
466 ia, sw_if_index,
467 1 /* honor unnumbered */ ,
468 ({
469 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
470 }));
471 /* *INDENT-ON* */
472
473 vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
474 vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
475
476 /*
477 * tell those that are interested that the binding is changing.
478 */
479 ip4_table_bind_callback_t *cb;
480 vec_foreach (cb, ip4_main.table_bind_callbacks)
481 cb->function (&ip4_main, cb->function_opaque,
482 sw_if_index,
483 fib_index,
484 ip4_main.fib_index_by_sw_if_index[sw_if_index]);
485
486 if (0 == table_id)
487 {
488 /* reset back to default */
489 if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
490 fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
491 FIB_PROTOCOL_IP4, src);
492 if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
493 mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
494 [sw_if_index], FIB_PROTOCOL_IP4, msrc);
495
496 }
497 else
498 {
499 /* we need to lock the table now it's inuse */
500 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
501 table_id, src);
502
503 mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
504 table_id, msrc);
505 }
506
507 ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
508 ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
509 }
510
Florin Corasd0a59722017-10-15 17:41:21 +0000511 /*
512 * Temporary. undo the locks from the find and create at the staart
513 */
514 if (0 != table_id)
515 {
516 fib_table_unlock (fib_index, fproto, src);
517 mfib_table_unlock (mfib_index, fproto, msrc);
518 }
519
Neale Ranns15002542017-09-10 04:39:11 -0700520 return (0);
521}
522
Juraj Slobodadfc19232016-12-05 13:20:37 +0100523static void
524send_sw_interface_get_table_reply (unix_shared_memory_queue_t * q,
525 u32 context, int retval, u32 vrf_id)
526{
527 vl_api_sw_interface_get_table_reply_t *mp;
528
529 mp = vl_msg_api_alloc (sizeof (*mp));
530 memset (mp, 0, sizeof (*mp));
531 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
532 mp->context = context;
533 mp->retval = htonl (retval);
534 mp->vrf_id = htonl (vrf_id);
535
536 vl_msg_api_send_shmem (q, (u8 *) & mp);
537}
538
539static void
540vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
541{
542 unix_shared_memory_queue_t *q;
543 fib_table_t *fib_table = 0;
544 u32 sw_if_index = ~0;
545 u32 fib_index = ~0;
546 u32 table_id = ~0;
547 fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
548 int rv = 0;
549
550 q = vl_api_client_index_to_input_queue (mp->client_index);
551 if (q == 0)
552 return;
553
554 VALIDATE_SW_IF_INDEX (mp);
555
556 sw_if_index = ntohl (mp->sw_if_index);
557
558 if (mp->is_ipv6)
559 fib_proto = FIB_PROTOCOL_IP6;
560
561 fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
562 if (fib_index != ~0)
563 {
564 fib_table = fib_table_get (fib_index, fib_proto);
565 table_id = fib_table->ft_table_id;
566 }
567
568 BAD_SW_IF_INDEX_LABEL;
569
570 send_sw_interface_get_table_reply (q, mp->context, rv, table_id);
571}
572
Dave Barach6d963c22016-12-05 09:50:05 -0500573static void vl_api_sw_interface_set_unnumbered_t_handler
574 (vl_api_sw_interface_set_unnumbered_t * mp)
575{
576 vl_api_sw_interface_set_unnumbered_reply_t *rmp;
577 int rv = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500578 vnet_main_t *vnm = vnet_get_main ();
Eyal Bari1c82cd42017-03-14 14:39:51 +0200579 u32 sw_if_index = ntohl (mp->sw_if_index);
580 u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
Neale Ranns898273f2017-03-18 02:57:38 -0700581 u32 was_unnum;
Dave Barach6d963c22016-12-05 09:50:05 -0500582
583 /*
584 * The API message field names are backwards from
585 * the underlying data structure names.
586 * It's not worth changing them now.
587 */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200588 if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500589 {
590 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
591 goto done;
592 }
593
594 /* Only check the "use loop0" field when setting the binding */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200595 if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500596 {
597 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
598 goto done;
599 }
600
Eyal Bari1c82cd42017-03-14 14:39:51 +0200601 vnet_sw_interface_t *si =
602 vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
Neale Ranns898273f2017-03-18 02:57:38 -0700603 was_unnum = (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED);
Dave Barach6d963c22016-12-05 09:50:05 -0500604
605 if (mp->is_add)
606 {
607 si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
608 si->unnumbered_sw_if_index = sw_if_index;
Neale Ranns4b919a52017-03-11 05:55:21 -0800609
610 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
611 [unnumbered_sw_if_index] =
612 ip4_main.
613 lookup_main.if_address_pool_index_by_sw_if_index[sw_if_index];
614 ip6_main.
615 lookup_main.if_address_pool_index_by_sw_if_index
616 [unnumbered_sw_if_index] =
617 ip6_main.
618 lookup_main.if_address_pool_index_by_sw_if_index[sw_if_index];
Dave Barach6d963c22016-12-05 09:50:05 -0500619 }
620 else
621 {
622 si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
623 si->unnumbered_sw_if_index = (u32) ~ 0;
Neale Ranns4b919a52017-03-11 05:55:21 -0800624
625 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
626 [unnumbered_sw_if_index] = ~0;
627 ip6_main.lookup_main.if_address_pool_index_by_sw_if_index
628 [unnumbered_sw_if_index] = ~0;
Dave Barach6d963c22016-12-05 09:50:05 -0500629 }
Neale Ranns898273f2017-03-18 02:57:38 -0700630
631 if (was_unnum != (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
632 {
633 ip4_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add);
634 ip6_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add);
635 }
Dave Barach6d963c22016-12-05 09:50:05 -0500636
637done:
638 REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
639}
640
641static void
642vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
643 mp)
644{
645 vl_api_sw_interface_clear_stats_reply_t *rmp;
646
647 vnet_main_t *vnm = vnet_get_main ();
648 vnet_interface_main_t *im = &vnm->interface_main;
649 vlib_simple_counter_main_t *sm;
650 vlib_combined_counter_main_t *cm;
651 static vnet_main_t **my_vnet_mains;
652 int i, j, n_counters;
653 int rv = 0;
654
655 if (mp->sw_if_index != ~0)
656 VALIDATE_SW_IF_INDEX (mp);
657
658 vec_reset_length (my_vnet_mains);
659
660 for (i = 0; i < vec_len (vnet_mains); i++)
661 {
662 if (vnet_mains[i])
663 vec_add1 (my_vnet_mains, vnet_mains[i]);
664 }
665
666 if (vec_len (vnet_mains) == 0)
667 vec_add1 (my_vnet_mains, vnm);
668
669 n_counters = vec_len (im->combined_sw_if_counters);
670
671 for (j = 0; j < n_counters; j++)
672 {
673 for (i = 0; i < vec_len (my_vnet_mains); i++)
674 {
675 im = &my_vnet_mains[i]->interface_main;
676 cm = im->combined_sw_if_counters + j;
677 if (mp->sw_if_index == (u32) ~ 0)
678 vlib_clear_combined_counters (cm);
679 else
680 vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
681 }
682 }
683
684 n_counters = vec_len (im->sw_if_counters);
685
686 for (j = 0; j < n_counters; j++)
687 {
688 for (i = 0; i < vec_len (my_vnet_mains); i++)
689 {
690 im = &my_vnet_mains[i]->interface_main;
691 sm = im->sw_if_counters + j;
692 if (mp->sw_if_index == (u32) ~ 0)
693 vlib_clear_simple_counters (sm);
694 else
695 vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
696 }
697 }
698
699 BAD_SW_IF_INDEX_LABEL;
700
701 REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
702}
703
704#define API_LINK_STATE_EVENT 1
705#define API_ADMIN_UP_DOWN_EVENT 2
706
707static int
708event_data_cmp (void *a1, void *a2)
709{
710 uword *e1 = a1;
711 uword *e2 = a2;
712
713 return (word) e1[0] - (word) e2[0];
714}
715
716static void
Neale Rannsa07bd702017-08-07 07:53:49 -0700717send_sw_interface_event (vpe_api_main_t * am,
Neale Rannsd292ab12017-08-15 12:29:48 -0700718 vpe_client_registration_t * reg,
Dave Barach6d963c22016-12-05 09:50:05 -0500719 unix_shared_memory_queue_t * q,
720 vnet_sw_interface_t * swif)
721{
Neale Rannsa07bd702017-08-07 07:53:49 -0700722 vl_api_sw_interface_event_t *mp;
Dave Barach6d963c22016-12-05 09:50:05 -0500723 vnet_main_t *vnm = am->vnet_main;
724
725 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
726 swif->sw_if_index);
727 mp = vl_msg_api_alloc (sizeof (*mp));
728 memset (mp, 0, sizeof (*mp));
Neale Rannsa07bd702017-08-07 07:53:49 -0700729 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
Dave Barach6d963c22016-12-05 09:50:05 -0500730 mp->sw_if_index = ntohl (swif->sw_if_index);
Neale Rannsd292ab12017-08-15 12:29:48 -0700731 mp->client_index = reg->client_index;
732 mp->pid = reg->client_pid;
Dave Barach6d963c22016-12-05 09:50:05 -0500733
734 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
735 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
736 vl_msg_api_send_shmem (q, (u8 *) & mp);
737}
738
739static uword
740link_state_process (vlib_main_t * vm,
741 vlib_node_runtime_t * rt, vlib_frame_t * f)
742{
743 vpe_api_main_t *vam = &vpe_api_main;
744 vnet_main_t *vnm = vam->vnet_main;
745 vnet_sw_interface_t *swif;
746 uword *event_data = 0;
747 vpe_client_registration_t *reg;
748 int i;
749 u32 prev_sw_if_index;
750 unix_shared_memory_queue_t *q;
751
752 vam->link_state_process_up = 1;
753
754 while (1)
755 {
756 vlib_process_wait_for_event (vm);
757
758 /* Unified list of changed link or admin state sw_if_indices */
759 vlib_process_get_events_with_type
760 (vm, &event_data, API_LINK_STATE_EVENT);
761 vlib_process_get_events_with_type
762 (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
763
764 /* Sort, so we can eliminate duplicates */
765 vec_sort_with_function (event_data, event_data_cmp);
766
767 prev_sw_if_index = ~0;
768
769 for (i = 0; i < vec_len (event_data); i++)
770 {
771 /* Only one message per swif */
772 if (prev_sw_if_index == event_data[i])
773 continue;
774 prev_sw_if_index = event_data[i];
775
776 /* *INDENT-OFF* */
777 pool_foreach(reg, vam->interface_events_registrations,
778 ({
779 q = vl_api_client_index_to_input_queue (reg->client_index);
780 if (q)
781 {
782 /* sw_interface may be deleted already */
783 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
784 event_data[i]))
785 {
786 swif = vnet_get_sw_interface (vnm, event_data[i]);
Neale Rannsd292ab12017-08-15 12:29:48 -0700787 send_sw_interface_event (vam, reg, q, swif);
Dave Barach6d963c22016-12-05 09:50:05 -0500788 }
789 }
790 }));
791 /* *INDENT-ON* */
792 }
793 vec_reset_length (event_data);
794 }
795
796 return 0;
797}
798
799static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
800 u32 flags);
801static clib_error_t *admin_up_down_function (vnet_main_t * vm,
802 u32 hw_if_index, u32 flags);
803
804/* *INDENT-OFF* */
805VLIB_REGISTER_NODE (link_state_process_node,static) = {
806 .function = link_state_process,
807 .type = VLIB_NODE_TYPE_PROCESS,
808 .name = "vpe-link-state-process",
809};
810/* *INDENT-ON* */
811
812VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
813VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
814
815static clib_error_t *
816link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
817{
818 vpe_api_main_t *vam = &vpe_api_main;
819 vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
820
821 if (vam->link_state_process_up)
822 vlib_process_signal_event (vam->vlib_main,
823 link_state_process_node.index,
824 API_LINK_STATE_EVENT, hi->sw_if_index);
825 return 0;
826}
827
828static clib_error_t *
829admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
830{
831 vpe_api_main_t *vam = &vpe_api_main;
832
833 /*
834 * Note: it's perfectly fair to set a subif admin up / admin down.
835 * Note the subtle distinction between this routine and the previous
836 * routine.
837 */
838 if (vam->link_state_process_up)
839 vlib_process_signal_event (vam->vlib_main,
840 link_state_process_node.index,
841 API_ADMIN_UP_DOWN_EVENT, sw_if_index);
842 return 0;
843}
844
845static void vl_api_sw_interface_tag_add_del_t_handler
846 (vl_api_sw_interface_tag_add_del_t * mp)
847{
848 vnet_main_t *vnm = vnet_get_main ();
849 vl_api_sw_interface_tag_add_del_reply_t *rmp;
850 int rv = 0;
851 u8 *tag;
852 u32 sw_if_index = ntohl (mp->sw_if_index);
853
854 VALIDATE_SW_IF_INDEX (mp);
855
856 if (mp->is_add)
857 {
858 if (mp->tag[0] == 0)
859 {
860 rv = VNET_API_ERROR_INVALID_VALUE;
861 goto out;
862 }
863
864 mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
865 tag = format (0, "%s%c", mp->tag, 0);
866 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
867 }
868 else
869 vnet_clear_sw_interface_tag (vnm, sw_if_index);
870
871 BAD_SW_IF_INDEX_LABEL;
872out:
873 REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
874}
875
Jon Loeliger10c273b2017-03-30 08:39:33 -0500876static void vl_api_sw_interface_set_mac_address_t_handler
877 (vl_api_sw_interface_set_mac_address_t * mp)
878{
879 vl_api_sw_interface_set_mac_address_reply_t *rmp;
880 vnet_main_t *vnm = vnet_get_main ();
881 u32 sw_if_index = ntohl (mp->sw_if_index);
Neale Rannsd867a7c2017-10-04 02:29:07 -0700882 vnet_sw_interface_t *si;
Jon Loeliger10c273b2017-03-30 08:39:33 -0500883 u64 mac;
884 clib_error_t *error;
885 int rv = 0;
886
887 VALIDATE_SW_IF_INDEX (mp);
888
889 mac = ((u64) mp->mac_address[0] << (8 * 0)
890 | (u64) mp->mac_address[1] << (8 * 1)
891 | (u64) mp->mac_address[2] << (8 * 2)
892 | (u64) mp->mac_address[3] << (8 * 3)
893 | (u64) mp->mac_address[4] << (8 * 4)
894 | (u64) mp->mac_address[5] << (8 * 5));
895
Neale Rannsd867a7c2017-10-04 02:29:07 -0700896 si = vnet_get_sw_interface (vnm, sw_if_index);
897 error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index, mac);
Jon Loeliger10c273b2017-03-30 08:39:33 -0500898 if (error)
899 {
900 rv = VNET_API_ERROR_UNIMPLEMENTED;
901 clib_error_report (error);
902 goto out;
903 }
904
905 BAD_SW_IF_INDEX_LABEL;
906out:
907 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
908}
909
Dave Barachaff70772016-10-31 11:59:07 -0400910/*
911 * vpe_api_hookup
912 * Add vpe's API message handlers to the table.
913 * vlib has alread mapped shared memory and
914 * added the client registration handlers.
915 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
916 */
917#define vl_msg_name_crc_list
Dave Barachb5e8a772016-12-06 12:04:42 -0500918#include <vnet/interface.api.h>
Dave Barachaff70772016-10-31 11:59:07 -0400919#undef vl_msg_name_crc_list
920
921static void
922setup_message_id_table (api_main_t * am)
923{
924#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
925 foreach_vl_msg_name_crc_interface;
926#undef _
927}
928
Dave Barach6d963c22016-12-05 09:50:05 -0500929pub_sub_handler (interface_events, INTERFACE_EVENTS);
930
Dave Barachaff70772016-10-31 11:59:07 -0400931static clib_error_t *
932interface_api_hookup (vlib_main_t * vm)
933{
934 api_main_t *am = &api_main;
935
936#define _(N,n) \
937 vl_msg_api_set_handlers(VL_API_##N, #n, \
938 vl_api_##n##_t_handler, \
939 vl_noop_handler, \
940 vl_api_##n##_t_endian, \
941 vl_api_##n##_t_print, \
942 sizeof(vl_api_##n##_t), 1);
943 foreach_vpe_api_msg;
944#undef _
945
946 /*
947 * Set up the (msg_name, crc, message-id) table
948 */
949 setup_message_id_table (am);
950
951 return 0;
952}
953
954VLIB_API_INIT_FUNCTION (interface_api_hookup);
955
956/*
957 * fd.io coding-style-patch-verification: ON
958 *
959 * Local Variables:
960 * eval: (c-set-style "gnu")
961 * End:
962 */