blob: eb5b57346ce66d61a61b126a6f2491e0e147745e [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
Jakub Grajciare63325e2019-03-01 08:55:49 +010034#include <vlibapi/api_types.h>
35
36#include <vnet/ip/ip_types_api.h>
37#include <vnet/ethernet/ethernet_types_api.h>
38
Dave Barachaff70772016-10-31 11:59:07 -040039#define vl_typedefs /* define message structures */
40#include <vnet/vnet_all_api_h.h>
41#undef vl_typedefs
42
43#define vl_endianfun /* define message structures */
44#include <vnet/vnet_all_api_h.h>
45#undef vl_endianfun
46
47/* instantiate all the print functions we know about */
48#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
49#define vl_printfun
50#include <vnet/vnet_all_api_h.h>
51#undef vl_printfun
52
53#include <vlibapi/api_helper_macros.h>
Dave Barachf1586612017-03-30 09:33:01 -040054vpe_api_main_t vpe_api_main;
Dave Barachaff70772016-10-31 11:59:07 -040055
Dave Barach6d963c22016-12-05 09:50:05 -050056#define foreach_vpe_api_msg \
57_(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags) \
Ole Troand7231612018-06-07 10:17:57 +020058_(HW_INTERFACE_SET_MTU, hw_interface_set_mtu) \
Dave Barach6d963c22016-12-05 09:50:05 -050059_(SW_INTERFACE_SET_MTU, sw_interface_set_mtu) \
60_(WANT_INTERFACE_EVENTS, want_interface_events) \
61_(SW_INTERFACE_DUMP, sw_interface_dump) \
Dave Barach6d963c22016-12-05 09:50:05 -050062_(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address) \
Stevenad8015b2017-10-29 22:10:46 -070063_(SW_INTERFACE_SET_RX_MODE, sw_interface_set_rx_mode) \
Mohsin Kazmif0b42f42018-09-10 18:11:00 +020064_(SW_INTERFACE_RX_PLACEMENT_DUMP, sw_interface_rx_placement_dump) \
Mohsin Kazmi54f7c512018-08-23 18:28:11 +020065_(SW_INTERFACE_SET_RX_PLACEMENT, sw_interface_set_rx_placement) \
Dave Barach6d963c22016-12-05 09:50:05 -050066_(SW_INTERFACE_SET_TABLE, sw_interface_set_table) \
Juraj Slobodadfc19232016-12-05 13:20:37 +010067_(SW_INTERFACE_GET_TABLE, sw_interface_get_table) \
Dave Barach6d963c22016-12-05 09:50:05 -050068_(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered) \
69_(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats) \
Jon Loeliger10c273b2017-03-30 08:39:33 -050070_(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del) \
Neale Rannsb8d44812017-11-10 06:53:54 -080071_(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address) \
Juraj Slobodac0374232018-02-01 15:18:49 +010072_(SW_INTERFACE_GET_MAC_ADDRESS, sw_interface_get_mac_address) \
Neale Rannsb8d44812017-11-10 06:53:54 -080073_(CREATE_VLAN_SUBIF, create_vlan_subif) \
74_(CREATE_SUBIF, create_subif) \
75_(DELETE_SUBIF, delete_subif) \
76_(CREATE_LOOPBACK, create_loopback) \
77_(CREATE_LOOPBACK_INSTANCE, create_loopback_instance) \
78_(DELETE_LOOPBACK, delete_loopback) \
Neale Ranns6f4a6be2018-03-16 16:26:21 -070079_(INTERFACE_NAME_RENUMBER, interface_name_renumber) \
Neale Ranns1855b8e2018-07-11 10:31:26 -070080_(COLLECT_DETAILED_INTERFACE_STATS, collect_detailed_interface_stats) \
81_(SW_INTERFACE_SET_IP_DIRECTED_BROADCAST, \
82 sw_interface_set_ip_directed_broadcast)
Dave Barachaff70772016-10-31 11:59:07 -040083
84static void
85vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
86{
87 vl_api_sw_interface_set_flags_reply_t *rmp;
88 vnet_main_t *vnm = vnet_get_main ();
89 int rv = 0;
90 clib_error_t *error;
91 u16 flags;
92
93 VALIDATE_SW_IF_INDEX (mp);
94
Jakub Grajciare63325e2019-03-01 08:55:49 +010095 flags =
96 ((ntohl (mp->flags)) & IF_STATUS_API_FLAG_ADMIN_UP) ?
97 VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
Dave Barachaff70772016-10-31 11:59:07 -040098
99 error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
100 if (error)
101 {
102 rv = -1;
103 clib_error_report (error);
104 }
105
106 BAD_SW_IF_INDEX_LABEL;
107 REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
108}
109
Matus Fabiand162f3d2016-12-05 01:05:35 -0800110static void
Ole Troand7231612018-06-07 10:17:57 +0200111vl_api_hw_interface_set_mtu_t_handler (vl_api_hw_interface_set_mtu_t * mp)
Matus Fabiand162f3d2016-12-05 01:05:35 -0800112{
Ole Troand7231612018-06-07 10:17:57 +0200113 vl_api_hw_interface_set_mtu_reply_t *rmp;
Matus Fabiand162f3d2016-12-05 01:05:35 -0800114 vnet_main_t *vnm = vnet_get_main ();
Matus Fabiand162f3d2016-12-05 01:05:35 -0800115 u32 sw_if_index = ntohl (mp->sw_if_index);
116 u16 mtu = ntohs (mp->mtu);
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200117 ethernet_main_t *em = &ethernet_main;
Matus Fabiand162f3d2016-12-05 01:05:35 -0800118 int rv = 0;
119
120 VALIDATE_SW_IF_INDEX (mp);
121
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200122 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
123 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
124 {
125 rv = VNET_API_ERROR_INVALID_VALUE;
126 goto bad_sw_if_index;
127 }
128
129 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index);
130 ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index);
131
132 if (!eif)
133 {
134 rv = VNET_API_ERROR_FEATURE_DISABLED;
135 goto bad_sw_if_index;
136 }
137
138 if (mtu < hi->min_supported_packet_bytes)
139 {
140 rv = VNET_API_ERROR_INVALID_VALUE;
141 goto bad_sw_if_index;
142 }
143
144 if (mtu > hi->max_supported_packet_bytes)
145 {
146 rv = VNET_API_ERROR_INVALID_VALUE;
147 goto bad_sw_if_index;
148 }
149
150 vnet_hw_interface_set_mtu (vnm, si->hw_if_index, mtu);
Matus Fabiand162f3d2016-12-05 01:05:35 -0800151
152 BAD_SW_IF_INDEX_LABEL;
Ole Troand7231612018-06-07 10:17:57 +0200153 REPLY_MACRO (VL_API_HW_INTERFACE_SET_MTU_REPLY);
154}
155
156static void
157vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
158{
159 vl_api_sw_interface_set_mtu_reply_t *rmp;
160 vnet_main_t *vnm = vnet_get_main ();
161 u32 sw_if_index = ntohl (mp->sw_if_index);
162 int rv = 0;
163 int i;
164 u32 per_protocol_mtu[VNET_N_MTU];
165
166 VALIDATE_SW_IF_INDEX (mp);
167
168 for (i = 0; i < VNET_N_MTU; i++)
169 per_protocol_mtu[i] = ntohl (mp->mtu[i]);
170
171 vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, per_protocol_mtu);
172
173 BAD_SW_IF_INDEX_LABEL;
Matus Fabiand162f3d2016-12-05 01:05:35 -0800174 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
175}
176
Dave Barach6d963c22016-12-05 09:50:05 -0500177static void
Neale Ranns1855b8e2018-07-11 10:31:26 -0700178 vl_api_sw_interface_set_ip_directed_broadcast_t_handler
179 (vl_api_sw_interface_set_ip_directed_broadcast_t * mp)
180{
181 vl_api_sw_interface_set_ip_directed_broadcast_reply_t *rmp;
182 u32 sw_if_index = ntohl (mp->sw_if_index);
183 int rv = 0;
184
185 VALIDATE_SW_IF_INDEX (mp);
186
187 vnet_sw_interface_ip_directed_broadcast (vnet_get_main (),
188 sw_if_index, mp->enable);
189
190 BAD_SW_IF_INDEX_LABEL;
191 REPLY_MACRO (VL_API_SW_INTERFACE_SET_IP_DIRECTED_BROADCAST_REPLY);
192}
193
194static void
Dave Barach6d963c22016-12-05 09:50:05 -0500195send_sw_interface_details (vpe_api_main_t * am,
Dave Barach59b25652017-09-10 15:04:27 -0400196 vl_api_registration_t * rp,
Dave Barach6d963c22016-12-05 09:50:05 -0500197 vnet_sw_interface_t * swif,
198 u8 * interface_name, u32 context)
199{
Eyal Bari1c82cd42017-03-14 14:39:51 +0200200 vnet_hw_interface_t *hi =
201 vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500202
Jakub Grajciare63325e2019-03-01 08:55:49 +0100203 uint32_t if_name_len = strlen ((char *) interface_name);
204 u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index);
205 uint32_t tag_len = 0;
206 if (tag != NULL)
207 tag_len = strlen ((char *) tag);
208 vl_api_sw_interface_details_t *mp =
209 vl_msg_api_alloc (sizeof (*mp) + if_name_len + tag_len);
210 clib_memset (mp, 0, sizeof (*mp) + if_name_len + tag_len);
Dave Barach6d963c22016-12-05 09:50:05 -0500211 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
212 mp->sw_if_index = ntohl (swif->sw_if_index);
213 mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
Jakub Grajciare63325e2019-03-01 08:55:49 +0100214
215 mp->flags |= (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
216 IF_STATUS_API_FLAG_ADMIN_UP : 0;
217 mp->flags |= (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
218 IF_STATUS_API_FLAG_LINK_UP : 0;
219 mp->flags = ntohl (mp->flags);
220
Dave Barach6d963c22016-12-05 09:50:05 -0500221 mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
222 VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
Ole Troan8ee72512018-11-20 11:03:10 +0100223 mp->link_speed = ntohl (hi->link_speed);
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200224 mp->link_mtu = ntohs (hi->max_packet_bytes);
Ole Troand7231612018-06-07 10:17:57 +0200225 mp->mtu[VNET_MTU_L3] = ntohl (swif->mtu[VNET_MTU_L3]);
226 mp->mtu[VNET_MTU_IP4] = ntohl (swif->mtu[VNET_MTU_IP4]);
227 mp->mtu[VNET_MTU_IP6] = ntohl (swif->mtu[VNET_MTU_IP6]);
228 mp->mtu[VNET_MTU_MPLS] = ntohl (swif->mtu[VNET_MTU_MPLS]);
229
Dave Barach6d963c22016-12-05 09:50:05 -0500230 mp->context = context;
231
Dave Barach6d963c22016-12-05 09:50:05 -0500232 /* Send the L2 address for ethernet physical intfcs */
233 if (swif->sup_sw_if_index == swif->sw_if_index
234 && hi->hw_class_index == ethernet_hw_interface_class.index)
235 {
236 ethernet_main_t *em = ethernet_get_main (am->vlib_main);
237 ethernet_interface_t *ei;
238
239 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
240 ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
Jakub Grajciare63325e2019-03-01 08:55:49 +0100241 mac_address_encode ((mac_address_t *) ei->address, mp->l2_address);
Dave Barach6d963c22016-12-05 09:50:05 -0500242 }
243 else if (swif->sup_sw_if_index != swif->sw_if_index)
244 {
245 vnet_sub_interface_t *sub = &swif->sub;
246 mp->sub_id = ntohl (sub->id);
Dave Barach6d963c22016-12-05 09:50:05 -0500247 mp->sub_number_of_tags =
248 sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
249 mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
250 mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
Jakub Grajciare63325e2019-03-01 08:55:49 +0100251 mp->sub_if_flags =
252 ntohl (sub->eth.raw_flags & SUB_IF_API_FLAG_MASK_VNET);
Dave Barach6d963c22016-12-05 09:50:05 -0500253
254 /* vlan tag rewrite data */
255 u32 vtr_op = L2_VTR_DISABLED;
256 u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
257
258 if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
259 &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
260 {
261 // error - default to disabled
262 mp->vtr_op = ntohl (L2_VTR_DISABLED);
263 clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
264 swif->sw_if_index);
265 }
266 else
267 {
268 mp->vtr_op = ntohl (vtr_op);
269 mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
270 mp->vtr_tag1 = ntohl (vtr_tag1);
271 mp->vtr_tag2 = ntohl (vtr_tag2);
272 }
273 }
274
Pavel Kotucek65e84572017-01-16 17:01:56 +0100275 /* pbb tag rewrite data */
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100276 ethernet_header_t eth_hdr;
Pavel Kotucek65e84572017-01-16 17:01:56 +0100277 u32 vtr_op = L2_VTR_DISABLED;
278 u16 outer_tag = 0;
Pavel Kotucek65e84572017-01-16 17:01:56 +0100279 u16 b_vlanid = 0;
280 u32 i_sid = 0;
Dave Barachb7b92992018-10-17 10:38:51 -0400281 clib_memset (&eth_hdr, 0, sizeof (eth_hdr));
Pavel Kotucek65e84572017-01-16 17:01:56 +0100282
283 if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100284 &vtr_op, &outer_tag, &eth_hdr, &b_vlanid, &i_sid))
Pavel Kotucek65e84572017-01-16 17:01:56 +0100285 {
Jakub Grajciare63325e2019-03-01 08:55:49 +0100286 mp->sub_if_flags |= ntohl (SUB_IF_API_FLAG_DOT1AH);
287 mac_address_encode ((mac_address_t *) eth_hdr.dst_address, mp->b_dmac);
288 mac_address_encode ((mac_address_t *) eth_hdr.src_address, mp->b_smac);
Pavel Kotucek65e84572017-01-16 17:01:56 +0100289 mp->b_vlanid = b_vlanid;
290 mp->i_sid = i_sid;
291 }
292
Jakub Grajciare63325e2019-03-01 08:55:49 +0100293 char *p = (char *) &mp->interface_name;
294 p +=
295 vl_api_to_api_string (if_name_len, (char *) interface_name,
296 (vl_api_string_t *) p);
297 if (tag != NULL)
298 vl_api_to_api_string (tag_len, (char *) tag, (vl_api_string_t *) p);
Dave Barach6d963c22016-12-05 09:50:05 -0500299
Florin Corase86a8ed2018-01-05 03:20:25 -0800300 vl_api_send_msg (rp, (u8 *) mp);
Dave Barach6d963c22016-12-05 09:50:05 -0500301}
302
303static void
304vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
305{
306 vpe_api_main_t *am = &vpe_api_main;
307 vnet_sw_interface_t *swif;
308 vnet_interface_main_t *im = &am->vnet_main->interface_main;
Dave Barach59b25652017-09-10 15:04:27 -0400309 vl_api_registration_t *rp;
Dave Barach6d963c22016-12-05 09:50:05 -0500310
Dave Barach59b25652017-09-10 15:04:27 -0400311 rp = vl_api_client_index_to_registration (mp->client_index);
312
313 if (rp == 0)
314 {
315 clib_warning ("Client %d AWOL", mp->client_index);
316 return;
317 }
Dave Barach6d963c22016-12-05 09:50:05 -0500318
Eyal Bari1c82cd42017-03-14 14:39:51 +0200319 u8 *filter = 0, *name = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500320 if (mp->name_filter_valid)
321 {
Jakub Grajciare63325e2019-03-01 08:55:49 +0100322 filter =
323 format (0, "%s%c", vl_api_from_api_string_c (&mp->name_filter), 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500324 }
325
Eyal Bari1c82cd42017-03-14 14:39:51 +0200326 char *strcasestr (char *, char *); /* lnx hdr file botch */
Dave Barach6d963c22016-12-05 09:50:05 -0500327 /* *INDENT-OFF* */
328 pool_foreach (swif, im->sw_interfaces,
329 ({
Eyal Bari1c82cd42017-03-14 14:39:51 +0200330 if (!vnet_swif_is_api_visible (swif))
331 continue;
332 vec_reset_length(name);
333 name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
334 swif, 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500335
Eyal Bari1c82cd42017-03-14 14:39:51 +0200336 if (filter && !strcasestr((char *) name, (char *) filter))
337 continue;
Dave Barach6d963c22016-12-05 09:50:05 -0500338
Dave Barach59b25652017-09-10 15:04:27 -0400339 send_sw_interface_details (am, rp, swif, name, mp->context);
Dave Barach6d963c22016-12-05 09:50:05 -0500340 }));
341 /* *INDENT-ON* */
342
Eyal Bari1c82cd42017-03-14 14:39:51 +0200343 vec_free (name);
344 vec_free (filter);
Dave Barach6d963c22016-12-05 09:50:05 -0500345}
346
347static void
348 vl_api_sw_interface_add_del_address_t_handler
349 (vl_api_sw_interface_add_del_address_t * mp)
350{
351 vlib_main_t *vm = vlib_get_main ();
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500352 vnet_main_t *vnm = vnet_get_main ();
Dave Barach6d963c22016-12-05 09:50:05 -0500353 vl_api_sw_interface_add_del_address_reply_t *rmp;
354 int rv = 0;
355 u32 is_del;
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500356 clib_error_t *error = 0;
Jakub Grajciare63325e2019-03-01 08:55:49 +0100357 ip46_address_t address;
Dave Barach6d963c22016-12-05 09:50:05 -0500358
359 VALIDATE_SW_IF_INDEX (mp);
360
361 is_del = mp->is_add == 0;
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500362 vnm->api_errno = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500363
364 if (mp->del_all)
365 ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
Jakub Grajciare63325e2019-03-01 08:55:49 +0100366 else if (ip_address_decode (&mp->prefix.address, &address) == IP46_TYPE_IP6)
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500367 error = ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
Jakub Grajciare63325e2019-03-01 08:55:49 +0100368 (void *) &address.ip6,
369 mp->prefix.address_length, is_del);
Dave Barach6d963c22016-12-05 09:50:05 -0500370 else
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500371 error = ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
Jakub Grajciare63325e2019-03-01 08:55:49 +0100372 (void *) &address.ip4,
373 mp->prefix.address_length, is_del);
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500374
375 if (error)
376 {
377 rv = vnm->api_errno;
378 clib_error_report (error);
379 goto done;
380 }
Dave Barach6d963c22016-12-05 09:50:05 -0500381
382 BAD_SW_IF_INDEX_LABEL;
383
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500384done:
Dave Barach6d963c22016-12-05 09:50:05 -0500385 REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
386}
387
388void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
389void
390stats_dslock_with_hint (int hint, int tag)
391{
392}
393
394void stats_dsunlock (void) __attribute__ ((weak));
395void
396stats_dsunlock (void)
397{
398}
399
400static void
401vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
402{
Dave Barach6d963c22016-12-05 09:50:05 -0500403 vl_api_sw_interface_set_table_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700404 u32 sw_if_index = ntohl (mp->sw_if_index);
405 u32 table_id = ntohl (mp->vrf_id);
406 int rv = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500407
408 VALIDATE_SW_IF_INDEX (mp);
409
410 stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
411
412 if (mp->is_ipv6)
Neale Ranns15002542017-09-10 04:39:11 -0700413 rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
Dave Barach6d963c22016-12-05 09:50:05 -0500414 else
Neale Ranns15002542017-09-10 04:39:11 -0700415 rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
Dave Barach6d963c22016-12-05 09:50:05 -0500416
Dave Barach6d963c22016-12-05 09:50:05 -0500417 stats_dsunlock ();
418
419 BAD_SW_IF_INDEX_LABEL;
420
421 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
422}
423
Neale Ranns15002542017-09-10 04:39:11 -0700424int
425ip_table_bind (fib_protocol_t fproto,
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700426 u32 sw_if_index, u32 table_id, u8 is_api)
Neale Ranns15002542017-09-10 04:39:11 -0700427{
428 CLIB_UNUSED (ip_interface_address_t * ia);
429 u32 fib_index, mfib_index;
430 fib_source_t src;
431 mfib_source_t msrc;
432
433 if (is_api)
434 {
435 src = FIB_SOURCE_API;
436 msrc = MFIB_SOURCE_API;
437 }
438 else
439 {
440 src = FIB_SOURCE_CLI;
441 msrc = MFIB_SOURCE_CLI;
442 }
443
Florin Corasd0a59722017-10-15 17:41:21 +0000444 /*
Florin Corasd0a59722017-10-15 17:41:21 +0000445 * This if table does not exist = error is what we want in the end.
446 */
Neale Ranns8f6dd322018-05-17 06:34:24 -0700447 fib_index = fib_table_find (fproto, table_id);
448 mfib_index = mfib_table_find (fproto, table_id);
Florin Corasd0a59722017-10-15 17:41:21 +0000449
Neale Ranns8f6dd322018-05-17 06:34:24 -0700450 if (~0 == fib_index || ~0 == mfib_index)
451 {
452 return (VNET_API_ERROR_NO_SUCH_FIB);
453 }
Florin Corasd0a59722017-10-15 17:41:21 +0000454
Neale Ranns15002542017-09-10 04:39:11 -0700455 if (FIB_PROTOCOL_IP6 == fproto)
456 {
457 /*
458 * If the interface already has in IP address, then a change int
459 * VRF is not allowed. The IP address applied must first be removed.
460 * We do not do that automatically here, since VPP has no knowledge
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700461 * of whether those subnets are valid in the destination VRF.
Neale Ranns15002542017-09-10 04:39:11 -0700462 */
463 /* *INDENT-OFF* */
464 foreach_ip_interface_address (&ip6_main.lookup_main,
465 ia, sw_if_index,
466 1 /* honor unnumbered */ ,
467 ({
468 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
469 }));
470 /* *INDENT-ON* */
471
472 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
473 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
474
475 /*
476 * tell those that are interested that the binding is changing.
477 */
478 ip6_table_bind_callback_t *cb;
479 vec_foreach (cb, ip6_main.table_bind_callbacks)
480 cb->function (&ip6_main, cb->function_opaque,
481 sw_if_index,
482 fib_index,
483 ip6_main.fib_index_by_sw_if_index[sw_if_index]);
484
485 if (0 == table_id)
486 {
487 /* reset back to default */
488 if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
489 fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
490 FIB_PROTOCOL_IP6, src);
491 if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
492 mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
493 [sw_if_index], FIB_PROTOCOL_IP6, msrc);
494
495 }
496 else
497 {
498 /* we need to lock the table now it's inuse */
499 fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
500 mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
501 }
502
503 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
504 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
505 }
506 else
507 {
508 /*
509 * If the interface already has in IP address, then a change int
510 * VRF is not allowed. The IP address applied must first be removed.
511 * We do not do that automatically here, since VPP has no knowledge
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700512 * of whether those subnets are valid in the destination VRF.
Neale Ranns15002542017-09-10 04:39:11 -0700513 */
514 /* *INDENT-OFF* */
515 foreach_ip_interface_address (&ip4_main.lookup_main,
516 ia, sw_if_index,
517 1 /* honor unnumbered */ ,
518 ({
519 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
520 }));
521 /* *INDENT-ON* */
522
523 vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
524 vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
525
526 /*
527 * tell those that are interested that the binding is changing.
528 */
529 ip4_table_bind_callback_t *cb;
530 vec_foreach (cb, ip4_main.table_bind_callbacks)
531 cb->function (&ip4_main, cb->function_opaque,
532 sw_if_index,
533 fib_index,
534 ip4_main.fib_index_by_sw_if_index[sw_if_index]);
535
536 if (0 == table_id)
537 {
538 /* reset back to default */
539 if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
540 fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
541 FIB_PROTOCOL_IP4, src);
542 if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
543 mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
544 [sw_if_index], FIB_PROTOCOL_IP4, msrc);
545
546 }
547 else
548 {
549 /* we need to lock the table now it's inuse */
550 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
551 table_id, src);
552
553 mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
554 table_id, msrc);
555 }
556
557 ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
558 ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
559 }
560
Neale Ranns15002542017-09-10 04:39:11 -0700561 return (0);
562}
563
Juraj Slobodadfc19232016-12-05 13:20:37 +0100564static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800565send_sw_interface_get_table_reply (vl_api_registration_t * reg,
Juraj Slobodadfc19232016-12-05 13:20:37 +0100566 u32 context, int retval, u32 vrf_id)
567{
568 vl_api_sw_interface_get_table_reply_t *mp;
569
570 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400571 clib_memset (mp, 0, sizeof (*mp));
Juraj Slobodadfc19232016-12-05 13:20:37 +0100572 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
573 mp->context = context;
574 mp->retval = htonl (retval);
575 mp->vrf_id = htonl (vrf_id);
576
Florin Coras6c4dae22018-01-09 06:39:23 -0800577 vl_api_send_msg (reg, (u8 *) mp);
Juraj Slobodadfc19232016-12-05 13:20:37 +0100578}
579
580static void
581vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
582{
Florin Coras6c4dae22018-01-09 06:39:23 -0800583 vl_api_registration_t *reg;
Juraj Slobodadfc19232016-12-05 13:20:37 +0100584 fib_table_t *fib_table = 0;
585 u32 sw_if_index = ~0;
586 u32 fib_index = ~0;
587 u32 table_id = ~0;
588 fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
589 int rv = 0;
590
Florin Coras6c4dae22018-01-09 06:39:23 -0800591 reg = vl_api_client_index_to_registration (mp->client_index);
592 if (!reg)
Juraj Slobodadfc19232016-12-05 13:20:37 +0100593 return;
594
595 VALIDATE_SW_IF_INDEX (mp);
596
597 sw_if_index = ntohl (mp->sw_if_index);
598
599 if (mp->is_ipv6)
600 fib_proto = FIB_PROTOCOL_IP6;
601
602 fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
603 if (fib_index != ~0)
604 {
605 fib_table = fib_table_get (fib_index, fib_proto);
606 table_id = fib_table->ft_table_id;
607 }
608
609 BAD_SW_IF_INDEX_LABEL;
610
Florin Coras6c4dae22018-01-09 06:39:23 -0800611 send_sw_interface_get_table_reply (reg, mp->context, rv, table_id);
Juraj Slobodadfc19232016-12-05 13:20:37 +0100612}
613
Dave Barach6d963c22016-12-05 09:50:05 -0500614static void vl_api_sw_interface_set_unnumbered_t_handler
615 (vl_api_sw_interface_set_unnumbered_t * mp)
616{
617 vl_api_sw_interface_set_unnumbered_reply_t *rmp;
618 int rv = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500619 vnet_main_t *vnm = vnet_get_main ();
Eyal Bari1c82cd42017-03-14 14:39:51 +0200620 u32 sw_if_index = ntohl (mp->sw_if_index);
621 u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500622
623 /*
624 * The API message field names are backwards from
625 * the underlying data structure names.
626 * It's not worth changing them now.
627 */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200628 if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500629 {
630 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
631 goto done;
632 }
633
634 /* Only check the "use loop0" field when setting the binding */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200635 if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500636 {
637 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
638 goto done;
639 }
640
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700641 vnet_sw_interface_update_unnumbered (unnumbered_sw_if_index,
642 sw_if_index, mp->is_add);
Dave Barach6d963c22016-12-05 09:50:05 -0500643done:
644 REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
645}
646
647static void
648vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
649 mp)
650{
651 vl_api_sw_interface_clear_stats_reply_t *rmp;
652
653 vnet_main_t *vnm = vnet_get_main ();
654 vnet_interface_main_t *im = &vnm->interface_main;
655 vlib_simple_counter_main_t *sm;
656 vlib_combined_counter_main_t *cm;
657 static vnet_main_t **my_vnet_mains;
658 int i, j, n_counters;
659 int rv = 0;
660
661 if (mp->sw_if_index != ~0)
662 VALIDATE_SW_IF_INDEX (mp);
663
664 vec_reset_length (my_vnet_mains);
665
666 for (i = 0; i < vec_len (vnet_mains); i++)
667 {
668 if (vnet_mains[i])
669 vec_add1 (my_vnet_mains, vnet_mains[i]);
670 }
671
672 if (vec_len (vnet_mains) == 0)
673 vec_add1 (my_vnet_mains, vnm);
674
675 n_counters = vec_len (im->combined_sw_if_counters);
676
677 for (j = 0; j < n_counters; j++)
678 {
679 for (i = 0; i < vec_len (my_vnet_mains); i++)
680 {
681 im = &my_vnet_mains[i]->interface_main;
682 cm = im->combined_sw_if_counters + j;
683 if (mp->sw_if_index == (u32) ~ 0)
684 vlib_clear_combined_counters (cm);
685 else
686 vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
687 }
688 }
689
690 n_counters = vec_len (im->sw_if_counters);
691
692 for (j = 0; j < n_counters; j++)
693 {
694 for (i = 0; i < vec_len (my_vnet_mains); i++)
695 {
696 im = &my_vnet_mains[i]->interface_main;
697 sm = im->sw_if_counters + j;
698 if (mp->sw_if_index == (u32) ~ 0)
699 vlib_clear_simple_counters (sm);
700 else
701 vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
702 }
703 }
704
705 BAD_SW_IF_INDEX_LABEL;
706
707 REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
708}
709
710#define API_LINK_STATE_EVENT 1
711#define API_ADMIN_UP_DOWN_EVENT 2
712
713static int
714event_data_cmp (void *a1, void *a2)
715{
716 uword *e1 = a1;
717 uword *e2 = a2;
718
719 return (word) e1[0] - (word) e2[0];
720}
721
722static void
Neale Rannsa07bd702017-08-07 07:53:49 -0700723send_sw_interface_event (vpe_api_main_t * am,
Neale Rannsd292ab12017-08-15 12:29:48 -0700724 vpe_client_registration_t * reg,
Florin Coras6c4dae22018-01-09 06:39:23 -0800725 vl_api_registration_t * vl_reg,
726 vnet_sw_interface_t * swif)
Dave Barach6d963c22016-12-05 09:50:05 -0500727{
Neale Rannsa07bd702017-08-07 07:53:49 -0700728 vl_api_sw_interface_event_t *mp;
Dave Barach6d963c22016-12-05 09:50:05 -0500729 vnet_main_t *vnm = am->vnet_main;
730
731 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
732 swif->sw_if_index);
733 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400734 clib_memset (mp, 0, sizeof (*mp));
Neale Rannsa07bd702017-08-07 07:53:49 -0700735 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
Dave Barach6d963c22016-12-05 09:50:05 -0500736 mp->sw_if_index = ntohl (swif->sw_if_index);
Neale Rannsd292ab12017-08-15 12:29:48 -0700737 mp->client_index = reg->client_index;
738 mp->pid = reg->client_pid;
Dave Barach6d963c22016-12-05 09:50:05 -0500739
Jakub Grajciare63325e2019-03-01 08:55:49 +0100740 mp->flags = 0;
741 mp->flags |= (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
742 IF_STATUS_API_FLAG_ADMIN_UP : 0;
743 mp->flags |= (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
744 IF_STATUS_API_FLAG_LINK_UP : 0;
745 mp->flags = ntohl (mp->flags);
746
Florin Coras6c4dae22018-01-09 06:39:23 -0800747 vl_api_send_msg (vl_reg, (u8 *) mp);
Dave Barach6d963c22016-12-05 09:50:05 -0500748}
749
750static uword
751link_state_process (vlib_main_t * vm,
752 vlib_node_runtime_t * rt, vlib_frame_t * f)
753{
754 vpe_api_main_t *vam = &vpe_api_main;
755 vnet_main_t *vnm = vam->vnet_main;
756 vnet_sw_interface_t *swif;
757 uword *event_data = 0;
758 vpe_client_registration_t *reg;
759 int i;
760 u32 prev_sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -0800761 vl_api_registration_t *vl_reg;
Dave Barach6d963c22016-12-05 09:50:05 -0500762
763 vam->link_state_process_up = 1;
764
765 while (1)
766 {
767 vlib_process_wait_for_event (vm);
768
769 /* Unified list of changed link or admin state sw_if_indices */
770 vlib_process_get_events_with_type
771 (vm, &event_data, API_LINK_STATE_EVENT);
772 vlib_process_get_events_with_type
773 (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
774
775 /* Sort, so we can eliminate duplicates */
776 vec_sort_with_function (event_data, event_data_cmp);
777
778 prev_sw_if_index = ~0;
779
780 for (i = 0; i < vec_len (event_data); i++)
781 {
782 /* Only one message per swif */
783 if (prev_sw_if_index == event_data[i])
784 continue;
785 prev_sw_if_index = event_data[i];
786
787 /* *INDENT-OFF* */
788 pool_foreach(reg, vam->interface_events_registrations,
789 ({
Florin Coras6c4dae22018-01-09 06:39:23 -0800790 vl_reg = vl_api_client_index_to_registration (reg->client_index);
791 if (vl_reg)
Dave Barach6d963c22016-12-05 09:50:05 -0500792 {
793 /* sw_interface may be deleted already */
794 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
795 event_data[i]))
796 {
797 swif = vnet_get_sw_interface (vnm, event_data[i]);
Florin Coras6c4dae22018-01-09 06:39:23 -0800798 send_sw_interface_event (vam, reg, vl_reg, swif);
Dave Barach6d963c22016-12-05 09:50:05 -0500799 }
800 }
801 }));
802 /* *INDENT-ON* */
803 }
804 vec_reset_length (event_data);
805 }
806
807 return 0;
808}
809
810static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
811 u32 flags);
812static clib_error_t *admin_up_down_function (vnet_main_t * vm,
813 u32 hw_if_index, u32 flags);
814
815/* *INDENT-OFF* */
816VLIB_REGISTER_NODE (link_state_process_node,static) = {
817 .function = link_state_process,
818 .type = VLIB_NODE_TYPE_PROCESS,
819 .name = "vpe-link-state-process",
820};
821/* *INDENT-ON* */
822
823VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
824VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
825
826static clib_error_t *
827link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
828{
829 vpe_api_main_t *vam = &vpe_api_main;
830 vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
831
832 if (vam->link_state_process_up)
833 vlib_process_signal_event (vam->vlib_main,
834 link_state_process_node.index,
835 API_LINK_STATE_EVENT, hi->sw_if_index);
836 return 0;
837}
838
839static clib_error_t *
840admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
841{
842 vpe_api_main_t *vam = &vpe_api_main;
843
844 /*
845 * Note: it's perfectly fair to set a subif admin up / admin down.
846 * Note the subtle distinction between this routine and the previous
847 * routine.
848 */
849 if (vam->link_state_process_up)
850 vlib_process_signal_event (vam->vlib_main,
851 link_state_process_node.index,
852 API_ADMIN_UP_DOWN_EVENT, sw_if_index);
853 return 0;
854}
855
856static void vl_api_sw_interface_tag_add_del_t_handler
857 (vl_api_sw_interface_tag_add_del_t * mp)
858{
859 vnet_main_t *vnm = vnet_get_main ();
860 vl_api_sw_interface_tag_add_del_reply_t *rmp;
861 int rv = 0;
862 u8 *tag;
863 u32 sw_if_index = ntohl (mp->sw_if_index);
864
865 VALIDATE_SW_IF_INDEX (mp);
866
867 if (mp->is_add)
868 {
Jakub Grajciare63325e2019-03-01 08:55:49 +0100869 if (vl_api_from_api_string_c (&mp->tag)[0] == 0)
Dave Barach6d963c22016-12-05 09:50:05 -0500870 {
871 rv = VNET_API_ERROR_INVALID_VALUE;
872 goto out;
873 }
874
Jakub Grajciare63325e2019-03-01 08:55:49 +0100875 tag = format (0, "%s%c", vl_api_from_api_string_c (&mp->tag), 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500876 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
877 }
878 else
879 vnet_clear_sw_interface_tag (vnm, sw_if_index);
880
881 BAD_SW_IF_INDEX_LABEL;
882out:
883 REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
884}
885
Jon Loeliger10c273b2017-03-30 08:39:33 -0500886static void vl_api_sw_interface_set_mac_address_t_handler
887 (vl_api_sw_interface_set_mac_address_t * mp)
888{
889 vl_api_sw_interface_set_mac_address_reply_t *rmp;
890 vnet_main_t *vnm = vnet_get_main ();
891 u32 sw_if_index = ntohl (mp->sw_if_index);
Neale Rannsd867a7c2017-10-04 02:29:07 -0700892 vnet_sw_interface_t *si;
Jon Loeliger10c273b2017-03-30 08:39:33 -0500893 clib_error_t *error;
894 int rv = 0;
Jakub Grajciare63325e2019-03-01 08:55:49 +0100895 mac_address_t mac;
Jon Loeliger10c273b2017-03-30 08:39:33 -0500896
897 VALIDATE_SW_IF_INDEX (mp);
898
Neale Rannsd867a7c2017-10-04 02:29:07 -0700899 si = vnet_get_sw_interface (vnm, sw_if_index);
Jakub Grajciare63325e2019-03-01 08:55:49 +0100900 mac_address_decode (mp->mac_address, &mac);
901 error =
902 vnet_hw_interface_change_mac_address (vnm, si->hw_if_index, (u8 *) & mac);
Jon Loeliger10c273b2017-03-30 08:39:33 -0500903 if (error)
904 {
905 rv = VNET_API_ERROR_UNIMPLEMENTED;
906 clib_error_report (error);
907 goto out;
908 }
909
910 BAD_SW_IF_INDEX_LABEL;
911out:
912 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
913}
914
Juraj Slobodac0374232018-02-01 15:18:49 +0100915static void vl_api_sw_interface_get_mac_address_t_handler
916 (vl_api_sw_interface_get_mac_address_t * mp)
917{
918 vl_api_sw_interface_get_mac_address_reply_t *rmp;
919 vl_api_registration_t *reg;
920 vnet_main_t *vnm = vnet_get_main ();
921 u32 sw_if_index = ntohl (mp->sw_if_index);
922 vnet_sw_interface_t *si;
923 ethernet_interface_t *eth_if = 0;
924 int rv = 0;
925
926 VALIDATE_SW_IF_INDEX (mp);
927
928 si = vnet_get_sup_sw_interface (vnm, sw_if_index);
929 if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
930 eth_if = ethernet_get_interface (&ethernet_main, si->hw_if_index);
931
932 BAD_SW_IF_INDEX_LABEL;
933
934 reg = vl_api_client_index_to_registration (mp->client_index);
935 if (!reg)
936 return;
937 rmp = vl_msg_api_alloc (sizeof (*rmp));
938 rmp->_vl_msg_id = htons (VL_API_SW_INTERFACE_GET_MAC_ADDRESS_REPLY);
939 rmp->context = mp->context;
940 rmp->retval = htonl (rv);
941 if (!rv && eth_if)
Jakub Grajciare63325e2019-03-01 08:55:49 +0100942 mac_address_encode ((mac_address_t *) eth_if->address, rmp->mac_address);
Juraj Slobodac0374232018-02-01 15:18:49 +0100943 vl_api_send_msg (reg, (u8 *) rmp);
944}
945
Stevenad8015b2017-10-29 22:10:46 -0700946static void vl_api_sw_interface_set_rx_mode_t_handler
947 (vl_api_sw_interface_set_rx_mode_t * mp)
948{
949 vl_api_sw_interface_set_rx_mode_reply_t *rmp;
950 vnet_main_t *vnm = vnet_get_main ();
951 u32 sw_if_index = ntohl (mp->sw_if_index);
952 vnet_sw_interface_t *si;
953 clib_error_t *error;
954 int rv = 0;
955
956 VALIDATE_SW_IF_INDEX (mp);
957
958 si = vnet_get_sw_interface (vnm, sw_if_index);
Mohsin Kazmi54f7c512018-08-23 18:28:11 +0200959 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
960 {
961 rv = VNET_API_ERROR_INVALID_VALUE;
962 goto bad_sw_if_index;
963 }
964
Stevenad8015b2017-10-29 22:10:46 -0700965 error = set_hw_interface_change_rx_mode (vnm, si->hw_if_index,
966 mp->queue_id_valid,
967 ntohl (mp->queue_id), mp->mode);
968 if (error)
969 {
970 rv = VNET_API_ERROR_UNIMPLEMENTED;
971 clib_error_report (error);
972 goto out;
973 }
974
975 BAD_SW_IF_INDEX_LABEL;
976out:
977 REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_MODE_REPLY);
978}
979
Mohsin Kazmif0b42f42018-09-10 18:11:00 +0200980static void
981send_interface_rx_placement_details (vpe_api_main_t * am,
982 vl_api_registration_t * rp,
983 u32 sw_if_index, u32 worker_id,
984 u32 queue_id, u8 mode, u32 context)
985{
986 vl_api_sw_interface_rx_placement_details_t *mp;
987 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400988 clib_memset (mp, 0, sizeof (*mp));
Mohsin Kazmif0b42f42018-09-10 18:11:00 +0200989
990 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_RX_PLACEMENT_DETAILS);
991 mp->sw_if_index = htonl (sw_if_index);
992 mp->queue_id = htonl (queue_id);
993 mp->worker_id = htonl (worker_id);
994 mp->mode = mode;
995 mp->context = context;
996
997 vl_api_send_msg (rp, (u8 *) mp);
998}
999
1000static void vl_api_sw_interface_rx_placement_dump_t_handler
1001 (vl_api_sw_interface_rx_placement_dump_t * mp)
1002{
1003 vnet_main_t *vnm = vnet_get_main ();
1004 vpe_api_main_t *am = &vpe_api_main;
1005 u32 sw_if_index = ntohl (mp->sw_if_index);
1006 vl_api_registration_t *reg;
1007
1008 reg = vl_api_client_index_to_registration (mp->client_index);
1009 if (!reg)
1010 return;
1011
1012 if (sw_if_index == ~0)
1013 {
1014 vnet_device_input_runtime_t *rt;
1015 vnet_device_and_queue_t *dq;
1016 vlib_node_t *pn = vlib_get_node_by_name (am->vlib_main,
1017 (u8 *) "device-input");
1018 uword si;
1019 int index = 0;
1020
1021 /* *INDENT-OFF* */
1022 foreach_vlib_main (({
1023 clib_bitmap_foreach (si, pn->sibling_bitmap,
1024 ({
1025 rt = vlib_node_get_runtime_data (this_vlib_main, si);
1026 vec_foreach (dq, rt->devices_and_queues)
1027 {
1028 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm,
1029 dq->hw_if_index);
1030 send_interface_rx_placement_details (am, reg, hw->sw_if_index, index,
1031 dq->queue_id, dq->mode, mp->context);
1032 }
1033 }));
1034 index++;
1035 }));
1036 /* *INDENT-ON* */
1037 }
1038 else
1039 {
1040 int i;
1041 vnet_sw_interface_t *si;
1042
1043 if (!vnet_sw_if_index_is_api_valid (sw_if_index))
1044 {
1045 clib_warning ("sw_if_index %u does not exist", sw_if_index);
1046 goto bad_sw_if_index;
1047 }
1048
1049 si = vnet_get_sw_interface (vnm, sw_if_index);
1050 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
1051 {
1052 clib_warning ("interface type is not HARDWARE! P2P, PIPE and SUB"
1053 " interfaces are not supported");
1054 goto bad_sw_if_index;
1055 }
1056
1057 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, si->hw_if_index);
1058
1059 for (i = 0; i < vec_len (hw->dq_runtime_index_by_queue); i++)
1060 {
1061 send_interface_rx_placement_details (am, reg, hw->sw_if_index,
1062 hw->input_node_thread_index_by_queue
1063 [i], i,
1064 hw->rx_mode_by_queue[i],
1065 mp->context);
1066 }
1067 }
1068
1069 BAD_SW_IF_INDEX_LABEL;
1070}
1071
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001072static void vl_api_sw_interface_set_rx_placement_t_handler
1073 (vl_api_sw_interface_set_rx_placement_t * mp)
1074{
1075 vl_api_sw_interface_set_rx_placement_reply_t *rmp;
1076 vnet_main_t *vnm = vnet_get_main ();
1077 u32 sw_if_index = ntohl (mp->sw_if_index);
1078 vnet_sw_interface_t *si;
1079 clib_error_t *error = 0;
1080 int rv = 0;
1081
1082 VALIDATE_SW_IF_INDEX (mp);
1083
1084 si = vnet_get_sw_interface (vnm, sw_if_index);
1085 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
1086 {
1087 rv = VNET_API_ERROR_INVALID_VALUE;
1088 goto bad_sw_if_index;
1089 }
1090
1091 error = set_hw_interface_rx_placement (si->hw_if_index,
1092 ntohl (mp->queue_id),
1093 ntohl (mp->worker_id), mp->is_main);
1094 if (error)
1095 {
1096 rv = VNET_API_ERROR_UNIMPLEMENTED;
1097 clib_error_report (error);
1098 goto out;
1099 }
1100
1101 BAD_SW_IF_INDEX_LABEL;
1102out:
1103 REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_PLACEMENT_REPLY);
1104}
1105
Neale Rannsb8d44812017-11-10 06:53:54 -08001106static void
1107vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
1108{
1109 vl_api_create_vlan_subif_reply_t *rmp;
1110 vnet_main_t *vnm = vnet_get_main ();
1111 u32 sw_if_index = (u32) ~ 0;
1112 vnet_hw_interface_t *hi;
1113 int rv = 0;
1114 u32 id;
1115 vnet_sw_interface_t template;
1116 uword *p;
1117 vnet_interface_main_t *im = &vnm->interface_main;
1118 u64 sup_and_sub_key;
Florin Coras6c4dae22018-01-09 06:39:23 -08001119 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08001120 clib_error_t *error;
1121
1122 VALIDATE_SW_IF_INDEX (mp);
1123
1124 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1125
1126 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1127 {
1128 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1129 goto out;
1130 }
1131
1132 id = ntohl (mp->vlan_id);
1133 if (id == 0 || id > 4095)
1134 {
1135 rv = VNET_API_ERROR_INVALID_VLAN;
1136 goto out;
1137 }
1138
1139 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
1140
1141 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1142 if (p)
1143 {
1144 rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
1145 goto out;
1146 }
1147
Dave Barachb7b92992018-10-17 10:38:51 -04001148 clib_memset (&template, 0, sizeof (template));
Neale Rannsb8d44812017-11-10 06:53:54 -08001149 template.type = VNET_SW_INTERFACE_TYPE_SUB;
Joe Zhou715f94e2019-03-06 23:05:32 -08001150 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
Neale Rannsb8d44812017-11-10 06:53:54 -08001151 template.sup_sw_if_index = hi->sw_if_index;
1152 template.sub.id = id;
1153 template.sub.eth.raw_flags = 0;
1154 template.sub.eth.flags.one_tag = 1;
1155 template.sub.eth.outer_vlan_id = id;
1156 template.sub.eth.flags.exact_match = 1;
1157
1158 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1159 if (error)
1160 {
1161 clib_error_report (error);
1162 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1163 goto out;
1164 }
1165
1166 u64 *kp = clib_mem_alloc (sizeof (*kp));
1167 *kp = sup_and_sub_key;
1168
1169 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
1170 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1171
1172 BAD_SW_IF_INDEX_LABEL;
1173
1174out:
Florin Coras6c4dae22018-01-09 06:39:23 -08001175 reg = vl_api_client_index_to_registration (mp->client_index);
1176 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08001177 return;
1178
1179 rmp = vl_msg_api_alloc (sizeof (*rmp));
1180 rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
1181 rmp->context = mp->context;
1182 rmp->retval = htonl (rv);
1183 rmp->sw_if_index = htonl (sw_if_index);
Florin Coras6c4dae22018-01-09 06:39:23 -08001184 vl_api_send_msg (reg, (u8 *) rmp);
Neale Rannsb8d44812017-11-10 06:53:54 -08001185}
1186
1187static void
1188vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
1189{
1190 vl_api_create_subif_reply_t *rmp;
1191 vnet_main_t *vnm = vnet_get_main ();
1192 u32 sw_if_index = ~0;
1193 int rv = 0;
1194 u32 sub_id;
1195 vnet_sw_interface_t *si;
1196 vnet_hw_interface_t *hi;
1197 vnet_sw_interface_t template;
1198 uword *p;
1199 vnet_interface_main_t *im = &vnm->interface_main;
1200 u64 sup_and_sub_key;
1201 clib_error_t *error;
1202
1203 VALIDATE_SW_IF_INDEX (mp);
1204
1205 si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
1206 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1207
1208 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1209 {
1210 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1211 goto out;
1212 }
1213
1214 sw_if_index = si->sw_if_index;
1215 sub_id = ntohl (mp->sub_id);
1216
1217 sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
1218
1219 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1220 if (p)
1221 {
1222 if (CLIB_DEBUG > 0)
1223 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
1224 sw_if_index, sub_id);
1225 rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
1226 goto out;
1227 }
1228
Dave Barachb7b92992018-10-17 10:38:51 -04001229 clib_memset (&template, 0, sizeof (template));
Neale Rannsb8d44812017-11-10 06:53:54 -08001230 template.type = VNET_SW_INTERFACE_TYPE_SUB;
Joe Zhou715f94e2019-03-06 23:05:32 -08001231 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
Neale Rannsb8d44812017-11-10 06:53:54 -08001232 template.sup_sw_if_index = sw_if_index;
1233 template.sub.id = sub_id;
Jakub Grajciare63325e2019-03-01 08:55:49 +01001234 template.sub.eth.raw_flags = ntohl (mp->sub_if_flags);
Neale Rannsb8d44812017-11-10 06:53:54 -08001235 template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
1236 template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
1237
1238 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1239 if (error)
1240 {
1241 clib_error_report (error);
1242 rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
1243 goto out;
1244 }
1245
1246 u64 *kp = clib_mem_alloc (sizeof (*kp));
1247 *kp = sup_and_sub_key;
1248
1249 hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
1250 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1251
1252 BAD_SW_IF_INDEX_LABEL;
1253
1254out:
1255
1256 /* *INDENT-OFF* */
1257 REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
1258 ({
1259 rmp->sw_if_index = ntohl(sw_if_index);
1260 }));
1261 /* *INDENT-ON* */
1262}
1263
1264static void
1265vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
1266{
1267 vl_api_delete_subif_reply_t *rmp;
1268 int rv;
1269
1270 rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
1271
1272 REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
1273}
1274
1275static void
1276vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
1277 mp)
1278{
1279 vl_api_interface_name_renumber_reply_t *rmp;
1280 int rv = 0;
1281
1282 VALIDATE_SW_IF_INDEX (mp);
1283
1284 rv = vnet_interface_name_renumber
1285 (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
1286
1287 BAD_SW_IF_INDEX_LABEL;
1288
1289 REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
1290}
1291
1292static void
1293vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
1294{
1295 vl_api_create_loopback_reply_t *rmp;
1296 u32 sw_if_index;
1297 int rv;
Jakub Grajciare63325e2019-03-01 08:55:49 +01001298 mac_address_t mac;
Neale Rannsb8d44812017-11-10 06:53:54 -08001299
Jakub Grajciare63325e2019-03-01 08:55:49 +01001300 mac_address_decode (mp->mac_address, &mac);
1301 rv = vnet_create_loopback_interface (&sw_if_index, (u8 *) & mac, 0, 0);
Neale Rannsb8d44812017-11-10 06:53:54 -08001302
1303 /* *INDENT-OFF* */
1304 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
1305 ({
1306 rmp->sw_if_index = ntohl (sw_if_index);
1307 }));
1308 /* *INDENT-ON* */
1309}
1310
1311static void vl_api_create_loopback_instance_t_handler
1312 (vl_api_create_loopback_instance_t * mp)
1313{
1314 vl_api_create_loopback_instance_reply_t *rmp;
1315 u32 sw_if_index;
1316 u8 is_specified = mp->is_specified;
1317 u32 user_instance = ntohl (mp->user_instance);
1318 int rv;
Jakub Grajciare63325e2019-03-01 08:55:49 +01001319 mac_address_t mac;
Neale Rannsb8d44812017-11-10 06:53:54 -08001320
Jakub Grajciare63325e2019-03-01 08:55:49 +01001321 mac_address_decode (mp->mac_address, &mac);
1322 rv = vnet_create_loopback_interface (&sw_if_index, (u8 *) & mac,
Neale Rannsb8d44812017-11-10 06:53:54 -08001323 is_specified, user_instance);
1324
1325 /* *INDENT-OFF* */
1326 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY,
1327 ({
1328 rmp->sw_if_index = ntohl (sw_if_index);
1329 }));
1330 /* *INDENT-ON* */
1331}
1332
1333static void
1334vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
1335{
1336 vl_api_delete_loopback_reply_t *rmp;
1337 u32 sw_if_index;
1338 int rv;
1339
1340 sw_if_index = ntohl (mp->sw_if_index);
1341 rv = vnet_delete_loopback_interface (sw_if_index);
1342
1343 REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
1344}
1345
Neale Ranns6f4a6be2018-03-16 16:26:21 -07001346static void
1347 vl_api_collect_detailed_interface_stats_t_handler
1348 (vl_api_collect_detailed_interface_stats_t * mp)
1349{
1350 vl_api_collect_detailed_interface_stats_reply_t *rmp;
1351 int rv = 0;
1352
Neale Ranns871dc422018-03-29 01:28:09 -07001353 rv =
1354 vnet_sw_interface_stats_collect_enable_disable (ntohl (mp->sw_if_index),
1355 mp->enable_disable);
Neale Ranns6f4a6be2018-03-16 16:26:21 -07001356
1357 REPLY_MACRO (VL_API_COLLECT_DETAILED_INTERFACE_STATS_REPLY);
1358}
1359
Dave Barachaff70772016-10-31 11:59:07 -04001360/*
1361 * vpe_api_hookup
1362 * Add vpe's API message handlers to the table.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -07001363 * vlib has already mapped shared memory and
Dave Barachaff70772016-10-31 11:59:07 -04001364 * added the client registration handlers.
1365 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1366 */
1367#define vl_msg_name_crc_list
Dave Barachb5e8a772016-12-06 12:04:42 -05001368#include <vnet/interface.api.h>
Dave Barachaff70772016-10-31 11:59:07 -04001369#undef vl_msg_name_crc_list
1370
1371static void
1372setup_message_id_table (api_main_t * am)
1373{
1374#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1375 foreach_vl_msg_name_crc_interface;
1376#undef _
1377}
1378
Dave Barach6d963c22016-12-05 09:50:05 -05001379pub_sub_handler (interface_events, INTERFACE_EVENTS);
1380
Dave Barachaff70772016-10-31 11:59:07 -04001381static clib_error_t *
1382interface_api_hookup (vlib_main_t * vm)
1383{
1384 api_main_t *am = &api_main;
1385
1386#define _(N,n) \
1387 vl_msg_api_set_handlers(VL_API_##N, #n, \
1388 vl_api_##n##_t_handler, \
1389 vl_noop_handler, \
1390 vl_api_##n##_t_endian, \
1391 vl_api_##n##_t_print, \
1392 sizeof(vl_api_##n##_t), 1);
1393 foreach_vpe_api_msg;
1394#undef _
1395
1396 /*
1397 * Set up the (msg_name, crc, message-id) table
1398 */
1399 setup_message_id_table (am);
1400
1401 return 0;
1402}
1403
1404VLIB_API_INIT_FUNCTION (interface_api_hookup);
1405
1406/*
1407 * fd.io coding-style-patch-verification: ON
1408 *
1409 * Local Variables:
1410 * eval: (c-set-style "gnu")
1411 * End:
1412 */