blob: bd11d45d85380227a12efc40c5bb44d4d5be1c41 [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) \
Stevenad8015b2017-10-29 22:10:46 -070057_(SW_INTERFACE_SET_RX_MODE, sw_interface_set_rx_mode) \
Dave Barach6d963c22016-12-05 09:50:05 -050058_(SW_INTERFACE_SET_TABLE, sw_interface_set_table) \
Juraj Slobodadfc19232016-12-05 13:20:37 +010059_(SW_INTERFACE_GET_TABLE, sw_interface_get_table) \
Dave Barach6d963c22016-12-05 09:50:05 -050060_(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered) \
61_(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats) \
Jon Loeliger10c273b2017-03-30 08:39:33 -050062_(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del) \
63_(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address)
Dave Barachaff70772016-10-31 11:59:07 -040064
65static void
66vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
67{
68 vl_api_sw_interface_set_flags_reply_t *rmp;
69 vnet_main_t *vnm = vnet_get_main ();
70 int rv = 0;
71 clib_error_t *error;
72 u16 flags;
73
74 VALIDATE_SW_IF_INDEX (mp);
75
76 flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
77
78 error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
79 if (error)
80 {
81 rv = -1;
82 clib_error_report (error);
83 }
84
85 BAD_SW_IF_INDEX_LABEL;
86 REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
87}
88
Matus Fabiand162f3d2016-12-05 01:05:35 -080089static void
90vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
91{
92 vl_api_sw_interface_set_mtu_reply_t *rmp;
93 vnet_main_t *vnm = vnet_get_main ();
94 u32 flags = ETHERNET_INTERFACE_FLAG_MTU;
95 u32 sw_if_index = ntohl (mp->sw_if_index);
96 u16 mtu = ntohs (mp->mtu);
97 ethernet_main_t *em = &ethernet_main;
98 int rv = 0;
99
100 VALIDATE_SW_IF_INDEX (mp);
101
Andrew Yourtchenko81d1e272017-08-22 13:10:01 +0200102 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
103 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
104 {
105 rv = VNET_API_ERROR_INVALID_VALUE;
106 goto bad_sw_if_index;
107 }
108
109 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index);
110 ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index);
Matus Fabiand162f3d2016-12-05 01:05:35 -0800111
112 if (!eif)
113 {
114 rv = VNET_API_ERROR_FEATURE_DISABLED;
115 goto bad_sw_if_index;
116 }
117
118 if (mtu < hi->min_supported_packet_bytes)
119 {
120 rv = VNET_API_ERROR_INVALID_VALUE;
121 goto bad_sw_if_index;
122 }
123
124 if (mtu > hi->max_supported_packet_bytes)
125 {
126 rv = VNET_API_ERROR_INVALID_VALUE;
127 goto bad_sw_if_index;
128 }
129
130 if (hi->max_packet_bytes != mtu)
131 {
132 hi->max_packet_bytes = mtu;
Andrew Yourtchenko81d1e272017-08-22 13:10:01 +0200133 ethernet_set_flags (vnm, si->hw_if_index, flags);
Matus Fabiand162f3d2016-12-05 01:05:35 -0800134 }
135
136 BAD_SW_IF_INDEX_LABEL;
137 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
138}
139
Dave Barach6d963c22016-12-05 09:50:05 -0500140static void
141send_sw_interface_details (vpe_api_main_t * am,
Dave Barach59b25652017-09-10 15:04:27 -0400142 vl_api_registration_t * rp,
Dave Barach6d963c22016-12-05 09:50:05 -0500143 vnet_sw_interface_t * swif,
144 u8 * interface_name, u32 context)
145{
Eyal Bari1c82cd42017-03-14 14:39:51 +0200146 vnet_hw_interface_t *hi =
147 vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500148
Eyal Bari1c82cd42017-03-14 14:39:51 +0200149 vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barach6d963c22016-12-05 09:50:05 -0500150 memset (mp, 0, sizeof (*mp));
151 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
152 mp->sw_if_index = ntohl (swif->sw_if_index);
153 mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
154 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
155 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
156 mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
157 VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
158 mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
159 VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
160 mp->link_mtu = ntohs (hi->max_packet_bytes);
161 mp->context = context;
162
163 strncpy ((char *) mp->interface_name,
164 (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
165
166 /* Send the L2 address for ethernet physical intfcs */
167 if (swif->sup_sw_if_index == swif->sw_if_index
168 && hi->hw_class_index == ethernet_hw_interface_class.index)
169 {
170 ethernet_main_t *em = ethernet_get_main (am->vlib_main);
171 ethernet_interface_t *ei;
172
173 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
174 ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
175 clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
176 mp->l2_address_length = ntohl (sizeof (ei->address));
177 }
178 else if (swif->sup_sw_if_index != swif->sw_if_index)
179 {
180 vnet_sub_interface_t *sub = &swif->sub;
181 mp->sub_id = ntohl (sub->id);
182 mp->sub_dot1ad = sub->eth.flags.dot1ad;
183 mp->sub_number_of_tags =
184 sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
185 mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
186 mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
187 mp->sub_exact_match = sub->eth.flags.exact_match;
188 mp->sub_default = sub->eth.flags.default_sub;
189 mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
190 mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
191
192 /* vlan tag rewrite data */
193 u32 vtr_op = L2_VTR_DISABLED;
194 u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
195
196 if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
197 &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
198 {
199 // error - default to disabled
200 mp->vtr_op = ntohl (L2_VTR_DISABLED);
201 clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
202 swif->sw_if_index);
203 }
204 else
205 {
206 mp->vtr_op = ntohl (vtr_op);
207 mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
208 mp->vtr_tag1 = ntohl (vtr_tag1);
209 mp->vtr_tag2 = ntohl (vtr_tag2);
210 }
211 }
212
Pavel Kotucek65e84572017-01-16 17:01:56 +0100213 /* pbb tag rewrite data */
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100214 ethernet_header_t eth_hdr;
Pavel Kotucek65e84572017-01-16 17:01:56 +0100215 u32 vtr_op = L2_VTR_DISABLED;
216 u16 outer_tag = 0;
Pavel Kotucek65e84572017-01-16 17:01:56 +0100217 u16 b_vlanid = 0;
218 u32 i_sid = 0;
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100219 memset (&eth_hdr, 0, sizeof (eth_hdr));
Pavel Kotucek65e84572017-01-16 17:01:56 +0100220
221 if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100222 &vtr_op, &outer_tag, &eth_hdr, &b_vlanid, &i_sid))
Pavel Kotucek65e84572017-01-16 17:01:56 +0100223 {
224 mp->sub_dot1ah = 1;
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100225 clib_memcpy (mp->b_dmac, eth_hdr.dst_address,
226 sizeof (eth_hdr.dst_address));
227 clib_memcpy (mp->b_smac, eth_hdr.src_address,
228 sizeof (eth_hdr.src_address));
Pavel Kotucek65e84572017-01-16 17:01:56 +0100229 mp->b_vlanid = b_vlanid;
230 mp->i_sid = i_sid;
231 }
232
Eyal Bari1c82cd42017-03-14 14:39:51 +0200233 u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500234 if (tag)
235 strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
236
Dave Barach59b25652017-09-10 15:04:27 -0400237 vl_msg_api_send (rp, (u8 *) mp);
Dave Barach6d963c22016-12-05 09:50:05 -0500238}
239
240static void
241vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
242{
243 vpe_api_main_t *am = &vpe_api_main;
244 vnet_sw_interface_t *swif;
245 vnet_interface_main_t *im = &am->vnet_main->interface_main;
Dave Barach59b25652017-09-10 15:04:27 -0400246 vl_api_registration_t *rp;
Dave Barach6d963c22016-12-05 09:50:05 -0500247
Dave Barach59b25652017-09-10 15:04:27 -0400248 rp = vl_api_client_index_to_registration (mp->client_index);
249
250 if (rp == 0)
251 {
252 clib_warning ("Client %d AWOL", mp->client_index);
253 return;
254 }
Dave Barach6d963c22016-12-05 09:50:05 -0500255
Eyal Bari1c82cd42017-03-14 14:39:51 +0200256 u8 *filter = 0, *name = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500257 if (mp->name_filter_valid)
258 {
259 mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
Eyal Bari1c82cd42017-03-14 14:39:51 +0200260 filter = format (0, "%s%c", mp->name_filter, 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500261 }
262
Eyal Bari1c82cd42017-03-14 14:39:51 +0200263 char *strcasestr (char *, char *); /* lnx hdr file botch */
Dave Barach6d963c22016-12-05 09:50:05 -0500264 /* *INDENT-OFF* */
265 pool_foreach (swif, im->sw_interfaces,
266 ({
Eyal Bari1c82cd42017-03-14 14:39:51 +0200267 if (!vnet_swif_is_api_visible (swif))
268 continue;
269 vec_reset_length(name);
270 name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
271 swif, 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500272
Eyal Bari1c82cd42017-03-14 14:39:51 +0200273 if (filter && !strcasestr((char *) name, (char *) filter))
274 continue;
Dave Barach6d963c22016-12-05 09:50:05 -0500275
Dave Barach59b25652017-09-10 15:04:27 -0400276 send_sw_interface_details (am, rp, swif, name, mp->context);
Dave Barach6d963c22016-12-05 09:50:05 -0500277 }));
278 /* *INDENT-ON* */
279
Eyal Bari1c82cd42017-03-14 14:39:51 +0200280 vec_free (name);
281 vec_free (filter);
Dave Barach6d963c22016-12-05 09:50:05 -0500282}
283
284static void
285 vl_api_sw_interface_add_del_address_t_handler
286 (vl_api_sw_interface_add_del_address_t * mp)
287{
288 vlib_main_t *vm = vlib_get_main ();
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500289 vnet_main_t *vnm = vnet_get_main ();
Dave Barach6d963c22016-12-05 09:50:05 -0500290 vl_api_sw_interface_add_del_address_reply_t *rmp;
291 int rv = 0;
292 u32 is_del;
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500293 clib_error_t *error = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500294
295 VALIDATE_SW_IF_INDEX (mp);
296
297 is_del = mp->is_add == 0;
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500298 vnm->api_errno = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500299
300 if (mp->del_all)
301 ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
302 else if (mp->is_ipv6)
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500303 error = ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
304 (void *) mp->address,
305 mp->address_length, is_del);
Dave Barach6d963c22016-12-05 09:50:05 -0500306 else
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500307 error = ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
308 (void *) mp->address,
309 mp->address_length, is_del);
310
311 if (error)
312 {
313 rv = vnm->api_errno;
314 clib_error_report (error);
315 goto done;
316 }
Dave Barach6d963c22016-12-05 09:50:05 -0500317
318 BAD_SW_IF_INDEX_LABEL;
319
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500320done:
Dave Barach6d963c22016-12-05 09:50:05 -0500321 REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
322}
323
324void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
325void
326stats_dslock_with_hint (int hint, int tag)
327{
328}
329
330void stats_dsunlock (void) __attribute__ ((weak));
331void
332stats_dsunlock (void)
333{
334}
335
336static void
337vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
338{
Dave Barach6d963c22016-12-05 09:50:05 -0500339 vl_api_sw_interface_set_table_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700340 u32 sw_if_index = ntohl (mp->sw_if_index);
341 u32 table_id = ntohl (mp->vrf_id);
342 int rv = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500343
344 VALIDATE_SW_IF_INDEX (mp);
345
346 stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
347
348 if (mp->is_ipv6)
Neale Ranns15002542017-09-10 04:39:11 -0700349 rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
Dave Barach6d963c22016-12-05 09:50:05 -0500350 else
Neale Ranns15002542017-09-10 04:39:11 -0700351 rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
Dave Barach6d963c22016-12-05 09:50:05 -0500352
Dave Barach6d963c22016-12-05 09:50:05 -0500353 stats_dsunlock ();
354
355 BAD_SW_IF_INDEX_LABEL;
356
357 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
358}
359
Neale Ranns15002542017-09-10 04:39:11 -0700360int
361ip_table_bind (fib_protocol_t fproto,
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700362 u32 sw_if_index, u32 table_id, u8 is_api)
Neale Ranns15002542017-09-10 04:39:11 -0700363{
364 CLIB_UNUSED (ip_interface_address_t * ia);
365 u32 fib_index, mfib_index;
366 fib_source_t src;
367 mfib_source_t msrc;
368
369 if (is_api)
370 {
371 src = FIB_SOURCE_API;
372 msrc = MFIB_SOURCE_API;
373 }
374 else
375 {
376 src = FIB_SOURCE_CLI;
377 msrc = MFIB_SOURCE_CLI;
378 }
379
Florin Corasd0a59722017-10-15 17:41:21 +0000380 /*
381 * This is temporary whilst I do the song and dance with the CSIT version
382 */
383 if (0 != table_id)
384 {
385 fib_index = fib_table_find_or_create_and_lock (fproto, table_id, src);
386 mfib_index =
387 mfib_table_find_or_create_and_lock (fproto, table_id, msrc);
388 }
389 else
390 {
391 fib_index = 0;
392 mfib_index = 0;
393 }
394
395 /*
396 * This if table does not exist = error is what we want in the end.
397 */
398 /* fib_index = fib_table_find (fproto, table_id); */
399 /* mfib_index = mfib_table_find (fproto, table_id); */
400
401 /* if (~0 == fib_index || ~0 == mfib_index) */
402 /* { */
403 /* return (VNET_API_ERROR_NO_SUCH_FIB); */
404 /* } */
405
Neale Ranns15002542017-09-10 04:39:11 -0700406 if (FIB_PROTOCOL_IP6 == fproto)
407 {
408 /*
409 * If the interface already has in IP address, then a change int
410 * VRF is not allowed. The IP address applied must first be removed.
411 * We do not do that automatically here, since VPP has no knowledge
412 * of whether thoses subnets are valid in the destination VRF.
413 */
414 /* *INDENT-OFF* */
415 foreach_ip_interface_address (&ip6_main.lookup_main,
416 ia, sw_if_index,
417 1 /* honor unnumbered */ ,
418 ({
419 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
420 }));
421 /* *INDENT-ON* */
422
423 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
424 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
425
426 /*
427 * tell those that are interested that the binding is changing.
428 */
429 ip6_table_bind_callback_t *cb;
430 vec_foreach (cb, ip6_main.table_bind_callbacks)
431 cb->function (&ip6_main, cb->function_opaque,
432 sw_if_index,
433 fib_index,
434 ip6_main.fib_index_by_sw_if_index[sw_if_index]);
435
436 if (0 == table_id)
437 {
438 /* reset back to default */
439 if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
440 fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
441 FIB_PROTOCOL_IP6, src);
442 if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
443 mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
444 [sw_if_index], FIB_PROTOCOL_IP6, msrc);
445
446 }
447 else
448 {
449 /* we need to lock the table now it's inuse */
450 fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
451 mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
452 }
453
454 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
455 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
456 }
457 else
458 {
459 /*
460 * If the interface already has in IP address, then a change int
461 * VRF is not allowed. The IP address applied must first be removed.
462 * We do not do that automatically here, since VPP has no knowledge
463 * of whether thoses subnets are valid in the destination VRF.
464 */
465 /* *INDENT-OFF* */
466 foreach_ip_interface_address (&ip4_main.lookup_main,
467 ia, sw_if_index,
468 1 /* honor unnumbered */ ,
469 ({
470 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
471 }));
472 /* *INDENT-ON* */
473
474 vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
475 vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
476
477 /*
478 * tell those that are interested that the binding is changing.
479 */
480 ip4_table_bind_callback_t *cb;
481 vec_foreach (cb, ip4_main.table_bind_callbacks)
482 cb->function (&ip4_main, cb->function_opaque,
483 sw_if_index,
484 fib_index,
485 ip4_main.fib_index_by_sw_if_index[sw_if_index]);
486
487 if (0 == table_id)
488 {
489 /* reset back to default */
490 if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
491 fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
492 FIB_PROTOCOL_IP4, src);
493 if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
494 mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
495 [sw_if_index], FIB_PROTOCOL_IP4, msrc);
496
497 }
498 else
499 {
500 /* we need to lock the table now it's inuse */
501 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
502 table_id, src);
503
504 mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
505 table_id, msrc);
506 }
507
508 ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
509 ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
510 }
511
Florin Corasd0a59722017-10-15 17:41:21 +0000512 /*
513 * Temporary. undo the locks from the find and create at the staart
514 */
515 if (0 != table_id)
516 {
517 fib_table_unlock (fib_index, fproto, src);
518 mfib_table_unlock (mfib_index, fproto, msrc);
519 }
520
Neale Ranns15002542017-09-10 04:39:11 -0700521 return (0);
522}
523
Juraj Slobodadfc19232016-12-05 13:20:37 +0100524static void
525send_sw_interface_get_table_reply (unix_shared_memory_queue_t * q,
526 u32 context, int retval, u32 vrf_id)
527{
528 vl_api_sw_interface_get_table_reply_t *mp;
529
530 mp = vl_msg_api_alloc (sizeof (*mp));
531 memset (mp, 0, sizeof (*mp));
532 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
533 mp->context = context;
534 mp->retval = htonl (retval);
535 mp->vrf_id = htonl (vrf_id);
536
537 vl_msg_api_send_shmem (q, (u8 *) & mp);
538}
539
540static void
541vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
542{
543 unix_shared_memory_queue_t *q;
544 fib_table_t *fib_table = 0;
545 u32 sw_if_index = ~0;
546 u32 fib_index = ~0;
547 u32 table_id = ~0;
548 fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
549 int rv = 0;
550
551 q = vl_api_client_index_to_input_queue (mp->client_index);
552 if (q == 0)
553 return;
554
555 VALIDATE_SW_IF_INDEX (mp);
556
557 sw_if_index = ntohl (mp->sw_if_index);
558
559 if (mp->is_ipv6)
560 fib_proto = FIB_PROTOCOL_IP6;
561
562 fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
563 if (fib_index != ~0)
564 {
565 fib_table = fib_table_get (fib_index, fib_proto);
566 table_id = fib_table->ft_table_id;
567 }
568
569 BAD_SW_IF_INDEX_LABEL;
570
571 send_sw_interface_get_table_reply (q, mp->context, rv, table_id);
572}
573
Dave Barach6d963c22016-12-05 09:50:05 -0500574static void vl_api_sw_interface_set_unnumbered_t_handler
575 (vl_api_sw_interface_set_unnumbered_t * mp)
576{
577 vl_api_sw_interface_set_unnumbered_reply_t *rmp;
578 int rv = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500579 vnet_main_t *vnm = vnet_get_main ();
Eyal Bari1c82cd42017-03-14 14:39:51 +0200580 u32 sw_if_index = ntohl (mp->sw_if_index);
581 u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
Neale Ranns898273f2017-03-18 02:57:38 -0700582 u32 was_unnum;
Dave Barach6d963c22016-12-05 09:50:05 -0500583
584 /*
585 * The API message field names are backwards from
586 * the underlying data structure names.
587 * It's not worth changing them now.
588 */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200589 if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500590 {
591 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
592 goto done;
593 }
594
595 /* Only check the "use loop0" field when setting the binding */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200596 if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500597 {
598 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
599 goto done;
600 }
601
Eyal Bari1c82cd42017-03-14 14:39:51 +0200602 vnet_sw_interface_t *si =
603 vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
Neale Ranns898273f2017-03-18 02:57:38 -0700604 was_unnum = (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED);
Dave Barach6d963c22016-12-05 09:50:05 -0500605
606 if (mp->is_add)
607 {
608 si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
609 si->unnumbered_sw_if_index = sw_if_index;
Neale Ranns4b919a52017-03-11 05:55:21 -0800610
611 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
612 [unnumbered_sw_if_index] =
613 ip4_main.
614 lookup_main.if_address_pool_index_by_sw_if_index[sw_if_index];
615 ip6_main.
616 lookup_main.if_address_pool_index_by_sw_if_index
617 [unnumbered_sw_if_index] =
618 ip6_main.
619 lookup_main.if_address_pool_index_by_sw_if_index[sw_if_index];
Dave Barach6d963c22016-12-05 09:50:05 -0500620 }
621 else
622 {
623 si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
624 si->unnumbered_sw_if_index = (u32) ~ 0;
Neale Ranns4b919a52017-03-11 05:55:21 -0800625
626 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
627 [unnumbered_sw_if_index] = ~0;
628 ip6_main.lookup_main.if_address_pool_index_by_sw_if_index
629 [unnumbered_sw_if_index] = ~0;
Dave Barach6d963c22016-12-05 09:50:05 -0500630 }
Neale Ranns898273f2017-03-18 02:57:38 -0700631
632 if (was_unnum != (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
633 {
634 ip4_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add);
635 ip6_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add);
636 }
Dave Barach6d963c22016-12-05 09:50:05 -0500637
638done:
639 REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
640}
641
642static void
643vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
644 mp)
645{
646 vl_api_sw_interface_clear_stats_reply_t *rmp;
647
648 vnet_main_t *vnm = vnet_get_main ();
649 vnet_interface_main_t *im = &vnm->interface_main;
650 vlib_simple_counter_main_t *sm;
651 vlib_combined_counter_main_t *cm;
652 static vnet_main_t **my_vnet_mains;
653 int i, j, n_counters;
654 int rv = 0;
655
656 if (mp->sw_if_index != ~0)
657 VALIDATE_SW_IF_INDEX (mp);
658
659 vec_reset_length (my_vnet_mains);
660
661 for (i = 0; i < vec_len (vnet_mains); i++)
662 {
663 if (vnet_mains[i])
664 vec_add1 (my_vnet_mains, vnet_mains[i]);
665 }
666
667 if (vec_len (vnet_mains) == 0)
668 vec_add1 (my_vnet_mains, vnm);
669
670 n_counters = vec_len (im->combined_sw_if_counters);
671
672 for (j = 0; j < n_counters; j++)
673 {
674 for (i = 0; i < vec_len (my_vnet_mains); i++)
675 {
676 im = &my_vnet_mains[i]->interface_main;
677 cm = im->combined_sw_if_counters + j;
678 if (mp->sw_if_index == (u32) ~ 0)
679 vlib_clear_combined_counters (cm);
680 else
681 vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
682 }
683 }
684
685 n_counters = vec_len (im->sw_if_counters);
686
687 for (j = 0; j < n_counters; j++)
688 {
689 for (i = 0; i < vec_len (my_vnet_mains); i++)
690 {
691 im = &my_vnet_mains[i]->interface_main;
692 sm = im->sw_if_counters + j;
693 if (mp->sw_if_index == (u32) ~ 0)
694 vlib_clear_simple_counters (sm);
695 else
696 vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
697 }
698 }
699
700 BAD_SW_IF_INDEX_LABEL;
701
702 REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
703}
704
705#define API_LINK_STATE_EVENT 1
706#define API_ADMIN_UP_DOWN_EVENT 2
707
708static int
709event_data_cmp (void *a1, void *a2)
710{
711 uword *e1 = a1;
712 uword *e2 = a2;
713
714 return (word) e1[0] - (word) e2[0];
715}
716
717static void
Neale Rannsa07bd702017-08-07 07:53:49 -0700718send_sw_interface_event (vpe_api_main_t * am,
Neale Rannsd292ab12017-08-15 12:29:48 -0700719 vpe_client_registration_t * reg,
Dave Barach6d963c22016-12-05 09:50:05 -0500720 unix_shared_memory_queue_t * q,
721 vnet_sw_interface_t * swif)
722{
Neale Rannsa07bd702017-08-07 07:53:49 -0700723 vl_api_sw_interface_event_t *mp;
Dave Barach6d963c22016-12-05 09:50:05 -0500724 vnet_main_t *vnm = am->vnet_main;
725
726 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
727 swif->sw_if_index);
728 mp = vl_msg_api_alloc (sizeof (*mp));
729 memset (mp, 0, sizeof (*mp));
Neale Rannsa07bd702017-08-07 07:53:49 -0700730 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
Dave Barach6d963c22016-12-05 09:50:05 -0500731 mp->sw_if_index = ntohl (swif->sw_if_index);
Neale Rannsd292ab12017-08-15 12:29:48 -0700732 mp->client_index = reg->client_index;
733 mp->pid = reg->client_pid;
Dave Barach6d963c22016-12-05 09:50:05 -0500734
735 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
736 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
737 vl_msg_api_send_shmem (q, (u8 *) & mp);
738}
739
740static uword
741link_state_process (vlib_main_t * vm,
742 vlib_node_runtime_t * rt, vlib_frame_t * f)
743{
744 vpe_api_main_t *vam = &vpe_api_main;
745 vnet_main_t *vnm = vam->vnet_main;
746 vnet_sw_interface_t *swif;
747 uword *event_data = 0;
748 vpe_client_registration_t *reg;
749 int i;
750 u32 prev_sw_if_index;
751 unix_shared_memory_queue_t *q;
752
753 vam->link_state_process_up = 1;
754
755 while (1)
756 {
757 vlib_process_wait_for_event (vm);
758
759 /* Unified list of changed link or admin state sw_if_indices */
760 vlib_process_get_events_with_type
761 (vm, &event_data, API_LINK_STATE_EVENT);
762 vlib_process_get_events_with_type
763 (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
764
765 /* Sort, so we can eliminate duplicates */
766 vec_sort_with_function (event_data, event_data_cmp);
767
768 prev_sw_if_index = ~0;
769
770 for (i = 0; i < vec_len (event_data); i++)
771 {
772 /* Only one message per swif */
773 if (prev_sw_if_index == event_data[i])
774 continue;
775 prev_sw_if_index = event_data[i];
776
777 /* *INDENT-OFF* */
778 pool_foreach(reg, vam->interface_events_registrations,
779 ({
780 q = vl_api_client_index_to_input_queue (reg->client_index);
781 if (q)
782 {
783 /* sw_interface may be deleted already */
784 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
785 event_data[i]))
786 {
787 swif = vnet_get_sw_interface (vnm, event_data[i]);
Neale Rannsd292ab12017-08-15 12:29:48 -0700788 send_sw_interface_event (vam, reg, q, swif);
Dave Barach6d963c22016-12-05 09:50:05 -0500789 }
790 }
791 }));
792 /* *INDENT-ON* */
793 }
794 vec_reset_length (event_data);
795 }
796
797 return 0;
798}
799
800static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
801 u32 flags);
802static clib_error_t *admin_up_down_function (vnet_main_t * vm,
803 u32 hw_if_index, u32 flags);
804
805/* *INDENT-OFF* */
806VLIB_REGISTER_NODE (link_state_process_node,static) = {
807 .function = link_state_process,
808 .type = VLIB_NODE_TYPE_PROCESS,
809 .name = "vpe-link-state-process",
810};
811/* *INDENT-ON* */
812
813VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
814VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
815
816static clib_error_t *
817link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
818{
819 vpe_api_main_t *vam = &vpe_api_main;
820 vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
821
822 if (vam->link_state_process_up)
823 vlib_process_signal_event (vam->vlib_main,
824 link_state_process_node.index,
825 API_LINK_STATE_EVENT, hi->sw_if_index);
826 return 0;
827}
828
829static clib_error_t *
830admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
831{
832 vpe_api_main_t *vam = &vpe_api_main;
833
834 /*
835 * Note: it's perfectly fair to set a subif admin up / admin down.
836 * Note the subtle distinction between this routine and the previous
837 * routine.
838 */
839 if (vam->link_state_process_up)
840 vlib_process_signal_event (vam->vlib_main,
841 link_state_process_node.index,
842 API_ADMIN_UP_DOWN_EVENT, sw_if_index);
843 return 0;
844}
845
846static void vl_api_sw_interface_tag_add_del_t_handler
847 (vl_api_sw_interface_tag_add_del_t * mp)
848{
849 vnet_main_t *vnm = vnet_get_main ();
850 vl_api_sw_interface_tag_add_del_reply_t *rmp;
851 int rv = 0;
852 u8 *tag;
853 u32 sw_if_index = ntohl (mp->sw_if_index);
854
855 VALIDATE_SW_IF_INDEX (mp);
856
857 if (mp->is_add)
858 {
859 if (mp->tag[0] == 0)
860 {
861 rv = VNET_API_ERROR_INVALID_VALUE;
862 goto out;
863 }
864
865 mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
866 tag = format (0, "%s%c", mp->tag, 0);
867 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
868 }
869 else
870 vnet_clear_sw_interface_tag (vnm, sw_if_index);
871
872 BAD_SW_IF_INDEX_LABEL;
873out:
874 REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
875}
876
Jon Loeliger10c273b2017-03-30 08:39:33 -0500877static void vl_api_sw_interface_set_mac_address_t_handler
878 (vl_api_sw_interface_set_mac_address_t * mp)
879{
880 vl_api_sw_interface_set_mac_address_reply_t *rmp;
881 vnet_main_t *vnm = vnet_get_main ();
882 u32 sw_if_index = ntohl (mp->sw_if_index);
Neale Rannsd867a7c2017-10-04 02:29:07 -0700883 vnet_sw_interface_t *si;
Jon Loeliger10c273b2017-03-30 08:39:33 -0500884 clib_error_t *error;
885 int rv = 0;
886
887 VALIDATE_SW_IF_INDEX (mp);
888
Neale Rannsd867a7c2017-10-04 02:29:07 -0700889 si = vnet_get_sw_interface (vnm, sw_if_index);
John Lo62fcc0a2017-10-31 14:31:10 -0400890 error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index,
891 mp->mac_address);
Jon Loeliger10c273b2017-03-30 08:39:33 -0500892 if (error)
893 {
894 rv = VNET_API_ERROR_UNIMPLEMENTED;
895 clib_error_report (error);
896 goto out;
897 }
898
899 BAD_SW_IF_INDEX_LABEL;
900out:
901 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
902}
903
Stevenad8015b2017-10-29 22:10:46 -0700904static void vl_api_sw_interface_set_rx_mode_t_handler
905 (vl_api_sw_interface_set_rx_mode_t * mp)
906{
907 vl_api_sw_interface_set_rx_mode_reply_t *rmp;
908 vnet_main_t *vnm = vnet_get_main ();
909 u32 sw_if_index = ntohl (mp->sw_if_index);
910 vnet_sw_interface_t *si;
911 clib_error_t *error;
912 int rv = 0;
913
914 VALIDATE_SW_IF_INDEX (mp);
915
916 si = vnet_get_sw_interface (vnm, sw_if_index);
917 error = set_hw_interface_change_rx_mode (vnm, si->hw_if_index,
918 mp->queue_id_valid,
919 ntohl (mp->queue_id), mp->mode);
920 if (error)
921 {
922 rv = VNET_API_ERROR_UNIMPLEMENTED;
923 clib_error_report (error);
924 goto out;
925 }
926
927 BAD_SW_IF_INDEX_LABEL;
928out:
929 REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_MODE_REPLY);
930}
931
Dave Barachaff70772016-10-31 11:59:07 -0400932/*
933 * vpe_api_hookup
934 * Add vpe's API message handlers to the table.
935 * vlib has alread mapped shared memory and
936 * added the client registration handlers.
937 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
938 */
939#define vl_msg_name_crc_list
Dave Barachb5e8a772016-12-06 12:04:42 -0500940#include <vnet/interface.api.h>
Dave Barachaff70772016-10-31 11:59:07 -0400941#undef vl_msg_name_crc_list
942
943static void
944setup_message_id_table (api_main_t * am)
945{
946#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
947 foreach_vl_msg_name_crc_interface;
948#undef _
949}
950
Dave Barach6d963c22016-12-05 09:50:05 -0500951pub_sub_handler (interface_events, INTERFACE_EVENTS);
952
Dave Barachaff70772016-10-31 11:59:07 -0400953static clib_error_t *
954interface_api_hookup (vlib_main_t * vm)
955{
956 api_main_t *am = &api_main;
957
958#define _(N,n) \
959 vl_msg_api_set_handlers(VL_API_##N, #n, \
960 vl_api_##n##_t_handler, \
961 vl_noop_handler, \
962 vl_api_##n##_t_endian, \
963 vl_api_##n##_t_print, \
964 sizeof(vl_api_##n##_t), 1);
965 foreach_vpe_api_msg;
966#undef _
967
968 /*
969 * Set up the (msg_name, crc, message-id) table
970 */
971 setup_message_id_table (am);
972
973 return 0;
974}
975
976VLIB_API_INIT_FUNCTION (interface_api_hookup);
977
978/*
979 * fd.io coding-style-patch-verification: ON
980 *
981 * Local Variables:
982 * eval: (c-set-style "gnu")
983 * End:
984 */