blob: 0731ab3563943d3cb1bddaa67aa809e0054ea131 [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,
141 unix_shared_memory_queue_t * q,
142 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
236 vl_msg_api_send_shmem (q, (u8 *) & mp);
237}
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 Barach6d963c22016-12-05 09:50:05 -0500245
Eyal Bari1c82cd42017-03-14 14:39:51 +0200246 unix_shared_memory_queue_t *q =
247 vl_api_client_index_to_input_queue (mp->client_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500248 if (q == 0)
249 return;
250
Eyal Bari1c82cd42017-03-14 14:39:51 +0200251 u8 *filter = 0, *name = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500252 if (mp->name_filter_valid)
253 {
254 mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
Eyal Bari1c82cd42017-03-14 14:39:51 +0200255 filter = format (0, "%s%c", mp->name_filter, 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500256 }
257
Eyal Bari1c82cd42017-03-14 14:39:51 +0200258 char *strcasestr (char *, char *); /* lnx hdr file botch */
Dave Barach6d963c22016-12-05 09:50:05 -0500259 /* *INDENT-OFF* */
260 pool_foreach (swif, im->sw_interfaces,
261 ({
Eyal Bari1c82cd42017-03-14 14:39:51 +0200262 if (!vnet_swif_is_api_visible (swif))
263 continue;
264 vec_reset_length(name);
265 name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
266 swif, 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500267
Eyal Bari1c82cd42017-03-14 14:39:51 +0200268 if (filter && !strcasestr((char *) name, (char *) filter))
269 continue;
Dave Barach6d963c22016-12-05 09:50:05 -0500270
Eyal Bari1c82cd42017-03-14 14:39:51 +0200271 send_sw_interface_details (am, q, swif, name, mp->context);
Dave Barach6d963c22016-12-05 09:50:05 -0500272 }));
273 /* *INDENT-ON* */
274
Eyal Bari1c82cd42017-03-14 14:39:51 +0200275 vec_free (name);
276 vec_free (filter);
Dave Barach6d963c22016-12-05 09:50:05 -0500277}
278
279static void
280 vl_api_sw_interface_add_del_address_t_handler
281 (vl_api_sw_interface_add_del_address_t * mp)
282{
283 vlib_main_t *vm = vlib_get_main ();
284 vl_api_sw_interface_add_del_address_reply_t *rmp;
285 int rv = 0;
286 u32 is_del;
287
288 VALIDATE_SW_IF_INDEX (mp);
289
290 is_del = mp->is_add == 0;
291
292 if (mp->del_all)
293 ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
294 else if (mp->is_ipv6)
295 ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
296 (void *) mp->address,
297 mp->address_length, is_del);
298 else
299 ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
300 (void *) mp->address,
301 mp->address_length, is_del);
302
303 BAD_SW_IF_INDEX_LABEL;
304
305 REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
306}
307
308void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
309void
310stats_dslock_with_hint (int hint, int tag)
311{
312}
313
314void stats_dsunlock (void) __attribute__ ((weak));
315void
316stats_dsunlock (void)
317{
318}
319
320static void
321vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
322{
Dave Barach6d963c22016-12-05 09:50:05 -0500323 vl_api_sw_interface_set_table_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700324 u32 sw_if_index = ntohl (mp->sw_if_index);
325 u32 table_id = ntohl (mp->vrf_id);
326 int rv = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500327
328 VALIDATE_SW_IF_INDEX (mp);
329
330 stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
331
332 if (mp->is_ipv6)
Neale Ranns15002542017-09-10 04:39:11 -0700333 rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
Dave Barach6d963c22016-12-05 09:50:05 -0500334 else
Neale Ranns15002542017-09-10 04:39:11 -0700335 rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
Dave Barach6d963c22016-12-05 09:50:05 -0500336
Dave Barach6d963c22016-12-05 09:50:05 -0500337 stats_dsunlock ();
338
339 BAD_SW_IF_INDEX_LABEL;
340
341 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
342}
343
Neale Ranns15002542017-09-10 04:39:11 -0700344int
345ip_table_bind (fib_protocol_t fproto,
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700346 u32 sw_if_index, u32 table_id, u8 is_api)
Neale Ranns15002542017-09-10 04:39:11 -0700347{
348 CLIB_UNUSED (ip_interface_address_t * ia);
349 u32 fib_index, mfib_index;
350 fib_source_t src;
351 mfib_source_t msrc;
352
353 if (is_api)
354 {
355 src = FIB_SOURCE_API;
356 msrc = MFIB_SOURCE_API;
357 }
358 else
359 {
360 src = FIB_SOURCE_CLI;
361 msrc = MFIB_SOURCE_CLI;
362 }
363
364 /*
365 * This is temporary whilst I do the song and dance with the CSIT version
366 */
367 if (0 != table_id)
368 {
369 fib_index = fib_table_find_or_create_and_lock (fproto, table_id, src);
370 mfib_index =
371 mfib_table_find_or_create_and_lock (fproto, table_id, msrc);
372 }
373 else
374 {
375 fib_index = 0;
376 mfib_index = 0;
377 }
378
379 /*
380 * This if table does not exist = error is what we want in the end.
381 */
382 /* fib_index = fib_table_find (fproto, table_id); */
383 /* mfib_index = mfib_table_find (fproto, table_id); */
384
385 /* if (~0 == fib_index || ~0 == mfib_index) */
386 /* { */
387 /* return (VNET_API_ERROR_NO_SUCH_FIB); */
388 /* } */
389
390 if (FIB_PROTOCOL_IP6 == fproto)
391 {
392 /*
393 * If the interface already has in IP address, then a change int
394 * VRF is not allowed. The IP address applied must first be removed.
395 * We do not do that automatically here, since VPP has no knowledge
396 * of whether thoses subnets are valid in the destination VRF.
397 */
398 /* *INDENT-OFF* */
399 foreach_ip_interface_address (&ip6_main.lookup_main,
400 ia, sw_if_index,
401 1 /* honor unnumbered */ ,
402 ({
403 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
404 }));
405 /* *INDENT-ON* */
406
407 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
408 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
409
410 /*
411 * tell those that are interested that the binding is changing.
412 */
413 ip6_table_bind_callback_t *cb;
414 vec_foreach (cb, ip6_main.table_bind_callbacks)
415 cb->function (&ip6_main, cb->function_opaque,
416 sw_if_index,
417 fib_index,
418 ip6_main.fib_index_by_sw_if_index[sw_if_index]);
419
420 if (0 == table_id)
421 {
422 /* reset back to default */
423 if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
424 fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
425 FIB_PROTOCOL_IP6, src);
426 if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
427 mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
428 [sw_if_index], FIB_PROTOCOL_IP6, msrc);
429
430 }
431 else
432 {
433 /* we need to lock the table now it's inuse */
434 fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
435 mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
436 }
437
438 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
439 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
440 }
441 else
442 {
443 /*
444 * If the interface already has in IP address, then a change int
445 * VRF is not allowed. The IP address applied must first be removed.
446 * We do not do that automatically here, since VPP has no knowledge
447 * of whether thoses subnets are valid in the destination VRF.
448 */
449 /* *INDENT-OFF* */
450 foreach_ip_interface_address (&ip4_main.lookup_main,
451 ia, sw_if_index,
452 1 /* honor unnumbered */ ,
453 ({
454 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
455 }));
456 /* *INDENT-ON* */
457
458 vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
459 vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
460
461 /*
462 * tell those that are interested that the binding is changing.
463 */
464 ip4_table_bind_callback_t *cb;
465 vec_foreach (cb, ip4_main.table_bind_callbacks)
466 cb->function (&ip4_main, cb->function_opaque,
467 sw_if_index,
468 fib_index,
469 ip4_main.fib_index_by_sw_if_index[sw_if_index]);
470
471 if (0 == table_id)
472 {
473 /* reset back to default */
474 if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
475 fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
476 FIB_PROTOCOL_IP4, src);
477 if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
478 mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
479 [sw_if_index], FIB_PROTOCOL_IP4, msrc);
480
481 }
482 else
483 {
484 /* we need to lock the table now it's inuse */
485 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
486 table_id, src);
487
488 mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
489 table_id, msrc);
490 }
491
492 ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
493 ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
494 }
495
496 /*
497 * Temporary. undo the locks from the find and create at the staart
498 */
499 if (0 != table_id)
500 {
501 fib_table_unlock (fib_index, fproto, src);
502 mfib_table_unlock (mfib_index, fproto, msrc);
503 }
504
505 return (0);
506}
507
Juraj Slobodadfc19232016-12-05 13:20:37 +0100508static void
509send_sw_interface_get_table_reply (unix_shared_memory_queue_t * q,
510 u32 context, int retval, u32 vrf_id)
511{
512 vl_api_sw_interface_get_table_reply_t *mp;
513
514 mp = vl_msg_api_alloc (sizeof (*mp));
515 memset (mp, 0, sizeof (*mp));
516 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
517 mp->context = context;
518 mp->retval = htonl (retval);
519 mp->vrf_id = htonl (vrf_id);
520
521 vl_msg_api_send_shmem (q, (u8 *) & mp);
522}
523
524static void
525vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
526{
527 unix_shared_memory_queue_t *q;
528 fib_table_t *fib_table = 0;
529 u32 sw_if_index = ~0;
530 u32 fib_index = ~0;
531 u32 table_id = ~0;
532 fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
533 int rv = 0;
534
535 q = vl_api_client_index_to_input_queue (mp->client_index);
536 if (q == 0)
537 return;
538
539 VALIDATE_SW_IF_INDEX (mp);
540
541 sw_if_index = ntohl (mp->sw_if_index);
542
543 if (mp->is_ipv6)
544 fib_proto = FIB_PROTOCOL_IP6;
545
546 fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
547 if (fib_index != ~0)
548 {
549 fib_table = fib_table_get (fib_index, fib_proto);
550 table_id = fib_table->ft_table_id;
551 }
552
553 BAD_SW_IF_INDEX_LABEL;
554
555 send_sw_interface_get_table_reply (q, mp->context, rv, table_id);
556}
557
Dave Barach6d963c22016-12-05 09:50:05 -0500558static void vl_api_sw_interface_set_unnumbered_t_handler
559 (vl_api_sw_interface_set_unnumbered_t * mp)
560{
561 vl_api_sw_interface_set_unnumbered_reply_t *rmp;
562 int rv = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500563 vnet_main_t *vnm = vnet_get_main ();
Eyal Bari1c82cd42017-03-14 14:39:51 +0200564 u32 sw_if_index = ntohl (mp->sw_if_index);
565 u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
Neale Ranns898273f2017-03-18 02:57:38 -0700566 u32 was_unnum;
Dave Barach6d963c22016-12-05 09:50:05 -0500567
568 /*
569 * The API message field names are backwards from
570 * the underlying data structure names.
571 * It's not worth changing them now.
572 */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200573 if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500574 {
575 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
576 goto done;
577 }
578
579 /* Only check the "use loop0" field when setting the binding */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200580 if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500581 {
582 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
583 goto done;
584 }
585
Eyal Bari1c82cd42017-03-14 14:39:51 +0200586 vnet_sw_interface_t *si =
587 vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
Neale Ranns898273f2017-03-18 02:57:38 -0700588 was_unnum = (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED);
Dave Barach6d963c22016-12-05 09:50:05 -0500589
590 if (mp->is_add)
591 {
592 si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
593 si->unnumbered_sw_if_index = sw_if_index;
Neale Ranns4b919a52017-03-11 05:55:21 -0800594
595 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
596 [unnumbered_sw_if_index] =
597 ip4_main.
598 lookup_main.if_address_pool_index_by_sw_if_index[sw_if_index];
599 ip6_main.
600 lookup_main.if_address_pool_index_by_sw_if_index
601 [unnumbered_sw_if_index] =
602 ip6_main.
603 lookup_main.if_address_pool_index_by_sw_if_index[sw_if_index];
Dave Barach6d963c22016-12-05 09:50:05 -0500604 }
605 else
606 {
607 si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
608 si->unnumbered_sw_if_index = (u32) ~ 0;
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] = ~0;
612 ip6_main.lookup_main.if_address_pool_index_by_sw_if_index
613 [unnumbered_sw_if_index] = ~0;
Dave Barach6d963c22016-12-05 09:50:05 -0500614 }
Neale Ranns898273f2017-03-18 02:57:38 -0700615
616 if (was_unnum != (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
617 {
618 ip4_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add);
619 ip6_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add);
620 }
Dave Barach6d963c22016-12-05 09:50:05 -0500621
622done:
623 REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
624}
625
626static void
627vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
628 mp)
629{
630 vl_api_sw_interface_clear_stats_reply_t *rmp;
631
632 vnet_main_t *vnm = vnet_get_main ();
633 vnet_interface_main_t *im = &vnm->interface_main;
634 vlib_simple_counter_main_t *sm;
635 vlib_combined_counter_main_t *cm;
636 static vnet_main_t **my_vnet_mains;
637 int i, j, n_counters;
638 int rv = 0;
639
640 if (mp->sw_if_index != ~0)
641 VALIDATE_SW_IF_INDEX (mp);
642
643 vec_reset_length (my_vnet_mains);
644
645 for (i = 0; i < vec_len (vnet_mains); i++)
646 {
647 if (vnet_mains[i])
648 vec_add1 (my_vnet_mains, vnet_mains[i]);
649 }
650
651 if (vec_len (vnet_mains) == 0)
652 vec_add1 (my_vnet_mains, vnm);
653
654 n_counters = vec_len (im->combined_sw_if_counters);
655
656 for (j = 0; j < n_counters; j++)
657 {
658 for (i = 0; i < vec_len (my_vnet_mains); i++)
659 {
660 im = &my_vnet_mains[i]->interface_main;
661 cm = im->combined_sw_if_counters + j;
662 if (mp->sw_if_index == (u32) ~ 0)
663 vlib_clear_combined_counters (cm);
664 else
665 vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
666 }
667 }
668
669 n_counters = vec_len (im->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 sm = im->sw_if_counters + j;
677 if (mp->sw_if_index == (u32) ~ 0)
678 vlib_clear_simple_counters (sm);
679 else
680 vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
681 }
682 }
683
684 BAD_SW_IF_INDEX_LABEL;
685
686 REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
687}
688
689#define API_LINK_STATE_EVENT 1
690#define API_ADMIN_UP_DOWN_EVENT 2
691
692static int
693event_data_cmp (void *a1, void *a2)
694{
695 uword *e1 = a1;
696 uword *e2 = a2;
697
698 return (word) e1[0] - (word) e2[0];
699}
700
701static void
Neale Rannsa07bd702017-08-07 07:53:49 -0700702send_sw_interface_event (vpe_api_main_t * am,
Neale Rannsd292ab12017-08-15 12:29:48 -0700703 vpe_client_registration_t * reg,
Dave Barach6d963c22016-12-05 09:50:05 -0500704 unix_shared_memory_queue_t * q,
705 vnet_sw_interface_t * swif)
706{
Neale Rannsa07bd702017-08-07 07:53:49 -0700707 vl_api_sw_interface_event_t *mp;
Dave Barach6d963c22016-12-05 09:50:05 -0500708 vnet_main_t *vnm = am->vnet_main;
709
710 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
711 swif->sw_if_index);
712 mp = vl_msg_api_alloc (sizeof (*mp));
713 memset (mp, 0, sizeof (*mp));
Neale Rannsa07bd702017-08-07 07:53:49 -0700714 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
Dave Barach6d963c22016-12-05 09:50:05 -0500715 mp->sw_if_index = ntohl (swif->sw_if_index);
Neale Rannsd292ab12017-08-15 12:29:48 -0700716 mp->client_index = reg->client_index;
717 mp->pid = reg->client_pid;
Dave Barach6d963c22016-12-05 09:50:05 -0500718
719 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
720 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
721 vl_msg_api_send_shmem (q, (u8 *) & mp);
722}
723
724static uword
725link_state_process (vlib_main_t * vm,
726 vlib_node_runtime_t * rt, vlib_frame_t * f)
727{
728 vpe_api_main_t *vam = &vpe_api_main;
729 vnet_main_t *vnm = vam->vnet_main;
730 vnet_sw_interface_t *swif;
731 uword *event_data = 0;
732 vpe_client_registration_t *reg;
733 int i;
734 u32 prev_sw_if_index;
735 unix_shared_memory_queue_t *q;
736
737 vam->link_state_process_up = 1;
738
739 while (1)
740 {
741 vlib_process_wait_for_event (vm);
742
743 /* Unified list of changed link or admin state sw_if_indices */
744 vlib_process_get_events_with_type
745 (vm, &event_data, API_LINK_STATE_EVENT);
746 vlib_process_get_events_with_type
747 (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
748
749 /* Sort, so we can eliminate duplicates */
750 vec_sort_with_function (event_data, event_data_cmp);
751
752 prev_sw_if_index = ~0;
753
754 for (i = 0; i < vec_len (event_data); i++)
755 {
756 /* Only one message per swif */
757 if (prev_sw_if_index == event_data[i])
758 continue;
759 prev_sw_if_index = event_data[i];
760
761 /* *INDENT-OFF* */
762 pool_foreach(reg, vam->interface_events_registrations,
763 ({
764 q = vl_api_client_index_to_input_queue (reg->client_index);
765 if (q)
766 {
767 /* sw_interface may be deleted already */
768 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
769 event_data[i]))
770 {
771 swif = vnet_get_sw_interface (vnm, event_data[i]);
Neale Rannsd292ab12017-08-15 12:29:48 -0700772 send_sw_interface_event (vam, reg, q, swif);
Dave Barach6d963c22016-12-05 09:50:05 -0500773 }
774 }
775 }));
776 /* *INDENT-ON* */
777 }
778 vec_reset_length (event_data);
779 }
780
781 return 0;
782}
783
784static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
785 u32 flags);
786static clib_error_t *admin_up_down_function (vnet_main_t * vm,
787 u32 hw_if_index, u32 flags);
788
789/* *INDENT-OFF* */
790VLIB_REGISTER_NODE (link_state_process_node,static) = {
791 .function = link_state_process,
792 .type = VLIB_NODE_TYPE_PROCESS,
793 .name = "vpe-link-state-process",
794};
795/* *INDENT-ON* */
796
797VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
798VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
799
800static clib_error_t *
801link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
802{
803 vpe_api_main_t *vam = &vpe_api_main;
804 vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
805
806 if (vam->link_state_process_up)
807 vlib_process_signal_event (vam->vlib_main,
808 link_state_process_node.index,
809 API_LINK_STATE_EVENT, hi->sw_if_index);
810 return 0;
811}
812
813static clib_error_t *
814admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
815{
816 vpe_api_main_t *vam = &vpe_api_main;
817
818 /*
819 * Note: it's perfectly fair to set a subif admin up / admin down.
820 * Note the subtle distinction between this routine and the previous
821 * routine.
822 */
823 if (vam->link_state_process_up)
824 vlib_process_signal_event (vam->vlib_main,
825 link_state_process_node.index,
826 API_ADMIN_UP_DOWN_EVENT, sw_if_index);
827 return 0;
828}
829
830static void vl_api_sw_interface_tag_add_del_t_handler
831 (vl_api_sw_interface_tag_add_del_t * mp)
832{
833 vnet_main_t *vnm = vnet_get_main ();
834 vl_api_sw_interface_tag_add_del_reply_t *rmp;
835 int rv = 0;
836 u8 *tag;
837 u32 sw_if_index = ntohl (mp->sw_if_index);
838
839 VALIDATE_SW_IF_INDEX (mp);
840
841 if (mp->is_add)
842 {
843 if (mp->tag[0] == 0)
844 {
845 rv = VNET_API_ERROR_INVALID_VALUE;
846 goto out;
847 }
848
849 mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
850 tag = format (0, "%s%c", mp->tag, 0);
851 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
852 }
853 else
854 vnet_clear_sw_interface_tag (vnm, sw_if_index);
855
856 BAD_SW_IF_INDEX_LABEL;
857out:
858 REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
859}
860
Jon Loeliger10c273b2017-03-30 08:39:33 -0500861static void vl_api_sw_interface_set_mac_address_t_handler
862 (vl_api_sw_interface_set_mac_address_t * mp)
863{
864 vl_api_sw_interface_set_mac_address_reply_t *rmp;
865 vnet_main_t *vnm = vnet_get_main ();
866 u32 sw_if_index = ntohl (mp->sw_if_index);
867 u64 mac;
868 clib_error_t *error;
869 int rv = 0;
870
871 VALIDATE_SW_IF_INDEX (mp);
872
873 mac = ((u64) mp->mac_address[0] << (8 * 0)
874 | (u64) mp->mac_address[1] << (8 * 1)
875 | (u64) mp->mac_address[2] << (8 * 2)
876 | (u64) mp->mac_address[3] << (8 * 3)
877 | (u64) mp->mac_address[4] << (8 * 4)
878 | (u64) mp->mac_address[5] << (8 * 5));
879
880 error = vnet_hw_interface_change_mac_address (vnm, sw_if_index, mac);
881 if (error)
882 {
883 rv = VNET_API_ERROR_UNIMPLEMENTED;
884 clib_error_report (error);
885 goto out;
886 }
887
888 BAD_SW_IF_INDEX_LABEL;
889out:
890 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
891}
892
Dave Barachaff70772016-10-31 11:59:07 -0400893/*
894 * vpe_api_hookup
895 * Add vpe's API message handlers to the table.
896 * vlib has alread mapped shared memory and
897 * added the client registration handlers.
898 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
899 */
900#define vl_msg_name_crc_list
Dave Barachb5e8a772016-12-06 12:04:42 -0500901#include <vnet/interface.api.h>
Dave Barachaff70772016-10-31 11:59:07 -0400902#undef vl_msg_name_crc_list
903
904static void
905setup_message_id_table (api_main_t * am)
906{
907#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
908 foreach_vl_msg_name_crc_interface;
909#undef _
910}
911
Dave Barach6d963c22016-12-05 09:50:05 -0500912pub_sub_handler (interface_events, INTERFACE_EVENTS);
913
Dave Barachaff70772016-10-31 11:59:07 -0400914static clib_error_t *
915interface_api_hookup (vlib_main_t * vm)
916{
917 api_main_t *am = &api_main;
918
919#define _(N,n) \
920 vl_msg_api_set_handlers(VL_API_##N, #n, \
921 vl_api_##n##_t_handler, \
922 vl_noop_handler, \
923 vl_api_##n##_t_endian, \
924 vl_api_##n##_t_print, \
925 sizeof(vl_api_##n##_t), 1);
926 foreach_vpe_api_msg;
927#undef _
928
929 /*
930 * Set up the (msg_name, crc, message-id) table
931 */
932 setup_message_id_table (am);
933
934 return 0;
935}
936
937VLIB_API_INIT_FUNCTION (interface_api_hookup);
938
939/*
940 * fd.io coding-style-patch-verification: ON
941 *
942 * Local Variables:
943 * eval: (c-set-style "gnu")
944 * End:
945 */