blob: 03434eab6719126784b6519e3b99b2964d9fb29a [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) \
Ole Troand7231612018-06-07 10:17:57 +020053_(HW_INTERFACE_SET_MTU, hw_interface_set_mtu) \
Dave Barach6d963c22016-12-05 09:50:05 -050054_(SW_INTERFACE_SET_MTU, sw_interface_set_mtu) \
55_(WANT_INTERFACE_EVENTS, want_interface_events) \
56_(SW_INTERFACE_DUMP, sw_interface_dump) \
Dave Barach6d963c22016-12-05 09:50:05 -050057_(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address) \
Stevenad8015b2017-10-29 22:10:46 -070058_(SW_INTERFACE_SET_RX_MODE, sw_interface_set_rx_mode) \
Mohsin Kazmi54f7c512018-08-23 18:28:11 +020059_(SW_INTERFACE_SET_RX_PLACEMENT, sw_interface_set_rx_placement) \
Dave Barach6d963c22016-12-05 09:50:05 -050060_(SW_INTERFACE_SET_TABLE, sw_interface_set_table) \
Juraj Slobodadfc19232016-12-05 13:20:37 +010061_(SW_INTERFACE_GET_TABLE, sw_interface_get_table) \
Dave Barach6d963c22016-12-05 09:50:05 -050062_(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered) \
63_(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats) \
Jon Loeliger10c273b2017-03-30 08:39:33 -050064_(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del) \
Neale Rannsb8d44812017-11-10 06:53:54 -080065_(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address) \
Juraj Slobodac0374232018-02-01 15:18:49 +010066_(SW_INTERFACE_GET_MAC_ADDRESS, sw_interface_get_mac_address) \
Neale Rannsb8d44812017-11-10 06:53:54 -080067_(CREATE_VLAN_SUBIF, create_vlan_subif) \
68_(CREATE_SUBIF, create_subif) \
69_(DELETE_SUBIF, delete_subif) \
70_(CREATE_LOOPBACK, create_loopback) \
71_(CREATE_LOOPBACK_INSTANCE, create_loopback_instance) \
72_(DELETE_LOOPBACK, delete_loopback) \
Neale Ranns6f4a6be2018-03-16 16:26:21 -070073_(INTERFACE_NAME_RENUMBER, interface_name_renumber) \
Neale Ranns1855b8e2018-07-11 10:31:26 -070074_(COLLECT_DETAILED_INTERFACE_STATS, collect_detailed_interface_stats) \
75_(SW_INTERFACE_SET_IP_DIRECTED_BROADCAST, \
76 sw_interface_set_ip_directed_broadcast)
Dave Barachaff70772016-10-31 11:59:07 -040077
78static void
79vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
80{
81 vl_api_sw_interface_set_flags_reply_t *rmp;
82 vnet_main_t *vnm = vnet_get_main ();
83 int rv = 0;
84 clib_error_t *error;
85 u16 flags;
86
87 VALIDATE_SW_IF_INDEX (mp);
88
89 flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
90
91 error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
92 if (error)
93 {
94 rv = -1;
95 clib_error_report (error);
96 }
97
98 BAD_SW_IF_INDEX_LABEL;
99 REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
100}
101
Matus Fabiand162f3d2016-12-05 01:05:35 -0800102static void
Ole Troand7231612018-06-07 10:17:57 +0200103vl_api_hw_interface_set_mtu_t_handler (vl_api_hw_interface_set_mtu_t * mp)
Matus Fabiand162f3d2016-12-05 01:05:35 -0800104{
Ole Troand7231612018-06-07 10:17:57 +0200105 vl_api_hw_interface_set_mtu_reply_t *rmp;
Matus Fabiand162f3d2016-12-05 01:05:35 -0800106 vnet_main_t *vnm = vnet_get_main ();
Matus Fabiand162f3d2016-12-05 01:05:35 -0800107 u32 sw_if_index = ntohl (mp->sw_if_index);
108 u16 mtu = ntohs (mp->mtu);
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200109 ethernet_main_t *em = &ethernet_main;
Matus Fabiand162f3d2016-12-05 01:05:35 -0800110 int rv = 0;
111
112 VALIDATE_SW_IF_INDEX (mp);
113
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200114 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
115 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
116 {
117 rv = VNET_API_ERROR_INVALID_VALUE;
118 goto bad_sw_if_index;
119 }
120
121 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index);
122 ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index);
123
124 if (!eif)
125 {
126 rv = VNET_API_ERROR_FEATURE_DISABLED;
127 goto bad_sw_if_index;
128 }
129
130 if (mtu < hi->min_supported_packet_bytes)
131 {
132 rv = VNET_API_ERROR_INVALID_VALUE;
133 goto bad_sw_if_index;
134 }
135
136 if (mtu > hi->max_supported_packet_bytes)
137 {
138 rv = VNET_API_ERROR_INVALID_VALUE;
139 goto bad_sw_if_index;
140 }
141
142 vnet_hw_interface_set_mtu (vnm, si->hw_if_index, mtu);
Matus Fabiand162f3d2016-12-05 01:05:35 -0800143
144 BAD_SW_IF_INDEX_LABEL;
Ole Troand7231612018-06-07 10:17:57 +0200145 REPLY_MACRO (VL_API_HW_INTERFACE_SET_MTU_REPLY);
146}
147
148static void
149vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
150{
151 vl_api_sw_interface_set_mtu_reply_t *rmp;
152 vnet_main_t *vnm = vnet_get_main ();
153 u32 sw_if_index = ntohl (mp->sw_if_index);
154 int rv = 0;
155 int i;
156 u32 per_protocol_mtu[VNET_N_MTU];
157
158 VALIDATE_SW_IF_INDEX (mp);
159
160 for (i = 0; i < VNET_N_MTU; i++)
161 per_protocol_mtu[i] = ntohl (mp->mtu[i]);
162
163 vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, per_protocol_mtu);
164
165 BAD_SW_IF_INDEX_LABEL;
Matus Fabiand162f3d2016-12-05 01:05:35 -0800166 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
167}
168
Dave Barach6d963c22016-12-05 09:50:05 -0500169static void
Neale Ranns1855b8e2018-07-11 10:31:26 -0700170 vl_api_sw_interface_set_ip_directed_broadcast_t_handler
171 (vl_api_sw_interface_set_ip_directed_broadcast_t * mp)
172{
173 vl_api_sw_interface_set_ip_directed_broadcast_reply_t *rmp;
174 u32 sw_if_index = ntohl (mp->sw_if_index);
175 int rv = 0;
176
177 VALIDATE_SW_IF_INDEX (mp);
178
179 vnet_sw_interface_ip_directed_broadcast (vnet_get_main (),
180 sw_if_index, mp->enable);
181
182 BAD_SW_IF_INDEX_LABEL;
183 REPLY_MACRO (VL_API_SW_INTERFACE_SET_IP_DIRECTED_BROADCAST_REPLY);
184}
185
186static void
Dave Barach6d963c22016-12-05 09:50:05 -0500187send_sw_interface_details (vpe_api_main_t * am,
Dave Barach59b25652017-09-10 15:04:27 -0400188 vl_api_registration_t * rp,
Dave Barach6d963c22016-12-05 09:50:05 -0500189 vnet_sw_interface_t * swif,
190 u8 * interface_name, u32 context)
191{
Eyal Bari1c82cd42017-03-14 14:39:51 +0200192 vnet_hw_interface_t *hi =
193 vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500194
Eyal Bari1c82cd42017-03-14 14:39:51 +0200195 vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barach6d963c22016-12-05 09:50:05 -0500196 memset (mp, 0, sizeof (*mp));
197 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
198 mp->sw_if_index = ntohl (swif->sw_if_index);
199 mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
200 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
201 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
202 mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
203 VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
204 mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
205 VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200206 mp->link_mtu = ntohs (hi->max_packet_bytes);
Ole Troand7231612018-06-07 10:17:57 +0200207 mp->mtu[VNET_MTU_L3] = ntohl (swif->mtu[VNET_MTU_L3]);
208 mp->mtu[VNET_MTU_IP4] = ntohl (swif->mtu[VNET_MTU_IP4]);
209 mp->mtu[VNET_MTU_IP6] = ntohl (swif->mtu[VNET_MTU_IP6]);
210 mp->mtu[VNET_MTU_MPLS] = ntohl (swif->mtu[VNET_MTU_MPLS]);
211
Dave Barach6d963c22016-12-05 09:50:05 -0500212 mp->context = context;
213
214 strncpy ((char *) mp->interface_name,
215 (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
216
217 /* Send the L2 address for ethernet physical intfcs */
218 if (swif->sup_sw_if_index == swif->sw_if_index
219 && hi->hw_class_index == ethernet_hw_interface_class.index)
220 {
221 ethernet_main_t *em = ethernet_get_main (am->vlib_main);
222 ethernet_interface_t *ei;
223
224 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
225 ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
226 clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
227 mp->l2_address_length = ntohl (sizeof (ei->address));
228 }
229 else if (swif->sup_sw_if_index != swif->sw_if_index)
230 {
231 vnet_sub_interface_t *sub = &swif->sub;
232 mp->sub_id = ntohl (sub->id);
233 mp->sub_dot1ad = sub->eth.flags.dot1ad;
234 mp->sub_number_of_tags =
235 sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
236 mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
237 mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
238 mp->sub_exact_match = sub->eth.flags.exact_match;
239 mp->sub_default = sub->eth.flags.default_sub;
240 mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
241 mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
242
243 /* vlan tag rewrite data */
244 u32 vtr_op = L2_VTR_DISABLED;
245 u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
246
247 if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
248 &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
249 {
250 // error - default to disabled
251 mp->vtr_op = ntohl (L2_VTR_DISABLED);
252 clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
253 swif->sw_if_index);
254 }
255 else
256 {
257 mp->vtr_op = ntohl (vtr_op);
258 mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
259 mp->vtr_tag1 = ntohl (vtr_tag1);
260 mp->vtr_tag2 = ntohl (vtr_tag2);
261 }
262 }
263
Pavel Kotucek65e84572017-01-16 17:01:56 +0100264 /* pbb tag rewrite data */
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100265 ethernet_header_t eth_hdr;
Pavel Kotucek65e84572017-01-16 17:01:56 +0100266 u32 vtr_op = L2_VTR_DISABLED;
267 u16 outer_tag = 0;
Pavel Kotucek65e84572017-01-16 17:01:56 +0100268 u16 b_vlanid = 0;
269 u32 i_sid = 0;
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100270 memset (&eth_hdr, 0, sizeof (eth_hdr));
Pavel Kotucek65e84572017-01-16 17:01:56 +0100271
272 if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100273 &vtr_op, &outer_tag, &eth_hdr, &b_vlanid, &i_sid))
Pavel Kotucek65e84572017-01-16 17:01:56 +0100274 {
275 mp->sub_dot1ah = 1;
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100276 clib_memcpy (mp->b_dmac, eth_hdr.dst_address,
277 sizeof (eth_hdr.dst_address));
278 clib_memcpy (mp->b_smac, eth_hdr.src_address,
279 sizeof (eth_hdr.src_address));
Pavel Kotucek65e84572017-01-16 17:01:56 +0100280 mp->b_vlanid = b_vlanid;
281 mp->i_sid = i_sid;
282 }
283
Eyal Bari1c82cd42017-03-14 14:39:51 +0200284 u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500285 if (tag)
286 strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
287
Florin Corase86a8ed2018-01-05 03:20:25 -0800288 vl_api_send_msg (rp, (u8 *) mp);
Dave Barach6d963c22016-12-05 09:50:05 -0500289}
290
291static void
292vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
293{
294 vpe_api_main_t *am = &vpe_api_main;
295 vnet_sw_interface_t *swif;
296 vnet_interface_main_t *im = &am->vnet_main->interface_main;
Dave Barach59b25652017-09-10 15:04:27 -0400297 vl_api_registration_t *rp;
Dave Barach6d963c22016-12-05 09:50:05 -0500298
Dave Barach59b25652017-09-10 15:04:27 -0400299 rp = vl_api_client_index_to_registration (mp->client_index);
300
301 if (rp == 0)
302 {
303 clib_warning ("Client %d AWOL", mp->client_index);
304 return;
305 }
Dave Barach6d963c22016-12-05 09:50:05 -0500306
Eyal Bari1c82cd42017-03-14 14:39:51 +0200307 u8 *filter = 0, *name = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500308 if (mp->name_filter_valid)
309 {
310 mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
Eyal Bari1c82cd42017-03-14 14:39:51 +0200311 filter = format (0, "%s%c", mp->name_filter, 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500312 }
313
Eyal Bari1c82cd42017-03-14 14:39:51 +0200314 char *strcasestr (char *, char *); /* lnx hdr file botch */
Dave Barach6d963c22016-12-05 09:50:05 -0500315 /* *INDENT-OFF* */
316 pool_foreach (swif, im->sw_interfaces,
317 ({
Eyal Bari1c82cd42017-03-14 14:39:51 +0200318 if (!vnet_swif_is_api_visible (swif))
319 continue;
320 vec_reset_length(name);
321 name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
322 swif, 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500323
Eyal Bari1c82cd42017-03-14 14:39:51 +0200324 if (filter && !strcasestr((char *) name, (char *) filter))
325 continue;
Dave Barach6d963c22016-12-05 09:50:05 -0500326
Dave Barach59b25652017-09-10 15:04:27 -0400327 send_sw_interface_details (am, rp, swif, name, mp->context);
Dave Barach6d963c22016-12-05 09:50:05 -0500328 }));
329 /* *INDENT-ON* */
330
Eyal Bari1c82cd42017-03-14 14:39:51 +0200331 vec_free (name);
332 vec_free (filter);
Dave Barach6d963c22016-12-05 09:50:05 -0500333}
334
335static void
336 vl_api_sw_interface_add_del_address_t_handler
337 (vl_api_sw_interface_add_del_address_t * mp)
338{
339 vlib_main_t *vm = vlib_get_main ();
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500340 vnet_main_t *vnm = vnet_get_main ();
Dave Barach6d963c22016-12-05 09:50:05 -0500341 vl_api_sw_interface_add_del_address_reply_t *rmp;
342 int rv = 0;
343 u32 is_del;
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500344 clib_error_t *error = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500345
346 VALIDATE_SW_IF_INDEX (mp);
347
348 is_del = mp->is_add == 0;
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500349 vnm->api_errno = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500350
351 if (mp->del_all)
352 ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
353 else if (mp->is_ipv6)
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500354 error = ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
355 (void *) mp->address,
356 mp->address_length, is_del);
Dave Barach6d963c22016-12-05 09:50:05 -0500357 else
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500358 error = ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
359 (void *) mp->address,
360 mp->address_length, is_del);
361
362 if (error)
363 {
364 rv = vnm->api_errno;
365 clib_error_report (error);
366 goto done;
367 }
Dave Barach6d963c22016-12-05 09:50:05 -0500368
369 BAD_SW_IF_INDEX_LABEL;
370
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500371done:
Dave Barach6d963c22016-12-05 09:50:05 -0500372 REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
373}
374
375void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
376void
377stats_dslock_with_hint (int hint, int tag)
378{
379}
380
381void stats_dsunlock (void) __attribute__ ((weak));
382void
383stats_dsunlock (void)
384{
385}
386
387static void
388vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
389{
Dave Barach6d963c22016-12-05 09:50:05 -0500390 vl_api_sw_interface_set_table_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700391 u32 sw_if_index = ntohl (mp->sw_if_index);
392 u32 table_id = ntohl (mp->vrf_id);
393 int rv = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500394
395 VALIDATE_SW_IF_INDEX (mp);
396
397 stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
398
399 if (mp->is_ipv6)
Neale Ranns15002542017-09-10 04:39:11 -0700400 rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
Dave Barach6d963c22016-12-05 09:50:05 -0500401 else
Neale Ranns15002542017-09-10 04:39:11 -0700402 rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
Dave Barach6d963c22016-12-05 09:50:05 -0500403
Dave Barach6d963c22016-12-05 09:50:05 -0500404 stats_dsunlock ();
405
406 BAD_SW_IF_INDEX_LABEL;
407
408 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
409}
410
Neale Ranns15002542017-09-10 04:39:11 -0700411int
412ip_table_bind (fib_protocol_t fproto,
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700413 u32 sw_if_index, u32 table_id, u8 is_api)
Neale Ranns15002542017-09-10 04:39:11 -0700414{
415 CLIB_UNUSED (ip_interface_address_t * ia);
416 u32 fib_index, mfib_index;
417 fib_source_t src;
418 mfib_source_t msrc;
419
420 if (is_api)
421 {
422 src = FIB_SOURCE_API;
423 msrc = MFIB_SOURCE_API;
424 }
425 else
426 {
427 src = FIB_SOURCE_CLI;
428 msrc = MFIB_SOURCE_CLI;
429 }
430
Florin Corasd0a59722017-10-15 17:41:21 +0000431 /*
Florin Corasd0a59722017-10-15 17:41:21 +0000432 * This if table does not exist = error is what we want in the end.
433 */
Neale Ranns8f6dd322018-05-17 06:34:24 -0700434 fib_index = fib_table_find (fproto, table_id);
435 mfib_index = mfib_table_find (fproto, table_id);
Florin Corasd0a59722017-10-15 17:41:21 +0000436
Neale Ranns8f6dd322018-05-17 06:34:24 -0700437 if (~0 == fib_index || ~0 == mfib_index)
438 {
439 return (VNET_API_ERROR_NO_SUCH_FIB);
440 }
Florin Corasd0a59722017-10-15 17:41:21 +0000441
Neale Ranns15002542017-09-10 04:39:11 -0700442 if (FIB_PROTOCOL_IP6 == fproto)
443 {
444 /*
445 * If the interface already has in IP address, then a change int
446 * VRF is not allowed. The IP address applied must first be removed.
447 * We do not do that automatically here, since VPP has no knowledge
448 * of whether thoses subnets are valid in the destination VRF.
449 */
450 /* *INDENT-OFF* */
451 foreach_ip_interface_address (&ip6_main.lookup_main,
452 ia, sw_if_index,
453 1 /* honor unnumbered */ ,
454 ({
455 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
456 }));
457 /* *INDENT-ON* */
458
459 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
460 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
461
462 /*
463 * tell those that are interested that the binding is changing.
464 */
465 ip6_table_bind_callback_t *cb;
466 vec_foreach (cb, ip6_main.table_bind_callbacks)
467 cb->function (&ip6_main, cb->function_opaque,
468 sw_if_index,
469 fib_index,
470 ip6_main.fib_index_by_sw_if_index[sw_if_index]);
471
472 if (0 == table_id)
473 {
474 /* reset back to default */
475 if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
476 fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
477 FIB_PROTOCOL_IP6, src);
478 if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
479 mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
480 [sw_if_index], FIB_PROTOCOL_IP6, msrc);
481
482 }
483 else
484 {
485 /* we need to lock the table now it's inuse */
486 fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
487 mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
488 }
489
490 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
491 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
492 }
493 else
494 {
495 /*
496 * If the interface already has in IP address, then a change int
497 * VRF is not allowed. The IP address applied must first be removed.
498 * We do not do that automatically here, since VPP has no knowledge
499 * of whether thoses subnets are valid in the destination VRF.
500 */
501 /* *INDENT-OFF* */
502 foreach_ip_interface_address (&ip4_main.lookup_main,
503 ia, sw_if_index,
504 1 /* honor unnumbered */ ,
505 ({
506 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
507 }));
508 /* *INDENT-ON* */
509
510 vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
511 vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
512
513 /*
514 * tell those that are interested that the binding is changing.
515 */
516 ip4_table_bind_callback_t *cb;
517 vec_foreach (cb, ip4_main.table_bind_callbacks)
518 cb->function (&ip4_main, cb->function_opaque,
519 sw_if_index,
520 fib_index,
521 ip4_main.fib_index_by_sw_if_index[sw_if_index]);
522
523 if (0 == table_id)
524 {
525 /* reset back to default */
526 if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
527 fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
528 FIB_PROTOCOL_IP4, src);
529 if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
530 mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
531 [sw_if_index], FIB_PROTOCOL_IP4, msrc);
532
533 }
534 else
535 {
536 /* we need to lock the table now it's inuse */
537 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
538 table_id, src);
539
540 mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
541 table_id, msrc);
542 }
543
544 ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
545 ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
546 }
547
Neale Ranns15002542017-09-10 04:39:11 -0700548 return (0);
549}
550
Juraj Slobodadfc19232016-12-05 13:20:37 +0100551static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800552send_sw_interface_get_table_reply (vl_api_registration_t * reg,
Juraj Slobodadfc19232016-12-05 13:20:37 +0100553 u32 context, int retval, u32 vrf_id)
554{
555 vl_api_sw_interface_get_table_reply_t *mp;
556
557 mp = vl_msg_api_alloc (sizeof (*mp));
558 memset (mp, 0, sizeof (*mp));
559 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
560 mp->context = context;
561 mp->retval = htonl (retval);
562 mp->vrf_id = htonl (vrf_id);
563
Florin Coras6c4dae22018-01-09 06:39:23 -0800564 vl_api_send_msg (reg, (u8 *) mp);
Juraj Slobodadfc19232016-12-05 13:20:37 +0100565}
566
567static void
568vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
569{
Florin Coras6c4dae22018-01-09 06:39:23 -0800570 vl_api_registration_t *reg;
Juraj Slobodadfc19232016-12-05 13:20:37 +0100571 fib_table_t *fib_table = 0;
572 u32 sw_if_index = ~0;
573 u32 fib_index = ~0;
574 u32 table_id = ~0;
575 fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
576 int rv = 0;
577
Florin Coras6c4dae22018-01-09 06:39:23 -0800578 reg = vl_api_client_index_to_registration (mp->client_index);
579 if (!reg)
Juraj Slobodadfc19232016-12-05 13:20:37 +0100580 return;
581
582 VALIDATE_SW_IF_INDEX (mp);
583
584 sw_if_index = ntohl (mp->sw_if_index);
585
586 if (mp->is_ipv6)
587 fib_proto = FIB_PROTOCOL_IP6;
588
589 fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
590 if (fib_index != ~0)
591 {
592 fib_table = fib_table_get (fib_index, fib_proto);
593 table_id = fib_table->ft_table_id;
594 }
595
596 BAD_SW_IF_INDEX_LABEL;
597
Florin Coras6c4dae22018-01-09 06:39:23 -0800598 send_sw_interface_get_table_reply (reg, mp->context, rv, table_id);
Juraj Slobodadfc19232016-12-05 13:20:37 +0100599}
600
Dave Barach6d963c22016-12-05 09:50:05 -0500601static void vl_api_sw_interface_set_unnumbered_t_handler
602 (vl_api_sw_interface_set_unnumbered_t * mp)
603{
604 vl_api_sw_interface_set_unnumbered_reply_t *rmp;
605 int rv = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500606 vnet_main_t *vnm = vnet_get_main ();
Eyal Bari1c82cd42017-03-14 14:39:51 +0200607 u32 sw_if_index = ntohl (mp->sw_if_index);
608 u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500609
610 /*
611 * The API message field names are backwards from
612 * the underlying data structure names.
613 * It's not worth changing them now.
614 */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200615 if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500616 {
617 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
618 goto done;
619 }
620
621 /* Only check the "use loop0" field when setting the binding */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200622 if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500623 {
624 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
625 goto done;
626 }
627
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700628 vnet_sw_interface_update_unnumbered (unnumbered_sw_if_index,
629 sw_if_index, mp->is_add);
Dave Barach6d963c22016-12-05 09:50:05 -0500630done:
631 REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
632}
633
634static void
635vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
636 mp)
637{
638 vl_api_sw_interface_clear_stats_reply_t *rmp;
639
640 vnet_main_t *vnm = vnet_get_main ();
641 vnet_interface_main_t *im = &vnm->interface_main;
642 vlib_simple_counter_main_t *sm;
643 vlib_combined_counter_main_t *cm;
644 static vnet_main_t **my_vnet_mains;
645 int i, j, n_counters;
646 int rv = 0;
647
648 if (mp->sw_if_index != ~0)
649 VALIDATE_SW_IF_INDEX (mp);
650
651 vec_reset_length (my_vnet_mains);
652
653 for (i = 0; i < vec_len (vnet_mains); i++)
654 {
655 if (vnet_mains[i])
656 vec_add1 (my_vnet_mains, vnet_mains[i]);
657 }
658
659 if (vec_len (vnet_mains) == 0)
660 vec_add1 (my_vnet_mains, vnm);
661
662 n_counters = vec_len (im->combined_sw_if_counters);
663
664 for (j = 0; j < n_counters; j++)
665 {
666 for (i = 0; i < vec_len (my_vnet_mains); i++)
667 {
668 im = &my_vnet_mains[i]->interface_main;
669 cm = im->combined_sw_if_counters + j;
670 if (mp->sw_if_index == (u32) ~ 0)
671 vlib_clear_combined_counters (cm);
672 else
673 vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
674 }
675 }
676
677 n_counters = vec_len (im->sw_if_counters);
678
679 for (j = 0; j < n_counters; j++)
680 {
681 for (i = 0; i < vec_len (my_vnet_mains); i++)
682 {
683 im = &my_vnet_mains[i]->interface_main;
684 sm = im->sw_if_counters + j;
685 if (mp->sw_if_index == (u32) ~ 0)
686 vlib_clear_simple_counters (sm);
687 else
688 vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
689 }
690 }
691
692 BAD_SW_IF_INDEX_LABEL;
693
694 REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
695}
696
697#define API_LINK_STATE_EVENT 1
698#define API_ADMIN_UP_DOWN_EVENT 2
699
700static int
701event_data_cmp (void *a1, void *a2)
702{
703 uword *e1 = a1;
704 uword *e2 = a2;
705
706 return (word) e1[0] - (word) e2[0];
707}
708
709static void
Neale Rannsa07bd702017-08-07 07:53:49 -0700710send_sw_interface_event (vpe_api_main_t * am,
Neale Rannsd292ab12017-08-15 12:29:48 -0700711 vpe_client_registration_t * reg,
Florin Coras6c4dae22018-01-09 06:39:23 -0800712 vl_api_registration_t * vl_reg,
713 vnet_sw_interface_t * swif)
Dave Barach6d963c22016-12-05 09:50:05 -0500714{
Neale Rannsa07bd702017-08-07 07:53:49 -0700715 vl_api_sw_interface_event_t *mp;
Dave Barach6d963c22016-12-05 09:50:05 -0500716 vnet_main_t *vnm = am->vnet_main;
717
718 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
719 swif->sw_if_index);
720 mp = vl_msg_api_alloc (sizeof (*mp));
721 memset (mp, 0, sizeof (*mp));
Neale Rannsa07bd702017-08-07 07:53:49 -0700722 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
Dave Barach6d963c22016-12-05 09:50:05 -0500723 mp->sw_if_index = ntohl (swif->sw_if_index);
Neale Rannsd292ab12017-08-15 12:29:48 -0700724 mp->client_index = reg->client_index;
725 mp->pid = reg->client_pid;
Dave Barach6d963c22016-12-05 09:50:05 -0500726
727 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
728 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
Florin Coras6c4dae22018-01-09 06:39:23 -0800729 vl_api_send_msg (vl_reg, (u8 *) mp);
Dave Barach6d963c22016-12-05 09:50:05 -0500730}
731
732static uword
733link_state_process (vlib_main_t * vm,
734 vlib_node_runtime_t * rt, vlib_frame_t * f)
735{
736 vpe_api_main_t *vam = &vpe_api_main;
737 vnet_main_t *vnm = vam->vnet_main;
738 vnet_sw_interface_t *swif;
739 uword *event_data = 0;
740 vpe_client_registration_t *reg;
741 int i;
742 u32 prev_sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -0800743 vl_api_registration_t *vl_reg;
Dave Barach6d963c22016-12-05 09:50:05 -0500744
745 vam->link_state_process_up = 1;
746
747 while (1)
748 {
749 vlib_process_wait_for_event (vm);
750
751 /* Unified list of changed link or admin state sw_if_indices */
752 vlib_process_get_events_with_type
753 (vm, &event_data, API_LINK_STATE_EVENT);
754 vlib_process_get_events_with_type
755 (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
756
757 /* Sort, so we can eliminate duplicates */
758 vec_sort_with_function (event_data, event_data_cmp);
759
760 prev_sw_if_index = ~0;
761
762 for (i = 0; i < vec_len (event_data); i++)
763 {
764 /* Only one message per swif */
765 if (prev_sw_if_index == event_data[i])
766 continue;
767 prev_sw_if_index = event_data[i];
768
769 /* *INDENT-OFF* */
770 pool_foreach(reg, vam->interface_events_registrations,
771 ({
Florin Coras6c4dae22018-01-09 06:39:23 -0800772 vl_reg = vl_api_client_index_to_registration (reg->client_index);
773 if (vl_reg)
Dave Barach6d963c22016-12-05 09:50:05 -0500774 {
775 /* sw_interface may be deleted already */
776 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
777 event_data[i]))
778 {
779 swif = vnet_get_sw_interface (vnm, event_data[i]);
Florin Coras6c4dae22018-01-09 06:39:23 -0800780 send_sw_interface_event (vam, reg, vl_reg, swif);
Dave Barach6d963c22016-12-05 09:50:05 -0500781 }
782 }
783 }));
784 /* *INDENT-ON* */
785 }
786 vec_reset_length (event_data);
787 }
788
789 return 0;
790}
791
792static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
793 u32 flags);
794static clib_error_t *admin_up_down_function (vnet_main_t * vm,
795 u32 hw_if_index, u32 flags);
796
797/* *INDENT-OFF* */
798VLIB_REGISTER_NODE (link_state_process_node,static) = {
799 .function = link_state_process,
800 .type = VLIB_NODE_TYPE_PROCESS,
801 .name = "vpe-link-state-process",
802};
803/* *INDENT-ON* */
804
805VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
806VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
807
808static clib_error_t *
809link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
810{
811 vpe_api_main_t *vam = &vpe_api_main;
812 vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
813
814 if (vam->link_state_process_up)
815 vlib_process_signal_event (vam->vlib_main,
816 link_state_process_node.index,
817 API_LINK_STATE_EVENT, hi->sw_if_index);
818 return 0;
819}
820
821static clib_error_t *
822admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
823{
824 vpe_api_main_t *vam = &vpe_api_main;
825
826 /*
827 * Note: it's perfectly fair to set a subif admin up / admin down.
828 * Note the subtle distinction between this routine and the previous
829 * routine.
830 */
831 if (vam->link_state_process_up)
832 vlib_process_signal_event (vam->vlib_main,
833 link_state_process_node.index,
834 API_ADMIN_UP_DOWN_EVENT, sw_if_index);
835 return 0;
836}
837
838static void vl_api_sw_interface_tag_add_del_t_handler
839 (vl_api_sw_interface_tag_add_del_t * mp)
840{
841 vnet_main_t *vnm = vnet_get_main ();
842 vl_api_sw_interface_tag_add_del_reply_t *rmp;
843 int rv = 0;
844 u8 *tag;
845 u32 sw_if_index = ntohl (mp->sw_if_index);
846
847 VALIDATE_SW_IF_INDEX (mp);
848
849 if (mp->is_add)
850 {
851 if (mp->tag[0] == 0)
852 {
853 rv = VNET_API_ERROR_INVALID_VALUE;
854 goto out;
855 }
856
857 mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
858 tag = format (0, "%s%c", mp->tag, 0);
859 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
860 }
861 else
862 vnet_clear_sw_interface_tag (vnm, sw_if_index);
863
864 BAD_SW_IF_INDEX_LABEL;
865out:
866 REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
867}
868
Jon Loeliger10c273b2017-03-30 08:39:33 -0500869static void vl_api_sw_interface_set_mac_address_t_handler
870 (vl_api_sw_interface_set_mac_address_t * mp)
871{
872 vl_api_sw_interface_set_mac_address_reply_t *rmp;
873 vnet_main_t *vnm = vnet_get_main ();
874 u32 sw_if_index = ntohl (mp->sw_if_index);
Neale Rannsd867a7c2017-10-04 02:29:07 -0700875 vnet_sw_interface_t *si;
Jon Loeliger10c273b2017-03-30 08:39:33 -0500876 clib_error_t *error;
877 int rv = 0;
878
879 VALIDATE_SW_IF_INDEX (mp);
880
Neale Rannsd867a7c2017-10-04 02:29:07 -0700881 si = vnet_get_sw_interface (vnm, sw_if_index);
John Lo62fcc0a2017-10-31 14:31:10 -0400882 error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index,
883 mp->mac_address);
Jon Loeliger10c273b2017-03-30 08:39:33 -0500884 if (error)
885 {
886 rv = VNET_API_ERROR_UNIMPLEMENTED;
887 clib_error_report (error);
888 goto out;
889 }
890
891 BAD_SW_IF_INDEX_LABEL;
892out:
893 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
894}
895
Juraj Slobodac0374232018-02-01 15:18:49 +0100896static void vl_api_sw_interface_get_mac_address_t_handler
897 (vl_api_sw_interface_get_mac_address_t * mp)
898{
899 vl_api_sw_interface_get_mac_address_reply_t *rmp;
900 vl_api_registration_t *reg;
901 vnet_main_t *vnm = vnet_get_main ();
902 u32 sw_if_index = ntohl (mp->sw_if_index);
903 vnet_sw_interface_t *si;
904 ethernet_interface_t *eth_if = 0;
905 int rv = 0;
906
907 VALIDATE_SW_IF_INDEX (mp);
908
909 si = vnet_get_sup_sw_interface (vnm, sw_if_index);
910 if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
911 eth_if = ethernet_get_interface (&ethernet_main, si->hw_if_index);
912
913 BAD_SW_IF_INDEX_LABEL;
914
915 reg = vl_api_client_index_to_registration (mp->client_index);
916 if (!reg)
917 return;
918 rmp = vl_msg_api_alloc (sizeof (*rmp));
919 rmp->_vl_msg_id = htons (VL_API_SW_INTERFACE_GET_MAC_ADDRESS_REPLY);
920 rmp->context = mp->context;
921 rmp->retval = htonl (rv);
922 if (!rv && eth_if)
923 memcpy (rmp->mac_address, eth_if->address, 6);
924 vl_api_send_msg (reg, (u8 *) rmp);
925}
926
Stevenad8015b2017-10-29 22:10:46 -0700927static void vl_api_sw_interface_set_rx_mode_t_handler
928 (vl_api_sw_interface_set_rx_mode_t * mp)
929{
930 vl_api_sw_interface_set_rx_mode_reply_t *rmp;
931 vnet_main_t *vnm = vnet_get_main ();
932 u32 sw_if_index = ntohl (mp->sw_if_index);
933 vnet_sw_interface_t *si;
934 clib_error_t *error;
935 int rv = 0;
936
937 VALIDATE_SW_IF_INDEX (mp);
938
939 si = vnet_get_sw_interface (vnm, sw_if_index);
Mohsin Kazmi54f7c512018-08-23 18:28:11 +0200940 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
941 {
942 rv = VNET_API_ERROR_INVALID_VALUE;
943 goto bad_sw_if_index;
944 }
945
Stevenad8015b2017-10-29 22:10:46 -0700946 error = set_hw_interface_change_rx_mode (vnm, si->hw_if_index,
947 mp->queue_id_valid,
948 ntohl (mp->queue_id), mp->mode);
949 if (error)
950 {
951 rv = VNET_API_ERROR_UNIMPLEMENTED;
952 clib_error_report (error);
953 goto out;
954 }
955
956 BAD_SW_IF_INDEX_LABEL;
957out:
958 REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_MODE_REPLY);
959}
960
Mohsin Kazmi54f7c512018-08-23 18:28:11 +0200961static void vl_api_sw_interface_set_rx_placement_t_handler
962 (vl_api_sw_interface_set_rx_placement_t * mp)
963{
964 vl_api_sw_interface_set_rx_placement_reply_t *rmp;
965 vnet_main_t *vnm = vnet_get_main ();
966 u32 sw_if_index = ntohl (mp->sw_if_index);
967 vnet_sw_interface_t *si;
968 clib_error_t *error = 0;
969 int rv = 0;
970
971 VALIDATE_SW_IF_INDEX (mp);
972
973 si = vnet_get_sw_interface (vnm, sw_if_index);
974 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
975 {
976 rv = VNET_API_ERROR_INVALID_VALUE;
977 goto bad_sw_if_index;
978 }
979
980 error = set_hw_interface_rx_placement (si->hw_if_index,
981 ntohl (mp->queue_id),
982 ntohl (mp->worker_id), mp->is_main);
983 if (error)
984 {
985 rv = VNET_API_ERROR_UNIMPLEMENTED;
986 clib_error_report (error);
987 goto out;
988 }
989
990 BAD_SW_IF_INDEX_LABEL;
991out:
992 REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_PLACEMENT_REPLY);
993}
994
Neale Rannsb8d44812017-11-10 06:53:54 -0800995static void
996vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
997{
998 vl_api_create_vlan_subif_reply_t *rmp;
999 vnet_main_t *vnm = vnet_get_main ();
1000 u32 sw_if_index = (u32) ~ 0;
1001 vnet_hw_interface_t *hi;
1002 int rv = 0;
1003 u32 id;
1004 vnet_sw_interface_t template;
1005 uword *p;
1006 vnet_interface_main_t *im = &vnm->interface_main;
1007 u64 sup_and_sub_key;
Florin Coras6c4dae22018-01-09 06:39:23 -08001008 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08001009 clib_error_t *error;
1010
1011 VALIDATE_SW_IF_INDEX (mp);
1012
1013 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1014
1015 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1016 {
1017 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1018 goto out;
1019 }
1020
1021 id = ntohl (mp->vlan_id);
1022 if (id == 0 || id > 4095)
1023 {
1024 rv = VNET_API_ERROR_INVALID_VLAN;
1025 goto out;
1026 }
1027
1028 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
1029
1030 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1031 if (p)
1032 {
1033 rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
1034 goto out;
1035 }
1036
1037 memset (&template, 0, sizeof (template));
1038 template.type = VNET_SW_INTERFACE_TYPE_SUB;
1039 template.sup_sw_if_index = hi->sw_if_index;
1040 template.sub.id = id;
1041 template.sub.eth.raw_flags = 0;
1042 template.sub.eth.flags.one_tag = 1;
1043 template.sub.eth.outer_vlan_id = id;
1044 template.sub.eth.flags.exact_match = 1;
1045
1046 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1047 if (error)
1048 {
1049 clib_error_report (error);
1050 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1051 goto out;
1052 }
1053
1054 u64 *kp = clib_mem_alloc (sizeof (*kp));
1055 *kp = sup_and_sub_key;
1056
1057 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
1058 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1059
1060 BAD_SW_IF_INDEX_LABEL;
1061
1062out:
Florin Coras6c4dae22018-01-09 06:39:23 -08001063 reg = vl_api_client_index_to_registration (mp->client_index);
1064 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08001065 return;
1066
1067 rmp = vl_msg_api_alloc (sizeof (*rmp));
1068 rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
1069 rmp->context = mp->context;
1070 rmp->retval = htonl (rv);
1071 rmp->sw_if_index = htonl (sw_if_index);
Florin Coras6c4dae22018-01-09 06:39:23 -08001072 vl_api_send_msg (reg, (u8 *) rmp);
Neale Rannsb8d44812017-11-10 06:53:54 -08001073}
1074
1075static void
1076vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
1077{
1078 vl_api_create_subif_reply_t *rmp;
1079 vnet_main_t *vnm = vnet_get_main ();
1080 u32 sw_if_index = ~0;
1081 int rv = 0;
1082 u32 sub_id;
1083 vnet_sw_interface_t *si;
1084 vnet_hw_interface_t *hi;
1085 vnet_sw_interface_t template;
1086 uword *p;
1087 vnet_interface_main_t *im = &vnm->interface_main;
1088 u64 sup_and_sub_key;
1089 clib_error_t *error;
1090
1091 VALIDATE_SW_IF_INDEX (mp);
1092
1093 si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
1094 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1095
1096 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1097 {
1098 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1099 goto out;
1100 }
1101
1102 sw_if_index = si->sw_if_index;
1103 sub_id = ntohl (mp->sub_id);
1104
1105 sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
1106
1107 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1108 if (p)
1109 {
1110 if (CLIB_DEBUG > 0)
1111 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
1112 sw_if_index, sub_id);
1113 rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
1114 goto out;
1115 }
1116
1117 memset (&template, 0, sizeof (template));
1118 template.type = VNET_SW_INTERFACE_TYPE_SUB;
1119 template.sup_sw_if_index = sw_if_index;
1120 template.sub.id = sub_id;
1121 template.sub.eth.flags.no_tags = mp->no_tags;
1122 template.sub.eth.flags.one_tag = mp->one_tag;
1123 template.sub.eth.flags.two_tags = mp->two_tags;
1124 template.sub.eth.flags.dot1ad = mp->dot1ad;
1125 template.sub.eth.flags.exact_match = mp->exact_match;
1126 template.sub.eth.flags.default_sub = mp->default_sub;
1127 template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
1128 template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
1129 template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
1130 template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
1131
1132 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1133 if (error)
1134 {
1135 clib_error_report (error);
1136 rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
1137 goto out;
1138 }
1139
1140 u64 *kp = clib_mem_alloc (sizeof (*kp));
1141 *kp = sup_and_sub_key;
1142
1143 hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
1144 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1145
1146 BAD_SW_IF_INDEX_LABEL;
1147
1148out:
1149
1150 /* *INDENT-OFF* */
1151 REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
1152 ({
1153 rmp->sw_if_index = ntohl(sw_if_index);
1154 }));
1155 /* *INDENT-ON* */
1156}
1157
1158static void
1159vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
1160{
1161 vl_api_delete_subif_reply_t *rmp;
1162 int rv;
1163
1164 rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
1165
1166 REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
1167}
1168
1169static void
1170vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
1171 mp)
1172{
1173 vl_api_interface_name_renumber_reply_t *rmp;
1174 int rv = 0;
1175
1176 VALIDATE_SW_IF_INDEX (mp);
1177
1178 rv = vnet_interface_name_renumber
1179 (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
1180
1181 BAD_SW_IF_INDEX_LABEL;
1182
1183 REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
1184}
1185
1186static void
1187vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
1188{
1189 vl_api_create_loopback_reply_t *rmp;
1190 u32 sw_if_index;
1191 int rv;
1192
1193 rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address, 0, 0);
1194
1195 /* *INDENT-OFF* */
1196 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
1197 ({
1198 rmp->sw_if_index = ntohl (sw_if_index);
1199 }));
1200 /* *INDENT-ON* */
1201}
1202
1203static void vl_api_create_loopback_instance_t_handler
1204 (vl_api_create_loopback_instance_t * mp)
1205{
1206 vl_api_create_loopback_instance_reply_t *rmp;
1207 u32 sw_if_index;
1208 u8 is_specified = mp->is_specified;
1209 u32 user_instance = ntohl (mp->user_instance);
1210 int rv;
1211
1212 rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address,
1213 is_specified, user_instance);
1214
1215 /* *INDENT-OFF* */
1216 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY,
1217 ({
1218 rmp->sw_if_index = ntohl (sw_if_index);
1219 }));
1220 /* *INDENT-ON* */
1221}
1222
1223static void
1224vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
1225{
1226 vl_api_delete_loopback_reply_t *rmp;
1227 u32 sw_if_index;
1228 int rv;
1229
1230 sw_if_index = ntohl (mp->sw_if_index);
1231 rv = vnet_delete_loopback_interface (sw_if_index);
1232
1233 REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
1234}
1235
Neale Ranns6f4a6be2018-03-16 16:26:21 -07001236static void
1237 vl_api_collect_detailed_interface_stats_t_handler
1238 (vl_api_collect_detailed_interface_stats_t * mp)
1239{
1240 vl_api_collect_detailed_interface_stats_reply_t *rmp;
1241 int rv = 0;
1242
Neale Ranns871dc422018-03-29 01:28:09 -07001243 rv =
1244 vnet_sw_interface_stats_collect_enable_disable (ntohl (mp->sw_if_index),
1245 mp->enable_disable);
Neale Ranns6f4a6be2018-03-16 16:26:21 -07001246
1247 REPLY_MACRO (VL_API_COLLECT_DETAILED_INTERFACE_STATS_REPLY);
1248}
1249
Dave Barachaff70772016-10-31 11:59:07 -04001250/*
1251 * vpe_api_hookup
1252 * Add vpe's API message handlers to the table.
1253 * vlib has alread mapped shared memory and
1254 * added the client registration handlers.
1255 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1256 */
1257#define vl_msg_name_crc_list
Dave Barachb5e8a772016-12-06 12:04:42 -05001258#include <vnet/interface.api.h>
Dave Barachaff70772016-10-31 11:59:07 -04001259#undef vl_msg_name_crc_list
1260
1261static void
1262setup_message_id_table (api_main_t * am)
1263{
1264#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1265 foreach_vl_msg_name_crc_interface;
1266#undef _
1267}
1268
Dave Barach6d963c22016-12-05 09:50:05 -05001269pub_sub_handler (interface_events, INTERFACE_EVENTS);
1270
Dave Barachaff70772016-10-31 11:59:07 -04001271static clib_error_t *
1272interface_api_hookup (vlib_main_t * vm)
1273{
1274 api_main_t *am = &api_main;
1275
1276#define _(N,n) \
1277 vl_msg_api_set_handlers(VL_API_##N, #n, \
1278 vl_api_##n##_t_handler, \
1279 vl_noop_handler, \
1280 vl_api_##n##_t_endian, \
1281 vl_api_##n##_t_print, \
1282 sizeof(vl_api_##n##_t), 1);
1283 foreach_vpe_api_msg;
1284#undef _
1285
1286 /*
1287 * Set up the (msg_name, crc, message-id) table
1288 */
1289 setup_message_id_table (am);
1290
1291 return 0;
1292}
1293
1294VLIB_API_INIT_FUNCTION (interface_api_hookup);
1295
1296/*
1297 * fd.io coding-style-patch-verification: ON
1298 *
1299 * Local Variables:
1300 * eval: (c-set-style "gnu")
1301 * End:
1302 */