blob: 9a1838fa321f92da96fafcc415421b444bad4364 [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) \
Neale Rannsb8d44812017-11-10 06:53:54 -080063_(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address) \
Juraj Slobodac0374232018-02-01 15:18:49 +010064_(SW_INTERFACE_GET_MAC_ADDRESS, sw_interface_get_mac_address) \
Neale Rannsb8d44812017-11-10 06:53:54 -080065_(CREATE_VLAN_SUBIF, create_vlan_subif) \
66_(CREATE_SUBIF, create_subif) \
67_(DELETE_SUBIF, delete_subif) \
68_(CREATE_LOOPBACK, create_loopback) \
69_(CREATE_LOOPBACK_INSTANCE, create_loopback_instance) \
70_(DELETE_LOOPBACK, delete_loopback) \
Neale Ranns6f4a6be2018-03-16 16:26:21 -070071_(INTERFACE_NAME_RENUMBER, interface_name_renumber) \
72_(COLLECT_DETAILED_INTERFACE_STATS, collect_detailed_interface_stats)
Dave Barachaff70772016-10-31 11:59:07 -040073
74static void
75vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
76{
77 vl_api_sw_interface_set_flags_reply_t *rmp;
78 vnet_main_t *vnm = vnet_get_main ();
79 int rv = 0;
80 clib_error_t *error;
81 u16 flags;
82
83 VALIDATE_SW_IF_INDEX (mp);
84
85 flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
86
87 error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
88 if (error)
89 {
90 rv = -1;
91 clib_error_report (error);
92 }
93
94 BAD_SW_IF_INDEX_LABEL;
95 REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
96}
97
Matus Fabiand162f3d2016-12-05 01:05:35 -080098static void
99vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
100{
101 vl_api_sw_interface_set_mtu_reply_t *rmp;
102 vnet_main_t *vnm = vnet_get_main ();
Matus Fabiand162f3d2016-12-05 01:05:35 -0800103 u32 sw_if_index = ntohl (mp->sw_if_index);
104 u16 mtu = ntohs (mp->mtu);
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200105 ethernet_main_t *em = &ethernet_main;
Matus Fabiand162f3d2016-12-05 01:05:35 -0800106 int rv = 0;
107
108 VALIDATE_SW_IF_INDEX (mp);
109
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200110 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
111 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
112 {
113 rv = VNET_API_ERROR_INVALID_VALUE;
114 goto bad_sw_if_index;
115 }
116
117 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index);
118 ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index);
119
120 if (!eif)
121 {
122 rv = VNET_API_ERROR_FEATURE_DISABLED;
123 goto bad_sw_if_index;
124 }
125
126 if (mtu < hi->min_supported_packet_bytes)
127 {
128 rv = VNET_API_ERROR_INVALID_VALUE;
129 goto bad_sw_if_index;
130 }
131
132 if (mtu > hi->max_supported_packet_bytes)
133 {
134 rv = VNET_API_ERROR_INVALID_VALUE;
135 goto bad_sw_if_index;
136 }
137
138 vnet_hw_interface_set_mtu (vnm, si->hw_if_index, mtu);
Matus Fabiand162f3d2016-12-05 01:05:35 -0800139
140 BAD_SW_IF_INDEX_LABEL;
141 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
142}
143
Dave Barach6d963c22016-12-05 09:50:05 -0500144static void
145send_sw_interface_details (vpe_api_main_t * am,
Dave Barach59b25652017-09-10 15:04:27 -0400146 vl_api_registration_t * rp,
Dave Barach6d963c22016-12-05 09:50:05 -0500147 vnet_sw_interface_t * swif,
148 u8 * interface_name, u32 context)
149{
Eyal Bari1c82cd42017-03-14 14:39:51 +0200150 vnet_hw_interface_t *hi =
151 vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500152
Eyal Bari1c82cd42017-03-14 14:39:51 +0200153 vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barach6d963c22016-12-05 09:50:05 -0500154 memset (mp, 0, sizeof (*mp));
155 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
156 mp->sw_if_index = ntohl (swif->sw_if_index);
157 mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
158 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
159 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
160 mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
161 VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
162 mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
163 VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200164 mp->link_mtu = ntohs (hi->max_packet_bytes);
Dave Barach6d963c22016-12-05 09:50:05 -0500165 mp->context = context;
166
167 strncpy ((char *) mp->interface_name,
168 (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
169
170 /* Send the L2 address for ethernet physical intfcs */
171 if (swif->sup_sw_if_index == swif->sw_if_index
172 && hi->hw_class_index == ethernet_hw_interface_class.index)
173 {
174 ethernet_main_t *em = ethernet_get_main (am->vlib_main);
175 ethernet_interface_t *ei;
176
177 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
178 ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
179 clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
180 mp->l2_address_length = ntohl (sizeof (ei->address));
181 }
182 else if (swif->sup_sw_if_index != swif->sw_if_index)
183 {
184 vnet_sub_interface_t *sub = &swif->sub;
185 mp->sub_id = ntohl (sub->id);
186 mp->sub_dot1ad = sub->eth.flags.dot1ad;
187 mp->sub_number_of_tags =
188 sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
189 mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
190 mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
191 mp->sub_exact_match = sub->eth.flags.exact_match;
192 mp->sub_default = sub->eth.flags.default_sub;
193 mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
194 mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
195
196 /* vlan tag rewrite data */
197 u32 vtr_op = L2_VTR_DISABLED;
198 u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
199
200 if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
201 &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
202 {
203 // error - default to disabled
204 mp->vtr_op = ntohl (L2_VTR_DISABLED);
205 clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
206 swif->sw_if_index);
207 }
208 else
209 {
210 mp->vtr_op = ntohl (vtr_op);
211 mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
212 mp->vtr_tag1 = ntohl (vtr_tag1);
213 mp->vtr_tag2 = ntohl (vtr_tag2);
214 }
215 }
216
Pavel Kotucek65e84572017-01-16 17:01:56 +0100217 /* pbb tag rewrite data */
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100218 ethernet_header_t eth_hdr;
Pavel Kotucek65e84572017-01-16 17:01:56 +0100219 u32 vtr_op = L2_VTR_DISABLED;
220 u16 outer_tag = 0;
Pavel Kotucek65e84572017-01-16 17:01:56 +0100221 u16 b_vlanid = 0;
222 u32 i_sid = 0;
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100223 memset (&eth_hdr, 0, sizeof (eth_hdr));
Pavel Kotucek65e84572017-01-16 17:01:56 +0100224
225 if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100226 &vtr_op, &outer_tag, &eth_hdr, &b_vlanid, &i_sid))
Pavel Kotucek65e84572017-01-16 17:01:56 +0100227 {
228 mp->sub_dot1ah = 1;
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100229 clib_memcpy (mp->b_dmac, eth_hdr.dst_address,
230 sizeof (eth_hdr.dst_address));
231 clib_memcpy (mp->b_smac, eth_hdr.src_address,
232 sizeof (eth_hdr.src_address));
Pavel Kotucek65e84572017-01-16 17:01:56 +0100233 mp->b_vlanid = b_vlanid;
234 mp->i_sid = i_sid;
235 }
236
Eyal Bari1c82cd42017-03-14 14:39:51 +0200237 u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500238 if (tag)
239 strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
240
Florin Corase86a8ed2018-01-05 03:20:25 -0800241 vl_api_send_msg (rp, (u8 *) mp);
Dave Barach6d963c22016-12-05 09:50:05 -0500242}
243
244static void
245vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
246{
247 vpe_api_main_t *am = &vpe_api_main;
248 vnet_sw_interface_t *swif;
249 vnet_interface_main_t *im = &am->vnet_main->interface_main;
Dave Barach59b25652017-09-10 15:04:27 -0400250 vl_api_registration_t *rp;
Dave Barach6d963c22016-12-05 09:50:05 -0500251
Dave Barach59b25652017-09-10 15:04:27 -0400252 rp = vl_api_client_index_to_registration (mp->client_index);
253
254 if (rp == 0)
255 {
256 clib_warning ("Client %d AWOL", mp->client_index);
257 return;
258 }
Dave Barach6d963c22016-12-05 09:50:05 -0500259
Eyal Bari1c82cd42017-03-14 14:39:51 +0200260 u8 *filter = 0, *name = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500261 if (mp->name_filter_valid)
262 {
263 mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
Eyal Bari1c82cd42017-03-14 14:39:51 +0200264 filter = format (0, "%s%c", mp->name_filter, 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500265 }
266
Eyal Bari1c82cd42017-03-14 14:39:51 +0200267 char *strcasestr (char *, char *); /* lnx hdr file botch */
Dave Barach6d963c22016-12-05 09:50:05 -0500268 /* *INDENT-OFF* */
269 pool_foreach (swif, im->sw_interfaces,
270 ({
Eyal Bari1c82cd42017-03-14 14:39:51 +0200271 if (!vnet_swif_is_api_visible (swif))
272 continue;
273 vec_reset_length(name);
274 name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
275 swif, 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500276
Eyal Bari1c82cd42017-03-14 14:39:51 +0200277 if (filter && !strcasestr((char *) name, (char *) filter))
278 continue;
Dave Barach6d963c22016-12-05 09:50:05 -0500279
Dave Barach59b25652017-09-10 15:04:27 -0400280 send_sw_interface_details (am, rp, swif, name, mp->context);
Dave Barach6d963c22016-12-05 09:50:05 -0500281 }));
282 /* *INDENT-ON* */
283
Eyal Bari1c82cd42017-03-14 14:39:51 +0200284 vec_free (name);
285 vec_free (filter);
Dave Barach6d963c22016-12-05 09:50:05 -0500286}
287
288static void
289 vl_api_sw_interface_add_del_address_t_handler
290 (vl_api_sw_interface_add_del_address_t * mp)
291{
292 vlib_main_t *vm = vlib_get_main ();
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500293 vnet_main_t *vnm = vnet_get_main ();
Dave Barach6d963c22016-12-05 09:50:05 -0500294 vl_api_sw_interface_add_del_address_reply_t *rmp;
295 int rv = 0;
296 u32 is_del;
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500297 clib_error_t *error = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500298
299 VALIDATE_SW_IF_INDEX (mp);
300
301 is_del = mp->is_add == 0;
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500302 vnm->api_errno = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500303
304 if (mp->del_all)
305 ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
306 else if (mp->is_ipv6)
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500307 error = ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
308 (void *) mp->address,
309 mp->address_length, is_del);
Dave Barach6d963c22016-12-05 09:50:05 -0500310 else
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500311 error = ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
312 (void *) mp->address,
313 mp->address_length, is_del);
314
315 if (error)
316 {
317 rv = vnm->api_errno;
318 clib_error_report (error);
319 goto done;
320 }
Dave Barach6d963c22016-12-05 09:50:05 -0500321
322 BAD_SW_IF_INDEX_LABEL;
323
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500324done:
Dave Barach6d963c22016-12-05 09:50:05 -0500325 REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
326}
327
328void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
329void
330stats_dslock_with_hint (int hint, int tag)
331{
332}
333
334void stats_dsunlock (void) __attribute__ ((weak));
335void
336stats_dsunlock (void)
337{
338}
339
340static void
341vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
342{
Dave Barach6d963c22016-12-05 09:50:05 -0500343 vl_api_sw_interface_set_table_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700344 u32 sw_if_index = ntohl (mp->sw_if_index);
345 u32 table_id = ntohl (mp->vrf_id);
346 int rv = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500347
348 VALIDATE_SW_IF_INDEX (mp);
349
350 stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
351
352 if (mp->is_ipv6)
Neale Ranns15002542017-09-10 04:39:11 -0700353 rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
Dave Barach6d963c22016-12-05 09:50:05 -0500354 else
Neale Ranns15002542017-09-10 04:39:11 -0700355 rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
Dave Barach6d963c22016-12-05 09:50:05 -0500356
Dave Barach6d963c22016-12-05 09:50:05 -0500357 stats_dsunlock ();
358
359 BAD_SW_IF_INDEX_LABEL;
360
361 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
362}
363
Neale Ranns15002542017-09-10 04:39:11 -0700364int
365ip_table_bind (fib_protocol_t fproto,
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700366 u32 sw_if_index, u32 table_id, u8 is_api)
Neale Ranns15002542017-09-10 04:39:11 -0700367{
368 CLIB_UNUSED (ip_interface_address_t * ia);
369 u32 fib_index, mfib_index;
370 fib_source_t src;
371 mfib_source_t msrc;
372
373 if (is_api)
374 {
375 src = FIB_SOURCE_API;
376 msrc = MFIB_SOURCE_API;
377 }
378 else
379 {
380 src = FIB_SOURCE_CLI;
381 msrc = MFIB_SOURCE_CLI;
382 }
383
Florin Corasd0a59722017-10-15 17:41:21 +0000384 /*
Florin Corasd0a59722017-10-15 17:41:21 +0000385 * This if table does not exist = error is what we want in the end.
386 */
Neale Ranns8f6dd322018-05-17 06:34:24 -0700387 fib_index = fib_table_find (fproto, table_id);
388 mfib_index = mfib_table_find (fproto, table_id);
Florin Corasd0a59722017-10-15 17:41:21 +0000389
Neale Ranns8f6dd322018-05-17 06:34:24 -0700390 if (~0 == fib_index || ~0 == mfib_index)
391 {
392 return (VNET_API_ERROR_NO_SUCH_FIB);
393 }
Florin Corasd0a59722017-10-15 17:41:21 +0000394
Neale Ranns15002542017-09-10 04:39:11 -0700395 if (FIB_PROTOCOL_IP6 == fproto)
396 {
397 /*
398 * If the interface already has in IP address, then a change int
399 * VRF is not allowed. The IP address applied must first be removed.
400 * We do not do that automatically here, since VPP has no knowledge
401 * of whether thoses subnets are valid in the destination VRF.
402 */
403 /* *INDENT-OFF* */
404 foreach_ip_interface_address (&ip6_main.lookup_main,
405 ia, sw_if_index,
406 1 /* honor unnumbered */ ,
407 ({
408 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
409 }));
410 /* *INDENT-ON* */
411
412 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
413 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
414
415 /*
416 * tell those that are interested that the binding is changing.
417 */
418 ip6_table_bind_callback_t *cb;
419 vec_foreach (cb, ip6_main.table_bind_callbacks)
420 cb->function (&ip6_main, cb->function_opaque,
421 sw_if_index,
422 fib_index,
423 ip6_main.fib_index_by_sw_if_index[sw_if_index]);
424
425 if (0 == table_id)
426 {
427 /* reset back to default */
428 if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
429 fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
430 FIB_PROTOCOL_IP6, src);
431 if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
432 mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
433 [sw_if_index], FIB_PROTOCOL_IP6, msrc);
434
435 }
436 else
437 {
438 /* we need to lock the table now it's inuse */
439 fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
440 mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
441 }
442
443 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
444 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
445 }
446 else
447 {
448 /*
449 * If the interface already has in IP address, then a change int
450 * VRF is not allowed. The IP address applied must first be removed.
451 * We do not do that automatically here, since VPP has no knowledge
452 * of whether thoses subnets are valid in the destination VRF.
453 */
454 /* *INDENT-OFF* */
455 foreach_ip_interface_address (&ip4_main.lookup_main,
456 ia, sw_if_index,
457 1 /* honor unnumbered */ ,
458 ({
459 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
460 }));
461 /* *INDENT-ON* */
462
463 vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
464 vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
465
466 /*
467 * tell those that are interested that the binding is changing.
468 */
469 ip4_table_bind_callback_t *cb;
470 vec_foreach (cb, ip4_main.table_bind_callbacks)
471 cb->function (&ip4_main, cb->function_opaque,
472 sw_if_index,
473 fib_index,
474 ip4_main.fib_index_by_sw_if_index[sw_if_index]);
475
476 if (0 == table_id)
477 {
478 /* reset back to default */
479 if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
480 fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
481 FIB_PROTOCOL_IP4, src);
482 if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
483 mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
484 [sw_if_index], FIB_PROTOCOL_IP4, msrc);
485
486 }
487 else
488 {
489 /* we need to lock the table now it's inuse */
490 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
491 table_id, src);
492
493 mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
494 table_id, msrc);
495 }
496
497 ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
498 ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
499 }
500
Neale Ranns15002542017-09-10 04:39:11 -0700501 return (0);
502}
503
Juraj Slobodadfc19232016-12-05 13:20:37 +0100504static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800505send_sw_interface_get_table_reply (vl_api_registration_t * reg,
Juraj Slobodadfc19232016-12-05 13:20:37 +0100506 u32 context, int retval, u32 vrf_id)
507{
508 vl_api_sw_interface_get_table_reply_t *mp;
509
510 mp = vl_msg_api_alloc (sizeof (*mp));
511 memset (mp, 0, sizeof (*mp));
512 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
513 mp->context = context;
514 mp->retval = htonl (retval);
515 mp->vrf_id = htonl (vrf_id);
516
Florin Coras6c4dae22018-01-09 06:39:23 -0800517 vl_api_send_msg (reg, (u8 *) mp);
Juraj Slobodadfc19232016-12-05 13:20:37 +0100518}
519
520static void
521vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
522{
Florin Coras6c4dae22018-01-09 06:39:23 -0800523 vl_api_registration_t *reg;
Juraj Slobodadfc19232016-12-05 13:20:37 +0100524 fib_table_t *fib_table = 0;
525 u32 sw_if_index = ~0;
526 u32 fib_index = ~0;
527 u32 table_id = ~0;
528 fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
529 int rv = 0;
530
Florin Coras6c4dae22018-01-09 06:39:23 -0800531 reg = vl_api_client_index_to_registration (mp->client_index);
532 if (!reg)
Juraj Slobodadfc19232016-12-05 13:20:37 +0100533 return;
534
535 VALIDATE_SW_IF_INDEX (mp);
536
537 sw_if_index = ntohl (mp->sw_if_index);
538
539 if (mp->is_ipv6)
540 fib_proto = FIB_PROTOCOL_IP6;
541
542 fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
543 if (fib_index != ~0)
544 {
545 fib_table = fib_table_get (fib_index, fib_proto);
546 table_id = fib_table->ft_table_id;
547 }
548
549 BAD_SW_IF_INDEX_LABEL;
550
Florin Coras6c4dae22018-01-09 06:39:23 -0800551 send_sw_interface_get_table_reply (reg, mp->context, rv, table_id);
Juraj Slobodadfc19232016-12-05 13:20:37 +0100552}
553
Dave Barach6d963c22016-12-05 09:50:05 -0500554static void vl_api_sw_interface_set_unnumbered_t_handler
555 (vl_api_sw_interface_set_unnumbered_t * mp)
556{
557 vl_api_sw_interface_set_unnumbered_reply_t *rmp;
558 int rv = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500559 vnet_main_t *vnm = vnet_get_main ();
Eyal Bari1c82cd42017-03-14 14:39:51 +0200560 u32 sw_if_index = ntohl (mp->sw_if_index);
561 u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500562
563 /*
564 * The API message field names are backwards from
565 * the underlying data structure names.
566 * It's not worth changing them now.
567 */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200568 if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500569 {
570 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
571 goto done;
572 }
573
574 /* Only check the "use loop0" field when setting the binding */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200575 if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500576 {
577 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
578 goto done;
579 }
580
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700581 vnet_sw_interface_update_unnumbered (unnumbered_sw_if_index,
582 sw_if_index, mp->is_add);
Dave Barach6d963c22016-12-05 09:50:05 -0500583done:
584 REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
585}
586
587static void
588vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
589 mp)
590{
591 vl_api_sw_interface_clear_stats_reply_t *rmp;
592
593 vnet_main_t *vnm = vnet_get_main ();
594 vnet_interface_main_t *im = &vnm->interface_main;
595 vlib_simple_counter_main_t *sm;
596 vlib_combined_counter_main_t *cm;
597 static vnet_main_t **my_vnet_mains;
598 int i, j, n_counters;
599 int rv = 0;
600
601 if (mp->sw_if_index != ~0)
602 VALIDATE_SW_IF_INDEX (mp);
603
604 vec_reset_length (my_vnet_mains);
605
606 for (i = 0; i < vec_len (vnet_mains); i++)
607 {
608 if (vnet_mains[i])
609 vec_add1 (my_vnet_mains, vnet_mains[i]);
610 }
611
612 if (vec_len (vnet_mains) == 0)
613 vec_add1 (my_vnet_mains, vnm);
614
615 n_counters = vec_len (im->combined_sw_if_counters);
616
617 for (j = 0; j < n_counters; j++)
618 {
619 for (i = 0; i < vec_len (my_vnet_mains); i++)
620 {
621 im = &my_vnet_mains[i]->interface_main;
622 cm = im->combined_sw_if_counters + j;
623 if (mp->sw_if_index == (u32) ~ 0)
624 vlib_clear_combined_counters (cm);
625 else
626 vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
627 }
628 }
629
630 n_counters = vec_len (im->sw_if_counters);
631
632 for (j = 0; j < n_counters; j++)
633 {
634 for (i = 0; i < vec_len (my_vnet_mains); i++)
635 {
636 im = &my_vnet_mains[i]->interface_main;
637 sm = im->sw_if_counters + j;
638 if (mp->sw_if_index == (u32) ~ 0)
639 vlib_clear_simple_counters (sm);
640 else
641 vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
642 }
643 }
644
645 BAD_SW_IF_INDEX_LABEL;
646
647 REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
648}
649
650#define API_LINK_STATE_EVENT 1
651#define API_ADMIN_UP_DOWN_EVENT 2
652
653static int
654event_data_cmp (void *a1, void *a2)
655{
656 uword *e1 = a1;
657 uword *e2 = a2;
658
659 return (word) e1[0] - (word) e2[0];
660}
661
662static void
Neale Rannsa07bd702017-08-07 07:53:49 -0700663send_sw_interface_event (vpe_api_main_t * am,
Neale Rannsd292ab12017-08-15 12:29:48 -0700664 vpe_client_registration_t * reg,
Florin Coras6c4dae22018-01-09 06:39:23 -0800665 vl_api_registration_t * vl_reg,
666 vnet_sw_interface_t * swif)
Dave Barach6d963c22016-12-05 09:50:05 -0500667{
Neale Rannsa07bd702017-08-07 07:53:49 -0700668 vl_api_sw_interface_event_t *mp;
Dave Barach6d963c22016-12-05 09:50:05 -0500669 vnet_main_t *vnm = am->vnet_main;
670
671 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
672 swif->sw_if_index);
673 mp = vl_msg_api_alloc (sizeof (*mp));
674 memset (mp, 0, sizeof (*mp));
Neale Rannsa07bd702017-08-07 07:53:49 -0700675 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
Dave Barach6d963c22016-12-05 09:50:05 -0500676 mp->sw_if_index = ntohl (swif->sw_if_index);
Neale Rannsd292ab12017-08-15 12:29:48 -0700677 mp->client_index = reg->client_index;
678 mp->pid = reg->client_pid;
Dave Barach6d963c22016-12-05 09:50:05 -0500679
680 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
681 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
Florin Coras6c4dae22018-01-09 06:39:23 -0800682 vl_api_send_msg (vl_reg, (u8 *) mp);
Dave Barach6d963c22016-12-05 09:50:05 -0500683}
684
685static uword
686link_state_process (vlib_main_t * vm,
687 vlib_node_runtime_t * rt, vlib_frame_t * f)
688{
689 vpe_api_main_t *vam = &vpe_api_main;
690 vnet_main_t *vnm = vam->vnet_main;
691 vnet_sw_interface_t *swif;
692 uword *event_data = 0;
693 vpe_client_registration_t *reg;
694 int i;
695 u32 prev_sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -0800696 vl_api_registration_t *vl_reg;
Dave Barach6d963c22016-12-05 09:50:05 -0500697
698 vam->link_state_process_up = 1;
699
700 while (1)
701 {
702 vlib_process_wait_for_event (vm);
703
704 /* Unified list of changed link or admin state sw_if_indices */
705 vlib_process_get_events_with_type
706 (vm, &event_data, API_LINK_STATE_EVENT);
707 vlib_process_get_events_with_type
708 (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
709
710 /* Sort, so we can eliminate duplicates */
711 vec_sort_with_function (event_data, event_data_cmp);
712
713 prev_sw_if_index = ~0;
714
715 for (i = 0; i < vec_len (event_data); i++)
716 {
717 /* Only one message per swif */
718 if (prev_sw_if_index == event_data[i])
719 continue;
720 prev_sw_if_index = event_data[i];
721
722 /* *INDENT-OFF* */
723 pool_foreach(reg, vam->interface_events_registrations,
724 ({
Florin Coras6c4dae22018-01-09 06:39:23 -0800725 vl_reg = vl_api_client_index_to_registration (reg->client_index);
726 if (vl_reg)
Dave Barach6d963c22016-12-05 09:50:05 -0500727 {
728 /* sw_interface may be deleted already */
729 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
730 event_data[i]))
731 {
732 swif = vnet_get_sw_interface (vnm, event_data[i]);
Florin Coras6c4dae22018-01-09 06:39:23 -0800733 send_sw_interface_event (vam, reg, vl_reg, swif);
Dave Barach6d963c22016-12-05 09:50:05 -0500734 }
735 }
736 }));
737 /* *INDENT-ON* */
738 }
739 vec_reset_length (event_data);
740 }
741
742 return 0;
743}
744
745static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
746 u32 flags);
747static clib_error_t *admin_up_down_function (vnet_main_t * vm,
748 u32 hw_if_index, u32 flags);
749
750/* *INDENT-OFF* */
751VLIB_REGISTER_NODE (link_state_process_node,static) = {
752 .function = link_state_process,
753 .type = VLIB_NODE_TYPE_PROCESS,
754 .name = "vpe-link-state-process",
755};
756/* *INDENT-ON* */
757
758VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
759VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
760
761static clib_error_t *
762link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
763{
764 vpe_api_main_t *vam = &vpe_api_main;
765 vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
766
767 if (vam->link_state_process_up)
768 vlib_process_signal_event (vam->vlib_main,
769 link_state_process_node.index,
770 API_LINK_STATE_EVENT, hi->sw_if_index);
771 return 0;
772}
773
774static clib_error_t *
775admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
776{
777 vpe_api_main_t *vam = &vpe_api_main;
778
779 /*
780 * Note: it's perfectly fair to set a subif admin up / admin down.
781 * Note the subtle distinction between this routine and the previous
782 * routine.
783 */
784 if (vam->link_state_process_up)
785 vlib_process_signal_event (vam->vlib_main,
786 link_state_process_node.index,
787 API_ADMIN_UP_DOWN_EVENT, sw_if_index);
788 return 0;
789}
790
791static void vl_api_sw_interface_tag_add_del_t_handler
792 (vl_api_sw_interface_tag_add_del_t * mp)
793{
794 vnet_main_t *vnm = vnet_get_main ();
795 vl_api_sw_interface_tag_add_del_reply_t *rmp;
796 int rv = 0;
797 u8 *tag;
798 u32 sw_if_index = ntohl (mp->sw_if_index);
799
800 VALIDATE_SW_IF_INDEX (mp);
801
802 if (mp->is_add)
803 {
804 if (mp->tag[0] == 0)
805 {
806 rv = VNET_API_ERROR_INVALID_VALUE;
807 goto out;
808 }
809
810 mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
811 tag = format (0, "%s%c", mp->tag, 0);
812 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
813 }
814 else
815 vnet_clear_sw_interface_tag (vnm, sw_if_index);
816
817 BAD_SW_IF_INDEX_LABEL;
818out:
819 REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
820}
821
Jon Loeliger10c273b2017-03-30 08:39:33 -0500822static void vl_api_sw_interface_set_mac_address_t_handler
823 (vl_api_sw_interface_set_mac_address_t * mp)
824{
825 vl_api_sw_interface_set_mac_address_reply_t *rmp;
826 vnet_main_t *vnm = vnet_get_main ();
827 u32 sw_if_index = ntohl (mp->sw_if_index);
Neale Rannsd867a7c2017-10-04 02:29:07 -0700828 vnet_sw_interface_t *si;
Jon Loeliger10c273b2017-03-30 08:39:33 -0500829 clib_error_t *error;
830 int rv = 0;
831
832 VALIDATE_SW_IF_INDEX (mp);
833
Neale Rannsd867a7c2017-10-04 02:29:07 -0700834 si = vnet_get_sw_interface (vnm, sw_if_index);
John Lo62fcc0a2017-10-31 14:31:10 -0400835 error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index,
836 mp->mac_address);
Jon Loeliger10c273b2017-03-30 08:39:33 -0500837 if (error)
838 {
839 rv = VNET_API_ERROR_UNIMPLEMENTED;
840 clib_error_report (error);
841 goto out;
842 }
843
844 BAD_SW_IF_INDEX_LABEL;
845out:
846 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
847}
848
Juraj Slobodac0374232018-02-01 15:18:49 +0100849static void vl_api_sw_interface_get_mac_address_t_handler
850 (vl_api_sw_interface_get_mac_address_t * mp)
851{
852 vl_api_sw_interface_get_mac_address_reply_t *rmp;
853 vl_api_registration_t *reg;
854 vnet_main_t *vnm = vnet_get_main ();
855 u32 sw_if_index = ntohl (mp->sw_if_index);
856 vnet_sw_interface_t *si;
857 ethernet_interface_t *eth_if = 0;
858 int rv = 0;
859
860 VALIDATE_SW_IF_INDEX (mp);
861
862 si = vnet_get_sup_sw_interface (vnm, sw_if_index);
863 if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
864 eth_if = ethernet_get_interface (&ethernet_main, si->hw_if_index);
865
866 BAD_SW_IF_INDEX_LABEL;
867
868 reg = vl_api_client_index_to_registration (mp->client_index);
869 if (!reg)
870 return;
871 rmp = vl_msg_api_alloc (sizeof (*rmp));
872 rmp->_vl_msg_id = htons (VL_API_SW_INTERFACE_GET_MAC_ADDRESS_REPLY);
873 rmp->context = mp->context;
874 rmp->retval = htonl (rv);
875 if (!rv && eth_if)
876 memcpy (rmp->mac_address, eth_if->address, 6);
877 vl_api_send_msg (reg, (u8 *) rmp);
878}
879
Stevenad8015b2017-10-29 22:10:46 -0700880static void vl_api_sw_interface_set_rx_mode_t_handler
881 (vl_api_sw_interface_set_rx_mode_t * mp)
882{
883 vl_api_sw_interface_set_rx_mode_reply_t *rmp;
884 vnet_main_t *vnm = vnet_get_main ();
885 u32 sw_if_index = ntohl (mp->sw_if_index);
886 vnet_sw_interface_t *si;
887 clib_error_t *error;
888 int rv = 0;
889
890 VALIDATE_SW_IF_INDEX (mp);
891
892 si = vnet_get_sw_interface (vnm, sw_if_index);
893 error = set_hw_interface_change_rx_mode (vnm, si->hw_if_index,
894 mp->queue_id_valid,
895 ntohl (mp->queue_id), mp->mode);
896 if (error)
897 {
898 rv = VNET_API_ERROR_UNIMPLEMENTED;
899 clib_error_report (error);
900 goto out;
901 }
902
903 BAD_SW_IF_INDEX_LABEL;
904out:
905 REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_MODE_REPLY);
906}
907
Neale Rannsb8d44812017-11-10 06:53:54 -0800908static void
909vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
910{
911 vl_api_create_vlan_subif_reply_t *rmp;
912 vnet_main_t *vnm = vnet_get_main ();
913 u32 sw_if_index = (u32) ~ 0;
914 vnet_hw_interface_t *hi;
915 int rv = 0;
916 u32 id;
917 vnet_sw_interface_t template;
918 uword *p;
919 vnet_interface_main_t *im = &vnm->interface_main;
920 u64 sup_and_sub_key;
Florin Coras6c4dae22018-01-09 06:39:23 -0800921 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -0800922 clib_error_t *error;
923
924 VALIDATE_SW_IF_INDEX (mp);
925
926 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
927
928 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
929 {
930 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
931 goto out;
932 }
933
934 id = ntohl (mp->vlan_id);
935 if (id == 0 || id > 4095)
936 {
937 rv = VNET_API_ERROR_INVALID_VLAN;
938 goto out;
939 }
940
941 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
942
943 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
944 if (p)
945 {
946 rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
947 goto out;
948 }
949
950 memset (&template, 0, sizeof (template));
951 template.type = VNET_SW_INTERFACE_TYPE_SUB;
952 template.sup_sw_if_index = hi->sw_if_index;
953 template.sub.id = id;
954 template.sub.eth.raw_flags = 0;
955 template.sub.eth.flags.one_tag = 1;
956 template.sub.eth.outer_vlan_id = id;
957 template.sub.eth.flags.exact_match = 1;
958
959 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
960 if (error)
961 {
962 clib_error_report (error);
963 rv = VNET_API_ERROR_INVALID_REGISTRATION;
964 goto out;
965 }
966
967 u64 *kp = clib_mem_alloc (sizeof (*kp));
968 *kp = sup_and_sub_key;
969
970 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
971 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
972
973 BAD_SW_IF_INDEX_LABEL;
974
975out:
Florin Coras6c4dae22018-01-09 06:39:23 -0800976 reg = vl_api_client_index_to_registration (mp->client_index);
977 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -0800978 return;
979
980 rmp = vl_msg_api_alloc (sizeof (*rmp));
981 rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
982 rmp->context = mp->context;
983 rmp->retval = htonl (rv);
984 rmp->sw_if_index = htonl (sw_if_index);
Florin Coras6c4dae22018-01-09 06:39:23 -0800985 vl_api_send_msg (reg, (u8 *) rmp);
Neale Rannsb8d44812017-11-10 06:53:54 -0800986}
987
988static void
989vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
990{
991 vl_api_create_subif_reply_t *rmp;
992 vnet_main_t *vnm = vnet_get_main ();
993 u32 sw_if_index = ~0;
994 int rv = 0;
995 u32 sub_id;
996 vnet_sw_interface_t *si;
997 vnet_hw_interface_t *hi;
998 vnet_sw_interface_t template;
999 uword *p;
1000 vnet_interface_main_t *im = &vnm->interface_main;
1001 u64 sup_and_sub_key;
1002 clib_error_t *error;
1003
1004 VALIDATE_SW_IF_INDEX (mp);
1005
1006 si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
1007 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1008
1009 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1010 {
1011 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1012 goto out;
1013 }
1014
1015 sw_if_index = si->sw_if_index;
1016 sub_id = ntohl (mp->sub_id);
1017
1018 sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
1019
1020 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1021 if (p)
1022 {
1023 if (CLIB_DEBUG > 0)
1024 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
1025 sw_if_index, sub_id);
1026 rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
1027 goto out;
1028 }
1029
1030 memset (&template, 0, sizeof (template));
1031 template.type = VNET_SW_INTERFACE_TYPE_SUB;
1032 template.sup_sw_if_index = sw_if_index;
1033 template.sub.id = sub_id;
1034 template.sub.eth.flags.no_tags = mp->no_tags;
1035 template.sub.eth.flags.one_tag = mp->one_tag;
1036 template.sub.eth.flags.two_tags = mp->two_tags;
1037 template.sub.eth.flags.dot1ad = mp->dot1ad;
1038 template.sub.eth.flags.exact_match = mp->exact_match;
1039 template.sub.eth.flags.default_sub = mp->default_sub;
1040 template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
1041 template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
1042 template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
1043 template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
1044
1045 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1046 if (error)
1047 {
1048 clib_error_report (error);
1049 rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
1050 goto out;
1051 }
1052
1053 u64 *kp = clib_mem_alloc (sizeof (*kp));
1054 *kp = sup_and_sub_key;
1055
1056 hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
1057 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1058
1059 BAD_SW_IF_INDEX_LABEL;
1060
1061out:
1062
1063 /* *INDENT-OFF* */
1064 REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
1065 ({
1066 rmp->sw_if_index = ntohl(sw_if_index);
1067 }));
1068 /* *INDENT-ON* */
1069}
1070
1071static void
1072vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
1073{
1074 vl_api_delete_subif_reply_t *rmp;
1075 int rv;
1076
1077 rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
1078
1079 REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
1080}
1081
1082static void
1083vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
1084 mp)
1085{
1086 vl_api_interface_name_renumber_reply_t *rmp;
1087 int rv = 0;
1088
1089 VALIDATE_SW_IF_INDEX (mp);
1090
1091 rv = vnet_interface_name_renumber
1092 (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
1093
1094 BAD_SW_IF_INDEX_LABEL;
1095
1096 REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
1097}
1098
1099static void
1100vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
1101{
1102 vl_api_create_loopback_reply_t *rmp;
1103 u32 sw_if_index;
1104 int rv;
1105
1106 rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address, 0, 0);
1107
1108 /* *INDENT-OFF* */
1109 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
1110 ({
1111 rmp->sw_if_index = ntohl (sw_if_index);
1112 }));
1113 /* *INDENT-ON* */
1114}
1115
1116static void vl_api_create_loopback_instance_t_handler
1117 (vl_api_create_loopback_instance_t * mp)
1118{
1119 vl_api_create_loopback_instance_reply_t *rmp;
1120 u32 sw_if_index;
1121 u8 is_specified = mp->is_specified;
1122 u32 user_instance = ntohl (mp->user_instance);
1123 int rv;
1124
1125 rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address,
1126 is_specified, user_instance);
1127
1128 /* *INDENT-OFF* */
1129 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY,
1130 ({
1131 rmp->sw_if_index = ntohl (sw_if_index);
1132 }));
1133 /* *INDENT-ON* */
1134}
1135
1136static void
1137vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
1138{
1139 vl_api_delete_loopback_reply_t *rmp;
1140 u32 sw_if_index;
1141 int rv;
1142
1143 sw_if_index = ntohl (mp->sw_if_index);
1144 rv = vnet_delete_loopback_interface (sw_if_index);
1145
1146 REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
1147}
1148
Neale Ranns6f4a6be2018-03-16 16:26:21 -07001149static void
1150 vl_api_collect_detailed_interface_stats_t_handler
1151 (vl_api_collect_detailed_interface_stats_t * mp)
1152{
1153 vl_api_collect_detailed_interface_stats_reply_t *rmp;
1154 int rv = 0;
1155
Neale Ranns871dc422018-03-29 01:28:09 -07001156 rv =
1157 vnet_sw_interface_stats_collect_enable_disable (ntohl (mp->sw_if_index),
1158 mp->enable_disable);
Neale Ranns6f4a6be2018-03-16 16:26:21 -07001159
1160 REPLY_MACRO (VL_API_COLLECT_DETAILED_INTERFACE_STATS_REPLY);
1161}
1162
Dave Barachaff70772016-10-31 11:59:07 -04001163/*
1164 * vpe_api_hookup
1165 * Add vpe's API message handlers to the table.
1166 * vlib has alread mapped shared memory and
1167 * added the client registration handlers.
1168 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1169 */
1170#define vl_msg_name_crc_list
Dave Barachb5e8a772016-12-06 12:04:42 -05001171#include <vnet/interface.api.h>
Dave Barachaff70772016-10-31 11:59:07 -04001172#undef vl_msg_name_crc_list
1173
1174static void
1175setup_message_id_table (api_main_t * am)
1176{
1177#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1178 foreach_vl_msg_name_crc_interface;
1179#undef _
1180}
1181
Dave Barach6d963c22016-12-05 09:50:05 -05001182pub_sub_handler (interface_events, INTERFACE_EVENTS);
1183
Dave Barachaff70772016-10-31 11:59:07 -04001184static clib_error_t *
1185interface_api_hookup (vlib_main_t * vm)
1186{
1187 api_main_t *am = &api_main;
1188
1189#define _(N,n) \
1190 vl_msg_api_set_handlers(VL_API_##N, #n, \
1191 vl_api_##n##_t_handler, \
1192 vl_noop_handler, \
1193 vl_api_##n##_t_endian, \
1194 vl_api_##n##_t_print, \
1195 sizeof(vl_api_##n##_t), 1);
1196 foreach_vpe_api_msg;
1197#undef _
1198
1199 /*
1200 * Set up the (msg_name, crc, message-id) table
1201 */
1202 setup_message_id_table (am);
1203
1204 return 0;
1205}
1206
1207VLIB_API_INIT_FUNCTION (interface_api_hookup);
1208
1209/*
1210 * fd.io coding-style-patch-verification: ON
1211 *
1212 * Local Variables:
1213 * eval: (c-set-style "gnu")
1214 * End:
1215 */